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
30 #include <u-boot/zlib.h>
31 #include <asm/byteorder.h>
33 #include <fdt_support.h>
34 #include <asm/bootm.h>
35 #include <linux/compiler.h>
37 DECLARE_GLOBAL_DATA_PTR;
39 static struct tag *params;
41 static ulong get_sp(void)
45 asm("mov %0, sp" : "=r"(ret) : );
49 void arch_lmb_reserve(struct lmb *lmb)
54 * Booting a (Linux) kernel image
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
63 debug("## Current stack ends at 0x%08lx ", sp);
65 /* adjust sp by 4K to be safe */
68 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
72 * announce_and_cleanup() - Print message and prepare for kernel boot
74 * @fake: non-zero to do everything except actually boot
76 static void announce_and_cleanup(int fake)
78 printf("\nStarting kernel ...%s\n\n", fake ?
79 "(fake run for tracing)" : "");
80 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
81 #ifdef CONFIG_BOOTSTAGE_FDT
82 if (flag == BOOTM_STATE_OS_FAKE_GO)
83 bootstage_fdt_add_report();
85 #ifdef CONFIG_BOOTSTAGE_REPORT
89 #ifdef CONFIG_USB_DEVICE
92 cleanup_before_linux();
95 static void setup_start_tag (bd_t *bd)
97 params = (struct tag *)bd->bi_boot_params;
99 params->hdr.tag = ATAG_CORE;
100 params->hdr.size = tag_size (tag_core);
102 params->u.core.flags = 0;
103 params->u.core.pagesize = 0;
104 params->u.core.rootdev = 0;
106 params = tag_next (params);
109 static void setup_memory_tags(bd_t *bd)
113 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
114 params->hdr.tag = ATAG_MEM;
115 params->hdr.size = tag_size (tag_mem32);
117 params->u.mem.start = bd->bi_dram[i].start;
118 params->u.mem.size = bd->bi_dram[i].size;
120 params = tag_next (params);
124 static void setup_commandline_tag(bd_t *bd, char *commandline)
131 /* eat leading white space */
132 for (p = commandline; *p == ' '; p++);
134 /* skip non-existent command lines so the kernel will still
135 * use its default command line.
140 params->hdr.tag = ATAG_CMDLINE;
142 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
144 strcpy (params->u.cmdline.cmdline, p);
146 params = tag_next (params);
149 static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
151 /* an ATAG_INITRD node tells the kernel where the compressed
152 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
154 params->hdr.tag = ATAG_INITRD2;
155 params->hdr.size = tag_size (tag_initrd);
157 params->u.initrd.start = initrd_start;
158 params->u.initrd.size = initrd_end - initrd_start;
160 params = tag_next (params);
163 static void setup_serial_tag(struct tag **tmp)
165 struct tag *params = *tmp;
166 struct tag_serialnr serialnr;
168 get_board_serial(&serialnr);
169 params->hdr.tag = ATAG_SERIAL;
170 params->hdr.size = tag_size (tag_serialnr);
171 params->u.serialnr.low = serialnr.low;
172 params->u.serialnr.high= serialnr.high;
173 params = tag_next (params);
177 static void setup_revision_tag(struct tag **in_params)
181 rev = get_board_rev();
182 params->hdr.tag = ATAG_REVISION;
183 params->hdr.size = tag_size (tag_revision);
184 params->u.revision.rev = rev;
185 params = tag_next (params);
188 static void setup_end_tag(bd_t *bd)
190 params->hdr.tag = ATAG_NONE;
191 params->hdr.size = 0;
194 __weak void setup_board_tags(struct tag **in_params) {}
196 /* Subcommand: PREP */
197 static void boot_prep_linux(bootm_headers_t *images)
199 char *commandline = getenv("bootargs");
201 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
202 #ifdef CONFIG_OF_LIBFDT
203 debug("using: FDT\n");
204 if (image_setup_linux(images)) {
205 printf("FDT creation failed! hanging...");
209 } else if (BOOTM_ENABLE_TAGS) {
210 debug("using: ATAGS\n");
211 setup_start_tag(gd->bd);
212 if (BOOTM_ENABLE_SERIAL_TAG)
213 setup_serial_tag(¶ms);
214 if (BOOTM_ENABLE_CMDLINE_TAG)
215 setup_commandline_tag(gd->bd, commandline);
216 if (BOOTM_ENABLE_REVISION_TAG)
217 setup_revision_tag(¶ms);
218 if (BOOTM_ENABLE_MEMORY_TAGS)
219 setup_memory_tags(gd->bd);
220 if (BOOTM_ENABLE_INITRD_TAG) {
221 if (images->rd_start && images->rd_end) {
222 setup_initrd_tag(gd->bd, images->rd_start,
226 setup_board_tags(¶ms);
227 setup_end_tag(gd->bd);
229 printf("FDT and ATAGS support not compiled in - hanging\n");
235 static void boot_jump_linux(bootm_headers_t *images, int flag)
237 unsigned long machid = gd->bd->bi_arch_number;
239 void (*kernel_entry)(int zero, int arch, uint params);
241 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
243 kernel_entry = (void (*)(int, int, uint))images->ep;
245 s = getenv("machid");
247 strict_strtoul(s, 16, &machid);
248 printf("Using machid 0x%lx from environment\n", machid);
251 debug("## Transferring control to Linux (at address %08lx)" \
252 "...\n", (ulong) kernel_entry);
253 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
254 announce_and_cleanup(fake);
256 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
257 r2 = (unsigned long)images->ft_addr;
259 r2 = gd->bd->bi_boot_params;
262 kernel_entry(0, machid, r2);
265 /* Main Entry point for arm bootm implementation
267 * Modeled after the powerpc implementation
268 * DIFFERENCE: Instead of calling prep and go at the end
269 * they are called if subcommand is equal 0.
271 int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
273 /* No need for those on ARM */
274 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
277 if (flag & BOOTM_STATE_OS_PREP) {
278 boot_prep_linux(images);
282 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
283 boot_jump_linux(images, flag);
287 boot_prep_linux(images);
288 boot_jump_linux(images, flag);
292 #ifdef CONFIG_CMD_BOOTZ
294 struct zimage_header {
301 #define LINUX_ARM_ZIMAGE_MAGIC 0x016f2818
303 int bootz_setup(ulong image, ulong *start, ulong *end)
305 struct zimage_header *zi;
307 zi = (struct zimage_header *)map_sysmem(image, 0);
308 if (zi->zi_magic != LINUX_ARM_ZIMAGE_MAGIC) {
309 puts("Bad Linux ARM zImage magic!\n");
313 *start = zi->zi_start;
316 printf("Kernel image @ %#08lx [ %#08lx - %#08lx ]\n", image, *start,
322 #endif /* CONFIG_CMD_BOOTZ */