1 // SPDX-License-Identifier: GPL-2.0-only
4 * Misc librarized functions for cmdline poking.
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/ctype.h>
10 #include <asm/cmdline.h>
12 static inline int myisspace(u8 c)
14 return c <= ' '; /* Close enough approximation */
18 * Find a boolean option (like quiet,noapic,nosmp....)
20 * @cmdline: the cmdline string
21 * @max_cmdline_size: the maximum size of cmdline
22 * @option: option string to look for
24 * Returns the position of that @option (starts counting with 1)
25 * or 0 on not found. @option will only be found if it is found
26 * as an entire word in @cmdline. For instance, if @option="car"
27 * then a cmdline which contains "cart" will not match.
30 __cmdline_find_option_bool(const char *cmdline, int max_cmdline_size,
34 int pos = 0, wstart = 0;
35 const char *opptr = NULL;
37 st_wordstart = 0, /* Start of word/after whitespace */
38 st_wordcmp, /* Comparing this word */
39 st_wordskip, /* Miscompare, skip */
40 } state = st_wordstart;
43 return -1; /* No command line */
46 * This 'pos' check ensures we do not overrun
47 * a non-NULL-terminated 'cmdline'
49 while (pos < max_cmdline_size) {
50 c = *(char *)cmdline++;
57 else if (myisspace(c))
68 * We matched all the way to the end of the
69 * option we were looking for. If the
70 * command-line has a space _or_ ends, then
73 if (!c || myisspace(c))
76 * We hit the end of the option, but _not_
77 * the end of a word on the cmdline. Not
82 * Hit the NULL terminator on the end of
86 } else if (c == *opptr++) {
88 * We are currently matching, so continue
89 * to the next character on the cmdline.
99 else if (myisspace(c))
100 state = st_wordstart;
105 return 0; /* Buffer overrun */
109 * Find a non-boolean option (i.e. option=argument). In accordance with
110 * standard Linux practice, if this option is repeated, this returns the
111 * last instance on the command line.
113 * @cmdline: the cmdline string
114 * @max_cmdline_size: the maximum size of cmdline
115 * @option: option string to look for
116 * @buffer: memory buffer to return the option argument
117 * @bufsize: size of the supplied memory buffer
119 * Returns the length of the argument (regardless of if it was
120 * truncated to fit in the buffer), or -1 on not found.
123 __cmdline_find_option(const char *cmdline, int max_cmdline_size,
124 const char *option, char *buffer, int bufsize)
127 int pos = 0, len = -1;
128 const char *opptr = NULL;
129 char *bufptr = buffer;
131 st_wordstart = 0, /* Start of word/after whitespace */
132 st_wordcmp, /* Comparing this word */
133 st_wordskip, /* Miscompare, skip */
134 st_bufcpy, /* Copying this to buffer */
135 } state = st_wordstart;
138 return -1; /* No command line */
141 * This 'pos' check ensures we do not overrun
142 * a non-NULL-terminated 'cmdline'
144 while (pos++ < max_cmdline_size) {
145 c = *(char *)cmdline++;
159 if ((c == '=') && !*opptr) {
161 * We matched all the way to the end of the
162 * option we were looking for, prepare to
169 } else if (c == *opptr++) {
171 * We are currently matching, so continue
172 * to the next character on the cmdline.
181 state = st_wordstart;
186 state = st_wordstart;
189 * Increment len, but don't overrun the
190 * supplied buffer and leave room for the
206 int cmdline_find_option_bool(const char *cmdline, const char *option)
208 return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE, option);
211 int cmdline_find_option(const char *cmdline, const char *option, char *buffer,
214 return __cmdline_find_option(cmdline, COMMAND_LINE_SIZE, option,