bootm_os.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 *)simple_strtoul(s, NULL, 16);
  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 = simple_strtoul(&bootargs[pos + 1],
  297. NULL, 16);
  298. if (vxflags & VXWORKS_SYSFLG_STD_DTB)
  299. std_dtb = true;
  300. }
  301. }
  302. }
  303. if (std_dtb) {
  304. if (flag & BOOTM_STATE_OS_PREP)
  305. printf(" Using standard DTB\n");
  306. return do_bootm_linux(flag, argc, argv, images);
  307. } else {
  308. if (flag & BOOTM_STATE_OS_PREP)
  309. printf(" !!! WARNING !!! Using legacy DTB\n");
  310. return do_bootm_vxworks_legacy(flag, argc, argv, images);
  311. }
  312. }
  313. #endif
  314. #if defined(CONFIG_CMD_ELF)
  315. static int do_bootm_qnxelf(int flag, int argc, char *const argv[],
  316. bootm_headers_t *images)
  317. {
  318. char *local_args[2];
  319. char str[16];
  320. int dcache;
  321. if (flag != BOOTM_STATE_OS_GO)
  322. return 0;
  323. #if defined(CONFIG_FIT)
  324. if (!images->legacy_hdr_valid) {
  325. fit_unsupported_reset("QNX");
  326. return 1;
  327. }
  328. #endif
  329. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  330. local_args[0] = argv[0];
  331. local_args[1] = str; /* and provide it via the arguments */
  332. /*
  333. * QNX images require the data cache is disabled.
  334. */
  335. dcache = dcache_status();
  336. if (dcache)
  337. dcache_disable();
  338. do_bootelf(NULL, 0, 2, local_args);
  339. if (dcache)
  340. dcache_enable();
  341. return 1;
  342. }
  343. #endif
  344. #ifdef CONFIG_INTEGRITY
  345. static int do_bootm_integrity(int flag, int argc, char *const argv[],
  346. bootm_headers_t *images)
  347. {
  348. void (*entry_point)(void);
  349. if (flag != BOOTM_STATE_OS_GO)
  350. return 0;
  351. #if defined(CONFIG_FIT)
  352. if (!images->legacy_hdr_valid) {
  353. fit_unsupported_reset("INTEGRITY");
  354. return 1;
  355. }
  356. #endif
  357. entry_point = (void (*)(void))images->ep;
  358. printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
  359. (ulong)entry_point);
  360. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  361. /*
  362. * INTEGRITY Parameters:
  363. * None
  364. */
  365. (*entry_point)();
  366. return 1;
  367. }
  368. #endif
  369. #ifdef CONFIG_BOOTM_OPENRTOS
  370. static int do_bootm_openrtos(int flag, int argc, char *const argv[],
  371. bootm_headers_t *images)
  372. {
  373. void (*entry_point)(void);
  374. if (flag != BOOTM_STATE_OS_GO)
  375. return 0;
  376. entry_point = (void (*)(void))images->ep;
  377. printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
  378. (ulong)entry_point);
  379. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  380. /*
  381. * OpenRTOS Parameters:
  382. * None
  383. */
  384. (*entry_point)();
  385. return 1;
  386. }
  387. #endif
  388. #ifdef CONFIG_BOOTM_OPTEE
  389. static int do_bootm_tee(int flag, int argc, char *const argv[],
  390. bootm_headers_t *images)
  391. {
  392. int ret;
  393. /* Verify OS type */
  394. if (images->os.os != IH_OS_TEE) {
  395. return 1;
  396. };
  397. /* Validate OPTEE header */
  398. ret = optee_verify_bootm_image(images->os.image_start,
  399. images->os.load,
  400. images->os.image_len);
  401. if (ret)
  402. return ret;
  403. /* Locate FDT etc */
  404. ret = bootm_find_images(flag, argc, argv, 0, 0);
  405. if (ret)
  406. return ret;
  407. /* From here we can run the regular linux boot path */
  408. return do_bootm_linux(flag, argc, argv, images);
  409. }
  410. #endif
  411. #ifdef CONFIG_BOOTM_EFI
  412. static int do_bootm_efi(int flag, int argc, char *const argv[],
  413. bootm_headers_t *images)
  414. {
  415. int ret;
  416. efi_status_t efi_ret;
  417. void *image_buf;
  418. if (flag != BOOTM_STATE_OS_GO)
  419. return 0;
  420. /* Locate FDT, if provided */
  421. ret = bootm_find_images(flag, argc, argv, 0, 0);
  422. if (ret)
  423. return ret;
  424. /* Initialize EFI drivers */
  425. efi_ret = efi_init_obj_list();
  426. if (efi_ret != EFI_SUCCESS) {
  427. printf("## Failed to initialize UEFI sub-system: r = %lu\n",
  428. efi_ret & ~EFI_ERROR_MASK);
  429. return 1;
  430. }
  431. /* Install device tree */
  432. efi_ret = efi_install_fdt(images->ft_len
  433. ? images->ft_addr : EFI_FDT_USE_INTERNAL);
  434. if (efi_ret != EFI_SUCCESS) {
  435. printf("## Failed to install device tree: r = %lu\n",
  436. efi_ret & ~EFI_ERROR_MASK);
  437. return 1;
  438. }
  439. /* Run EFI image */
  440. printf("## Transferring control to EFI (at address %08lx) ...\n",
  441. images->ep);
  442. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  443. /* We expect to return */
  444. images->os.type = IH_TYPE_STANDALONE;
  445. image_buf = map_sysmem(images->ep, images->os.image_len);
  446. efi_ret = efi_run_image(image_buf, images->os.image_len);
  447. if (efi_ret != EFI_SUCCESS)
  448. return 1;
  449. return 0;
  450. }
  451. #endif
  452. static boot_os_fn *boot_os[] = {
  453. [IH_OS_U_BOOT] = do_bootm_standalone,
  454. #ifdef CONFIG_BOOTM_LINUX
  455. [IH_OS_LINUX] = do_bootm_linux,
  456. #endif
  457. #ifdef CONFIG_BOOTM_NETBSD
  458. [IH_OS_NETBSD] = do_bootm_netbsd,
  459. #endif
  460. #ifdef CONFIG_LYNXKDI
  461. [IH_OS_LYNXOS] = do_bootm_lynxkdi,
  462. #endif
  463. #ifdef CONFIG_BOOTM_RTEMS
  464. [IH_OS_RTEMS] = do_bootm_rtems,
  465. #endif
  466. #if defined(CONFIG_BOOTM_OSE)
  467. [IH_OS_OSE] = do_bootm_ose,
  468. #endif
  469. #if defined(CONFIG_BOOTM_PLAN9)
  470. [IH_OS_PLAN9] = do_bootm_plan9,
  471. #endif
  472. #if defined(CONFIG_BOOTM_VXWORKS) && \
  473. (defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
  474. [IH_OS_VXWORKS] = do_bootm_vxworks,
  475. #endif
  476. #if defined(CONFIG_CMD_ELF)
  477. [IH_OS_QNX] = do_bootm_qnxelf,
  478. #endif
  479. #ifdef CONFIG_INTEGRITY
  480. [IH_OS_INTEGRITY] = do_bootm_integrity,
  481. #endif
  482. #ifdef CONFIG_BOOTM_OPENRTOS
  483. [IH_OS_OPENRTOS] = do_bootm_openrtos,
  484. #endif
  485. #ifdef CONFIG_BOOTM_OPTEE
  486. [IH_OS_TEE] = do_bootm_tee,
  487. #endif
  488. #ifdef CONFIG_BOOTM_EFI
  489. [IH_OS_EFI] = do_bootm_efi,
  490. #endif
  491. };
  492. /* Allow for arch specific config before we boot */
  493. __weak void arch_preboot_os(void)
  494. {
  495. /* please define platform specific arch_preboot_os() */
  496. }
  497. /* Allow for board specific config before we boot */
  498. __weak void board_preboot_os(void)
  499. {
  500. /* please define board specific board_preboot_os() */
  501. }
  502. int boot_selected_os(int argc, char *const argv[], int state,
  503. bootm_headers_t *images, boot_os_fn *boot_fn)
  504. {
  505. arch_preboot_os();
  506. board_preboot_os();
  507. boot_fn(state, argc, argv, images);
  508. /* Stand-alone may return when 'autostart' is 'no' */
  509. if (images->os.type == IH_TYPE_STANDALONE ||
  510. IS_ENABLED(CONFIG_SANDBOX) ||
  511. state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
  512. return 0;
  513. bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
  514. debug("\n## Control returned to monitor - resetting...\n");
  515. return BOOTM_ERR_RESET;
  516. }
  517. boot_os_fn *bootm_os_get_boot_func(int os)
  518. {
  519. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  520. static bool relocated;
  521. if (!relocated) {
  522. int i;
  523. /* relocate boot function table */
  524. for (i = 0; i < ARRAY_SIZE(boot_os); i++)
  525. if (boot_os[i] != NULL)
  526. boot_os[i] += gd->reloc_off;
  527. relocated = true;
  528. }
  529. #endif
  530. return boot_os[os];
  531. }