bootm_os.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * (C) Copyright 2000-2009
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <bootm.h>
  9. #include <fdt_support.h>
  10. #include <libfdt.h>
  11. #include <malloc.h>
  12. #include <vxworks.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 = getenv("autostart");
  21. if ((s != NULL) && !strcmp(s, "no")) {
  22. setenv_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 *consdev;
  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. consdev = "";
  80. #if defined(CONFIG_8xx_CONS_SMC1)
  81. consdev = "smc1";
  82. #elif defined(CONFIG_8xx_CONS_SMC2)
  83. consdev = "smc2";
  84. #elif defined(CONFIG_8xx_CONS_SCC2)
  85. consdev = "scc2";
  86. #elif defined(CONFIG_8xx_CONS_SCC3)
  87. consdev = "scc3";
  88. #endif
  89. if (argc > 0) {
  90. ulong len;
  91. int i;
  92. for (i = 0, len = 0; i < argc; i += 1)
  93. len += strlen(argv[i]) + 1;
  94. cmdline = malloc(len);
  95. copy_args(cmdline, argc, argv, ' ');
  96. } else {
  97. cmdline = getenv("bootargs");
  98. if (cmdline == NULL)
  99. cmdline = "";
  100. }
  101. loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
  102. printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
  103. (ulong)loader);
  104. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  105. /*
  106. * NetBSD Stage-2 Loader Parameters:
  107. * arg[0]: pointer to board info data
  108. * arg[1]: image load address
  109. * arg[2]: char pointer to the console device to use
  110. * arg[3]: char pointer to the boot arguments
  111. */
  112. (*loader)(gd->bd, os_hdr, consdev, cmdline);
  113. return 1;
  114. }
  115. #endif /* CONFIG_BOOTM_NETBSD*/
  116. #ifdef CONFIG_LYNXKDI
  117. static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
  118. bootm_headers_t *images)
  119. {
  120. image_header_t *hdr = &images->legacy_hdr_os_copy;
  121. if (flag != BOOTM_STATE_OS_GO)
  122. return 0;
  123. #if defined(CONFIG_FIT)
  124. if (!images->legacy_hdr_valid) {
  125. fit_unsupported_reset("Lynx");
  126. return 1;
  127. }
  128. #endif
  129. lynxkdi_boot((image_header_t *)hdr);
  130. return 1;
  131. }
  132. #endif /* CONFIG_LYNXKDI */
  133. #ifdef CONFIG_BOOTM_RTEMS
  134. static int do_bootm_rtems(int flag, int argc, char * const argv[],
  135. bootm_headers_t *images)
  136. {
  137. void (*entry_point)(bd_t *);
  138. if (flag != BOOTM_STATE_OS_GO)
  139. return 0;
  140. #if defined(CONFIG_FIT)
  141. if (!images->legacy_hdr_valid) {
  142. fit_unsupported_reset("RTEMS");
  143. return 1;
  144. }
  145. #endif
  146. entry_point = (void (*)(bd_t *))images->ep;
  147. printf("## Transferring control to RTEMS (at address %08lx) ...\n",
  148. (ulong)entry_point);
  149. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  150. /*
  151. * RTEMS Parameters:
  152. * r3: ptr to board info data
  153. */
  154. (*entry_point)(gd->bd);
  155. return 1;
  156. }
  157. #endif /* CONFIG_BOOTM_RTEMS */
  158. #if defined(CONFIG_BOOTM_OSE)
  159. static int do_bootm_ose(int flag, int argc, char * const argv[],
  160. bootm_headers_t *images)
  161. {
  162. void (*entry_point)(void);
  163. if (flag != BOOTM_STATE_OS_GO)
  164. return 0;
  165. #if defined(CONFIG_FIT)
  166. if (!images->legacy_hdr_valid) {
  167. fit_unsupported_reset("OSE");
  168. return 1;
  169. }
  170. #endif
  171. entry_point = (void (*)(void))images->ep;
  172. printf("## Transferring control to OSE (at address %08lx) ...\n",
  173. (ulong)entry_point);
  174. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  175. /*
  176. * OSE Parameters:
  177. * None
  178. */
  179. (*entry_point)();
  180. return 1;
  181. }
  182. #endif /* CONFIG_BOOTM_OSE */
  183. #if defined(CONFIG_BOOTM_PLAN9)
  184. static int do_bootm_plan9(int flag, int argc, char * const argv[],
  185. bootm_headers_t *images)
  186. {
  187. void (*entry_point)(void);
  188. char *s;
  189. if (flag != BOOTM_STATE_OS_GO)
  190. return 0;
  191. #if defined(CONFIG_FIT)
  192. if (!images->legacy_hdr_valid) {
  193. fit_unsupported_reset("Plan 9");
  194. return 1;
  195. }
  196. #endif
  197. /* See README.plan9 */
  198. s = getenv("confaddr");
  199. if (s != NULL) {
  200. char *confaddr = (char *)simple_strtoul(s, NULL, 16);
  201. if (argc > 0) {
  202. copy_args(confaddr, argc, argv, '\n');
  203. } else {
  204. s = getenv("bootargs");
  205. if (s != NULL)
  206. strcpy(confaddr, s);
  207. }
  208. }
  209. entry_point = (void (*)(void))images->ep;
  210. printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
  211. (ulong)entry_point);
  212. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  213. /*
  214. * Plan 9 Parameters:
  215. * None
  216. */
  217. (*entry_point)();
  218. return 1;
  219. }
  220. #endif /* CONFIG_BOOTM_PLAN9 */
  221. #if defined(CONFIG_BOOTM_VXWORKS) && \
  222. (defined(CONFIG_PPC) || defined(CONFIG_ARM))
  223. void do_bootvx_fdt(bootm_headers_t *images)
  224. {
  225. #if defined(CONFIG_OF_LIBFDT)
  226. int ret;
  227. char *bootline;
  228. ulong of_size = images->ft_len;
  229. char **of_flat_tree = &images->ft_addr;
  230. struct lmb *lmb = &images->lmb;
  231. if (*of_flat_tree) {
  232. boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
  233. ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
  234. if (ret)
  235. return;
  236. fdt_fixup_ethernet(*of_flat_tree);
  237. ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
  238. if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
  239. bootline = getenv("bootargs");
  240. if (bootline) {
  241. ret = fdt_find_and_setprop(*of_flat_tree,
  242. "/chosen", "bootargs",
  243. bootline,
  244. strlen(bootline) + 1, 1);
  245. if (ret < 0) {
  246. printf("## ERROR: %s : %s\n", __func__,
  247. fdt_strerror(ret));
  248. return;
  249. }
  250. }
  251. } else {
  252. printf("## ERROR: %s : %s\n", __func__,
  253. fdt_strerror(ret));
  254. return;
  255. }
  256. }
  257. #endif
  258. boot_prep_vxworks(images);
  259. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  260. #if defined(CONFIG_OF_LIBFDT)
  261. printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
  262. (ulong)images->ep, (ulong)*of_flat_tree);
  263. #else
  264. printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
  265. #endif
  266. boot_jump_vxworks(images);
  267. puts("## vxWorks terminated\n");
  268. }
  269. static int do_bootm_vxworks(int flag, int argc, char * const argv[],
  270. bootm_headers_t *images)
  271. {
  272. if (flag != BOOTM_STATE_OS_GO)
  273. return 0;
  274. #if defined(CONFIG_FIT)
  275. if (!images->legacy_hdr_valid) {
  276. fit_unsupported_reset("VxWorks");
  277. return 1;
  278. }
  279. #endif
  280. do_bootvx_fdt(images);
  281. return 1;
  282. }
  283. #endif
  284. #if defined(CONFIG_CMD_ELF)
  285. static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
  286. bootm_headers_t *images)
  287. {
  288. char *local_args[2];
  289. char str[16];
  290. int dcache;
  291. if (flag != BOOTM_STATE_OS_GO)
  292. return 0;
  293. #if defined(CONFIG_FIT)
  294. if (!images->legacy_hdr_valid) {
  295. fit_unsupported_reset("QNX");
  296. return 1;
  297. }
  298. #endif
  299. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  300. local_args[0] = argv[0];
  301. local_args[1] = str; /* and provide it via the arguments */
  302. /*
  303. * QNX images require the data cache is disabled.
  304. */
  305. dcache = dcache_status();
  306. if (dcache)
  307. dcache_disable();
  308. do_bootelf(NULL, 0, 2, local_args);
  309. if (dcache)
  310. dcache_enable();
  311. return 1;
  312. }
  313. #endif
  314. #ifdef CONFIG_INTEGRITY
  315. static int do_bootm_integrity(int flag, int argc, char * const argv[],
  316. bootm_headers_t *images)
  317. {
  318. void (*entry_point)(void);
  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("INTEGRITY");
  324. return 1;
  325. }
  326. #endif
  327. entry_point = (void (*)(void))images->ep;
  328. printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
  329. (ulong)entry_point);
  330. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  331. /*
  332. * INTEGRITY Parameters:
  333. * None
  334. */
  335. (*entry_point)();
  336. return 1;
  337. }
  338. #endif
  339. #ifdef CONFIG_BOOTM_OPENRTOS
  340. static int do_bootm_openrtos(int flag, int argc, char * const argv[],
  341. bootm_headers_t *images)
  342. {
  343. void (*entry_point)(void);
  344. if (flag != BOOTM_STATE_OS_GO)
  345. return 0;
  346. entry_point = (void (*)(void))images->ep;
  347. printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
  348. (ulong)entry_point);
  349. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  350. /*
  351. * OpenRTOS Parameters:
  352. * None
  353. */
  354. (*entry_point)();
  355. return 1;
  356. }
  357. #endif
  358. static boot_os_fn *boot_os[] = {
  359. [IH_OS_U_BOOT] = do_bootm_standalone,
  360. #ifdef CONFIG_BOOTM_LINUX
  361. [IH_OS_LINUX] = do_bootm_linux,
  362. #endif
  363. #ifdef CONFIG_BOOTM_NETBSD
  364. [IH_OS_NETBSD] = do_bootm_netbsd,
  365. #endif
  366. #ifdef CONFIG_LYNXKDI
  367. [IH_OS_LYNXOS] = do_bootm_lynxkdi,
  368. #endif
  369. #ifdef CONFIG_BOOTM_RTEMS
  370. [IH_OS_RTEMS] = do_bootm_rtems,
  371. #endif
  372. #if defined(CONFIG_BOOTM_OSE)
  373. [IH_OS_OSE] = do_bootm_ose,
  374. #endif
  375. #if defined(CONFIG_BOOTM_PLAN9)
  376. [IH_OS_PLAN9] = do_bootm_plan9,
  377. #endif
  378. #if defined(CONFIG_BOOTM_VXWORKS) && \
  379. (defined(CONFIG_PPC) || defined(CONFIG_ARM))
  380. [IH_OS_VXWORKS] = do_bootm_vxworks,
  381. #endif
  382. #if defined(CONFIG_CMD_ELF)
  383. [IH_OS_QNX] = do_bootm_qnxelf,
  384. #endif
  385. #ifdef CONFIG_INTEGRITY
  386. [IH_OS_INTEGRITY] = do_bootm_integrity,
  387. #endif
  388. #ifdef CONFIG_BOOTM_OPENRTOS
  389. [IH_OS_OPENRTOS] = do_bootm_openrtos,
  390. #endif
  391. };
  392. /* Allow for arch specific config before we boot */
  393. __weak void arch_preboot_os(void)
  394. {
  395. /* please define platform specific arch_preboot_os() */
  396. }
  397. int boot_selected_os(int argc, char * const argv[], int state,
  398. bootm_headers_t *images, boot_os_fn *boot_fn)
  399. {
  400. arch_preboot_os();
  401. boot_fn(state, argc, argv, images);
  402. /* Stand-alone may return when 'autostart' is 'no' */
  403. if (images->os.type == IH_TYPE_STANDALONE ||
  404. IS_ENABLED(CONFIG_SANDBOX) ||
  405. state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
  406. return 0;
  407. bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
  408. debug("\n## Control returned to monitor - resetting...\n");
  409. return BOOTM_ERR_RESET;
  410. }
  411. boot_os_fn *bootm_os_get_boot_func(int os)
  412. {
  413. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  414. static bool relocated;
  415. if (!relocated) {
  416. int i;
  417. /* relocate boot function table */
  418. for (i = 0; i < ARRAY_SIZE(boot_os); i++)
  419. if (boot_os[i] != NULL)
  420. boot_os[i] += gd->reloc_off;
  421. relocated = true;
  422. }
  423. #endif
  424. return boot_os[os];
  425. }