GNU Linux-libre 4.4.285-gnu1
[releases.git] / arch / x86 / lib / cmdline.c
1 /*
2  * This file is part of the Linux kernel, and is made available under
3  * the terms of the GNU General Public License version 2.
4  *
5  * Misc librarized functions for cmdline poking.
6  */
7 #include <linux/kernel.h>
8 #include <linux/string.h>
9 #include <linux/ctype.h>
10 #include <asm/setup.h>
11
12 static inline int myisspace(u8 c)
13 {
14         return c <= ' ';        /* Close enough approximation */
15 }
16
17 /**
18  * Find a boolean option (like quiet,noapic,nosmp....)
19  *
20  * @cmdline: the cmdline string
21  * @option: option string to look for
22  *
23  * Returns the position of that @option (starts counting with 1)
24  * or 0 on not found.  @option will only be found if it is found
25  * as an entire word in @cmdline.  For instance, if @option="car"
26  * then a cmdline which contains "cart" will not match.
27  */
28 int cmdline_find_option_bool(const char *cmdline, const char *option)
29 {
30         char c;
31         int pos = 0, wstart = 0;
32         const char *opptr = NULL;
33         enum {
34                 st_wordstart = 0,       /* Start of word/after whitespace */
35                 st_wordcmp,     /* Comparing this word */
36                 st_wordskip,    /* Miscompare, skip */
37         } state = st_wordstart;
38
39         if (!cmdline)
40                 return -1;      /* No command line */
41
42         if (!strlen(cmdline))
43                 return 0;
44
45         /*
46          * This 'pos' check ensures we do not overrun
47          * a non-NULL-terminated 'cmdline'
48          */
49         while (pos < COMMAND_LINE_SIZE) {
50                 c = *(char *)cmdline++;
51                 pos++;
52
53                 switch (state) {
54                 case st_wordstart:
55                         if (!c)
56                                 return 0;
57                         else if (myisspace(c))
58                                 break;
59
60                         state = st_wordcmp;
61                         opptr = option;
62                         wstart = pos;
63                         /* fall through */
64
65                 case st_wordcmp:
66                         if (!*opptr) {
67                                 /*
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
71                                  * we matched!
72                                  */
73                                 if (!c || myisspace(c))
74                                         return wstart;
75                                 else
76                                         state = st_wordskip;
77                         } else if (!c) {
78                                 /*
79                                  * Hit the NULL terminator on the end of
80                                  * cmdline.
81                                  */
82                                 return 0;
83                         } else if (c != *opptr++) {
84                                 state = st_wordskip;
85                         }
86                         break;
87
88                 case st_wordskip:
89                         if (!c)
90                                 return 0;
91                         else if (myisspace(c))
92                                 state = st_wordstart;
93                         break;
94                 }
95         }
96
97         return 0;       /* Buffer overrun */
98 }
99
100 /*
101  * Find a non-boolean option (i.e. option=argument). In accordance with
102  * standard Linux practice, if this option is repeated, this returns the
103  * last instance on the command line.
104  *
105  * @cmdline: the cmdline string
106  * @max_cmdline_size: the maximum size of cmdline
107  * @option: option string to look for
108  * @buffer: memory buffer to return the option argument
109  * @bufsize: size of the supplied memory buffer
110  *
111  * Returns the length of the argument (regardless of if it was
112  * truncated to fit in the buffer), or -1 on not found.
113  */
114 static int
115 __cmdline_find_option(const char *cmdline, int max_cmdline_size,
116                       const char *option, char *buffer, int bufsize)
117 {
118         char c;
119         int pos = 0, len = -1;
120         const char *opptr = NULL;
121         char *bufptr = buffer;
122         enum {
123                 st_wordstart = 0,       /* Start of word/after whitespace */
124                 st_wordcmp,     /* Comparing this word */
125                 st_wordskip,    /* Miscompare, skip */
126                 st_bufcpy,      /* Copying this to buffer */
127         } state = st_wordstart;
128
129         if (!cmdline)
130                 return -1;      /* No command line */
131
132         /*
133          * This 'pos' check ensures we do not overrun
134          * a non-NULL-terminated 'cmdline'
135          */
136         while (pos++ < max_cmdline_size) {
137                 c = *(char *)cmdline++;
138                 if (!c)
139                         break;
140
141                 switch (state) {
142                 case st_wordstart:
143                         if (myisspace(c))
144                                 break;
145
146                         state = st_wordcmp;
147                         opptr = option;
148                         /* fall through */
149
150                 case st_wordcmp:
151                         if ((c == '=') && !*opptr) {
152                                 /*
153                                  * We matched all the way to the end of the
154                                  * option we were looking for, prepare to
155                                  * copy the argument.
156                                  */
157                                 len = 0;
158                                 bufptr = buffer;
159                                 state = st_bufcpy;
160                                 break;
161                         } else if (c == *opptr++) {
162                                 /*
163                                  * We are currently matching, so continue
164                                  * to the next character on the cmdline.
165                                  */
166                                 break;
167                         }
168                         state = st_wordskip;
169                         /* fall through */
170
171                 case st_wordskip:
172                         if (myisspace(c))
173                                 state = st_wordstart;
174                         break;
175
176                 case st_bufcpy:
177                         if (myisspace(c)) {
178                                 state = st_wordstart;
179                         } else {
180                                 /*
181                                  * Increment len, but don't overrun the
182                                  * supplied buffer and leave room for the
183                                  * NULL terminator.
184                                  */
185                                 if (++len < bufsize)
186                                         *bufptr++ = c;
187                         }
188                         break;
189                 }
190         }
191
192         if (bufsize)
193                 *bufptr = '\0';
194
195         return len;
196 }
197
198 int cmdline_find_option(const char *cmdline, const char *option, char *buffer,
199                         int bufsize)
200 {
201         return __cmdline_find_option(cmdline, COMMAND_LINE_SIZE, option,
202                                      buffer, bufsize);
203 }