xref: /netbsd-src/sys/dev/dkwedge/dkwedge_bsdlabel.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: dkwedge_bsdlabel.c,v 1.16 2009/12/05 16:29:14 pooka 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.16 2009/12/05 16:29:14 pooka 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, hp700,
122 			   hpcarm, hpcmips, i386, ibmnws, mipsco, mvmeppc,
123 			   ofppc, 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 } mbr_args_t;
141 
142 static const char *
143 bsdlabel_fstype_to_str(uint8_t fstype)
144 {
145 	const char *str;
146 
147 	switch (fstype) {
148 	case FS_UNUSED:		str = DKW_PTYPE_UNUSED;		break;
149 	case FS_SWAP:		str = DKW_PTYPE_SWAP;		break;
150 	case FS_BSDFFS:		str = DKW_PTYPE_FFS;		break;
151 	case FS_MSDOS:		str = DKW_PTYPE_FAT;		break;
152 	case FS_BSDLFS:		str = DKW_PTYPE_LFS;		break;
153 	case FS_ISO9660:	str = DKW_PTYPE_ISO9660;	break;
154 	case FS_ADOS:		str = DKW_PTYPE_AMIGADOS;	break;
155 	case FS_HFS:		str = DKW_PTYPE_APPLEHFS;	break;
156 	case FS_FILECORE:	str = DKW_PTYPE_FILECORE;	break;
157 	case FS_EX2FS:		str = DKW_PTYPE_EXT2FS;		break;
158 	case FS_NTFS:		str = DKW_PTYPE_NTFS;		break;
159 	case FS_RAID:		str = DKW_PTYPE_RAIDFRAME;	break;
160 	case FS_CCD:		str = DKW_PTYPE_CCD;		break;
161 	case FS_APPLEUFS:	str = DKW_PTYPE_APPLEUFS;	break;
162 	default:		str = NULL;			break;
163 	}
164 
165 	return (str);
166 }
167 
168 static void
169 swap_disklabel(struct disklabel *lp)
170 {
171 	int i;
172 
173 #define	SWAP16(x)	lp->x = bswap16(lp->x)
174 #define	SWAP32(x)	lp->x = bswap32(lp->x)
175 
176 	SWAP32(d_magic);
177 	SWAP16(d_type);
178 	SWAP16(d_subtype);
179 	SWAP32(d_secsize);
180 	SWAP32(d_nsectors);
181 	SWAP32(d_ntracks);
182 	SWAP32(d_ncylinders);
183 	SWAP32(d_secpercyl);
184 	SWAP32(d_secperunit);
185 	SWAP16(d_sparespertrack);
186 	SWAP16(d_sparespercyl);
187 	SWAP32(d_acylinders);
188 	SWAP16(d_rpm);
189 	SWAP16(d_interleave);
190 	SWAP16(d_trackskew);
191 	SWAP16(d_cylskew);
192 	SWAP32(d_headswitch);
193 	SWAP32(d_trkseek);
194 	SWAP32(d_flags);
195 
196 	for (i = 0; i < NDDATA; i++)
197 		SWAP32(d_drivedata[i]);
198 	for (i = 0; i < NSPARE; i++)
199 		SWAP32(d_spare[i]);
200 
201 	SWAP32(d_magic2);
202 	SWAP16(d_checksum);
203 	SWAP16(d_npartitions);
204 	SWAP32(d_bbsize);
205 	SWAP32(d_sbsize);
206 
207 	for (i = 0; i < lp->d_npartitions; i++) {
208 		SWAP32(d_partitions[i].p_size);
209 		SWAP32(d_partitions[i].p_offset);
210 		SWAP32(d_partitions[i].p_fsize);
211 		SWAP16(d_partitions[i].p_cpg);
212 	}
213 
214 #undef SWAP16
215 #undef SWAP32
216 }
217 
218 /*
219  * Add wedges for a valid NetBSD disklabel.
220  */
221 static void
222 addwedges(const mbr_args_t *a, const struct disklabel *lp)
223 {
224 	int error, i;
225 
226 	for (i = 0; i < lp->d_npartitions; i++) {
227 		struct dkwedge_info dkw;
228 		const struct partition *p;
229 		const char *ptype;
230 
231 		p = &lp->d_partitions[i];
232 
233 		if (p->p_fstype == FS_UNUSED)
234 			continue;
235 		if ((ptype = bsdlabel_fstype_to_str(p->p_fstype)) == NULL) {
236 			/*
237 			 * XXX Should probably just add these...
238 			 * XXX maybe just have an empty ptype?
239 			 */
240 			aprint_verbose("%s: skipping partition %d, type %d\n",
241 			    a->pdk->dk_name, i, p->p_fstype);
242 			continue;
243 		}
244 		strcpy(dkw.dkw_ptype, ptype);
245 
246 		strcpy(dkw.dkw_parent, a->pdk->dk_name);
247 		dkw.dkw_offset = p->p_offset;
248 		dkw.dkw_size = p->p_size;
249 
250 		/*
251 		 * These get historical disk naming style
252 		 * wedge names.
253 		 */
254 		snprintf((char *)&dkw.dkw_wname, sizeof(dkw.dkw_wname),
255 		    "%s%c", a->pdk->dk_name, 'a' + i);
256 
257 		error = dkwedge_add(&dkw);
258 		if (error == EEXIST)
259 			aprint_error("%s: wedge named '%s' already "
260 			    "exists, manual intervention required\n",
261 			    a->pdk->dk_name, dkw.dkw_wname);
262 		else if (error)
263 			aprint_error("%s: error %d adding partition "
264 			    "%d type %d\n", a->pdk->dk_name, error,
265 			    i, p->p_fstype);
266 	}
267 }
268 
269 static int
270 validate_label(mbr_args_t *a, daddr_t label_sector, size_t label_offset)
271 {
272 	struct disklabel *lp;
273 	void *lp_lim;
274 	int error, swapped;
275 	uint16_t npartitions;
276 
277 	error = dkwedge_read(a->pdk, a->vp, label_sector, a->buf, DEV_BSIZE);
278 	if (error) {
279 		aprint_error("%s: unable to read BSD disklabel @ %" PRId64
280 		    ", error = %d\n", a->pdk->dk_name, label_sector, error);
281 		a->error = error;
282 		return (SCAN_ERROR);
283 	}
284 
285 	/*
286 	 * We ignore label_offset; this seems to have not been used
287 	 * consistently in the old code, requiring us to do the search
288 	 * in the sector.
289 	 */
290 	lp = a->buf;
291 	lp_lim = (char *)a->buf + DEV_BSIZE - DISKLABEL_MINSIZE;
292 	for (;; lp = (void *)((char *)lp + sizeof(uint32_t))) {
293 		if ((char *)lp > (char *)lp_lim)
294 			return (SCAN_CONTINUE);
295 		label_offset = (size_t)((char *)lp - (char *)a->buf);
296 		if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC) {
297 			if (lp->d_magic != bswap32(DISKMAGIC) ||
298 			    lp->d_magic2 != bswap32(DISKMAGIC))
299 				continue;
300 			/* Label is in the other byte order. */
301 			swapped = 1;
302 		} else
303 			swapped = 0;
304 
305 		npartitions = (swapped) ? bswap16(lp->d_npartitions)
306 					: lp->d_npartitions;
307 
308 		/* Validate label length. */
309 		if ((char *)lp + DISKLABEL_SIZE(npartitions) >
310 		    (char *)a->buf + DEV_BSIZE) {
311 			aprint_error("%s: BSD disklabel @ "
312 			    "%" PRId64 "+%zd has bogus partition count (%u)\n",
313 			    a->pdk->dk_name, label_sector, label_offset,
314 			    npartitions);
315 			continue;
316 		}
317 
318 		/*
319 		 * We have validated the partition count.  Checksum it.
320 		 * Note that we purposefully checksum before swapping
321 		 * the byte order.
322 		 */
323 		if (dkcksum_sized(lp, npartitions) != 0) {
324 			aprint_error("%s: BSD disklabel @ %" PRId64
325 			    "+%zd has bad checksum\n", a->pdk->dk_name,
326 			    label_sector, label_offset);
327 			continue;
328 		}
329 		/* Put the disklabel in the right order. */
330 		if (swapped)
331 			swap_disklabel(lp);
332 		addwedges(a, lp);
333 		return (SCAN_FOUND);
334 	}
335 }
336 
337 static int
338 scan_mbr(mbr_args_t *a, int (*actn)(mbr_args_t *, struct mbr_partition *,
339 				    int, u_int))
340 {
341 	struct mbr_partition ptns[MBR_PART_COUNT];
342 	struct mbr_partition *dp;
343 	struct mbr_sector *mbr;
344 	u_int ext_base, this_ext, next_ext;
345 	int i, rval;
346 #ifdef COMPAT_386BSD_MBRPART
347 	int dp_386bsd = -1;
348 #endif
349 
350 	ext_base = 0;
351 	this_ext = 0;
352 	for (;;) {
353 		a->error = dkwedge_read(a->pdk, a->vp, this_ext, a->buf,
354 					DEV_BSIZE);
355 		if (a->error) {
356 			aprint_error("%s: unable to read MBR @ %u, "
357 			    "error = %d\n", a->pdk->dk_name, this_ext,
358 			    a->error);
359 			return (SCAN_ERROR);
360 		}
361 
362 		mbr = a->buf;
363 		if (mbr->mbr_magic != htole16(MBR_MAGIC))
364 			return (SCAN_CONTINUE);
365 
366 		/* Copy data out of buffer so action can use the buffer. */
367 		memcpy(ptns, &mbr->mbr_parts, sizeof(ptns));
368 
369 		/* Looks for NetBSD partition. */
370 		next_ext = 0;
371 		dp = ptns;
372 		for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
373 			if (dp->mbrp_type == 0)
374 				continue;
375 			if (MBR_IS_EXTENDED(dp->mbrp_type)) {
376 				next_ext = le32toh(dp->mbrp_start);
377 				continue;
378 			}
379 #ifdef COMPAT_386BSD_MBRPART
380 			if (dp->mbrp_type == MBR_PTYPE_386BSD) {
381 				/*
382 				 * If more than one matches, take last,
383 				 * as NetBSD install tool does.
384 				 */
385 				if (this_ext == 0)
386 					dp_386bsd = i;
387 				continue;
388 			}
389 #endif
390 			rval = (*actn)(a, dp, i, this_ext);
391 			if (rval != SCAN_CONTINUE)
392 				return (rval);
393 		}
394 		if (next_ext == 0)
395 			break;
396 		if (ext_base == 0) {
397 			ext_base = next_ext;
398 			next_ext = 0;
399 		}
400 		next_ext += ext_base;
401 		if (next_ext <= this_ext)
402 			break;
403 		this_ext = next_ext;
404 	}
405 #ifdef COMPAT_386BSD_MBRPART
406 	if (this_ext == 0 && dp_386bsd != -1)
407 		return ((*actn)(a, &ptns[dp_386bsd], dp_386bsd, 0));
408 #endif
409 	return (SCAN_CONTINUE);
410 }
411 
412 static int
413 look_netbsd_part(mbr_args_t *a, struct mbr_partition *dp, int slot,
414     u_int ext_base)
415 {
416 	int ptn_base = ext_base + le32toh(dp->mbrp_start);
417 	int rval;
418 
419 	if (
420 #ifdef COMPAT_386BSD_MBRPART
421 	    dp->mbrp_type == MBR_PTYPE_386BSD ||
422 #endif
423 	    dp->mbrp_type == MBR_PTYPE_NETBSD) {
424 		rval = validate_label(a, ptn_base + BSD44_MBR_LABELSECTOR, 0);
425 
426 		/* If we got a NetBSD label, look no further. */
427 		if (rval == SCAN_FOUND)
428 			return (rval);
429 	}
430 
431 	return (SCAN_CONTINUE);
432 }
433 
434 #ifdef _KERNEL
435 #define	DKW_MALLOC(SZ)	malloc((SZ), M_DEVBUF, M_WAITOK)
436 #define	DKW_FREE(PTR)	free((PTR), M_DEVBUF)
437 #else
438 #define	DKW_MALLOC(SZ)	malloc((SZ))
439 #define	DKW_FREE(PTR)	free((PTR))
440 #endif
441 
442 static int
443 dkwedge_discover_bsdlabel(struct disk *pdk, struct vnode *vp)
444 {
445 	mbr_args_t a;
446 	const struct disklabel_location *dl;
447 	int rval;
448 
449 	a.pdk = pdk;
450 	a.vp = vp;
451 	a.buf = DKW_MALLOC(DEV_BSIZE);
452 	a.error = 0;
453 
454 	/* MBR search. */
455 	rval = scan_mbr(&a, look_netbsd_part);
456 	if (rval != SCAN_CONTINUE) {
457 		if (rval == SCAN_FOUND)
458 			a.error = 0;	/* found it, wedges installed */
459 		goto out;
460 	}
461 
462 	/* Known location search. */
463 	for (dl = disklabel_locations; dl->label_sector != -1; dl++) {
464 		rval = validate_label(&a, dl->label_sector, dl->label_offset);
465 		if (rval != SCAN_CONTINUE) {
466 			if (rval == SCAN_FOUND)
467 				a.error = 0;	/* found it, wedges installed */
468 			goto out;
469 		}
470 	}
471 
472 	/* No NetBSD disklabel found. */
473 	a.error = ESRCH;
474  out:
475 	DKW_FREE(a.buf);
476 	return (a.error);
477 }
478 
479 #ifdef _KERNEL
480 DKWEDGE_DISCOVERY_METHOD_DECL(BSD44, 5, dkwedge_discover_bsdlabel);
481 #endif
482