Home | History | Annotate | Line # | Download | only in bootxx
boot1.c revision 1.1.12.2
      1 /*	$NetBSD: boot1.c,v 1.1.12.2 2006/12/30 20:46:24 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by David Laight.
      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 #include <sys/cdefs.h>
     40 __RCSID("$NetBSD: boot1.c,v 1.1.12.2 2006/12/30 20:46:24 yamt Exp $");
     41 
     42 #include <lib/libsa/stand.h>
     43 #include <lib/libkern/libkern.h>
     44 
     45 #include <sys/param.h>
     46 #include <sys/bootblock.h>
     47 #include <sys/disklabel.h>
     48 #include <dev/raidframe/raidframevar.h>	/* For RF_PROTECTED_SECTORS */
     49 
     50 #include "biosdisk_ll.h"
     51 
     52 #define	XSTR(x)	#x
     53 #define	STR(x)	XSTR(x)
     54 
     55 static uint32_t bios_sector;
     56 
     57 const char *boot1(uint32_t *);
     58 void putstr(const char *str);
     59 int raise(int sig);
     60 
     61 extern struct disklabel ptn_disklabel;
     62 
     63 const char *
     64 boot1(uint32_t *sector)
     65 {
     66         struct stat sb;
     67 	int fd;
     68 
     69 	bios_sector = *sector;
     70 
     71 	putstr("\r\nNetBSD/" MACHINE " " STR(FS) " Primary Bootstrap\r\n");
     72 
     73 	do {
     74 		/*
     75 		 * We default to the filesystem at the start of the
     76 		 * MBR partition
     77 		 */
     78 		fd = open("boot", 0);
     79 		if (fd != -1)
     80 			break;
     81 
     82 		/*
     83 		 * Maybe the filesystem is enclosed in a raid set.
     84 		 * add in size of raidframe header and try again.
     85 		 * (Maybe this should only be done if the filesystem
     86 		 * magic number is absent.)
     87 		 */
     88 		bios_sector += RF_PROTECTED_SECTORS;
     89 		fd = open("boot", 0);
     90 		if (fd != -1)
     91 			break;
     92 
     93 		/*
     94 		 * Nothing at the start of the MBR partition, fallback on
     95 		 * partition 'a' from the disklabel in this MBR partition.
     96 		 */
     97 		if (ptn_disklabel.d_magic != DISKMAGIC)
     98 			break;
     99 		if (ptn_disklabel.d_magic2 != DISKMAGIC)
    100 			break;
    101 		if (ptn_disklabel.d_partitions[0].p_fstype == FS_UNUSED)
    102 			break;
    103 
    104 		bios_sector = ptn_disklabel.d_partitions[0].p_offset;
    105 		*sector = bios_sector;
    106 		if (ptn_disklabel.d_partitions[0].p_fstype == FS_RAID)
    107 			bios_sector += RF_PROTECTED_SECTORS;
    108 		fd = open("boot", 0);
    109 	} while (0);
    110 
    111 	if (fd == -1 || fstat(fd, &sb) == -1)
    112 		return "Can't open /boot.\r\n";
    113 
    114 #if 0
    115 	if (sb.st_size > SECONDARY_MAX_LOAD)
    116 		return "/boot too large.\r\n";
    117 #endif
    118 
    119 	if (read(fd, (void *)SECONDARY_LOAD_ADDRESS, sb.st_size) != sb.st_size)
    120 		return "/boot load failed.\r\n";
    121 
    122 	if (*(uint32_t *)(SECONDARY_LOAD_ADDRESS + 4) != LANDISK_BOOT_MAGIC_2)
    123 		return "Invalid /boot file format.\r\n";
    124 
    125 	return 0;
    126 }
    127 
    128 int
    129 blkdevstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize)
    130 {
    131 
    132 	if (flag != F_READ)
    133 		return EROFS;
    134 
    135 	if (size & (BIOSDISK_SECSIZE - 1))
    136 		return EINVAL;
    137 
    138 	if (rsize)
    139 		*rsize = size;
    140 
    141 	if (size != 0 && readsects(0x40, bios_sector + dblk, buf,
    142 				   size / BIOSDISK_SECSIZE) != 0)
    143 		return EIO;
    144 
    145 	return 0;
    146 }
    147 
    148 /*ARGUSED*/
    149 int
    150 raise(int sig)
    151 {
    152 
    153 	return 0;
    154 }
    155