1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Tom Lendacky <thomas.lendacky@amd.com>
3 Date: Mon, 17 Jul 2017 16:10:33 -0500
4 Subject: [PATCH] x86/boot: Add early cmdline parsing for options with
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
12 Add a cmdline_find_option() function to look for cmdline options that
13 take arguments. The argument is returned in a supplied buffer and the
14 argument length (regardless of whether it fits in the supplied buffer)
15 is returned, with -1 indicating not found.
17 Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
18 Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
19 Cc: Alexander Potapenko <glider@google.com>
20 Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
21 Cc: Andy Lutomirski <luto@kernel.org>
22 Cc: Arnd Bergmann <arnd@arndb.de>
23 Cc: Borislav Petkov <bp@alien8.de>
24 Cc: Brijesh Singh <brijesh.singh@amd.com>
25 Cc: Dave Young <dyoung@redhat.com>
26 Cc: Dmitry Vyukov <dvyukov@google.com>
27 Cc: Jonathan Corbet <corbet@lwn.net>
28 Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
29 Cc: Larry Woodman <lwoodman@redhat.com>
30 Cc: Linus Torvalds <torvalds@linux-foundation.org>
31 Cc: Matt Fleming <matt@codeblueprint.co.uk>
32 Cc: Michael S. Tsirkin <mst@redhat.com>
33 Cc: Paolo Bonzini <pbonzini@redhat.com>
34 Cc: Peter Zijlstra <peterz@infradead.org>
35 Cc: Radim Krčmář <rkrcmar@redhat.com>
36 Cc: Rik van Riel <riel@redhat.com>
37 Cc: Toshimitsu Kani <toshi.kani@hpe.com>
38 Cc: kasan-dev@googlegroups.com
39 Cc: kvm@vger.kernel.org
40 Cc: linux-arch@vger.kernel.org
41 Cc: linux-doc@vger.kernel.org
42 Cc: linux-efi@vger.kernel.org
43 Cc: linux-mm@kvack.org
44 Link: http://lkml.kernel.org/r/36b5f97492a9745dce27682305f990fc20e5cf8a.1500319216.git.thomas.lendacky@amd.com
45 Signed-off-by: Ingo Molnar <mingo@kernel.org>
46 (cherry picked from commit e505371dd83963caae1a37ead9524e8d997341be)
47 Signed-off-by: Andy Whitcroft <apw@kathleen.maas>
48 (cherry picked from commit 37569cd003aa69a57d5666530436c2d973a57b26)
49 Signed-off-by: Andy Whitcroft <apw@canonical.com>
50 Signed-off-by: Kleber Sacilotto de Souza <kleber.souza@canonical.com>
51 (cherry picked from commit b9f03418aa9b8ecbb1c7f32ac2bfe68fd21de4f5)
52 Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
54 arch/x86/include/asm/cmdline.h | 2 +
55 arch/x86/lib/cmdline.c | 105 +++++++++++++++++++++++++++++++++++++++++
56 2 files changed, 107 insertions(+)
58 diff --git a/arch/x86/include/asm/cmdline.h b/arch/x86/include/asm/cmdline.h
59 index e01f7f7ccb0c..84ae170bc3d0 100644
60 --- a/arch/x86/include/asm/cmdline.h
61 +++ b/arch/x86/include/asm/cmdline.h
63 #define _ASM_X86_CMDLINE_H
65 int cmdline_find_option_bool(const char *cmdline_ptr, const char *option);
66 +int cmdline_find_option(const char *cmdline_ptr, const char *option,
67 + char *buffer, int bufsize);
69 #endif /* _ASM_X86_CMDLINE_H */
70 diff --git a/arch/x86/lib/cmdline.c b/arch/x86/lib/cmdline.c
71 index 5cc78bf57232..3261abb21ef4 100644
72 --- a/arch/x86/lib/cmdline.c
73 +++ b/arch/x86/lib/cmdline.c
74 @@ -104,7 +104,112 @@ __cmdline_find_option_bool(const char *cmdline, int max_cmdline_size,
75 return 0; /* Buffer overrun */
79 + * Find a non-boolean option (i.e. option=argument). In accordance with
80 + * standard Linux practice, if this option is repeated, this returns the
81 + * last instance on the command line.
83 + * @cmdline: the cmdline string
84 + * @max_cmdline_size: the maximum size of cmdline
85 + * @option: option string to look for
86 + * @buffer: memory buffer to return the option argument
87 + * @bufsize: size of the supplied memory buffer
89 + * Returns the length of the argument (regardless of if it was
90 + * truncated to fit in the buffer), or -1 on not found.
93 +__cmdline_find_option(const char *cmdline, int max_cmdline_size,
94 + const char *option, char *buffer, int bufsize)
97 + int pos = 0, len = -1;
98 + const char *opptr = NULL;
99 + char *bufptr = buffer;
101 + st_wordstart = 0, /* Start of word/after whitespace */
102 + st_wordcmp, /* Comparing this word */
103 + st_wordskip, /* Miscompare, skip */
104 + st_bufcpy, /* Copying this to buffer */
105 + } state = st_wordstart;
108 + return -1; /* No command line */
111 + * This 'pos' check ensures we do not overrun
112 + * a non-NULL-terminated 'cmdline'
114 + while (pos++ < max_cmdline_size) {
115 + c = *(char *)cmdline++;
124 + state = st_wordcmp;
129 + if ((c == '=') && !*opptr) {
131 + * We matched all the way to the end of the
132 + * option we were looking for, prepare to
133 + * copy the argument.
139 + } else if (c == *opptr++) {
141 + * We are currently matching, so continue
142 + * to the next character on the cmdline.
146 + state = st_wordskip;
151 + state = st_wordstart;
155 + if (myisspace(c)) {
156 + state = st_wordstart;
159 + * Increment len, but don't overrun the
160 + * supplied buffer and leave room for the
163 + if (++len < bufsize)
176 int cmdline_find_option_bool(const char *cmdline, const char *option)
178 return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE, option);
181 +int cmdline_find_option(const char *cmdline, const char *option, char *buffer,
184 + return __cmdline_find_option(cmdline, COMMAND_LINE_SIZE, option,