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/setup.h>
11 #include <asm/cmdline.h>
14 static inline int myisspace(u8 c)
16 return c <= ' '; /* Close enough approximation */
20 * Find a boolean option (like quiet,noapic,nosmp....)
22 * @cmdline: the cmdline string
23 * @max_cmdline_size: the maximum size of cmdline
24 * @option: option string to look for
26 * Returns the position of that @option (starts counting with 1)
27 * or 0 on not found. @option will only be found if it is found
28 * as an entire word in @cmdline. For instance, if @option="car"
29 * then a cmdline which contains "cart" will not match.
32 __cmdline_find_option_bool(const char *cmdline, int max_cmdline_size,
36 int pos = 0, wstart = 0;
37 const char *opptr = NULL;
39 st_wordstart = 0, /* Start of word/after whitespace */
40 st_wordcmp, /* Comparing this word */
41 st_wordskip, /* Miscompare, skip */
42 } state = st_wordstart;
45 return -1; /* No command line */
48 * This 'pos' check ensures we do not overrun
49 * a non-NULL-terminated 'cmdline'
51 while (pos < max_cmdline_size) {
52 c = *(char *)cmdline++;
59 else if (myisspace(c))
70 * We matched all the way to the end of the
71 * option we were looking for. If the
72 * command-line has a space _or_ ends, then
75 if (!c || myisspace(c))
78 * We hit the end of the option, but _not_
79 * the end of a word on the cmdline. Not
84 * Hit the NULL terminator on the end of
88 } else if (c == *opptr++) {
90 * We are currently matching, so continue
91 * to the next character on the cmdline.
101 else if (myisspace(c))
102 state = st_wordstart;
107 return 0; /* Buffer overrun */
111 * Find a non-boolean option (i.e. option=argument). In accordance with
112 * standard Linux practice, if this option is repeated, this returns the
113 * last instance on the command line.
115 * @cmdline: the cmdline string
116 * @max_cmdline_size: the maximum size of cmdline
117 * @option: option string to look for
118 * @buffer: memory buffer to return the option argument
119 * @bufsize: size of the supplied memory buffer
121 * Returns the length of the argument (regardless of if it was
122 * truncated to fit in the buffer), or -1 on not found.
125 __cmdline_find_option(const char *cmdline, int max_cmdline_size,
126 const char *option, char *buffer, int bufsize)
129 int pos = 0, len = -1;
130 const char *opptr = NULL;
131 char *bufptr = buffer;
133 st_wordstart = 0, /* Start of word/after whitespace */
134 st_wordcmp, /* Comparing this word */
135 st_wordskip, /* Miscompare, skip */
136 st_bufcpy, /* Copying this to buffer */
137 } state = st_wordstart;
140 return -1; /* No command line */
143 * This 'pos' check ensures we do not overrun
144 * a non-NULL-terminated 'cmdline'
146 while (pos++ < max_cmdline_size) {
147 c = *(char *)cmdline++;
161 if ((c == '=') && !*opptr) {
163 * We matched all the way to the end of the
164 * option we were looking for, prepare to
171 } else if (c == *opptr++) {
173 * We are currently matching, so continue
174 * to the next character on the cmdline.
183 state = st_wordstart;
188 state = st_wordstart;
191 * Increment len, but don't overrun the
192 * supplied buffer and leave room for the
208 int cmdline_find_option_bool(const char *cmdline, const char *option)
212 ret = __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE, option);
216 if (IS_ENABLED(CONFIG_CMDLINE_BOOL) && !builtin_cmdline_added)
217 return __cmdline_find_option_bool(builtin_cmdline, COMMAND_LINE_SIZE, option);
222 int cmdline_find_option(const char *cmdline, const char *option, char *buffer,
227 ret = __cmdline_find_option(cmdline, COMMAND_LINE_SIZE, option, buffer, bufsize);
231 if (IS_ENABLED(CONFIG_CMDLINE_BOOL) && !builtin_cmdline_added)
232 return __cmdline_find_option(builtin_cmdline, COMMAND_LINE_SIZE, option, buffer, bufsize);