dkwedge_bsdlabel.c revision 1.24 1 /* $NetBSD: dkwedge_bsdlabel.c,v 1.24 2019/07/09 17:06:46 maxv 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.24 2019/07/09 17:06:46 maxv 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
231 memset(&dkw, 0, sizeof(dkw));
232
233 ptype = bsdlabel_fstype_to_str(p->p_fstype);
234 if (ptype == NULL)
235 snprintf(dkw.dkw_ptype, sizeof(dkw.dkw_ptype),
236 "unknown#%u", p->p_fstype);
237 else
238 strlcpy(dkw.dkw_ptype, ptype, sizeof(dkw.dkw_ptype));
239
240 strlcpy(dkw.dkw_parent, a->pdk->dk_name,
241 sizeof(dkw.dkw_parent));
242 dkw.dkw_offset = p->p_offset;
243 dkw.dkw_size = p->p_size;
244
245 /*
246 * If the label defines a name, append the partition
247 * letter and use it as the wedge name.
248 * Otherwise use historical disk naming style
249 * wedge names.
250 */
251 if (lp->d_packname[0] &&
252 strcmp(lp->d_packname,"fictitious") != 0) {
253 snprintf((char *)&dkw.dkw_wname, sizeof(dkw.dkw_wname),
254 "%.*s/%c", (int)sizeof(dkw.dkw_wname)-3,
255 lp->d_packname, 'a' + i);
256 } else {
257 snprintf((char *)&dkw.dkw_wname, sizeof(dkw.dkw_wname),
258 "%s%c", a->pdk->dk_name, 'a' + i);
259 }
260
261 error = dkwedge_add(&dkw);
262 if (error == EEXIST)
263 aprint_error("%s: wedge named '%s' already "
264 "exists, manual intervention required\n",
265 a->pdk->dk_name, dkw.dkw_wname);
266 else if (error)
267 aprint_error("%s: error %d adding partition "
268 "%d type %d\n", a->pdk->dk_name, error,
269 i, p->p_fstype);
270 }
271 }
272
273 static int
274 validate_label(mbr_args_t *a, daddr_t label_sector, size_t label_offset)
275 {
276 struct disklabel *lp;
277 void *lp_lim;
278 int error, swapped;
279 uint16_t npartitions;
280
281 error = dkwedge_read(a->pdk, a->vp, label_sector, a->buf, a->secsize);
282 if (error) {
283 aprint_error("%s: unable to read BSD disklabel @ %" PRId64
284 ", error = %d\n", a->pdk->dk_name, label_sector, error);
285 a->error = error;
286 return (SCAN_ERROR);
287 }
288
289 /*
290 * We ignore label_offset; this seems to have not been used
291 * consistently in the old code, requiring us to do the search
292 * in the sector.
293 */
294 lp = a->buf;
295 lp_lim = (char *)a->buf + a->secsize - DISKLABEL_MINSIZE;
296 for (;; lp = (void *)((char *)lp + sizeof(uint32_t))) {
297 if ((char *)lp > (char *)lp_lim)
298 return (SCAN_CONTINUE);
299 label_offset = (size_t)((char *)lp - (char *)a->buf);
300 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC) {
301 if (lp->d_magic != bswap32(DISKMAGIC) ||
302 lp->d_magic2 != bswap32(DISKMAGIC))
303 continue;
304 /* Label is in the other byte order. */
305 swapped = 1;
306 } else
307 swapped = 0;
308
309 npartitions = (swapped) ? bswap16(lp->d_npartitions)
310 : lp->d_npartitions;
311
312 /* Validate label length. */
313 if ((char *)lp + DISKLABEL_SIZE(npartitions) >
314 (char *)a->buf + a->secsize) {
315 aprint_error("%s: BSD disklabel @ "
316 "%" PRId64 "+%zd has bogus partition count (%u)\n",
317 a->pdk->dk_name, label_sector, label_offset,
318 npartitions);
319 continue;
320 }
321
322 /*
323 * We have validated the partition count. Checksum it.
324 * Note that we purposefully checksum before swapping
325 * the byte order.
326 */
327 if (dkcksum_sized(lp, npartitions) != 0) {
328 aprint_error("%s: BSD disklabel @ %" PRId64
329 "+%zd has bad checksum\n", a->pdk->dk_name,
330 label_sector, label_offset);
331 continue;
332 }
333 /* Put the disklabel in the right order. */
334 if (swapped)
335 swap_disklabel(lp);
336 addwedges(a, lp);
337 return (SCAN_FOUND);
338 }
339 }
340
341 static int
342 scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, struct mbr_partition *,
343 int, u_int))
344 {
345 struct mbr_partition ptns[MBR_PART_COUNT];
346 struct mbr_partition *dp;
347 struct mbr_sector *mbr;
348 u_int ext_base, this_ext, next_ext;
349 int i, rval;
350 #ifdef COMPAT_386BSD_MBRPART
351 int dp_386bsd = -1;
352 #endif
353
354 ext_base = 0;
355 this_ext = 0;
356 for (;;) {
357 a->error = dkwedge_read(a->pdk, a->vp, this_ext, a->buf,
358 a->secsize);
359 if (a->error) {
360 aprint_error("%s: unable to read MBR @ %u, "
361 "error = %d\n", a->pdk->dk_name, this_ext,
362 a->error);
363 return (SCAN_ERROR);
364 }
365
366 mbr = a->buf;
367 if (mbr->mbr_magic != htole16(MBR_MAGIC))
368 return (SCAN_CONTINUE);
369
370 /* Copy data out of buffer so action can use the buffer. */
371 memcpy(ptns, &mbr->mbr_parts, sizeof(ptns));
372
373 /* Looks for NetBSD partition. */
374 next_ext = 0;
375 dp = ptns;
376 for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
377 if (dp->mbrp_type == 0)
378 continue;
379 if (MBR_IS_EXTENDED(dp->mbrp_type)) {
380 next_ext = le32toh(dp->mbrp_start);
381 continue;
382 }
383 #ifdef COMPAT_386BSD_MBRPART
384 if (dp->mbrp_type == MBR_PTYPE_386BSD) {
385 /*
386 * If more than one matches, take last,
387 * as NetBSD install tool does.
388 */
389 if (this_ext == 0)
390 dp_386bsd = i;
391 continue;
392 }
393 #endif
394 rval = (*actn)(a, dp, i, this_ext);
395 if (rval != SCAN_CONTINUE)
396 return (rval);
397 }
398 if (next_ext == 0)
399 break;
400 if (ext_base == 0) {
401 ext_base = next_ext;
402 next_ext = 0;
403 }
404 next_ext += ext_base;
405 if (next_ext <= this_ext)
406 break;
407 this_ext = next_ext;
408 }
409 #ifdef COMPAT_386BSD_MBRPART
410 if (this_ext == 0 && dp_386bsd != -1)
411 return ((*actn)(a, &ptns[dp_386bsd], dp_386bsd, 0));
412 #endif
413 return (SCAN_CONTINUE);
414 }
415
416 static int
417 look_netbsd_part(mbr_args_t *a, struct mbr_partition *dp, int slot,
418 u_int ext_base)
419 {
420 int ptn_base = ext_base + le32toh(dp->mbrp_start);
421 int rval;
422
423 if (
424 #ifdef COMPAT_386BSD_MBRPART
425 dp->mbrp_type == MBR_PTYPE_386BSD ||
426 #endif
427 dp->mbrp_type == MBR_PTYPE_NETBSD) {
428 rval = validate_label(a, ptn_base + BSD44_MBR_LABELSECTOR, 0);
429
430 /* If we got a NetBSD label, look no further. */
431 if (rval == SCAN_FOUND)
432 return (rval);
433 }
434
435 return (SCAN_CONTINUE);
436 }
437
438 #ifdef _KERNEL
439 #define DKW_MALLOC(SZ) malloc((SZ), M_DEVBUF, M_WAITOK)
440 #define DKW_FREE(PTR) free((PTR), M_DEVBUF)
441 #else
442 #define DKW_MALLOC(SZ) malloc((SZ))
443 #define DKW_FREE(PTR) free((PTR))
444 #endif
445
446 static int
447 dkwedge_discover_bsdlabel(struct disk *pdk, struct vnode *vp)
448 {
449 mbr_args_t a;
450 const struct disklabel_location *dl;
451 int rval;
452
453 a.pdk = pdk;
454 a.secsize = DEV_BSIZE << pdk->dk_blkshift;
455 a.vp = vp;
456 a.buf = DKW_MALLOC(a.secsize);
457 a.error = 0;
458
459 /* MBR search. */
460 rval = scan_mbr(&a, look_netbsd_part);
461 if (rval != SCAN_CONTINUE) {
462 if (rval == SCAN_FOUND)
463 a.error = 0; /* found it, wedges installed */
464 goto out;
465 }
466
467 /* Known location search. */
468 for (dl = disklabel_locations; dl->label_sector != -1; dl++) {
469 rval = validate_label(&a, dl->label_sector, dl->label_offset);
470 if (rval != SCAN_CONTINUE) {
471 if (rval == SCAN_FOUND)
472 a.error = 0; /* found it, wedges installed */
473 goto out;
474 }
475 }
476
477 /* No NetBSD disklabel found. */
478 a.error = ESRCH;
479 out:
480 DKW_FREE(a.buf);
481 return (a.error);
482 }
483
484 #ifdef _KERNEL
485 DKWEDGE_DISCOVERY_METHOD_DECL(BSD44, 5, dkwedge_discover_bsdlabel);
486 #endif
487