No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / hp300 / stand / common / netio.c
blob1562dbb3abbfe7b9367b7d7324429ff715b2f58d
1 /* $NetBSD: netio.c,v 1.12 2008/04/28 20:23:19 martin Exp $ */
3 /*-
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1995 Gordon W. Ross
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 * This module implements a "raw device" interface suitable for
59 * use by the stand-alone I/O library NFS code. This interface
60 * does not support any "block" access, and exists only for the
61 * purpose of initializing the network interface, getting boot
62 * parameters, and performing the NFS mount.
64 * At open time, this does:
66 * find interface - netif_open()
67 * RARP for IP address - rarp_getipaddress()
68 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
69 * RPC/mountd - nfs_mount(sock, ip, path)
71 * the root file handle from mountd is saved in a global
72 * for use by the NFS open code (NFS/lookup).
75 #include <sys/param.h>
76 #include <sys/socket.h>
78 #include <machine/stdarg.h>
80 #include <net/if.h>
81 #include <netinet/in.h>
82 #include <netinet/in_systm.h>
84 #include <lib/libsa/stand.h>
85 #include <lib/libsa/net.h>
86 #include <lib/libsa/netif.h>
87 #include <lib/libsa/bootp.h>
88 #include <lib/libsa/bootparam.h>
89 #include <lib/libsa/nfs.h>
91 #include <lib/libkern/libkern.h>
93 #include <hp300/stand/common/conf.h>
94 #include <hp300/stand/common/samachdep.h>
96 extern int nfs_root_node[]; /* XXX - get from nfs_mount() */
98 struct in_addr myip, rootip, gateip;
99 n_long netmask;
100 char rootpath[FNAME_SIZE];
102 static int netdev_sock = -1;
103 static int open_count;
105 int netio_ask = 0; /* default to bootparam, can override */
107 static char input_line[100];
109 int netmountroot(struct open_file *, char *);
112 * Called by devopen after it sets f->f_dev to our devsw entry.
113 * This opens the low-level device and sets f->f_devdata.
116 netopen(struct open_file *f, ...)
118 va_list ap;
119 char *devname;
120 int error = 0;
122 va_start(ap, f);
123 devname = va_arg(ap, char *);
124 va_end(ap);
126 /* On first open, do netif open, mount, etc. */
127 if (open_count == 0) {
128 /* Find network interface. */
129 if ((netdev_sock = netif_open(devname)) < 0)
130 return ENXIO;
131 if ((error = netmountroot(f, devname)) != 0)
132 return error;
134 open_count++;
135 f->f_devdata = nfs_root_node;
136 return error;
140 netclose(struct open_file *f)
142 /* On last close, do netif close, etc. */
143 if (open_count > 0)
144 if (--open_count == 0)
145 netif_close(netdev_sock);
146 f->f_devdata = NULL;
148 return 0;
152 netstrategy(void *devdata, int func, daddr_t dblk, size_t size, void *v_buf,
153 size_t *rsize)
156 *rsize = size;
157 return EIO;
161 netmountroot(struct open_file *f, char *devname)
163 int error;
164 struct iodesc *d;
166 #ifdef DEBUG
167 printf("netmountroot: %s\n", devname);
168 #endif
170 if (netio_ask) {
171 get_my_ip:
172 printf("My IP address? ");
173 memset(input_line, 0, sizeof(input_line));
174 gets(input_line);
175 if ((myip.s_addr = inet_addr(input_line)) ==
176 htonl(INADDR_NONE)) {
177 printf("invalid IP address: %s\n", input_line);
178 goto get_my_ip;
181 get_my_netmask:
182 printf("My netmask? ");
183 memset(input_line, 0, sizeof(input_line));
184 gets(input_line);
185 if ((netmask = inet_addr(input_line)) ==
186 htonl(INADDR_NONE)) {
187 printf("invalid netmask: %s\n", input_line);
188 goto get_my_netmask;
191 get_my_gateway:
192 printf("My gateway? ");
193 memset(input_line, 0, sizeof(input_line));
194 gets(input_line);
195 if ((gateip.s_addr = inet_addr(input_line)) ==
196 htonl(INADDR_NONE)) {
197 printf("invalid IP address: %s\n", input_line);
198 goto get_my_gateway;
201 get_server_ip:
202 printf("Server IP address? ");
203 memset(input_line, 0, sizeof(input_line));
204 gets(input_line);
205 if ((rootip.s_addr = inet_addr(input_line)) ==
206 htonl(INADDR_NONE)) {
207 printf("invalid IP address: %s\n", input_line);
208 goto get_server_ip;
211 get_server_path:
212 printf("Server path? ");
213 memset(rootpath, 0, sizeof(rootpath));
214 gets(rootpath);
215 if (rootpath[0] == '\0' || rootpath[0] == '\n')
216 goto get_server_path;
218 if ((d = socktodesc(netdev_sock)) == NULL)
219 return EMFILE;
221 d->myip = myip;
223 goto do_nfs_mount;
227 * Get info for NFS boot: our IP address, our hostname,
228 * server IP address, and our root path on the server.
229 * There are two ways to do this: The old, Sun way,
230 * and the more modern, BOOTP way. (RFC951, RFC1048)
233 #ifdef SUN_BOOTPARAMS
234 /* Get boot info using RARP and Sun bootparams. */
236 /* Get our IP address. (rarp.c) */
237 if (rarp_getipaddress(netdev_sock) == -1)
238 return errno;
240 printf("boot: client IP address: %s\n", inet_ntoa(myip));
242 /* Get our hostname, server IP address. */
243 if (bp_whoami(netdev_sock))
244 return errno;
246 printf("boot: client name: %s\n", hostname);
248 /* Get the root pathname. */
249 if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
250 return errno;
252 #else
254 /* Get boot info using BOOTP way. (RFC951, RFC1048) */
255 bootp(netdev_sock);
257 printf("boot: client IP address: %s \n", inet_ntoa(myip));
259 #endif /* SUN_BOOTPARAMS */
261 printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
263 do_nfs_mount:
264 /* Get the NFS file handle (mount). */
265 error = nfs_mount(netdev_sock, rootip, rootpath);
267 return error;