/* $NetBSD: efinet.c,v 1.9 2024/01/01 13:38:57 rin Exp $ */
/*-
* Copyright (c) 2001 Doug Rabson
* Copyright (c) 2002, 2006 Marcel Moolenaar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
for (valid = 0, n = 0; n < mode->HwAddressSize; n++)
if (mode->CurrentAddress.Addr[n] != 0x00) {
valid = true;
break;
}
if (!valid)
goto use_permanent;
for (valid = 0, n = 0; n < mode->HwAddressSize; n++)
if (mode->CurrentAddress.Addr[n] != 0xff) {
valid = true;
break;
}
if (!valid)
goto use_permanent;
memcpy(ptr, pkt, len);
status = uefi_call_wrapper(net->Transmit, 7, net, 0, (UINTN)len, ptr, NULL,
NULL, NULL);
if (EFI_ERROR(status))
return -1;
/* Wait for the buffer to be transmitted */
do {
buf = NULL; /* XXX Is this needed? */
status = uefi_call_wrapper(net->GetStatus, 3, net, NULL, &buf);
/*
* XXX EFI1.1 and the E1000 card returns a different
* address than we gave. Sigh.
*/
} while (!EFI_ERROR(status) && buf == NULL);
/* XXX How do we deal with status != EFI_SUCCESS now? */
return EFI_ERROR(status) ? -1 : len;
}
if (efi_bootdp) {
/*
* Either Hardware or Messaging Device Paths can be used
* here, see Sec 10.4.4 of UEFI Spec 2.10. Try both.
*/
depth = efi_device_path_depth(efi_bootdp, HARDWARE_DEVICE_PATH);
if (depth == -1) {
depth = efi_device_path_depth(efi_bootdp,
MESSAGING_DEVICE_PATH);
}
if (depth == 0)
depth = 1;
}
nifs = 0;
for (i = 0; i < nhandles; i++) {
status = uefi_call_wrapper(BS->HandleProtocol, 3, handles[i],
&DevicePathProtocol, (void **)&dp0);
if (EFI_ERROR(status))
continue;
found = false;
for (dp = dp0; !IsDevicePathEnd(dp); dp = NextDevicePathNode(dp)) {
if (DevicePathType(dp) == MESSAGING_DEVICE_PATH &&
DevicePathSubType(dp) == MSG_MAC_ADDR_DP) {
found = true;
break;
}
}
if (!found)
continue;
status = uefi_call_wrapper(BS->OpenProtocol, 6, handles[i],
&SimpleNetworkProtocol, (void **)&net, IH, NULL,
EFI_OPEN_PROTOCOL_EXCLUSIVE);
if (EFI_ERROR(status)) {
printf("Unable to open network interface %" PRIuMAX
" for exclusive access: %" PRIxMAX "\n",
(uintmax_t)i, (uintmax_t)status);
continue;
}
for (i = 0; i < efinetif.netif_nifs; i++) {
dif = &efinetif.netif_ifs[i];
eni = dif->dif_private;
net = eni->net;
printf("net%d", dif->dif_unit);
if (net->Mode != NULL) {
const EFI_MAC_ADDRESS *mac = efinet_hwaddr(net->Mode);
for (UINT32 x = 0; x < net->Mode->HwAddressSize; x++) {
printf("%c%02x", x == 0 ? ' ' : ':',
mac->Addr[x]);
}
}
if (eni->bootdev)
printf(" pxeboot");
printf("\n");
}
}
int
efi_net_get_booted_interface_unit(void)
{
const struct netif_dif *dif;
const struct efinetinfo *eni;
int i;
for (i = 0; i < efinetif.netif_nifs; i++) {
dif = &efinetif.netif_ifs[i];
eni = dif->dif_private;
if (eni->bootdev)
return dif->dif_unit;
}
return -1;
}
int
efi_net_get_booted_macaddr(uint8_t *mac)
{
const struct netif_dif *dif;
const struct efinetinfo *eni;
EFI_SIMPLE_NETWORK *net;
int i;
for (i = 0; i < efinetif.netif_nifs; i++) {
dif = &efinetif.netif_ifs[i];
eni = dif->dif_private;
net = eni->net;
if (eni->bootdev && net->Mode != NULL && net->Mode->HwAddressSize == 6) {
memcpy(mac, net->Mode->PermanentAddress.Addr, 6);
return 0;
}
}
return -1;
}
int
efi_net_open(struct open_file *f, ...)
{
char **file, pathbuf[PATH_MAX], *default_device, *path, *ep;
const char *fname, *full_path;
struct devdesc desc;
intmax_t dev;
va_list ap;
int n, error;