cmdline.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. }