blob: d11fdc57917099b93cf87823531e395f6661dbc8 [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
38 * @pint: (output) integer value parsed from @str
39 *
40 * Read an int from an option string; if available accept a subsequent
41 * comma as well.
42 *
43 * Return values:
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080044 * 0 - no int in string
45 * 1 - int found, no subsequent comma
46 * 2 - int found including a subsequent comma
47 * 3 - hyphen found to denote a range
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Felipe Contreras9fd43052014-01-23 15:54:35 -080050int get_option(char **str, int *pint)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 char *cur = *str;
53
54 if (!cur || !(*cur))
55 return 0;
Felipe Contreras9fd43052014-01-23 15:54:35 -080056 *pint = simple_strtol(cur, str, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (cur == *str)
58 return 0;
59 if (**str == ',') {
60 (*str)++;
61 return 2;
62 }
Derek Fults22f2e282006-12-06 20:37:11 -080063 if (**str == '-')
64 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 return 1;
67}
Felipe Contrerasff6f9bb2014-01-23 15:54:36 -080068EXPORT_SYMBOL(get_option);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/**
71 * get_options - Parse a string into a list of integers
72 * @str: String to be parsed
73 * @nints: size of integer array
74 * @ints: integer array
75 *
76 * This function parses a string containing a comma-separated
Derek Fults22f2e282006-12-06 20:37:11 -080077 * list of integers, a hyphen-separated range of _positive_ integers,
78 * or a combination of both. The parse halts when the array is
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * full, or when no more numbers can be retrieved from the
80 * string.
81 *
82 * Return value is the character in the string which caused
83 * the parse to end (typically a null terminator, if @str is
84 * completely parseable).
85 */
Felipe Contreras9fd43052014-01-23 15:54:35 -080086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087char *get_options(const char *str, int nints, int *ints)
88{
89 int res, i = 1;
90
91 while (i < nints) {
Felipe Contreras9fd43052014-01-23 15:54:35 -080092 res = get_option((char **)&str, ints + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (res == 0)
94 break;
Derek Fults22f2e282006-12-06 20:37:11 -080095 if (res == 3) {
96 int range_nums;
Ilya Matveychikova91e0f62017-06-23 15:08:49 -070097 range_nums = get_range((char **)&str, ints + i, nints - i);
Derek Fults22f2e282006-12-06 20:37:11 -080098 if (range_nums < 0)
99 break;
100 /*
101 * Decrement the result by one to leave out the
102 * last number in the range. The next iteration
103 * will handle the upper number in the range
104 */
105 i += (range_nums - 1);
106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 i++;
108 if (res == 1)
109 break;
110 }
111 ints[0] = i - 1;
112 return (char *)str;
113}
Felipe Contrerasff6f9bb2014-01-23 15:54:36 -0800114EXPORT_SYMBOL(get_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/**
117 * memparse - parse a string with mem suffixes into a number
118 * @ptr: Where parse begins
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700119 * @retptr: (output) Optional pointer to next char after parse completes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 *
121 * Parses a string into a number. The number stored at @ptr is
Gui Hechenge004f3c2014-08-06 16:09:29 -0700122 * potentially suffixed with K, M, G, T, P, E.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
124
Jeremy Fitzhardinged974ae32008-07-24 16:27:46 -0700125unsigned long long memparse(const char *ptr, char **retptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700127 char *endptr; /* local pointer to end of parsed string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700129 unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
130
131 switch (*endptr) {
Gui Hechenge004f3c2014-08-06 16:09:29 -0700132 case 'E':
133 case 'e':
134 ret <<= 10;
Nick Desaulniers4c1ca832020-11-15 20:35:31 -0800135 fallthrough;
Gui Hechenge004f3c2014-08-06 16:09:29 -0700136 case 'P':
137 case 'p':
138 ret <<= 10;
Nick Desaulniers4c1ca832020-11-15 20:35:31 -0800139 fallthrough;
Gui Hechenge004f3c2014-08-06 16:09:29 -0700140 case 'T':
141 case 't':
142 ret <<= 10;
Nick Desaulniers4c1ca832020-11-15 20:35:31 -0800143 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 case 'G':
145 case 'g':
146 ret <<= 10;
Nick Desaulniers4c1ca832020-11-15 20:35:31 -0800147 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 case 'M':
149 case 'm':
150 ret <<= 10;
Nick Desaulniers4c1ca832020-11-15 20:35:31 -0800151 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 case 'K':
153 case 'k':
154 ret <<= 10;
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700155 endptr++;
Gustavo A. R. Silva36f9ff92020-11-19 07:11:44 -0600156 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 default:
158 break;
159 }
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700160
161 if (retptr)
162 *retptr = endptr;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return ret;
165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166EXPORT_SYMBOL(memparse);
Dave Young6ccc72b82014-08-14 17:15:27 +0800167
168/**
169 * parse_option_str - Parse a string and check an option is set or not
170 * @str: String to be parsed
171 * @option: option name
172 *
173 * This function parses a string containing a comma-separated list of
174 * strings like a=b,c.
175 *
176 * Return true if there's such option in the string, or return false.
177 */
178bool parse_option_str(const char *str, const char *option)
179{
180 while (*str) {
181 if (!strncmp(str, option, strlen(option))) {
182 str += strlen(option);
183 if (!*str || *str == ',')
184 return true;
185 }
186
187 while (*str && *str != ',')
188 str++;
189
190 if (*str == ',')
191 str++;
192 }
193
194 return false;
195}
Baoquan Hef51b17c2017-04-17 21:34:56 +0800196
197/*
198 * Parse a string to get a param value pair.
199 * You can use " around spaces, but can't escape ".
200 * Hyphens and underscores equivalent in parameter names.
201 */
202char *next_arg(char *args, char **param, char **val)
203{
204 unsigned int i, equals = 0;
205 int in_quote = 0, quoted = 0;
206 char *next;
207
208 if (*args == '"') {
209 args++;
210 in_quote = 1;
211 quoted = 1;
212 }
213
214 for (i = 0; args[i]; i++) {
215 if (isspace(args[i]) && !in_quote)
216 break;
217 if (equals == 0) {
218 if (args[i] == '=')
219 equals = i;
220 }
221 if (args[i] == '"')
222 in_quote = !in_quote;
223 }
224
225 *param = args;
226 if (!equals)
227 *val = NULL;
228 else {
229 args[equals] = '\0';
230 *val = args + equals + 1;
231
232 /* Don't include quotes in value. */
233 if (**val == '"') {
234 (*val)++;
235 if (args[i-1] == '"')
236 args[i-1] = '\0';
237 }
238 }
239 if (quoted && args[i-1] == '"')
240 args[i-1] = '\0';
241
242 if (args[i]) {
243 args[i] = '\0';
244 next = args + i + 1;
245 } else
246 next = args + i;
247
248 /* Chew up trailing spaces. */
249 return skip_spaces(next);
Baoquan Hef51b17c2017-04-17 21:34:56 +0800250}