ofdev.c revision 1.8 1 /* $NetBSD: ofdev.c,v 1.8 2002/03/29 15:15:08 tsutsui 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/disklabel_mbr.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
48 #include "hfs.h"
49 #include "ofdev.h"
50
51 extern char bootdev[];
52
53 static char *
54 filename(str, ppart)
55 char *str;
56 char *ppart;
57 {
58 char *cp, *lp;
59 char savec;
60 int dhandle;
61 char devtype[16];
62
63 lp = str;
64 devtype[0] = 0;
65 *ppart = 0;
66 for (cp = str; *cp; lp = cp) {
67 /* For each component of the path name... */
68 while (*++cp && *cp != '/')
69 ;
70 savec = *cp;
71 *cp = 0;
72 /* ...look whether there is a device with this name */
73 dhandle = OF_finddevice(str);
74 *cp = savec;
75 if (dhandle == -1) {
76 /*
77 * if not, lp is the delimiter between device and path
78 */
79 /* if the last component was a block device... */
80 if (!strcmp(devtype, "block")) {
81 /* search for arguments */
82 for (cp = lp;
83 --cp >= str && *cp != '/' && *cp != ':';)
84 ;
85 if (cp >= str && *cp == ':') {
86 /* found arguments */
87 for (cp = lp;
88 *--cp != ':' && *cp != ',';)
89 ;
90 if (*++cp >= 'a' &&
91 *cp <= 'a' + MAXPARTITIONS)
92 *ppart = *cp;
93 }
94 }
95 return lp;
96 } else if (OF_getprop(dhandle, "device_type", devtype,
97 sizeof devtype) < 0)
98 devtype[0] = 0;
99 }
100 return 0;
101 }
102
103 static int
104 strategy(devdata, rw, blk, size, buf, rsize)
105 void *devdata;
106 int rw;
107 daddr_t blk;
108 size_t size;
109 void *buf;
110 size_t *rsize;
111 {
112 struct of_dev *dev = devdata;
113 u_quad_t pos;
114 int n;
115
116 if (rw != F_READ)
117 return EPERM;
118 if (dev->type != OFDEV_DISK)
119 panic("strategy");
120
121 pos = (u_quad_t)(blk + dev->partoff) * dev->bsize;
122
123 for (;;) {
124 if (OF_seek(dev->handle, pos) < 0)
125 break;
126 n = OF_read(dev->handle, buf, size);
127 if (n == -2)
128 continue;
129 if (n < 0)
130 break;
131 *rsize = n;
132 return 0;
133 }
134 return EIO;
135 }
136
137 static int
138 devclose(of)
139 struct open_file *of;
140 {
141 struct of_dev *op = of->f_devdata;
142
143 if (op->type == OFDEV_NET)
144 net_close(op);
145 OF_call_method("dma-free", op->handle, 2, 0, op->dmabuf, MAXPHYS);
146 OF_close(op->handle);
147 op->handle = -1;
148 }
149
150 static struct devsw devsw[1] = {
151 "OpenFirmware",
152 strategy,
153 (int (*)__P((struct open_file *, ...)))nodev,
154 devclose,
155 noioctl
156 };
157 int ndevs = sizeof devsw / sizeof devsw[0];
158
159 static struct fs_ops file_system_ufs = {
160 ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
161 };
162 static struct fs_ops file_system_hfs = {
163 hfs_open, hfs_close, hfs_read, hfs_write, hfs_seek, hfs_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[3];
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 (buf[510] != 0x55 || buf[511] != 0xaa)
214 return ERDLAB;
215
216 if (recursion++ <= 1)
217 off0 += off;
218 for (p = (struct mbr_partition *)(buf + MBR_PARTOFF), i = 4;
219 --i >= 0; p++) {
220 if (p->mbrp_typ == MBR_PTYPE_NETBSD
221 #ifdef COMPAT_386BSD_MBRPART
222 || (p->mbrp_typ == 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 + LABELSECTOR,
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_typ == 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_cd9660;
351 file_system[2] = file_system_hfs;
352 nfsys = 3;
353 return 0;
354 }
355 if (!strcmp(buf, "network")) {
356 ofdev.type = OFDEV_NET;
357 of->f_dev = devsw;
358 of->f_devdata = &ofdev;
359 file_system[0] = file_system_nfs;
360 nfsys = 1;
361 if (error = net_open(&ofdev))
362 goto bad;
363 return 0;
364 }
365 error = EFTYPE;
366 bad:
367 OF_close(handle);
368 ofdev.handle = -1;
369 return error;
370 }
371