xref: /netbsd-src/sys/arch/sparc/stand/ofwboot/net.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: net.c,v 1.3 2006/07/13 20:03:34 uwe Exp $	*/
2 
3 /*
4  * Copyright (C) 1995 Wolfgang Solfrank.
5  * Copyright (C) 1995 TooLs GmbH.
6  * All rights reserved.
7  *
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.
21  *
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.
32  */
33 
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.
40  *
41  * At open time, this does:
42  *
43  * find interface	- netif_open()
44  * BOOTP		- bootp()
45  * RPC/mountd		- nfs_mount()
46  *
47  * The root file handle from mountd is saved in a global
48  * for use by the NFS open code (NFS/lookup).
49  *
50  * Note: this is based in part on sys/arch/sparc/stand/net.c
51  */
52 
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 
56 #include <net/if.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 
60 #include <lib/libsa/stand.h>
61 #include <lib/libsa/net.h>
62 #include <lib/libsa/netif.h>
63 
64 #include <lib/libkern/libkern.h>
65 
66 #include "ofdev.h"
67 
68 
69 static int net_mountroot_bootparams(void);
70 static int net_mountroot_bootp(void);
71 
72 char	rootpath[FNAME_SIZE];
73 
74 static	int netdev_sock = -1;
75 static	int open_count;
76 
77 /*
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.
80  */
81 int
82 net_open(struct of_dev *op)
83 {
84 	int error = 0;
85 
86 	/*
87 	 * On first open, do netif open, mount, etc.
88 	 */
89 	if (open_count == 0) {
90 		/* Find network interface. */
91 		if ((netdev_sock = netif_open(op)) < 0) {
92 			error = errno;
93 			goto bad;
94 		}
95 		if ((error = net_mountroot()) != 0)
96 			goto bad;
97 	}
98 	open_count++;
99 bad:
100 	if (netdev_sock >= 0 && open_count == 0) {
101 		netif_close(netdev_sock);
102 		netdev_sock = -1;
103 	}
104 	return error;
105 }
106 
107 int
108 net_close(struct of_dev *op)
109 {
110 
111 	/*
112 	 * On last close, do netif close, etc.
113 	 */
114 	if (open_count > 0)
115 		if (--open_count == 0) {
116 			netif_close(netdev_sock);
117 			netdev_sock = -1;
118 		}
119 }
120 
121 int
122 net_mountroot_bootparams(void)
123 {
124 
125 	/* Get our IP address.  (rarp.c) */
126 	if (rarp_getipaddress(netdev_sock) == -1)
127 		return (errno);
128 
129 	printf("Using BOOTPARAMS protocol: ");
130 	printf("ip address: %s", inet_ntoa(myip));
131 
132 	/* Get our hostname, server IP address. */
133 	if (bp_whoami(netdev_sock))
134 		return (errno);
135 
136 	printf(", hostname: %s\n", hostname);
137 
138 	/* Get the root pathname. */
139 	if (bp_getfile(netdev_sock, "root", &rootip, rootpath))
140 		return (errno);
141 
142 	return (0);
143 }
144 
145 int
146 net_mountroot_bootp(void)
147 {
148 
149 	bootp(netdev_sock);
150 
151 	if (myip.s_addr == 0)
152 		return(ENOENT);
153 
154 	printf("Using BOOTP protocol: ");
155 	printf("ip address: %s", inet_ntoa(myip));
156 
157 	if (hostname[0])
158 		printf(", hostname: %s", hostname);
159 	if (netmask)
160 		printf(", netmask: %s", intoa(netmask));
161 	if (gateip.s_addr)
162 		printf(", gateway: %s", inet_ntoa(gateip));
163 	printf("\n");
164 
165 	return (0);
166 }
167 
168 int
169 net_mountroot(void)
170 {
171 	int error;
172 
173 #ifdef DEBUG
174 	printf("net_mountroot\n");
175 #endif
176 
177 	/*
178 	 * Get info for NFS boot: our IP address, our hostname,
179 	 * server IP address, and our root path on the server.
180 	 * There are two ways to do this:  The old, Sun way,
181 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
182 	 */
183 
184 		/* Try BOOTP first */
185 	error = net_mountroot_bootp();
186 		/* Historically, we've used BOOTPARAMS, so try that next */
187 	if (error != 0)
188 		error = net_mountroot_bootparams();
189 	if (error != 0)
190 		return (error);
191 
192 	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
193 
194 	/* Get the NFS file handle (mount). */
195 	if (nfs_mount(netdev_sock, rootip, rootpath) != 0)
196 		return (errno);
197 
198 	return (0);
199 }
200