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