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