xref: /netbsd-src/sys/arch/i386/stand/lib/biosdisk.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*	$NetBSD: biosdisk.c,v 1.42 2012/07/03 15:24:37 tsutsui 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 		/* Sanity check values returned from BIOS */
320 		if (ed.sbytes >= 512 && (ed.sbytes & (ed.sbytes - 1)) == 0)
321 			d->ll.secsize = ed.sbytes;
322 	} else {
323 #ifdef DISK_DEBUG
324 		printf("Unable to determine extended disk geometry - "
325 			"using CHS\n");
326 #endif
327 		/* at least try some other reasonable values then */
328 		gptsector[1] = d->ll.chs_sectors - 1;
329 	}
330 
331 	/*
332 	 * Use any valid GPT available, do not require both GPTs to be valid
333 	 */
334 	for (i = 0; i < __arraycount(gptsector); i++) {
335 		error = check_gpt(d, gptsector[i]);
336 		if (error == 0)
337 			break;
338 	}
339 
340 	if (i >= __arraycount(gptsector)) {
341 		memset(d->part, 0, sizeof(d->part));
342 		return -1;
343 	}
344 
345 #ifdef DISK_DEBUG
346 	printf("using %s GPT\n", (i == 0) ? "primary" : "secondary");
347 #endif
348 	return 0;
349 }
350 #endif	/* !NO_GPT */
351 
352 #ifndef NO_DISKLABEL
353 static void
354 ingest_label(struct biosdisk *d, struct disklabel *lp)
355 {
356 	int part;
357 
358 	memset(d->part, 0, sizeof(d->part));
359 
360 	for (part = 0; part < lp->d_npartitions; part++) {
361 		if (lp->d_partitions[part].p_size == 0)
362 			continue;
363 		if (lp->d_partitions[part].p_fstype == FS_UNUSED)
364 			continue;
365 		d->part[part].fstype = lp->d_partitions[part].p_fstype;
366 		d->part[part].offset = lp->d_partitions[part].p_offset;
367 		d->part[part].size = lp->d_partitions[part].p_size;
368 	}
369 }
370 
371 static int
372 check_label(struct biosdisk *d, daddr_t sector)
373 {
374 	struct disklabel *lp;
375 
376 	/* find partition in NetBSD disklabel */
377 	if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
378 #ifdef DISK_DEBUG
379 		printf("Error reading disklabel\n");
380 #endif
381 		return EIO;
382 	}
383 	lp = (struct disklabel *) (d->buf + LABELOFFSET);
384 	if (lp->d_magic != DISKMAGIC || dkcksum(lp)) {
385 #ifdef DISK_DEBUG
386 		printf("warning: no disklabel in sector %"PRId64"\n", sector);
387 #endif
388 		return -1;
389 	}
390 
391 	ingest_label(d, lp);
392 
393 #ifdef _STANDALONE
394 	bi_disk.labelsector = sector + LABELSECTOR;
395 	bi_disk.label.type = lp->d_type;
396 	memcpy(bi_disk.label.packname, lp->d_packname, 16);
397 	bi_disk.label.checksum = lp->d_checksum;
398 
399 	bi_wedge.matchblk = sector + LABELSECTOR;
400 	bi_wedge.matchnblks = 1;
401 
402 	md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
403 #endif
404 
405 	return 0;
406 }
407 
408 static int
409 read_minix_subp(struct biosdisk *d, struct disklabel* dflt_lbl,
410 			int this_ext, daddr_t sector)
411 {
412 	struct mbr_partition mbr[MBR_PART_COUNT];
413 	int i;
414 	int typ;
415 	struct partition *p;
416 
417 	if (readsects(&d->ll, sector, 1, d->buf, 0)) {
418 #ifdef DISK_DEBUG
419 		printf("Error reading MFS sector %"PRId64"\n", sector);
420 #endif
421 		return EIO;
422 	}
423 	if ((uint8_t)d->buf[510] != 0x55 || (uint8_t)d->buf[511] != 0xAA) {
424 		return -1;
425 	}
426 	memcpy(&mbr, ((struct mbr_sector *)d->buf)->mbr_parts, sizeof(mbr));
427 	for (i = 0; i < MBR_PART_COUNT; i++) {
428 		typ = mbr[i].mbrp_type;
429 		if (typ == 0)
430 			continue;
431 		sector = this_ext + mbr[i].mbrp_start;
432 		if (dflt_lbl->d_npartitions >= MAXPARTITIONS)
433 			continue;
434 		p = &dflt_lbl->d_partitions[dflt_lbl->d_npartitions++];
435 		p->p_offset = sector;
436 		p->p_size = mbr[i].mbrp_size;
437 		p->p_fstype = xlat_mbr_fstype(typ);
438 	}
439 	return 0;
440 }
441 
442 static int
443 read_label(struct biosdisk *d)
444 {
445 	struct disklabel dflt_lbl;
446 	struct mbr_partition mbr[MBR_PART_COUNT];
447 	struct partition *p;
448 	uint32_t sector;
449 	int i;
450 	int error;
451 	int typ;
452 	uint32_t ext_base, this_ext, next_ext;
453 #ifdef COMPAT_386BSD_MBRPART
454 	int sector_386bsd = -1;
455 #endif
456 
457 	memset(&dflt_lbl, 0, sizeof(dflt_lbl));
458 	dflt_lbl.d_npartitions = 8;
459 
460 	d->boff = 0;
461 
462 	if (d->ll.type != BIOSDISK_TYPE_HD)
463 		/* No label on floppy and CD */
464 		return -1;
465 
466 	/*
467 	 * find NetBSD Partition in DOS partition table
468 	 * XXX check magic???
469 	 */
470 	ext_base = 0;
471 	next_ext = 0;
472 	for (;;) {
473 		this_ext = ext_base + next_ext;
474 		next_ext = 0;
475 		if (readsects(&d->ll, this_ext, 1, d->buf, 0)) {
476 #ifdef DISK_DEBUG
477 			printf("error reading MBR sector %u\n", this_ext);
478 #endif
479 			return EIO;
480 		}
481 		memcpy(&mbr, ((struct mbr_sector *)d->buf)->mbr_parts,
482 		       sizeof(mbr));
483 		/* Look for NetBSD partition ID */
484 		for (i = 0; i < MBR_PART_COUNT; i++) {
485 			typ = mbr[i].mbrp_type;
486 			if (typ == 0)
487 				continue;
488 			sector = this_ext + mbr[i].mbrp_start;
489 #ifdef DISK_DEBUG
490 			printf("ptn type %d in sector %u\n", typ, sector);
491 #endif
492                         if (typ == MBR_PTYPE_MINIX_14B) {
493 				if (!read_minix_subp(d, &dflt_lbl,
494 						   this_ext, sector)) {
495 					/* Don't add "container" partition */
496 					continue;
497 				}
498 			}
499 			if (typ == MBR_PTYPE_NETBSD) {
500 				error = check_label(d, sector);
501 				if (error >= 0)
502 					return error;
503 			}
504 			if (MBR_IS_EXTENDED(typ)) {
505 				next_ext = mbr[i].mbrp_start;
506 				continue;
507 			}
508 #ifdef COMPAT_386BSD_MBRPART
509 			if (this_ext == 0 && typ == MBR_PTYPE_386BSD)
510 				sector_386bsd = sector;
511 #endif
512 			if (this_ext != 0) {
513 				if (dflt_lbl.d_npartitions >= MAXPARTITIONS)
514 					continue;
515 				p = &dflt_lbl.d_partitions[dflt_lbl.d_npartitions++];
516 			} else
517 				p = &dflt_lbl.d_partitions[i];
518 			p->p_offset = sector;
519 			p->p_size = mbr[i].mbrp_size;
520 			p->p_fstype = xlat_mbr_fstype(typ);
521 		}
522 		if (next_ext == 0)
523 			break;
524 		if (ext_base == 0) {
525 			ext_base = next_ext;
526 			next_ext = 0;
527 		}
528 	}
529 
530 	sector = 0;
531 #ifdef COMPAT_386BSD_MBRPART
532 	if (sector_386bsd != -1) {
533 		printf("old BSD partition ID!\n");
534 		sector = sector_386bsd;
535 	}
536 #endif
537 
538 	/*
539 	 * One of two things:
540 	 * 	1. no MBR
541 	 *	2. no NetBSD partition in MBR
542 	 *
543 	 * We simply default to "start of disk" in this case and
544 	 * press on.
545 	 */
546 	error = check_label(d, sector);
547 	if (error >= 0)
548 		return error;
549 
550 	/*
551 	 * Nothing at start of disk, return info from mbr partitions.
552 	 */
553 	/* XXX fill it to make checksum match kernel one */
554 	dflt_lbl.d_checksum = dkcksum(&dflt_lbl);
555 	ingest_label(d, &dflt_lbl);
556 	return 0;
557 }
558 #endif /* NO_DISKLABEL */
559 
560 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
561 static int
562 read_partitions(struct biosdisk *d)
563 {
564 	int error;
565 
566 	error = -1;
567 
568 #ifndef NO_GPT
569 	error = read_gpt(d);
570 	if (error == 0)
571 		return 0;
572 
573 #endif
574 #ifndef NO_DISKLABEL
575 	error = read_label(d);
576 
577 #endif
578 	return error;
579 }
580 #endif
581 
582 void
583 biosdisk_probe(void)
584 {
585 	struct biosdisk d;
586 	struct biosdisk_extinfo ed;
587 	uint64_t size;
588 	int first;
589 	int i;
590 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
591 	int part;
592 #endif
593 
594 	for (i = 0; i < MAX_BIOSDISKS + 2; i++) {
595 		first = 1;
596 		memset(&d, 0, sizeof(d));
597 		memset(&ed, 0, sizeof(ed));
598 		if (i >= MAX_BIOSDISKS)
599 			d.ll.dev = 0x00 + i - MAX_BIOSDISKS;	/* fd */
600 		else
601 			d.ll.dev = 0x80 + i;			/* hd/cd */
602 		if (set_geometry(&d.ll, &ed))
603 			continue;
604 		printf("disk ");
605 		switch (d.ll.type) {
606 		case BIOSDISK_TYPE_CD:
607 			printf("cd0\n  cd0a\n");
608 			break;
609 		case BIOSDISK_TYPE_FD:
610 			printf("fd%d\n", d.ll.dev & 0x7f);
611 			printf("  fd%da\n", d.ll.dev & 0x7f);
612 			break;
613 		case BIOSDISK_TYPE_HD:
614 			printf("hd%d", d.ll.dev & 0x7f);
615 			if (d.ll.flags & BIOSDISK_INT13EXT) {
616 				printf(" size ");
617 				size = ed.totsec * ed.sbytes;
618 				if (size >= (10ULL * 1024 * 1024 * 1024))
619 					printf("%"PRIu64" GB",
620 					    size / (1024 * 1024 * 1024));
621 				else
622 					printf("%"PRIu64" MB",
623 					    size / (1024 * 1024));
624 			}
625 			printf("\n");
626 			break;
627 		}
628 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
629 		if (d.ll.type != BIOSDISK_TYPE_HD)
630 			continue;
631 
632 		if (read_partitions(&d) != 0)
633 			continue;
634 
635 		for (part = 0; part < BIOSDISKNPART; part++) {
636 			if (d.part[part].size == 0)
637 				continue;
638 			if (d.part[part].fstype == FS_UNUSED)
639 				continue;
640 			if (first) {
641 				printf(" ");
642 				first = 0;
643 			}
644 			printf(" hd%d%c(", d.ll.dev & 0x7f, part + 'a');
645 			if (d.part[part].fstype < FSMAXTYPES)
646 				printf("%s",
647 				  fstypenames[d.part[part].fstype]);
648 			else
649 				printf("%d", d.part[part].fstype);
650 			printf(")");
651 		}
652 #endif
653 		if (first == 0)
654 			printf("\n");
655 	}
656 }
657 
658 /* Determine likely partition for possible sector number of dos
659  * partition.
660  */
661 
662 int
663 biosdisk_findpartition(int biosdev, daddr_t sector)
664 {
665 #if defined(NO_DISKLABEL) && defined(NO_GPT)
666 	return 0;
667 #else
668 	struct biosdisk *d;
669 	int partition = 0;
670 #ifdef DISK_DEBUG
671 	printf("looking for partition device %x, sector %"PRId64"\n", biosdev, sector);
672 #endif
673 
674 	/* Look for netbsd partition that is the dos boot one */
675 	d = alloc_biosdisk(biosdev);
676 	if (d == NULL)
677 		return 0;
678 
679 	if (read_partitions(d) == 0) {
680 		for (partition = (BIOSDISKNPART-1); --partition;) {
681 			if (d->part[partition].fstype == FS_UNUSED)
682 				continue;
683 			if (d->part[partition].offset == sector)
684 				break;
685 		}
686 	}
687 
688 	dealloc(d, sizeof(*d));
689 	return partition;
690 #endif /* NO_DISKLABEL && NO_GPT */
691 }
692 
693 #ifdef _STANDALONE
694 static void
695 add_biosdisk_bootinfo(void)
696 {
697 	static bool done;
698 
699 	if (bootinfo == NULL) {
700 		done = false;
701 		return;
702 	}
703 
704 	if (done)
705 		return;
706 
707 	BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
708 	BI_ADD(&bi_wedge, BTINFO_BOOTWEDGE, sizeof(bi_wedge));
709 
710 	done = true;
711 
712 	return;
713 }
714 
715 #endif
716 
717 int
718 biosdisk_open(struct open_file *f, ...)
719 /* struct open_file *f, int biosdev, int partition */
720 {
721 	va_list ap;
722 	struct biosdisk *d;
723 	int biosdev;
724 	int partition;
725 	int error = 0;
726 
727 	va_start(ap, f);
728 	biosdev = va_arg(ap, int);
729 	d = alloc_biosdisk(biosdev);
730 	if (d == NULL) {
731 		error = ENXIO;
732 		goto out;
733 	}
734 
735 	partition = va_arg(ap, int);
736 #ifdef _STANDALONE
737 	bi_disk.biosdev = d->ll.dev;
738 	bi_disk.partition = partition;
739 	bi_disk.labelsector = -1;
740 
741 	bi_wedge.biosdev = d->ll.dev;
742 	bi_wedge.matchblk = -1;
743 #endif
744 
745 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
746 	error = read_partitions(d);
747 	if (error == -1) {
748 		error = 0;
749 		goto nolabel;
750 	}
751 	if (error)
752 		goto out;
753 
754 	if (partition >= BIOSDISKNPART ||
755 	    d->part[partition].fstype == FS_UNUSED) {
756 #ifdef DISK_DEBUG
757 		printf("illegal partition\n");
758 #endif
759 		error = EPART;
760 		goto out;
761 	}
762 
763 	d->boff = d->part[partition].offset;
764 
765 	if (d->part[partition].fstype == FS_RAID)
766 		d->boff += RF_PROTECTED_SECTORS;
767 
768 #ifdef _STANDALONE
769 	bi_wedge.startblk = d->part[partition].offset;
770 	bi_wedge.nblks = d->part[partition].size;
771 #endif
772 
773 nolabel:
774 #endif
775 #ifdef DISK_DEBUG
776 	printf("partition @%"PRId64"\n", d->boff);
777 #endif
778 
779 #ifdef _STANDALONE
780 	add_biosdisk_bootinfo();
781 #endif
782 
783 	f->f_devdata = d;
784 out:
785         va_end(ap);
786 	if (error)
787 		dealloc(d, sizeof(*d));
788 	return error;
789 }
790 
791 #ifndef LIBSA_NO_FS_CLOSE
792 int
793 biosdisk_close(struct open_file *f)
794 {
795 	struct biosdisk *d = f->f_devdata;
796 
797 	/* let the floppy drive go off */
798 	if (d->ll.type == BIOSDISK_TYPE_FD)
799 		wait_sec(3);	/* 2s is enough on all PCs I found */
800 
801 	dealloc(d, sizeof(*d));
802 	f->f_devdata = NULL;
803 	return 0;
804 }
805 #endif
806 
807 int
808 biosdisk_ioctl(struct open_file *f, u_long cmd, void *arg)
809 {
810 	return EIO;
811 }
812