cmdline.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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(const char *option, char *buffer, int bufsize)
  27. {
  28. u32 cmdline_ptr = boot_params.hdr.cmd_line_ptr;
  29. addr_t cptr;
  30. char c;
  31. int len = -1;
  32. const char *opptr = NULL;
  33. char *bufptr = buffer;
  34. enum {
  35. st_wordstart, /* Start of word/after whitespace */
  36. st_wordcmp, /* Comparing this word */
  37. st_wordskip, /* Miscompare, skip */
  38. st_bufcpy /* Copying this to buffer */
  39. } state = st_wordstart;
  40. if (!cmdline_ptr || cmdline_ptr >= 0x100000)
  41. return -1; /* No command line, or inaccessible */
  42. cptr = cmdline_ptr & 0xf;
  43. set_fs(cmdline_ptr >> 4);
  44. while (cptr < 0x10000 && (c = rdfs8(cptr++))) {
  45. switch (state) {
  46. case st_wordstart:
  47. if (myisspace(c))
  48. break;
  49. /* else */
  50. state = st_wordcmp;
  51. opptr = option;
  52. /* fall through */
  53. case st_wordcmp:
  54. if (c == '=' && !*opptr) {
  55. len = 0;
  56. bufptr = buffer;
  57. state = st_bufcpy;
  58. } else if (myisspace(c)) {
  59. state = st_wordstart;
  60. } else if (c != *opptr++) {
  61. state = st_wordskip;
  62. }
  63. break;
  64. case st_wordskip:
  65. if (myisspace(c))
  66. state = st_wordstart;
  67. break;
  68. case st_bufcpy:
  69. if (myisspace(c)) {
  70. state = st_wordstart;
  71. } else {
  72. if (len < bufsize-1)
  73. *bufptr++ = c;
  74. len++;
  75. }
  76. break;
  77. }
  78. }
  79. if (bufsize)
  80. *bufptr = '\0';
  81. return len;
  82. }
  83. /*
  84. * Find a boolean option (like quiet,noapic,nosmp....)
  85. *
  86. * Returns the position of that option (starts counting with 1)
  87. * or 0 on not found
  88. */
  89. int cmdline_find_option_bool(const char *option)
  90. {
  91. u32 cmdline_ptr = boot_params.hdr.cmd_line_ptr;
  92. addr_t cptr;
  93. char c;
  94. int pos = 0, wstart = 0;
  95. const char *opptr = NULL;
  96. enum {
  97. st_wordstart, /* Start of word/after whitespace */
  98. st_wordcmp, /* Comparing this word */
  99. st_wordskip, /* Miscompare, skip */
  100. } state = st_wordstart;
  101. if (!cmdline_ptr || cmdline_ptr >= 0x100000)
  102. return -1; /* No command line, or inaccessible */
  103. cptr = cmdline_ptr & 0xf;
  104. set_fs(cmdline_ptr >> 4);
  105. while (cptr < 0x10000) {
  106. c = rdfs8(cptr++);
  107. pos++;
  108. switch (state) {
  109. case st_wordstart:
  110. if (!c)
  111. return 0;
  112. else if (myisspace(c))
  113. break;
  114. state = st_wordcmp;
  115. opptr = option;
  116. wstart = pos;
  117. /* fall through */
  118. case st_wordcmp:
  119. if (!*opptr)
  120. if (!c || myisspace(c))
  121. return wstart;
  122. else
  123. state = st_wordskip;
  124. else if (!c)
  125. return 0;
  126. else if (c != *opptr++)
  127. state = st_wordskip;
  128. break;
  129. case st_wordskip:
  130. if (!c)
  131. return 0;
  132. else if (myisspace(c))
  133. state = st_wordstart;
  134. break;
  135. }
  136. }
  137. return 0; /* Buffer overrun */
  138. }