ofdev.c revision 1.9 1 /* $NetBSD: ofdev.c,v 1.9 2003/06/26 20:47:10 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 <string.h>
40
41 #include <sys/param.h>
42 #include <sys/disklabel.h>
43 #include <sys/disklabel_mbr.h>
44
45 #include <netinet/in.h>
46
47 #include <lib/libsa/byteorder.h>
48 #include <lib/libsa/ufs.h>
49 #include <lib/libsa/cd9660.h>
50 #include <lib/libsa/dosfs.h>
51 #include <lib/libsa/nfs.h>
52
53 #include "net.h"
54 #include "openfirm.h"
55
56 extern char bootdev[];
57
58 #ifdef DEBUG
59 # define DPRINTF printf
60 #else
61 # define DPRINTF while (0) printf
62 #endif
63
64 static char *
65 filename(char *str, char *ppart)
66 {
67 char *cp, *lp;
68 char savec;
69 int dhandle;
70 char devtype[16];
71
72 lp = str;
73 devtype[0] = 0;
74 *ppart = 0;
75 for (cp = str; *cp; lp = cp) {
76
77 /* For each component of the path name... */
78 while (*++cp && *cp != '/')
79 ;
80 savec = *cp;
81 *cp = 0;
82
83 /* ...look whether there is a device with this name */
84 dhandle = OF_finddevice(str);
85 *cp = savec;
86 if (dhandle == -1) {
87
88 /*
89 * if not, lp is the delimiter between device and path
90 * if the last component was a block device.
91 */
92
93 if (!strcmp(devtype, "block")) {
94
95 /* search for arguments */
96 for (cp = lp;
97 --cp >= str && *cp != '/' && *cp != ':';)
98 ;
99 if (cp >= str && *cp == ':') {
100
101 /*
102 * found some arguments,
103 * make OFW ignore them.
104 */
105
106 *cp = 0;
107 for (cp = lp; *--cp && *cp != ',';)
108 ;
109 if (*++cp >= 'a' &&
110 *cp < 'a' + MAXPARTITIONS)
111 *ppart = *cp;
112 }
113 }
114 return lp;
115 }
116 if (OF_getprop(dhandle, "device_type", devtype,
117 sizeof devtype) < 0)
118 devtype[0] = 0;
119 }
120 return 0;
121 }
122
123 static int
124 strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
125 size_t *rsize)
126 {
127 struct of_dev *dev = devdata;
128 u_quad_t pos;
129 int n;
130
131 if (rw != F_READ)
132 return EPERM;
133 if (dev->type != OFDEV_DISK)
134 panic("strategy");
135
136 pos = (u_quad_t)((blk + dev->partoff) * dev->bsize);
137
138 for (;;) {
139 if (OF_seek(dev->handle, pos) < 0)
140 break;
141 n = OF_read(dev->handle, buf, size);
142 if (n == -2)
143 continue;
144 if (n < 0)
145 break;
146 *rsize = n;
147 return 0;
148 }
149 return EIO;
150 }
151
152 static int
153 devopen_dummy(struct open_file *of, ...) {
154 return -1;
155 }
156
157 static int
158 devclose(struct open_file *of)
159 {
160 struct of_dev *op = of->f_devdata;
161
162 if (op->type == OFDEV_NET)
163 net_close(op);
164 OF_close(op->handle);
165 op->handle = -1;
166 return 0;
167 }
168
169 static struct devsw devsw[1] = {
170 { "OpenFirmware", strategy, devopen_dummy, devclose, noioctl }
171 };
172 int ndevs = sizeof devsw / sizeof devsw[0];
173
174 static struct fs_ops file_system_ufs = {
175 ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
176 };
177 static struct fs_ops file_system_cd9660 = {
178 cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek,
179 cd9660_stat
180 };
181 static struct fs_ops file_system_dosfs = {
182 dosfs_open, dosfs_close, dosfs_read, dosfs_write, dosfs_seek,
183 dosfs_stat
184 };
185 static struct fs_ops file_system_nfs = {
186 nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
187 };
188
189 struct fs_ops file_system[3];
190 int nfsys;
191
192 static struct of_dev ofdev = {
193 -1,
194 };
195
196 char opened_name[256];
197 int floppyboot;
198
199 static u_long
200 get_long(const void *p)
201 {
202 const unsigned char *cp = p;
203
204 return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
205 }
206
207 /*
208 * Find a valid disklabel.
209 */
210 static int
211 search_label(struct of_dev *devp, u_long off, char *buf, struct disklabel *lp,
212 u_long off0)
213 {
214 size_t read;
215 struct mbr_partition *p;
216 int i;
217 u_long poff;
218 static int recursion;
219
220 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
221 || read != DEV_BSIZE)
222 return ERDLAB;
223
224 if (*(u_int16_t *)&buf[MBR_MAGICOFF] != sa_htole16(MBR_MAGIC))
225 return ERDLAB;
226
227 if (recursion++ <= 1)
228 off0 += off;
229 for (p = (struct mbr_partition *)(buf + MBR_PARTOFF), i = 0;
230 i < NMBRPART; i++, p++) {
231 if (p->mbrp_typ == MBR_PTYPE_NETBSD
232 #ifdef COMPAT_386BSD_MBRPART
233 || (p->mbrp_typ == MBR_PTYPE_386BSD &&
234 (printf("WARNING: old BSD partition ID!\n"), 1)
235 /* XXX XXX - libsa printf() is void */ )
236 #endif
237 ) {
238 poff = get_long(&p->mbrp_start) + off0;
239 if (strategy(devp, F_READ, poff + LABELSECTOR,
240 DEV_BSIZE, buf, &read) == 0
241 && read == DEV_BSIZE) {
242 if (!getdisklabel(buf, lp)) {
243 recursion--;
244 return 0;
245 }
246 }
247 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
248 || read != DEV_BSIZE) {
249 recursion--;
250 return ERDLAB;
251 }
252 } else if (p->mbrp_typ == MBR_PTYPE_EXT) {
253 poff = get_long(&p->mbrp_start);
254 if (!search_label(devp, poff, buf, lp, off0)) {
255 recursion--;
256 return 0;
257 }
258 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
259 || read != DEV_BSIZE) {
260 recursion--;
261 return ERDLAB;
262 }
263 }
264 }
265
266 recursion--;
267 return ERDLAB;
268 }
269
270 int
271 devopen(struct open_file *of, const char *name, char **file)
272 {
273 char *cp;
274 char partition;
275 char fname[256];
276 char buf[DEV_BSIZE];
277 struct disklabel label;
278 int handle, part;
279 size_t read;
280 int error = 0;
281
282 if (ofdev.handle != -1)
283 panic("devopen");
284 if (of->f_flags != F_READ)
285 return EPERM;
286 strcpy(fname, name);
287 cp = filename(fname, &partition);
288 if (cp) {
289 DPRINTF("filename=%s\n", cp);
290 strcpy(buf, cp);
291 *cp = 0;
292 }
293 if (!cp || !*buf)
294 strcpy(buf, DEFAULT_KERNEL);
295 if (!*fname)
296 strcpy(fname, bootdev);
297 DPRINTF("fname=%s\n", fname);
298 strcpy(opened_name, fname);
299 if (partition) {
300 cp = opened_name + strlen(opened_name);
301 *cp++ = ':';
302 *cp++ = partition;
303 *cp = 0;
304 }
305 if (*buf != '/')
306 strcat(opened_name, "/");
307 strcat(opened_name, buf);
308 *file = opened_name + strlen(fname) + 1;
309 if (partition) {
310 *file += 2;
311 }
312 if ((handle = OF_finddevice(fname)) == -1) {
313 DPRINTF("OF_finddevice(\"%s\") failed\n", fname);
314 return ENOENT;
315 }
316 if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
317 return ENXIO;
318 floppyboot = !strcmp(buf, "floppy");
319 if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
320 return ENXIO;
321 if (!strcmp(buf, "block")) {
322
323 /*
324 * For block devices, indicate raw partition
325 * (:0 in OpenFirmware)
326 */
327
328 strcat(fname, ":0");
329 }
330 if ((handle = OF_open(fname)) == -1)
331 return ENXIO;
332 memset(&ofdev, 0, sizeof ofdev);
333 ofdev.handle = handle;
334 if (!strcmp(buf, "block")) {
335 ofdev.type = OFDEV_DISK;
336 ofdev.bsize = DEV_BSIZE;
337
338 /* First try to find a disklabel without MBR partitions */
339 if (strategy(&ofdev, F_READ,
340 LABELSECTOR, DEV_BSIZE, buf, &read) != 0
341 || read != DEV_BSIZE
342 || getdisklabel(buf, &label)) {
343
344 /* Else try MBR partitions */
345 error = search_label(&ofdev, 0, buf, &label, 0);
346 if (error && error != ERDLAB)
347 goto bad;
348 }
349
350 if (error == ERDLAB) {
351 if (partition) {
352
353 /*
354 * User specified a parititon,
355 * but there is none.
356 */
357
358 goto bad;
359 }
360
361 /* No label, just use complete disk */
362 ofdev.partoff = 0;
363 } else {
364 part = partition ? partition - 'a' : 0;
365 ofdev.partoff = label.d_partitions[part].p_offset;
366 }
367
368 of->f_dev = devsw;
369 of->f_devdata = &ofdev;
370 file_system[0] = file_system_ufs;
371 file_system[1] = file_system_cd9660;
372 file_system[2] = file_system_dosfs;
373 nfsys = 3;
374 return 0;
375 }
376 if (!strcmp(buf, "network")) {
377 ofdev.type = OFDEV_NET;
378 of->f_dev = devsw;
379 of->f_devdata = &ofdev;
380 file_system[0] = file_system_nfs;
381 nfsys = 1;
382 if ((error = net_open(&ofdev)) != 0)
383 goto bad;
384 return 0;
385 }
386 error = EFTYPE;
387 bad:
388 OF_close(handle);
389 ofdev.handle = -1;
390 return error;
391 }
392