ofdev.c revision 1.13 1 /* $NetBSD: ofdev.c,v 1.13 2003/10/08 04:25:45 lukem 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
37 #include <sys/param.h>
38 #include <sys/disklabel.h>
39 #include <sys/bootblock.h>
40
41 #include <netinet/in.h>
42
43 #include <lib/libsa/stand.h>
44 #include <lib/libsa/cd9660.h>
45 #include <lib/libsa/nfs.h>
46 #include <lib/libsa/ufs.h>
47 #include <lib/libsa/ustarfs.h>
48
49 #include "hfs.h"
50 #include "ofdev.h"
51
52 extern char bootdev[];
53
54 static char *
55 filename(str, ppart)
56 char *str;
57 char *ppart;
58 {
59 char *cp, *lp;
60 char savec;
61 int dhandle;
62 char devtype[16];
63
64 lp = str;
65 devtype[0] = 0;
66 *ppart = 0;
67 for (cp = str; *cp; lp = cp) {
68 /* For each component of the path name... */
69 while (*++cp && *cp != '/')
70 ;
71 savec = *cp;
72 *cp = 0;
73 /* ...look whether there is a device with this name */
74 dhandle = OF_finddevice(str);
75 *cp = savec;
76 if (dhandle == -1) {
77 /*
78 * if not, lp is the delimiter between device and path
79 */
80 /* if the last component was a block device... */
81 if (!strcmp(devtype, "block")) {
82 /* search for arguments */
83 for (cp = lp;
84 --cp >= str && *cp != '/' && *cp != ':';)
85 ;
86 if (cp >= str && *cp == ':') {
87 /* found arguments */
88 for (cp = lp;
89 *--cp != ':' && *cp != ',';)
90 ;
91 if (*++cp >= 'a' &&
92 *cp <= 'a' + MAXPARTITIONS)
93 *ppart = *cp;
94 }
95 }
96 return lp;
97 } else if (OF_getprop(dhandle, "device_type", devtype,
98 sizeof devtype) < 0)
99 devtype[0] = 0;
100 }
101 return 0;
102 }
103
104 static int
105 strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
106 size_t *rsize)
107 {
108 struct of_dev *dev = devdata;
109 u_quad_t pos;
110 int n;
111
112 if (rw != F_READ)
113 return EPERM;
114 if (dev->type != OFDEV_DISK)
115 panic("strategy");
116
117 pos = (u_quad_t)(blk + dev->partoff) * dev->bsize;
118
119 for (;;) {
120 if (OF_seek(dev->handle, pos) < 0)
121 break;
122 n = OF_read(dev->handle, buf, size);
123 if (n == -2)
124 continue;
125 if (n < 0)
126 break;
127 *rsize = n;
128 return 0;
129 }
130 return EIO;
131 }
132
133 static int
134 devclose(of)
135 struct open_file *of;
136 {
137 struct of_dev *op = of->f_devdata;
138
139 if (op->type == OFDEV_NET)
140 net_close(op);
141 OF_call_method("dma-free", op->handle, 2, 0, op->dmabuf, MAXPHYS);
142 OF_close(op->handle);
143 op->handle = -1;
144 }
145
146 static struct devsw devsw[1] = {
147 "OpenFirmware",
148 strategy,
149 (int (*)__P((struct open_file *, ...)))nodev,
150 devclose,
151 noioctl
152 };
153 int ndevs = sizeof devsw / sizeof devsw[0];
154
155 static struct fs_ops file_system_ufs = {
156 ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
157 };
158 static struct fs_ops file_system_hfs = {
159 hfs_open, hfs_close, hfs_read, hfs_write, hfs_seek, hfs_stat
160 };
161 static struct fs_ops file_system_ustarfs = {
162 ustarfs_open, ustarfs_close, ustarfs_read, ustarfs_write, ustarfs_seek,
163 ustarfs_stat
164 };
165 static struct fs_ops file_system_cd9660 = {
166 cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek,
167 cd9660_stat
168 };
169 static struct fs_ops file_system_nfs = {
170 nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
171 };
172
173 struct fs_ops file_system[4];
174 int nfsys;
175
176 static struct of_dev ofdev = {
177 -1,
178 };
179
180 char opened_name[256];
181 int floppyboot;
182
183 static u_long
184 get_long(p)
185 const void *p;
186 {
187 const unsigned char *cp = p;
188
189 return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
190 }
191
192 /*
193 * Find a valid disklabel.
194 */
195 static int
196 search_label(devp, off, buf, lp, off0)
197 struct of_dev *devp;
198 u_long off;
199 u_char *buf;
200 struct disklabel *lp;
201 u_long off0;
202 {
203 size_t read;
204 struct mbr_partition *p;
205 int i;
206 u_long poff;
207 static int recursion;
208
209 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
210 || read != DEV_BSIZE)
211 return ERDLAB;
212
213 if (*(u_int16_t *)&buf[MBR_MAGIC_OFFSET] != sa_htole16(MBR_MAGIC))
214 return ERDLAB;
215
216 if (recursion++ <= 1)
217 off0 += off;
218 for (p = (struct mbr_partition *)(buf + MBR_PART_OFFSET), i = 4;
219 --i >= 0; p++) {
220 if (p->mbrp_type == MBR_PTYPE_NETBSD
221 #ifdef COMPAT_386BSD_MBRPART
222 || (p->mbrp_type == MBR_PTYPE_386BSD &&
223 (printf("WARNING: old BSD partition ID!\n"), 1)
224 /* XXX XXX - libsa printf() is void */ )
225 #endif
226 ) {
227 poff = get_long(&p->mbrp_start) + off0;
228 if (strategy(devp, F_READ, poff + 1,
229 DEV_BSIZE, buf, &read) == 0
230 && read == DEV_BSIZE) {
231 if (!getdisklabel(buf, lp)) {
232 recursion--;
233 return 0;
234 }
235 }
236 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
237 || read != DEV_BSIZE) {
238 recursion--;
239 return ERDLAB;
240 }
241 } else if (p->mbrp_type == MBR_PTYPE_EXT) {
242 poff = get_long(&p->mbrp_start);
243 if (!search_label(devp, poff, buf, lp, off0)) {
244 recursion--;
245 return 0;
246 }
247 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
248 || read != DEV_BSIZE) {
249 recursion--;
250 return ERDLAB;
251 }
252 }
253 }
254 recursion--;
255 return ERDLAB;
256 }
257
258 int
259 devopen(of, name, file)
260 struct open_file *of;
261 const char *name;
262 char **file;
263 {
264 char *cp;
265 char partition;
266 char fname[256];
267 char buf[DEV_BSIZE];
268 struct disklabel label;
269 int handle, part;
270 size_t read;
271 int error = 0;
272
273 if (ofdev.handle != -1)
274 panic("devopen");
275 if (of->f_flags != F_READ)
276 return EPERM;
277 strcpy(fname, name);
278 cp = filename(fname, &partition);
279 if (cp) {
280 strcpy(buf, cp);
281 *cp = 0;
282 }
283 if (!cp || !*buf)
284 return ENOENT;
285 if (!*fname)
286 strcpy(fname, bootdev);
287 strcpy(opened_name, fname);
288 if (partition) {
289 cp = opened_name + strlen(opened_name);
290 *cp++ = ':';
291 *cp++ = partition;
292 *cp = 0;
293 }
294 if (*buf != '/')
295 strcat(opened_name, "/");
296 strcat(opened_name, buf);
297 *file = opened_name + strlen(fname) + 1;
298 if ((handle = OF_finddevice(fname)) == -1)
299 return ENOENT;
300 if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
301 return ENXIO;
302 floppyboot = !strcmp(buf, "floppy");
303 if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
304 return ENXIO;
305 #if 0
306 if (!strcmp(buf, "block"))
307 /*
308 * For block devices, indicate raw partition
309 * (:0 in OpenFirmware)
310 */
311 strcat(fname, ":0");
312 #endif
313 if ((handle = OF_open(fname)) == -1)
314 return ENXIO;
315 memset(&ofdev, 0, sizeof ofdev);
316 ofdev.handle = handle;
317 ofdev.dmabuf = NULL;
318 OF_call_method("dma-alloc", handle, 1, 1, MAXPHYS, &ofdev.dmabuf);
319 if (!strcmp(buf, "block")) {
320 ofdev.type = OFDEV_DISK;
321 ofdev.bsize = DEV_BSIZE;
322 /* First try to find a disklabel without MBR partitions */
323 if (strategy(&ofdev, F_READ,
324 LABELSECTOR, DEV_BSIZE, buf, &read) != 0
325 || read != DEV_BSIZE
326 || getdisklabel(buf, &label)) {
327 /* Else try MBR partitions */
328 error = search_label(&ofdev, 0, buf, &label, 0);
329 if (error && error != ERDLAB)
330 goto bad;
331 }
332
333 if (error == ERDLAB) {
334 if (partition)
335 /*
336 * User specified a parititon,
337 * but there is none
338 */
339 goto bad;
340 /* No, label, just use complete disk */
341 ofdev.partoff = 0;
342 } else {
343 part = partition ? partition - 'a' : 0;
344 ofdev.partoff = label.d_partitions[part].p_offset;
345 }
346
347 of->f_dev = devsw;
348 of->f_devdata = &ofdev;
349 file_system[0] = file_system_ufs;
350 file_system[1] = file_system_ustarfs;
351 file_system[2] = file_system_cd9660;
352 file_system[3] = file_system_hfs;
353 nfsys = 4;
354 return 0;
355 }
356 if (!strcmp(buf, "network")) {
357 ofdev.type = OFDEV_NET;
358 of->f_dev = devsw;
359 of->f_devdata = &ofdev;
360 file_system[0] = file_system_nfs;
361 nfsys = 1;
362 if (error = net_open(&ofdev))
363 goto bad;
364 return 0;
365 }
366 error = EFTYPE;
367 bad:
368 OF_close(handle);
369 ofdev.handle = -1;
370 return error;
371 }
372