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 <linux/libfdt.h>
  17. #include <malloc.h>
  18. #include <mapmem.h>
  19. #include <vxworks.h>
  20. #include <tee/optee.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. static int do_bootm_standalone(int flag, int argc, char *const argv[],
  23. bootm_headers_t *images)
  24. {
  25. char *s;
  26. int (*appl)(int, char *const[]);
  27. /* Don't start if "autostart" is set to "no" */
  28. s = env_get("autostart");
  29. if ((s != NULL) && !strcmp(s, "no")) {
  30. env_set_hex("filesize", images->os.image_len);
  31. return 0;
  32. }
  33. appl = (int (*)(int, char * const []))images->ep;
  34. appl(argc, argv);
  35. return 0;
  36. }
  37. /*******************************************************************/
  38. /* OS booting routines */
  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. for (i = 0; i < argc; i++) {
  45. if (i > 0)
  46. *dest++ = delim;
  47. strcpy(dest, argv[i]);
  48. dest += strlen(argv[i]);
  49. }
  50. }
  51. #endif
  52. #ifdef CONFIG_BOOTM_NETBSD
  53. static int do_bootm_netbsd(int flag, int argc, char *const argv[],
  54. bootm_headers_t *images)
  55. {
  56. void (*loader)(struct bd_info *, image_header_t *, char *, char *);
  57. image_header_t *os_hdr, *hdr;
  58. ulong kernel_data, kernel_len;
  59. char *cmdline;
  60. if (flag != BOOTM_STATE_OS_GO)
  61. return 0;
  62. #if defined(CONFIG_FIT)
  63. if (!images->legacy_hdr_valid) {
  64. fit_unsupported_reset("NetBSD");
  65. return 1;
  66. }
  67. #endif
  68. hdr = images->legacy_hdr_os;
  69. /*
  70. * Booting a (NetBSD) kernel image
  71. *
  72. * This process is pretty similar to a standalone application:
  73. * The (first part of an multi-) image must be a stage-2 loader,
  74. * which in turn is responsible for loading & invoking the actual
  75. * kernel. The only differences are the parameters being passed:
  76. * besides the board info strucure, the loader expects a command
  77. * line, the name of the console device, and (optionally) the
  78. * address of the original image header.
  79. */
  80. os_hdr = NULL;
  81. if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
  82. image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
  83. if (kernel_len)
  84. os_hdr = hdr;
  85. }
  86. if (argc > 0) {
  87. ulong len;
  88. int i;
  89. for (i = 0, len = 0; i < argc; i += 1)
  90. len += strlen(argv[i]) + 1;
  91. cmdline = malloc(len);
  92. copy_args(cmdline, argc, argv, ' ');
  93. } else {
  94. cmdline = env_get("bootargs");
  95. if (cmdline == NULL)
  96. cmdline = "";
  97. }
  98. loader = (void (*)(struct bd_info *, image_header_t *, char *, char *))images->ep;
  99. printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
  100. (ulong)loader);
  101. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  102. /*
  103. * NetBSD Stage-2 Loader Parameters:
  104. * arg[0]: pointer to board info data
  105. * arg[1]: image load address
  106. * arg[2]: char pointer to the console device to use
  107. * arg[3]: char pointer to the boot arguments
  108. */
  109. (*loader)(gd->bd, os_hdr, "", cmdline);
  110. return 1;
  111. }
  112. #endif /* CONFIG_BOOTM_NETBSD*/
  113. #ifdef CONFIG_LYNXKDI
  114. static int do_bootm_lynxkdi(int flag, int argc, char *const argv[],
  115. bootm_headers_t *images)
  116. {
  117. image_header_t *hdr = &images->legacy_hdr_os_copy;
  118. if (flag != BOOTM_STATE_OS_GO)
  119. return 0;
  120. #if defined(CONFIG_FIT)
  121. if (!images->legacy_hdr_valid) {
  122. fit_unsupported_reset("Lynx");
  123. return 1;
  124. }
  125. #endif
  126. lynxkdi_boot((image_header_t *)hdr);
  127. return 1;
  128. }
  129. #endif /* CONFIG_LYNXKDI */
  130. #ifdef CONFIG_BOOTM_RTEMS
  131. static int do_bootm_rtems(int flag, int argc, char *const argv[],
  132. bootm_headers_t *images)
  133. {
  134. void (*entry_point)(struct bd_info *);
  135. if (flag != BOOTM_STATE_OS_GO)
  136. return 0;
  137. #if defined(CONFIG_FIT)
  138. if (!images->legacy_hdr_valid) {
  139. fit_unsupported_reset("RTEMS");
  140. return 1;
  141. }
  142. #endif
  143. entry_point = (void (*)(struct bd_info *))images->ep;
  144. printf("## Transferring control to RTEMS (at address %08lx) ...\n",
  145. (ulong)entry_point);
  146. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  147. /*
  148. * RTEMS Parameters:
  149. * r3: ptr to board info data
  150. */
  151. (*entry_point)(gd->bd);
  152. return 1;
  153. }
  154. #endif /* CONFIG_BOOTM_RTEMS */
  155. #if defined(CONFIG_BOOTM_OSE)
  156. static int do_bootm_ose(int flag, int argc, char *const argv[],
  157. bootm_headers_t *images)
  158. {
  159. void (*entry_point)(void);
  160. if (flag != BOOTM_STATE_OS_GO)
  161. return 0;
  162. #if defined(CONFIG_FIT)
  163. if (!images->legacy_hdr_valid) {
  164. fit_unsupported_reset("OSE");
  165. return 1;
  166. }
  167. #endif
  168. entry_point = (void (*)(void))images->ep;
  169. printf("## Transferring control to OSE (at address %08lx) ...\n",
  170. (ulong)entry_point);
  171. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  172. /*
  173. * OSE Parameters:
  174. * None
  175. */
  176. (*entry_point)();
  177. return 1;
  178. }
  179. #endif /* CONFIG_BOOTM_OSE */
  180. #if defined(CONFIG_BOOTM_PLAN9)
  181. static int do_bootm_plan9(int flag, int argc, char *const argv[],
  182. bootm_headers_t *images)
  183. {
  184. void (*entry_point)(void);
  185. char *s;
  186. if (flag != BOOTM_STATE_OS_GO)
  187. return 0;
  188. #if defined(CONFIG_FIT)
  189. if (!images->legacy_hdr_valid) {
  190. fit_unsupported_reset("Plan 9");
  191. return 1;
  192. }
  193. #endif
  194. /* See README.plan9 */
  195. s = env_get("confaddr");
  196. if (s != NULL) {
  197. char *confaddr = (char *)simple_strtoul(s, NULL, 16);
  198. if (argc > 0) {
  199. copy_args(confaddr, argc, argv, '\n');
  200. } else {
  201. s = env_get("bootargs");
  202. if (s != NULL)
  203. strcpy(confaddr, s);
  204. }
  205. }
  206. entry_point = (void (*)(void))images->ep;
  207. printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
  208. (ulong)entry_point);
  209. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  210. /*
  211. * Plan 9 Parameters:
  212. * None
  213. */
  214. (*entry_point)();
  215. return 1;
  216. }
  217. #endif /* CONFIG_BOOTM_PLAN9 */
  218. #if defined(CONFIG_BOOTM_VXWORKS) && \
  219. (defined(CONFIG_PPC) || defined(CONFIG_ARM))
  220. static void do_bootvx_fdt(bootm_headers_t *images)
  221. {
  222. #if defined(CONFIG_OF_LIBFDT)
  223. int ret;
  224. char *bootline;
  225. ulong of_size = images->ft_len;
  226. char **of_flat_tree = &images->ft_addr;
  227. struct lmb *lmb = &images->lmb;
  228. if (*of_flat_tree) {
  229. boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
  230. ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
  231. if (ret)
  232. return;
  233. /* Update ethernet nodes */
  234. fdt_fixup_ethernet(*of_flat_tree);
  235. ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
  236. if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
  237. bootline = env_get("bootargs");
  238. if (bootline) {
  239. ret = fdt_find_and_setprop(*of_flat_tree,
  240. "/chosen", "bootargs",
  241. bootline,
  242. strlen(bootline) + 1, 1);
  243. if (ret < 0) {
  244. printf("## ERROR: %s : %s\n", __func__,
  245. fdt_strerror(ret));
  246. return;
  247. }
  248. }
  249. } else {
  250. printf("## ERROR: %s : %s\n", __func__,
  251. fdt_strerror(ret));
  252. return;
  253. }
  254. }
  255. #endif
  256. boot_prep_vxworks(images);
  257. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  258. #if defined(CONFIG_OF_LIBFDT)
  259. printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
  260. (ulong)images->ep, (ulong)*of_flat_tree);
  261. #else
  262. printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
  263. #endif
  264. boot_jump_vxworks(images);
  265. puts("## vxWorks terminated\n");
  266. }
  267. static int do_bootm_vxworks_legacy(int flag, int argc, char *const argv[],
  268. bootm_headers_t *images)
  269. {
  270. if (flag != BOOTM_STATE_OS_GO)
  271. return 0;
  272. #if defined(CONFIG_FIT)
  273. if (!images->legacy_hdr_valid) {
  274. fit_unsupported_reset("VxWorks");
  275. return 1;
  276. }
  277. #endif
  278. do_bootvx_fdt(images);
  279. return 1;
  280. }
  281. int do_bootm_vxworks(int flag, int argc, char *const argv[],
  282. bootm_headers_t *images)
  283. {
  284. char *bootargs;
  285. int pos;
  286. unsigned long vxflags;
  287. bool std_dtb = false;
  288. /* get bootargs env */
  289. bootargs = env_get("bootargs");
  290. if (bootargs != NULL) {
  291. for (pos = 0; pos < strlen(bootargs); pos++) {
  292. /* find f=0xnumber flag */
  293. if ((bootargs[pos] == '=') && (pos >= 1) &&
  294. (bootargs[pos - 1] == 'f')) {
  295. vxflags = simple_strtoul(&bootargs[pos + 1],
  296. NULL, 16);
  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. }