xref: /netbsd/sys/arch/x68k/x68k/disksubr.c (revision 6550d01e)
1 /*	$NetBSD: disksubr.c,v 1.33 2008/01/02 11:48:32 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.33 2008/01/02 11:48:32 ad Exp $");
36 
37 #include "opt_compat_netbsd.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/buf.h>
42 #include <sys/disklabel.h>
43 #include <sys/syslog.h>
44 #include <sys/disk.h>
45 
46 /* get rid of DEV_BSIZE dependency */
47 #define DEF_BSIZE	DEV_BSIZE  /* default sector size = 512 */
48 
49 static void parttbl_consistency_check(struct disklabel *,
50 				      struct dos_partition *);
51 
52 
53 /*
54  * Attempt to read a disk label from a device
55  * using the indicated strategy routine.
56  * The label must be partly set up before this:
57  * secpercyl, secsize and anything required for a block i/o read
58  * operation in the driver's strategy/start routines
59  * must be filled in before calling us.
60  *
61  * Returns null on success and an error string on failure.
62  */
63 const char *
64 readdisklabel(dev_t dev, void (*strat)(struct buf *),
65     struct disklabel *lp, struct cpu_disklabel *osdep)
66 {
67 	struct dos_partition *dp = 0;
68 	struct dkbad *bdp = &osdep->bad;
69 	struct buf *bp;
70 	struct disklabel *dlp;
71 	const char *msg = NULL;
72 	int i, labelsz;
73 
74 	if (osdep)
75 		dp = osdep->dosparts;
76 	/* minimal requirements for archtypal disk label */
77 	if (lp->d_secsize == 0)
78 		lp->d_secsize = DEF_BSIZE;
79 	if (lp->d_secperunit == 0)
80 		lp->d_secperunit = 0x1fffffff;
81 	if (lp->d_secpercyl == 0)
82 		lp->d_secpercyl = 0x1fffffff;
83 	lp->d_npartitions = RAW_PART + 1;
84 	for (i = 0; i < RAW_PART; i++) {
85 		lp->d_partitions[i].p_size = 0;
86 		lp->d_partitions[i].p_offset = 0;
87 	}
88 	if (lp->d_partitions[0].p_size == 0)
89 		lp->d_partitions[0].p_size = 0x1fffffff;
90 	lp->d_partitions[0].p_offset = 0;
91 
92 	/* get a buffer and initialize it */
93 	bp = geteblk((int)lp->d_secsize);
94 	bp->b_dev = dev;
95 
96 	/* read BSD disklabel first */
97 	bp->b_blkno = LABELSECTOR;
98 	bp->b_cylinder = LABELSECTOR/lp->d_secpercyl;
99 	labelsz = howmany(LABELOFFSET+sizeof(struct disklabel), lp->d_secsize)
100 		* lp->d_secsize;
101 	bp->b_bcount = labelsz;	/* to support < 512B/sector disks */
102 	bp->b_flags |= B_READ;
103 	(*strat)(bp);
104 
105 	/* if successful, locate disk label within block and validate */
106 	if (biowait(bp)) {
107 		msg = "disk label I/O error";
108 		goto dodospart;
109 	}
110 	for (dlp = (struct disklabel *)bp->b_data;
111 	     dlp <= (struct disklabel *)
112 		((char *)bp->b_data + labelsz - sizeof(*dlp));
113 	     dlp = (struct disklabel *)((uint8_t *)dlp + sizeof(long))) {
114 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
115 			if (msg == NULL)
116 				msg = "no disk label";
117 		} else if (dlp->d_npartitions > MAXPARTITIONS ||
118 			   dkcksum(dlp) != 0)
119 			msg = "disk label corrupted";
120 		else {
121 			*lp = *dlp;
122 			msg = NULL;
123 			break;
124 		}
125 	}
126 
127 dodospart:
128 	/* next do the Human68k-style partition table */
129 	/* Human68k does not support > 2048B/sector devices (?) */
130 	if (lp->d_secsize >= 2048) {
131 		if (msg)
132 			goto done;
133 		goto dobadsect;
134 	}
135 	bp->b_blkno = DOSPARTOFF * DEF_BSIZE / lp->d_secsize;
136 				/* DOSPARTOFF in DEV_BSIZE unit */
137 	bp->b_cylinder = DOSBBSECTOR / lp->d_secpercyl;
138 	labelsz = howmany(sizeof(struct cpu_disklabel),
139 			  lp->d_secsize) * lp->d_secsize;
140 	bp->b_bcount = labelsz;	/* to support < 512B/sector disks */
141 	bp->b_oflags &= ~(BO_DONE);
142 	(*strat)(bp);
143 
144 	/* if successful, wander through Human68k partition table */
145 	if (biowait(bp))
146 		goto done;
147 	if (strncmp(bp->b_data, "X68K", 4) != 0) {
148 		/* Human68k-style partition table does not exist */
149 		if (msg)
150 			goto done;
151 		goto dobadsect;
152 	}
153 
154 	/* XXX how do we check veracity/bounds of this? */
155 	if (dp)
156 		memcpy(dp, (char *)bp->b_data + sizeof(*dp) /*DOSPARTOFF*/,
157 		    NDOSPART * sizeof(*dp));
158 	else
159 		dp = (void *)((char *)bp->b_data + sizeof(*dp) /*DOSPARTOFF*/);
160 
161 	/* if BSD disklabel does not exist, fall back to Human68k partition */
162 	if (msg != NULL) {
163 		msg = NULL;
164 		lp->d_bbsize = 8192;
165 		lp->d_sbsize = 2048;
166 		for (i = 0; i < NDOSPART; i++, dp++)
167 			/* is this ours? */
168 			if (dp->dp_size) {
169 				u_char fstype;
170 				int part = i + (i < RAW_PART ? 0 : 1);
171 				int start = dp->dp_start * 2;
172 				int size = dp->dp_size * 2;
173 
174 				/* update disklabel with details */
175 				lp->d_partitions[part].p_size = size;
176 				lp->d_partitions[part].p_offset =  start;
177 				/* get partition type */
178 #ifndef COMPAT_10
179 				if (dp->dp_flag == 1)
180 					fstype = FS_UNUSED;
181 				else
182 #endif
183 				if (!memcmp(dp->dp_typname, "Human68k", 8))
184 					fstype = FS_MSDOS;
185 				else if (!memcmp(dp->dp_typname,
186 					     "BSD ffs ", 8))
187 					fstype = FS_BSDFFS;
188 				else if (!memcmp(dp->dp_typname,
189 					     "BSD lfs ", 8))
190 					fstype = FS_BSDLFS;
191 				else if (!memcmp(dp->dp_typname,
192 					     "BSD swap", 8))
193 					fstype = FS_SWAP;
194 #ifndef COMPAT_14
195 				else if (part == 1)
196 					fstype = FS_SWAP;
197 #endif
198 				else
199 					fstype = FS_BSDFFS; /* XXX */
200 				lp->d_partitions[part].p_fstype = fstype; /* XXX */
201 				if (lp->d_npartitions <= part)
202 					lp->d_npartitions = part + 1;
203 			}
204 	} else {
205 		parttbl_consistency_check(lp, dp);
206 	}
207 
208 dobadsect:
209 	/* obtain bad sector table if requested and present */
210 	if (bdp && (lp->d_flags & D_BADSECT)) {
211 		struct dkbad *db;
212 
213 		i = 0;
214 		do {
215 			/* read a bad sector table */
216 			bp->b_oflags &= ~(BO_DONE);
217 			bp->b_flags |= B_READ;
218 			bp->b_blkno = lp->d_secperunit - lp->d_nsectors + i;
219 			if (lp->d_secsize > DEF_BSIZE)
220 				bp->b_blkno *= lp->d_secsize / DEF_BSIZE;
221 			else
222 				bp->b_blkno /= DEF_BSIZE / lp->d_secsize;
223 			bp->b_bcount = lp->d_secsize;
224 			bp->b_cylinder = lp->d_ncylinders - 1;
225 			(*strat)(bp);
226 
227 			/* if successful, validate, otherwise try another */
228 			if (biowait(bp)) {
229 				msg = "bad sector table I/O error";
230 			} else {
231 				db = (struct dkbad *)(bp->b_data);
232 #define DKBAD_MAGIC 0x4321
233 				if (db->bt_mbz == 0
234 					&& db->bt_flag == DKBAD_MAGIC) {
235 					msg = NULL;
236 					*bdp = *db;
237 					break;
238 				} else
239 					msg = "bad sector table corrupted";
240 			}
241 		} while (bp->b_error != 0 && (i += 2) < 10 &&
242 			i < lp->d_nsectors);
243 	}
244 
245 done:
246 	brelse(bp, 0);
247 	return (msg);
248 }
249 
250 /*
251  * Check new disk label for sensibility
252  * before setting it.
253  */
254 int
255 setdisklabel(struct disklabel *olp, struct disklabel *nlp, u_long openmask,
256     struct cpu_disklabel *osdep)
257 {
258 	int i;
259 	struct partition *opp, *npp;
260 
261 	/* sanity clause */
262 	if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0
263 		/*|| (nlp->d_secsize % DEV_BSIZE) != 0*/)
264 			return(EINVAL);
265 
266 	/* special case to allow disklabel to be invalidated */
267 	if (nlp->d_magic == 0xffffffff) {
268 		*olp = *nlp;
269 		return (0);
270 	}
271 
272 	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
273 	    dkcksum(nlp) != 0)
274 		return (EINVAL);
275 
276 	if (osdep)
277 		parttbl_consistency_check(nlp, osdep->dosparts);
278 	/* XXX missing check if other dos partitions will be overwritten */
279 
280 	while (openmask != 0) {
281 		i = ffs(openmask) - 1;
282 		openmask &= ~(1 << i);
283 		if (nlp->d_npartitions <= i)
284 			return (EBUSY);
285 		opp = &olp->d_partitions[i];
286 		npp = &nlp->d_partitions[i];
287 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
288 			return (EBUSY);
289 		/*
290 		 * Copy internally-set partition information
291 		 * if new label doesn't include it.		XXX
292 		 */
293 		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
294 			npp->p_fstype = opp->p_fstype;
295 			npp->p_fsize = opp->p_fsize;
296 			npp->p_frag = opp->p_frag;
297 			npp->p_cpg = opp->p_cpg;
298 		}
299 	}
300 	nlp->d_checksum = 0;
301 	nlp->d_checksum = dkcksum(nlp);
302 	*olp = *nlp;
303 	return (0);
304 }
305 
306 /*
307  * Write disk label back to device after modification.
308  */
309 int
310 writedisklabel(dev_t dev, void (*strat)(struct buf *),
311     struct disklabel *lp, struct cpu_disklabel *osdep)
312 {
313 	struct dos_partition *dp = 0;
314 	struct buf *bp;
315 	struct disklabel *dlp;
316 	int error, labelsz, i;
317 	const char *np;
318 
319 	if (osdep)
320 		dp = osdep->dosparts;
321 	/* sanity clause */
322 	if (lp->d_secpercyl == 0 || lp->d_secsize == 0
323 		/*|| (lp->d_secsize % DEF_BSIZE) != 0*/)
324 			return(EINVAL);
325 	if (dp)
326 		parttbl_consistency_check(lp, dp);
327 
328 	/* get a buffer and initialize it */
329 	bp = geteblk((int)lp->d_secsize);
330 	bp->b_dev = dev;
331 
332 	/* attempt to write BSD disklabel first */
333 	bp->b_blkno = LABELSECTOR;
334 	bp->b_cylinder = LABELSECTOR / lp->d_secpercyl;
335 	labelsz = howmany(LABELOFFSET+sizeof(struct disklabel), lp->d_secsize)
336 		* lp->d_secsize;
337 	bp->b_bcount = labelsz;	/* to support < 512B/sector disks */
338 	bp->b_flags |= B_READ;
339 	(*strat)(bp);
340 
341 	/* if successful, locate disk label within block and validate */
342 	if (biowait(bp))
343 		goto dodospart;
344 	error = ESRCH;
345 	for (dlp = (struct disklabel *)bp->b_data;
346 	     dlp <= (struct disklabel *)
347 		((char *)bp->b_data + labelsz - sizeof(*dlp));
348 	     dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
349 		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
350 		    dkcksum(dlp) == 0) {
351 			*dlp = *lp;
352 			bp->b_oflags &= ~(BO_DONE);
353 			bp->b_flags &= ~(B_READ);
354 			bp->b_flags |= B_WRITE;
355 			(*strat)(bp);
356 			error = biowait(bp);
357 			break;
358 		}
359 	}
360 
361 	/* do dos partitions in the process of getting disklabel? */
362 	if (error) {
363 dodospart:
364 		if (lp->d_secsize >= 2048) {
365 			error = ESRCH;
366 			goto done;
367 		}
368 #if 0				/* there is no mark on floppies */
369 		/* read the x68k disk magic */
370 		bp->b_blkno = DOSBBSECTOR;
371 		bp->b_bcount = lp->d_secsize;
372 		bp->b_oflags &= ~(BO_DONE);
373 		bp->b_flags |= B_READ;
374 		bp->b_cylinder = DOSBBSECTOR / lp->d_secpercyl;
375 		(*strat)(bp);
376 		if ((error = biowait(bp)) || memcmp(bp->b_data, "X68SCSI1", 8))
377 			printf("warning: disk not marked for x68k");
378 #endif
379 
380 		/* read the partition table */
381 		bp->b_blkno = DOSPARTOFF;
382 		labelsz = howmany(sizeof(struct cpu_disklabel),
383 				  lp->d_secsize) * lp->d_secsize;
384 		bp->b_bcount = labelsz;
385 		bp->b_oflags &= ~(BO_DONE);
386 		bp->b_flags |= B_READ;
387 		bp->b_cylinder = DOSPARTOFF / lp->d_secpercyl;
388 		(*strat)(bp);
389 
390 		if ((error = biowait(bp)) == 0) {
391 			/* XXX how do we check veracity/bounds of this? */
392 			dp = (struct dos_partition *)bp->b_data + 1;
393 			for (i = 0; i < NDOSPART; i++, dp++) {
394 				int part = i + (i < RAW_PART ? 0 : 1);
395 				int start, size;
396 
397 				start = lp->d_partitions[part].p_offset >> 1;
398 				size = lp->d_partitions[part].p_size >> 1;
399 
400 				switch (lp->d_partitions[part].p_fstype) {
401 				case FS_MSDOS:
402 					np = "Human68k";
403 					dp->dp_flag = 0; /* autoboot */
404 					break;
405 
406 				case FS_SWAP:
407 					np = "BSD swap";
408 					dp->dp_flag = 2; /* in use */
409 					break;
410 
411 				case FS_BSDFFS:
412 					np = "BSD ffs ";
413 					if (part == 0)
414 						dp->dp_flag = 0; /* autoboot */
415 					else
416 						dp->dp_flag = 2; /* in use */
417 					break;
418 
419 				case FS_BSDLFS:
420 					np = "BSD lfs ";
421 					if (part == 0)
422 						dp->dp_flag = 0; /* autoboot */
423 					else
424 						dp->dp_flag = 2; /* in use */
425 					break;
426 
427 				case FS_UNUSED:
428 					np = "\0\0\0\0\0\0\0\0";
429 					start = size = 0;
430 					if (part < lp->d_npartitions) {
431 						dp->dp_flag = 1;
432 					} else {
433 						dp->dp_flag = 0;
434 					}
435 					break;
436 
437 				default:
438 					/* XXX OS-9, MINIX etc. */
439 					continue;
440 				}
441 				memcpy(dp->dp_typname, np, 8);
442 				dp->dp_start = start;
443 				dp->dp_size = size;
444 			}
445 			bp->b_oflags &= ~(BO_DONE);
446 			bp->b_flags &= ~(B_READ);
447 			bp->b_flags |= B_WRITE;
448 			(*strat)(bp);
449 			error = biowait(bp);
450 		}
451 	}
452 
453 #ifdef maybe
454 	/* disklabel in appropriate location? */
455 	if (lp->d_partitions[0].p_offset != 0
456 		&& lp->d_partitions[0].p_offset != dospartoff) {
457 		error = EXDEV;
458 		goto done;
459 	}
460 #endif
461 
462 done:
463 	brelse(bp, 0);
464 	return (error);
465 }
466 
467 static void
468 parttbl_consistency_check(struct disklabel *lp, struct dos_partition *dp)
469 {
470 	int i, j;
471 	int f = (lp->d_secsize >= 1024) ? lp->d_secsize/1024 : 1;
472 	int g = (lp->d_secsize >= 1024) ? 1 : 1024/lp->d_secsize;
473 
474 	/* 1. overlapping check on partition table */
475 	for (i = 0; i < NDOSPART; i++) {
476 		if (dp[i].dp_size == 0)
477 			continue;
478 		for (j = i+1; j < NDOSPART; j++) {
479 			if (dp[j].dp_size == 0)
480 				continue;
481 			if (((dp[i].dp_start <= dp[j].dp_start) &&
482 			     (dp[i].dp_start + dp[i].dp_size > dp[j].dp_start))||
483 			    ((dp[j].dp_start <= dp[i].dp_start) &&
484 			     (dp[j].dp_start + dp[j].dp_size > dp[i].dp_start))) {
485 				printf("warning: Human68k partition %d and %d"
486 				       " are overlapping\n", i+1, j+1);
487 				return;
488 			}
489 		}
490 	}
491 
492 	/* 2. scan disklabel partitions */
493 #define bp	lp->d_partitions
494 	for (i = 0; i < lp->d_npartitions; i++) {
495 		int c = 0;
496 
497 		if (lp->d_partitions[i].p_fstype == FS_UNUSED ||
498 		    lp->d_partitions[i].p_size == 0)
499 			continue;
500 		for (j = 0; j < NDOSPART; j++) {
501 			if (dp[j].dp_size == 0)
502 				continue;
503 			if ((bp[i].p_offset * f < (dp[j].dp_start + dp[j].dp_size) * g) &&
504 			    ((bp[i].p_offset + bp[i].p_size) * f >= (dp[j].dp_start + dp[j].dp_size) * g))
505 				c++;
506 			if ((bp[i].p_offset * f > dp[j].dp_start * g) &&
507 			    ((bp[i].p_offset + bp[i].p_size) * f < (dp[j].dp_start + dp[j].dp_size) * g))
508 				c++;
509 			if ((bp[i].p_offset * f >= dp[j].dp_start * g) &&
510 			    ((bp[i].p_offset + bp[i].p_size) * f < dp[j].dp_start * g))
511 				c++;
512 		}
513 		if (c > 1)
514 			printf ("warning: partition %c spans for 2 or more"
515 				" partitions in Human68k partition table.\n",
516 				i+'a');
517 	}
518 #undef bp
519 
520 	/* more checks? */
521 }
522