1 From: Paul Burton <paul.burton@mips.com>
2 Date: Wed, 9 Oct 2019 23:09:45 +0000
3 Subject: MIPS: cmdline: Clean up boot_command_line initialization
5 Our current code to initialize boot_command_line is a mess. Some of this
6 is due to the addition of too many options over the years, and some of
7 this is due to workarounds for early_init_dt_scan_chosen() performing
8 actions specific to options from other architectures that probably
9 shouldn't be in generic code.
11 Clean this up by introducing a new bootcmdline_init() function that
12 simplifies the initialization somewhat. The major changes are:
14 - Because bootcmdline_init() is a function it can return early in the
15 CONFIG_CMDLINE_OVERRIDE case.
17 - We clear boot_command_line rather than inheriting whatever
18 early_init_dt_scan_chosen() may have left us. This means we no longer
19 need to set boot_command_line to a space character in an attempt to
20 prevent early_init_dt_scan_chosen() from copying CONFIG_CMDLINE into
21 boot_command_line without us knowing about it.
23 - Indirection via USE_PROM_CMDLINE, USE_DTB_CMDLINE, EXTEND_WITH_PROM &
24 BUILTIN_EXTEND_WITH_PROM macros is removed; they seemingly served only
25 to obfuscate the code.
27 - The logic is cleaner, clearer & commented.
29 Two minor drawbacks of this approach are:
31 1) We call of_scan_flat_dt(), which means we scan through the DT again.
32 The overhead is fairly minimal & shouldn't be noticeable.
34 2) cmdline_scan_chosen() duplicates a small amount of the logic from
35 early_init_dt_scan_chosen(). Alternatives might be to allow the
36 generic FDT code to keep & expose a copy of the arguments taken from
37 the /chosen node's bootargs property, or to introduce a function like
38 early_init_dt_scan_chosen() that retrieves them without modification
39 to handle CONFIG_CMDLINE. Neither of these sounds particularly
40 cleaner though, and this way we at least keep the extra work in
43 Origin: upstream, https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7784cac697351f0cc0a4bb619594c0c99348c5aa
44 Signed-off-by: Paul Burton <paul.burton@mips.com>
45 Cc: linux-mips@vger.kernel.org
47 diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c
48 index b8884de89c81e1b444b218a15519556b4a374089..e56dd6c25d46336fcb9af21326f697d0c37aac1c 100644
49 --- a/arch/mips/kernel/setup.c
50 +++ b/arch/mips/kernel/setup.c
51 @@ -538,11 +538,88 @@ static void __init check_kernel_sections_mem(void)
55 -#define USE_PROM_CMDLINE IS_ENABLED(CONFIG_MIPS_CMDLINE_FROM_BOOTLOADER)
56 -#define USE_DTB_CMDLINE IS_ENABLED(CONFIG_MIPS_CMDLINE_FROM_DTB)
57 -#define EXTEND_WITH_PROM IS_ENABLED(CONFIG_MIPS_CMDLINE_DTB_EXTEND)
58 -#define BUILTIN_EXTEND_WITH_PROM \
59 - IS_ENABLED(CONFIG_MIPS_CMDLINE_BUILTIN_EXTEND)
60 +static void __init bootcmdline_append(const char *s, size_t max)
65 + if (boot_command_line[0])
66 + strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
68 + strlcat(boot_command_line, s, max);
71 +static int __init bootcmdline_scan_chosen(unsigned long node, const char *uname,
72 + int depth, void *data)
74 + bool *dt_bootargs = data;
78 + if (depth != 1 || !data ||
79 + (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
82 + p = of_get_flat_dt_prop(node, "bootargs", &l);
83 + if (p != NULL && l > 0) {
84 + bootcmdline_append(p, min(l, COMMAND_LINE_SIZE));
85 + *dt_bootargs = true;
91 +static void __init bootcmdline_init(char **cmdline_p)
93 + bool dt_bootargs = false;
96 + * If CMDLINE_OVERRIDE is enabled then initializing the command line is
97 + * trivial - we simply use the built-in command line unconditionally &
100 + if (IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
101 + strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
106 + * If the user specified a built-in command line &
107 + * MIPS_CMDLINE_BUILTIN_EXTEND, then the built-in command line is
108 + * prepended to arguments from the bootloader or DT so we'll copy them
109 + * to the start of boot_command_line here. Otherwise, empty
110 + * boot_command_line to undo anything early_init_dt_scan_chosen() did.
112 + if (IS_ENABLED(CONFIG_MIPS_CMDLINE_BUILTIN_EXTEND))
113 + strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
115 + boot_command_line[0] = 0;
118 + * If we're configured to take boot arguments from DT, look for those
121 + if (IS_ENABLED(CONFIG_MIPS_CMDLINE_FROM_DTB))
122 + of_scan_flat_dt(bootcmdline_scan_chosen, &dt_bootargs);
125 + * If we didn't get any arguments from DT (regardless of whether that's
126 + * because we weren't configured to look for them, or because we looked
127 + * & found none) then we'll take arguments from the bootloader.
128 + * plat_mem_setup() should have filled arcs_cmdline with arguments from
131 + if (IS_ENABLED(CONFIG_MIPS_CMDLINE_DTB_EXTEND) || !dt_bootargs)
132 + bootcmdline_append(arcs_cmdline, COMMAND_LINE_SIZE);
135 + * If the user specified a built-in command line & we didn't already
136 + * prepend it, we append it to boot_command_line here.
138 + if (IS_ENABLED(CONFIG_CMDLINE_BOOL) &&
139 + !IS_ENABLED(CONFIG_MIPS_CMDLINE_BUILTIN_EXTEND))
140 + bootcmdline_append(builtin_cmdline, COMMAND_LINE_SIZE);
144 * arch_mem_init - initialize memory management subsystem
145 @@ -570,48 +647,12 @@ static void __init arch_mem_init(char **cmdline_p)
147 extern void plat_mem_setup(void);
150 - * Initialize boot_command_line to an innocuous but non-empty string in
151 - * order to prevent early_init_dt_scan_chosen() from copying
152 - * CONFIG_CMDLINE into it without our knowledge. We handle
153 - * CONFIG_CMDLINE ourselves below & don't want to duplicate its
154 - * content because repeating arguments can be problematic.
156 - strlcpy(boot_command_line, " ", COMMAND_LINE_SIZE);
158 /* call board setup routine */
160 memblock_set_bottom_up(true);
162 -#if defined(CONFIG_CMDLINE_BOOL) && defined(CONFIG_CMDLINE_OVERRIDE)
163 - strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
165 - if ((USE_PROM_CMDLINE && arcs_cmdline[0]) ||
166 - (USE_DTB_CMDLINE && !boot_command_line[0]))
167 - strlcpy(boot_command_line, arcs_cmdline, COMMAND_LINE_SIZE);
169 - if (EXTEND_WITH_PROM && arcs_cmdline[0]) {
170 - if (boot_command_line[0])
171 - strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
172 - strlcat(boot_command_line, arcs_cmdline, COMMAND_LINE_SIZE);
175 -#if defined(CONFIG_CMDLINE_BOOL)
176 - if (builtin_cmdline[0]) {
177 - if (boot_command_line[0])
178 - strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
179 - strlcat(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
182 - if (BUILTIN_EXTEND_WITH_PROM && arcs_cmdline[0]) {
183 - if (boot_command_line[0])
184 - strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
185 - strlcat(boot_command_line, arcs_cmdline, COMMAND_LINE_SIZE);
189 + bootcmdline_init(cmdline_p);
190 strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
192 *cmdline_p = command_line;