1 /* $NetBSD: dkwedge_bsdlabel.c,v 1.25 2020/04/11 16:00:34 jdolecek 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.25 2020/04/11 16:00:34 jdolecek 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/buf.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 struct buf *bp;
139 int error;
140 uint32_t secsize;
141 } mbr_args_t;
142
143 static const char *
bsdlabel_fstype_to_str(uint8_t fstype)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
swap_disklabel(struct disklabel * lp)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
addwedges(const mbr_args_t * a,const struct disklabel * lp)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
validate_label(mbr_args_t * a,daddr_t label_sector,size_t label_offset)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->bp->b_data,
282 a->secsize);
283 if (error) {
284 aprint_error("%s: unable to read BSD disklabel @ %" PRId64
285 ", error = %d\n", a->pdk->dk_name, label_sector, error);
286 a->error = error;
287 return (SCAN_ERROR);
288 }
289
290 /*
291 * We ignore label_offset; this seems to have not been used
292 * consistently in the old code, requiring us to do the search
293 * in the sector.
294 */
295 lp = a->bp->b_data;
296 lp_lim = (char *)a->bp->b_data + a->secsize - DISKLABEL_MINSIZE;
297 for (;; lp = (void *)((char *)lp + sizeof(uint32_t))) {
298 if ((char *)lp > (char *)lp_lim)
299 return (SCAN_CONTINUE);
300 label_offset = (size_t)((char *)lp - (char *)a->bp->b_data);
301 if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC) {
302 if (lp->d_magic != bswap32(DISKMAGIC) ||
303 lp->d_magic2 != bswap32(DISKMAGIC))
304 continue;
305 /* Label is in the other byte order. */
306 swapped = 1;
307 } else
308 swapped = 0;
309
310 npartitions = (swapped) ? bswap16(lp->d_npartitions)
311 : lp->d_npartitions;
312
313 /* Validate label length. */
314 if ((char *)lp + DISKLABEL_SIZE(npartitions) >
315 (char *)a->bp->b_data + a->secsize) {
316 aprint_error("%s: BSD disklabel @ "
317 "%" PRId64 "+%zd has bogus partition count (%u)\n",
318 a->pdk->dk_name, label_sector, label_offset,
319 npartitions);
320 continue;
321 }
322
323 /*
324 * We have validated the partition count. Checksum it.
325 * Note that we purposefully checksum before swapping
326 * the byte order.
327 */
328 if (dkcksum_sized(lp, npartitions) != 0) {
329 aprint_error("%s: BSD disklabel @ %" PRId64
330 "+%zd has bad checksum\n", a->pdk->dk_name,
331 label_sector, label_offset);
332 continue;
333 }
334 /* Put the disklabel in the right order. */
335 if (swapped)
336 swap_disklabel(lp);
337 addwedges(a, lp);
338 return (SCAN_FOUND);
339 }
340 }
341
342 static int
scan_mbr(mbr_args_t * a,int (* actn)(mbr_args_t *,struct mbr_partition *,int,u_int))343 scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, struct mbr_partition *,
344 int, u_int))
345 {
346 struct mbr_partition ptns[MBR_PART_COUNT];
347 struct mbr_partition *dp;
348 struct mbr_sector *mbr;
349 u_int ext_base, this_ext, next_ext;
350 int i, rval;
351 #ifdef COMPAT_386BSD_MBRPART
352 int dp_386bsd = -1;
353 #endif
354
355 ext_base = 0;
356 this_ext = 0;
357 for (;;) {
358 a->error = dkwedge_read(a->pdk, a->vp, this_ext, a->bp->b_data,
359 a->secsize);
360 if (a->error) {
361 aprint_error("%s: unable to read MBR @ %u, "
362 "error = %d\n", a->pdk->dk_name, this_ext,
363 a->error);
364 return (SCAN_ERROR);
365 }
366
367 mbr = a->bp->b_data;
368 if (mbr->mbr_magic != htole16(MBR_MAGIC))
369 return (SCAN_CONTINUE);
370
371 /* Copy data out of buffer so action can use the buffer. */
372 memcpy(ptns, &mbr->mbr_parts, sizeof(ptns));
373
374 /* Looks for NetBSD partition. */
375 next_ext = 0;
376 dp = ptns;
377 for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
378 if (dp->mbrp_type == 0)
379 continue;
380 if (MBR_IS_EXTENDED(dp->mbrp_type)) {
381 next_ext = le32toh(dp->mbrp_start);
382 continue;
383 }
384 #ifdef COMPAT_386BSD_MBRPART
385 if (dp->mbrp_type == MBR_PTYPE_386BSD) {
386 /*
387 * If more than one matches, take last,
388 * as NetBSD install tool does.
389 */
390 if (this_ext == 0)
391 dp_386bsd = i;
392 continue;
393 }
394 #endif
395 rval = (*actn)(a, dp, i, this_ext);
396 if (rval != SCAN_CONTINUE)
397 return (rval);
398 }
399 if (next_ext == 0)
400 break;
401 if (ext_base == 0) {
402 ext_base = next_ext;
403 next_ext = 0;
404 }
405 next_ext += ext_base;
406 if (next_ext <= this_ext)
407 break;
408 this_ext = next_ext;
409 }
410 #ifdef COMPAT_386BSD_MBRPART
411 if (this_ext == 0 && dp_386bsd != -1)
412 return ((*actn)(a, &ptns[dp_386bsd], dp_386bsd, 0));
413 #endif
414 return (SCAN_CONTINUE);
415 }
416
417 static int
look_netbsd_part(mbr_args_t * a,struct mbr_partition * dp,int slot,u_int ext_base)418 look_netbsd_part(mbr_args_t *a, struct mbr_partition *dp, int slot,
419 u_int ext_base)
420 {
421 int ptn_base = ext_base + le32toh(dp->mbrp_start);
422 int rval;
423
424 if (
425 #ifdef COMPAT_386BSD_MBRPART
426 dp->mbrp_type == MBR_PTYPE_386BSD ||
427 #endif
428 dp->mbrp_type == MBR_PTYPE_NETBSD) {
429 rval = validate_label(a, ptn_base + BSD44_MBR_LABELSECTOR, 0);
430
431 /* If we got a NetBSD label, look no further. */
432 if (rval == SCAN_FOUND)
433 return (rval);
434 }
435
436 return (SCAN_CONTINUE);
437 }
438
439 static int
dkwedge_discover_bsdlabel(struct disk * pdk,struct vnode * vp)440 dkwedge_discover_bsdlabel(struct disk *pdk, struct vnode *vp)
441 {
442 mbr_args_t a;
443 const struct disklabel_location *dl;
444 int rval;
445
446 a.pdk = pdk;
447 a.secsize = DEV_BSIZE << pdk->dk_blkshift;
448 a.vp = vp;
449 a.bp = geteblk(a.secsize);
450 a.error = 0;
451
452 /* MBR search. */
453 rval = scan_mbr(&a, look_netbsd_part);
454 if (rval != SCAN_CONTINUE) {
455 if (rval == SCAN_FOUND)
456 a.error = 0; /* found it, wedges installed */
457 goto out;
458 }
459
460 /* Known location search. */
461 for (dl = disklabel_locations; dl->label_sector != -1; dl++) {
462 rval = validate_label(&a, dl->label_sector, dl->label_offset);
463 if (rval != SCAN_CONTINUE) {
464 if (rval == SCAN_FOUND)
465 a.error = 0; /* found it, wedges installed */
466 goto out;
467 }
468 }
469
470 /* No NetBSD disklabel found. */
471 a.error = ESRCH;
472 out:
473 brelse(a.bp, 0);
474 return (a.error);
475 }
476
477 #ifdef _KERNEL
478 DKWEDGE_DISCOVERY_METHOD_DECL(BSD44, 5, dkwedge_discover_bsdlabel);
479 #endif
480