No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / ofppc / stand / ofwboot / ofdev.c
blobe8b1c0a0aa499c12408c69adfca0973ca77b7752
1 /* $NetBSD: ofdev.c,v 1.16 2009/01/12 07:49:57 tsutsui Exp $ */
3 /*
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * All rights reserved.
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.
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.
34 * Device I/O routines using Open Firmware
37 #include "ofdev.h"
39 #include <sys/param.h>
41 #include <netinet/in.h>
43 #include <lib/libkern/libkern.h>
45 #include <lib/libsa/byteorder.h>
46 #include <lib/libsa/ufs.h>
47 #include <lib/libsa/cd9660.h>
48 #include <lib/libsa/dosfs.h>
49 #include <lib/libsa/nfs.h>
51 #include "net.h"
52 #include "openfirm.h"
53 #include "mbr.h"
54 #include "rdb.h"
56 extern char bootdev[];
58 #ifdef DEBUG
59 # define DPRINTF printf
60 #else
61 # define DPRINTF while (0) printf
62 #endif
64 static char *
65 filename(char *str, char *ppart)
67 char *cp, *lp;
68 char savec;
69 int dhandle;
70 char devtype[16];
72 lp = str;
73 devtype[0] = 0;
74 *ppart = 0;
75 for (cp = str; *cp; lp = cp) {
77 /* For each component of the path name... */
78 while (*++cp && *cp != '/')
80 savec = *cp;
81 *cp = 0;
83 /* ...look whether there is a device with this name */
84 dhandle = OF_finddevice(str);
85 *cp = savec;
86 if (dhandle == -1) {
89 * if not, lp is the delimiter between device and path
90 * if the last component was a block device.
93 if (!strcmp(devtype, "block")) {
95 /* search for arguments */
96 for (cp = lp;
97 --cp >= str && *cp != '/' && *cp != ':';)
99 if (cp >= str && *cp == ':') {
102 * found some arguments,
103 * make OFW ignore them.
106 *cp = 0;
107 for (cp = lp; *--cp && *cp != ',';)
109 if (*++cp >= 'a' &&
110 *cp < 'a' + MAXPARTITIONS)
111 *ppart = *cp;
114 return lp;
116 if (OF_getprop(dhandle, "device_type", devtype,
117 sizeof devtype) < 0)
118 devtype[0] = 0;
120 return 0;
124 strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
125 size_t *rsize)
127 struct of_dev *dev = devdata;
128 u_quad_t pos;
129 int n;
131 if (rw != F_READ)
132 return EPERM;
133 if (dev->type != OFDEV_DISK)
134 panic("strategy");
136 pos = (u_quad_t)((blk + dev->partoff) * dev->bsize);
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;
149 return EIO;
152 static int
153 devopen_dummy(struct open_file *of, ...) {
154 return -1;
157 static int
158 devclose(struct open_file *of)
160 struct of_dev *op = of->f_devdata;
162 if (op->type == OFDEV_NET)
163 net_close(op);
164 OF_close(op->handle);
165 op->handle = -1;
166 return 0;
169 struct devsw devsw[1] = {
170 { "OpenFirmware", strategy, devopen_dummy, devclose, noioctl }
172 int ndevs = sizeof devsw / sizeof devsw[0];
174 static struct fs_ops file_system_ufs = FS_OPS(ufs);
175 static struct fs_ops file_system_cd9660 = FS_OPS(cd9660);
176 static struct fs_ops file_system_dosfs = FS_OPS(dosfs);
177 static struct fs_ops file_system_nfs = FS_OPS(nfs);
179 struct fs_ops file_system[3];
180 int nfsys;
182 static struct of_dev ofdev = {
186 char opened_name[256];
187 int floppyboot;
191 devopen(struct open_file *of, const char *name, char **file)
193 char *cp;
194 char partition;
195 char fname[256];
196 struct disklabel label;
197 int handle, part;
198 size_t read;
199 int error = 0;
200 /* allow disk blocks up to 65536 bytes */
201 char buf[DEV_BSIZE<<7];
203 if (ofdev.handle != -1)
204 panic("devopen");
205 if (of->f_flags != F_READ)
206 return EPERM;
207 strcpy(fname, name);
208 cp = filename(fname, &partition);
209 if (cp) {
210 DPRINTF("filename=%s\n", cp);
211 strcpy(buf, cp);
212 *cp = 0;
214 if (!cp || !*buf)
215 strcpy(buf, DEFAULT_KERNEL);
216 if (!*fname)
217 strcpy(fname, bootdev);
218 DPRINTF("fname=%s\n", fname);
219 strcpy(opened_name, fname);
220 if (partition) {
221 cp = opened_name + strlen(opened_name);
222 *cp++ = ':';
223 *cp++ = partition;
224 *cp = 0;
226 if (*buf != '/')
227 strcat(opened_name, "/");
228 strcat(opened_name, buf);
229 *file = opened_name + strlen(fname) + 1;
230 if (partition) {
231 *file += 2;
233 if ((handle = OF_finddevice(fname)) == -1) {
234 DPRINTF("OF_finddevice(\"%s\") failed\n", fname);
235 return ENOENT;
237 if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
238 return ENXIO;
239 floppyboot = !strcmp(buf, "floppy");
240 if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
241 return ENXIO;
242 if (!strcmp(buf, "block")) {
245 * For block devices, indicate raw partition
246 * (:0 in OpenFirmware)
249 strcat(fname, ":0");
251 DPRINTF("calling OF_open(fname=%s)\n", fname);
252 if ((handle = OF_open(fname)) == -1)
253 return ENXIO;
254 memset(&ofdev, 0, sizeof ofdev);
255 ofdev.handle = handle;
256 if (!strcmp(buf, "block")) {
257 ofdev.type = OFDEV_DISK;
258 ofdev.bsize = DEV_BSIZE;
260 /* First try to find a disklabel without MBR/RDB partitions */
261 if (strategy(&ofdev, F_READ,
262 LABELSECTOR, DEV_BSIZE, buf, &read) != 0
263 || read != DEV_BSIZE
264 || getdisklabel(buf, &label)) {
266 /* Else try MBR partitions */
267 error = search_mbr_label(&ofdev, 0, buf, &label, 0);
268 if (error && error != ERDLAB)
269 goto bad;
271 /* and finally try RDB partitions */
272 error = search_rdb_label(&ofdev, buf, &label);
273 if (error && error != ERDLAB)
274 goto bad;
277 if (error == ERDLAB) {
278 if (partition) {
281 * User specified a parititon,
282 * but there is none.
285 goto bad;
288 /* No label, just use complete disk */
289 ofdev.partoff = 0;
290 } else {
291 part = partition ? partition - 'a' : 0;
292 ofdev.partoff = label.d_partitions[part].p_offset;
293 if (label.d_partitions[part].p_fstype == FS_RAID) {
294 #define RF_PROTECTED_SECTORS 64
295 ofdev.partoff += RF_PROTECTED_SECTORS;
296 DPRINTF("devopen: found RAID partition, "
297 "adjusting offset to %lx\n", ofdev.partoff);
301 of->f_dev = devsw;
302 of->f_devdata = &ofdev;
303 file_system[0] = file_system_ufs;
304 file_system[1] = file_system_cd9660;
305 file_system[2] = file_system_dosfs;
306 nfsys = 3;
307 return 0;
309 if (!strcmp(buf, "network")) {
310 ofdev.type = OFDEV_NET;
311 of->f_dev = devsw;
312 of->f_devdata = &ofdev;
313 file_system[0] = file_system_nfs;
314 nfsys = 1;
315 if ((error = net_open(&ofdev)) != 0)
316 goto bad;
317 return 0;
319 error = EFTYPE;
320 bad:
321 OF_close(handle);
322 ofdev.handle = -1;
323 return error;