xref: /netbsd-src/sys/arch/vax/boot/boot/netio.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: netio.c,v 1.6 2000/05/26 20:16:46 ragge Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
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.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1995 Gordon W. Ross
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. The name of the author may not be used to endorse or promote products
52  *    derived from this software without specific prior written permission.
53  * 4. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *	This product includes software developed by Gordon W. Ross
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  */
68 
69 /*
70  * This module implements a "raw device" interface suitable for
71  * use by the stand-alone I/O library NFS code.	 This interface
72  * does not support any "block" access, and exists only for the
73  * purpose of initializing the network interface, getting boot
74  * parameters, and performing the NFS mount.
75  *
76  * At open time, this does:
77  *
78  * find interface      - netif_open()
79  * RARP for IP address - rarp_getipaddress()
80  * RPC/bootparams      - callrpc(d, RPC_BOOTPARAMS, ...)
81  * RPC/mountd	       - nfs_mount(sock, ip, path)
82  *
83  * the root file handle from mountd is saved in a global
84  * for use by the NFS open code (NFS/lookup).
85  */
86 
87 #include <sys/param.h>
88 #include <sys/socket.h>
89 #include <net/if.h>
90 #include <netinet/in.h>
91 #include <netinet/if_ether.h>
92 #include <netinet/in_systm.h>
93 
94 #include "lib/libsa/stand.h"
95 #include "lib/libsa/net.h"
96 #include "lib/libsa/netif.h"
97 #include "lib/libsa/bootparam.h"
98 #include "lib/libsa/nfs.h"
99 #include "lib/libsa/bootp.h"
100 
101 #include <lib/libkern/libkern.h>
102 
103 #include "vaxstand.h"
104 
105 static struct iodesc desc;
106 static int inited = 0;
107 
108 struct iodesc *
109 socktodesc(sock)
110 {
111 	return &desc;
112 }
113 
114 int
115 net_devinit(struct open_file *f, struct netif_driver *drv, u_char *eaddr) {
116 	static struct netif best_if;
117 	struct iodesc *s;
118 	int r;
119 
120 	if (inited)
121 		return 0;
122 	/* find a free socket */
123 	s = &desc;
124 
125 	bzero(s, sizeof(*s));
126 	best_if.nif_driver = drv;
127 	s->io_netif = &best_if;
128 	bcopy(eaddr, s->myea, 6);
129 
130 	/*
131 	 * Get info for NFS boot: our IP address, our hostname,
132 	 * server IP address, and our root path on the server.
133 	 * There are two ways to do this:  The old, Sun way,
134 	 * and the more modern, BOOTP way. (RFC951, RFC1048)
135 	 */
136 
137 #ifdef SUPPORT_BOOTP
138 
139 	/* Get boot info using BOOTP way. (RFC951, RFC1048) */
140 	printf("Trying BOOTP\n");
141 	bootp(0);
142 
143 	if (myip.s_addr) {
144 		printf("Using IP address: %s\n", inet_ntoa(myip));
145 
146 		printf("myip: %s (%s)\n", hostname, inet_ntoa(myip));
147 	} else
148 
149 #endif /* SUPPORT_BOOTP */
150 	{
151 #ifdef	SUPPORT_BOOTPARAMS
152 		/* Get boot info using RARP and Sun bootparams. */
153 
154 		printf("Trying BOOTPARAMS\n");
155 		/* Get our IP address.	(rarp.c) */
156 		if (rarp_getipaddress(0) == -1)
157 			return (errno);
158 
159 		printf("boot: client IP address: %s\n", inet_ntoa(myip));
160 
161 		/* Get our hostname, server IP address. */
162 		if (bp_whoami(0))
163 			return (errno);
164 
165 		printf("boot: client name: %s\n", hostname);
166 
167 		/* Get the root pathname. */
168 		if (bp_getfile(0, "root", &rootip, rootpath))
169 			return (errno);
170 #endif
171 	}
172 	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
173 	f->f_devdata = s;
174 
175 	/* Get the NFS file handle (mount). */
176 	r = nfs_mount(0, rootip, rootpath);
177 	if (r)
178 		return r;
179 
180 	inited = 1;
181 	return 0;
182 }
183 
184 ssize_t
185 netif_put(struct iodesc *desc, void *pkt, size_t len)
186 {
187 	return (*desc->io_netif->nif_driver->netif_put)(desc, pkt, len);
188 }
189 
190 ssize_t
191 netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timo)
192 {
193 	return (*desc->io_netif->nif_driver->netif_get)(desc, pkt, len, timo);
194 }
195