Home | History | Annotate | Line # | Download | only in ofwboot
boot.c revision 1.5.6.1
      1 /*	$NetBSD: boot.c,v 1.5.6.1 1999/12/27 18:33:16 wrstuden Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
     41  * Copyright (C) 1995, 1996 TooLs GmbH.
     42  * All rights reserved.
     43  *
     44  * ELF support derived from NetBSD/alpha's boot loader, written
     45  * by Christopher G. Demetriou.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. All advertising materials mentioning features or use of this software
     56  *    must display the following acknowledgement:
     57  *	This product includes software developed by TooLs GmbH.
     58  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     59  *    derived from this software without specific prior written permission.
     60  *
     61  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     62  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     63  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     64  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     65  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     66  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     67  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     68  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     69  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     70  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     71  */
     72 
     73 /*
     74  * First try for the boot code
     75  *
     76  * Input syntax is:
     77  *	[promdev[{:|,}partition]]/[filename] [flags]
     78  */
     79 
     80 #define	ELFSIZE		32		/* We use 32-bit ELF. */
     81 
     82 #include <sys/param.h>
     83 #include <sys/exec.h>
     84 #include <sys/exec_elf.h>
     85 #include <sys/reboot.h>
     86 #include <sys/disklabel.h>
     87 
     88 #include <lib/libsa/stand.h>
     89 #include <lib/libkern/libkern.h>
     90 
     91 #include <machine/cpu.h>
     92 #include <machine/machine_type.h>
     93 
     94 #include "ofdev.h"
     95 #include "openfirm.h"
     96 
     97 char bootdev[128];
     98 char bootfile[128];
     99 int boothowto;
    100 int debug;
    101 
    102 #ifdef POWERPC_BOOT_ELF
    103 int	elf_exec __P((int, Elf_Ehdr *, u_int32_t *, void **));
    104 #endif
    105 
    106 #ifdef POWERPC_BOOT_AOUT
    107 int	aout_exec __P((int, struct exec *, u_int32_t *, void **));
    108 #endif
    109 
    110 static void
    111 prom2boot(dev)
    112 	char *dev;
    113 {
    114 	char *cp;
    115 
    116 	for (cp = dev; *cp != '\0'; cp++)
    117 		if (*cp == ':') {
    118 			*cp = '\0';
    119 			return;
    120 		}
    121 }
    122 
    123 static void
    124 parseargs(str, howtop)
    125 	char *str;
    126 	int *howtop;
    127 {
    128 	char *cp;
    129 
    130 	/* Allow user to drop back to the PROM. */
    131 	if (strcmp(str, "exit") == 0)
    132 		OF_exit();
    133 
    134 	*howtop = 0;
    135 	for (cp = str; *cp; cp++)
    136 		if (*cp == ' ' || *cp == '-')
    137 			break;
    138 	if (!*cp)
    139 		return;
    140 
    141 	*cp++ = 0;
    142 	while (*cp) {
    143 		switch (*cp++) {
    144 		case 'a':
    145 			*howtop |= RB_ASKNAME;
    146 			break;
    147 		case 's':
    148 			*howtop |= RB_SINGLE;
    149 			break;
    150 		case 'd':
    151 			*howtop |= RB_KDB;
    152 			debug = 1;
    153 			break;
    154 		}
    155 	}
    156 }
    157 
    158 static void
    159 chain(entry, args, esym)
    160 	void (*entry)();
    161 	char *args;
    162 	void *esym;
    163 {
    164 	extern char end[];
    165 	int l, machine_tag;
    166 
    167 	freeall();
    168 
    169 	/*
    170 	 * Stash pointer to end of symbol table after the argument
    171 	 * strings.
    172 	 */
    173 	l = strlen(args) + 1;
    174 	bcopy(&esym, args + l, sizeof(esym));
    175 	l += sizeof(esym);
    176 
    177 	/*
    178 	 * Tell the kernel we're an OpenFirmware system.
    179 	 */
    180 	machine_tag = POWERPC_MACHINE_OPENFIRMWARE;
    181 	bcopy(&machine_tag, args + l, sizeof(machine_tag));
    182 	l += sizeof(machine_tag);
    183 
    184 	OF_chain((void *)RELOC, end - (char *)RELOC, entry, args, l);
    185 	panic("chain");
    186 }
    187 
    188 int
    189 loadfile(fd, args)
    190 	int fd;
    191 	char *args;
    192 {
    193 	union {
    194 #ifdef POWERPC_BOOT_AOUT
    195 		struct exec aout;
    196 #endif
    197 #ifdef POWERPC_BOOT_ELF
    198 		Elf_Ehdr elf;
    199 #endif
    200 	} hdr;
    201 	int rval;
    202 	u_int32_t entry;
    203 	void *esym;
    204 
    205 	rval = 1;
    206 	esym = NULL;
    207 
    208 	/* Load the header. */
    209 	if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
    210 		printf("read header: %s\n", strerror(errno));
    211 		goto err;
    212 	}
    213 
    214 	/* Determine file type, load kernel. */
    215 #ifdef POWERPC_BOOT_AOUT
    216 	if (N_BADMAG(hdr.aout) == 0 && N_GETMID(hdr.aout) == MID_POWERPC) {
    217 		rval = aout_exec(fd, &hdr.aout, &entry, &esym);
    218 	} else
    219 #endif
    220 #ifdef POWERPC_BOOT_ELF
    221 	if (memcmp(hdr.elf.e_ident, ELFMAG, SELFMAG) == 0 &&
    222 	    hdr.elf.e_ident[EI_CLASS] == ELFCLASS) {
    223 		rval = elf_exec(fd, &hdr.elf, &entry, &esym);
    224 	} else
    225 #endif
    226 	{
    227 		printf("unknown executable format\n");
    228 	}
    229 
    230 	if (rval)
    231 		goto err;
    232 
    233 	printf(" start=0x%x\n", entry);
    234 
    235 	close(fd);
    236 
    237 	/* XXX this should be replaced w/ a mountroothook. */
    238 	if (floppyboot) {
    239 		printf("Please insert root disk and press ENTER ");
    240 		getchar();
    241 		printf("\n");
    242 	}
    243 
    244 	chain((void *)entry, args, esym);
    245 	/* NOTREACHED */
    246 
    247  err:
    248 	close(fd);
    249 	return (rval);
    250 }
    251 
    252 __dead void
    253 _rtt()
    254 {
    255 
    256 	OF_exit();
    257 }
    258 
    259 #ifdef POWERPC_BOOT_AOUT
    260 int
    261 aout_exec(fd, hdr, entryp, esymp)
    262 	int fd;
    263 	struct exec *hdr;
    264 	u_int32_t *entryp;
    265 	void **esymp;
    266 {
    267 	void *addr;
    268 	int n, *paddr;
    269 
    270 	/* Display the load address (entry point) for a.out. */
    271 	printf("Booting %s @ 0x%lx\n", opened_name, hdr->a_entry);
    272 	addr = (void *)(hdr->a_entry);
    273 
    274 	/*
    275 	 * Determine memory needed for kernel and allocate it from
    276 	 * the firmware.
    277 	 */
    278 	n = hdr->a_text + hdr->a_data + hdr->a_bss + hdr->a_syms + sizeof(int);
    279 	if ((paddr = OF_claim(addr, n, 0)) == (int *)-1)
    280 		panic("cannot claim memory");
    281 
    282 	/* Load text. */
    283 	lseek(fd, N_TXTOFF(*hdr), SEEK_SET);
    284 	printf("%lu", hdr->a_text);
    285 	if (read(fd, paddr, hdr->a_text) != hdr->a_text) {
    286 		printf("read text: %s\n", strerror(errno));
    287 		return (1);
    288 	}
    289 	__syncicache((void *)paddr, hdr->a_text);
    290 
    291 	/* Load data. */
    292 	printf("+%lu", hdr->a_data);
    293 	if (read(fd, (void *)paddr + hdr->a_text, hdr->a_data) != hdr->a_data) {
    294 		printf("read data: %s\n", strerror(errno));
    295 		return (1);
    296 	}
    297 
    298 	/* Zero BSS. */
    299 	printf("+%lu", hdr->a_bss);
    300 	bzero((void *)paddr + hdr->a_text + hdr->a_data, hdr->a_bss);
    301 
    302 	/* Symbols. */
    303 	*esymp = paddr;
    304 	paddr = (int *)((void *)paddr + hdr->a_text + hdr->a_data + hdr->a_bss);
    305 	*paddr++ = hdr->a_syms;
    306 	if (hdr->a_syms) {
    307 		printf(" [%lu", hdr->a_syms);
    308 		if (read(fd, paddr, hdr->a_syms) != hdr->a_syms) {
    309 			printf("read symbols: %s\n", strerror(errno));
    310 			return (1);
    311 		}
    312 		paddr = (int *)((void *)paddr + hdr->a_syms);
    313 		if (read(fd, &n, sizeof(int)) != sizeof(int)) {
    314 			printf("read symbols: %s\n", strerror(errno));
    315 			return (1);
    316 		}
    317 		if (OF_claim((void *)paddr, n + sizeof(int), 0) == (void *)-1)
    318 			panic("cannot claim memory");
    319 		*paddr++ = n;
    320 		if (read(fd, paddr, n - sizeof(int)) != n - sizeof(int)) {
    321 			printf("read symbols: %s\n", strerror(errno));
    322 			return (1);
    323 		}
    324 		printf("+%d]", n - sizeof(int));
    325 		*esymp = paddr + (n - sizeof(int));
    326 	}
    327 
    328 	*entryp = hdr->a_entry;
    329 	return (0);
    330 }
    331 #endif /* POWERPC_BOOT_AOUT */
    332 
    333 #ifdef POWERPC_BOOT_ELF
    334 int
    335 elf_exec(fd, elf, entryp, esymp)
    336 	int fd;
    337 	Elf_Ehdr *elf;
    338 	u_int32_t *entryp;
    339 	void **esymp;
    340 {
    341 	Elf32_Shdr *shp;
    342 	Elf32_Off off;
    343 	void *addr;
    344 	size_t size;
    345 	int i, first = 1;
    346 	int n;
    347 
    348 	/*
    349 	 * Don't display load address for ELF; it's encoded in
    350 	 * each section.
    351 	 */
    352 	printf("Booting %s\n", opened_name);
    353 
    354 	for (i = 0; i < elf->e_phnum; i++) {
    355 		Elf_Phdr phdr;
    356 		(void)lseek(fd, elf->e_phoff + sizeof(phdr) * i, SEEK_SET);
    357 		if (read(fd, (void *)&phdr, sizeof(phdr)) != sizeof(phdr)) {
    358 			printf("read phdr: %s\n", strerror(errno));
    359 			return (1);
    360 		}
    361 		if (phdr.p_type != PT_LOAD ||
    362 		    (phdr.p_flags & (PF_W|PF_X)) == 0)
    363 			continue;
    364 
    365 		/* Read in segment. */
    366 		printf("%s%lu@0x%lx", first ? "" : "+", phdr.p_filesz,
    367 		    (u_long)phdr.p_vaddr);
    368 		(void)lseek(fd, phdr.p_offset, SEEK_SET);
    369 		if (OF_claim((void *)phdr.p_vaddr, phdr.p_memsz, 0) ==
    370 		    (void *)-1)
    371 			panic("cannot claim memory");
    372 		if (read(fd, (void *)phdr.p_vaddr, phdr.p_filesz) !=
    373 		    phdr.p_filesz) {
    374 			printf("read segment: %s\n", strerror(errno));
    375 			return (1);
    376 		}
    377 		__syncicache((void *)phdr.p_vaddr, phdr.p_filesz);
    378 
    379 		/* Zero BSS. */
    380 		if (phdr.p_filesz < phdr.p_memsz) {
    381 			printf("+%lu@0x%lx", phdr.p_memsz - phdr.p_filesz,
    382 			    (u_long)(phdr.p_vaddr + phdr.p_filesz));
    383 			bzero((void *)phdr.p_vaddr + phdr.p_filesz,
    384 			    phdr.p_memsz - phdr.p_filesz);
    385 		}
    386 		first = 0;
    387 	}
    388 
    389 	printf(" \n");
    390 
    391 #if 0 /* I want to rethink this... --thorpej (at) netbsd.org */
    392 	/*
    393 	 * Compute the size of the symbol table.
    394 	 */
    395 	size = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
    396 	shp = addr = alloc(elf->e_shnum * sizeof(Elf32_Shdr));
    397 	(void)lseek(fd, elf->e_shoff, SEEK_SET);
    398 	if (read(fd, addr, elf->e_shnum * sizeof(Elf32_Shdr)) !=
    399 	    elf->e_shnum * sizeof(Elf32_Shdr)) {
    400 		printf("read section headers: %s\n", strerror(errno));
    401 		return (1);
    402 	}
    403 	for (i = 0; i < elf->e_shnum; i++, shp++) {
    404 		if (shp->sh_type == SHT_NULL)
    405 			continue;
    406 		if (shp->sh_type != SHT_SYMTAB
    407 		    && shp->sh_type != SHT_STRTAB) {
    408 			shp->sh_offset = 0;
    409 			shp->sh_type = SHT_NOBITS;
    410 			continue;
    411 		}
    412 		size += shp->sh_size;
    413 	}
    414 	shp = addr;
    415 
    416 	/*
    417 	 * Reserve memory for the symbols.
    418 	 */
    419 	if ((addr = OF_claim(0, size, NBPG)) == (void *)-1)
    420 		panic("no space for symbol table");
    421 
    422 	/*
    423 	 * Copy the headers.
    424 	 */
    425 	elf->e_phoff = 0;
    426 	elf->e_shoff = sizeof(Elf_Ehdr);
    427 	elf->e_phentsize = 0;
    428 	elf->e_phnum = 0;
    429 	bcopy(elf, addr, sizeof(Elf_Ehdr));
    430 	bcopy(shp, addr + sizeof(Elf_Ehdr), elf->e_shnum * sizeof(Elf32_Shdr));
    431 	free(shp, elf->e_shnum * sizeof(Elf32_Shdr));
    432 	*ssymp = addr;
    433 
    434 	/*
    435 	 * Now load the symbol sections themselves.
    436 	 */
    437 	shp = addr + sizeof(Elf_Ehdr);
    438 	addr += sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
    439 	off = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
    440 	for (first = 1, i = 0; i < elf->e_shnum; i++, shp++) {
    441 		if (shp->sh_type == SHT_SYMTAB
    442 		    || shp->sh_type == SHT_STRTAB) {
    443 			if (first)
    444 				printf("symbols @ 0x%lx ", (u_long)addr);
    445 			printf("%s%d", first ? "" : "+", shp->sh_size);
    446 			(void)lseek(fd, shp->sh_offset, SEEK_SET);
    447 			if (read(fd, addr, shp->sh_size) != shp->sh_size) {
    448 				printf("read symbols: %s\n", strerror(errno));
    449 				return (1);
    450 			}
    451 			addr += shp->sh_size;
    452 			shp->sh_offset = off;
    453 			off += shp->sh_size;
    454 			first = 0;
    455 		}
    456 	}
    457 	*esymp = addr;
    458 #endif /* 0 */
    459 
    460 	*entryp = elf->e_entry;
    461 	return (0);
    462 }
    463 #endif /* POWERPC_BOOT_ELF */
    464 
    465 void
    466 main()
    467 {
    468 	extern char bootprog_name[], bootprog_rev[],
    469 	    bootprog_maker[], bootprog_date[];
    470 	int chosen;
    471 	char bootline[512];		/* Should check size? */
    472 	char *cp;
    473 	int fd;
    474 
    475 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    476 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    477 
    478 	/*
    479 	 * Get the boot arguments from Openfirmware
    480 	 */
    481 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
    482 	    OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
    483 	    OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
    484 		printf("Invalid Openfirmware environment\n");
    485 		OF_exit();
    486 	}
    487 
    488 	prom2boot(bootdev);
    489 	parseargs(bootline, &boothowto);
    490 
    491 	for (;;) {
    492 		if (boothowto & RB_ASKNAME) {
    493 			printf("Boot: ");
    494 			gets(bootline);
    495 			parseargs(bootline, &boothowto);
    496 		}
    497 		if ((fd = open(bootline, 0)) >= 0)
    498 			break;
    499 		if (errno)
    500 			printf("open %s: %s\n", opened_name, strerror(errno));
    501 		boothowto |= RB_ASKNAME;
    502 	}
    503 #ifdef	__notyet__
    504 	OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
    505 	cp = bootline;
    506 #else
    507 	strcpy(bootline, opened_name);
    508 	cp = bootline + strlen(bootline);
    509 	*cp++ = ' ';
    510 #endif
    511 	*cp = '-';
    512 	if (boothowto & RB_ASKNAME)
    513 		*++cp = 'a';
    514 	if (boothowto & RB_SINGLE)
    515 		*++cp = 's';
    516 	if (boothowto & RB_KDB)
    517 		*++cp = 'd';
    518 	if (*cp == '-')
    519 #ifdef	__notyet__
    520 		*cp = 0;
    521 #else
    522 		*--cp = 0;
    523 #endif
    524 	else
    525 		*++cp = 0;
    526 #ifdef	__notyet__
    527 	OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
    528 #endif
    529 	/* XXX void, for now */
    530 	(void)loadfile(fd, bootline);
    531 
    532 	OF_exit();
    533 }
    534