Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C) Paul Mackerras 1997.
0004  *
0005  * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner.
0006  */
0007 #include <stdarg.h>
0008 #include <stddef.h>
0009 #include "elf.h"
0010 #include "page.h"
0011 #include "string.h"
0012 #include "stdio.h"
0013 #include "ops.h"
0014 #include "reg.h"
0015 
0016 struct addr_range {
0017     void *addr;
0018     unsigned long size;
0019 };
0020 
0021 #undef DEBUG
0022 
0023 static struct addr_range prep_kernel(void)
0024 {
0025     char elfheader[256];
0026     unsigned char *vmlinuz_addr = (unsigned char *)_vmlinux_start;
0027     unsigned long vmlinuz_size = _vmlinux_end - _vmlinux_start;
0028     void *addr = 0;
0029     struct elf_info ei;
0030     long len;
0031     int uncompressed_image = 0;
0032 
0033     len = partial_decompress(vmlinuz_addr, vmlinuz_size,
0034         elfheader, sizeof(elfheader), 0);
0035     /* assume uncompressed data if -1 is returned */
0036     if (len == -1) {
0037         uncompressed_image = 1;
0038         memcpy(elfheader, vmlinuz_addr, sizeof(elfheader));
0039         printf("No valid compressed data found, assume uncompressed data\n\r");
0040     }
0041 
0042     if (!parse_elf64(elfheader, &ei) && !parse_elf32(elfheader, &ei))
0043         fatal("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
0044 
0045     if (platform_ops.image_hdr)
0046         platform_ops.image_hdr(elfheader);
0047 
0048     /* We need to alloc the memsize: gzip will expand the kernel
0049      * text/data, then possible rubbish we don't care about. But
0050      * the kernel bss must be claimed (it will be zero'd by the
0051      * kernel itself)
0052      */
0053     printf("Allocating 0x%lx bytes for kernel...\n\r", ei.memsize);
0054 
0055     if (platform_ops.vmlinux_alloc) {
0056         addr = platform_ops.vmlinux_alloc(ei.memsize);
0057     } else {
0058         /*
0059          * Check if the kernel image (without bss) would overwrite the
0060          * bootwrapper. The device tree has been moved in fdt_init()
0061          * to an area allocated with malloc() (somewhere past _end).
0062          */
0063         if ((unsigned long)_start < ei.loadsize)
0064             fatal("Insufficient memory for kernel at address 0!"
0065                    " (_start=%p, uncompressed size=%08lx)\n\r",
0066                    _start, ei.loadsize);
0067 
0068         if ((unsigned long)_end < ei.memsize)
0069             fatal("The final kernel image would overwrite the "
0070                     "device tree\n\r");
0071     }
0072 
0073     if (uncompressed_image) {
0074         memcpy(addr, vmlinuz_addr + ei.elfoffset, ei.loadsize);
0075         printf("0x%lx bytes of uncompressed data copied\n\r",
0076                ei.loadsize);
0077         goto out;
0078     }
0079 
0080     /* Finally, decompress the kernel */
0081     printf("Decompressing (0x%p <- 0x%p:0x%p)...\n\r", addr,
0082            vmlinuz_addr, vmlinuz_addr+vmlinuz_size);
0083 
0084     len = partial_decompress(vmlinuz_addr, vmlinuz_size,
0085         addr, ei.loadsize, ei.elfoffset);
0086 
0087     if (len < 0)
0088         fatal("Decompression failed with error code %ld\n\r", len);
0089 
0090     if (len != ei.loadsize)
0091          fatal("Decompression error: got 0x%lx bytes, expected 0x%lx.\n\r",
0092              len, ei.loadsize);
0093 
0094     printf("Done! Decompressed 0x%lx bytes\n\r", len);
0095 out:
0096     flush_cache(addr, ei.loadsize);
0097 
0098     return (struct addr_range){addr, ei.memsize};
0099 }
0100 
0101 static struct addr_range prep_initrd(struct addr_range vmlinux, void *chosen,
0102                      unsigned long initrd_addr,
0103                      unsigned long initrd_size)
0104 {
0105     /* If we have an image attached to us, it overrides anything
0106      * supplied by the loader. */
0107     if (&_initrd_end > &_initrd_start) {
0108         printf("Attached initrd image at 0x%p-0x%p\n\r",
0109                _initrd_start, _initrd_end);
0110         initrd_addr = (unsigned long)_initrd_start;
0111         initrd_size = _initrd_end - _initrd_start;
0112     } else if (initrd_size > 0) {
0113         printf("Using loader supplied ramdisk at 0x%lx-0x%lx\n\r",
0114                initrd_addr, initrd_addr + initrd_size);
0115     }
0116 
0117     /* If there's no initrd at all, we're done */
0118     if (! initrd_size)
0119         return (struct addr_range){0, 0};
0120 
0121     /*
0122      * If the initrd is too low it will be clobbered when the
0123      * kernel relocates to its final location.  In this case,
0124      * allocate a safer place and move it.
0125      */
0126     if (initrd_addr < vmlinux.size) {
0127         void *old_addr = (void *)initrd_addr;
0128 
0129         printf("Allocating 0x%lx bytes for initrd ...\n\r",
0130                initrd_size);
0131         initrd_addr = (unsigned long)malloc(initrd_size);
0132         if (! initrd_addr)
0133             fatal("Can't allocate memory for initial "
0134                    "ramdisk !\n\r");
0135         printf("Relocating initrd 0x%lx <- 0x%p (0x%lx bytes)\n\r",
0136                initrd_addr, old_addr, initrd_size);
0137         memmove((void *)initrd_addr, old_addr, initrd_size);
0138     }
0139 
0140     printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd_addr));
0141 
0142     /* Tell the kernel initrd address via device tree */
0143     setprop_val(chosen, "linux,initrd-start", (u32)(initrd_addr));
0144     setprop_val(chosen, "linux,initrd-end", (u32)(initrd_addr+initrd_size));
0145 
0146     return (struct addr_range){(void *)initrd_addr, initrd_size};
0147 }
0148 
0149 #ifdef __powerpc64__
0150 static void prep_esm_blob(struct addr_range vmlinux, void *chosen)
0151 {
0152     unsigned long esm_blob_addr, esm_blob_size;
0153 
0154     /* Do we have an ESM (Enter Secure Mode) blob? */
0155     if (&_esm_blob_end <= &_esm_blob_start)
0156         return;
0157 
0158     printf("Attached ESM blob at 0x%p-0x%p\n\r",
0159            _esm_blob_start, _esm_blob_end);
0160     esm_blob_addr = (unsigned long)_esm_blob_start;
0161     esm_blob_size = _esm_blob_end - _esm_blob_start;
0162 
0163     /*
0164      * If the ESM blob is too low it will be clobbered when the
0165      * kernel relocates to its final location.  In this case,
0166      * allocate a safer place and move it.
0167      */
0168     if (esm_blob_addr < vmlinux.size) {
0169         void *old_addr = (void *)esm_blob_addr;
0170 
0171         printf("Allocating 0x%lx bytes for esm_blob ...\n\r",
0172                esm_blob_size);
0173         esm_blob_addr = (unsigned long)malloc(esm_blob_size);
0174         if (!esm_blob_addr)
0175             fatal("Can't allocate memory for ESM blob !\n\r");
0176         printf("Relocating ESM blob 0x%lx <- 0x%p (0x%lx bytes)\n\r",
0177                esm_blob_addr, old_addr, esm_blob_size);
0178         memmove((void *)esm_blob_addr, old_addr, esm_blob_size);
0179     }
0180 
0181     /* Tell the kernel ESM blob address via device tree. */
0182     setprop_val(chosen, "linux,esm-blob-start", (u32)(esm_blob_addr));
0183     setprop_val(chosen, "linux,esm-blob-end", (u32)(esm_blob_addr + esm_blob_size));
0184 }
0185 #else
0186 static inline void prep_esm_blob(struct addr_range vmlinux, void *chosen) { }
0187 #endif
0188 
0189 /* A buffer that may be edited by tools operating on a zImage binary so as to
0190  * edit the command line passed to vmlinux (by setting /chosen/bootargs).
0191  * The buffer is put in it's own section so that tools may locate it easier.
0192  */
0193 static char cmdline[BOOT_COMMAND_LINE_SIZE]
0194     __attribute__((__section__("__builtin_cmdline")));
0195 
0196 static void prep_cmdline(void *chosen)
0197 {
0198     unsigned int getline_timeout = 5000;
0199     int v;
0200     int n;
0201 
0202     /* Wait-for-input time */
0203     n = getprop(chosen, "linux,cmdline-timeout", &v, sizeof(v));
0204     if (n == sizeof(v))
0205         getline_timeout = v;
0206 
0207     if (cmdline[0] == '\0')
0208         getprop(chosen, "bootargs", cmdline, BOOT_COMMAND_LINE_SIZE-1);
0209 
0210     printf("\n\rLinux/PowerPC load: %s", cmdline);
0211 
0212     /* If possible, edit the command line */
0213     if (console_ops.edit_cmdline && getline_timeout)
0214         console_ops.edit_cmdline(cmdline, BOOT_COMMAND_LINE_SIZE, getline_timeout);
0215 
0216     printf("\n\r");
0217 
0218     /* Put the command line back into the devtree for the kernel */
0219     setprop_str(chosen, "bootargs", cmdline);
0220 }
0221 
0222 struct platform_ops platform_ops;
0223 struct dt_ops dt_ops;
0224 struct console_ops console_ops;
0225 struct loader_info loader_info;
0226 
0227 void start(void)
0228 {
0229     struct addr_range vmlinux, initrd;
0230     kernel_entry_t kentry;
0231     unsigned long ft_addr = 0;
0232     void *chosen;
0233 
0234     /* Do this first, because malloc() could clobber the loader's
0235      * command line.  Only use the loader command line if a
0236      * built-in command line wasn't set by an external tool */
0237     if ((loader_info.cmdline_len > 0) && (cmdline[0] == '\0'))
0238         memmove(cmdline, loader_info.cmdline,
0239             min(loader_info.cmdline_len, BOOT_COMMAND_LINE_SIZE-1));
0240 
0241     if (console_ops.open && (console_ops.open() < 0))
0242         exit();
0243     if (platform_ops.fixups)
0244         platform_ops.fixups();
0245 
0246     printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r",
0247            _start, get_sp());
0248 
0249     /* Ensure that the device tree has a /chosen node */
0250     chosen = finddevice("/chosen");
0251     if (!chosen)
0252         chosen = create_node(NULL, "chosen");
0253 
0254     vmlinux = prep_kernel();
0255     initrd = prep_initrd(vmlinux, chosen,
0256                  loader_info.initrd_addr, loader_info.initrd_size);
0257     prep_esm_blob(vmlinux, chosen);
0258     prep_cmdline(chosen);
0259 
0260     printf("Finalizing device tree...");
0261     if (dt_ops.finalize)
0262         ft_addr = dt_ops.finalize();
0263     if (ft_addr)
0264         printf(" flat tree at 0x%lx\n\r", ft_addr);
0265     else
0266         printf(" using OF tree (promptr=%p)\n\r", loader_info.promptr);
0267 
0268     if (console_ops.close)
0269         console_ops.close();
0270 
0271     kentry = (kernel_entry_t) vmlinux.addr;
0272     if (ft_addr) {
0273         if(platform_ops.kentry)
0274             platform_ops.kentry(ft_addr, vmlinux.addr);
0275         else
0276             kentry(ft_addr, 0, NULL);
0277     }
0278     else
0279         kentry((unsigned long)initrd.addr, initrd.size,
0280                loader_info.promptr);
0281 
0282     /* console closed so printf in fatal below may not work */
0283     fatal("Error: Linux kernel returned to zImage boot wrapper!\n\r");
0284 }