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