2 * Copyright (c) 2011 The Chromium OS Authors.
4 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
6 * SPDX-License-Identifier: GPL-2.0+
10 * Linux x86 zImage and bzImage loading
12 * based on the procdure described in
13 * linux/Documentation/i386/boot.txt
18 #include <asm/ptrace.h>
19 #include <asm/zimage.h>
20 #include <asm/byteorder.h>
21 #include <asm/bootparam.h>
22 #ifdef CONFIG_SYS_COREBOOT
23 #include <asm/arch/timestamp.h>
25 #include <linux/compiler.h>
30 * relative to setup_base (which is 0x90000 currently)
32 * 0x0000-0x7FFF Real mode kernel
33 * 0x8000-0x8FFF Stack and heap
34 * 0x9000-0x90FF Kernel command line
36 #define DEFAULT_SETUP_BASE 0x90000
37 #define COMMAND_LINE_OFFSET 0x9000
38 #define HEAP_END_OFFSET 0x8e00
40 #define COMMAND_LINE_SIZE 2048
42 unsigned generic_install_e820_map(unsigned max_entries
,
43 struct e820entry
*entries
)
48 unsigned install_e820_map(unsigned max_entries
,
49 struct e820entry
*entries
)
50 __attribute__((weak
, alias("generic_install_e820_map")));
52 static void build_command_line(char *command_line
, int auto_boot
)
54 char *env_command_line
;
56 command_line
[0] = '\0';
58 env_command_line
= getenv("bootargs");
60 /* set console= argument if we use a serial console */
61 if (!strstr(env_command_line
, "console=")) {
62 if (!strcmp(getenv("stdout"), "serial")) {
64 /* We seem to use serial console */
65 sprintf(command_line
, "console=ttyS0,%s ",
71 strcat(command_line
, "auto ");
74 strcat(command_line
, env_command_line
);
76 printf("Kernel command line: \"%s\"\n", command_line
);
79 static int kernel_magic_ok(struct setup_header
*hdr
)
81 if (KERNEL_MAGIC
!= hdr
->boot_flag
) {
82 printf("Error: Invalid Boot Flag "
83 "(found 0x%04x, expected 0x%04x)\n",
84 hdr
->boot_flag
, KERNEL_MAGIC
);
87 printf("Valid Boot Flag\n");
92 static int get_boot_protocol(struct setup_header
*hdr
)
94 if (hdr
->header
== KERNEL_V2_MAGIC
) {
95 printf("Magic signature found\n");
99 printf("Magic signature not found\n");
104 struct boot_params
*load_zimage(char *image
, unsigned long kernel_size
,
107 struct boot_params
*setup_base
;
112 struct boot_params
*params
= (struct boot_params
*)image
;
113 struct setup_header
*hdr
= ¶ms
->hdr
;
115 /* base address for real-mode segment */
116 setup_base
= (struct boot_params
*)DEFAULT_SETUP_BASE
;
118 if (!kernel_magic_ok(hdr
))
121 /* determine size of setup */
122 if (0 == hdr
->setup_sects
) {
123 printf("Setup Sectors = 0 (defaulting to 4)\n");
124 setup_size
= 5 * 512;
126 setup_size
= (hdr
->setup_sects
+ 1) * 512;
129 printf("Setup Size = 0x%8.8lx\n", (ulong
)setup_size
);
131 if (setup_size
> SETUP_MAX_SIZE
)
132 printf("Error: Setup is too large (%d bytes)\n", setup_size
);
134 /* determine boot protocol version */
135 bootproto
= get_boot_protocol(hdr
);
137 printf("Using boot protocol version %x.%02x\n",
138 (bootproto
& 0xff00) >> 8, bootproto
& 0xff);
140 if (bootproto
>= 0x0200) {
141 if (hdr
->setup_sects
>= 15) {
142 printf("Linux kernel version %s\n",
144 hdr
->kernel_version
+ 0x200);
146 printf("Setup Sectors < 15 - "
147 "Cannot print kernel version.\n");
151 /* Determine image type */
152 big_image
= (bootproto
>= 0x0200) &&
153 (hdr
->loadflags
& BIG_KERNEL_FLAG
);
155 /* Determine load address */
157 *load_address
= (void *)BZIMAGE_LOAD_ADDR
;
159 *load_address
= (void *)ZIMAGE_LOAD_ADDR
;
161 printf("Building boot_params at 0x%8.8lx\n", (ulong
)setup_base
);
162 memset(setup_base
, 0, sizeof(*setup_base
));
163 setup_base
->hdr
= params
->hdr
;
165 if (bootproto
>= 0x0204)
166 kernel_size
= hdr
->syssize
* 16;
168 kernel_size
-= setup_size
;
170 if (bootproto
== 0x0100) {
172 * A very old kernel MUST have its real-mode code
175 if ((u32
)setup_base
!= 0x90000) {
176 /* Copy the real-mode kernel */
177 memmove((void *)0x90000, setup_base
, setup_size
);
179 /* Copy the command line */
180 memmove((void *)0x99000,
181 (u8
*)setup_base
+ COMMAND_LINE_OFFSET
,
185 setup_base
= (struct boot_params
*)0x90000;
188 /* It is recommended to clear memory up to the 32K mark */
189 memset((u8
*)0x90000 + setup_size
, 0,
190 SETUP_MAX_SIZE
- setup_size
);
194 if (kernel_size
> BZIMAGE_MAX_SIZE
) {
195 printf("Error: bzImage kernel too big! "
196 "(size: %ld, max: %d)\n",
197 kernel_size
, BZIMAGE_MAX_SIZE
);
200 } else if ((kernel_size
) > ZIMAGE_MAX_SIZE
) {
201 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
202 kernel_size
, ZIMAGE_MAX_SIZE
);
206 printf("Loading %s at address %p (%ld bytes)\n",
207 big_image
? "bzImage" : "zImage", *load_address
, kernel_size
);
209 memmove(*load_address
, image
+ setup_size
, kernel_size
);
214 int setup_zimage(struct boot_params
*setup_base
, char *cmd_line
, int auto_boot
,
215 unsigned long initrd_addr
, unsigned long initrd_size
)
217 struct setup_header
*hdr
= &setup_base
->hdr
;
218 int bootproto
= get_boot_protocol(hdr
);
220 setup_base
->e820_entries
= install_e820_map(
221 ARRAY_SIZE(setup_base
->e820_map
), setup_base
->e820_map
);
223 if (bootproto
== 0x0100) {
224 setup_base
->screen_info
.cl_magic
= COMMAND_LINE_MAGIC
;
225 setup_base
->screen_info
.cl_offset
= COMMAND_LINE_OFFSET
;
227 if (bootproto
>= 0x0200) {
228 hdr
->type_of_loader
= 8;
231 printf("Initial RAM disk at linear address "
232 "0x%08lx, size %ld bytes\n",
233 initrd_addr
, initrd_size
);
235 hdr
->ramdisk_image
= initrd_addr
;
236 hdr
->ramdisk_size
= initrd_size
;
240 if (bootproto
>= 0x0201) {
241 hdr
->heap_end_ptr
= HEAP_END_OFFSET
;
242 hdr
->loadflags
|= HEAP_FLAG
;
245 if (bootproto
>= 0x0202) {
246 hdr
->cmd_line_ptr
= (uintptr_t)cmd_line
;
247 } else if (bootproto
>= 0x0200) {
248 setup_base
->screen_info
.cl_magic
= COMMAND_LINE_MAGIC
;
249 setup_base
->screen_info
.cl_offset
=
250 (uintptr_t)cmd_line
- (uintptr_t)setup_base
;
252 hdr
->setup_move_size
= 0x9100;
255 /* build command line at COMMAND_LINE_OFFSET */
256 build_command_line(cmd_line
, auto_boot
);
261 * Implement a weak default function for boards that optionally
262 * need to clean up the system before jumping to the kernel.
264 __weak
void board_final_cleanup(void)
268 void boot_zimage(void *setup_base
, void *load_address
)
270 debug("## Transferring control to Linux (at address %08x) ...\n",
273 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF
, "start_kernel");
274 #ifdef CONFIG_BOOTSTAGE_REPORT
277 board_final_cleanup();
279 printf("\nStarting kernel ...\n\n");
281 #ifdef CONFIG_SYS_COREBOOT
282 timestamp_add_now(TS_U_BOOT_START_KERNEL
);
285 * Set %ebx, %ebp, and %edi to 0, %esi to point to the boot_params
286 * structure, and then jump to the kernel. We assume that %cs is
287 * 0x10, 4GB flat, and read/execute, and the data segments are 0x18,
288 * 4GB flat, and read/write. U-boot is setting them up that way for
289 * itself in arch/i386/cpu/cpu.c.
291 __asm__
__volatile__ (
294 "jmp *%[kernel_entry]\n"
295 :: [kernel_entry
]"a"(load_address
),
296 [boot_params
] "S"(setup_base
),
302 void setup_pcat_compatibility(void)
303 __attribute__((weak
, alias("__setup_pcat_compatibility")));
305 void __setup_pcat_compatibility(void)
309 int do_zboot(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *const argv
[])
311 struct boot_params
*base_ptr
;
312 void *bzImage_addr
= NULL
;
315 ulong bzImage_size
= 0;
316 ulong initrd_addr
= 0;
317 ulong initrd_size
= 0;
319 disable_interrupts();
321 /* Setup board for maximum PC/AT Compatibility */
322 setup_pcat_compatibility();
325 /* argv[1] holds the address of the bzImage */
328 s
= getenv("fileaddr");
332 bzImage_addr
= (void *)simple_strtoul(s
, NULL
, 16);
335 /* argv[2] holds the size of the bzImage */
336 bzImage_size
= simple_strtoul(argv
[2], NULL
, 16);
340 initrd_addr
= simple_strtoul(argv
[3], NULL
, 16);
342 initrd_size
= simple_strtoul(argv
[4], NULL
, 16);
345 base_ptr
= load_zimage(bzImage_addr
, bzImage_size
, &load_address
);
348 printf("## Kernel loading failed ...\n");
351 if (setup_zimage(base_ptr
, (char *)base_ptr
+ COMMAND_LINE_OFFSET
,
352 0, initrd_addr
, initrd_size
)) {
353 printf("Setting up boot parameters failed ...\n");
357 /* we assume that the kernel is in place */
358 boot_zimage(base_ptr
, load_address
);
359 /* does not return */
365 zboot
, 5, 0, do_zboot
,
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"
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"