cmdline.c 4.8 KB

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