1 /*        $NetBSD: biosdisk.c,v 1.61 2024/01/06 21:26:43 mlelstv Exp $          */
2 
3 /*
4  * Copyright (c) 1996, 1998
5  *        Matthias Drochner.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*
30  * raw BIOS disk device for libsa.
31  * needs lowlevel parts from bios_disk.S and biosdisk_ll.c
32  * partly from netbsd:sys/arch/i386/boot/disk.c
33  * no bad144 handling!
34  *
35  * A lot of this must match sys/kern/subr_disk_mbr.c
36  */
37 
38 /*
39  * Ported to boot 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
40  *
41  * Mach Operating System
42  * Copyright (c) 1992, 1991 Carnegie Mellon University
43  * All Rights Reserved.
44  *
45  * Permission to use, copy, modify and distribute this software and its
46  * documentation is hereby granted, provided that both the copyright
47  * notice and this permission notice appear in all copies of the
48  * software, derivative works or modified versions, and any portions
49  * thereof, and that both notices appear in supporting documentation.
50  *
51  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
52  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
53  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54  *
55  * Carnegie Mellon requests users of this software to return to
56  *
57  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
58  *  School of Computer Science
59  *  Carnegie Mellon University
60  *  Pittsburgh PA 15213-3890
61  *
62  * any improvements or extensions that they make and grant Carnegie Mellon
63  * the rights to redistribute these changes.
64  */
65 
66 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
67 #define FSTYPENAMES
68 #endif
69 
70 #include <lib/libkern/libkern.h>
71 #include <lib/libsa/stand.h>
72 
73 #include <sys/types.h>
74 #include <sys/md5.h>
75 #include <sys/param.h>
76 #include <sys/disklabel.h>
77 #include <sys/disklabel_gpt.h>
78 #include <sys/uuid.h>
79 
80 #include <fs/cd9660/iso.h>
81 #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           daddr_t         size;
109           char            buf[BIOSDISK_BUFSIZE];
110 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
111           struct biosdisk_partition part[BIOSDISKNPART];
112 #endif
113 };
114 
115 #include <dev/raidframe/raidframevar.h>
116 #define RF_COMPONENT_INFO_OFFSET   16384   /* from sys/dev/raidframe/rf_netbsdkintf.c */
117 #define RF_COMPONENT_LABEL_VERSION     2   /* from <dev/raidframe/rf_raid.h> */
118 
119 #define RAIDFRAME_NDEV 16 /* abitrary limit to 15 raidframe devices */
120 struct raidframe {
121           int       last_unit;
122           int       serial;
123           int       biosdev;
124           int       parent_part;
125 #ifndef NO_GPT
126           char    parent_name[MAXDEVNAME + 1];
127 #endif
128           daddr_t   offset;
129           daddr_t   size;
130 };
131 
132 
133 #ifndef NO_GPT
134 const struct uuid GET_nbsd_raid = GPT_ENT_TYPE_NETBSD_RAIDFRAME;
135 const struct uuid GET_nbsd_ffs = GPT_ENT_TYPE_NETBSD_FFS;
136 const struct uuid GET_nbsd_lfs = GPT_ENT_TYPE_NETBSD_LFS;
137 const struct uuid GET_nbsd_swap = GPT_ENT_TYPE_NETBSD_SWAP;
138 const struct uuid GET_nbsd_ccd = GPT_ENT_TYPE_NETBSD_CCD;
139 const struct uuid GET_nbsd_cgd = GPT_ENT_TYPE_NETBSD_CGD;
140 
141 const struct uuid GET_efi = GPT_ENT_TYPE_EFI;
142 const struct uuid GET_mbr = GPT_ENT_TYPE_MBR;
143 const struct uuid GET_fbsd = GPT_ENT_TYPE_FREEBSD;
144 const struct uuid GET_fbsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
145 const struct uuid GET_fbsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
146 const struct uuid GET_fbsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
147 const struct uuid GET_fbsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
148 const struct uuid GET_ms_rsvd = GPT_ENT_TYPE_MS_RESERVED;
149 const struct uuid GET_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
150 const struct uuid GET_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
151 const struct uuid GET_ms_ldm_data = GPT_ENT_TYPE_MS_LDM_DATA;
152 const struct uuid GET_linux_data = GPT_ENT_TYPE_LINUX_DATA;
153 const struct uuid GET_linux_raid = GPT_ENT_TYPE_LINUX_RAID;
154 const struct uuid GET_linux_swap = GPT_ENT_TYPE_LINUX_SWAP;
155 const struct uuid GET_linux_lvm = GPT_ENT_TYPE_LINUX_LVM;
156 const struct uuid GET_apple_hfs = GPT_ENT_TYPE_APPLE_HFS;
157 const struct uuid GET_apple_ufs = GPT_ENT_TYPE_APPLE_UFS;
158 const struct uuid GET_bios = GPT_ENT_TYPE_BIOS;
159 
160 const struct gpt_part gpt_parts[] = {
161           { &GET_nbsd_raid,   "NetBSD RAID" },
162           { &GET_nbsd_ffs,    "NetBSD FFS" },
163           { &GET_nbsd_lfs,    "NetBSD LFS" },
164           { &GET_nbsd_swap,   "NetBSD Swap" },
165           { &GET_nbsd_ccd,    "NetBSD ccd" },
166           { &GET_nbsd_cgd,    "NetBSD cgd" },
167           { &GET_efi,                   "EFI System" },
168           { &GET_mbr,                   "MBR" },
169           { &GET_fbsd,                  "FreeBSD" },
170           { &GET_fbsd_swap,   "FreeBSD Swap" },
171           { &GET_fbsd_ufs,    "FreeBSD UFS" },
172           { &GET_fbsd_vinum,  "FreeBSD Vinum" },
173           { &GET_fbsd_zfs,    "FreeBSD ZFS" },
174           { &GET_ms_rsvd,               "Microsoft Reserved" },
175           { &GET_ms_basic_data,         "Microsoft Basic data" },
176           { &GET_ms_ldm_metadata,       "Microsoft LDM metadata" },
177           { &GET_ms_ldm_data, "Microsoft LDM data" },
178           { &GET_linux_data,  "Linux data" },
179           { &GET_linux_raid,  "Linux RAID" },
180           { &GET_linux_swap,  "Linux Swap" },
181           { &GET_linux_lvm,   "Linux LVM" },
182           { &GET_apple_hfs,   "Apple HFS" },
183           { &GET_apple_ufs,   "Apple UFS" },
184           { &GET_bios,                  "BIOS Boot (GRUB)" },
185 };
186 #endif /* NO_GPT */
187 
188 struct btinfo_bootdisk bi_disk;
189 struct btinfo_bootwedge bi_wedge;
190 struct btinfo_rootdevice bi_root;
191 
192 #define MBR_PARTS(buf) ((char *)(buf) + offsetof(struct mbr_sector, mbr_parts))
193 
194 #ifndef   devb2cdb
195 #define   devb2cdb(bno)       (((bno) * DEV_BSIZE) / ISO_DEFAULT_BLOCK_SIZE)
196 #endif
197 
198 static void
dealloc_biosdisk(struct biosdisk * d)199 dealloc_biosdisk(struct biosdisk *d)
200 {
201 #ifndef NO_GPT
202           int i;
203 
204           for (i = 0; i < __arraycount(d->part); i++) {
205                     if (d->part[i].part_name != NULL)
206                               dealloc(d->part[i].part_name, BIOSDISK_PART_NAME_LEN);
207           }
208 #endif
209 
210           dealloc(d, sizeof(*d));
211 
212           return;
213 }
214 
215 static struct biosdisk_partition *
copy_biosdisk_part(struct biosdisk * d)216 copy_biosdisk_part(struct biosdisk *d)
217 {
218           struct biosdisk_partition *part;
219 
220           part = alloc(sizeof(d->part));
221           if (part == NULL)
222                     goto out;
223 
224           memcpy(part, d->part, sizeof(d->part));
225 
226 #ifndef NO_GPT
227           int i;
228 
229           for (i = 0; i < __arraycount(d->part); i++) {
230                     if (d->part[i].part_name != NULL) {
231                               part[i].part_name = alloc(BIOSDISK_PART_NAME_LEN);
232                               memcpy(part[i].part_name, d->part[i].part_name,
233                                      BIOSDISK_PART_NAME_LEN);
234                     }
235           }
236 #endif
237 
238 out:
239           return part;
240 }
241 
242 int
biosdisk_strategy(void * devdata,int flag,daddr_t dblk,size_t size,void * buf,size_t * rsize)243 biosdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
244                       void *buf, size_t *rsize)
245 {
246           struct biosdisk *d;
247           int blks, frag;
248 
249           if (flag != F_READ)
250                     return EROFS;
251 
252           d = (struct biosdisk *) devdata;
253 
254           if (d->ll.type == BIOSDISK_TYPE_CD)
255                     dblk = devb2cdb(dblk);
256 
257           dblk += d->boff;
258 
259           blks = size / d->ll.secsize;
260           if (blks && readsects(&d->ll, dblk, blks, buf, 0)) {
261                     if (rsize)
262                               *rsize = 0;
263                     return EIO;
264           }
265 
266           /* needed for CD */
267           frag = size % d->ll.secsize;
268           if (frag) {
269                     if (readsects(&d->ll, dblk + blks, 1, d->buf, 0)) {
270                               if (rsize)
271                                         *rsize = blks * d->ll.secsize;
272                               return EIO;
273                     }
274                     memcpy(buf + blks * d->ll.secsize, d->buf, frag);
275           }
276 
277           if (rsize)
278                     *rsize = size;
279           return 0;
280 }
281 
282 static struct biosdisk *
alloc_biosdisk(int biosdev)283 alloc_biosdisk(int biosdev)
284 {
285           struct biosdisk *d;
286 
287           d = alloc(sizeof(*d));
288           if (d == NULL)
289                     return NULL;
290           memset(d, 0, sizeof(*d));
291 
292           d->ll.dev = biosdev;
293           if (set_geometry(&d->ll, NULL)) {
294 #ifdef DISK_DEBUG
295                     printf("no geometry information\n");
296 #endif
297                     dealloc_biosdisk(d);
298                     return NULL;
299           }
300           return d;
301 }
302 
303 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
304 static void
md5(void * hash,const void * data,size_t len)305 md5(void *hash, const void *data, size_t len)
306 {
307           MD5_CTX ctx;
308 
309           MD5Init(&ctx);
310           MD5Update(&ctx, data, len);
311           MD5Final(hash, &ctx);
312 
313           return;
314 }
315 #endif
316 
317 #ifndef NO_GPT
318 bool
guid_is_nil(const struct uuid * u)319 guid_is_nil(const struct uuid *u)
320 {
321           static const struct uuid nil = { .time_low = 0 };
322           return (memcmp(u, &nil, sizeof(*u)) == 0 ? true : false);
323 }
324 
325 bool
guid_is_equal(const struct uuid * a,const struct uuid * b)326 guid_is_equal(const struct uuid *a, const struct uuid *b)
327 {
328           return (memcmp(a, b, sizeof(*a)) == 0 ? true : false);
329 }
330 
331 static void
part_name_utf8(const uint16_t * utf16_src,size_t utf16_srclen,char * utf8_dst,size_t utf8_dstlen)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 
354 static int
check_gpt(struct biosdisk * d,daddr_t rf_offset,daddr_t sector)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
read_gpt(struct biosdisk * d,daddr_t rf_offset,daddr_t rf_size)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
ingest_label(struct biosdisk * d,struct disklabel * lp)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
check_label(struct biosdisk * d,daddr_t sector)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
read_minix_subp(struct biosdisk * d,struct disklabel * dflt_lbl,int this_ext,daddr_t sector)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
check_cd9660(struct biosdisk * d)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
read_label(struct biosdisk * d,daddr_t offset)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           d->size = 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
read_partitions(struct biosdisk * d,daddr_t offset,daddr_t size)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
raidframe_probe(struct raidframe * raidframe,int * raidframe_count,struct biosdisk * d,int part)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
biosdisk_probe(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
biosdisk_findpartition(int biosdev,daddr_t sector,int * partition,const char ** part_name)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
biosdisk_readpartition(int biosdev,daddr_t offset,daddr_t size,struct biosdisk_partition ** partpp,int * rnum)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
biosdisk_read_raidframe(int biosdev,daddr_t offset,struct RF_ComponentLabel_s * label)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
add_biosdisk_bootinfo(void)1199 add_biosdisk_bootinfo(void)
1200 {
1201 #ifndef EFIBOOT
1202           if (bootinfo == NULL) {
1203                     return;
1204           }
1205           BI_ADD(&bi_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
1206           BI_ADD(&bi_wedge, BTINFO_BOOTWEDGE, sizeof(bi_wedge));
1207 #endif
1208           return;
1209 }
1210 #endif
1211 
1212 #ifndef NO_GPT
1213 static void
raidframe_part_offset(struct biosdisk * d,int part)1214 raidframe_part_offset(struct biosdisk *d, int part)
1215 {
1216           struct biosdisk raidframe;
1217           daddr_t rf_offset;
1218           daddr_t rf_size;
1219           int i, candidate;
1220 
1221           memset(&raidframe, 0, sizeof(raidframe));
1222           raidframe.ll = d->ll;
1223 
1224           rf_offset = d->part[part].offset + RF_PROTECTED_SECTORS;
1225           rf_size = d->part[part].size;
1226           if (read_gpt(&raidframe, rf_offset, rf_size) != 0) {
1227                     d->boff += RF_PROTECTED_SECTORS;
1228                     return;
1229           }
1230 
1231           candidate = 0;
1232           for (i = 0; i < BIOSDISKNPART; i++) {
1233                     if (raidframe.part[i].size == 0)
1234                               continue;
1235                     if (raidframe.part[i].fstype == FS_UNUSED)
1236                               continue;
1237 #ifndef NO_GPT
1238                     if (raidframe.part[i].attr & GPT_ENT_ATTR_BOOTME) {
1239                               candidate = i;
1240                               break;
1241                     }
1242 #endif
1243                     if (raidframe.part[i].fstype == FS_BSDFFS ||
1244                         raidframe.part[i].fstype == FS_BSDLFS) {
1245                               if (candidate == 0)
1246                                         candidate = i;
1247                     }
1248           }
1249 
1250           d->boff += RF_PROTECTED_SECTORS + raidframe.part[candidate].offset;
1251           d->size = raidframe.part[candidate].size;
1252 }
1253 #endif
1254 
1255 int
biosdisk_open(struct open_file * f,...)1256 biosdisk_open(struct open_file *f, ...)
1257 /* struct open_file *f, int biosdev, int partition */
1258 {
1259           va_list ap;
1260           struct biosdisk *d;
1261           int biosdev;
1262           int partition;
1263           int error = 0;
1264 
1265           va_start(ap, f);
1266           biosdev = va_arg(ap, int);
1267           d = alloc_biosdisk(biosdev);
1268           if (d == NULL) {
1269                     error = ENXIO;
1270                     goto out;
1271           }
1272 
1273           partition = va_arg(ap, int);
1274           bi_disk.biosdev = d->ll.dev;
1275           bi_disk.partition = partition;
1276           bi_disk.labelsector = -1;
1277 
1278           bi_wedge.biosdev = d->ll.dev;
1279           bi_wedge.matchblk = -1;
1280 
1281 #if !defined(NO_DISKLABEL) || !defined(NO_GPT)
1282           error = read_partitions(d, 0, 0);
1283           if (error == -1) {
1284                     error = 0;
1285                     goto nolabel;
1286           }
1287           if (error)
1288                     goto out;
1289 
1290           if (partition >= BIOSDISKNPART ||
1291               d->part[partition].fstype == FS_UNUSED) {
1292 #ifdef DISK_DEBUG
1293                     printf("illegal partition\n");
1294 #endif
1295                     error = EPART;
1296                     goto out;
1297           }
1298 
1299           d->boff = d->part[partition].offset;
1300           d->size = d->part[partition].size;
1301 
1302           if (d->part[partition].fstype == FS_RAID)
1303 #ifndef NO_GPT
1304                     raidframe_part_offset(d, partition);
1305 #else
1306                     d->boff += RF_PROTECTED_SECTORS;
1307 #endif
1308 
1309 #ifdef _STANDALONE
1310           bi_wedge.startblk = d->boff;
1311           bi_wedge.nblks = d->size;
1312 #endif
1313 
1314 nolabel:
1315 #endif
1316 #ifdef DISK_DEBUG
1317           printf("partition @%"PRId64"\n", d->boff);
1318 #endif
1319 
1320 #ifdef _STANDALONE
1321           add_biosdisk_bootinfo();
1322 #endif
1323 
1324           f->f_devdata = d;
1325 out:
1326         va_end(ap);
1327           if (error)
1328                     dealloc_biosdisk(d);
1329           return error;
1330 }
1331 
1332 #ifndef NO_GPT
1333 static int
biosdisk_find_name(const char * fname,int * biosdev,daddr_t * offset,daddr_t * size)1334 biosdisk_find_name(const char *fname, int *biosdev,
1335                        daddr_t *offset, daddr_t *size)
1336 {
1337           struct biosdisk *d;
1338           char name[MAXDEVNAME + 1];
1339           char *sep;
1340 #ifndef NO_RAIDFRAME
1341           struct raidframe raidframe[RAIDFRAME_NDEV];
1342           int raidframe_count = 0;
1343 #endif
1344           int i;
1345           int part;
1346           int ret = -1;
1347 
1348           /* Strip leadinf NAME= and cut after the coloon included */
1349           strlcpy(name, fname + 5, MAXDEVNAME);
1350           sep = strchr(name, ':');
1351           if (sep)
1352                     *sep = '\0';
1353 
1354           for (i = 0; i < MAX_BIOSDISKS; i++) {
1355                     d = alloc(sizeof(*d));
1356                     if (d == NULL) {
1357                               printf("Out of memory\n");
1358                               goto out;
1359                     }
1360 
1361                     memset(d, 0, sizeof(*d));
1362                     d->ll.dev = 0x80 + i;                             /* hd/cd */
1363                     if (set_geometry(&d->ll, NULL))
1364                               goto next_disk;
1365 
1366                     if (d->ll.type != BIOSDISK_TYPE_HD)
1367                               goto next_disk;
1368 
1369                     if (read_partitions(d, 0, 0) != 0)
1370                               goto next_disk;
1371 
1372                     for (part = 0; part < BIOSDISKNPART; part++) {
1373                               if (d->part[part].size == 0)
1374                                         continue;
1375                               if (d->part[part].fstype == FS_UNUSED)
1376                                         continue;
1377 #ifndef NO_RAIDFRAME
1378                               if (d->part[part].fstype == FS_RAID) {
1379                                         raidframe_probe(raidframe,
1380                                                             &raidframe_count, d, part);
1381                                         /*
1382                                          * Do not match RAID partition for a name,
1383                                          * we want to report an inner partition.
1384                                          */
1385                                         continue;
1386                               }
1387 #endif
1388                               if (d->part[part].part_name != NULL &&
1389                                   strcmp(d->part[part].part_name, name) == 0) {
1390                                         *biosdev = d->ll.dev;
1391                                         *offset = d->part[part].offset;
1392                                         *size = d->part[part].size;
1393                                         ret = 0;
1394                                         goto out;
1395                               }
1396 
1397                     }
1398 next_disk:
1399                     dealloc_biosdisk(d);
1400                     d = NULL;
1401           }
1402 
1403 #ifndef NO_RAIDFRAME
1404           for (i = 0; i < raidframe_count; i++) {
1405                     int first_bootme = -1;
1406                     int first_ffs = -1;
1407                     int candidate = -1;
1408 
1409                     if ((d = alloc_biosdisk(raidframe[i].biosdev)) == NULL) {
1410                               printf("Out of memory\n");
1411                               goto out;
1412                     }
1413 
1414                     if (read_partitions(d,
1415                         raidframe[i].offset + RF_PROTECTED_SECTORS,
1416                         raidframe[i].size) != 0)
1417                               goto next_raidframe;
1418 
1419                     for (part = 0; part < BIOSDISKNPART; part++) {
1420                               if (d->part[part].size == 0)
1421                                         continue;
1422                               if (d->part[part].fstype == FS_UNUSED)
1423                                         continue;
1424 
1425                               if (first_bootme == -1 &&
1426                                   d->part[part].attr & GPT_ENT_ATTR_BOOTME)
1427                                         first_bootme = part;
1428 
1429                               if (first_ffs == -1 &&
1430                                   (d->part[part].fstype == FS_BSDFFS ||
1431                                    d->part[part].fstype == FS_BSDLFS))
1432                                         first_ffs = part;
1433 
1434                               if (d->part[part].part_name != NULL &&
1435                                   strcmp(d->part[part].part_name, name) == 0) {
1436                                         *biosdev = raidframe[i].biosdev;
1437                                         *offset = raidframe[i].offset
1438                                                   + RF_PROTECTED_SECTORS
1439                                                   + d->part[part].offset;
1440                                         *size = d->part[part].size;
1441                                         ret = 0;
1442                                         goto out;
1443                               }
1444                     }
1445 
1446                     if (strcmp(raidframe[i].parent_name, name) == 0) {
1447                               if (first_bootme != -1)
1448                                         candidate = first_bootme;
1449                               else if (first_ffs != -1)
1450                                         candidate = first_ffs;
1451                     }
1452 
1453                     if (candidate != -1) {
1454                               *biosdev = raidframe[i].biosdev;
1455                               *offset = raidframe[i].offset
1456                                         + RF_PROTECTED_SECTORS
1457                                         + d->part[candidate].offset;
1458                               *size = d->part[candidate].size;
1459                               ret = 0;
1460                               goto out;
1461                     }
1462 
1463 next_raidframe:
1464                     dealloc_biosdisk(d);
1465                     d = NULL;
1466           }
1467 #endif
1468 
1469 out:
1470           if (d != NULL)
1471                     dealloc_biosdisk(d);
1472 
1473           return ret;
1474 }
1475 #endif
1476 
1477 #ifndef NO_RAIDFRAME
1478 static int
biosdisk_find_raid(const char * name,int * biosdev,daddr_t * offset,daddr_t * size)1479 biosdisk_find_raid(const char *name, int *biosdev,
1480                        daddr_t *offset, daddr_t *size)
1481 {
1482           struct biosdisk *d = NULL;
1483           struct raidframe raidframe[RAIDFRAME_NDEV];
1484           int raidframe_count = 0;
1485           int i;
1486           int target_unit = 0;
1487           int target_part;
1488           int part;
1489           int ret = -1;
1490 
1491           if (strstr(name, "raid") != name)
1492                     goto out;
1493 
1494 #define isnum(c) ((c) >= '0' && (c) <= '9')
1495           i = 4; /* skip leading "raid" */
1496           if (!isnum(name[i]))
1497                     goto out;
1498           do {
1499                     target_unit *= 10;
1500                     target_unit += name[i++] - '0';
1501           } while (isnum(name[i]));
1502 
1503 #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z')
1504 
1505           if (!isvalidpart(name[i]))
1506                     goto out;
1507           target_part = name[i] - 'a';
1508 
1509           for (i = 0; i < MAX_BIOSDISKS; i++) {
1510                     d = alloc(sizeof(*d));
1511                     if (d == NULL) {
1512                               printf("Out of memory\n");
1513                               goto out;
1514                     }
1515 
1516                     memset(d, 0, sizeof(*d));
1517                     d->ll.dev = 0x80 + i;                             /* hd/cd */
1518                     if (set_geometry(&d->ll, NULL))
1519                               goto next_disk;
1520 
1521                     if (d->ll.type != BIOSDISK_TYPE_HD)
1522                               goto next_disk;
1523 
1524                     if (read_partitions(d, 0, 0) != 0)
1525                               goto next_disk;
1526 
1527                     for (part = 0; part < BIOSDISKNPART; part++) {
1528                               if (d->part[part].size == 0)
1529                                         continue;
1530                               if (d->part[part].fstype != FS_RAID)
1531                                         continue;
1532                               raidframe_probe(raidframe,
1533                                                   &raidframe_count, d, part);
1534 
1535                     }
1536 next_disk:
1537                     dealloc_biosdisk(d);
1538                     d = NULL;
1539           }
1540 
1541           for (i = 0; i < raidframe_count; i++) {
1542                     int first_bootme = -1;
1543                     int first_ffs = -1;
1544                     int candidate = -1;
1545 
1546                     if (raidframe[i].last_unit != target_unit)
1547                               continue;
1548 
1549                     if ((d = alloc_biosdisk(raidframe[i].biosdev)) == NULL) {
1550                               printf("Out of memory\n");
1551                               goto out;
1552                     }
1553 
1554                     if (read_partitions(d,
1555                         raidframe[i].offset + RF_PROTECTED_SECTORS,
1556                         raidframe[i].size) != 0)
1557                               goto next_raidframe;
1558 
1559                     for (part = 0; part < BIOSDISKNPART; part++) {
1560                               if (d->part[part].size == 0)
1561                                         continue;
1562                               if (d->part[part].fstype == FS_UNUSED)
1563                                         continue;
1564 
1565 #ifndef NO_GPT
1566                               if (first_bootme == -1 &&
1567                                   d->part[part].attr & GPT_ENT_ATTR_BOOTME)
1568                                         first_bootme = part;
1569 #endif
1570 
1571                               if (first_ffs == -1 &&
1572                                   (d->part[part].fstype == FS_BSDFFS ||
1573                                    d->part[part].fstype == FS_BSDLFS))
1574                                         first_ffs = part;
1575 
1576                               if (part == target_part) {
1577                                         *biosdev = raidframe[i].biosdev;
1578                                         *offset = raidframe[i].offset
1579                                                   + RF_PROTECTED_SECTORS
1580                                                   + d->part[part].offset;
1581                                         *size = d->part[part].size;
1582                                         ret = 0;
1583                                         goto out;
1584                               }
1585                     }
1586 
1587                     if (first_bootme != -1)
1588                               candidate = first_bootme;
1589                     else if (first_ffs != -1)
1590                               candidate = first_ffs;
1591 
1592                     if (candidate != -1) {
1593                               *biosdev = raidframe[i].biosdev;
1594                               *offset = raidframe[i].offset
1595                                         + RF_PROTECTED_SECTORS
1596                                         + d->part[candidate].offset;
1597                               *size = d->part[candidate].size;
1598                               ret = 0;
1599                               goto out;
1600                     }
1601 
1602 next_raidframe:
1603                     dealloc_biosdisk(d);
1604                     d = NULL;
1605           }
1606 out:
1607           if (d != NULL)
1608                     dealloc_biosdisk(d);
1609 
1610           return ret;
1611 }
1612 #endif
1613 
1614 int
biosdisk_open_name(struct open_file * f,const char * name)1615 biosdisk_open_name(struct open_file *f, const char *name)
1616 {
1617 #if defined(NO_GPT) && defined(NO_RAIDFRAME)
1618           return ENXIO;
1619 #else
1620           struct biosdisk *d = NULL;
1621           int biosdev;
1622           daddr_t offset;
1623           daddr_t size;
1624           int error = -1;
1625 
1626 #ifndef NO_GPT
1627           if (error && strstr(name, "NAME=") == name)
1628                     error = biosdisk_find_name(name, &biosdev, &offset, &size);
1629 #endif
1630 #ifndef NO_RAIDFRAME
1631           if (error && strstr(name, "raid") == name)
1632                     error = biosdisk_find_raid(name, &biosdev, &offset, &size);
1633 #endif
1634 
1635           if (error != 0) {
1636                     printf("%s not found\n", name);
1637                     error = ENXIO;
1638                     goto out;
1639           }
1640 
1641           d = alloc_biosdisk(biosdev);
1642           if (d == NULL) {
1643                     error = ENXIO;
1644                     goto out;
1645           }
1646 
1647           bi_disk.biosdev = d->ll.dev;
1648           bi_disk.partition = 0;
1649           bi_disk.labelsector = -1;
1650 
1651           bi_wedge.biosdev = d->ll.dev;
1652 
1653           /*
1654            * If we did not get wedge match info from check_gpt()
1655            * compute it now.
1656            */
1657           if (bi_wedge.matchblk == -1) {
1658                     if (readsects(&d->ll, offset, 1, d->buf, 1)) {
1659 #ifdef DISK_DEBUG
1660                               printf("Error reading sector at %"PRId64"\n", offset);
1661 #endif
1662                               error =  EIO;
1663                               goto out;
1664                     }
1665 
1666                     bi_wedge.matchblk = offset;
1667                     bi_wedge.matchnblks = 1;
1668 
1669                     md5(bi_wedge.matchhash, d->buf, d->ll.secsize);
1670           }
1671 
1672           d->boff = offset;
1673 
1674           bi_wedge.startblk = offset;
1675           bi_wedge.nblks = size;
1676 
1677 #ifdef _STANDALONE
1678           add_biosdisk_bootinfo();
1679 #endif
1680 
1681           f->f_devdata = d;
1682 out:
1683           if (error && d != NULL)
1684                     dealloc_biosdisk(d);
1685           return error;
1686 #endif
1687 }
1688 
1689 
1690 
1691 #ifndef LIBSA_NO_FS_CLOSE
1692 int
biosdisk_close(struct open_file * f)1693 biosdisk_close(struct open_file *f)
1694 {
1695           struct biosdisk *d = f->f_devdata;
1696 
1697           /* let the floppy drive go off */
1698           if (d->ll.type == BIOSDISK_TYPE_FD)
1699                     wait_sec(3);        /* 2s is enough on all PCs I found */
1700 
1701           dealloc_biosdisk(d);
1702           f->f_devdata = NULL;
1703           return 0;
1704 }
1705 #endif
1706 
1707 int
biosdisk_ioctl(struct open_file * f,u_long cmd,void * arg)1708 biosdisk_ioctl(struct open_file *f, u_long cmd, void *arg)
1709 {
1710           return EIO;
1711 }
1712