1 /* $NetBSD: netio.c,v 1.11 2005/12/11 12:17:19 christos 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(struct open_file *f) 153 { 154 /* On last close, do netif close, etc. */ 155 if (open_count > 0) 156 if (--open_count == 0) 157 netif_close(netdev_sock); 158 f->f_devdata = NULL; 159 160 return 0; 161 } 162 163 int 164 netstrategy(void *devdata, int func, daddr_t dblk, size_t size, void *v_buf, 165 size_t *rsize) 166 { 167 168 *rsize = size; 169 return EIO; 170 } 171 172 int 173 netmountroot(struct open_file *f, char *devname) 174 { 175 int error; 176 struct iodesc *d; 177 178 #ifdef DEBUG 179 printf("netmountroot: %s\n", devname); 180 #endif 181 182 if (netio_ask) { 183 get_my_ip: 184 printf("My IP address? "); 185 memset(input_line, 0, sizeof(input_line)); 186 gets(input_line); 187 if ((myip.s_addr = inet_addr(input_line)) == 188 htonl(INADDR_NONE)) { 189 printf("invalid IP address: %s\n", input_line); 190 goto get_my_ip; 191 } 192 193 get_my_netmask: 194 printf("My netmask? "); 195 memset(input_line, 0, sizeof(input_line)); 196 gets(input_line); 197 if ((netmask = inet_addr(input_line)) == 198 htonl(INADDR_NONE)) { 199 printf("invalid netmask: %s\n", input_line); 200 goto get_my_netmask; 201 } 202 203 get_my_gateway: 204 printf("My gateway? "); 205 memset(input_line, 0, sizeof(input_line)); 206 gets(input_line); 207 if ((gateip.s_addr = inet_addr(input_line)) == 208 htonl(INADDR_NONE)) { 209 printf("invalid IP address: %s\n", input_line); 210 goto get_my_gateway; 211 } 212 213 get_server_ip: 214 printf("Server IP address? "); 215 memset(input_line, 0, sizeof(input_line)); 216 gets(input_line); 217 if ((rootip.s_addr = inet_addr(input_line)) == 218 htonl(INADDR_NONE)) { 219 printf("invalid IP address: %s\n", input_line); 220 goto get_server_ip; 221 } 222 223 get_server_path: 224 printf("Server path? "); 225 memset(rootpath, 0, sizeof(rootpath)); 226 gets(rootpath); 227 if (rootpath[0] == '\0' || rootpath[0] == '\n') 228 goto get_server_path; 229 230 if ((d = socktodesc(netdev_sock)) == NULL) 231 return EMFILE; 232 233 d->myip = myip; 234 235 goto do_nfs_mount; 236 } 237 238 /* 239 * Get info for NFS boot: our IP address, our hostname, 240 * server IP address, and our root path on the server. 241 * There are two ways to do this: The old, Sun way, 242 * and the more modern, BOOTP way. (RFC951, RFC1048) 243 */ 244 245 #ifdef SUN_BOOTPARAMS 246 /* Get boot info using RARP and Sun bootparams. */ 247 248 /* Get our IP address. (rarp.c) */ 249 if (rarp_getipaddress(netdev_sock) == -1) 250 return errno; 251 252 printf("boot: client IP address: %s\n", inet_ntoa(myip)); 253 254 /* Get our hostname, server IP address. */ 255 if (bp_whoami(netdev_sock)) 256 return errno; 257 258 printf("boot: client name: %s\n", hostname); 259 260 /* Get the root pathname. */ 261 if (bp_getfile(netdev_sock, "root", &rootip, rootpath)) 262 return errno; 263 264 #else 265 266 /* Get boot info using BOOTP way. (RFC951, RFC1048) */ 267 bootp(netdev_sock); 268 269 printf("boot: client IP address: %s \n", inet_ntoa(myip)); 270 271 #endif /* SUN_BOOTPARAMS */ 272 273 printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath); 274 275 do_nfs_mount: 276 /* Get the NFS file handle (mount). */ 277 error = nfs_mount(netdev_sock, rootip, rootpath); 278 279 return error; 280 } 281