2 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
3 * - Added prep subcommand support
4 * - Reorganized source - modeled after powerpc version
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Marius Groeger <mgroeger@sysgo.de>
10 * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
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.
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.
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
31 #include <u-boot/zlib.h>
32 #include <asm/byteorder.h>
34 #include <fdt_support.h>
35 #include <asm/bootm.h>
36 #include <linux/compiler.h>
38 DECLARE_GLOBAL_DATA_PTR;
40 static struct tag *params;
42 static ulong get_sp(void)
46 asm("mov %0, sp" : "=r"(ret) : );
50 void arch_lmb_reserve(struct lmb *lmb)
55 * Booting a (Linux) kernel image
57 * Allocate space for command line and board info - the
58 * address should be as high as possible within the reach of
59 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
60 * memory, which means far enough below the current stack
64 debug("## Current stack ends at 0x%08lx ", sp);
66 /* adjust sp by 4K to be safe */
69 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
72 #ifdef CONFIG_OF_LIBFDT
73 static int fixup_memory_node(void *blob)
77 u64 start[CONFIG_NR_DRAM_BANKS];
78 u64 size[CONFIG_NR_DRAM_BANKS];
80 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
81 start[bank] = bd->bi_dram[bank].start;
82 size[bank] = bd->bi_dram[bank].size;
85 return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS);
89 static void announce_and_cleanup(void)
91 printf("\nStarting kernel ...\n\n");
92 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
93 #ifdef CONFIG_BOOTSTAGE_FDT
94 bootstage_fdt_add_report();
96 #ifdef CONFIG_BOOTSTAGE_REPORT
100 #ifdef CONFIG_USB_DEVICE
103 cleanup_before_linux();
106 static void setup_start_tag (bd_t *bd)
108 params = (struct tag *)bd->bi_boot_params;
110 params->hdr.tag = ATAG_CORE;
111 params->hdr.size = tag_size (tag_core);
113 params->u.core.flags = 0;
114 params->u.core.pagesize = 0;
115 params->u.core.rootdev = 0;
117 params = tag_next (params);
120 static void setup_memory_tags(bd_t *bd)
124 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
125 params->hdr.tag = ATAG_MEM;
126 params->hdr.size = tag_size (tag_mem32);
128 params->u.mem.start = bd->bi_dram[i].start;
129 params->u.mem.size = bd->bi_dram[i].size;
131 params = tag_next (params);
135 static void setup_commandline_tag(bd_t *bd, char *commandline)
142 /* eat leading white space */
143 for (p = commandline; *p == ' '; p++);
145 /* skip non-existent command lines so the kernel will still
146 * use its default command line.
151 params->hdr.tag = ATAG_CMDLINE;
153 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
155 strcpy (params->u.cmdline.cmdline, p);
157 params = tag_next (params);
160 static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
162 /* an ATAG_INITRD node tells the kernel where the compressed
163 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
165 params->hdr.tag = ATAG_INITRD2;
166 params->hdr.size = tag_size (tag_initrd);
168 params->u.initrd.start = initrd_start;
169 params->u.initrd.size = initrd_end - initrd_start;
171 params = tag_next (params);
174 static void setup_serial_tag(struct tag **tmp)
176 struct tag *params = *tmp;
177 struct tag_serialnr serialnr;
179 get_board_serial(&serialnr);
180 params->hdr.tag = ATAG_SERIAL;
181 params->hdr.size = tag_size (tag_serialnr);
182 params->u.serialnr.low = serialnr.low;
183 params->u.serialnr.high= serialnr.high;
184 params = tag_next (params);
188 static void setup_revision_tag(struct tag **in_params)
192 rev = get_board_rev();
193 params->hdr.tag = ATAG_REVISION;
194 params->hdr.size = tag_size (tag_revision);
195 params->u.revision.rev = rev;
196 params = tag_next (params);
199 static void setup_end_tag(bd_t *bd)
201 params->hdr.tag = ATAG_NONE;
202 params->hdr.size = 0;
205 #ifdef CONFIG_OF_LIBFDT
206 static int create_fdt(bootm_headers_t *images)
208 ulong of_size = images->ft_len;
209 char **of_flat_tree = &images->ft_addr;
210 ulong *initrd_start = &images->initrd_start;
211 ulong *initrd_end = &images->initrd_end;
212 struct lmb *lmb = &images->lmb;
216 debug("using: FDT\n");
218 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
220 rd_len = images->rd_end - images->rd_start;
221 ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
222 initrd_start, initrd_end);
226 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
230 fdt_chosen(*of_flat_tree, 1);
231 fixup_memory_node(*of_flat_tree);
232 fdt_fixup_ethernet(*of_flat_tree);
233 fdt_initrd(*of_flat_tree, *initrd_start, *initrd_end, 1);
234 #ifdef CONFIG_OF_BOARD_SETUP
235 ft_board_setup(*of_flat_tree, gd->bd);
242 __weak void setup_board_tags(struct tag **in_params) {}
244 /* Subcommand: PREP */
245 static void boot_prep_linux(bootm_headers_t *images)
247 char *commandline = getenv("bootargs");
249 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
250 #ifdef CONFIG_OF_LIBFDT
251 debug("using: FDT\n");
252 if (create_fdt(images)) {
253 printf("FDT creation failed! hanging...");
257 } else if (BOOTM_ENABLE_TAGS) {
258 debug("using: ATAGS\n");
259 setup_start_tag(gd->bd);
260 if (BOOTM_ENABLE_SERIAL_TAG)
261 setup_serial_tag(¶ms);
262 if (BOOTM_ENABLE_CMDLINE_TAG)
263 setup_commandline_tag(gd->bd, commandline);
264 if (BOOTM_ENABLE_REVISION_TAG)
265 setup_revision_tag(¶ms);
266 if (BOOTM_ENABLE_MEMORY_TAGS)
267 setup_memory_tags(gd->bd);
268 if (BOOTM_ENABLE_INITRD_TAG) {
269 if (images->rd_start && images->rd_end) {
270 setup_initrd_tag(gd->bd, images->rd_start,
274 setup_board_tags(¶ms);
275 setup_end_tag(gd->bd);
277 printf("FDT and ATAGS support not compiled in - hanging\n");
283 static void boot_jump_linux(bootm_headers_t *images)
285 unsigned long machid = gd->bd->bi_arch_number;
287 void (*kernel_entry)(int zero, int arch, uint params);
290 kernel_entry = (void (*)(int, int, uint))images->ep;
292 s = getenv("machid");
294 strict_strtoul(s, 16, &machid);
295 printf("Using machid 0x%lx from environment\n", machid);
298 debug("## Transferring control to Linux (at address %08lx)" \
299 "...\n", (ulong) kernel_entry);
300 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
301 announce_and_cleanup();
303 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
304 r2 = (unsigned long)images->ft_addr;
306 r2 = gd->bd->bi_boot_params;
308 kernel_entry(0, machid, r2);
311 /* Main Entry point for arm bootm implementation
313 * Modeled after the powerpc implementation
314 * DIFFERENCE: Instead of calling prep and go at the end
315 * they are called if subcommand is equal 0.
317 int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
319 /* No need for those on ARM */
320 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
323 if (flag & BOOTM_STATE_OS_PREP) {
324 boot_prep_linux(images);
328 if (flag & BOOTM_STATE_OS_GO) {
329 boot_jump_linux(images);
333 boot_prep_linux(images);
334 boot_jump_linux(images);
338 #ifdef CONFIG_CMD_BOOTZ
340 struct zimage_header {
347 #define LINUX_ARM_ZIMAGE_MAGIC 0x016f2818
349 int bootz_setup(void *image, void **start, void **end)
351 struct zimage_header *zi = (struct zimage_header *)image;
353 if (zi->zi_magic != LINUX_ARM_ZIMAGE_MAGIC) {
354 puts("Bad Linux ARM zImage magic!\n");
358 *start = (void *)zi->zi_start;
359 *end = (void *)zi->zi_end;
361 debug("Kernel image @ 0x%08x [ 0x%08x - 0x%08x ]\n",
362 (uint32_t)image, (uint32_t)*start, (uint32_t)*end);
366 #endif /* CONFIG_CMD_BOOTZ */