cmdline.c 4.7 KB

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