xref: /netbsd-src/sys/arch/i386/stand/lib/biosdisk.c (revision 9aa0541bdf64142d9a27c2cf274394d60182818f)
1 /*	$NetBSD: biosdisk.c,v 1.39 2011/09/21 08:57:12 gsutre Exp $	*/
2 
3 /*
4  * Copyright (c) 1996, 1998
5  *	Matthias Drochner.  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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*
30  * raw BIOS disk device for libsa.
31  * needs lowlevel parts from bios_disk.S and biosdisk_ll.c
32  * partly from netbsd:sys/arch/i386/boot/disk.c
33  * no bad144 handling!
34  *
35  * A lot of this must match sys/kern/subr_disk_mbr.c
36  */
37 
38 /*
39  * Ported to boot 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
40  *
41  * Mach Operating System
42  * Copyright (c) 1992, 1991 Carnegie Mellon University
43  * All Rights Reserved.
44  *
45  * Permission to use, copy, modify and distribute this software and its
46  * documentation is hereby granted, provided that both the copyright
47  * notice and this permission notice appear in all copies of the
48  * software, derivative works or modified versions, and any portions
49  * thereof, and that both notices appear in supporting documentation.
50  *
51  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
52  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
53  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54  *
55  * Carnegie Mellon requests users of this software to return to
56  *
57  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
58  *  School of Computer Science
59  *  Carnegie Mellon University
60  *  Pittsburgh PA 15213-3890
61  *
62  * any improvements or extensions that they make and grant Carnegie Mellon
63  * the rights to redistribute these changes.
64  */
65 
66 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
67 #define FSTYPENAMES
68 #endif
69 
70 #include <lib/libkern/libkern.h>
71 #include <lib/libsa/stand.h>
72 
73 #include <sys/types.h>
74 #include <sys/md5.h>
75 #include <sys/param.h>
76 #include <sys/disklabel.h>
77 #include <sys/disklabel_gpt.h>
78 #include <sys/uuid.h>
79 
80 #include <fs/cd9660/iso.h>
81 
82 #include <lib/libsa/saerrno.h>
83 #include <machine/cpu.h>
84 
85 #include "libi386.h"
86 #include "biosdisk_ll.h"
87 #include "biosdisk.h"
88 #ifdef _STANDALONE
89 #include "bootinfo.h"
90 #endif
91 
92 #define BUFSIZE	2048	/* must be large enough for a CD sector */
93 
94 #define BIOSDISKNPART 26
95 
96 struct biosdisk {
97 	struct biosdisk_ll ll;
98 	daddr_t         boff;
99 	char            buf[BUFSIZE];
100 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
101 	struct {
102 		daddr_t offset;
103 		daddr_t size;
104 		int     fstype;
105 	} part[BIOSDISKNPART];
106 #endif
107 };
108 
109 #ifndef NO_GPT
110 const struct uuid GET_nbsd_raid = GPT_ENT_TYPE_NETBSD_RAIDFRAME;
111 const struct uuid GET_nbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
112 const struct uuid GET_nbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
113 const struct uuid GET_nbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
114 #endif /* NO_GPT */
115 
116 #ifdef _STANDALONE
117 static struct btinfo_bootdisk bi_disk;
118 static struct btinfo_bootwedge bi_wedge;
119 #endif
120 
121 #define	RF_PROTECTED_SECTORS	64	/* XXX refer to <.../rf_optnames.h> */
122 
123 int
124 biosdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
125 		  void *buf, size_t *rsize)
126 {
127 	struct biosdisk *d;
128 	int blks, frag;
129 
130 	if (flag != F_READ)
131 		return EROFS;
132 
133 	d = (struct biosdisk *) devdata;
134 
135 	if (d->ll.type == BIOSDISK_TYPE_CD)
136 		dblk = dblk * DEV_BSIZE / ISO_DEFAULT_BLOCK_SIZE;
137 
138 	dblk += d->boff;
139 
140 	blks = size / d->ll.secsize;
141 	if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
142 		if (rsize)
143 			*rsize = 0;
144 		return EIO;
145 	}
146 
147 	/* needed for CD */
148 	frag = size % d->ll.secsize;
149 	if (frag) {
150 		if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
151 			if (rsize)
152 				*rsize = blks * d->ll.secsize;
153 			return EIO;
154 		}
155 		memcpy(buf + blks * d->ll.secsize, d->buf, frag);
156 	}
157 
158 	if (rsize)
159 		*rsize = size;
160 	return 0;
161 }
162 
163 static struct biosdisk *
164 alloc_biosdisk(int biosdev)
165 {
166 	struct biosdisk *d;
167 
168 	d = alloc(sizeof(*d));
169 	if (d == NULL)
170 		return NULL;
171 	memset(d, 0, sizeof(*d));
172 
173 	d->ll.dev = biosdev;
174 	if (set_geometry(&d->ll, NULL)) {
175 #ifdef DISK_DEBUG
176 		printf("no geometry information\n");
177 #endif
178 		dealloc(d, sizeof(*d));
179 		return NULL;
180 	}
181 	return d;
182 }
183 
184 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
185 static void
186 md5(void *hash, const void *data, size_t len)
187 {
188 	MD5_CTX ctx;
189 
190 	MD5Init(&ctx);
191 	MD5Update(&ctx, data, len);
192 	MD5Final(hash, &ctx);
193 
194 	return;
195 }
196 #endif
197 
198 #ifndef NO_GPT
199 static bool
200 guid_is_nil(const struct uuid *u)
201 {
202 	static const struct uuid nil = { .time_low = 0 };
203 	return (memcmp(u, &nil, sizeof(*u)) == 0 ? true : false);
204 }
205 
206 static bool
207 guid_is_equal(const struct uuid *a, const struct uuid *b)
208 {
209 	return (memcmp(a, b, sizeof(*a)) == 0 ? true : false);
210 }
211 
212 static int
213 check_gpt(struct biosdisk *d, daddr_t sector)
214 {
215 	struct gpt_hdr gpth;
216 	const struct gpt_ent *ep;
217 	const struct uuid *u;
218 	daddr_t entblk;
219 	size_t size;
220 	uint32_t crc;
221 	int sectors;
222 	int entries;
223 	int entry;
224 	int i, j;
225 
226 	/* read in gpt_hdr sector */
227 	if (readsects(&d->ll, sector, 1, d->buf, 1)) {
228 #ifdef DISK_DEBUG
229 		printf("Error reading GPT header at %"PRId64"\n", sector);
230 #endif
231 		return EIO;
232 	}
233 
234 	gpth = *(const struct gpt_hdr *)d->buf;
235 
236 	if (memcmp(GPT_HDR_SIG, gpth.hdr_sig, sizeof(gpth.hdr_sig)))
237 		return -1;
238 
239 	crc = gpth.hdr_crc_self;
240 	gpth.hdr_crc_self = 0;
241 	gpth.hdr_crc_self = crc32(0, (const void *)&gpth, GPT_HDR_SIZE);
242 	if (gpth.hdr_crc_self != crc) {
243 		return -1;
244 	}
245 
246 	if (gpth.hdr_lba_self != sector)
247 		return -1;
248 
249 #ifdef _STANDALONE
250 	bi_wedge.matchblk = sector;
251 	bi_wedge.matchnblks = 1;
252 
253 	md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
254 #endif
255 
256 	sectors = sizeof(d->buf)/d->ll.secsize; /* sectors per buffer */
257 	entries = sizeof(d->buf)/gpth.hdr_entsz; /* entries per buffer */
258 	entblk = gpth.hdr_lba_table;
259 	crc = crc32(0, NULL, 0);
260 
261 	j = 0;
262 	ep = (const struct gpt_ent *)d->buf;
263 
264 	for (entry = 0; entry < gpth.hdr_entries; entry += entries) {
265 		size = MIN(sizeof(d->buf),
266 		    (gpth.hdr_entries - entry) * gpth.hdr_entsz);
267 		entries = size / gpth.hdr_entsz;
268 		sectors = roundup(size, d->ll.secsize) / d->ll.secsize;
269 		if (readsects(&d->ll, entblk, sectors, d->buf, 1))
270 			return -1;
271 		entblk += sectors;
272 		crc = crc32(crc, (const void *)d->buf, size);
273 
274 		for (i = 0; j < BIOSDISKNPART && i < entries; i++, j++) {
275 			u = (const struct uuid *)ep[i].ent_type;
276 			if (!guid_is_nil(u)) {
277 				d->part[j].offset = ep[i].ent_lba_start;
278 				d->part[j].size = ep[i].ent_lba_end -
279 				    ep[i].ent_lba_start + 1;
280 				if (guid_is_equal(u, &GET_nbsd_ffs))
281 					d->part[j].fstype = FS_BSDFFS;
282 				else if (guid_is_equal(u, &GET_nbsd_lfs))
283 					d->part[j].fstype = FS_BSDLFS;
284 				else if (guid_is_equal(u, &GET_nbsd_raid))
285 					d->part[j].fstype = FS_RAID;
286 				else if (guid_is_equal(u, &GET_nbsd_swap))
287 					d->part[j].fstype = FS_SWAP;
288 				else
289 					d->part[j].fstype = FS_OTHER;
290 			}
291 		}
292 
293 	}
294 
295 	if (crc != gpth.hdr_crc_table) {
296 #ifdef DISK_DEBUG
297 		printf("GPT table CRC invalid\n");
298 #endif
299 		return -1;
300 	}
301 
302 	return 0;
303 }
304 
305 static int
306 read_gpt(struct biosdisk *d)
307 {
308 	struct biosdisk_extinfo ed;
309 	daddr_t gptsector[2];
310 	int i, error;
311 
312 	if (d->ll.type != BIOSDISK_TYPE_HD)
313 		/* No GPT on floppy and CD */
314 		return -1;
315 
316 	gptsector[0] = GPT_HDR_BLKNO;
317 	if (set_geometry(&d->ll, &ed) == 0 && d->ll.flags & BIOSDISK_INT13EXT) {
318 		gptsector[1] = ed.totsec - 1;
319 		d->ll.secsize = ed.sbytes;
320 	} else {
321 #ifdef DISK_DEBUG
322 		printf("Unable to determine extended disk geometry - "
323 			"using CHS\n");
324 #endif
325 		/* at least try some other reasonable values then */
326 		gptsector[1] = d->ll.chs_sectors - 1;
327 	}
328 
329 	/*
330 	 * Use any valid GPT available, do not require both GPTs to be valid
331 	 */
332 	for (i = 0; i < __arraycount(gptsector); i++) {
333 		error = check_gpt(d, gptsector[i]);
334 		if (error == 0)
335 			break;
336 	}
337 
338 	if (i >= __arraycount(gptsector)) {
339 		memset(d->part, 0, sizeof(d->part));
340 		return -1;
341 	}
342 
343 #ifdef DISK_DEBUG
344 	printf("using %s GPT\n", (i == 0) ? "primary" : "secondary");
345 #endif
346 	return 0;
347 }
348 #endif	/* !NO_GPT */
349 
350 #ifndef NO_DISKLABEL
351 static void
352 ingest_label(struct biosdisk *d, struct disklabel *lp)
353 {
354 	int part;
355 
356 	memset(d->part, 0, sizeof(d->part));
357 
358 	for (part = 0; part < lp->d_npartitions; part++) {
359 		if (lp->d_partitions[part].p_size == 0)
360 			continue;
361 		if (lp->d_partitions[part].p_fstype == FS_UNUSED)
362 			continue;
363 		d->part[part].fstype = lp->d_partitions[part].p_fstype;
364 		d->part[part].offset = lp->d_partitions[part].p_offset;
365 		d->part[part].size = lp->d_partitions[part].p_size;
366 	}
367 }
368 
369 static int
370 check_label(struct biosdisk *d, daddr_t sector)
371 {
372 	struct disklabel *lp;
373 
374 	/* find partition in NetBSD disklabel */
375 	if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
376 #ifdef DISK_DEBUG
377 		printf("Error reading disklabel\n");
378 #endif
379 		return EIO;
380 	}
381 	lp = (struct disklabel *) (d->buf + LABELOFFSET);
382 	if (lp->d_magic != DISKMAGIC || dkcksum(lp)) {
383 #ifdef DISK_DEBUG
384 		printf("warning: no disklabel in sector %"PRId64"\n", sector);
385 #endif
386 		return -1;
387 	}
388 
389 	ingest_label(d, lp);
390 
391 #ifdef _STANDALONE
392 	bi_disk.labelsector = sector + LABELSECTOR;
393 	bi_disk.label.type = lp->d_type;
394 	memcpy(bi_disk.label.packname, lp->d_packname, 16);
395 	bi_disk.label.checksum = lp->d_checksum;
396 
397 	bi_wedge.matchblk = sector + LABELSECTOR;
398 	bi_wedge.matchnblks = 1;
399 
400 	md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
401 #endif
402 
403 	return 0;
404 }
405 
406 static int
407 read_label(struct biosdisk *d)
408 {
409 	struct disklabel dflt_lbl;
410 	struct mbr_partition mbr[MBR_PART_COUNT];
411 	struct partition *p;
412 	int sector, i;
413 	int error;
414 	int typ;
415 	int ext_base, this_ext, next_ext;
416 #ifdef COMPAT_386BSD_MBRPART
417 	int sector_386bsd = -1;
418 #endif
419 
420 	memset(&dflt_lbl, 0, sizeof(dflt_lbl));
421 	dflt_lbl.d_npartitions = 8;
422 
423 	d->boff = 0;
424 
425 	if (d->ll.type != BIOSDISK_TYPE_HD)
426 		/* No label on floppy and CD */
427 		return -1;
428 
429 	/*
430 	 * find NetBSD Partition in DOS partition table
431 	 * XXX check magic???
432 	 */
433 	ext_base = 0;
434 	next_ext = 0;
435 	for (;;) {
436 		this_ext = ext_base + next_ext;
437 		next_ext = 0;
438 		if (readsects(&d->ll, this_ext, 1, d->buf, 0)) {
439 #ifdef DISK_DEBUG
440 			printf("error reading MBR sector %d\n", this_ext);
441 #endif
442 			return EIO;
443 		}
444 		memcpy(&mbr, ((struct mbr_sector *)d->buf)->mbr_parts,
445 		       sizeof(mbr));
446 		/* Look for NetBSD partition ID */
447 		for (i = 0; i < MBR_PART_COUNT; i++) {
448 			typ = mbr[i].mbrp_type;
449 			if (typ == 0)
450 				continue;
451 			sector = this_ext + mbr[i].mbrp_start;
452 #ifdef DISK_DEBUG
453 			printf("ptn type %d in sector %d\n", typ, sector);
454 #endif
455 			if (typ == MBR_PTYPE_NETBSD) {
456 				error = check_label(d, sector);
457 				if (error >= 0)
458 					return error;
459 			}
460 			if (MBR_IS_EXTENDED(typ)) {
461 				next_ext = mbr[i].mbrp_start;
462 				continue;
463 			}
464 #ifdef COMPAT_386BSD_MBRPART
465 			if (this_ext == 0 && typ == MBR_PTYPE_386BSD)
466 				sector_386bsd = sector;
467 #endif
468 			if (this_ext != 0) {
469 				if (dflt_lbl.d_npartitions >= MAXPARTITIONS)
470 					continue;
471 				p = &dflt_lbl.d_partitions[dflt_lbl.d_npartitions++];
472 			} else
473 				p = &dflt_lbl.d_partitions[i];
474 			p->p_offset = sector;
475 			p->p_size = mbr[i].mbrp_size;
476 			p->p_fstype = xlat_mbr_fstype(typ);
477 		}
478 		if (next_ext == 0)
479 			break;
480 		if (ext_base == 0) {
481 			ext_base = next_ext;
482 			next_ext = 0;
483 		}
484 	}
485 
486 	sector = 0;
487 #ifdef COMPAT_386BSD_MBRPART
488 	if (sector_386bsd != -1) {
489 		printf("old BSD partition ID!\n");
490 		sector = sector_386bsd;
491 	}
492 #endif
493 
494 	/*
495 	 * One of two things:
496 	 * 	1. no MBR
497 	 *	2. no NetBSD partition in MBR
498 	 *
499 	 * We simply default to "start of disk" in this case and
500 	 * press on.
501 	 */
502 	error = check_label(d, sector);
503 	if (error >= 0)
504 		return error;
505 
506 	/*
507 	 * Nothing at start of disk, return info from mbr partitions.
508 	 */
509 	/* XXX fill it to make checksum match kernel one */
510 	dflt_lbl.d_checksum = dkcksum(&dflt_lbl);
511 	ingest_label(d, &dflt_lbl);
512 	return 0;
513 }
514 #endif /* NO_DISKLABEL */
515 
516 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
517 static int
518 read_partitions(struct biosdisk *d)
519 {
520 	int error;
521 
522 	error = -1;
523 
524 #ifndef NO_GPT
525 	error = read_gpt(d);
526 	if (error == 0)
527 		return 0;
528 
529 #endif
530 #ifndef NO_DISKLABEL
531 	error = read_label(d);
532 
533 #endif
534 	return error;
535 }
536 #endif
537 
538 void
539 biosdisk_probe(void)
540 {
541 	struct biosdisk d;
542 	struct biosdisk_extinfo ed;
543 	uint64_t size;
544 	int first;
545 	int i;
546 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
547 	int part;
548 #endif
549 
550 	for (i = 0; i < MAX_BIOSDISKS + 2; i++) {
551 		first = 1;
552 		memset(&d, 0, sizeof(d));
553 		memset(&ed, 0, sizeof(ed));
554 		if (i >= MAX_BIOSDISKS)
555 			d.ll.dev = 0x00 + i - MAX_BIOSDISKS;	/* fd */
556 		else
557 			d.ll.dev = 0x80 + i;			/* hd/cd */
558 		if (set_geometry(&d.ll, &ed))
559 			continue;
560 		printf("disk ");
561 		switch (d.ll.type) {
562 		case BIOSDISK_TYPE_CD:
563 			printf("cd0\n  cd0a\n");
564 			break;
565 		case BIOSDISK_TYPE_FD:
566 			printf("fd%d\n", d.ll.dev & 0x7f);
567 			printf("  fd%da\n", d.ll.dev & 0x7f);
568 			break;
569 		case BIOSDISK_TYPE_HD:
570 			printf("hd%d", d.ll.dev & 0x7f);
571 			if (d.ll.flags & BIOSDISK_INT13EXT) {
572 				printf(" size ");
573 				size = ed.totsec * ed.sbytes;
574 				if (size >= (10ULL * 1024 * 1024 * 1024))
575 					printf("%"PRIu64" GB",
576 					    size / (1024 * 1024 * 1024));
577 				else
578 					printf("%"PRIu64" MB",
579 					    size / (1024 * 1024));
580 			}
581 			printf("\n");
582 			break;
583 		}
584 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
585 		if (d.ll.type != BIOSDISK_TYPE_HD)
586 			continue;
587 
588 		if (read_partitions(&d) != 0)
589 			continue;
590 
591 		for (part = 0; part < BIOSDISKNPART; part++) {
592 			if (d.part[part].size == 0)
593 				continue;
594 			if (d.part[part].fstype == FS_UNUSED)
595 				continue;
596 			if (first) {
597 				printf(" ");
598 				first = 0;
599 			}
600 			printf(" hd%d%c(", d.ll.dev & 0x7f, part + 'a');
601 			if (d.part[part].fstype < FSMAXTYPES)
602 				printf("%s",
603 				  fstypenames[d.part[part].fstype]);
604 			else
605 				printf("%d", d.part[part].fstype);
606 			printf(")");
607 		}
608 #endif
609 		if (first == 0)
610 			printf("\n");
611 	}
612 }
613 
614 /* Determine likely partition for possible sector number of dos
615  * partition.
616  */
617 
618 int
619 biosdisk_findpartition(int biosdev, daddr_t sector)
620 {
621 #if defined(NO_DISKLABEL) && defined(NO_GPT)
622 	return 0;
623 #else
624 	struct biosdisk *d;
625 	int partition = 0;
626 #ifdef DISK_DEBUG
627 	printf("looking for partition device %x, sector %"PRId64"\n", biosdev, sector);
628 #endif
629 
630 	/* Look for netbsd partition that is the dos boot one */
631 	d = alloc_biosdisk(biosdev);
632 	if (d == NULL)
633 		return 0;
634 
635 	if (read_partitions(d) == 0) {
636 		for (partition = (BIOSDISKNPART-1); --partition;) {
637 			if (d->part[partition].fstype == FS_UNUSED)
638 				continue;
639 			if (d->part[partition].offset == sector)
640 				break;
641 		}
642 	}
643 
644 	dealloc(d, sizeof(*d));
645 	return partition;
646 #endif /* NO_DISKLABEL && NO_GPT */
647 }
648 
649 #ifdef _STANDALONE
650 static void
651 add_biosdisk_bootinfo(void)
652 {
653 	static bool done;
654 
655 	if (bootinfo == NULL) {
656 		done = false;
657 		return;
658 	}
659 
660 	if (done)
661 		return;
662 
663 	BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
664 	BI_ADD(&bi_wedge, BTINFO_BOOTWEDGE, sizeof(bi_wedge));
665 
666 	done = true;
667 
668 	return;
669 }
670 
671 #endif
672 
673 int
674 biosdisk_open(struct open_file *f, ...)
675 /* struct open_file *f, int biosdev, int partition */
676 {
677 	va_list ap;
678 	struct biosdisk *d;
679 	int biosdev;
680 	int partition;
681 	int error = 0;
682 
683 	va_start(ap, f);
684 	biosdev = va_arg(ap, int);
685 	d = alloc_biosdisk(biosdev);
686 	if (d == NULL) {
687 		error = ENXIO;
688 		goto out;
689 	}
690 
691 	partition = va_arg(ap, int);
692 #ifdef _STANDALONE
693 	bi_disk.biosdev = d->ll.dev;
694 	bi_disk.partition = partition;
695 	bi_disk.labelsector = -1;
696 
697 	bi_wedge.biosdev = d->ll.dev;
698 	bi_wedge.matchblk = -1;
699 #endif
700 
701 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
702 	error = read_partitions(d);
703 	if (error == -1) {
704 		error = 0;
705 		goto nolabel;
706 	}
707 	if (error)
708 		goto out;
709 
710 	if (partition >= BIOSDISKNPART ||
711 	    d->part[partition].fstype == FS_UNUSED) {
712 #ifdef DISK_DEBUG
713 		printf("illegal partition\n");
714 #endif
715 		error = EPART;
716 		goto out;
717 	}
718 
719 	d->boff = d->part[partition].offset;
720 
721 	if (d->part[partition].fstype == FS_RAID)
722 		d->boff += RF_PROTECTED_SECTORS;
723 
724 #ifdef _STANDALONE
725 	bi_wedge.startblk = d->part[partition].offset;
726 	bi_wedge.nblks = d->part[partition].size;
727 #endif
728 
729 nolabel:
730 #endif
731 #ifdef DISK_DEBUG
732 	printf("partition @%"PRId64"\n", d->boff);
733 #endif
734 
735 #ifdef _STANDALONE
736 	add_biosdisk_bootinfo();
737 #endif
738 
739 	f->f_devdata = d;
740 out:
741         va_end(ap);
742 	if (error)
743 		dealloc(d, sizeof(*d));
744 	return error;
745 }
746 
747 #ifndef LIBSA_NO_FS_CLOSE
748 int
749 biosdisk_close(struct open_file *f)
750 {
751 	struct biosdisk *d = f->f_devdata;
752 
753 	/* let the floppy drive go off */
754 	if (d->ll.type == BIOSDISK_TYPE_FD)
755 		wait_sec(3);	/* 2s is enough on all PCs I found */
756 
757 	dealloc(d, sizeof(*d));
758 	f->f_devdata = NULL;
759 	return 0;
760 }
761 #endif
762 
763 int
764 biosdisk_ioctl(struct open_file *f, u_long cmd, void *arg)
765 {
766 	return EIO;
767 }
768