biosdisk.c revision 1.58 1 /* $NetBSD: biosdisk.c,v 1.58 2022/05/03 10:09:40 jmcneill 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 #include <fs/unicode.h>
82
83 #include <lib/libsa/saerrno.h>
84 #include <machine/cpu.h>
85
86 #include "libi386.h"
87 #include "biosdisk_ll.h"
88 #include "biosdisk.h"
89 #ifdef _STANDALONE
90 #include "bootinfo.h"
91 #endif
92
93 #ifndef NO_GPT
94 #define MAXDEVNAME 39 /* "NAME=" + 34 char part_name */
95 #else
96 #define MAXDEVNAME 16
97 #endif
98
99 #ifndef BIOSDISK_BUFSIZE
100 #define BIOSDISK_BUFSIZE 2048 /* must be large enough for a CD sector */
101 #endif
102
103 #define BIOSDISKNPART 26
104
105 struct biosdisk {
106 struct biosdisk_ll ll;
107 daddr_t boff;
108 char buf[BIOSDISK_BUFSIZE];
109 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
110 struct biosdisk_partition part[BIOSDISKNPART];
111 #endif
112 };
113
114 #include <dev/raidframe/raidframevar.h>
115 #define RF_COMPONENT_INFO_OFFSET 16384 /* from sys/dev/raidframe/rf_netbsdkintf.c */
116 #define RF_COMPONENT_LABEL_VERSION 2 /* from <dev/raidframe/rf_raid.h> */
117
118 #define RAIDFRAME_NDEV 16 /* abitrary limit to 15 raidframe devices */
119 struct raidframe {
120 int last_unit;
121 int serial;
122 int biosdev;
123 int parent_part;
124 #ifndef NO_GPT
125 char parent_name[MAXDEVNAME + 1];
126 #endif
127 daddr_t offset;
128 daddr_t size;
129 };
130
131
132 #ifndef NO_GPT
133 const struct uuid GET_nbsd_raid = GPT_ENT_TYPE_NETBSD_RAIDFRAME;
134 const struct uuid GET_nbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
135 const struct uuid GET_nbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
136 const struct uuid GET_nbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
137 const struct uuid GET_nbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD;
138 const struct uuid GET_nbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD;
139
140 const struct uuid GET_efi = GPT_ENT_TYPE_EFI;
141 const struct uuid GET_mbr = GPT_ENT_TYPE_MBR;
142 const struct uuid GET_fbsd = GPT_ENT_TYPE_FREEBSD;
143 const struct uuid GET_fbsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
144 const struct uuid GET_fbsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
145 const struct uuid GET_fbsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
146 const struct uuid GET_fbsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
147 const struct uuid GET_ms_rsvd = GPT_ENT_TYPE_MS_RESERVED;
148 const struct uuid GET_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
149 const struct uuid GET_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
150 const struct uuid GET_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
151 const struct uuid GET_linux_data = GPT_ENT_TYPE_LINUX_DATA;
152 const struct uuid GET_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
153 const struct uuid GET_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
154 const struct uuid GET_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
155 const struct uuid GET_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
156 const struct uuid GET_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
157 const struct uuid GET_bios = GPT_ENT_TYPE_BIOS;
158
159 const struct gpt_part gpt_parts[] = {
160 { &GET_nbsd_raid, "NetBSD RAID" },
161 { &GET_nbsd_ffs, "NetBSD FFS" },
162 { &GET_nbsd_lfs, "NetBSD LFS" },
163 { &GET_nbsd_swap, "NetBSD Swap" },
164 { &GET_nbsd_ccd, "NetBSD ccd" },
165 { &GET_nbsd_cgd, "NetBSD cgd" },
166 { &GET_efi, "EFI System" },
167 { &GET_mbr, "MBR" },
168 { &GET_fbsd, "FreeBSD" },
169 { &GET_fbsd_swap, "FreeBSD Swap" },
170 { &GET_fbsd_ufs, "FreeBSD UFS" },
171 { &GET_fbsd_vinum, "FreeBSD Vinum" },
172 { &GET_fbsd_zfs, "FreeBSD ZFS" },
173 { &GET_ms_rsvd, "Microsoft Reserved" },
174 { &GET_ms_basic_data, "Microsoft Basic data" },
175 { &GET_ms_ldm_metadata, "Microsoft LDM metadata" },
176 { &GET_ms_ldm_data, "Microsoft LDM data" },
177 { &GET_linux_data, "Linux data" },
178 { &GET_linux_raid, "Linux RAID" },
179 { &GET_linux_swap, "Linux Swap" },
180 { &GET_linux_lvm, "Linux LVM" },
181 { &GET_apple_hfs, "Apple HFS" },
182 { &GET_apple_ufs, "Apple UFS" },
183 { &GET_bios, "BIOS Boot (GRUB)" },
184 };
185 #endif /* NO_GPT */
186
187 struct btinfo_bootdisk bi_disk;
188 struct btinfo_bootwedge bi_wedge;
189 struct btinfo_rootdevice bi_root;
190
191 #define MBR_PARTS(buf) ((char *)(buf) + offsetof(struct mbr_sector, mbr_parts))
192
193 #ifndef devb2cdb
194 #define devb2cdb(bno) (((bno) * DEV_BSIZE) / ISO_DEFAULT_BLOCK_SIZE)
195 #endif
196
197 static void
198 dealloc_biosdisk(struct biosdisk *d)
199 {
200 #ifndef NO_GPT
201 int i;
202
203 for (i = 0; i < __arraycount(d->part); i++) {
204 if (d->part[i].part_name != NULL)
205 dealloc(d->part[i].part_name, BIOSDISK_PART_NAME_LEN);
206 }
207 #endif
208
209 dealloc(d, sizeof(*d));
210
211 return;
212 }
213
214 static struct biosdisk_partition *
215 copy_biosdisk_part(struct biosdisk *d)
216 {
217 struct biosdisk_partition *part;
218
219 part = alloc(sizeof(d->part));
220 if (part == NULL)
221 goto out;
222
223 memcpy(part, d->part, sizeof(d->part));
224
225 #ifndef NO_GPT
226 int i;
227
228 for (i = 0; i < __arraycount(d->part); i++) {
229 if (d->part[i].part_name != NULL) {
230 part[i].part_name = alloc(BIOSDISK_PART_NAME_LEN);
231 memcpy(part[i].part_name, d->part[i].part_name,
232 BIOSDISK_PART_NAME_LEN);
233 }
234 }
235 #endif
236
237 out:
238 return part;
239 }
240
241 int
242 biosdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
243 void *buf, size_t *rsize)
244 {
245 struct biosdisk *d;
246 int blks, frag;
247
248 if (flag != F_READ)
249 return EROFS;
250
251 d = (struct biosdisk *) devdata;
252
253 if (d->ll.type == BIOSDISK_TYPE_CD)
254 dblk = devb2cdb(dblk);
255
256 dblk += d->boff;
257
258 blks = size / d->ll.secsize;
259 if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
260 if (rsize)
261 *rsize = 0;
262 return EIO;
263 }
264
265 /* needed for CD */
266 frag = size % d->ll.secsize;
267 if (frag) {
268 if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
269 if (rsize)
270 *rsize = blks * d->ll.secsize;
271 return EIO;
272 }
273 memcpy(buf + blks * d->ll.secsize, d->buf, frag);
274 }
275
276 if (rsize)
277 *rsize = size;
278 return 0;
279 }
280
281 static struct biosdisk *
282 alloc_biosdisk(int biosdev)
283 {
284 struct biosdisk *d;
285
286 d = alloc(sizeof(*d));
287 if (d == NULL)
288 return NULL;
289 memset(d, 0, sizeof(*d));
290
291 d->ll.dev = biosdev;
292 if (set_geometry(&d->ll, NULL)) {
293 #ifdef DISK_DEBUG
294 printf("no geometry information\n");
295 #endif
296 dealloc_biosdisk(d);
297 return NULL;
298 }
299 return d;
300 }
301
302 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
303 static void
304 md5(void *hash, const void *data, size_t len)
305 {
306 MD5_CTX ctx;
307
308 MD5Init(&ctx);
309 MD5Update(&ctx, data, len);
310 MD5Final(hash, &ctx);
311
312 return;
313 }
314 #endif
315
316 #ifndef NO_GPT
317 bool
318 guid_is_nil(const struct uuid *u)
319 {
320 static const struct uuid nil = { .time_low = 0 };
321 return (memcmp(u, &nil, sizeof(*u)) == 0 ? true : false);
322 }
323
324 bool
325 guid_is_equal(const struct uuid *a, const struct uuid *b)
326 {
327 return (memcmp(a, b, sizeof(*a)) == 0 ? true : false);
328 }
329
330 #ifndef NO_GPT
331 static void
332 part_name_utf8(const uint16_t *utf16_src, size_t utf16_srclen,
333 char *utf8_dst, size_t utf8_dstlen)
334 {
335 char *c = utf8_dst;
336 size_t r = utf8_dstlen - 1;
337 size_t n;
338 int j;
339
340 if (utf8_dst == NULL)
341 return;
342
343 for (j = 0; j < utf16_srclen && utf16_src[j] != 0x0000; j++) {
344 n = wput_utf8(c, r, le16toh(utf16_src[j]));
345 if (n == 0)
346 break;
347 c += n; r -= n;
348 }
349 *c = '\0';
350
351 return;
352 }
353 #endif
354
355 static int
356 check_gpt(struct biosdisk *d, daddr_t rf_offset, daddr_t sector)
357 {
358 struct gpt_hdr gpth;
359 const struct gpt_ent *ep;
360 const struct uuid *u;
361 daddr_t entblk;
362 size_t size;
363 uint32_t crc;
364 int sectors;
365 int entries;
366 int entry;
367 int i, j;
368
369 /* read in gpt_hdr sector */
370 if (readsects(&d->ll, sector, 1, d->buf, 1)) {
371 #ifdef DISK_DEBUG
372 printf("Error reading GPT header at %"PRId64"\n", sector);
373 #endif
374 return EIO;
375 }
376
377 memcpy(&gpth, d->buf, sizeof(gpth));
378
379 if (memcmp(GPT_HDR_SIG, gpth.hdr_sig, sizeof(gpth.hdr_sig)))
380 return -1;
381
382 crc = gpth.hdr_crc_self;
383 gpth.hdr_crc_self = 0;
384 gpth.hdr_crc_self = crc32(0, (const void *)&gpth, GPT_HDR_SIZE);
385 if (gpth.hdr_crc_self != crc) {
386 return -1;
387 }
388
389 if (gpth.hdr_lba_self + rf_offset != sector)
390 return -1;
391
392 #ifdef _STANDALONE
393 bi_wedge.matchblk = sector;
394 bi_wedge.matchnblks = 1;
395
396 md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
397 #endif
398
399 sectors = sizeof(d->buf)/d->ll.secsize; /* sectors per buffer */
400 entries = sizeof(d->buf)/gpth.hdr_entsz; /* entries per buffer */
401 entblk = gpth.hdr_lba_table + rf_offset;
402 crc = crc32(0, NULL, 0);
403
404 j = 0;
405 ep = (const struct gpt_ent *)d->buf;
406
407 for (entry = 0; entry < gpth.hdr_entries; entry += entries) {
408 size = MIN(sizeof(d->buf),
409 (gpth.hdr_entries - entry) * gpth.hdr_entsz);
410 entries = size / gpth.hdr_entsz;
411 sectors = roundup(size, d->ll.secsize) / d->ll.secsize;
412 if (readsects(&d->ll, entblk, sectors, d->buf, 1))
413 return -1;
414 entblk += sectors;
415 crc = crc32(crc, (const void *)d->buf, size);
416
417 for (i = 0; j < BIOSDISKNPART && i < entries; i++) {
418 u = (const struct uuid *)ep[i].ent_type;
419 if (!guid_is_nil(u)) {
420 d->part[j].offset = ep[i].ent_lba_start;
421 d->part[j].size = ep[i].ent_lba_end -
422 ep[i].ent_lba_start + 1;
423 if (guid_is_equal(u, &GET_nbsd_ffs))
424 d->part[j].fstype = FS_BSDFFS;
425 else if (guid_is_equal(u, &GET_nbsd_lfs))
426 d->part[j].fstype = FS_BSDLFS;
427 else if (guid_is_equal(u, &GET_nbsd_raid))
428 d->part[j].fstype = FS_RAID;
429 else if (guid_is_equal(u, &GET_nbsd_swap))
430 d->part[j].fstype = FS_SWAP;
431 else if (guid_is_equal(u, &GET_nbsd_ccd))
432 d->part[j].fstype = FS_CCD;
433 else if (guid_is_equal(u, &GET_nbsd_cgd))
434 d->part[j].fstype = FS_CGD;
435 else
436 d->part[j].fstype = FS_OTHER;
437 #ifndef NO_GPT
438 for (int k = 0;
439 k < __arraycount(gpt_parts);
440 k++) {
441 if (guid_is_equal(u, gpt_parts[k].guid))
442 d->part[j].guid = &gpt_parts[k];
443 }
444 d->part[j].attr = ep[i].ent_attr;
445
446 d->part[j].part_name =
447 alloc(BIOSDISK_PART_NAME_LEN);
448 part_name_utf8(ep[i].ent_name,
449 sizeof(ep[i].ent_name),
450 d->part[j].part_name,
451 BIOSDISK_PART_NAME_LEN);
452 #endif
453 j++;
454 }
455 }
456
457 }
458
459 if (crc != gpth.hdr_crc_table) {
460 #ifdef DISK_DEBUG
461 printf("GPT table CRC invalid\n");
462 #endif
463 return -1;
464 }
465
466 return 0;
467 }
468
469 static int
470 read_gpt(struct biosdisk *d, daddr_t rf_offset, daddr_t rf_size)
471 {
472 struct biosdisk_extinfo ed;
473 daddr_t gptsector[2];
474 int i, error;
475
476 if (d->ll.type != BIOSDISK_TYPE_HD)
477 /* No GPT on floppy and CD */
478 return -1;
479
480 if (rf_offset && rf_size) {
481 gptsector[0] = rf_offset + GPT_HDR_BLKNO;
482 gptsector[1] = rf_offset + rf_size - 1;
483 } else {
484 gptsector[0] = GPT_HDR_BLKNO;
485 if (set_geometry(&d->ll, &ed) == 0 &&
486 d->ll.flags & BIOSDISK_INT13EXT) {
487 gptsector[1] = ed.totsec - 1;
488 /* Sanity check values returned from BIOS */
489 if (ed.sbytes >= 512 &&
490 (ed.sbytes & (ed.sbytes - 1)) == 0)
491 d->ll.secsize = ed.sbytes;
492 } else {
493 #ifdef DISK_DEBUG
494 printf("Unable to determine extended disk geometry - "
495 "using CHS\n");
496 #endif
497 /* at least try some other reasonable values then */
498 gptsector[1] = d->ll.chs_sectors - 1;
499 }
500 }
501
502 for (i = 0; i < __arraycount(gptsector); i++) {
503 error = check_gpt(d, rf_offset, gptsector[i]);
504 if (error == 0)
505 break;
506 }
507
508 if (i >= __arraycount(gptsector)) {
509 memset(d->part, 0, sizeof(d->part));
510 return -1;
511 }
512
513 #ifndef USE_SECONDARY_GPT
514 if (i > 0) {
515 #ifdef DISK_DEBUG
516 printf("ignoring valid secondary GPT\n");
517 #endif
518 return -1;
519 }
520 #endif
521
522 #ifdef DISK_DEBUG
523 printf("using %s GPT\n", (i == 0) ? "primary" : "secondary");
524 #endif
525 return 0;
526 }
527 #endif /* !NO_GPT */
528
529 #ifndef NO_DISKLABEL
530 static void
531 ingest_label(struct biosdisk *d, struct disklabel *lp)
532 {
533 int part;
534
535 memset(d->part, 0, sizeof(d->part));
536
537 for (part = 0; part < lp->d_npartitions; part++) {
538 if (lp->d_partitions[part].p_size == 0)
539 continue;
540 if (lp->d_partitions[part].p_fstype == FS_UNUSED)
541 continue;
542 d->part[part].fstype = lp->d_partitions[part].p_fstype;
543 d->part[part].offset = lp->d_partitions[part].p_offset;
544 d->part[part].size = lp->d_partitions[part].p_size;
545 }
546 }
547
548 static int
549 check_label(struct biosdisk *d, daddr_t sector)
550 {
551 struct disklabel *lp;
552
553 /* find partition in NetBSD disklabel */
554 if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
555 #ifdef DISK_DEBUG
556 printf("Error reading disklabel\n");
557 #endif
558 return EIO;
559 }
560 lp = (struct disklabel *) (d->buf + LABELOFFSET);
561 if (lp->d_magic != DISKMAGIC || dkcksum(lp)) {
562 #ifdef DISK_DEBUG
563 printf("warning: no disklabel in sector %"PRId64"\n", sector);
564 #endif
565 return -1;
566 }
567
568 ingest_label(d, lp);
569
570 bi_disk.labelsector = sector + LABELSECTOR;
571 bi_disk.label.type = lp->d_type;
572 memcpy(bi_disk.label.packname, lp->d_packname, 16);
573 bi_disk.label.checksum = lp->d_checksum;
574
575 bi_wedge.matchblk = sector + LABELSECTOR;
576 bi_wedge.matchnblks = 1;
577
578 md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
579
580 return 0;
581 }
582
583 static int
584 read_minix_subp(struct biosdisk *d, struct disklabel* dflt_lbl,
585 int this_ext, daddr_t sector)
586 {
587 struct mbr_partition mbr[MBR_PART_COUNT];
588 int i;
589 int typ;
590 struct partition *p;
591
592 if (readsects(&d->ll, sector, 1, d->buf, 0)) {
593 #ifdef DISK_DEBUG
594 printf("Error reading MFS sector %"PRId64"\n", sector);
595 #endif
596 return EIO;
597 }
598 if ((uint8_t)d->buf[510] != 0x55 || (uint8_t)d->buf[511] != 0xAA) {
599 return -1;
600 }
601 memcpy(&mbr, MBR_PARTS(d->buf), sizeof(mbr));
602 for (i = 0; i < MBR_PART_COUNT; i++) {
603 typ = mbr[i].mbrp_type;
604 if (typ == 0)
605 continue;
606 sector = this_ext + mbr[i].mbrp_start;
607 if (dflt_lbl->d_npartitions >= MAXPARTITIONS)
608 continue;
609 p = &dflt_lbl->d_partitions[dflt_lbl->d_npartitions++];
610 p->p_offset = sector;
611 p->p_size = mbr[i].mbrp_size;
612 p->p_fstype = xlat_mbr_fstype(typ);
613 }
614 return 0;
615 }
616
617 #if defined(EFIBOOT) && defined(SUPPORT_CD9660)
618 static int
619 check_cd9660(struct biosdisk *d)
620 {
621 struct biosdisk_extinfo ed;
622 struct iso_primary_descriptor *vd;
623 daddr_t bno;
624
625 for (bno = 16;; bno++) {
626 if (readsects(&d->ll, bno, 1, d->buf, 0))
627 return -1;
628 vd = (struct iso_primary_descriptor *)d->buf;
629 if (memcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
630 return -1;
631 if (isonum_711(vd->type) == ISO_VD_END)
632 return -1;
633 if (isonum_711(vd->type) == ISO_VD_PRIMARY)
634 break;
635 }
636 if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
637 return -1;
638
639 if (set_geometry(&d->ll, &ed))
640 return -1;
641
642 memset(d->part, 0, sizeof(d->part));
643 d->part[0].fstype = FS_ISO9660;
644 d->part[0].offset = 0;
645 d->part[0].size = ed.totsec;
646 return 0;
647 }
648 #endif
649
650 static int
651 read_label(struct biosdisk *d, daddr_t offset)
652 {
653 struct disklabel dflt_lbl;
654 struct mbr_partition mbr[MBR_PART_COUNT];
655 struct partition *p;
656 uint32_t sector;
657 int i;
658 int error;
659 int typ;
660 uint32_t ext_base, this_ext, next_ext;
661 #ifdef COMPAT_386BSD_MBRPART
662 int sector_386bsd = -1;
663 #endif
664
665 memset(&dflt_lbl, 0, sizeof(dflt_lbl));
666 dflt_lbl.d_npartitions = 8;
667
668 d->boff = 0;
669
670 if (d->ll.type != BIOSDISK_TYPE_HD)
671 /* No label on floppy and CD */
672 return -1;
673
674 /*
675 * find NetBSD Partition in DOS partition table
676 * XXX check magic???
677 */
678 ext_base = offset;
679 next_ext = offset;
680 for (;;) {
681 this_ext = ext_base + next_ext;
682 next_ext = offset;
683 if (readsects(&d->ll, this_ext, 1, d->buf, 0)) {
684 #ifdef DISK_DEBUG
685 printf("error reading MBR sector %u\n", this_ext);
686 #endif
687 return EIO;
688 }
689 memcpy(&mbr, MBR_PARTS(d->buf), sizeof(mbr));
690 /* Look for NetBSD partition ID */
691 for (i = 0; i < MBR_PART_COUNT; i++) {
692 typ = mbr[i].mbrp_type;
693 if (typ == 0)
694 continue;
695 sector = this_ext + mbr[i].mbrp_start;
696 #ifdef DISK_DEBUG
697 printf("ptn type %d in sector %u\n", typ, sector);
698 #endif
699 if (typ == MBR_PTYPE_MINIX_14B) {
700 if (!read_minix_subp(d, &dflt_lbl,
701 this_ext, sector)) {
702 /* Don't add "container" partition */
703 continue;
704 }
705 }
706 if (typ == MBR_PTYPE_NETBSD) {
707 error = check_label(d, sector);
708 if (error >= 0)
709 return error;
710 }
711 if (MBR_IS_EXTENDED(typ)) {
712 next_ext = mbr[i].mbrp_start + offset;
713 continue;
714 }
715 #ifdef COMPAT_386BSD_MBRPART
716 if (this_ext == offset && typ == MBR_PTYPE_386BSD)
717 sector_386bsd = sector;
718 #endif
719 if (this_ext != offset) {
720 if (dflt_lbl.d_npartitions >= MAXPARTITIONS)
721 continue;
722 p = &dflt_lbl.d_partitions[dflt_lbl.d_npartitions++];
723 } else
724 p = &dflt_lbl.d_partitions[i];
725 p->p_offset = sector;
726 p->p_size = mbr[i].mbrp_size;
727 p->p_fstype = xlat_mbr_fstype(typ);
728 }
729 if (next_ext == offset)
730 break;
731 if (ext_base == offset) {
732 ext_base = next_ext;
733 next_ext = offset;
734 }
735 }
736
737 sector = offset;
738 #ifdef COMPAT_386BSD_MBRPART
739 if (sector_386bsd != -1) {
740 printf("old BSD partition ID!\n");
741 sector = sector_386bsd;
742 }
743 #endif
744
745 /*
746 * One of two things:
747 * 1. no MBR
748 * 2. no NetBSD partition in MBR
749 *
750 * We simply default to "start of disk" in this case and
751 * press on.
752 */
753 error = check_label(d, sector);
754 if (error >= 0)
755 return error;
756
757 #if defined(EFIBOOT) && defined(SUPPORT_CD9660)
758 /* Check CD/DVD */
759 error = check_cd9660(d);
760 if (error >= 0)
761 return error;
762 #endif
763
764 /*
765 * Nothing at start of disk, return info from mbr partitions.
766 */
767 /* XXX fill it to make checksum match kernel one */
768 dflt_lbl.d_checksum = dkcksum(&dflt_lbl);
769 ingest_label(d, &dflt_lbl);
770 return 0;
771 }
772 #endif /* NO_DISKLABEL */
773
774 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
775 static int
776 read_partitions(struct biosdisk *d, daddr_t offset, daddr_t size)
777 {
778 int error;
779
780 error = -1;
781
782 #ifndef NO_GPT
783 error = read_gpt(d, offset, size);
784 if (error == 0)
785 return 0;
786
787 #endif
788 #ifndef NO_DISKLABEL
789 error = read_label(d, offset);
790 #endif
791 return error;
792 }
793 #endif
794
795 #ifndef NO_RAIDFRAME
796 static void
797 raidframe_probe(struct raidframe *raidframe, int *raidframe_count,
798 struct biosdisk *d, int part)
799 {
800 int i = *raidframe_count;
801 struct RF_ComponentLabel_s label;
802 daddr_t offset;
803
804 if (i + 1 > RAIDFRAME_NDEV)
805 return;
806
807 offset = d->part[part].offset;
808 if ((biosdisk_read_raidframe(d->ll.dev, offset, &label)) != 0)
809 return;
810
811 if (label.version != RF_COMPONENT_LABEL_VERSION)
812 printf("Unexpected raidframe label version\n");
813
814 raidframe[i].last_unit = label.last_unit;
815 raidframe[i].serial = label.serial_number;
816 raidframe[i].biosdev = d->ll.dev;
817 raidframe[i].parent_part = part;
818 #ifndef NO_GPT
819 if (d->part[part].part_name)
820 strlcpy(raidframe[i].parent_name,
821 d->part[part].part_name, MAXDEVNAME);
822 else
823 raidframe[i].parent_name[0] = '\0';
824 #endif
825 raidframe[i].offset = offset;
826 raidframe[i].size = label.__numBlocks;
827
828 (*raidframe_count)++;
829
830 return;
831 }
832 #endif
833
834 void
835 biosdisk_probe(void)
836 {
837 struct biosdisk *d;
838 struct biosdisk_extinfo ed;
839 #ifndef NO_RAIDFRAME
840 struct raidframe raidframe[RAIDFRAME_NDEV];
841 int raidframe_count = 0;
842 #endif
843 uint64_t size;
844 int first;
845 int i;
846 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
847 int part;
848 #endif
849
850 for (i = 0; i < MAX_BIOSDISKS + 2; i++) {
851 first = 1;
852 d = alloc(sizeof(*d));
853 if (d == NULL) {
854 printf("Out of memory\n");
855 return;
856 }
857 memset(d, 0, sizeof(*d));
858 memset(&ed, 0, sizeof(ed));
859 if (i >= MAX_BIOSDISKS)
860 d->ll.dev = 0x00 + i - MAX_BIOSDISKS; /* fd */
861 else
862 d->ll.dev = 0x80 + i; /* hd/cd */
863 if (set_geometry(&d->ll, &ed))
864 goto next_disk;
865 printf("disk ");
866 switch (d->ll.type) {
867 case BIOSDISK_TYPE_CD:
868 printf("cd0\n cd0a\n");
869 break;
870 case BIOSDISK_TYPE_FD:
871 printf("fd%d\n", d->ll.dev & 0x7f);
872 printf(" fd%da\n", d->ll.dev & 0x7f);
873 break;
874 case BIOSDISK_TYPE_HD:
875 printf("hd%d", d->ll.dev & 0x7f);
876 if (d->ll.flags & BIOSDISK_INT13EXT) {
877 printf(" size ");
878 size = ed.totsec * ed.sbytes;
879 if (size >= (10ULL * 1024 * 1024 * 1024))
880 printf("%"PRIu64" GB",
881 size / (1024 * 1024 * 1024));
882 else
883 printf("%"PRIu64" MB",
884 size / (1024 * 1024));
885 }
886 printf("\n");
887 break;
888 }
889 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
890 if (d->ll.type != BIOSDISK_TYPE_HD)
891 goto next_disk;
892
893 if (read_partitions(d, 0, 0) != 0)
894 goto next_disk;
895
896 for (part = 0; part < BIOSDISKNPART; part++) {
897 if (d->part[part].size == 0)
898 continue;
899 if (d->part[part].fstype == FS_UNUSED)
900 continue;
901 #ifndef NO_RAIDFRAME
902 if (d->part[part].fstype == FS_RAID)
903 raidframe_probe(raidframe,
904 &raidframe_count, d, part);
905 #endif
906 if (first) {
907 printf(" ");
908 first = 0;
909 }
910 #ifndef NO_GPT
911 if (d->part[part].part_name &&
912 d->part[part].part_name[0])
913 printf(" NAME=%s(", d->part[part].part_name);
914 else
915 #endif
916 printf(" hd%d%c(", d->ll.dev & 0x7f, part + 'a');
917
918 #ifndef NO_GPT
919 if (d->part[part].guid != NULL)
920 printf("%s", d->part[part].guid->name);
921 else
922 #endif
923
924 if (d->part[part].fstype < FSMAXTYPES)
925 printf("%s",
926 fstypenames[d->part[part].fstype]);
927 else
928 printf("%d", d->part[part].fstype);
929 printf(")");
930 }
931 #endif
932 if (first == 0)
933 printf("\n");
934
935 next_disk:
936 dealloc_biosdisk(d);
937 }
938
939 #ifndef NO_RAIDFRAME
940 for (i = 0; i < raidframe_count; i++) {
941 size_t secsize;
942
943 if ((d = alloc_biosdisk(raidframe[i].biosdev)) == NULL) {
944 printf("Out of memory\n");
945 return;
946 }
947
948 secsize = d->ll.secsize;
949
950 printf("raidframe raid%d serial %d in ",
951 raidframe[i].last_unit, raidframe[i].serial);
952 #ifndef NO_GPT
953 if (raidframe[i].parent_name[0])
954 printf("NAME=%s size ", raidframe[i].parent_name);
955 else
956 #endif
957 printf("hd%d%c size ", d->ll.dev & 0x7f,
958 raidframe[i].parent_part + 'a');
959 if (raidframe[i].size >= (10ULL * 1024 * 1024 * 1024 / secsize))
960 printf("%"PRIu64" GB",
961 raidframe[i].size / (1024 * 1024 * 1024 / secsize));
962 else
963 printf("%"PRIu64" MB",
964 raidframe[i].size / (1024 * 1024 / secsize));
965 printf("\n");
966
967 if (read_partitions(d,
968 raidframe[i].offset + RF_PROTECTED_SECTORS,
969 raidframe[i].size) != 0)
970 goto next_raidrame;
971
972 first = 1;
973 for (part = 0; part < BIOSDISKNPART; part++) {
974 #ifndef NO_GPT
975 bool bootme = d->part[part].attr & GPT_ENT_ATTR_BOOTME;
976 #else
977 bool bootme = 0;
978 #endif
979
980 if (d->part[part].size == 0)
981 continue;
982 if (d->part[part].fstype == FS_UNUSED)
983 continue;
984 if (d->part[part].fstype == FS_RAID)
985 continue;
986 if (first) {
987 printf(" ");
988 first = 0;
989 }
990 #ifndef NO_GPT
991 if (d->part[part].part_name &&
992 d->part[part].part_name[0])
993 printf(" NAME=%s(", d->part[part].part_name);
994 else
995 #endif
996 printf(" raid%d%c(", raidframe[i].last_unit,
997 part + 'a');
998 #ifndef NO_GPT
999 if (d->part[part].guid != NULL)
1000 printf("%s", d->part[part].guid->name);
1001 else
1002 #endif
1003 if (d->part[part].fstype < FSMAXTYPES)
1004 printf("%s",
1005 fstypenames[d->part[part].fstype]);
1006 else
1007 printf("%d", d->part[part].fstype);
1008 printf("%s)", bootme ? ", bootme" : "");
1009 }
1010
1011 next_raidrame:
1012 if (first == 0)
1013 printf("\n");
1014
1015 dealloc_biosdisk(d);
1016 }
1017 #endif
1018 }
1019
1020 /* Determine likely partition for possible sector number of dos
1021 * partition.
1022 */
1023
1024 int
1025 biosdisk_findpartition(int biosdev, daddr_t sector,
1026 int *partition, const char **part_name)
1027 {
1028 #if defined(NO_DISKLABEL) && defined(NO_GPT)
1029 *partition = 0;
1030 if (part_name)
1031 *part_name = NULL;
1032 return 0;
1033 #else
1034 int i;
1035 struct biosdisk *d;
1036 int biosboot_sector_part = -1;
1037 int bootable_fs_part = -1;
1038 int boot_part = 0;
1039 #ifndef NO_GPT
1040 int gpt_bootme_part = -1;
1041 static char namebuf[MAXDEVNAME + 1];
1042 #endif
1043
1044 #ifdef DISK_DEBUG
1045 printf("looking for partition device %x, sector %"PRId64"\n", biosdev, sector);
1046 #endif
1047
1048 /* default to first partition */
1049 *partition = 0;
1050 if (part_name)
1051 *part_name = NULL;
1052
1053 /* Look for netbsd partition that is the dos boot one */
1054 d = alloc_biosdisk(biosdev);
1055 if (d == NULL)
1056 return -1;
1057
1058 if (read_partitions(d, 0, 0) == 0) {
1059 for (i = 0; i < BIOSDISKNPART; i++) {
1060 if (d->part[i].fstype == FS_UNUSED)
1061 continue;
1062
1063 if (d->part[i].offset == sector &&
1064 biosboot_sector_part == -1)
1065 biosboot_sector_part = i;
1066
1067 #ifndef NO_GPT
1068 if (d->part[i].attr & GPT_ENT_ATTR_BOOTME &&
1069 gpt_bootme_part == -1)
1070 gpt_bootme_part = i;
1071 #endif
1072 switch (d->part[i].fstype) {
1073 case FS_BSDFFS:
1074 case FS_BSDLFS:
1075 case FS_RAID:
1076 case FS_CCD:
1077 case FS_CGD:
1078 case FS_ISO9660:
1079 if (bootable_fs_part == -1)
1080 bootable_fs_part = i;
1081 break;
1082
1083 default:
1084 break;
1085 }
1086 }
1087
1088 #ifndef NO_GPT
1089 if (gpt_bootme_part != -1)
1090 boot_part = gpt_bootme_part;
1091 else
1092 #endif
1093 if (biosboot_sector_part != -1)
1094 boot_part = biosboot_sector_part;
1095 else if (bootable_fs_part != -1)
1096 boot_part = bootable_fs_part;
1097 else
1098 boot_part = 0;
1099
1100 *partition = boot_part;
1101 #ifndef NO_GPT
1102 if (part_name &&
1103 d->part[boot_part].part_name &&
1104 d->part[boot_part].part_name[0]) {
1105 strlcpy(namebuf, d->part[boot_part].part_name,
1106 BIOSDISK_PART_NAME_LEN);
1107 *part_name = namebuf;
1108 }
1109 #endif
1110 }
1111
1112 dealloc_biosdisk(d);
1113 return 0;
1114 #endif /* NO_DISKLABEL && NO_GPT */
1115 }
1116
1117 int
1118 biosdisk_readpartition(int biosdev, daddr_t offset, daddr_t size,
1119 struct biosdisk_partition **partpp, int *rnum)
1120 {
1121 #if defined(NO_DISKLABEL) && defined(NO_GPT)
1122 return ENOTSUP;
1123 #else
1124 struct biosdisk *d;
1125 struct biosdisk_partition *part;
1126 int rv;
1127
1128 /* Look for netbsd partition that is the dos boot one */
1129 d = alloc_biosdisk(biosdev);
1130 if (d == NULL)
1131 return ENOMEM;
1132
1133 if (read_partitions(d, offset, size)) {
1134 rv = EINVAL;
1135 goto out;
1136 }
1137
1138 part = copy_biosdisk_part(d);
1139 if (part == NULL) {
1140 rv = ENOMEM;
1141 goto out;
1142 }
1143
1144 *partpp = part;
1145 *rnum = (int)__arraycount(d->part);
1146 rv = 0;
1147 out:
1148 dealloc_biosdisk(d);
1149 return rv;
1150 #endif /* NO_DISKLABEL && NO_GPT */
1151 }
1152
1153 #ifndef NO_RAIDFRAME
1154 int
1155 biosdisk_read_raidframe(int biosdev, daddr_t offset,
1156 struct RF_ComponentLabel_s *label)
1157 {
1158 #if defined(NO_DISKLABEL) && defined(NO_GPT)
1159 return ENOTSUP;
1160 #else
1161 struct biosdisk *d;
1162 struct biosdisk_extinfo ed;
1163 daddr_t size;
1164 int rv = -1;
1165
1166 /* Look for netbsd partition that is the dos boot one */
1167 d = alloc_biosdisk(biosdev);
1168 if (d == NULL)
1169 goto out;
1170
1171 if (d->ll.type != BIOSDISK_TYPE_HD)
1172 /* No raidframe on floppy and CD */
1173 goto out;
1174
1175 if (set_geometry(&d->ll, &ed) != 0)
1176 goto out;
1177
1178 /* Sanity check values returned from BIOS */
1179 if (ed.sbytes >= 512 &&
1180 (ed.sbytes & (ed.sbytes - 1)) == 0)
1181 d->ll.secsize = ed.sbytes;
1182
1183 offset += (RF_COMPONENT_INFO_OFFSET / d->ll.secsize);
1184 size = roundup(sizeof(*label), d->ll.secsize) / d->ll.secsize;
1185 if (readsects(&d->ll, offset, size, d->buf, 0))
1186 goto out;
1187 memcpy(label, d->buf, sizeof(*label));
1188 rv = 0;
1189 out:
1190 if (d != NULL)
1191 dealloc_biosdisk(d);
1192 return rv;
1193 #endif /* NO_DISKLABEL && NO_GPT */
1194 }
1195 #endif /* NO_RAIDFRAME */
1196
1197 #ifdef _STANDALONE
1198 static void
1199 add_biosdisk_bootinfo(void)
1200 {
1201 if (bootinfo == NULL) {
1202 return;
1203 }
1204 BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
1205 BI_ADD(&bi_wedge, BTINFO_BOOTWEDGE, sizeof(bi_wedge));
1206 return;
1207 }
1208 #endif
1209
1210 #ifndef NO_GPT
1211 static daddr_t
1212 raidframe_part_offset(struct biosdisk *d, int part)
1213 {
1214 struct biosdisk raidframe;
1215 daddr_t rf_offset;
1216 daddr_t rf_size;
1217 int i, candidate;
1218
1219 memset(&raidframe, 0, sizeof(raidframe));
1220 raidframe.ll = d->ll;
1221
1222 rf_offset = d->part[part].offset + RF_PROTECTED_SECTORS;
1223 rf_size = d->part[part].size;
1224 if (read_gpt(&raidframe, rf_offset, rf_size) != 0)
1225 return RF_PROTECTED_SECTORS;
1226
1227 candidate = 0;
1228 for (i = 0; i < BIOSDISKNPART; i++) {
1229 if (raidframe.part[i].size == 0)
1230 continue;
1231 if (raidframe.part[i].fstype == FS_UNUSED)
1232 continue;
1233 #ifndef NO_GPT
1234 if (raidframe.part[i].attr & GPT_ENT_ATTR_BOOTME)
1235 candidate = i;
1236 #endif
1237 }
1238
1239 return RF_PROTECTED_SECTORS + raidframe.part[candidate].offset;
1240 }
1241 #endif
1242
1243 int
1244 biosdisk_open(struct open_file *f, ...)
1245 /* struct open_file *f, int biosdev, int partition */
1246 {
1247 va_list ap;
1248 struct biosdisk *d;
1249 int biosdev;
1250 int partition;
1251 int error = 0;
1252
1253 va_start(ap, f);
1254 biosdev = va_arg(ap, int);
1255 d = alloc_biosdisk(biosdev);
1256 if (d == NULL) {
1257 error = ENXIO;
1258 goto out;
1259 }
1260
1261 partition = va_arg(ap, int);
1262 bi_disk.biosdev = d->ll.dev;
1263 bi_disk.partition = partition;
1264 bi_disk.labelsector = -1;
1265
1266 bi_wedge.biosdev = d->ll.dev;
1267 bi_wedge.matchblk = -1;
1268
1269 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
1270 error = read_partitions(d, 0, 0);
1271 if (error == -1) {
1272 error = 0;
1273 goto nolabel;
1274 }
1275 if (error)
1276 goto out;
1277
1278 if (partition >= BIOSDISKNPART ||
1279 d->part[partition].fstype == FS_UNUSED) {
1280 #ifdef DISK_DEBUG
1281 printf("illegal partition\n");
1282 #endif
1283 error = EPART;
1284 goto out;
1285 }
1286
1287 d->boff = d->part[partition].offset;
1288
1289 if (d->part[partition].fstype == FS_RAID)
1290 #ifndef NO_GPT
1291 d->boff += raidframe_part_offset(d, partition);
1292 #else
1293 d->boff += RF_PROTECTED_SECTORS;
1294 #endif
1295
1296 #ifdef _STANDALONE
1297 bi_wedge.startblk = d->part[partition].offset;
1298 bi_wedge.nblks = d->part[partition].size;
1299 #endif
1300
1301 nolabel:
1302 #endif
1303 #ifdef DISK_DEBUG
1304 printf("partition @%"PRId64"\n", d->boff);
1305 #endif
1306
1307 #ifdef _STANDALONE
1308 add_biosdisk_bootinfo();
1309 #endif
1310
1311 f->f_devdata = d;
1312 out:
1313 va_end(ap);
1314 if (error)
1315 dealloc_biosdisk(d);
1316 return error;
1317 }
1318
1319 #ifndef NO_GPT
1320 static int
1321 biosdisk_find_name(const char *fname, int *biosdev,
1322 daddr_t *offset, daddr_t *size)
1323 {
1324 struct biosdisk *d;
1325 char name[MAXDEVNAME + 1];
1326 char *sep;
1327 #ifndef NO_RAIDFRAME
1328 struct raidframe raidframe[RAIDFRAME_NDEV];
1329 int raidframe_count = 0;
1330 #endif
1331 int i;
1332 int part;
1333 int ret = -1;
1334
1335 /* Strip leadinf NAME= and cut after the coloon included */
1336 strlcpy(name, fname + 5, MAXDEVNAME);
1337 sep = strchr(name, ':');
1338 if (sep)
1339 *sep = '\0';
1340
1341 for (i = 0; i < MAX_BIOSDISKS; i++) {
1342 d = alloc(sizeof(*d));
1343 if (d == NULL) {
1344 printf("Out of memory\n");
1345 goto out;
1346 }
1347
1348 memset(d, 0, sizeof(*d));
1349 d->ll.dev = 0x80 + i; /* hd/cd */
1350 if (set_geometry(&d->ll, NULL))
1351 goto next_disk;
1352
1353 if (d->ll.type != BIOSDISK_TYPE_HD)
1354 goto next_disk;
1355
1356 if (read_partitions(d, 0, 0) != 0)
1357 goto next_disk;
1358
1359 for (part = 0; part < BIOSDISKNPART; part++) {
1360 if (d->part[part].size == 0)
1361 continue;
1362 if (d->part[part].fstype == FS_UNUSED)
1363 continue;
1364 #ifndef NO_RAIDFRAME
1365 if (d->part[part].fstype == FS_RAID) {
1366 raidframe_probe(raidframe,
1367 &raidframe_count, d, part);
1368 /*
1369 * Do not match RAID partition for a name,
1370 * we want to report an inner partition.
1371 */
1372 continue;
1373 }
1374 #endif
1375 if (d->part[part].part_name != NULL &&
1376 strcmp(d->part[part].part_name, name) == 0) {
1377 *biosdev = d->ll.dev;
1378 *offset = d->part[part].offset;
1379 *size = d->part[part].size;
1380 ret = 0;
1381 goto out;
1382 }
1383
1384 }
1385 next_disk:
1386 dealloc_biosdisk(d);
1387 d = NULL;
1388 }
1389
1390 #ifndef NO_RAIDFRAME
1391 for (i = 0; i < raidframe_count; i++) {
1392 int candidate = -1;
1393
1394 if ((d = alloc_biosdisk(raidframe[i].biosdev)) == NULL) {
1395 printf("Out of memory\n");
1396 goto out;
1397 }
1398
1399 if (read_partitions(d,
1400 raidframe[i].offset + RF_PROTECTED_SECTORS,
1401 raidframe[i].size) != 0)
1402 goto next_raidframe;
1403
1404 for (part = 0; part < BIOSDISKNPART; part++) {
1405 bool bootme = d->part[part].attr & GPT_ENT_ATTR_BOOTME;
1406 if (d->part[part].size == 0)
1407 continue;
1408 if (d->part[part].fstype == FS_UNUSED)
1409 continue;
1410
1411 if (d->part[part].part_name != NULL &&
1412 strcmp(d->part[part].part_name, name) == 0) {
1413 *biosdev = raidframe[i].biosdev;
1414 *offset = raidframe[i].offset
1415 + RF_PROTECTED_SECTORS
1416 + d->part[part].offset;
1417 *size = d->part[part].size;
1418 ret = 0;
1419 goto out;
1420 }
1421 if (strcmp(raidframe[i].parent_name, name) == 0) {
1422 if (candidate == -1 || bootme)
1423 candidate = part;
1424 continue;
1425 }
1426 }
1427
1428 if (candidate != -1) {
1429 *biosdev = raidframe[i].biosdev;
1430 *offset = raidframe[i].offset
1431 + RF_PROTECTED_SECTORS
1432 + d->part[candidate].offset;
1433 *size = d->part[candidate].size;
1434 ret = 0;
1435 goto out;
1436 }
1437
1438 next_raidframe:
1439 dealloc_biosdisk(d);
1440 d = NULL;
1441 }
1442 #endif
1443
1444 out:
1445 if (d != NULL)
1446 dealloc_biosdisk(d);
1447
1448 return ret;
1449 }
1450 #endif
1451
1452 #ifndef NO_RAIDFRAME
1453 static int
1454 biosdisk_find_raid(const char *name, int *biosdev,
1455 daddr_t *offset, daddr_t *size)
1456 {
1457 struct biosdisk *d = NULL;
1458 struct raidframe raidframe[RAIDFRAME_NDEV];
1459 int raidframe_count = 0;
1460 int i;
1461 int target_unit = 0;
1462 int target_part;
1463 int part;
1464 int ret = -1;
1465
1466 if (strstr(name, "raid") != name)
1467 goto out;
1468
1469 #define isnum(c) ((c) >= '0' && (c) <= '9')
1470 i = 4; /* skip leading "raid" */
1471 if (!isnum(name[i]))
1472 goto out;
1473 do {
1474 target_unit *= 10;
1475 target_unit += name[i++] - '0';
1476 } while (isnum(name[i]));
1477
1478 #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')
1479
1480 if (!isvalidpart(name[i]))
1481 goto out;
1482 target_part = name[i] - 'a';
1483
1484 for (i = 0; i < MAX_BIOSDISKS; i++) {
1485 d = alloc(sizeof(*d));
1486 if (d == NULL) {
1487 printf("Out of memory\n");
1488 goto out;
1489 }
1490
1491 memset(d, 0, sizeof(*d));
1492 d->ll.dev = 0x80 + i; /* hd/cd */
1493 if (set_geometry(&d->ll, NULL))
1494 goto next_disk;
1495
1496 if (d->ll.type != BIOSDISK_TYPE_HD)
1497 goto next_disk;
1498
1499 if (read_partitions(d, 0, 0) != 0)
1500 goto next_disk;
1501
1502 for (part = 0; part < BIOSDISKNPART; part++) {
1503 if (d->part[part].size == 0)
1504 continue;
1505 if (d->part[part].fstype != FS_RAID)
1506 continue;
1507 raidframe_probe(raidframe,
1508 &raidframe_count, d, part);
1509
1510 }
1511 next_disk:
1512 dealloc_biosdisk(d);
1513 d = NULL;
1514 }
1515
1516 for (i = 0; i < raidframe_count; i++) {
1517 if (raidframe[i].last_unit != target_unit)
1518 continue;
1519
1520 if ((d = alloc_biosdisk(raidframe[i].biosdev)) == NULL) {
1521 printf("Out of memory\n");
1522 goto out;
1523 }
1524
1525 if (read_partitions(d,
1526 raidframe[i].offset + RF_PROTECTED_SECTORS,
1527 raidframe[i].size) != 0)
1528 goto next_raidframe;
1529
1530 for (part = 0; part < BIOSDISKNPART; part++) {
1531 if (d->part[part].size == 0)
1532 continue;
1533 if (d->part[part].fstype == FS_UNUSED)
1534 continue;
1535 if (part == target_part) {
1536 *biosdev = raidframe[i].biosdev;
1537 *offset = raidframe[i].offset
1538 + RF_PROTECTED_SECTORS
1539 + d->part[part].offset;
1540 *size = d->part[part].size;
1541 ret = 0;
1542 goto out;
1543 }
1544 }
1545 next_raidframe:
1546 dealloc_biosdisk(d);
1547 d = NULL;
1548 }
1549 out:
1550 if (d != NULL)
1551 dealloc_biosdisk(d);
1552
1553 return ret;
1554 }
1555 #endif
1556
1557 int
1558 biosdisk_open_name(struct open_file *f, const char *name)
1559 {
1560 #if defined(NO_GPT) && defined(NO_RAIDFRAME)
1561 return ENXIO;
1562 #else
1563 struct biosdisk *d = NULL;
1564 int biosdev;
1565 daddr_t offset;
1566 daddr_t size;
1567 int error = -1;
1568
1569 #ifndef NO_GPT
1570 if (strstr(name, "NAME=") == name)
1571 error = biosdisk_find_name(name, &biosdev, &offset, &size);
1572 #endif
1573 #ifndef NO_RAIDFRAME
1574 if (strstr(name, "raid") == name)
1575 error = biosdisk_find_raid(name, &biosdev, &offset, &size);
1576 #endif
1577
1578 if (error != 0) {
1579 printf("%s not found\n", name);
1580 error = ENXIO;
1581 goto out;
1582 }
1583
1584 d = alloc_biosdisk(biosdev);
1585 if (d == NULL) {
1586 error = ENXIO;
1587 goto out;
1588 }
1589
1590 bi_disk.biosdev = d->ll.dev;
1591 bi_disk.partition = 0;
1592 bi_disk.labelsector = -1;
1593
1594 bi_wedge.biosdev = d->ll.dev;
1595
1596 /*
1597 * If we did not get wedge match info from check_gpt()
1598 * compute it now.
1599 */
1600 if (bi_wedge.matchblk == -1) {
1601 if (readsects(&d->ll, offset, 1, d->buf, 1)) {
1602 #ifdef DISK_DEBUG
1603 printf("Error reading sector at %"PRId64"\n", offset);
1604 #endif
1605 error = EIO;
1606 goto out;
1607 }
1608
1609 bi_wedge.matchblk = offset;
1610 bi_wedge.matchnblks = 1;
1611
1612 md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
1613 }
1614
1615 d->boff = offset;
1616
1617 bi_wedge.startblk = offset;
1618 bi_wedge.nblks = size;
1619
1620 #ifdef _STANDALONE
1621 add_biosdisk_bootinfo();
1622 #endif
1623
1624 f->f_devdata = d;
1625 out:
1626 if (error && d != NULL)
1627 dealloc_biosdisk(d);
1628 return error;
1629 #endif
1630 }
1631
1632
1633
1634 #ifndef LIBSA_NO_FS_CLOSE
1635 int
1636 biosdisk_close(struct open_file *f)
1637 {
1638 struct biosdisk *d = f->f_devdata;
1639
1640 /* let the floppy drive go off */
1641 if (d->ll.type == BIOSDISK_TYPE_FD)
1642 wait_sec(3); /* 2s is enough on all PCs I found */
1643
1644 dealloc_biosdisk(d);
1645 f->f_devdata = NULL;
1646 return 0;
1647 }
1648 #endif
1649
1650 int
1651 biosdisk_ioctl(struct open_file *f, u_long cmd, void *arg)
1652 {
1653 return EIO;
1654 }
1655