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 <linux/libfdt.h>
  16. #include <malloc.h>
  17. #include <mapmem.h>
  18. #include <vxworks.h>
  19. #include <tee/optee.h>
  20. DECLARE_GLOBAL_DATA_PTR;
  21. static int do_bootm_standalone(int flag, int argc, char *const argv[],
  22. bootm_headers_t *images)
  23. {
  24. char *s;
  25. int (*appl)(int, char *const[]);
  26. /* Don't start if "autostart" is set to "no" */
  27. s = env_get("autostart");
  28. if ((s != NULL) && !strcmp(s, "no")) {
  29. env_set_hex("filesize", images->os.image_len);
  30. return 0;
  31. }
  32. appl = (int (*)(int, char * const []))images->ep;
  33. appl(argc, argv);
  34. return 0;
  35. }
  36. /*******************************************************************/
  37. /* OS booting routines */
  38. /*******************************************************************/
  39. #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
  40. static void copy_args(char *dest, int argc, char *const argv[], char delim)
  41. {
  42. int i;
  43. for (i = 0; i < argc; i++) {
  44. if (i > 0)
  45. *dest++ = delim;
  46. strcpy(dest, argv[i]);
  47. dest += strlen(argv[i]);
  48. }
  49. }
  50. #endif
  51. #ifdef CONFIG_BOOTM_NETBSD
  52. static int do_bootm_netbsd(int flag, int argc, char *const argv[],
  53. bootm_headers_t *images)
  54. {
  55. void (*loader)(bd_t *, image_header_t *, char *, char *);
  56. image_header_t *os_hdr, *hdr;
  57. ulong kernel_data, kernel_len;
  58. char *cmdline;
  59. if (flag != BOOTM_STATE_OS_GO)
  60. return 0;
  61. #if defined(CONFIG_FIT)
  62. if (!images->legacy_hdr_valid) {
  63. fit_unsupported_reset("NetBSD");
  64. return 1;
  65. }
  66. #endif
  67. hdr = images->legacy_hdr_os;
  68. /*
  69. * Booting a (NetBSD) kernel image
  70. *
  71. * This process is pretty similar to a standalone application:
  72. * The (first part of an multi-) image must be a stage-2 loader,
  73. * which in turn is responsible for loading & invoking the actual
  74. * kernel. The only differences are the parameters being passed:
  75. * besides the board info strucure, the loader expects a command
  76. * line, the name of the console device, and (optionally) the
  77. * address of the original image header.
  78. */
  79. os_hdr = NULL;
  80. if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
  81. image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
  82. if (kernel_len)
  83. os_hdr = hdr;
  84. }
  85. if (argc > 0) {
  86. ulong len;
  87. int i;
  88. for (i = 0, len = 0; i < argc; i += 1)
  89. len += strlen(argv[i]) + 1;
  90. cmdline = malloc(len);
  91. copy_args(cmdline, argc, argv, ' ');
  92. } else {
  93. cmdline = env_get("bootargs");
  94. if (cmdline == NULL)
  95. cmdline = "";
  96. }
  97. loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
  98. printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
  99. (ulong)loader);
  100. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  101. /*
  102. * NetBSD Stage-2 Loader Parameters:
  103. * arg[0]: pointer to board info data
  104. * arg[1]: image load address
  105. * arg[2]: char pointer to the console device to use
  106. * arg[3]: char pointer to the boot arguments
  107. */
  108. (*loader)(gd->bd, os_hdr, "", cmdline);
  109. return 1;
  110. }
  111. #endif /* CONFIG_BOOTM_NETBSD*/
  112. #ifdef CONFIG_LYNXKDI
  113. static int do_bootm_lynxkdi(int flag, int argc, char *const argv[],
  114. bootm_headers_t *images)
  115. {
  116. image_header_t *hdr = &images->legacy_hdr_os_copy;
  117. if (flag != BOOTM_STATE_OS_GO)
  118. return 0;
  119. #if defined(CONFIG_FIT)
  120. if (!images->legacy_hdr_valid) {
  121. fit_unsupported_reset("Lynx");
  122. return 1;
  123. }
  124. #endif
  125. lynxkdi_boot((image_header_t *)hdr);
  126. return 1;
  127. }
  128. #endif /* CONFIG_LYNXKDI */
  129. #ifdef CONFIG_BOOTM_RTEMS
  130. static int do_bootm_rtems(int flag, int argc, char *const argv[],
  131. bootm_headers_t *images)
  132. {
  133. void (*entry_point)(bd_t *);
  134. if (flag != BOOTM_STATE_OS_GO)
  135. return 0;
  136. #if defined(CONFIG_FIT)
  137. if (!images->legacy_hdr_valid) {
  138. fit_unsupported_reset("RTEMS");
  139. return 1;
  140. }
  141. #endif
  142. entry_point = (void (*)(bd_t *))images->ep;
  143. printf("## Transferring control to RTEMS (at address %08lx) ...\n",
  144. (ulong)entry_point);
  145. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  146. /*
  147. * RTEMS Parameters:
  148. * r3: ptr to board info data
  149. */
  150. (*entry_point)(gd->bd);
  151. return 1;
  152. }
  153. #endif /* CONFIG_BOOTM_RTEMS */
  154. #if defined(CONFIG_BOOTM_OSE)
  155. static int do_bootm_ose(int flag, int argc, char *const argv[],
  156. bootm_headers_t *images)
  157. {
  158. void (*entry_point)(void);
  159. if (flag != BOOTM_STATE_OS_GO)
  160. return 0;
  161. #if defined(CONFIG_FIT)
  162. if (!images->legacy_hdr_valid) {
  163. fit_unsupported_reset("OSE");
  164. return 1;
  165. }
  166. #endif
  167. entry_point = (void (*)(void))images->ep;
  168. printf("## Transferring control to OSE (at address %08lx) ...\n",
  169. (ulong)entry_point);
  170. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  171. /*
  172. * OSE Parameters:
  173. * None
  174. */
  175. (*entry_point)();
  176. return 1;
  177. }
  178. #endif /* CONFIG_BOOTM_OSE */
  179. #if defined(CONFIG_BOOTM_PLAN9)
  180. static int do_bootm_plan9(int flag, int argc, char *const argv[],
  181. bootm_headers_t *images)
  182. {
  183. void (*entry_point)(void);
  184. char *s;
  185. if (flag != BOOTM_STATE_OS_GO)
  186. return 0;
  187. #if defined(CONFIG_FIT)
  188. if (!images->legacy_hdr_valid) {
  189. fit_unsupported_reset("Plan 9");
  190. return 1;
  191. }
  192. #endif
  193. /* See README.plan9 */
  194. s = env_get("confaddr");
  195. if (s != NULL) {
  196. char *confaddr = (char *)simple_strtoul(s, NULL, 16);
  197. if (argc > 0) {
  198. copy_args(confaddr, argc, argv, '\n');
  199. } else {
  200. s = env_get("bootargs");
  201. if (s != NULL)
  202. strcpy(confaddr, s);
  203. }
  204. }
  205. entry_point = (void (*)(void))images->ep;
  206. printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
  207. (ulong)entry_point);
  208. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  209. /*
  210. * Plan 9 Parameters:
  211. * None
  212. */
  213. (*entry_point)();
  214. return 1;
  215. }
  216. #endif /* CONFIG_BOOTM_PLAN9 */
  217. #if defined(CONFIG_BOOTM_VXWORKS) && \
  218. (defined(CONFIG_PPC) || defined(CONFIG_ARM))
  219. static void do_bootvx_fdt(bootm_headers_t *images)
  220. {
  221. #if defined(CONFIG_OF_LIBFDT)
  222. int ret;
  223. char *bootline;
  224. ulong of_size = images->ft_len;
  225. char **of_flat_tree = &images->ft_addr;
  226. struct lmb *lmb = &images->lmb;
  227. if (*of_flat_tree) {
  228. boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
  229. ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
  230. if (ret)
  231. return;
  232. /* Update ethernet nodes */
  233. fdt_fixup_ethernet(*of_flat_tree);
  234. ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
  235. if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
  236. bootline = env_get("bootargs");
  237. if (bootline) {
  238. ret = fdt_find_and_setprop(*of_flat_tree,
  239. "/chosen", "bootargs",
  240. bootline,
  241. strlen(bootline) + 1, 1);
  242. if (ret < 0) {
  243. printf("## ERROR: %s : %s\n", __func__,
  244. fdt_strerror(ret));
  245. return;
  246. }
  247. }
  248. } else {
  249. printf("## ERROR: %s : %s\n", __func__,
  250. fdt_strerror(ret));
  251. return;
  252. }
  253. }
  254. #endif
  255. boot_prep_vxworks(images);
  256. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  257. #if defined(CONFIG_OF_LIBFDT)
  258. printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
  259. (ulong)images->ep, (ulong)*of_flat_tree);
  260. #else
  261. printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
  262. #endif
  263. boot_jump_vxworks(images);
  264. puts("## vxWorks terminated\n");
  265. }
  266. static int do_bootm_vxworks_legacy(int flag, int argc, char *const argv[],
  267. bootm_headers_t *images)
  268. {
  269. if (flag != BOOTM_STATE_OS_GO)
  270. return 0;
  271. #if defined(CONFIG_FIT)
  272. if (!images->legacy_hdr_valid) {
  273. fit_unsupported_reset("VxWorks");
  274. return 1;
  275. }
  276. #endif
  277. do_bootvx_fdt(images);
  278. return 1;
  279. }
  280. int do_bootm_vxworks(int flag, int argc, char *const argv[],
  281. bootm_headers_t *images)
  282. {
  283. char *bootargs;
  284. int pos;
  285. unsigned long vxflags;
  286. bool std_dtb = false;
  287. /* get bootargs env */
  288. bootargs = env_get("bootargs");
  289. if (bootargs != NULL) {
  290. for (pos = 0; pos < strlen(bootargs); pos++) {
  291. /* find f=0xnumber flag */
  292. if ((bootargs[pos] == '=') && (pos >= 1) &&
  293. (bootargs[pos - 1] == 'f')) {
  294. vxflags = simple_strtoul(&bootargs[pos + 1],
  295. NULL, 16);
  296. if (vxflags & VXWORKS_SYSFLG_STD_DTB)
  297. std_dtb = true;
  298. }
  299. }
  300. }
  301. if (std_dtb) {
  302. if (flag & BOOTM_STATE_OS_PREP)
  303. printf(" Using standard DTB\n");
  304. return do_bootm_linux(flag, argc, argv, images);
  305. } else {
  306. if (flag & BOOTM_STATE_OS_PREP)
  307. printf(" !!! WARNING !!! Using legacy DTB\n");
  308. return do_bootm_vxworks_legacy(flag, argc, argv, images);
  309. }
  310. }
  311. #endif
  312. #if defined(CONFIG_CMD_ELF)
  313. static int do_bootm_qnxelf(int flag, int argc, char *const argv[],
  314. bootm_headers_t *images)
  315. {
  316. char *local_args[2];
  317. char str[16];
  318. int dcache;
  319. if (flag != BOOTM_STATE_OS_GO)
  320. return 0;
  321. #if defined(CONFIG_FIT)
  322. if (!images->legacy_hdr_valid) {
  323. fit_unsupported_reset("QNX");
  324. return 1;
  325. }
  326. #endif
  327. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  328. local_args[0] = argv[0];
  329. local_args[1] = str; /* and provide it via the arguments */
  330. /*
  331. * QNX images require the data cache is disabled.
  332. */
  333. dcache = dcache_status();
  334. if (dcache)
  335. dcache_disable();
  336. do_bootelf(NULL, 0, 2, local_args);
  337. if (dcache)
  338. dcache_enable();
  339. return 1;
  340. }
  341. #endif
  342. #ifdef CONFIG_INTEGRITY
  343. static int do_bootm_integrity(int flag, int argc, char *const argv[],
  344. bootm_headers_t *images)
  345. {
  346. void (*entry_point)(void);
  347. if (flag != BOOTM_STATE_OS_GO)
  348. return 0;
  349. #if defined(CONFIG_FIT)
  350. if (!images->legacy_hdr_valid) {
  351. fit_unsupported_reset("INTEGRITY");
  352. return 1;
  353. }
  354. #endif
  355. entry_point = (void (*)(void))images->ep;
  356. printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
  357. (ulong)entry_point);
  358. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  359. /*
  360. * INTEGRITY Parameters:
  361. * None
  362. */
  363. (*entry_point)();
  364. return 1;
  365. }
  366. #endif
  367. #ifdef CONFIG_BOOTM_OPENRTOS
  368. static int do_bootm_openrtos(int flag, int argc, char *const argv[],
  369. bootm_headers_t *images)
  370. {
  371. void (*entry_point)(void);
  372. if (flag != BOOTM_STATE_OS_GO)
  373. return 0;
  374. entry_point = (void (*)(void))images->ep;
  375. printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
  376. (ulong)entry_point);
  377. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  378. /*
  379. * OpenRTOS Parameters:
  380. * None
  381. */
  382. (*entry_point)();
  383. return 1;
  384. }
  385. #endif
  386. #ifdef CONFIG_BOOTM_OPTEE
  387. static int do_bootm_tee(int flag, int argc, char *const argv[],
  388. bootm_headers_t *images)
  389. {
  390. int ret;
  391. /* Verify OS type */
  392. if (images->os.os != IH_OS_TEE) {
  393. return 1;
  394. };
  395. /* Validate OPTEE header */
  396. ret = optee_verify_bootm_image(images->os.image_start,
  397. images->os.load,
  398. images->os.image_len);
  399. if (ret)
  400. return ret;
  401. /* Locate FDT etc */
  402. ret = bootm_find_images(flag, argc, argv);
  403. if (ret)
  404. return ret;
  405. /* From here we can run the regular linux boot path */
  406. return do_bootm_linux(flag, argc, argv, images);
  407. }
  408. #endif
  409. #ifdef CONFIG_BOOTM_EFI
  410. static int do_bootm_efi(int flag, int argc, char *const argv[],
  411. bootm_headers_t *images)
  412. {
  413. int ret;
  414. efi_status_t efi_ret;
  415. void *image_buf;
  416. if (flag != BOOTM_STATE_OS_GO)
  417. return 0;
  418. /* Locate FDT, if provided */
  419. ret = bootm_find_images(flag, argc, argv);
  420. if (ret)
  421. return ret;
  422. /* Initialize EFI drivers */
  423. efi_ret = efi_init_obj_list();
  424. if (efi_ret != EFI_SUCCESS) {
  425. printf("## Failed to initialize UEFI sub-system: r = %lu\n",
  426. efi_ret & ~EFI_ERROR_MASK);
  427. return 1;
  428. }
  429. /* Install device tree */
  430. efi_ret = efi_install_fdt(images->ft_len
  431. ? images->ft_addr : EFI_FDT_USE_INTERNAL);
  432. if (efi_ret != EFI_SUCCESS) {
  433. printf("## Failed to install device tree: r = %lu\n",
  434. efi_ret & ~EFI_ERROR_MASK);
  435. return 1;
  436. }
  437. /* Run EFI image */
  438. printf("## Transferring control to EFI (at address %08lx) ...\n",
  439. images->ep);
  440. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  441. image_buf = map_sysmem(images->ep, images->os.image_len);
  442. efi_ret = efi_run_image(image_buf, images->os.image_len);
  443. if (efi_ret != EFI_SUCCESS) {
  444. printf("## Failed to run EFI image: r = %lu\n",
  445. efi_ret & ~EFI_ERROR_MASK);
  446. return 1;
  447. }
  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. }