blob: dc59d62163181abcc1bb4b7b66f385e11d728d1c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/lib/cmdline.c
3 * Helper functions generally used for parsing kernel command line
4 * and module options.
5 *
6 * Code and copyrights come from init/main.c and arch/i386/kernel/setup.c.
7 *
8 * This source code is licensed under the GNU General Public License,
9 * Version 2. See the file COPYING for more details.
10 *
11 * GNU Indent formatting options for this file: -kr -i8 -npsl -pcs
12 *
13 */
14
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050015#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/string.h>
Baoquan Hef51b17c2017-04-17 21:34:56 +080018#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Derek Fults22f2e282006-12-06 20:37:11 -080020/*
21 * If a hyphen was found in get_option, this will handle the
22 * range of numbers, M-N. This will expand the range and insert
23 * the values[M, M+1, ..., N] into the ints array in get_options.
24 */
25
Ilya Matveychikova91e0f62017-06-23 15:08:49 -070026static int get_range(char **str, int *pint, int n)
Derek Fults22f2e282006-12-06 20:37:11 -080027{
28 int x, inc_counter, upper_range;
29
30 (*str)++;
31 upper_range = simple_strtol((*str), NULL, 0);
32 inc_counter = upper_range - *pint;
Ilya Matveychikova91e0f62017-06-23 15:08:49 -070033 for (x = *pint; n && x < upper_range; x++, n--)
Derek Fults22f2e282006-12-06 20:37:11 -080034 *pint++ = x;
35 return inc_counter;
36}
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/**
39 * get_option - Parse integer from an option string
40 * @str: option string
41 * @pint: (output) integer value parsed from @str
42 *
43 * Read an int from an option string; if available accept a subsequent
44 * comma as well.
45 *
46 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
52
Felipe Contreras9fd43052014-01-23 15:54:35 -080053int get_option(char **str, int *pint)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 char *cur = *str;
56
57 if (!cur || !(*cur))
58 return 0;
Felipe Contreras9fd43052014-01-23 15:54:35 -080059 *pint = simple_strtol(cur, str, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (cur == *str)
61 return 0;
62 if (**str == ',') {
63 (*str)++;
64 return 2;
65 }
Derek Fults22f2e282006-12-06 20:37:11 -080066 if (**str == '-')
67 return 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 return 1;
70}
Felipe Contrerasff6f9bb2014-01-23 15:54:36 -080071EXPORT_SYMBOL(get_option);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/**
74 * get_options - Parse a string into a list of integers
75 * @str: String to be parsed
76 * @nints: size of integer array
77 * @ints: integer array
78 *
79 * This function parses a string containing a comma-separated
Derek Fults22f2e282006-12-06 20:37:11 -080080 * list of integers, a hyphen-separated range of _positive_ integers,
81 * or a combination of both. The parse halts when the array is
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * full, or when no more numbers can be retrieved from the
83 * string.
84 *
85 * Return value is the character in the string which caused
86 * the parse to end (typically a null terminator, if @str is
87 * completely parseable).
88 */
Felipe Contreras9fd43052014-01-23 15:54:35 -080089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090char *get_options(const char *str, int nints, int *ints)
91{
92 int res, i = 1;
93
94 while (i < nints) {
Felipe Contreras9fd43052014-01-23 15:54:35 -080095 res = get_option((char **)&str, ints + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (res == 0)
97 break;
Derek Fults22f2e282006-12-06 20:37:11 -080098 if (res == 3) {
99 int range_nums;
Ilya Matveychikova91e0f62017-06-23 15:08:49 -0700100 range_nums = get_range((char **)&str, ints + i, nints - i);
Derek Fults22f2e282006-12-06 20:37:11 -0800101 if (range_nums < 0)
102 break;
103 /*
104 * Decrement the result by one to leave out the
105 * last number in the range. The next iteration
106 * will handle the upper number in the range
107 */
108 i += (range_nums - 1);
109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 i++;
111 if (res == 1)
112 break;
113 }
114 ints[0] = i - 1;
115 return (char *)str;
116}
Felipe Contrerasff6f9bb2014-01-23 15:54:36 -0800117EXPORT_SYMBOL(get_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119/**
120 * memparse - parse a string with mem suffixes into a number
121 * @ptr: Where parse begins
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700122 * @retptr: (output) Optional pointer to next char after parse completes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 *
124 * Parses a string into a number. The number stored at @ptr is
Gui Hechenge004f3c2014-08-06 16:09:29 -0700125 * potentially suffixed with K, M, G, T, P, E.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
127
Jeremy Fitzhardinged974ae32008-07-24 16:27:46 -0700128unsigned long long memparse(const char *ptr, char **retptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700130 char *endptr; /* local pointer to end of parsed string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700132 unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
133
134 switch (*endptr) {
Gui Hechenge004f3c2014-08-06 16:09:29 -0700135 case 'E':
136 case 'e':
137 ret <<= 10;
Gustavo A. R. Silva8a054522019-01-25 14:25:24 -0600138 /* fall through */
Gui Hechenge004f3c2014-08-06 16:09:29 -0700139 case 'P':
140 case 'p':
141 ret <<= 10;
Gustavo A. R. Silva8a054522019-01-25 14:25:24 -0600142 /* fall through */
Gui Hechenge004f3c2014-08-06 16:09:29 -0700143 case 'T':
144 case 't':
145 ret <<= 10;
Gustavo A. R. Silva8a054522019-01-25 14:25:24 -0600146 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 case 'G':
148 case 'g':
149 ret <<= 10;
Gustavo A. R. Silva8a054522019-01-25 14:25:24 -0600150 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 case 'M':
152 case 'm':
153 ret <<= 10;
Gustavo A. R. Silva8a054522019-01-25 14:25:24 -0600154 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 case 'K':
156 case 'k':
157 ret <<= 10;
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700158 endptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 default:
160 break;
161 }
Robert P. J. Dayfd193822008-07-25 01:45:31 -0700162
163 if (retptr)
164 *retptr = endptr;
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return ret;
167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168EXPORT_SYMBOL(memparse);
Dave Young6ccc72b82014-08-14 17:15:27 +0800169
170/**
171 * parse_option_str - Parse a string and check an option is set or not
172 * @str: String to be parsed
173 * @option: option name
174 *
175 * This function parses a string containing a comma-separated list of
176 * strings like a=b,c.
177 *
178 * Return true if there's such option in the string, or return false.
179 */
180bool parse_option_str(const char *str, const char *option)
181{
182 while (*str) {
183 if (!strncmp(str, option, strlen(option))) {
184 str += strlen(option);
185 if (!*str || *str == ',')
186 return true;
187 }
188
189 while (*str && *str != ',')
190 str++;
191
192 if (*str == ',')
193 str++;
194 }
195
196 return false;
197}
Baoquan Hef51b17c2017-04-17 21:34:56 +0800198
199/*
200 * Parse a string to get a param value pair.
201 * You can use " around spaces, but can't escape ".
202 * Hyphens and underscores equivalent in parameter names.
203 */
204char *next_arg(char *args, char **param, char **val)
205{
206 unsigned int i, equals = 0;
207 int in_quote = 0, quoted = 0;
208 char *next;
209
210 if (*args == '"') {
211 args++;
212 in_quote = 1;
213 quoted = 1;
214 }
215
216 for (i = 0; args[i]; i++) {
217 if (isspace(args[i]) && !in_quote)
218 break;
219 if (equals == 0) {
220 if (args[i] == '=')
221 equals = i;
222 }
223 if (args[i] == '"')
224 in_quote = !in_quote;
225 }
226
227 *param = args;
228 if (!equals)
229 *val = NULL;
230 else {
231 args[equals] = '\0';
232 *val = args + equals + 1;
233
234 /* Don't include quotes in value. */
235 if (**val == '"') {
236 (*val)++;
237 if (args[i-1] == '"')
238 args[i-1] = '\0';
239 }
240 }
241 if (quoted && args[i-1] == '"')
242 args[i-1] = '\0';
243
244 if (args[i]) {
245 args[i] = '\0';
246 next = args + i + 1;
247 } else
248 next = args + i;
249
250 /* Chew up trailing spaces. */
251 return skip_spaces(next);
Baoquan Hef51b17c2017-04-17 21:34:56 +0800252}