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