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