ofdev.c revision 1.3 1 /* $NetBSD: ofdev.c,v 1.3 1999/02/04 15:41:15 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/ufs.h>
45 #include <lib/libsa/cd9660.h>
46 #include <lib/libsa/nfs.h>
47
48 #include "ofdev.h"
49
50 extern char bootdev[];
51
52 static char *
53 filename(str, ppart)
54 char *str;
55 char *ppart;
56 {
57 char *cp, *lp;
58 char savec;
59 int dhandle;
60 char devtype[16];
61
62 lp = str;
63 devtype[0] = 0;
64 *ppart = 0;
65 for (cp = str; *cp; lp = cp) {
66 /* For each component of the path name... */
67 while (*++cp && *cp != '/');
68 savec = *cp;
69 *cp = 0;
70 /* ...look whether there is a device with this name */
71 dhandle = OF_finddevice(str);
72 *cp = savec;
73 if (dhandle == -1) {
74 /* if not, lp is the delimiter between device and path */
75 /* if the last component was a block device... */
76 if (!strcmp(devtype, "block")) {
77 /* search for arguments */
78 for (cp = lp;
79 --cp >= str && *cp != '/' && *cp != ':';);
80 if (cp >= str && *cp == ':') {
81 /* found arguments, make firmware ignore them */
82 *cp = 0;
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_cd9660 = {
155 cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek,
156 cd9660_stat
157 };
158 static struct fs_ops file_system_nfs = {
159 nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
160 };
161
162 struct fs_ops file_system[3];
163 int nfsys;
164
165 static struct of_dev ofdev = {
166 -1,
167 };
168
169 char opened_name[256];
170 int floppyboot;
171
172 static u_long
173 get_long(p)
174 const void *p;
175 {
176 const unsigned char *cp = p;
177
178 return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
179 }
180
181 /*
182 * Find a valid disklabel.
183 */
184 static int
185 search_label(devp, off, buf, lp, off0)
186 struct of_dev *devp;
187 u_long off;
188 u_char *buf;
189 struct disklabel *lp;
190 u_long off0;
191 {
192 size_t read;
193 struct mbr_partition *p;
194 int i;
195 u_long poff;
196 static int recursion;
197
198 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
199 || read != DEV_BSIZE)
200 return ERDLAB;
201
202 if (buf[510] != 0x55 || buf[511] != 0xaa)
203 return ERDLAB;
204
205 if (recursion++ <= 1)
206 off0 += off;
207 for (p = (struct mbr_partition *)(buf + MBR_PARTOFF), i = 4;
208 --i >= 0; p++) {
209 if (p->mbrp_typ == MBR_PTYPE_NETBSD
210 #ifdef COMPAT_386BSD_MBRPART
211 || (p->mbrp_typ == MBR_PTYPE_386BSD &&
212 (printf("WARNING: old BSD partition ID!\n"), 1)
213 /* XXX XXX - libsa printf() is void */ )
214 #endif
215 ) {
216 poff = get_long(&p->mbrp_start) + off0;
217 if (strategy(devp, F_READ, poff + LABELSECTOR,
218 DEV_BSIZE, buf, &read) == 0
219 && read == DEV_BSIZE) {
220 if (!getdisklabel(buf, lp)) {
221 recursion--;
222 return 0;
223 }
224 }
225 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
226 || read != DEV_BSIZE) {
227 recursion--;
228 return ERDLAB;
229 }
230 } else if (p->mbrp_typ == MBR_PTYPE_EXT) {
231 poff = get_long(&p->mbrp_start);
232 if (!search_label(devp, poff, buf, lp, off0)) {
233 recursion--;
234 return 0;
235 }
236 if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
237 || read != DEV_BSIZE) {
238 recursion--;
239 return ERDLAB;
240 }
241 }
242 }
243 recursion--;
244 return ERDLAB;
245 }
246
247 int
248 devopen(of, name, file)
249 struct open_file *of;
250 const char *name;
251 char **file;
252 {
253 char *cp;
254 char partition;
255 char fname[256];
256 char buf[DEV_BSIZE];
257 struct disklabel label;
258 int handle, part;
259 size_t read;
260 int error = 0;
261
262 if (ofdev.handle != -1)
263 panic("devopen");
264 if (of->f_flags != F_READ)
265 return EPERM;
266 strcpy(fname, name);
267 cp = filename(fname, &partition);
268 if (cp) {
269 strcpy(buf, cp);
270 *cp = 0;
271 }
272 if (!cp || !*buf)
273 strcpy(buf, DEFAULT_KERNEL);
274 if (!*fname)
275 strcpy(fname, bootdev);
276 strcpy(opened_name, fname);
277 if (partition) {
278 cp = opened_name + strlen(opened_name);
279 *cp++ = ':';
280 *cp++ = partition;
281 *cp = 0;
282 }
283 if (*buf != '/')
284 strcat(opened_name, "/");
285 strcat(opened_name, buf);
286 *file = opened_name + strlen(fname) + 1;
287 if ((handle = OF_finddevice(fname)) == -1)
288 return ENOENT;
289 if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
290 return ENXIO;
291 floppyboot = !strcmp(buf, "floppy");
292 if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
293 return ENXIO;
294 #if 0
295 if (!strcmp(buf, "block"))
296 /* For block devices, indicate raw partition (:0 in OpenFirmware) */
297 strcat(fname, ":0");
298 #endif
299 if ((handle = OF_open(fname)) == -1)
300 return ENXIO;
301 bzero(&ofdev, sizeof ofdev);
302 ofdev.handle = handle;
303 ofdev.dmabuf = NULL;
304 OF_call_method("dma-alloc", handle, 1, 1, MAXPHYS, &ofdev.dmabuf);
305 if (!strcmp(buf, "block")) {
306 ofdev.type = OFDEV_DISK;
307 ofdev.bsize = DEV_BSIZE;
308 /* First try to find a disklabel without MBR partitions */
309 if (strategy(&ofdev, F_READ,
310 LABELSECTOR, DEV_BSIZE, buf, &read) != 0
311 || read != DEV_BSIZE
312 || getdisklabel(buf, &label)) {
313 /* Else try MBR partitions */
314 error = search_label(&ofdev, 0, buf, &label, 0);
315 if (error && error != ERDLAB)
316 goto bad;
317 }
318
319 if (error == ERDLAB) {
320 if (partition)
321 /* User specified a parititon, but there is none */
322 goto bad;
323 /* No, label, just use complete disk */
324 ofdev.partoff = 0;
325 } else {
326 part = partition ? partition - 'a' : 0;
327 ofdev.partoff = label.d_partitions[part].p_offset;
328 }
329
330 of->f_dev = devsw;
331 of->f_devdata = &ofdev;
332 bcopy(&file_system_ufs, file_system, sizeof file_system[0]);
333 bcopy(&file_system_cd9660, file_system + 1,
334 sizeof file_system[0]);
335 nfsys = 2;
336 return 0;
337 }
338 if (!strcmp(buf, "network")) {
339 ofdev.type = OFDEV_NET;
340 of->f_dev = devsw;
341 of->f_devdata = &ofdev;
342 bcopy(&file_system_nfs, file_system, sizeof file_system[0]);
343 nfsys = 1;
344 if (error = net_open(&ofdev))
345 goto bad;
346 return 0;
347 }
348 error = EFTYPE;
349 bad:
350 OF_close(handle);
351 ofdev.handle = -1;
352 return error;
353 }
354