/*1* arch/alpha/boot/bootpz.c2*3* Copyright (C) 1997 Jay Estabrook4*5* This file is used for creating a compressed BOOTP file for the6* Linux/AXP kernel7*8* based significantly on the arch/alpha/boot/main.c of Linus Torvalds9* and the decompression code from MILO.10*/11#include <linux/kernel.h>12#include <linux/slab.h>13#include <linux/string.h>14#include <generated/utsrelease.h>15#include <linux/mm.h>1617#include <asm/system.h>18#include <asm/console.h>19#include <asm/hwrpb.h>20#include <asm/pgtable.h>21#include <asm/io.h>2223#include <stdarg.h>2425#include "kzsize.h"2627/* FIXME FIXME FIXME */28#define MALLOC_AREA_SIZE 0x200000 /* 2MB for now */29/* FIXME FIXME FIXME */303132/*33WARNING NOTE3435It is very possible that turning on additional messages may cause36kernel image corruption due to stack usage to do the printing.3738*/3940#undef DEBUG_CHECK_RANGE41#undef DEBUG_ADDRESSES42#undef DEBUG_LAST_STEPS4344extern unsigned long switch_to_osf_pal(unsigned long nr,45struct pcb_struct * pcb_va, struct pcb_struct * pcb_pa,46unsigned long *vptb);4748extern int decompress_kernel(void* destination, void *source,49size_t ksize, size_t kzsize);5051extern void move_stack(unsigned long new_stack);5253struct hwrpb_struct *hwrpb = INIT_HWRPB;54static struct pcb_struct pcb_va[1];5556/*57* Find a physical address of a virtual object..58*59* This is easy using the virtual page table address.60*/61#define VPTB ((unsigned long *) 0x200000000)6263static inline unsigned long64find_pa(unsigned long address)65{66unsigned long result;6768result = VPTB[address >> 13];69result >>= 32;70result <<= 13;71result |= address & 0x1fff;72return result;73}7475int76check_range(unsigned long vstart, unsigned long vend,77unsigned long kstart, unsigned long kend)78{79unsigned long vaddr, kaddr;8081#ifdef DEBUG_CHECK_RANGE82srm_printk("check_range: V[0x%lx:0x%lx] K[0x%lx:0x%lx]\n",83vstart, vend, kstart, kend);84#endif85/* do some range checking for detecting an overlap... */86for (vaddr = vstart; vaddr <= vend; vaddr += PAGE_SIZE)87{88kaddr = (find_pa(vaddr) | PAGE_OFFSET);89if (kaddr >= kstart && kaddr <= kend)90{91#ifdef DEBUG_CHECK_RANGE92srm_printk("OVERLAP: vaddr 0x%lx kaddr 0x%lx"93" [0x%lx:0x%lx]\n",94vaddr, kaddr, kstart, kend);95#endif96return 1;97}98}99return 0;100}101102/*103* This function moves into OSF/1 pal-code, and has a temporary104* PCB for that. The kernel proper should replace this PCB with105* the real one as soon as possible.106*107* The page table muckery in here depends on the fact that the boot108* code has the L1 page table identity-map itself in the second PTE109* in the L1 page table. Thus the L1-page is virtually addressable110* itself (through three levels) at virtual address 0x200802000.111*/112113#define L1 ((unsigned long *) 0x200802000)114115void116pal_init(void)117{118unsigned long i, rev;119struct percpu_struct * percpu;120struct pcb_struct * pcb_pa;121122/* Create the dummy PCB. */123pcb_va->ksp = 0;124pcb_va->usp = 0;125pcb_va->ptbr = L1[1] >> 32;126pcb_va->asn = 0;127pcb_va->pcc = 0;128pcb_va->unique = 0;129pcb_va->flags = 1;130pcb_va->res1 = 0;131pcb_va->res2 = 0;132pcb_pa = (struct pcb_struct *)find_pa((unsigned long)pcb_va);133134/*135* a0 = 2 (OSF)136* a1 = return address, but we give the asm the vaddr of the PCB137* a2 = physical addr of PCB138* a3 = new virtual page table pointer139* a4 = KSP (but the asm sets it)140*/141srm_printk("Switching to OSF PAL-code... ");142143i = switch_to_osf_pal(2, pcb_va, pcb_pa, VPTB);144if (i) {145srm_printk("failed, code %ld\n", i);146__halt();147}148149percpu = (struct percpu_struct *)150(INIT_HWRPB->processor_offset + (unsigned long) INIT_HWRPB);151rev = percpu->pal_revision = percpu->palcode_avail[2];152153srm_printk("OK (rev %lx)\n", rev);154155tbia(); /* do it directly in case we are SMP */156}157158/*159* Start the kernel.160*/161static inline void162runkernel(void)163{164__asm__ __volatile__(165"bis %0,%0,$27\n\t"166"jmp ($27)"167: /* no outputs: it doesn't even return */168: "r" (START_ADDR));169}170171/* Must record the SP (it is virtual) on entry, so we can make sure172not to overwrite it during movement or decompression. */173unsigned long SP_on_entry;174175/* Calculate the kernel image address based on the end of the BOOTP176bootstrapper (ie this program).177*/178extern char _end;179#define KERNEL_ORIGIN \180((((unsigned long)&_end) + 511) & ~511)181182/* Round address to next higher page boundary. */183#define NEXT_PAGE(a) (((a) | (PAGE_SIZE - 1)) + 1)184185#ifdef INITRD_IMAGE_SIZE186# define REAL_INITRD_SIZE INITRD_IMAGE_SIZE187#else188# define REAL_INITRD_SIZE 0189#endif190191/* Defines from include/asm-alpha/system.h192193BOOT_ADDR Virtual address at which the consoles loads194the BOOTP image.195196KERNEL_START KSEG address at which the kernel is built to run,197which includes some initial data pages before the198code.199200START_ADDR KSEG address of the entry point of kernel code.201202ZERO_PGE KSEG address of page full of zeroes, but203upon entry to kerne cvan be expected204to hold the parameter list and possible205INTRD information.206207These are used in the local defines below.208*/209210211/* Virtual addresses for the BOOTP image. Note that this includes the212bootstrapper code as well as the compressed kernel image, and213possibly the INITRD image.214215Oh, and do NOT forget the STACK, which appears to be placed virtually216beyond the end of the loaded image.217*/218#define V_BOOT_IMAGE_START BOOT_ADDR219#define V_BOOT_IMAGE_END SP_on_entry220221/* Virtual addresses for just the bootstrapper part of the BOOTP image. */222#define V_BOOTSTRAPPER_START BOOT_ADDR223#define V_BOOTSTRAPPER_END KERNEL_ORIGIN224225/* Virtual addresses for just the data part of the BOOTP226image. This may also include the INITRD image, but always227includes the STACK.228*/229#define V_DATA_START KERNEL_ORIGIN230#define V_INITRD_START (KERNEL_ORIGIN + KERNEL_Z_SIZE)231#define V_INTRD_END (V_INITRD_START + REAL_INITRD_SIZE)232#define V_DATA_END V_BOOT_IMAGE_END233234/* KSEG addresses for the uncompressed kernel.235236Note that the end address includes workspace for the decompression.237Note also that the DATA_START address is ZERO_PGE, to which we write238just before jumping to the kernel image at START_ADDR.239*/240#define K_KERNEL_DATA_START ZERO_PGE241#define K_KERNEL_IMAGE_START START_ADDR242#define K_KERNEL_IMAGE_END (START_ADDR + KERNEL_SIZE)243244/* Define to where we may have to decompress the kernel image, before245we move it to the final position, in case of overlap. This will be246above the final position of the kernel.247248Regardless of overlap, we move the INITRD image to the end of this249copy area, because there needs to be a buffer area after the kernel250for "bootmem" anyway.251*/252#define K_COPY_IMAGE_START NEXT_PAGE(K_KERNEL_IMAGE_END)253/* Reserve one page below INITRD for the new stack. */254#define K_INITRD_START \255NEXT_PAGE(K_COPY_IMAGE_START + KERNEL_SIZE + PAGE_SIZE)256#define K_COPY_IMAGE_END \257(K_INITRD_START + REAL_INITRD_SIZE + MALLOC_AREA_SIZE)258#define K_COPY_IMAGE_SIZE \259NEXT_PAGE(K_COPY_IMAGE_END - K_COPY_IMAGE_START)260261void262start_kernel(void)263{264int must_move = 0;265266/* Initialize these for the decompression-in-place situation,267which is the smallest amount of work and most likely to268occur when using the normal START_ADDR of the kernel269(currently set to 16MB, to clear all console code.270*/271unsigned long uncompressed_image_start = K_KERNEL_IMAGE_START;272unsigned long uncompressed_image_end = K_KERNEL_IMAGE_END;273274unsigned long initrd_image_start = K_INITRD_START;275276/*277* Note that this crufty stuff with static and envval278* and envbuf is because:279*280* 1. Frequently, the stack is short, and we don't want to overrun;281* 2. Frequently the stack is where we are going to copy the kernel to;282* 3. A certain SRM console required the GET_ENV output to stack.283* ??? A comment in the aboot sources indicates that the GET_ENV284* destination must be quadword aligned. Might this explain the285* behaviour, rather than requiring output to the stack, which286* seems rather far-fetched.287*/288static long nbytes;289static char envval[256] __attribute__((aligned(8)));290register unsigned long asm_sp asm("30");291292SP_on_entry = asm_sp;293294srm_printk("Linux/Alpha BOOTPZ Loader for Linux " UTS_RELEASE "\n");295296/* Validity check the HWRPB. */297if (INIT_HWRPB->pagesize != 8192) {298srm_printk("Expected 8kB pages, got %ldkB\n",299INIT_HWRPB->pagesize >> 10);300return;301}302if (INIT_HWRPB->vptb != (unsigned long) VPTB) {303srm_printk("Expected vptb at %p, got %p\n",304VPTB, (void *)INIT_HWRPB->vptb);305return;306}307308/* PALcode (re)initialization. */309pal_init();310311/* Get the parameter list from the console environment variable. */312nbytes = callback_getenv(ENV_BOOTED_OSFLAGS, envval, sizeof(envval));313if (nbytes < 0 || nbytes >= sizeof(envval)) {314nbytes = 0;315}316envval[nbytes] = '\0';317318#ifdef DEBUG_ADDRESSES319srm_printk("START_ADDR 0x%lx\n", START_ADDR);320srm_printk("KERNEL_ORIGIN 0x%lx\n", KERNEL_ORIGIN);321srm_printk("KERNEL_SIZE 0x%x\n", KERNEL_SIZE);322srm_printk("KERNEL_Z_SIZE 0x%x\n", KERNEL_Z_SIZE);323#endif324325/* Since all the SRM consoles load the BOOTP image at virtual326* 0x20000000, we have to ensure that the physical memory327* pages occupied by that image do NOT overlap the physical328* address range where the kernel wants to be run. This329* causes real problems when attempting to cdecompress the330* former into the latter... :-(331*332* So, we may have to decompress/move the kernel/INITRD image333* virtual-to-physical someplace else first before moving334* kernel /INITRD to their final resting places... ;-}335*336* Sigh...337*/338339/* First, check to see if the range of addresses occupied by340the bootstrapper part of the BOOTP image include any of the341physical pages into which the kernel will be placed for342execution.343344We only need check on the final kernel image range, since we345will put the INITRD someplace that we can be sure is not346in conflict.347*/348if (check_range(V_BOOTSTRAPPER_START, V_BOOTSTRAPPER_END,349K_KERNEL_DATA_START, K_KERNEL_IMAGE_END))350{351srm_printk("FATAL ERROR: overlap of bootstrapper code\n");352__halt();353}354355/* Next, check to see if the range of addresses occupied by356the compressed kernel/INITRD/stack portion of the BOOTP357image include any of the physical pages into which the358decompressed kernel or the INITRD will be placed for359execution.360*/361if (check_range(V_DATA_START, V_DATA_END,362K_KERNEL_IMAGE_START, K_COPY_IMAGE_END))363{364#ifdef DEBUG_ADDRESSES365srm_printk("OVERLAP: cannot decompress in place\n");366#endif367uncompressed_image_start = K_COPY_IMAGE_START;368uncompressed_image_end = K_COPY_IMAGE_END;369must_move = 1;370371/* Finally, check to see if the range of addresses372occupied by the compressed kernel/INITRD part of373the BOOTP image include any of the physical pages374into which that part is to be copied for375decompression.376*/377while (check_range(V_DATA_START, V_DATA_END,378uncompressed_image_start,379uncompressed_image_end))380{381#if 0382uncompressed_image_start += K_COPY_IMAGE_SIZE;383uncompressed_image_end += K_COPY_IMAGE_SIZE;384initrd_image_start += K_COPY_IMAGE_SIZE;385#else386/* Keep as close as possible to end of BOOTP image. */387uncompressed_image_start += PAGE_SIZE;388uncompressed_image_end += PAGE_SIZE;389initrd_image_start += PAGE_SIZE;390#endif391}392}393394srm_printk("Starting to load the kernel with args '%s'\n", envval);395396#ifdef DEBUG_ADDRESSES397srm_printk("Decompressing the kernel...\n"398"...from 0x%lx to 0x%lx size 0x%x\n",399V_DATA_START,400uncompressed_image_start,401KERNEL_SIZE);402#endif403decompress_kernel((void *)uncompressed_image_start,404(void *)V_DATA_START,405KERNEL_SIZE, KERNEL_Z_SIZE);406407/*408* Now, move things to their final positions, if/as required.409*/410411#ifdef INITRD_IMAGE_SIZE412413/* First, we always move the INITRD image, if present. */414#ifdef DEBUG_ADDRESSES415srm_printk("Moving the INITRD image...\n"416" from 0x%lx to 0x%lx size 0x%x\n",417V_INITRD_START,418initrd_image_start,419INITRD_IMAGE_SIZE);420#endif421memcpy((void *)initrd_image_start, (void *)V_INITRD_START,422INITRD_IMAGE_SIZE);423424#endif /* INITRD_IMAGE_SIZE */425426/* Next, we may have to move the uncompressed kernel to the427final destination.428*/429if (must_move) {430#ifdef DEBUG_ADDRESSES431srm_printk("Moving the uncompressed kernel...\n"432"...from 0x%lx to 0x%lx size 0x%x\n",433uncompressed_image_start,434K_KERNEL_IMAGE_START,435(unsigned)KERNEL_SIZE);436#endif437/*438* Move the stack to a safe place to ensure it won't be439* overwritten by kernel image.440*/441move_stack(initrd_image_start - PAGE_SIZE);442443memcpy((void *)K_KERNEL_IMAGE_START,444(void *)uncompressed_image_start, KERNEL_SIZE);445}446447/* Clear the zero page, then move the argument list in. */448#ifdef DEBUG_LAST_STEPS449srm_printk("Preparing ZERO_PGE...\n");450#endif451memset((char*)ZERO_PGE, 0, PAGE_SIZE);452strcpy((char*)ZERO_PGE, envval);453454#ifdef INITRD_IMAGE_SIZE455456#ifdef DEBUG_LAST_STEPS457srm_printk("Preparing INITRD info...\n");458#endif459/* Finally, set the INITRD paramenters for the kernel. */460((long *)(ZERO_PGE+256))[0] = initrd_image_start;461((long *)(ZERO_PGE+256))[1] = INITRD_IMAGE_SIZE;462463#endif /* INITRD_IMAGE_SIZE */464465#ifdef DEBUG_LAST_STEPS466srm_printk("Doing 'runkernel()'...\n");467#endif468runkernel();469}470471/* dummy function, should never be called. */472void *__kmalloc(size_t size, gfp_t flags)473{474return (void *)NULL;475}476477478