xref: /netbsd-src/sys/arch/playstation2/playstation2/disksubr.c (revision 1cd43426d582b6650b153797f2db305dcd93c554)
1 /*	$NetBSD: disksubr.c,v 1.21 2024/02/10 18:43:52 andvar 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 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.21 2024/02/10 18:43:52 andvar Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/buf.h>
38 #include <sys/disklabel.h>
39 #include <sys/disk.h>
40 
41 #define NO_MBR_SIGNATURE ((struct mbr_partition *) -1)
42 
43 static struct mbr_partition *
44 mbr_findslice(struct mbr_partition* dp, struct buf *bp);
45 
46 /*
47  * Scan MBR for NetBSD partition.  Return NO_MBR_SIGNATURE if no MBR found
48  * Otherwise, copy valid MBR partition-table into dp, and if a NetBSD
49  * partition is found, return a pointer to it; else return  NULL.
50  */
51 static
52 struct mbr_partition *
mbr_findslice(struct mbr_partition * dp,struct buf * bp)53 mbr_findslice(struct mbr_partition *dp, struct buf *bp)
54 {
55 	struct mbr_partition *ourdp = NULL;
56 	u_int16_t *mbrmagicp;
57 	int i;
58 
59 	/* Note: Magic number is little-endian. */
60 	mbrmagicp = (u_int16_t *)((char*)bp->b_data + MBR_MAGIC_OFFSET);
61 	if (*mbrmagicp != MBR_MAGIC)
62 		return (NO_MBR_SIGNATURE);
63 
64 	/* XXX how do we check veracity/bounds of this? */
65 	memcpy(dp, (char*)bp->b_data + MBR_PART_OFFSET, MBR_PART_COUNT * sizeof(*dp));
66 
67 	/* look for NetBSD partition */
68 	for (i = 0; i < MBR_PART_COUNT; i++) {
69 		if (dp[i].mbrp_type == MBR_PTYPE_NETBSD) {
70 			ourdp = &dp[i];
71 			break;
72 		}
73 	}
74 
75 	return (ourdp);
76 }
77 
78 
79 /*
80  * Attempt to read a disk label from a device
81  * using the indicated strategy routine.
82  * The label must be partly set up before this:
83  * secpercyl, secsize and anything required for a block i/o read
84  * operation in the driver's strategy/start routines
85  * must be filled in before calling us.
86  *
87  * If dos partition table requested, attempt to load it and
88  * find disklabel inside a DOS partition. Also, if bad block
89  * table needed, attempt to extract it as well. Return buffer
90  * for use in signalling errors if requested.
91  *
92  * Returns null on success and an error string on failure.
93  */
94 const char *
readdisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,struct cpu_disklabel * osdep)95 readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
96     struct cpu_disklabel *osdep)
97 {
98 	struct mbr_partition *dp;
99 	struct partition *pp;
100 	struct dkbad *bdp;
101 	struct buf *bp;
102 	struct disklabel *dlp;
103 	const char *msg = NULL;
104 	int dospartoff, cyl, i;
105 
106 	/* minimal requirements for archtypal disk label */
107 	if (lp->d_secsize == 0)
108 		lp->d_secsize = DEV_BSIZE;
109 	if (lp->d_secperunit == 0)
110 		lp->d_secperunit = 0x1fffffff;
111 #if 0
112 	if (lp->d_ncylinders == 16383) {
113 		printf("disklabel: Disk > 8G ... readjusting chs %d/%d/%d to ",
114 			lp->d_ncylinders, lp->d_ntracks, lp->d_nsectors);
115 		lp->d_ncylinders = lp->d_secperunit /  lp->d_ntracks / lp->d_nsectors;
116 		printf("%d/%d/%d\n",
117 			lp->d_ncylinders, lp->d_ntracks, lp->d_nsectors);
118 	}
119 #endif
120 	lp->d_npartitions = RAW_PART + 1;
121 	for (i = 0; i < RAW_PART; i++) {
122 		lp->d_partitions[i].p_size = 0;
123 		lp->d_partitions[i].p_offset = 0;
124 	}
125 	if (lp->d_partitions[i].p_size == 0)
126 		lp->d_partitions[i].p_size = 0x1fffffff;
127 	lp->d_partitions[i].p_offset = 0;
128 
129 	/* get a buffer and initialize it */
130 	bp = geteblk((int)lp->d_secsize);
131 	bp->b_dev = dev;
132 
133 	/* do dos partitions in the process of getting disklabel? */
134 	dospartoff = 0;
135 	cyl = LABELSECTOR / lp->d_secpercyl;
136 	if (!osdep)
137 		goto nombrpart;
138 	dp = osdep->dosparts;
139 
140 	/* read master boot record */
141 	bp->b_blkno = MBR_BBSECTOR;
142 	bp->b_bcount = lp->d_secsize;
143 	bp->b_flags |= B_READ;
144 	bp->b_cylinder = MBR_BBSECTOR / lp->d_secpercyl;
145 	(*strat)(bp);
146 
147 	/* if successful, wander through dos partition table */
148 	if (biowait(bp)) {
149 		msg = "dos partition I/O error";
150 		goto done;
151 	} else {
152 		struct mbr_partition *ourdp = NULL;
153 
154 		ourdp = mbr_findslice(dp, bp);
155 		if (ourdp ==  NO_MBR_SIGNATURE)
156 			goto nombrpart;
157 
158 		for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
159 			/* Install in partition e, f, g, or h. */
160 			pp = &lp->d_partitions[RAW_PART + 1 + i];
161 			pp->p_offset = dp->mbrp_start;
162 			pp->p_size = dp->mbrp_size;
163 			if (dp->mbrp_type == MBR_PTYPE_LNXEXT2)
164 				pp->p_fstype = FS_EX2FS;
165 
166 			if (dp->mbrp_type == MBR_PTYPE_LNXSWAP)
167 				pp->p_fstype = FS_SWAP;
168 
169 			/* is this ours? */
170 			if (dp == ourdp) {
171 				/* need sector address for SCSI/IDE,
172 				 cylinder for ESDI/ST506/RLL */
173 				dospartoff = dp->mbrp_start;
174 				cyl = MBR_PCYL(dp->mbrp_scyl, dp->mbrp_ssect);
175 
176 				/* update disklabel with details */
177 				lp->d_partitions[2].p_size =
178 				    dp->mbrp_size;
179 				lp->d_partitions[2].p_offset =
180 				    dp->mbrp_start;
181 			}
182 		}
183 		lp->d_npartitions = RAW_PART + 1 + i;
184 	}
185 
186 nombrpart:
187 	/* next, dig out disk label */
188 	bp->b_blkno = dospartoff + LABELSECTOR;
189 	bp->b_cylinder = cyl;
190 	bp->b_bcount = lp->d_secsize;
191 	bp->b_oflags &= ~(BO_DONE);
192 	bp->b_flags |= B_READ;
193 	(*strat)(bp);
194 
195 	/* if successful, locate disk label within block and validate */
196 	if (biowait(bp)) {
197 		msg = "disk label I/O error";
198 		goto done;
199 	}
200 	for (dlp = (struct disklabel *)bp->b_data;
201 	    dlp <= (struct disklabel *)((char*)bp->b_data + lp->d_secsize - sizeof(*dlp));
202 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
203 		if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
204 			if (msg == NULL)
205 				msg = "no disk label";
206 		} else if (dlp->d_npartitions > MAXPARTITIONS ||
207 			   dkcksum(dlp) != 0)
208 			msg = "disk label corrupted";
209 		else {
210 			*lp = *dlp;
211 			msg = NULL;
212 			break;
213 		}
214 	}
215 
216 	if (msg)
217 		goto done;
218 
219 	/* obtain bad sector table if requested and present */
220 	if (osdep && (lp->d_flags & D_BADSECT)) {
221 		struct dkbad *db;
222 
223 		bdp = &osdep->bad;
224 		i = 0;
225 		do {
226 			/* read a bad sector table */
227 			bp->b_oflags &= ~(BO_DONE);
228 			bp->b_flags |= B_READ;
229 			bp->b_blkno = lp->d_secperunit - lp->d_nsectors + i;
230 			if (lp->d_secsize > DEV_BSIZE)
231 				bp->b_blkno *= lp->d_secsize / DEV_BSIZE;
232 			else
233 				bp->b_blkno /= DEV_BSIZE / lp->d_secsize;
234 			bp->b_bcount = lp->d_secsize;
235 			bp->b_cylinder = lp->d_ncylinders - 1;
236 			(*strat)(bp);
237 
238 			/* if successful, validate, otherwise try another */
239 			if (biowait(bp)) {
240 				msg = "bad sector table I/O error";
241 			} else {
242 				db = (struct dkbad *)(bp->b_data);
243 #define DKBAD_MAGIC 0x4321
244 				if (db->bt_mbz == 0
245 					&& db->bt_flag == DKBAD_MAGIC) {
246 					msg = NULL;
247 					*bdp = *db;
248 					break;
249 				} else
250 					msg = "bad sector table corrupted";
251 			}
252 		} while (bp->b_error != 0 && (i += 2) < 10 &&
253 			i < lp->d_nsectors);
254 	}
255 
256 done:
257 	brelse(bp, 0);
258 	return (msg);
259 }
260 
261 /*
262  * Write disk label back to device after modification.
263  */
264 int
writedisklabel(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp,struct cpu_disklabel * osdep)265 writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
266     struct cpu_disklabel *osdep)
267 {
268 	struct mbr_partition *dp;
269 	struct buf *bp;
270 	struct disklabel *dlp;
271 	int error, dospartoff, cyl;
272 
273 	/* get a buffer and initialize it */
274 	bp = geteblk((int)lp->d_secsize);
275 	bp->b_dev = dev;
276 
277 	/* do dos partitions in the process of getting disklabel? */
278 	dospartoff = 0;
279 	cyl = LABELSECTOR / lp->d_secpercyl;
280 	if (!osdep)
281 		goto nombrpart;
282 	dp = osdep->dosparts;
283 
284 	/* read master boot record */
285 	bp->b_blkno = MBR_BBSECTOR;
286 	bp->b_bcount = lp->d_secsize;
287 	bp->b_flags |= B_READ;
288 	bp->b_cylinder = MBR_BBSECTOR / lp->d_secpercyl;
289 	(*strat)(bp);
290 
291 	if ((error = biowait(bp)) == 0) {
292 		struct mbr_partition *ourdp = NULL;
293 
294 		ourdp = mbr_findslice(dp, bp);
295 		if (ourdp ==  NO_MBR_SIGNATURE)
296 			goto nombrpart;
297 
298 		if (ourdp) {
299 			/* need sector address for SCSI/IDE,
300 			 cylinder for ESDI/ST506/RLL */
301 			dospartoff = ourdp->mbrp_start;
302 			cyl = MBR_PCYL(ourdp->mbrp_scyl, ourdp->mbrp_ssect);
303 		}
304 	}
305 
306 nombrpart:
307 	/* next, dig out disk label */
308 	bp->b_blkno = dospartoff + LABELSECTOR;
309 	bp->b_cylinder = cyl;
310 	bp->b_bcount = lp->d_secsize;
311 	bp->b_oflags &= ~(BO_DONE);
312 	bp->b_flags |= B_READ;
313 	(*strat)(bp);
314 
315 	/* if successful, locate disk label within block and validate */
316 	if ((error = biowait(bp)) != 0)
317 		goto done;
318 	for (dlp = (struct disklabel *)bp->b_data;
319 	    dlp <= (struct disklabel *)((char*)bp->b_data + lp->d_secsize - sizeof(*dlp));
320 	    dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
321 		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
322 		    dkcksum(dlp) == 0) {
323 			*dlp = *lp;
324 			bp->b_oflags &= ~(BO_DONE);
325 			bp->b_flags &= ~(B_READ);
326 			bp->b_flags |= B_WRITE;
327 			(*strat)(bp);
328 			error = biowait(bp);
329 			goto done;
330 		}
331 	}
332 	error = ESRCH;
333 
334 done:
335 	brelse(bp, 0);
336 	return (error);
337 }
338