ofdev.c revision 1.22 1 1.22 tsutsui /* $NetBSD: ofdev.c,v 1.22 2009/01/28 15:03:28 tsutsui Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*
4 1.1 tsubai * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 1.1 tsubai * Copyright (C) 1995, 1996 TooLs GmbH.
6 1.1 tsubai * All rights reserved.
7 1.1 tsubai *
8 1.1 tsubai * Redistribution and use in source and binary forms, with or without
9 1.1 tsubai * modification, are permitted provided that the following conditions
10 1.1 tsubai * are met:
11 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
12 1.1 tsubai * notice, this list of conditions and the following disclaimer.
13 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
15 1.1 tsubai * documentation and/or other materials provided with the distribution.
16 1.1 tsubai * 3. All advertising materials mentioning features or use of this software
17 1.1 tsubai * must display the following acknowledgement:
18 1.1 tsubai * This product includes software developed by TooLs GmbH.
19 1.1 tsubai * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.1 tsubai * derived from this software without specific prior written permission.
21 1.1 tsubai *
22 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 tsubai * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 tsubai * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.1 tsubai * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.1 tsubai * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 tsubai * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.1 tsubai * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.1 tsubai * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 tsubai */
33 1.1 tsubai /*
34 1.1 tsubai * Device I/O routines using Open Firmware
35 1.1 tsubai */
36 1.1 tsubai
37 1.14 aymeric #include "ofdev.h"
38 1.17 uwe #include "openfirm.h"
39 1.14 aymeric
40 1.1 tsubai #include <sys/param.h>
41 1.1 tsubai #include <sys/disklabel.h>
42 1.13 lukem #include <sys/bootblock.h>
43 1.1 tsubai
44 1.1 tsubai #include <netinet/in.h>
45 1.1 tsubai
46 1.18 uwe #include <lib/libkern/libkern.h>
47 1.18 uwe
48 1.1 tsubai #include <lib/libsa/stand.h>
49 1.18 uwe #include <lib/libsa/byteorder.h>
50 1.1 tsubai #include <lib/libsa/cd9660.h>
51 1.1 tsubai #include <lib/libsa/nfs.h>
52 1.7 tsubai #include <lib/libsa/ufs.h>
53 1.20 hauke #include <lib/libsa/lfs.h>
54 1.9 tsutsui #include <lib/libsa/ustarfs.h>
55 1.1 tsubai
56 1.7 tsubai #include "hfs.h"
57 1.17 uwe #include "net.h"
58 1.1 tsubai
59 1.22 tsutsui #ifdef DEBUG
60 1.22 tsutsui # define DPRINTF printf
61 1.22 tsutsui #else
62 1.22 tsutsui # define DPRINTF while (0) printf
63 1.22 tsutsui #endif
64 1.1 tsubai
65 1.1 tsubai static int
66 1.12 aymeric strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
67 1.12 aymeric size_t *rsize)
68 1.1 tsubai {
69 1.1 tsubai struct of_dev *dev = devdata;
70 1.1 tsubai u_quad_t pos;
71 1.1 tsubai int n;
72 1.8 tsutsui
73 1.1 tsubai if (rw != F_READ)
74 1.1 tsubai return EPERM;
75 1.1 tsubai if (dev->type != OFDEV_DISK)
76 1.1 tsubai panic("strategy");
77 1.8 tsutsui
78 1.1 tsubai pos = (u_quad_t)(blk + dev->partoff) * dev->bsize;
79 1.8 tsutsui
80 1.1 tsubai for (;;) {
81 1.1 tsubai if (OF_seek(dev->handle, pos) < 0)
82 1.1 tsubai break;
83 1.1 tsubai n = OF_read(dev->handle, buf, size);
84 1.1 tsubai if (n == -2)
85 1.1 tsubai continue;
86 1.1 tsubai if (n < 0)
87 1.1 tsubai break;
88 1.1 tsubai *rsize = n;
89 1.1 tsubai return 0;
90 1.1 tsubai }
91 1.1 tsubai return EIO;
92 1.1 tsubai }
93 1.1 tsubai
94 1.1 tsubai static int
95 1.14 aymeric devopen_dummy(struct open_file *of, ...) {
96 1.14 aymeric return -1;
97 1.14 aymeric }
98 1.14 aymeric
99 1.14 aymeric static int
100 1.14 aymeric devclose(struct open_file *of)
101 1.1 tsubai {
102 1.1 tsubai struct of_dev *op = of->f_devdata;
103 1.8 tsutsui
104 1.1 tsubai if (op->type == OFDEV_NET)
105 1.1 tsubai net_close(op);
106 1.3 tsubai OF_call_method("dma-free", op->handle, 2, 0, op->dmabuf, MAXPHYS);
107 1.1 tsubai OF_close(op->handle);
108 1.1 tsubai op->handle = -1;
109 1.17 uwe return 0;
110 1.1 tsubai }
111 1.1 tsubai
112 1.19 mrg static struct devsw of_devsw[1] = {
113 1.17 uwe { "OpenFirmware", strategy, devopen_dummy, devclose, noioctl }
114 1.1 tsubai };
115 1.19 mrg int ndevs = sizeof of_devsw / sizeof of_devsw[0];
116 1.1 tsubai
117 1.20 hauke static struct fs_ops file_system_ffsv1 = FS_OPS(ffsv1);
118 1.20 hauke static struct fs_ops file_system_ffsv2 = FS_OPS(ffsv2);
119 1.20 hauke static struct fs_ops file_system_lfsv1 = FS_OPS(lfsv1);
120 1.20 hauke static struct fs_ops file_system_lfsv2 = FS_OPS(lfsv2);
121 1.15 junyoung static struct fs_ops file_system_hfs = FS_OPS(hfs);
122 1.15 junyoung static struct fs_ops file_system_ustarfs = FS_OPS(ustarfs);
123 1.15 junyoung static struct fs_ops file_system_cd9660 = FS_OPS(cd9660);
124 1.15 junyoung static struct fs_ops file_system_nfs = FS_OPS(nfs);
125 1.1 tsubai
126 1.20 hauke struct fs_ops file_system[8];
127 1.1 tsubai int nfsys;
128 1.1 tsubai
129 1.1 tsubai static struct of_dev ofdev = {
130 1.1 tsubai -1,
131 1.1 tsubai };
132 1.1 tsubai
133 1.22 tsutsui char opened_name[MAXBOOTPATHLEN];
134 1.1 tsubai
135 1.1 tsubai static u_long
136 1.14 aymeric get_long(const void *p)
137 1.1 tsubai {
138 1.1 tsubai const unsigned char *cp = p;
139 1.8 tsutsui
140 1.1 tsubai return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
141 1.1 tsubai }
142 1.1 tsubai
143 1.1 tsubai /*
144 1.1 tsubai * Find a valid disklabel.
145 1.1 tsubai */
146 1.1 tsubai static int
147 1.21 tsutsui search_label(struct of_dev *devp, u_long off, char *buf, struct disklabel *lp,
148 1.14 aymeric u_long off0)
149 1.1 tsubai {
150 1.17 uwe size_t nread;
151 1.1 tsubai struct mbr_partition *p;
152 1.1 tsubai int i;
153 1.1 tsubai u_long poff;
154 1.1 tsubai static int recursion;
155 1.8 tsutsui
156 1.17 uwe if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &nread)
157 1.17 uwe || nread != DEV_BSIZE)
158 1.1 tsubai return ERDLAB;
159 1.8 tsutsui
160 1.13 lukem if (*(u_int16_t *)&buf[MBR_MAGIC_OFFSET] != sa_htole16(MBR_MAGIC))
161 1.1 tsubai return ERDLAB;
162 1.1 tsubai
163 1.1 tsubai if (recursion++ <= 1)
164 1.1 tsubai off0 += off;
165 1.13 lukem for (p = (struct mbr_partition *)(buf + MBR_PART_OFFSET), i = 4;
166 1.1 tsubai --i >= 0; p++) {
167 1.13 lukem if (p->mbrp_type == MBR_PTYPE_NETBSD
168 1.1 tsubai #ifdef COMPAT_386BSD_MBRPART
169 1.13 lukem || (p->mbrp_type == MBR_PTYPE_386BSD &&
170 1.1 tsubai (printf("WARNING: old BSD partition ID!\n"), 1)
171 1.1 tsubai /* XXX XXX - libsa printf() is void */ )
172 1.1 tsubai #endif
173 1.1 tsubai ) {
174 1.2 tsubai poff = get_long(&p->mbrp_start) + off0;
175 1.10 itojun if (strategy(devp, F_READ, poff + 1,
176 1.17 uwe DEV_BSIZE, buf, &nread) == 0
177 1.17 uwe && nread == DEV_BSIZE) {
178 1.1 tsubai if (!getdisklabel(buf, lp)) {
179 1.1 tsubai recursion--;
180 1.1 tsubai return 0;
181 1.1 tsubai }
182 1.1 tsubai }
183 1.17 uwe if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &nread)
184 1.17 uwe || nread != DEV_BSIZE) {
185 1.1 tsubai recursion--;
186 1.1 tsubai return ERDLAB;
187 1.1 tsubai }
188 1.13 lukem } else if (p->mbrp_type == MBR_PTYPE_EXT) {
189 1.2 tsubai poff = get_long(&p->mbrp_start);
190 1.1 tsubai if (!search_label(devp, poff, buf, lp, off0)) {
191 1.1 tsubai recursion--;
192 1.1 tsubai return 0;
193 1.1 tsubai }
194 1.17 uwe if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &nread)
195 1.17 uwe || nread != DEV_BSIZE) {
196 1.1 tsubai recursion--;
197 1.1 tsubai return ERDLAB;
198 1.1 tsubai }
199 1.1 tsubai }
200 1.1 tsubai }
201 1.1 tsubai recursion--;
202 1.1 tsubai return ERDLAB;
203 1.1 tsubai }
204 1.1 tsubai
205 1.22 tsutsui
206 1.22 tsutsui bool
207 1.22 tsutsui parsefilepath(const char *path, char *devname, char *fname, char *ppart)
208 1.22 tsutsui {
209 1.22 tsutsui char *cp, *lp;
210 1.22 tsutsui char savec;
211 1.22 tsutsui int dhandle;
212 1.22 tsutsui char str[MAXBOOTPATHLEN];
213 1.22 tsutsui char devtype[16];
214 1.22 tsutsui
215 1.22 tsutsui DPRINTF("%s: path = %s\n", __func__, path);
216 1.22 tsutsui
217 1.22 tsutsui devtype[0] = '\0';
218 1.22 tsutsui
219 1.22 tsutsui if (devname != NULL)
220 1.22 tsutsui devname[0] = '\0';
221 1.22 tsutsui if (fname != NULL)
222 1.22 tsutsui fname[0] = '\0';
223 1.22 tsutsui if (ppart != NULL)
224 1.22 tsutsui *ppart = 0;
225 1.22 tsutsui
226 1.22 tsutsui strlcpy(str, path, sizeof(str));
227 1.22 tsutsui lp = str;
228 1.22 tsutsui for (cp = str; *cp != '\0'; lp = cp) {
229 1.22 tsutsui /* For each component of the path name... */
230 1.22 tsutsui while (*++cp != '\0' && *cp != '/')
231 1.22 tsutsui continue;
232 1.22 tsutsui savec = *cp;
233 1.22 tsutsui *cp = '\0';
234 1.22 tsutsui /* ...look whether there is a device with this name */
235 1.22 tsutsui dhandle = OF_finddevice(str);
236 1.22 tsutsui DPRINTF("%s: Checking %s: dhandle = %d\n",
237 1.22 tsutsui __func__, str, dhandle);
238 1.22 tsutsui *cp = savec;
239 1.22 tsutsui if (dhandle != -1) {
240 1.22 tsutsui /*
241 1.22 tsutsui * If it's a vaild device, lp is a delimiter
242 1.22 tsutsui * in the OF device path.
243 1.22 tsutsui */
244 1.22 tsutsui if (OF_getprop(dhandle, "device_type", devtype,
245 1.22 tsutsui sizeof devtype) < 0)
246 1.22 tsutsui devtype[0] = '\0';
247 1.22 tsutsui continue;
248 1.22 tsutsui }
249 1.22 tsutsui
250 1.22 tsutsui /*
251 1.22 tsutsui * If not, lp is the delimiter between OF device path
252 1.22 tsutsui * and the specified filename.
253 1.22 tsutsui */
254 1.22 tsutsui
255 1.22 tsutsui /* Check if the last component was a block device... */
256 1.22 tsutsui if (strcmp(devtype, "block") == 0) {
257 1.22 tsutsui /* search for arguments */
258 1.22 tsutsui for (cp = lp;
259 1.22 tsutsui --cp >= str && *cp != '/' && *cp != ':';)
260 1.22 tsutsui continue;
261 1.22 tsutsui if (cp >= str && *cp == ':') {
262 1.22 tsutsui /* found arguments */
263 1.22 tsutsui for (cp = lp;
264 1.22 tsutsui *--cp != ':' && *cp != ',';)
265 1.22 tsutsui continue;
266 1.22 tsutsui if (*++cp >= 'a' &&
267 1.22 tsutsui *cp <= 'a' + MAXPARTITIONS &&
268 1.22 tsutsui ppart != NULL)
269 1.22 tsutsui *ppart = *cp;
270 1.22 tsutsui }
271 1.22 tsutsui }
272 1.22 tsutsui if (*lp != '\0') {
273 1.22 tsutsui /* set filename */
274 1.22 tsutsui DPRINTF("%s: filename = %s\n", __func__, lp);
275 1.22 tsutsui if (fname != NULL)
276 1.22 tsutsui strcpy(fname, lp);
277 1.22 tsutsui if (str != lp) {
278 1.22 tsutsui /* set device path */
279 1.22 tsutsui *lp = '\0';
280 1.22 tsutsui DPRINTF("%s: device path = %s\n",
281 1.22 tsutsui __func__, str);
282 1.22 tsutsui if (devname != NULL)
283 1.22 tsutsui strcpy(devname, str);
284 1.22 tsutsui }
285 1.22 tsutsui }
286 1.22 tsutsui return true;
287 1.22 tsutsui }
288 1.22 tsutsui
289 1.22 tsutsui DPRINTF("%s: invalid path?\n", __func__);
290 1.22 tsutsui return false;
291 1.22 tsutsui }
292 1.22 tsutsui
293 1.1 tsubai int
294 1.14 aymeric devopen(struct open_file *of, const char *name, char **file)
295 1.1 tsubai {
296 1.1 tsubai char *cp;
297 1.1 tsubai char partition;
298 1.22 tsutsui char devname[MAXBOOTPATHLEN];
299 1.22 tsutsui char filename[MAXBOOTPATHLEN];
300 1.1 tsubai char buf[DEV_BSIZE];
301 1.1 tsubai struct disklabel label;
302 1.1 tsubai int handle, part;
303 1.17 uwe size_t nread;
304 1.1 tsubai int error = 0;
305 1.1 tsubai
306 1.1 tsubai if (ofdev.handle != -1)
307 1.1 tsubai panic("devopen");
308 1.1 tsubai if (of->f_flags != F_READ)
309 1.1 tsubai return EPERM;
310 1.22 tsutsui
311 1.22 tsutsui if (!parsefilepath(name, devname, filename, &partition))
312 1.22 tsutsui return ENOENT;
313 1.22 tsutsui
314 1.22 tsutsui if (filename[0] == '\0')
315 1.22 tsutsui /* no filename */
316 1.7 tsubai return ENOENT;
317 1.22 tsutsui
318 1.22 tsutsui if (devname[0] == '\0')
319 1.22 tsutsui /* no device, use default bootdev */
320 1.22 tsutsui strlcpy(devname, bootdev, sizeof(devname));
321 1.22 tsutsui
322 1.22 tsutsui DPRINTF("%s: devname = %s, filename = %s\n",
323 1.22 tsutsui __func__, devname, filename);
324 1.22 tsutsui
325 1.22 tsutsui strlcpy(opened_name, devname, sizeof(opened_name));
326 1.1 tsubai if (partition) {
327 1.1 tsubai cp = opened_name + strlen(opened_name);
328 1.1 tsubai *cp++ = ':';
329 1.1 tsubai *cp++ = partition;
330 1.1 tsubai *cp = 0;
331 1.1 tsubai }
332 1.22 tsutsui if (filename[0] != '/')
333 1.22 tsutsui strlcat(opened_name, "/", sizeof(opened_name));
334 1.22 tsutsui strlcat(opened_name, filename, sizeof(opened_name));
335 1.22 tsutsui
336 1.22 tsutsui DPRINTF("%s: opened_name = %s\n", __func__, opened_name);
337 1.22 tsutsui
338 1.22 tsutsui *file = opened_name + strlen(devname) + 1;
339 1.22 tsutsui if ((handle = OF_finddevice(devname)) == -1)
340 1.1 tsubai return ENOENT;
341 1.1 tsubai if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
342 1.1 tsubai return ENXIO;
343 1.1 tsubai #if 0
344 1.1 tsubai if (!strcmp(buf, "block"))
345 1.8 tsutsui /*
346 1.8 tsutsui * For block devices, indicate raw partition
347 1.8 tsutsui * (:0 in OpenFirmware)
348 1.8 tsutsui */
349 1.22 tsutsui strlcat(devname, ":0", sizeof(devname));
350 1.1 tsubai #endif
351 1.22 tsutsui if ((handle = OF_open(devname)) == -1)
352 1.1 tsubai return ENXIO;
353 1.6 wiz memset(&ofdev, 0, sizeof ofdev);
354 1.1 tsubai ofdev.handle = handle;
355 1.3 tsubai ofdev.dmabuf = NULL;
356 1.3 tsubai OF_call_method("dma-alloc", handle, 1, 1, MAXPHYS, &ofdev.dmabuf);
357 1.1 tsubai if (!strcmp(buf, "block")) {
358 1.1 tsubai ofdev.type = OFDEV_DISK;
359 1.1 tsubai ofdev.bsize = DEV_BSIZE;
360 1.1 tsubai /* First try to find a disklabel without MBR partitions */
361 1.1 tsubai if (strategy(&ofdev, F_READ,
362 1.17 uwe LABELSECTOR, DEV_BSIZE, buf, &nread) != 0
363 1.17 uwe || nread != DEV_BSIZE
364 1.1 tsubai || getdisklabel(buf, &label)) {
365 1.1 tsubai /* Else try MBR partitions */
366 1.1 tsubai error = search_label(&ofdev, 0, buf, &label, 0);
367 1.1 tsubai if (error && error != ERDLAB)
368 1.1 tsubai goto bad;
369 1.1 tsubai }
370 1.1 tsubai
371 1.1 tsubai if (error == ERDLAB) {
372 1.1 tsubai if (partition)
373 1.8 tsutsui /*
374 1.8 tsutsui * User specified a parititon,
375 1.8 tsutsui * but there is none
376 1.8 tsutsui */
377 1.1 tsubai goto bad;
378 1.1 tsubai /* No, label, just use complete disk */
379 1.1 tsubai ofdev.partoff = 0;
380 1.1 tsubai } else {
381 1.1 tsubai part = partition ? partition - 'a' : 0;
382 1.1 tsubai ofdev.partoff = label.d_partitions[part].p_offset;
383 1.1 tsubai }
384 1.8 tsutsui
385 1.19 mrg of->f_dev = of_devsw;
386 1.1 tsubai of->f_devdata = &ofdev;
387 1.20 hauke file_system[0] = file_system_ffsv1;
388 1.20 hauke file_system[1] = file_system_ffsv2;
389 1.20 hauke file_system[2] = file_system_lfsv1;
390 1.20 hauke file_system[3] = file_system_lfsv2;
391 1.20 hauke file_system[4] = file_system_ustarfs;
392 1.20 hauke file_system[5] = file_system_cd9660;
393 1.20 hauke file_system[6] = file_system_hfs;
394 1.20 hauke nfsys = 7;
395 1.1 tsubai return 0;
396 1.1 tsubai }
397 1.1 tsubai if (!strcmp(buf, "network")) {
398 1.1 tsubai ofdev.type = OFDEV_NET;
399 1.19 mrg of->f_dev = of_devsw;
400 1.1 tsubai of->f_devdata = &ofdev;
401 1.5 tsubai file_system[0] = file_system_nfs;
402 1.1 tsubai nfsys = 1;
403 1.17 uwe if ((error = net_open(&ofdev)))
404 1.1 tsubai goto bad;
405 1.1 tsubai return 0;
406 1.1 tsubai }
407 1.1 tsubai error = EFTYPE;
408 1.1 tsubai bad:
409 1.1 tsubai OF_close(handle);
410 1.1 tsubai ofdev.handle = -1;
411 1.1 tsubai return error;
412 1.1 tsubai }
413