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