disksubr.c revision 1.1 1 1.1 bouyer /* $NetBSD: disksubr.c,v 1.1 2003/11/15 17:52:31 bouyer Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
5 1.1 bouyer * All rights reserved.
6 1.1 bouyer *
7 1.1 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1 bouyer * modification, are permitted provided that the following conditions
9 1.1 bouyer * are met:
10 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1 bouyer * notice, this list of conditions and the following disclaimer.
12 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1 bouyer * documentation and/or other materials provided with the distribution.
15 1.1 bouyer * 3. Neither the name of the University nor the names of its contributors
16 1.1 bouyer * may be used to endorse or promote products derived from this software
17 1.1 bouyer * without specific prior written permission.
18 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 1.1 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 1.1 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 bouyer * SUCH DAMAGE.
29 1.1 bouyer */
30 1.1 bouyer
31 1.1 bouyer /*
32 1.1 bouyer * Copyright (c) 1994, 1995 Gordon W. Ross
33 1.1 bouyer * Copyright (c) 1994 Theo de Raadt
34 1.1 bouyer * All rights reserved.
35 1.1 bouyer *
36 1.1 bouyer * Redistribution and use in source and binary forms, with or without
37 1.1 bouyer * modification, are permitted provided that the following conditions
38 1.1 bouyer * are met:
39 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
40 1.1 bouyer * notice, this list of conditions and the following disclaimer.
41 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
43 1.1 bouyer * documentation and/or other materials provided with the distribution.
44 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
45 1.1 bouyer * must display the following acknowledgement:
46 1.1 bouyer * This product includes software developed by Theo de Raadt.
47 1.1 bouyer * 4. The name of the author may not be used to endorse or promote products
48 1.1 bouyer * derived from this software without specific prior written permission
49 1.1 bouyer *
50 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 1.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 1.1 bouyer */
61 1.1 bouyer
62 1.1 bouyer #include <sys/cdefs.h>
63 1.1 bouyer __KERNEL_RCSID(0, "$NetBSD: disksubr.c,v 1.1 2003/11/15 17:52:31 bouyer Exp $");
64 1.1 bouyer
65 1.1 bouyer #include <sys/param.h>
66 1.1 bouyer #include <sys/systm.h>
67 1.1 bouyer #include <sys/buf.h>
68 1.1 bouyer #include <sys/ioccom.h>
69 1.1 bouyer #include <sys/device.h>
70 1.1 bouyer #include <sys/disklabel.h>
71 1.1 bouyer #include <sys/disk.h>
72 1.1 bouyer #include <sys/dkbad.h>
73 1.1 bouyer
74 1.1 bouyer #include <dev/sun/disklabel.h>
75 1.1 bouyer
76 1.1 bouyer #if LABELSECTOR != 0
77 1.1 bouyer #error "Default value of LABELSECTOR no longer zero?"
78 1.1 bouyer #endif
79 1.1 bouyer
80 1.1 bouyer static char *disklabel_sun_to_bsd __P((char *, struct disklabel *));
81 1.1 bouyer static int disklabel_bsd_to_sun __P((struct disklabel *, char *));
82 1.1 bouyer
83 1.1 bouyer /*
84 1.1 bouyer * Attempt to read a disk label from a device
85 1.1 bouyer * using the indicated strategy routine.
86 1.1 bouyer * The label must be partly set up before this:
87 1.1 bouyer * secpercyl, secsize and anything required for a block i/o read
88 1.1 bouyer * operation in the driver's strategy/start routines
89 1.1 bouyer * must be filled in before calling us.
90 1.1 bouyer *
91 1.1 bouyer * Return buffer for use in signalling errors if requested.
92 1.1 bouyer *
93 1.1 bouyer * Returns null on success and an error string on failure.
94 1.1 bouyer */
95 1.1 bouyer const char *
96 1.1 bouyer readdisklabel(dev, strat, lp, clp)
97 1.1 bouyer dev_t dev;
98 1.1 bouyer void (*strat) __P((struct buf *));
99 1.1 bouyer struct disklabel *lp;
100 1.1 bouyer struct cpu_disklabel *clp;
101 1.1 bouyer {
102 1.1 bouyer struct buf *bp;
103 1.1 bouyer struct disklabel *dlp;
104 1.1 bouyer struct sun_disklabel *slp;
105 1.1 bouyer int error;
106 1.1 bouyer
107 1.1 bouyer /* minimal requirements for archtypal disk label */
108 1.1 bouyer if (lp->d_secperunit == 0)
109 1.1 bouyer lp->d_secperunit = 0x1fffffff;
110 1.1 bouyer if (lp->d_npartitions == 0) {
111 1.1 bouyer lp->d_npartitions = RAW_PART + 1;
112 1.1 bouyer if (lp->d_partitions[RAW_PART].p_size == 0)
113 1.1 bouyer lp->d_partitions[RAW_PART].p_size = 0x1fffffff;
114 1.1 bouyer lp->d_partitions[RAW_PART].p_offset = 0;
115 1.1 bouyer }
116 1.1 bouyer
117 1.1 bouyer /* obtain buffer to probe drive with */
118 1.1 bouyer bp = geteblk((int)lp->d_secsize);
119 1.1 bouyer
120 1.1 bouyer /* next, dig out disk label */
121 1.1 bouyer bp->b_dev = dev;
122 1.1 bouyer bp->b_blkno = LABELSECTOR;
123 1.1 bouyer bp->b_cylinder = 0;
124 1.1 bouyer bp->b_bcount = lp->d_secsize;
125 1.1 bouyer bp->b_flags |= B_READ;
126 1.1 bouyer (*strat)(bp);
127 1.1 bouyer
128 1.1 bouyer /* if successful, locate disk label within block and validate */
129 1.1 bouyer error = biowait(bp);
130 1.1 bouyer if (error == 0) {
131 1.1 bouyer /* Save the whole block in case it has info we need. */
132 1.1 bouyer memcpy(clp->cd_block, bp->b_data, sizeof(clp->cd_block));
133 1.1 bouyer }
134 1.1 bouyer brelse(bp);
135 1.1 bouyer if (error)
136 1.1 bouyer return ("disk label read error");
137 1.1 bouyer
138 1.1 bouyer /* Check for a NetBSD disk label at LABELOFFSET */
139 1.1 bouyer dlp = (struct disklabel *) (clp->cd_block + LABELOFFSET);
140 1.1 bouyer if (dlp->d_magic == DISKMAGIC) {
141 1.1 bouyer if (dkcksum(dlp))
142 1.1 bouyer return ("NetBSD disk label corrupted");
143 1.1 bouyer *lp = *dlp;
144 1.1 bouyer return (NULL);
145 1.1 bouyer }
146 1.1 bouyer
147 1.1 bouyer /* Check for a Sun disk label (for PROM compatibility). */
148 1.1 bouyer slp = (struct sun_disklabel *) clp->cd_block;
149 1.1 bouyer if (slp->sl_magic == SUN_DKMAGIC)
150 1.1 bouyer return (disklabel_sun_to_bsd(clp->cd_block, lp));
151 1.1 bouyer
152 1.1 bouyer /*
153 1.1 bouyer * Check for a NetBSD disk label somewhere in LABELSECTOR
154 1.1 bouyer * (compat with others big-endian boxes)
155 1.1 bouyer */
156 1.1 bouyer for (dlp = (struct disklabel *)clp->cd_block;
157 1.1 bouyer dlp <= (struct disklabel *)((char *)clp->cd_block +
158 1.1 bouyer DEV_BSIZE - sizeof(*dlp));
159 1.1 bouyer dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
160 1.1 bouyer if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
161 1.1 bouyer continue;
162 1.1 bouyer }
163 1.1 bouyer if (dlp->d_npartitions > MAXPARTITIONS || dkcksum(dlp) != 0)
164 1.1 bouyer return("NetBSD disk label corrupted");
165 1.1 bouyer else {
166 1.1 bouyer *lp = *dlp;
167 1.1 bouyer return(NULL);
168 1.1 bouyer }
169 1.1 bouyer }
170 1.1 bouyer
171 1.1 bouyer memset(clp->cd_block, 0, sizeof(clp->cd_block));
172 1.1 bouyer return ("no disk label");
173 1.1 bouyer }
174 1.1 bouyer
175 1.1 bouyer /*
176 1.1 bouyer * Check new disk label for sensibility
177 1.1 bouyer * before setting it.
178 1.1 bouyer */
179 1.1 bouyer int
180 1.1 bouyer setdisklabel(olp, nlp, openmask, clp)
181 1.1 bouyer struct disklabel *olp, *nlp;
182 1.1 bouyer u_long openmask;
183 1.1 bouyer struct cpu_disklabel *clp;
184 1.1 bouyer {
185 1.1 bouyer int i;
186 1.1 bouyer struct partition *opp, *npp;
187 1.1 bouyer
188 1.1 bouyer /* sanity clause */
189 1.1 bouyer if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0 ||
190 1.1 bouyer (nlp->d_secsize % DEV_BSIZE) != 0)
191 1.1 bouyer return(EINVAL);
192 1.1 bouyer
193 1.1 bouyer /* special case to allow disklabel to be invalidated */
194 1.1 bouyer if (nlp->d_magic == 0xffffffff) {
195 1.1 bouyer *olp = *nlp;
196 1.1 bouyer return (0);
197 1.1 bouyer }
198 1.1 bouyer
199 1.1 bouyer if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
200 1.1 bouyer dkcksum(nlp) != 0)
201 1.1 bouyer return (EINVAL);
202 1.1 bouyer
203 1.1 bouyer while ((i = ffs(openmask)) != 0) {
204 1.1 bouyer i--;
205 1.1 bouyer openmask &= ~(1 << i);
206 1.1 bouyer if (nlp->d_npartitions <= i)
207 1.1 bouyer return (EBUSY);
208 1.1 bouyer opp = &olp->d_partitions[i];
209 1.1 bouyer npp = &nlp->d_partitions[i];
210 1.1 bouyer if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
211 1.1 bouyer return (EBUSY);
212 1.1 bouyer }
213 1.1 bouyer
214 1.1 bouyer *olp = *nlp;
215 1.1 bouyer return (0);
216 1.1 bouyer }
217 1.1 bouyer
218 1.1 bouyer /*
219 1.1 bouyer * Write disk label back to device after modification.
220 1.1 bouyer * Current label is already in clp->cd_block[]
221 1.1 bouyer */
222 1.1 bouyer int
223 1.1 bouyer writedisklabel(dev, strat, lp, clp)
224 1.1 bouyer dev_t dev;
225 1.1 bouyer void (*strat) __P((struct buf *));
226 1.1 bouyer struct disklabel *lp;
227 1.1 bouyer struct cpu_disklabel *clp;
228 1.1 bouyer {
229 1.1 bouyer struct buf *bp;
230 1.1 bouyer int error;
231 1.1 bouyer struct disklabel *dlp;
232 1.1 bouyer struct sun_disklabel *slp;
233 1.1 bouyer
234 1.1 bouyer /*
235 1.1 bouyer * Embed native label in a piece of wasteland.
236 1.1 bouyer */
237 1.1 bouyer if (sizeof(struct disklabel) > sizeof slp->sl_bsdlabel)
238 1.1 bouyer return EFBIG;
239 1.1 bouyer
240 1.1 bouyer slp = (struct sun_disklabel *)clp->cd_block;
241 1.1 bouyer memset(slp->sl_bsdlabel, 0, sizeof(slp->sl_bsdlabel));
242 1.1 bouyer dlp = (struct disklabel *)slp->sl_bsdlabel;
243 1.1 bouyer *dlp = *lp;
244 1.1 bouyer
245 1.1 bouyer /* Build a SunOS compatible label around the native label */
246 1.1 bouyer error = disklabel_bsd_to_sun(lp, clp->cd_block);
247 1.1 bouyer if (error)
248 1.1 bouyer return (error);
249 1.1 bouyer
250 1.1 bouyer /* Get a buffer and copy the new label into it. */
251 1.1 bouyer bp = geteblk((int)lp->d_secsize);
252 1.1 bouyer memcpy(bp->b_data, clp->cd_block, sizeof(clp->cd_block));
253 1.1 bouyer
254 1.1 bouyer /* Write out the updated label. */
255 1.1 bouyer bp->b_dev = dev;
256 1.1 bouyer bp->b_blkno = LABELSECTOR;
257 1.1 bouyer bp->b_cylinder = 0;
258 1.1 bouyer bp->b_bcount = lp->d_secsize;
259 1.1 bouyer bp->b_flags |= B_WRITE;
260 1.1 bouyer (*strat)(bp);
261 1.1 bouyer error = biowait(bp);
262 1.1 bouyer brelse(bp);
263 1.1 bouyer
264 1.1 bouyer return (error);
265 1.1 bouyer }
266 1.1 bouyer
267 1.1 bouyer /*
268 1.1 bouyer * Determine the size of the transfer, and make sure it is
269 1.1 bouyer * within the boundaries of the partition. Adjust transfer
270 1.1 bouyer * if needed, and signal errors or early completion.
271 1.1 bouyer */
272 1.1 bouyer int
273 1.1 bouyer bounds_check_with_label(dk, bp, wlabel)
274 1.1 bouyer struct disk *dk;
275 1.1 bouyer struct buf *bp;
276 1.1 bouyer int wlabel;
277 1.1 bouyer {
278 1.1 bouyer struct disklabel *lp = dk->dk_label;
279 1.1 bouyer struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
280 1.1 bouyer int maxsz = p->p_size;
281 1.1 bouyer int sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
282 1.1 bouyer
283 1.1 bouyer /*
284 1.1 bouyer * overwriting disk label ?
285 1.1 bouyer * The label is always in sector LABELSECTOR.
286 1.1 bouyer * XXX should also protect bootstrap in first 8K
287 1.1 bouyer */
288 1.1 bouyer if (bp->b_blkno + p->p_offset <= LABELSECTOR &&
289 1.1 bouyer (bp->b_flags & B_READ) == 0 && wlabel == 0) {
290 1.1 bouyer bp->b_error = EROFS;
291 1.1 bouyer goto bad;
292 1.1 bouyer }
293 1.1 bouyer
294 1.1 bouyer /* beyond partition? */
295 1.1 bouyer if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
296 1.1 bouyer /* if exactly at end of disk, return an EOF */
297 1.1 bouyer if (bp->b_blkno == maxsz) {
298 1.1 bouyer bp->b_resid = bp->b_bcount;
299 1.1 bouyer return(0);
300 1.1 bouyer }
301 1.1 bouyer /* or truncate if part of it fits */
302 1.1 bouyer sz = maxsz - bp->b_blkno;
303 1.1 bouyer if (sz <= 0) {
304 1.1 bouyer bp->b_error = EINVAL;
305 1.1 bouyer goto bad;
306 1.1 bouyer }
307 1.1 bouyer bp->b_bcount = sz << DEV_BSHIFT;
308 1.1 bouyer }
309 1.1 bouyer
310 1.1 bouyer /* calculate cylinder for disksort to order transfers with */
311 1.1 bouyer bp->b_resid = (bp->b_blkno + p->p_offset) / lp->d_secpercyl;
312 1.1 bouyer return(1);
313 1.1 bouyer bad:
314 1.1 bouyer bp->b_flags |= B_ERROR;
315 1.1 bouyer return(-1);
316 1.1 bouyer }
317 1.1 bouyer
318 1.1 bouyer /************************************************************************
319 1.1 bouyer *
320 1.1 bouyer * The rest of this was taken from arch/sparc/scsi/sun_disklabel.c
321 1.1 bouyer * and then substantially rewritten by Gordon W. Ross
322 1.1 bouyer *
323 1.1 bouyer ************************************************************************/
324 1.1 bouyer
325 1.1 bouyer /* What partition types to assume for Sun disklabels: */
326 1.1 bouyer static u_char
327 1.1 bouyer sun_fstypes[8] = {
328 1.1 bouyer FS_BSDFFS, /* a */
329 1.1 bouyer FS_SWAP, /* b */
330 1.1 bouyer FS_OTHER, /* c - whole disk */
331 1.1 bouyer FS_BSDFFS, /* d */
332 1.1 bouyer FS_BSDFFS, /* e */
333 1.1 bouyer FS_BSDFFS, /* f */
334 1.1 bouyer FS_BSDFFS, /* g */
335 1.1 bouyer FS_BSDFFS, /* h */
336 1.1 bouyer };
337 1.1 bouyer
338 1.1 bouyer /*
339 1.1 bouyer * Given a SunOS disk label, set lp to a BSD disk label.
340 1.1 bouyer * Returns NULL on success, else an error string.
341 1.1 bouyer *
342 1.1 bouyer * The BSD label is cleared out before this is called.
343 1.1 bouyer */
344 1.1 bouyer static char *
345 1.1 bouyer disklabel_sun_to_bsd(cp, lp)
346 1.1 bouyer char *cp;
347 1.1 bouyer struct disklabel *lp;
348 1.1 bouyer {
349 1.1 bouyer struct sun_disklabel *sl;
350 1.1 bouyer struct partition *npp;
351 1.1 bouyer struct sun_dkpart *spp;
352 1.1 bouyer int i, secpercyl;
353 1.1 bouyer u_short cksum, *sp1, *sp2;
354 1.1 bouyer
355 1.1 bouyer sl = (struct sun_disklabel *)cp;
356 1.1 bouyer
357 1.1 bouyer /* Verify the XOR check. */
358 1.1 bouyer sp1 = (u_short *)sl;
359 1.1 bouyer sp2 = (u_short *)(sl + 1);
360 1.1 bouyer cksum = 0;
361 1.1 bouyer while (sp1 < sp2)
362 1.1 bouyer cksum ^= *sp1++;
363 1.1 bouyer if (cksum != 0)
364 1.1 bouyer return("SunOS disk label, bad checksum");
365 1.1 bouyer
366 1.1 bouyer /* Format conversion. */
367 1.1 bouyer lp->d_magic = DISKMAGIC;
368 1.1 bouyer lp->d_magic2 = DISKMAGIC;
369 1.1 bouyer memcpy(lp->d_packname, sl->sl_text, sizeof(lp->d_packname));
370 1.1 bouyer
371 1.1 bouyer lp->d_secsize = 512;
372 1.1 bouyer lp->d_nsectors = sl->sl_nsectors;
373 1.1 bouyer lp->d_ntracks = sl->sl_ntracks;
374 1.1 bouyer lp->d_ncylinders = sl->sl_ncylinders;
375 1.1 bouyer
376 1.1 bouyer secpercyl = sl->sl_nsectors * sl->sl_ntracks;
377 1.1 bouyer lp->d_secpercyl = secpercyl;
378 1.1 bouyer lp->d_secperunit = secpercyl * sl->sl_ncylinders;
379 1.1 bouyer
380 1.1 bouyer lp->d_sparespercyl = sl->sl_sparespercyl;
381 1.1 bouyer lp->d_acylinders = sl->sl_acylinders;
382 1.1 bouyer lp->d_rpm = sl->sl_rpm;
383 1.1 bouyer lp->d_interleave = sl->sl_interleave;
384 1.1 bouyer
385 1.1 bouyer lp->d_npartitions = 8;
386 1.1 bouyer /* These are as defined in <ufs/ffs/fs.h> */
387 1.1 bouyer lp->d_bbsize = 8192; /* XXX */
388 1.1 bouyer lp->d_sbsize = 8192; /* XXX */
389 1.1 bouyer
390 1.1 bouyer for (i = 0; i < 8; i++) {
391 1.1 bouyer spp = &sl->sl_part[i];
392 1.1 bouyer npp = &lp->d_partitions[i];
393 1.1 bouyer npp->p_offset = spp->sdkp_cyloffset * secpercyl;
394 1.1 bouyer npp->p_size = spp->sdkp_nsectors;
395 1.1 bouyer if (npp->p_size == 0) {
396 1.1 bouyer npp->p_fstype = FS_UNUSED;
397 1.1 bouyer } else {
398 1.1 bouyer npp->p_fstype = sun_fstypes[i];
399 1.1 bouyer if (npp->p_fstype == FS_BSDFFS) {
400 1.1 bouyer /*
401 1.1 bouyer * The sun label does not store the FFS fields,
402 1.1 bouyer * so just set them with default values here.
403 1.1 bouyer */
404 1.1 bouyer npp->p_fsize = 1024;
405 1.1 bouyer npp->p_frag = 8;
406 1.1 bouyer npp->p_cpg = 16;
407 1.1 bouyer }
408 1.1 bouyer }
409 1.1 bouyer }
410 1.1 bouyer
411 1.1 bouyer lp->d_checksum = 0;
412 1.1 bouyer lp->d_checksum = dkcksum(lp);
413 1.1 bouyer return (NULL);
414 1.1 bouyer }
415 1.1 bouyer
416 1.1 bouyer /*
417 1.1 bouyer * Given a BSD disk label, update the Sun disklabel
418 1.1 bouyer * pointed to by cp with the new info. Note that the
419 1.1 bouyer * Sun disklabel may have other info we need to keep.
420 1.1 bouyer * Returns zero or error code.
421 1.1 bouyer */
422 1.1 bouyer static int
423 1.1 bouyer disklabel_bsd_to_sun(lp, cp)
424 1.1 bouyer struct disklabel *lp;
425 1.1 bouyer char *cp;
426 1.1 bouyer {
427 1.1 bouyer struct sun_disklabel *sl;
428 1.1 bouyer struct partition *npp;
429 1.1 bouyer struct sun_dkpart *spp;
430 1.1 bouyer int i, secpercyl;
431 1.1 bouyer u_short cksum, *sp1, *sp2;
432 1.1 bouyer
433 1.1 bouyer if (lp->d_secsize != 512)
434 1.1 bouyer return (EINVAL);
435 1.1 bouyer
436 1.1 bouyer sl = (struct sun_disklabel *)cp;
437 1.1 bouyer
438 1.1 bouyer /*
439 1.1 bouyer * Format conversion.
440 1.1 bouyer */
441 1.1 bouyer memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname));
442 1.1 bouyer sl->sl_rpm = lp->d_rpm;
443 1.1 bouyer sl->sl_pcylinders = lp->d_ncylinders + lp->d_acylinders; /* XXX */
444 1.1 bouyer sl->sl_sparespercyl = lp->d_sparespercyl;
445 1.1 bouyer sl->sl_interleave = lp->d_interleave;
446 1.1 bouyer sl->sl_ncylinders = lp->d_ncylinders;
447 1.1 bouyer sl->sl_acylinders = lp->d_acylinders;
448 1.1 bouyer sl->sl_ntracks = lp->d_ntracks;
449 1.1 bouyer sl->sl_nsectors = lp->d_nsectors;
450 1.1 bouyer
451 1.1 bouyer secpercyl = sl->sl_nsectors * sl->sl_ntracks;
452 1.1 bouyer for (i = 0; i < 8; i++) {
453 1.1 bouyer spp = &sl->sl_part[i];
454 1.1 bouyer npp = &lp->d_partitions[i];
455 1.1 bouyer
456 1.1 bouyer /*
457 1.1 bouyer * SunOS partitions must start on a cylinder boundary.
458 1.1 bouyer * Note this restriction is forced upon NetBSD/sparc
459 1.1 bouyer * labels too, since we want to keep both labels
460 1.1 bouyer * synchronised.
461 1.1 bouyer */
462 1.1 bouyer if (npp->p_offset % secpercyl)
463 1.1 bouyer return (EINVAL);
464 1.1 bouyer spp->sdkp_cyloffset = npp->p_offset / secpercyl;
465 1.1 bouyer spp->sdkp_nsectors = npp->p_size;
466 1.1 bouyer }
467 1.1 bouyer sl->sl_magic = SUN_DKMAGIC;
468 1.1 bouyer
469 1.1 bouyer /* Compute the XOR check. */
470 1.1 bouyer sp1 = (u_short *)sl;
471 1.1 bouyer sp2 = (u_short *)(sl + 1);
472 1.1 bouyer sl->sl_cksum = cksum = 0;
473 1.1 bouyer while (sp1 < sp2)
474 1.1 bouyer cksum ^= *sp1++;
475 1.1 bouyer sl->sl_cksum = cksum;
476 1.1 bouyer
477 1.1 bouyer return (0);
478 1.1 bouyer }
479 1.1 bouyer
480 1.1 bouyer /*
481 1.1 bouyer * Search the bad sector table looking for the specified sector.
482 1.1 bouyer * Return index if found.
483 1.1 bouyer * Return -1 if not found.
484 1.1 bouyer */
485 1.1 bouyer int
486 1.1 bouyer isbad(bt, cyl, trk, sec)
487 1.1 bouyer struct dkbad *bt;
488 1.1 bouyer int cyl, trk, sec;
489 1.1 bouyer {
490 1.1 bouyer int i;
491 1.1 bouyer long blk, bblk;
492 1.1 bouyer
493 1.1 bouyer blk = ((long)cyl << 16) + (trk << 8) + sec;
494 1.1 bouyer for (i = 0; i < 126; i++) {
495 1.1 bouyer bblk = ((long)bt->bt_bad[i].bt_cyl << 16) +
496 1.1 bouyer bt->bt_bad[i].bt_trksec;
497 1.1 bouyer if (blk == bblk)
498 1.1 bouyer return (i);
499 1.1 bouyer if (blk < bblk || bblk < 0)
500 1.1 bouyer break;
501 1.1 bouyer }
502 1.1 bouyer return (-1);
503 1.1 bouyer }
504