Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 2 | /* |
Gabe Black | 1cf5121 | 2011-12-05 12:09:23 +0000 | [diff] [blame] | 3 | * Copyright (c) 2011 The Chromium OS Authors. |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 4 | * (C) Copyright 2002 |
Albert ARIBAUD | 60fbc8d | 2011-08-04 18:45:45 +0200 | [diff] [blame] | 5 | * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se> |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 8 | /* |
Graeme Russ | 45fc1d8 | 2011-04-13 19:43:26 +1000 | [diff] [blame] | 9 | * Linux x86 zImage and bzImage loading |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 10 | * |
| 11 | * based on the procdure described in |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 12 | * linux/Documentation/i386/boot.txt |
| 13 | */ |
| 14 | |
| 15 | #include <common.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 16 | #include <command.h> |
Simon Glass | 83c2e49 | 2019-08-01 09:46:50 -0600 | [diff] [blame] | 17 | #include <env.h> |
Simon Glass | 8f3f761 | 2019-11-14 12:57:42 -0700 | [diff] [blame] | 18 | #include <irq_func.h> |
Ivan Gorinov | 901bb0b | 2018-03-26 18:06:54 -0700 | [diff] [blame] | 19 | #include <malloc.h> |
Simon Glass | 858fed1 | 2020-04-08 16:57:36 -0600 | [diff] [blame] | 20 | #include <acpi/acpi_table.h> |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 21 | #include <asm/io.h> |
| 22 | #include <asm/ptrace.h> |
| 23 | #include <asm/zimage.h> |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 24 | #include <asm/byteorder.h> |
Simon Glass | aa83f2b | 2014-10-19 21:11:20 -0600 | [diff] [blame] | 25 | #include <asm/bootm.h> |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 26 | #include <asm/bootparam.h> |
Vadim Bendebury | 8cd22c8 | 2012-10-23 18:04:34 +0000 | [diff] [blame] | 27 | #ifdef CONFIG_SYS_COREBOOT |
| 28 | #include <asm/arch/timestamp.h> |
| 29 | #endif |
Stefan Reinauer | 2e5225e | 2012-10-23 18:04:37 +0000 | [diff] [blame] | 30 | #include <linux/compiler.h> |
Ivan Gorinov | 901bb0b | 2018-03-26 18:06:54 -0700 | [diff] [blame] | 31 | #include <linux/libfdt.h> |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Memory lay-out: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 35 | * |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 36 | * relative to setup_base (which is 0x90000 currently) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 37 | * |
| 38 | * 0x0000-0x7FFF Real mode kernel |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 39 | * 0x8000-0x8FFF Stack and heap |
| 40 | * 0x9000-0x90FF Kernel command line |
| 41 | */ |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 42 | #define DEFAULT_SETUP_BASE 0x90000 |
| 43 | #define COMMAND_LINE_OFFSET 0x9000 |
| 44 | #define HEAP_END_OFFSET 0x8e00 |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 45 | |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 46 | #define COMMAND_LINE_SIZE 2048 |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 47 | |
Simon Glass | 48800f4 | 2020-09-05 14:50:38 -0600 | [diff] [blame] | 48 | /** |
| 49 | * struct zboot_state - Current state of the boot |
| 50 | * |
| 51 | * @bzimage_addr: Address of the bzImage to boot |
| 52 | * @bzimage_size: Size of the bzImage, or 0 to detect this |
| 53 | * @initrd_addr: Address of the initial ramdisk, or 0 if none |
| 54 | * @initrd_size: Size of the initial ramdisk, or 0 if none |
| 55 | * @load_address: Address where the bzImage is moved before booting, either |
| 56 | * BZIMAGE_LOAD_ADDR or ZIMAGE_LOAD_ADDR |
| 57 | */ |
| 58 | struct zboot_state { |
| 59 | ulong bzimage_addr; |
| 60 | ulong bzimage_size; |
| 61 | ulong initrd_addr; |
| 62 | ulong initrd_size; |
| 63 | ulong load_address; |
| 64 | } state; |
| 65 | |
Simon Glass | d9b3c98 | 2020-09-05 14:50:43 -0600 | [diff] [blame^] | 66 | enum { |
| 67 | ZBOOT_STATE_START = BIT(0), |
| 68 | |
| 69 | ZBOOT_STATE_COUNT = 1, |
| 70 | }; |
| 71 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 72 | static void build_command_line(char *command_line, int auto_boot) |
| 73 | { |
| 74 | char *env_command_line; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 75 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 76 | command_line[0] = '\0'; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 77 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 78 | env_command_line = env_get("bootargs"); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 79 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 80 | /* set console= argument if we use a serial console */ |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 81 | if (!strstr(env_command_line, "console=")) { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 82 | if (!strcmp(env_get("stdout"), "serial")) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 83 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 84 | /* We seem to use serial console */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 85 | sprintf(command_line, "console=ttyS0,%s ", |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 86 | env_get("baudrate")); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 89 | |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 90 | if (auto_boot) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 91 | strcat(command_line, "auto "); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 92 | |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 93 | if (env_command_line) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 94 | strcat(command_line, env_command_line); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 95 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 96 | printf("Kernel command line: \"%s\"\n", command_line); |
| 97 | } |
| 98 | |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 99 | static int kernel_magic_ok(struct setup_header *hdr) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 100 | { |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 101 | if (KERNEL_MAGIC != hdr->boot_flag) { |
Gabe Black | 1cf5121 | 2011-12-05 12:09:23 +0000 | [diff] [blame] | 102 | printf("Error: Invalid Boot Flag " |
| 103 | "(found 0x%04x, expected 0x%04x)\n", |
| 104 | hdr->boot_flag, KERNEL_MAGIC); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 105 | return 0; |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 106 | } else { |
| 107 | printf("Valid Boot Flag\n"); |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 108 | return 1; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 109 | } |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 110 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 111 | |
Simon Glass | df30d41 | 2020-09-05 14:50:40 -0600 | [diff] [blame] | 112 | static int get_boot_protocol(struct setup_header *hdr, bool verbose) |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 113 | { |
| 114 | if (hdr->header == KERNEL_V2_MAGIC) { |
Simon Glass | df30d41 | 2020-09-05 14:50:40 -0600 | [diff] [blame] | 115 | if (verbose) |
| 116 | printf("Magic signature found\n"); |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 117 | return hdr->version; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 118 | } else { |
| 119 | /* Very old kernel */ |
Simon Glass | df30d41 | 2020-09-05 14:50:40 -0600 | [diff] [blame] | 120 | if (verbose) |
| 121 | printf("Magic signature not found\n"); |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 122 | return 0x0100; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 123 | } |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Ivan Gorinov | 901bb0b | 2018-03-26 18:06:54 -0700 | [diff] [blame] | 126 | static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob) |
| 127 | { |
Simon Glass | df30d41 | 2020-09-05 14:50:40 -0600 | [diff] [blame] | 128 | int bootproto = get_boot_protocol(hdr, false); |
Ivan Gorinov | 901bb0b | 2018-03-26 18:06:54 -0700 | [diff] [blame] | 129 | struct setup_data *sd; |
| 130 | int size; |
| 131 | |
| 132 | if (bootproto < 0x0209) |
| 133 | return -ENOTSUPP; |
| 134 | |
| 135 | if (!fdt_blob) |
| 136 | return 0; |
| 137 | |
| 138 | size = fdt_totalsize(fdt_blob); |
| 139 | if (size < 0) |
| 140 | return -EINVAL; |
| 141 | |
| 142 | size += sizeof(struct setup_data); |
| 143 | sd = (struct setup_data *)malloc(size); |
| 144 | if (!sd) { |
| 145 | printf("Not enough memory for DTB setup data\n"); |
| 146 | return -ENOMEM; |
| 147 | } |
| 148 | |
| 149 | sd->next = hdr->setup_data; |
| 150 | sd->type = SETUP_DTB; |
| 151 | sd->len = fdt_totalsize(fdt_blob); |
| 152 | memcpy(sd->data, fdt_blob, sd->len); |
| 153 | hdr->setup_data = (unsigned long)sd; |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
Simon Glass | df30d41 | 2020-09-05 14:50:40 -0600 | [diff] [blame] | 158 | static const char *get_kernel_version(struct boot_params *params, |
| 159 | void *kernel_base) |
| 160 | { |
| 161 | struct setup_header *hdr = ¶ms->hdr; |
| 162 | int bootproto; |
| 163 | |
| 164 | bootproto = get_boot_protocol(hdr, false); |
| 165 | if (bootproto < 0x0200 || hdr->setup_sects < 15) |
| 166 | return NULL; |
| 167 | |
| 168 | return kernel_base + hdr->kernel_version + 0x200; |
| 169 | } |
| 170 | |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 171 | struct boot_params *load_zimage(char *image, unsigned long kernel_size, |
Simon Glass | d98a5bd | 2014-10-10 08:21:56 -0600 | [diff] [blame] | 172 | ulong *load_addressp) |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 173 | { |
| 174 | struct boot_params *setup_base; |
Simon Glass | df30d41 | 2020-09-05 14:50:40 -0600 | [diff] [blame] | 175 | const char *version; |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 176 | int setup_size; |
| 177 | int bootproto; |
| 178 | int big_image; |
| 179 | |
| 180 | struct boot_params *params = (struct boot_params *)image; |
| 181 | struct setup_header *hdr = ¶ms->hdr; |
| 182 | |
| 183 | /* base address for real-mode segment */ |
| 184 | setup_base = (struct boot_params *)DEFAULT_SETUP_BASE; |
| 185 | |
| 186 | if (!kernel_magic_ok(hdr)) |
| 187 | return 0; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 188 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 189 | /* determine size of setup */ |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 190 | if (0 == hdr->setup_sects) { |
| 191 | printf("Setup Sectors = 0 (defaulting to 4)\n"); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 192 | setup_size = 5 * 512; |
| 193 | } else { |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 194 | setup_size = (hdr->setup_sects + 1) * 512; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 195 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 196 | |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 197 | printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size); |
| 198 | |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 199 | if (setup_size > SETUP_MAX_SIZE) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 200 | printf("Error: Setup is too large (%d bytes)\n", setup_size); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 201 | |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 202 | /* determine boot protocol version */ |
Simon Glass | df30d41 | 2020-09-05 14:50:40 -0600 | [diff] [blame] | 203 | bootproto = get_boot_protocol(hdr, true); |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 204 | |
| 205 | printf("Using boot protocol version %x.%02x\n", |
| 206 | (bootproto & 0xff00) >> 8, bootproto & 0xff); |
| 207 | |
Simon Glass | df30d41 | 2020-09-05 14:50:40 -0600 | [diff] [blame] | 208 | version = get_kernel_version(params, image); |
| 209 | if (version) |
| 210 | printf("Linux kernel version %s\n", version); |
| 211 | else |
| 212 | printf("Setup Sectors < 15 - Cannot print kernel version\n"); |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 213 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 214 | /* Determine image type */ |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 215 | big_image = (bootproto >= 0x0200) && |
| 216 | (hdr->loadflags & BIG_KERNEL_FLAG); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 217 | |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 218 | /* Determine load address */ |
Gabe Black | 1cf5121 | 2011-12-05 12:09:23 +0000 | [diff] [blame] | 219 | if (big_image) |
Simon Glass | d98a5bd | 2014-10-10 08:21:56 -0600 | [diff] [blame] | 220 | *load_addressp = BZIMAGE_LOAD_ADDR; |
Gabe Black | 1cf5121 | 2011-12-05 12:09:23 +0000 | [diff] [blame] | 221 | else |
Simon Glass | d98a5bd | 2014-10-10 08:21:56 -0600 | [diff] [blame] | 222 | *load_addressp = ZIMAGE_LOAD_ADDR; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 223 | |
Gabe Black | c67a948 | 2011-12-05 12:09:24 +0000 | [diff] [blame] | 224 | printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base); |
| 225 | memset(setup_base, 0, sizeof(*setup_base)); |
| 226 | setup_base->hdr = params->hdr; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 227 | |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 228 | if (bootproto >= 0x0204) |
| 229 | kernel_size = hdr->syssize * 16; |
| 230 | else |
| 231 | kernel_size -= setup_size; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 232 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 233 | if (bootproto == 0x0100) { |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 234 | /* |
| 235 | * A very old kernel MUST have its real-mode code |
| 236 | * loaded at 0x90000 |
| 237 | */ |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 238 | if ((ulong)setup_base != 0x90000) { |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 239 | /* Copy the real-mode kernel */ |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 240 | memmove((void *)0x90000, setup_base, setup_size); |
| 241 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 242 | /* Copy the command line */ |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 243 | memmove((void *)0x99000, |
Gabe Black | 1cf5121 | 2011-12-05 12:09:23 +0000 | [diff] [blame] | 244 | (u8 *)setup_base + COMMAND_LINE_OFFSET, |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 245 | COMMAND_LINE_SIZE); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 246 | |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 247 | /* Relocated */ |
Gabe Black | 1cf5121 | 2011-12-05 12:09:23 +0000 | [diff] [blame] | 248 | setup_base = (struct boot_params *)0x90000; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 249 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 250 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 251 | /* It is recommended to clear memory up to the 32K mark */ |
Gabe Black | 1cf5121 | 2011-12-05 12:09:23 +0000 | [diff] [blame] | 252 | memset((u8 *)0x90000 + setup_size, 0, |
| 253 | SETUP_MAX_SIZE - setup_size); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 254 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 255 | |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 256 | if (big_image) { |
| 257 | if (kernel_size > BZIMAGE_MAX_SIZE) { |
| 258 | printf("Error: bzImage kernel too big! " |
| 259 | "(size: %ld, max: %d)\n", |
| 260 | kernel_size, BZIMAGE_MAX_SIZE); |
| 261 | return 0; |
| 262 | } |
| 263 | } else if ((kernel_size) > ZIMAGE_MAX_SIZE) { |
| 264 | printf("Error: zImage kernel too big! (size: %ld, max: %d)\n", |
| 265 | kernel_size, ZIMAGE_MAX_SIZE); |
| 266 | return 0; |
| 267 | } |
| 268 | |
Simon Glass | d98a5bd | 2014-10-10 08:21:56 -0600 | [diff] [blame] | 269 | printf("Loading %s at address %lx (%ld bytes)\n", |
| 270 | big_image ? "bzImage" : "zImage", *load_addressp, kernel_size); |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 271 | |
Simon Glass | d98a5bd | 2014-10-10 08:21:56 -0600 | [diff] [blame] | 272 | memmove((void *)*load_addressp, image + setup_size, kernel_size); |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 273 | |
| 274 | return setup_base; |
| 275 | } |
| 276 | |
| 277 | int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot, |
| 278 | unsigned long initrd_addr, unsigned long initrd_size) |
| 279 | { |
| 280 | struct setup_header *hdr = &setup_base->hdr; |
Simon Glass | df30d41 | 2020-09-05 14:50:40 -0600 | [diff] [blame] | 281 | int bootproto = get_boot_protocol(hdr, false); |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 282 | |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 283 | setup_base->e820_entries = install_e820_map( |
| 284 | ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map); |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 285 | |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 286 | if (bootproto == 0x0100) { |
| 287 | setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC; |
| 288 | setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET; |
| 289 | } |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 290 | if (bootproto >= 0x0200) { |
Simon Glass | d67ff03 | 2020-09-05 14:50:41 -0600 | [diff] [blame] | 291 | hdr->type_of_loader = 0x80; /* U-Boot version 0 */ |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 292 | if (initrd_addr) { |
Gabe Black | 1cf5121 | 2011-12-05 12:09:23 +0000 | [diff] [blame] | 293 | printf("Initial RAM disk at linear address " |
| 294 | "0x%08lx, size %ld bytes\n", |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 295 | initrd_addr, initrd_size); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 296 | |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 297 | hdr->ramdisk_image = initrd_addr; |
| 298 | hdr->ramdisk_size = initrd_size; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 301 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 302 | if (bootproto >= 0x0201) { |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 303 | hdr->heap_end_ptr = HEAP_END_OFFSET; |
| 304 | hdr->loadflags |= HEAP_FLAG; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 305 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 306 | |
Simon Glass | 4b4f273 | 2014-10-19 21:11:21 -0600 | [diff] [blame] | 307 | if (cmd_line) { |
| 308 | if (bootproto >= 0x0202) { |
| 309 | hdr->cmd_line_ptr = (uintptr_t)cmd_line; |
| 310 | } else if (bootproto >= 0x0200) { |
| 311 | setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC; |
| 312 | setup_base->screen_info.cl_offset = |
| 313 | (uintptr_t)cmd_line - (uintptr_t)setup_base; |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 314 | |
Simon Glass | 4b4f273 | 2014-10-19 21:11:21 -0600 | [diff] [blame] | 315 | hdr->setup_move_size = 0x9100; |
| 316 | } |
| 317 | |
| 318 | /* build command line at COMMAND_LINE_OFFSET */ |
| 319 | build_command_line(cmd_line, auto_boot); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 320 | } |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 321 | |
Simon Glass | 3a17717 | 2020-09-05 14:50:39 -0600 | [diff] [blame] | 322 | if (IS_ENABLED(CONFIG_INTEL_MID) && bootproto >= 0x0207) |
Andy Shevchenko | 8b7adf8 | 2018-01-10 19:40:14 +0200 | [diff] [blame] | 323 | hdr->hardware_subarch = X86_SUBARCH_INTEL_MID; |
Andy Shevchenko | 8b7adf8 | 2018-01-10 19:40:14 +0200 | [diff] [blame] | 324 | |
Simon Glass | 3a17717 | 2020-09-05 14:50:39 -0600 | [diff] [blame] | 325 | if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) |
| 326 | setup_base->acpi_rsdp_addr = acpi_get_rsdp_addr(); |
Andy Shevchenko | d87a21d | 2019-09-13 18:42:00 +0300 | [diff] [blame] | 327 | |
Ivan Gorinov | 901bb0b | 2018-03-26 18:06:54 -0700 | [diff] [blame] | 328 | setup_device_tree(hdr, (const void *)env_get_hex("fdtaddr", 0)); |
Bin Meng | f6d504f | 2015-07-06 16:31:36 +0800 | [diff] [blame] | 329 | setup_video(&setup_base->screen_info); |
| 330 | |
Simon Glass | 3a17717 | 2020-09-05 14:50:39 -0600 | [diff] [blame] | 331 | if (IS_ENABLED(CONFIG_EFI_STUB)) |
| 332 | setup_efi_info(&setup_base->efi_info); |
Bin Meng | 106dcfc | 2018-08-23 08:24:10 -0700 | [diff] [blame] | 333 | |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 334 | return 0; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Simon Glass | d9b3c98 | 2020-09-05 14:50:43 -0600 | [diff] [blame^] | 337 | static int do_zboot_start(struct cmd_tbl *cmdtp, int flag, int argc, |
| 338 | char *const argv[]) |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 339 | { |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 340 | struct boot_params *base_ptr; |
Simon Glass | d9b3c98 | 2020-09-05 14:50:43 -0600 | [diff] [blame^] | 341 | const char *s; |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 342 | |
Simon Glass | 48800f4 | 2020-09-05 14:50:38 -0600 | [diff] [blame] | 343 | memset(&state, '\0', sizeof(state)); |
Gabe Black | 1cf5121 | 2011-12-05 12:09:23 +0000 | [diff] [blame] | 344 | if (argc >= 2) { |
Graeme Russ | 3134be2 | 2010-10-07 20:03:19 +1100 | [diff] [blame] | 345 | /* argv[1] holds the address of the bzImage */ |
| 346 | s = argv[1]; |
Gabe Black | 1cf5121 | 2011-12-05 12:09:23 +0000 | [diff] [blame] | 347 | } else { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 348 | s = env_get("fileaddr"); |
Gabe Black | 1cf5121 | 2011-12-05 12:09:23 +0000 | [diff] [blame] | 349 | } |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 350 | |
Graeme Russ | 3134be2 | 2010-10-07 20:03:19 +1100 | [diff] [blame] | 351 | if (s) |
Simon Glass | 48800f4 | 2020-09-05 14:50:38 -0600 | [diff] [blame] | 352 | state.bzimage_addr = simple_strtoul(s, NULL, 16); |
Graeme Russ | 3134be2 | 2010-10-07 20:03:19 +1100 | [diff] [blame] | 353 | |
Gabe Black | 69b49ee | 2011-12-05 12:09:27 +0000 | [diff] [blame] | 354 | if (argc >= 3) { |
Graeme Russ | 3134be2 | 2010-10-07 20:03:19 +1100 | [diff] [blame] | 355 | /* argv[2] holds the size of the bzImage */ |
Simon Glass | 48800f4 | 2020-09-05 14:50:38 -0600 | [diff] [blame] | 356 | state.bzimage_size = simple_strtoul(argv[2], NULL, 16); |
Gabe Black | 69b49ee | 2011-12-05 12:09:27 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | if (argc >= 4) |
Simon Glass | 48800f4 | 2020-09-05 14:50:38 -0600 | [diff] [blame] | 360 | state.initrd_addr = simple_strtoul(argv[3], NULL, 16); |
Gabe Black | 69b49ee | 2011-12-05 12:09:27 +0000 | [diff] [blame] | 361 | if (argc >= 5) |
Simon Glass | 48800f4 | 2020-09-05 14:50:38 -0600 | [diff] [blame] | 362 | state.initrd_size = simple_strtoul(argv[4], NULL, 16); |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 363 | |
Gabe Black | 1cf5121 | 2011-12-05 12:09:23 +0000 | [diff] [blame] | 364 | /* Lets look for */ |
Simon Glass | 48800f4 | 2020-09-05 14:50:38 -0600 | [diff] [blame] | 365 | base_ptr = load_zimage((void *)state.bzimage_addr, state.bzimage_size, |
| 366 | &state.load_address); |
Graeme Russ | 883c603 | 2011-11-08 02:33:15 +0000 | [diff] [blame] | 367 | if (!base_ptr) { |
Simon Glass | ddf446b | 2014-10-10 08:21:59 -0600 | [diff] [blame] | 368 | puts("## Kernel loading failed ...\n"); |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 369 | return -1; |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 370 | } |
Simon Glass | 48800f4 | 2020-09-05 14:50:38 -0600 | [diff] [blame] | 371 | |
| 372 | if (setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET, 0, |
| 373 | state.initrd_addr, state.initrd_size)) { |
Simon Glass | ddf446b | 2014-10-10 08:21:59 -0600 | [diff] [blame] | 374 | puts("Setting up boot parameters failed ...\n"); |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 375 | return -1; |
| 376 | } |
| 377 | |
Simon Glass | 0db800c | 2020-09-05 14:50:42 -0600 | [diff] [blame] | 378 | disable_interrupts(); |
Gabe Black | 540c262 | 2011-12-05 12:09:26 +0000 | [diff] [blame] | 379 | /* we assume that the kernel is in place */ |
Simon Glass | 48800f4 | 2020-09-05 14:50:38 -0600 | [diff] [blame] | 380 | return boot_linux_kernel((ulong)base_ptr, state.load_address, false); |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 381 | } |
| 382 | |
Simon Glass | d9b3c98 | 2020-09-05 14:50:43 -0600 | [diff] [blame^] | 383 | /* Note: This defines the complete_zboot() function */ |
| 384 | U_BOOT_SUBCMDS(zboot, |
| 385 | U_BOOT_CMD_MKENT(start, 6, 1, do_zboot_start, "", ""), |
| 386 | ) |
| 387 | |
| 388 | int do_zboot_states(struct cmd_tbl *cmdtp, int flag, int argc, |
| 389 | char *const argv[], int state_mask) |
| 390 | { |
| 391 | int i; |
| 392 | |
| 393 | for (i = 0; i < ZBOOT_STATE_COUNT; i++) { |
| 394 | struct cmd_tbl *cmd = &zboot_subcmds[i]; |
| 395 | int mask = 1 << i; |
| 396 | int ret; |
| 397 | |
| 398 | if (mask & state_mask) { |
| 399 | ret = cmd->cmd(cmd, flag, argc, argv); |
| 400 | if (ret) |
| 401 | return ret; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc, |
| 409 | char *const argv[], int *repeatable) |
| 410 | { |
| 411 | /* determine if we have a sub command */ |
| 412 | if (argc > 1) { |
| 413 | char *endp; |
| 414 | |
| 415 | simple_strtoul(argv[1], &endp, 16); |
| 416 | /* |
| 417 | * endp pointing to nul means that argv[1] was just a valid |
| 418 | * number, so pass it along to the normal processing |
| 419 | */ |
| 420 | if (*endp) |
| 421 | return do_zboot(cmdtp, flag, argc, argv, repeatable); |
| 422 | } |
| 423 | |
| 424 | do_zboot_states(cmdtp, flag, argc, argv, ZBOOT_STATE_START); |
| 425 | |
| 426 | return CMD_RET_FAILURE; |
| 427 | } |
| 428 | |
| 429 | U_BOOT_CMDREP_COMPLETE( |
| 430 | zboot, 6, do_zboot_parent, "Boot bzImage", |
Gabe Black | 69b49ee | 2011-12-05 12:09:27 +0000 | [diff] [blame] | 431 | "[addr] [size] [initrd addr] [initrd size]\n" |
| 432 | " addr - The optional starting address of the bzimage.\n" |
| 433 | " If not set it defaults to the environment\n" |
| 434 | " variable \"fileaddr\".\n" |
| 435 | " size - The optional size of the bzimage. Defaults to\n" |
| 436 | " zero.\n" |
| 437 | " initrd addr - The address of the initrd image to use, if any.\n" |
| 438 | " initrd size - The size of the initrd image to use, if any.\n" |
Simon Glass | d9b3c98 | 2020-09-05 14:50:43 -0600 | [diff] [blame^] | 439 | "\n" |
| 440 | "Sub-commands to do part of the zboot sequence:\n" |
| 441 | "\tstart [addr [arg ...]] - specify arguments\n", |
| 442 | complete_zboot |
Graeme Russ | 1bab104 | 2010-04-24 00:05:49 +1000 | [diff] [blame] | 443 | ); |