cmdline.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. *
  6. * This file is part of the Linux kernel, and is made available under
  7. * the terms of the GNU General Public License version 2.
  8. *
  9. * ----------------------------------------------------------------------- */
  10. /*
  11. * Simple command-line parser for early boot.
  12. */
  13. #include "boot.h"
  14. static inline int myisspace(u8 c)
  15. {
  16. return c <= ' '; /* Close enough approximation */
  17. }
  18. /*
  19. * Find a non-boolean option, that is, "option=argument". In accordance
  20. * with standard Linux practice, if this option is repeated, this returns
  21. * the last instance on the command line.
  22. *
  23. * Returns the length of the argument (regardless of if it was
  24. * truncated to fit in the buffer), or -1 on not found.
  25. */
  26. int __cmdline_find_option(unsigned long cmdline_ptr, const char *option, char *buffer, int bufsize)
  27. {
  28. addr_t cptr;
  29. char c;
  30. int len = -1;
  31. const char *opptr = NULL;
  32. char *bufptr = buffer;
  33. enum {
  34. st_wordstart, /* Start of word/after whitespace */
  35. st_wordcmp, /* Comparing this word */
  36. st_wordskip, /* Miscompare, skip */
  37. st_bufcpy /* Copying this to buffer */
  38. } state = st_wordstart;
  39. if (!cmdline_ptr)
  40. return -1; /* No command line */
  41. cptr = cmdline_ptr & 0xf;
  42. set_fs(cmdline_ptr >> 4);
  43. while (cptr < 0x10000 && (c = rdfs8(cptr++))) {
  44. switch (state) {
  45. case st_wordstart:
  46. if (myisspace(c))
  47. break;
  48. /* else */
  49. state = st_wordcmp;
  50. opptr = option;
  51. /* fall through */
  52. case st_wordcmp:
  53. if (c == '=' && !*opptr) {
  54. len = 0;
  55. bufptr = buffer;
  56. state = st_bufcpy;
  57. } else if (myisspace(c)) {
  58. state = st_wordstart;
  59. } else if (c != *opptr++) {
  60. state = st_wordskip;
  61. }
  62. break;
  63. case st_wordskip:
  64. if (myisspace(c))
  65. state = st_wordstart;
  66. break;
  67. case st_bufcpy:
  68. if (myisspace(c)) {
  69. state = st_wordstart;
  70. } else {
  71. if (len < bufsize-1)
  72. *bufptr++ = c;
  73. len++;
  74. }
  75. break;
  76. }
  77. }
  78. if (bufsize)
  79. *bufptr = '\0';
  80. return len;
  81. }
  82. /*
  83. * Find a boolean option (like quiet,noapic,nosmp....)
  84. *
  85. * Returns the position of that option (starts counting with 1)
  86. * or 0 on not found
  87. */
  88. int __cmdline_find_option_bool(unsigned long cmdline_ptr, const char *option)
  89. {
  90. addr_t cptr;
  91. char c;
  92. int pos = 0, wstart = 0;
  93. const char *opptr = NULL;
  94. enum {
  95. st_wordstart, /* Start of word/after whitespace */
  96. st_wordcmp, /* Comparing this word */
  97. st_wordskip, /* Miscompare, skip */
  98. } state = st_wordstart;
  99. if (!cmdline_ptr)
  100. return -1; /* No command line */
  101. cptr = cmdline_ptr & 0xf;
  102. set_fs(cmdline_ptr >> 4);
  103. while (cptr < 0x10000) {
  104. c = rdfs8(cptr++);
  105. pos++;
  106. switch (state) {
  107. case st_wordstart:
  108. if (!c)
  109. return 0;
  110. else if (myisspace(c))
  111. break;
  112. state = st_wordcmp;
  113. opptr = option;
  114. wstart = pos;
  115. /* fall through */
  116. case st_wordcmp:
  117. if (!*opptr)
  118. if (!c || myisspace(c))
  119. return wstart;
  120. else
  121. state = st_wordskip;
  122. else if (!c)
  123. return 0;
  124. else if (c != *opptr++)
  125. state = st_wordskip;
  126. break;
  127. case st_wordskip:
  128. if (!c)
  129. return 0;
  130. else if (myisspace(c))
  131. state = st_wordstart;
  132. break;
  133. }
  134. }
  135. return 0; /* Buffer overrun */
  136. }