1 /* $NetBSD: ofdev.c,v 1.11 2006/05/11 00:48:44 mrg 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 <machine/promlib.h> 54 55 #include "ofdev.h" 56 57 extern char bootdev[]; 58 59 /* 60 * This is ugly. A path on a sparc machine is something like this: 61 * 62 * [device] [-<options] [path] [-options] [otherstuff] [-<more options] 63 * 64 */ 65 66 static char * 67 filename(str, ppart) 68 char *str; 69 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 = prom_finddevice(str); 86 #ifdef NOTDEF_DEBUG 87 printf("filename: prom_finddevice(%s) returned %x\n", 88 str, dhandle); 89 #endif 90 *cp = savec; 91 if (dhandle == -1) { 92 /* 93 * if not, lp is the delimiter between device and 94 * path. if the last component was a block device. 95 */ 96 if (!strcmp(devtype, "block")) { 97 /* search for arguments */ 98 #ifdef NOTDEF_DEBUG 99 printf("filename: hunting for arguments " 100 "in %s\n", str); 101 #endif 102 for (cp = lp; ; ) { 103 cp--; 104 if (cp < str || 105 cp[0] == '/' || 106 (cp[0] == ' ' && (cp+1) != lp && 107 cp[1] == '-')) 108 break; 109 } 110 if (cp >= str && *cp == '-') 111 *cp = 0; /* found arguments, make firmware ignore them */ 112 for (cp = lp; *--cp && *cp != ',' && *cp != ':';); 113 if (*++cp >= 'a' && *cp <= 'a' + MAXPARTITIONS) { 114 *ppart = *cp; 115 *--cp = '\0'; 116 } 117 } 118 #ifdef NOTDEF_DEBUG 119 printf("filename: found %s\n",lp); 120 #endif 121 return lp; 122 } else if (_prom_getprop(dhandle, "device_type", devtype, sizeof devtype) < 0) 123 devtype[0] = 0; 124 } 125 #ifdef NOTDEF_DEBUG 126 printf("filename: not found\n",lp); 127 #endif 128 return 0; 129 } 130 131 static int 132 strategy(devdata, rw, blk, size, buf, rsize) 133 void *devdata; 134 int rw; 135 daddr_t blk; 136 size_t size; 137 void *buf; 138 size_t *rsize; 139 { 140 struct of_dev *dev = devdata; 141 u_quad_t pos; 142 int n; 143 144 if (rw != F_READ) 145 return EPERM; 146 if (dev->type != OFDEV_DISK) 147 panic("strategy"); 148 149 #ifdef NON_DEBUG 150 printf("strategy: block %lx, partition offset %lx, blksz %lx\n", 151 (long)blk, (long)dev->partoff, (long)dev->bsize); 152 printf("strategy: seek position should be: %lx\n", 153 (long)((blk + dev->partoff) * dev->bsize)); 154 #endif 155 pos = (u_quad_t)(blk + dev->partoff) * dev->bsize; 156 157 for (;;) { 158 #ifdef NON_DEBUG 159 printf("strategy: seeking to %lx\n", (long)pos); 160 #endif 161 if (prom_seek(dev->handle, pos) < 0) 162 break; 163 #ifdef NON_DEBUG 164 printf("strategy: reading %lx at %p\n", (long)size, buf); 165 #endif 166 n = prom_read(dev->handle, buf, size); 167 if (n == -2) 168 continue; 169 if (n < 0) 170 break; 171 *rsize = n; 172 return 0; 173 } 174 return EIO; 175 } 176 177 static int 178 devclose(of) 179 struct open_file *of; 180 { 181 struct of_dev *op = of->f_devdata; 182 183 #ifdef NETBOOT 184 if (op->type == OFDEV_NET) 185 net_close(op); 186 #endif 187 prom_close(op->handle); 188 op->handle = -1; 189 } 190 191 static struct devsw ofdevsw[1] = { 192 "OpenFirmware", 193 strategy, 194 (int (*)__P((struct open_file *, ...)))nodev, 195 devclose, 196 noioctl 197 }; 198 int ndevs = sizeof ofdevsw / sizeof ofdevsw[0]; 199 200 #ifdef SPARC_BOOT_UFS 201 static struct fs_ops file_system_ufs = FS_OPS(ufs); 202 #endif 203 #ifdef SPARC_BOOT_HSFS 204 static struct fs_ops file_system_cd9660 = FS_OPS(cd9660); 205 #endif 206 #ifdef NETBOOT 207 static struct fs_ops file_system_nfs = FS_OPS(nfs); 208 #endif 209 210 struct fs_ops file_system[3]; 211 int nfsys; 212 213 static struct of_dev ofdev = { 214 -1, 215 }; 216 217 char opened_name[256]; 218 int floppyboot; 219 220 static u_long 221 get_long(p) 222 const void *p; 223 { 224 const unsigned char *cp = p; 225 226 return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24); 227 } 228 /************************************************************************ 229 * 230 * The rest of this was taken from arch/sparc64/scsi/sun_disklabel.c 231 * and then substantially rewritten by Gordon W. Ross 232 * 233 ************************************************************************/ 234 235 /* What partition types to assume for Sun disklabels: */ 236 static u_char 237 sun_fstypes[8] = { 238 FS_BSDFFS, /* a */ 239 FS_SWAP, /* b */ 240 FS_OTHER, /* c - whole disk */ 241 FS_BSDFFS, /* d */ 242 FS_BSDFFS, /* e */ 243 FS_BSDFFS, /* f */ 244 FS_BSDFFS, /* g */ 245 FS_BSDFFS, /* h */ 246 }; 247 248 /* 249 * Given a SunOS disk label, set lp to a BSD disk label. 250 * Returns NULL on success, else an error string. 251 * 252 * The BSD label is cleared out before this is called. 253 */ 254 static char * 255 disklabel_sun_to_bsd(cp, lp) 256 char *cp; 257 struct disklabel *lp; 258 { 259 struct sun_disklabel *sl; 260 struct partition *npp; 261 struct sun_dkpart *spp; 262 int i, secpercyl; 263 u_short cksum, *sp1, *sp2; 264 265 sl = (struct sun_disklabel *)cp; 266 267 /* Verify the XOR check. */ 268 sp1 = (u_short *)sl; 269 sp2 = (u_short *)(sl + 1); 270 cksum = 0; 271 while (sp1 < sp2) 272 cksum ^= *sp1++; 273 if (cksum != 0) 274 return("SunOS disk label, bad checksum"); 275 276 /* Format conversion. */ 277 lp->d_magic = DISKMAGIC; 278 lp->d_magic2 = DISKMAGIC; 279 memcpy(lp->d_packname, sl->sl_text, sizeof(lp->d_packname)); 280 281 lp->d_secsize = 512; 282 lp->d_nsectors = sl->sl_nsectors; 283 lp->d_ntracks = sl->sl_ntracks; 284 lp->d_ncylinders = sl->sl_ncylinders; 285 286 secpercyl = sl->sl_nsectors * sl->sl_ntracks; 287 lp->d_secpercyl = secpercyl; 288 lp->d_secperunit = secpercyl * sl->sl_ncylinders; 289 290 lp->d_sparespercyl = sl->sl_sparespercyl; 291 lp->d_acylinders = sl->sl_acylinders; 292 lp->d_rpm = sl->sl_rpm; 293 lp->d_interleave = sl->sl_interleave; 294 295 lp->d_npartitions = 8; 296 /* These are as defined in <ufs/ffs/fs.h> */ 297 lp->d_bbsize = 8192; /* XXX */ 298 lp->d_sbsize = 8192; /* XXX */ 299 300 for (i = 0; i < 8; i++) { 301 spp = &sl->sl_part[i]; 302 npp = &lp->d_partitions[i]; 303 npp->p_offset = spp->sdkp_cyloffset * secpercyl; 304 npp->p_size = spp->sdkp_nsectors; 305 #ifdef NOTDEF_DEBUG 306 printf("partition %d start %x size %x\n", i, (int)npp->p_offset, (int)npp->p_size); 307 #endif 308 if (npp->p_size == 0) { 309 npp->p_fstype = FS_UNUSED; 310 } else { 311 npp->p_fstype = sun_fstypes[i]; 312 if (npp->p_fstype == FS_BSDFFS) { 313 /* 314 * The sun label does not store the FFS fields, 315 * so just set them with default values here. 316 */ 317 npp->p_fsize = 1024; 318 npp->p_frag = 8; 319 npp->p_cpg = 16; 320 } 321 } 322 } 323 324 lp->d_checksum = 0; 325 lp->d_checksum = dkcksum(lp); 326 #ifdef NOTDEF_DEBUG 327 printf("disklabel_sun_to_bsd: success!\n"); 328 #endif 329 return (NULL); 330 } 331 332 /* 333 * Find a valid disklabel. 334 */ 335 static char * 336 search_label(devp, off, buf, lp, off0) 337 struct of_dev *devp; 338 u_long off; 339 char *buf; 340 struct disklabel *lp; 341 u_long off0; 342 { 343 size_t read; 344 struct mbr_partition *p; 345 int i; 346 u_long poff; 347 static int recursion; 348 349 struct disklabel *dlp; 350 struct sun_disklabel *slp; 351 int error; 352 353 /* minimal requirements for archtypal disk label */ 354 if (lp->d_secperunit == 0) 355 lp->d_secperunit = 0x1fffffff; 356 lp->d_npartitions = 1; 357 if (lp->d_partitions[0].p_size == 0) 358 lp->d_partitions[0].p_size = 0x1fffffff; 359 lp->d_partitions[0].p_offset = 0; 360 361 if (strategy(devp, F_READ, LABELSECTOR, DEV_BSIZE, buf, &read) 362 || read != DEV_BSIZE) 363 return ("Cannot read label"); 364 /* Check for a NetBSD disk label. */ 365 dlp = (struct disklabel *) (buf + LABELOFFSET); 366 if (dlp->d_magic == DISKMAGIC) { 367 if (dkcksum(dlp)) 368 return ("NetBSD disk label corrupted"); 369 *lp = *dlp; 370 #ifdef NOTDEF_DEBUG 371 printf("search_label: found NetBSD label\n"); 372 #endif 373 return (NULL); 374 } 375 376 /* Check for a Sun disk label (for PROM compatibility). */ 377 slp = (struct sun_disklabel *) buf; 378 if (slp->sl_magic == SUN_DKMAGIC) 379 return (disklabel_sun_to_bsd(buf, lp)); 380 381 382 bzero(buf, sizeof(buf)); 383 return ("no disk label"); 384 } 385 386 int 387 devopen(of, name, file) 388 struct open_file *of; 389 const char *name; 390 char **file; 391 { 392 char *cp; 393 char partition; 394 char fname[256]; 395 char buf[DEV_BSIZE]; 396 struct disklabel label; 397 int handle, part; 398 size_t read; 399 char *errmsg = NULL; 400 int error = 0; 401 402 if (ofdev.handle != -1) 403 panic("devopen"); 404 if (of->f_flags != F_READ) 405 return EPERM; 406 #ifdef NOTDEF_DEBUG 407 printf("devopen: you want %s\n", name); 408 #endif 409 strcpy(fname, name); 410 cp = filename(fname, &partition); 411 if (cp) { 412 strcpy(buf, cp); 413 *cp = 0; 414 } 415 if (!cp || !*buf) 416 strcpy(buf, DEFAULT_KERNEL); 417 if (!*fname) 418 strcpy(fname, bootdev); 419 strcpy(opened_name, fname); 420 if (partition) { 421 cp = opened_name + strlen(opened_name); 422 *cp++ = ':'; 423 *cp++ = partition; 424 *cp = 0; 425 } 426 *file = opened_name + strlen(opened_name); 427 if (*buf != '/') 428 strcat(opened_name, "/"); 429 strcat(opened_name, buf); 430 #ifdef NOTDEF_DEBUG 431 printf("devopen: trying %s\n", fname); 432 #endif 433 if ((handle = prom_finddevice(fname)) == -1) 434 return ENOENT; 435 #ifdef NOTDEF_DEBUG 436 printf("devopen: found %s\n", fname); 437 #endif 438 if (_prom_getprop(handle, "name", buf, sizeof buf) < 0) 439 return ENXIO; 440 #ifdef NOTDEF_DEBUG 441 printf("devopen: %s is called %s\n", fname, buf); 442 #endif 443 floppyboot = !strcmp(buf, "floppy"); 444 if (_prom_getprop(handle, "device_type", buf, sizeof buf) < 0) 445 return ENXIO; 446 #ifdef NOTDEF_DEBUG 447 printf("devopen: %s is a %s device\n", fname, buf); 448 #endif 449 #ifdef NOTDEF_DEBUG 450 printf("devopen: opening %s\n", fname); 451 #endif 452 if ((handle = prom_open(fname)) == -1) { 453 #ifdef NOTDEF_DEBUG 454 printf("devopen: open of %s failed\n", fname); 455 #endif 456 return ENXIO; 457 } 458 #ifdef NOTDEF_DEBUG 459 printf("devopen: %s is now open\n", fname); 460 #endif 461 bzero(&ofdev, sizeof ofdev); 462 ofdev.handle = handle; 463 if (!strcmp(buf, "block")) { 464 ofdev.type = OFDEV_DISK; 465 ofdev.bsize = DEV_BSIZE; 466 /* First try to find a disklabel without MBR partitions */ 467 #ifdef NOTDEF_DEBUG 468 printf("devopen: trying to read disklabel\n"); 469 #endif 470 if (strategy(&ofdev, F_READ, 471 LABELSECTOR, DEV_BSIZE, buf, &read) != 0 472 || read != DEV_BSIZE 473 || (errmsg = getdisklabel(buf, &label))) { 474 if (errmsg) printf("devopen: getdisklabel returned %s\n", errmsg); 475 /* Else try MBR partitions */ 476 errmsg = search_label(&ofdev, 0, buf, &label, 0); 477 if (errmsg) { 478 printf("devopen: search_label returned %s\n", errmsg); 479 error = ERDLAB; 480 } 481 if (error && error != ERDLAB) 482 goto bad; 483 } 484 485 if (error == ERDLAB) { 486 if (partition) 487 /* User specified a parititon, but there is none */ 488 goto bad; 489 /* No, label, just use complete disk */ 490 ofdev.partoff = 0; 491 } else { 492 part = partition ? partition - 'a' : 0; 493 ofdev.partoff = label.d_partitions[part].p_offset; 494 #ifdef NOTDEF_DEBUG 495 printf("devopen: setting partition %d offset %x\n", 496 part, ofdev.partoff); 497 #endif 498 if (label.d_partitions[part].p_fstype == FS_RAID) { 499 ofdev.partoff += RF_PROTECTED_SECTORS; 500 #ifdef NOTDEF_DEBUG 501 printf("devopen: found RAID partition, " 502 "adjusting offset to %x\n", ofdev.partoff); 503 #endif 504 } 505 } 506 507 of->f_dev = ofdevsw; 508 of->f_devdata = &ofdev; 509 #ifdef SPARC_BOOT_UFS 510 bcopy(&file_system_ufs, &file_system[nfsys++], sizeof file_system[0]); 511 #endif 512 #ifdef SPARC_BOOT_HSFS 513 bcopy(&file_system_cd9660, &file_system[nfsys++], 514 sizeof file_system[0]); 515 #endif 516 #ifdef NOTDEF_DEBUG 517 printf("devopen: return 0\n"); 518 #endif 519 return 0; 520 } 521 #ifdef NETBOOT 522 if (!strcmp(buf, "network")) { 523 ofdev.type = OFDEV_NET; 524 of->f_dev = ofdevsw; 525 of->f_devdata = &ofdev; 526 bcopy(&file_system_nfs, file_system, sizeof file_system[0]); 527 nfsys = 1; 528 if (error = net_open(&ofdev)) 529 goto bad; 530 return 0; 531 } 532 #endif 533 error = EFTYPE; 534 bad: 535 #ifdef NOTDEF_DEBUG 536 printf("devopen: error %d, cannot open device\n", error); 537 #endif 538 prom_close(handle); 539 ofdev.handle = -1; 540 return error; 541 } 542