Home | History | Annotate | Line # | Download | only in common
bootxx.c revision 1.5
      1  1.5  tsutsui /*	$NetBSD: bootxx.c,v 1.5 2009/02/04 15:22:13 tsutsui 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.5  tsutsui int __dk_unit, __dk_type, __spb;
     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.5  tsutsui 		if (fd_format == FD_FORMAT_2D) {
    110  1.5  tsutsui 			fd_position = blk_to_2d_position;
    111  1.5  tsutsui 			__spb = 2;		/* 256bytes/sector */
    112  1.5  tsutsui 		} else {
    113  1.1  tsutsui 			fd_position = blk_to_2hd_position;
    114  1.1  tsutsui 			__dk_unit |= 0x1000000;		/* | 2HD flag */
    115  1.5  tsutsui 			__spb = 1;
    116  1.1  tsutsui 		}
    117  1.1  tsutsui 	}
    118  1.1  tsutsui #else
    119  1.1  tsutsui 	printf("%s\n", msg);
    120  1.1  tsutsui 	sector_init("/dev/rsd1p", DEV_BSIZE);
    121  1.1  tsutsui 	sector_read(SDBOOT_PDINFOADDR, PDINFO_SECTOR);
    122  1.1  tsutsui #endif
    123  1.1  tsutsui 
    124  1.1  tsutsui 	for (boot = boottab; *boot; boot++) {
    125  1.1  tsutsui 		if ((err = loader(*boot, &entry)) == 0) {
    126  1.1  tsutsui #ifdef _STANDALONE
    127  1.1  tsutsui 			ROM_PUTC(0, 0, '\n');
    128  1.1  tsutsui 			ROM_PUTC(0, 0, '\r');
    129  1.1  tsutsui 			return entry;
    130  1.1  tsutsui #else
    131  1.1  tsutsui 			printf("entry=%p\n", (void *)entry);
    132  1.1  tsutsui 			sector_fini();
    133  1.1  tsutsui 			return 0;
    134  1.1  tsutsui #endif
    135  1.1  tsutsui 		}
    136  1.1  tsutsui 		p = errmsg[err];
    137  1.1  tsutsui #ifdef _STANDALONE
    138  1.1  tsutsui 		for (i = 0; p[i] != 0; i++)
    139  1.1  tsutsui 			ROM_PUTC(600 + i * 12, 536, p[i]);
    140  1.1  tsutsui #else
    141  1.1  tsutsui 		DPRINTF("***ERROR *** %s\n", p);
    142  1.1  tsutsui #endif
    143  1.1  tsutsui 	}
    144  1.1  tsutsui 
    145  1.1  tsutsui #ifdef _STANDALONE
    146  1.1  tsutsui 	while (/*CONSTCOND*/1)
    147  1.1  tsutsui 		;
    148  1.1  tsutsui 	/* NOTREACHED */
    149  1.1  tsutsui #else
    150  1.1  tsutsui 	sector_fini();
    151  1.1  tsutsui #endif
    152  1.1  tsutsui 	return 0;
    153  1.1  tsutsui }
    154  1.1  tsutsui 
    155  1.1  tsutsui int
    156  1.1  tsutsui loader(const char *fname, uint32_t *entry)
    157  1.1  tsutsui {
    158  1.1  tsutsui 	int err;
    159  1.1  tsutsui 	uint16_t mag;
    160  1.1  tsutsui 	size_t sz;
    161  1.1  tsutsui #ifdef _STANDALONE
    162  1.1  tsutsui 	int i;
    163  1.1  tsutsui 
    164  1.1  tsutsui 	for (i = 0; fname[i] != 0; i++)
    165  1.1  tsutsui 		ROM_PUTC(380 + i * 12, 536, fname[i]);
    166  1.1  tsutsui #else
    167  1.1  tsutsui 	printf("%s\n", fname);
    168  1.1  tsutsui #endif
    169  1.1  tsutsui 
    170  1.1  tsutsui 	if ((err = fileread(fname, &sz)) != 0)
    171  1.1  tsutsui 		return err;
    172  1.1  tsutsui 
    173  1.1  tsutsui 	DPRINTF("%s found. %d bytes.\n", fname, sz);
    174  1.1  tsutsui 
    175  1.1  tsutsui 	mag = *(uint16_t *)SDBOOT_SCRATCHADDR;
    176  1.1  tsutsui 	if (mag == 0x7f45)
    177  1.1  tsutsui 		return load_elf((uint8_t *)SDBOOT_SCRATCHADDR, entry);
    178  1.1  tsutsui 	else if (mag == ECOFF_MAGIC_MIPSEB)
    179  1.1  tsutsui 		return load_coff((uint8_t *)SDBOOT_SCRATCHADDR, entry);
    180  1.1  tsutsui 
    181  1.1  tsutsui 	return BERR_FILEMAGIC;
    182  1.1  tsutsui }
    183  1.1  tsutsui 
    184  1.1  tsutsui int
    185  1.1  tsutsui load_elf(uint8_t *buf, uint32_t *entry)
    186  1.1  tsutsui {
    187  1.1  tsutsui 	Elf32_Ehdr *e = (void *)buf;
    188  1.1  tsutsui 	Elf32_Phdr *p;
    189  1.1  tsutsui 
    190  1.1  tsutsui 	if (e->e_ident[EI_MAG2] != 'L' || e->e_ident[EI_MAG3] != 'F' ||
    191  1.1  tsutsui 	    e->e_ident[EI_CLASS] != ELFCLASS32 ||
    192  1.1  tsutsui 	    e->e_ident[EI_DATA] != ELFDATA2MSB ||
    193  1.1  tsutsui 	    e->e_type != ET_EXEC ||
    194  1.1  tsutsui 	    e->e_machine != EM_MIPS)
    195  1.1  tsutsui 		return BERR_ELFHDR;
    196  1.1  tsutsui 
    197  1.1  tsutsui 	BASSERT(e->e_phentsize == sizeof(Elf32_Phdr));
    198  1.1  tsutsui 	p = (void *)(buf + e->e_phoff);
    199  1.1  tsutsui #ifdef _STANDALONE
    200  1.1  tsutsui 	memcpy((void *)p->p_vaddr, buf + p->p_offset, p->p_filesz);
    201  1.1  tsutsui 	p++;
    202  1.1  tsutsui 	memcpy((void *)p->p_vaddr, buf + p->p_offset, p->p_filesz);
    203  1.1  tsutsui #else
    204  1.1  tsutsui 	DPRINTF("ELF entry point 0x%08x\n", e->e_entry);
    205  1.1  tsutsui 	DPRINTF("[text] 0x%08x 0x%x %dbyte.\n", p->p_vaddr, p->p_offset,
    206  1.1  tsutsui 	    p->p_filesz);
    207  1.1  tsutsui 	p++;
    208  1.1  tsutsui 	DPRINTF("[data] 0x%08x 0x%x %dbyte.\n", p->p_vaddr, p->p_offset,
    209  1.1  tsutsui 	    p->p_filesz);
    210  1.1  tsutsui #endif
    211  1.1  tsutsui 	*entry = e->e_entry;
    212  1.1  tsutsui 
    213  1.1  tsutsui 	return 0;
    214  1.1  tsutsui }
    215  1.1  tsutsui 
    216  1.1  tsutsui int
    217  1.1  tsutsui load_coff(uint8_t *p, uint32_t *entry)
    218  1.1  tsutsui {
    219  1.1  tsutsui 	struct ecoff_exechdr *eh = (void *)p;
    220  1.1  tsutsui 	struct ecoff_aouthdr *a = &eh->a;
    221  1.1  tsutsui 	struct ecoff_filehdr *f = &eh->f;
    222  1.1  tsutsui 
    223  1.1  tsutsui 	if (a->magic != ECOFF_OMAGIC)
    224  1.1  tsutsui 		return BERR_AOUTHDR;
    225  1.1  tsutsui 
    226  1.1  tsutsui 	p += N_TXTOFF(f, a);
    227  1.1  tsutsui 	DPRINTF("file offset = 0x%x\n", N_TXTOFF(f, a));
    228  1.1  tsutsui #ifdef _STANDALONE
    229  1.1  tsutsui 	memcpy((void *)a->text_start, p, a->tsize);
    230  1.1  tsutsui 	memcpy((void *)a->data_start, p + a->tsize, a->dsize);
    231  1.1  tsutsui #else
    232  1.1  tsutsui 	DPRINTF("COFF entry point 0x%08lx\n", a->entry);
    233  1.1  tsutsui 	DPRINTF("[text] 0x%08lx %ld byte.\n", a->text_start, a->tsize);
    234  1.1  tsutsui 	DPRINTF("[data] 0x%08lx %ld byte.\n", a->data_start, a->dsize);
    235  1.1  tsutsui #endif
    236  1.1  tsutsui 
    237  1.1  tsutsui 	*entry = a->entry;
    238  1.1  tsutsui 
    239  1.1  tsutsui 	return 0;
    240  1.1  tsutsui }
    241  1.1  tsutsui 
    242  1.1  tsutsui int
    243  1.1  tsutsui dk_read(int sector, int count, void *buf)
    244  1.1  tsutsui {
    245  1.1  tsutsui #ifdef _STANDALONE
    246  1.5  tsutsui 	int cnt;
    247  1.2  tsutsui 	uint32_t pos;
    248  1.1  tsutsui 	uint8_t *p = buf;
    249  1.1  tsutsui 
    250  1.1  tsutsui 	if (__dk_type == NVSRAM_BOOTDEV_HARDDISK) {	/* DISK */
    251  1.1  tsutsui 		if ((ROM_DK_READ(__dk_unit, sector, count, p) & 0x7f) != 0)
    252  1.1  tsutsui 			return 1;
    253  1.1  tsutsui 	} else {	/* FD */
    254  1.5  tsutsui 		while (count > 0) {
    255  1.5  tsutsui 			fd_position(sector, &pos, &cnt);
    256  1.5  tsutsui 			if (cnt > count)
    257  1.5  tsutsui 				cnt = count;
    258  1.5  tsutsui 			if ((ROM_FD_READ(__dk_unit, pos, cnt * __spb, p)
    259  1.5  tsutsui 			    & 0x7f) != 0)
    260  1.1  tsutsui 				return 1;
    261  1.5  tsutsui 			count -= cnt;
    262  1.5  tsutsui 			sector += cnt;
    263  1.5  tsutsui 			p += 512 * cnt;
    264  1.1  tsutsui 		}
    265  1.1  tsutsui 	}
    266  1.1  tsutsui #else
    267  1.1  tsutsui 	sector_read_n(buf, sector, count);
    268  1.1  tsutsui #endif
    269  1.1  tsutsui 	return 0;
    270  1.1  tsutsui }
    271