xref: /netbsd-src/sys/arch/i386/stand/bootxx/boot1.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: boot1.c,v 1.1 2003/04/16 22:17:44 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.1 2003/04/16 22:17:44 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 
49 static uint32_t bios_dev;
50 static uint32_t bios_sector;
51 
52 struct biosdisk_ll d;
53 
54 const char *boot1(uint32_t biosdev, uint32_t sector);
55 extern void putstr(const char *);
56 
57 const char *
58 boot1(uint32_t biosdev, uint32_t sector)
59 {
60         struct stat sb;
61 	int fd;
62 
63 	bios_sector = sector;
64 	bios_dev = biosdev;
65 	d.dev = biosdev;
66 
67         putstr("\r\nNetBSD/i386 Primary Bootstrap\r\n");
68 
69 	if (set_geometry(&d, NULL))
70 		return "set_geometry\r\n";
71 
72 	fd = open("boot", 0);
73 	if (fd == -1 || fstat(fd, &sb) == -1)
74 		return "Can't open /boot.\r\n";
75 
76 #if 0
77 	if (sb.st_size > SECONDARY_MAX_LOAD)
78 		return "/boot too large.\r\n";
79 #endif
80 
81 	if (read(fd, (void *)SECONDARY_LOAD_ADDRESS, sb.st_size) != sb.st_size)
82 		return "/boot load failed.\r\n";
83 
84 	if (*(uint32_t *)(SECONDARY_LOAD_ADDRESS + 4) != X86_BOOT_MAGIC_2)
85 		return "Invalid /boot file format.\r\n";
86 
87 	/* We need to jump to the secondary bootstrap in realmode */
88 	return 0;
89 }
90 
91 int
92 blkdevstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize)
93 {
94 	if (flag != F_READ)
95 		return EROFS;
96 
97 	if (size & (BIOSDISK_SECSIZE - 1))
98 		return EINVAL;
99 
100 	if (rsize)
101 		*rsize = size;
102 
103 	if (size != 0 && readsects(&d, bios_sector + dblk,
104 				   size / BIOSDISK_SECSIZE, buf, 1) != 0)
105 		return EIO;
106 
107 	return 0;
108 }
109