bootm_os.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
  237. if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
  238. bootline = getenv("bootargs");
  239. if (bootline) {
  240. ret = fdt_find_and_setprop(*of_flat_tree,
  241. "/chosen", "bootargs",
  242. bootline,
  243. strlen(bootline) + 1, 1);
  244. if (ret < 0) {
  245. printf("## ERROR: %s : %s\n", __func__,
  246. fdt_strerror(ret));
  247. return;
  248. }
  249. }
  250. } else {
  251. printf("## ERROR: %s : %s\n", __func__,
  252. fdt_strerror(ret));
  253. return;
  254. }
  255. }
  256. #endif
  257. boot_prep_vxworks(images);
  258. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  259. #if defined(CONFIG_OF_LIBFDT)
  260. printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
  261. (ulong)images->ep, (ulong)*of_flat_tree);
  262. #else
  263. printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
  264. #endif
  265. boot_jump_vxworks(images);
  266. puts("## vxWorks terminated\n");
  267. }
  268. static int do_bootm_vxworks(int flag, int argc, char * const argv[],
  269. bootm_headers_t *images)
  270. {
  271. if (flag != BOOTM_STATE_OS_GO)
  272. return 0;
  273. #if defined(CONFIG_FIT)
  274. if (!images->legacy_hdr_valid) {
  275. fit_unsupported_reset("VxWorks");
  276. return 1;
  277. }
  278. #endif
  279. do_bootvx_fdt(images);
  280. return 1;
  281. }
  282. #endif
  283. #if defined(CONFIG_CMD_ELF)
  284. static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
  285. bootm_headers_t *images)
  286. {
  287. char *local_args[2];
  288. char str[16];
  289. if (flag != BOOTM_STATE_OS_GO)
  290. return 0;
  291. #if defined(CONFIG_FIT)
  292. if (!images->legacy_hdr_valid) {
  293. fit_unsupported_reset("QNX");
  294. return 1;
  295. }
  296. #endif
  297. sprintf(str, "%lx", images->ep); /* write entry-point into string */
  298. local_args[0] = argv[0];
  299. local_args[1] = str; /* and provide it via the arguments */
  300. do_bootelf(NULL, 0, 2, local_args);
  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. static boot_os_fn *boot_os[] = {
  330. [IH_OS_U_BOOT] = do_bootm_standalone,
  331. #ifdef CONFIG_BOOTM_LINUX
  332. [IH_OS_LINUX] = do_bootm_linux,
  333. #endif
  334. #ifdef CONFIG_BOOTM_NETBSD
  335. [IH_OS_NETBSD] = do_bootm_netbsd,
  336. #endif
  337. #ifdef CONFIG_LYNXKDI
  338. [IH_OS_LYNXOS] = do_bootm_lynxkdi,
  339. #endif
  340. #ifdef CONFIG_BOOTM_RTEMS
  341. [IH_OS_RTEMS] = do_bootm_rtems,
  342. #endif
  343. #if defined(CONFIG_BOOTM_OSE)
  344. [IH_OS_OSE] = do_bootm_ose,
  345. #endif
  346. #if defined(CONFIG_BOOTM_PLAN9)
  347. [IH_OS_PLAN9] = do_bootm_plan9,
  348. #endif
  349. #if defined(CONFIG_BOOTM_VXWORKS) && \
  350. (defined(CONFIG_PPC) || defined(CONFIG_ARM))
  351. [IH_OS_VXWORKS] = do_bootm_vxworks,
  352. #endif
  353. #if defined(CONFIG_CMD_ELF)
  354. [IH_OS_QNX] = do_bootm_qnxelf,
  355. #endif
  356. #ifdef CONFIG_INTEGRITY
  357. [IH_OS_INTEGRITY] = do_bootm_integrity,
  358. #endif
  359. };
  360. /* Allow for arch specific config before we boot */
  361. __weak void arch_preboot_os(void)
  362. {
  363. /* please define platform specific arch_preboot_os() */
  364. }
  365. int boot_selected_os(int argc, char * const argv[], int state,
  366. bootm_headers_t *images, boot_os_fn *boot_fn)
  367. {
  368. arch_preboot_os();
  369. boot_fn(state, argc, argv, images);
  370. /* Stand-alone may return when 'autostart' is 'no' */
  371. if (images->os.type == IH_TYPE_STANDALONE ||
  372. state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
  373. return 0;
  374. bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
  375. #ifdef DEBUG
  376. puts("\n## Control returned to monitor - resetting...\n");
  377. #endif
  378. return BOOTM_ERR_RESET;
  379. }
  380. boot_os_fn *bootm_os_get_boot_func(int os)
  381. {
  382. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  383. static bool relocated;
  384. if (!relocated) {
  385. int i;
  386. /* relocate boot function table */
  387. for (i = 0; i < ARRAY_SIZE(boot_os); i++)
  388. if (boot_os[i] != NULL)
  389. boot_os[i] += gd->reloc_off;
  390. relocated = true;
  391. }
  392. #endif
  393. return boot_os[os];
  394. }