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