xref: /netbsd-src/sys/arch/hp300/stand/common/netio.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: netio.c,v 1.4 1997/10/04 17:20:20 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1995 Gordon W. Ross
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. The name of the author may not be used to endorse or promote products
52  *    derived from this software without specific prior written permission.
53  * 4. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *      This product includes software developed by Gordon W. Ross
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  */
68 
69 /*
70  * This module implements a "raw device" interface suitable for
71  * use by the stand-alone I/O library NFS code.  This interface
72  * does not support any "block" access, and exists only for the
73  * purpose of initializing the network interface, getting boot
74  * parameters, and performing the NFS mount.
75  *
76  * At open time, this does:
77  *
78  * find interface      - netif_open()
79  * RARP for IP address - rarp_getipaddress()
80  * RPC/bootparams      - callrpc(d, RPC_BOOTPARAMS, ...)
81  * RPC/mountd          - nfs_mount(sock, ip, path)
82  *
83  * the root file handle from mountd is saved in a global
84  * for use by the NFS open code (NFS/lookup).
85  */
86 
87 #include <sys/param.h>
88 #include <sys/socket.h>
89 
90 #include <net/if.h>
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 
94 #include <lib/libsa/stand.h>
95 #include <lib/libsa/net.h>
96 #include <lib/libsa/netif.h>
97 #include <lib/libsa/bootparam.h>
98 #include <lib/libsa/nfs.h>
99 
100 #include <hp300/stand/common/samachdep.h>
101 
102 extern int nfs_root_node[];	/* XXX - get from nfs_mount() */
103 
104 struct	in_addr myip, rootip, gateip;
105 n_long	netmask;
106 char rootpath[FNAME_SIZE];
107 
108 int netdev_sock = -1;
109 static int open_count;
110 
111 int netio_ask = 0;		/* default to bootparam, can override */
112 
113 static	char input_line[100];
114 
115 /* Why be any different? */
116 #define SUN_BOOTPARAMS
117 
118 /*
119  * Called by devopen after it sets f->f_dev to our devsw entry.
120  * This opens the low-level device and sets f->f_devdata.
121  */
122 int
123 netopen(f, devname)
124 	struct open_file *f;
125 	char *devname;		/* Device part of file name (or NULL). */
126 {
127 	int error = 0;
128 
129 	/* On first open, do netif open, mount, etc. */
130 	if (open_count == 0) {
131 		/* Find network interface. */
132 		if ((netdev_sock = netif_open(devname)) < 0)
133 			return (error=ENXIO);
134 		if ((error = netmountroot(f, devname)) != 0)
135 			return (error);
136 	}
137 	open_count++;
138 	f->f_devdata = nfs_root_node;
139 	return (error);
140 }
141 
142 int
143 netclose(f)
144 	struct open_file *f;
145 {
146 	/* On last close, do netif close, etc. */
147 	if (open_count > 0)
148 		if (--open_count == 0)
149 			netif_close(netdev_sock);
150 	f->f_devdata = NULL;
151 }
152 
153 int
154 netstrategy(devdata, func, dblk, size, v_buf, rsize)
155 	void *devdata;
156 	int func;
157 	daddr_t dblk;
158 	size_t size;
159 	void *v_buf;
160 	size_t *rsize;
161 {
162 
163 	*rsize = size;
164 	return EIO;
165 }
166 
167 int
168 netmountroot(f, devname)
169 	struct open_file *f;
170 	char *devname;		/* Device part of file name (or NULL). */
171 {
172 	int error;
173 	struct iodesc *d;
174 
175 #ifdef DEBUG
176 	printf("netmountroot: %s\n", devname);
177 #endif
178 
179 	if (netio_ask) {
180  get_my_ip:
181 		printf("My IP address? ");
182 		bzero(input_line, sizeof(input_line));
183 		gets(input_line);
184 		if ((myip.s_addr = inet_addr(input_line)) ==
185 		    htonl(INADDR_NONE)) {
186 			printf("invalid IP address: %s\n", input_line);
187 			goto get_my_ip;
188 		}
189 
190  get_my_netmask:
191 		printf("My netmask? ");
192 		bzero(input_line, sizeof(input_line));
193 		gets(input_line);
194 		if ((netmask = inet_addr(input_line)) ==
195 		    htonl(INADDR_NONE)) {
196 			printf("invalid netmask: %s\n", input_line);
197 			goto get_my_netmask;
198 		}
199 
200  get_my_gateway:
201 		printf("My gateway? ");
202 		bzero(input_line, sizeof(input_line));
203 		gets(input_line);
204 		if ((gateip.s_addr = inet_addr(input_line)) ==
205 		    htonl(INADDR_NONE)) {
206 			printf("invalid IP address: %s\n", input_line);
207 			goto get_my_gateway;
208 		}
209 
210  get_server_ip:
211 		printf("Server IP address? ");
212 		bzero(input_line, sizeof(input_line));
213 		gets(input_line);
214 		if ((rootip.s_addr = inet_addr(input_line)) ==
215 		    htonl(INADDR_NONE)) {
216 			printf("invalid IP address: %s\n", input_line);
217 			goto get_server_ip;
218 		}
219 
220  get_server_path:
221 		printf("Server path? ");
222 		bzero(rootpath, sizeof(rootpath));
223 		gets(rootpath);
224 		if (rootpath[0] == '\0' || rootpath[0] == '\n')
225 			goto get_server_path;
226 
227 		if ((d = socktodesc(netdev_sock)) == NULL)
228 			return (EMFILE);
229 
230 		d->myip = myip;
231 
232 		goto do_nfs_mount;
233 	}
234 
235 	/*
236 	 * Get info for NFS boot: our IP address, our hostname,
237 	 * server IP address, and our root path on the server.
238 	 * There are two ways to do this:  The old, Sun way,
239 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
240 	 */
241 
242 #ifdef	SUN_BOOTPARAMS
243 	/* Get boot info using RARP and Sun bootparams. */
244 
245 	/* Get our IP address.  (rarp.c) */
246 	if (rarp_getipaddress(netdev_sock) == -1)
247 		return (errno);
248 
249 	printf("boot: client IP address: %s\n", inet_ntoa(myip));
250 
251 	/* Get our hostname, server IP address. */
252 	if (bp_whoami(netdev_sock))
253 		return (errno);
254 
255 	printf("boot: client name: %s\n", hostname);
256 
257 	/* Get the root pathname. */
258 	if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
259 		return (errno);
260 
261 #else
262 
263 	/* Get boot info using BOOTP way. (RFC951, RFC1048) */
264 	bootp(netdev_sock);
265 
266 	printf("Using IP address: %s\n", inet_ntoa(myip));
267 
268 	printf("myip: %s (%s)", hostname, inet_ntoa(myip));
269 	if (gateip)
270 		printf(", gateip: %s", inet_ntoa(gateip));
271 	if (mask)
272 		printf(", mask: %s", intoa(netmask));
273 	printf("\n");
274 
275 #endif /* SUN_BOOTPARAMS */
276 
277 	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
278 
279  do_nfs_mount:
280 	/* Get the NFS file handle (mount). */
281 	error = nfs_mount(netdev_sock, rootip, rootpath);
282 
283 	return (error);
284 }
285