Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / alpha / stand / netboot / dev_net.c
bloba4144e93d8ecc1eef4fd1383b7e0e78d04097649
1 /* $NetBSD: dev_net.c,v 1.14 2009/03/14 15:36:00 dsl Exp $ */
3 /*
4 * Copyright (c) 1995 Gordon W. Ross
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * This module implements a "raw device" interface suitable for
30 * use by the stand-alone I/O library NFS code. This interface
31 * does not support any "block" access, and exists only for the
32 * purpose of initializing the network interface, getting boot
33 * parameters, and performing the NFS mount.
35 * At open time, this does:
37 * find interface - netif_open()
38 * RARP for IP address - rarp_getipaddress()
39 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
40 * RPC/mountd - nfs_mount(sock, ip, path)
42 * the root file handle from mountd is saved in a global
43 * for use by the NFS open code (NFS/lookup).
46 #include <machine/stdarg.h>
47 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <net/if.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
53 #include <lib/libsa/stand.h>
54 #include <lib/libsa/net.h>
55 #include <lib/libsa/netif.h>
56 #include <lib/libsa/bootparam.h>
57 #include <lib/libsa/nfs.h>
59 #include <lib/libkern/libkern.h>
61 #include "dev_net.h"
63 #ifndef SUN_BOOTPARAMS
64 void bootp(int);
65 #endif
67 extern int debug;
68 extern int nfs_root_node[]; /* XXX - get from nfs_mount() */
71 * Various globals needed by the network code:
74 #if 0
75 /* for arp.c, rarp.c */
76 u_char bcea[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
77 #endif
79 struct in_addr myip; /* my ip address */
80 struct in_addr rootip; /* root ip address */
81 struct in_addr gateip; /* swap ip address */
82 n_long netmask; /* subnet or net mask */
84 char rootpath[FNAME_SIZE];
85 char hostname[FNAME_SIZE];
88 * Local things...
90 static int netdev_sock = -1;
91 static int netdev_opens;
93 int net_getparams(int);
96 * Called by devopen after it sets f->f_dev to our devsw entry.
97 * This opens the low-level device and sets f->f_devdata.
98 * This is declared with variable arguments...
101 net_open(struct open_file *f, ...)
103 va_list ap;
104 char *devname; /* Device part of file name (or NULL). */
105 int error = 0;
107 va_start(ap, f);
108 devname = va_arg(ap, char*);
109 va_end(ap);
111 #ifdef NETIF_DEBUG
112 if (debug)
113 printf("net_open: %s\n", devname);
114 #endif
116 /* On first open, do netif open, mount, etc. */
117 if (netdev_opens == 0) {
118 /* Find network interface. */
119 if (netdev_sock < 0) {
120 netdev_sock = netif_open(devname);
121 if (netdev_sock < 0) {
122 printf("net_open: netif_open() failed\n");
123 return (ENXIO);
125 if (debug)
126 printf("net_open: netif_open() succeeded\n");
128 if (rootip.s_addr == 0) {
129 /* Get root IP address, and path, etc. */
130 error = net_getparams(netdev_sock);
131 if (error) {
132 /* getparams makes its own noise */
133 goto fail;
135 /* Get the NFS file handle (mountd). */
136 error = nfs_mount(netdev_sock, rootip, rootpath);
137 if (error) {
138 error = errno;
139 printf("net_open: NFS mount error=%d\n", error);
140 rootip.s_addr = 0;
141 fail:
142 netif_close(netdev_sock);
143 netdev_sock = -1;
144 return (error);
146 if (debug)
147 printf("net_open: NFS mount succeeded\n");
150 netdev_opens++;
151 f->f_devdata = nfs_root_node;
152 return (error);
156 net_close(struct open_file *f)
159 #ifdef NETIF_DEBUG
160 if (debug)
161 printf("net_close: opens=%d\n", netdev_opens);
162 #endif
164 /* On last close, do netif close, etc. */
165 f->f_devdata = NULL;
166 /* Extra close call? */
167 if (netdev_opens <= 0)
168 return (0);
169 netdev_opens--;
170 /* Not last close? */
171 if (netdev_opens > 0)
172 return(0);
173 rootip.s_addr = 0;
174 if (netdev_sock >= 0) {
175 if (debug)
176 printf("net_close: calling netif_close()\n");
177 netif_close(netdev_sock);
178 netdev_sock = -1;
180 return (0);
184 net_ioctl(struct open_file *f, u_long cmd, void *data)
186 return EIO;
190 net_strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf, size_t *rsize)
192 return EIO;
196 net_getparams(int sock)
199 * Get info for NFS boot: our IP address, our hostname,
200 * server IP address, and our root path on the server.
201 * There are two ways to do this: The old, Sun way,
202 * and the more modern, BOOTP way. (RFC951, RFC1048)
205 #ifdef SUN_BOOTPARAMS
206 /* Get our IP address. (rarp.c) */
207 if (rarp_getipaddress(sock)) {
208 printf("net_open: RARP failed\n");
209 return (EIO);
211 #else /* BOOTPARAMS */
213 * Get boot info using BOOTP. (RFC951, RFC1048)
214 * This also gets the server IP address, gateway,
215 * root path, etc.
217 bootp(sock);
218 if (myip.s_addr == 0) {
219 printf("net_open: BOOTP failed\n");
220 return (EIO);
222 #endif /* BOOTPARAMS */
224 printf("boot: client addr: %s\n", inet_ntoa(myip));
226 #ifdef SUN_BOOTPARAMS
227 /* Get our hostname, server IP address, gateway. */
228 if (bp_whoami(sock)) {
229 printf("net_open: bootparam/whoami RPC failed\n");
230 return (EIO);
232 #endif /* BOOTPARAMS */
234 printf("boot: client name: %s\n", hostname);
235 if (gateip.s_addr) {
236 printf("boot: subnet mask: %s\n", intoa(netmask));
237 printf("boot: net gateway: %s\n", inet_ntoa(gateip));
240 #ifdef SUN_BOOTPARAMS
241 /* Get the root pathname. */
242 if (bp_getfile(sock, "root", &rootip, rootpath)) {
243 printf("net_open: bootparam/getfile RPC failed\n");
244 return (EIO);
246 #endif /* BOOTPARAMS */
248 printf("boot: server addr: %s\n", inet_ntoa(rootip));
249 printf("boot: server path: %s\n", rootpath);
251 return (0);