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