12874c5fdSThomas 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
prep_kernel(void)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
prep_initrd(struct addr_range vmlinux,void * chosen,unsigned long initrd_addr,unsigned long initrd_size)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. */
107*df4232d9SNathan Chancellor 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
149528229d2SBenjamin Herrenschmidt #ifdef __powerpc64__
prep_esm_blob(struct addr_range vmlinux,void * chosen)150528229d2SBenjamin Herrenschmidt static void prep_esm_blob(struct addr_range vmlinux, void *chosen)
151528229d2SBenjamin Herrenschmidt {
152528229d2SBenjamin Herrenschmidt unsigned long esm_blob_addr, esm_blob_size;
153528229d2SBenjamin Herrenschmidt
154528229d2SBenjamin Herrenschmidt /* Do we have an ESM (Enter Secure Mode) blob? */
155*df4232d9SNathan Chancellor if (&_esm_blob_end <= &_esm_blob_start)
156528229d2SBenjamin Herrenschmidt return;
157528229d2SBenjamin Herrenschmidt
158528229d2SBenjamin Herrenschmidt printf("Attached ESM blob at 0x%p-0x%p\n\r",
159528229d2SBenjamin Herrenschmidt _esm_blob_start, _esm_blob_end);
160528229d2SBenjamin Herrenschmidt esm_blob_addr = (unsigned long)_esm_blob_start;
161528229d2SBenjamin Herrenschmidt esm_blob_size = _esm_blob_end - _esm_blob_start;
162528229d2SBenjamin Herrenschmidt
163528229d2SBenjamin Herrenschmidt /*
164528229d2SBenjamin Herrenschmidt * If the ESM blob is too low it will be clobbered when the
165528229d2SBenjamin Herrenschmidt * kernel relocates to its final location. In this case,
166528229d2SBenjamin Herrenschmidt * allocate a safer place and move it.
167528229d2SBenjamin Herrenschmidt */
168528229d2SBenjamin Herrenschmidt if (esm_blob_addr < vmlinux.size) {
169528229d2SBenjamin Herrenschmidt void *old_addr = (void *)esm_blob_addr;
170528229d2SBenjamin Herrenschmidt
171528229d2SBenjamin Herrenschmidt printf("Allocating 0x%lx bytes for esm_blob ...\n\r",
172528229d2SBenjamin Herrenschmidt esm_blob_size);
173528229d2SBenjamin Herrenschmidt esm_blob_addr = (unsigned long)malloc(esm_blob_size);
174528229d2SBenjamin Herrenschmidt if (!esm_blob_addr)
175528229d2SBenjamin Herrenschmidt fatal("Can't allocate memory for ESM blob !\n\r");
176528229d2SBenjamin Herrenschmidt printf("Relocating ESM blob 0x%lx <- 0x%p (0x%lx bytes)\n\r",
177528229d2SBenjamin Herrenschmidt esm_blob_addr, old_addr, esm_blob_size);
178528229d2SBenjamin Herrenschmidt memmove((void *)esm_blob_addr, old_addr, esm_blob_size);
179528229d2SBenjamin Herrenschmidt }
180528229d2SBenjamin Herrenschmidt
181528229d2SBenjamin Herrenschmidt /* Tell the kernel ESM blob address via device tree. */
182528229d2SBenjamin Herrenschmidt setprop_val(chosen, "linux,esm-blob-start", (u32)(esm_blob_addr));
183528229d2SBenjamin Herrenschmidt setprop_val(chosen, "linux,esm-blob-end", (u32)(esm_blob_addr + esm_blob_size));
184528229d2SBenjamin Herrenschmidt }
185528229d2SBenjamin Herrenschmidt #else
prep_esm_blob(struct addr_range vmlinux,void * chosen)186528229d2SBenjamin Herrenschmidt static inline void prep_esm_blob(struct addr_range vmlinux, void *chosen) { }
187528229d2SBenjamin Herrenschmidt #endif
188528229d2SBenjamin Herrenschmidt
189b2c5f619SMark A. Greer /* A buffer that may be edited by tools operating on a zImage binary so as to
190b2c5f619SMark A. Greer * edit the command line passed to vmlinux (by setting /chosen/bootargs).
191b2c5f619SMark A. Greer * The buffer is put in it's own section so that tools may locate it easier.
192b2c5f619SMark A. Greer */
193a2dd5da7SAnton Blanchard static char cmdline[BOOT_COMMAND_LINE_SIZE]
194b2c5f619SMark A. Greer __attribute__((__section__("__builtin_cmdline")));
195b2c5f619SMark A. Greer
prep_cmdline(void * chosen)1963af82a8bSDavid Gibson static void prep_cmdline(void *chosen)
197b2c5f619SMark A. Greer {
1984b4b13d5SSimon Kagstrom unsigned int getline_timeout = 5000;
1994b4b13d5SSimon Kagstrom int v;
2004b4b13d5SSimon Kagstrom int n;
2014b4b13d5SSimon Kagstrom
2024b4b13d5SSimon Kagstrom /* Wait-for-input time */
2034b4b13d5SSimon Kagstrom n = getprop(chosen, "linux,cmdline-timeout", &v, sizeof(v));
2044b4b13d5SSimon Kagstrom if (n == sizeof(v))
2054b4b13d5SSimon Kagstrom getline_timeout = v;
2064b4b13d5SSimon Kagstrom
2073af82a8bSDavid Gibson if (cmdline[0] == '\0')
208a2dd5da7SAnton Blanchard getprop(chosen, "bootargs", cmdline, BOOT_COMMAND_LINE_SIZE-1);
209b2c5f619SMark A. Greer
2103af82a8bSDavid Gibson printf("\n\rLinux/PowerPC load: %s", cmdline);
2114b4b13d5SSimon Kagstrom
2123af82a8bSDavid Gibson /* If possible, edit the command line */
2134b4b13d5SSimon Kagstrom if (console_ops.edit_cmdline && getline_timeout)
2144b4b13d5SSimon Kagstrom console_ops.edit_cmdline(cmdline, BOOT_COMMAND_LINE_SIZE, getline_timeout);
2154b4b13d5SSimon Kagstrom
2163af82a8bSDavid Gibson printf("\n\r");
217b2c5f619SMark A. Greer
2183af82a8bSDavid Gibson /* Put the command line back into the devtree for the kernel */
2193af82a8bSDavid Gibson setprop_str(chosen, "bootargs", cmdline);
220b2c5f619SMark A. Greer }
221b2c5f619SMark A. Greer
222b2c5f619SMark A. Greer struct platform_ops platform_ops;
223b2c5f619SMark A. Greer struct dt_ops dt_ops;
224b2c5f619SMark A. Greer struct console_ops console_ops;
225cd197ffcSDavid Gibson struct loader_info loader_info;
226b2c5f619SMark A. Greer
start(void)227e5a2072bSDavid Gibson void start(void)
228b2c5f619SMark A. Greer {
22979c85419SDavid Gibson struct addr_range vmlinux, initrd;
230b2c5f619SMark A. Greer kernel_entry_t kentry;
23135af89ebSDavid Gibson unsigned long ft_addr = 0;
23227fbaa97SDavid Gibson void *chosen;
233b2c5f619SMark A. Greer
2343af82a8bSDavid Gibson /* Do this first, because malloc() could clobber the loader's
2353af82a8bSDavid Gibson * command line. Only use the loader command line if a
2363af82a8bSDavid Gibson * built-in command line wasn't set by an external tool */
2373af82a8bSDavid Gibson if ((loader_info.cmdline_len > 0) && (cmdline[0] == '\0'))
2383af82a8bSDavid Gibson memmove(cmdline, loader_info.cmdline,
239a2dd5da7SAnton Blanchard min(loader_info.cmdline_len, BOOT_COMMAND_LINE_SIZE-1));
2403af82a8bSDavid Gibson
241b2c5f619SMark A. Greer if (console_ops.open && (console_ops.open() < 0))
242b2c5f619SMark A. Greer exit();
243b2c5f619SMark A. Greer if (platform_ops.fixups)
244b2c5f619SMark A. Greer platform_ops.fixups();
245b2c5f619SMark A. Greer
246b2c5f619SMark A. Greer printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r",
247e5a2072bSDavid Gibson _start, get_sp());
248b2c5f619SMark A. Greer
24927fbaa97SDavid Gibson /* Ensure that the device tree has a /chosen node */
25027fbaa97SDavid Gibson chosen = finddevice("/chosen");
25127fbaa97SDavid Gibson if (!chosen)
25227fbaa97SDavid Gibson chosen = create_node(NULL, "chosen");
25327fbaa97SDavid Gibson
25479c85419SDavid Gibson vmlinux = prep_kernel();
25527fbaa97SDavid Gibson initrd = prep_initrd(vmlinux, chosen,
25627fbaa97SDavid Gibson loader_info.initrd_addr, loader_info.initrd_size);
257528229d2SBenjamin Herrenschmidt prep_esm_blob(vmlinux, chosen);
2583af82a8bSDavid Gibson prep_cmdline(chosen);
259b2c5f619SMark A. Greer
26035af89ebSDavid Gibson printf("Finalizing device tree...");
26135af89ebSDavid Gibson if (dt_ops.finalize)
26235af89ebSDavid Gibson ft_addr = dt_ops.finalize();
26335af89ebSDavid Gibson if (ft_addr)
26435af89ebSDavid Gibson printf(" flat tree at 0x%lx\n\r", ft_addr);
26535af89ebSDavid Gibson else
266cd197ffcSDavid Gibson printf(" using OF tree (promptr=%p)\n\r", loader_info.promptr);
26735af89ebSDavid Gibson
268b2c5f619SMark A. Greer if (console_ops.close)
269b2c5f619SMark A. Greer console_ops.close();
270b2c5f619SMark A. Greer
271b2c5f619SMark A. Greer kentry = (kernel_entry_t) vmlinux.addr;
272a1ff5741SOliver O'Halloran if (ft_addr) {
273a1ff5741SOliver O'Halloran if(platform_ops.kentry)
274a1ff5741SOliver O'Halloran platform_ops.kentry(ft_addr, vmlinux.addr);
275a1ff5741SOliver O'Halloran else
27635af89ebSDavid Gibson kentry(ft_addr, 0, NULL);
277a1ff5741SOliver O'Halloran }
278b2c5f619SMark A. Greer else
279cd197ffcSDavid Gibson kentry((unsigned long)initrd.addr, initrd.size,
280cd197ffcSDavid Gibson loader_info.promptr);
281b2c5f619SMark A. Greer
2826a923216SMilton Miller /* console closed so printf in fatal below may not work */
2836a923216SMilton Miller fatal("Error: Linux kernel returned to zImage boot wrapper!\n\r");
284b2c5f619SMark A. Greer }
285