cmdline.c 5.1 KB

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