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