2 * Jailhouse, a Linux-based partitioning hypervisor
4 * Copyright (c) Siemens AG, 2016
7 * Jan Kiszka <jan.kiszka@siemens.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
15 extern const char cmdline[];
17 static bool get_param(const char *param, char *value_buffer,
18 unsigned long buffer_size)
20 unsigned long param_len = strlen(param);
21 const char *p = cmdline;
24 /* read over leading blanks */
31 if (strncmp(p, param, param_len) == 0) {
34 /* check for boolean parameter */
35 if ((buffer_size == 0 && (*p == ' ' || *p == 0)))
38 /* extract parameter value */
41 while (buffer_size > 1) {
42 if (*p == ' ' || *p == 0)
44 *value_buffer++ = *p++;
53 /* search for end of this parameter */
62 const char *cmdline_parse_str(const char *param, char *value_buffer,
63 unsigned long buffer_size,
64 const char *default_value)
66 if (get_param(param, value_buffer, buffer_size))
72 long long cmdline_parse_int(const char *param, long long default_value)
74 char value_buffer[32];
75 char *p = value_buffer;
76 bool negative = false;
79 if (!get_param(param, value_buffer, sizeof(value_buffer)))
82 if (strncmp(p, "0x", 2) == 0) {
85 if (*p >= '0' && *p <= '9')
86 value = (value << 4) + *p - '0';
87 else if (*p >= 'A' && *p <= 'F')
88 value = (value << 4) + *p - 'A';
89 else if (*p >= 'a' && *p <= 'f')
90 value = (value << 4) + *p - 'a';
96 if (*p == '-' || *p == '+')
97 negative = (*p++ == '-');
100 if (*p >= '0' && *p <= '9')
101 value = (value * 10) + *p - '0';
103 return default_value;
108 return negative ? -value : value;
111 bool cmdline_parse_bool(const char *param)
113 return get_param(param, NULL, 0);