ofdev.c revision 1.15 1 /* $NetBSD: ofdev.c,v 1.15 2005/06/23 19:44:01 junyoung 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 = FS_OPS(ufs);
155 static struct fs_ops file_system_hfs = FS_OPS(hfs);
156 static struct fs_ops file_system_ustarfs = FS_OPS(ustarfs);
157 static struct fs_ops file_system_cd9660 = FS_OPS(cd9660);
158 static struct fs_ops file_system_nfs = FS_OPS(nfs);
159
160 struct fs_ops file_system[4];
161 int nfsys;
162
163 static struct of_dev ofdev = {
164 -1,
165 };
166
167 char opened_name[256];
168 int floppyboot;
169
170 static u_long
171 get_long(const void *p)
172 {
173 const unsigned char *cp = p;
174
175 return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
176 }
177
178 /*
179 * Find a valid disklabel.
180 */
181 static int
182 search_label(struct of_dev *devp, u_long off, u_char *buf, struct disklabel *lp,
183 u_long off0)
184 {
185 size_t read;
186 struct mbr_partition *p;
187 int i;
188 u_long poff;
189 static int recursion;
190
191 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
192 || read != DEV_BSIZE)
193 return ERDLAB;
194
195 if (*(u_int16_t *)&buf[MBR_MAGIC_OFFSET] != sa_htole16(MBR_MAGIC))
196 return ERDLAB;
197
198 if (recursion++ <= 1)
199 off0 += off;
200 for (p = (struct mbr_partition *)(buf + MBR_PART_OFFSET), i = 4;
201 --i >= 0; p++) {
202 if (p->mbrp_type == MBR_PTYPE_NETBSD
203 #ifdef COMPAT_386BSD_MBRPART
204 || (p->mbrp_type == MBR_PTYPE_386BSD &&
205 (printf("WARNING: old BSD partition ID!\n"), 1)
206 /* XXX XXX - libsa printf() is void */ )
207 #endif
208 ) {
209 poff = get_long(&p->mbrp_start) + off0;
210 if (strategy(devp, F_READ, poff + 1,
211 DEV_BSIZE, buf, &read) == 0
212 && read == DEV_BSIZE) {
213 if (!getdisklabel(buf, lp)) {
214 recursion--;
215 return 0;
216 }
217 }
218 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
219 || read != DEV_BSIZE) {
220 recursion--;
221 return ERDLAB;
222 }
223 } else if (p->mbrp_type == MBR_PTYPE_EXT) {
224 poff = get_long(&p->mbrp_start);
225 if (!search_label(devp, poff, buf, lp, off0)) {
226 recursion--;
227 return 0;
228 }
229 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
230 || read != DEV_BSIZE) {
231 recursion--;
232 return ERDLAB;
233 }
234 }
235 }
236 recursion--;
237 return ERDLAB;
238 }
239
240 int
241 devopen(struct open_file *of, const char *name, char **file)
242 {
243 char *cp;
244 char partition;
245 char fname[256];
246 char buf[DEV_BSIZE];
247 struct disklabel label;
248 int handle, part;
249 size_t read;
250 int error = 0;
251
252 if (ofdev.handle != -1)
253 panic("devopen");
254 if (of->f_flags != F_READ)
255 return EPERM;
256 strcpy(fname, name);
257 cp = filename(fname, &partition);
258 if (cp) {
259 strcpy(buf, cp);
260 *cp = 0;
261 }
262 if (!cp || !*buf)
263 return ENOENT;
264 if (!*fname)
265 strcpy(fname, bootdev);
266 strcpy(opened_name, fname);
267 if (partition) {
268 cp = opened_name + strlen(opened_name);
269 *cp++ = ':';
270 *cp++ = partition;
271 *cp = 0;
272 }
273 if (*buf != '/')
274 strcat(opened_name, "/");
275 strcat(opened_name, buf);
276 *file = opened_name + strlen(fname) + 1;
277 if ((handle = OF_finddevice(fname)) == -1)
278 return ENOENT;
279 if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
280 return ENXIO;
281 floppyboot = !strcmp(buf, "floppy");
282 if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
283 return ENXIO;
284 #if 0
285 if (!strcmp(buf, "block"))
286 /*
287 * For block devices, indicate raw partition
288 * (:0 in OpenFirmware)
289 */
290 strcat(fname, ":0");
291 #endif
292 if ((handle = OF_open(fname)) == -1)
293 return ENXIO;
294 memset(&ofdev, 0, sizeof ofdev);
295 ofdev.handle = handle;
296 ofdev.dmabuf = NULL;
297 OF_call_method("dma-alloc", handle, 1, 1, MAXPHYS, &ofdev.dmabuf);
298 if (!strcmp(buf, "block")) {
299 ofdev.type = OFDEV_DISK;
300 ofdev.bsize = DEV_BSIZE;
301 /* First try to find a disklabel without MBR partitions */
302 if (strategy(&ofdev, F_READ,
303 LABELSECTOR, DEV_BSIZE, buf, &read) != 0
304 || read != DEV_BSIZE
305 || getdisklabel(buf, &label)) {
306 /* Else try MBR partitions */
307 error = search_label(&ofdev, 0, buf, &label, 0);
308 if (error && error != ERDLAB)
309 goto bad;
310 }
311
312 if (error == ERDLAB) {
313 if (partition)
314 /*
315 * User specified a parititon,
316 * but there is none
317 */
318 goto bad;
319 /* No, label, just use complete disk */
320 ofdev.partoff = 0;
321 } else {
322 part = partition ? partition - 'a' : 0;
323 ofdev.partoff = label.d_partitions[part].p_offset;
324 }
325
326 of->f_dev = devsw;
327 of->f_devdata = &ofdev;
328 file_system[0] = file_system_ufs;
329 file_system[1] = file_system_ustarfs;
330 file_system[2] = file_system_cd9660;
331 file_system[3] = file_system_hfs;
332 nfsys = 4;
333 return 0;
334 }
335 if (!strcmp(buf, "network")) {
336 ofdev.type = OFDEV_NET;
337 of->f_dev = devsw;
338 of->f_devdata = &ofdev;
339 file_system[0] = file_system_nfs;
340 nfsys = 1;
341 if (error = net_open(&ofdev))
342 goto bad;
343 return 0;
344 }
345 error = EFTYPE;
346 bad:
347 OF_close(handle);
348 ofdev.handle = -1;
349 return error;
350 }
351