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