bootm.c 9.5 KB

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