bootm.c 7.3 KB

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