1 /* $OpenBSD: ofdev.c,v 1.25 2015/10/01 16:08:20 krw Exp $ */ 2 /* $NetBSD: ofdev.c,v 1.1 2000/08/20 14:58:41 mrg Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 6 * Copyright (C) 1995, 1996 TooLs GmbH. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by TooLs GmbH. 20 * 4. The name of TooLs GmbH may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 /* 35 * Device I/O routines using Open Firmware 36 */ 37 #include <sys/param.h> 38 #include <sys/disklabel.h> 39 #ifdef NETBOOT 40 #include <netinet/in.h> 41 #endif 42 43 #include <lib/libsa/stand.h> 44 #include <lib/libsa/ufs.h> 45 #include <lib/libsa/cd9660.h> 46 #ifdef NETBOOT 47 #include <lib/libsa/nfs.h> 48 #endif 49 50 #ifdef SOFTRAID 51 #include <sys/queue.h> 52 #include <dev/softraidvar.h> 53 #include "disk.h" 54 #endif 55 56 #include <dev/sun/disklabel.h> 57 #include "ofdev.h" 58 59 extern char bootdev[]; 60 61 /* 62 * This is ugly. A path on a sparc machine is something like this: 63 * 64 * [device] [-<options] [path] [-options] [otherstuff] [-<more options] 65 * 66 */ 67 68 static char * 69 filename(char *str, char *ppart) 70 { 71 char *cp, *lp; 72 char savec; 73 int dhandle; 74 char devtype[16]; 75 76 lp = str; 77 devtype[0] = 0; 78 *ppart = 0; 79 for (cp = str; *cp; lp = cp) { 80 /* For each component of the path name... */ 81 while (*++cp && *cp != '/'); 82 savec = *cp; 83 *cp = 0; 84 /* ...look whether there is a device with this name */ 85 dhandle = OF_finddevice(str); 86 DNPRINTF(BOOT_D_OFDEV, "filename: OF_finddevice(%s) says %x\n", 87 str, dhandle); 88 *cp = savec; 89 if (dhandle == -1) { 90 /* if not, lp is the delimiter between device and path */ 91 /* if the last component was a block device... */ 92 if (!strcmp(devtype, "block")) { 93 /* search for arguments */ 94 DNPRINTF(BOOT_D_OFDEV, "filename: hunting for " 95 "arguments in %s\n", str); 96 for (cp = lp; 97 --cp >= str && *cp != '/' && *cp != '-';); 98 if (cp >= str && *cp == '-') { 99 /* found arguments, make firmware ignore them */ 100 *cp = 0; 101 for (cp = lp; *--cp && *cp != ',';); 102 if (*++cp >= 'a' && *cp <= 'a' + MAXPARTITIONS) 103 *ppart = *cp; 104 } 105 } 106 DNPRINTF(BOOT_D_OFDEV, "filename: found %s\n", lp); 107 return lp; 108 } else if (OF_getprop(dhandle, "device_type", devtype, sizeof devtype) < 0) 109 devtype[0] = 0; 110 } 111 DNPRINTF(BOOT_D_OFDEV, "filename: not found\n", lp); 112 return 0; 113 } 114 115 int 116 strategy(void *devdata, int rw, daddr32_t blk, size_t size, void *buf, 117 size_t *rsize) 118 { 119 struct of_dev *dev = devdata; 120 u_quad_t pos; 121 int n; 122 123 if (rw != F_READ) 124 return EPERM; 125 #ifdef SOFTRAID 126 /* Intercept strategy for softraid volumes. */ 127 if (dev->type == OFDEV_SOFTRAID) 128 return sr_strategy(bootdev_dip->sr_vol, rw, 129 blk, size, buf, rsize); 130 #endif 131 if (dev->type != OFDEV_DISK) 132 panic("strategy"); 133 134 DNPRINTF(BOOT_D_OFDEV, "strategy: block %lx, partition offset %lx, " 135 "blksz %lx\n", (long)blk, (long)dev->partoff, (long)dev->bsize); 136 DNPRINTF(BOOT_D_OFDEV, "strategy: seek position should be: %lx\n", 137 (long)((blk + dev->partoff) * dev->bsize)); 138 pos = (u_quad_t)(blk + dev->partoff) * dev->bsize; 139 140 for (;;) { 141 DNPRINTF(BOOT_D_OFDEV, "strategy: seeking to %lx\n", (long)pos); 142 if (OF_seek(dev->handle, pos) < 0) 143 break; 144 DNPRINTF(BOOT_D_OFDEV, "strategy: reading %lx at %p\n", 145 (long)size, buf); 146 n = OF_read(dev->handle, buf, size); 147 if (n == -2) 148 continue; 149 if (n < 0) 150 break; 151 *rsize = n; 152 return 0; 153 } 154 return EIO; 155 } 156 157 static int 158 devclose(struct open_file *of) 159 { 160 struct of_dev *op = of->f_devdata; 161 162 #ifdef NETBOOT 163 if (op->type == OFDEV_NET) 164 net_close(op); 165 #endif 166 #ifdef SOFTRAID 167 if (op->type == OFDEV_SOFTRAID) { 168 op->handle = -1; 169 return 0; 170 } 171 #endif 172 OF_close(op->handle); 173 op->handle = -1; 174 return 0; 175 } 176 177 struct devsw devsw[1] = { 178 "OpenFirmware", 179 strategy, 180 (int (*)(struct open_file *, ...))nodev, 181 devclose, 182 noioctl 183 }; 184 int ndevs = sizeof devsw / sizeof devsw[0]; 185 186 #ifdef SPARC_BOOT_UFS 187 static struct fs_ops file_system_ufs = { 188 ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat 189 }; 190 #endif 191 #ifdef SPARC_BOOT_HSFS 192 static struct fs_ops file_system_cd9660 = { 193 cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek, 194 cd9660_stat 195 }; 196 #endif 197 #ifdef NETBOOT 198 static struct fs_ops file_system_nfs = { 199 nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat 200 }; 201 #endif 202 203 struct fs_ops file_system[3]; 204 int nfsys; 205 206 static struct of_dev ofdev = { 207 -1, 208 }; 209 210 char opened_name[256]; 211 212 static u_long 213 get_long(const void *p) 214 { 215 const unsigned char *cp = p; 216 217 return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24); 218 } 219 220 /************************************************************************ 221 * 222 * The rest of this was taken from arch/sparc64/scsi/sun_disklabel.c 223 * and then substantially rewritten by Gordon W. Ross 224 * 225 ************************************************************************/ 226 227 /* What partition types to assume for Sun disklabels: */ 228 static u_char 229 sun_fstypes[8] = { 230 FS_BSDFFS, /* a */ 231 FS_SWAP, /* b */ 232 FS_OTHER, /* c - whole disk */ 233 FS_BSDFFS, /* d */ 234 FS_BSDFFS, /* e */ 235 FS_BSDFFS, /* f */ 236 FS_BSDFFS, /* g */ 237 FS_BSDFFS, /* h */ 238 }; 239 240 /* 241 * Given a struct sun_disklabel, assume it has an extended partition 242 * table and compute the correct value for sl_xpsum. 243 */ 244 static __inline u_int 245 sun_extended_sum(struct sun_disklabel *sl, void *end) 246 { 247 u_int sum, *xp, *ep; 248 249 xp = (u_int *)&sl->sl_xpmag; 250 ep = (u_int *)end; 251 252 sum = 0; 253 for (; xp < ep; xp++) 254 sum += *xp; 255 return (sum); 256 } 257 258 /* 259 * Given a SunOS disk label, set lp to a BSD disk label. 260 * The BSD label is cleared out before this is called. 261 */ 262 static int 263 disklabel_sun_to_bsd(struct sun_disklabel *sl, struct disklabel *lp) 264 { 265 struct sun_preamble *preamble = (struct sun_preamble *)sl; 266 struct sun_partinfo *ppp; 267 struct sun_dkpart *spp; 268 struct partition *npp; 269 u_short cksum = 0, *sp1, *sp2; 270 int i, secpercyl; 271 272 /* Verify the XOR check. */ 273 sp1 = (u_short *)sl; 274 sp2 = (u_short *)(sl + 1); 275 while (sp1 < sp2) 276 cksum ^= *sp1++; 277 if (cksum != 0) 278 return (EINVAL); /* SunOS disk label, bad checksum */ 279 280 /* Format conversion. */ 281 lp->d_magic = DISKMAGIC; 282 lp->d_magic2 = DISKMAGIC; 283 lp->d_flags = D_VENDOR; 284 memcpy(lp->d_packname, sl->sl_text, sizeof(lp->d_packname)); 285 286 lp->d_secsize = DEV_BSIZE; 287 lp->d_nsectors = sl->sl_nsectors; 288 lp->d_ntracks = sl->sl_ntracks; 289 lp->d_ncylinders = sl->sl_ncylinders; 290 291 secpercyl = sl->sl_nsectors * sl->sl_ntracks; 292 lp->d_secpercyl = secpercyl; 293 if (DL_GETDSIZE(lp) == 0) 294 DL_SETDSIZE(lp, (u_int64_t)secpercyl * sl->sl_ncylinders); 295 lp->d_version = 1; 296 297 memcpy(&lp->d_uid, &sl->sl_uid, sizeof(lp->d_uid)); 298 299 lp->d_acylinders = sl->sl_acylinders; 300 301 lp->d_npartitions = MAXPARTITIONS; 302 /* These are as defined in <ufs/ffs/fs.h> */ 303 lp->d_bbsize = 8192; /* XXX */ 304 lp->d_sbsize = 8192; /* XXX */ 305 306 for (i = 0; i < 8; i++) { 307 spp = &sl->sl_part[i]; 308 npp = &lp->d_partitions[i]; 309 DL_SETPOFFSET(npp, spp->sdkp_cyloffset * secpercyl); 310 DL_SETPSIZE(npp, spp->sdkp_nsectors); 311 if (DL_GETPSIZE(npp) == 0) { 312 npp->p_fstype = FS_UNUSED; 313 } else { 314 npp->p_fstype = sun_fstypes[i]; 315 if (npp->p_fstype == FS_BSDFFS) { 316 /* 317 * The sun label does not store the FFS fields, 318 * so just set them with default values here. 319 */ 320 npp->p_fragblock = 321 DISKLABELV1_FFS_FRAGBLOCK(2048, 8); 322 npp->p_cpg = 16; 323 } 324 } 325 } 326 327 /* Clear "extended" partition info, tentatively */ 328 for (i = 0; i < SUNXPART; i++) { 329 npp = &lp->d_partitions[i+8]; 330 DL_SETPOFFSET(npp, 0); 331 DL_SETPSIZE(npp, 0); 332 npp->p_fstype = FS_UNUSED; 333 } 334 335 /* Check to see if there's an "extended" partition table 336 * SL_XPMAG partitions had checksums up to just before the 337 * (new) sl_types variable, while SL_XPMAGTYP partitions have 338 * checksums up to the just before the (new) sl_xxx1 variable. 339 * Also, disklabels created prior to the addition of sl_uid will 340 * have a checksum to just before the sl_uid variable. 341 */ 342 if ((sl->sl_xpmag == SL_XPMAG && 343 sun_extended_sum(sl, &sl->sl_types) == sl->sl_xpsum) || 344 (sl->sl_xpmag == SL_XPMAGTYP && 345 sun_extended_sum(sl, &sl->sl_uid) == sl->sl_xpsum) || 346 (sl->sl_xpmag == SL_XPMAGTYP && 347 sun_extended_sum(sl, &sl->sl_xxx1) == sl->sl_xpsum)) { 348 /* 349 * There is. Copy over the "extended" partitions. 350 * This code parallels the loop for partitions a-h. 351 */ 352 for (i = 0; i < SUNXPART; i++) { 353 spp = &sl->sl_xpart[i]; 354 npp = &lp->d_partitions[i+8]; 355 DL_SETPOFFSET(npp, spp->sdkp_cyloffset * secpercyl); 356 DL_SETPSIZE(npp, spp->sdkp_nsectors); 357 if (DL_GETPSIZE(npp) == 0) { 358 npp->p_fstype = FS_UNUSED; 359 continue; 360 } 361 npp->p_fstype = FS_BSDFFS; 362 npp->p_fragblock = 363 DISKLABELV1_FFS_FRAGBLOCK(2048, 8); 364 npp->p_cpg = 16; 365 } 366 if (sl->sl_xpmag == SL_XPMAGTYP) { 367 for (i = 0; i < MAXPARTITIONS; i++) { 368 npp = &lp->d_partitions[i]; 369 npp->p_fstype = sl->sl_types[i]; 370 npp->p_fragblock = sl->sl_fragblock[i]; 371 npp->p_cpg = sl->sl_cpg[i]; 372 } 373 } 374 } else if (preamble->sl_nparts <= 8) { 375 /* 376 * A more traditional Sun label. Recognise certain filesystem 377 * types from it, if they are available. 378 */ 379 i = preamble->sl_nparts; 380 if (i == 0) 381 i = 8; 382 383 npp = &lp->d_partitions[i-1]; 384 ppp = &preamble->sl_part[i-1]; 385 for (; i > 0; i--, npp--, ppp--) { 386 if (npp->p_size == 0) 387 continue; 388 if ((ppp->spi_tag == 0) && (ppp->spi_flag == 0)) 389 continue; 390 391 switch (ppp->spi_tag) { 392 case SPTAG_SUNOS_ROOT: 393 case SPTAG_SUNOS_USR: 394 case SPTAG_SUNOS_VAR: 395 case SPTAG_SUNOS_HOME: 396 npp->p_fstype = FS_BSDFFS; 397 npp->p_fragblock = 398 DISKLABELV1_FFS_FRAGBLOCK(2048, 8); 399 npp->p_cpg = 16; 400 break; 401 case SPTAG_LINUX_EXT2: 402 npp->p_fstype = FS_EXT2FS; 403 break; 404 default: 405 /* FS_SWAP for _SUNOS_SWAP and _LINUX_SWAP? */ 406 npp->p_fstype = FS_UNUSED; 407 break; 408 } 409 } 410 } 411 412 lp->d_checksum = 0; 413 lp->d_checksum = dkcksum(lp); 414 DNPRINTF(BOOT_D_OFDEV, "disklabel_sun_to_bsd: success!\n"); 415 return (0); 416 } 417 418 /* 419 * Find a valid disklabel. 420 */ 421 static char * 422 search_label(struct of_dev *devp, u_long off, char *buf, struct disklabel *lp, 423 u_long off0) 424 { 425 size_t read; 426 struct mbr_partition *p; 427 int i; 428 u_long poff; 429 430 struct disklabel *dlp; 431 struct sun_disklabel *slp; 432 int error; 433 434 /* minimal requirements for archetypal disk label */ 435 if (DL_GETDSIZE(lp) == 0) 436 DL_SETDSIZE(lp, 0x1fffffff); 437 lp->d_npartitions = MAXPARTITIONS; 438 if (DL_GETPSIZE(&lp->d_partitions[0]) == 0) 439 DL_SETPSIZE(&lp->d_partitions[0], 0x1fffffff); 440 DL_SETPOFFSET(&lp->d_partitions[0], 0); 441 442 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read) 443 || read != DEV_BSIZE) 444 return ("Cannot read label"); 445 446 /* Check for a disk label. */ 447 dlp = (struct disklabel *) (buf + LABELOFFSET); 448 if (dlp->d_magic == DISKMAGIC) { 449 if (dkcksum(dlp)) 450 return ("corrupt disk label"); 451 *lp = *dlp; 452 DNPRINTF(BOOT_D_OFDEV, "search_label: found disk label\n"); 453 return (NULL); 454 } 455 456 /* Check for a Sun disk label (for PROM compatibility). */ 457 slp = (struct sun_disklabel *)buf; 458 if (slp->sl_magic == SUN_DKMAGIC) { 459 if (disklabel_sun_to_bsd(slp, lp) != 0) 460 return ("corrupt disk label"); 461 DNPRINTF(BOOT_D_OFDEV, "search_label: found disk label\n"); 462 return (NULL); 463 } 464 465 return ("no disk label"); 466 } 467 468 int 469 load_disklabel(struct of_dev *ofdev, struct disklabel *label) 470 { 471 char buf[DEV_BSIZE]; 472 size_t read; 473 int error = 0; 474 char *errmsg = NULL; 475 476 /* First try to find a disklabel without MBR partitions */ 477 DNPRINTF(BOOT_D_OFDEV, "load_disklabel: trying to read disklabel\n"); 478 if (strategy(ofdev, F_READ, 479 LABELSECTOR, DEV_BSIZE, buf, &read) != 0 480 || read != DEV_BSIZE 481 || (errmsg = getdisklabel(buf, label))) { 482 #ifdef BOOT_DEBUG 483 if (errmsg) 484 DNPRINTF(BOOT_D_OFDEV, 485 "load_disklabel: getdisklabel says %s\n", errmsg); 486 #endif 487 /* Else try MBR partitions */ 488 errmsg = search_label(ofdev, LABELSECTOR, buf, 489 label, 0); 490 if (errmsg) { 491 printf("load_disklabel: search_label says %s\n", 492 errmsg); 493 error = ERDLAB; 494 } 495 } 496 497 return (error); 498 } 499 500 int 501 devopen(struct open_file *of, const char *name, char **file) 502 { 503 char *cp; 504 char partition; 505 char fname[256]; 506 char buf[DEV_BSIZE]; 507 struct disklabel label; 508 int handle, part; 509 int error = 0; 510 #ifdef SOFTRAID 511 char volno; 512 #endif 513 514 if (ofdev.handle != -1) 515 panic("devopen"); 516 if (of->f_flags != F_READ) 517 return EPERM; 518 DNPRINTF(BOOT_D_OFDEV, "devopen: you want %s\n", name); 519 if (strlcpy(fname, name, sizeof fname) >= sizeof fname) 520 return ENAMETOOLONG; 521 #ifdef SOFTRAID 522 if (bootdev_dip) { 523 if (fname[0] == 's' && fname[1] == 'r' && 524 '0' <= fname[2] && fname[2] <= '9') { 525 volno = fname[2]; 526 if ('a' <= fname[3] && 527 fname[3] <= 'a' + MAXPARTITIONS) { 528 partition = fname[3]; 529 if (fname[4] == ':') 530 cp = &fname[5]; 531 else 532 cp = &fname[4]; 533 } else { 534 partition = 'a'; 535 cp = &fname[3]; 536 } 537 } else { 538 volno = '0'; 539 partition = 'a'; 540 cp = &fname[0]; 541 } 542 snprintf(buf, sizeof buf, "sr%c:%c", volno, partition); 543 if (strlcpy(opened_name, buf, sizeof opened_name) 544 >= sizeof opened_name) 545 return ENAMETOOLONG; 546 *file = opened_name + strlen(opened_name); 547 if (!*cp) { 548 if (strlcpy(buf, DEFAULT_KERNEL, sizeof buf) 549 >= sizeof buf) 550 return ENAMETOOLONG; 551 } else { 552 if (snprintf(buf, sizeof buf, "%s%s", 553 *cp == '/' ? "" : "/", cp) >= sizeof buf) 554 return ENAMETOOLONG; 555 } 556 if (strlcat(opened_name, buf, sizeof opened_name) >= 557 sizeof opened_name) 558 return ENAMETOOLONG; 559 } else { 560 #endif 561 cp = filename(fname, &partition); 562 if (cp) { 563 if (strlcpy(buf, cp, sizeof buf) >= sizeof buf) 564 return ENAMETOOLONG; 565 *cp = 0; 566 } 567 if (!cp || !*buf) { 568 if (strlcpy(buf, DEFAULT_KERNEL, sizeof buf) 569 >= sizeof buf) 570 return ENAMETOOLONG; 571 } 572 if (!*fname) { 573 if (strlcpy(fname, bootdev, sizeof fname) 574 >= sizeof fname) 575 return ENAMETOOLONG; 576 } 577 if (strlcpy(opened_name, fname, 578 partition ? (sizeof opened_name) - 2 : sizeof opened_name) 579 >= sizeof opened_name) 580 return ENAMETOOLONG; 581 if (partition) { 582 cp = opened_name + strlen(opened_name); 583 *cp++ = ':'; 584 *cp++ = partition; 585 *cp = 0; 586 } 587 if (*buf != '/') { 588 if (strlcat(opened_name, "/", sizeof opened_name) >= 589 sizeof opened_name) 590 return ENAMETOOLONG; 591 } 592 if (strlcat(opened_name, buf, sizeof opened_name) >= 593 sizeof opened_name) 594 return ENAMETOOLONG; 595 *file = opened_name + strlen(fname) + 1; 596 #ifdef SOFTRAID 597 } 598 #endif 599 DNPRINTF(BOOT_D_OFDEV, "devopen: trying %s\n", fname); 600 #ifdef SOFTRAID 601 if (bootdev_dip) { 602 /* Redirect to the softraid boot volume. */ 603 struct partition *pp; 604 605 bzero(&ofdev, sizeof ofdev); 606 ofdev.type = OFDEV_SOFTRAID; 607 608 if (partition) { 609 if (partition < 'a' || 610 partition >= 'a' + MAXPARTITIONS) { 611 printf("invalid partition '%c'\n", partition); 612 return EINVAL; 613 } 614 part = partition - 'a'; 615 pp = &bootdev_dip->disklabel.d_partitions[part]; 616 if (pp->p_fstype == FS_UNUSED || pp->p_size == 0) { 617 printf("invalid partition '%c'\n", partition); 618 return EINVAL; 619 } 620 bootdev_dip->sr_vol->sbv_part = partition; 621 } else 622 bootdev_dip->sr_vol->sbv_part = 'a'; 623 624 of->f_dev = devsw; 625 of->f_devdata = &ofdev; 626 627 #ifdef SPARC_BOOT_UFS 628 bcopy(&file_system_ufs, &file_system[nfsys++], sizeof file_system[0]); 629 #else 630 #error "-DSOFTRAID requires -DSPARC_BOOT_UFS" 631 #endif 632 return 0; 633 } 634 #endif 635 if ((handle = OF_finddevice(fname)) == -1) 636 return ENOENT; 637 DNPRINTF(BOOT_D_OFDEV, "devopen: found %s\n", fname); 638 if (OF_getprop(handle, "name", buf, sizeof buf) < 0) 639 return ENXIO; 640 DNPRINTF(BOOT_D_OFDEV, "devopen: %s is called %s\n", fname, buf); 641 if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0) 642 return ENXIO; 643 DNPRINTF(BOOT_D_OFDEV, "devopen: %s is a %s device\n", fname, buf); 644 DNPRINTF(BOOT_D_OFDEV, "devopen: opening %s\n", fname); 645 if ((handle = OF_open(fname)) == -1) { 646 DNPRINTF(BOOT_D_OFDEV, "devopen: open of %s failed\n", fname); 647 return ENXIO; 648 } 649 DNPRINTF(BOOT_D_OFDEV, "devopen: %s is now open\n", fname); 650 bzero(&ofdev, sizeof ofdev); 651 ofdev.handle = handle; 652 ofdev.type = OFDEV_DISK; 653 ofdev.bsize = DEV_BSIZE; 654 if (!strcmp(buf, "block")) { 655 error = load_disklabel(&ofdev, &label); 656 if (error && error != ERDLAB) 657 goto bad; 658 else if (error == ERDLAB) { 659 if (partition) 660 /* User specified a parititon, but there is none */ 661 goto bad; 662 /* No, label, just use complete disk */ 663 ofdev.partoff = 0; 664 } else { 665 part = partition ? partition - 'a' : 0; 666 ofdev.partoff = label.d_partitions[part].p_offset; 667 DNPRINTF(BOOT_D_OFDEV, "devopen: setting partition %d " 668 "offset %x\n", part, ofdev.partoff); 669 } 670 671 of->f_dev = devsw; 672 of->f_devdata = &ofdev; 673 #ifdef SPARC_BOOT_UFS 674 bcopy(&file_system_ufs, &file_system[nfsys++], sizeof file_system[0]); 675 #endif 676 #ifdef SPARC_BOOT_HSFS 677 bcopy(&file_system_cd9660, &file_system[nfsys++], 678 sizeof file_system[0]); 679 #endif 680 DNPRINTF(BOOT_D_OFDEV, "devopen: return 0\n"); 681 return 0; 682 } 683 #ifdef NETBOOT 684 if (!strcmp(buf, "network")) { 685 ofdev.type = OFDEV_NET; 686 of->f_dev = devsw; 687 of->f_devdata = &ofdev; 688 bcopy(&file_system_nfs, file_system, sizeof file_system[0]); 689 nfsys = 1; 690 if (error = net_open(&ofdev)) 691 goto bad; 692 return 0; 693 } 694 #endif 695 error = EFTYPE; 696 bad: 697 DNPRINTF(BOOT_D_OFDEV, "devopen: error %d, cannot open device\n", 698 error); 699 OF_close(handle); 700 ofdev.handle = -1; 701 return error; 702 } 703