194b212c2SPaul Mackerras /* 294b212c2SPaul Mackerras * Copyright (C) Paul Mackerras 1997. 394b212c2SPaul Mackerras * 494b212c2SPaul Mackerras * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner. 594b212c2SPaul Mackerras * 694b212c2SPaul Mackerras * This program is free software; you can redistribute it and/or 794b212c2SPaul Mackerras * modify it under the terms of the GNU General Public License 894b212c2SPaul Mackerras * as published by the Free Software Foundation; either version 994b212c2SPaul Mackerras * 2 of the License, or (at your option) any later version. 1094b212c2SPaul Mackerras */ 1194b212c2SPaul Mackerras #include <stdarg.h> 1294b212c2SPaul Mackerras #include <stddef.h> 1394b212c2SPaul Mackerras #include "elf.h" 1494b212c2SPaul Mackerras #include "page.h" 1594b212c2SPaul Mackerras #include "string.h" 1694b212c2SPaul Mackerras #include "stdio.h" 17b2c5f619SMark A. Greer #include "ops.h" 18ad9d2716SDavid Gibson #include "gunzip_util.h" 19b2c5f619SMark A. Greer #include "flatdevtree.h" 20*e5a2072bSDavid Gibson #include "reg.h" 2194b212c2SPaul Mackerras 2294b212c2SPaul Mackerras extern char _start[]; 2394b212c2SPaul Mackerras extern char __bss_start[]; 2494b212c2SPaul Mackerras extern char _end[]; 2594b212c2SPaul Mackerras extern char _vmlinux_start[]; 2694b212c2SPaul Mackerras extern char _vmlinux_end[]; 2794b212c2SPaul Mackerras extern char _initrd_start[]; 2894b212c2SPaul Mackerras extern char _initrd_end[]; 29c888554bSMark A. Greer extern char _dtb_start[]; 30c888554bSMark A. Greer extern char _dtb_end[]; 3194b212c2SPaul Mackerras 32ad9d2716SDavid Gibson static struct gunzip_state gzstate; 33ad9d2716SDavid Gibson 3494b212c2SPaul Mackerras struct addr_range { 3579c85419SDavid Gibson void *addr; 3694b212c2SPaul Mackerras unsigned long size; 3794b212c2SPaul Mackerras }; 3894b212c2SPaul Mackerras 3979c85419SDavid Gibson struct elf_info { 4079c85419SDavid Gibson unsigned long loadsize; 4179c85419SDavid Gibson unsigned long memsize; 4279c85419SDavid Gibson unsigned long elfoffset; 4379c85419SDavid Gibson }; 4494b212c2SPaul Mackerras 45b2c5f619SMark A. Greer typedef void (*kernel_entry_t)(unsigned long, unsigned long, void *); 4694b212c2SPaul Mackerras 4794b212c2SPaul Mackerras #undef DEBUG 4894b212c2SPaul Mackerras 4979c85419SDavid Gibson static int parse_elf64(void *hdr, struct elf_info *info) 5094b212c2SPaul Mackerras { 5194b212c2SPaul Mackerras Elf64_Ehdr *elf64 = hdr; 5294b212c2SPaul Mackerras Elf64_Phdr *elf64ph; 5394b212c2SPaul Mackerras unsigned int i; 5494b212c2SPaul Mackerras 5594b212c2SPaul Mackerras if (!(elf64->e_ident[EI_MAG0] == ELFMAG0 && 5694b212c2SPaul Mackerras elf64->e_ident[EI_MAG1] == ELFMAG1 && 5794b212c2SPaul Mackerras elf64->e_ident[EI_MAG2] == ELFMAG2 && 5894b212c2SPaul Mackerras elf64->e_ident[EI_MAG3] == ELFMAG3 && 5994b212c2SPaul Mackerras elf64->e_ident[EI_CLASS] == ELFCLASS64 && 6094b212c2SPaul Mackerras elf64->e_ident[EI_DATA] == ELFDATA2MSB && 6194b212c2SPaul Mackerras elf64->e_type == ET_EXEC && 6294b212c2SPaul Mackerras elf64->e_machine == EM_PPC64)) 6394b212c2SPaul Mackerras return 0; 6494b212c2SPaul Mackerras 6594b212c2SPaul Mackerras elf64ph = (Elf64_Phdr *)((unsigned long)elf64 + 6694b212c2SPaul Mackerras (unsigned long)elf64->e_phoff); 6794b212c2SPaul Mackerras for (i = 0; i < (unsigned int)elf64->e_phnum; i++, elf64ph++) 68158daa4cSOlaf Hering if (elf64ph->p_type == PT_LOAD) 6994b212c2SPaul Mackerras break; 7094b212c2SPaul Mackerras if (i >= (unsigned int)elf64->e_phnum) 7194b212c2SPaul Mackerras return 0; 7294b212c2SPaul Mackerras 7379c85419SDavid Gibson info->loadsize = (unsigned long)elf64ph->p_filesz; 7479c85419SDavid Gibson info->memsize = (unsigned long)elf64ph->p_memsz; 7579c85419SDavid Gibson info->elfoffset = (unsigned long)elf64ph->p_offset; 7666a45dd3SPaul Mackerras 7794b212c2SPaul Mackerras return 1; 7894b212c2SPaul Mackerras } 7994b212c2SPaul Mackerras 8079c85419SDavid Gibson static int parse_elf32(void *hdr, struct elf_info *info) 8194b212c2SPaul Mackerras { 8294b212c2SPaul Mackerras Elf32_Ehdr *elf32 = hdr; 8394b212c2SPaul Mackerras Elf32_Phdr *elf32ph; 8494b212c2SPaul Mackerras unsigned int i; 8594b212c2SPaul Mackerras 8694b212c2SPaul Mackerras if (!(elf32->e_ident[EI_MAG0] == ELFMAG0 && 8794b212c2SPaul Mackerras elf32->e_ident[EI_MAG1] == ELFMAG1 && 8894b212c2SPaul Mackerras elf32->e_ident[EI_MAG2] == ELFMAG2 && 8994b212c2SPaul Mackerras elf32->e_ident[EI_MAG3] == ELFMAG3 && 9094b212c2SPaul Mackerras elf32->e_ident[EI_CLASS] == ELFCLASS32 && 9194b212c2SPaul Mackerras elf32->e_ident[EI_DATA] == ELFDATA2MSB && 9294b212c2SPaul Mackerras elf32->e_type == ET_EXEC && 9394b212c2SPaul Mackerras elf32->e_machine == EM_PPC)) 9494b212c2SPaul Mackerras return 0; 9594b212c2SPaul Mackerras 9694b212c2SPaul Mackerras elf32ph = (Elf32_Phdr *) ((unsigned long)elf32 + elf32->e_phoff); 9794b212c2SPaul Mackerras for (i = 0; i < elf32->e_phnum; i++, elf32ph++) 98158daa4cSOlaf Hering if (elf32ph->p_type == PT_LOAD) 9994b212c2SPaul Mackerras break; 10094b212c2SPaul Mackerras if (i >= elf32->e_phnum) 10194b212c2SPaul Mackerras return 0; 10294b212c2SPaul Mackerras 10379c85419SDavid Gibson info->loadsize = elf32ph->p_filesz; 10479c85419SDavid Gibson info->memsize = elf32ph->p_memsz; 10579c85419SDavid Gibson info->elfoffset = elf32ph->p_offset; 10694b212c2SPaul Mackerras return 1; 10794b212c2SPaul Mackerras } 10894b212c2SPaul Mackerras 10979c85419SDavid Gibson static struct addr_range prep_kernel(void) 11030d8caf7Smostrows@watson.ibm.com { 11179c85419SDavid Gibson char elfheader[256]; 11279c85419SDavid Gibson void *vmlinuz_addr = _vmlinux_start; 11379c85419SDavid Gibson unsigned long vmlinuz_size = _vmlinux_end - _vmlinux_start; 11479c85419SDavid Gibson void *addr = 0; 11579c85419SDavid Gibson struct elf_info ei; 11630d8caf7Smostrows@watson.ibm.com int len; 11766a45dd3SPaul Mackerras 11894b212c2SPaul Mackerras /* gunzip the ELF header of the kernel */ 11979c85419SDavid Gibson gunzip_start(&gzstate, vmlinuz_addr, vmlinuz_size); 120ad9d2716SDavid Gibson gunzip_exactly(&gzstate, elfheader, sizeof(elfheader)); 12194b212c2SPaul Mackerras 1226a923216SMilton Miller if (!parse_elf64(elfheader, &ei) && !parse_elf32(elfheader, &ei)) 1236a923216SMilton Miller fatal("Error: not a valid PPC32 or PPC64 ELF file!\n\r"); 1246a923216SMilton Miller 125b2c5f619SMark A. Greer if (platform_ops.image_hdr) 126b2c5f619SMark A. Greer platform_ops.image_hdr(elfheader); 12794b212c2SPaul Mackerras 128ad9d2716SDavid Gibson /* We need to alloc the memsize: gzip will expand the kernel 129ad9d2716SDavid Gibson * text/data, then possible rubbish we don't care about. But 130ad9d2716SDavid Gibson * the kernel bss must be claimed (it will be zero'd by the 131ad9d2716SDavid Gibson * kernel itself) 13294b212c2SPaul Mackerras */ 13379c85419SDavid Gibson printf("Allocating 0x%lx bytes for kernel ...\n\r", ei.memsize); 13479c85419SDavid Gibson 13579c85419SDavid Gibson if (platform_ops.vmlinux_alloc) { 13679c85419SDavid Gibson addr = platform_ops.vmlinux_alloc(ei.memsize); 13779c85419SDavid Gibson } else { 1386a923216SMilton Miller if ((unsigned long)_start < ei.memsize) 1396a923216SMilton Miller fatal("Insufficient memory for kernel at address 0!" 140fae59c39SDavid Gibson " (_start=%p)\n\r", _start); 14179c85419SDavid Gibson } 14279c85419SDavid Gibson 14379c85419SDavid Gibson /* Finally, gunzip the kernel */ 14479c85419SDavid Gibson printf("gunzipping (0x%p <- 0x%p:0x%p)...", addr, 14579c85419SDavid Gibson vmlinuz_addr, vmlinuz_addr+vmlinuz_size); 14679c85419SDavid Gibson /* discard up to the actual load data */ 14779c85419SDavid Gibson gunzip_discard(&gzstate, ei.elfoffset - sizeof(elfheader)); 14879c85419SDavid Gibson len = gunzip_finish(&gzstate, addr, ei.memsize); 149fae59c39SDavid Gibson printf("done 0x%x bytes\n\r", len); 15079c85419SDavid Gibson 15179c85419SDavid Gibson flush_cache(addr, ei.loadsize); 15279c85419SDavid Gibson 15379c85419SDavid Gibson return (struct addr_range){addr, ei.memsize}; 15479c85419SDavid Gibson } 15579c85419SDavid Gibson 15679c85419SDavid Gibson static struct addr_range prep_initrd(struct addr_range vmlinux, 15779c85419SDavid Gibson unsigned long initrd_addr, 15879c85419SDavid Gibson unsigned long initrd_size) 15979c85419SDavid Gibson { 16079c85419SDavid Gibson void *devp; 16179c85419SDavid Gibson u32 initrd_start, initrd_end; 16279c85419SDavid Gibson 16379c85419SDavid Gibson /* If we have an image attached to us, it overrides anything 16479c85419SDavid Gibson * supplied by the loader. */ 16579c85419SDavid Gibson if (_initrd_end > _initrd_start) { 16679c85419SDavid Gibson printf("Attached initrd image at 0x%p-0x%p\n\r", 16779c85419SDavid Gibson _initrd_start, _initrd_end); 16879c85419SDavid Gibson initrd_addr = (unsigned long)_initrd_start; 16979c85419SDavid Gibson initrd_size = _initrd_end - _initrd_start; 17079c85419SDavid Gibson } else if (initrd_size > 0) { 17179c85419SDavid Gibson printf("Using loader supplied ramdisk at 0x%lx-0x%lx\n\r", 17279c85419SDavid Gibson initrd_addr, initrd_addr + initrd_size); 17379c85419SDavid Gibson } 17479c85419SDavid Gibson 17579c85419SDavid Gibson /* If there's no initrd at all, we're done */ 17679c85419SDavid Gibson if (! initrd_size) 17779c85419SDavid Gibson return (struct addr_range){0, 0}; 17894b212c2SPaul Mackerras 17994b212c2SPaul Mackerras /* 18079c85419SDavid Gibson * If the initrd is too low it will be clobbered when the 18179c85419SDavid Gibson * kernel relocates to its final location. In this case, 18279c85419SDavid Gibson * allocate a safer place and move it. 18394b212c2SPaul Mackerras */ 18479c85419SDavid Gibson if (initrd_addr < vmlinux.size) { 18579c85419SDavid Gibson void *old_addr = (void *)initrd_addr; 18679c85419SDavid Gibson 187b2c5f619SMark A. Greer printf("Allocating 0x%lx bytes for initrd ...\n\r", 18879c85419SDavid Gibson initrd_size); 18979c85419SDavid Gibson initrd_addr = (unsigned long)malloc(initrd_size); 1906a923216SMilton Miller if (! initrd_addr) 1916a923216SMilton Miller fatal("Can't allocate memory for initial " 192b2c5f619SMark A. Greer "ramdisk !\n\r"); 193fae59c39SDavid Gibson printf("Relocating initrd 0x%lx <- 0x%p (0x%lx bytes)\n\r", 19479c85419SDavid Gibson initrd_addr, old_addr, initrd_size); 19579c85419SDavid Gibson memmove((void *)initrd_addr, old_addr, initrd_size); 19694b212c2SPaul Mackerras } 19794b212c2SPaul Mackerras 19879c85419SDavid Gibson printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd_addr)); 19994b212c2SPaul Mackerras 20079c85419SDavid Gibson /* Tell the kernel initrd address via device tree */ 20179c85419SDavid Gibson devp = finddevice("/chosen"); 2026a923216SMilton Miller if (! devp) 2036a923216SMilton Miller fatal("Device tree has no chosen node!\n\r"); 20479c85419SDavid Gibson 20579c85419SDavid Gibson initrd_start = (u32)initrd_addr; 20679c85419SDavid Gibson initrd_end = (u32)initrd_addr + initrd_size; 20779c85419SDavid Gibson 20879c85419SDavid Gibson setprop(devp, "linux,initrd-start", &initrd_start, 20979c85419SDavid Gibson sizeof(initrd_start)); 21079c85419SDavid Gibson setprop(devp, "linux,initrd-end", &initrd_end, sizeof(initrd_end)); 21179c85419SDavid Gibson 21279c85419SDavid Gibson return (struct addr_range){(void *)initrd_addr, initrd_size}; 21394b212c2SPaul Mackerras } 21494b212c2SPaul Mackerras 215b2c5f619SMark A. Greer /* A buffer that may be edited by tools operating on a zImage binary so as to 216b2c5f619SMark A. Greer * edit the command line passed to vmlinux (by setting /chosen/bootargs). 217b2c5f619SMark A. Greer * The buffer is put in it's own section so that tools may locate it easier. 218b2c5f619SMark A. Greer */ 219b2c5f619SMark A. Greer static char builtin_cmdline[COMMAND_LINE_SIZE] 220b2c5f619SMark A. Greer __attribute__((__section__("__builtin_cmdline"))); 221b2c5f619SMark A. Greer 222b2c5f619SMark A. Greer static void get_cmdline(char *buf, int size) 223b2c5f619SMark A. Greer { 224b2c5f619SMark A. Greer void *devp; 225b2c5f619SMark A. Greer int len = strlen(builtin_cmdline); 226b2c5f619SMark A. Greer 227b2c5f619SMark A. Greer buf[0] = '\0'; 228b2c5f619SMark A. Greer 229b2c5f619SMark A. Greer if (len > 0) { /* builtin_cmdline overrides dt's /chosen/bootargs */ 230b2c5f619SMark A. Greer len = min(len, size-1); 231b2c5f619SMark A. Greer strncpy(buf, builtin_cmdline, len); 232b2c5f619SMark A. Greer buf[len] = '\0'; 233b2c5f619SMark A. Greer } 234b2c5f619SMark A. Greer else if ((devp = finddevice("/chosen"))) 235b2c5f619SMark A. Greer getprop(devp, "bootargs", buf, size); 236b2c5f619SMark A. Greer } 237b2c5f619SMark A. Greer 238b2c5f619SMark A. Greer static void set_cmdline(char *buf) 239b2c5f619SMark A. Greer { 240b2c5f619SMark A. Greer void *devp; 241b2c5f619SMark A. Greer 242b2c5f619SMark A. Greer if ((devp = finddevice("/chosen"))) 243b2c5f619SMark A. Greer setprop(devp, "bootargs", buf, strlen(buf) + 1); 244b2c5f619SMark A. Greer } 245b2c5f619SMark A. Greer 246b2c5f619SMark A. Greer struct platform_ops platform_ops; 247b2c5f619SMark A. Greer struct dt_ops dt_ops; 248b2c5f619SMark A. Greer struct console_ops console_ops; 249cd197ffcSDavid Gibson struct loader_info loader_info; 250b2c5f619SMark A. Greer 251*e5a2072bSDavid Gibson void start(void) 252b2c5f619SMark A. Greer { 25379c85419SDavid Gibson struct addr_range vmlinux, initrd; 254b2c5f619SMark A. Greer kernel_entry_t kentry; 255b2c5f619SMark A. Greer char cmdline[COMMAND_LINE_SIZE]; 25635af89ebSDavid Gibson unsigned long ft_addr = 0; 257b2c5f619SMark A. Greer 258b2c5f619SMark A. Greer if (console_ops.open && (console_ops.open() < 0)) 259b2c5f619SMark A. Greer exit(); 260b2c5f619SMark A. Greer if (platform_ops.fixups) 261b2c5f619SMark A. Greer platform_ops.fixups(); 262b2c5f619SMark A. Greer 263b2c5f619SMark A. Greer printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r", 264*e5a2072bSDavid Gibson _start, get_sp()); 265b2c5f619SMark A. Greer 26679c85419SDavid Gibson vmlinux = prep_kernel(); 267cd197ffcSDavid Gibson initrd = prep_initrd(vmlinux, loader_info.initrd_addr, 268cd197ffcSDavid Gibson loader_info.initrd_size); 269b2c5f619SMark A. Greer 270b2c5f619SMark A. Greer /* If cmdline came from zimage wrapper or if we can edit the one 271b2c5f619SMark A. Greer * in the dt, print it out and edit it, if possible. 272b2c5f619SMark A. Greer */ 273b2c5f619SMark A. Greer if ((strlen(builtin_cmdline) > 0) || console_ops.edit_cmdline) { 274b2c5f619SMark A. Greer get_cmdline(cmdline, COMMAND_LINE_SIZE); 275b2c5f619SMark A. Greer printf("\n\rLinux/PowerPC load: %s", cmdline); 276b2c5f619SMark A. Greer if (console_ops.edit_cmdline) 277b2c5f619SMark A. Greer console_ops.edit_cmdline(cmdline, COMMAND_LINE_SIZE); 278b2c5f619SMark A. Greer printf("\n\r"); 279b2c5f619SMark A. Greer set_cmdline(cmdline); 280b2c5f619SMark A. Greer } 281b2c5f619SMark A. Greer 28235af89ebSDavid Gibson printf("Finalizing device tree..."); 28335af89ebSDavid Gibson if (dt_ops.finalize) 28435af89ebSDavid Gibson ft_addr = dt_ops.finalize(); 28535af89ebSDavid Gibson if (ft_addr) 28635af89ebSDavid Gibson printf(" flat tree at 0x%lx\n\r", ft_addr); 28735af89ebSDavid Gibson else 288cd197ffcSDavid Gibson printf(" using OF tree (promptr=%p)\n\r", loader_info.promptr); 28935af89ebSDavid Gibson 290b2c5f619SMark A. Greer if (console_ops.close) 291b2c5f619SMark A. Greer console_ops.close(); 292b2c5f619SMark A. Greer 293b2c5f619SMark A. Greer kentry = (kernel_entry_t) vmlinux.addr; 29435af89ebSDavid Gibson if (ft_addr) 29535af89ebSDavid Gibson kentry(ft_addr, 0, NULL); 296b2c5f619SMark A. Greer else 297cd197ffcSDavid Gibson kentry((unsigned long)initrd.addr, initrd.size, 298cd197ffcSDavid Gibson loader_info.promptr); 299b2c5f619SMark A. Greer 3006a923216SMilton Miller /* console closed so printf in fatal below may not work */ 3016a923216SMilton Miller fatal("Error: Linux kernel returned to zImage boot wrapper!\n\r"); 302b2c5f619SMark A. Greer } 303