1 /* $NetBSD: net.c,v 1.8 2006/07/13 20:03:34 uwe Exp $ */
4 * Copyright (c) 1995 Gordon W. Ross
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 <sys/param.h>
47 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
52 #include <lib/libsa/stand.h>
53 #include <lib/libsa/net.h>
54 #include <lib/libsa/netif.h>
55 #include <lib/libsa/bootparam.h>
56 #include <lib/libsa/bootp.h>
57 #include <lib/libsa/nfs.h>
59 #include <lib/libkern/libkern.h>
61 #include <sparc/stand/common/promdev.h>
63 char rootpath
[FNAME_SIZE
];
66 static int open_count
;
68 static int net_mountroot_bootparams(void);
69 static int net_mountroot_bootp(void);
72 * Called by devopen after it sets f->f_dev to our devsw entry.
73 * This opens the low-level device and sets f->f_devdata.
76 net_open(struct promdata
*pd
)
80 /* On first open, do netif open, mount, etc. */
81 if (open_count
== 0) {
82 /* Find network interface. */
83 if ((netdev_sock
= netif_open(pd
)) < 0) {
87 if ((error
= net_mountroot()) != 0)
96 net_close(struct promdata
*pd
)
98 /* On last close, do netif close, etc. */
102 if (--open_count
== 0)
103 return (netif_close(netdev_sock
));
109 net_mountroot_bootparams(void)
111 printf("Trying BOOTPARAMS protocol... ");
113 /* Get our IP address. (rarp.c) */
114 if (rarp_getipaddress(netdev_sock
) == -1)
117 printf("ip address: %s", inet_ntoa(myip
));
119 /* Get our hostname, server IP address. */
120 if (bp_whoami(netdev_sock
))
123 printf(", hostname: %s\n", hostname
);
125 /* Get the root pathname. */
126 if (bp_getfile(netdev_sock
, "root", &rootip
, rootpath
))
133 net_mountroot_bootp(void)
135 printf("Trying BOOTP protocol... ");
139 if (myip
.s_addr
== 0)
142 printf("ip address: %s", inet_ntoa(myip
));
145 printf(", hostname: %s", hostname
);
147 printf(", netmask: %s", intoa(netmask
));
149 printf(", gateway: %s", inet_ntoa(gateip
));
161 printf("net_mountroot\n");
165 * Get info for NFS boot: our IP address, our hostname,
166 * server IP address, and our root path on the server.
167 * There are two ways to do this: The old, Sun way,
168 * and the more modern, BOOTP way. (RFC951, RFC1048)
171 /* Try BOOTP first */
172 error
= net_mountroot_bootp();
173 /* Historically, we've used BOOTPARAMS, so try that next */
175 error
= net_mountroot_bootparams();
179 printf("root addr=%s path=%s\n", inet_ntoa(rootip
), rootpath
);
181 /* Get the NFS file handle (mount). */
182 if (nfs_mount(netdev_sock
, rootip
, rootpath
) != 0)