xref: /netbsd-src/sys/arch/atari/atari/disksubr.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: disksubr.c,v 1.23 2001/09/16 16:34:28 wiz Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Leo Weppelman.
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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Leo Weppelman.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef DISKLABEL_NBDA
34 #define	DISKLABEL_NBDA	/* required */
35 #endif
36 
37 #include "opt_compat_netbsd.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/buf.h>
42 #include <ufs/ufs/dinode.h>
43 #include <ufs/ffs/fs.h>
44 #include <sys/disk.h>
45 #include <sys/disklabel.h>
46 #include <machine/ahdilabel.h>
47 
48 /*
49  * BBSIZE in <ufs/ffs/fs.h> must be greater than
50  * or equal to BBMINSIZE in <machine/disklabel.h>
51  */
52 #if BBSIZE < BBMINSIZE
53 #error BBSIZE smaller than BBMINSIZE
54 #endif
55 
56 static void  ck_label __P((struct disklabel *, struct cpu_disklabel *));
57 static int   bsd_label __P((dev_t, void (*)(struct buf *),
58 			struct disklabel *, u_int, u_int *));
59 static int   ahdi_label __P((dev_t, void (*)(struct buf *),
60 			struct disklabel *, struct cpu_disklabel *));
61 static void  ahdi_to_bsd __P((struct disklabel *, struct ahdi_ptbl *));
62 static u_int ahdi_getparts __P((dev_t, void (*)(struct buf *), u_int,
63 					u_int, u_int, struct ahdi_ptbl *));
64 
65 /*
66  * Determine the size of the transfer, and make sure it is
67  * within the boundaries of the partition. Adjust transfer
68  * if needed, and signal errors or early completion.
69  */
70 int
71 bounds_check_with_label(bp, lp, wlabel)
72 	struct buf		*bp;
73 	struct disklabel	*lp;
74 	int			wlabel;
75 {
76 	struct partition	*pp;
77 	u_int			maxsz, sz;
78 
79 	pp = &lp->d_partitions[DISKPART(bp->b_dev)];
80 	if (bp->b_flags & B_RAW) {
81 		if (bp->b_bcount & (lp->d_secsize - 1)) {
82 			bp->b_error = EINVAL;
83 			bp->b_flags |= B_ERROR;
84 			return(-1);
85 		}
86 		if (lp->d_secsize < DEV_BSIZE)
87 			maxsz = pp->p_size / (DEV_BSIZE / lp->d_secsize);
88 		else maxsz = pp->p_size * (lp->d_secsize / DEV_BSIZE);
89 		sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
90 	} else {
91 		maxsz = pp->p_size;
92 		sz = (bp->b_bcount + lp->d_secsize - 1) / lp->d_secsize;
93 	}
94 
95 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
96 		if (bp->b_blkno == maxsz) {
97 			/*
98 			 * trying to get one block beyond return EOF.
99 			 */
100 			bp->b_resid = bp->b_bcount;
101 			return(0);
102 		}
103 		if (bp->b_blkno > maxsz || bp->b_blkno < 0) {
104 			bp->b_error = EINVAL;
105 			bp->b_flags |= B_ERROR;
106 			return(-1);
107 		}
108 		sz = maxsz - bp->b_blkno;
109 
110 		/*
111 		 * adjust count down
112 		 */
113 		if (bp->b_flags & B_RAW)
114 			bp->b_bcount = sz << DEV_BSHIFT;
115 		else bp->b_bcount = sz * lp->d_secsize;
116 	}
117 
118 	/*
119 	 * calc cylinder for disksort to order transfers with
120 	 */
121 	bp->b_cylinder = (bp->b_blkno + pp->p_offset) / lp->d_secpercyl;
122 	return(1);
123 }
124 
125 /*
126  * Attempt to read a disk label from a device using the
127  * indicated strategy routine. The label must be partly
128  * set up before this:
129  * secpercyl and anything required in the strategy routine
130  * (e.g. sector size) must be filled in before calling us.
131  * Returns NULL on success and an error string on failure.
132  */
133 char *
134 readdisklabel(dev, strat, lp, clp)
135 	dev_t			dev;
136 	void			(*strat)(struct buf *);
137 	struct disklabel	*lp;
138 	struct cpu_disklabel	*clp;
139 {
140 	int			e;
141 
142 	if (clp != NULL)
143 		bzero(clp, sizeof *clp);
144 	else printf("Warning: clp == NULL\n");
145 
146 	/*
147 	 * Give some guaranteed validity to the disk label.
148 	 */
149 	if (lp->d_secsize == 0)
150 		lp->d_secsize = DEV_BSIZE;
151 	if (lp->d_secperunit == 0)
152 		lp->d_secperunit = 0x1fffffff;
153 	if (lp->d_secpercyl == 0)
154 		return("Zero secpercyl");
155 
156 	/*
157 	 * Some parts of the kernel (see scsipi/cd.c for an example)
158 	 * assume that stuff they already had setup in d_partitions
159 	 * is still there after reading the disklabel. Hence the
160 	 * 'if 0'
161 	 */
162 #if 0
163 	bzero(lp->d_partitions, sizeof lp->d_partitions);
164 #endif
165 
166 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
167 	lp->d_npartitions                 = RAW_PART + 1;
168 	lp->d_bbsize                      = BBSIZE;
169 	lp->d_sbsize                      = SBSIZE;
170 
171 #ifdef DISKLABEL_NBDA
172 	/* Try the native NetBSD/Atari format first. */
173 	e = bsd_label(dev, strat, lp, 0, clp != NULL ? &clp->cd_label : NULL);
174 #endif
175 #if 0
176 	/* Other label formats go here. */
177 	if (e > 0)
178 		e = foo_label(dev, strat, lp, ...);
179 #endif
180 #ifdef DISKLABEL_AHDI
181 	/* The unprotected AHDI format comes last. */
182 	if (e > 0 && (clp != NULL))
183 		e = ahdi_label(dev, strat, lp, clp);
184 #endif
185 	if (e < 0)
186 		return("I/O error");
187 
188 	/* Unknown format or unitialised volume? */
189 	if (e > 0)
190 		uprintf("Warning: unknown disklabel format"
191 			"- assuming empty disk\n");
192 
193 	/* Calulate new checksum. */
194 	lp->d_magic = lp->d_magic2 = DISKMAGIC;
195 	lp->d_checksum = 0;
196 	lp->d_checksum = dkcksum(lp);
197 
198 	return(NULL);
199 }
200 
201 /*
202  * Check new disk label for sensibility before setting it.
203  */
204 int
205 setdisklabel(olp, nlp, openmask, clp)
206 	struct disklabel	*olp, *nlp;
207 	u_long			openmask;
208 	struct cpu_disklabel	*clp;
209 {
210 	/* special case to allow disklabel to be invalidated */
211 	if (nlp->d_magic == 0xffffffff) {
212 		*olp = *nlp;
213 		return(0);
214 	}
215 
216 	/* sanity clause */
217 	if (nlp->d_secpercyl == 0 || nlp->d_npartitions > MAXPARTITIONS
218 	  || nlp->d_secsize  == 0 || (nlp->d_secsize % DEV_BSIZE) != 0
219 	  || nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC
220 	  || dkcksum(nlp) != 0)
221 		return(EINVAL);
222 
223 #ifdef DISKLABEL_AHDI
224 	if (clp && clp->cd_bblock)
225 		ck_label(nlp, clp);
226 #endif
227 	while (openmask) {
228 		struct partition *op, *np;
229 		int i = ffs(openmask) - 1;
230 		openmask &= ~(1 << i);
231 		if (i >= nlp->d_npartitions)
232 			return(EBUSY);
233 		op = &olp->d_partitions[i];
234 		np = &nlp->d_partitions[i];
235 		if (np->p_offset != op->p_offset || np->p_size < op->p_size)
236 			return(EBUSY);
237 		/*
238 		 * Copy internally-set partition information
239 		 * if new label doesn't include it.		XXX
240 		 */
241 		if (np->p_fstype == FS_UNUSED && op->p_fstype != FS_UNUSED) {
242 			np->p_fstype = op->p_fstype;
243 			np->p_fsize  = op->p_fsize;
244 			np->p_frag   = op->p_frag;
245 			np->p_cpg    = op->p_cpg;
246 		}
247 	}
248  	nlp->d_checksum = 0;
249  	nlp->d_checksum = dkcksum(nlp);
250 	*olp = *nlp;
251 	return(0);
252 }
253 
254 /*
255  * Write disk label back to device after modification.
256  */
257 int
258 writedisklabel(dev, strat, lp, clp)
259 	dev_t			dev;
260 	void			(*strat)(struct buf *);
261 	struct disklabel	*lp;
262 	struct cpu_disklabel	*clp;
263 {
264 	struct buf		*bp;
265 	u_int			blk;
266 	int			rv;
267 
268 	blk = clp->cd_bblock;
269 	if (blk == NO_BOOT_BLOCK)
270 		return(ENXIO);
271 
272 	bp = geteblk(BBMINSIZE);
273 	bp->b_dev      = MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART);
274 	bp->b_flags    |= B_READ;
275 	bp->b_bcount   = BBMINSIZE;
276 	bp->b_blkno    = blk;
277 	bp->b_cylinder = blk / lp->d_secpercyl;
278 	(*strat)(bp);
279 	rv = biowait(bp);
280 	if (!rv) {
281 		struct bootblock *bb = (struct bootblock *)bp->b_data;
282 		/*
283 		 * Allthough the disk pack label may appear anywhere
284 		 * in the boot block while reading, it is always
285 		 * written at a fixed location.
286 		 */
287 		if (clp->cd_label != LABELOFFSET) {
288 			clp->cd_label = LABELOFFSET;
289 			bzero(bb, sizeof(*bb));
290 		}
291 		bb->bb_magic = (blk == 0) ? NBDAMAGIC : AHDIMAGIC;
292 		BBSETLABEL(bb, lp);
293 
294 		bp->b_flags    &= ~(B_READ|B_DONE);
295 		bp->b_flags    |= B_WRITE;
296 		bp->b_bcount   = BBMINSIZE;
297 		bp->b_blkno    = blk;
298 		bp->b_cylinder = blk / lp->d_secpercyl;
299 		(*strat)(bp);
300 		rv = biowait(bp);
301 	}
302 	brelse(bp);
303 	return(rv);
304 }
305 
306 /*
307  * Read bootblock at block `blkno' and check
308  * if it contains a valid NetBSD disk label.
309  *
310  * Returns:  0 if successfull,
311  *          -1 if an I/O error occurred,
312  *          +1 if no valid label was found.
313  */
314 static int
315 bsd_label(dev, strat, label, blkno, offsetp)
316 	dev_t			dev;
317 	void			(*strat)(struct buf *);
318 	struct disklabel	*label;
319 	u_int			blkno,
320 				*offsetp;
321 {
322 	struct buf		*bp;
323 	int			rv;
324 
325 	bp = geteblk(BBMINSIZE);
326 	bp->b_dev      = MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART);
327 	bp->b_flags    |= B_READ;
328 	bp->b_bcount   = BBMINSIZE;
329 	bp->b_blkno    = blkno;
330 	bp->b_cylinder = blkno / label->d_secpercyl;
331 	(*strat)(bp);
332 
333 	rv = -1;
334 	if (!biowait(bp)) {
335 		struct bootblock *bb;
336 		u_int32_t   *p, *end;
337 
338 		rv  = 1;
339 		bb  = (struct bootblock *)bp->b_data;
340 		end = (u_int32_t *)((char *)&bb[1] - sizeof(struct disklabel));
341 		for (p = (u_int32_t *)bb; p < end; ++p) {
342 			struct disklabel *dl = (struct disklabel *)&p[1];
343 			/*
344 			 * Compatibility kludge: the boot block magic number is
345 			 * new in 1.1A, in previous versions the disklabel was
346 			 * stored at the end of the boot block (offset 7168).
347 			 */
348 			if (  (  (p[0] == NBDAMAGIC && blkno == 0)
349 			      || (p[0] == AHDIMAGIC && blkno != 0)
350 #ifdef COMPAT_11
351 			      || (char *)dl - (char *)bb == 7168
352 #endif
353 			      )
354 			   && dl->d_npartitions <= MAXPARTITIONS
355 			   && dl->d_magic2 == DISKMAGIC
356 			   && dl->d_magic  == DISKMAGIC
357 		  	   && dkcksum(dl)  == 0
358 			   )	{
359 				if (offsetp != NULL)
360 					*offsetp = (char *)dl - (char *)bb;
361 				*label = *dl;
362 				rv     = 0;
363 				break;
364 			}
365 		}
366 	}
367 	brelse(bp);
368 	return(rv);
369 }
370 
371 #ifdef DISKLABEL_AHDI
372 /*
373  * Check for consistency between the NetBSD partition table
374  * and the AHDI auxiliary root sectors. There's no good reason
375  * to force such consistency, but issuing a warning may help
376  * an inexperienced sysadmin to prevent corruption of AHDI
377  * partitions.
378  */
379 static void
380 ck_label(dl, cdl)
381 	struct disklabel	*dl;
382 	struct cpu_disklabel	*cdl;
383 {
384 	u_int			*rp, i;
385 
386 	for (i = 0; i < dl->d_npartitions; ++i) {
387 		struct partition *p = &dl->d_partitions[i];
388 		if (i == RAW_PART || p->p_size == 0)
389 			continue;
390 		if ( (p->p_offset >= cdl->cd_bslst
391 		   && p->p_offset <= cdl->cd_bslend)
392 		  || (cdl->cd_bslst >= p->p_offset
393 		   && cdl->cd_bslst <  p->p_offset + p->p_size)) {
394 			uprintf("Warning: NetBSD partition %c includes"
395 				" AHDI bad sector list\n", 'a'+i);
396 		}
397 		for (rp = &cdl->cd_roots[0]; *rp; ++rp) {
398 			if (*rp >= p->p_offset
399 			  && *rp < p->p_offset + p->p_size) {
400 				uprintf("Warning: NetBSD partition %c"
401 				" includes AHDI auxiliary root\n", 'a'+i);
402 			}
403 		}
404 	}
405 }
406 
407 /*
408  * Check volume for the existance of an AHDI label. Fetch
409  * NetBSD label from NBD or RAW partition, or otherwise
410  * create a fake NetBSD label based on the AHDI label.
411  *
412  * Returns:  0 if successful,
413  *          -1 if an I/O error occurred,
414  *          +1 if no valid AHDI label was found.
415  */
416 int
417 ahdi_label(dev, strat, dl, cdl)
418 	dev_t			dev;
419 	void			(*strat)(struct buf *);
420 	struct disklabel	*dl;
421 	struct cpu_disklabel	*cdl;
422 {
423 	struct ahdi_ptbl	apt;
424 	u_int			i;
425 	int			j;
426 
427 	/*
428 	 * The AHDI format requires a specific block size.
429 	 */
430 	if (dl->d_secsize != AHDI_BSIZE)
431 		return(1);
432 
433 	/*
434 	 * Fetch the AHDI partition descriptors.
435 	 */
436 	apt.at_cdl    = cdl;
437 	apt.at_nroots = apt.at_nparts = 0;
438 	i = ahdi_getparts(dev, strat, dl->d_secpercyl,
439 			  AHDI_BBLOCK, AHDI_BBLOCK, &apt);
440 	if (i) {
441 		if (i < dl->d_secperunit)
442 			return(-1);	/* disk read error		*/
443 		else return(1);		/* reading past end of medium	*/
444 	}
445 
446 	/*
447 	 * Perform sanity checks.
448 	 */
449 	if (apt.at_bslst == 0 || apt.at_bslend == 0) {
450 		/*
451 		 * Illegal according to Atari, however some hd-utils
452 		 * use it - notably ICD *sigh*
453 		 * Work around it.....
454 		 */
455 		apt.at_bslst = apt.at_bslend = 0;
456 		uprintf("Warning: Illegal 'bad sector list' format"
457 			"- assuming non exists\n");
458 	}
459 	if (apt.at_hdsize == 0 || apt.at_nparts == 0)	/* unlikely */
460 		return(1);
461 	if (apt.at_nparts > AHDI_MAXPARTS)		/* XXX kludge */
462 		return(-1);
463 	for (i = 0; i < apt.at_nparts; ++i) {
464 		struct ahdi_part *p1 = &apt.at_parts[i];
465 
466 		for (j = 0; j < apt.at_nroots; ++j) {
467 			u_int	aux = apt.at_roots[j];
468 			if (aux >= p1->ap_st && aux <= p1->ap_end)
469 				return(1);
470 		}
471 		for (j = i + 1; j < apt.at_nparts; ++j) {
472 			struct ahdi_part *p2 = &apt.at_parts[j];
473 			if (p1->ap_st >= p2->ap_st && p1->ap_st <= p2->ap_end)
474 				return(1);
475 			if (p2->ap_st >= p1->ap_st && p2->ap_st <= p1->ap_end)
476 				return(1);
477 		}
478 		if (p1->ap_st >= apt.at_bslst && p1->ap_st <= apt.at_bslend)
479 			return(1);
480 		if (apt.at_bslst >= p1->ap_st && apt.at_bslst <= p1->ap_end)
481 			return(1);
482 	}
483 
484 	/*
485 	 * Search for a NetBSD disk label
486 	 */
487 	apt.at_bblock = NO_BOOT_BLOCK;
488 	for (i = 0; i < apt.at_nparts; ++i) {
489 		struct ahdi_part *pd = &apt.at_parts[i];
490 		u_int		 id  = *((u_int32_t *)&pd->ap_flg);
491 		if (id == AHDI_PID_NBD || id == AHDI_PID_RAW) {
492 			u_int	blkno = pd->ap_st;
493 			j = bsd_label(dev, strat, dl, blkno, &apt.at_label);
494 			if (j < 0) {
495 				return(j);		/* I/O error */
496 			}
497 			if (!j) {
498 				apt.at_bblock = blkno;	/* got it */
499 				ck_label(dl, cdl);
500 				return(0);
501 			}
502 			/*
503 			 * Not yet, but if this is the first NBD partition
504 			 * on this volume, we'll mark it anyway as a possible
505 			 * destination for future writedisklabel() calls, just
506 			 * in case there is no valid disk label on any of the
507 			 * other AHDI partitions.
508 			 */
509 			if (id == AHDI_PID_NBD
510 			    && apt.at_bblock == NO_BOOT_BLOCK)
511 				apt.at_bblock = blkno;
512 		}
513 	}
514 
515 	/*
516 	 * No NetBSD disk label on this volume, use the AHDI
517 	 * label to create a fake BSD label. If there is no
518 	 * NBD partition on this volume either, subsequent
519 	 * writedisklabel() calls will fail.
520 	 */
521 	ahdi_to_bsd(dl, &apt);
522 	return(0);
523 }
524 
525 /*
526  * Map the AHDI partition table to the NetBSD table.
527  *
528  * This means:
529  *  Part 0   : Root
530  *  Part 1   : Swap
531  *  Part 2   : Whole disk
532  *  Part 3.. : User partitions
533  *
534  * When more than one root partition is found, only the first one will
535  * be recognized as such. The others are mapped as user partitions.
536  */
537 static void
538 ahdi_to_bsd(dl, apt)
539 	struct disklabel	*dl;
540 	struct ahdi_ptbl	*apt;
541 {
542 	int		i, have_root, user_part;
543 
544 	user_part = RAW_PART;
545 	have_root = (apt->at_bblock != NO_BOOT_BLOCK);
546 
547 	for (i = 0; i < apt->at_nparts; ++i) {
548 		struct ahdi_part *pd = &apt->at_parts[i];
549 		int		 fst, pno = -1;
550 
551 		switch (*((u_int32_t *)&pd->ap_flg)) {
552 			case AHDI_PID_NBD:
553 				/*
554 				 * If this partition has been marked as the
555 				 * first NBD partition, it will be the root
556 				 * partition.
557 				 */
558 				if (pd->ap_st == apt->at_bblock)
559 					pno = 0;
560 				/* FALL THROUGH */
561 			case AHDI_PID_NBR:
562 				/*
563 				 * If there is no NBD partition and this is
564 				 * the first NBR partition, it will be the
565 				 * root partition.
566 				 */
567 				if (!have_root) {
568 					have_root = 1;
569 					pno = 0;
570 				}
571 				/* FALL THROUGH */
572 			case AHDI_PID_NBU:
573 				fst = FS_BSDFFS;
574 				break;
575 			case AHDI_PID_NBS:
576 			case AHDI_PID_SWP:
577 				if (dl->d_partitions[1].p_size == 0)
578 					pno = 1;
579 				fst = FS_SWAP;
580 				break;
581 			case AHDI_PID_BGM:
582 			case AHDI_PID_GEM:
583 				fst = FS_MSDOS;
584 				break;
585 			default:
586 				fst = FS_OTHER;
587 				break;
588 		}
589 		if (pno < 0) {
590 			if((pno = user_part + 1) >= MAXPARTITIONS)
591 				continue;
592 			user_part = pno;
593 		}
594 		dl->d_partitions[pno].p_size   = pd->ap_end - pd->ap_st + 1;
595 		dl->d_partitions[pno].p_offset = pd->ap_st;
596 		dl->d_partitions[pno].p_fstype = fst;
597 	}
598 	dl->d_npartitions = user_part + 1;
599 }
600 
601 /*
602  * Fetch the AHDI partitions and auxiliary roots.
603  *
604  * Returns:  0 if successful,
605  *           otherwise an I/O error occurred, and the
606  *           number of the offending block is returned.
607  */
608 static u_int
609 ahdi_getparts(dev, strat, secpercyl, rsec, esec, apt)
610 	dev_t			dev;
611 	void			(*strat)(struct buf *);
612 	u_int			secpercyl,
613 				rsec, esec;
614 	struct ahdi_ptbl	*apt;
615 {
616 	struct ahdi_part	*part, *end;
617 	struct ahdi_root	*root;
618 	struct buf		*bp;
619 	u_int			rv;
620 
621 	bp = geteblk(AHDI_BSIZE);
622 	bp->b_dev      = MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART);
623 	bp->b_flags    |= B_READ;
624 	bp->b_bcount   = AHDI_BSIZE;
625 	bp->b_blkno    = rsec;
626 	bp->b_cylinder = rsec / secpercyl;
627 	(*strat)(bp);
628 	if (biowait(bp)) {
629 		rv = rsec + (rsec == 0);
630 		goto done;
631 	}
632 	root = (struct ahdi_root *)bp->b_data;
633 
634 	if (rsec == AHDI_BBLOCK)
635 		end = &root->ar_parts[AHDI_MAXRPD];
636 	else end = &root->ar_parts[AHDI_MAXARPD];
637 	for (part = root->ar_parts; part < end; ++part) {
638 		u_int	id = *((u_int32_t *)&part->ap_flg);
639 		if (!(id & 0x01000000))
640 			continue;
641 		if ((id &= 0x00ffffff) == AHDI_PID_XGM) {
642 			u_int	offs = part->ap_st + esec;
643 			if (apt->at_nroots < AHDI_MAXROOTS)
644 				apt->at_roots[apt->at_nroots] = offs;
645 			apt->at_nroots += 1;
646 			rv = ahdi_getparts(dev, strat, secpercyl, offs,
647 				(esec == AHDI_BBLOCK) ? offs : esec, apt);
648 			if (rv)
649 				goto done;
650 			continue;
651 		}
652 		else if (apt->at_nparts < AHDI_MAXPARTS) {
653 			struct ahdi_part *p = &apt->at_parts[apt->at_nparts];
654 			*((u_int32_t *)&p->ap_flg) = id;
655 			p->ap_st  = part->ap_st + rsec;
656 			p->ap_end = p->ap_st + part->ap_size - 1;
657 		}
658 		apt->at_nparts += 1;
659 	}
660 	apt->at_hdsize = root->ar_hdsize;
661 	apt->at_bslst  = root->ar_bslst;
662 	apt->at_bslend = root->ar_bslst + root->ar_bslsize - 1;
663 	rv = 0;
664 done:
665 	brelse(bp);
666 	return(rv);
667 }
668 #endif /* DISKLABEL_AHDI */
669