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