bootm_os.c 12 KB

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