bootm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. 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 1K to be safe */
  65. sp -= 1024;
  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_REPORT
  88. bootstage_report();
  89. #endif
  90. #ifdef CONFIG_USB_DEVICE
  91. udc_disconnect();
  92. #endif
  93. cleanup_before_linux();
  94. }
  95. #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
  96. defined(CONFIG_CMDLINE_TAG) || \
  97. defined(CONFIG_INITRD_TAG) || \
  98. defined(CONFIG_SERIAL_TAG) || \
  99. defined(CONFIG_REVISION_TAG)
  100. static void setup_start_tag (bd_t *bd)
  101. {
  102. params = (struct tag *)bd->bi_boot_params;
  103. params->hdr.tag = ATAG_CORE;
  104. params->hdr.size = tag_size (tag_core);
  105. params->u.core.flags = 0;
  106. params->u.core.pagesize = 0;
  107. params->u.core.rootdev = 0;
  108. params = tag_next (params);
  109. }
  110. #endif
  111. #ifdef CONFIG_SETUP_MEMORY_TAGS
  112. static void setup_memory_tags(bd_t *bd)
  113. {
  114. int i;
  115. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  116. params->hdr.tag = ATAG_MEM;
  117. params->hdr.size = tag_size (tag_mem32);
  118. params->u.mem.start = bd->bi_dram[i].start;
  119. params->u.mem.size = bd->bi_dram[i].size;
  120. params = tag_next (params);
  121. }
  122. }
  123. #endif
  124. #ifdef CONFIG_CMDLINE_TAG
  125. static void setup_commandline_tag(bd_t *bd, char *commandline)
  126. {
  127. char *p;
  128. if (!commandline)
  129. return;
  130. /* eat leading white space */
  131. for (p = commandline; *p == ' '; p++);
  132. /* skip non-existent command lines so the kernel will still
  133. * use its default command line.
  134. */
  135. if (*p == '\0')
  136. return;
  137. params->hdr.tag = ATAG_CMDLINE;
  138. params->hdr.size =
  139. (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
  140. strcpy (params->u.cmdline.cmdline, p);
  141. params = tag_next (params);
  142. }
  143. #endif
  144. #ifdef CONFIG_INITRD_TAG
  145. static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
  146. {
  147. /* an ATAG_INITRD node tells the kernel where the compressed
  148. * ramdisk can be found. ATAG_RDIMG is a better name, actually.
  149. */
  150. params->hdr.tag = ATAG_INITRD2;
  151. params->hdr.size = tag_size (tag_initrd);
  152. params->u.initrd.start = initrd_start;
  153. params->u.initrd.size = initrd_end - initrd_start;
  154. params = tag_next (params);
  155. }
  156. #endif
  157. #ifdef CONFIG_SERIAL_TAG
  158. void setup_serial_tag(struct tag **tmp)
  159. {
  160. struct tag *params = *tmp;
  161. struct tag_serialnr serialnr;
  162. void get_board_serial(struct tag_serialnr *serialnr);
  163. get_board_serial(&serialnr);
  164. params->hdr.tag = ATAG_SERIAL;
  165. params->hdr.size = tag_size (tag_serialnr);
  166. params->u.serialnr.low = serialnr.low;
  167. params->u.serialnr.high= serialnr.high;
  168. params = tag_next (params);
  169. *tmp = params;
  170. }
  171. #endif
  172. #ifdef CONFIG_REVISION_TAG
  173. void setup_revision_tag(struct tag **in_params)
  174. {
  175. u32 rev = 0;
  176. u32 get_board_rev(void);
  177. rev = get_board_rev();
  178. params->hdr.tag = ATAG_REVISION;
  179. params->hdr.size = tag_size (tag_revision);
  180. params->u.revision.rev = rev;
  181. params = tag_next (params);
  182. }
  183. #endif
  184. #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
  185. defined(CONFIG_CMDLINE_TAG) || \
  186. defined(CONFIG_INITRD_TAG) || \
  187. defined(CONFIG_SERIAL_TAG) || \
  188. defined(CONFIG_REVISION_TAG)
  189. static void setup_end_tag(bd_t *bd)
  190. {
  191. params->hdr.tag = ATAG_NONE;
  192. params->hdr.size = 0;
  193. }
  194. #endif
  195. #ifdef CONFIG_OF_LIBFDT
  196. static int create_fdt(bootm_headers_t *images)
  197. {
  198. ulong of_size = images->ft_len;
  199. char **of_flat_tree = &images->ft_addr;
  200. ulong *initrd_start = &images->initrd_start;
  201. ulong *initrd_end = &images->initrd_end;
  202. struct lmb *lmb = &images->lmb;
  203. ulong rd_len;
  204. int ret;
  205. debug("using: FDT\n");
  206. boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
  207. rd_len = images->rd_end - images->rd_start;
  208. ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
  209. initrd_start, initrd_end);
  210. if (ret)
  211. return ret;
  212. ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
  213. if (ret)
  214. return ret;
  215. fdt_chosen(*of_flat_tree, 1);
  216. fixup_memory_node(*of_flat_tree);
  217. fdt_initrd(*of_flat_tree, *initrd_start, *initrd_end, 1);
  218. return 0;
  219. }
  220. #endif
  221. /* Subcommand: PREP */
  222. static void boot_prep_linux(bootm_headers_t *images)
  223. {
  224. #ifdef CONFIG_CMDLINE_TAG
  225. char *commandline = getenv("bootargs");
  226. #endif
  227. #ifdef CONFIG_OF_LIBFDT
  228. if (images->ft_len) {
  229. debug("using: FDT\n");
  230. if (create_fdt(images)) {
  231. printf("FDT creation failed! hanging...");
  232. hang();
  233. }
  234. } else
  235. #endif
  236. {
  237. #if defined(CONFIG_SETUP_MEMORY_TAGS) || \
  238. defined(CONFIG_CMDLINE_TAG) || \
  239. defined(CONFIG_INITRD_TAG) || \
  240. defined(CONFIG_SERIAL_TAG) || \
  241. defined(CONFIG_REVISION_TAG)
  242. debug("using: ATAGS\n");
  243. setup_start_tag(gd->bd);
  244. #ifdef CONFIG_SERIAL_TAG
  245. setup_serial_tag(&params);
  246. #endif
  247. #ifdef CONFIG_CMDLINE_TAG
  248. setup_commandline_tag(gd->bd, commandline);
  249. #endif
  250. #ifdef CONFIG_REVISION_TAG
  251. setup_revision_tag(&params);
  252. #endif
  253. #ifdef CONFIG_SETUP_MEMORY_TAGS
  254. setup_memory_tags(gd->bd);
  255. #endif
  256. #ifdef CONFIG_INITRD_TAG
  257. if (images->rd_start && images->rd_end)
  258. setup_initrd_tag(gd->bd, images->rd_start,
  259. images->rd_end);
  260. #endif
  261. setup_end_tag(gd->bd);
  262. #else /* all tags */
  263. printf("FDT and ATAGS support not compiled in - hanging\n");
  264. hang();
  265. #endif /* all tags */
  266. }
  267. }
  268. /* Subcommand: GO */
  269. static void boot_jump_linux(bootm_headers_t *images)
  270. {
  271. unsigned long machid = gd->bd->bi_arch_number;
  272. char *s;
  273. void (*kernel_entry)(int zero, int arch, uint params);
  274. kernel_entry = (void (*)(int, int, uint))images->ep;
  275. s = getenv("machid");
  276. if (s) {
  277. strict_strtoul(s, 16, &machid);
  278. printf("Using machid 0x%lx from environment\n", machid);
  279. }
  280. debug("## Transferring control to Linux (at address %08lx)" \
  281. "...\n", (ulong) kernel_entry);
  282. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  283. announce_and_cleanup();
  284. kernel_entry(0, machid, gd->bd->bi_boot_params);
  285. }
  286. /* Main Entry point for arm bootm implementation
  287. *
  288. * Modeled after the powerpc implementation
  289. * DIFFERENCE: Instead of calling prep and go at the end
  290. * they are called if subcommand is equal 0.
  291. */
  292. int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
  293. {
  294. /* No need for those on ARM */
  295. if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
  296. return -1;
  297. if (flag & BOOTM_STATE_OS_PREP) {
  298. boot_prep_linux(images);
  299. return 0;
  300. }
  301. if (flag & BOOTM_STATE_OS_GO) {
  302. boot_jump_linux(images);
  303. return 0;
  304. }
  305. boot_prep_linux(images);
  306. boot_jump_linux(images);
  307. return 0;
  308. }