140b0b3f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/lib/cmdline.c
41da177e4SLinus Torvalds * Helper functions generally used for parsing kernel command line
51da177e4SLinus Torvalds * and module options.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Code and copyrights come from init/main.c and arch/i386/kernel/setup.c.
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * GNU Indent formatting options for this file: -kr -i8 -npsl -pcs
101da177e4SLinus Torvalds */
111da177e4SLinus Torvalds
128bc3bcc9SPaul Gortmaker #include <linux/export.h>
131da177e4SLinus Torvalds #include <linux/kernel.h>
141da177e4SLinus Torvalds #include <linux/string.h>
15f51b17c8SBaoquan He #include <linux/ctype.h>
161da177e4SLinus Torvalds
1722f2e280SDerek Fults /*
1822f2e280SDerek Fults * If a hyphen was found in get_option, this will handle the
1922f2e280SDerek Fults * range of numbers, M-N. This will expand the range and insert
2022f2e280SDerek Fults * the values[M, M+1, ..., N] into the ints array in get_options.
2122f2e280SDerek Fults */
2222f2e280SDerek Fults
get_range(char ** str,int * pint,int n)23a91e0f68SIlya Matveychikov static int get_range(char **str, int *pint, int n)
2422f2e280SDerek Fults {
2522f2e280SDerek Fults int x, inc_counter, upper_range;
2622f2e280SDerek Fults
2722f2e280SDerek Fults (*str)++;
2822f2e280SDerek Fults upper_range = simple_strtol((*str), NULL, 0);
2922f2e280SDerek Fults inc_counter = upper_range - *pint;
30a91e0f68SIlya Matveychikov for (x = *pint; n && x < upper_range; x++, n--)
3122f2e280SDerek Fults *pint++ = x;
3222f2e280SDerek Fults return inc_counter;
3322f2e280SDerek Fults }
341da177e4SLinus Torvalds
351da177e4SLinus Torvalds /**
361da177e4SLinus Torvalds * get_option - Parse integer from an option string
371da177e4SLinus Torvalds * @str: option string
386b2b6b86SAndy Shevchenko * @pint: (optional output) integer value parsed from @str
391da177e4SLinus Torvalds *
401da177e4SLinus Torvalds * Read an int from an option string; if available accept a subsequent
411da177e4SLinus Torvalds * comma as well.
421da177e4SLinus Torvalds *
436b2b6b86SAndy Shevchenko * When @pint is NULL the function can be used as a validator of
446b2b6b86SAndy Shevchenko * the current option in the string.
456b2b6b86SAndy Shevchenko *
461da177e4SLinus Torvalds * Return values:
4772fd4a35SRobert P. J. Day * 0 - no int in string
4872fd4a35SRobert P. J. Day * 1 - int found, no subsequent comma
4972fd4a35SRobert P. J. Day * 2 - int found including a subsequent comma
5072fd4a35SRobert P. J. Day * 3 - hyphen found to denote a range
51e291851dSAndy Shevchenko *
52e291851dSAndy Shevchenko * Leading hyphen without integer is no integer case, but we consume it
53e291851dSAndy Shevchenko * for the sake of simplification.
541da177e4SLinus Torvalds */
551da177e4SLinus Torvalds
get_option(char ** str,int * pint)561da177e4SLinus Torvalds int get_option(char **str, int *pint)
571da177e4SLinus Torvalds {
581da177e4SLinus Torvalds char *cur = *str;
596b2b6b86SAndy Shevchenko int value;
601da177e4SLinus Torvalds
611da177e4SLinus Torvalds if (!cur || !(*cur))
621da177e4SLinus Torvalds return 0;
63e291851dSAndy Shevchenko if (*cur == '-')
646b2b6b86SAndy Shevchenko value = -simple_strtoull(++cur, str, 0);
65e291851dSAndy Shevchenko else
666b2b6b86SAndy Shevchenko value = simple_strtoull(cur, str, 0);
676b2b6b86SAndy Shevchenko if (pint)
686b2b6b86SAndy Shevchenko *pint = value;
691da177e4SLinus Torvalds if (cur == *str)
701da177e4SLinus Torvalds return 0;
711da177e4SLinus Torvalds if (**str == ',') {
721da177e4SLinus Torvalds (*str)++;
731da177e4SLinus Torvalds return 2;
741da177e4SLinus Torvalds }
7522f2e280SDerek Fults if (**str == '-')
7622f2e280SDerek Fults return 3;
771da177e4SLinus Torvalds
781da177e4SLinus Torvalds return 1;
791da177e4SLinus Torvalds }
80ff6f9bbbSFelipe Contreras EXPORT_SYMBOL(get_option);
811da177e4SLinus Torvalds
821da177e4SLinus Torvalds /**
831da177e4SLinus Torvalds * get_options - Parse a string into a list of integers
841da177e4SLinus Torvalds * @str: String to be parsed
851da177e4SLinus Torvalds * @nints: size of integer array
86f1f405c3SAndy Shevchenko * @ints: integer array (must have room for at least one element)
871da177e4SLinus Torvalds *
881da177e4SLinus Torvalds * This function parses a string containing a comma-separated
8922f2e280SDerek Fults * list of integers, a hyphen-separated range of _positive_ integers,
9022f2e280SDerek Fults * or a combination of both. The parse halts when the array is
911da177e4SLinus Torvalds * full, or when no more numbers can be retrieved from the
921da177e4SLinus Torvalds * string.
931da177e4SLinus Torvalds *
940ea09083SAndy Shevchenko * When @nints is 0, the function just validates the given @str and
950ea09083SAndy Shevchenko * returns the amount of parseable integers as described below.
960ea09083SAndy Shevchenko *
97f1f405c3SAndy Shevchenko * Returns:
98f1f405c3SAndy Shevchenko *
99f1f405c3SAndy Shevchenko * The first element is filled by the number of collected integers
100f1f405c3SAndy Shevchenko * in the range. The rest is what was parsed from the @str.
101f1f405c3SAndy Shevchenko *
1021da177e4SLinus Torvalds * Return value is the character in the string which caused
1031da177e4SLinus Torvalds * the parse to end (typically a null terminator, if @str is
1041da177e4SLinus Torvalds * completely parseable).
1051da177e4SLinus Torvalds */
1061da177e4SLinus Torvalds
get_options(const char * str,int nints,int * ints)1071da177e4SLinus Torvalds char *get_options(const char *str, int nints, int *ints)
1081da177e4SLinus Torvalds {
1090ea09083SAndy Shevchenko bool validate = (nints == 0);
1101da177e4SLinus Torvalds int res, i = 1;
1111da177e4SLinus Torvalds
1120ea09083SAndy Shevchenko while (i < nints || validate) {
1130ea09083SAndy Shevchenko int *pint = validate ? ints : ints + i;
1140ea09083SAndy Shevchenko
1150ea09083SAndy Shevchenko res = get_option((char **)&str, pint);
1161da177e4SLinus Torvalds if (res == 0)
1171da177e4SLinus Torvalds break;
11822f2e280SDerek Fults if (res == 3) {
1190ea09083SAndy Shevchenko int n = validate ? 0 : nints - i;
12022f2e280SDerek Fults int range_nums;
1210ea09083SAndy Shevchenko
1220ea09083SAndy Shevchenko range_nums = get_range((char **)&str, pint, n);
12322f2e280SDerek Fults if (range_nums < 0)
12422f2e280SDerek Fults break;
12522f2e280SDerek Fults /*
12622f2e280SDerek Fults * Decrement the result by one to leave out the
12722f2e280SDerek Fults * last number in the range. The next iteration
12822f2e280SDerek Fults * will handle the upper number in the range
12922f2e280SDerek Fults */
13022f2e280SDerek Fults i += (range_nums - 1);
13122f2e280SDerek Fults }
1321da177e4SLinus Torvalds i++;
1331da177e4SLinus Torvalds if (res == 1)
1341da177e4SLinus Torvalds break;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds ints[0] = i - 1;
1371da177e4SLinus Torvalds return (char *)str;
1381da177e4SLinus Torvalds }
139ff6f9bbbSFelipe Contreras EXPORT_SYMBOL(get_options);
1401da177e4SLinus Torvalds
1411da177e4SLinus Torvalds /**
1421da177e4SLinus Torvalds * memparse - parse a string with mem suffixes into a number
1431da177e4SLinus Torvalds * @ptr: Where parse begins
144fd193829SRobert P. J. Day * @retptr: (output) Optional pointer to next char after parse completes
1451da177e4SLinus Torvalds *
1461da177e4SLinus Torvalds * Parses a string into a number. The number stored at @ptr is
147e004f3c7SGui Hecheng * potentially suffixed with K, M, G, T, P, E.
1481da177e4SLinus Torvalds */
1491da177e4SLinus Torvalds
memparse(const char * ptr,char ** retptr)150d974ae37SJeremy Fitzhardinge unsigned long long memparse(const char *ptr, char **retptr)
1511da177e4SLinus Torvalds {
152fd193829SRobert P. J. Day char *endptr; /* local pointer to end of parsed string */
1531da177e4SLinus Torvalds
154fd193829SRobert P. J. Day unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
155fd193829SRobert P. J. Day
156fd193829SRobert P. J. Day switch (*endptr) {
157e004f3c7SGui Hecheng case 'E':
158e004f3c7SGui Hecheng case 'e':
159e004f3c7SGui Hecheng ret <<= 10;
1604c1ca831SNick Desaulniers fallthrough;
161e004f3c7SGui Hecheng case 'P':
162e004f3c7SGui Hecheng case 'p':
163e004f3c7SGui Hecheng ret <<= 10;
1644c1ca831SNick Desaulniers fallthrough;
165e004f3c7SGui Hecheng case 'T':
166e004f3c7SGui Hecheng case 't':
167e004f3c7SGui Hecheng ret <<= 10;
1684c1ca831SNick Desaulniers fallthrough;
1691da177e4SLinus Torvalds case 'G':
1701da177e4SLinus Torvalds case 'g':
1711da177e4SLinus Torvalds ret <<= 10;
1724c1ca831SNick Desaulniers fallthrough;
1731da177e4SLinus Torvalds case 'M':
1741da177e4SLinus Torvalds case 'm':
1751da177e4SLinus Torvalds ret <<= 10;
1764c1ca831SNick Desaulniers fallthrough;
1771da177e4SLinus Torvalds case 'K':
1781da177e4SLinus Torvalds case 'k':
1791da177e4SLinus Torvalds ret <<= 10;
180fd193829SRobert P. J. Day endptr++;
18136f9ff9eSGustavo A. R. Silva fallthrough;
1821da177e4SLinus Torvalds default:
1831da177e4SLinus Torvalds break;
1841da177e4SLinus Torvalds }
185fd193829SRobert P. J. Day
186fd193829SRobert P. J. Day if (retptr)
187fd193829SRobert P. J. Day *retptr = endptr;
188fd193829SRobert P. J. Day
1891da177e4SLinus Torvalds return ret;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds EXPORT_SYMBOL(memparse);
1926ccc72b8SDave Young
1936ccc72b8SDave Young /**
1946ccc72b8SDave Young * parse_option_str - Parse a string and check an option is set or not
1956ccc72b8SDave Young * @str: String to be parsed
1966ccc72b8SDave Young * @option: option name
1976ccc72b8SDave Young *
1986ccc72b8SDave Young * This function parses a string containing a comma-separated list of
1996ccc72b8SDave Young * strings like a=b,c.
2006ccc72b8SDave Young *
2016ccc72b8SDave Young * Return true if there's such option in the string, or return false.
2026ccc72b8SDave Young */
parse_option_str(const char * str,const char * option)2036ccc72b8SDave Young bool parse_option_str(const char *str, const char *option)
2046ccc72b8SDave Young {
2056ccc72b8SDave Young while (*str) {
2066ccc72b8SDave Young if (!strncmp(str, option, strlen(option))) {
2076ccc72b8SDave Young str += strlen(option);
2086ccc72b8SDave Young if (!*str || *str == ',')
2096ccc72b8SDave Young return true;
2106ccc72b8SDave Young }
2116ccc72b8SDave Young
2126ccc72b8SDave Young while (*str && *str != ',')
2136ccc72b8SDave Young str++;
2146ccc72b8SDave Young
2156ccc72b8SDave Young if (*str == ',')
2166ccc72b8SDave Young str++;
2176ccc72b8SDave Young }
2186ccc72b8SDave Young
2196ccc72b8SDave Young return false;
2206ccc72b8SDave Young }
221f51b17c8SBaoquan He
222f51b17c8SBaoquan He /*
223f51b17c8SBaoquan He * Parse a string to get a param value pair.
224f51b17c8SBaoquan He * You can use " around spaces, but can't escape ".
225f51b17c8SBaoquan He * Hyphens and underscores equivalent in parameter names.
226f51b17c8SBaoquan He */
next_arg(char * args,char ** param,char ** val)227f51b17c8SBaoquan He char *next_arg(char *args, char **param, char **val)
228f51b17c8SBaoquan He {
229f51b17c8SBaoquan He unsigned int i, equals = 0;
230f51b17c8SBaoquan He int in_quote = 0, quoted = 0;
231f51b17c8SBaoquan He
232f51b17c8SBaoquan He if (*args == '"') {
233f51b17c8SBaoquan He args++;
234f51b17c8SBaoquan He in_quote = 1;
235f51b17c8SBaoquan He quoted = 1;
236f51b17c8SBaoquan He }
237f51b17c8SBaoquan He
238f51b17c8SBaoquan He for (i = 0; args[i]; i++) {
239f51b17c8SBaoquan He if (isspace(args[i]) && !in_quote)
240f51b17c8SBaoquan He break;
241f51b17c8SBaoquan He if (equals == 0) {
242f51b17c8SBaoquan He if (args[i] == '=')
243f51b17c8SBaoquan He equals = i;
244f51b17c8SBaoquan He }
245f51b17c8SBaoquan He if (args[i] == '"')
246f51b17c8SBaoquan He in_quote = !in_quote;
247f51b17c8SBaoquan He }
248f51b17c8SBaoquan He
249f51b17c8SBaoquan He *param = args;
250f51b17c8SBaoquan He if (!equals)
251f51b17c8SBaoquan He *val = NULL;
252f51b17c8SBaoquan He else {
253f51b17c8SBaoquan He args[equals] = '\0';
254f51b17c8SBaoquan He *val = args + equals + 1;
255f51b17c8SBaoquan He
256f51b17c8SBaoquan He /* Don't include quotes in value. */
257f51b17c8SBaoquan He if (**val == '"') {
258f51b17c8SBaoquan He (*val)++;
259f51b17c8SBaoquan He if (args[i-1] == '"')
260f51b17c8SBaoquan He args[i-1] = '\0';
261f51b17c8SBaoquan He }
262f51b17c8SBaoquan He }
263*9847f212SNeel Natu if (quoted && i > 0 && args[i-1] == '"')
264f51b17c8SBaoquan He args[i-1] = '\0';
265f51b17c8SBaoquan He
266f51b17c8SBaoquan He if (args[i]) {
267f51b17c8SBaoquan He args[i] = '\0';
26896251a75SMasahiro Yamada args += i + 1;
269f51b17c8SBaoquan He } else
27096251a75SMasahiro Yamada args += i;
271f51b17c8SBaoquan He
272f51b17c8SBaoquan He /* Chew up trailing spaces. */
27396251a75SMasahiro Yamada return skip_spaces(args);
274f51b17c8SBaoquan He }
27565dd36a3SAndy Shevchenko EXPORT_SYMBOL(next_arg);
276