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