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