xref: /netbsd-src/sys/arch/newsmips/stand/boot/devopen.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: devopen.c,v 1.2 1999/12/22 05:54:41 tsubai 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 <netinet/in.h>
33 #include <lib/libsa/nfs.h>
34 
35 #include <machine/apcall.h>
36 #include <machine/romcall.h>
37 #include <promdev.h>
38 
39 #ifdef BOOT_DEBUG
40 # define DPRINTF printf
41 #else
42 # define DPRINTF while (0) printf
43 #endif
44 
45 int dkopen __P((struct open_file *, ...));
46 int dkclose __P((struct open_file *));
47 int dkstrategy __P((void *, int, daddr_t, size_t, void *, size_t *));
48 
49 struct devsw devsw[] = {
50 	{ "dk", dkstrategy, dkopen, dkclose, noioctl }
51 };
52 int ndevs = sizeof(devsw) / sizeof(devsw[0]);
53 
54 struct fs_ops file_system_ufs[] = {
55 	{ ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat }
56 };
57 struct fs_ops file_system_nfs[] = {
58 	{ nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat },
59 };
60 struct fs_ops file_system[1];
61 int nfsys = sizeof(file_system) / sizeof(file_system[0]);
62 
63 struct romdev romdev;
64 
65 extern int apbus;
66 
67 int
68 devopen(f, fname, file)
69 	struct open_file *f;
70 	const char *fname;
71 	char **file;	/* out */
72 {
73 	int fd;
74 	char devname[32];
75 	char *cp;
76 	int error = 0;
77 
78 	DPRINTF("devopen: %s\n", fname);
79 
80 	strcpy(devname, fname);
81 	cp = strchr(devname, ')') + 1;
82 	*cp = 0;
83 	if (apbus)
84 		fd = apcall_open(devname, 2);
85 	else
86 		fd = rom_open(devname, 2);
87 
88 	DPRINTF("devname = %s, fd = %d\n", devname, fd);
89 	if (fd == -1)
90 		return -1;
91 
92 	romdev.fd = fd;
93 	if (strncmp(devname, "sonic", 5) == 0)
94 		romdev.devtype = DT_NET;
95 	else
96 		romdev.devtype = DT_BLOCK;
97 
98 	f->f_dev = devsw;
99 	f->f_devdata = &romdev;
100 	*file = strchr(fname, ')') + 1;
101 
102 	if (romdev.devtype == DT_BLOCK)
103 		bcopy(file_system_ufs, file_system, sizeof(file_system));
104 	else {	/* DT_NET */
105 		bcopy(file_system_nfs, file_system, sizeof(file_system));
106 
107 		if ((error = net_open(&romdev)) != 0) {
108 			printf("Can't open NFS network connection on `%s'\n",
109 			       devname);
110 			return error;
111 		}
112 	}
113 
114 	return 0;
115 }
116 
117 int
118 dkopen(struct open_file *f, ...)
119 {
120 	DPRINTF("dkopen\n");
121 	return 0;
122 }
123 
124 int
125 dkclose(f)
126 	struct open_file *f;
127 {
128 	struct romdev *dev = f->f_devdata;
129 
130 	DPRINTF("dkclose\n");
131 	if (apbus)
132 		apcall_close(dev->fd);
133 	else
134 		rom_close(dev->fd);
135 
136 	return 0;
137 }
138 
139 int
140 dkstrategy(devdata, rw, blk, size, buf, rsize)
141 	void *devdata;
142 	int rw;
143 	daddr_t blk;
144 	size_t size;
145 	void *buf;
146 	size_t *rsize;	/* out: number of bytes transfered */
147 {
148 	struct romdev *dev = devdata;
149 
150 	/* XXX should use partition offset */
151 
152 	if (apbus) {
153 		apcall_lseek(dev->fd, blk * 512, 0);
154 		apcall_read(dev->fd, buf, size);
155 	} else {
156 		rom_lseek(dev->fd, blk * 512, 0);
157 		rom_read(dev->fd, buf, size);
158 	}
159 	*rsize = size;		/* XXX */
160 	return 0;
161 }
162