1 /* $NetBSD: boot1.c,v 1.15 2007/10/17 19:54:59 garbled 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.15 2007/10/17 19:54:59 garbled 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/x86 " 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 done; 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 done; 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 done; 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 115 done: 116 /* if we fail here, so will fstat, so keep going */ 117 if (fstat(fd, &sb) == -1) { 118 return "Can't open /boot\r\n"; 119 } 120 121 biosdev = (uint32_t)sb.st_size; 122 #if 0 123 if (biosdev > SECONDARY_MAX_LOAD) 124 return "/boot too large\r\n"; 125 #endif 126 127 if (read(fd, (void *)SECONDARY_LOAD_ADDRESS, biosdev) != biosdev) 128 return "/boot load failed\r\n"; 129 130 if (*(uint32_t *)(SECONDARY_LOAD_ADDRESS + 4) != X86_BOOT_MAGIC_2) 131 return "Invalid /boot file format\r\n"; 132 133 /* We need to jump to the secondary bootstrap in realmode */ 134 return 0; 135 } 136 137 int 138 blkdevstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize) 139 { 140 if (flag != F_READ) 141 return EROFS; 142 143 if (size & (BIOSDISK_DEFAULT_SECSIZE - 1)) 144 return EINVAL; 145 146 if (rsize) 147 *rsize = size; 148 149 if (size != 0 && readsects(&d, bios_sector + dblk, 150 size / BIOSDISK_DEFAULT_SECSIZE, 151 buf, 1) != 0) 152 return EIO; 153 154 return 0; 155 } 156