Home | History | Annotate | Line # | Download | only in ofwboot
boot.c revision 1.9.2.1
      1 /*	$NetBSD: boot.c,v 1.9.2.1 2001/08/03 04:12:11 lukem 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 #include <sys/boot_flag.h>
     88 
     89 #include <lib/libsa/stand.h>
     90 #include <lib/libkern/libkern.h>
     91 
     92 #include <machine/cpu.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 		BOOT_FLAG(*cp++, *howtop);
    144 }
    145 
    146 static void
    147 chain(entry, args, esym)
    148 	void (*entry)();
    149 	char *args;
    150 	void *esym;
    151 {
    152 	extern char end[];
    153 	int l;
    154 
    155 	freeall();
    156 
    157 	/*
    158 	 * Stash pointer to end of symbol table after the argument
    159 	 * strings.
    160 	 */
    161 	l = strlen(args) + 1;
    162 	memcpy(args + l, &esym, sizeof(esym));
    163 	l += sizeof(esym);
    164 
    165 	OF_chain((void *)RELOC, end - (char *)RELOC, entry, args, l);
    166 	panic("chain");
    167 }
    168 
    169 int
    170 loadfile(fd, args)
    171 	int fd;
    172 	char *args;
    173 {
    174 	union {
    175 #ifdef POWERPC_BOOT_AOUT
    176 		struct exec aout;
    177 #endif
    178 #ifdef POWERPC_BOOT_ELF
    179 		Elf_Ehdr elf;
    180 #endif
    181 	} hdr;
    182 	int rval;
    183 	u_int32_t entry;
    184 	void *esym;
    185 
    186 	rval = 1;
    187 	esym = NULL;
    188 
    189 	/* Load the header. */
    190 	if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
    191 		printf("read header: %s\n", strerror(errno));
    192 		goto err;
    193 	}
    194 
    195 	/* Determine file type, load kernel. */
    196 #ifdef POWERPC_BOOT_AOUT
    197 	if (N_BADMAG(hdr.aout) == 0 && N_GETMID(hdr.aout) == MID_POWERPC) {
    198 		rval = aout_exec(fd, &hdr.aout, &entry, &esym);
    199 	} else
    200 #endif
    201 #ifdef POWERPC_BOOT_ELF
    202 	if (memcmp(hdr.elf.e_ident, ELFMAG, SELFMAG) == 0 &&
    203 	    hdr.elf.e_ident[EI_CLASS] == ELFCLASS) {
    204 		rval = elf_exec(fd, &hdr.elf, &entry, &esym);
    205 	} else
    206 #endif
    207 	{
    208 		printf("unknown executable format\n");
    209 	}
    210 
    211 	if (rval)
    212 		goto err;
    213 
    214 	printf(" start=0x%x\n", entry);
    215 
    216 	close(fd);
    217 
    218 	/* XXX this should be replaced w/ a mountroothook. */
    219 	if (floppyboot) {
    220 		printf("Please insert root disk and press ENTER ");
    221 		getchar();
    222 		printf("\n");
    223 	}
    224 
    225 	chain((void *)entry, args, esym);
    226 	/* NOTREACHED */
    227 
    228  err:
    229 	close(fd);
    230 	return (rval);
    231 }
    232 
    233 __dead void
    234 _rtt()
    235 {
    236 
    237 	OF_exit();
    238 }
    239 
    240 #ifdef POWERPC_BOOT_AOUT
    241 int
    242 aout_exec(fd, hdr, entryp, esymp)
    243 	int fd;
    244 	struct exec *hdr;
    245 	u_int32_t *entryp;
    246 	void **esymp;
    247 {
    248 	int n, *addr;
    249 
    250 	/* Display the load address (entry point) for a.out. */
    251 	printf("Booting %s @ 0x%lx\n", opened_name, hdr->a_entry);
    252 	addr = (void *)(hdr->a_entry);
    253 
    254 	/*
    255 	 * Determine memory needed for kernel and allocate it from
    256 	 * the firmware.
    257 	 */
    258 	n = hdr->a_text + hdr->a_data + hdr->a_bss + hdr->a_syms + sizeof(int);
    259 
    260 	/* Load text. */
    261 	lseek(fd, N_TXTOFF(*hdr), SEEK_SET);
    262 	printf("%lu", hdr->a_text);
    263 	if (read(fd, addr, hdr->a_text) != hdr->a_text) {
    264 		printf("read text: %s\n", strerror(errno));
    265 		return (1);
    266 	}
    267 	__syncicache((void *)addr, hdr->a_text);
    268 
    269 	/* Load data. */
    270 	printf("+%lu", hdr->a_data);
    271 	if (read(fd, (char *)addr + hdr->a_text, hdr->a_data) != hdr->a_data) {
    272 		printf("read data: %s\n", strerror(errno));
    273 		return (1);
    274 	}
    275 
    276 	/* Zero BSS. */
    277 	printf("+%lu", hdr->a_bss);
    278 	memset((char *)addr + hdr->a_text + hdr->a_data, 0, hdr->a_bss);
    279 
    280 	/* Symbols. */
    281 	*esymp = addr;
    282 	addr = (int *)((char *)addr + hdr->a_text + hdr->a_data + hdr->a_bss);
    283 	*addr++ = hdr->a_syms;
    284 	if (hdr->a_syms) {
    285 		printf(" [%lu", hdr->a_syms);
    286 		if (read(fd, addr, hdr->a_syms) != hdr->a_syms) {
    287 			printf("read symbols: %s\n", strerror(errno));
    288 			return (1);
    289 		}
    290 		addr = (int *)((char *)addr + hdr->a_syms);
    291 		if (read(fd, &n, sizeof(int)) != sizeof(int)) {
    292 			printf("read symbols: %s\n", strerror(errno));
    293 			return (1);
    294 		}
    295 		*addr++ = n;
    296 		if (read(fd, addr, n - sizeof(int)) != n - sizeof(int)) {
    297 			printf("read symbols: %s\n", strerror(errno));
    298 			return (1);
    299 		}
    300 		printf("+%d]", n - sizeof(int));
    301 		*esymp = addr + (n - sizeof(int));
    302 	}
    303 
    304 	*entryp = hdr->a_entry;
    305 	return (0);
    306 }
    307 #endif /* POWERPC_BOOT_AOUT */
    308 
    309 #ifdef POWERPC_BOOT_ELF
    310 int
    311 elf_exec(fd, elf, entryp, esymp)
    312 	int fd;
    313 	Elf_Ehdr *elf;
    314 	u_int32_t *entryp;
    315 	void **esymp;
    316 {
    317 	Elf32_Shdr *shp;
    318 	Elf32_Off off;
    319 	void *addr;
    320 	size_t size;
    321 	int i, first = 1;
    322 	int n;
    323 
    324 	/*
    325 	 * Don't display load address for ELF; it's encoded in
    326 	 * each section.
    327 	 */
    328 	printf("Booting %s\n", opened_name);
    329 
    330 	for (i = 0; i < elf->e_phnum; i++) {
    331 		Elf_Phdr phdr;
    332 		(void)lseek(fd, elf->e_phoff + sizeof(phdr) * i, SEEK_SET);
    333 		if (read(fd, (void *)&phdr, sizeof(phdr)) != sizeof(phdr)) {
    334 			printf("read phdr: %s\n", strerror(errno));
    335 			return (1);
    336 		}
    337 		if (phdr.p_type != PT_LOAD ||
    338 		    (phdr.p_flags & (PF_W|PF_X)) == 0)
    339 			continue;
    340 
    341 		/* Read in segment. */
    342 		printf("%s%lu@0x%lx", first ? "" : "+", phdr.p_filesz,
    343 		    (u_long)phdr.p_vaddr);
    344 		(void)lseek(fd, phdr.p_offset, SEEK_SET);
    345 		if (read(fd, (void *)phdr.p_vaddr, phdr.p_filesz) !=
    346 		    phdr.p_filesz) {
    347 			printf("read segment: %s\n", strerror(errno));
    348 			return (1);
    349 		}
    350 		__syncicache((void *)phdr.p_vaddr, phdr.p_filesz);
    351 
    352 		/* Zero BSS. */
    353 		if (phdr.p_filesz < phdr.p_memsz) {
    354 			printf("+%lu@0x%lx", phdr.p_memsz - phdr.p_filesz,
    355 			    (u_long)(phdr.p_vaddr + phdr.p_filesz));
    356 			memset((void *)phdr.p_vaddr + phdr.p_filesz, 0,
    357 			    phdr.p_memsz - phdr.p_filesz);
    358 		}
    359 		first = 0;
    360 	}
    361 
    362 	printf(" \n");
    363 
    364 #if 0 /* I want to rethink this... --thorpej (at) netbsd.org */
    365 	/*
    366 	 * Compute the size of the symbol table.
    367 	 */
    368 	size = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
    369 	shp = addr = alloc(elf->e_shnum * sizeof(Elf32_Shdr));
    370 	(void)lseek(fd, elf->e_shoff, SEEK_SET);
    371 	if (read(fd, addr, elf->e_shnum * sizeof(Elf32_Shdr)) !=
    372 	    elf->e_shnum * sizeof(Elf32_Shdr)) {
    373 		printf("read section headers: %s\n", strerror(errno));
    374 		return (1);
    375 	}
    376 	for (i = 0; i < elf->e_shnum; i++, shp++) {
    377 		if (shp->sh_type == SHT_NULL)
    378 			continue;
    379 		if (shp->sh_type != SHT_SYMTAB
    380 		    && shp->sh_type != SHT_STRTAB) {
    381 			shp->sh_offset = 0;
    382 			shp->sh_type = SHT_NOBITS;
    383 			continue;
    384 		}
    385 		size += shp->sh_size;
    386 	}
    387 	shp = addr;
    388 
    389 	panic("no space for symbol table");
    390 
    391 	/*
    392 	 * Copy the headers.
    393 	 */
    394 	elf->e_phoff = 0;
    395 	elf->e_shoff = sizeof(Elf_Ehdr);
    396 	elf->e_phentsize = 0;
    397 	elf->e_phnum = 0;
    398 	memcpy(addr, elf, sizeof(Elf_Ehdr));
    399 	memcpy(addr + sizeof(Elf_Ehdr), shp, elf->e_shnum * sizeof(Elf32_Shdr));
    400 	free(shp, elf->e_shnum * sizeof(Elf32_Shdr));
    401 	*ssymp = addr;
    402 
    403 	/*
    404 	 * Now load the symbol sections themselves.
    405 	 */
    406 	shp = addr + sizeof(Elf_Ehdr);
    407 	addr += sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
    408 	off = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
    409 	for (first = 1, i = 0; i < elf->e_shnum; i++, shp++) {
    410 		if (shp->sh_type == SHT_SYMTAB
    411 		    || shp->sh_type == SHT_STRTAB) {
    412 			if (first)
    413 				printf("symbols @ 0x%lx ", (u_long)addr);
    414 			printf("%s%d", first ? "" : "+", shp->sh_size);
    415 			(void)lseek(fd, shp->sh_offset, SEEK_SET);
    416 			if (read(fd, addr, shp->sh_size) != shp->sh_size) {
    417 				printf("read symbols: %s\n", strerror(errno));
    418 				return (1);
    419 			}
    420 			addr += shp->sh_size;
    421 			shp->sh_offset = off;
    422 			off += shp->sh_size;
    423 			first = 0;
    424 		}
    425 	}
    426 	*esymp = addr;
    427 #endif /* 0 */
    428 
    429 	*entryp = elf->e_entry;
    430 	return (0);
    431 }
    432 #endif /* POWERPC_BOOT_ELF */
    433 
    434 void
    435 main()
    436 {
    437 	extern char bootprog_name[], bootprog_rev[],
    438 	    bootprog_maker[], bootprog_date[];
    439 	int chosen;
    440 	char bootline[512];		/* Should check size? */
    441 	char *cp;
    442 	int fd;
    443 
    444 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    445 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    446 
    447 	/*
    448 	 * Get the boot arguments from Openfirmware
    449 	 */
    450 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
    451 	    OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
    452 	    OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
    453 		printf("Invalid Openfirmware environment\n");
    454 		OF_exit();
    455 	}
    456 
    457 	prom2boot(bootdev);
    458 	parseargs(bootline, &boothowto);
    459 
    460 	for (;;) {
    461 		if (boothowto & RB_ASKNAME) {
    462 			printf("Boot: ");
    463 			gets(bootline);
    464 			parseargs(bootline, &boothowto);
    465 		}
    466 		if ((fd = open(bootline, 0)) >= 0)
    467 			break;
    468 		if (errno)
    469 			printf("open %s: %s\n", opened_name, strerror(errno));
    470 		boothowto |= RB_ASKNAME;
    471 	}
    472 #ifdef	__notyet__
    473 	OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
    474 	cp = bootline;
    475 #else
    476 	strcpy(bootline, opened_name);
    477 	cp = bootline + strlen(bootline);
    478 	*cp++ = ' ';
    479 #endif
    480 	*cp = '-';
    481 	if (boothowto & RB_ASKNAME)
    482 		*++cp = 'a';
    483 	if (boothowto & RB_SINGLE)
    484 		*++cp = 's';
    485 	if (boothowto & RB_KDB)
    486 		*++cp = 'd';
    487 	if (*cp == '-')
    488 #ifdef	__notyet__
    489 		*cp = 0;
    490 #else
    491 		*--cp = 0;
    492 #endif
    493 	else
    494 		*++cp = 0;
    495 #ifdef	__notyet__
    496 	OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
    497 #endif
    498 	/* XXX void, for now */
    499 	(void)loadfile(fd, bootline);
    500 
    501 	OF_exit();
    502 }
    503