From 1ea717577ff49c661edf602ce7e361345165c52b Mon Sep 17 00:00:00 2001 From: Justin Hibbits Date: Fri, 12 Jul 2019 00:54:20 +0000 Subject: [PATCH] Allow efi loader to get network params from uboot Summary: efi loader does not work with static network parameters. It always uses BOOTP/DHCP and also uses RARP as a fallback. Problems with DHCP servers can cause the loader to fail to populate network parameters. Submitted by: Siddharth Tuli Reviewed by: imp Sponsored by: Juniper Networks, Inc. Differential Revision: https://reviews.freebsd.org/D20811 --- stand/efi/libefi/efinet.c | 73 +++++++++++++++++++++++++++++++++++++++ stand/libsa/net.h | 1 + 2 files changed, 74 insertions(+) diff --git a/stand/efi/libefi/efinet.c b/stand/efi/libefi/efinet.c index ff8988d7e33c..51b9687d6772 100644 --- a/stand/efi/libefi/efinet.c +++ b/stand/efi/libefi/efinet.c @@ -40,6 +40,8 @@ __FBSDID("$FreeBSD$"); #include #include +#include "dev_net.h" + static EFI_GUID sn_guid = EFI_SIMPLE_NETWORK_PROTOCOL; static void efinet_end(struct netif *); @@ -198,6 +200,74 @@ efinet_get(struct iodesc *desc, void **pkt, time_t timeout) return (ret); } +/* + * Loader uses BOOTP/DHCP and also uses RARP as a fallback to populate + * network parameters and problems with DHCP servers can cause the loader + * to fail to populate them. Allow the device to ask about the basic + * network parameters and if present use them. + */ +static void +efi_env_net_params(struct iodesc *desc) +{ + char *envstr; + in_addr_t ipaddr, mask, gwaddr, serveraddr; + n_long rootaddr; + + if ((envstr = getenv("rootpath")) != NULL) + strlcpy(rootpath, envstr, sizeof(rootpath)); + + /* + * Get network parameters. + */ + envstr = getenv("ipaddr"); + ipaddr = (envstr != NULL) ? inet_addr(envstr) : 0; + + envstr = getenv("netmask"); + mask = (envstr != NULL) ? inet_addr(envstr) : 0; + + envstr = getenv("gatewayip"); + gwaddr = (envstr != NULL) ? inet_addr(envstr) : 0; + + envstr = getenv("serverip"); + serveraddr = (envstr != NULL) ? inet_addr(envstr) : 0; + + /* No network params. */ + if (ipaddr == 0 && mask == 0 && gwaddr == 0 && serveraddr == 0) + return; + + /* Partial network params. */ + if (ipaddr == 0 || mask == 0 || gwaddr == 0 || serveraddr == 0) { + printf("Incomplete network settings from U-Boot\n"); + return; + } + + /* + * Set network parameters. + */ + myip.s_addr = ipaddr; + netmask = mask; + gateip.s_addr = gwaddr; + servip.s_addr = serveraddr; + + /* + * There must be a rootpath. It may be ip:/path or it may be just the + * path in which case the ip needs to be serverip. + */ + rootaddr = net_parse_rootpath(); + if (rootaddr == INADDR_NONE) + rootaddr = serveraddr; + rootip.s_addr = rootaddr; + +#ifdef EFINET_DEBUG + printf("%s: ip=%s\n", __func__, inet_ntoa(myip)); + printf("%s: mask=%s\n", __func__, intoa(netmask)); + printf("%s: gateway=%s\n", __func__, inet_ntoa(gateip)); + printf("%s: server=%s\n", __func__, inet_ntoa(servip)); +#endif + + desc->myip = myip; +} + static void efinet_init(struct iodesc *desc, void *machdep_hint) { @@ -207,6 +277,9 @@ efinet_init(struct iodesc *desc, void *machdep_hint) EFI_STATUS status; UINT32 mask; + /* Attempt to get netboot params from env */ + efi_env_net_params(desc); + if (nif->nif_driver->netif_ifs[nif->nif_unit].dif_unit < 0) { printf("Invalid network interface %d\n", nif->nif_unit); return; diff --git a/stand/libsa/net.h b/stand/libsa/net.h index b1b1277f52de..36b7cacc95ef 100644 --- a/stand/libsa/net.h +++ b/stand/libsa/net.h @@ -91,6 +91,7 @@ extern struct in_addr rootip; extern struct in_addr swapip; extern struct in_addr gateip; extern struct in_addr nameip; +extern struct in_addr servip; extern n_long netmask; extern u_int intf_mtu;