bootm_os.c 14 KB

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