bootm_os.c 14 KB

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