bootm_os.c 13 KB

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