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