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