xref: /netbsd-src/sys/arch/hp300/stand/common/netio.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: netio.c,v 1.7 1999/11/13 21:21:38 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 <lib/libkern/libkern.h>
101 
102 #include <hp300/stand/common/samachdep.h>
103 
104 extern int nfs_root_node[];	/* XXX - get from nfs_mount() */
105 
106 struct	in_addr myip, rootip, gateip;
107 n_long	netmask;
108 char rootpath[FNAME_SIZE];
109 
110 int netdev_sock = -1;
111 static int open_count;
112 
113 int netio_ask = 0;		/* default to bootparam, can override */
114 
115 static	char input_line[100];
116 
117 /* Why be any different? */
118 #define SUN_BOOTPARAMS
119 
120 /*
121  * Called by devopen after it sets f->f_dev to our devsw entry.
122  * This opens the low-level device and sets f->f_devdata.
123  */
124 int
125 netopen(f, devname)
126 	struct open_file *f;
127 	char *devname;		/* Device part of file name (or NULL). */
128 {
129 	int error = 0;
130 
131 	/* On first open, do netif open, mount, etc. */
132 	if (open_count == 0) {
133 		/* Find network interface. */
134 		if ((netdev_sock = netif_open(devname)) < 0)
135 			return (error=ENXIO);
136 		if ((error = netmountroot(f, devname)) != 0)
137 			return (error);
138 	}
139 	open_count++;
140 	f->f_devdata = nfs_root_node;
141 	return (error);
142 }
143 
144 int
145 netclose(f)
146 	struct open_file *f;
147 {
148 	/* On last close, do netif close, etc. */
149 	if (open_count > 0)
150 		if (--open_count == 0)
151 			netif_close(netdev_sock);
152 	f->f_devdata = NULL;
153 }
154 
155 int
156 netstrategy(devdata, func, dblk, size, v_buf, rsize)
157 	void *devdata;
158 	int func;
159 	daddr_t dblk;
160 	size_t size;
161 	void *v_buf;
162 	size_t *rsize;
163 {
164 
165 	*rsize = size;
166 	return EIO;
167 }
168 
169 int
170 netmountroot(f, devname)
171 	struct open_file *f;
172 	char *devname;		/* Device part of file name (or NULL). */
173 {
174 	int error;
175 	struct iodesc *d;
176 
177 #ifdef DEBUG
178 	printf("netmountroot: %s\n", devname);
179 #endif
180 
181 	if (netio_ask) {
182  get_my_ip:
183 		printf("My IP address? ");
184 		bzero(input_line, sizeof(input_line));
185 		gets(input_line);
186 		if ((myip.s_addr = inet_addr(input_line)) ==
187 		    htonl(INADDR_NONE)) {
188 			printf("invalid IP address: %s\n", input_line);
189 			goto get_my_ip;
190 		}
191 
192  get_my_netmask:
193 		printf("My netmask? ");
194 		bzero(input_line, sizeof(input_line));
195 		gets(input_line);
196 		if ((netmask = inet_addr(input_line)) ==
197 		    htonl(INADDR_NONE)) {
198 			printf("invalid netmask: %s\n", input_line);
199 			goto get_my_netmask;
200 		}
201 
202  get_my_gateway:
203 		printf("My gateway? ");
204 		bzero(input_line, sizeof(input_line));
205 		gets(input_line);
206 		if ((gateip.s_addr = inet_addr(input_line)) ==
207 		    htonl(INADDR_NONE)) {
208 			printf("invalid IP address: %s\n", input_line);
209 			goto get_my_gateway;
210 		}
211 
212  get_server_ip:
213 		printf("Server IP address? ");
214 		bzero(input_line, sizeof(input_line));
215 		gets(input_line);
216 		if ((rootip.s_addr = inet_addr(input_line)) ==
217 		    htonl(INADDR_NONE)) {
218 			printf("invalid IP address: %s\n", input_line);
219 			goto get_server_ip;
220 		}
221 
222  get_server_path:
223 		printf("Server path? ");
224 		bzero(rootpath, sizeof(rootpath));
225 		gets(rootpath);
226 		if (rootpath[0] == '\0' || rootpath[0] == '\n')
227 			goto get_server_path;
228 
229 		if ((d = socktodesc(netdev_sock)) == NULL)
230 			return (EMFILE);
231 
232 		d->myip = myip;
233 
234 		goto do_nfs_mount;
235 	}
236 
237 	/*
238 	 * Get info for NFS boot: our IP address, our hostname,
239 	 * server IP address, and our root path on the server.
240 	 * There are two ways to do this:  The old, Sun way,
241 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
242 	 */
243 
244 #ifdef	SUN_BOOTPARAMS
245 	/* Get boot info using RARP and Sun bootparams. */
246 
247 	/* Get our IP address.  (rarp.c) */
248 	if (rarp_getipaddress(netdev_sock) == -1)
249 		return (errno);
250 
251 	printf("boot: client IP address: %s\n", inet_ntoa(myip));
252 
253 	/* Get our hostname, server IP address. */
254 	if (bp_whoami(netdev_sock))
255 		return (errno);
256 
257 	printf("boot: client name: %s\n", hostname);
258 
259 	/* Get the root pathname. */
260 	if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
261 		return (errno);
262 
263 #else
264 
265 	/* Get boot info using BOOTP way. (RFC951, RFC1048) */
266 	bootp(netdev_sock);
267 
268 	printf("Using IP address: %s\n", inet_ntoa(myip));
269 
270 	printf("myip: %s (%s)", hostname, inet_ntoa(myip));
271 	if (gateip)
272 		printf(", gateip: %s", inet_ntoa(gateip));
273 	if (mask)
274 		printf(", mask: %s", intoa(netmask));
275 	printf("\n");
276 
277 #endif /* SUN_BOOTPARAMS */
278 
279 	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
280 
281  do_nfs_mount:
282 	/* Get the NFS file handle (mount). */
283 	error = nfs_mount(netdev_sock, rootip, rootpath);
284 
285 	return (error);
286 }
287