bootm_os.c 12 KB

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