Thomas Gleixner | 40b0b3f | 2019-06-03 07:44:46 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * GNU Indent formatting options for this file: -kr -i8 -npsl -pcs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 12 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/string.h> |
Baoquan He | f51b17c | 2017-04-17 21:34:56 +0800 | [diff] [blame] | 15 | #include <linux/ctype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Derek Fults | 22f2e28 | 2006-12-06 20:37:11 -0800 | [diff] [blame] | 17 | /* |
| 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 Matveychikov | a91e0f6 | 2017-06-23 15:08:49 -0700 | [diff] [blame] | 23 | static int get_range(char **str, int *pint, int n) |
Derek Fults | 22f2e28 | 2006-12-06 20:37:11 -0800 | [diff] [blame] | 24 | { |
| 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 Matveychikov | a91e0f6 | 2017-06-23 15:08:49 -0700 | [diff] [blame] | 30 | for (x = *pint; n && x < upper_range; x++, n--) |
Derek Fults | 22f2e28 | 2006-12-06 20:37:11 -0800 | [diff] [blame] | 31 | *pint++ = x; |
| 32 | return inc_counter; |
| 33 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 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. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 44 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | */ |
| 49 | |
Felipe Contreras | 9fd4305 | 2014-01-23 15:54:35 -0800 | [diff] [blame] | 50 | int get_option(char **str, int *pint) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
| 52 | char *cur = *str; |
| 53 | |
| 54 | if (!cur || !(*cur)) |
| 55 | return 0; |
Felipe Contreras | 9fd4305 | 2014-01-23 15:54:35 -0800 | [diff] [blame] | 56 | *pint = simple_strtol(cur, str, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | if (cur == *str) |
| 58 | return 0; |
| 59 | if (**str == ',') { |
| 60 | (*str)++; |
| 61 | return 2; |
| 62 | } |
Derek Fults | 22f2e28 | 2006-12-06 20:37:11 -0800 | [diff] [blame] | 63 | if (**str == '-') |
| 64 | return 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | return 1; |
| 67 | } |
Felipe Contreras | ff6f9bb | 2014-01-23 15:54:36 -0800 | [diff] [blame] | 68 | EXPORT_SYMBOL(get_option); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 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 Fults | 22f2e28 | 2006-12-06 20:37:11 -0800 | [diff] [blame] | 77 | * list of integers, a hyphen-separated range of _positive_ integers, |
| 78 | * or a combination of both. The parse halts when the array is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | * 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 Contreras | 9fd4305 | 2014-01-23 15:54:35 -0800 | [diff] [blame] | 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | char *get_options(const char *str, int nints, int *ints) |
| 88 | { |
| 89 | int res, i = 1; |
| 90 | |
| 91 | while (i < nints) { |
Felipe Contreras | 9fd4305 | 2014-01-23 15:54:35 -0800 | [diff] [blame] | 92 | res = get_option((char **)&str, ints + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | if (res == 0) |
| 94 | break; |
Derek Fults | 22f2e28 | 2006-12-06 20:37:11 -0800 | [diff] [blame] | 95 | if (res == 3) { |
| 96 | int range_nums; |
Ilya Matveychikov | a91e0f6 | 2017-06-23 15:08:49 -0700 | [diff] [blame] | 97 | range_nums = get_range((char **)&str, ints + i, nints - i); |
Derek Fults | 22f2e28 | 2006-12-06 20:37:11 -0800 | [diff] [blame] | 98 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | i++; |
| 108 | if (res == 1) |
| 109 | break; |
| 110 | } |
| 111 | ints[0] = i - 1; |
| 112 | return (char *)str; |
| 113 | } |
Felipe Contreras | ff6f9bb | 2014-01-23 15:54:36 -0800 | [diff] [blame] | 114 | EXPORT_SYMBOL(get_options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * memparse - parse a string with mem suffixes into a number |
| 118 | * @ptr: Where parse begins |
Robert P. J. Day | fd19382 | 2008-07-25 01:45:31 -0700 | [diff] [blame] | 119 | * @retptr: (output) Optional pointer to next char after parse completes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | * |
| 121 | * Parses a string into a number. The number stored at @ptr is |
Gui Hecheng | e004f3c | 2014-08-06 16:09:29 -0700 | [diff] [blame] | 122 | * potentially suffixed with K, M, G, T, P, E. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | */ |
| 124 | |
Jeremy Fitzhardinge | d974ae3 | 2008-07-24 16:27:46 -0700 | [diff] [blame] | 125 | unsigned long long memparse(const char *ptr, char **retptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Robert P. J. Day | fd19382 | 2008-07-25 01:45:31 -0700 | [diff] [blame] | 127 | char *endptr; /* local pointer to end of parsed string */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Robert P. J. Day | fd19382 | 2008-07-25 01:45:31 -0700 | [diff] [blame] | 129 | unsigned long long ret = simple_strtoull(ptr, &endptr, 0); |
| 130 | |
| 131 | switch (*endptr) { |
Gui Hecheng | e004f3c | 2014-08-06 16:09:29 -0700 | [diff] [blame] | 132 | case 'E': |
| 133 | case 'e': |
| 134 | ret <<= 10; |
Gustavo A. R. Silva | 6a9dc5f | 2020-08-24 15:36:14 -0500 | [diff] [blame] | 135 | /* fall through */ |
Gui Hecheng | e004f3c | 2014-08-06 16:09:29 -0700 | [diff] [blame] | 136 | case 'P': |
| 137 | case 'p': |
| 138 | ret <<= 10; |
Gustavo A. R. Silva | 6a9dc5f | 2020-08-24 15:36:14 -0500 | [diff] [blame] | 139 | /* fall through */ |
Gui Hecheng | e004f3c | 2014-08-06 16:09:29 -0700 | [diff] [blame] | 140 | case 'T': |
| 141 | case 't': |
| 142 | ret <<= 10; |
Gustavo A. R. Silva | 6a9dc5f | 2020-08-24 15:36:14 -0500 | [diff] [blame] | 143 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | case 'G': |
| 145 | case 'g': |
| 146 | ret <<= 10; |
Gustavo A. R. Silva | 6a9dc5f | 2020-08-24 15:36:14 -0500 | [diff] [blame] | 147 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | case 'M': |
| 149 | case 'm': |
| 150 | ret <<= 10; |
Gustavo A. R. Silva | 6a9dc5f | 2020-08-24 15:36:14 -0500 | [diff] [blame] | 151 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | case 'K': |
| 153 | case 'k': |
| 154 | ret <<= 10; |
Robert P. J. Day | fd19382 | 2008-07-25 01:45:31 -0700 | [diff] [blame] | 155 | endptr++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | default: |
| 157 | break; |
| 158 | } |
Robert P. J. Day | fd19382 | 2008-07-25 01:45:31 -0700 | [diff] [blame] | 159 | |
| 160 | if (retptr) |
| 161 | *retptr = endptr; |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return ret; |
| 164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | EXPORT_SYMBOL(memparse); |
Dave Young | 6ccc72b8 | 2014-08-14 17:15:27 +0800 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * parse_option_str - Parse a string and check an option is set or not |
| 169 | * @str: String to be parsed |
| 170 | * @option: option name |
| 171 | * |
| 172 | * This function parses a string containing a comma-separated list of |
| 173 | * strings like a=b,c. |
| 174 | * |
| 175 | * Return true if there's such option in the string, or return false. |
| 176 | */ |
| 177 | bool parse_option_str(const char *str, const char *option) |
| 178 | { |
| 179 | while (*str) { |
| 180 | if (!strncmp(str, option, strlen(option))) { |
| 181 | str += strlen(option); |
| 182 | if (!*str || *str == ',') |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | while (*str && *str != ',') |
| 187 | str++; |
| 188 | |
| 189 | if (*str == ',') |
| 190 | str++; |
| 191 | } |
| 192 | |
| 193 | return false; |
| 194 | } |
Baoquan He | f51b17c | 2017-04-17 21:34:56 +0800 | [diff] [blame] | 195 | |
| 196 | /* |
| 197 | * Parse a string to get a param value pair. |
| 198 | * You can use " around spaces, but can't escape ". |
| 199 | * Hyphens and underscores equivalent in parameter names. |
| 200 | */ |
| 201 | char *next_arg(char *args, char **param, char **val) |
| 202 | { |
| 203 | unsigned int i, equals = 0; |
| 204 | int in_quote = 0, quoted = 0; |
| 205 | char *next; |
| 206 | |
| 207 | if (*args == '"') { |
| 208 | args++; |
| 209 | in_quote = 1; |
| 210 | quoted = 1; |
| 211 | } |
| 212 | |
| 213 | for (i = 0; args[i]; i++) { |
| 214 | if (isspace(args[i]) && !in_quote) |
| 215 | break; |
| 216 | if (equals == 0) { |
| 217 | if (args[i] == '=') |
| 218 | equals = i; |
| 219 | } |
| 220 | if (args[i] == '"') |
| 221 | in_quote = !in_quote; |
| 222 | } |
| 223 | |
| 224 | *param = args; |
| 225 | if (!equals) |
| 226 | *val = NULL; |
| 227 | else { |
| 228 | args[equals] = '\0'; |
| 229 | *val = args + equals + 1; |
| 230 | |
| 231 | /* Don't include quotes in value. */ |
| 232 | if (**val == '"') { |
| 233 | (*val)++; |
| 234 | if (args[i-1] == '"') |
| 235 | args[i-1] = '\0'; |
| 236 | } |
| 237 | } |
| 238 | if (quoted && args[i-1] == '"') |
| 239 | args[i-1] = '\0'; |
| 240 | |
| 241 | if (args[i]) { |
| 242 | args[i] = '\0'; |
| 243 | next = args + i + 1; |
| 244 | } else |
| 245 | next = args + i; |
| 246 | |
| 247 | /* Chew up trailing spaces. */ |
| 248 | return skip_spaces(next); |
Baoquan He | f51b17c | 2017-04-17 21:34:56 +0800 | [diff] [blame] | 249 | } |