bootm.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * (C) Copyright 2002
  3. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Marius Groeger <mgroeger@sysgo.de>
  5. *
  6. * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <image.h>
  26. #include <u-boot/zlib.h>
  27. #include <asm/byteorder.h>
  28. #include <fdt.h>
  29. #include <libfdt.h>
  30. #include <fdt_support.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. #if defined (CONFIG_SETUP_MEMORY_TAGS) || \
  33. defined (CONFIG_CMDLINE_TAG) || \
  34. defined (CONFIG_INITRD_TAG) || \
  35. defined (CONFIG_SERIAL_TAG) || \
  36. defined (CONFIG_REVISION_TAG)
  37. static void setup_start_tag (bd_t *bd);
  38. # ifdef CONFIG_SETUP_MEMORY_TAGS
  39. static void setup_memory_tags (bd_t *bd);
  40. # endif
  41. static void setup_commandline_tag (bd_t *bd, char *commandline);
  42. # ifdef CONFIG_INITRD_TAG
  43. static void setup_initrd_tag (bd_t *bd, ulong initrd_start,
  44. ulong initrd_end);
  45. # endif
  46. static void setup_end_tag (bd_t *bd);
  47. static struct tag *params;
  48. #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
  49. static ulong get_sp(void);
  50. #if defined(CONFIG_OF_LIBFDT)
  51. static int bootm_linux_fdt(int machid, bootm_headers_t *images);
  52. #endif
  53. void arch_lmb_reserve(struct lmb *lmb)
  54. {
  55. ulong sp;
  56. /*
  57. * Booting a (Linux) kernel image
  58. *
  59. * Allocate space for command line and board info - the
  60. * address should be as high as possible within the reach of
  61. * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
  62. * memory, which means far enough below the current stack
  63. * pointer.
  64. */
  65. sp = get_sp();
  66. debug("## Current stack ends at 0x%08lx ", sp);
  67. /* adjust sp by 1K to be safe */
  68. sp -= 1024;
  69. lmb_reserve(lmb, sp,
  70. gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
  71. }
  72. static void announce_and_cleanup(void)
  73. {
  74. printf("\nStarting kernel ...\n\n");
  75. #ifdef CONFIG_USB_DEVICE
  76. {
  77. extern void udc_disconnect(void);
  78. udc_disconnect();
  79. }
  80. #endif
  81. cleanup_before_linux();
  82. }
  83. int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
  84. {
  85. bd_t *bd = gd->bd;
  86. char *s;
  87. int machid = bd->bi_arch_number;
  88. void (*kernel_entry)(int zero, int arch, uint params);
  89. #ifdef CONFIG_CMDLINE_TAG
  90. char *commandline = getenv ("bootargs");
  91. #endif
  92. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  93. return 1;
  94. s = getenv ("machid");
  95. if (s) {
  96. machid = simple_strtoul (s, NULL, 16);
  97. printf ("Using machid 0x%x from environment\n", machid);
  98. }
  99. show_boot_progress (15);
  100. #ifdef CONFIG_OF_LIBFDT
  101. if (images->ft_len)
  102. return bootm_linux_fdt(machid, images);
  103. #endif
  104. kernel_entry = (void (*)(int, int, uint))images->ep;
  105. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  106. (ulong) kernel_entry);
  107. #if defined (CONFIG_SETUP_MEMORY_TAGS) || \
  108. defined (CONFIG_CMDLINE_TAG) || \
  109. defined (CONFIG_INITRD_TAG) || \
  110. defined (CONFIG_SERIAL_TAG) || \
  111. defined (CONFIG_REVISION_TAG)
  112. setup_start_tag (bd);
  113. #ifdef CONFIG_SERIAL_TAG
  114. setup_serial_tag (&params);
  115. #endif
  116. #ifdef CONFIG_REVISION_TAG
  117. setup_revision_tag (&params);
  118. #endif
  119. #ifdef CONFIG_SETUP_MEMORY_TAGS
  120. setup_memory_tags (bd);
  121. #endif
  122. #ifdef CONFIG_CMDLINE_TAG
  123. setup_commandline_tag (bd, commandline);
  124. #endif
  125. #ifdef CONFIG_INITRD_TAG
  126. if (images->rd_start && images->rd_end)
  127. setup_initrd_tag (bd, images->rd_start, images->rd_end);
  128. #endif
  129. setup_end_tag(bd);
  130. #endif
  131. announce_and_cleanup();
  132. kernel_entry(0, machid, bd->bi_boot_params);
  133. /* does not return */
  134. return 1;
  135. }
  136. #if defined(CONFIG_OF_LIBFDT)
  137. static int fixup_memory_node(void *blob)
  138. {
  139. bd_t *bd = gd->bd;
  140. int bank;
  141. u64 start[CONFIG_NR_DRAM_BANKS];
  142. u64 size[CONFIG_NR_DRAM_BANKS];
  143. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  144. start[bank] = bd->bi_dram[bank].start;
  145. size[bank] = bd->bi_dram[bank].size;
  146. }
  147. return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS);
  148. }
  149. static int bootm_linux_fdt(int machid, bootm_headers_t *images)
  150. {
  151. ulong rd_len;
  152. bd_t *bd = gd->bd;
  153. char *s;
  154. void (*kernel_entry)(int zero, int dt_machid, void *dtblob);
  155. ulong bootmap_base = getenv_bootm_low();
  156. ulong of_size = images->ft_len;
  157. char **of_flat_tree = &images->ft_addr;
  158. ulong *initrd_start = &images->initrd_start;
  159. ulong *initrd_end = &images->initrd_end;
  160. struct lmb *lmb = &images->lmb;
  161. int ret;
  162. kernel_entry = (void (*)(int, int, void *))images->ep;
  163. rd_len = images->rd_end - images->rd_start;
  164. ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
  165. initrd_start, initrd_end);
  166. if (ret)
  167. return ret;
  168. ret = boot_relocate_fdt(lmb, bootmap_base, of_flat_tree, &of_size);
  169. if (ret)
  170. return ret;
  171. debug("## Transferring control to Linux (at address %08lx) ...\n",
  172. (ulong) kernel_entry);
  173. fdt_chosen(*of_flat_tree, 1);
  174. fixup_memory_node(*of_flat_tree);
  175. fdt_initrd(*of_flat_tree, *initrd_start, *initrd_end, 1);
  176. announce_and_cleanup();
  177. kernel_entry(0, machid, *of_flat_tree);
  178. /* does not return */
  179. return 1;
  180. }
  181. #endif
  182. #if defined (CONFIG_SETUP_MEMORY_TAGS) || \
  183. defined (CONFIG_CMDLINE_TAG) || \
  184. defined (CONFIG_INITRD_TAG) || \
  185. defined (CONFIG_SERIAL_TAG) || \
  186. defined (CONFIG_REVISION_TAG)
  187. static void setup_start_tag (bd_t *bd)
  188. {
  189. params = (struct tag *) bd->bi_boot_params;
  190. params->hdr.tag = ATAG_CORE;
  191. params->hdr.size = tag_size (tag_core);
  192. params->u.core.flags = 0;
  193. params->u.core.pagesize = 0;
  194. params->u.core.rootdev = 0;
  195. params = tag_next (params);
  196. }
  197. #ifdef CONFIG_SETUP_MEMORY_TAGS
  198. static void setup_memory_tags (bd_t *bd)
  199. {
  200. int i;
  201. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  202. params->hdr.tag = ATAG_MEM;
  203. params->hdr.size = tag_size (tag_mem32);
  204. params->u.mem.start = bd->bi_dram[i].start;
  205. params->u.mem.size = bd->bi_dram[i].size;
  206. params = tag_next (params);
  207. }
  208. }
  209. #endif /* CONFIG_SETUP_MEMORY_TAGS */
  210. static void setup_commandline_tag (bd_t *bd, char *commandline)
  211. {
  212. char *p;
  213. if (!commandline)
  214. return;
  215. /* eat leading white space */
  216. for (p = commandline; *p == ' '; p++);
  217. /* skip non-existent command lines so the kernel will still
  218. * use its default command line.
  219. */
  220. if (*p == '\0')
  221. return;
  222. params->hdr.tag = ATAG_CMDLINE;
  223. params->hdr.size =
  224. (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
  225. strcpy (params->u.cmdline.cmdline, p);
  226. params = tag_next (params);
  227. }
  228. #ifdef CONFIG_INITRD_TAG
  229. static void setup_initrd_tag (bd_t *bd, ulong initrd_start, ulong initrd_end)
  230. {
  231. /* an ATAG_INITRD node tells the kernel where the compressed
  232. * ramdisk can be found. ATAG_RDIMG is a better name, actually.
  233. */
  234. params->hdr.tag = ATAG_INITRD2;
  235. params->hdr.size = tag_size (tag_initrd);
  236. params->u.initrd.start = initrd_start;
  237. params->u.initrd.size = initrd_end - initrd_start;
  238. params = tag_next (params);
  239. }
  240. #endif /* CONFIG_INITRD_TAG */
  241. #ifdef CONFIG_SERIAL_TAG
  242. void setup_serial_tag (struct tag **tmp)
  243. {
  244. struct tag *params = *tmp;
  245. struct tag_serialnr serialnr;
  246. void get_board_serial(struct tag_serialnr *serialnr);
  247. get_board_serial(&serialnr);
  248. params->hdr.tag = ATAG_SERIAL;
  249. params->hdr.size = tag_size (tag_serialnr);
  250. params->u.serialnr.low = serialnr.low;
  251. params->u.serialnr.high= serialnr.high;
  252. params = tag_next (params);
  253. *tmp = params;
  254. }
  255. #endif
  256. #ifdef CONFIG_REVISION_TAG
  257. void setup_revision_tag(struct tag **in_params)
  258. {
  259. u32 rev = 0;
  260. u32 get_board_rev(void);
  261. rev = get_board_rev();
  262. params->hdr.tag = ATAG_REVISION;
  263. params->hdr.size = tag_size (tag_revision);
  264. params->u.revision.rev = rev;
  265. params = tag_next (params);
  266. }
  267. #endif /* CONFIG_REVISION_TAG */
  268. static void setup_end_tag (bd_t *bd)
  269. {
  270. params->hdr.tag = ATAG_NONE;
  271. params->hdr.size = 0;
  272. }
  273. static ulong get_sp(void)
  274. {
  275. ulong ret;
  276. asm("mov %0, sp" : "=r"(ret) : );
  277. return ret;
  278. }
  279. #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */