Prepare v2023.10
[platform/kernel/u-boot.git] / arch / mips / lib / bootm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2003
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 #include <common.h>
8 #include <bootstage.h>
9 #include <env.h>
10 #include <image.h>
11 #include <fdt_support.h>
12 #include <lmb.h>
13 #include <log.h>
14 #include <asm/addrspace.h>
15 #include <asm/global_data.h>
16 #include <asm/io.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 #define LINUX_MAX_ENVS          256
21 #define LINUX_MAX_ARGS          256
22
23 static int linux_argc;
24 static char **linux_argv;
25 static char *linux_argp;
26
27 static char **linux_env;
28 static char *linux_env_p;
29 static int linux_env_idx;
30
31 static ulong arch_get_sp(void)
32 {
33         ulong ret;
34
35         __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
36
37         return ret;
38 }
39
40 void arch_lmb_reserve(struct lmb *lmb)
41 {
42         arch_lmb_reserve_generic(lmb, arch_get_sp(), gd->ram_top, 4096);
43 }
44
45 static void linux_cmdline_init(void)
46 {
47         linux_argc = 1;
48         linux_argv = (char **)CKSEG1ADDR(gd->bd->bi_boot_params);
49         linux_argv[0] = 0;
50         linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
51 }
52
53 static void linux_cmdline_set(const char *value, size_t len)
54 {
55         linux_argv[linux_argc] = linux_argp;
56         memcpy(linux_argp, value, len);
57         linux_argp[len] = 0;
58
59         linux_argp += len + 1;
60         linux_argc++;
61 }
62
63 static void linux_cmdline_dump(void)
64 {
65         int i;
66
67         debug("## cmdline argv at 0x%p, argp at 0x%p\n",
68               linux_argv, linux_argp);
69
70         for (i = 1; i < linux_argc; i++)
71                 debug("   arg %03d: %s\n", i, linux_argv[i]);
72 }
73
74 static void linux_cmdline_legacy(struct bootm_headers *images)
75 {
76         const char *bootargs, *next, *quote;
77
78         linux_cmdline_init();
79
80         bootargs = env_get("bootargs");
81         if (!bootargs)
82                 return;
83
84         next = bootargs;
85
86         while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
87                 quote = strchr(bootargs, '"');
88                 next = strchr(bootargs, ' ');
89
90                 while (next && quote && quote < next) {
91                         /*
92                          * we found a left quote before the next blank
93                          * now we have to find the matching right quote
94                          */
95                         next = strchr(quote + 1, '"');
96                         if (next) {
97                                 quote = strchr(next + 1, '"');
98                                 next = strchr(next + 1, ' ');
99                         }
100                 }
101
102                 if (!next)
103                         next = bootargs + strlen(bootargs);
104
105                 linux_cmdline_set(bootargs, next - bootargs);
106
107                 if (*next)
108                         next++;
109
110                 bootargs = next;
111         }
112 }
113
114 static void linux_cmdline_append(struct bootm_headers *images)
115 {
116         char buf[24];
117         ulong mem, rd_start, rd_size;
118
119         /* append mem */
120         mem = gd->ram_size >> 20;
121         sprintf(buf, "mem=%luM", mem);
122         linux_cmdline_set(buf, strlen(buf));
123
124         /* append rd_start and rd_size */
125         rd_start = images->initrd_start;
126         rd_size = images->initrd_end - images->initrd_start;
127
128         if (rd_size) {
129                 sprintf(buf, "rd_start=0x%08lX", rd_start);
130                 linux_cmdline_set(buf, strlen(buf));
131                 sprintf(buf, "rd_size=0x%lX", rd_size);
132                 linux_cmdline_set(buf, strlen(buf));
133         }
134 }
135
136 static void linux_env_init(void)
137 {
138         linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
139         linux_env[0] = 0;
140         linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
141         linux_env_idx = 0;
142 }
143
144 static void linux_env_set(const char *env_name, const char *env_val)
145 {
146         if (linux_env_idx < LINUX_MAX_ENVS - 1) {
147                 linux_env[linux_env_idx] = linux_env_p;
148
149                 strcpy(linux_env_p, env_name);
150                 linux_env_p += strlen(env_name);
151
152                 if (CONFIG_IS_ENABLED(MALTA)) {
153                         linux_env_p++;
154                         linux_env[++linux_env_idx] = linux_env_p;
155                 } else {
156                         *linux_env_p++ = '=';
157                 }
158
159                 strcpy(linux_env_p, env_val);
160                 linux_env_p += strlen(env_val);
161
162                 linux_env_p++;
163                 linux_env[++linux_env_idx] = 0;
164         }
165 }
166
167 static void linux_env_legacy(struct bootm_headers *images)
168 {
169         char env_buf[12];
170         const char *cp;
171         ulong rd_start, rd_size;
172
173         if (CONFIG_IS_ENABLED(MEMSIZE_IN_BYTES)) {
174                 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
175                 debug("## Giving linux memsize in bytes, %lu\n",
176                       (ulong)gd->ram_size);
177         } else {
178                 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
179                 debug("## Giving linux memsize in MB, %lu\n",
180                       (ulong)(gd->ram_size >> 20));
181         }
182
183         rd_start = CKSEG1ADDR(images->initrd_start);
184         rd_size = images->initrd_end - images->initrd_start;
185
186         linux_env_init();
187
188         linux_env_set("memsize", env_buf);
189
190         sprintf(env_buf, "0x%08lX", rd_start);
191         linux_env_set("initrd_start", env_buf);
192
193         sprintf(env_buf, "0x%lX", rd_size);
194         linux_env_set("initrd_size", env_buf);
195
196         sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
197         linux_env_set("flash_start", env_buf);
198
199         sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
200         linux_env_set("flash_size", env_buf);
201
202         cp = env_get("ethaddr");
203         if (cp)
204                 linux_env_set("ethaddr", cp);
205
206         cp = env_get("eth1addr");
207         if (cp)
208                 linux_env_set("eth1addr", cp);
209
210         if (CONFIG_IS_ENABLED(MALTA)) {
211                 sprintf(env_buf, "%un8r", gd->baudrate);
212                 linux_env_set("modetty0", env_buf);
213         }
214 }
215
216 static int boot_reloc_fdt(struct bootm_headers *images)
217 {
218         /*
219          * In case of legacy uImage's, relocation of FDT is already done
220          * by do_bootm_states() and should not repeated in 'bootm prep'.
221          */
222         if (images->state & BOOTM_STATE_FDT) {
223                 debug("## FDT already relocated\n");
224                 return 0;
225         }
226
227 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
228         boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
229         return boot_relocate_fdt(&images->lmb, &images->ft_addr,
230                 &images->ft_len);
231 #else
232         return 0;
233 #endif
234 }
235
236 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
237 int arch_fixup_fdt(void *blob)
238 {
239         u64 mem_start = virt_to_phys((void *)gd->ram_base);
240         u64 mem_size = gd->ram_size;
241
242         return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
243 }
244 #endif
245
246 static int boot_setup_fdt(struct bootm_headers *images)
247 {
248         images->initrd_start = virt_to_phys((void *)images->initrd_start);
249         images->initrd_end = virt_to_phys((void *)images->initrd_end);
250         return image_setup_libfdt(images, images->ft_addr, images->ft_len,
251                 &images->lmb);
252 }
253
254 static void boot_prep_linux(struct bootm_headers *images)
255 {
256         if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
257                 boot_reloc_fdt(images);
258                 boot_setup_fdt(images);
259         } else {
260                 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
261                         linux_cmdline_legacy(images);
262
263                         if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
264                                 linux_cmdline_append(images);
265
266                         linux_cmdline_dump();
267                 }
268
269                 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
270                         linux_env_legacy(images);
271         }
272 }
273
274 static void boot_jump_linux(struct bootm_headers *images)
275 {
276         typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
277         kernel_entry_t kernel = (kernel_entry_t) images->ep;
278         ulong linux_extra = 0;
279
280         debug("## Transferring control to Linux (at address %p) ...\n", kernel);
281
282         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
283
284         if (CONFIG_IS_ENABLED(MALTA))
285                 linux_extra = gd->ram_size;
286
287 #if IS_ENABLED(CONFIG_BOOTSTAGE_FDT)
288         bootstage_fdt_add_report();
289 #endif
290 #if IS_ENABLED(CONFIG_BOOTSTAGE_REPORT)
291         bootstage_report();
292 #endif
293
294         if (CONFIG_IS_ENABLED(RESTORE_EXCEPTION_VECTOR_BASE))
295                 trap_restore();
296
297         if (images->ft_len)
298                 kernel(-2, (ulong)images->ft_addr, 0, 0);
299         else
300                 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
301                         linux_extra);
302 }
303
304 int do_bootm_linux(int flag, int argc, char *const argv[],
305                    struct bootm_headers *images)
306 {
307         /* No need for those on MIPS */
308         if (flag & BOOTM_STATE_OS_BD_T)
309                 return -1;
310
311         /*
312          * Cmdline init has been moved to 'bootm prep' because it has to be
313          * done after relocation of ramdisk to always pass correct values
314          * for rd_start and rd_size to Linux kernel.
315          */
316         if (flag & BOOTM_STATE_OS_CMDLINE)
317                 return 0;
318
319         if (flag & BOOTM_STATE_OS_PREP) {
320                 boot_prep_linux(images);
321                 return 0;
322         }
323
324         if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
325                 boot_jump_linux(images);
326                 return 0;
327         }
328
329         /* does not return */
330         return 1;
331 }