bootm.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* Copyright (C) 2011
  2. * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
  3. * - Added prep subcommand support
  4. * - Reorganized source - modeled after powerpc version
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Marius Groeger <mgroeger@sysgo.de>
  9. *
  10. * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. #include <common.h>
  28. #include <command.h>
  29. #include <image.h>
  30. #include <u-boot/zlib.h>
  31. #include <asm/byteorder.h>
  32. #include <fdt.h>
  33. #include <libfdt.h>
  34. #include <fdt_support.h>
  35. #include <asm/bootm.h>
  36. #include <linux/compiler.h>
  37. DECLARE_GLOBAL_DATA_PTR;
  38. #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
  39. defined(CONFIG_CMDLINE_TAG) || \
  40. defined(CONFIG_INITRD_TAG) || \
  41. defined(CONFIG_SERIAL_TAG) || \
  42. defined(CONFIG_REVISION_TAG)
  43. static struct tag *params;
  44. #endif
  45. static ulong get_sp(void)
  46. {
  47. ulong ret;
  48. asm("mov %0, sp" : "=r"(ret) : );
  49. return ret;
  50. }
  51. void arch_lmb_reserve(struct lmb *lmb)
  52. {
  53. ulong sp;
  54. /*
  55. * Booting a (Linux) kernel image
  56. *
  57. * Allocate space for command line and board info - the
  58. * address should be as high as possible within the reach of
  59. * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
  60. * memory, which means far enough below the current stack
  61. * pointer.
  62. */
  63. sp = get_sp();
  64. debug("## Current stack ends at 0x%08lx ", sp);
  65. /* adjust sp by 4K to be safe */
  66. sp -= 4096;
  67. lmb_reserve(lmb, sp,
  68. gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
  69. }
  70. #ifdef CONFIG_OF_LIBFDT
  71. static int fixup_memory_node(void *blob)
  72. {
  73. bd_t *bd = gd->bd;
  74. int bank;
  75. u64 start[CONFIG_NR_DRAM_BANKS];
  76. u64 size[CONFIG_NR_DRAM_BANKS];
  77. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  78. start[bank] = bd->bi_dram[bank].start;
  79. size[bank] = bd->bi_dram[bank].size;
  80. }
  81. return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS);
  82. }
  83. #endif
  84. static void announce_and_cleanup(void)
  85. {
  86. printf("\nStarting kernel ...\n\n");
  87. bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
  88. #ifdef CONFIG_BOOTSTAGE_FDT
  89. bootstage_fdt_add_report();
  90. #endif
  91. #ifdef CONFIG_BOOTSTAGE_REPORT
  92. bootstage_report();
  93. #endif
  94. #ifdef CONFIG_USB_DEVICE
  95. udc_disconnect();
  96. #endif
  97. cleanup_before_linux();
  98. }
  99. #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
  100. defined(CONFIG_CMDLINE_TAG) || \
  101. defined(CONFIG_INITRD_TAG) || \
  102. defined(CONFIG_SERIAL_TAG) || \
  103. defined(CONFIG_REVISION_TAG)
  104. static void setup_start_tag (bd_t *bd)
  105. {
  106. params = (struct tag *)bd->bi_boot_params;
  107. params->hdr.tag = ATAG_CORE;
  108. params->hdr.size = tag_size (tag_core);
  109. params->u.core.flags = 0;
  110. params->u.core.pagesize = 0;
  111. params->u.core.rootdev = 0;
  112. params = tag_next (params);
  113. }
  114. #endif
  115. #ifdef CONFIG_SETUP_MEMORY_TAGS
  116. static void setup_memory_tags(bd_t *bd)
  117. {
  118. int i;
  119. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  120. params->hdr.tag = ATAG_MEM;
  121. params->hdr.size = tag_size (tag_mem32);
  122. params->u.mem.start = bd->bi_dram[i].start;
  123. params->u.mem.size = bd->bi_dram[i].size;
  124. params = tag_next (params);
  125. }
  126. }
  127. #endif
  128. #ifdef CONFIG_CMDLINE_TAG
  129. static void setup_commandline_tag(bd_t *bd, char *commandline)
  130. {
  131. char *p;
  132. if (!commandline)
  133. return;
  134. /* eat leading white space */
  135. for (p = commandline; *p == ' '; p++);
  136. /* skip non-existent command lines so the kernel will still
  137. * use its default command line.
  138. */
  139. if (*p == '\0')
  140. return;
  141. params->hdr.tag = ATAG_CMDLINE;
  142. params->hdr.size =
  143. (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
  144. strcpy (params->u.cmdline.cmdline, p);
  145. params = tag_next (params);
  146. }
  147. #endif
  148. #ifdef CONFIG_INITRD_TAG
  149. static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
  150. {
  151. /* an ATAG_INITRD node tells the kernel where the compressed
  152. * ramdisk can be found. ATAG_RDIMG is a better name, actually.
  153. */
  154. params->hdr.tag = ATAG_INITRD2;
  155. params->hdr.size = tag_size (tag_initrd);
  156. params->u.initrd.start = initrd_start;
  157. params->u.initrd.size = initrd_end - initrd_start;
  158. params = tag_next (params);
  159. }
  160. #endif
  161. #ifdef CONFIG_SERIAL_TAG
  162. void setup_serial_tag(struct tag **tmp)
  163. {
  164. struct tag *params = *tmp;
  165. struct tag_serialnr serialnr;
  166. void get_board_serial(struct tag_serialnr *serialnr);
  167. get_board_serial(&serialnr);
  168. params->hdr.tag = ATAG_SERIAL;
  169. params->hdr.size = tag_size (tag_serialnr);
  170. params->u.serialnr.low = serialnr.low;
  171. params->u.serialnr.high= serialnr.high;
  172. params = tag_next (params);
  173. *tmp = params;
  174. }
  175. #endif
  176. #ifdef CONFIG_REVISION_TAG
  177. void setup_revision_tag(struct tag **in_params)
  178. {
  179. u32 rev = 0;
  180. u32 get_board_rev(void);
  181. rev = get_board_rev();
  182. params->hdr.tag = ATAG_REVISION;
  183. params->hdr.size = tag_size (tag_revision);
  184. params->u.revision.rev = rev;
  185. params = tag_next (params);
  186. }
  187. #endif
  188. #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
  189. defined(CONFIG_CMDLINE_TAG) || \
  190. defined(CONFIG_INITRD_TAG) || \
  191. defined(CONFIG_SERIAL_TAG) || \
  192. defined(CONFIG_REVISION_TAG)
  193. static void setup_end_tag(bd_t *bd)
  194. {
  195. params->hdr.tag = ATAG_NONE;
  196. params->hdr.size = 0;
  197. }
  198. #endif
  199. #ifdef CONFIG_OF_LIBFDT
  200. static int create_fdt(bootm_headers_t *images)
  201. {
  202. ulong of_size = images->ft_len;
  203. char **of_flat_tree = &images->ft_addr;
  204. ulong *initrd_start = &images->initrd_start;
  205. ulong *initrd_end = &images->initrd_end;
  206. struct lmb *lmb = &images->lmb;
  207. ulong rd_len;
  208. int ret;
  209. debug("using: FDT\n");
  210. boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
  211. rd_len = images->rd_end - images->rd_start;
  212. ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
  213. initrd_start, initrd_end);
  214. if (ret)
  215. return ret;
  216. ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
  217. if (ret)
  218. return ret;
  219. fdt_chosen(*of_flat_tree, 1);
  220. fixup_memory_node(*of_flat_tree);
  221. fdt_fixup_ethernet(*of_flat_tree);
  222. fdt_initrd(*of_flat_tree, *initrd_start, *initrd_end, 1);
  223. #ifdef CONFIG_OF_BOARD_SETUP
  224. ft_board_setup(*of_flat_tree, gd->bd);
  225. #endif
  226. return 0;
  227. }
  228. #endif
  229. __weak void setup_board_tags(struct tag **in_params) {}
  230. /* Subcommand: PREP */
  231. static void boot_prep_linux(bootm_headers_t *images)
  232. {
  233. #ifdef CONFIG_CMDLINE_TAG
  234. char *commandline = getenv("bootargs");
  235. #endif
  236. #ifdef CONFIG_OF_LIBFDT
  237. if (images->ft_len) {
  238. debug("using: FDT\n");
  239. if (create_fdt(images)) {
  240. printf("FDT creation failed! hanging...");
  241. hang();
  242. }
  243. } else
  244. #endif
  245. {
  246. #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
  247. defined(CONFIG_CMDLINE_TAG) || \
  248. defined(CONFIG_INITRD_TAG) || \
  249. defined(CONFIG_SERIAL_TAG) || \
  250. defined(CONFIG_REVISION_TAG)
  251. debug("using: ATAGS\n");
  252. setup_start_tag(gd->bd);
  253. #ifdef CONFIG_SERIAL_TAG
  254. setup_serial_tag(&params);
  255. #endif
  256. #ifdef CONFIG_CMDLINE_TAG
  257. setup_commandline_tag(gd->bd, commandline);
  258. #endif
  259. #ifdef CONFIG_REVISION_TAG
  260. setup_revision_tag(&params);
  261. #endif
  262. #ifdef CONFIG_SETUP_MEMORY_TAGS
  263. setup_memory_tags(gd->bd);
  264. #endif
  265. #ifdef CONFIG_INITRD_TAG
  266. if (images->rd_start && images->rd_end)
  267. setup_initrd_tag(gd->bd, images->rd_start,
  268. images->rd_end);
  269. #endif
  270. setup_board_tags(&params);
  271. setup_end_tag(gd->bd);
  272. #else /* all tags */
  273. printf("FDT and ATAGS support not compiled in - hanging\n");
  274. hang();
  275. #endif /* all tags */
  276. }
  277. }
  278. /* Subcommand: GO */
  279. static void boot_jump_linux(bootm_headers_t *images)
  280. {
  281. unsigned long machid = gd->bd->bi_arch_number;
  282. char *s;
  283. void (*kernel_entry)(int zero, int arch, uint params);
  284. unsigned long r2;
  285. kernel_entry = (void (*)(int, int, uint))images->ep;
  286. s = getenv("machid");
  287. if (s) {
  288. strict_strtoul(s, 16, &machid);
  289. printf("Using machid 0x%lx from environment\n", machid);
  290. }
  291. debug("## Transferring control to Linux (at address %08lx)" \
  292. "...\n", (ulong) kernel_entry);
  293. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  294. announce_and_cleanup();
  295. #ifdef CONFIG_OF_LIBFDT
  296. if (images->ft_len)
  297. r2 = (unsigned long)images->ft_addr;
  298. else
  299. #endif
  300. r2 = gd->bd->bi_boot_params;
  301. kernel_entry(0, machid, r2);
  302. }
  303. /* Main Entry point for arm bootm implementation
  304. *
  305. * Modeled after the powerpc implementation
  306. * DIFFERENCE: Instead of calling prep and go at the end
  307. * they are called if subcommand is equal 0.
  308. */
  309. int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
  310. {
  311. /* No need for those on ARM */
  312. if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
  313. return -1;
  314. if (flag & BOOTM_STATE_OS_PREP) {
  315. boot_prep_linux(images);
  316. return 0;
  317. }
  318. if (flag & BOOTM_STATE_OS_GO) {
  319. boot_jump_linux(images);
  320. return 0;
  321. }
  322. boot_prep_linux(images);
  323. boot_jump_linux(images);
  324. return 0;
  325. }
  326. #ifdef CONFIG_CMD_BOOTZ
  327. struct zimage_header {
  328. uint32_t code[9];
  329. uint32_t zi_magic;
  330. uint32_t zi_start;
  331. uint32_t zi_end;
  332. };
  333. #define LINUX_ARM_ZIMAGE_MAGIC 0x016f2818
  334. int bootz_setup(void *image, void **start, void **end)
  335. {
  336. struct zimage_header *zi = (struct zimage_header *)image;
  337. if (zi->zi_magic != LINUX_ARM_ZIMAGE_MAGIC) {
  338. puts("Bad Linux ARM zImage magic!\n");
  339. return 1;
  340. }
  341. *start = (void *)zi->zi_start;
  342. *end = (void *)zi->zi_end;
  343. debug("Kernel image @ 0x%08x [ 0x%08x - 0x%08x ]\n",
  344. (uint32_t)image, (uint32_t)*start, (uint32_t)*end);
  345. return 0;
  346. }
  347. #endif /* CONFIG_CMD_BOOTZ */