1 /* $NetBSD: ofdev.c,v 1.16 2009/01/12 07:49:57 tsutsui Exp $ */
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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>
56 extern char bootdev
[];
59 # define DPRINTF printf
61 # define DPRINTF while (0) printf
65 filename(char *str
, char *ppart
)
75 for (cp
= str
; *cp
; lp
= cp
) {
77 /* For each component of the path name... */
78 while (*++cp
&& *cp
!= '/')
83 /* ...look whether there is a device with this name */
84 dhandle
= OF_finddevice(str
);
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 */
97 --cp
>= str
&& *cp
!= '/' && *cp
!= ':';)
99 if (cp
>= str
&& *cp
== ':') {
102 * found some arguments,
103 * make OFW ignore them.
107 for (cp
= lp
; *--cp
&& *cp
!= ',';)
110 *cp
< 'a' + MAXPARTITIONS
)
116 if (OF_getprop(dhandle
, "device_type", devtype
,
124 strategy(void *devdata
, int rw
, daddr_t blk
, size_t size
, void *buf
,
127 struct of_dev
*dev
= devdata
;
133 if (dev
->type
!= OFDEV_DISK
)
136 pos
= (u_quad_t
)((blk
+ dev
->partoff
) * dev
->bsize
);
139 if (OF_seek(dev
->handle
, pos
) < 0)
141 n
= OF_read(dev
->handle
, buf
, size
);
153 devopen_dummy(struct open_file
*of
, ...) {
158 devclose(struct open_file
*of
)
160 struct of_dev
*op
= of
->f_devdata
;
162 if (op
->type
== OFDEV_NET
)
164 OF_close(op
->handle
);
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];
182 static struct of_dev ofdev
= {
186 char opened_name
[256];
191 devopen(struct open_file
*of
, const char *name
, char **file
)
196 struct disklabel label
;
200 /* allow disk blocks up to 65536 bytes */
201 char buf
[DEV_BSIZE
<<7];
203 if (ofdev
.handle
!= -1)
205 if (of
->f_flags
!= F_READ
)
208 cp
= filename(fname
, &partition
);
210 DPRINTF("filename=%s\n", cp
);
215 strcpy(buf
, DEFAULT_KERNEL
);
217 strcpy(fname
, bootdev
);
218 DPRINTF("fname=%s\n", fname
);
219 strcpy(opened_name
, fname
);
221 cp
= opened_name
+ strlen(opened_name
);
227 strcat(opened_name
, "/");
228 strcat(opened_name
, buf
);
229 *file
= opened_name
+ strlen(fname
) + 1;
233 if ((handle
= OF_finddevice(fname
)) == -1) {
234 DPRINTF("OF_finddevice(\"%s\") failed\n", fname
);
237 if (OF_getprop(handle
, "name", buf
, sizeof buf
) < 0)
239 floppyboot
= !strcmp(buf
, "floppy");
240 if (OF_getprop(handle
, "device_type", buf
, sizeof buf
) < 0)
242 if (!strcmp(buf
, "block")) {
245 * For block devices, indicate raw partition
246 * (:0 in OpenFirmware)
251 DPRINTF("calling OF_open(fname=%s)\n", fname
);
252 if ((handle
= OF_open(fname
)) == -1)
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
264 || getdisklabel(buf
, &label
)) {
266 /* Else try MBR partitions */
267 error
= search_mbr_label(&ofdev
, 0, buf
, &label
, 0);
268 if (error
&& error
!= ERDLAB
)
271 /* and finally try RDB partitions */
272 error
= search_rdb_label(&ofdev
, buf
, &label
);
273 if (error
&& error
!= ERDLAB
)
277 if (error
== ERDLAB
) {
281 * User specified a parititon,
288 /* No label, just use complete disk */
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
);
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
;
309 if (!strcmp(buf
, "network")) {
310 ofdev
.type
= OFDEV_NET
;
312 of
->f_devdata
= &ofdev
;
313 file_system
[0] = file_system_nfs
;
315 if ((error
= net_open(&ofdev
)) != 0)