Home | History | Annotate | Line # | Download | only in boot_ufs
bootmain.c revision 1.10
      1 /*	$NetBSD: bootmain.c,v 1.10 2009/03/14 15:36:15 dsl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994 Takumi Nakamura.
      5  * Copyright (c) 1999, 2000 Itoh Yasufumi.
      6  * Copyright (c) 2001 Minoura Makoto.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Takumi Nakamura.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/param.h>
     37 #include <sys/reboot.h>
     38 #include <sys/types.h>
     39 #include <sys/exec_aout.h>
     40 #include <ufs/ufs/dinode.h>
     41 #include <ufs/ffs/fs.h>
     42 #include <ufs/ufs/dir.h>
     43 #include <machine/cpu.h>
     44 #include <machine/bootinfo.h>
     45 #ifdef SCSI_ADHOC_BOOTPART
     46 #include <machine/disklabel.h>
     47 #endif
     48 #include <lib/libsa/stand.h>
     49 
     50 #include "boot_ufs.h"
     51 #include "readufs.h"
     52 #include "exec_image.h"
     53 #include "../../x68k/iodevice.h"
     54 #define IODEVbase ((volatile struct IODEVICE *)INTIOBASE)
     55 
     56 /* for debug;  */
     57 unsigned int startregs[16];
     58 
     59 #ifdef SCSI_ADHOC_BOOTPART
     60 static int get_scsi_part (void);
     61 #endif
     62 #ifdef BOOT_DEBUG
     63 static int get_scsi_host_adapter (char *);
     64 #else
     65 static int get_scsi_host_adapter (void);
     66 #endif
     67 
     68 #ifdef BOOT_DEBUG
     69 void print_hex (unsigned int, int);
     70 #endif
     71 
     72 static int load_file (const char*, unsigned int, struct exec *);
     73 static int load_file_ino (ino32_t, const char*, unsigned int, struct exec *);
     74 
     75 void bootufs (void) __attribute__ ((__noreturn__));
     76 
     77 #ifdef BOOT_DEBUG
     78 void
     79 print_hex(x, l)
     80 	unsigned int x;	/*  */
     81 	int l;		/*  */
     82 {
     83 
     84 	if (l > 0) {
     85 		print_hex(x >> 4, l - 1);
     86 		x &= 0x0F;
     87 		if (x > 9)
     88 			x += 7;
     89 		B_PUTC((unsigned int) '0' + x);
     90 	}
     91 }
     92 #endif
     93 
     94 #ifdef SCSI_ADHOC_BOOTPART
     95 /*
     96  * get partition # from partition start position
     97  */
     98 
     99 #define NPART		15
    100 #define PARTTBL_TOP	((unsigned)4)	/* pos of part inf in 512byte-blocks */
    101 #define MAXPART		6
    102 const unsigned char partition_conv[MAXPART + 1] = { 0, 1, 3, 4, 5, 6, 7 };
    103 
    104 static int
    105 get_scsi_part()
    106 {
    107 	struct {
    108 		u_int32_t	magic;		/* 0x5836384B ("X68K") */
    109 		u_int32_t	parttotal;
    110 		u_int32_t	diskblocks;
    111 		u_int32_t	diskblocks2;	/* backup? */
    112 		struct dos_partition parttbl[NPART];
    113 		unsigned char	formatstr[256];
    114 		unsigned char	rest[512];
    115 	} partbuf;
    116 	int i;
    117 	u_int32_t part_top;
    118 
    119 #ifdef BOOT_DEBUG
    120 	B_PRINT("seclen: ");
    121 	print_hex(SCSI_BLKLEN, 8);	/* 0: 256, 1: 512, 2: 1024 */
    122 	B_PRINT(", topsec: ");
    123 	print_hex(SCSI_PARTTOP, 8);	/* partition top in sector */
    124 #endif
    125 	/*
    126 	 * read partition table
    127 	 */
    128 	RAW_READ0(&partbuf, PARTTBL_TOP, sizeof partbuf);
    129 
    130 	part_top = SCSI_PARTTOP >> (2 - SCSI_BLKLEN);
    131 	for (i = 0; i < MAXPART; i++)
    132 		if ((u_int32_t) partbuf.parttbl[i].dp_start == part_top)
    133 			goto found;
    134 
    135 	BOOT_ERROR("Can't boot from this partition");
    136 	/* NOTREACHED */
    137 found:
    138 #ifdef BOOT_DEBUG
    139 	B_PRINT("; sd");
    140 	B_PUTC(ID + '0');	/* SCSI ID (not NetBSD unit #) */
    141 	B_PUTC((unsigned int) partition_conv[i] + 'a');
    142 	B_PRINT("\r\n");
    143 #endif
    144 	return partition_conv[i];
    145 }
    146 #endif	/* SCSI_ADHOC_BOOTPART */
    147 
    148 /*
    149  * Check the type of SCSI interface
    150  */
    151 #ifdef BOOT_DEBUG
    152 static int
    153 get_scsi_host_adapter(devstr)
    154 	char *devstr;
    155 #else
    156 static int
    157 get_scsi_host_adapter(void)
    158 #endif
    159 {
    160 	char *bootrom;
    161 	int ha;
    162 
    163 #ifdef BOOT_DEBUG
    164 	B_PRINT(" at ");
    165 	*(int *)devstr = '/' << 24 | 's' << 16 | 'p' << 8 | 'c';
    166 	*(int *)(devstr + 4) = '@' << 24 | '0' << 16 | '/' << 8 | 's';
    167 	*(int *)(devstr + 8) = 'd' << 24 | '@' << 16 | '0' << 8 | ',';
    168 	*(int *)(devstr + 12) = '0' << 24 | ':' << 16 | 'a' << 8 | '\0';
    169 #endif
    170 
    171 	bootrom = (char *) (BOOT_INFO & 0x00ffffe0);
    172 	/*
    173 	 * bootrom+0x24	"SCSIIN" ... Internal SCSI (spc@0)
    174 	 *		"SCSIEX" ... External SCSI (spc@1 or mha@0)
    175 	 */
    176 	if (*(u_short *)(bootrom + 0x24 + 4) == 0x494e) {	/* "IN" */
    177 #ifdef BOOT_DEBUG
    178 		B_PRINT("spc0");
    179 #endif
    180 		ha = (X68K_BOOT_SCSIIF_SPC << 4) | 0;
    181 	} else if (badbaddr(&IODEVbase->io_exspc.bdid)) {
    182 #ifdef BOOT_DEBUG
    183 		B_PRINT("mha0");
    184 #endif
    185 		ha = (X68K_BOOT_SCSIIF_MHA << 4) | 0;
    186 #ifdef BOOT_DEBUG
    187 		*(int *)devstr = '/' << 24 | 'm' << 16 | 'h' << 8 | 'a';
    188 #endif
    189 	} else {
    190 #ifdef BOOT_DEBUG
    191 		B_PRINT("spc1");
    192 #endif
    193 		ha = (X68K_BOOT_SCSIIF_SPC << 4) | 1;
    194 #ifdef BOOT_DEBUG
    195 		devstr[5] = '1';
    196 #endif
    197 	}
    198 
    199 	return ha;
    200 }
    201 
    202 static int
    203 load_file(const char *path, unsigned int addr, struct exec *header)
    204 {
    205 
    206 	return load_file_ino(ufs_lookup_path(path), path, addr, header);
    207 }
    208 
    209 static int
    210 load_file_ino(ino, fn, addr, header)
    211 	ino32_t ino;
    212 	const char *fn;		/* for message only */
    213 	unsigned int addr;
    214 	struct exec *header;
    215 {
    216 	union ufs_dinode dinode;
    217 
    218 	/* look-up the file */
    219 	if (ino == 0 || ufs_get_inode(ino, &dinode)) {
    220 		B_PRINT(fn);
    221 		B_PRINT(": not found\r\n");
    222 		return 0;
    223 	}
    224 
    225 	ufs_read(&dinode, (void *)addr, 0, sizeof(struct exec));
    226 	memcpy(header, (void *)addr, sizeof(struct exec));
    227 
    228 	if ((N_GETMAGIC(*header) != OMAGIC) ||
    229 	    (N_GETMID(*header) != MID_M68K)) {
    230 		B_PRINT(fn);
    231 		B_PRINT(": inappropriate format");
    232 		return 0;
    233 	}
    234 
    235 	/* read text and data */
    236 	ufs_read(&dinode, ((char *)addr)-sizeof(struct exec), 0,/* XXX */
    237 		 header->a_text+header->a_data);
    238 
    239 	/* clear out bss */
    240 	memset((char*) addr + header->a_text+header->a_data,
    241 	       0, header->a_bss);
    242 
    243 	/* PLANNED: fallback NMAGIC loader for the kernel. */
    244 
    245 	/* return the image size. */
    246 	return header->a_text+header->a_data+header->a_bss;
    247 }
    248 
    249 
    250 void
    251 bootufs(void)
    252 {
    253 	int bootdev;
    254 #ifdef BOOT_DEBUG
    255 	int i;
    256 	char bootdevstr[16];
    257 #endif
    258 	struct exec header;
    259 	int size;
    260 	extern const char bootprog_name[], bootprog_rev[];
    261 
    262 #ifdef BOOT_DEBUG
    263 	/* for debug;  */
    264 	for (i = 0; i < 16; i++) {
    265 		print_hex(startregs[i], 8);
    266 		B_PRINT((i & 7) == 7 ? "\r\n" : " ");
    267 	}
    268 #endif
    269 
    270 	B_PRINT(bootprog_name);
    271 	B_PRINT(" rev.");	B_PRINT(bootprog_rev);
    272 	B_PRINT("\r\n");
    273 
    274 	/*
    275 	 * get boot device
    276 	 */
    277 	if (BINF_ISFD(&BOOT_INFO)) {
    278 		/* floppy */
    279 #ifdef BOOT_DEBUG
    280 		*(int *)bootdevstr = ('f' << 24 | 'd' << 16 | '@' << 8 | '0') +
    281 					(BOOT_INFO & 3);
    282 		bootdevstr[4] = '\0';
    283 #endif
    284 		bootdev = X68K_MAKEBOOTDEV(X68K_MAJOR_FD, BOOT_INFO & 3,
    285 				(FDSECMINMAX.minsec.N == 3) ? 0 : 2);
    286 	} else {
    287 		/* SCSI */
    288 		int part, ha;
    289 
    290 #ifdef SCSI_ADHOC_BOOTPART
    291 		if (SCSI_PARTTOP == 0)
    292 			part = 0;
    293 		else
    294 			part = get_scsi_part();
    295 #else
    296 		part = 0;			/* sd?a only */
    297 #endif
    298 #ifndef BOOT_DEBUG
    299 		ha = get_scsi_host_adapter();
    300 #else
    301 		ha = get_scsi_host_adapter(bootdevstr);
    302 		bootdevstr[10] = '0' + (ID & 7);
    303 		bootdevstr[14] = 'a' + part;
    304 #endif
    305 		bootdev = X68K_MAKESCSIBOOTDEV(X68K_MAJOR_SD, ha >> 4, ha & 15,
    306 						ID & 7, 0, part);
    307 	}
    308 #ifdef BOOT_DEBUG
    309 	B_PRINT("boot device: ");
    310 	B_PRINT(bootdevstr);
    311 #endif
    312 	B_PRINT("\r\n");
    313 
    314 	/* initialize filesystem code */
    315 	if (ufs_init()) {
    316 		BOOT_ERROR("bogus super block: "
    317 			   "");
    318 		/* NOTREACHED */
    319 	}
    320 #if defined(BOOT_DEBUG) && defined(USE_FFS) && defined(USE_LFS)
    321 	B_PRINT("file system: ");
    322 	B_PUTC(ufs_info.fstype == UFSTYPE_FFS ?
    323 			(unsigned int) 'F' : (unsigned int) 'L');
    324 	B_PRINT("FS\r\n");
    325 #endif
    326 
    327 #ifdef BOOT_DEBUG
    328 	B_PRINT("\r\nlooking up secondary boot... ");
    329 #endif
    330 
    331 	/*
    332 	 * Look for the 2nd stage boot.
    333 	 */
    334 
    335 	/* Try "boot" first */
    336 	size = load_file("boot", BOOT_TEXTADDR, &header);
    337 #ifdef BOOT_DEBUG
    338 	B_PRINT("done.\r\n");
    339 #endif
    340 	if (size > 0)
    341 		exec_image(BOOT_TEXTADDR, /* image loaded at */
    342 			   BOOT_TEXTADDR, /* image executed at */
    343 			   header.a_entry, /* entry point */
    344 			   size, /* image size */
    345 			   bootdev, RB_SINGLE); /* arguments */
    346 
    347 	B_PRINT("can't load the secondary bootstrap.;"
    348 		"trying /netbsd...\r\n");
    349 
    350 	/* fallback to /netbsd. */
    351 	/* always fails since NMAGIC loader is not yet implemented. */
    352 
    353 	size = load_file("netbsd", 0x6000, &header);
    354 	if (size > 0) {
    355 		if (*((short *)(0x6000 + header.a_entry - 2)) != 0) {
    356 			B_PRINT("boot interface of /netbsd is too new!\r\n");
    357 			goto fail;
    358 		}
    359 		exec_image(0x6000, /* image loaded at */
    360 			   0,	/* image executed at */
    361 			   header.a_entry, /* entry point */
    362 			   size, /* image size */
    363 			   bootdev, RB_SINGLE); /* arguments */
    364 		/* NOTREACHED */
    365 	}
    366 
    367  fail:
    368 	BOOT_ERROR("can't load the secondary bootstrap nor the kernel.");
    369 	/* NOTREACHED */
    370 }
    371