xref: /freebsd/stand/efi/libefi/efinet.c (revision 43b8edb320519c9887a5d953c4cf8a91f0ca8d14)
1 /*-
2  * Copyright (c) 2001 Doug Rabson
3  * Copyright (c) 2002, 2006 Marcel Moolenaar
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <net/ethernet.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 
33 #include <stand.h>
34 #include <net.h>
35 #include <netif.h>
36 
37 #include <efi.h>
38 #include <efilib.h>
39 #include <Protocol/SimpleNetwork.h>
40 
41 #include "dev_net.h"
42 
43 static EFI_GUID sn_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
44 
45 static void efinet_end(struct netif *);
46 static ssize_t efinet_get(struct iodesc *, void **, time_t);
47 static void efinet_init(struct iodesc *, void *);
48 static int efinet_match(struct netif *, void *);
49 static int efinet_probe(struct netif *, void *);
50 static ssize_t efinet_put(struct iodesc *, void *, size_t);
51 
52 struct netif_driver efinetif = {
53 	.netif_bname = "efinet",
54 	.netif_match = efinet_match,
55 	.netif_probe = efinet_probe,
56 	.netif_init = efinet_init,
57 	.netif_get = efinet_get,
58 	.netif_put = efinet_put,
59 	.netif_end = efinet_end,
60 	.netif_ifs = NULL,
61 	.netif_nifs = 0
62 };
63 
64 #ifdef EFINET_DEBUG
65 static void
dump_mode(EFI_SIMPLE_NETWORK_MODE * mode)66 dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
67 {
68 	int i;
69 
70 	printf("State                 = %x\n", mode->State);
71 	printf("HwAddressSize         = %u\n", mode->HwAddressSize);
72 	printf("MediaHeaderSize       = %u\n", mode->MediaHeaderSize);
73 	printf("MaxPacketSize         = %u\n", mode->MaxPacketSize);
74 	printf("NvRamSize             = %u\n", mode->NvRamSize);
75 	printf("NvRamAccessSize       = %u\n", mode->NvRamAccessSize);
76 	printf("ReceiveFilterMask     = %x\n", mode->ReceiveFilterMask);
77 	printf("ReceiveFilterSetting  = %u\n", mode->ReceiveFilterSetting);
78 	printf("MaxMCastFilterCount   = %u\n", mode->MaxMCastFilterCount);
79 	printf("MCastFilterCount      = %u\n", mode->MCastFilterCount);
80 	printf("MCastFilter           = {");
81 	for (i = 0; i < mode->MCastFilterCount; i++)
82 		printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr));
83 	printf(" }\n");
84 	printf("CurrentAddress        = %s\n",
85 	    ether_sprintf(mode->CurrentAddress.Addr));
86 	printf("BroadcastAddress      = %s\n",
87 	    ether_sprintf(mode->BroadcastAddress.Addr));
88 	printf("PermanentAddress      = %s\n",
89 	    ether_sprintf(mode->PermanentAddress.Addr));
90 	printf("IfType                = %u\n", mode->IfType);
91 	printf("MacAddressChangeable  = %d\n", mode->MacAddressChangeable);
92 	printf("MultipleTxSupported   = %d\n", mode->MultipleTxSupported);
93 	printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported);
94 	printf("MediaPresent          = %d\n", mode->MediaPresent);
95 }
96 #endif
97 
98 static int
efinet_match(struct netif * nif,void * machdep_hint)99 efinet_match(struct netif *nif, void *machdep_hint)
100 {
101 	struct devdesc *dev = machdep_hint;
102 
103 	if (dev->d_unit == nif->nif_unit)
104 		return (1);
105 	return(0);
106 }
107 
108 static int
efinet_probe(struct netif * nif,void * machdep_hint)109 efinet_probe(struct netif *nif, void *machdep_hint)
110 {
111 	EFI_SIMPLE_NETWORK *net;
112 	EFI_HANDLE h;
113 	EFI_STATUS status;
114 
115 	h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
116 	/*
117 	 * Open the network device in exclusive mode. Without this
118 	 * we will be racing with the UEFI network stack. It will
119 	 * pull packets off the network leading to lost packets.
120 	 */
121 	status = BS->OpenProtocol(h, &sn_guid, (void **)&net,
122 	    IH, NULL, EFI_OPEN_PROTOCOL_EXCLUSIVE);
123 	if (status != EFI_SUCCESS) {
124 		printf("Unable to open network interface %d for "
125 		    "exclusive access: %lu\n", nif->nif_unit,
126 		    DECODE_ERROR(status));
127 		return (efi_status_to_errno(status));
128 	}
129 
130 	return (0);
131 }
132 
133 static ssize_t
efinet_put(struct iodesc * desc,void * pkt,size_t len)134 efinet_put(struct iodesc *desc, void *pkt, size_t len)
135 {
136 	struct netif *nif = desc->io_netif;
137 	EFI_SIMPLE_NETWORK *net;
138 	EFI_STATUS status;
139 	void *buf;
140 
141 	net = nif->nif_devdata;
142 	if (net == NULL)
143 		return (-1);
144 
145 	status = net->Transmit(net, 0, len, pkt, NULL, NULL, NULL);
146 	if (status != EFI_SUCCESS)
147 		return (-1);
148 
149 	/* Wait for the buffer to be transmitted */
150 	do {
151 		buf = NULL;	/* XXX Is this needed? */
152 		status = net->GetStatus(net, NULL, &buf);
153 		/*
154 		 * XXX EFI1.1 and the E1000 card returns a different
155 		 * address than we gave.  Sigh.
156 		 */
157 	} while (status == EFI_SUCCESS && buf == NULL);
158 
159 	/* XXX How do we deal with status != EFI_SUCCESS now? */
160 	return ((status == EFI_SUCCESS) ? len : -1);
161 }
162 
163 static ssize_t
efinet_get(struct iodesc * desc,void ** pkt,time_t timeout)164 efinet_get(struct iodesc *desc, void **pkt, time_t timeout)
165 {
166 	struct netif *nif = desc->io_netif;
167 	EFI_SIMPLE_NETWORK *net;
168 	EFI_STATUS status;
169 	UINTN bufsz;
170 	time_t t;
171 	char *buf, *ptr;
172 	ssize_t ret = -1;
173 
174 	net = nif->nif_devdata;
175 	if (net == NULL)
176 		return (ret);
177 
178 	bufsz = net->Mode->MaxPacketSize + ETHER_HDR_LEN + ETHER_CRC_LEN;
179 	buf = malloc(bufsz + ETHER_ALIGN);
180 	if (buf == NULL)
181 		return (ret);
182 	ptr = buf + ETHER_ALIGN;
183 
184 	t = getsecs();
185 	while ((getsecs() - t) < timeout) {
186 		status = net->Receive(net, NULL, &bufsz, ptr, NULL, NULL, NULL);
187 		if (status == EFI_SUCCESS) {
188 			*pkt = buf;
189 			ret = (ssize_t)bufsz;
190 			break;
191 		}
192 		if (status != EFI_NOT_READY)
193 			break;
194 	}
195 
196 	if (ret == -1)
197 		free(buf);
198 	return (ret);
199 }
200 
201 /*
202  * Loader uses BOOTP/DHCP and also uses RARP as a fallback to populate
203  * network parameters and problems with DHCP servers can cause the loader
204  * to fail to populate them. Allow the device to ask about the basic
205  * network parameters and if present use them.
206  */
207 static void
efi_env_net_params(struct iodesc * desc)208 efi_env_net_params(struct iodesc *desc)
209 {
210 	char *envstr;
211 	in_addr_t ipaddr, mask, gwaddr, serveraddr;
212 	n_long rootaddr;
213 
214 	if ((envstr = getenv("rootpath")) != NULL)
215 		strlcpy(rootpath, envstr, sizeof(rootpath));
216 
217 	/*
218 	 * Get network parameters.
219 	 */
220 	envstr = getenv("ipaddr");
221 	ipaddr = (envstr != NULL) ? inet_addr(envstr) : 0;
222 
223 	envstr = getenv("netmask");
224 	mask = (envstr != NULL) ? inet_addr(envstr) : 0;
225 
226 	envstr = getenv("gatewayip");
227 	gwaddr = (envstr != NULL) ? inet_addr(envstr) : 0;
228 
229 	envstr = getenv("serverip");
230 	serveraddr = (envstr != NULL) ? inet_addr(envstr) : 0;
231 
232 	/* No network params. */
233 	if (ipaddr == 0 && mask == 0 && gwaddr == 0 && serveraddr == 0)
234 		return;
235 
236 	/* Partial network params. */
237 	if (ipaddr == 0 || mask == 0 || gwaddr == 0 || serveraddr == 0) {
238 		printf("Incomplete network settings from U-Boot\n");
239 		return;
240 	}
241 
242 	/*
243 	 * Set network parameters.
244 	 */
245 	myip.s_addr = ipaddr;
246 	netmask = mask;
247 	gateip.s_addr = gwaddr;
248 	servip.s_addr = serveraddr;
249 
250 	/*
251 	 * There must be a rootpath. It may be ip:/path or it may be just the
252 	 * path in which case the ip needs to be serverip.
253 	 */
254 	rootaddr = net_parse_rootpath();
255 	if (rootaddr == INADDR_NONE)
256 		rootaddr = serveraddr;
257 	rootip.s_addr = rootaddr;
258 
259 #ifdef EFINET_DEBUG
260 	printf("%s: proto=%d\n", __func__, netproto);
261 	printf("%s: ip=%s\n", __func__, inet_ntoa(myip));
262 	printf("%s: mask=%s\n", __func__, intoa(netmask));
263 	printf("%s: gateway=%s\n", __func__, inet_ntoa(gateip));
264 	printf("%s: server=%s\n", __func__, inet_ntoa(servip));
265 #endif
266 
267 	desc->myip = myip;
268 }
269 
270 static void
efinet_init(struct iodesc * desc,void * machdep_hint)271 efinet_init(struct iodesc *desc, void *machdep_hint)
272 {
273 	struct netif *nif = desc->io_netif;
274 	EFI_SIMPLE_NETWORK *net;
275 	EFI_HANDLE h;
276 	EFI_STATUS status;
277 	UINT32 mask;
278 
279 	/* Attempt to get netboot params from env */
280 	efi_env_net_params(desc);
281 
282 	if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) {
283 		printf("Invalid network interface %d\n", nif->nif_unit);
284 		return;
285 	}
286 
287 	h = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
288 	status = OpenProtocolByHandle(h, &sn_guid, (void **)&nif->nif_devdata);
289 	if (status != EFI_SUCCESS) {
290 		printf("net%d: cannot fetch interface data (status=%lu)\n",
291 		    nif->nif_unit, DECODE_ERROR(status));
292 		return;
293 	}
294 
295 	net = nif->nif_devdata;
296 	if (net->Mode->State == EfiSimpleNetworkStopped) {
297 		status = net->Start(net);
298 		if (status != EFI_SUCCESS) {
299 			printf("net%d: cannot start interface (status=%lu)\n",
300 			    nif->nif_unit, DECODE_ERROR(status));
301 			return;
302 		}
303 	}
304 
305 	if (net->Mode->State != EfiSimpleNetworkInitialized) {
306 		status = net->Initialize(net, 0, 0);
307 		if (status != EFI_SUCCESS) {
308 			printf("net%d: cannot init. interface (status=%lu)\n",
309 			    nif->nif_unit, DECODE_ERROR(status));
310 			return;
311 		}
312 	}
313 
314 	mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
315 	    EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
316 
317 	status = net->ReceiveFilters(net, mask, 0, FALSE, 0, NULL);
318 	if (status != EFI_SUCCESS)
319 		printf("net%d: cannot set rx. filters (status=%lu)\n",
320 		    nif->nif_unit, DECODE_ERROR(status));
321 
322 #ifdef EFINET_DEBUG
323 	dump_mode(net->Mode);
324 #endif
325 
326 	bcopy(net->Mode->CurrentAddress.Addr, desc->myea, 6);
327 	desc->xid = 1;
328 }
329 
330 static void
efinet_end(struct netif * nif)331 efinet_end(struct netif *nif)
332 {
333 	EFI_SIMPLE_NETWORK *net = nif->nif_devdata;
334 
335 	if (net == NULL)
336 		return;
337 
338 	net->Shutdown(net);
339 }
340 
341 static int efinet_dev_init(void);
342 static int efinet_dev_print(int);
343 
344 struct devsw efinet_dev = {
345 	.dv_name = "net",
346 	.dv_type = DEVT_NET,
347 	.dv_init = efinet_dev_init,
348 	.dv_strategy = NULL,		/* Will be set in efinet_dev_init */
349 	.dv_open = NULL,		/* Will be set in efinet_dev_init */
350 	.dv_close = NULL,		/* Will be set in efinet_dev_init */
351 	.dv_ioctl = noioctl,
352 	.dv_print = efinet_dev_print,
353 	.dv_cleanup = nullsys,
354 };
355 
356 static int
efinet_dev_init(void)357 efinet_dev_init(void)
358 {
359 	struct netif_dif *dif;
360 	struct netif_stats *stats;
361 	EFI_DEVICE_PATH *devpath, *node;
362 	EFI_HANDLE *handles, *handles2;
363 	EFI_STATUS status;
364 	UINTN sz;
365 	int err, i, nifs;
366 	extern struct devsw netdev;
367 
368 	sz = 0;
369 	handles = NULL;
370 	status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz, NULL);
371 	if (status == EFI_BUFFER_TOO_SMALL) {
372 		handles = (EFI_HANDLE *)malloc(sz);
373 		if (handles == NULL)
374 			return (ENOMEM);
375 		status = BS->LocateHandle(ByProtocol, &sn_guid, NULL, &sz,
376 		    handles);
377 		if (EFI_ERROR(status))
378 			free(handles);
379 	}
380 	if (EFI_ERROR(status))
381 		return (efi_status_to_errno(status));
382 	handles2 = (EFI_HANDLE *)malloc(sz);
383 	if (handles2 == NULL) {
384 		free(handles);
385 		return (ENOMEM);
386 	}
387 	nifs = 0;
388 	for (i = 0; i < sz / sizeof(EFI_HANDLE); i++) {
389 		devpath = efi_lookup_devpath(handles[i]);
390 		if (devpath == NULL)
391 			continue;
392 		if ((node = efi_devpath_last_node(devpath)) == NULL)
393 			continue;
394 
395 		if (DevicePathType(node) != MESSAGING_DEVICE_PATH ||
396 		    DevicePathSubType(node) != MSG_MAC_ADDR_DP)
397 			continue;
398 
399 		handles2[nifs] = handles[i];
400 		nifs++;
401 	}
402 	free(handles);
403 	if (nifs == 0) {
404 		err = ENOENT;
405 		goto done;
406 	}
407 
408 	err = efi_register_handles(&efinet_dev, handles2, NULL, nifs);
409 	if (err != 0)
410 		goto done;
411 
412 	efinetif.netif_ifs = calloc(nifs, sizeof(struct netif_dif));
413 	stats = calloc(nifs, sizeof(struct netif_stats));
414 	if (efinetif.netif_ifs == NULL || stats == NULL) {
415 		free(efinetif.netif_ifs);
416 		free(stats);
417 		efinetif.netif_ifs = NULL;
418 		err = ENOMEM;
419 		goto done;
420 	}
421 	efinetif.netif_nifs = nifs;
422 
423 	for (i = 0; i < nifs; i++) {
424 
425 		dif = &efinetif.netif_ifs[i];
426 		dif->dif_unit = i;
427 		dif->dif_nsel = 1;
428 		dif->dif_stats = &stats[i];
429 		dif->dif_private = handles2[i];
430 	}
431 
432 	efinet_dev.dv_cleanup = netdev.dv_cleanup;
433 	efinet_dev.dv_open = netdev.dv_open;
434 	efinet_dev.dv_close = netdev.dv_close;
435 	efinet_dev.dv_strategy = netdev.dv_strategy;
436 
437 done:
438 	free(handles2);
439 	return (err);
440 }
441 
442 static int
efinet_dev_print(int verbose)443 efinet_dev_print(int verbose)
444 {
445 	CHAR16 *text;
446 	EFI_HANDLE h;
447 	int unit, ret = 0;
448 
449 	printf("%s devices:", efinet_dev.dv_name);
450 	if ((ret = pager_output("\n")) != 0)
451 		return (ret);
452 
453 	for (unit = 0, h = efi_find_handle(&efinet_dev, 0);
454 	    h != NULL; h = efi_find_handle(&efinet_dev, ++unit)) {
455 		printf("    %s%d:", efinet_dev.dv_name, unit);
456 		if (verbose) {
457 			text = efi_devpath_name(efi_lookup_devpath(h));
458 			if (text != NULL) {
459 				printf("    %S", text);
460 				efi_free_devpath_name(text);
461 			}
462 		}
463 		if ((ret = pager_output("\n")) != 0)
464 			break;
465 	}
466 	return (ret);
467 }
468