Annotation of sys/arch/sparc/sparc/disksubr.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: disksubr.c,v 1.70 2007/07/01 19:06:58 miod Exp $ */
2: /* $NetBSD: disksubr.c,v 1.16 1996/04/28 20:25:59 thorpej Exp $ */
3:
4: /*
5: * Copyright (c) 1994, 1995 Gordon W. Ross
6: * Copyright (c) 1994 Theo de Raadt
7: * All rights reserved.
8: *
9: * Redistribution and use in source and binary forms, with or without
10: * modification, are permitted provided that the following conditions
11: * are met:
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: * 2. Redistributions in binary form must reproduce the above copyright
15: * notice, this list of conditions and the following disclaimer in the
16: * documentation and/or other materials provided with the distribution.
17: *
18: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: */
29:
30: #include <sys/param.h>
31: #include <sys/systm.h>
32: #include <sys/buf.h>
33: #include <sys/ioccom.h>
34: #include <sys/device.h>
35: #include <sys/disklabel.h>
36: #include <sys/disk.h>
37: #include <sys/dkbad.h>
38:
39: #include <machine/cpu.h>
40: #include <machine/autoconf.h>
41: #include <dev/sun/disklabel.h>
42:
43: #if defined(SUN4)
44: #include <machine/oldmon.h>
45: #endif
46:
47: #include "cd.h"
48:
49: static char *disklabel_sun_to_bsd(struct sun_disklabel *, struct disklabel *);
50: static int disklabel_bsd_to_sun(struct disklabel *, struct sun_disklabel *);
51: static __inline u_int sun_extended_sum(struct sun_disklabel *, void *);
52:
53: #if NCD > 0
54: extern void cdstrategy(struct buf *);
55: #endif
56:
57: /*
58: * Attempt to read a disk label from a device
59: * using the indicated strategy routine.
60: * The label must be partly set up before this:
61: * secpercyl, secsize and anything required for a block i/o read
62: * operation in the driver's strategy/start routines
63: * must be filled in before calling us.
64: *
65: * Return buffer for use in signalling errors if requested.
66: *
67: * Returns null on success and an error string on failure.
68: */
69: char *
70: readdisklabel(dev_t dev, void (*strat)(struct buf *),
71: struct disklabel *lp, int spoofonly)
72: {
73: struct sun_disklabel *slp;
74: struct buf *bp = NULL;
75: char *msg;
76:
77: if ((msg = initdisklabel(lp)))
78: goto done;
79:
80: /*
81: * On sparc64 we check for a CD label first, because our
82: * CD install media contains both sparc & sparc64 labels.
83: * We want the sparc64 machine to find the "CD label", not
84: * the SunOS label, for loading it's kernel.
85: */
86: #if NCD > 0
87: if (strat == cdstrategy) {
88: #if defined(CD9660)
89: if (iso_disklabelspoof(dev, strat, lp) == 0)
90: goto done;
91: #endif
92: #if defined(UDF)
93: if (udf_disklabelspoof(dev, strat, lp) == 0)
94: goto done;
95: #endif
96: }
97: #endif /* NCD > 0 */
98:
99: /* get a buffer and initialize it */
100: bp = geteblk((int)lp->d_secsize);
101: bp->b_dev = dev;
102:
103: if (spoofonly)
104: goto doslabel;
105:
106: bp->b_blkno = LABELSECTOR;
107: bp->b_bcount = lp->d_secsize;
108: bp->b_flags = B_BUSY | B_READ;
109: (*strat)(bp);
110: if (biowait(bp)) {
111: msg = "disk label read error";
112: goto done;
113: }
114:
115: slp = (struct sun_disklabel *)bp->b_data;
116: if (slp->sl_magic == SUN_DKMAGIC) {
117: msg = disklabel_sun_to_bsd(slp, lp);
118: goto done;
119: }
120:
121: msg = checkdisklabel(bp->b_data + LABELOFFSET, lp);
122: if (msg == NULL)
123: goto done;
124:
125: doslabel:
126: msg = readdoslabel(bp, strat, lp, NULL, spoofonly);
127: if (msg == NULL)
128: goto done;
129:
130: /* A CD9660/UDF label may be on a non-CD drive, so recheck */
131: #if defined(CD9660)
132: if (iso_disklabelspoof(dev, strat, lp) == 0) {
133: msg = NULL;
134: goto done;
135: }
136: #endif
137: #if defined(UDF)
138: if (udf_disklabelspoof(dev, strat, lp) == 0) {
139: msg = NULL;
140: goto done;
141: }
142: #endif
143:
144: done:
145: if (bp) {
146: bp->b_flags |= B_INVAL;
147: brelse(bp);
148: }
149: return (msg);
150: }
151:
152: /*
153: * Write disk label back to device after modification.
154: */
155: int
156: writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp)
157: {
158: struct buf *bp = NULL;
159: int error;
160:
161: /* get a buffer and initialize it */
162: bp = geteblk((int)lp->d_secsize);
163: bp->b_dev = dev;
164:
165: error = disklabel_bsd_to_sun(lp, (struct sun_disklabel *)bp->b_data);
166: if (error)
167: goto done;
168:
169: /* Write out the updated label. */
170: bp->b_blkno = LABELSECTOR;
171: bp->b_bcount = lp->d_secsize;
172: bp->b_flags = B_BUSY | B_WRITE;
173: (*strat)(bp);
174: error = biowait(bp);
175:
176: done:
177: if (bp) {
178: bp->b_flags |= B_INVAL;
179: brelse(bp);
180: }
181: return (error);
182: }
183:
184: /************************************************************************
185: *
186: * The rest of this was taken from arch/sparc/scsi/sun_disklabel.c
187: * and then substantially rewritten by Gordon W. Ross
188: *
189: ************************************************************************/
190:
191: /* What partition types to assume for Sun disklabels: */
192: static u_char
193: sun_fstypes[16] = {
194: FS_BSDFFS, /* a */
195: FS_SWAP, /* b */
196: FS_UNUSED, /* c - whole disk */
197: FS_BSDFFS, /* d */
198: FS_BSDFFS, /* e */
199: FS_BSDFFS, /* f */
200: FS_BSDFFS, /* g */
201: FS_BSDFFS, /* h */
202: FS_BSDFFS, /* i */
203: FS_BSDFFS, /* j */
204: FS_BSDFFS, /* k */
205: FS_BSDFFS, /* l */
206: FS_BSDFFS, /* m */
207: FS_BSDFFS, /* n */
208: FS_BSDFFS, /* o */
209: FS_BSDFFS /* p */
210: };
211:
212: /*
213: * Given a struct sun_disklabel, assume it has an extended partition
214: * table and compute the correct value for sl_xpsum.
215: */
216: static __inline u_int
217: sun_extended_sum(struct sun_disklabel *sl, void *end)
218: {
219: u_int sum, *xp, *ep;
220:
221: xp = (u_int *)&sl->sl_xpmag;
222: ep = (u_int *)end;
223:
224: sum = 0;
225: for (; xp < ep; xp++)
226: sum += *xp;
227: return (sum);
228: }
229:
230: /*
231: * Given a SunOS disk label, set lp to a BSD disk label.
232: * Returns NULL on success, else an error string.
233: *
234: * The BSD label is cleared out before this is called.
235: */
236: char *
237: disklabel_sun_to_bsd(struct sun_disklabel *sl, struct disklabel *lp)
238: {
239: struct partition *npp;
240: struct sun_dkpart *spp;
241: int i, secpercyl;
242: u_short cksum = 0, *sp1, *sp2;
243:
244: /* Verify the XOR check. */
245: sp1 = (u_short *)sl;
246: sp2 = (u_short *)(sl + 1);
247: while (sp1 < sp2)
248: cksum ^= *sp1++;
249: if (cksum != 0)
250: return ("SunOS disk label, bad checksum");
251:
252: /* Format conversion. */
253: lp->d_magic = DISKMAGIC;
254: lp->d_magic2 = DISKMAGIC;
255: lp->d_flags = D_VENDOR;
256: memcpy(lp->d_packname, sl->sl_text, sizeof(lp->d_packname));
257:
258: lp->d_secsize = 512;
259: lp->d_nsectors = sl->sl_nsectors;
260: lp->d_ntracks = sl->sl_ntracks;
261: lp->d_ncylinders = sl->sl_ncylinders;
262:
263: secpercyl = sl->sl_nsectors * sl->sl_ntracks;
264: lp->d_secpercyl = secpercyl;
265: if (DL_GETDSIZE(lp) == 0)
266: DL_SETDSIZE(lp, (daddr64_t)secpercyl * sl->sl_ncylinders);
267: lp->d_version = 1;
268:
269: lp->d_sparespercyl = sl->sl_sparespercyl;
270: lp->d_acylinders = sl->sl_acylinders;
271: lp->d_rpm = sl->sl_rpm;
272: lp->d_interleave = sl->sl_interleave;
273:
274: lp->d_npartitions = MAXPARTITIONS;
275: /* These are as defined in <ufs/ffs/fs.h> */
276: lp->d_bbsize = 8192; /* XXX */
277: lp->d_sbsize = 8192; /* XXX */
278:
279: for (i = 0; i < 8; i++) {
280: spp = &sl->sl_part[i];
281: npp = &lp->d_partitions[i];
282: DL_SETPOFFSET(npp, spp->sdkp_cyloffset * secpercyl);
283: DL_SETPSIZE(npp, spp->sdkp_nsectors);
284: if (DL_GETPSIZE(npp) == 0) {
285: npp->p_fstype = FS_UNUSED;
286: } else {
287: npp->p_fstype = sun_fstypes[i];
288: if (npp->p_fstype == FS_BSDFFS) {
289: /*
290: * The sun label does not store the FFS fields,
291: * so just set them with default values here.
292: */
293: npp->p_fragblock =
294: DISKLABELV1_FFS_FRAGBLOCK(2048, 8);
295: npp->p_cpg = 16;
296: }
297: }
298: }
299:
300: /* Clear "extended" partition info, tentatively */
301: for (i = 0; i < SUNXPART; i++) {
302: npp = &lp->d_partitions[i+8];
303: DL_SETPOFFSET(npp, 0);
304: DL_SETPSIZE(npp, 0);
305: npp->p_fstype = FS_UNUSED;
306: }
307:
308: /* Check to see if there's an "extended" partition table
309: * SL_XPMAG partitions had checksums up to just before the
310: * (new) sl_types variable, while SL_XPMAGTYP partitions have
311: * checksums up to the just before the (new) sl_xxx1 variable.
312: */
313: if ((sl->sl_xpmag == SL_XPMAG &&
314: sun_extended_sum(sl, &sl->sl_types) == sl->sl_xpsum) ||
315: (sl->sl_xpmag == SL_XPMAGTYP &&
316: sun_extended_sum(sl, &sl->sl_xxx1) == sl->sl_xpsum)) {
317: /*
318: * There is. Copy over the "extended" partitions.
319: * This code parallels the loop for partitions a-h.
320: */
321: for (i = 0; i < SUNXPART; i++) {
322: spp = &sl->sl_xpart[i];
323: npp = &lp->d_partitions[i+8];
324: DL_SETPOFFSET(npp, spp->sdkp_cyloffset * secpercyl);
325: DL_SETPSIZE(npp, spp->sdkp_nsectors);
326: if (DL_GETPSIZE(npp) == 0) {
327: npp->p_fstype = FS_UNUSED;
328: continue;
329: }
330: npp->p_fstype = sun_fstypes[i+8];
331: if (npp->p_fstype == FS_BSDFFS) {
332: npp->p_fragblock =
333: DISKLABELV1_FFS_FRAGBLOCK(2048, 8);
334: npp->p_cpg = 16;
335: }
336: }
337: if (sl->sl_xpmag == SL_XPMAGTYP)
338: for (i = 0; i < MAXPARTITIONS; i++) {
339: npp = &lp->d_partitions[i];
340: npp->p_fstype = sl->sl_types[i];
341: npp->p_fragblock = sl->sl_fragblock[i];
342: npp->p_cpg = sl->sl_cpg[i];
343: }
344: }
345:
346: lp->d_checksum = 0;
347: lp->d_checksum = dkcksum(lp);
348: return (NULL);
349: }
350:
351: /*
352: * Given a BSD disk label, update the Sun disklabel
353: * pointed to by cp with the new info. Note that the
354: * Sun disklabel may have other info we need to keep.
355: * Returns zero or error code.
356: */
357: static int
358: disklabel_bsd_to_sun(struct disklabel *lp, struct sun_disklabel *sl)
359: {
360: struct partition *npp;
361: struct sun_dkpart *spp;
362: int i, secpercyl;
363: u_short cksum, *sp1, *sp2;
364:
365: /* Enforce preconditions */
366: if (lp->d_secsize != 512 || lp->d_nsectors == 0 || lp->d_ntracks == 0)
367: return (EINVAL);
368:
369: /* Format conversion. */
370: memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname));
371: sl->sl_rpm = lp->d_rpm;
372: sl->sl_pcylinders = lp->d_ncylinders + lp->d_acylinders; /* XXX */
373: sl->sl_sparespercyl = lp->d_sparespercyl;
374: sl->sl_interleave = lp->d_interleave;
375: sl->sl_ncylinders = lp->d_ncylinders;
376: sl->sl_acylinders = lp->d_acylinders;
377: sl->sl_ntracks = lp->d_ntracks;
378: sl->sl_nsectors = lp->d_nsectors;
379:
380: secpercyl = sl->sl_nsectors * sl->sl_ntracks;
381: for (i = 0; i < 8; i++) {
382: spp = &sl->sl_part[i];
383: npp = &lp->d_partitions[i];
384:
385: if (DL_GETPOFFSET(npp) % secpercyl)
386: return (EINVAL);
387: spp->sdkp_cyloffset = DL_GETPOFFSET(npp) / secpercyl;
388: spp->sdkp_nsectors = DL_GETPSIZE(npp);
389: }
390: sl->sl_magic = SUN_DKMAGIC;
391:
392: for (i = 0; i < SUNXPART; i++) {
393: if (DL_GETPOFFSET(&lp->d_partitions[i+8]) ||
394: DL_GETPSIZE(&lp->d_partitions[i+8]))
395: break;
396: }
397: for (i = 0; i < SUNXPART; i++) {
398: spp = &sl->sl_xpart[i];
399: npp = &lp->d_partitions[i+8];
400: if (DL_GETPOFFSET(npp) % secpercyl)
401: return (EINVAL);
402: sl->sl_xpart[i].sdkp_cyloffset =
403: DL_GETPOFFSET(npp) / secpercyl;
404: sl->sl_xpart[i].sdkp_nsectors = DL_GETPSIZE(npp);
405: }
406: for (i = 0; i < MAXPARTITIONS; i++) {
407: npp = &lp->d_partitions[i];
408: sl->sl_types[i] = npp->p_fstype;
409: sl->sl_fragblock[i] = npp->p_fragblock;
410: sl->sl_cpg[i] = npp->p_cpg;
411: }
412: sl->sl_xpmag = SL_XPMAGTYP;
413: sl->sl_xpsum = sun_extended_sum(sl, &sl->sl_xxx1);
414:
415: /* Correct the XOR check. */
416: sp1 = (u_short *)sl;
417: sp2 = (u_short *)(sl + 1);
418: sl->sl_cksum = cksum = 0;
419: while (sp1 < sp2)
420: cksum ^= *sp1++;
421: sl->sl_cksum = cksum;
422:
423: return (0);
424: }
425:
426: /*
427: * Search the bad sector table looking for the specified sector.
428: * Return index if found.
429: * Return -1 if not found.
430: */
431: int
432: isbad(struct dkbad *bt, int cyl, int trk, int sec)
433: {
434: int i;
435: long blk, bblk;
436:
437: blk = ((long)cyl << 16) + (trk << 8) + sec;
438: for (i = 0; i < NBT_BAD; i++) {
439: bblk = ((long)bt->bt_bad[i].bt_cyl << 16) +
440: bt->bt_bad[i].bt_trksec;
441: if (blk == bblk)
442: return (i);
443: if (blk < bblk || bblk < 0)
444: break;
445: }
446: return (-1);
447: }
CVSweb