bootm.c 8.5 KB

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