xref: /netbsd-src/sys/arch/newsmips/stand/boot/devopen.c (revision 1c9b56c830954ccf3b57004ac65562e3d6afacf6)
1 /*	$NetBSD: devopen.c,v 1.7 2005/02/06 02:18:03 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 = sizeof(devsw) / sizeof(devsw[0]);
57 
58 struct fs_ops file_system_ufs = {
59 	ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
60 };
61 struct fs_ops file_system_nfs = {
62 	nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
63 };
64 #ifdef SUPPORT_USTARFS
65 struct fs_ops file_system_ustarfs = {
66 	ustarfs_open, ustarfs_close, ustarfs_read, ustarfs_write,
67 	    ustarfs_seek, ustarfs_stat
68 };
69 struct fs_ops file_system[2];
70 #else
71 struct fs_ops file_system[1];
72 #endif
73 int nfsys;
74 
75 struct romdev romdev;
76 
77 extern int apbus;
78 
79 int
80 devopen(struct open_file *f, const char *fname, char **file)
81 {
82 	int fd;
83 	char *cp;
84 	int error = 0;
85 
86 	DPRINTF("devopen: %s\n", fname);
87 
88 	strcpy(romdev.devname, fname);
89 	cp = strchr(romdev.devname, ')') + 1;
90 	*cp = 0;
91 	if (apbus)
92 		fd = apcall_open(romdev.devname, 2);
93 	else
94 		fd = rom_open(romdev.devname, 2);
95 
96 	DPRINTF("devname = %s, fd = %d\n", romdev.devname, fd);
97 	if (fd == -1)
98 		return -1;
99 
100 	romdev.fd = fd;
101 	if (strncmp(romdev.devname, "sonic", 5) == 0)
102 		romdev.devtype = DT_NET;
103 	else
104 		romdev.devtype = DT_BLOCK;
105 
106 	f->f_dev = devsw;
107 	f->f_devdata = &romdev;
108 	*file = strchr(fname, ')') + 1;
109 
110 	if (romdev.devtype == DT_BLOCK) {
111 		file_system[0] = file_system_ufs;
112 #ifdef SUPPORT_USTARFS
113 		file_system[1] = file_system_ustarfs;
114 		nfsys = 2;
115 #else
116 		nfsys = 1;
117 #endif
118 	} else {	/* DT_NET */
119 		file_system[0] = file_system_nfs;
120 		nfsys = 1;
121 
122 		if ((error = net_open(&romdev)) != 0) {
123 			printf("Can't open NFS network connection on `%s'\n",
124 			    romdev.devname);
125 			return error;
126 		}
127 	}
128 
129 	return 0;
130 }
131 
132 int
133 dkopen(struct open_file *f, ...)
134 {
135 
136 	DPRINTF("dkopen\n");
137 	return 0;
138 }
139 
140 int
141 dkclose(struct open_file *f)
142 {
143 	struct romdev *dev = f->f_devdata;
144 
145 	DPRINTF("dkclose\n");
146 	if (apbus)
147 		apcall_close(dev->fd);
148 	else
149 		rom_close(dev->fd);
150 
151 	return 0;
152 }
153 
154 int
155 dkstrategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
156     size_t *rsize)
157 {
158 	struct romdev *dev = devdata;
159 
160 	/* XXX should use partition offset */
161 
162 	if (apbus) {
163 		apcall_lseek(dev->fd, blk * 512, 0);
164 		apcall_read(dev->fd, buf, size);
165 	} else {
166 		rom_lseek(dev->fd, blk * 512, 0);
167 		rom_read(dev->fd, buf, size);
168 	}
169 	*rsize = size;		/* XXX */
170 	return 0;
171 }
172 
173 #ifdef HAVE_CHANGEDISK_HOOK
174 void
175 changedisk_hook(struct open_file *f)
176 {
177 	struct romdev *dev = f->f_devdata;
178 
179 	if (apbus) {
180 		apcall_ioctl(dev->fd, APIOCEJECT, NULL);
181 		apcall_close(dev->fd);
182 		getchar();
183 		dev->fd = apcall_open(dev->devname, 2);
184 	} else {
185 		rom_ioctl(dev->fd, SYSIOCEJECT, NULL);
186 		rom_close(dev->fd);
187 		getchar();
188 		dev->fd = rom_open(dev->devname, 2);
189 	}
190 }
191 #endif
192