blob: 1b6e0ace45372417af0b671aac632fce309b12f3 [file] [log] [blame]
Simon Schwarz0a672d42012-03-15 04:01:45 +00001/* 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 *
wdenkc6097192002-11-03 00:24:07 +00006 * (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
wdenk232c1502004-03-12 00:14:09 +000019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkc6097192002-11-03 00:24:07 +000020 * 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
wdenk232c1502004-03-12 00:14:09 +000024 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
wdenkc6097192002-11-03 00:24:07 +000025 */
26
27#include <common.h>
28#include <command.h>
wdenkc6097192002-11-03 00:24:07 +000029#include <image.h>
Jean-Christophe PLAGNIOL-VILLARDa31e0912009-04-04 12:49:11 +020030#include <u-boot/zlib.h>
wdenkc6097192002-11-03 00:24:07 +000031#include <asm/byteorder.h>
John Rigby2d1916e2010-10-13 13:57:36 -060032#include <libfdt.h>
33#include <fdt_support.h>
Simon Schwarz0a672d42012-03-15 04:01:45 +000034#include <asm/bootm.h>
Pali Rohár89e6f132012-10-19 02:00:04 +000035#include <linux/compiler.h>
wdenkc6097192002-11-03 00:24:07 +000036
Wolfgang Denkd87080b2006-03-31 18:32:53 +020037DECLARE_GLOBAL_DATA_PTR;
38
wdenkc6097192002-11-03 00:24:07 +000039static struct tag *params;
John Rigby2d1916e2010-10-13 13:57:36 -060040
Simon Schwarz0a672d42012-03-15 04:01:45 +000041static ulong get_sp(void)
42{
43 ulong ret;
44
45 asm("mov %0, sp" : "=r"(ret) : );
46 return ret;
47}
48
John Rigby2d1916e2010-10-13 13:57:36 -060049void arch_lmb_reserve(struct lmb *lmb)
50{
51 ulong sp;
52
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
Rob Herringfcfa6962012-06-28 03:54:11 +000065 /* adjust sp by 4K to be safe */
66 sp -= 4096;
John Rigby2d1916e2010-10-13 13:57:36 -060067 lmb_reserve(lmb, sp,
68 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
69}
70
Simon Schwarz0a672d42012-03-15 04:01:45 +000071static void announce_and_cleanup(void)
72{
73 printf("\nStarting kernel ...\n\n");
74 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
Simon Glass94fd1312012-09-28 08:56:37 +000075#ifdef CONFIG_BOOTSTAGE_FDT
76 bootstage_fdt_add_report();
77#endif
Simon Schwarz0a672d42012-03-15 04:01:45 +000078#ifdef CONFIG_BOOTSTAGE_REPORT
79 bootstage_report();
80#endif
81
82#ifdef CONFIG_USB_DEVICE
83 udc_disconnect();
84#endif
85 cleanup_before_linux();
86}
87
wdenk5779d8d2003-12-06 23:55:10 +000088static void setup_start_tag (bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +000089{
Simon Schwarz0a672d42012-03-15 04:01:45 +000090 params = (struct tag *)bd->bi_boot_params;
wdenkc6097192002-11-03 00:24:07 +000091
wdenk5779d8d2003-12-06 23:55:10 +000092 params->hdr.tag = ATAG_CORE;
93 params->hdr.size = tag_size (tag_core);
wdenkc6097192002-11-03 00:24:07 +000094
wdenk5779d8d2003-12-06 23:55:10 +000095 params->u.core.flags = 0;
96 params->u.core.pagesize = 0;
97 params->u.core.rootdev = 0;
wdenkc6097192002-11-03 00:24:07 +000098
wdenk5779d8d2003-12-06 23:55:10 +000099 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000100}
wdenkc6097192002-11-03 00:24:07 +0000101
Simon Schwarz0a672d42012-03-15 04:01:45 +0000102static void setup_memory_tags(bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000103{
wdenk5779d8d2003-12-06 23:55:10 +0000104 int i;
wdenkc6097192002-11-03 00:24:07 +0000105
wdenk5779d8d2003-12-06 23:55:10 +0000106 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
107 params->hdr.tag = ATAG_MEM;
108 params->hdr.size = tag_size (tag_mem32);
wdenkc6097192002-11-03 00:24:07 +0000109
wdenk5779d8d2003-12-06 23:55:10 +0000110 params->u.mem.start = bd->bi_dram[i].start;
111 params->u.mem.size = bd->bi_dram[i].size;
wdenkc6097192002-11-03 00:24:07 +0000112
wdenk5779d8d2003-12-06 23:55:10 +0000113 params = tag_next (params);
114 }
wdenkc6097192002-11-03 00:24:07 +0000115}
116
Simon Schwarz0a672d42012-03-15 04:01:45 +0000117static void setup_commandline_tag(bd_t *bd, char *commandline)
wdenkc6097192002-11-03 00:24:07 +0000118{
wdenk5779d8d2003-12-06 23:55:10 +0000119 char *p;
wdenkc6097192002-11-03 00:24:07 +0000120
wdenk109c0e32004-03-23 21:43:07 +0000121 if (!commandline)
122 return;
123
wdenk5779d8d2003-12-06 23:55:10 +0000124 /* eat leading white space */
125 for (p = commandline; *p == ' '; p++);
wdenkc6097192002-11-03 00:24:07 +0000126
wdenk5779d8d2003-12-06 23:55:10 +0000127 /* skip non-existent command lines so the kernel will still
128 * use its default command line.
129 */
130 if (*p == '\0')
131 return;
wdenkc6097192002-11-03 00:24:07 +0000132
wdenk5779d8d2003-12-06 23:55:10 +0000133 params->hdr.tag = ATAG_CMDLINE;
134 params->hdr.size =
135 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
wdenkc6097192002-11-03 00:24:07 +0000136
wdenk5779d8d2003-12-06 23:55:10 +0000137 strcpy (params->u.cmdline.cmdline, p);
wdenkc6097192002-11-03 00:24:07 +0000138
wdenk5779d8d2003-12-06 23:55:10 +0000139 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000140}
wdenkc6097192002-11-03 00:24:07 +0000141
Simon Schwarz0a672d42012-03-15 04:01:45 +0000142static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
wdenkc6097192002-11-03 00:24:07 +0000143{
wdenk5779d8d2003-12-06 23:55:10 +0000144 /* an ATAG_INITRD node tells the kernel where the compressed
145 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
146 */
wdenk498b8db2004-04-18 22:26:17 +0000147 params->hdr.tag = ATAG_INITRD2;
wdenk5779d8d2003-12-06 23:55:10 +0000148 params->hdr.size = tag_size (tag_initrd);
wdenkc6097192002-11-03 00:24:07 +0000149
wdenk5779d8d2003-12-06 23:55:10 +0000150 params->u.initrd.start = initrd_start;
151 params->u.initrd.size = initrd_end - initrd_start;
wdenkc6097192002-11-03 00:24:07 +0000152
wdenk5779d8d2003-12-06 23:55:10 +0000153 params = tag_next (params);
wdenkc6097192002-11-03 00:24:07 +0000154}
wdenkc6097192002-11-03 00:24:07 +0000155
Simon Glassc19d13b2013-05-08 08:06:02 +0000156static void setup_serial_tag(struct tag **tmp)
wdenk3a574cb2005-05-19 22:39:42 +0000157{
158 struct tag *params = *tmp;
159 struct tag_serialnr serialnr;
wdenk3a574cb2005-05-19 22:39:42 +0000160
161 get_board_serial(&serialnr);
162 params->hdr.tag = ATAG_SERIAL;
163 params->hdr.size = tag_size (tag_serialnr);
164 params->u.serialnr.low = serialnr.low;
165 params->u.serialnr.high= serialnr.high;
166 params = tag_next (params);
167 *tmp = params;
168}
wdenk3a574cb2005-05-19 22:39:42 +0000169
Simon Glassc19d13b2013-05-08 08:06:02 +0000170static void setup_revision_tag(struct tag **in_params)
wdenk289f9322005-01-12 00:15:14 +0000171{
172 u32 rev = 0;
wdenk289f9322005-01-12 00:15:14 +0000173
174 rev = get_board_rev();
wdenk289f9322005-01-12 00:15:14 +0000175 params->hdr.tag = ATAG_REVISION;
176 params->hdr.size = tag_size (tag_revision);
177 params->u.revision.rev = rev;
178 params = tag_next (params);
179}
wdenk289f9322005-01-12 00:15:14 +0000180
Simon Schwarz0a672d42012-03-15 04:01:45 +0000181static void setup_end_tag(bd_t *bd)
wdenkc6097192002-11-03 00:24:07 +0000182{
wdenk5779d8d2003-12-06 23:55:10 +0000183 params->hdr.tag = ATAG_NONE;
184 params->hdr.size = 0;
wdenkc6097192002-11-03 00:24:07 +0000185}
186
Pali Rohár89e6f132012-10-19 02:00:04 +0000187__weak void setup_board_tags(struct tag **in_params) {}
188
Simon Schwarz0a672d42012-03-15 04:01:45 +0000189/* Subcommand: PREP */
190static void boot_prep_linux(bootm_headers_t *images)
191{
Simon Schwarz0a672d42012-03-15 04:01:45 +0000192 char *commandline = getenv("bootargs");
Simon Schwarz0a672d42012-03-15 04:01:45 +0000193
Simon Glassc19d13b2013-05-08 08:06:02 +0000194 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
Simon Schwarz0a672d42012-03-15 04:01:45 +0000195#ifdef CONFIG_OF_LIBFDT
Simon Schwarz0a672d42012-03-15 04:01:45 +0000196 debug("using: FDT\n");
Simon Glass6caa1952013-05-08 08:06:03 +0000197 if (image_setup_linux(images)) {
Simon Schwarz0a672d42012-03-15 04:01:45 +0000198 printf("FDT creation failed! hanging...");
199 hang();
200 }
Simon Schwarz0a672d42012-03-15 04:01:45 +0000201#endif
Simon Glassc19d13b2013-05-08 08:06:02 +0000202 } else if (BOOTM_ENABLE_TAGS) {
Simon Schwarz0a672d42012-03-15 04:01:45 +0000203 debug("using: ATAGS\n");
204 setup_start_tag(gd->bd);
Simon Glassc19d13b2013-05-08 08:06:02 +0000205 if (BOOTM_ENABLE_SERIAL_TAG)
206 setup_serial_tag(&params);
207 if (BOOTM_ENABLE_CMDLINE_TAG)
208 setup_commandline_tag(gd->bd, commandline);
209 if (BOOTM_ENABLE_REVISION_TAG)
210 setup_revision_tag(&params);
211 if (BOOTM_ENABLE_MEMORY_TAGS)
212 setup_memory_tags(gd->bd);
213 if (BOOTM_ENABLE_INITRD_TAG) {
214 if (images->rd_start && images->rd_end) {
215 setup_initrd_tag(gd->bd, images->rd_start,
216 images->rd_end);
217 }
218 }
Pali Rohár89e6f132012-10-19 02:00:04 +0000219 setup_board_tags(&params);
Simon Schwarz0a672d42012-03-15 04:01:45 +0000220 setup_end_tag(gd->bd);
Simon Glassc19d13b2013-05-08 08:06:02 +0000221 } else {
Simon Schwarz0a672d42012-03-15 04:01:45 +0000222 printf("FDT and ATAGS support not compiled in - hanging\n");
223 hang();
Simon Schwarz0a672d42012-03-15 04:01:45 +0000224 }
225}
226
227/* Subcommand: GO */
228static void boot_jump_linux(bootm_headers_t *images)
229{
230 unsigned long machid = gd->bd->bi_arch_number;
231 char *s;
232 void (*kernel_entry)(int zero, int arch, uint params);
Stephen Warren17239972012-04-19 11:34:00 +0000233 unsigned long r2;
Simon Schwarz0a672d42012-03-15 04:01:45 +0000234
235 kernel_entry = (void (*)(int, int, uint))images->ep;
236
237 s = getenv("machid");
238 if (s) {
239 strict_strtoul(s, 16, &machid);
240 printf("Using machid 0x%lx from environment\n", machid);
241 }
242
243 debug("## Transferring control to Linux (at address %08lx)" \
244 "...\n", (ulong) kernel_entry);
245 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
246 announce_and_cleanup();
Stephen Warren17239972012-04-19 11:34:00 +0000247
Simon Glassc19d13b2013-05-08 08:06:02 +0000248 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
Stephen Warren17239972012-04-19 11:34:00 +0000249 r2 = (unsigned long)images->ft_addr;
250 else
Stephen Warren17239972012-04-19 11:34:00 +0000251 r2 = gd->bd->bi_boot_params;
252
253 kernel_entry(0, machid, r2);
Simon Schwarz0a672d42012-03-15 04:01:45 +0000254}
255
256/* Main Entry point for arm bootm implementation
257 *
258 * Modeled after the powerpc implementation
259 * DIFFERENCE: Instead of calling prep and go at the end
260 * they are called if subcommand is equal 0.
261 */
262int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
263{
264 /* No need for those on ARM */
265 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
266 return -1;
267
268 if (flag & BOOTM_STATE_OS_PREP) {
269 boot_prep_linux(images);
270 return 0;
271 }
272
273 if (flag & BOOTM_STATE_OS_GO) {
274 boot_jump_linux(images);
275 return 0;
276 }
277
278 boot_prep_linux(images);
279 boot_jump_linux(images);
280 return 0;
John Rigby2d1916e2010-10-13 13:57:36 -0600281}
Marek Vasut44f074c2012-03-14 21:52:45 +0000282
283#ifdef CONFIG_CMD_BOOTZ
284
285struct zimage_header {
286 uint32_t code[9];
287 uint32_t zi_magic;
288 uint32_t zi_start;
289 uint32_t zi_end;
290};
291
292#define LINUX_ARM_ZIMAGE_MAGIC 0x016f2818
293
294int bootz_setup(void *image, void **start, void **end)
295{
296 struct zimage_header *zi = (struct zimage_header *)image;
297
298 if (zi->zi_magic != LINUX_ARM_ZIMAGE_MAGIC) {
299 puts("Bad Linux ARM zImage magic!\n");
300 return 1;
301 }
302
303 *start = (void *)zi->zi_start;
304 *end = (void *)zi->zi_end;
305
306 debug("Kernel image @ 0x%08x [ 0x%08x - 0x%08x ]\n",
307 (uint32_t)image, (uint32_t)*start, (uint32_t)*end);
308
309 return 0;
310}
311#endif /* CONFIG_CMD_BOOTZ */