common: Move some cache and MMU functions out of common.h
[oweals/u-boot.git] / common / bootm_os.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2009
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 #include <common.h>
8 #include <bootm.h>
9 #include <cpu_func.h>
10 #include <env.h>
11 #include <fdt_support.h>
12 #include <linux/libfdt.h>
13 #include <malloc.h>
14 #include <vxworks.h>
15 #include <tee/optee.h>
16
17 DECLARE_GLOBAL_DATA_PTR;
18
19 static int do_bootm_standalone(int flag, int argc, char * const argv[],
20                                bootm_headers_t *images)
21 {
22         char *s;
23         int (*appl)(int, char *const[]);
24
25         /* Don't start if "autostart" is set to "no" */
26         s = env_get("autostart");
27         if ((s != NULL) && !strcmp(s, "no")) {
28                 env_set_hex("filesize", images->os.image_len);
29                 return 0;
30         }
31         appl = (int (*)(int, char * const []))images->ep;
32         appl(argc, argv);
33         return 0;
34 }
35
36 /*******************************************************************/
37 /* OS booting routines */
38 /*******************************************************************/
39
40 #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
41 static void copy_args(char *dest, int argc, char * const argv[], char delim)
42 {
43         int i;
44
45         for (i = 0; i < argc; i++) {
46                 if (i > 0)
47                         *dest++ = delim;
48                 strcpy(dest, argv[i]);
49                 dest += strlen(argv[i]);
50         }
51 }
52 #endif
53
54 #ifdef CONFIG_BOOTM_NETBSD
55 static int do_bootm_netbsd(int flag, int argc, char * const argv[],
56                             bootm_headers_t *images)
57 {
58         void (*loader)(bd_t *, image_header_t *, char *, char *);
59         image_header_t *os_hdr, *hdr;
60         ulong kernel_data, kernel_len;
61         char *cmdline;
62
63         if (flag != BOOTM_STATE_OS_GO)
64                 return 0;
65
66 #if defined(CONFIG_FIT)
67         if (!images->legacy_hdr_valid) {
68                 fit_unsupported_reset("NetBSD");
69                 return 1;
70         }
71 #endif
72         hdr = images->legacy_hdr_os;
73
74         /*
75          * Booting a (NetBSD) kernel image
76          *
77          * This process is pretty similar to a standalone application:
78          * The (first part of an multi-) image must be a stage-2 loader,
79          * which in turn is responsible for loading & invoking the actual
80          * kernel.  The only differences are the parameters being passed:
81          * besides the board info strucure, the loader expects a command
82          * line, the name of the console device, and (optionally) the
83          * address of the original image header.
84          */
85         os_hdr = NULL;
86         if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
87                 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
88                 if (kernel_len)
89                         os_hdr = hdr;
90         }
91
92         if (argc > 0) {
93                 ulong len;
94                 int   i;
95
96                 for (i = 0, len = 0; i < argc; i += 1)
97                         len += strlen(argv[i]) + 1;
98                 cmdline = malloc(len);
99                 copy_args(cmdline, argc, argv, ' ');
100         } else {
101                 cmdline = env_get("bootargs");
102                 if (cmdline == NULL)
103                         cmdline = "";
104         }
105
106         loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
107
108         printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
109                (ulong)loader);
110
111         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
112
113         /*
114          * NetBSD Stage-2 Loader Parameters:
115          *   arg[0]: pointer to board info data
116          *   arg[1]: image load address
117          *   arg[2]: char pointer to the console device to use
118          *   arg[3]: char pointer to the boot arguments
119          */
120         (*loader)(gd->bd, os_hdr, "", cmdline);
121
122         return 1;
123 }
124 #endif /* CONFIG_BOOTM_NETBSD*/
125
126 #ifdef CONFIG_LYNXKDI
127 static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
128                              bootm_headers_t *images)
129 {
130         image_header_t *hdr = &images->legacy_hdr_os_copy;
131
132         if (flag != BOOTM_STATE_OS_GO)
133                 return 0;
134
135 #if defined(CONFIG_FIT)
136         if (!images->legacy_hdr_valid) {
137                 fit_unsupported_reset("Lynx");
138                 return 1;
139         }
140 #endif
141
142         lynxkdi_boot((image_header_t *)hdr);
143
144         return 1;
145 }
146 #endif /* CONFIG_LYNXKDI */
147
148 #ifdef CONFIG_BOOTM_RTEMS
149 static int do_bootm_rtems(int flag, int argc, char * const argv[],
150                            bootm_headers_t *images)
151 {
152         void (*entry_point)(bd_t *);
153
154         if (flag != BOOTM_STATE_OS_GO)
155                 return 0;
156
157 #if defined(CONFIG_FIT)
158         if (!images->legacy_hdr_valid) {
159                 fit_unsupported_reset("RTEMS");
160                 return 1;
161         }
162 #endif
163
164         entry_point = (void (*)(bd_t *))images->ep;
165
166         printf("## Transferring control to RTEMS (at address %08lx) ...\n",
167                (ulong)entry_point);
168
169         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
170
171         /*
172          * RTEMS Parameters:
173          *   r3: ptr to board info data
174          */
175         (*entry_point)(gd->bd);
176
177         return 1;
178 }
179 #endif /* CONFIG_BOOTM_RTEMS */
180
181 #if defined(CONFIG_BOOTM_OSE)
182 static int do_bootm_ose(int flag, int argc, char * const argv[],
183                            bootm_headers_t *images)
184 {
185         void (*entry_point)(void);
186
187         if (flag != BOOTM_STATE_OS_GO)
188                 return 0;
189
190 #if defined(CONFIG_FIT)
191         if (!images->legacy_hdr_valid) {
192                 fit_unsupported_reset("OSE");
193                 return 1;
194         }
195 #endif
196
197         entry_point = (void (*)(void))images->ep;
198
199         printf("## Transferring control to OSE (at address %08lx) ...\n",
200                (ulong)entry_point);
201
202         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
203
204         /*
205          * OSE Parameters:
206          *   None
207          */
208         (*entry_point)();
209
210         return 1;
211 }
212 #endif /* CONFIG_BOOTM_OSE */
213
214 #if defined(CONFIG_BOOTM_PLAN9)
215 static int do_bootm_plan9(int flag, int argc, char * const argv[],
216                            bootm_headers_t *images)
217 {
218         void (*entry_point)(void);
219         char *s;
220
221         if (flag != BOOTM_STATE_OS_GO)
222                 return 0;
223
224 #if defined(CONFIG_FIT)
225         if (!images->legacy_hdr_valid) {
226                 fit_unsupported_reset("Plan 9");
227                 return 1;
228         }
229 #endif
230
231         /* See README.plan9 */
232         s = env_get("confaddr");
233         if (s != NULL) {
234                 char *confaddr = (char *)simple_strtoul(s, NULL, 16);
235
236                 if (argc > 0) {
237                         copy_args(confaddr, argc, argv, '\n');
238                 } else {
239                         s = env_get("bootargs");
240                         if (s != NULL)
241                                 strcpy(confaddr, s);
242                 }
243         }
244
245         entry_point = (void (*)(void))images->ep;
246
247         printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
248                (ulong)entry_point);
249
250         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
251
252         /*
253          * Plan 9 Parameters:
254          *   None
255          */
256         (*entry_point)();
257
258         return 1;
259 }
260 #endif /* CONFIG_BOOTM_PLAN9 */
261
262 #if defined(CONFIG_BOOTM_VXWORKS) && \
263         (defined(CONFIG_PPC) || defined(CONFIG_ARM))
264
265 static void do_bootvx_fdt(bootm_headers_t *images)
266 {
267 #if defined(CONFIG_OF_LIBFDT)
268         int ret;
269         char *bootline;
270         ulong of_size = images->ft_len;
271         char **of_flat_tree = &images->ft_addr;
272         struct lmb *lmb = &images->lmb;
273
274         if (*of_flat_tree) {
275                 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
276
277                 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
278                 if (ret)
279                         return;
280
281                 /* Update ethernet nodes */
282                 fdt_fixup_ethernet(*of_flat_tree);
283
284                 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
285                 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
286                         bootline = env_get("bootargs");
287                         if (bootline) {
288                                 ret = fdt_find_and_setprop(*of_flat_tree,
289                                                 "/chosen", "bootargs",
290                                                 bootline,
291                                                 strlen(bootline) + 1, 1);
292                                 if (ret < 0) {
293                                         printf("## ERROR: %s : %s\n", __func__,
294                                                fdt_strerror(ret));
295                                         return;
296                                 }
297                         }
298                 } else {
299                         printf("## ERROR: %s : %s\n", __func__,
300                                fdt_strerror(ret));
301                         return;
302                 }
303         }
304 #endif
305
306         boot_prep_vxworks(images);
307
308         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
309
310 #if defined(CONFIG_OF_LIBFDT)
311         printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
312                (ulong)images->ep, (ulong)*of_flat_tree);
313 #else
314         printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
315 #endif
316
317         boot_jump_vxworks(images);
318
319         puts("## vxWorks terminated\n");
320 }
321
322 int do_bootm_vxworks(int flag, int argc, char * const argv[],
323                      bootm_headers_t *images)
324 {
325         if (flag != BOOTM_STATE_OS_GO)
326                 return 0;
327
328 #if defined(CONFIG_FIT)
329         if (!images->legacy_hdr_valid) {
330                 fit_unsupported_reset("VxWorks");
331                 return 1;
332         }
333 #endif
334
335         do_bootvx_fdt(images);
336
337         return 1;
338 }
339 #endif
340
341 #if defined(CONFIG_CMD_ELF)
342 static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
343                             bootm_headers_t *images)
344 {
345         char *local_args[2];
346         char str[16];
347         int dcache;
348
349         if (flag != BOOTM_STATE_OS_GO)
350                 return 0;
351
352 #if defined(CONFIG_FIT)
353         if (!images->legacy_hdr_valid) {
354                 fit_unsupported_reset("QNX");
355                 return 1;
356         }
357 #endif
358
359         sprintf(str, "%lx", images->ep); /* write entry-point into string */
360         local_args[0] = argv[0];
361         local_args[1] = str;    /* and provide it via the arguments */
362
363         /*
364          * QNX images require the data cache is disabled.
365          */
366         dcache = dcache_status();
367         if (dcache)
368                 dcache_disable();
369
370         do_bootelf(NULL, 0, 2, local_args);
371
372         if (dcache)
373                 dcache_enable();
374
375         return 1;
376 }
377 #endif
378
379 #ifdef CONFIG_INTEGRITY
380 static int do_bootm_integrity(int flag, int argc, char * const argv[],
381                            bootm_headers_t *images)
382 {
383         void (*entry_point)(void);
384
385         if (flag != BOOTM_STATE_OS_GO)
386                 return 0;
387
388 #if defined(CONFIG_FIT)
389         if (!images->legacy_hdr_valid) {
390                 fit_unsupported_reset("INTEGRITY");
391                 return 1;
392         }
393 #endif
394
395         entry_point = (void (*)(void))images->ep;
396
397         printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
398                (ulong)entry_point);
399
400         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
401
402         /*
403          * INTEGRITY Parameters:
404          *   None
405          */
406         (*entry_point)();
407
408         return 1;
409 }
410 #endif
411
412 #ifdef CONFIG_BOOTM_OPENRTOS
413 static int do_bootm_openrtos(int flag, int argc, char * const argv[],
414                            bootm_headers_t *images)
415 {
416         void (*entry_point)(void);
417
418         if (flag != BOOTM_STATE_OS_GO)
419                 return 0;
420
421         entry_point = (void (*)(void))images->ep;
422
423         printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
424                 (ulong)entry_point);
425
426         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
427
428         /*
429          * OpenRTOS Parameters:
430          *   None
431          */
432         (*entry_point)();
433
434         return 1;
435 }
436 #endif
437
438 #ifdef CONFIG_BOOTM_OPTEE
439 static int do_bootm_tee(int flag, int argc, char * const argv[],
440                         bootm_headers_t *images)
441 {
442         int ret;
443
444         /* Verify OS type */
445         if (images->os.os != IH_OS_TEE) {
446                 return 1;
447         };
448
449         /* Validate OPTEE header */
450         ret = optee_verify_bootm_image(images->os.image_start,
451                                        images->os.load,
452                                        images->os.image_len);
453         if (ret)
454                 return ret;
455
456         /* Locate FDT etc */
457         ret = bootm_find_images(flag, argc, argv);
458         if (ret)
459                 return ret;
460
461         /* From here we can run the regular linux boot path */
462         return do_bootm_linux(flag, argc, argv, images);
463 }
464 #endif
465
466 static boot_os_fn *boot_os[] = {
467         [IH_OS_U_BOOT] = do_bootm_standalone,
468 #ifdef CONFIG_BOOTM_LINUX
469         [IH_OS_LINUX] = do_bootm_linux,
470 #endif
471 #ifdef CONFIG_BOOTM_NETBSD
472         [IH_OS_NETBSD] = do_bootm_netbsd,
473 #endif
474 #ifdef CONFIG_LYNXKDI
475         [IH_OS_LYNXOS] = do_bootm_lynxkdi,
476 #endif
477 #ifdef CONFIG_BOOTM_RTEMS
478         [IH_OS_RTEMS] = do_bootm_rtems,
479 #endif
480 #if defined(CONFIG_BOOTM_OSE)
481         [IH_OS_OSE] = do_bootm_ose,
482 #endif
483 #if defined(CONFIG_BOOTM_PLAN9)
484         [IH_OS_PLAN9] = do_bootm_plan9,
485 #endif
486 #if defined(CONFIG_BOOTM_VXWORKS) && \
487         (defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
488         [IH_OS_VXWORKS] = do_bootm_vxworks,
489 #endif
490 #if defined(CONFIG_CMD_ELF)
491         [IH_OS_QNX] = do_bootm_qnxelf,
492 #endif
493 #ifdef CONFIG_INTEGRITY
494         [IH_OS_INTEGRITY] = do_bootm_integrity,
495 #endif
496 #ifdef CONFIG_BOOTM_OPENRTOS
497         [IH_OS_OPENRTOS] = do_bootm_openrtos,
498 #endif
499 #ifdef CONFIG_BOOTM_OPTEE
500         [IH_OS_TEE] = do_bootm_tee,
501 #endif
502 };
503
504 /* Allow for arch specific config before we boot */
505 __weak void arch_preboot_os(void)
506 {
507         /* please define platform specific arch_preboot_os() */
508 }
509
510 /* Allow for board specific config before we boot */
511 __weak void board_preboot_os(void)
512 {
513         /* please define board specific board_preboot_os() */
514 }
515
516 int boot_selected_os(int argc, char * const argv[], int state,
517                      bootm_headers_t *images, boot_os_fn *boot_fn)
518 {
519         arch_preboot_os();
520         board_preboot_os();
521         boot_fn(state, argc, argv, images);
522
523         /* Stand-alone may return when 'autostart' is 'no' */
524         if (images->os.type == IH_TYPE_STANDALONE ||
525             IS_ENABLED(CONFIG_SANDBOX) ||
526             state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
527                 return 0;
528         bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
529         debug("\n## Control returned to monitor - resetting...\n");
530
531         return BOOTM_ERR_RESET;
532 }
533
534 boot_os_fn *bootm_os_get_boot_func(int os)
535 {
536 #ifdef CONFIG_NEEDS_MANUAL_RELOC
537         static bool relocated;
538
539         if (!relocated) {
540                 int i;
541
542                 /* relocate boot function table */
543                 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
544                         if (boot_os[i] != NULL)
545                                 boot_os[i] += gd->reloc_off;
546
547                 relocated = true;
548         }
549 #endif
550         return boot_os[os];
551 }