xref: /netbsd/sys/arch/newsmips/stand/boot/devopen.c (revision bf9ec67e)
1 /*	$NetBSD: devopen.c,v 1.4 2002/04/30 01:14:39 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(x) printf x
42 #else
43 # define DPRINTF(x)
44 #endif
45 
46 int dkopen __P((struct open_file *, ...));
47 int dkclose __P((struct open_file *));
48 int dkstrategy __P((void *, int, daddr_t, size_t, void *, size_t *));
49 
50 struct devsw devsw[] = {
51 	{ "dk", dkstrategy, dkopen, dkclose, noioctl }
52 };
53 int ndevs = sizeof(devsw) / sizeof(devsw[0]);
54 
55 struct fs_ops file_system_ufs = {
56 	ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
57 };
58 struct fs_ops file_system_nfs = {
59 	nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
60 };
61 #ifdef SUPPORT_USTARFS
62 struct fs_ops file_system_ustarfs = {
63 	ustarfs_open, ustarfs_close, ustarfs_read, ustarfs_write,
64 	    ustarfs_seek, ustarfs_stat
65 };
66 struct fs_ops file_system[2];
67 #else
68 struct fs_ops file_system[1];
69 #endif
70 int nfsys;
71 
72 struct romdev romdev;
73 
74 extern int apbus;
75 
76 int
77 devopen(f, fname, file)
78 	struct open_file *f;
79 	const char *fname;
80 	char **file;	/* out */
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 	DPRINTF(("dkopen\n"));
136 	return 0;
137 }
138 
139 int
140 dkclose(f)
141 	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(devdata, rw, blk, size, buf, rsize)
156 	void *devdata;
157 	int rw;
158 	daddr_t blk;
159 	size_t size;
160 	void *buf;
161 	size_t *rsize;	/* out: number of bytes transfered */
162 {
163 	struct romdev *dev = devdata;
164 
165 	/* XXX should use partition offset */
166 
167 	if (apbus) {
168 		apcall_lseek(dev->fd, blk * 512, 0);
169 		apcall_read(dev->fd, buf, size);
170 	} else {
171 		rom_lseek(dev->fd, blk * 512, 0);
172 		rom_read(dev->fd, buf, size);
173 	}
174 	*rsize = size;		/* XXX */
175 	return 0;
176 }
177 
178 #ifdef HAVE_CHANGEDISK_HOOK
179 void
180 changedisk_hook(f)
181 	struct open_file *f;
182 {
183 	struct romdev *dev = f->f_devdata;
184 
185 	if (apbus) {
186 		apcall_ioctl(dev->fd, APIOCEJECT, NULL);
187 		apcall_close(dev->fd);
188 		getchar();
189 		dev->fd = apcall_open(dev->devname, 2);
190 	} else {
191 		rom_ioctl(dev->fd, SYSIOCEJECT, NULL);
192 		rom_close(dev->fd);
193 		getchar();
194 		dev->fd = rom_open(dev->devname, 2);
195 	}
196 
197 }
198 #endif
199