bootm.c 7.6 KB

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