dkwedge_bsdlabel.c revision 1.1.2.2 1 1.1.2.2 skrll /* $NetBSD: dkwedge_bsdlabel.c,v 1.1.2.2 2004/10/19 15:56:45 skrll Exp $ */
2 1.1.2.2 skrll
3 1.1.2.2 skrll /*-
4 1.1.2.2 skrll * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 1.1.2.2 skrll * All rights reserved.
6 1.1.2.2 skrll *
7 1.1.2.2 skrll * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 skrll * by Jason R. Thorpe.
9 1.1.2.2 skrll *
10 1.1.2.2 skrll * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 skrll * modification, are permitted provided that the following conditions
12 1.1.2.2 skrll * are met:
13 1.1.2.2 skrll * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 skrll * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 skrll * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 skrll * documentation and/or other materials provided with the distribution.
18 1.1.2.2 skrll * 3. All advertising materials mentioning features or use of this software
19 1.1.2.2 skrll * must display the following acknowledgement:
20 1.1.2.2 skrll * This product includes software developed by the NetBSD
21 1.1.2.2 skrll * Foundation, Inc. and its contributors.
22 1.1.2.2 skrll * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.2 skrll * contributors may be used to endorse or promote products derived
24 1.1.2.2 skrll * from this software without specific prior written permission.
25 1.1.2.2 skrll *
26 1.1.2.2 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.2 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.2 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.2 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.2 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.2 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.2 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.2 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.2 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.2 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.2 skrll * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.2 skrll */
38 1.1.2.2 skrll
39 1.1.2.2 skrll /*
40 1.1.2.2 skrll * Adapted from kern/subr_disk_mbr.c:
41 1.1.2.2 skrll *
42 1.1.2.2 skrll * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
43 1.1.2.2 skrll * All rights reserved.
44 1.1.2.2 skrll *
45 1.1.2.2 skrll * Redistribution and use in source and binary forms, with or without
46 1.1.2.2 skrll * modification, are permitted provided that the following conditions
47 1.1.2.2 skrll * are met:
48 1.1.2.2 skrll * 1. Redistributions of source code must retain the above copyright
49 1.1.2.2 skrll * notice, this list of conditions and the following disclaimer.
50 1.1.2.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
51 1.1.2.2 skrll * notice, this list of conditions and the following disclaimer in the
52 1.1.2.2 skrll * documentation and/or other materials provided with the distribution.
53 1.1.2.2 skrll * 3. Neither the name of the University nor the names of its contributors
54 1.1.2.2 skrll * may be used to endorse or promote products derived from this software
55 1.1.2.2 skrll * without specific prior written permission.
56 1.1.2.2 skrll *
57 1.1.2.2 skrll * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 1.1.2.2 skrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 1.1.2.2 skrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 1.1.2.2 skrll * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 1.1.2.2 skrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 1.1.2.2 skrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 1.1.2.2 skrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 1.1.2.2 skrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 1.1.2.2 skrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 1.1.2.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 1.1.2.2 skrll * SUCH DAMAGE.
68 1.1.2.2 skrll *
69 1.1.2.2 skrll * @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91
70 1.1.2.2 skrll */
71 1.1.2.2 skrll
72 1.1.2.2 skrll /*
73 1.1.2.2 skrll * 4.4BSD disklabel support for disk wedges
74 1.1.2.2 skrll *
75 1.1.2.2 skrll * Here is the basic search algorithm in use here:
76 1.1.2.2 skrll *
77 1.1.2.2 skrll * For historical reasons, we scan for x86-style MBR partitions looking
78 1.1.2.2 skrll * for a MBR_PTYPE_NETBSD (or MBR_PTYPE_386BSD) partition. The first
79 1.1.2.2 skrll * 4.4BSD disklabel found in the 2nd sector of such a partition is used.
80 1.1.2.2 skrll * We assume that the 4.4BSD disklabel describes all partitions on the
81 1.1.2.2 skrll * disk; we do not use any partition information from the MBR partition
82 1.1.2.2 skrll * table.
83 1.1.2.2 skrll *
84 1.1.2.2 skrll * If that fails, then we fall back on a table of known locations for
85 1.1.2.2 skrll * various platforms.
86 1.1.2.2 skrll */
87 1.1.2.2 skrll
88 1.1.2.2 skrll #include <sys/cdefs.h>
89 1.1.2.2 skrll __KERNEL_RCSID(0, "$NetBSD: dkwedge_bsdlabel.c,v 1.1.2.2 2004/10/19 15:56:45 skrll Exp $");
90 1.1.2.2 skrll
91 1.1.2.2 skrll #include <sys/param.h>
92 1.1.2.2 skrll #include <sys/systm.h>
93 1.1.2.2 skrll #include <sys/proc.h>
94 1.1.2.2 skrll #include <sys/errno.h>
95 1.1.2.2 skrll #include <sys/disk.h>
96 1.1.2.2 skrll #include <sys/vnode.h>
97 1.1.2.2 skrll #include <sys/malloc.h>
98 1.1.2.2 skrll
99 1.1.2.2 skrll #include <sys/bootblock.h>
100 1.1.2.2 skrll #include <sys/disklabel.h>
101 1.1.2.2 skrll
102 1.1.2.2 skrll #define BSD44_MBR_LABELSECTOR 1
103 1.1.2.2 skrll
104 1.1.2.2 skrll #define DISKLABEL_SIZE(x) \
105 1.1.2.2 skrll (offsetof(struct disklabel, d_partitions) + \
106 1.1.2.2 skrll (sizeof(struct partition) * (x)))
107 1.1.2.2 skrll
108 1.1.2.2 skrll /*
109 1.1.2.2 skrll * Note the smallest MAXPARTITIONS was 8, so we allow a disklabel
110 1.1.2.2 skrll * that size to be locted at the end of the sector.
111 1.1.2.2 skrll */
112 1.1.2.2 skrll #define DISKLABEL_MINSIZE DISKLABEL_SIZE(8)
113 1.1.2.2 skrll
114 1.1.2.2 skrll /*
115 1.1.2.2 skrll * Table of known platform-specific disklabel locations.
116 1.1.2.2 skrll */
117 1.1.2.2 skrll static const struct disklabel_location {
118 1.1.2.2 skrll daddr_t label_sector; /* sector containing label */
119 1.1.2.2 skrll size_t label_offset; /* byte offset of label in sector */
120 1.1.2.2 skrll } disklabel_locations[] = {
121 1.1.2.2 skrll { 0, 0 }, /* mvme68k, next68k */
122 1.1.2.2 skrll { 0, 64 }, /* algor, alpha, amiga, amigappc, evbmips, evbppc,
123 1.1.2.2 skrll luna68k, mac68k, macppc, news68k, newsmips,
124 1.1.2.2 skrll pc532, pdp11, pmax, vax, x68k */
125 1.1.2.2 skrll { 0, 128 }, /* sparc, sun68k */
126 1.1.2.2 skrll { 1, 0 }, /* amd64, arc, arm, bebox, cobalt, evbppc, hp700,
127 1.1.2.2 skrll hpcarm, hpcmips, i386, ibmnws, mipsco, mvmeppc,
128 1.1.2.2 skrll ofppc, playstation2, pmppc, prep, sandpoint,
129 1.1.2.2 skrll sbmips, sgimips, sh3, sh5 */
130 1.1.2.2 skrll /* XXX atari is weird */
131 1.1.2.2 skrll { 2, 0 }, /* cesfic, hp300 */
132 1.1.2.2 skrll
133 1.1.2.2 skrll { -1, 0 },
134 1.1.2.2 skrll };
135 1.1.2.2 skrll
136 1.1.2.2 skrll #define SCAN_CONTINUE 0
137 1.1.2.2 skrll #define SCAN_FOUND 1
138 1.1.2.2 skrll #define SCAN_ERROR 2
139 1.1.2.2 skrll
140 1.1.2.2 skrll typedef struct mbr_args {
141 1.1.2.2 skrll struct disk *pdk;
142 1.1.2.2 skrll struct vnode *vp;
143 1.1.2.2 skrll void *buf;
144 1.1.2.2 skrll int error;
145 1.1.2.2 skrll } mbr_args_t;
146 1.1.2.2 skrll
147 1.1.2.2 skrll static const char *
148 1.1.2.2 skrll bsdlabel_fstype_to_str(uint8_t fstype)
149 1.1.2.2 skrll {
150 1.1.2.2 skrll const char *str;
151 1.1.2.2 skrll
152 1.1.2.2 skrll switch (fstype) {
153 1.1.2.2 skrll case FS_UNUSED: str = DKW_PTYPE_UNUSED; break;
154 1.1.2.2 skrll case FS_SWAP: str = DKW_PTYPE_SWAP; break;
155 1.1.2.2 skrll case FS_BSDFFS: str = DKW_PTYPE_FFS; break;
156 1.1.2.2 skrll case FS_MSDOS: str = DKW_PTYPE_FAT; break;
157 1.1.2.2 skrll case FS_BSDLFS: str = DKW_PTYPE_LFS; break;
158 1.1.2.2 skrll case FS_ISO9660: str = DKW_PTYPE_ISO9660; break;
159 1.1.2.2 skrll case FS_ADOS: str = DKW_PTYPE_AMIGADOS; break;
160 1.1.2.2 skrll case FS_HFS: str = DKW_PTYPE_APPLEHFS; break;
161 1.1.2.2 skrll case FS_FILECORE: str = DKW_PTYPE_FILECORE; break;
162 1.1.2.2 skrll case FS_EX2FS: str = DKW_PTYPE_EXT2FS; break;
163 1.1.2.2 skrll case FS_NTFS: str = DKW_PTYPE_NTFS; break;
164 1.1.2.2 skrll case FS_RAID: str = DKW_PTYPE_RAIDFRAME; break;
165 1.1.2.2 skrll case FS_CCD: str = DKW_PTYPE_CCD; break;
166 1.1.2.2 skrll case FS_APPLEUFS: str = DKW_PTYPE_APPLEUFS; break;
167 1.1.2.2 skrll case FS_VINUM: str = "vinum"; break;
168 1.1.2.2 skrll default: str = NULL; break;
169 1.1.2.2 skrll }
170 1.1.2.2 skrll
171 1.1.2.2 skrll return (str);
172 1.1.2.2 skrll }
173 1.1.2.2 skrll
174 1.1.2.2 skrll static void
175 1.1.2.2 skrll swap_disklabel(struct disklabel *lp)
176 1.1.2.2 skrll {
177 1.1.2.2 skrll int i;
178 1.1.2.2 skrll
179 1.1.2.2 skrll #define SWAP16(x) lp->x = bswap16(lp->x)
180 1.1.2.2 skrll #define SWAP32(x) lp->x = bswap32(lp->x)
181 1.1.2.2 skrll
182 1.1.2.2 skrll SWAP32(d_magic);
183 1.1.2.2 skrll SWAP16(d_type);
184 1.1.2.2 skrll SWAP16(d_subtype);
185 1.1.2.2 skrll SWAP32(d_secsize);
186 1.1.2.2 skrll SWAP32(d_nsectors);
187 1.1.2.2 skrll SWAP32(d_ntracks);
188 1.1.2.2 skrll SWAP32(d_ncylinders);
189 1.1.2.2 skrll SWAP32(d_secpercyl);
190 1.1.2.2 skrll SWAP32(d_secperunit);
191 1.1.2.2 skrll SWAP16(d_sparespertrack);
192 1.1.2.2 skrll SWAP16(d_sparespercyl);
193 1.1.2.2 skrll SWAP32(d_acylinders);
194 1.1.2.2 skrll SWAP16(d_rpm);
195 1.1.2.2 skrll SWAP16(d_interleave);
196 1.1.2.2 skrll SWAP16(d_trackskew);
197 1.1.2.2 skrll SWAP16(d_cylskew);
198 1.1.2.2 skrll SWAP32(d_headswitch);
199 1.1.2.2 skrll SWAP32(d_trkseek);
200 1.1.2.2 skrll SWAP32(d_flags);
201 1.1.2.2 skrll
202 1.1.2.2 skrll for (i = 0; i < NDDATA; i++)
203 1.1.2.2 skrll SWAP32(d_drivedata[i]);
204 1.1.2.2 skrll for (i = 0; i < NSPARE; i++)
205 1.1.2.2 skrll SWAP32(d_spare[i]);
206 1.1.2.2 skrll
207 1.1.2.2 skrll SWAP32(d_magic2);
208 1.1.2.2 skrll SWAP16(d_checksum);
209 1.1.2.2 skrll SWAP16(d_npartitions);
210 1.1.2.2 skrll SWAP32(d_bbsize);
211 1.1.2.2 skrll SWAP32(d_sbsize);
212 1.1.2.2 skrll
213 1.1.2.2 skrll for (i = 0; i < lp->d_npartitions; i++) {
214 1.1.2.2 skrll SWAP32(d_partitions[i].p_size);
215 1.1.2.2 skrll SWAP32(d_partitions[i].p_offset);
216 1.1.2.2 skrll SWAP32(d_partitions[i].p_fsize);
217 1.1.2.2 skrll SWAP16(d_partitions[i].p_cpg);
218 1.1.2.2 skrll }
219 1.1.2.2 skrll
220 1.1.2.2 skrll #undef SWAP16
221 1.1.2.2 skrll #undef SWAP32
222 1.1.2.2 skrll }
223 1.1.2.2 skrll
224 1.1.2.2 skrll static int
225 1.1.2.2 skrll validate_label(mbr_args_t *a, daddr_t label_sector, size_t label_offset)
226 1.1.2.2 skrll {
227 1.1.2.2 skrll struct disklabel *lp;
228 1.1.2.2 skrll caddr_t lp_lim;
229 1.1.2.2 skrll int i, error;
230 1.1.2.2 skrll
231 1.1.2.2 skrll error = dkwedge_read(a->pdk, a->vp, label_sector, a->buf, DEV_BSIZE);
232 1.1.2.2 skrll if (error) {
233 1.1.2.2 skrll aprint_error("%s: unable to read BSD disklabel @ %" PRId64
234 1.1.2.2 skrll ", error = %d\n", a->pdk->dk_name, label_sector, error);
235 1.1.2.2 skrll a->error = error;
236 1.1.2.2 skrll return (SCAN_ERROR);
237 1.1.2.2 skrll }
238 1.1.2.2 skrll
239 1.1.2.2 skrll /*
240 1.1.2.2 skrll * We ignore label_offset; this seems to have not been used
241 1.1.2.2 skrll * consistently in the old code, requiring us to do the search
242 1.1.2.2 skrll * in the sector.
243 1.1.2.2 skrll */
244 1.1.2.2 skrll lp = a->buf;
245 1.1.2.2 skrll lp_lim = (caddr_t)a->buf + DEV_BSIZE - DISKLABEL_MINSIZE;
246 1.1.2.2 skrll for (;; lp = (void *)((caddr_t)lp + sizeof(uint32_t))) {
247 1.1.2.2 skrll if ((caddr_t)lp > lp_lim)
248 1.1.2.2 skrll return (SCAN_CONTINUE);
249 1.1.2.2 skrll label_offset = (size_t)((caddr_t)lp - (caddr_t)a->buf);
250 1.1.2.2 skrll if (lp->d_magic != DISKMAGIC ||
251 1.1.2.2 skrll lp->d_magic != DISKMAGIC) {
252 1.1.2.2 skrll if (lp->d_magic == bswap32(DISKMAGIC) &&
253 1.1.2.2 skrll lp->d_magic == bswap32(DISKMAGIC)) {
254 1.1.2.2 skrll /*
255 1.1.2.2 skrll * Label is in the other byte order; validate
256 1.1.2.2 skrll * its length, then byte-swap it.
257 1.1.2.2 skrll */
258 1.1.2.2 skrll if ((caddr_t)lp +
259 1.1.2.2 skrll DISKLABEL_SIZE(bswap16(lp->d_npartitions)) >
260 1.1.2.2 skrll (caddr_t)a->buf + DEV_BSIZE) {
261 1.1.2.2 skrll aprint_error("%s: BSD disklabel @ "
262 1.1.2.2 skrll "%" PRId64
263 1.1.2.2 skrll "+%zd has bogus partition "
264 1.1.2.2 skrll "count (%u)\n", a->pdk->dk_name,
265 1.1.2.2 skrll label_sector, label_offset,
266 1.1.2.2 skrll bswap16(lp->d_npartitions));
267 1.1.2.2 skrll continue;
268 1.1.2.2 skrll }
269 1.1.2.2 skrll swap_disklabel(lp);
270 1.1.2.2 skrll } else
271 1.1.2.2 skrll continue;
272 1.1.2.2 skrll } else {
273 1.1.2.2 skrll /*
274 1.1.2.2 skrll * Validate the disklabel length.
275 1.1.2.2 skrll */
276 1.1.2.2 skrll if ((caddr_t)lp + DISKLABEL_SIZE(lp->d_npartitions) >
277 1.1.2.2 skrll (caddr_t)a->buf + DEV_BSIZE) {
278 1.1.2.2 skrll aprint_error("%s: BSD disklabel @ %" PRId64
279 1.1.2.2 skrll "+%zd has bogus partition count (%u)\n",
280 1.1.2.2 skrll a->pdk->dk_name, label_sector,
281 1.1.2.2 skrll label_offset, lp->d_npartitions);
282 1.1.2.2 skrll continue;
283 1.1.2.2 skrll }
284 1.1.2.2 skrll }
285 1.1.2.2 skrll
286 1.1.2.2 skrll /*
287 1.1.2.2 skrll * Disklabel is now in the right order and we have validated
288 1.1.2.2 skrll * the partition count, checksum it as the final check.
289 1.1.2.2 skrll */
290 1.1.2.2 skrll if (dkcksum(lp) != 0) {
291 1.1.2.2 skrll aprint_error("%s: BSD disklabel @ %" PRId64
292 1.1.2.2 skrll "+%zd has bad checksum\n", a->pdk->dk_name,
293 1.1.2.2 skrll label_sector, label_offset);
294 1.1.2.2 skrll continue;
295 1.1.2.2 skrll }
296 1.1.2.2 skrll
297 1.1.2.2 skrll /*
298 1.1.2.2 skrll * Ok, we have a valid NetBSD disklabel, add wedges for it.
299 1.1.2.2 skrll */
300 1.1.2.2 skrll for (i = 0; i < lp->d_npartitions; i++) {
301 1.1.2.2 skrll struct dkwedge_info dkw;
302 1.1.2.2 skrll struct partition *p;
303 1.1.2.2 skrll const char *ptype;
304 1.1.2.2 skrll
305 1.1.2.2 skrll p = &lp->d_partitions[i];
306 1.1.2.2 skrll
307 1.1.2.2 skrll if (p->p_fstype == FS_UNUSED)
308 1.1.2.2 skrll continue;
309 1.1.2.2 skrll if ((ptype =
310 1.1.2.2 skrll bsdlabel_fstype_to_str(p->p_fstype)) == NULL) {
311 1.1.2.2 skrll /*
312 1.1.2.2 skrll * XXX Should probably just add these...
313 1.1.2.2 skrll * XXX maybe just have an empty ptype?
314 1.1.2.2 skrll */
315 1.1.2.2 skrll aprint_verbose("%s: skipping partition %d, "
316 1.1.2.2 skrll "type %d\n", a->pdk->dk_name, i,
317 1.1.2.2 skrll p->p_fstype);
318 1.1.2.2 skrll continue;
319 1.1.2.2 skrll }
320 1.1.2.2 skrll strcpy(dkw.dkw_ptype, ptype);
321 1.1.2.2 skrll
322 1.1.2.2 skrll strcpy(dkw.dkw_parent, a->pdk->dk_name);
323 1.1.2.2 skrll dkw.dkw_offset = p->p_offset;
324 1.1.2.2 skrll dkw.dkw_size = p->p_size;
325 1.1.2.2 skrll
326 1.1.2.2 skrll /*
327 1.1.2.2 skrll * These get historical disk naming style
328 1.1.2.2 skrll * wedge names.
329 1.1.2.2 skrll */
330 1.1.2.2 skrll snprintf(dkw.dkw_wname, sizeof(dkw.dkw_wname),
331 1.1.2.2 skrll "%s%c", a->pdk->dk_name, 'a' + i);
332 1.1.2.2 skrll
333 1.1.2.2 skrll error = dkwedge_add(&dkw);
334 1.1.2.2 skrll if (error == EEXIST)
335 1.1.2.2 skrll aprint_error("%s: wedge named '%s' already "
336 1.1.2.2 skrll "exists, manual intervention required\n",
337 1.1.2.2 skrll a->pdk->dk_name, dkw.dkw_wname);
338 1.1.2.2 skrll else if (error)
339 1.1.2.2 skrll aprint_error("%s: error %d adding partition "
340 1.1.2.2 skrll "%d type %d\n", a->pdk->dk_name, error,
341 1.1.2.2 skrll i, p->p_fstype);
342 1.1.2.2 skrll }
343 1.1.2.2 skrll return (SCAN_FOUND);
344 1.1.2.2 skrll }
345 1.1.2.2 skrll }
346 1.1.2.2 skrll
347 1.1.2.2 skrll static int
348 1.1.2.2 skrll scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, struct mbr_partition *,
349 1.1.2.2 skrll int, u_int))
350 1.1.2.2 skrll {
351 1.1.2.2 skrll struct mbr_partition ptns[MBR_PART_COUNT];
352 1.1.2.2 skrll struct mbr_partition *dp;
353 1.1.2.2 skrll struct mbr_sector *mbr;
354 1.1.2.2 skrll u_int ext_base, this_ext, next_ext;
355 1.1.2.2 skrll int i, rval;
356 1.1.2.2 skrll #ifdef COMPAT_386BSD_MBRPART
357 1.1.2.2 skrll int dp_386bsd = -1;
358 1.1.2.2 skrll #endif
359 1.1.2.2 skrll
360 1.1.2.2 skrll ext_base = 0;
361 1.1.2.2 skrll this_ext = 0;
362 1.1.2.2 skrll for (;;) {
363 1.1.2.2 skrll a->error = dkwedge_read(a->pdk, a->vp, this_ext, a->buf,
364 1.1.2.2 skrll DEV_BSIZE);
365 1.1.2.2 skrll if (a->error) {
366 1.1.2.2 skrll aprint_error("%s: unable to read MBR @ %u, "
367 1.1.2.2 skrll "error = %d\n", a->pdk->dk_name, this_ext,
368 1.1.2.2 skrll a->error);
369 1.1.2.2 skrll return (SCAN_ERROR);
370 1.1.2.2 skrll }
371 1.1.2.2 skrll
372 1.1.2.2 skrll mbr = a->buf;
373 1.1.2.2 skrll if (mbr->mbr_magic != htole16(MBR_MAGIC))
374 1.1.2.2 skrll return (SCAN_CONTINUE);
375 1.1.2.2 skrll
376 1.1.2.2 skrll /* Copy data out of buffer so action can use the buffer. */
377 1.1.2.2 skrll memcpy(ptns, &mbr->mbr_parts, sizeof(ptns));
378 1.1.2.2 skrll
379 1.1.2.2 skrll /* Looks for NetBSD partition. */
380 1.1.2.2 skrll next_ext = 0;
381 1.1.2.2 skrll dp = ptns;
382 1.1.2.2 skrll for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
383 1.1.2.2 skrll if (dp->mbrp_type == 0)
384 1.1.2.2 skrll continue;
385 1.1.2.2 skrll if (MBR_IS_EXTENDED(dp->mbrp_type)) {
386 1.1.2.2 skrll next_ext = le32toh(dp->mbrp_start);
387 1.1.2.2 skrll continue;
388 1.1.2.2 skrll }
389 1.1.2.2 skrll #ifdef COMPAT_386BSD_MBRPART
390 1.1.2.2 skrll if (dp->mbrp_type == MBR_PTYPE_386BSD) {
391 1.1.2.2 skrll /*
392 1.1.2.2 skrll * If more than one matches, take last,
393 1.1.2.2 skrll * as NetBSD install tool does.
394 1.1.2.2 skrll */
395 1.1.2.2 skrll if (this_ext == 0)
396 1.1.2.2 skrll dp_386bsd = i;
397 1.1.2.2 skrll continue;
398 1.1.2.2 skrll }
399 1.1.2.2 skrll #endif
400 1.1.2.2 skrll rval = (*actn)(a, dp, i, this_ext);
401 1.1.2.2 skrll if (rval != SCAN_CONTINUE)
402 1.1.2.2 skrll return (rval);
403 1.1.2.2 skrll }
404 1.1.2.2 skrll if (next_ext == 0)
405 1.1.2.2 skrll break;
406 1.1.2.2 skrll if (ext_base == 0) {
407 1.1.2.2 skrll ext_base = next_ext;
408 1.1.2.2 skrll next_ext = 0;
409 1.1.2.2 skrll }
410 1.1.2.2 skrll next_ext += ext_base;
411 1.1.2.2 skrll if (next_ext <= this_ext)
412 1.1.2.2 skrll break;
413 1.1.2.2 skrll this_ext = next_ext;
414 1.1.2.2 skrll }
415 1.1.2.2 skrll #ifdef COMPAT_386BSD_MBRPART
416 1.1.2.2 skrll if (this_ext == 0 && dp_386bsd != -1)
417 1.1.2.2 skrll return ((*actn)(a, &ptns[dp_386bsd], dp_386bsd, 0));
418 1.1.2.2 skrll #endif
419 1.1.2.2 skrll return (SCAN_CONTINUE);
420 1.1.2.2 skrll }
421 1.1.2.2 skrll
422 1.1.2.2 skrll static int
423 1.1.2.2 skrll look_netbsd_part(mbr_args_t *a, struct mbr_partition *dp, int slot,
424 1.1.2.2 skrll u_int ext_base)
425 1.1.2.2 skrll {
426 1.1.2.2 skrll int ptn_base = ext_base + le32toh(dp->mbrp_start);
427 1.1.2.2 skrll int rval;
428 1.1.2.2 skrll
429 1.1.2.2 skrll if (
430 1.1.2.2 skrll #ifdef COMPAT_386BSD_MBRPART
431 1.1.2.2 skrll dp->mbrp_type == MBR_PTYPE_386BSD ||
432 1.1.2.2 skrll #endif
433 1.1.2.2 skrll dp->mbrp_type == MBR_PTYPE_NETBSD) {
434 1.1.2.2 skrll rval = validate_label(a, ptn_base + BSD44_MBR_LABELSECTOR, 0);
435 1.1.2.2 skrll
436 1.1.2.2 skrll /* If we got a NetBSD label, look no further. */
437 1.1.2.2 skrll if (rval == SCAN_FOUND)
438 1.1.2.2 skrll return (rval);
439 1.1.2.2 skrll }
440 1.1.2.2 skrll
441 1.1.2.2 skrll return (SCAN_CONTINUE);
442 1.1.2.2 skrll }
443 1.1.2.2 skrll
444 1.1.2.2 skrll static int
445 1.1.2.2 skrll dkwedge_discover_bsdlabel(struct disk *pdk, struct vnode *vp)
446 1.1.2.2 skrll {
447 1.1.2.2 skrll mbr_args_t a;
448 1.1.2.2 skrll const struct disklabel_location *dl;
449 1.1.2.2 skrll int rval;
450 1.1.2.2 skrll
451 1.1.2.2 skrll a.pdk = pdk;
452 1.1.2.2 skrll a.vp = vp;
453 1.1.2.2 skrll a.buf = malloc(DEV_BSIZE, M_DEVBUF, M_WAITOK);
454 1.1.2.2 skrll a.error = 0;
455 1.1.2.2 skrll
456 1.1.2.2 skrll /* MBR search. */
457 1.1.2.2 skrll rval = scan_mbr(&a, look_netbsd_part);
458 1.1.2.2 skrll if (rval != SCAN_CONTINUE) {
459 1.1.2.2 skrll if (rval == SCAN_FOUND)
460 1.1.2.2 skrll a.error = 0; /* found it, wedges installed */
461 1.1.2.2 skrll goto out;
462 1.1.2.2 skrll }
463 1.1.2.2 skrll
464 1.1.2.2 skrll /* Known location search. */
465 1.1.2.2 skrll for (dl = disklabel_locations; dl->label_sector != -1; dl++) {
466 1.1.2.2 skrll rval = validate_label(&a, dl->label_sector, dl->label_offset);
467 1.1.2.2 skrll if (rval != SCAN_CONTINUE) {
468 1.1.2.2 skrll if (rval == SCAN_FOUND)
469 1.1.2.2 skrll a.error = 0; /* found it, wedges installed */
470 1.1.2.2 skrll goto out;
471 1.1.2.2 skrll }
472 1.1.2.2 skrll }
473 1.1.2.2 skrll
474 1.1.2.2 skrll /* No NetBSD disklabel found. */
475 1.1.2.2 skrll a.error = ESRCH;
476 1.1.2.2 skrll out:
477 1.1.2.2 skrll free(a.buf, M_DEVBUF);
478 1.1.2.2 skrll return (a.error);
479 1.1.2.2 skrll }
480 1.1.2.2 skrll
481 1.1.2.2 skrll DKWEDGE_DISCOVERY_METHOD_DECL(BSD44, 5, dkwedge_discover_bsdlabel);
482