Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / sparc / stand / ofwboot / net.c
blob70aa6fc32b67fa1fe095e9d952c3e26bce3e4be3
1 /* $NetBSD: net.c,v 1.4 2008/05/12 11:16:31 mlelstv Exp $ */
3 /*
4 * Copyright (C) 1995 Wolfgang Solfrank.
5 * Copyright (C) 1995 TooLs GmbH.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 * BOOTP - bootp()
45 * RPC/mountd - nfs_mount()
47 * The root file handle from mountd is saved in a global
48 * for use by the NFS open code (NFS/lookup).
50 * Note: this is based in part on sys/arch/sparc/stand/net.c
53 #include <sys/param.h>
54 #include <sys/socket.h>
56 #include <net/if.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
60 #include <lib/libsa/stand.h>
61 #include <lib/libsa/net.h>
62 #include <lib/libsa/netif.h>
64 #include <lib/libkern/libkern.h>
66 #include "ofdev.h"
69 static int net_mountroot_bootparams(void);
70 static int net_mountroot_bootp(void);
72 char rootpath[FNAME_SIZE];
74 static int netdev_sock = -1;
75 static int open_count;
78 * Called by devopen after it sets f->f_dev to our devsw entry.
79 * This opens the low-level device and sets f->f_devdata.
81 int
82 net_open(struct of_dev *op)
84 int error = 0;
87 * On first open, do netif open, mount, etc.
89 if (open_count == 0) {
90 /* Find network interface. */
91 if ((netdev_sock = netif_open(op)) < 0) {
92 error = errno;
93 goto bad;
96 open_count++;
97 bad:
98 if (netdev_sock >= 0 && open_count == 0) {
99 netif_close(netdev_sock);
100 netdev_sock = -1;
102 return error;
106 net_close(struct of_dev *op)
110 * On last close, do netif close, etc.
112 if (open_count > 0)
113 if (--open_count == 0) {
114 netif_close(netdev_sock);
115 netdev_sock = -1;
119 static void
120 net_clear_params(void)
123 myip.s_addr = 0;
124 netmask = 0;
125 gateip.s_addr = 0;
126 *hostname = '\0';
127 rootip.s_addr = 0;
128 *rootpath = '\0';
132 net_mountroot_bootparams(void)
135 net_clear_params();
137 /* Get our IP address. (rarp.c) */
138 if (rarp_getipaddress(netdev_sock) == -1)
139 return (errno);
140 printf("Using BOOTPARAMS protocol:\n ip addr=%s\n", inet_ntoa(myip));
141 if (bp_whoami(netdev_sock))
142 return (errno);
143 printf(" hostname=%s\n", hostname);
144 if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
145 return (errno);
147 return (0);
151 net_mountroot_bootp(void)
153 int attempts;
155 /* We need a few attempts here as some DHCP servers
156 * require >1 packet and my wireless bridge is always
157 * in learning mode until the 2nd attempt ... */
158 for (attempts = 0; attempts < 3; attempts++) {
159 net_clear_params();
160 bootp(netdev_sock);
161 if (myip.s_addr != 0)
162 break;
164 if (myip.s_addr == 0)
165 return(ENOENT);
167 printf("Using BOOTP protocol:\n ip addr=%s\n", inet_ntoa(myip));
168 if (hostname[0])
169 printf(" hostname=%s\n", hostname);
170 if (netmask)
171 printf(" netmask=%s\n", intoa(netmask));
172 if (gateip.s_addr)
173 printf(" gateway=%s\n", inet_ntoa(gateip));
175 return (0);
179 net_tftp_bootp(int **sock)
182 net_mountroot_bootp();
183 if (myip.s_addr == 0)
184 return(ENOENT);
186 *sock = &netdev_sock;
187 return (0);
191 net_mountroot(void)
193 int error;
195 #ifdef DEBUG
196 printf("net_mountroot\n");
197 #endif
200 * Get info for NFS boot: our IP address, our hostname,
201 * server IP address, and our root path on the server.
202 * There are two ways to do this: The old, Sun way,
203 * and the more modern, BOOTP way. (RFC951, RFC1048)
206 /* Try BOOTP first */
207 error = net_mountroot_bootp();
208 /* Historically, we've used BOOTPARAMS, so try that next */
209 if (error != 0)
210 error = net_mountroot_bootparams();
211 if (error != 0)
212 return (error);
214 printf(" root addr=%s\n path=%s\n", inet_ntoa(rootip), rootpath);
216 /* Get the NFS file handle (mount). */
217 if (nfs_mount(netdev_sock, rootip, rootpath) != 0)
218 return (errno);
220 return (0);