blob: b90ca653fdf8fc3727ad09e7a98275b1f3eb847d [file] [log] [blame]
wdenk2262cfe2002-11-18 00:14:45 +00001/*
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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk2262cfe2002-11-18 00:14:45 +00009 */
10
11#include <common.h>
12#include <command.h>
Simon Glass76539382014-10-10 08:21:56 -060013#include <errno.h>
Simon Glass0d0ba592014-10-19 21:11:20 -060014#include <fdt_support.h>
wdenk2262cfe2002-11-18 00:14:45 +000015#include <image.h>
Jean-Christophe PLAGNIOL-VILLARDa31e0912009-04-04 12:49:11 +020016#include <u-boot/zlib.h>
Gabe Black69370d12011-12-05 12:09:26 +000017#include <asm/bootparam.h>
wdenk2262cfe2002-11-18 00:14:45 +000018#include <asm/byteorder.h>
19#include <asm/zimage.h>
Simon Glass0d0ba592014-10-19 21:11:20 -060020#ifdef CONFIG_SYS_COREBOOT
21#include <asm/arch/timestamp.h>
22#endif
wdenk2262cfe2002-11-18 00:14:45 +000023
Gabe Black69370d12011-12-05 12:09:26 +000024#define COMMAND_LINE_OFFSET 0x9000
25
Simon Glass0d0ba592014-10-19 21:11:20 -060026/*
27 * Implement a weak default function for boards that optionally
28 * need to clean up the system before jumping to the kernel.
29 */
30__weak void board_final_cleanup(void)
wdenk2262cfe2002-11-18 00:14:45 +000031{
Simon Glass0d0ba592014-10-19 21:11:20 -060032}
Graeme Russ3ef96de2008-09-07 07:08:42 +100033
Simon Glass0d0ba592014-10-19 21:11:20 -060034void bootm_announce_and_cleanup(void)
35{
36 printf("\nStarting kernel ...\n\n");
37
38#ifdef CONFIG_SYS_COREBOOT
39 timestamp_add_now(TS_U_BOOT_START_KERNEL);
40#endif
41 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
42#ifdef CONFIG_BOOTSTAGE_REPORT
43 bootstage_report();
44#endif
45 board_final_cleanup();
46}
47
48#if defined(CONFIG_OF_LIBFDT) && !defined(CONFIG_OF_NO_KERNEL)
49int arch_fixup_memory_node(void *blob)
50{
51 bd_t *bd = gd->bd;
52 int bank;
53 u64 start[CONFIG_NR_DRAM_BANKS];
54 u64 size[CONFIG_NR_DRAM_BANKS];
55
56 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
57 start[bank] = bd->bi_dram[bank].start;
58 size[bank] = bd->bi_dram[bank].size;
59 }
60
61 return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS);
62}
Marian Balakowiczcd7c5962008-03-12 10:33:00 +010063#endif
wdenk8bde7f72003-06-27 21:31:46 +000064
Simon Glass0d0ba592014-10-19 21:11:20 -060065/* Subcommand: PREP */
66static int boot_prep_linux(bootm_headers_t *images)
67{
68 char *cmd_line_dest = NULL;
69 image_header_t *hdr;
70 int is_zimage = 0;
71 void *data = NULL;
72 size_t len;
73 int ret;
Kumar Gala49c3a862008-10-21 17:25:45 -050074
Simon Glass0d0ba592014-10-19 21:11:20 -060075#ifdef CONFIG_OF_LIBFDT
76 if (images->ft_len) {
77 debug("using: FDT\n");
78 if (image_setup_linux(images)) {
79 puts("FDT creation failed! hanging...");
80 hang();
81 }
82 }
83#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010084 if (images->legacy_hdr_valid) {
85 hdr = images->legacy_hdr_os;
Graeme Russ83088af2011-11-08 02:33:15 +000086 if (image_check_type(hdr, IH_TYPE_MULTI)) {
Simon Glass0d0ba592014-10-19 21:11:20 -060087 ulong os_data, os_len;
88
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010089 /* if multi-part image, we need to get first subimage */
Graeme Russ83088af2011-11-08 02:33:15 +000090 image_multi_getimg(hdr, 0, &os_data, &os_len);
Simon Glass0d0ba592014-10-19 21:11:20 -060091 data = (void *)os_data;
92 len = os_len;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010093 } else {
94 /* otherwise get image data */
Simon Glass0d0ba592014-10-19 21:11:20 -060095 data = (void *)image_get_data(hdr);
96 len = image_get_data_size(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010097 }
Simon Glass0d0ba592014-10-19 21:11:20 -060098 is_zimage = 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010099#if defined(CONFIG_FIT)
Simon Glass0d0ba592014-10-19 21:11:20 -0600100 } else if (images->fit_uname_os && is_zimage) {
Graeme Russ83088af2011-11-08 02:33:15 +0000101 ret = fit_image_get_data(images->fit_hdr_os,
Simon Glass0d0ba592014-10-19 21:11:20 -0600102 images->fit_noffset_os,
103 (const void **)&data, &len);
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100104 if (ret) {
Graeme Russ83088af2011-11-08 02:33:15 +0000105 puts("Can't get image data/size!\n");
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100106 goto error;
107 }
Simon Glass0d0ba592014-10-19 21:11:20 -0600108 is_zimage = 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100109#endif
Simon Glass0d0ba592014-10-19 21:11:20 -0600110 }
111
112 if (is_zimage) {
Simon Glass76539382014-10-10 08:21:56 -0600113 ulong load_address;
Simon Glass0d0ba592014-10-19 21:11:20 -0600114 char *base_ptr;
115
116 base_ptr = (char *)load_zimage(data, len, &load_address);
Simon Glass76539382014-10-10 08:21:56 -0600117 images->os.load = load_address;
Simon Glass0d0ba592014-10-19 21:11:20 -0600118 cmd_line_dest = base_ptr + COMMAND_LINE_OFFSET;
119 images->ep = (ulong)base_ptr;
120 } else if (images->ep) {
121 cmd_line_dest = (void *)images->ep + COMMAND_LINE_OFFSET;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100122 } else {
Simon Glass0d0ba592014-10-19 21:11:20 -0600123 printf("## Kernel loading failed (no setup) ...\n");
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100124 goto error;
Wolfgang Denke6446702006-07-21 11:30:18 +0200125 }
126
Simon Glass0d0ba592014-10-19 21:11:20 -0600127 printf("Setup at %#08lx\n", images->ep);
128 ret = setup_zimage((void *)images->ep, cmd_line_dest,
Gabe Black69370d12011-12-05 12:09:26 +0000129 0, images->rd_start,
Simon Glass0d0ba592014-10-19 21:11:20 -0600130 images->rd_end - images->rd_start);
131
132 if (ret) {
Gabe Black69370d12011-12-05 12:09:26 +0000133 printf("## Setting up boot parameters failed ...\n");
Simon Glass0d0ba592014-10-19 21:11:20 -0600134 return 1;
wdenk2262cfe2002-11-18 00:14:45 +0000135 }
wdenk8bde7f72003-06-27 21:31:46 +0000136
Simon Glass0d0ba592014-10-19 21:11:20 -0600137 return 0;
wdenk8bde7f72003-06-27 21:31:46 +0000138
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100139error:
Kumar Gala40d7e992008-08-15 08:24:45 -0500140 return 1;
wdenk2262cfe2002-11-18 00:14:45 +0000141}
Simon Glass0d0ba592014-10-19 21:11:20 -0600142
Simon Glass76539382014-10-10 08:21:56 -0600143int boot_linux_kernel(ulong setup_base, ulong load_address, bool image_64bit)
144{
145 bootm_announce_and_cleanup();
146
147#ifdef CONFIG_SYS_COREBOOT
148 timestamp_add_now(TS_U_BOOT_START_KERNEL);
149#endif
150 if (image_64bit) {
151 /* TODO(boot 64-bit kernel) */
152 } else {
153 /*
154 * Set %ebx, %ebp, and %edi to 0, %esi to point to the
155 * boot_params structure, and then jump to the kernel. We
156 * assume that %cs is 0x10, 4GB flat, and read/execute, and
157 * the data segments are 0x18, 4GB flat, and read/write.
158 * U-boot is setting them up that way for itself in
159 * arch/i386/cpu/cpu.c.
160 */
161 __asm__ __volatile__ (
162 "movl $0, %%ebp\n"
163 "cli\n"
164 "jmp *%[kernel_entry]\n"
165 :: [kernel_entry]"a"(load_address),
166 [boot_params] "S"(setup_base),
167 "b"(0), "D"(0)
168 );
169 }
170
171 /* We can't get to here */
172 return -EFAULT;
173}
174
Simon Glass0d0ba592014-10-19 21:11:20 -0600175/* Subcommand: GO */
176static int boot_jump_linux(bootm_headers_t *images)
177{
178 debug("## Transferring control to Linux (at address %08lx, kernel %08lx) ...\n",
179 images->ep, images->os.load);
180
Simon Glass76539382014-10-10 08:21:56 -0600181 return boot_linux_kernel(images->ep, images->os.load, false);
Simon Glass0d0ba592014-10-19 21:11:20 -0600182}
183
184int do_bootm_linux(int flag, int argc, char * const argv[],
185 bootm_headers_t *images)
186{
187 /* No need for those on x86 */
188 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
189 return -1;
190
191 if (flag & BOOTM_STATE_OS_PREP)
192 return boot_prep_linux(images);
193
Simon Glass76539382014-10-10 08:21:56 -0600194 if (flag & BOOTM_STATE_OS_GO)
195 return boot_jump_linux(images);
Simon Glass0d0ba592014-10-19 21:11:20 -0600196
197 return boot_jump_linux(images);
198}