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