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