xref: /netbsd/sys/arch/arm/arm/disksubr.c (revision c4a72b64)
1 /*	$NetBSD: disksubr.c,v 1.3 2002/08/27 17:30:02 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 Christopher G. Demetriou.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Christopher G. Demetriou
17  *	for the NetBSD Project.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
35  * Copyright (c) 1995 Mark Brinicombe
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. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	from: @(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
67  */
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/buf.h>
72 #include <sys/dkbad.h>
73 #include <sys/disklabel.h>
74 #include <sys/syslog.h>
75 #include <sys/device.h>
76 #include <sys/disk.h>
77 
78 /*
79  * Attempt to read a disk label from a device
80  * using the indicated strategy routine.
81  * The label must be partly set up before this:
82  * secpercyl, secsize and anything required for a block i/o read
83  * operation in the driver's strategy/start routines
84  * must be filled in before calling us.
85  *
86  * If dos partition table requested, attempt to load it and
87  * find disklabel inside a DOS partition. Also, if bad block
88  * table needed, attempt to extract it as well. Return buffer
89  * for use in signalling errors if requested.
90  *
91  * Returns null on success and an error string on failure.
92  */
93 
94 char *
95 readdisklabel(dev, strat, lp, osdep)
96 	dev_t dev;
97 	void (*strat) __P((struct buf *));
98 	struct disklabel *lp;
99 	struct cpu_disklabel *osdep;
100 {
101 	struct buf *bp;
102 	struct disklabel *dlp;
103 	char *msg = NULL;
104 	int cyl, netbsdpartoff, i;
105 
106 /*	printf("Reading disclabel for %04x\n", dev);*/
107 
108 	/* minimal requirements for archtypal disk label */
109 
110 	if (lp->d_secsize == 0)
111 		lp->d_secsize = DEV_BSIZE;
112 
113 	if (lp->d_secperunit == 0)
114 		lp->d_secperunit = 0x1fffffff;
115 
116 	lp->d_npartitions = MAXPARTITIONS;
117 	for (i = 0; i < MAXPARTITIONS; i++) {
118 		if (i == RAW_PART) continue;
119 		lp->d_partitions[i].p_offset = 0;
120 		lp->d_partitions[i].p_fstype = FS_UNUSED;
121 		lp->d_partitions[i].p_size = 0;
122 	}
123 
124 	if (lp->d_partitions[RAW_PART].p_size == 0) {
125 		lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
126 		lp->d_partitions[RAW_PART].p_offset = 0;
127 		lp->d_partitions[RAW_PART].p_size = 0x1fffffff;
128 	}
129 
130 	/* obtain buffer to probe drive with */
131 
132 	bp = geteblk((int)lp->d_secsize);
133 
134 	/* request no partition relocation by driver on I/O operations */
135 
136 	bp->b_dev = dev;
137 
138 	/* do netbsd partitions in the process of getting disklabel? */
139 
140 	netbsdpartoff = 0;
141 	cyl = LABELSECTOR / lp->d_secpercyl;
142 
143 	if (osdep) {
144 		if (filecore_label_read(dev, strat,lp, osdep, &msg, &cyl,
145 		      &netbsdpartoff) ||
146 		    mbr_label_read(dev, strat, lp, osdep, &msg, &cyl,
147 		      &netbsdpartoff)) {
148 			if (msg != NULL)
149 				goto done;
150 		} else {
151 			/*
152 			 * We didn't find anything we like; NetBSD native.
153 			 * netbsdpartoff and cyl should be unchanged.
154 			 */
155 			KASSERT(netbsdpartoff == 0);
156 			KASSERT(cyl == (LABELSECTOR / lp->d_secpercyl));
157 		}
158 	}
159 
160 	/* next, dig out disk label */
161 
162 /*	printf("Reading disklabel addr=%08x\n", netbsdpartoff * DEV_BSIZE);*/
163 
164 	bp->b_blkno = netbsdpartoff + LABELSECTOR;
165 	bp->b_cylinder = bp->b_blkno / lp->d_secpercyl;
166 	bp->b_bcount = lp->d_secsize;
167 	bp->b_flags |= B_READ;
168 	(*strat)(bp);
169 
170 	/* if successful, locate disk label within block and validate */
171 
172 	if (biowait(bp)) {
173 		msg = "disk label I/O error";
174 		goto done;
175 	}
176 	for (dlp = (struct disklabel *)bp->b_data;
177 	    dlp <= (struct disklabel *)(bp->b_data + lp->d_secsize - sizeof(*dlp));
178 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
179 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
180 			if (msg == NULL)
181 				msg = "no disk label";
182 		} else if (dlp->d_npartitions > MAXPARTITIONS ||
183 			   dkcksum(dlp) != 0)
184 			msg = "disk label corrupted";
185 		else {
186 			*lp = *dlp;
187 			msg = NULL;
188 			break;
189 		}
190 	}
191 
192 	if (msg)
193 		goto done;
194 
195 	/* obtain bad sector table if requested and present */
196 	if (osdep && (lp->d_flags & D_BADSECT)) {
197 		struct dkbad *bdp = &osdep->bad;
198 		struct dkbad *db;
199 
200 		i = 0;
201 		do {
202 			/* read a bad sector table */
203 			bp->b_flags &= ~(B_DONE);
204 			bp->b_flags |= B_READ;
205 			bp->b_blkno = lp->d_secperunit - lp->d_nsectors + i;
206 			if (lp->d_secsize > DEV_BSIZE)
207 				bp->b_blkno *= lp->d_secsize / DEV_BSIZE;
208 			else
209 				bp->b_blkno /= DEV_BSIZE / lp->d_secsize;
210 			bp->b_bcount = lp->d_secsize;
211 			bp->b_cylinder = lp->d_ncylinders - 1;
212 			(*strat)(bp);
213 
214 			/* if successful, validate, otherwise try another */
215 			if (biowait(bp)) {
216 				msg = "bad sector table I/O error";
217 			} else {
218 				db = (struct dkbad *)(bp->b_data);
219 #define DKBAD_MAGIC 0x4321
220 				if (db->bt_mbz == 0
221 					&& db->bt_flag == DKBAD_MAGIC) {
222 					msg = NULL;
223 					*bdp = *db;
224 					break;
225 				} else
226 					msg = "bad sector table corrupted";
227 			}
228 		} while ((bp->b_flags & B_ERROR) && (i += 2) < 10 &&
229 			i < lp->d_nsectors);
230 	}
231 
232 done:
233 	brelse(bp);
234 	return (msg);
235 }
236 
237 
238 /*
239  * Check new disk label for sensibility
240  * before setting it.
241  */
242 
243 int
244 setdisklabel(olp, nlp, openmask, osdep)
245 	struct disklabel *olp;
246 	struct disklabel *nlp;
247 	u_long openmask;
248 	struct cpu_disklabel *osdep;
249 {
250 	int i;
251 	struct partition *opp, *npp;
252 
253 	/* sanity clause */
254 
255 	if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0
256 	    || (nlp->d_secsize % DEV_BSIZE) != 0)
257 		return(EINVAL);
258 
259 	/* special case to allow disklabel to be invalidated */
260 
261 	if (nlp->d_magic == 0xffffffff) {
262 		*olp = *nlp;
263 		return (0);
264 	}
265 
266 	if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC
267 	    || dkcksum(nlp) != 0)
268 		return (EINVAL);
269 
270 	/* XXX missing check if other acorn/dos partitions will be overwritten */
271 
272 	while (openmask != 0) {
273 		i = ffs(openmask) - 1;
274 		openmask &= ~(1 << i);
275 		if (nlp->d_npartitions <= i)
276 			return (EBUSY);
277 		opp = &olp->d_partitions[i];
278 		npp = &nlp->d_partitions[i];
279 		if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
280 			return (EBUSY);
281 		/*
282 		 * Copy internally-set partition information
283 		 * if new label doesn't include it.		XXX
284 		 */
285 		if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
286 			npp->p_fstype = opp->p_fstype;
287 			npp->p_fsize = opp->p_fsize;
288 			npp->p_frag = opp->p_frag;
289 			npp->p_cpg = opp->p_cpg;
290 		}
291 	}
292 
293 	nlp->d_checksum = 0;
294 	nlp->d_checksum = dkcksum(nlp);
295 	*olp = *nlp;
296 	return (0);
297 }
298 
299 
300 /*
301  * Write disk label back to device after modification.
302  */
303 
304 int
305 writedisklabel(dev, strat, lp, osdep)
306 	dev_t dev;
307 	void (*strat) __P((struct buf *));
308 	struct disklabel *lp;
309 	struct cpu_disklabel *osdep;
310 {
311 	struct buf *bp;
312 	struct disklabel *dlp;
313 	int cyl, netbsdpartoff;
314 	int error = 0, rv;
315 
316 	/* get a buffer and initialize it */
317 
318 	bp = geteblk((int)lp->d_secsize);
319 	bp->b_dev = dev;
320 
321 	/* do netbsd partitions in the process of getting disklabel? */
322 
323 	netbsdpartoff = 0;
324 	cyl = LABELSECTOR / lp->d_secpercyl;
325 
326 	if (osdep) {
327 		if ((rv = filecore_label_locate(dev, strat,lp, osdep, &cyl,
328 		      &netbsdpartoff)) != 0||
329 		    (rv = mbr_label_locate(dev, strat, lp, osdep, &cyl,
330 		      &netbsdpartoff)) != 0) {
331 			if (rv < 0) {
332 			    error = -rv;
333 			    goto done;
334 			}
335 		} else {
336 			/*
337 			 * We didn't find anything we like; NetBSD native.
338 			 * netbsdpartoff and cyl should be unchanged.
339 			 */
340 			KASSERT(netbsdpartoff == 0);
341 			KASSERT(cyl == (LABELSECTOR / lp->d_secpercyl));
342 		}
343 	}
344 
345 /* writelabel: */
346 
347 /*	printf("writedisklabel: Reading disklabel addr=%08x\n",
348 	     netbsdpartoff * DEV_BSIZE);*/
349 
350 	/* next, dig out disk label */
351 
352 	bp->b_blkno = netbsdpartoff + LABELSECTOR;
353 	bp->b_cylinder = cyl;
354 	bp->b_bcount = lp->d_secsize;
355 	bp->b_flags &= ~(B_DONE);
356 	bp->b_flags |= B_READ;
357 	(*strat)(bp);
358 
359 	/* if successful, locate disk label within block and validate */
360 
361 	if ((error = biowait(bp)))
362 		goto done;
363 	for (dlp = (struct disklabel *)bp->b_data;
364 	    dlp <= (struct disklabel *)(bp->b_data + lp->d_secsize - sizeof(*dlp));
365 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
366 		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
367 		    dkcksum(dlp) == 0) {
368 			*dlp = *lp;
369 			bp->b_flags &= ~(B_READ|B_DONE);
370 			bp->b_flags |= B_WRITE;
371 			(*strat)(bp);
372 			error = biowait(bp);
373 			goto done;
374 		}
375 	}
376 
377 	error = ESRCH;
378 
379 done:
380 	brelse(bp);
381 	return (error);
382 }
383 
384 
385 /*
386  * Determine the size of the transfer, and make sure it is
387  * within the boundaries of the partition. Adjust transfer
388  * if needed, and signal errors or early completion.
389  */
390 int
391 bounds_check_with_label(bp, lp, wlabel)
392 	struct buf *bp;
393 	struct disklabel *lp;
394 	int wlabel;
395 {
396 	struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
397 	int labelsector = lp->d_partitions[RAW_PART].p_offset + LABELSECTOR;
398 	int sz;
399 
400 	sz = howmany(bp->b_bcount, lp->d_secsize);
401 
402 	if (bp->b_blkno + sz > p->p_size) {
403 		sz = p->p_size - bp->b_blkno;
404 		if (sz == 0) {
405 			/* If exactly at end of disk, return EOF. */
406 			bp->b_resid = bp->b_bcount;
407 			goto done;
408 		}
409 		if (sz < 0) {
410 			/* If past end of disk, return EINVAL. */
411 			bp->b_error = EINVAL;
412 			goto bad;
413 		}
414 		/* Otherwise, truncate request. */
415 		bp->b_bcount = sz << DEV_BSHIFT;
416 	}
417 
418 	/* Overwriting disk label? */
419 	if (bp->b_blkno + p->p_offset <= labelsector &&
420 #if LABELSECTOR != 0
421 	    bp->b_blkno + p->p_offset + sz > labelsector &&
422 #endif
423 	    (bp->b_flags & B_READ) == 0 && !wlabel) {
424 		bp->b_error = EROFS;
425 		goto bad;
426 	}
427 
428 	/* calculate cylinder for disksort to order transfers with */
429 	bp->b_cylinder = (bp->b_blkno + p->p_offset) /
430 	    (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
431 	return (1);
432 
433 bad:
434 	bp->b_flags |= B_ERROR;
435 done:
436 	return (0);
437 }
438 
439 /* End of disksubr.c */
440