xref: /openbmc/u-boot/arch/x86/lib/zimage.c (revision 0b304a24)
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * (C) Copyright 2002
4  * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 /*
10  * Linux x86 zImage and bzImage loading
11  *
12  * based on the procdure described in
13  * linux/Documentation/i386/boot.txt
14  */
15 
16 #include <common.h>
17 #include <asm/io.h>
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>
24 #endif
25 #include <linux/compiler.h>
26 
27 /*
28  * Memory lay-out:
29  *
30  * relative to setup_base (which is 0x90000 currently)
31  *
32  *	0x0000-0x7FFF	Real mode kernel
33  *	0x8000-0x8FFF	Stack and heap
34  *	0x9000-0x90FF	Kernel command line
35  */
36 #define DEFAULT_SETUP_BASE	0x90000
37 #define COMMAND_LINE_OFFSET	0x9000
38 #define HEAP_END_OFFSET		0x8e00
39 
40 #define COMMAND_LINE_SIZE	2048
41 
42 unsigned generic_install_e820_map(unsigned max_entries,
43 				  struct e820entry *entries)
44 {
45 	return 0;
46 }
47 
48 unsigned install_e820_map(unsigned max_entries,
49 			  struct e820entry *entries)
50 	__attribute__((weak, alias("generic_install_e820_map")));
51 
52 static void build_command_line(char *command_line, int auto_boot)
53 {
54 	char *env_command_line;
55 
56 	command_line[0] = '\0';
57 
58 	env_command_line =  getenv("bootargs");
59 
60 	/* set console= argument if we use a serial console */
61 	if (!strstr(env_command_line, "console=")) {
62 		if (!strcmp(getenv("stdout"), "serial")) {
63 
64 			/* We seem to use serial console */
65 			sprintf(command_line, "console=ttyS0,%s ",
66 				getenv("baudrate"));
67 		}
68 	}
69 
70 	if (auto_boot)
71 		strcat(command_line, "auto ");
72 
73 	if (env_command_line)
74 		strcat(command_line, env_command_line);
75 
76 	printf("Kernel command line: \"%s\"\n", command_line);
77 }
78 
79 static int kernel_magic_ok(struct setup_header *hdr)
80 {
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);
85 		return 0;
86 	} else {
87 		printf("Valid Boot Flag\n");
88 		return 1;
89 	}
90 }
91 
92 static int get_boot_protocol(struct setup_header *hdr)
93 {
94 	if (hdr->header == KERNEL_V2_MAGIC) {
95 		printf("Magic signature found\n");
96 		return hdr->version;
97 	} else {
98 		/* Very old kernel */
99 		printf("Magic signature not found\n");
100 		return 0x0100;
101 	}
102 }
103 
104 struct boot_params *load_zimage(char *image, unsigned long kernel_size,
105 				void **load_address)
106 {
107 	struct boot_params *setup_base;
108 	int setup_size;
109 	int bootproto;
110 	int big_image;
111 
112 	struct boot_params *params = (struct boot_params *)image;
113 	struct setup_header *hdr = &params->hdr;
114 
115 	/* base address for real-mode segment */
116 	setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
117 
118 	if (!kernel_magic_ok(hdr))
119 		return 0;
120 
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;
125 	} else {
126 		setup_size = (hdr->setup_sects + 1) * 512;
127 	}
128 
129 	printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
130 
131 	if (setup_size > SETUP_MAX_SIZE)
132 		printf("Error: Setup is too large (%d bytes)\n", setup_size);
133 
134 	/* determine boot protocol version */
135 	bootproto = get_boot_protocol(hdr);
136 
137 	printf("Using boot protocol version %x.%02x\n",
138 	       (bootproto & 0xff00) >> 8, bootproto & 0xff);
139 
140 	if (bootproto >= 0x0200) {
141 		if (hdr->setup_sects >= 15) {
142 			printf("Linux kernel version %s\n",
143 				(char *)params +
144 				hdr->kernel_version + 0x200);
145 		} else {
146 			printf("Setup Sectors < 15 - "
147 				"Cannot print kernel version.\n");
148 		}
149 	}
150 
151 	/* Determine image type */
152 	big_image = (bootproto >= 0x0200) &&
153 		    (hdr->loadflags & BIG_KERNEL_FLAG);
154 
155 	/* Determine load address */
156 	if (big_image)
157 		*load_address = (void *)BZIMAGE_LOAD_ADDR;
158 	else
159 		*load_address = (void *)ZIMAGE_LOAD_ADDR;
160 
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;
164 
165 	if (bootproto >= 0x0204)
166 		kernel_size = hdr->syssize * 16;
167 	else
168 		kernel_size -= setup_size;
169 
170 	if (bootproto == 0x0100) {
171 		/*
172 		 * A very old kernel MUST have its real-mode code
173 		 * loaded at 0x90000
174 		 */
175 		if ((u32)setup_base != 0x90000) {
176 			/* Copy the real-mode kernel */
177 			memmove((void *)0x90000, setup_base, setup_size);
178 
179 			/* Copy the command line */
180 			memmove((void *)0x99000,
181 				(u8 *)setup_base + COMMAND_LINE_OFFSET,
182 				COMMAND_LINE_SIZE);
183 
184 			 /* Relocated */
185 			setup_base = (struct boot_params *)0x90000;
186 		}
187 
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);
191 	}
192 
193 	if (big_image) {
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);
198 			return 0;
199 		}
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);
203 		return 0;
204 	}
205 
206 	printf("Loading %s at address %p (%ld bytes)\n",
207 		big_image ? "bzImage" : "zImage", *load_address, kernel_size);
208 
209 	memmove(*load_address, image + setup_size, kernel_size);
210 
211 	return setup_base;
212 }
213 
214 int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
215 		 unsigned long initrd_addr, unsigned long initrd_size)
216 {
217 	struct setup_header *hdr = &setup_base->hdr;
218 	int bootproto = get_boot_protocol(hdr);
219 
220 	setup_base->e820_entries = install_e820_map(
221 		ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
222 
223 	if (bootproto == 0x0100) {
224 		setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
225 		setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
226 	}
227 	if (bootproto >= 0x0200) {
228 		hdr->type_of_loader = 8;
229 
230 		if (initrd_addr) {
231 			printf("Initial RAM disk at linear address "
232 			       "0x%08lx, size %ld bytes\n",
233 			       initrd_addr, initrd_size);
234 
235 			hdr->ramdisk_image = initrd_addr;
236 			hdr->ramdisk_size = initrd_size;
237 		}
238 	}
239 
240 	if (bootproto >= 0x0201) {
241 		hdr->heap_end_ptr = HEAP_END_OFFSET;
242 		hdr->loadflags |= HEAP_FLAG;
243 	}
244 
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;
251 
252 		hdr->setup_move_size = 0x9100;
253 	}
254 
255 	/* build command line at COMMAND_LINE_OFFSET */
256 	build_command_line(cmd_line, auto_boot);
257 	return 0;
258 }
259 
260 /*
261  * Implement a weak default function for boards that optionally
262  * need to clean up the system before jumping to the kernel.
263  */
264 __weak void board_final_cleanup(void)
265 {
266 }
267 
268 void boot_zimage(void *setup_base, void *load_address)
269 {
270 	debug("## Transferring control to Linux (at address %08x) ...\n",
271 	      (u32)setup_base);
272 
273 	bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
274 #ifdef CONFIG_BOOTSTAGE_REPORT
275 	bootstage_report();
276 #endif
277 	board_final_cleanup();
278 
279 	printf("\nStarting kernel ...\n\n");
280 
281 #ifdef CONFIG_SYS_COREBOOT
282 	timestamp_add_now(TS_U_BOOT_START_KERNEL);
283 #endif
284 	/*
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.
290 	 */
291 	__asm__ __volatile__ (
292 	"movl $0, %%ebp\n"
293 	"cli\n"
294 	"jmp *%[kernel_entry]\n"
295 	:: [kernel_entry]"a"(load_address),
296 	   [boot_params] "S"(setup_base),
297 	   "b"(0), "D"(0)
298 	:  "%ebp"
299 	);
300 }
301 
302 void setup_pcat_compatibility(void)
303 	__attribute__((weak, alias("__setup_pcat_compatibility")));
304 
305 void __setup_pcat_compatibility(void)
306 {
307 }
308 
309 int do_zboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
310 {
311 	struct boot_params *base_ptr;
312 	void *bzImage_addr = NULL;
313 	void *load_address;
314 	char *s;
315 	ulong bzImage_size = 0;
316 	ulong initrd_addr = 0;
317 	ulong initrd_size = 0;
318 
319 	disable_interrupts();
320 
321 	/* Setup board for maximum PC/AT Compatibility */
322 	setup_pcat_compatibility();
323 
324 	if (argc >= 2) {
325 		/* argv[1] holds the address of the bzImage */
326 		s = argv[1];
327 	} else {
328 		s = getenv("fileaddr");
329 	}
330 
331 	if (s)
332 		bzImage_addr = (void *)simple_strtoul(s, NULL, 16);
333 
334 	if (argc >= 3) {
335 		/* argv[2] holds the size of the bzImage */
336 		bzImage_size = simple_strtoul(argv[2], NULL, 16);
337 	}
338 
339 	if (argc >= 4)
340 		initrd_addr = simple_strtoul(argv[3], NULL, 16);
341 	if (argc >= 5)
342 		initrd_size = simple_strtoul(argv[4], NULL, 16);
343 
344 	/* Lets look for */
345 	base_ptr = load_zimage(bzImage_addr, bzImage_size, &load_address);
346 
347 	if (!base_ptr) {
348 		printf("## Kernel loading failed ...\n");
349 		return -1;
350 	}
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");
354 		return -1;
355 	}
356 
357 	/* we assume that the kernel is in place */
358 	boot_zimage(base_ptr, load_address);
359 	/* does not return */
360 
361 	return -1;
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 );
376