d07041fd4ceb4dfd2a5a6cae47675e63b6fa0758
[oweals/u-boot.git] / arch / x86 / lib / zimage.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011 The Chromium OS Authors.
4  * (C) Copyright 2002
5  * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
6  */
7
8 /*
9  * Linux x86 zImage and bzImage loading
10  *
11  * based on the procdure described in
12  * linux/Documentation/i386/boot.txt
13  */
14
15 #include <common.h>
16 #include <env.h>
17 #include <malloc.h>
18 #include <asm/acpi_table.h>
19 #include <asm/io.h>
20 #include <asm/ptrace.h>
21 #include <asm/zimage.h>
22 #include <asm/byteorder.h>
23 #include <asm/bootm.h>
24 #include <asm/bootparam.h>
25 #ifdef CONFIG_SYS_COREBOOT
26 #include <asm/arch/timestamp.h>
27 #endif
28 #include <linux/compiler.h>
29 #include <linux/libfdt.h>
30
31 /*
32  * Memory lay-out:
33  *
34  * relative to setup_base (which is 0x90000 currently)
35  *
36  *      0x0000-0x7FFF   Real mode kernel
37  *      0x8000-0x8FFF   Stack and heap
38  *      0x9000-0x90FF   Kernel command line
39  */
40 #define DEFAULT_SETUP_BASE      0x90000
41 #define COMMAND_LINE_OFFSET     0x9000
42 #define HEAP_END_OFFSET         0x8e00
43
44 #define COMMAND_LINE_SIZE       2048
45
46 static void build_command_line(char *command_line, int auto_boot)
47 {
48         char *env_command_line;
49
50         command_line[0] = '\0';
51
52         env_command_line =  env_get("bootargs");
53
54         /* set console= argument if we use a serial console */
55         if (!strstr(env_command_line, "console=")) {
56                 if (!strcmp(env_get("stdout"), "serial")) {
57
58                         /* We seem to use serial console */
59                         sprintf(command_line, "console=ttyS0,%s ",
60                                 env_get("baudrate"));
61                 }
62         }
63
64         if (auto_boot)
65                 strcat(command_line, "auto ");
66
67         if (env_command_line)
68                 strcat(command_line, env_command_line);
69
70         printf("Kernel command line: \"%s\"\n", command_line);
71 }
72
73 static int kernel_magic_ok(struct setup_header *hdr)
74 {
75         if (KERNEL_MAGIC != hdr->boot_flag) {
76                 printf("Error: Invalid Boot Flag "
77                         "(found 0x%04x, expected 0x%04x)\n",
78                         hdr->boot_flag, KERNEL_MAGIC);
79                 return 0;
80         } else {
81                 printf("Valid Boot Flag\n");
82                 return 1;
83         }
84 }
85
86 static int get_boot_protocol(struct setup_header *hdr)
87 {
88         if (hdr->header == KERNEL_V2_MAGIC) {
89                 printf("Magic signature found\n");
90                 return hdr->version;
91         } else {
92                 /* Very old kernel */
93                 printf("Magic signature not found\n");
94                 return 0x0100;
95         }
96 }
97
98 static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob)
99 {
100         int bootproto = get_boot_protocol(hdr);
101         struct setup_data *sd;
102         int size;
103
104         if (bootproto < 0x0209)
105                 return -ENOTSUPP;
106
107         if (!fdt_blob)
108                 return 0;
109
110         size = fdt_totalsize(fdt_blob);
111         if (size < 0)
112                 return -EINVAL;
113
114         size += sizeof(struct setup_data);
115         sd = (struct setup_data *)malloc(size);
116         if (!sd) {
117                 printf("Not enough memory for DTB setup data\n");
118                 return -ENOMEM;
119         }
120
121         sd->next = hdr->setup_data;
122         sd->type = SETUP_DTB;
123         sd->len = fdt_totalsize(fdt_blob);
124         memcpy(sd->data, fdt_blob, sd->len);
125         hdr->setup_data = (unsigned long)sd;
126
127         return 0;
128 }
129
130 struct boot_params *load_zimage(char *image, unsigned long kernel_size,
131                                 ulong *load_addressp)
132 {
133         struct boot_params *setup_base;
134         int setup_size;
135         int bootproto;
136         int big_image;
137
138         struct boot_params *params = (struct boot_params *)image;
139         struct setup_header *hdr = &params->hdr;
140
141         /* base address for real-mode segment */
142         setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
143
144         if (!kernel_magic_ok(hdr))
145                 return 0;
146
147         /* determine size of setup */
148         if (0 == hdr->setup_sects) {
149                 printf("Setup Sectors = 0 (defaulting to 4)\n");
150                 setup_size = 5 * 512;
151         } else {
152                 setup_size = (hdr->setup_sects + 1) * 512;
153         }
154
155         printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
156
157         if (setup_size > SETUP_MAX_SIZE)
158                 printf("Error: Setup is too large (%d bytes)\n", setup_size);
159
160         /* determine boot protocol version */
161         bootproto = get_boot_protocol(hdr);
162
163         printf("Using boot protocol version %x.%02x\n",
164                (bootproto & 0xff00) >> 8, bootproto & 0xff);
165
166         if (bootproto >= 0x0200) {
167                 if (hdr->setup_sects >= 15) {
168                         printf("Linux kernel version %s\n",
169                                 (char *)params +
170                                 hdr->kernel_version + 0x200);
171                 } else {
172                         printf("Setup Sectors < 15 - "
173                                 "Cannot print kernel version.\n");
174                 }
175         }
176
177         /* Determine image type */
178         big_image = (bootproto >= 0x0200) &&
179                     (hdr->loadflags & BIG_KERNEL_FLAG);
180
181         /* Determine load address */
182         if (big_image)
183                 *load_addressp = BZIMAGE_LOAD_ADDR;
184         else
185                 *load_addressp = ZIMAGE_LOAD_ADDR;
186
187         printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
188         memset(setup_base, 0, sizeof(*setup_base));
189         setup_base->hdr = params->hdr;
190
191         if (bootproto >= 0x0204)
192                 kernel_size = hdr->syssize * 16;
193         else
194                 kernel_size -= setup_size;
195
196         if (bootproto == 0x0100) {
197                 /*
198                  * A very old kernel MUST have its real-mode code
199                  * loaded at 0x90000
200                  */
201                 if ((ulong)setup_base != 0x90000) {
202                         /* Copy the real-mode kernel */
203                         memmove((void *)0x90000, setup_base, setup_size);
204
205                         /* Copy the command line */
206                         memmove((void *)0x99000,
207                                 (u8 *)setup_base + COMMAND_LINE_OFFSET,
208                                 COMMAND_LINE_SIZE);
209
210                          /* Relocated */
211                         setup_base = (struct boot_params *)0x90000;
212                 }
213
214                 /* It is recommended to clear memory up to the 32K mark */
215                 memset((u8 *)0x90000 + setup_size, 0,
216                        SETUP_MAX_SIZE - setup_size);
217         }
218
219         if (big_image) {
220                 if (kernel_size > BZIMAGE_MAX_SIZE) {
221                         printf("Error: bzImage kernel too big! "
222                                 "(size: %ld, max: %d)\n",
223                                 kernel_size, BZIMAGE_MAX_SIZE);
224                         return 0;
225                 }
226         } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
227                 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
228                        kernel_size, ZIMAGE_MAX_SIZE);
229                 return 0;
230         }
231
232         printf("Loading %s at address %lx (%ld bytes)\n",
233                big_image ? "bzImage" : "zImage", *load_addressp, kernel_size);
234
235         memmove((void *)*load_addressp, image + setup_size, kernel_size);
236
237         return setup_base;
238 }
239
240 int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
241                  unsigned long initrd_addr, unsigned long initrd_size)
242 {
243         struct setup_header *hdr = &setup_base->hdr;
244         int bootproto = get_boot_protocol(hdr);
245
246         setup_base->e820_entries = install_e820_map(
247                 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
248
249         if (bootproto == 0x0100) {
250                 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
251                 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
252         }
253         if (bootproto >= 0x0200) {
254                 hdr->type_of_loader = 8;
255
256                 if (initrd_addr) {
257                         printf("Initial RAM disk at linear address "
258                                "0x%08lx, size %ld bytes\n",
259                                initrd_addr, initrd_size);
260
261                         hdr->ramdisk_image = initrd_addr;
262                         hdr->ramdisk_size = initrd_size;
263                 }
264         }
265
266         if (bootproto >= 0x0201) {
267                 hdr->heap_end_ptr = HEAP_END_OFFSET;
268                 hdr->loadflags |= HEAP_FLAG;
269         }
270
271         if (cmd_line) {
272                 if (bootproto >= 0x0202) {
273                         hdr->cmd_line_ptr = (uintptr_t)cmd_line;
274                 } else if (bootproto >= 0x0200) {
275                         setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
276                         setup_base->screen_info.cl_offset =
277                                 (uintptr_t)cmd_line - (uintptr_t)setup_base;
278
279                         hdr->setup_move_size = 0x9100;
280                 }
281
282                 /* build command line at COMMAND_LINE_OFFSET */
283                 build_command_line(cmd_line, auto_boot);
284         }
285
286 #ifdef CONFIG_INTEL_MID
287         if (bootproto >= 0x0207)
288                 hdr->hardware_subarch = X86_SUBARCH_INTEL_MID;
289 #endif
290
291 #ifdef CONFIG_GENERATE_ACPI_TABLE
292         setup_base->acpi_rsdp_addr = acpi_get_rsdp_addr();
293 #endif
294
295         setup_device_tree(hdr, (const void *)env_get_hex("fdtaddr", 0));
296         setup_video(&setup_base->screen_info);
297
298 #ifdef CONFIG_EFI_STUB
299         setup_efi_info(&setup_base->efi_info);
300 #endif
301
302         return 0;
303 }
304
305 void setup_pcat_compatibility(void)
306         __attribute__((weak, alias("__setup_pcat_compatibility")));
307
308 void __setup_pcat_compatibility(void)
309 {
310 }
311
312 int do_zboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
313 {
314         struct boot_params *base_ptr;
315         void *bzImage_addr = NULL;
316         ulong load_address;
317         char *s;
318         ulong bzImage_size = 0;
319         ulong initrd_addr = 0;
320         ulong initrd_size = 0;
321
322         disable_interrupts();
323
324         /* Setup board for maximum PC/AT Compatibility */
325         setup_pcat_compatibility();
326
327         if (argc >= 2) {
328                 /* argv[1] holds the address of the bzImage */
329                 s = argv[1];
330         } else {
331                 s = env_get("fileaddr");
332         }
333
334         if (s)
335                 bzImage_addr = (void *)simple_strtoul(s, NULL, 16);
336
337         if (argc >= 3) {
338                 /* argv[2] holds the size of the bzImage */
339                 bzImage_size = simple_strtoul(argv[2], NULL, 16);
340         }
341
342         if (argc >= 4)
343                 initrd_addr = simple_strtoul(argv[3], NULL, 16);
344         if (argc >= 5)
345                 initrd_size = simple_strtoul(argv[4], NULL, 16);
346
347         /* Lets look for */
348         base_ptr = load_zimage(bzImage_addr, bzImage_size, &load_address);
349
350         if (!base_ptr) {
351                 puts("## Kernel loading failed ...\n");
352                 return -1;
353         }
354         if (setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
355                         0, initrd_addr, initrd_size)) {
356                 puts("Setting up boot parameters failed ...\n");
357                 return -1;
358         }
359
360         /* we assume that the kernel is in place */
361         return boot_linux_kernel((ulong)base_ptr, load_address, false);
362 }
363
364 U_BOOT_CMD(
365         zboot, 5, 0,    do_zboot,
366         "Boot bzImage",
367         "[addr] [size] [initrd addr] [initrd size]\n"
368         "      addr -        The optional starting address of the bzimage.\n"
369         "                    If not set it defaults to the environment\n"
370         "                    variable \"fileaddr\".\n"
371         "      size -        The optional size of the bzimage. Defaults to\n"
372         "                    zero.\n"
373         "      initrd addr - The address of the initrd image to use, if any.\n"
374         "      initrd size - The size of the initrd image to use, if any.\n"
375 );