blob: 1569d30f8ed0aa3aec13a02371c8af438e55a0ca [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk2262cfe2002-11-18 00:14:45 +00002/*
3 * (C) Copyright 2002
4 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
5 * Marius Groeger <mgroeger@sysgo.de>
6 *
7 * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
wdenk2262cfe2002-11-18 00:14:45 +00008 */
9
10#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -060011#include <bootstage.h>
wdenk2262cfe2002-11-18 00:14:45 +000012#include <command.h>
Simon Glassdb41d652019-12-28 10:45:07 -070013#include <hang.h>
Stefan Roese7025b052017-04-24 09:48:03 +020014#include <dm/device.h>
15#include <dm/root.h>
Simon Glass76539382014-10-10 08:21:56 -060016#include <errno.h>
Simon Glass0d0ba592014-10-19 21:11:20 -060017#include <fdt_support.h>
wdenk2262cfe2002-11-18 00:14:45 +000018#include <image.h>
Jean-Christophe PLAGNIOL-VILLARDa31e0912009-04-04 12:49:11 +020019#include <u-boot/zlib.h>
Gabe Black69370d12011-12-05 12:09:26 +000020#include <asm/bootparam.h>
Simon Glass61643ae2014-10-10 08:21:58 -060021#include <asm/cpu.h>
wdenk2262cfe2002-11-18 00:14:45 +000022#include <asm/byteorder.h>
23#include <asm/zimage.h>
Simon Glass0d0ba592014-10-19 21:11:20 -060024#ifdef CONFIG_SYS_COREBOOT
25#include <asm/arch/timestamp.h>
26#endif
wdenk2262cfe2002-11-18 00:14:45 +000027
Simon Glass8b097912015-07-31 09:31:31 -060028DECLARE_GLOBAL_DATA_PTR;
29
Gabe Black69370d12011-12-05 12:09:26 +000030#define COMMAND_LINE_OFFSET 0x9000
31
Simon Glass0d0ba592014-10-19 21:11:20 -060032void bootm_announce_and_cleanup(void)
33{
34 printf("\nStarting kernel ...\n\n");
35
36#ifdef CONFIG_SYS_COREBOOT
37 timestamp_add_now(TS_U_BOOT_START_KERNEL);
38#endif
39 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
Simon Glassbf4d8be2019-04-25 21:59:07 -060040#if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
Simon Glass0d0ba592014-10-19 21:11:20 -060041 bootstage_report();
42#endif
Stefan Roese7025b052017-04-24 09:48:03 +020043
44 /*
45 * Call remove function of all devices with a removal flag set.
46 * This may be useful for last-stage operations, like cancelling
47 * of DMA operation or releasing device internal buffers.
48 */
49 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
Simon Glass0d0ba592014-10-19 21:11:20 -060050}
51
52#if defined(CONFIG_OF_LIBFDT) && !defined(CONFIG_OF_NO_KERNEL)
53int arch_fixup_memory_node(void *blob)
54{
55 bd_t *bd = gd->bd;
56 int bank;
57 u64 start[CONFIG_NR_DRAM_BANKS];
58 u64 size[CONFIG_NR_DRAM_BANKS];
59
60 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
61 start[bank] = bd->bi_dram[bank].start;
62 size[bank] = bd->bi_dram[bank].size;
63 }
64
65 return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS);
66}
Marian Balakowiczcd7c5962008-03-12 10:33:00 +010067#endif
wdenk8bde7f72003-06-27 21:31:46 +000068
Simon Glass0d0ba592014-10-19 21:11:20 -060069/* Subcommand: PREP */
70static int boot_prep_linux(bootm_headers_t *images)
71{
72 char *cmd_line_dest = NULL;
73 image_header_t *hdr;
74 int is_zimage = 0;
75 void *data = NULL;
76 size_t len;
77 int ret;
Kumar Gala49c3a862008-10-21 17:25:45 -050078
Simon Glass0d0ba592014-10-19 21:11:20 -060079#ifdef CONFIG_OF_LIBFDT
80 if (images->ft_len) {
81 debug("using: FDT\n");
82 if (image_setup_linux(images)) {
83 puts("FDT creation failed! hanging...");
84 hang();
85 }
86 }
87#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010088 if (images->legacy_hdr_valid) {
89 hdr = images->legacy_hdr_os;
Graeme Russ83088af2011-11-08 02:33:15 +000090 if (image_check_type(hdr, IH_TYPE_MULTI)) {
Simon Glass0d0ba592014-10-19 21:11:20 -060091 ulong os_data, os_len;
92
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010093 /* if multi-part image, we need to get first subimage */
Graeme Russ83088af2011-11-08 02:33:15 +000094 image_multi_getimg(hdr, 0, &os_data, &os_len);
Simon Glass0d0ba592014-10-19 21:11:20 -060095 data = (void *)os_data;
96 len = os_len;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +010097 } else {
98 /* otherwise get image data */
Simon Glass0d0ba592014-10-19 21:11:20 -060099 data = (void *)image_get_data(hdr);
100 len = image_get_data_size(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100101 }
Simon Glass0d0ba592014-10-19 21:11:20 -0600102 is_zimage = 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100103#if defined(CONFIG_FIT)
Anatolij Gustschin25475242017-11-23 18:59:45 +0100104 } else if (images->fit_uname_os && is_zimage) {
Graeme Russ83088af2011-11-08 02:33:15 +0000105 ret = fit_image_get_data(images->fit_hdr_os,
Simon Glass0d0ba592014-10-19 21:11:20 -0600106 images->fit_noffset_os,
107 (const void **)&data, &len);
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100108 if (ret) {
Graeme Russ83088af2011-11-08 02:33:15 +0000109 puts("Can't get image data/size!\n");
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100110 goto error;
111 }
Simon Glass0d0ba592014-10-19 21:11:20 -0600112 is_zimage = 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100113#endif
Simon Glass0d0ba592014-10-19 21:11:20 -0600114 }
115
116 if (is_zimage) {
Simon Glass76539382014-10-10 08:21:56 -0600117 ulong load_address;
Simon Glass0d0ba592014-10-19 21:11:20 -0600118 char *base_ptr;
119
120 base_ptr = (char *)load_zimage(data, len, &load_address);
Hannes Schmelzerc74e3292018-10-11 07:44:42 +0200121 if (!base_ptr) {
122 puts("## Kernel loading failed ...\n");
123 goto error;
124 }
Simon Glass76539382014-10-10 08:21:56 -0600125 images->os.load = load_address;
Simon Glass0d0ba592014-10-19 21:11:20 -0600126 cmd_line_dest = base_ptr + COMMAND_LINE_OFFSET;
127 images->ep = (ulong)base_ptr;
128 } else if (images->ep) {
129 cmd_line_dest = (void *)images->ep + COMMAND_LINE_OFFSET;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100130 } else {
Simon Glass2c363cb2014-10-10 08:21:59 -0600131 printf("## Kernel loading failed (missing x86 kernel setup) ...\n");
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100132 goto error;
Wolfgang Denke6446702006-07-21 11:30:18 +0200133 }
134
Simon Glass0d0ba592014-10-19 21:11:20 -0600135 printf("Setup at %#08lx\n", images->ep);
136 ret = setup_zimage((void *)images->ep, cmd_line_dest,
Gabe Black69370d12011-12-05 12:09:26 +0000137 0, images->rd_start,
Simon Glass0d0ba592014-10-19 21:11:20 -0600138 images->rd_end - images->rd_start);
139
140 if (ret) {
Gabe Black69370d12011-12-05 12:09:26 +0000141 printf("## Setting up boot parameters failed ...\n");
Simon Glass0d0ba592014-10-19 21:11:20 -0600142 return 1;
wdenk2262cfe2002-11-18 00:14:45 +0000143 }
wdenk8bde7f72003-06-27 21:31:46 +0000144
Simon Glass0d0ba592014-10-19 21:11:20 -0600145 return 0;
wdenk8bde7f72003-06-27 21:31:46 +0000146
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100147error:
Kumar Gala40d7e992008-08-15 08:24:45 -0500148 return 1;
wdenk2262cfe2002-11-18 00:14:45 +0000149}
Simon Glass0d0ba592014-10-19 21:11:20 -0600150
Simon Glass76539382014-10-10 08:21:56 -0600151int boot_linux_kernel(ulong setup_base, ulong load_address, bool image_64bit)
152{
153 bootm_announce_and_cleanup();
154
155#ifdef CONFIG_SYS_COREBOOT
156 timestamp_add_now(TS_U_BOOT_START_KERNEL);
157#endif
158 if (image_64bit) {
Simon Glass61643ae2014-10-10 08:21:58 -0600159 if (!cpu_has_64bit()) {
160 puts("Cannot boot 64-bit kernel on 32-bit machine\n");
161 return -EFAULT;
162 }
Simon Glass23b89d42017-01-16 07:04:10 -0700163 /* At present 64-bit U-Boot does not support booting a
164 * kernel.
165 * TODO(sjg@chromium.org): Support booting both 32-bit and
166 * 64-bit kernels from 64-bit U-Boot.
167 */
168#if !CONFIG_IS_ENABLED(X86_64)
Simon Glass61643ae2014-10-10 08:21:58 -0600169 return cpu_jump_to_64bit(setup_base, load_address);
Simon Glass23b89d42017-01-16 07:04:10 -0700170#endif
Simon Glass76539382014-10-10 08:21:56 -0600171 } else {
172 /*
173 * Set %ebx, %ebp, and %edi to 0, %esi to point to the
174 * boot_params structure, and then jump to the kernel. We
175 * assume that %cs is 0x10, 4GB flat, and read/execute, and
176 * the data segments are 0x18, 4GB flat, and read/write.
Bin Menga1875592016-02-05 19:30:11 -0800177 * U-Boot is setting them up that way for itself in
Simon Glass76539382014-10-10 08:21:56 -0600178 * arch/i386/cpu/cpu.c.
Simon Glasse49ccea2015-08-04 12:34:00 -0600179 *
180 * Note that we cannot currently boot a kernel while running as
181 * an EFI application. Please use the payload option for that.
Simon Glass76539382014-10-10 08:21:56 -0600182 */
Simon Glasse49ccea2015-08-04 12:34:00 -0600183#ifndef CONFIG_EFI_APP
Simon Glass76539382014-10-10 08:21:56 -0600184 __asm__ __volatile__ (
185 "movl $0, %%ebp\n"
186 "cli\n"
187 "jmp *%[kernel_entry]\n"
188 :: [kernel_entry]"a"(load_address),
189 [boot_params] "S"(setup_base),
190 "b"(0), "D"(0)
191 );
Simon Glasse49ccea2015-08-04 12:34:00 -0600192#endif
Simon Glass76539382014-10-10 08:21:56 -0600193 }
194
195 /* We can't get to here */
196 return -EFAULT;
197}
198
Simon Glass0d0ba592014-10-19 21:11:20 -0600199/* Subcommand: GO */
200static int boot_jump_linux(bootm_headers_t *images)
201{
202 debug("## Transferring control to Linux (at address %08lx, kernel %08lx) ...\n",
203 images->ep, images->os.load);
204
Simon Glass61643ae2014-10-10 08:21:58 -0600205 return boot_linux_kernel(images->ep, images->os.load,
206 images->os.arch == IH_ARCH_X86_64);
Simon Glass0d0ba592014-10-19 21:11:20 -0600207}
208
Simon Glass09140112020-05-10 11:40:03 -0600209int do_bootm_linux(int flag, int argc, char *const argv[],
210 bootm_headers_t *images)
Simon Glass0d0ba592014-10-19 21:11:20 -0600211{
212 /* No need for those on x86 */
213 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
214 return -1;
215
216 if (flag & BOOTM_STATE_OS_PREP)
217 return boot_prep_linux(images);
218
Simon Glass76539382014-10-10 08:21:56 -0600219 if (flag & BOOTM_STATE_OS_GO)
220 return boot_jump_linux(images);
Simon Glass0d0ba592014-10-19 21:11:20 -0600221
222 return boot_jump_linux(images);
223}