bootxx.c revision 1.3.46.1       1  1.3.46.1     yamt /*	$NetBSD: bootxx.c,v 1.3.46.1 2008/05/16 02:22:20 yamt Exp $	*/
      2       1.1  tsutsui 
      3       1.1  tsutsui /*-
      4       1.1  tsutsui  * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
      5       1.1  tsutsui  * All rights reserved.
      6       1.1  tsutsui  *
      7       1.1  tsutsui  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  tsutsui  * by UCHIYAMA Yasushi.
      9       1.1  tsutsui  *
     10       1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
     11       1.1  tsutsui  * modification, are permitted provided that the following conditions
     12       1.1  tsutsui  * are met:
     13       1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     14       1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     15       1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     18       1.1  tsutsui  *
     19       1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  tsutsui  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  tsutsui  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  tsutsui  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  tsutsui  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  tsutsui  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  tsutsui  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  tsutsui  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  tsutsui  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  tsutsui  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  tsutsui  */
     31       1.1  tsutsui 
     32       1.1  tsutsui #ifdef _STANDALONE
     33       1.1  tsutsui #include <lib/libsa/stand.h>
     34       1.1  tsutsui #include <lib/libkern/libkern.h>
     35       1.1  tsutsui #endif
     36       1.1  tsutsui 
     37       1.1  tsutsui #include <sys/types.h>
     38       1.1  tsutsui #include <sys/param.h>
     39       1.1  tsutsui #include "bootxx.h"
     40       1.1  tsutsui 
     41       1.1  tsutsui #include <sys/exec_ecoff.h>
     42       1.1  tsutsui #include <sys/exec_elf.h>
     43       1.1  tsutsui 
     44       1.1  tsutsui #include <machine/sector.h>
     45       1.1  tsutsui #include <machine/sbd.h>
     46       1.1  tsutsui 
     47       1.1  tsutsui #include "common.h"
     48       1.1  tsutsui 
     49       1.1  tsutsui #define	FILHSZ	(sizeof(struct ecoff_filehdr))
     50       1.1  tsutsui #define	SCNHSZ	(sizeof(struct ecoff_scnhdr))
     51       1.1  tsutsui #define	N_TXTOFF(f, a)							\
     52       1.1  tsutsui 	((a)->vstamp < 23 ?						\
     53       1.1  tsutsui 	((FILHSZ +(f)->f_opthdr +(f)->f_nscns * SCNHSZ + 7) & ~7) :	\
     54       1.1  tsutsui 	((FILHSZ +(f)->f_opthdr +(f)->f_nscns * SCNHSZ + 15) & ~15))
     55       1.1  tsutsui 
     56       1.1  tsutsui int main(void);
     57       1.1  tsutsui int loader(const char *, uint32_t *);
     58       1.1  tsutsui int load_elf(uint8_t *, uint32_t *);
     59       1.1  tsutsui int load_coff(uint8_t *, uint32_t *);
     60       1.1  tsutsui int dk_read(int, int, void *);
     61       1.1  tsutsui 
     62       1.1  tsutsui const char *errmsg[] = {
     63       1.1  tsutsui 	[BERR_PDINFO]		= "No PDINFO",
     64       1.1  tsutsui 	[BERR_RDVTOC]		= "VTOC read error",
     65       1.1  tsutsui 	[BERR_VTOC]		= "VTOC bad magic",
     66       1.1  tsutsui 	[BERR_NOBFS]		= "No BFS partition",
     67       1.1  tsutsui 	[BERR_RDINODE]		= "I-node read error",
     68       1.1  tsutsui 	[BERR_NOROOT]		= "No root",
     69       1.1  tsutsui 	[BERR_RDDIRENT]		= "Dirent read error",
     70       1.1  tsutsui 	[BERR_NOFILE]		= "No such a file",
     71       1.1  tsutsui 	[BERR_RDFILE]		= "File read error",
     72       1.1  tsutsui 	[BERR_FILEMAGIC]	= "Bad file magic",
     73       1.1  tsutsui 	[BERR_AOUTHDR]		= "Not a OMAGIC",
     74       1.1  tsutsui 	[BERR_ELFHDR]		= "Not a ELF",
     75       1.1  tsutsui 	[BERR_TARHDR]		= "Read tar file",
     76       1.1  tsutsui 	[BERR_TARMAGIC]		= "Bad tar magic",
     77       1.1  tsutsui };
     78       1.1  tsutsui 
     79       1.1  tsutsui const char *boottab[] = {
     80       1.1  tsutsui 	"boot",		/* NetBSD (bfs,ustarfs) */
     81       1.1  tsutsui 	"iopboot",	/* EWS-UX (bfs) */
     82       1.1  tsutsui 	0		/* terminate */
     83       1.1  tsutsui };
     84       1.1  tsutsui 
     85       1.2  tsutsui int __dk_unit, __dk_type;
     86       1.3  thorpej bool (*fd_position)(uint32_t, uint32_t *, int *);
     87       1.1  tsutsui 
     88       1.1  tsutsui int
     89       1.1  tsutsui main(void)
     90       1.1  tsutsui {
     91       1.1  tsutsui 	char msg[] = "[+++] NetBSD/ews4800mips >> ";
     92       1.1  tsutsui 	const char *p;
     93       1.1  tsutsui 	const char **boot;
     94       1.1  tsutsui 	uint32_t entry;
     95       1.1  tsutsui 	int err;
     96       1.1  tsutsui 
     97       1.1  tsutsui #ifdef _STANDALONE
     98       1.1  tsutsui 	int i, fd_format;
     99       1.1  tsutsui 
    100       1.1  tsutsui 	boot_device(&__dk_type, &__dk_unit, &fd_format);
    101       1.1  tsutsui 	msg[1] = __dk_type + '0';
    102       1.1  tsutsui 	msg[2] = __dk_unit + '0';
    103       1.1  tsutsui 	msg[3] = FS_SIGNATURE;
    104       1.1  tsutsui 	for (i = 0; msg[i] != 0; i++)
    105       1.1  tsutsui 		ROM_PUTC(32 + i * 12, 536, msg[i]);
    106       1.1  tsutsui 
    107       1.1  tsutsui 	if (__dk_type == NVSRAM_BOOTDEV_FLOPPYDISK) {
    108       1.1  tsutsui 		fd_position = blk_to_2d_position;
    109       1.1  tsutsui 		if (fd_format == FD_FORMAT_2HD) {
    110       1.1  tsutsui 			fd_position = blk_to_2hd_position;
    111       1.1  tsutsui 			__dk_unit |= 0x1000000;		/* | 2HD flag */
    112       1.1  tsutsui 		}
    113       1.1  tsutsui 	}
    114       1.1  tsutsui #else
    115       1.1  tsutsui 	printf("%s\n", msg);
    116       1.1  tsutsui 	sector_init("/dev/rsd1p", DEV_BSIZE);
    117       1.1  tsutsui 	sector_read(SDBOOT_PDINFOADDR, PDINFO_SECTOR);
    118       1.1  tsutsui #endif
    119       1.1  tsutsui 
    120       1.1  tsutsui 	for (boot = boottab; *boot; boot++) {
    121       1.1  tsutsui 		if ((err = loader(*boot, &entry)) == 0) {
    122       1.1  tsutsui #ifdef _STANDALONE
    123       1.1  tsutsui 			ROM_PUTC(0, 0, '\n');
    124       1.1  tsutsui 			ROM_PUTC(0, 0, '\r');
    125       1.1  tsutsui 			return entry;
    126       1.1  tsutsui #else
    127       1.1  tsutsui 			printf("entry=%p\n", (void *)entry);
    128       1.1  tsutsui 			sector_fini();
    129       1.1  tsutsui 			return 0;
    130       1.1  tsutsui #endif
    131       1.1  tsutsui 		}
    132       1.1  tsutsui 		p = errmsg[err];
    133       1.1  tsutsui #ifdef _STANDALONE
    134       1.1  tsutsui 		for (i = 0; p[i] != 0; i++)
    135       1.1  tsutsui 			ROM_PUTC(600 + i * 12, 536, p[i]);
    136       1.1  tsutsui #else
    137       1.1  tsutsui 		DPRINTF("***ERROR *** %s\n", p);
    138       1.1  tsutsui #endif
    139       1.1  tsutsui 	}
    140       1.1  tsutsui 
    141       1.1  tsutsui #ifdef _STANDALONE
    142       1.1  tsutsui 	while (/*CONSTCOND*/1)
    143       1.1  tsutsui 		;
    144       1.1  tsutsui 	/* NOTREACHED */
    145       1.1  tsutsui #else
    146       1.1  tsutsui 	sector_fini();
    147       1.1  tsutsui #endif
    148       1.1  tsutsui 	return 0;
    149       1.1  tsutsui }
    150       1.1  tsutsui 
    151       1.1  tsutsui int
    152       1.1  tsutsui loader(const char *fname, uint32_t *entry)
    153       1.1  tsutsui {
    154       1.1  tsutsui 	int err;
    155       1.1  tsutsui 	uint16_t mag;
    156       1.1  tsutsui 	size_t sz;
    157       1.1  tsutsui #ifdef _STANDALONE
    158       1.1  tsutsui 	int i;
    159       1.1  tsutsui 
    160       1.1  tsutsui 	for (i = 0; fname[i] != 0; i++)
    161       1.1  tsutsui 		ROM_PUTC(380 + i * 12, 536, fname[i]);
    162       1.1  tsutsui #else
    163       1.1  tsutsui 	printf("%s\n", fname);
    164       1.1  tsutsui #endif
    165       1.1  tsutsui 
    166       1.1  tsutsui 	if ((err = fileread(fname, &sz)) != 0)
    167       1.1  tsutsui 		return err;
    168       1.1  tsutsui 
    169       1.1  tsutsui 	DPRINTF("%s found. %d bytes.\n", fname, sz);
    170       1.1  tsutsui 
    171       1.1  tsutsui 	mag = *(uint16_t *)SDBOOT_SCRATCHADDR;
    172       1.1  tsutsui 	if (mag == 0x7f45)
    173       1.1  tsutsui 		return load_elf((uint8_t *)SDBOOT_SCRATCHADDR, entry);
    174       1.1  tsutsui 	else if (mag == ECOFF_MAGIC_MIPSEB)
    175       1.1  tsutsui 		return load_coff((uint8_t *)SDBOOT_SCRATCHADDR, entry);
    176       1.1  tsutsui 
    177       1.1  tsutsui 	return BERR_FILEMAGIC;
    178       1.1  tsutsui }
    179       1.1  tsutsui 
    180       1.1  tsutsui int
    181       1.1  tsutsui load_elf(uint8_t *buf, uint32_t *entry)
    182       1.1  tsutsui {
    183       1.1  tsutsui 	Elf32_Ehdr *e = (void *)buf;
    184       1.1  tsutsui 	Elf32_Phdr *p;
    185       1.1  tsutsui 
    186       1.1  tsutsui 	if (e->e_ident[EI_MAG2] != 'L' || e->e_ident[EI_MAG3] != 'F' ||
    187       1.1  tsutsui 	    e->e_ident[EI_CLASS] != ELFCLASS32 ||
    188       1.1  tsutsui 	    e->e_ident[EI_DATA] != ELFDATA2MSB ||
    189       1.1  tsutsui 	    e->e_type != ET_EXEC ||
    190       1.1  tsutsui 	    e->e_machine != EM_MIPS)
    191       1.1  tsutsui 		return BERR_ELFHDR;
    192       1.1  tsutsui 
    193       1.1  tsutsui 	BASSERT(e->e_phentsize == sizeof(Elf32_Phdr));
    194       1.1  tsutsui 	p = (void *)(buf + e->e_phoff);
    195       1.1  tsutsui #ifdef _STANDALONE
    196       1.1  tsutsui 	memcpy((void *)p->p_vaddr, buf + p->p_offset, p->p_filesz);
    197       1.1  tsutsui 	p++;
    198       1.1  tsutsui 	memcpy((void *)p->p_vaddr, buf + p->p_offset, p->p_filesz);
    199       1.1  tsutsui #else
    200       1.1  tsutsui 	DPRINTF("ELF entry point 0x%08x\n", e->e_entry);
    201       1.1  tsutsui 	DPRINTF("[text] 0x%08x 0x%x %dbyte.\n", p->p_vaddr, p->p_offset,
    202       1.1  tsutsui 	    p->p_filesz);
    203       1.1  tsutsui 	p++;
    204       1.1  tsutsui 	DPRINTF("[data] 0x%08x 0x%x %dbyte.\n", p->p_vaddr, p->p_offset,
    205       1.1  tsutsui 	    p->p_filesz);
    206       1.1  tsutsui #endif
    207       1.1  tsutsui 	*entry = e->e_entry;
    208       1.1  tsutsui 
    209       1.1  tsutsui 	return 0;
    210       1.1  tsutsui }
    211       1.1  tsutsui 
    212       1.1  tsutsui int
    213       1.1  tsutsui load_coff(uint8_t *p, uint32_t *entry)
    214       1.1  tsutsui {
    215       1.1  tsutsui 	struct ecoff_exechdr *eh = (void *)p;
    216       1.1  tsutsui 	struct ecoff_aouthdr *a = &eh->a;
    217       1.1  tsutsui 	struct ecoff_filehdr *f = &eh->f;
    218       1.1  tsutsui 
    219       1.1  tsutsui 	if (a->magic != ECOFF_OMAGIC)
    220       1.1  tsutsui 		return BERR_AOUTHDR;
    221       1.1  tsutsui 
    222       1.1  tsutsui 	p += N_TXTOFF(f, a);
    223       1.1  tsutsui 	DPRINTF("file offset = 0x%x\n", N_TXTOFF(f, a));
    224       1.1  tsutsui #ifdef _STANDALONE
    225       1.1  tsutsui 	memcpy((void *)a->text_start, p, a->tsize);
    226       1.1  tsutsui 	memcpy((void *)a->data_start, p + a->tsize, a->dsize);
    227       1.1  tsutsui #else
    228       1.1  tsutsui 	DPRINTF("COFF entry point 0x%08lx\n", a->entry);
    229       1.1  tsutsui 	DPRINTF("[text] 0x%08lx %ld byte.\n", a->text_start, a->tsize);
    230       1.1  tsutsui 	DPRINTF("[data] 0x%08lx %ld byte.\n", a->data_start, a->dsize);
    231       1.1  tsutsui #endif
    232       1.1  tsutsui 
    233       1.1  tsutsui 	*entry = a->entry;
    234       1.1  tsutsui 
    235       1.1  tsutsui 	return 0;
    236       1.1  tsutsui }
    237       1.1  tsutsui 
    238       1.1  tsutsui int
    239       1.1  tsutsui dk_read(int sector, int count, void *buf)
    240       1.1  tsutsui {
    241       1.1  tsutsui #ifdef _STANDALONE
    242       1.2  tsutsui 	int i, cnt;
    243       1.2  tsutsui 	uint32_t pos;
    244       1.1  tsutsui 	uint8_t *p = buf;
    245       1.1  tsutsui 
    246       1.1  tsutsui 	if (__dk_type == NVSRAM_BOOTDEV_HARDDISK) {	/* DISK */
    247       1.1  tsutsui 		if ((ROM_DK_READ(__dk_unit, sector, count, p) & 0x7f) != 0)
    248       1.1  tsutsui 			return 1;
    249       1.1  tsutsui 	} else {	/* FD */
    250       1.1  tsutsui 		for (i = 0; i < count; i++, p += 512) {
    251       1.1  tsutsui 			fd_position(sector + i, &pos, &cnt);
    252       1.1  tsutsui 			if ((ROM_FD_READ(__dk_unit, pos, cnt, p) & 0x7f) != 0)
    253       1.1  tsutsui 				return 1;
    254       1.1  tsutsui 		}
    255       1.1  tsutsui 	}
    256       1.1  tsutsui #else
    257       1.1  tsutsui 	sector_read_n(buf, sector, count);
    258       1.1  tsutsui #endif
    259       1.1  tsutsui 	return 0;
    260       1.1  tsutsui }
    261