bootm_os.c 14 KB

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