ofdev.c revision 1.2.2.2 1 1.2.2.2 jdolecek /* $NetBSD: ofdev.c,v 1.2.2.2 2002/06/23 17:41:39 jdolecek Exp $ */
2 1.2.2.2 jdolecek
3 1.2.2.2 jdolecek /*
4 1.2.2.2 jdolecek * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 1.2.2.2 jdolecek * Copyright (C) 1995, 1996 TooLs GmbH.
6 1.2.2.2 jdolecek * All rights reserved.
7 1.2.2.2 jdolecek *
8 1.2.2.2 jdolecek * Redistribution and use in source and binary forms, with or without
9 1.2.2.2 jdolecek * modification, are permitted provided that the following conditions
10 1.2.2.2 jdolecek * are met:
11 1.2.2.2 jdolecek * 1. Redistributions of source code must retain the above copyright
12 1.2.2.2 jdolecek * notice, this list of conditions and the following disclaimer.
13 1.2.2.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
14 1.2.2.2 jdolecek * notice, this list of conditions and the following disclaimer in the
15 1.2.2.2 jdolecek * documentation and/or other materials provided with the distribution.
16 1.2.2.2 jdolecek * 3. All advertising materials mentioning features or use of this software
17 1.2.2.2 jdolecek * must display the following acknowledgement:
18 1.2.2.2 jdolecek * This product includes software developed by TooLs GmbH.
19 1.2.2.2 jdolecek * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.2.2.2 jdolecek * derived from this software without specific prior written permission.
21 1.2.2.2 jdolecek *
22 1.2.2.2 jdolecek * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.2.2.2 jdolecek * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.2.2.2 jdolecek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.2.2.2 jdolecek * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.2.2.2 jdolecek * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.2.2.2 jdolecek * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.2.2.2 jdolecek * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.2.2.2 jdolecek * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.2.2.2 jdolecek * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.2.2.2 jdolecek * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.2.2.2 jdolecek */
33 1.2.2.2 jdolecek /*
34 1.2.2.2 jdolecek * Device I/O routines using Open Firmware
35 1.2.2.2 jdolecek */
36 1.2.2.2 jdolecek
37 1.2.2.2 jdolecek #include <sys/param.h>
38 1.2.2.2 jdolecek #include <sys/disklabel.h>
39 1.2.2.2 jdolecek #include <sys/disklabel_mbr.h>
40 1.2.2.2 jdolecek
41 1.2.2.2 jdolecek #include <netinet/in.h>
42 1.2.2.2 jdolecek
43 1.2.2.2 jdolecek #include <lib/libsa/stand.h>
44 1.2.2.2 jdolecek #include <lib/libsa/ufs.h>
45 1.2.2.2 jdolecek #include <lib/libsa/cd9660.h>
46 1.2.2.2 jdolecek #include <lib/libsa/dosfs.h>
47 1.2.2.2 jdolecek #include <lib/libsa/nfs.h>
48 1.2.2.2 jdolecek
49 1.2.2.2 jdolecek #include "ofdev.h"
50 1.2.2.2 jdolecek
51 1.2.2.2 jdolecek extern char bootdev[];
52 1.2.2.2 jdolecek
53 1.2.2.2 jdolecek #ifdef DEBUG
54 1.2.2.2 jdolecek # define DPRINTF printf
55 1.2.2.2 jdolecek #else
56 1.2.2.2 jdolecek # define DPRINTF while (0) printf
57 1.2.2.2 jdolecek #endif
58 1.2.2.2 jdolecek
59 1.2.2.2 jdolecek static char *
60 1.2.2.2 jdolecek filename(str, ppart)
61 1.2.2.2 jdolecek char *str;
62 1.2.2.2 jdolecek char *ppart;
63 1.2.2.2 jdolecek {
64 1.2.2.2 jdolecek char *cp, *lp;
65 1.2.2.2 jdolecek char savec;
66 1.2.2.2 jdolecek int dhandle;
67 1.2.2.2 jdolecek char devtype[16];
68 1.2.2.2 jdolecek
69 1.2.2.2 jdolecek lp = str;
70 1.2.2.2 jdolecek devtype[0] = 0;
71 1.2.2.2 jdolecek *ppart = 0;
72 1.2.2.2 jdolecek for (cp = str; *cp; lp = cp) {
73 1.2.2.2 jdolecek /* For each component of the path name... */
74 1.2.2.2 jdolecek while (*++cp && *cp != '/');
75 1.2.2.2 jdolecek savec = *cp;
76 1.2.2.2 jdolecek *cp = 0;
77 1.2.2.2 jdolecek /* ...look whether there is a device with this name */
78 1.2.2.2 jdolecek dhandle = OF_finddevice(str);
79 1.2.2.2 jdolecek *cp = savec;
80 1.2.2.2 jdolecek if (dhandle == -1) {
81 1.2.2.2 jdolecek /* if not, lp is the delimiter between device and path */
82 1.2.2.2 jdolecek /* if the last component was a block device... */
83 1.2.2.2 jdolecek if (!strcmp(devtype, "block")) {
84 1.2.2.2 jdolecek /* search for arguments */
85 1.2.2.2 jdolecek for (cp = lp;
86 1.2.2.2 jdolecek --cp >= str && *cp != '/' && *cp != ':';);
87 1.2.2.2 jdolecek if (cp >= str && *cp == ':') {
88 1.2.2.2 jdolecek /* found arguments, make firmware ignore them */
89 1.2.2.2 jdolecek *cp = 0;
90 1.2.2.2 jdolecek for (cp = lp; *--cp && *cp != ',';);
91 1.2.2.2 jdolecek if (*++cp >= 'a' && *cp <= 'a' + MAXPARTITIONS)
92 1.2.2.2 jdolecek *ppart = *cp;
93 1.2.2.2 jdolecek }
94 1.2.2.2 jdolecek }
95 1.2.2.2 jdolecek return lp;
96 1.2.2.2 jdolecek } else if (OF_getprop(dhandle, "device_type", devtype, sizeof devtype) < 0)
97 1.2.2.2 jdolecek devtype[0] = 0;
98 1.2.2.2 jdolecek }
99 1.2.2.2 jdolecek return 0;
100 1.2.2.2 jdolecek }
101 1.2.2.2 jdolecek
102 1.2.2.2 jdolecek static int
103 1.2.2.2 jdolecek strategy(devdata, rw, blk, size, buf, rsize)
104 1.2.2.2 jdolecek void *devdata;
105 1.2.2.2 jdolecek int rw;
106 1.2.2.2 jdolecek daddr_t blk;
107 1.2.2.2 jdolecek size_t size;
108 1.2.2.2 jdolecek void *buf;
109 1.2.2.2 jdolecek size_t *rsize;
110 1.2.2.2 jdolecek {
111 1.2.2.2 jdolecek struct of_dev *dev = devdata;
112 1.2.2.2 jdolecek u_quad_t pos;
113 1.2.2.2 jdolecek int n;
114 1.2.2.2 jdolecek
115 1.2.2.2 jdolecek if (rw != F_READ)
116 1.2.2.2 jdolecek return EPERM;
117 1.2.2.2 jdolecek if (dev->type != OFDEV_DISK)
118 1.2.2.2 jdolecek panic("strategy");
119 1.2.2.2 jdolecek
120 1.2.2.2 jdolecek pos = (u_quad_t)(blk + dev->partoff) * dev->bsize;
121 1.2.2.2 jdolecek
122 1.2.2.2 jdolecek for (;;) {
123 1.2.2.2 jdolecek if (OF_seek(dev->handle, pos) < 0)
124 1.2.2.2 jdolecek break;
125 1.2.2.2 jdolecek n = OF_read(dev->handle, buf, size);
126 1.2.2.2 jdolecek if (n == -2)
127 1.2.2.2 jdolecek continue;
128 1.2.2.2 jdolecek if (n < 0)
129 1.2.2.2 jdolecek break;
130 1.2.2.2 jdolecek *rsize = n;
131 1.2.2.2 jdolecek return 0;
132 1.2.2.2 jdolecek }
133 1.2.2.2 jdolecek return EIO;
134 1.2.2.2 jdolecek }
135 1.2.2.2 jdolecek
136 1.2.2.2 jdolecek static int
137 1.2.2.2 jdolecek devclose(of)
138 1.2.2.2 jdolecek struct open_file *of;
139 1.2.2.2 jdolecek {
140 1.2.2.2 jdolecek struct of_dev *op = of->f_devdata;
141 1.2.2.2 jdolecek
142 1.2.2.2 jdolecek if (op->type == OFDEV_NET)
143 1.2.2.2 jdolecek net_close(op);
144 1.2.2.2 jdolecek OF_close(op->handle);
145 1.2.2.2 jdolecek op->handle = -1;
146 1.2.2.2 jdolecek }
147 1.2.2.2 jdolecek
148 1.2.2.2 jdolecek static struct devsw devsw[1] = {
149 1.2.2.2 jdolecek "OpenFirmware",
150 1.2.2.2 jdolecek strategy,
151 1.2.2.2 jdolecek (int (*)__P((struct open_file *, ...)))nodev,
152 1.2.2.2 jdolecek devclose,
153 1.2.2.2 jdolecek noioctl
154 1.2.2.2 jdolecek };
155 1.2.2.2 jdolecek int ndevs = sizeof devsw / sizeof devsw[0];
156 1.2.2.2 jdolecek
157 1.2.2.2 jdolecek static struct fs_ops file_system_ufs = {
158 1.2.2.2 jdolecek ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
159 1.2.2.2 jdolecek };
160 1.2.2.2 jdolecek static struct fs_ops file_system_cd9660 = {
161 1.2.2.2 jdolecek cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek,
162 1.2.2.2 jdolecek cd9660_stat
163 1.2.2.2 jdolecek };
164 1.2.2.2 jdolecek static struct fs_ops file_system_dosfs = {
165 1.2.2.2 jdolecek dosfs_open, dosfs_close, dosfs_read, dosfs_write, dosfs_seek,
166 1.2.2.2 jdolecek dosfs_stat
167 1.2.2.2 jdolecek };
168 1.2.2.2 jdolecek static struct fs_ops file_system_nfs = {
169 1.2.2.2 jdolecek nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
170 1.2.2.2 jdolecek };
171 1.2.2.2 jdolecek
172 1.2.2.2 jdolecek struct fs_ops file_system[3];
173 1.2.2.2 jdolecek int nfsys;
174 1.2.2.2 jdolecek
175 1.2.2.2 jdolecek static struct of_dev ofdev = {
176 1.2.2.2 jdolecek -1,
177 1.2.2.2 jdolecek };
178 1.2.2.2 jdolecek
179 1.2.2.2 jdolecek char opened_name[256];
180 1.2.2.2 jdolecek int floppyboot;
181 1.2.2.2 jdolecek
182 1.2.2.2 jdolecek static u_long
183 1.2.2.2 jdolecek get_long(p)
184 1.2.2.2 jdolecek const void *p;
185 1.2.2.2 jdolecek {
186 1.2.2.2 jdolecek const unsigned char *cp = p;
187 1.2.2.2 jdolecek
188 1.2.2.2 jdolecek return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
189 1.2.2.2 jdolecek }
190 1.2.2.2 jdolecek
191 1.2.2.2 jdolecek /*
192 1.2.2.2 jdolecek * Find a valid disklabel.
193 1.2.2.2 jdolecek */
194 1.2.2.2 jdolecek static int
195 1.2.2.2 jdolecek search_label(devp, off, buf, lp, off0)
196 1.2.2.2 jdolecek struct of_dev *devp;
197 1.2.2.2 jdolecek u_long off;
198 1.2.2.2 jdolecek char *buf;
199 1.2.2.2 jdolecek struct disklabel *lp;
200 1.2.2.2 jdolecek u_long off0;
201 1.2.2.2 jdolecek {
202 1.2.2.2 jdolecek size_t read;
203 1.2.2.2 jdolecek struct mbr_partition *p;
204 1.2.2.2 jdolecek int i;
205 1.2.2.2 jdolecek u_long poff;
206 1.2.2.2 jdolecek static int recursion;
207 1.2.2.2 jdolecek
208 1.2.2.2 jdolecek if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
209 1.2.2.2 jdolecek || read != DEV_BSIZE)
210 1.2.2.2 jdolecek return ERDLAB;
211 1.2.2.2 jdolecek
212 1.2.2.2 jdolecek if (buf[510] != 0x55 || buf[511] != 0xaa)
213 1.2.2.2 jdolecek return ERDLAB;
214 1.2.2.2 jdolecek
215 1.2.2.2 jdolecek if (recursion++ <= 1)
216 1.2.2.2 jdolecek off0 += off;
217 1.2.2.2 jdolecek for (p = (struct mbr_partition *)(buf + MBR_PARTOFF), i = 4;
218 1.2.2.2 jdolecek --i >= 0; p++) {
219 1.2.2.2 jdolecek if (p->mbrp_typ == MBR_PTYPE_NETBSD
220 1.2.2.2 jdolecek #ifdef COMPAT_386BSD_MBRPART
221 1.2.2.2 jdolecek || (p->mbrp_typ == MBR_PTYPE_386BSD &&
222 1.2.2.2 jdolecek (printf("WARNING: old BSD partition ID!\n"), 1)
223 1.2.2.2 jdolecek /* XXX XXX - libsa printf() is void */ )
224 1.2.2.2 jdolecek #endif
225 1.2.2.2 jdolecek ) {
226 1.2.2.2 jdolecek poff = get_long(&p->mbrp_start) + off0;
227 1.2.2.2 jdolecek if (strategy(devp, F_READ, poff + LABELSECTOR,
228 1.2.2.2 jdolecek DEV_BSIZE, buf, &read) == 0
229 1.2.2.2 jdolecek && read == DEV_BSIZE) {
230 1.2.2.2 jdolecek if (!getdisklabel(buf, lp)) {
231 1.2.2.2 jdolecek recursion--;
232 1.2.2.2 jdolecek return 0;
233 1.2.2.2 jdolecek }
234 1.2.2.2 jdolecek }
235 1.2.2.2 jdolecek if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
236 1.2.2.2 jdolecek || read != DEV_BSIZE) {
237 1.2.2.2 jdolecek recursion--;
238 1.2.2.2 jdolecek return ERDLAB;
239 1.2.2.2 jdolecek }
240 1.2.2.2 jdolecek } else if (p->mbrp_typ == MBR_PTYPE_EXT) {
241 1.2.2.2 jdolecek poff = get_long(&p->mbrp_start);
242 1.2.2.2 jdolecek if (!search_label(devp, poff, buf, lp, off0)) {
243 1.2.2.2 jdolecek recursion--;
244 1.2.2.2 jdolecek return 0;
245 1.2.2.2 jdolecek }
246 1.2.2.2 jdolecek if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
247 1.2.2.2 jdolecek || read != DEV_BSIZE) {
248 1.2.2.2 jdolecek recursion--;
249 1.2.2.2 jdolecek return ERDLAB;
250 1.2.2.2 jdolecek }
251 1.2.2.2 jdolecek }
252 1.2.2.2 jdolecek }
253 1.2.2.2 jdolecek recursion--;
254 1.2.2.2 jdolecek return ERDLAB;
255 1.2.2.2 jdolecek }
256 1.2.2.2 jdolecek
257 1.2.2.2 jdolecek int
258 1.2.2.2 jdolecek devopen(of, name, file)
259 1.2.2.2 jdolecek struct open_file *of;
260 1.2.2.2 jdolecek const char *name;
261 1.2.2.2 jdolecek char **file;
262 1.2.2.2 jdolecek {
263 1.2.2.2 jdolecek char *cp;
264 1.2.2.2 jdolecek char partition;
265 1.2.2.2 jdolecek char fname[256];
266 1.2.2.2 jdolecek char buf[DEV_BSIZE];
267 1.2.2.2 jdolecek struct disklabel label;
268 1.2.2.2 jdolecek int handle, part;
269 1.2.2.2 jdolecek size_t read;
270 1.2.2.2 jdolecek int error = 0;
271 1.2.2.2 jdolecek
272 1.2.2.2 jdolecek if (ofdev.handle != -1)
273 1.2.2.2 jdolecek panic("devopen");
274 1.2.2.2 jdolecek if (of->f_flags != F_READ)
275 1.2.2.2 jdolecek return EPERM;
276 1.2.2.2 jdolecek strcpy(fname, name);
277 1.2.2.2 jdolecek cp = filename(fname, &partition);
278 1.2.2.2 jdolecek if (cp) {
279 1.2.2.2 jdolecek DPRINTF("filename=%s\n", cp);
280 1.2.2.2 jdolecek strcpy(buf, cp);
281 1.2.2.2 jdolecek *cp = 0;
282 1.2.2.2 jdolecek }
283 1.2.2.2 jdolecek if (!cp || !*buf)
284 1.2.2.2 jdolecek strcpy(buf, DEFAULT_KERNEL);
285 1.2.2.2 jdolecek if (!*fname)
286 1.2.2.2 jdolecek strcpy(fname, bootdev);
287 1.2.2.2 jdolecek DPRINTF("fname=%s\n", fname);
288 1.2.2.2 jdolecek strcpy(opened_name, fname);
289 1.2.2.2 jdolecek if (partition) {
290 1.2.2.2 jdolecek cp = opened_name + strlen(opened_name);
291 1.2.2.2 jdolecek *cp++ = ':';
292 1.2.2.2 jdolecek *cp++ = partition;
293 1.2.2.2 jdolecek *cp = 0;
294 1.2.2.2 jdolecek }
295 1.2.2.2 jdolecek if (*buf != '/')
296 1.2.2.2 jdolecek strcat(opened_name, "/");
297 1.2.2.2 jdolecek strcat(opened_name, buf);
298 1.2.2.2 jdolecek *file = opened_name + strlen(fname) + 1;
299 1.2.2.2 jdolecek if ((handle = OF_finddevice(fname)) == -1) {
300 1.2.2.2 jdolecek DPRINTF("OF_finddevice(\"%s\") failed\n", fname);
301 1.2.2.2 jdolecek return ENOENT;
302 1.2.2.2 jdolecek }
303 1.2.2.2 jdolecek if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
304 1.2.2.2 jdolecek return ENXIO;
305 1.2.2.2 jdolecek floppyboot = !strcmp(buf, "floppy");
306 1.2.2.2 jdolecek if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
307 1.2.2.2 jdolecek return ENXIO;
308 1.2.2.2 jdolecek if (!strcmp(buf, "block"))
309 1.2.2.2 jdolecek /* For block devices, indicate raw partition (:0 in OpenFirmware) */
310 1.2.2.2 jdolecek strcat(fname, ":0");
311 1.2.2.2 jdolecek if ((handle = OF_open(fname)) == -1)
312 1.2.2.2 jdolecek return ENXIO;
313 1.2.2.2 jdolecek memset(&ofdev, 0, sizeof ofdev);
314 1.2.2.2 jdolecek ofdev.handle = handle;
315 1.2.2.2 jdolecek if (!strcmp(buf, "block")) {
316 1.2.2.2 jdolecek ofdev.type = OFDEV_DISK;
317 1.2.2.2 jdolecek ofdev.bsize = DEV_BSIZE;
318 1.2.2.2 jdolecek /* First try to find a disklabel without MBR partitions */
319 1.2.2.2 jdolecek if (strategy(&ofdev, F_READ,
320 1.2.2.2 jdolecek LABELSECTOR, DEV_BSIZE, buf, &read) != 0
321 1.2.2.2 jdolecek || read != DEV_BSIZE
322 1.2.2.2 jdolecek || getdisklabel(buf, &label)) {
323 1.2.2.2 jdolecek /* Else try MBR partitions */
324 1.2.2.2 jdolecek error = search_label(&ofdev, 0, buf, &label, 0);
325 1.2.2.2 jdolecek if (error && error != ERDLAB)
326 1.2.2.2 jdolecek goto bad;
327 1.2.2.2 jdolecek }
328 1.2.2.2 jdolecek
329 1.2.2.2 jdolecek if (error == ERDLAB) {
330 1.2.2.2 jdolecek if (partition)
331 1.2.2.2 jdolecek /* User specified a parititon, but there is none */
332 1.2.2.2 jdolecek goto bad;
333 1.2.2.2 jdolecek /* No, label, just use complete disk */
334 1.2.2.2 jdolecek ofdev.partoff = 0;
335 1.2.2.2 jdolecek } else {
336 1.2.2.2 jdolecek part = partition ? partition - 'a' : 0;
337 1.2.2.2 jdolecek ofdev.partoff = label.d_partitions[part].p_offset;
338 1.2.2.2 jdolecek }
339 1.2.2.2 jdolecek
340 1.2.2.2 jdolecek of->f_dev = devsw;
341 1.2.2.2 jdolecek of->f_devdata = &ofdev;
342 1.2.2.2 jdolecek file_system[0] = file_system_ufs;
343 1.2.2.2 jdolecek file_system[1] = file_system_cd9660;
344 1.2.2.2 jdolecek file_system[1] = file_system_dosfs;
345 1.2.2.2 jdolecek nfsys = 2;
346 1.2.2.2 jdolecek return 0;
347 1.2.2.2 jdolecek }
348 1.2.2.2 jdolecek if (!strcmp(buf, "network")) {
349 1.2.2.2 jdolecek ofdev.type = OFDEV_NET;
350 1.2.2.2 jdolecek of->f_dev = devsw;
351 1.2.2.2 jdolecek of->f_devdata = &ofdev;
352 1.2.2.2 jdolecek file_system[0] = file_system_nfs;
353 1.2.2.2 jdolecek nfsys = 1;
354 1.2.2.2 jdolecek if (error = net_open(&ofdev))
355 1.2.2.2 jdolecek goto bad;
356 1.2.2.2 jdolecek return 0;
357 1.2.2.2 jdolecek }
358 1.2.2.2 jdolecek error = EFTYPE;
359 1.2.2.2 jdolecek bad:
360 1.2.2.2 jdolecek OF_close(handle);
361 1.2.2.2 jdolecek ofdev.handle = -1;
362 1.2.2.2 jdolecek return error;
363 1.2.2.2 jdolecek }
364