8171 loader: distinguish NFS versus TFTP boot by rootpath
[unleashed.git] / usr / src / boot / sys / boot / common / dev_net.c
blob08ee5c8a86d088eaaed992eea4317e22045da783
1 /* $NetBSD: dev_net.c,v 1.23 2008/04/28 20:24:06 martin Exp $ */
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross.
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.
32 #include <sys/cdefs.h>
34 /*-
35 * This module implements a "raw device" interface suitable for
36 * use by the stand-alone I/O library NFS code. This interface
37 * does not support any "block" access, and exists only for the
38 * purpose of initializing the network interface, getting boot
39 * parameters, and performing the NFS mount.
41 * At open time, this does:
43 * find interface - netif_open()
44 * RARP for IP address - rarp_getipaddress()
45 * RPC/bootparams - callrpc(d, RPC_BOOTPARAMS, ...)
46 * RPC/mountd - nfs_mount(sock, ip, path)
48 * the root file handle from mountd is saved in a global
49 * for use by the NFS open code (NFS/lookup).
52 #include <machine/stdarg.h>
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <net/if.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
59 #include <stand.h>
60 #include <string.h>
61 #include <net.h>
62 #include <netif.h>
63 #include <bootp.h>
64 #include <bootparam.h>
66 #include "dev_net.h"
67 #include "bootstrap.h"
69 #ifdef NETIF_DEBUG
70 int debug = 0;
71 #endif
73 static char *netdev_name;
74 static int netdev_sock = -1;
75 static int netdev_opens;
77 static int net_init(void);
78 static int net_open(struct open_file *, ...);
79 static int net_close(struct open_file *);
80 static void net_cleanup(void);
81 static int net_strategy();
82 static int net_print(int);
84 static int net_getparams(int sock);
86 struct devsw netdev = {
87 "net",
88 DEVT_NET,
89 net_init,
90 net_strategy,
91 net_open,
92 net_close,
93 noioctl,
94 net_print,
95 net_cleanup
98 static int
99 net_init(void)
102 return (0);
106 * Called by devopen after it sets f->f_dev to our devsw entry.
107 * This opens the low-level device and sets f->f_devdata.
108 * This is declared with variable arguments...
110 static int
111 net_open(struct open_file *f, ...)
113 struct iodesc *d;
114 va_list args;
115 char *devname; /* Device part of file name (or NULL). */
116 int error = 0;
118 va_start(args, f);
119 devname = va_arg(args, char*);
120 va_end(args);
122 /* Before opening another interface, close the previous one first. */
123 if (netdev_sock >= 0 && strcmp(devname, netdev_name) != 0)
124 net_cleanup();
126 /* On first open, do netif open, mount, etc. */
127 if (netdev_opens == 0) {
128 /* Find network interface. */
129 if (netdev_sock < 0) {
130 netdev_sock = netif_open(devname);
131 if (netdev_sock < 0) {
132 printf("net_open: netif_open() failed\n");
133 return (ENXIO);
135 netdev_name = strdup(devname);
136 #ifdef NETIF_DEBUG
137 if (debug)
138 printf("net_open: netif_open() succeeded\n");
139 #endif
142 * If network params were not set by netif_open(), try to get
143 * them via bootp, rarp, etc.
145 if (rootip.s_addr == 0) {
146 /* Get root IP address, and path, etc. */
147 error = net_getparams(netdev_sock);
148 if (error) {
149 /* getparams makes its own noise */
150 free(netdev_name);
151 netif_close(netdev_sock);
152 netdev_sock = -1;
153 return (error);
157 * Set the variables required by the kernel's nfs_diskless
158 * mechanism. This is the minimum set of variables required to
159 * mount a root filesystem without needing to obtain additional
160 * info from bootp or other sources.
162 d = socktodesc(netdev_sock);
163 setenv("boot.netif.hwaddr", ether_sprintf(d->myea), 1);
164 setenv("boot.netif.ip", inet_ntoa(myip), 1);
165 setenv("boot.netif.netmask", intoa(netmask), 1);
166 setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
167 setenv("boot.netif.server", inet_ntoa(rootip), 1);
168 if (netproto == NET_TFTP) {
169 setenv("boot.tftproot.server", inet_ntoa(rootip), 1);
170 setenv("boot.tftproot.path", rootpath, 1);
171 } else {
172 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
173 setenv("boot.nfsroot.path", rootpath, 1);
175 if (intf_mtu != 0) {
176 char mtu[16];
177 snprintf(mtu, sizeof(mtu), "%u", intf_mtu);
178 setenv("boot.netif.mtu", mtu, 1);
181 netdev_opens++;
182 f->f_devdata = &netdev_sock;
183 return (error);
186 static int
187 net_close(struct open_file *f)
190 #ifdef NETIF_DEBUG
191 if (debug)
192 printf("net_close: opens=%d\n", netdev_opens);
193 #endif
195 f->f_devdata = NULL;
197 return (0);
200 static void
201 net_cleanup(void)
204 if (netdev_sock >= 0) {
205 #ifdef NETIF_DEBUG
206 if (debug)
207 printf("net_cleanup: calling netif_close()\n");
208 #endif
209 rootip.s_addr = 0;
210 free(netdev_name);
211 netif_close(netdev_sock);
212 netdev_sock = -1;
216 static int
217 net_strategy()
220 return (EIO);
223 #define SUPPORT_BOOTP
226 * Get info for NFS boot: our IP address, our hostname,
227 * server IP address, and our root path on the server.
228 * There are two ways to do this: The old, Sun way,
229 * and the more modern, BOOTP way. (RFC951, RFC1048)
231 * The default is to use the Sun bootparams RPC
232 * (because that is what the kernel will do).
233 * MD code can make try_bootp initialied data,
234 * which will override this common definition.
236 #ifdef SUPPORT_BOOTP
237 int try_bootp = 1;
238 #endif
240 extern n_long ip_convertaddr(char *p);
242 static int
243 net_getparams(int sock)
245 char buf[MAXHOSTNAMELEN];
246 n_long rootaddr, smask;
248 #ifdef SUPPORT_BOOTP
250 * Try to get boot info using BOOTP. If we succeed, then
251 * the server IP address, gateway, and root path will all
252 * be initialized. If any remain uninitialized, we will
253 * use RARP and RPC/bootparam (the Sun way) to get them.
255 if (try_bootp)
256 bootp(sock, BOOTP_NONE);
257 if (myip.s_addr != 0)
258 goto exit;
259 #ifdef NETIF_DEBUG
260 if (debug)
261 printf("net_open: BOOTP failed, trying RARP/RPC...\n");
262 #endif
263 #endif
266 * Use RARP to get our IP address. This also sets our
267 * netmask to the "natural" default for our address.
269 if (rarp_getipaddress(sock)) {
270 printf("net_open: RARP failed\n");
271 return (EIO);
273 printf("net_open: client addr: %s\n", inet_ntoa(myip));
275 /* Get our hostname, server IP address, gateway. */
276 if (bp_whoami(sock)) {
277 printf("net_open: bootparam/whoami RPC failed\n");
278 return (EIO);
280 #ifdef NETIF_DEBUG
281 if (debug)
282 printf("net_open: client name: %s\n", hostname);
283 #endif
286 * Ignore the gateway from whoami (unreliable).
287 * Use the "gateway" parameter instead.
289 smask = 0;
290 gateip.s_addr = 0;
291 if (bp_getfile(sock, "gateway", &gateip, buf) == 0) {
292 /* Got it! Parse the netmask. */
293 smask = ip_convertaddr(buf);
295 if (smask) {
296 netmask = smask;
297 #ifdef NETIF_DEBUG
298 if (debug)
299 printf("net_open: subnet mask: %s\n", intoa(netmask));
300 #endif
302 #ifdef NETIF_DEBUG
303 if (gateip.s_addr && debug)
304 printf("net_open: net gateway: %s\n", inet_ntoa(gateip));
305 #endif
307 /* Get the root server and pathname. */
308 if (bp_getfile(sock, "root", &rootip, rootpath)) {
309 printf("net_open: bootparam/getfile RPC failed\n");
310 return (EIO);
312 exit:
313 netproto = NET_TFTP;
314 if ((rootaddr = net_parse_rootpath()) != INADDR_NONE) {
315 netproto = NET_NFS;
316 rootip.s_addr = rootaddr;
319 #ifdef NETIF_DEBUG
320 if (debug) {
321 printf("net_open: server addr: %s\n", inet_ntoa(rootip));
322 printf("net_open: server path: %s\n", rootpath);
324 #endif
326 return (0);
329 static int
330 net_print(int verbose)
332 struct netif_driver *drv;
333 int i, d, cnt;
334 int ret = 0;
336 if (netif_drivers[0] == NULL)
337 return (ret);
339 printf("%s devices:", netdev.dv_name);
340 if ((ret = pager_output("\n")) != 0)
341 return (ret);
343 cnt = 0;
344 for (d = 0; netif_drivers[d]; d++) {
345 drv = netif_drivers[d];
346 for (i = 0; i < drv->netif_nifs; i++) {
347 printf("\t%s%d:", netdev.dv_name, cnt++);
348 if (verbose) {
349 printf(" (%s%d)", drv->netif_bname,
350 drv->netif_ifs[i].dif_unit);
352 if ((ret = pager_output("\n")) != 0)
353 return (ret);
356 return (ret);
360 * Strip the server's address off of the rootpath if present and return it in
361 * network byte order, leaving just the pathname part in the global rootpath.
363 uint32_t
364 net_parse_rootpath()
366 int i;
367 n_long addr = INADDR_NONE;
369 for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++)
370 if (rootpath[i] == ':')
371 break;
372 if (i && i != FNAME_SIZE && rootpath[i] == ':') {
373 rootpath[i++] = '\0';
374 addr = inet_addr(&rootpath[0]);
375 bcopy(&rootpath[i], rootpath, strlen(&rootpath[i])+1);
377 return (addr);