cmdline.c 3.4 KB

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