boot1.c revision 1.1 1 1.1 uwe /* $NetBSD: boot1.c,v 1.1 2006/09/01 21:26:19 uwe Exp $ */
2 1.1 uwe
3 1.1 uwe /*-
4 1.1 uwe * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.1 uwe * All rights reserved.
6 1.1 uwe *
7 1.1 uwe * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uwe * by David Laight.
9 1.1 uwe *
10 1.1 uwe * Redistribution and use in source and binary forms, with or without
11 1.1 uwe * modification, are permitted provided that the following conditions
12 1.1 uwe * are met:
13 1.1 uwe * 1. Redistributions of source code must retain the above copyright
14 1.1 uwe * notice, this list of conditions and the following disclaimer.
15 1.1 uwe * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uwe * notice, this list of conditions and the following disclaimer in the
17 1.1 uwe * documentation and/or other materials provided with the distribution.
18 1.1 uwe * 3. All advertising materials mentioning features or use of this software
19 1.1 uwe * must display the following acknowledgement:
20 1.1 uwe * This product includes software developed by the NetBSD
21 1.1 uwe * Foundation, Inc. and its contributors.
22 1.1 uwe * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 uwe * contributors may be used to endorse or promote products derived
24 1.1 uwe * from this software without specific prior written permission.
25 1.1 uwe *
26 1.1 uwe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 uwe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 uwe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 uwe * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 uwe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 uwe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 uwe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 uwe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 uwe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 uwe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 uwe * POSSIBILITY OF SUCH DAMAGE.
37 1.1 uwe */
38 1.1 uwe
39 1.1 uwe #include <sys/cdefs.h>
40 1.1 uwe __RCSID("$NetBSD: boot1.c,v 1.1 2006/09/01 21:26:19 uwe Exp $");
41 1.1 uwe
42 1.1 uwe #include <lib/libsa/stand.h>
43 1.1 uwe #include <lib/libkern/libkern.h>
44 1.1 uwe
45 1.1 uwe #include <sys/param.h>
46 1.1 uwe #include <sys/bootblock.h>
47 1.1 uwe #include <sys/disklabel.h>
48 1.1 uwe #include <dev/raidframe/raidframevar.h> /* For RF_PROTECTED_SECTORS */
49 1.1 uwe
50 1.1 uwe #include "biosdisk_ll.h"
51 1.1 uwe
52 1.1 uwe #define XSTR(x) #x
53 1.1 uwe #define STR(x) XSTR(x)
54 1.1 uwe
55 1.1 uwe static uint32_t bios_sector;
56 1.1 uwe
57 1.1 uwe const char *boot1(uint32_t *);
58 1.1 uwe void putstr(const char *str);
59 1.1 uwe int raise(int sig);
60 1.1 uwe
61 1.1 uwe extern struct disklabel ptn_disklabel;
62 1.1 uwe
63 1.1 uwe const char *
64 1.1 uwe boot1(uint32_t *sector)
65 1.1 uwe {
66 1.1 uwe struct stat sb;
67 1.1 uwe int fd;
68 1.1 uwe
69 1.1 uwe bios_sector = *sector;
70 1.1 uwe
71 1.1 uwe putstr("\r\nNetBSD/" MACHINE " " STR(FS) " Primary Bootstrap\r\n");
72 1.1 uwe
73 1.1 uwe do {
74 1.1 uwe /*
75 1.1 uwe * We default to the filesystem at the start of the
76 1.1 uwe * MBR partition
77 1.1 uwe */
78 1.1 uwe fd = open("boot", 0);
79 1.1 uwe if (fd != -1)
80 1.1 uwe break;
81 1.1 uwe
82 1.1 uwe /*
83 1.1 uwe * Maybe the filesystem is enclosed in a raid set.
84 1.1 uwe * add in size of raidframe header and try again.
85 1.1 uwe * (Maybe this should only be done if the filesystem
86 1.1 uwe * magic number is absent.)
87 1.1 uwe */
88 1.1 uwe bios_sector += RF_PROTECTED_SECTORS;
89 1.1 uwe fd = open("boot", 0);
90 1.1 uwe if (fd != -1)
91 1.1 uwe break;
92 1.1 uwe
93 1.1 uwe /*
94 1.1 uwe * Nothing at the start of the MBR partition, fallback on
95 1.1 uwe * partition 'a' from the disklabel in this MBR partition.
96 1.1 uwe */
97 1.1 uwe if (ptn_disklabel.d_magic != DISKMAGIC)
98 1.1 uwe break;
99 1.1 uwe if (ptn_disklabel.d_magic2 != DISKMAGIC)
100 1.1 uwe break;
101 1.1 uwe if (ptn_disklabel.d_partitions[0].p_fstype == FS_UNUSED)
102 1.1 uwe break;
103 1.1 uwe
104 1.1 uwe bios_sector = ptn_disklabel.d_partitions[0].p_offset;
105 1.1 uwe *sector = bios_sector;
106 1.1 uwe if (ptn_disklabel.d_partitions[0].p_fstype == FS_RAID)
107 1.1 uwe bios_sector += RF_PROTECTED_SECTORS;
108 1.1 uwe fd = open("boot", 0);
109 1.1 uwe } while (0);
110 1.1 uwe
111 1.1 uwe if (fd == -1 || fstat(fd, &sb) == -1)
112 1.1 uwe return "Can't open /boot.\r\n";
113 1.1 uwe
114 1.1 uwe #if 0
115 1.1 uwe if (sb.st_size > SECONDARY_MAX_LOAD)
116 1.1 uwe return "/boot too large.\r\n";
117 1.1 uwe #endif
118 1.1 uwe
119 1.1 uwe if (read(fd, (void *)SECONDARY_LOAD_ADDRESS, sb.st_size) != sb.st_size)
120 1.1 uwe return "/boot load failed.\r\n";
121 1.1 uwe
122 1.1 uwe if (*(uint32_t *)(SECONDARY_LOAD_ADDRESS + 4) != LANDISK_BOOT_MAGIC_2)
123 1.1 uwe return "Invalid /boot file format.\r\n";
124 1.1 uwe
125 1.1 uwe return 0;
126 1.1 uwe }
127 1.1 uwe
128 1.1 uwe int
129 1.1 uwe blkdevstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize)
130 1.1 uwe {
131 1.1 uwe
132 1.1 uwe if (flag != F_READ)
133 1.1 uwe return EROFS;
134 1.1 uwe
135 1.1 uwe if (size & (BIOSDISK_SECSIZE - 1))
136 1.1 uwe return EINVAL;
137 1.1 uwe
138 1.1 uwe if (rsize)
139 1.1 uwe *rsize = size;
140 1.1 uwe
141 1.1 uwe if (size != 0 && readsects(0x40, bios_sector + dblk, buf,
142 1.1 uwe size / BIOSDISK_SECSIZE) != 0)
143 1.1 uwe return EIO;
144 1.1 uwe
145 1.1 uwe return 0;
146 1.1 uwe }
147 1.1 uwe
148 1.1 uwe /*ARGUSED*/
149 1.1 uwe int
150 1.1 uwe raise(int sig)
151 1.1 uwe {
152 1.1 uwe
153 1.1 uwe return 0;
154 1.1 uwe }
155