xref: /minix3/sys/arch/i386/stand/lib/biosdisk.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1 /*	$NetBSD: biosdisk.c,v 1.43 2013/10/31 20:31:04 christos 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 MBR_PARTS(buf) ((char *)(buf) + offsetof(struct mbr_sector, mbr_parts))
122 
123 #define	RF_PROTECTED_SECTORS	64	/* XXX refer to <.../rf_optnames.h> */
124 
125 int
126 biosdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
127 		  void *buf, size_t *rsize)
128 {
129 	struct biosdisk *d;
130 	int blks, frag;
131 
132 	if (flag != F_READ)
133 		return EROFS;
134 
135 	d = (struct biosdisk *) devdata;
136 
137 	if (d->ll.type == BIOSDISK_TYPE_CD)
138 		dblk = dblk * DEV_BSIZE / ISO_DEFAULT_BLOCK_SIZE;
139 
140 	dblk += d->boff;
141 
142 	blks = size / d->ll.secsize;
143 	if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
144 		if (rsize)
145 			*rsize = 0;
146 		return EIO;
147 	}
148 
149 	/* needed for CD */
150 	frag = size % d->ll.secsize;
151 	if (frag) {
152 		if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
153 			if (rsize)
154 				*rsize = blks * d->ll.secsize;
155 			return EIO;
156 		}
157 		memcpy(buf + blks * d->ll.secsize, d->buf, frag);
158 	}
159 
160 	if (rsize)
161 		*rsize = size;
162 	return 0;
163 }
164 
165 static struct biosdisk *
166 alloc_biosdisk(int biosdev)
167 {
168 	struct biosdisk *d;
169 
170 	d = alloc(sizeof(*d));
171 	if (d == NULL)
172 		return NULL;
173 	memset(d, 0, sizeof(*d));
174 
175 	d->ll.dev = biosdev;
176 	if (set_geometry(&d->ll, NULL)) {
177 #ifdef DISK_DEBUG
178 		printf("no geometry information\n");
179 #endif
180 		dealloc(d, sizeof(*d));
181 		return NULL;
182 	}
183 	return d;
184 }
185 
186 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
187 static void
188 md5(void *hash, const void *data, size_t len)
189 {
190 	MD5_CTX ctx;
191 
192 	MD5Init(&ctx);
193 	MD5Update(&ctx, data, len);
194 	MD5Final(hash, &ctx);
195 
196 	return;
197 }
198 #endif
199 
200 #ifndef NO_GPT
201 static bool
202 guid_is_nil(const struct uuid *u)
203 {
204 	static const struct uuid nil = { .time_low = 0 };
205 	return (memcmp(u, &nil, sizeof(*u)) == 0 ? true : false);
206 }
207 
208 static bool
209 guid_is_equal(const struct uuid *a, const struct uuid *b)
210 {
211 	return (memcmp(a, b, sizeof(*a)) == 0 ? true : false);
212 }
213 
214 static int
215 check_gpt(struct biosdisk *d, daddr_t sector)
216 {
217 	struct gpt_hdr gpth;
218 	const struct gpt_ent *ep;
219 	const struct uuid *u;
220 	daddr_t entblk;
221 	size_t size;
222 	uint32_t crc;
223 	int sectors;
224 	int entries;
225 	int entry;
226 	int i, j;
227 
228 	/* read in gpt_hdr sector */
229 	if (readsects(&d->ll, sector, 1, d->buf, 1)) {
230 #ifdef DISK_DEBUG
231 		printf("Error reading GPT header at %"PRId64"\n", sector);
232 #endif
233 		return EIO;
234 	}
235 
236 	memcpy(&gpth, d->buf, sizeof(gpth));
237 
238 	if (memcmp(GPT_HDR_SIG, gpth.hdr_sig, sizeof(gpth.hdr_sig)))
239 		return -1;
240 
241 	crc = gpth.hdr_crc_self;
242 	gpth.hdr_crc_self = 0;
243 	gpth.hdr_crc_self = crc32(0, (const void *)&gpth, GPT_HDR_SIZE);
244 	if (gpth.hdr_crc_self != crc) {
245 		return -1;
246 	}
247 
248 	if (gpth.hdr_lba_self != sector)
249 		return -1;
250 
251 #ifdef _STANDALONE
252 	bi_wedge.matchblk = sector;
253 	bi_wedge.matchnblks = 1;
254 
255 	md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
256 #endif
257 
258 	sectors = sizeof(d->buf)/d->ll.secsize; /* sectors per buffer */
259 	entries = sizeof(d->buf)/gpth.hdr_entsz; /* entries per buffer */
260 	entblk = gpth.hdr_lba_table;
261 	crc = crc32(0, NULL, 0);
262 
263 	j = 0;
264 	ep = (const struct gpt_ent *)d->buf;
265 
266 	for (entry = 0; entry < gpth.hdr_entries; entry += entries) {
267 		size = MIN(sizeof(d->buf),
268 		    (gpth.hdr_entries - entry) * gpth.hdr_entsz);
269 		entries = size / gpth.hdr_entsz;
270 		sectors = roundup(size, d->ll.secsize) / d->ll.secsize;
271 		if (readsects(&d->ll, entblk, sectors, d->buf, 1))
272 			return -1;
273 		entblk += sectors;
274 		crc = crc32(crc, (const void *)d->buf, size);
275 
276 		for (i = 0; j < BIOSDISKNPART && i < entries; i++, j++) {
277 			u = (const struct uuid *)ep[i].ent_type;
278 			if (!guid_is_nil(u)) {
279 				d->part[j].offset = ep[i].ent_lba_start;
280 				d->part[j].size = ep[i].ent_lba_end -
281 				    ep[i].ent_lba_start + 1;
282 				if (guid_is_equal(u, &GET_nbsd_ffs))
283 					d->part[j].fstype = FS_BSDFFS;
284 				else if (guid_is_equal(u, &GET_nbsd_lfs))
285 					d->part[j].fstype = FS_BSDLFS;
286 				else if (guid_is_equal(u, &GET_nbsd_raid))
287 					d->part[j].fstype = FS_RAID;
288 				else if (guid_is_equal(u, &GET_nbsd_swap))
289 					d->part[j].fstype = FS_SWAP;
290 				else
291 					d->part[j].fstype = FS_OTHER;
292 			}
293 		}
294 
295 	}
296 
297 	if (crc != gpth.hdr_crc_table) {
298 #ifdef DISK_DEBUG
299 		printf("GPT table CRC invalid\n");
300 #endif
301 		return -1;
302 	}
303 
304 	return 0;
305 }
306 
307 static int
308 read_gpt(struct biosdisk *d)
309 {
310 	struct biosdisk_extinfo ed;
311 	daddr_t gptsector[2];
312 	int i, error;
313 
314 	if (d->ll.type != BIOSDISK_TYPE_HD)
315 		/* No GPT on floppy and CD */
316 		return -1;
317 
318 	gptsector[0] = GPT_HDR_BLKNO;
319 	if (set_geometry(&d->ll, &ed) == 0 && d->ll.flags & BIOSDISK_INT13EXT) {
320 		gptsector[1] = ed.totsec - 1;
321 		/* Sanity check values returned from BIOS */
322 		if (ed.sbytes >= 512 && (ed.sbytes & (ed.sbytes - 1)) == 0)
323 			d->ll.secsize = ed.sbytes;
324 	} else {
325 #ifdef DISK_DEBUG
326 		printf("Unable to determine extended disk geometry - "
327 			"using CHS\n");
328 #endif
329 		/* at least try some other reasonable values then */
330 		gptsector[1] = d->ll.chs_sectors - 1;
331 	}
332 
333 	/*
334 	 * Use any valid GPT available, do not require both GPTs to be valid
335 	 */
336 	for (i = 0; i < __arraycount(gptsector); i++) {
337 		error = check_gpt(d, gptsector[i]);
338 		if (error == 0)
339 			break;
340 	}
341 
342 	if (i >= __arraycount(gptsector)) {
343 		memset(d->part, 0, sizeof(d->part));
344 		return -1;
345 	}
346 
347 #ifdef DISK_DEBUG
348 	printf("using %s GPT\n", (i == 0) ? "primary" : "secondary");
349 #endif
350 	return 0;
351 }
352 #endif	/* !NO_GPT */
353 
354 #ifndef NO_DISKLABEL
355 static void
356 ingest_label(struct biosdisk *d, struct disklabel *lp)
357 {
358 	int part;
359 
360 	memset(d->part, 0, sizeof(d->part));
361 
362 	for (part = 0; part < lp->d_npartitions; part++) {
363 		if (lp->d_partitions[part].p_size == 0)
364 			continue;
365 		if (lp->d_partitions[part].p_fstype == FS_UNUSED)
366 			continue;
367 		d->part[part].fstype = lp->d_partitions[part].p_fstype;
368 		d->part[part].offset = lp->d_partitions[part].p_offset;
369 		d->part[part].size = lp->d_partitions[part].p_size;
370 	}
371 }
372 
373 static int
374 check_label(struct biosdisk *d, daddr_t sector)
375 {
376 	struct disklabel *lp;
377 
378 	/* find partition in NetBSD disklabel */
379 	if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
380 #ifdef DISK_DEBUG
381 		printf("Error reading disklabel\n");
382 #endif
383 		return EIO;
384 	}
385 	lp = (struct disklabel *) (d->buf + LABELOFFSET);
386 	if (lp->d_magic != DISKMAGIC || dkcksum(lp)) {
387 #ifdef DISK_DEBUG
388 		printf("warning: no disklabel in sector %"PRId64"\n", sector);
389 #endif
390 		return -1;
391 	}
392 
393 	ingest_label(d, lp);
394 
395 #ifdef _STANDALONE
396 	bi_disk.labelsector = sector + LABELSECTOR;
397 	bi_disk.label.type = lp->d_type;
398 	memcpy(bi_disk.label.packname, lp->d_packname, 16);
399 	bi_disk.label.checksum = lp->d_checksum;
400 
401 	bi_wedge.matchblk = sector + LABELSECTOR;
402 	bi_wedge.matchnblks = 1;
403 
404 	md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
405 #endif
406 
407 	return 0;
408 }
409 
410 static int
411 read_minix_subp(struct biosdisk *d, struct disklabel* dflt_lbl,
412 			int this_ext, daddr_t sector)
413 {
414 	struct mbr_partition mbr[MBR_PART_COUNT];
415 	int i;
416 	int typ;
417 	struct partition *p;
418 
419 	if (readsects(&d->ll, sector, 1, d->buf, 0)) {
420 #ifdef DISK_DEBUG
421 		printf("Error reading MFS sector %"PRId64"\n", sector);
422 #endif
423 		return EIO;
424 	}
425 	if ((uint8_t)d->buf[510] != 0x55 || (uint8_t)d->buf[511] != 0xAA) {
426 		return -1;
427 	}
428 	memcpy(&mbr, MBR_PARTS(d->buf), sizeof(mbr));
429 	for (i = 0; i < MBR_PART_COUNT; i++) {
430 		typ = mbr[i].mbrp_type;
431 		if (typ == 0)
432 			continue;
433 		sector = this_ext + mbr[i].mbrp_start;
434 		if (dflt_lbl->d_npartitions >= MAXPARTITIONS)
435 			continue;
436 		p = &dflt_lbl->d_partitions[dflt_lbl->d_npartitions++];
437 		p->p_offset = sector;
438 		p->p_size = mbr[i].mbrp_size;
439 		p->p_fstype = xlat_mbr_fstype(typ);
440 	}
441 	return 0;
442 }
443 
444 static int
445 read_label(struct biosdisk *d)
446 {
447 	struct disklabel dflt_lbl;
448 	struct mbr_partition mbr[MBR_PART_COUNT];
449 	struct partition *p;
450 	uint32_t sector;
451 	int i;
452 	int error;
453 	int typ;
454 	uint32_t ext_base, this_ext, next_ext;
455 #ifdef COMPAT_386BSD_MBRPART
456 	int sector_386bsd = -1;
457 #endif
458 
459 	memset(&dflt_lbl, 0, sizeof(dflt_lbl));
460 	dflt_lbl.d_npartitions = 8;
461 
462 	d->boff = 0;
463 
464 	if (d->ll.type != BIOSDISK_TYPE_HD)
465 		/* No label on floppy and CD */
466 		return -1;
467 
468 	/*
469 	 * find NetBSD Partition in DOS partition table
470 	 * XXX check magic???
471 	 */
472 	ext_base = 0;
473 	next_ext = 0;
474 	for (;;) {
475 		this_ext = ext_base + next_ext;
476 		next_ext = 0;
477 		if (readsects(&d->ll, this_ext, 1, d->buf, 0)) {
478 #ifdef DISK_DEBUG
479 			printf("error reading MBR sector %u\n", this_ext);
480 #endif
481 			return EIO;
482 		}
483 		memcpy(&mbr, MBR_PARTS(d->buf), sizeof(mbr));
484 		/* Look for NetBSD partition ID */
485 		for (i = 0; i < MBR_PART_COUNT; i++) {
486 			typ = mbr[i].mbrp_type;
487 			if (typ == 0)
488 				continue;
489 			sector = this_ext + mbr[i].mbrp_start;
490 #ifdef DISK_DEBUG
491 			printf("ptn type %d in sector %u\n", typ, sector);
492 #endif
493                         if (typ == MBR_PTYPE_MINIX_14B) {
494 				if (!read_minix_subp(d, &dflt_lbl,
495 						   this_ext, sector)) {
496 					/* Don't add "container" partition */
497 					continue;
498 				}
499 			}
500 			if (typ == MBR_PTYPE_NETBSD) {
501 				error = check_label(d, sector);
502 				if (error >= 0)
503 					return error;
504 			}
505 			if (MBR_IS_EXTENDED(typ)) {
506 				next_ext = mbr[i].mbrp_start;
507 				continue;
508 			}
509 #ifdef COMPAT_386BSD_MBRPART
510 			if (this_ext == 0 && typ == MBR_PTYPE_386BSD)
511 				sector_386bsd = sector;
512 #endif
513 			if (this_ext != 0) {
514 				if (dflt_lbl.d_npartitions >= MAXPARTITIONS)
515 					continue;
516 				p = &dflt_lbl.d_partitions[dflt_lbl.d_npartitions++];
517 			} else
518 				p = &dflt_lbl.d_partitions[i];
519 			p->p_offset = sector;
520 			p->p_size = mbr[i].mbrp_size;
521 			p->p_fstype = xlat_mbr_fstype(typ);
522 		}
523 		if (next_ext == 0)
524 			break;
525 		if (ext_base == 0) {
526 			ext_base = next_ext;
527 			next_ext = 0;
528 		}
529 	}
530 
531 	sector = 0;
532 #ifdef COMPAT_386BSD_MBRPART
533 	if (sector_386bsd != -1) {
534 		printf("old BSD partition ID!\n");
535 		sector = sector_386bsd;
536 	}
537 #endif
538 
539 	/*
540 	 * One of two things:
541 	 * 	1. no MBR
542 	 *	2. no NetBSD partition in MBR
543 	 *
544 	 * We simply default to "start of disk" in this case and
545 	 * press on.
546 	 */
547 	error = check_label(d, sector);
548 	if (error >= 0)
549 		return error;
550 
551 	/*
552 	 * Nothing at start of disk, return info from mbr partitions.
553 	 */
554 	/* XXX fill it to make checksum match kernel one */
555 	dflt_lbl.d_checksum = dkcksum(&dflt_lbl);
556 	ingest_label(d, &dflt_lbl);
557 	return 0;
558 }
559 #endif /* NO_DISKLABEL */
560 
561 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
562 static int
563 read_partitions(struct biosdisk *d)
564 {
565 	int error;
566 
567 	error = -1;
568 
569 #ifndef NO_GPT
570 	error = read_gpt(d);
571 	if (error == 0)
572 		return 0;
573 
574 #endif
575 #ifndef NO_DISKLABEL
576 	error = read_label(d);
577 
578 #endif
579 	return error;
580 }
581 #endif
582 
583 void
584 biosdisk_probe(void)
585 {
586 	struct biosdisk d;
587 	struct biosdisk_extinfo ed;
588 	uint64_t size;
589 	int first;
590 	int i;
591 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
592 	int part;
593 #endif
594 
595 	for (i = 0; i < MAX_BIOSDISKS + 2; i++) {
596 		first = 1;
597 		memset(&d, 0, sizeof(d));
598 		memset(&ed, 0, sizeof(ed));
599 		if (i >= MAX_BIOSDISKS)
600 			d.ll.dev = 0x00 + i - MAX_BIOSDISKS;	/* fd */
601 		else
602 			d.ll.dev = 0x80 + i;			/* hd/cd */
603 		if (set_geometry(&d.ll, &ed))
604 			continue;
605 		printf("disk ");
606 		switch (d.ll.type) {
607 		case BIOSDISK_TYPE_CD:
608 			printf("cd0\n  cd0a\n");
609 			break;
610 		case BIOSDISK_TYPE_FD:
611 			printf("fd%d\n", d.ll.dev & 0x7f);
612 			printf("  fd%da\n", d.ll.dev & 0x7f);
613 			break;
614 		case BIOSDISK_TYPE_HD:
615 			printf("hd%d", d.ll.dev & 0x7f);
616 			if (d.ll.flags & BIOSDISK_INT13EXT) {
617 				printf(" size ");
618 				size = ed.totsec * ed.sbytes;
619 				if (size >= (10ULL * 1024 * 1024 * 1024))
620 					printf("%"PRIu64" GB",
621 					    size / (1024 * 1024 * 1024));
622 				else
623 					printf("%"PRIu64" MB",
624 					    size / (1024 * 1024));
625 			}
626 			printf("\n");
627 			break;
628 		}
629 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
630 		if (d.ll.type != BIOSDISK_TYPE_HD)
631 			continue;
632 
633 		if (read_partitions(&d) != 0)
634 			continue;
635 
636 		for (part = 0; part < BIOSDISKNPART; part++) {
637 			if (d.part[part].size == 0)
638 				continue;
639 			if (d.part[part].fstype == FS_UNUSED)
640 				continue;
641 			if (first) {
642 				printf(" ");
643 				first = 0;
644 			}
645 			printf(" hd%d%c(", d.ll.dev & 0x7f, part + 'a');
646 			if (d.part[part].fstype < FSMAXTYPES)
647 				printf("%s",
648 				  fstypenames[d.part[part].fstype]);
649 			else
650 				printf("%d", d.part[part].fstype);
651 			printf(")");
652 		}
653 #endif
654 		if (first == 0)
655 			printf("\n");
656 	}
657 }
658 
659 /* Determine likely partition for possible sector number of dos
660  * partition.
661  */
662 
663 int
664 biosdisk_findpartition(int biosdev, daddr_t sector)
665 {
666 #if defined(NO_DISKLABEL) && defined(NO_GPT)
667 	return 0;
668 #else
669 	struct biosdisk *d;
670 	int partition = 0;
671 #ifdef DISK_DEBUG
672 	printf("looking for partition device %x, sector %"PRId64"\n", biosdev, sector);
673 #endif
674 
675 	/* Look for netbsd partition that is the dos boot one */
676 	d = alloc_biosdisk(biosdev);
677 	if (d == NULL)
678 		return 0;
679 
680 	if (read_partitions(d) == 0) {
681 		for (partition = (BIOSDISKNPART-1); --partition;) {
682 			if (d->part[partition].fstype == FS_UNUSED)
683 				continue;
684 			if (d->part[partition].offset == sector)
685 				break;
686 		}
687 	}
688 
689 	dealloc(d, sizeof(*d));
690 	return partition;
691 #endif /* NO_DISKLABEL && NO_GPT */
692 }
693 
694 #ifdef _STANDALONE
695 static void
696 add_biosdisk_bootinfo(void)
697 {
698 	static bool done;
699 
700 	if (bootinfo == NULL) {
701 		done = false;
702 		return;
703 	}
704 
705 	if (done)
706 		return;
707 
708 	BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
709 	BI_ADD(&bi_wedge, BTINFO_BOOTWEDGE, sizeof(bi_wedge));
710 
711 	done = true;
712 
713 	return;
714 }
715 
716 #endif
717 
718 int
719 biosdisk_open(struct open_file *f, ...)
720 /* struct open_file *f, int biosdev, int partition */
721 {
722 	va_list ap;
723 	struct biosdisk *d;
724 	int biosdev;
725 	int partition;
726 	int error = 0;
727 
728 	va_start(ap, f);
729 	biosdev = va_arg(ap, int);
730 	d = alloc_biosdisk(biosdev);
731 	if (d == NULL) {
732 		error = ENXIO;
733 		goto out;
734 	}
735 
736 	partition = va_arg(ap, int);
737 #ifdef _STANDALONE
738 	bi_disk.biosdev = d->ll.dev;
739 	bi_disk.partition = partition;
740 	bi_disk.labelsector = -1;
741 
742 	bi_wedge.biosdev = d->ll.dev;
743 	bi_wedge.matchblk = -1;
744 #endif
745 
746 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
747 	error = read_partitions(d);
748 	if (error == -1) {
749 		error = 0;
750 		goto nolabel;
751 	}
752 	if (error)
753 		goto out;
754 
755 	if (partition >= BIOSDISKNPART ||
756 	    d->part[partition].fstype == FS_UNUSED) {
757 #ifdef DISK_DEBUG
758 		printf("illegal partition\n");
759 #endif
760 		error = EPART;
761 		goto out;
762 	}
763 
764 	d->boff = d->part[partition].offset;
765 
766 	if (d->part[partition].fstype == FS_RAID)
767 		d->boff += RF_PROTECTED_SECTORS;
768 
769 #ifdef _STANDALONE
770 	bi_wedge.startblk = d->part[partition].offset;
771 	bi_wedge.nblks = d->part[partition].size;
772 #endif
773 
774 nolabel:
775 #endif
776 #ifdef DISK_DEBUG
777 	printf("partition @%"PRId64"\n", d->boff);
778 #endif
779 
780 #ifdef _STANDALONE
781 	add_biosdisk_bootinfo();
782 #endif
783 
784 	f->f_devdata = d;
785 out:
786         va_end(ap);
787 	if (error)
788 		dealloc(d, sizeof(*d));
789 	return error;
790 }
791 
792 #ifndef LIBSA_NO_FS_CLOSE
793 int
794 biosdisk_close(struct open_file *f)
795 {
796 	struct biosdisk *d = f->f_devdata;
797 
798 	/* let the floppy drive go off */
799 	if (d->ll.type == BIOSDISK_TYPE_FD)
800 		wait_sec(3);	/* 2s is enough on all PCs I found */
801 
802 	dealloc(d, sizeof(*d));
803 	f->f_devdata = NULL;
804 	return 0;
805 }
806 #endif
807 
808 int
809 biosdisk_ioctl(struct open_file *f, u_long cmd, void *arg)
810 {
811 	return EIO;
812 }
813