xref: /dragonfly/sys/kern/subr_diskmbr.c (revision 49781055)
1 /*-
2  * Copyright (c) 1994 Bruce D. Evans.
3  * All rights reserved.
4  *
5  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	from: @(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
37  *	from: ufs_disksubr.c,v 1.8 1994/06/07 01:21:39 phk Exp $
38  * $FreeBSD: src/sys/kern/subr_diskmbr.c,v 1.45 2000/01/28 10:22:07 bde Exp $
39  * $DragonFly: src/sys/kern/subr_diskmbr.c,v 1.11 2006/02/17 19:18:06 dillon Exp $
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/buf.h>
45 #include <sys/conf.h>
46 #include <sys/disklabel.h>
47 #define	DOSPTYP_EXTENDED	5
48 #define	DOSPTYP_EXTENDEDX	15
49 #define	DOSPTYP_ONTRACK		84
50 #include <sys/diskslice.h>
51 #include <sys/diskmbr.h>
52 #include <sys/malloc.h>
53 #include <sys/syslog.h>
54 #include <sys/device.h>
55 
56 #define TRACE(str)	do { if (dsi_debug) printf str; } while (0)
57 
58 static volatile u_char dsi_debug;
59 
60 static struct dos_partition historical_bogus_partition_table[NDOSPART] = {
61 	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
62 	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
63 	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
64 	{ 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, },
65 };
66 
67 static int check_part (char *sname, struct dos_partition *dp,
68 			   u_long offset, int nsectors, int ntracks,
69 			   u_long mbr_offset);
70 static void mbr_extended (dev_t dev, struct disklabel *lp,
71 			      struct diskslices *ssp, u_long ext_offset,
72 			      u_long ext_size, u_long base_ext_offset,
73 			      int nsectors, int ntracks, u_long mbr_offset,
74 			      int level);
75 static int mbr_setslice (char *sname, struct disklabel *lp,
76 			     struct diskslice *sp, struct dos_partition *dp,
77 			     u_long br_offset);
78 
79 static int
80 check_part(char *sname, struct dos_partition *dp, u_long offset,
81 	    int nsectors, int ntracks, u_long mbr_offset)
82 {
83 	int	chs_ecyl;
84 	int	chs_esect;
85 	int	chs_scyl;
86 	int	chs_ssect;
87 	int	error;
88 	u_long	esector;
89 	u_long	esector1;
90 	u_long	secpercyl;
91 	u_long	ssector;
92 	u_long	ssector1;
93 
94 	secpercyl = (u_long)nsectors * ntracks;
95 	chs_scyl = DPCYL(dp->dp_scyl, dp->dp_ssect);
96 	chs_ssect = DPSECT(dp->dp_ssect);
97 	ssector = chs_ssect - 1 + dp->dp_shd * nsectors + chs_scyl * secpercyl
98 		  + mbr_offset;
99 	ssector1 = offset + dp->dp_start;
100 
101 	/*
102 	 * If ssector1 is on a cylinder >= 1024, then ssector can't be right.
103 	 * Allow the C/H/S for it to be 1023/ntracks-1/nsectors, or correct
104 	 * apart from the cylinder being reduced modulo 1024.  Always allow
105 	 * 1023/255/63.
106 	 */
107 	if ((ssector < ssector1
108 	     && ((chs_ssect == nsectors && dp->dp_shd == ntracks - 1
109 		  && chs_scyl == 1023)
110 		 || (secpercyl != 0
111 		     && (ssector1 - ssector) % (1024 * secpercyl) == 0)))
112 	    || (dp->dp_scyl == 255 && dp->dp_shd == 255
113 		&& dp->dp_ssect == 255)) {
114 		TRACE(("%s: C/H/S start %d/%d/%d, start %lu: allow\n",
115 		       sname, chs_scyl, dp->dp_shd, chs_ssect, ssector1));
116 		ssector = ssector1;
117 	}
118 
119 	chs_ecyl = DPCYL(dp->dp_ecyl, dp->dp_esect);
120 	chs_esect = DPSECT(dp->dp_esect);
121 	esector = chs_esect - 1 + dp->dp_ehd * nsectors + chs_ecyl * secpercyl
122 		  + mbr_offset;
123 	esector1 = ssector1 + dp->dp_size - 1;
124 
125 	/* Allow certain bogus C/H/S values for esector, as above. */
126 	if ((esector < esector1
127 	     && ((chs_esect == nsectors && dp->dp_ehd == ntracks - 1
128 		  && chs_ecyl == 1023)
129 		 || (secpercyl != 0
130 		     && (esector1 - esector) % (1024 * secpercyl) == 0)))
131 	    || (dp->dp_ecyl == 255 && dp->dp_ehd == 255
132 		&& dp->dp_esect == 255)) {
133 		TRACE(("%s: C/H/S end %d/%d/%d, end %lu: allow\n",
134 		       sname, chs_ecyl, dp->dp_ehd, chs_esect, esector1));
135 		esector = esector1;
136 	}
137 
138 	error = (ssector == ssector1 && esector == esector1) ? 0 : EINVAL;
139 	if (bootverbose)
140 		printf("%s: type 0x%x, start %lu, end = %lu, size %lu %s\n",
141 		       sname, dp->dp_typ, ssector1, esector1,
142 		       (u_long)dp->dp_size, error ? "" : ": OK");
143 	if (ssector != ssector1 && bootverbose)
144 		printf("%s: C/H/S start %d/%d/%d (%lu) != start %lu: invalid\n",
145 		       sname, chs_scyl, dp->dp_shd, chs_ssect,
146 		       ssector, ssector1);
147 	if (esector != esector1 && bootverbose)
148 		printf("%s: C/H/S end %d/%d/%d (%lu) != end %lu: invalid\n",
149 		       sname, chs_ecyl, dp->dp_ehd, chs_esect,
150 		       esector, esector1);
151 	return (error);
152 }
153 
154 int
155 dsinit(dev_t dev, struct disklabel *lp, struct diskslices **sspp)
156 {
157 	struct buf *bp;
158 	u_char	*cp;
159 	int	dospart;
160 	struct dos_partition *dp;
161 	struct dos_partition *dp0;
162 	struct dos_partition dpcopy[NDOSPART];
163 	int	error;
164 	int	max_ncyls;
165 	int	max_nsectors;
166 	int	max_ntracks;
167 	u_long	mbr_offset;
168 	char	partname[2];
169 	u_long	secpercyl;
170 	char	*sname;
171 	struct diskslice *sp;
172 	struct diskslices *ssp;
173 	dev_t wdev;
174 
175 	mbr_offset = DOSBBSECTOR;
176 reread_mbr:
177 	/* Read master boot record. */
178 	wdev = dkmodpart(dkmodslice(dev, WHOLE_DISK_SLICE), RAW_PART);
179 	bp = geteblk((int)lp->d_secsize);
180 	bp->b_bio1.bio_blkno = mbr_offset;
181 	bp->b_bcount = lp->d_secsize;
182 	bp->b_flags |= B_READ;
183 	dev_dstrategy(wdev, &bp->b_bio1);
184 	if (biowait(bp) != 0) {
185 		diskerr(&bp->b_bio1, wdev,
186 			"reading primary partition table: error",
187 			LOG_PRINTF, 0, (struct disklabel *)NULL);
188 		printf("\n");
189 		error = EIO;
190 		goto done;
191 	}
192 
193 	/* Weakly verify it. */
194 	cp = bp->b_data;
195 	sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, RAW_PART, partname);
196 	if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
197 		if (bootverbose)
198 			printf("%s: invalid primary partition table: no magic\n",
199 			       sname);
200 		error = EINVAL;
201 		goto done;
202 	}
203 
204 	/* Make a copy of the partition table to avoid alignment problems. */
205 	memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy));
206 
207 	dp0 = &dpcopy[0];
208 
209 	/* Check for "Ontrack Diskmanager". */
210 	for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
211 		if (dp->dp_typ == DOSPTYP_ONTRACK) {
212 			if (bootverbose)
213 				printf(
214 	    "%s: Found \"Ontrack Disk Manager\" on this disk.\n", sname);
215 			bp->b_flags |= B_INVAL | B_AGE;
216 			brelse(bp);
217 			mbr_offset = 63;
218 			goto reread_mbr;
219 		}
220 	}
221 
222 	if (bcmp(dp0, historical_bogus_partition_table,
223 		 sizeof historical_bogus_partition_table) == 0) {
224 		TRACE(("%s: invalid primary partition table: historical\n",
225 		       sname));
226 		error = EINVAL;
227 		goto done;
228 	}
229 
230 	/* Guess the geometry. */
231 	/*
232 	 * TODO:
233 	 * Perhaps skip entries with 0 size.
234 	 * Perhaps only look at entries of type DOSPTYP_386BSD.
235 	 */
236 	max_ncyls = 0;
237 	max_nsectors = 0;
238 	max_ntracks = 0;
239 	for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
240 		int	ncyls;
241 		int	nsectors;
242 		int	ntracks;
243 
244 		ncyls = DPCYL(dp->dp_ecyl, dp->dp_esect) + 1;
245 		if (max_ncyls < ncyls)
246 			max_ncyls = ncyls;
247 		nsectors = DPSECT(dp->dp_esect);
248 		if (max_nsectors < nsectors)
249 			max_nsectors = nsectors;
250 		ntracks = dp->dp_ehd + 1;
251 		if (max_ntracks < ntracks)
252 			max_ntracks = ntracks;
253 	}
254 
255 	/*
256 	 * Check that we have guessed the geometry right by checking the
257 	 * partition entries.
258 	 */
259 	/*
260 	 * TODO:
261 	 * As above.
262 	 * Check for overlaps.
263 	 * Check against d_secperunit if the latter is reliable.
264 	 */
265 	error = 0;
266 	for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++) {
267 		if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0
268 		    && dp->dp_start == 0 && dp->dp_size == 0)
269 			continue;
270 		sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
271 			       RAW_PART, partname);
272 
273 		/*
274 		 * Temporarily ignore errors from this check.  We could
275 		 * simplify things by accepting the table eariler if we
276 		 * always ignore errors here.  Perhaps we should always
277 		 * accept the table if the magic is right but not let
278 		 * bad entries affect the geometry.
279 		 */
280 		check_part(sname, dp, mbr_offset, max_nsectors, max_ntracks,
281 			   mbr_offset);
282 	}
283 	if (error != 0)
284 		goto done;
285 
286 	/*
287 	 * Accept the DOS partition table.
288 	 * First adjust the label (we have been careful not to change it
289 	 * before we can guarantee success).
290 	 */
291 	secpercyl = (u_long)max_nsectors * max_ntracks;
292 	if (secpercyl != 0) {
293 #if 0
294 		u_long	secperunit;
295 #endif
296 
297 		lp->d_nsectors = max_nsectors;
298 		lp->d_ntracks = max_ntracks;
299 		lp->d_secpercyl = secpercyl;
300 		/*
301 		 * Temporarily, don't even consider adjusting the drive's
302 		 * size, since the adjusted size may exceed the hardware's
303 		 * addressing capabilities.  The adjustment helped mainly
304 		 * for ancient MFM drives with > 1024 cylinders, but now
305 		 * breaks at least IDE drives with 63*16*65536 sectors if
306 		 * they are controlled by the wd driver in CHS mode.
307 		 */
308 #if 0
309 		secperunit = secpercyl * max_ncyls;
310 		if (lp->d_secperunit < secperunit)
311 			lp->d_secperunit = secperunit;
312 #endif
313 		lp->d_ncylinders = lp->d_secperunit / secpercyl;
314 	}
315 
316 	/*
317 	 * We are passed a pointer to a suitably initialized minimal
318 	 * slices "struct" with no dangling pointers in it.  Replace it
319 	 * by a maximal one.  This usually oversizes the "struct", but
320 	 * enlarging it while searching for logical drives would be
321 	 * inconvenient.
322 	 */
323 	free(*sspp, M_DEVBUF);
324 	ssp = dsmakeslicestruct(MAX_SLICES, lp);
325 	*sspp = ssp;
326 
327 	/* Initialize normal slices. */
328 	sp = &ssp->dss_slices[BASE_SLICE];
329 	for (dospart = 0, dp = dp0; dospart < NDOSPART; dospart++, dp++, sp++) {
330 		sname = dsname(dev, dkunit(dev), BASE_SLICE + dospart,
331 			       RAW_PART, partname);
332 		(void)mbr_setslice(sname, lp, sp, dp, mbr_offset);
333 	}
334 	ssp->dss_nslices = BASE_SLICE + NDOSPART;
335 
336 	/* Handle extended partitions. */
337 	sp -= NDOSPART;
338 	for (dospart = 0; dospart < NDOSPART; dospart++, sp++) {
339 		if (sp->ds_type == DOSPTYP_EXTENDED ||
340 		    sp->ds_type == DOSPTYP_EXTENDEDX) {
341 			mbr_extended(wdev, lp, ssp,
342 				     sp->ds_offset, sp->ds_size, sp->ds_offset,
343 				     max_nsectors, max_ntracks, mbr_offset, 1);
344 		}
345 	}
346 
347 	/*
348 	 * mbr_extended() abuses ssp->dss_nslices for the number of slices
349 	 * that would be found if there were no limit on the number of slices
350 	 * in *ssp.  Cut it back now.
351 	 */
352 	if (ssp->dss_nslices > MAX_SLICES)
353 		ssp->dss_nslices = MAX_SLICES;
354 
355 done:
356 	bp->b_flags |= B_INVAL | B_AGE;
357 	brelse(bp);
358 	if (error == EINVAL)
359 		error = 0;
360 	return (error);
361 }
362 
363 void
364 mbr_extended(dev_t dev, struct disklabel *lp, struct diskslices *ssp,
365 	    u_long ext_offset, u_long ext_size, u_long base_ext_offset,
366 	    int nsectors, int ntracks, u_long mbr_offset, int level)
367 {
368 	struct buf *bp;
369 	u_char	*cp;
370 	int	dospart;
371 	struct dos_partition *dp;
372 	struct dos_partition dpcopy[NDOSPART];
373 	u_long	ext_offsets[NDOSPART];
374 	u_long	ext_sizes[NDOSPART];
375 	char	partname[2];
376 	int	slice;
377 	char	*sname;
378 	struct diskslice *sp;
379 
380 	if (level >= 16) {
381 		printf(
382 	"%s: excessive recursion in search for slices; aborting search\n",
383 		       devtoname(dev));
384 		return;
385 	}
386 
387 	/* Read extended boot record. */
388 	bp = geteblk((int)lp->d_secsize);
389 	bp->b_bio1.bio_blkno = ext_offset;
390 	bp->b_bcount = lp->d_secsize;
391 	bp->b_flags |= B_READ;
392 	dev_dstrategy(dev, &bp->b_bio1);
393 	if (biowait(bp) != 0) {
394 		diskerr(&bp->b_bio1, dev,
395 			"reading extended partition table: error",
396 			LOG_PRINTF, 0, (struct disklabel *)NULL);
397 		printf("\n");
398 		goto done;
399 	}
400 
401 	/* Weakly verify it. */
402 	cp = bp->b_data;
403 	if (cp[0x1FE] != 0x55 || cp[0x1FF] != 0xAA) {
404 		sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE, RAW_PART,
405 			       partname);
406 		if (bootverbose)
407 			printf("%s: invalid extended partition table: no magic\n",
408 			       sname);
409 		goto done;
410 	}
411 
412 	/* Make a copy of the partition table to avoid alignment problems. */
413 	memcpy(&dpcopy[0], cp + DOSPARTOFF, sizeof(dpcopy));
414 
415 	slice = ssp->dss_nslices;
416 	for (dospart = 0, dp = &dpcopy[0]; dospart < NDOSPART;
417 	    dospart++, dp++) {
418 		ext_sizes[dospart] = 0;
419 		if (dp->dp_scyl == 0 && dp->dp_shd == 0 && dp->dp_ssect == 0
420 		    && dp->dp_start == 0 && dp->dp_size == 0)
421 			continue;
422 		if (dp->dp_typ == DOSPTYP_EXTENDED ||
423 		    dp->dp_typ == DOSPTYP_EXTENDEDX) {
424 			static char buf[32];
425 
426 			sname = dsname(dev, dkunit(dev), WHOLE_DISK_SLICE,
427 				       RAW_PART, partname);
428 			snprintf(buf, sizeof(buf), "%s", sname);
429 			if (strlen(buf) < sizeof buf - 11)
430 				strcat(buf, "<extended>");
431 			check_part(buf, dp, base_ext_offset, nsectors,
432 				   ntracks, mbr_offset);
433 			ext_offsets[dospart] = base_ext_offset + dp->dp_start;
434 			ext_sizes[dospart] = dp->dp_size;
435 		} else {
436 			sname = dsname(dev, dkunit(dev), slice, RAW_PART,
437 				       partname);
438 			check_part(sname, dp, ext_offset, nsectors, ntracks,
439 				   mbr_offset);
440 			if (slice >= MAX_SLICES) {
441 				printf("%s: too many slices\n", sname);
442 				slice++;
443 				continue;
444 			}
445 			sp = &ssp->dss_slices[slice];
446 			if (mbr_setslice(sname, lp, sp, dp, ext_offset) != 0)
447 				continue;
448 			slice++;
449 		}
450 	}
451 	ssp->dss_nslices = slice;
452 
453 	/* If we found any more slices, recursively find all the subslices. */
454 	for (dospart = 0; dospart < NDOSPART; dospart++) {
455 		if (ext_sizes[dospart] != 0) {
456 			mbr_extended(dev, lp, ssp, ext_offsets[dospart],
457 				     ext_sizes[dospart], base_ext_offset,
458 				     nsectors, ntracks, mbr_offset, ++level);
459 		}
460 	}
461 
462 done:
463 	bp->b_flags |= B_INVAL | B_AGE;
464 	brelse(bp);
465 }
466 
467 static int
468 mbr_setslice(char *sname, struct disklabel *lp, struct diskslice *sp,
469 	    struct dos_partition *dp, u_long br_offset)
470 {
471 	u_long	offset;
472 	u_long	size;
473 
474 	offset = br_offset + dp->dp_start;
475 	if (offset > lp->d_secperunit || offset < br_offset) {
476 		printf(
477 		"%s: slice starts beyond end of the disk: rejecting it\n",
478 		       sname);
479 		return (1);
480 	}
481 	size = lp->d_secperunit - offset;
482 	if (size >= dp->dp_size)
483 		size = dp->dp_size;
484 	else
485 		printf(
486 "%s: slice extends beyond end of disk: truncating from %lu to %lu sectors\n",
487 		       sname, (u_long)dp->dp_size, size);
488 	sp->ds_offset = offset;
489 	sp->ds_size = size;
490 	sp->ds_type = dp->dp_typ;
491 #if 0
492 	lp->d_subtype |= (lp->d_subtype & 3) | dospart | DSTYPE_INDOSPART;
493 #endif
494 	return (0);
495 }
496