bootm_os.c 14 KB

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