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