xref: /netbsd-src/sys/arch/i386/stand/pxeboot/dev_net.c (revision 3eb244d801190b3a7d8cd42193c38a77c1dbae1a)
1 /*	$NetBSD: dev_net.c,v 1.11 2011/07/17 20:54:42 joerg Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross.
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  *
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.
30  */
31 
32 /*
33  * This module implements a "raw device" interface suitable for
34  * use by the stand-alone I/O library NFS and TFTP code.  This interface
35  * does not support any "block" access, and exists only for the
36  * purpose of initializing the network interface and getting boot
37  * parameters.
38  *
39  * At open time, this does:
40  *
41  * find interface       - netif_open()
42  * BOOTP for IP address - bootp()
43  */
44 
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <net/if.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 
51 #include <lib/libkern/libkern.h>
52 
53 #include <lib/libsa/stand.h>
54 #include <lib/libsa/net.h>
55 #include "pxe_netif.h"
56 #include "dev_net.h"
57 
58 static int netdev_sock = -1;
59 static int netdev_opens;
60 
61 static int net_getparams(int sock);
62 
63 /*
64  * Called by devopen after it sets f->f_dev to our devsw entry.
65  * This opens the low-level device and sets f->f_devdata.
66  * This is declared with variable arguments...
67  */
68 int
net_open(struct open_file * f,...)69 net_open(struct open_file *f, ...)
70 {
71 	int error = 0;
72 
73 #ifdef	NETIF_DEBUG
74 	if (debug)
75 		printf("net_open\n");
76 #endif
77 
78 	/* On first open, do netif open, mount, etc. */
79 	if (netdev_opens == 0) {
80 		/* Find network interface. */
81 		if (netdev_sock < 0) {
82 			netdev_sock = pxe_netif_open();
83 			if (netdev_sock < 0) {
84 				printf("net_open: netif_open() failed\n");
85 				return (ENXIO);
86 			}
87 			if (debug)
88 				printf("net_open: netif_open() succeeded\n");
89 		}
90 		if (rootip.s_addr == 0) {
91 			/* Get root IP address, and path, etc. */
92 			error = net_getparams(netdev_sock);
93 			if (error) {
94 				/* getparams makes its own noise */
95 				pxe_netif_close(netdev_sock);
96 				netdev_sock = -1;
97 				return (error);
98 			}
99 		}
100 	}
101 	netdev_opens++;
102 	f->f_devdata = &netdev_sock;
103 	return (error);
104 }
105 
106 int
net_close(struct open_file * f)107 net_close(struct open_file *f)
108 {
109 
110 #ifdef	NETIF_DEBUG
111 	if (debug)
112 		printf("net_close: opens=%d\n", netdev_opens);
113 #endif
114 
115 	/* On last close, do netif close, etc. */
116 	f->f_devdata = NULL;
117 	/* Extra close call? */
118 	if (netdev_opens <= 0)
119 		return (0);
120 	netdev_opens--;
121 	/* Not last close? */
122 	if (netdev_opens > 0)
123 		return(0);
124 	rootip.s_addr = 0;
125 	if (netdev_sock >= 0) {
126 		if (debug)
127 			printf("net_close: calling netif_close()\n");
128 		pxe_netif_close(netdev_sock);
129 		netdev_sock = -1;
130 	}
131 	return (0);
132 }
133 
134 int
net_ioctl(struct open_file * f,u_long cmd,void * data)135 net_ioctl(struct open_file *f, u_long cmd, void *data)
136 {
137 	return EIO;
138 }
139 
140 int
net_strategy(void * devdata,int rw,daddr_t blk,size_t size,void * buf,size_t * rsize)141 net_strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf, size_t *rsize)
142 {
143 	return EIO;
144 }
145 
146 
147 /*
148  * Get info for network boot: our IP address, our hostname,
149  * server IP address, and our root path on the server.
150  */
151 #ifdef	SUPPORT_BOOTP
152 int bootp(int sock);
153 #endif
154 
155 static int
net_getparams(int sock)156 net_getparams(int sock)
157 {
158 
159 #ifdef	SUPPORT_BOOTP
160 	/*
161 	 * Try to get boot info using BOOTP.  If we succeed, then
162 	 * the server IP address, gateway, and root path will all
163 	 * be initialized.  If any remain uninitialized, we will
164 	 * use RARP and RPC/bootparam (the Sun way) to get them.
165 	 */
166 	bootp(sock);
167 	if (myip.s_addr != 0)
168 		return (0);
169 	if (debug)
170 		printf("net_open: BOOTP failed\n");
171 #endif
172 
173 	return (EIO);
174 }
175