1 /* $NetBSD: ofdev.c,v 1.6 2004/11/13 08:13:58 grant Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 5 * Copyright (C) 1995, 1996 TooLs GmbH. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by TooLs GmbH. 19 * 4. The name of TooLs GmbH may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 /* 34 * Device I/O routines using Open Firmware 35 */ 36 #include <sys/param.h> 37 #include <sys/disklabel.h> 38 #ifdef NETBOOT 39 #include <netinet/in.h> 40 #endif 41 42 #include <lib/libsa/stand.h> 43 #include <lib/libsa/ufs.h> 44 #include <lib/libsa/cd9660.h> 45 #ifdef NETBOOT 46 #include <lib/libsa/nfs.h> 47 #endif 48 #include <lib/libkern/libkern.h> 49 50 #include <dev/sun/disklabel.h> 51 #include <dev/raidframe/raidframevar.h> 52 53 #include "ofdev.h" 54 55 extern char bootdev[]; 56 57 /* 58 * This is ugly. A path on a sparc machine is something like this: 59 * 60 * [device] [-<options] [path] [-options] [otherstuff] [-<more options] 61 * 62 */ 63 64 static char * 65 filename(str, ppart) 66 char *str; 67 char *ppart; 68 { 69 char *cp, *lp; 70 char savec; 71 int dhandle; 72 char devtype[16]; 73 74 lp = str; 75 devtype[0] = 0; 76 *ppart = 0; 77 for (cp = str; *cp; lp = cp) { 78 /* For each component of the path name... */ 79 while (*++cp && *cp != '/'); 80 savec = *cp; 81 *cp = 0; 82 /* ...look whether there is a device with this name */ 83 dhandle = OF_finddevice(str); 84 #ifdef NOTDEF_DEBUG 85 printf("filename: OF_finddevice(%s) returned %x\n", 86 str, dhandle); 87 #endif 88 *cp = savec; 89 if (dhandle == -1) { 90 /* 91 * if not, lp is the delimiter between device and 92 * path. if the last component was a block device. 93 */ 94 if (!strcmp(devtype, "block")) { 95 /* search for arguments */ 96 #ifdef NOTDEF_DEBUG 97 printf("filename: hunting for arguments " 98 "in %s\n", str); 99 #endif 100 for (cp = lp; ; ) { 101 cp--; 102 if (cp < str || 103 cp[0] == '/' || 104 (cp[0] == ' ' && (cp+1) != lp && 105 cp[1] == '-')) 106 break; 107 } 108 if (cp >= str && *cp == '-') 109 *cp = 0; /* found arguments, make firmware ignore them */ 110 for (cp = lp; *--cp && *cp != ',' && *cp != ':';); 111 if (*++cp >= 'a' && *cp <= 'a' + MAXPARTITIONS) { 112 *ppart = *cp; 113 *--cp = '\0'; 114 } 115 } 116 #ifdef NOTDEF_DEBUG 117 printf("filename: found %s\n",lp); 118 #endif 119 return lp; 120 } else if (OF_getprop(dhandle, "device_type", devtype, sizeof devtype) < 0) 121 devtype[0] = 0; 122 } 123 #ifdef NOTDEF_DEBUG 124 printf("filename: not found\n",lp); 125 #endif 126 return 0; 127 } 128 129 static int 130 strategy(devdata, rw, blk, size, buf, rsize) 131 void *devdata; 132 int rw; 133 daddr_t blk; 134 size_t size; 135 void *buf; 136 size_t *rsize; 137 { 138 struct of_dev *dev = devdata; 139 u_quad_t pos; 140 int n; 141 142 if (rw != F_READ) 143 return EPERM; 144 if (dev->type != OFDEV_DISK) 145 panic("strategy"); 146 147 #ifdef NON_DEBUG 148 printf("strategy: block %lx, partition offset %lx, blksz %lx\n", 149 (long)blk, (long)dev->partoff, (long)dev->bsize); 150 printf("strategy: seek position should be: %lx\n", 151 (long)((blk + dev->partoff) * dev->bsize)); 152 #endif 153 pos = (u_quad_t)(blk + dev->partoff) * dev->bsize; 154 155 for (;;) { 156 #ifdef NON_DEBUG 157 printf("strategy: seeking to %lx\n", (long)pos); 158 #endif 159 if (OF_seek(dev->handle, pos) < 0) 160 break; 161 #ifdef NON_DEBUG 162 printf("strategy: reading %lx at %p\n", (long)size, buf); 163 #endif 164 n = OF_read(dev->handle, buf, size); 165 if (n == -2) 166 continue; 167 if (n < 0) 168 break; 169 *rsize = n; 170 return 0; 171 } 172 return EIO; 173 } 174 175 static int 176 devclose(of) 177 struct open_file *of; 178 { 179 struct of_dev *op = of->f_devdata; 180 181 #ifdef NETBOOT 182 if (op->type == OFDEV_NET) 183 net_close(op); 184 #endif 185 OF_close(op->handle); 186 op->handle = -1; 187 } 188 189 static struct devsw devsw[1] = { 190 "OpenFirmware", 191 strategy, 192 (int (*)__P((struct open_file *, ...)))nodev, 193 devclose, 194 noioctl 195 }; 196 int ndevs = sizeof devsw / sizeof devsw[0]; 197 198 #ifdef SPARC_BOOT_UFS 199 static struct fs_ops file_system_ufs = { 200 ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat 201 }; 202 #endif 203 #ifdef SPARC_BOOT_HSFS 204 static struct fs_ops file_system_cd9660 = { 205 cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek, 206 cd9660_stat 207 }; 208 #endif 209 #ifdef NETBOOT 210 static struct fs_ops file_system_nfs = { 211 nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat 212 }; 213 #endif 214 215 struct fs_ops file_system[3]; 216 int nfsys; 217 218 static struct of_dev ofdev = { 219 -1, 220 }; 221 222 char opened_name[256]; 223 int floppyboot; 224 225 static u_long 226 get_long(p) 227 const void *p; 228 { 229 const unsigned char *cp = p; 230 231 return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24); 232 } 233 /************************************************************************ 234 * 235 * The rest of this was taken from arch/sparc64/scsi/sun_disklabel.c 236 * and then substantially rewritten by Gordon W. Ross 237 * 238 ************************************************************************/ 239 240 /* What partition types to assume for Sun disklabels: */ 241 static u_char 242 sun_fstypes[8] = { 243 FS_BSDFFS, /* a */ 244 FS_SWAP, /* b */ 245 FS_OTHER, /* c - whole disk */ 246 FS_BSDFFS, /* d */ 247 FS_BSDFFS, /* e */ 248 FS_BSDFFS, /* f */ 249 FS_BSDFFS, /* g */ 250 FS_BSDFFS, /* h */ 251 }; 252 253 /* 254 * Given a SunOS disk label, set lp to a BSD disk label. 255 * Returns NULL on success, else an error string. 256 * 257 * The BSD label is cleared out before this is called. 258 */ 259 static char * 260 disklabel_sun_to_bsd(cp, lp) 261 char *cp; 262 struct disklabel *lp; 263 { 264 struct sun_disklabel *sl; 265 struct partition *npp; 266 struct sun_dkpart *spp; 267 int i, secpercyl; 268 u_short cksum, *sp1, *sp2; 269 270 sl = (struct sun_disklabel *)cp; 271 272 /* Verify the XOR check. */ 273 sp1 = (u_short *)sl; 274 sp2 = (u_short *)(sl + 1); 275 cksum = 0; 276 while (sp1 < sp2) 277 cksum ^= *sp1++; 278 if (cksum != 0) 279 return("SunOS disk label, bad checksum"); 280 281 /* Format conversion. */ 282 lp->d_magic = DISKMAGIC; 283 lp->d_magic2 = DISKMAGIC; 284 memcpy(lp->d_packname, sl->sl_text, sizeof(lp->d_packname)); 285 286 lp->d_secsize = 512; 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 lp->d_secperunit = secpercyl * sl->sl_ncylinders; 294 295 lp->d_sparespercyl = sl->sl_sparespercyl; 296 lp->d_acylinders = sl->sl_acylinders; 297 lp->d_rpm = sl->sl_rpm; 298 lp->d_interleave = sl->sl_interleave; 299 300 lp->d_npartitions = 8; 301 /* These are as defined in <ufs/ffs/fs.h> */ 302 lp->d_bbsize = 8192; /* XXX */ 303 lp->d_sbsize = 8192; /* XXX */ 304 305 for (i = 0; i < 8; i++) { 306 spp = &sl->sl_part[i]; 307 npp = &lp->d_partitions[i]; 308 npp->p_offset = spp->sdkp_cyloffset * secpercyl; 309 npp->p_size = spp->sdkp_nsectors; 310 #ifdef NOTDEF_DEBUG 311 printf("partition %d start %x size %x\n", i, (int)npp->p_offset, (int)npp->p_size); 312 #endif 313 if (npp->p_size == 0) { 314 npp->p_fstype = FS_UNUSED; 315 } else { 316 npp->p_fstype = sun_fstypes[i]; 317 if (npp->p_fstype == FS_BSDFFS) { 318 /* 319 * The sun label does not store the FFS fields, 320 * so just set them with default values here. 321 */ 322 npp->p_fsize = 1024; 323 npp->p_frag = 8; 324 npp->p_cpg = 16; 325 } 326 } 327 } 328 329 lp->d_checksum = 0; 330 lp->d_checksum = dkcksum(lp); 331 #ifdef NOTDEF_DEBUG 332 printf("disklabel_sun_to_bsd: success!\n"); 333 #endif 334 return (NULL); 335 } 336 337 /* 338 * Find a valid disklabel. 339 */ 340 static char * 341 search_label(devp, off, buf, lp, off0) 342 struct of_dev *devp; 343 u_long off; 344 char *buf; 345 struct disklabel *lp; 346 u_long off0; 347 { 348 size_t read; 349 struct mbr_partition *p; 350 int i; 351 u_long poff; 352 static int recursion; 353 354 struct disklabel *dlp; 355 struct sun_disklabel *slp; 356 int error; 357 358 /* minimal requirements for archtypal disk label */ 359 if (lp->d_secperunit == 0) 360 lp->d_secperunit = 0x1fffffff; 361 lp->d_npartitions = 1; 362 if (lp->d_partitions[0].p_size == 0) 363 lp->d_partitions[0].p_size = 0x1fffffff; 364 lp->d_partitions[0].p_offset = 0; 365 366 if (strategy(devp, F_READ, LABELSECTOR, DEV_BSIZE, buf, &read) 367 || read != DEV_BSIZE) 368 return ("Cannot read label"); 369 /* Check for a NetBSD disk label. */ 370 dlp = (struct disklabel *) (buf + LABELOFFSET); 371 if (dlp->d_magic == DISKMAGIC) { 372 if (dkcksum(dlp)) 373 return ("NetBSD disk label corrupted"); 374 *lp = *dlp; 375 #ifdef NOTDEF_DEBUG 376 printf("search_label: found NetBSD label\n"); 377 #endif 378 return (NULL); 379 } 380 381 /* Check for a Sun disk label (for PROM compatibility). */ 382 slp = (struct sun_disklabel *) buf; 383 if (slp->sl_magic == SUN_DKMAGIC) 384 return (disklabel_sun_to_bsd(buf, lp)); 385 386 387 bzero(buf, sizeof(buf)); 388 return ("no disk label"); 389 } 390 391 int 392 devopen(of, name, file) 393 struct open_file *of; 394 const char *name; 395 char **file; 396 { 397 char *cp; 398 char partition; 399 char fname[256]; 400 char buf[DEV_BSIZE]; 401 struct disklabel label; 402 int handle, part; 403 size_t read; 404 char *errmsg = NULL; 405 int error = 0; 406 407 if (ofdev.handle != -1) 408 panic("devopen"); 409 if (of->f_flags != F_READ) 410 return EPERM; 411 #ifdef NOTDEF_DEBUG 412 printf("devopen: you want %s\n", name); 413 #endif 414 strcpy(fname, name); 415 cp = filename(fname, &partition); 416 if (cp) { 417 strcpy(buf, cp); 418 *cp = 0; 419 } 420 if (!cp || !*buf) 421 strcpy(buf, DEFAULT_KERNEL); 422 if (!*fname) 423 strcpy(fname, bootdev); 424 strcpy(opened_name, fname); 425 if (partition) { 426 cp = opened_name + strlen(opened_name); 427 *cp++ = ':'; 428 *cp++ = partition; 429 *cp = 0; 430 } 431 *file = opened_name + strlen(opened_name); 432 if (*buf != '/') 433 strcat(opened_name, "/"); 434 strcat(opened_name, buf); 435 #ifdef NOTDEF_DEBUG 436 printf("devopen: trying %s\n", fname); 437 #endif 438 if ((handle = OF_finddevice(fname)) == -1) 439 return ENOENT; 440 #ifdef NOTDEF_DEBUG 441 printf("devopen: found %s\n", fname); 442 #endif 443 if (OF_getprop(handle, "name", buf, sizeof buf) < 0) 444 return ENXIO; 445 #ifdef NOTDEF_DEBUG 446 printf("devopen: %s is called %s\n", fname, buf); 447 #endif 448 floppyboot = !strcmp(buf, "floppy"); 449 if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0) 450 return ENXIO; 451 #ifdef NOTDEF_DEBUG 452 printf("devopen: %s is a %s device\n", fname, buf); 453 #endif 454 #ifdef NOTDEF_DEBUG 455 printf("devopen: opening %s\n", fname); 456 #endif 457 if ((handle = OF_open(fname)) == -1) { 458 #ifdef NOTDEF_DEBUG 459 printf("devopen: open of %s failed\n", fname); 460 #endif 461 return ENXIO; 462 } 463 #ifdef NOTDEF_DEBUG 464 printf("devopen: %s is now open\n", fname); 465 #endif 466 bzero(&ofdev, sizeof ofdev); 467 ofdev.handle = handle; 468 if (!strcmp(buf, "block")) { 469 ofdev.type = OFDEV_DISK; 470 ofdev.bsize = DEV_BSIZE; 471 /* First try to find a disklabel without MBR partitions */ 472 #ifdef NOTDEF_DEBUG 473 printf("devopen: trying to read disklabel\n"); 474 #endif 475 if (strategy(&ofdev, F_READ, 476 LABELSECTOR, DEV_BSIZE, buf, &read) != 0 477 || read != DEV_BSIZE 478 || (errmsg = getdisklabel(buf, &label))) { 479 if (errmsg) printf("devopen: getdisklabel returned %s\n", errmsg); 480 /* Else try MBR partitions */ 481 errmsg = search_label(&ofdev, 0, buf, &label, 0); 482 if (errmsg) { 483 printf("devopen: search_label returned %s\n", errmsg); 484 error = ERDLAB; 485 } 486 if (error && error != ERDLAB) 487 goto bad; 488 } 489 490 if (error == ERDLAB) { 491 if (partition) 492 /* User specified a parititon, but there is none */ 493 goto bad; 494 /* No, label, just use complete disk */ 495 ofdev.partoff = 0; 496 } else { 497 part = partition ? partition - 'a' : 0; 498 ofdev.partoff = label.d_partitions[part].p_offset; 499 #ifdef NOTDEF_DEBUG 500 printf("devopen: setting partition %d offset %x\n", 501 part, ofdev.partoff); 502 #endif 503 if (label.d_partitions[part].p_fstype == FS_RAID) { 504 ofdev.partoff += RF_PROTECTED_SECTORS; 505 #ifdef NOTDEF_DEBUG 506 printf("devopen: found RAID partition, " 507 "adjusting offset to %x\n", ofdev.partoff); 508 #endif 509 } 510 } 511 512 of->f_dev = devsw; 513 of->f_devdata = &ofdev; 514 #ifdef SPARC_BOOT_UFS 515 bcopy(&file_system_ufs, &file_system[nfsys++], sizeof file_system[0]); 516 #endif 517 #ifdef SPARC_BOOT_HSFS 518 bcopy(&file_system_cd9660, &file_system[nfsys++], 519 sizeof file_system[0]); 520 #endif 521 #ifdef NOTDEF_DEBUG 522 printf("devopen: return 0\n"); 523 #endif 524 return 0; 525 } 526 #ifdef NETBOOT 527 if (!strcmp(buf, "network")) { 528 ofdev.type = OFDEV_NET; 529 of->f_dev = devsw; 530 of->f_devdata = &ofdev; 531 bcopy(&file_system_nfs, file_system, sizeof file_system[0]); 532 nfsys = 1; 533 if (error = net_open(&ofdev)) 534 goto bad; 535 return 0; 536 } 537 #endif 538 error = EFTYPE; 539 bad: 540 #ifdef NOTDEF_DEBUG 541 printf("devopen: error %d, cannot open device\n", error); 542 #endif 543 OF_close(handle); 544 ofdev.handle = -1; 545 return error; 546 } 547