bootm_os.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 *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 = getenv("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 = getenv("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 = getenv("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. ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
  226. if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
  227. bootline = getenv("bootargs");
  228. if (bootline) {
  229. ret = fdt_find_and_setprop(*of_flat_tree,
  230. "/chosen", "bootargs",
  231. bootline,
  232. strlen(bootline) + 1, 1);
  233. if (ret < 0) {
  234. printf("## ERROR: %s : %s\n", __func__,
  235. fdt_strerror(ret));
  236. return;
  237. }
  238. }
  239. } else {
  240. printf("## ERROR: %s : %s\n", __func__,
  241. fdt_strerror(ret));
  242. return;
  243. }
  244. }
  245. #endif
  246. boot_prep_vxworks(images);
  247. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  248. #if defined(CONFIG_OF_LIBFDT)
  249. printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
  250. (ulong)images->ep, (ulong)*of_flat_tree);
  251. #else
  252. printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
  253. #endif
  254. boot_jump_vxworks(images);
  255. puts("## vxWorks terminated\n");
  256. }
  257. static int do_bootm_vxworks(int flag, int argc, char * const argv[],
  258. bootm_headers_t *images)
  259. {
  260. if (flag != BOOTM_STATE_OS_GO)
  261. return 0;
  262. #if defined(CONFIG_FIT)
  263. if (!images->legacy_hdr_valid) {
  264. fit_unsupported_reset("VxWorks");
  265. return 1;
  266. }
  267. #endif
  268. do_bootvx_fdt(images);
  269. return 1;
  270. }
  271. #endif
  272. #if defined(CONFIG_CMD_ELF)
  273. static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
  274. bootm_headers_t *images)
  275. {
  276. char *local_args[2];
  277. char str[16];
  278. int dcache;
  279. if (flag != BOOTM_STATE_OS_GO)
  280. return 0;
  281. #if defined(CONFIG_FIT)
  282. if (!images->legacy_hdr_valid) {
  283. fit_unsupported_reset("QNX");
  284. return 1;
  285. }
  286. #endif
  287. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  288. local_args[0] = argv[0];
  289. local_args[1] = str; /* and provide it via the arguments */
  290. /*
  291. * QNX images require the data cache is disabled.
  292. */
  293. dcache = dcache_status();
  294. if (dcache)
  295. dcache_disable();
  296. do_bootelf(NULL, 0, 2, local_args);
  297. if (dcache)
  298. dcache_enable();
  299. return 1;
  300. }
  301. #endif
  302. #ifdef CONFIG_INTEGRITY
  303. static int do_bootm_integrity(int flag, int argc, char * const argv[],
  304. bootm_headers_t *images)
  305. {
  306. void (*entry_point)(void);
  307. if (flag != BOOTM_STATE_OS_GO)
  308. return 0;
  309. #if defined(CONFIG_FIT)
  310. if (!images->legacy_hdr_valid) {
  311. fit_unsupported_reset("INTEGRITY");
  312. return 1;
  313. }
  314. #endif
  315. entry_point = (void (*)(void))images->ep;
  316. printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
  317. (ulong)entry_point);
  318. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  319. /*
  320. * INTEGRITY Parameters:
  321. * None
  322. */
  323. (*entry_point)();
  324. return 1;
  325. }
  326. #endif
  327. #ifdef CONFIG_BOOTM_OPENRTOS
  328. static int do_bootm_openrtos(int flag, int argc, char * const argv[],
  329. bootm_headers_t *images)
  330. {
  331. void (*entry_point)(void);
  332. if (flag != BOOTM_STATE_OS_GO)
  333. return 0;
  334. entry_point = (void (*)(void))images->ep;
  335. printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
  336. (ulong)entry_point);
  337. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  338. /*
  339. * OpenRTOS Parameters:
  340. * None
  341. */
  342. (*entry_point)();
  343. return 1;
  344. }
  345. #endif
  346. static boot_os_fn *boot_os[] = {
  347. [IH_OS_U_BOOT] = do_bootm_standalone,
  348. #ifdef CONFIG_BOOTM_LINUX
  349. [IH_OS_LINUX] = do_bootm_linux,
  350. #endif
  351. #ifdef CONFIG_BOOTM_NETBSD
  352. [IH_OS_NETBSD] = do_bootm_netbsd,
  353. #endif
  354. #ifdef CONFIG_LYNXKDI
  355. [IH_OS_LYNXOS] = do_bootm_lynxkdi,
  356. #endif
  357. #ifdef CONFIG_BOOTM_RTEMS
  358. [IH_OS_RTEMS] = do_bootm_rtems,
  359. #endif
  360. #if defined(CONFIG_BOOTM_OSE)
  361. [IH_OS_OSE] = do_bootm_ose,
  362. #endif
  363. #if defined(CONFIG_BOOTM_PLAN9)
  364. [IH_OS_PLAN9] = do_bootm_plan9,
  365. #endif
  366. #if defined(CONFIG_BOOTM_VXWORKS) && \
  367. (defined(CONFIG_PPC) || defined(CONFIG_ARM))
  368. [IH_OS_VXWORKS] = do_bootm_vxworks,
  369. #endif
  370. #if defined(CONFIG_CMD_ELF)
  371. [IH_OS_QNX] = do_bootm_qnxelf,
  372. #endif
  373. #ifdef CONFIG_INTEGRITY
  374. [IH_OS_INTEGRITY] = do_bootm_integrity,
  375. #endif
  376. #ifdef CONFIG_BOOTM_OPENRTOS
  377. [IH_OS_OPENRTOS] = do_bootm_openrtos,
  378. #endif
  379. };
  380. /* Allow for arch specific config before we boot */
  381. __weak void arch_preboot_os(void)
  382. {
  383. /* please define platform specific arch_preboot_os() */
  384. }
  385. int boot_selected_os(int argc, char * const argv[], int state,
  386. bootm_headers_t *images, boot_os_fn *boot_fn)
  387. {
  388. arch_preboot_os();
  389. boot_fn(state, argc, argv, images);
  390. /* Stand-alone may return when 'autostart' is 'no' */
  391. if (images->os.type == IH_TYPE_STANDALONE ||
  392. IS_ENABLED(CONFIG_SANDBOX) ||
  393. state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
  394. return 0;
  395. bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
  396. debug("\n## Control returned to monitor - resetting...\n");
  397. return BOOTM_ERR_RESET;
  398. }
  399. boot_os_fn *bootm_os_get_boot_func(int os)
  400. {
  401. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  402. static bool relocated;
  403. if (!relocated) {
  404. int i;
  405. /* relocate boot function table */
  406. for (i = 0; i < ARRAY_SIZE(boot_os); i++)
  407. if (boot_os[i] != NULL)
  408. boot_os[i] += gd->reloc_off;
  409. relocated = true;
  410. }
  411. #endif
  412. return boot_os[os];
  413. }