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