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>
11 static inline int myisspace(u8 c
)
13 return c
<= ' '; /* Close enough approximation */
17 * Find a boolean option (like quiet,noapic,nosmp....)
19 * @cmdline: the cmdline string
20 * @option: option string to look for
22 * Returns the position of that @option (starts counting with 1)
23 * or 0 on not found. @option will only be found if it is found
24 * as an entire word in @cmdline. For instance, if @option="car"
25 * then a cmdline which contains "cart" will not match.
28 __cmdline_find_option_bool(const char *cmdline
, int max_cmdline_size
,
32 int pos
= 0, wstart
= 0;
33 const char *opptr
= NULL
;
35 st_wordstart
= 0, /* Start of word/after whitespace */
36 st_wordcmp
, /* Comparing this word */
37 st_wordskip
, /* Miscompare, skip */
38 } state
= st_wordstart
;
41 return -1; /* No command line */
44 * This 'pos' check ensures we do not overrun
45 * a non-NULL-terminated 'cmdline'
47 while (pos
< max_cmdline_size
) {
48 c
= *(char *)cmdline
++;
55 else if (myisspace(c
))
66 * We matched all the way to the end of the
67 * option we were looking for. If the
68 * command-line has a space _or_ ends, then
71 if (!c
|| myisspace(c
))
74 * We hit the end of the option, but _not_
75 * the end of a word on the cmdline. Not
80 * Hit the NULL terminator on the end of
84 } else if (c
== *opptr
++) {
86 * We are currently matching, so continue
87 * to the next character on the cmdline.
97 else if (myisspace(c
))
103 return 0; /* Buffer overrun */
107 * Find a non-boolean option (i.e. option=argument). In accordance with
108 * standard Linux practice, if this option is repeated, this returns the
109 * last instance on the command line.
111 * @cmdline: the cmdline string
112 * @max_cmdline_size: the maximum size of cmdline
113 * @option: option string to look for
114 * @buffer: memory buffer to return the option argument
115 * @bufsize: size of the supplied memory buffer
117 * Returns the length of the argument (regardless of if it was
118 * truncated to fit in the buffer), or -1 on not found.
121 __cmdline_find_option(const char *cmdline
, int max_cmdline_size
,
122 const char *option
, char *buffer
, int bufsize
)
125 int pos
= 0, len
= -1;
126 const char *opptr
= NULL
;
127 char *bufptr
= buffer
;
129 st_wordstart
= 0, /* Start of word/after whitespace */
130 st_wordcmp
, /* Comparing this word */
131 st_wordskip
, /* Miscompare, skip */
132 st_bufcpy
, /* Copying this to buffer */
133 } state
= st_wordstart
;
136 return -1; /* No command line */
139 * This 'pos' check ensures we do not overrun
140 * a non-NULL-terminated 'cmdline'
142 while (pos
++ < max_cmdline_size
) {
143 c
= *(char *)cmdline
++;
157 if ((c
== '=') && !*opptr
) {
159 * We matched all the way to the end of the
160 * option we were looking for, prepare to
167 } else if (c
== *opptr
++) {
169 * We are currently matching, so continue
170 * to the next character on the cmdline.
179 state
= st_wordstart
;
184 state
= st_wordstart
;
187 * Increment len, but don't overrun the
188 * supplied buffer and leave room for the
204 int cmdline_find_option_bool(const char *cmdline
, const char *option
)
206 return __cmdline_find_option_bool(cmdline
, COMMAND_LINE_SIZE
, option
);
209 int cmdline_find_option(const char *cmdline
, const char *option
, char *buffer
,
212 return __cmdline_find_option(cmdline
, COMMAND_LINE_SIZE
, option
,