Home | History | Annotate | Line # | Download | only in efiboot
efinet.c revision 1.3.2.3
      1 /*	$NetBSD: efinet.c,v 1.3.2.3 2018/11/26 01:52:52 pgoyette Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 Doug Rabson
      5  * Copyright (c) 2002, 2006 Marcel Moolenaar
      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  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 #include <sys/param.h>
     32 
     33 #include "efiboot.h"
     34 
     35 #include <lib/libsa/net.h>
     36 #include <lib/libsa/netif.h>
     37 #include <lib/libsa/dev_net.h>
     38 
     39 #include "devopen.h"
     40 
     41 #ifndef ETHER_ALIGN
     42 #define ETHER_ALIGN	2
     43 #endif
     44 
     45 #define ETHER_EXT_LEN	(ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_ALIGN)
     46 
     47 #if defined(DEBUG) || defined(ARP_DEBUG) || defined(BOOTP_DEBUG) || \
     48     defined(NET_DEBUG) || defined(NETIF_DEBUG) || defined(NFS_DEBUG) || \
     49     defined(RARP_DEBUG) || defined(RPC_DEBUG)
     50 int debug = 1;
     51 #else
     52 int debug = 0;
     53 #endif
     54 
     55 extern bool kernel_loaded;
     56 
     57 struct efinetinfo {
     58 	EFI_SIMPLE_NETWORK *net;
     59 	bool bootdev;
     60 	size_t pktbufsz;
     61 	UINT8 *pktbuf;
     62 	struct {
     63 		int type;
     64 		u_int tag;
     65 	} bus;
     66 };
     67 #if notyet
     68 static struct btinfo_netif bi_netif;
     69 #endif
     70 
     71 static int	efinet_match(struct netif *, void *);
     72 static int	efinet_probe(struct netif *, void *);
     73 static void	efinet_init(struct iodesc *, void *);
     74 static int	efinet_get(struct iodesc *, void *, size_t, saseconds_t);
     75 static int	efinet_put(struct iodesc *, void *, size_t);
     76 static void	efinet_end(struct netif *);
     77 
     78 struct netif_driver efinetif = {
     79 	.netif_bname = "net",
     80 	.netif_match = efinet_match,
     81 	.netif_probe = efinet_probe,
     82 	.netif_init = efinet_init,
     83 	.netif_get = efinet_get,
     84 	.netif_put = efinet_put,
     85 	.netif_end = efinet_end,
     86 	.netif_ifs = NULL,
     87 	.netif_nifs = 0
     88 };
     89 
     90 #ifdef EFINET_DEBUG
     91 static void
     92 dump_mode(EFI_SIMPLE_NETWORK_MODE *mode)
     93 {
     94 	int i;
     95 
     96 	printf("State                 = %x\n", mode->State);
     97 	printf("HwAddressSize         = %u\n", mode->HwAddressSize);
     98 	printf("MediaHeaderSize       = %u\n", mode->MediaHeaderSize);
     99 	printf("MaxPacketSize         = %u\n", mode->MaxPacketSize);
    100 	printf("NvRamSize             = %u\n", mode->NvRamSize);
    101 	printf("NvRamAccessSize       = %u\n", mode->NvRamAccessSize);
    102 	printf("ReceiveFilterMask     = %x\n", mode->ReceiveFilterMask);
    103 	printf("ReceiveFilterSetting  = %u\n", mode->ReceiveFilterSetting);
    104 	printf("MaxMCastFilterCount   = %u\n", mode->MaxMCastFilterCount);
    105 	printf("MCastFilterCount      = %u\n", mode->MCastFilterCount);
    106 	printf("MCastFilter           = {");
    107 	for (i = 0; i < mode->MCastFilterCount; i++)
    108 		printf(" %s", ether_sprintf(mode->MCastFilter[i].Addr));
    109 	printf(" }\n");
    110 	printf("CurrentAddress        = %s\n",
    111 	    ether_sprintf(mode->CurrentAddress.Addr));
    112 	printf("BroadcastAddress      = %s\n",
    113 	    ether_sprintf(mode->BroadcastAddress.Addr));
    114 	printf("PermanentAddress      = %s\n",
    115 	    ether_sprintf(mode->PermanentAddress.Addr));
    116 	printf("IfType                = %u\n", mode->IfType);
    117 	printf("MacAddressChangeable  = %d\n", mode->MacAddressChangeable);
    118 	printf("MultipleTxSupported   = %d\n", mode->MultipleTxSupported);
    119 	printf("MediaPresentSupported = %d\n", mode->MediaPresentSupported);
    120 	printf("MediaPresent          = %d\n", mode->MediaPresent);
    121 }
    122 #endif
    123 
    124 static int
    125 efinet_match(struct netif *nif, void *machdep_hint)
    126 {
    127 	struct devdesc *dev = machdep_hint;
    128 
    129 	if (dev->d_unit != nif->nif_unit)
    130 		return 0;
    131 
    132 	return 1;
    133 }
    134 
    135 static int
    136 efinet_probe(struct netif *nif, void *machdep_hint)
    137 {
    138 
    139 	return 0;
    140 }
    141 
    142 static int
    143 efinet_put(struct iodesc *desc, void *pkt, size_t len)
    144 {
    145 	struct netif *nif = desc->io_netif;
    146 	struct efinetinfo *eni = nif->nif_devdata;
    147 	EFI_SIMPLE_NETWORK *net;
    148 	EFI_STATUS status;
    149 	void *buf;
    150 
    151 	if (eni == NULL)
    152 		return -1;
    153 	net = eni->net;
    154 
    155 	status = uefi_call_wrapper(net->Transmit, 7, net, 0, (UINTN)len, pkt, NULL,
    156 	    NULL, NULL);
    157 	if (EFI_ERROR(status))
    158 		return -1;
    159 
    160 	/* Wait for the buffer to be transmitted */
    161 	do {
    162 		buf = NULL;	/* XXX Is this needed? */
    163 		status = uefi_call_wrapper(net->GetStatus, 3, net, NULL, &buf);
    164 		/*
    165 		 * XXX EFI1.1 and the E1000 card returns a different
    166 		 * address than we gave.  Sigh.
    167 		 */
    168 	} while (!EFI_ERROR(status) && buf == NULL);
    169 
    170 	/* XXX How do we deal with status != EFI_SUCCESS now? */
    171 	return EFI_ERROR(status) ? -1 : len;
    172 }
    173 
    174 static int
    175 efinet_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout)
    176 {
    177 	struct netif *nif = desc->io_netif;
    178 	struct efinetinfo *eni = nif->nif_devdata;
    179 	EFI_SIMPLE_NETWORK *net;
    180 	EFI_STATUS status;
    181 	UINTN bufsz, rsz;
    182 	time_t t;
    183 	char *buf, *ptr;
    184 	int ret = -1;
    185 
    186 	if (eni == NULL)
    187 		return -1;
    188 	net = eni->net;
    189 
    190 	if (eni->pktbufsz < net->Mode->MaxPacketSize + ETHER_EXT_LEN) {
    191 		bufsz = net->Mode->MaxPacketSize + ETHER_EXT_LEN;
    192 		buf = alloc(bufsz);
    193 		if (buf == NULL)
    194 			return -1;
    195 		dealloc(eni->pktbuf, eni->pktbufsz);
    196 		eni->pktbufsz = bufsz;
    197 		eni->pktbuf = buf;
    198 	}
    199 	ptr = eni->pktbuf + ETHER_ALIGN;
    200 
    201 	t = getsecs();
    202 	while ((getsecs() - t) < timeout) {
    203 		rsz = eni->pktbufsz;
    204 		status = uefi_call_wrapper(net->Receive, 7, net, NULL, &rsz, ptr,
    205 		    NULL, NULL, NULL);
    206 		if (!EFI_ERROR(status)) {
    207 			rsz = uimin(rsz, len);
    208 			memcpy(pkt, ptr, rsz);
    209 			ret = (int)rsz;
    210 			break;
    211 		}
    212 		if (status != EFI_NOT_READY)
    213 			break;
    214 	}
    215 
    216 	return ret;
    217 }
    218 
    219 static void
    220 efinet_init(struct iodesc *desc, void *machdep_hint)
    221 {
    222 	struct netif *nif = desc->io_netif;
    223 	struct efinetinfo *eni;
    224 	EFI_SIMPLE_NETWORK *net;
    225 	EFI_STATUS status;
    226 	UINT32 mask;
    227 
    228 	if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) {
    229 		printf("Invalid network interface %d\n", nif->nif_unit);
    230 		return;
    231 	}
    232 
    233 	eni = nif->nif_driver->netif_ifs[nif->nif_unit].dif_private;
    234 	nif->nif_devdata = eni;
    235 	net = eni->net;
    236 	if (net->Mode->State == EfiSimpleNetworkStopped) {
    237 		status = uefi_call_wrapper(net->Start, 1, net);
    238 		if (EFI_ERROR(status)) {
    239 			printf("net%d: cannot start interface (status=%"
    240 			    PRIxMAX ")\n", nif->nif_unit, (uintmax_t)status);
    241 			return;
    242 		}
    243 	}
    244 
    245 	if (net->Mode->State != EfiSimpleNetworkInitialized) {
    246 		status = uefi_call_wrapper(net->Initialize, 3, net, 0, 0);
    247 		if (EFI_ERROR(status)) {
    248 			printf("net%d: cannot init. interface (status=%"
    249 			    PRIxMAX ")\n", nif->nif_unit, (uintmax_t)status);
    250 			return;
    251 		}
    252 	}
    253 
    254 	mask = EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
    255 	    EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
    256 
    257 	status = uefi_call_wrapper(net->ReceiveFilters, 6, net, mask, 0, FALSE,
    258 	    0, NULL);
    259 	if (EFI_ERROR(status) && status != EFI_INVALID_PARAMETER) {
    260 		printf("net%d: cannot set rx. filters (status=%" PRIxMAX ")\n",
    261 		    nif->nif_unit, (uintmax_t)status);
    262 		return;
    263 	}
    264 
    265 #if notyet
    266 	if (!kernel_loaded) {
    267 		bi_netif.bus = eni->bus.type;
    268 		bi_netif.addr.tag = eni->bus.tag;
    269 		snprintf(bi_netif.ifname, sizeof(bi_netif.ifname), "net%d",
    270 		    nif->nif_unit);
    271 		BI_ADD(&bi_netif, BTINFO_NETIF, sizeof(bi_netif));
    272 	}
    273 #endif
    274 
    275 #ifdef EFINET_DEBUG
    276 	dump_mode(net->Mode);
    277 #endif
    278 
    279 	memcpy(desc->myea, net->Mode->PermanentAddress.Addr, 6);
    280 	desc->xid = 1;
    281 }
    282 
    283 static void
    284 efinet_end(struct netif *nif)
    285 {
    286 	struct efinetinfo *eni = nif->nif_devdata;
    287 	EFI_SIMPLE_NETWORK *net;
    288 
    289 	if (eni == NULL)
    290 		return;
    291 	net = eni->net;
    292 
    293 	uefi_call_wrapper(net->Shutdown, 1, net);
    294 }
    295 
    296 static bool
    297 efi_net_pci_probe(struct efinetinfo *eni, EFI_DEVICE_PATH *dp, EFI_DEVICE_PATH *pdp)
    298 {
    299 #if notyet
    300 	PCI_DEVICE_PATH *pci = (PCI_DEVICE_PATH *)dp;
    301 #endif
    302 	int bus = -1;
    303 
    304 	if (pdp != NULL &&
    305 	    DevicePathType(pdp) == ACPI_DEVICE_PATH &&
    306 	    (DevicePathSubType(pdp) == ACPI_DP ||
    307 	     DevicePathSubType(pdp) == EXPANDED_ACPI_DP)) {
    308 		ACPI_HID_DEVICE_PATH *acpi = (ACPI_HID_DEVICE_PATH *)pdp;
    309 		/* PCI root bus */
    310 		if (acpi->HID == EISA_PNP_ID(0x0A08) ||
    311 		    acpi->HID == EISA_PNP_ID(0x0A03)) {
    312 			bus = acpi->UID;
    313 		}
    314 	}
    315 	if (bus < 0)
    316 		return false;
    317 
    318 #if notyet
    319 	eni->bus.type = BI_BUS_PCI;
    320 	eni->bus.tag = (bus & 0xff) << 8;
    321 	eni->bus.tag |= (pci->Device & 0x1f) << 3;
    322 	eni->bus.tag |= pci->Function & 0x7;
    323 #endif
    324 	return true;
    325 }
    326 
    327 void
    328 efi_net_probe(void)
    329 {
    330 	struct efinetinfo *enis;
    331 	struct netif_dif *dif;
    332 	struct netif_stats *stats;
    333 	EFI_DEVICE_PATH *dp0, *dp, *pdp;
    334 	EFI_SIMPLE_NETWORK *net;
    335 	EFI_HANDLE *handles;
    336 	EFI_STATUS status;
    337 	UINTN i, nhandles;
    338 	int nifs, depth = -1;
    339 	bool found;
    340 
    341 	status = LibLocateHandle(ByProtocol, &SimpleNetworkProtocol, NULL,
    342 	    &nhandles, &handles);
    343 	if (EFI_ERROR(status) || nhandles == 0)
    344 		return;
    345 
    346 	enis = alloc(nhandles * sizeof(*enis));
    347 	if (enis == NULL)
    348 		return;
    349 	memset(enis, 0, nhandles * sizeof(*enis));
    350 
    351 	if (efi_bootdp) {
    352 		depth = efi_device_path_depth(efi_bootdp, HARDWARE_DEVICE_PATH);
    353 		if (depth == 0)
    354 			depth = 1;
    355 	}
    356 
    357 	nifs = 0;
    358 	for (i = 0; i < nhandles; i++) {
    359 		status = uefi_call_wrapper(BS->HandleProtocol, 3, handles[i],
    360 		    &DevicePathProtocol, (void **)&dp0);
    361 		if (EFI_ERROR(status))
    362 			continue;
    363 
    364 		found = false;
    365 		for (dp = dp0; !IsDevicePathEnd(dp); dp = NextDevicePathNode(dp)) {
    366 			if (DevicePathType(dp) == MESSAGING_DEVICE_PATH &&
    367 			    DevicePathSubType(dp) == MSG_MAC_ADDR_DP) {
    368 				found = true;
    369 				break;
    370 			}
    371 		}
    372 		if (!found)
    373 			continue;
    374 
    375 		status = uefi_call_wrapper(BS->OpenProtocol, 6, handles[i],
    376 		    &SimpleNetworkProtocol, (void **)&net, IH, NULL,
    377 		    EFI_OPEN_PROTOCOL_EXCLUSIVE);
    378 		if (EFI_ERROR(status)) {
    379 			printf("Unable to open network interface %" PRIuMAX
    380 			    " for exclusive access: %" PRIxMAX "\n",
    381 			    (uintmax_t)i, (uintmax_t)status);
    382 		}
    383 
    384 		found = false;
    385 		for (pdp = NULL, dp = dp0;
    386 		    !IsDevicePathEnd(dp);
    387 		    pdp = dp, dp = NextDevicePathNode(dp)) {
    388 			if (DevicePathType(dp) == HARDWARE_DEVICE_PATH) {
    389 				if (DevicePathSubType(dp) == HW_PCI_DP)
    390 					found = efi_net_pci_probe(&enis[nifs],
    391 					    dp, pdp);
    392 				break;
    393 			}
    394 		}
    395 		if (found) {
    396 			enis[nifs].net = net;
    397 			enis[nifs].bootdev = efi_pxe_match_booted_interface(
    398 			    &net->Mode->PermanentAddress, net->Mode->HwAddressSize);
    399 			enis[nifs].pktbufsz = net->Mode->MaxPacketSize +
    400 			    ETHER_EXT_LEN;
    401 			enis[nifs].pktbuf = alloc(enis[nifs].pktbufsz);
    402 			if (enis[nifs].pktbuf == NULL) {
    403 				while (i-- > 0) {
    404 					dealloc(enis[i].pktbuf, enis[i].pktbufsz);
    405 					if (i == 0)
    406 						break;
    407 				}
    408 				dealloc(enis, nhandles * sizeof(*enis));
    409 				FreePool(handles);
    410 				return;
    411 			}
    412 
    413 			if (depth > 0 && efi_device_path_ncmp(efi_bootdp, dp0, depth) == 0) {
    414 				char devname[9];
    415 				snprintf(devname, sizeof(devname), "net%u", nifs);
    416 				set_default_device(devname);
    417 			}
    418 
    419 			nifs++;
    420 		}
    421 	}
    422 
    423 	FreePool(handles);
    424 
    425 	if (nifs == 0)
    426 		return;
    427 
    428 	efinetif.netif_ifs = alloc(nifs * sizeof(*dif));
    429 	stats = alloc(nifs * sizeof(*stats));
    430 	if (efinetif.netif_ifs == NULL || stats == NULL) {
    431 		if (efinetif.netif_ifs != NULL) {
    432 			dealloc(efinetif.netif_ifs, nifs * sizeof(*dif));
    433 			efinetif.netif_ifs = NULL;
    434 		}
    435 		if (stats != NULL)
    436 			dealloc(stats, nifs * sizeof(*stats));
    437 		for (i = 0; i < nifs; i++)
    438 			dealloc(enis[i].pktbuf, enis[i].pktbufsz);
    439 		dealloc(enis, nhandles * sizeof(*enis));
    440 		return;
    441 	}
    442 	memset(efinetif.netif_ifs, 0, nifs * sizeof(*dif));
    443 	memset(stats, 0, nifs * sizeof(*stats));
    444 	efinetif.netif_nifs = nifs;
    445 
    446 	for (i = 0; i < nifs; i++) {
    447 		dif = &efinetif.netif_ifs[i];
    448 		dif->dif_unit = i;
    449 		dif->dif_nsel = 1;
    450 		dif->dif_stats = &stats[i];
    451 		dif->dif_private = &enis[i];
    452 	}
    453 }
    454 
    455 void
    456 efi_net_show(void)
    457 {
    458 	const struct netif_dif *dif;
    459 	const struct efinetinfo *eni;
    460 	EFI_SIMPLE_NETWORK *net;
    461 	int i;
    462 
    463 	for (i = 0; i < efinetif.netif_nifs; i++) {
    464 		dif = &efinetif.netif_ifs[i];
    465 		eni = dif->dif_private;
    466 		net = eni->net;
    467 
    468 		printf("net%d", dif->dif_unit);
    469 		if (net->Mode != NULL) {
    470 			for (UINT32 x = 0; x < net->Mode->HwAddressSize; x++) {
    471 				printf("%c%02x", x == 0 ? ' ' : ':',
    472 				    net->Mode->PermanentAddress.Addr[x]);
    473 			}
    474 		}
    475 #if notyet
    476 		if (eni->bus.type == BI_BUS_PCI) {
    477 			printf(" pci%d,%d,%d", (eni->bus.tag >> 8) & 0xff,
    478 			    (eni->bus.tag >> 3) & 0x1f, eni->bus.tag & 0x7);
    479 		}
    480 #endif
    481 		if (eni->bootdev)
    482 			printf(" pxeboot");
    483 		printf("\n");
    484 	}
    485 }
    486 
    487 int
    488 efi_net_get_booted_interface_unit(void)
    489 {
    490 	const struct netif_dif *dif;
    491 	const struct efinetinfo *eni;
    492 	int i;
    493 
    494 	for (i = 0; i < efinetif.netif_nifs; i++) {
    495 		dif = &efinetif.netif_ifs[i];
    496 		eni = dif->dif_private;
    497 		if (eni->bootdev)
    498 			return dif->dif_unit;
    499 	}
    500 	return -1;
    501 }
    502 
    503 int
    504 efi_net_get_booted_macaddr(uint8_t *mac)
    505 {
    506 	const struct netif_dif *dif;
    507 	const struct efinetinfo *eni;
    508 	EFI_SIMPLE_NETWORK *net;
    509 	int i;
    510 
    511 	for (i = 0; i < efinetif.netif_nifs; i++) {
    512 		dif = &efinetif.netif_ifs[i];
    513 		eni = dif->dif_private;
    514 		net = eni->net;
    515 		if (eni->bootdev && net->Mode != NULL && net->Mode->HwAddressSize == 6) {
    516 			memcpy(mac, net->Mode->PermanentAddress.Addr, 6);
    517 			return 0;
    518 		}
    519 	}
    520 
    521 	return -1;
    522 }
    523 
    524 int
    525 efi_net_open(struct open_file *f, ...)
    526 {
    527 	char **file, pathbuf[PATH_MAX], *default_device, *path, *ep;
    528 	const char *fname, *full_path;
    529 	struct devdesc desc;
    530 	intmax_t dev;
    531 	va_list ap;
    532 	int n, error;
    533 
    534 	va_start(ap, f);
    535 	fname = va_arg(ap, const char *);
    536 	file = va_arg(ap, char **);
    537 	va_end(ap);
    538 
    539 	default_device = get_default_device();
    540 	if (strchr(fname, ':') == NULL) {
    541 		if (strlen(default_device) > 0) {
    542 			snprintf(pathbuf, sizeof(pathbuf), "%s:%s", default_device, fname);
    543 			full_path = pathbuf;
    544 			path = __UNCONST(fname);
    545 		} else {
    546 			return EINVAL;
    547 		}
    548 	} else {
    549 		full_path = fname;
    550 		path = strchr(fname, ':') + 1;
    551 	}
    552 
    553 	if (strncmp(full_path, "net", 3) != 0)
    554 		return EINVAL;
    555         dev = strtoimax(full_path + 3, &ep, 10);
    556         if (dev < 0 || dev >= efinetif.netif_nifs)
    557                 return ENXIO;
    558 
    559         for (n = 0; n < ndevs; n++)
    560                 if (strcmp(DEV_NAME(&devsw[n]), "net") == 0) {
    561                         f->f_dev = &devsw[n];
    562                         break;
    563                 }
    564         if (n == ndevs)
    565                 return ENXIO;
    566 
    567 	*file = path;
    568 
    569 	//try_bootp = 1;
    570 
    571 	memset(&desc, 0, sizeof(desc));
    572 	strlcpy(desc.d_name, "net", sizeof(desc.d_name));
    573 	desc.d_unit = dev;
    574 
    575 	error = DEV_OPEN(f->f_dev)(f, &desc);
    576 	if (error)
    577 		return error;
    578 
    579 	return 0;
    580 }
    581