xref: /openbmc/linux/arch/powerpc/boot/main.c (revision 2874c5fd284268364ece81a7bd936f3c8168e567)
1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
294b212c2SPaul Mackerras /*
394b212c2SPaul Mackerras  * Copyright (C) Paul Mackerras 1997.
494b212c2SPaul Mackerras  *
594b212c2SPaul Mackerras  * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner.
694b212c2SPaul Mackerras  */
794b212c2SPaul Mackerras #include <stdarg.h>
894b212c2SPaul Mackerras #include <stddef.h>
994b212c2SPaul Mackerras #include "elf.h"
1094b212c2SPaul Mackerras #include "page.h"
1194b212c2SPaul Mackerras #include "string.h"
1294b212c2SPaul Mackerras #include "stdio.h"
13b2c5f619SMark A. Greer #include "ops.h"
14e5a2072bSDavid Gibson #include "reg.h"
1594b212c2SPaul Mackerras 
1694b212c2SPaul Mackerras struct addr_range {
1779c85419SDavid Gibson 	void *addr;
1894b212c2SPaul Mackerras 	unsigned long size;
1994b212c2SPaul Mackerras };
2094b212c2SPaul Mackerras 
2194b212c2SPaul Mackerras #undef DEBUG
2294b212c2SPaul Mackerras 
2379c85419SDavid Gibson static struct addr_range prep_kernel(void)
2430d8caf7Smostrows@watson.ibm.com {
2579c85419SDavid Gibson 	char elfheader[256];
261b7898eeSOliver O'Halloran 	unsigned char *vmlinuz_addr = (unsigned char *)_vmlinux_start;
2779c85419SDavid Gibson 	unsigned long vmlinuz_size = _vmlinux_end - _vmlinux_start;
2879c85419SDavid Gibson 	void *addr = 0;
2979c85419SDavid Gibson 	struct elf_info ei;
301b7898eeSOliver O'Halloran 	long len;
3165bc3eceSHeiner Kallweit 	int uncompressed_image = 0;
3266a45dd3SPaul Mackerras 
3365bc3eceSHeiner Kallweit 	len = partial_decompress(vmlinuz_addr, vmlinuz_size,
341b7898eeSOliver O'Halloran 		elfheader, sizeof(elfheader), 0);
3565bc3eceSHeiner Kallweit 	/* assume uncompressed data if -1 is returned */
3665bc3eceSHeiner Kallweit 	if (len == -1) {
3765bc3eceSHeiner Kallweit 		uncompressed_image = 1;
3865bc3eceSHeiner Kallweit 		memcpy(elfheader, vmlinuz_addr, sizeof(elfheader));
3965bc3eceSHeiner Kallweit 		printf("No valid compressed data found, assume uncompressed data\n\r");
4065bc3eceSHeiner Kallweit 	}
4194b212c2SPaul Mackerras 
426a923216SMilton Miller 	if (!parse_elf64(elfheader, &ei) && !parse_elf32(elfheader, &ei))
436a923216SMilton Miller 		fatal("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
446a923216SMilton Miller 
45b2c5f619SMark A. Greer 	if (platform_ops.image_hdr)
46b2c5f619SMark A. Greer 		platform_ops.image_hdr(elfheader);
4794b212c2SPaul Mackerras 
48ad9d2716SDavid Gibson 	/* We need to alloc the memsize: gzip will expand the kernel
49ad9d2716SDavid Gibson 	 * text/data, then possible rubbish we don't care about. But
50ad9d2716SDavid Gibson 	 * the kernel bss must be claimed (it will be zero'd by the
51ad9d2716SDavid Gibson 	 * kernel itself)
5294b212c2SPaul Mackerras 	 */
5379c85419SDavid Gibson 	printf("Allocating 0x%lx bytes for kernel...\n\r", ei.memsize);
5479c85419SDavid Gibson 
5579c85419SDavid Gibson 	if (platform_ops.vmlinux_alloc) {
5679c85419SDavid Gibson 		addr = platform_ops.vmlinux_alloc(ei.memsize);
5779c85419SDavid Gibson 	} else {
58c10c178aSSebastian Siewior 		/*
59c10c178aSSebastian Siewior 		 * Check if the kernel image (without bss) would overwrite the
60c10c178aSSebastian Siewior 		 * bootwrapper. The device tree has been moved in fdt_init()
61c10c178aSSebastian Siewior 		 * to an area allocated with malloc() (somewhere past _end).
62c10c178aSSebastian Siewior 		 */
63c10c178aSSebastian Siewior 		if ((unsigned long)_start < ei.loadsize)
646a923216SMilton Miller 			fatal("Insufficient memory for kernel at address 0!"
658ba4773aSJon Smirl 			       " (_start=%p, uncompressed size=%08lx)\n\r",
66c10c178aSSebastian Siewior 			       _start, ei.loadsize);
67c10c178aSSebastian Siewior 
68c10c178aSSebastian Siewior 		if ((unsigned long)_end < ei.memsize)
69c10c178aSSebastian Siewior 			fatal("The final kernel image would overwrite the "
70c10c178aSSebastian Siewior 					"device tree\n\r");
7179c85419SDavid Gibson 	}
7279c85419SDavid Gibson 
7365bc3eceSHeiner Kallweit 	if (uncompressed_image) {
7465bc3eceSHeiner Kallweit 		memcpy(addr, vmlinuz_addr + ei.elfoffset, ei.loadsize);
7565bc3eceSHeiner Kallweit 		printf("0x%lx bytes of uncompressed data copied\n\r",
7665bc3eceSHeiner Kallweit 		       ei.loadsize);
7765bc3eceSHeiner Kallweit 		goto out;
7865bc3eceSHeiner Kallweit 	}
7965bc3eceSHeiner Kallweit 
801b7898eeSOliver O'Halloran 	/* Finally, decompress the kernel */
811b7898eeSOliver O'Halloran 	printf("Decompressing (0x%p <- 0x%p:0x%p)...\n\r", addr,
8279c85419SDavid Gibson 	       vmlinuz_addr, vmlinuz_addr+vmlinuz_size);
831b7898eeSOliver O'Halloran 
841b7898eeSOliver O'Halloran 	len = partial_decompress(vmlinuz_addr, vmlinuz_size,
851b7898eeSOliver O'Halloran 		addr, ei.loadsize, ei.elfoffset);
861b7898eeSOliver O'Halloran 
871b7898eeSOliver O'Halloran 	if (len < 0)
881b7898eeSOliver O'Halloran 		fatal("Decompression failed with error code %ld\n\r", len);
891b7898eeSOliver O'Halloran 
9002cc5114SMilton Miller 	if (len != ei.loadsize)
911b7898eeSOliver O'Halloran 		 fatal("Decompression error: got 0x%lx bytes, expected 0x%lx.\n\r",
9202cc5114SMilton Miller 			 len, ei.loadsize);
931b7898eeSOliver O'Halloran 
941b7898eeSOliver O'Halloran 	printf("Done! Decompressed 0x%lx bytes\n\r", len);
9565bc3eceSHeiner Kallweit out:
9679c85419SDavid Gibson 	flush_cache(addr, ei.loadsize);
9779c85419SDavid Gibson 
9879c85419SDavid Gibson 	return (struct addr_range){addr, ei.memsize};
9979c85419SDavid Gibson }
10079c85419SDavid Gibson 
10127fbaa97SDavid Gibson static struct addr_range prep_initrd(struct addr_range vmlinux, void *chosen,
10279c85419SDavid Gibson 				     unsigned long initrd_addr,
10379c85419SDavid Gibson 				     unsigned long initrd_size)
10479c85419SDavid Gibson {
10579c85419SDavid Gibson 	/* If we have an image attached to us, it overrides anything
10679c85419SDavid Gibson 	 * supplied by the loader. */
10779c85419SDavid Gibson 	if (_initrd_end > _initrd_start) {
10879c85419SDavid Gibson 		printf("Attached initrd image at 0x%p-0x%p\n\r",
10979c85419SDavid Gibson 		       _initrd_start, _initrd_end);
11079c85419SDavid Gibson 		initrd_addr = (unsigned long)_initrd_start;
11179c85419SDavid Gibson 		initrd_size = _initrd_end - _initrd_start;
11279c85419SDavid Gibson 	} else if (initrd_size > 0) {
11379c85419SDavid Gibson 		printf("Using loader supplied ramdisk at 0x%lx-0x%lx\n\r",
11479c85419SDavid Gibson 		       initrd_addr, initrd_addr + initrd_size);
11579c85419SDavid Gibson 	}
11679c85419SDavid Gibson 
11779c85419SDavid Gibson 	/* If there's no initrd at all, we're done */
11879c85419SDavid Gibson 	if (! initrd_size)
11979c85419SDavid Gibson 		return (struct addr_range){0, 0};
12094b212c2SPaul Mackerras 
12194b212c2SPaul Mackerras 	/*
12279c85419SDavid Gibson 	 * If the initrd is too low it will be clobbered when the
12379c85419SDavid Gibson 	 * kernel relocates to its final location.  In this case,
12479c85419SDavid Gibson 	 * allocate a safer place and move it.
12594b212c2SPaul Mackerras 	 */
12679c85419SDavid Gibson 	if (initrd_addr < vmlinux.size) {
12779c85419SDavid Gibson 		void *old_addr = (void *)initrd_addr;
12879c85419SDavid Gibson 
129b2c5f619SMark A. Greer 		printf("Allocating 0x%lx bytes for initrd ...\n\r",
13079c85419SDavid Gibson 		       initrd_size);
13179c85419SDavid Gibson 		initrd_addr = (unsigned long)malloc(initrd_size);
1326a923216SMilton Miller 		if (! initrd_addr)
1336a923216SMilton Miller 			fatal("Can't allocate memory for initial "
134b2c5f619SMark A. Greer 			       "ramdisk !\n\r");
135fae59c39SDavid Gibson 		printf("Relocating initrd 0x%lx <- 0x%p (0x%lx bytes)\n\r",
13679c85419SDavid Gibson 		       initrd_addr, old_addr, initrd_size);
13779c85419SDavid Gibson 		memmove((void *)initrd_addr, old_addr, initrd_size);
13894b212c2SPaul Mackerras 	}
13994b212c2SPaul Mackerras 
14079c85419SDavid Gibson 	printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd_addr));
14194b212c2SPaul Mackerras 
14279c85419SDavid Gibson 	/* Tell the kernel initrd address via device tree */
14327fbaa97SDavid Gibson 	setprop_val(chosen, "linux,initrd-start", (u32)(initrd_addr));
14427fbaa97SDavid Gibson 	setprop_val(chosen, "linux,initrd-end", (u32)(initrd_addr+initrd_size));
14579c85419SDavid Gibson 
14679c85419SDavid Gibson 	return (struct addr_range){(void *)initrd_addr, initrd_size};
14794b212c2SPaul Mackerras }
14894b212c2SPaul Mackerras 
149b2c5f619SMark A. Greer /* A buffer that may be edited by tools operating on a zImage binary so as to
150b2c5f619SMark A. Greer  * edit the command line passed to vmlinux (by setting /chosen/bootargs).
151b2c5f619SMark A. Greer  * The buffer is put in it's own section so that tools may locate it easier.
152b2c5f619SMark A. Greer  */
153a2dd5da7SAnton Blanchard static char cmdline[BOOT_COMMAND_LINE_SIZE]
154b2c5f619SMark A. Greer 	__attribute__((__section__("__builtin_cmdline")));
155b2c5f619SMark A. Greer 
1563af82a8bSDavid Gibson static void prep_cmdline(void *chosen)
157b2c5f619SMark A. Greer {
1584b4b13d5SSimon Kagstrom 	unsigned int getline_timeout = 5000;
1594b4b13d5SSimon Kagstrom 	int v;
1604b4b13d5SSimon Kagstrom 	int n;
1614b4b13d5SSimon Kagstrom 
1624b4b13d5SSimon Kagstrom 	/* Wait-for-input time */
1634b4b13d5SSimon Kagstrom 	n = getprop(chosen, "linux,cmdline-timeout", &v, sizeof(v));
1644b4b13d5SSimon Kagstrom 	if (n == sizeof(v))
1654b4b13d5SSimon Kagstrom 		getline_timeout = v;
1664b4b13d5SSimon Kagstrom 
1673af82a8bSDavid Gibson 	if (cmdline[0] == '\0')
168a2dd5da7SAnton Blanchard 		getprop(chosen, "bootargs", cmdline, BOOT_COMMAND_LINE_SIZE-1);
169b2c5f619SMark A. Greer 
1703af82a8bSDavid Gibson 	printf("\n\rLinux/PowerPC load: %s", cmdline);
1714b4b13d5SSimon Kagstrom 
1723af82a8bSDavid Gibson 	/* If possible, edit the command line */
1734b4b13d5SSimon Kagstrom 	if (console_ops.edit_cmdline && getline_timeout)
1744b4b13d5SSimon Kagstrom 		console_ops.edit_cmdline(cmdline, BOOT_COMMAND_LINE_SIZE, getline_timeout);
1754b4b13d5SSimon Kagstrom 
1763af82a8bSDavid Gibson 	printf("\n\r");
177b2c5f619SMark A. Greer 
1783af82a8bSDavid Gibson 	/* Put the command line back into the devtree for the kernel */
1793af82a8bSDavid Gibson 	setprop_str(chosen, "bootargs", cmdline);
180b2c5f619SMark A. Greer }
181b2c5f619SMark A. Greer 
182b2c5f619SMark A. Greer struct platform_ops platform_ops;
183b2c5f619SMark A. Greer struct dt_ops dt_ops;
184b2c5f619SMark A. Greer struct console_ops console_ops;
185cd197ffcSDavid Gibson struct loader_info loader_info;
186b2c5f619SMark A. Greer 
187e5a2072bSDavid Gibson void start(void)
188b2c5f619SMark A. Greer {
18979c85419SDavid Gibson 	struct addr_range vmlinux, initrd;
190b2c5f619SMark A. Greer 	kernel_entry_t kentry;
19135af89ebSDavid Gibson 	unsigned long ft_addr = 0;
19227fbaa97SDavid Gibson 	void *chosen;
193b2c5f619SMark A. Greer 
1943af82a8bSDavid Gibson 	/* Do this first, because malloc() could clobber the loader's
1953af82a8bSDavid Gibson 	 * command line.  Only use the loader command line if a
1963af82a8bSDavid Gibson 	 * built-in command line wasn't set by an external tool */
1973af82a8bSDavid Gibson 	if ((loader_info.cmdline_len > 0) && (cmdline[0] == '\0'))
1983af82a8bSDavid Gibson 		memmove(cmdline, loader_info.cmdline,
199a2dd5da7SAnton Blanchard 			min(loader_info.cmdline_len, BOOT_COMMAND_LINE_SIZE-1));
2003af82a8bSDavid Gibson 
201b2c5f619SMark A. Greer 	if (console_ops.open && (console_ops.open() < 0))
202b2c5f619SMark A. Greer 		exit();
203b2c5f619SMark A. Greer 	if (platform_ops.fixups)
204b2c5f619SMark A. Greer 		platform_ops.fixups();
205b2c5f619SMark A. Greer 
206b2c5f619SMark A. Greer 	printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r",
207e5a2072bSDavid Gibson 	       _start, get_sp());
208b2c5f619SMark A. Greer 
20927fbaa97SDavid Gibson 	/* Ensure that the device tree has a /chosen node */
21027fbaa97SDavid Gibson 	chosen = finddevice("/chosen");
21127fbaa97SDavid Gibson 	if (!chosen)
21227fbaa97SDavid Gibson 		chosen = create_node(NULL, "chosen");
21327fbaa97SDavid Gibson 
21479c85419SDavid Gibson 	vmlinux = prep_kernel();
21527fbaa97SDavid Gibson 	initrd = prep_initrd(vmlinux, chosen,
21627fbaa97SDavid Gibson 			     loader_info.initrd_addr, loader_info.initrd_size);
2173af82a8bSDavid Gibson 	prep_cmdline(chosen);
218b2c5f619SMark A. Greer 
21935af89ebSDavid Gibson 	printf("Finalizing device tree...");
22035af89ebSDavid Gibson 	if (dt_ops.finalize)
22135af89ebSDavid Gibson 		ft_addr = dt_ops.finalize();
22235af89ebSDavid Gibson 	if (ft_addr)
22335af89ebSDavid Gibson 		printf(" flat tree at 0x%lx\n\r", ft_addr);
22435af89ebSDavid Gibson 	else
225cd197ffcSDavid Gibson 		printf(" using OF tree (promptr=%p)\n\r", loader_info.promptr);
22635af89ebSDavid Gibson 
227b2c5f619SMark A. Greer 	if (console_ops.close)
228b2c5f619SMark A. Greer 		console_ops.close();
229b2c5f619SMark A. Greer 
230b2c5f619SMark A. Greer 	kentry = (kernel_entry_t) vmlinux.addr;
231a1ff5741SOliver O'Halloran 	if (ft_addr) {
232a1ff5741SOliver O'Halloran 		if(platform_ops.kentry)
233a1ff5741SOliver O'Halloran 			platform_ops.kentry(ft_addr, vmlinux.addr);
234a1ff5741SOliver O'Halloran 		else
23535af89ebSDavid Gibson 			kentry(ft_addr, 0, NULL);
236a1ff5741SOliver O'Halloran 	}
237b2c5f619SMark A. Greer 	else
238cd197ffcSDavid Gibson 		kentry((unsigned long)initrd.addr, initrd.size,
239cd197ffcSDavid Gibson 		       loader_info.promptr);
240b2c5f619SMark A. Greer 
2416a923216SMilton Miller 	/* console closed so printf in fatal below may not work */
2426a923216SMilton Miller 	fatal("Error: Linux kernel returned to zImage boot wrapper!\n\r");
243b2c5f619SMark A. Greer }
244