bootm_os.c 11 KB

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