blob: 1d58462509cc07111c0eeadcd0029ffcee2b9705 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb6396402014-06-12 07:24:46 -06002/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glassb6396402014-06-12 07:24:46 -06005 */
6
7#include <common.h>
8#include <bootm.h>
Simon Glass9edefc22019-11-14 12:57:37 -07009#include <cpu_func.h>
Cristian Ciocalteaecc7fda2019-12-24 18:05:39 +020010#include <efi_loader.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060011#include <env.h>
Simon Glassb6396402014-06-12 07:24:46 -060012#include <fdt_support.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090013#include <linux/libfdt.h>
Simon Glassb6396402014-06-12 07:24:46 -060014#include <malloc.h>
Cristian Ciocalteaecc7fda2019-12-24 18:05:39 +020015#include <mapmem.h>
Simon Glassb6396402014-06-12 07:24:46 -060016#include <vxworks.h>
Bryan O'Donoghuec225e7c2018-03-13 16:50:36 +000017#include <tee/optee.h>
Simon Glassb6396402014-06-12 07:24:46 -060018
19DECLARE_GLOBAL_DATA_PTR;
20
21static int do_bootm_standalone(int flag, int argc, char * const argv[],
22 bootm_headers_t *images)
23{
24 char *s;
25 int (*appl)(int, char *const[]);
26
27 /* Don't start if "autostart" is set to "no" */
Simon Glass00caae62017-08-03 12:22:12 -060028 s = env_get("autostart");
Simon Glassb6396402014-06-12 07:24:46 -060029 if ((s != NULL) && !strcmp(s, "no")) {
Simon Glass018f5302017-08-03 12:22:10 -060030 env_set_hex("filesize", images->os.image_len);
Simon Glassb6396402014-06-12 07:24:46 -060031 return 0;
32 }
33 appl = (int (*)(int, char * const []))images->ep;
34 appl(argc, argv);
35 return 0;
36}
37
38/*******************************************************************/
39/* OS booting routines */
40/*******************************************************************/
41
42#if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
43static void copy_args(char *dest, int argc, char * const argv[], char delim)
44{
45 int i;
46
47 for (i = 0; i < argc; i++) {
48 if (i > 0)
49 *dest++ = delim;
50 strcpy(dest, argv[i]);
51 dest += strlen(argv[i]);
52 }
53}
54#endif
55
56#ifdef CONFIG_BOOTM_NETBSD
57static int do_bootm_netbsd(int flag, int argc, char * const argv[],
58 bootm_headers_t *images)
59{
60 void (*loader)(bd_t *, image_header_t *, char *, char *);
61 image_header_t *os_hdr, *hdr;
62 ulong kernel_data, kernel_len;
Simon Glassb6396402014-06-12 07:24:46 -060063 char *cmdline;
64
65 if (flag != BOOTM_STATE_OS_GO)
66 return 0;
67
68#if defined(CONFIG_FIT)
69 if (!images->legacy_hdr_valid) {
70 fit_unsupported_reset("NetBSD");
71 return 1;
72 }
73#endif
74 hdr = images->legacy_hdr_os;
75
76 /*
77 * Booting a (NetBSD) kernel image
78 *
79 * This process is pretty similar to a standalone application:
80 * The (first part of an multi-) image must be a stage-2 loader,
81 * which in turn is responsible for loading & invoking the actual
82 * kernel. The only differences are the parameters being passed:
83 * besides the board info strucure, the loader expects a command
84 * line, the name of the console device, and (optionally) the
85 * address of the original image header.
86 */
87 os_hdr = NULL;
88 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
89 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
90 if (kernel_len)
91 os_hdr = hdr;
92 }
93
Simon Glassb6396402014-06-12 07:24:46 -060094 if (argc > 0) {
95 ulong len;
96 int i;
97
98 for (i = 0, len = 0; i < argc; i += 1)
99 len += strlen(argv[i]) + 1;
100 cmdline = malloc(len);
101 copy_args(cmdline, argc, argv, ' ');
102 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600103 cmdline = env_get("bootargs");
Simon Glassb6396402014-06-12 07:24:46 -0600104 if (cmdline == NULL)
105 cmdline = "";
106 }
107
108 loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
109
110 printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
111 (ulong)loader);
112
113 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
114
115 /*
116 * NetBSD Stage-2 Loader Parameters:
117 * arg[0]: pointer to board info data
118 * arg[1]: image load address
119 * arg[2]: char pointer to the console device to use
120 * arg[3]: char pointer to the boot arguments
121 */
Heiko Schocher5b8e76c2017-06-07 17:33:09 +0200122 (*loader)(gd->bd, os_hdr, "", cmdline);
Simon Glassb6396402014-06-12 07:24:46 -0600123
124 return 1;
125}
126#endif /* CONFIG_BOOTM_NETBSD*/
127
128#ifdef CONFIG_LYNXKDI
129static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
130 bootm_headers_t *images)
131{
132 image_header_t *hdr = &images->legacy_hdr_os_copy;
133
134 if (flag != BOOTM_STATE_OS_GO)
135 return 0;
136
137#if defined(CONFIG_FIT)
138 if (!images->legacy_hdr_valid) {
139 fit_unsupported_reset("Lynx");
140 return 1;
141 }
142#endif
143
144 lynxkdi_boot((image_header_t *)hdr);
145
146 return 1;
147}
148#endif /* CONFIG_LYNXKDI */
149
150#ifdef CONFIG_BOOTM_RTEMS
151static int do_bootm_rtems(int flag, int argc, char * const argv[],
152 bootm_headers_t *images)
153{
154 void (*entry_point)(bd_t *);
155
156 if (flag != BOOTM_STATE_OS_GO)
157 return 0;
158
159#if defined(CONFIG_FIT)
160 if (!images->legacy_hdr_valid) {
161 fit_unsupported_reset("RTEMS");
162 return 1;
163 }
164#endif
165
166 entry_point = (void (*)(bd_t *))images->ep;
167
168 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
169 (ulong)entry_point);
170
171 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
172
173 /*
174 * RTEMS Parameters:
175 * r3: ptr to board info data
176 */
177 (*entry_point)(gd->bd);
178
179 return 1;
180}
181#endif /* CONFIG_BOOTM_RTEMS */
182
183#if defined(CONFIG_BOOTM_OSE)
184static int do_bootm_ose(int flag, int argc, char * const argv[],
185 bootm_headers_t *images)
186{
187 void (*entry_point)(void);
188
189 if (flag != BOOTM_STATE_OS_GO)
190 return 0;
191
192#if defined(CONFIG_FIT)
193 if (!images->legacy_hdr_valid) {
194 fit_unsupported_reset("OSE");
195 return 1;
196 }
197#endif
198
199 entry_point = (void (*)(void))images->ep;
200
201 printf("## Transferring control to OSE (at address %08lx) ...\n",
202 (ulong)entry_point);
203
204 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
205
206 /*
207 * OSE Parameters:
208 * None
209 */
210 (*entry_point)();
211
212 return 1;
213}
214#endif /* CONFIG_BOOTM_OSE */
215
216#if defined(CONFIG_BOOTM_PLAN9)
217static int do_bootm_plan9(int flag, int argc, char * const argv[],
218 bootm_headers_t *images)
219{
220 void (*entry_point)(void);
221 char *s;
222
223 if (flag != BOOTM_STATE_OS_GO)
224 return 0;
225
226#if defined(CONFIG_FIT)
227 if (!images->legacy_hdr_valid) {
228 fit_unsupported_reset("Plan 9");
229 return 1;
230 }
231#endif
232
233 /* See README.plan9 */
Simon Glass00caae62017-08-03 12:22:12 -0600234 s = env_get("confaddr");
Simon Glassb6396402014-06-12 07:24:46 -0600235 if (s != NULL) {
236 char *confaddr = (char *)simple_strtoul(s, NULL, 16);
237
238 if (argc > 0) {
239 copy_args(confaddr, argc, argv, '\n');
240 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600241 s = env_get("bootargs");
Simon Glassb6396402014-06-12 07:24:46 -0600242 if (s != NULL)
243 strcpy(confaddr, s);
244 }
245 }
246
247 entry_point = (void (*)(void))images->ep;
248
249 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
250 (ulong)entry_point);
251
252 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
253
254 /*
255 * Plan 9 Parameters:
256 * None
257 */
258 (*entry_point)();
259
260 return 1;
261}
262#endif /* CONFIG_BOOTM_PLAN9 */
263
264#if defined(CONFIG_BOOTM_VXWORKS) && \
265 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
266
Bin Meng7ebfb372018-12-21 07:13:39 -0800267static void do_bootvx_fdt(bootm_headers_t *images)
Simon Glassb6396402014-06-12 07:24:46 -0600268{
269#if defined(CONFIG_OF_LIBFDT)
270 int ret;
271 char *bootline;
272 ulong of_size = images->ft_len;
273 char **of_flat_tree = &images->ft_addr;
274 struct lmb *lmb = &images->lmb;
275
276 if (*of_flat_tree) {
277 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
278
279 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
280 if (ret)
281 return;
282
Hannes Schmelzera223e2b2017-08-25 14:27:37 +0200283 /* Update ethernet nodes */
284 fdt_fixup_ethernet(*of_flat_tree);
285
Simon Glassb6396402014-06-12 07:24:46 -0600286 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
287 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
Simon Glass00caae62017-08-03 12:22:12 -0600288 bootline = env_get("bootargs");
Simon Glassb6396402014-06-12 07:24:46 -0600289 if (bootline) {
290 ret = fdt_find_and_setprop(*of_flat_tree,
291 "/chosen", "bootargs",
292 bootline,
293 strlen(bootline) + 1, 1);
294 if (ret < 0) {
295 printf("## ERROR: %s : %s\n", __func__,
296 fdt_strerror(ret));
297 return;
298 }
299 }
300 } else {
301 printf("## ERROR: %s : %s\n", __func__,
302 fdt_strerror(ret));
303 return;
304 }
305 }
306#endif
307
308 boot_prep_vxworks(images);
309
310 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
311
312#if defined(CONFIG_OF_LIBFDT)
313 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
314 (ulong)images->ep, (ulong)*of_flat_tree);
315#else
316 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
317#endif
318
319 boot_jump_vxworks(images);
320
321 puts("## vxWorks terminated\n");
322}
323
Lihua Zhao1e26f642019-11-15 00:21:17 -0800324static int do_bootm_vxworks_legacy(int flag, int argc, char * const argv[],
325 bootm_headers_t *images)
Simon Glassb6396402014-06-12 07:24:46 -0600326{
327 if (flag != BOOTM_STATE_OS_GO)
328 return 0;
329
330#if defined(CONFIG_FIT)
331 if (!images->legacy_hdr_valid) {
332 fit_unsupported_reset("VxWorks");
333 return 1;
334 }
335#endif
336
337 do_bootvx_fdt(images);
338
339 return 1;
340}
Lihua Zhao1e26f642019-11-15 00:21:17 -0800341
342int do_bootm_vxworks(int flag, int argc, char * const argv[],
343 bootm_headers_t *images)
344{
345 char *bootargs;
346 int pos;
347 unsigned long vxflags;
348 bool std_dtb = false;
349
350 /* get bootargs env */
351 bootargs = env_get("bootargs");
352
353 if (bootargs != NULL) {
354 for (pos = 0; pos < strlen(bootargs); pos++) {
355 /* find f=0xnumber flag */
356 if ((bootargs[pos] == '=') && (pos >= 1) &&
357 (bootargs[pos - 1] == 'f')) {
358 vxflags = simple_strtoul(&bootargs[pos + 1],
359 NULL, 16);
360 if (vxflags & VXWORKS_SYSFLG_STD_DTB)
361 std_dtb = true;
362 }
363 }
364 }
365
366 if (std_dtb) {
367 if (flag & BOOTM_STATE_OS_PREP)
368 printf(" Using standard DTB\n");
369 return do_bootm_linux(flag, argc, argv, images);
370 } else {
371 if (flag & BOOTM_STATE_OS_PREP)
372 printf(" !!! WARNING !!! Using legacy DTB\n");
373 return do_bootm_vxworks_legacy(flag, argc, argv, images);
374 }
375}
Simon Glassb6396402014-06-12 07:24:46 -0600376#endif
377
378#if defined(CONFIG_CMD_ELF)
379static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
380 bootm_headers_t *images)
381{
382 char *local_args[2];
383 char str[16];
Emmanuel Vadot995eab82017-01-19 10:23:56 +0100384 int dcache;
Simon Glassb6396402014-06-12 07:24:46 -0600385
386 if (flag != BOOTM_STATE_OS_GO)
387 return 0;
388
389#if defined(CONFIG_FIT)
390 if (!images->legacy_hdr_valid) {
391 fit_unsupported_reset("QNX");
392 return 1;
393 }
394#endif
395
396 sprintf(str, "%lx", images->ep); /* write entry-point into string */
397 local_args[0] = argv[0];
398 local_args[1] = str; /* and provide it via the arguments */
Emmanuel Vadot995eab82017-01-19 10:23:56 +0100399
400 /*
401 * QNX images require the data cache is disabled.
402 */
403 dcache = dcache_status();
404 if (dcache)
405 dcache_disable();
406
Simon Glassb6396402014-06-12 07:24:46 -0600407 do_bootelf(NULL, 0, 2, local_args);
408
Emmanuel Vadot995eab82017-01-19 10:23:56 +0100409 if (dcache)
410 dcache_enable();
411
Simon Glassb6396402014-06-12 07:24:46 -0600412 return 1;
413}
414#endif
415
416#ifdef CONFIG_INTEGRITY
417static int do_bootm_integrity(int flag, int argc, char * const argv[],
418 bootm_headers_t *images)
419{
420 void (*entry_point)(void);
421
422 if (flag != BOOTM_STATE_OS_GO)
423 return 0;
424
425#if defined(CONFIG_FIT)
426 if (!images->legacy_hdr_valid) {
427 fit_unsupported_reset("INTEGRITY");
428 return 1;
429 }
430#endif
431
432 entry_point = (void (*)(void))images->ep;
433
434 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
435 (ulong)entry_point);
436
437 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
438
439 /*
440 * INTEGRITY Parameters:
441 * None
442 */
443 (*entry_point)();
444
445 return 1;
446}
447#endif
448
Marek Vasut67ddd952014-12-16 14:07:21 +0100449#ifdef CONFIG_BOOTM_OPENRTOS
450static int do_bootm_openrtos(int flag, int argc, char * const argv[],
451 bootm_headers_t *images)
452{
453 void (*entry_point)(void);
454
455 if (flag != BOOTM_STATE_OS_GO)
456 return 0;
457
458 entry_point = (void (*)(void))images->ep;
459
460 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
461 (ulong)entry_point);
462
463 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
464
465 /*
466 * OpenRTOS Parameters:
467 * None
468 */
469 (*entry_point)();
470
471 return 1;
472}
473#endif
474
Bryan O'Donoghuec225e7c2018-03-13 16:50:36 +0000475#ifdef CONFIG_BOOTM_OPTEE
476static int do_bootm_tee(int flag, int argc, char * const argv[],
477 bootm_headers_t *images)
478{
479 int ret;
480
481 /* Verify OS type */
482 if (images->os.os != IH_OS_TEE) {
483 return 1;
484 };
485
486 /* Validate OPTEE header */
487 ret = optee_verify_bootm_image(images->os.image_start,
488 images->os.load,
489 images->os.image_len);
490 if (ret)
491 return ret;
492
493 /* Locate FDT etc */
494 ret = bootm_find_images(flag, argc, argv);
495 if (ret)
496 return ret;
497
498 /* From here we can run the regular linux boot path */
499 return do_bootm_linux(flag, argc, argv, images);
500}
501#endif
502
Cristian Ciocalteaecc7fda2019-12-24 18:05:39 +0200503#ifdef CONFIG_BOOTM_EFI
504static int do_bootm_efi(int flag, int argc, char * const argv[],
505 bootm_headers_t *images)
506{
507 int ret;
508 efi_status_t efi_ret;
509 void *image_buf;
510
511 if (flag != BOOTM_STATE_OS_GO)
512 return 0;
513
514 /* Locate FDT, if provided */
515 ret = bootm_find_images(flag, argc, argv);
516 if (ret)
517 return ret;
518
519 /* Initialize EFI drivers */
520 efi_ret = efi_init_obj_list();
521 if (efi_ret != EFI_SUCCESS) {
522 printf("## Failed to initialize UEFI sub-system: r = %lu\n",
523 efi_ret & ~EFI_ERROR_MASK);
524 return 1;
525 }
526
527 /* Install device tree */
528 efi_ret = efi_install_fdt(images->ft_len
529 ? images->ft_addr : EFI_FDT_USE_INTERNAL);
530 if (efi_ret != EFI_SUCCESS) {
531 printf("## Failed to install device tree: r = %lu\n",
532 efi_ret & ~EFI_ERROR_MASK);
533 return 1;
534 }
535
536 /* Run EFI image */
537 printf("## Transferring control to EFI (at address %08lx) ...\n",
538 images->ep);
539 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
540
541 image_buf = map_sysmem(images->ep, images->os.image_len);
542
543 efi_ret = efi_run_image(image_buf, images->os.image_len);
544 if (efi_ret != EFI_SUCCESS) {
545 printf("## Failed to run EFI image: r = %lu\n",
546 efi_ret & ~EFI_ERROR_MASK);
547 return 1;
548 }
549
550 return 0;
551}
552#endif
553
Simon Glassb6396402014-06-12 07:24:46 -0600554static boot_os_fn *boot_os[] = {
555 [IH_OS_U_BOOT] = do_bootm_standalone,
556#ifdef CONFIG_BOOTM_LINUX
557 [IH_OS_LINUX] = do_bootm_linux,
558#endif
559#ifdef CONFIG_BOOTM_NETBSD
560 [IH_OS_NETBSD] = do_bootm_netbsd,
561#endif
562#ifdef CONFIG_LYNXKDI
563 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
564#endif
565#ifdef CONFIG_BOOTM_RTEMS
566 [IH_OS_RTEMS] = do_bootm_rtems,
567#endif
568#if defined(CONFIG_BOOTM_OSE)
569 [IH_OS_OSE] = do_bootm_ose,
570#endif
571#if defined(CONFIG_BOOTM_PLAN9)
572 [IH_OS_PLAN9] = do_bootm_plan9,
573#endif
574#if defined(CONFIG_BOOTM_VXWORKS) && \
Bin Meng08337cd2018-12-21 07:13:41 -0800575 (defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
Simon Glassb6396402014-06-12 07:24:46 -0600576 [IH_OS_VXWORKS] = do_bootm_vxworks,
577#endif
578#if defined(CONFIG_CMD_ELF)
579 [IH_OS_QNX] = do_bootm_qnxelf,
580#endif
581#ifdef CONFIG_INTEGRITY
582 [IH_OS_INTEGRITY] = do_bootm_integrity,
583#endif
Marek Vasut67ddd952014-12-16 14:07:21 +0100584#ifdef CONFIG_BOOTM_OPENRTOS
585 [IH_OS_OPENRTOS] = do_bootm_openrtos,
586#endif
Bryan O'Donoghuec225e7c2018-03-13 16:50:36 +0000587#ifdef CONFIG_BOOTM_OPTEE
588 [IH_OS_TEE] = do_bootm_tee,
589#endif
Cristian Ciocalteaecc7fda2019-12-24 18:05:39 +0200590#ifdef CONFIG_BOOTM_EFI
591 [IH_OS_EFI] = do_bootm_efi,
592#endif
Simon Glassb6396402014-06-12 07:24:46 -0600593};
594
595/* Allow for arch specific config before we boot */
Jeroen Hofstee82c3a4c2014-07-10 23:06:25 +0200596__weak void arch_preboot_os(void)
Simon Glassb6396402014-06-12 07:24:46 -0600597{
598 /* please define platform specific arch_preboot_os() */
599}
Simon Glassb6396402014-06-12 07:24:46 -0600600
Marek Vasutfd3d1212018-10-04 21:16:31 +0200601/* Allow for board specific config before we boot */
602__weak void board_preboot_os(void)
603{
604 /* please define board specific board_preboot_os() */
605}
606
Simon Glassb6396402014-06-12 07:24:46 -0600607int boot_selected_os(int argc, char * const argv[], int state,
608 bootm_headers_t *images, boot_os_fn *boot_fn)
609{
610 arch_preboot_os();
Marek Vasutfd3d1212018-10-04 21:16:31 +0200611 board_preboot_os();
Simon Glassb6396402014-06-12 07:24:46 -0600612 boot_fn(state, argc, argv, images);
613
614 /* Stand-alone may return when 'autostart' is 'no' */
615 if (images->os.type == IH_TYPE_STANDALONE ||
Simon Glassb9c771b2016-07-03 09:40:35 -0600616 IS_ENABLED(CONFIG_SANDBOX) ||
Simon Glassb6396402014-06-12 07:24:46 -0600617 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
618 return 0;
619 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
Lukasz Majewski851bda82016-05-08 08:52:21 +0200620 debug("\n## Control returned to monitor - resetting...\n");
621
Simon Glassb6396402014-06-12 07:24:46 -0600622 return BOOTM_ERR_RESET;
623}
624
625boot_os_fn *bootm_os_get_boot_func(int os)
626{
627#ifdef CONFIG_NEEDS_MANUAL_RELOC
628 static bool relocated;
629
630 if (!relocated) {
631 int i;
632
633 /* relocate boot function table */
634 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
635 if (boot_os[i] != NULL)
636 boot_os[i] += gd->reloc_off;
637
638 relocated = true;
639 }
640#endif
641 return boot_os[os];
642}