xref: /minix3/sys/arch/i386/stand/bootxx/boot1.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1 /*	$NetBSD: boot1.c,v 1.20 2011/01/06 01:08:48 jakllsch 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: boot1.c,v 1.20 2011/01/06 01:08:48 jakllsch Exp $");
34 
35 #include <lib/libsa/stand.h>
36 #include <lib/libkern/libkern.h>
37 #include <biosdisk_ll.h>
38 
39 #include <sys/param.h>
40 #include <sys/bootblock.h>
41 #include <sys/disklabel.h>
42 #if !defined(__minix)
43 #include <dev/raidframe/raidframevar.h>	/* For RF_PROTECTED_SECTORS */
44 #else
45 #define RF_PROTECTED_SECTORS 64
46 #endif /* !defined(__minix) */
47 
48 #define XSTR(x) #x
49 #define STR(x) XSTR(x)
50 
51 static daddr_t bios_sector;
52 
53 static struct biosdisk_ll d;
54 
55 const char *boot1(uint32_t, uint64_t *);
56 extern void putstr(const char *);
57 
58 extern struct disklabel ptn_disklabel;
59 
60 static int
ob(void)61 ob(void)
62 {
63 #if !defined(__minix)
64 	return open("boot", 0);
65 #else
66 	return open("boot_monitor", 0);
67 #endif /* !defined(__minix) */
68 }
69 
70 const char *
boot1(uint32_t biosdev,uint64_t * sector)71 boot1(uint32_t biosdev, uint64_t *sector)
72 {
73 	struct stat sb;
74 	int fd;
75 
76 	bios_sector = *sector;
77 	d.dev = biosdev;
78 
79 #if defined(__minix)
80 	putstr("\r\nMINIX/x86 " STR(FS) " Primary Bootstrap\r\n");
81 #else
82 	putstr("\r\nNetBSD/x86 " STR(FS) " Primary Bootstrap\r\n");
83 #endif /* defined(__minix) */
84 
85 	if (set_geometry(&d, NULL))
86 		return "set_geometry\r\n";
87 
88 	/*
89 	 * We default to the filesystem at the start of the
90 	 * MBR partition
91 	 */
92 	fd = ob();
93 	if (fd != -1)
94 		goto done;
95 	/*
96 	 * Maybe the filesystem is enclosed in a raid set.
97 	 * add in size of raidframe header and try again.
98 	 * (Maybe this should only be done if the filesystem
99 	 * magic number is absent.)
100 	 */
101 	bios_sector += RF_PROTECTED_SECTORS;
102 	fd = ob();
103 	if (fd != -1)
104 		goto done;
105 
106 #if defined(__minix) && defined(BOOT_FROM_MINIXFS3)
107 	bios_sector -= RF_PROTECTED_SECTORS;
108 	bios_sector += MINIX3_FIRST_SUBP_OFFSET;
109 	*sector = bios_sector;
110 
111 	fd = ob();
112 	if (fd != -1)
113 		goto done;
114 #endif /* defined(__minix) && defined(BOOT_FROM_MINIXFS3) */
115 
116 	/*
117 	 * Nothing at the start of the MBR partition, fallback on
118 	 * partition 'a' from the disklabel in this MBR partition.
119 	 */
120 	if (ptn_disklabel.d_magic != DISKMAGIC ||
121 	    ptn_disklabel.d_magic2 != DISKMAGIC ||
122 	    ptn_disklabel.d_partitions[0].p_fstype == FS_UNUSED)
123 		goto done;
124 	bios_sector = ptn_disklabel.d_partitions[0].p_offset;
125 	*sector = bios_sector;
126 	if (ptn_disklabel.d_partitions[0].p_fstype == FS_RAID)
127 		bios_sector += RF_PROTECTED_SECTORS;
128 
129 	fd = ob();
130 
131 done:
132 	/* if we fail here, so will fstat, so keep going */
133 	if (fd == -1 || fstat(fd, &sb) == -1)
134 #if !defined(__minix)
135 		return "Can't open /boot\r\n";
136 #else
137 		return "Can't open /boot_monitor\r\n";
138 #endif /* !defined(__minix) */
139 
140 	biosdev = (uint32_t)sb.st_size;
141 #if 0
142 	if (biosdev > SECONDARY_MAX_LOAD)
143 		return "/boot too large\r\n";
144 #endif
145 
146 	if (read(fd, (void *)SECONDARY_LOAD_ADDRESS, biosdev) != biosdev)
147 #if !defined(__minix)
148 		return "/boot load failed\r\n";
149 #else
150 		return "/boot_monitor load failed\r\n";
151 #endif /* !defined(__minix) */
152 
153 	if (*(uint32_t *)(SECONDARY_LOAD_ADDRESS + 4) != X86_BOOT_MAGIC_2)
154 #if !defined(__minix)
155 		return "Invalid /boot file format\r\n";
156 #else
157 		return "Invalid /boot_monitor file format\r\n";
158 #endif /* !defined(__minix) */
159 
160 	/* We need to jump to the secondary bootstrap in realmode */
161 	return 0;
162 }
163 
164 int
blkdevstrategy(void * devdata,int flag,daddr_t dblk,size_t size,void * buf,size_t * rsize)165 blkdevstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize)
166 {
167 	if (flag != F_READ)
168 		return EROFS;
169 
170 	if (size & (BIOSDISK_DEFAULT_SECSIZE - 1))
171 		return EINVAL;
172 
173 	if (rsize)
174 		*rsize = size;
175 
176 	if (size != 0 && readsects(&d, bios_sector + dblk,
177 				   size / BIOSDISK_DEFAULT_SECSIZE,
178 				   buf, 1) != 0)
179 		return EIO;
180 
181 	return 0;
182 }
183