xref: /netbsd-src/sys/arch/x68k/x68k/disksubr.c (revision 6b664a713479c31d4f17b38b42182a5d5fa21802)
1 /*	$NetBSD: disksubr.c,v 1.37 2024/01/07 07:58:35 isaki 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.37 2024/01/07 07:58:35 isaki 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 *
readdisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,struct cpu_disklabel * osdep)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, bsdlabelsz, humanlabelsz;
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 	lp->d_npartitions = RAW_PART + 1;
82 	for (i = 0; i < RAW_PART; i++) {
83 		lp->d_partitions[i].p_size = 0;
84 		lp->d_partitions[i].p_offset = 0;
85 	}
86 	if (lp->d_partitions[RAW_PART].p_size == 0)
87 		lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
88 	lp->d_partitions[RAW_PART].p_offset = 0;
89 
90 	lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size;
91 
92 	/* get a buffer and initialize it */
93 	bsdlabelsz =
94 	    howmany(LABELOFFSET + sizeof(struct disklabel), lp->d_secsize)
95 	    * lp->d_secsize;
96 	humanlabelsz =
97 	    howmany(sizeof(struct cpu_disklabel), lp->d_secsize)
98 	    * lp->d_secsize;
99 	bp = geteblk(MAX(bsdlabelsz, humanlabelsz));
100 	bp->b_dev = dev;
101 
102 	/* read BSD disklabel first */
103 	bp->b_blkno = LABELSECTOR;
104 	bp->b_cylinder = LABELSECTOR/lp->d_secpercyl;
105 	bp->b_bcount = bsdlabelsz;	/* to support < 512B/sector disks */
106 	bp->b_flags |= B_READ;
107 	(*strat)(bp);
108 
109 	/* if successful, locate disk label within block and validate */
110 	if (biowait(bp)) {
111 		msg = "disk label I/O error";
112 		goto dodospart;
113 	}
114 	for (dlp = (struct disklabel *)bp->b_data;
115 	     dlp <= (struct disklabel *)
116 		((char *)bp->b_data + bsdlabelsz - sizeof(*dlp));
117 	     dlp = (struct disklabel *)((uint8_t *)dlp + sizeof(long))) {
118 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
119 			if (msg == NULL)
120 				msg = "no disk label";
121 		} else if (dlp->d_npartitions > MAXPARTITIONS ||
122 			   dkcksum(dlp) != 0)
123 			msg = "disk label corrupted";
124 		else {
125 			*lp = *dlp;
126 			msg = NULL;
127 			break;
128 		}
129 	}
130 
131 dodospart:
132 	/* next do the Human68k-style partition table */
133 	/* Human68k does not support > 2048B/sector devices (?) */
134 	if (lp->d_secsize >= 2048) {
135 		if (msg)
136 			goto done;
137 		goto dobadsect;
138 	}
139 	bp->b_blkno = DOSPARTOFF * DEF_BSIZE / lp->d_secsize;
140 				/* DOSPARTOFF in DEV_BSIZE unit */
141 	bp->b_cylinder = DOSBBSECTOR / lp->d_secpercyl;
142 	bp->b_bcount = humanlabelsz;	/* to support < 512B/sector disks */
143 	bp->b_oflags &= ~(BO_DONE);
144 	(*strat)(bp);
145 
146 	/* if successful, wander through Human68k partition table */
147 	if (biowait(bp))
148 		goto done;
149 	if (strncmp(bp->b_data, "X68K", 4) != 0) {
150 		/* Human68k-style partition table does not exist */
151 		if (msg)
152 			goto done;
153 		goto dobadsect;
154 	}
155 
156 	/* XXX how do we check veracity/bounds of this? */
157 	if (dp)
158 		memcpy(dp, (char *)bp->b_data + sizeof(*dp) /*DOSPARTOFF*/,
159 		    NDOSPART * sizeof(*dp));
160 	else
161 		dp = (void *)((char *)bp->b_data + sizeof(*dp) /*DOSPARTOFF*/);
162 
163 	/* if BSD disklabel does not exist, fall back to Human68k partition */
164 	if (msg != NULL) {
165 		msg = NULL;
166 		lp->d_bbsize = 8192;
167 		lp->d_sbsize = 2048;
168 		for (i = 0; i < NDOSPART; i++, dp++)
169 			/* is this ours? */
170 			if (dp->dp_size) {
171 				u_char fstype;
172 				int part = i + (i < RAW_PART ? 0 : 1);
173 				int start = dp->dp_start * 2;
174 				int size = dp->dp_size * 2;
175 
176 				/* update disklabel with details */
177 				lp->d_partitions[part].p_size = size;
178 				lp->d_partitions[part].p_offset =  start;
179 				/* get partition type */
180 #ifndef COMPAT_10
181 				if (dp->dp_flag == 1)
182 					fstype = FS_UNUSED;
183 				else
184 #endif
185 				if (!memcmp(dp->dp_typname, "Human68k", 8))
186 					fstype = FS_MSDOS;
187 				else if (!memcmp(dp->dp_typname,
188 					     "BSD ffs ", 8))
189 					fstype = FS_BSDFFS;
190 				else if (!memcmp(dp->dp_typname,
191 					     "BSD lfs ", 8))
192 					fstype = FS_BSDLFS;
193 				else if (!memcmp(dp->dp_typname,
194 					     "BSD swap", 8))
195 					fstype = FS_SWAP;
196 #ifndef COMPAT_14
197 				else if (part == 1)
198 					fstype = FS_SWAP;
199 #endif
200 				else
201 					fstype = FS_BSDFFS; /* XXX */
202 				lp->d_partitions[part].p_fstype = fstype; /* XXX */
203 				if (lp->d_npartitions <= part)
204 					lp->d_npartitions = part + 1;
205 			}
206 	} else {
207 		parttbl_consistency_check(lp, dp);
208 	}
209 
210 dobadsect:
211 	/* obtain bad sector table if requested and present */
212 	if (bdp && (lp->d_flags & D_BADSECT)) {
213 		struct dkbad *db;
214 
215 		i = 0;
216 		do {
217 			/* read a bad sector table */
218 			bp->b_oflags &= ~(BO_DONE);
219 			bp->b_flags |= B_READ;
220 			bp->b_blkno = lp->d_secperunit - lp->d_nsectors + i;
221 			if (lp->d_secsize > DEF_BSIZE)
222 				bp->b_blkno *= lp->d_secsize / DEF_BSIZE;
223 			else
224 				bp->b_blkno /= DEF_BSIZE / lp->d_secsize;
225 			bp->b_bcount = lp->d_secsize;
226 			bp->b_cylinder = lp->d_ncylinders - 1;
227 			(*strat)(bp);
228 
229 			/* if successful, validate, otherwise try another */
230 			if (biowait(bp)) {
231 				msg = "bad sector table I/O error";
232 			} else {
233 				db = (struct dkbad *)(bp->b_data);
234 #define DKBAD_MAGIC 0x4321
235 				if (db->bt_mbz == 0
236 					&& db->bt_flag == DKBAD_MAGIC) {
237 					msg = NULL;
238 					*bdp = *db;
239 					break;
240 				} else
241 					msg = "bad sector table corrupted";
242 			}
243 		} while (bp->b_error != 0 && (i += 2) < 10 &&
244 			i < lp->d_nsectors);
245 	}
246 
247 done:
248 	brelse(bp, 0);
249 	return (msg);
250 }
251 
252 /*
253  * Write disk label back to device after modification.
254  */
255 int
writedisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,struct cpu_disklabel * osdep)256 writedisklabel(dev_t dev, void (*strat)(struct buf *),
257     struct disklabel *lp, struct cpu_disklabel *osdep)
258 {
259 	struct dos_partition *dp = 0;
260 	struct buf *bp;
261 	struct disklabel *dlp;
262 	int error, bsdlabelsz, humanlabelsz, i;
263 	const char *np;
264 
265 	if (osdep)
266 		dp = osdep->dosparts;
267 	/* sanity clause */
268 	if (lp->d_secpercyl == 0 || lp->d_secsize == 0
269 		/*|| (lp->d_secsize % DEF_BSIZE) != 0*/)
270 			return(EINVAL);
271 	if (dp)
272 		parttbl_consistency_check(lp, dp);
273 
274 	/* get a buffer and initialize it */
275 	bsdlabelsz =
276 	    howmany(LABELOFFSET + sizeof(struct disklabel), lp->d_secsize)
277 	    * lp->d_secsize;
278 	humanlabelsz =
279 	    howmany(sizeof(struct cpu_disklabel), lp->d_secsize)
280 	    * lp->d_secsize;
281 	bp = geteblk(MAX(bsdlabelsz, humanlabelsz));
282 	bp->b_dev = dev;
283 
284 	/* attempt to write BSD disklabel first */
285 	bp->b_blkno = LABELSECTOR;
286 	bp->b_cylinder = LABELSECTOR / lp->d_secpercyl;
287 	bp->b_bcount = bsdlabelsz;	/* to support < 512B/sector disks */
288 	bp->b_flags |= B_READ;
289 	(*strat)(bp);
290 
291 	/* if successful, locate disk label within block and validate */
292 	if (biowait(bp))
293 		goto dodospart;
294 	error = ESRCH;
295 	for (dlp = (struct disklabel *)bp->b_data;
296 	     dlp <= (struct disklabel *)
297 		((char *)bp->b_data + bsdlabelsz - sizeof(*dlp));
298 	     dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
299 		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
300 		    dkcksum(dlp) == 0) {
301 			*dlp = *lp;
302 			bp->b_oflags &= ~(BO_DONE);
303 			bp->b_flags &= ~(B_READ);
304 			bp->b_flags |= B_WRITE;
305 			(*strat)(bp);
306 			error = biowait(bp);
307 			break;
308 		}
309 	}
310 
311 	/* do dos partitions in the process of getting disklabel? */
312 	if (error) {
313 dodospart:
314 		if (lp->d_secsize >= 2048) {
315 			error = ESRCH;
316 			goto done;
317 		}
318 #if 0				/* there is no mark on floppies */
319 		/* read the x68k disk magic */
320 		bp->b_blkno = DOSBBSECTOR;
321 		bp->b_bcount = lp->d_secsize;
322 		bp->b_oflags &= ~(BO_DONE);
323 		bp->b_flags &= ~(B_WRITE);
324 		bp->b_flags |= B_READ;
325 		bp->b_cylinder = DOSBBSECTOR / lp->d_secpercyl;
326 		(*strat)(bp);
327 		if ((error = biowait(bp)) || memcmp(bp->b_data, "X68SCSI1", 8))
328 			printf("warning: disk not marked for x68k");
329 #endif
330 
331 		/* read the partition table */
332 		bp->b_blkno = DOSPARTOFF;
333 		bp->b_bcount = humanlabelsz;
334 		bp->b_oflags &= ~(BO_DONE);
335 		bp->b_flags &= ~(B_WRITE);
336 		bp->b_flags |= B_READ;
337 		bp->b_cylinder = DOSPARTOFF / lp->d_secpercyl;
338 		(*strat)(bp);
339 
340 		if ((error = biowait(bp)) == 0) {
341 			/* XXX how do we check veracity/bounds of this? */
342 			dp = (struct dos_partition *)bp->b_data + 1;
343 			for (i = 0; i < NDOSPART; i++, dp++) {
344 				int part = i + (i < RAW_PART ? 0 : 1);
345 				int start, size;
346 
347 				start = lp->d_partitions[part].p_offset >> 1;
348 				size = lp->d_partitions[part].p_size >> 1;
349 
350 				switch (lp->d_partitions[part].p_fstype) {
351 				case FS_MSDOS:
352 					np = "Human68k";
353 					dp->dp_flag = 0; /* autoboot */
354 					break;
355 
356 				case FS_SWAP:
357 					np = "BSD swap";
358 					dp->dp_flag = 2; /* in use */
359 					break;
360 
361 				case FS_BSDFFS:
362 					np = "BSD ffs ";
363 					if (part == 0)
364 						dp->dp_flag = 0; /* autoboot */
365 					else
366 						dp->dp_flag = 2; /* in use */
367 					break;
368 
369 				case FS_BSDLFS:
370 					np = "BSD lfs ";
371 					if (part == 0)
372 						dp->dp_flag = 0; /* autoboot */
373 					else
374 						dp->dp_flag = 2; /* in use */
375 					break;
376 
377 				case FS_UNUSED:
378 					np = "\0\0\0\0\0\0\0\0";
379 					start = size = 0;
380 					if (part < lp->d_npartitions) {
381 						dp->dp_flag = 1;
382 					} else {
383 						dp->dp_flag = 0;
384 					}
385 					break;
386 
387 				default:
388 					/* XXX OS-9, MINIX etc. */
389 					continue;
390 				}
391 				memcpy(dp->dp_typname, np, 8);
392 				dp->dp_start = start;
393 				dp->dp_size = size;
394 			}
395 			bp->b_oflags &= ~(BO_DONE);
396 			bp->b_flags &= ~(B_READ);
397 			bp->b_flags |= B_WRITE;
398 			(*strat)(bp);
399 			error = biowait(bp);
400 		}
401 	}
402 
403 #ifdef maybe
404 	/* disklabel in appropriate location? */
405 	if (lp->d_partitions[0].p_offset != 0
406 		&& lp->d_partitions[0].p_offset != dospartoff) {
407 		error = EXDEV;
408 		goto done;
409 	}
410 #endif
411 
412 done:
413 	brelse(bp, 0);
414 	return (error);
415 }
416 
417 static void
parttbl_consistency_check(struct disklabel * lp,struct dos_partition * dp)418 parttbl_consistency_check(struct disklabel *lp, struct dos_partition *dp)
419 {
420 	int i, j;
421 	int f = (lp->d_secsize >= 1024) ? lp->d_secsize/1024 : 1;
422 	int g = (lp->d_secsize >= 1024) ? 1 : 1024/lp->d_secsize;
423 
424 	/* 1. overlapping check on partition table */
425 	for (i = 0; i < NDOSPART; i++) {
426 		if (dp[i].dp_size == 0)
427 			continue;
428 		for (j = i+1; j < NDOSPART; j++) {
429 			if (dp[j].dp_size == 0)
430 				continue;
431 			if (((dp[i].dp_start <= dp[j].dp_start) &&
432 			     (dp[i].dp_start + dp[i].dp_size > dp[j].dp_start))||
433 			    ((dp[j].dp_start <= dp[i].dp_start) &&
434 			     (dp[j].dp_start + dp[j].dp_size > dp[i].dp_start))) {
435 				printf("warning: Human68k partition %d and %d"
436 				       " are overlapping\n", i+1, j+1);
437 				return;
438 			}
439 		}
440 	}
441 
442 	/* 2. scan disklabel partitions */
443 #define bp	lp->d_partitions
444 	for (i = 0; i < lp->d_npartitions; i++) {
445 		int c = 0;
446 
447 		if (lp->d_partitions[i].p_fstype == FS_UNUSED ||
448 		    lp->d_partitions[i].p_size == 0)
449 			continue;
450 		for (j = 0; j < NDOSPART; j++) {
451 			if (dp[j].dp_size == 0)
452 				continue;
453 			if ((bp[i].p_offset * f < (dp[j].dp_start + dp[j].dp_size) * g) &&
454 			    ((bp[i].p_offset + bp[i].p_size) * f >= (dp[j].dp_start + dp[j].dp_size) * g))
455 				c++;
456 			if ((bp[i].p_offset * f > dp[j].dp_start * g) &&
457 			    ((bp[i].p_offset + bp[i].p_size) * f < (dp[j].dp_start + dp[j].dp_size) * g))
458 				c++;
459 			if ((bp[i].p_offset * f >= dp[j].dp_start * g) &&
460 			    ((bp[i].p_offset + bp[i].p_size) * f < dp[j].dp_start * g))
461 				c++;
462 		}
463 		if (c > 1)
464 			printf ("warning: partition %c spans for 2 or more"
465 				" partitions in Human68k partition table.\n",
466 				i+'a');
467 	}
468 #undef bp
469 
470 	/* more checks? */
471 }
472