cmdline.c 3.1 KB

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