1 /* $NetBSD: devopen.c,v 1.10 2008/02/21 14:30:41 tsutsui Exp $ */
2
3 /*-
4 * Copyright (C) 1999 Tsubai Masanari. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <lib/libkern/libkern.h>
30 #include <lib/libsa/stand.h>
31 #include <lib/libsa/ufs.h>
32 #include <lib/libsa/ustarfs.h>
33 #include <netinet/in.h>
34 #include <lib/libsa/nfs.h>
35
36 #include <machine/apcall.h>
37 #include <machine/romcall.h>
38 #include <promdev.h>
39
40 #ifdef BOOT_DEBUG
41 # define DPRINTF printf
42 #else
43 # define DPRINTF while (0) printf
44 #endif
45
46 int dkopen(struct open_file *, ...);
47 int dkclose(struct open_file *);
48 int dkstrategy(void *, int, daddr_t, size_t, void *, size_t *);
49 #ifdef HAVE_CHANGEDISK_HOOK
50 void changedisk_hook(struct open_file *);
51 #endif
52
53 struct devsw devsw[] = {
54 { "dk", dkstrategy, dkopen, dkclose, noioctl }
55 };
56 int ndevs = __arraycount(devsw);
57
58 struct fs_ops file_system_ufs = FS_OPS(ufs);
59 struct fs_ops file_system_nfs = FS_OPS(nfs);
60 #ifdef SUPPORT_USTARFS
61 struct fs_ops file_system_ustarfs = FS_OPS(ustarfs);
62 struct fs_ops file_system[2];
63 #else
64 struct fs_ops file_system[1];
65 #endif
66 int nfsys;
67
68 struct romdev romdev;
69
70 extern int apbus;
71
72 int
devopen(struct open_file * f,const char * fname,char ** file)73 devopen(struct open_file *f, const char *fname, char **file)
74 {
75 int fd;
76 char *cp;
77 int error = 0;
78
79 DPRINTF("devopen: %s\n", fname);
80
81 strcpy(romdev.devname, fname);
82 cp = strchr(romdev.devname, ')') + 1;
83 *cp = 0;
84 if (apbus)
85 fd = apcall_open(romdev.devname, 2);
86 else
87 fd = rom_open(romdev.devname, 2);
88
89 DPRINTF("devname = %s, fd = %d\n", romdev.devname, fd);
90 if (fd == -1)
91 return -1;
92
93 romdev.fd = fd;
94 if (strncmp(romdev.devname, "sonic", 5) == 0)
95 romdev.devtype = DT_NET;
96 else
97 romdev.devtype = DT_BLOCK;
98
99 f->f_dev = devsw;
100 f->f_devdata = &romdev;
101 *file = strchr(fname, ')') + 1;
102
103 if (romdev.devtype == DT_BLOCK) {
104 file_system[0] = file_system_ufs;
105 #ifdef SUPPORT_USTARFS
106 file_system[1] = file_system_ustarfs;
107 nfsys = 2;
108 #else
109 nfsys = 1;
110 #endif
111 } else { /* DT_NET */
112 file_system[0] = file_system_nfs;
113 nfsys = 1;
114
115 if ((error = net_open(&romdev)) != 0) {
116 printf("Can't open NFS network connection on `%s'\n",
117 romdev.devname);
118 return error;
119 }
120 }
121
122 return 0;
123 }
124
125 int
dkopen(struct open_file * f,...)126 dkopen(struct open_file *f, ...)
127 {
128
129 DPRINTF("dkopen\n");
130 return 0;
131 }
132
133 int
dkclose(struct open_file * f)134 dkclose(struct open_file *f)
135 {
136 struct romdev *dev = f->f_devdata;
137
138 DPRINTF("dkclose\n");
139 if (apbus)
140 apcall_close(dev->fd);
141 else
142 rom_close(dev->fd);
143
144 return 0;
145 }
146
147 int
dkstrategy(void * devdata,int rw,daddr_t blk,size_t size,void * buf,size_t * rsize)148 dkstrategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
149 size_t *rsize)
150 {
151 struct romdev *dev = devdata;
152
153 /* XXX should use partition offset */
154
155 if (apbus) {
156 apcall_lseek(dev->fd, blk * 512, 0);
157 apcall_read(dev->fd, buf, size);
158 } else {
159 rom_lseek(dev->fd, blk * 512, 0);
160 rom_read(dev->fd, buf, size);
161 }
162 *rsize = size; /* XXX */
163 return 0;
164 }
165
166 #ifdef HAVE_CHANGEDISK_HOOK
167 void
changedisk_hook(struct open_file * f)168 changedisk_hook(struct open_file *f)
169 {
170 struct romdev *dev = f->f_devdata;
171
172 if (apbus) {
173 apcall_ioctl(dev->fd, APIOCEJECT, NULL);
174 apcall_close(dev->fd);
175 getchar();
176 dev->fd = apcall_open(dev->devname, 2);
177 } else {
178 rom_ioctl(dev->fd, SYSIOCEJECT, NULL);
179 rom_close(dev->fd);
180 getchar();
181 dev->fd = rom_open(dev->devname, 2);
182 }
183 }
184 #endif
185