xref: /netbsd-src/sys/arch/evbppc/evbppc/disksubr.c (revision 021d832a1333a7c481b64a5d4c4c4d092bd0bc20)
1 /*	$NetBSD: disksubr.c,v 1.18 2019/04/03 22:10:50 christos 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.18 2019/04/03 22:10:50 christos Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/buf.h>
40 #include <sys/disklabel.h>
41 #include <sys/disk.h>
42 #include <sys/syslog.h>
43 
44 /*
45  * Attempt to read a disk label from a device
46  * using the indicated strategy routine.
47  * The label must be partly set up before this:
48  * secpercyl, secsize and anything required for a block i/o read
49  * operation in the driver's strategy/start routines
50  * must be filled in before calling us.
51  *
52  * If dos partition table requested, attempt to load it and
53  * find disklabel inside a DOS partition. Also, if bad block
54  * table needed, attempt to extract it as well. Return buffer
55  * for use in signalling errors if requested.
56  *
57  * Returns null on success and an error string on failure.
58  */
59 const char *
readdisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,struct cpu_disklabel * osdep)60 readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
61 	      struct cpu_disklabel *osdep)
62 {
63 	struct buf *bp;
64 	struct disklabel *dlp;
65 	struct dkbad *bdp;
66 	const char *msg = NULL;
67 	int i;
68 
69 	/* minimal requirements for archtypal disk label */
70 	if (lp->d_secsize == 0)
71 		lp->d_secsize = DEV_BSIZE;
72 	if (lp->d_secperunit == 0)
73 		lp->d_secperunit = 0x1fffffff;
74 	lp->d_npartitions = RAW_PART + 1;
75 	if (lp->d_partitions[RAW_PART].p_size == 0)
76 		lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
77 	lp->d_partitions[RAW_PART].p_offset = 0;
78 
79 	/* get a buffer and initialize it */
80 	bp = geteblk((int)lp->d_secsize);
81 
82 	/* next, dig out disk label */
83 	bp->b_dev = dev;
84 	bp->b_blkno = LABELSECTOR;
85 	bp->b_cylinder = 0;
86 	bp->b_bcount = lp->d_secsize;
87 	bp->b_flags |= B_READ;
88 	(*strat)(bp);
89 
90 	/* if successful, locate disk label within block and validate */
91 	if (biowait(bp)) {
92 		msg = "disk label I/O error";
93 		goto done;
94 	}
95 
96 	for (dlp = (struct disklabel *)bp->b_data;
97 	    dlp <= (struct disklabel *)((char *)bp->b_data + lp->d_secsize -
98 		sizeof(*dlp));
99 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
100 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
101 			if (msg == NULL)
102 				msg = "no disk label";
103 		} else if (dlp->d_npartitions > MAXPARTITIONS ||
104 			   dkcksum(dlp) != 0)
105 			msg = "disk label corrupted";
106 		else {
107 			*lp = *dlp;
108 			msg = NULL;
109 			break;
110 		}
111 	}
112 
113 	if (msg)
114 		goto done;
115 
116 	/* obtain bad sector table if requested and present */
117 	if (osdep && (lp->d_flags & D_BADSECT)) {
118 		struct dkbad *db;
119 
120 		bdp = &osdep->bad;
121 		i = 0;
122 		do {
123 			/* read a bad sector table */
124 			bp->b_oflags &= ~BO_DONE;
125 			bp->b_flags |= B_READ;
126 			bp->b_blkno = lp->d_secperunit - lp->d_nsectors + i;
127 			if (lp->d_secsize > DEV_BSIZE)
128 				bp->b_blkno *= lp->d_secsize / DEV_BSIZE;
129 			else
130 				bp->b_blkno /= DEV_BSIZE / lp->d_secsize;
131 			bp->b_bcount = lp->d_secsize;
132 			bp->b_cylinder = lp->d_ncylinders - 1;
133 			(*strat)(bp);
134 
135 			/* if successful, validate, otherwise try another */
136 			if (biowait(bp)) {
137 				msg = "bad sector table I/O error";
138 			} else {
139 				db = (struct dkbad *)(bp->b_data);
140 #define DKBAD_MAGIC 0x4321
141 				if (db->bt_mbz == 0
142 					&& db->bt_flag == DKBAD_MAGIC) {
143 					msg = NULL;
144 					*bdp = *db;
145 					break;
146 				} else
147 					msg = "bad sector table corrupted";
148 			}
149 		} while (bp->b_error != 0 && (i += 2) < 10 &&
150 			i < lp->d_nsectors);
151 	}
152 
153 done:
154 	brelse(bp, BC_INVAL);
155 	return (msg);
156 }
157 
158 
159 /*
160  * Write disk label back to device after modification.
161  */
162 int
writedisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,struct cpu_disklabel * osdep)163 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
164 	       struct cpu_disklabel *osdep)
165 {
166 	struct buf *bp;
167 	struct disklabel *dlp;
168 	int error;
169 
170 #ifdef maybe
171 	/* disklabel in appropriate location? */
172 	if (lp->d_partitions[2].p_offset != 0
173 		&& lp->d_partitions[2].p_offset != dospartoff) {
174 		error = EXDEV;
175 		goto done;
176 	}
177 #endif
178 
179 	/* get a buffer and initialize it */
180 	bp = geteblk((int)lp->d_secsize);
181 	bp->b_dev = dev;
182 	bp->b_blkno = LABELSECTOR;
183 	bp->b_cylinder = 0;
184 	bp->b_bcount = lp->d_secsize;
185 	bp->b_flags |= B_READ;
186 	(*strat)(bp);
187 
188 	/* if successful, locate disk label within block and validate */
189 	if ((error = biowait(bp)) != 0)
190 		goto done;
191 	for (dlp = (struct disklabel *)bp->b_data;
192 	    dlp <= (struct disklabel *)((char *)bp->b_data + lp->d_secsize -
193 		sizeof(*dlp));
194 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
195 		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
196 		    dkcksum(dlp) == 0) {
197 			*dlp = *lp;
198 			bp->b_cflags = BC_BUSY;
199 			bp->b_flags = B_WRITE;
200 			CLR(bp->b_oflags, BO_DONE);
201 			(*strat)(bp);
202 			error = biowait(bp);
203 			goto done;
204 		}
205 	}
206 	error = ESRCH;
207 
208 done:
209 	brelse(bp, 0);
210 	return (error);
211 }
212