blob: f33882f1cd529c18e75cc5a3bd3a851bff3900bd [file] [log] [blame]
Thomas Gleixner40b0b3f2019-06-03 07:44:46 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/lib/cmdline.c
4 * Helper functions generally used for parsing kernel command line
5 * and module options.
6 *
7 * Code and copyrights come from init/main.c and arch/i386/kernel/setup.c.
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * GNU Indent formatting options for this file: -kr -i8 -npsl -pcs
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/string.h>
Baoquan Hef51b17c2017-04-17 21:34:56 +080015#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Derek Fults22f2e282006-12-06 20:37:11 -080017/*
18 * If a hyphen was found in get_option, this will handle the
19 * range of numbers, M-N. This will expand the range and insert
20 * the values[M, M+1, ..., N] into the ints array in get_options.
21 */
22
Ilya Matveychikova91e0f62017-06-23 15:08:49 -070023static int get_range(char **str, int *pint, int n)
Derek Fults22f2e282006-12-06 20:37:11 -080024{
25 int x, inc_counter, upper_range;
26
27 (*str)++;
28 upper_range = simple_strtol((*str), NULL, 0);
29 inc_counter = upper_range - *pint;
Ilya Matveychikova91e0f62017-06-23 15:08:49 -070030 for (x = *pint; n && x < upper_range; x++, n--)
Derek Fults22f2e282006-12-06 20:37:11 -080031 *pint++ = x;
32 return inc_counter;
33}
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/**
36 * get_option - Parse integer from an option string
37 * @str: option string
Andy Shevchenko6b2b6b82020-12-15 20:43:30 -080038 * @pint: (optional output) integer value parsed from @str
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 *
40 * Read an int from an option string; if available accept a subsequent
41 * comma as well.
42 *
Andy Shevchenko6b2b6b82020-12-15 20:43:30 -080043 * When @pint is NULL the function can be used as a validator of
44 * the current option in the string.
45 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * Return values:
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080047 * 0 - no int in string
48 * 1 - int found, no subsequent comma
49 * 2 - int found including a subsequent comma
50 * 3 - hyphen found to denote a range
Andy Shevchenkoe2918512020-12-15 20:43:27 -080051 *
52 * Leading hyphen without integer is no integer case, but we consume it
53 * for the sake of simplification.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
55
Felipe Contreras9fd43052014-01-23 15:54:35 -080056int get_option(char **str, int *pint)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 char *cur = *str;
Andy Shevchenko6b2b6b82020-12-15 20:43:30 -080059 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 if (!cur || !(*cur))
62 return 0;
Andy Shevchenkoe2918512020-12-15 20:43:27 -080063 if (*cur == '-')
Andy Shevchenko6b2b6b82020-12-15 20:43:30 -080064 value = -simple_strtoull(++cur, str, 0);
Andy Shevchenkoe2918512020-12-15 20:43:27 -080065 else
Andy Shevchenko6b2b6b82020-12-15 20:43:30 -080066 value = simple_strtoull(cur, str, 0);
67 if (pint)
68 *pint = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (cur == *str)
70 return 0;
71 if (**str == ',') {
72 (*str)++;
73 return 2;
74 }
Derek Fults22f2e282006-12-06 20:37:11 -080075 if (**str == '-')
76 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 return 1;
79}
Felipe Contrerasff6f9bb2014-01-23 15:54:36 -080080EXPORT_SYMBOL(get_option);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/**
83 * get_options - Parse a string into a list of integers
84 * @str: String to be parsed
85 * @nints: size of integer array
Andy Shevchenkof1f405c2021-01-22 14:38:49 +020086 * @ints: integer array (must have room for at least one element)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *
88 * This function parses a string containing a comma-separated
Derek Fults22f2e282006-12-06 20:37:11 -080089 * list of integers, a hyphen-separated range of _positive_ integers,
90 * or a combination of both. The parse halts when the array is
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * full, or when no more numbers can be retrieved from the
92 * string.
93 *
Andy Shevchenkof1f405c2021-01-22 14:38:49 +020094 * Returns:
95 *
96 * The first element is filled by the number of collected integers
97 * in the range. The rest is what was parsed from the @str.
98 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * Return value is the character in the string which caused
100 * the parse to end (typically a null terminator, if @str is
101 * completely parseable).
102 */
Felipe Contreras9fd43052014-01-23 15:54:35 -0800103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104char *get_options(const char *str, int nints, int *ints)
105{
106 int res, i = 1;
107
108 while (i < nints) {
Felipe Contreras9fd43052014-01-23 15:54:35 -0800109 res = get_option((char **)&str, ints + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (res == 0)
111 break;
Derek Fults22f2e282006-12-06 20:37:11 -0800112 if (res == 3) {
113 int range_nums;
Ilya Matveychikova91e0f62017-06-23 15:08:49 -0700114 range_nums = get_range((char **)&str, ints + i, nints - i);
Derek Fults22f2e282006-12-06 20:37:11 -0800115 if (range_nums < 0)
116 break;
117 /*
118 * Decrement the result by one to leave out the
119 * last number in the range. The next iteration
120 * will handle the upper number in the range
121 */
122 i += (range_nums - 1);
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 i++;
125 if (res == 1)
126 break;
127 }
128 ints[0] = i - 1;
129 return (char *)str;
130}
Felipe Contrerasff6f9bb2014-01-23 15:54:36 -0800131EXPORT_SYMBOL(get_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/**
134 * memparse - parse a string with mem suffixes into a number
135 * @ptr: Where parse begins
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700136 * @retptr: (output) Optional pointer to next char after parse completes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
138 * Parses a string into a number. The number stored at @ptr is
Gui Hechenge004f3c2014-08-06 16:09:29 -0700139 * potentially suffixed with K, M, G, T, P, E.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
141
Jeremy Fitzhardinged974ae32008-07-24 16:27:46 -0700142unsigned long long memparse(const char *ptr, char **retptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700144 char *endptr; /* local pointer to end of parsed string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700146 unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
147
148 switch (*endptr) {
Gui Hechenge004f3c2014-08-06 16:09:29 -0700149 case 'E':
150 case 'e':
151 ret <<= 10;
Nick Desaulniers4c1ca832020-11-15 20:35:31 -0800152 fallthrough;
Gui Hechenge004f3c2014-08-06 16:09:29 -0700153 case 'P':
154 case 'p':
155 ret <<= 10;
Nick Desaulniers4c1ca832020-11-15 20:35:31 -0800156 fallthrough;
Gui Hechenge004f3c2014-08-06 16:09:29 -0700157 case 'T':
158 case 't':
159 ret <<= 10;
Nick Desaulniers4c1ca832020-11-15 20:35:31 -0800160 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 case 'G':
162 case 'g':
163 ret <<= 10;
Nick Desaulniers4c1ca832020-11-15 20:35:31 -0800164 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 case 'M':
166 case 'm':
167 ret <<= 10;
Nick Desaulniers4c1ca832020-11-15 20:35:31 -0800168 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 case 'K':
170 case 'k':
171 ret <<= 10;
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700172 endptr++;
Gustavo A. R. Silva36f9ff92020-11-19 07:11:44 -0600173 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 default:
175 break;
176 }
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700177
178 if (retptr)
179 *retptr = endptr;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return ret;
182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183EXPORT_SYMBOL(memparse);
Dave Young6ccc72b82014-08-14 17:15:27 +0800184
185/**
186 * parse_option_str - Parse a string and check an option is set or not
187 * @str: String to be parsed
188 * @option: option name
189 *
190 * This function parses a string containing a comma-separated list of
191 * strings like a=b,c.
192 *
193 * Return true if there's such option in the string, or return false.
194 */
195bool parse_option_str(const char *str, const char *option)
196{
197 while (*str) {
198 if (!strncmp(str, option, strlen(option))) {
199 str += strlen(option);
200 if (!*str || *str == ',')
201 return true;
202 }
203
204 while (*str && *str != ',')
205 str++;
206
207 if (*str == ',')
208 str++;
209 }
210
211 return false;
212}
Baoquan Hef51b17c2017-04-17 21:34:56 +0800213
214/*
215 * Parse a string to get a param value pair.
216 * You can use " around spaces, but can't escape ".
217 * Hyphens and underscores equivalent in parameter names.
218 */
219char *next_arg(char *args, char **param, char **val)
220{
221 unsigned int i, equals = 0;
222 int in_quote = 0, quoted = 0;
223 char *next;
224
225 if (*args == '"') {
226 args++;
227 in_quote = 1;
228 quoted = 1;
229 }
230
231 for (i = 0; args[i]; i++) {
232 if (isspace(args[i]) && !in_quote)
233 break;
234 if (equals == 0) {
235 if (args[i] == '=')
236 equals = i;
237 }
238 if (args[i] == '"')
239 in_quote = !in_quote;
240 }
241
242 *param = args;
243 if (!equals)
244 *val = NULL;
245 else {
246 args[equals] = '\0';
247 *val = args + equals + 1;
248
249 /* Don't include quotes in value. */
250 if (**val == '"') {
251 (*val)++;
252 if (args[i-1] == '"')
253 args[i-1] = '\0';
254 }
255 }
256 if (quoted && args[i-1] == '"')
257 args[i-1] = '\0';
258
259 if (args[i]) {
260 args[i] = '\0';
261 next = args + i + 1;
262 } else
263 next = args + i;
264
265 /* Chew up trailing spaces. */
266 return skip_spaces(next);
Baoquan Hef51b17c2017-04-17 21:34:56 +0800267}