biosdisk.c revision 1.54 1 /* $NetBSD: biosdisk.c,v 1.54 2019/12/17 01:37:53 manu 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
190 #define MBR_PARTS(buf) ((char *)(buf) + offsetof(struct mbr_sector, mbr_parts))
191
192 #ifndef devb2cdb
193 #define devb2cdb(bno) (((bno) * DEV_BSIZE) / ISO_DEFAULT_BLOCK_SIZE)
194 #endif
195
196 static void
197 dealloc_biosdisk(struct biosdisk *d)
198 {
199 #ifndef NO_GPT
200 int i;
201
202 for (i = 0; i < __arraycount(d->part); i++) {
203 if (d->part[i].part_name != NULL)
204 dealloc(d->part[i].part_name, BIOSDISK_PART_NAME_LEN);
205 }
206 #endif
207
208 dealloc(d, sizeof(*d));
209
210 return;
211 }
212
213 static struct biosdisk_partition *
214 copy_biosdisk_part(struct biosdisk *d)
215 {
216 struct biosdisk_partition *part;
217
218 part = alloc(sizeof(d->part));
219 if (part == NULL)
220 goto out;
221
222 memcpy(part, d->part, sizeof(d->part));
223
224 #ifndef NO_GPT
225 int i;
226
227 for (i = 0; i < __arraycount(d->part); i++) {
228 if (d->part[i].part_name != NULL) {
229 part[i].part_name = alloc(BIOSDISK_PART_NAME_LEN);
230 memcpy(part[i].part_name, d->part[i].part_name,
231 BIOSDISK_PART_NAME_LEN);
232 }
233 }
234 #endif
235
236 out:
237 return part;
238 }
239
240 int
241 biosdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
242 void *buf, size_t *rsize)
243 {
244 struct biosdisk *d;
245 int blks, frag;
246
247 if (flag != F_READ)
248 return EROFS;
249
250 d = (struct biosdisk *) devdata;
251
252 if (d->ll.type == BIOSDISK_TYPE_CD)
253 dblk = devb2cdb(dblk);
254
255 dblk += d->boff;
256
257 blks = size / d->ll.secsize;
258 if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
259 if (rsize)
260 *rsize = 0;
261 return EIO;
262 }
263
264 /* needed for CD */
265 frag = size % d->ll.secsize;
266 if (frag) {
267 if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
268 if (rsize)
269 *rsize = blks * d->ll.secsize;
270 return EIO;
271 }
272 memcpy(buf + blks * d->ll.secsize, d->buf, frag);
273 }
274
275 if (rsize)
276 *rsize = size;
277 return 0;
278 }
279
280 static struct biosdisk *
281 alloc_biosdisk(int biosdev)
282 {
283 struct biosdisk *d;
284
285 d = alloc(sizeof(*d));
286 if (d == NULL)
287 return NULL;
288 memset(d, 0, sizeof(*d));
289
290 d->ll.dev = biosdev;
291 if (set_geometry(&d->ll, NULL)) {
292 #ifdef DISK_DEBUG
293 printf("no geometry information\n");
294 #endif
295 dealloc_biosdisk(d);
296 return NULL;
297 }
298 return d;
299 }
300
301 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
302 static void
303 md5(void *hash, const void *data, size_t len)
304 {
305 MD5_CTX ctx;
306
307 MD5Init(&ctx);
308 MD5Update(&ctx, data, len);
309 MD5Final(hash, &ctx);
310
311 return;
312 }
313 #endif
314
315 #ifndef NO_GPT
316 bool
317 guid_is_nil(const struct uuid *u)
318 {
319 static const struct uuid nil = { .time_low = 0 };
320 return (memcmp(u, &nil, sizeof(*u)) == 0 ? true : false);
321 }
322
323 bool
324 guid_is_equal(const struct uuid *a, const struct uuid *b)
325 {
326 return (memcmp(a, b, sizeof(*a)) == 0 ? true : false);
327 }
328
329 #ifndef NO_GPT
330 static void
331 part_name_utf8(const uint16_t *utf16_src, size_t utf16_srclen,
332 char *utf8_dst, size_t utf8_dstlen)
333 {
334 char *c = utf8_dst;
335 size_t r = utf8_dstlen - 1;
336 size_t n;
337 int j;
338
339 if (utf8_dst == NULL)
340 return;
341
342 for (j = 0; j < utf16_srclen && utf16_src[j] != 0x0000; j++) {
343 n = wput_utf8(c, r, le16toh(utf16_src[j]));
344 if (n == 0)
345 break;
346 c += n; r -= n;
347 }
348 *c = '\0';
349
350 return;
351 }
352 #endif
353
354 static int
355 check_gpt(struct biosdisk *d, daddr_t rf_offset, daddr_t sector)
356 {
357 struct gpt_hdr gpth;
358 const struct gpt_ent *ep;
359 const struct uuid *u;
360 daddr_t entblk;
361 size_t size;
362 uint32_t crc;
363 int sectors;
364 int entries;
365 int entry;
366 int i, j;
367
368 /* read in gpt_hdr sector */
369 if (readsects(&d->ll, sector, 1, d->buf, 1)) {
370 #ifdef DISK_DEBUG
371 printf("Error reading GPT header at %"PRId64"\n", sector);
372 #endif
373 return EIO;
374 }
375
376 memcpy(&gpth, d->buf, sizeof(gpth));
377
378 if (memcmp(GPT_HDR_SIG, gpth.hdr_sig, sizeof(gpth.hdr_sig)))
379 return -1;
380
381 crc = gpth.hdr_crc_self;
382 gpth.hdr_crc_self = 0;
383 gpth.hdr_crc_self = crc32(0, (const void *)&gpth, GPT_HDR_SIZE);
384 if (gpth.hdr_crc_self != crc) {
385 return -1;
386 }
387
388 if (gpth.hdr_lba_self + rf_offset != sector)
389 return -1;
390
391 #ifdef _STANDALONE
392 bi_wedge.matchblk = sector;
393 bi_wedge.matchnblks = 1;
394
395 md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
396 #endif
397
398 sectors = sizeof(d->buf)/d->ll.secsize; /* sectors per buffer */
399 entries = sizeof(d->buf)/gpth.hdr_entsz; /* entries per buffer */
400 entblk = gpth.hdr_lba_table + rf_offset;
401 crc = crc32(0, NULL, 0);
402
403 j = 0;
404 ep = (const struct gpt_ent *)d->buf;
405
406 for (entry = 0; entry < gpth.hdr_entries; entry += entries) {
407 size = MIN(sizeof(d->buf),
408 (gpth.hdr_entries - entry) * gpth.hdr_entsz);
409 entries = size / gpth.hdr_entsz;
410 sectors = roundup(size, d->ll.secsize) / d->ll.secsize;
411 if (readsects(&d->ll, entblk, sectors, d->buf, 1))
412 return -1;
413 entblk += sectors;
414 crc = crc32(crc, (const void *)d->buf, size);
415
416 for (i = 0; j < BIOSDISKNPART && i < entries; i++) {
417 u = (const struct uuid *)ep[i].ent_type;
418 if (!guid_is_nil(u)) {
419 d->part[j].offset = ep[i].ent_lba_start;
420 d->part[j].size = ep[i].ent_lba_end -
421 ep[i].ent_lba_start + 1;
422 if (guid_is_equal(u, &GET_nbsd_ffs))
423 d->part[j].fstype = FS_BSDFFS;
424 else if (guid_is_equal(u, &GET_nbsd_lfs))
425 d->part[j].fstype = FS_BSDLFS;
426 else if (guid_is_equal(u, &GET_nbsd_raid))
427 d->part[j].fstype = FS_RAID;
428 else if (guid_is_equal(u, &GET_nbsd_swap))
429 d->part[j].fstype = FS_SWAP;
430 else if (guid_is_equal(u, &GET_nbsd_ccd))
431 d->part[j].fstype = FS_CCD;
432 else if (guid_is_equal(u, &GET_nbsd_cgd))
433 d->part[j].fstype = FS_CGD;
434 else
435 d->part[j].fstype = FS_OTHER;
436 #ifndef NO_GPT
437 for (int k = 0;
438 k < __arraycount(gpt_parts);
439 k++) {
440 if (guid_is_equal(u, gpt_parts[k].guid))
441 d->part[j].guid = &gpt_parts[k];
442 }
443 d->part[j].attr = ep[i].ent_attr;
444
445 d->part[j].part_name =
446 alloc(BIOSDISK_PART_NAME_LEN);
447 part_name_utf8(ep[i].ent_name,
448 sizeof(ep[i].ent_name),
449 d->part[j].part_name,
450 BIOSDISK_PART_NAME_LEN);
451 #endif
452 j++;
453 }
454 }
455
456 }
457
458 if (crc != gpth.hdr_crc_table) {
459 #ifdef DISK_DEBUG
460 printf("GPT table CRC invalid\n");
461 #endif
462 return -1;
463 }
464
465 return 0;
466 }
467
468 static int
469 read_gpt(struct biosdisk *d, daddr_t rf_offset, daddr_t rf_size)
470 {
471 struct biosdisk_extinfo ed;
472 daddr_t gptsector[2];
473 int i, error;
474
475 if (d->ll.type != BIOSDISK_TYPE_HD)
476 /* No GPT on floppy and CD */
477 return -1;
478
479 if (rf_offset && rf_size) {
480 gptsector[0] = rf_offset + GPT_HDR_BLKNO;
481 gptsector[1] = rf_offset + rf_size - 1;
482 } else {
483 gptsector[0] = GPT_HDR_BLKNO;
484 if (set_geometry(&d->ll, &ed) == 0 &&
485 d->ll.flags & BIOSDISK_INT13EXT) {
486 gptsector[1] = ed.totsec - 1;
487 /* Sanity check values returned from BIOS */
488 if (ed.sbytes >= 512 &&
489 (ed.sbytes & (ed.sbytes - 1)) == 0)
490 d->ll.secsize = ed.sbytes;
491 } else {
492 #ifdef DISK_DEBUG
493 printf("Unable to determine extended disk geometry - "
494 "using CHS\n");
495 #endif
496 /* at least try some other reasonable values then */
497 gptsector[1] = d->ll.chs_sectors - 1;
498 }
499 }
500
501 for (i = 0; i < __arraycount(gptsector); i++) {
502 error = check_gpt(d, rf_offset, gptsector[i]);
503 if (error == 0)
504 break;
505 }
506
507 if (i >= __arraycount(gptsector)) {
508 memset(d->part, 0, sizeof(d->part));
509 return -1;
510 }
511
512 #ifndef USE_SECONDARY_GPT
513 if (i > 0) {
514 #ifdef DISK_DEBUG
515 printf("ignoring valid secondary GPT\n");
516 #endif
517 return -1;
518 }
519 #endif
520
521 #ifdef DISK_DEBUG
522 printf("using %s GPT\n", (i == 0) ? "primary" : "secondary");
523 #endif
524 return 0;
525 }
526 #endif /* !NO_GPT */
527
528 #ifndef NO_DISKLABEL
529 static void
530 ingest_label(struct biosdisk *d, struct disklabel *lp)
531 {
532 int part;
533
534 memset(d->part, 0, sizeof(d->part));
535
536 for (part = 0; part < lp->d_npartitions; part++) {
537 if (lp->d_partitions[part].p_size == 0)
538 continue;
539 if (lp->d_partitions[part].p_fstype == FS_UNUSED)
540 continue;
541 d->part[part].fstype = lp->d_partitions[part].p_fstype;
542 d->part[part].offset = lp->d_partitions[part].p_offset;
543 d->part[part].size = lp->d_partitions[part].p_size;
544 }
545 }
546
547 static int
548 check_label(struct biosdisk *d, daddr_t sector)
549 {
550 struct disklabel *lp;
551
552 /* find partition in NetBSD disklabel */
553 if (readsects(&d->ll, sector + LABELSECTOR, 1, d->buf, 0)) {
554 #ifdef DISK_DEBUG
555 printf("Error reading disklabel\n");
556 #endif
557 return EIO;
558 }
559 lp = (struct disklabel *) (d->buf + LABELOFFSET);
560 if (lp->d_magic != DISKMAGIC || dkcksum(lp)) {
561 #ifdef DISK_DEBUG
562 printf("warning: no disklabel in sector %"PRId64"\n", sector);
563 #endif
564 return -1;
565 }
566
567 ingest_label(d, lp);
568
569 bi_disk.labelsector = sector + LABELSECTOR;
570 bi_disk.label.type = lp->d_type;
571 memcpy(bi_disk.label.packname, lp->d_packname, 16);
572 bi_disk.label.checksum = lp->d_checksum;
573
574 bi_wedge.matchblk = sector + LABELSECTOR;
575 bi_wedge.matchnblks = 1;
576
577 md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
578
579 return 0;
580 }
581
582 static int
583 read_minix_subp(struct biosdisk *d, struct disklabel* dflt_lbl,
584 int this_ext, daddr_t sector)
585 {
586 struct mbr_partition mbr[MBR_PART_COUNT];
587 int i;
588 int typ;
589 struct partition *p;
590
591 if (readsects(&d->ll, sector, 1, d->buf, 0)) {
592 #ifdef DISK_DEBUG
593 printf("Error reading MFS sector %"PRId64"\n", sector);
594 #endif
595 return EIO;
596 }
597 if ((uint8_t)d->buf[510] != 0x55 || (uint8_t)d->buf[511] != 0xAA) {
598 return -1;
599 }
600 memcpy(&mbr, MBR_PARTS(d->buf), sizeof(mbr));
601 for (i = 0; i < MBR_PART_COUNT; i++) {
602 typ = mbr[i].mbrp_type;
603 if (typ == 0)
604 continue;
605 sector = this_ext + mbr[i].mbrp_start;
606 if (dflt_lbl->d_npartitions >= MAXPARTITIONS)
607 continue;
608 p = &dflt_lbl->d_partitions[dflt_lbl->d_npartitions++];
609 p->p_offset = sector;
610 p->p_size = mbr[i].mbrp_size;
611 p->p_fstype = xlat_mbr_fstype(typ);
612 }
613 return 0;
614 }
615
616 #if defined(EFIBOOT) && defined(SUPPORT_CD9660)
617 static int
618 check_cd9660(struct biosdisk *d)
619 {
620 struct biosdisk_extinfo ed;
621 struct iso_primary_descriptor *vd;
622 daddr_t bno;
623
624 for (bno = 16;; bno++) {
625 if (readsects(&d->ll, bno, 1, d->buf, 0))
626 return -1;
627 vd = (struct iso_primary_descriptor *)d->buf;
628 if (memcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
629 return -1;
630 if (isonum_711(vd->type) == ISO_VD_END)
631 return -1;
632 if (isonum_711(vd->type) == ISO_VD_PRIMARY)
633 break;
634 }
635 if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
636 return -1;
637
638 if (set_geometry(&d->ll, &ed))
639 return -1;
640
641 memset(d->part, 0, sizeof(d->part));
642 d->part[0].fstype = FS_ISO9660;
643 d->part[0].offset = 0;
644 d->part[0].size = ed.totsec;
645 return 0;
646 }
647 #endif
648
649 static int
650 read_label(struct biosdisk *d, daddr_t offset)
651 {
652 struct disklabel dflt_lbl;
653 struct mbr_partition mbr[MBR_PART_COUNT];
654 struct partition *p;
655 uint32_t sector;
656 int i;
657 int error;
658 int typ;
659 uint32_t ext_base, this_ext, next_ext;
660 #ifdef COMPAT_386BSD_MBRPART
661 int sector_386bsd = -1;
662 #endif
663
664 memset(&dflt_lbl, 0, sizeof(dflt_lbl));
665 dflt_lbl.d_npartitions = 8;
666
667 d->boff = 0;
668
669 if (d->ll.type != BIOSDISK_TYPE_HD)
670 /* No label on floppy and CD */
671 return -1;
672
673 /*
674 * find NetBSD Partition in DOS partition table
675 * XXX check magic???
676 */
677 ext_base = offset;
678 next_ext = offset;
679 for (;;) {
680 this_ext = ext_base + next_ext;
681 next_ext = offset;
682 if (readsects(&d->ll, this_ext, 1, d->buf, 0)) {
683 #ifdef DISK_DEBUG
684 printf("error reading MBR sector %u\n", this_ext);
685 #endif
686 return EIO;
687 }
688 memcpy(&mbr, MBR_PARTS(d->buf), sizeof(mbr));
689 /* Look for NetBSD partition ID */
690 for (i = 0; i < MBR_PART_COUNT; i++) {
691 typ = mbr[i].mbrp_type;
692 if (typ == 0)
693 continue;
694 sector = this_ext + mbr[i].mbrp_start;
695 #ifdef DISK_DEBUG
696 printf("ptn type %d in sector %u\n", typ, sector);
697 #endif
698 if (typ == MBR_PTYPE_MINIX_14B) {
699 if (!read_minix_subp(d, &dflt_lbl,
700 this_ext, sector)) {
701 /* Don't add "container" partition */
702 continue;
703 }
704 }
705 if (typ == MBR_PTYPE_NETBSD) {
706 error = check_label(d, sector);
707 if (error >= 0)
708 return error;
709 }
710 if (MBR_IS_EXTENDED(typ)) {
711 next_ext = mbr[i].mbrp_start + offset;
712 continue;
713 }
714 #ifdef COMPAT_386BSD_MBRPART
715 if (this_ext == offset && typ == MBR_PTYPE_386BSD)
716 sector_386bsd = sector;
717 #endif
718 if (this_ext != offset) {
719 if (dflt_lbl.d_npartitions >= MAXPARTITIONS)
720 continue;
721 p = &dflt_lbl.d_partitions[dflt_lbl.d_npartitions++];
722 } else
723 p = &dflt_lbl.d_partitions[i];
724 p->p_offset = sector;
725 p->p_size = mbr[i].mbrp_size;
726 p->p_fstype = xlat_mbr_fstype(typ);
727 }
728 if (next_ext == offset)
729 break;
730 if (ext_base == offset) {
731 ext_base = next_ext;
732 next_ext = offset;
733 }
734 }
735
736 sector = offset;
737 #ifdef COMPAT_386BSD_MBRPART
738 if (sector_386bsd != -1) {
739 printf("old BSD partition ID!\n");
740 sector = sector_386bsd;
741 }
742 #endif
743
744 /*
745 * One of two things:
746 * 1. no MBR
747 * 2. no NetBSD partition in MBR
748 *
749 * We simply default to "start of disk" in this case and
750 * press on.
751 */
752 error = check_label(d, sector);
753 if (error >= 0)
754 return error;
755
756 #if defined(EFIBOOT) && defined(SUPPORT_CD9660)
757 /* Check CD/DVD */
758 error = check_cd9660(d);
759 if (error >= 0)
760 return error;
761 #endif
762
763 /*
764 * Nothing at start of disk, return info from mbr partitions.
765 */
766 /* XXX fill it to make checksum match kernel one */
767 dflt_lbl.d_checksum = dkcksum(&dflt_lbl);
768 ingest_label(d, &dflt_lbl);
769 return 0;
770 }
771 #endif /* NO_DISKLABEL */
772
773 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
774 static int
775 read_partitions(struct biosdisk *d, daddr_t offset, daddr_t size)
776 {
777 int error;
778
779 error = -1;
780
781 #ifndef NO_GPT
782 error = read_gpt(d, offset, size);
783 if (error == 0)
784 return 0;
785
786 #endif
787 #ifndef NO_DISKLABEL
788 error = read_label(d, offset);
789
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 *part_name = NULL;
1031 return 0;
1032 #else
1033 int i;
1034 struct biosdisk *d;
1035 int biosboot_sector_part = -1;
1036 int bootable_fs_part = -1;
1037 int boot_part = 0;
1038 #ifndef NO_GPT
1039 int gpt_bootme_part = -1;
1040 static char namebuf[MAXDEVNAME + 1];
1041 #endif
1042
1043 #ifdef DISK_DEBUG
1044 printf("looking for partition device %x, sector %"PRId64"\n", biosdev, sector);
1045 #endif
1046
1047 /* default ot first partition */
1048 *partition = 0;
1049 *part_name = NULL;
1050
1051 /* Look for netbsd partition that is the dos boot one */
1052 d = alloc_biosdisk(biosdev);
1053 if (d == NULL)
1054 return -1;
1055
1056 if (read_partitions(d, 0, 0) == 0) {
1057 for (i = 0; i < BIOSDISKNPART; i++) {
1058 if (d->part[i].fstype == FS_UNUSED)
1059 continue;
1060
1061 if (d->part[i].offset == sector &&
1062 biosboot_sector_part == -1)
1063 biosboot_sector_part = i;
1064
1065 #ifndef NO_GPT
1066 if (d->part[i].attr & GPT_ENT_ATTR_BOOTME &&
1067 gpt_bootme_part == -1)
1068 gpt_bootme_part = i;
1069 #endif
1070 switch (d->part[i].fstype) {
1071 case FS_BSDFFS:
1072 case FS_BSDLFS:
1073 case FS_RAID:
1074 case FS_CCD:
1075 case FS_CGD:
1076 case FS_ISO9660:
1077 if (bootable_fs_part == -1)
1078 bootable_fs_part = i;
1079 break;
1080
1081 default:
1082 break;
1083 }
1084 }
1085
1086 #ifndef NO_GPT
1087 if (gpt_bootme_part != -1)
1088 boot_part = gpt_bootme_part;
1089 else
1090 #endif
1091 if (biosboot_sector_part != -1)
1092 boot_part = biosboot_sector_part;
1093 else if (bootable_fs_part != -1)
1094 boot_part = bootable_fs_part;
1095 else
1096 boot_part = 0;
1097
1098 *partition = boot_part;
1099 #ifndef NO_GPT
1100 if (part_name &&
1101 d->part[boot_part].part_name &&
1102 d->part[boot_part].part_name[0]) {
1103 strlcpy(namebuf, d->part[boot_part].part_name,
1104 BIOSDISK_PART_NAME_LEN);
1105 *part_name = namebuf;
1106 }
1107 #endif
1108 }
1109
1110 dealloc_biosdisk(d);
1111 return 0;
1112 #endif /* NO_DISKLABEL && NO_GPT */
1113 }
1114
1115 int
1116 biosdisk_readpartition(int biosdev, daddr_t offset, daddr_t size,
1117 struct biosdisk_partition **partpp, int *rnum)
1118 {
1119 #if defined(NO_DISKLABEL) && defined(NO_GPT)
1120 return ENOTSUP;
1121 #else
1122 struct biosdisk *d;
1123 struct biosdisk_partition *part;
1124 int rv;
1125
1126 /* Look for netbsd partition that is the dos boot one */
1127 d = alloc_biosdisk(biosdev);
1128 if (d == NULL)
1129 return ENOMEM;
1130
1131 if (read_partitions(d, offset, size)) {
1132 rv = EINVAL;
1133 goto out;
1134 }
1135
1136 part = copy_biosdisk_part(d);
1137 if (part == NULL) {
1138 rv = ENOMEM;
1139 goto out;
1140 }
1141
1142 *partpp = part;
1143 *rnum = (int)__arraycount(d->part);
1144 rv = 0;
1145 out:
1146 dealloc_biosdisk(d);
1147 return rv;
1148 #endif /* NO_DISKLABEL && NO_GPT */
1149 }
1150
1151 #ifndef NO_RAIDFRAME
1152 int
1153 biosdisk_read_raidframe(int biosdev, daddr_t offset,
1154 struct RF_ComponentLabel_s *label)
1155 {
1156 #if defined(NO_DISKLABEL) && defined(NO_GPT)
1157 return ENOTSUP;
1158 #else
1159 struct biosdisk *d;
1160 struct biosdisk_extinfo ed;
1161 daddr_t size;
1162 int rv = -1;
1163
1164 /* Look for netbsd partition that is the dos boot one */
1165 d = alloc_biosdisk(biosdev);
1166 if (d == NULL)
1167 goto out;
1168
1169 if (d->ll.type != BIOSDISK_TYPE_HD)
1170 /* No raidframe on floppy and CD */
1171 goto out;
1172
1173 if (set_geometry(&d->ll, &ed) != 0)
1174 goto out;
1175
1176 /* Sanity check values returned from BIOS */
1177 if (ed.sbytes >= 512 &&
1178 (ed.sbytes & (ed.sbytes - 1)) == 0)
1179 d->ll.secsize = ed.sbytes;
1180
1181 offset += (RF_COMPONENT_INFO_OFFSET / d->ll.secsize);
1182 size = roundup(sizeof(*label), d->ll.secsize) / d->ll.secsize;
1183 if (readsects(&d->ll, offset, size, d->buf, 0))
1184 goto out;
1185 memcpy(label, d->buf, sizeof(*label));
1186 rv = 0;
1187 out:
1188 if (d != NULL)
1189 dealloc_biosdisk(d);
1190 return rv;
1191 #endif /* NO_DISKLABEL && NO_GPT */
1192 }
1193 #endif /* NO_RAIDFRAME */
1194
1195 #ifdef _STANDALONE
1196 static void
1197 add_biosdisk_bootinfo(void)
1198 {
1199 if (bootinfo == NULL) {
1200 return;
1201 }
1202 BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
1203 BI_ADD(&bi_wedge, BTINFO_BOOTWEDGE, sizeof(bi_wedge));
1204 return;
1205 }
1206 #endif
1207
1208 #ifndef NO_GPT
1209 static daddr_t
1210 raidframe_part_offset(struct biosdisk *d, int part)
1211 {
1212 struct biosdisk raidframe;
1213 daddr_t rf_offset;
1214 daddr_t rf_size;
1215 int i, candidate;
1216
1217 memset(&raidframe, 0, sizeof(raidframe));
1218 raidframe.ll = d->ll;
1219
1220 rf_offset = d->part[part].offset + RF_PROTECTED_SECTORS;
1221 rf_size = d->part[part].size;
1222 if (read_gpt(&raidframe, rf_offset, rf_size) != 0)
1223 return RF_PROTECTED_SECTORS;
1224
1225 candidate = 0;
1226 for (i = 0; i < BIOSDISKNPART; i++) {
1227 if (raidframe.part[i].size == 0)
1228 continue;
1229 if (raidframe.part[i].fstype == FS_UNUSED)
1230 continue;
1231 #ifndef NO_GPT
1232 if (raidframe.part[i].attr & GPT_ENT_ATTR_BOOTME)
1233 candidate = i;
1234 #endif
1235 }
1236
1237 return RF_PROTECTED_SECTORS + raidframe.part[candidate].offset;
1238 }
1239 #endif
1240
1241 int
1242 biosdisk_open(struct open_file *f, ...)
1243 /* struct open_file *f, int biosdev, int partition */
1244 {
1245 va_list ap;
1246 struct biosdisk *d;
1247 int biosdev;
1248 int partition;
1249 int error = 0;
1250
1251 va_start(ap, f);
1252 biosdev = va_arg(ap, int);
1253 d = alloc_biosdisk(biosdev);
1254 if (d == NULL) {
1255 error = ENXIO;
1256 goto out;
1257 }
1258
1259 partition = va_arg(ap, int);
1260 bi_disk.biosdev = d->ll.dev;
1261 bi_disk.partition = partition;
1262 bi_disk.labelsector = -1;
1263
1264 bi_wedge.biosdev = d->ll.dev;
1265 bi_wedge.matchblk = -1;
1266
1267 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
1268 error = read_partitions(d, 0, 0);
1269 if (error == -1) {
1270 error = 0;
1271 goto nolabel;
1272 }
1273 if (error)
1274 goto out;
1275
1276 if (partition >= BIOSDISKNPART ||
1277 d->part[partition].fstype == FS_UNUSED) {
1278 #ifdef DISK_DEBUG
1279 printf("illegal partition\n");
1280 #endif
1281 error = EPART;
1282 goto out;
1283 }
1284
1285 d->boff = d->part[partition].offset;
1286
1287 if (d->part[partition].fstype == FS_RAID)
1288 #ifndef NO_GPT
1289 d->boff += raidframe_part_offset(d, partition);
1290 #else
1291 d->boff += RF_PROTECTED_SECTORS;
1292 #endif
1293
1294 #ifdef _STANDALONE
1295 bi_wedge.startblk = d->part[partition].offset;
1296 bi_wedge.nblks = d->part[partition].size;
1297 #endif
1298
1299 nolabel:
1300 #endif
1301 #ifdef DISK_DEBUG
1302 printf("partition @%"PRId64"\n", d->boff);
1303 #endif
1304
1305 #ifdef _STANDALONE
1306 add_biosdisk_bootinfo();
1307 #endif
1308
1309 f->f_devdata = d;
1310 out:
1311 va_end(ap);
1312 if (error)
1313 dealloc_biosdisk(d);
1314 return error;
1315 }
1316
1317 #ifndef NO_GPT
1318 static int
1319 biosdisk_find_name(const char *fname, int *biosdev,
1320 daddr_t *offset, daddr_t *size)
1321 {
1322 struct biosdisk *d;
1323 char name[MAXDEVNAME + 1];
1324 char *sep;
1325 #ifndef NO_RAIDFRAME
1326 struct raidframe raidframe[RAIDFRAME_NDEV];
1327 int raidframe_count = 0;
1328 #endif
1329 int i;
1330 int part;
1331 int ret = -1;
1332
1333 /* Strip leadinf NAME= and cut after the coloon included */
1334 strlcpy(name, fname + 5, MAXDEVNAME);
1335 sep = strchr(name, ':');
1336 if (sep)
1337 *sep = '\0';
1338
1339 for (i = 0; i < MAX_BIOSDISKS; i++) {
1340 d = alloc(sizeof(*d));
1341 if (d == NULL) {
1342 printf("Out of memory\n");
1343 goto out;
1344 }
1345
1346 memset(d, 0, sizeof(*d));
1347 d->ll.dev = 0x80 + i; /* hd/cd */
1348 if (set_geometry(&d->ll, NULL))
1349 goto next_disk;
1350
1351 if (d->ll.type != BIOSDISK_TYPE_HD)
1352 goto next_disk;
1353
1354 if (read_partitions(d, 0, 0) != 0)
1355 goto next_disk;
1356
1357 for (part = 0; part < BIOSDISKNPART; part++) {
1358 if (d->part[part].size == 0)
1359 continue;
1360 if (d->part[part].fstype == FS_UNUSED)
1361 continue;
1362 #ifndef NO_RAIDFRAME
1363 if (d->part[part].fstype == FS_RAID) {
1364 raidframe_probe(raidframe,
1365 &raidframe_count, d, part);
1366 /*
1367 * Do not match RAID partition for a name,
1368 * we want to report an inner partition.
1369 */
1370 continue;
1371 }
1372 #endif
1373 if (d->part[part].part_name != NULL &&
1374 strcmp(d->part[part].part_name, name) == 0) {
1375 *biosdev = d->ll.dev;
1376 *offset = d->part[part].offset;
1377 *size = d->part[part].size;
1378 ret = 0;
1379 goto out;
1380 }
1381
1382 }
1383 next_disk:
1384 dealloc_biosdisk(d);
1385 d = NULL;
1386 }
1387
1388 #ifndef NO_RAIDFRAME
1389 for (i = 0; i < raidframe_count; i++) {
1390 int candidate = -1;
1391
1392 if ((d = alloc_biosdisk(raidframe[i].biosdev)) == NULL) {
1393 printf("Out of memory\n");
1394 goto out;
1395 }
1396
1397 if (read_partitions(d,
1398 raidframe[i].offset + RF_PROTECTED_SECTORS,
1399 raidframe[i].size) != 0)
1400 goto next_raidframe;
1401
1402 for (part = 0; part < BIOSDISKNPART; part++) {
1403 bool bootme = d->part[part].attr & GPT_ENT_ATTR_BOOTME;
1404 if (d->part[part].size == 0)
1405 continue;
1406 if (d->part[part].fstype == FS_UNUSED)
1407 continue;
1408
1409 if (d->part[part].part_name != NULL &&
1410 strcmp(d->part[part].part_name, name) == 0) {
1411 *biosdev = raidframe[i].biosdev;
1412 *offset = raidframe[i].offset
1413 + RF_PROTECTED_SECTORS
1414 + d->part[part].offset;
1415 *size = d->part[part].size;
1416 ret = 0;
1417 goto out;
1418 }
1419 if (strcmp(raidframe[i].parent_name, name) == 0) {
1420 if (candidate == -1 || bootme)
1421 candidate = part;
1422 continue;
1423 }
1424 }
1425
1426 if (candidate != -1) {
1427 *biosdev = raidframe[i].biosdev;
1428 *offset = raidframe[i].offset
1429 + RF_PROTECTED_SECTORS
1430 + d->part[candidate].offset;
1431 *size = d->part[candidate].size;
1432 ret = 0;
1433 goto out;
1434 }
1435
1436 next_raidframe:
1437 dealloc_biosdisk(d);
1438 d = NULL;
1439 }
1440 #endif
1441
1442 out:
1443 if (d != NULL)
1444 dealloc_biosdisk(d);
1445
1446 return ret;
1447 }
1448 #endif
1449
1450 #ifndef NO_RAIDFRAME
1451 static int
1452 biosdisk_find_raid(const char *name, int *biosdev,
1453 daddr_t *offset, daddr_t *size)
1454 {
1455 struct biosdisk *d = NULL;
1456 struct raidframe raidframe[RAIDFRAME_NDEV];
1457 int raidframe_count = 0;
1458 int i;
1459 int target_unit = 0;
1460 int target_part;
1461 int part;
1462 int ret = -1;
1463
1464 if (strstr(name, "raid") != name)
1465 goto out;
1466
1467 #define isnum(c) ((c) >= '0' && (c) <= '9')
1468 i = 4; /* skip leading "raid" */
1469 if (!isnum(name[i]))
1470 goto out;
1471 do {
1472 target_unit *= 10;
1473 target_unit += name[i++] - '0';
1474 } while (isnum(name[i]));
1475
1476 #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')
1477
1478 if (!isvalidpart(name[i]))
1479 goto out;
1480 target_part = name[i] - 'a';
1481
1482 for (i = 0; i < MAX_BIOSDISKS; i++) {
1483 d = alloc(sizeof(*d));
1484 if (d == NULL) {
1485 printf("Out of memory\n");
1486 goto out;
1487 }
1488
1489 memset(d, 0, sizeof(*d));
1490 d->ll.dev = 0x80 + i; /* hd/cd */
1491 if (set_geometry(&d->ll, NULL))
1492 goto next_disk;
1493
1494 if (d->ll.type != BIOSDISK_TYPE_HD)
1495 goto next_disk;
1496
1497 if (read_partitions(d, 0, 0) != 0)
1498 goto next_disk;
1499
1500 for (part = 0; part < BIOSDISKNPART; part++) {
1501 if (d->part[part].size == 0)
1502 continue;
1503 if (d->part[part].fstype != FS_RAID)
1504 continue;
1505 raidframe_probe(raidframe,
1506 &raidframe_count, d, part);
1507
1508 }
1509 next_disk:
1510 dealloc_biosdisk(d);
1511 d = NULL;
1512 }
1513
1514 for (i = 0; i < raidframe_count; i++) {
1515 if (raidframe[i].last_unit != target_unit)
1516 continue;
1517
1518 if ((d = alloc_biosdisk(raidframe[i].biosdev)) == NULL) {
1519 printf("Out of memory\n");
1520 goto out;
1521 }
1522
1523 if (read_partitions(d,
1524 raidframe[i].offset + RF_PROTECTED_SECTORS,
1525 raidframe[i].size) != 0)
1526 goto next_raidframe;
1527
1528 for (part = 0; part < BIOSDISKNPART; part++) {
1529 if (d->part[part].size == 0)
1530 continue;
1531 if (d->part[part].fstype == FS_UNUSED)
1532 continue;
1533 if (part == target_part) {
1534 *biosdev = raidframe[i].biosdev;
1535 *offset = raidframe[i].offset
1536 + RF_PROTECTED_SECTORS
1537 + d->part[part].offset;
1538 *size = d->part[part].size;
1539 ret = 0;
1540 goto out;
1541 }
1542 }
1543 next_raidframe:
1544 dealloc_biosdisk(d);
1545 d = NULL;
1546 }
1547 out:
1548 if (d != NULL)
1549 dealloc_biosdisk(d);
1550
1551 return ret;
1552 }
1553 #endif
1554
1555 int
1556 biosdisk_open_name(struct open_file *f, const char *name)
1557 {
1558 #if defined(NO_GPT) && defined(NO_RAIDFRAME)
1559 return ENXIO;
1560 #else
1561 struct biosdisk *d = NULL;
1562 int biosdev;
1563 daddr_t offset;
1564 daddr_t size;
1565 int error = -1;
1566
1567 #ifndef NO_GPT
1568 if (strstr(name, "NAME=") == name)
1569 error = biosdisk_find_name(name, &biosdev, &offset, &size);
1570 #endif
1571 #ifndef NO_RAIDFRAME
1572 if (strstr(name, "raid") == name)
1573 error = biosdisk_find_raid(name, &biosdev, &offset, &size);
1574 #endif
1575
1576 if (error != 0) {
1577 printf("%s not found\n", name);
1578 error = ENXIO;
1579 goto out;
1580 }
1581
1582 d = alloc_biosdisk(biosdev);
1583 if (d == NULL) {
1584 error = ENXIO;
1585 goto out;
1586 }
1587
1588 bi_disk.biosdev = d->ll.dev;
1589 bi_disk.partition = 0;
1590 bi_disk.labelsector = -1;
1591
1592 bi_wedge.biosdev = d->ll.dev;
1593
1594 /*
1595 * If we did not get wedge match info from check_gpt()
1596 * compute it now.
1597 */
1598 if (bi_wedge.matchblk == -1) {
1599 if (readsects(&d->ll, offset, 1, d->buf, 1)) {
1600 #ifdef DISK_DEBUG
1601 printf("Error reading sector at %"PRId64"\n", offset);
1602 #endif
1603 error = EIO;
1604 goto out;
1605 }
1606
1607 bi_wedge.matchblk = offset;
1608 bi_wedge.matchnblks = 1;
1609
1610 md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
1611 }
1612
1613 d->boff = offset;
1614
1615 bi_wedge.startblk = offset;
1616 bi_wedge.nblks = size;
1617
1618 #ifdef _STANDALONE
1619 add_biosdisk_bootinfo();
1620 #endif
1621
1622 f->f_devdata = d;
1623 out:
1624 if (error && d != NULL)
1625 dealloc_biosdisk(d);
1626 return error;
1627 #endif
1628 }
1629
1630
1631
1632 #ifndef LIBSA_NO_FS_CLOSE
1633 int
1634 biosdisk_close(struct open_file *f)
1635 {
1636 struct biosdisk *d = f->f_devdata;
1637
1638 /* let the floppy drive go off */
1639 if (d->ll.type == BIOSDISK_TYPE_FD)
1640 wait_sec(3); /* 2s is enough on all PCs I found */
1641
1642 dealloc_biosdisk(d);
1643 f->f_devdata = NULL;
1644 return 0;
1645 }
1646 #endif
1647
1648 int
1649 biosdisk_ioctl(struct open_file *f, u_long cmd, void *arg)
1650 {
1651 return EIO;
1652 }
1653