4cadd5a86f58a81b00bb0a6e8dce3474e8d1c5a2
[openwrt/staging/ldir.git] / target / linux / ath79 / patches-5.4 / 0001-MIPS-cmdline-Clean-up-boot_command_line-initializati.patch
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
4
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.
10
11 Clean this up by introducing a new bootcmdline_init() function that
12 simplifies the initialization somewhat. The major changes are:
13
14 - Because bootcmdline_init() is a function it can return early in the
15 CONFIG_CMDLINE_OVERRIDE case.
16
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.
22
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.
26
27 - The logic is cleaner, clearer & commented.
28
29 Two minor drawbacks of this approach are:
30
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.
33
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
41 arch/mips.
42
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
46
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)
52 }
53 }
54
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)
61 +{
62 + if (!s[0] || !max)
63 + return;
64 +
65 + if (boot_command_line[0])
66 + strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
67 +
68 + strlcat(boot_command_line, s, max);
69 +}
70 +
71 +static int __init bootcmdline_scan_chosen(unsigned long node, const char *uname,
72 + int depth, void *data)
73 +{
74 + bool *dt_bootargs = data;
75 + const char *p;
76 + int l;
77 +
78 + if (depth != 1 || !data ||
79 + (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
80 + return 0;
81 +
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;
86 + }
87 +
88 + return 1;
89 +}
90 +
91 +static void __init bootcmdline_init(char **cmdline_p)
92 +{
93 + bool dt_bootargs = false;
94 +
95 + /*
96 + * If CMDLINE_OVERRIDE is enabled then initializing the command line is
97 + * trivial - we simply use the built-in command line unconditionally &
98 + * unmodified.
99 + */
100 + if (IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
101 + strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
102 + return;
103 + }
104 +
105 + /*
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.
111 + */
112 + if (IS_ENABLED(CONFIG_MIPS_CMDLINE_BUILTIN_EXTEND))
113 + strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
114 + else
115 + boot_command_line[0] = 0;
116 +
117 + /*
118 + * If we're configured to take boot arguments from DT, look for those
119 + * now.
120 + */
121 + if (IS_ENABLED(CONFIG_MIPS_CMDLINE_FROM_DTB))
122 + of_scan_flat_dt(bootcmdline_scan_chosen, &dt_bootargs);
123 +
124 + /*
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
129 + * the bootloader.
130 + */
131 + if (IS_ENABLED(CONFIG_MIPS_CMDLINE_DTB_EXTEND) || !dt_bootargs)
132 + bootcmdline_append(arcs_cmdline, COMMAND_LINE_SIZE);
133 +
134 + /*
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.
137 + */
138 + if (IS_ENABLED(CONFIG_CMDLINE_BOOL) &&
139 + !IS_ENABLED(CONFIG_MIPS_CMDLINE_BUILTIN_EXTEND))
140 + bootcmdline_append(builtin_cmdline, COMMAND_LINE_SIZE);
141 +}
142
143 /*
144 * arch_mem_init - initialize memory management subsystem
145 @@ -570,48 +647,12 @@ static void __init arch_mem_init(char **cmdline_p)
146 {
147 extern void plat_mem_setup(void);
148
149 - /*
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.
155 - */
156 - strlcpy(boot_command_line, " ", COMMAND_LINE_SIZE);
157 -
158 /* call board setup routine */
159 plat_mem_setup();
160 memblock_set_bottom_up(true);
161
162 -#if defined(CONFIG_CMDLINE_BOOL) && defined(CONFIG_CMDLINE_OVERRIDE)
163 - strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
164 -#else
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);
168 -
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);
173 - }
174 -
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);
180 - }
181 -
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);
186 - }
187 -#endif
188 -#endif
189 + bootcmdline_init(cmdline_p);
190 strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
191 -
192 *cmdline_p = command_line;
193
194 parse_early_param();