svn commit: r300810 - in head/sys/boot/efi: libefi loader

John Baldwin jhb at FreeBSD.org
Thu May 26 23:32:30 UTC 2016


Author: jhb
Date: Thu May 26 23:32:28 2016
New Revision: 300810
URL: https://svnweb.freebsd.org/changeset/base/300810

Log:
  Fix unit number of EFI net interfaces and ignore psuedo network interfaces.
  
  In r277943, the efinet_match() routine was changed to use an off by one
  when matching network interfaces.  The effect was that using "net1"
  actually used the device attached to "net0".
  
  Digging into the hardware that needed this workaround more, I found that
  UEFI was creating two simple network protocol devices for each physical
  NIC.  The first device was a "raw" Ethernet device and the second device
  was a "IP" device that used the IP protocol on top of the underlying
  "raw" device.  The PXE code in the firmware used the "IP" device to pull
  across the loader.efi, so currdev was set to "net1" when booting from the
  physical interface "net0".  (The loaded image's device handle referenced
  the "IP" device that "net1" claimed.)
  
  However, the IP device isn't suitable for doing raw packet I/O (and the
  current code to open devices exclusively actually turns the "IP" devices
  off on these systems).
  
  To fix, change the efinet driver to only attach to "raw" devices.  This
  is determined by fetching the DEVICE_PATH for each handle which supports
  the simple network protocol and examining the last node in the path.  If
  the last node in the path is a MAC address, the device is assumed to be
  a "raw" device and is added as a 'netX' device.  If the last node is not
  a MAC address, the device is ignored.
  
  However, this causes a new problem as the device handle associated with
  the loaded image no longer matches any of the handles enumerated by
  efinet for systems that load the image via the "IP" device.  To handle
  this case, expand the logic that resolves currdev from the loaded image
  in main().  First, the existing logic of looking for a handle that
  matches the loaded image's handle is tried.  If that fails, the device
  path of the handle that loaded the loaded image is fetched via
  efi_lookup_image_devpath().  This device path is then walked from the
  end up to the beginning using efi_handle_lookup() to fetch the handle
  associated with a path.  If the handle is found and is a known handle,
  then that is used as currdev.  The effect for machines that load the
  image via the "IP" device is that the first lookup fails (the handle
  for the "IP" device isn't claimed by efinet), but walking up the
  image's device path finds the handle of the raw MAC device which is used
  as currdev.
  
  With these fixes in place, the hack to subtract 1 from the unit can now
  be removed, so that setting currdev to 'net0' actually uses 'net0'.
  
  PR:		202097
  Tested by:	ambrisko
  Sponsored by:	Cisco Systems

Modified:
  head/sys/boot/efi/libefi/efinet.c
  head/sys/boot/efi/loader/main.c

Modified: head/sys/boot/efi/libefi/efinet.c
==============================================================================
--- head/sys/boot/efi/libefi/efinet.c	Thu May 26 23:27:08 2016	(r300809)
+++ head/sys/boot/efi/libefi/efinet.c	Thu May 26 23:32:28 2016	(r300810)
@@ -101,7 +101,7 @@ efinet_match(struct netif *nif, void *ma
 {
 	struct devdesc *dev = machdep_hint;
 
-	if (dev->d_unit - 1 == nif->nif_unit)
+	if (dev->d_unit == nif->nif_unit)
 		return (1);
 	return(0);
 }
@@ -271,7 +271,9 @@ efinet_dev_init()
 {
 	struct netif_dif *dif;
 	struct netif_stats *stats;
-	EFI_HANDLE *handles;
+	EFI_DEVICE_PATH *devpath, *node;
+	EFI_SIMPLE_NETWORK *net;
+	EFI_HANDLE *handles, *handles2;
 	EFI_STATUS status;
 	UINTN sz;
 	int err, i, nifs;
@@ -288,43 +290,58 @@ efinet_dev_init()
 	}
 	if (EFI_ERROR(status))
 		return (efi_status_to_errno(status));
-	nifs = sz / sizeof(EFI_HANDLE);
-	err = efi_register_handles(&efinet_dev, handles, NULL, nifs);
-	free(handles);
-	if (err != 0)
-		return (err);
-
-	efinetif.netif_nifs = nifs;
-	efinetif.netif_ifs = calloc(nifs, sizeof(struct netif_dif));
-
-	stats = calloc(nifs, sizeof(struct netif_stats));
-
-	for (i = 0; i < nifs; i++) {
-		EFI_SIMPLE_NETWORK *net;
-		EFI_HANDLE h;
-
-		dif = &efinetif.netif_ifs[i];
-		dif->dif_unit = -1;
-
-		h = efi_find_handle(&efinet_dev, i);
+	handles2 = (EFI_HANDLE *)malloc(sz);
+	nifs = 0;
+	for (i = 0; i < sz / sizeof(EFI_HANDLE); i++) {
+		devpath = efi_lookup_devpath(handles[i]);
+		if (devpath == NULL)
+			continue;
+		node = efi_devpath_last_node(devpath);
+		if (DevicePathType(node) != MESSAGING_DEVICE_PATH ||
+		    DevicePathSubType(node) != MSG_MAC_ADDR_DP)
+			continue;
 
 		/*
 		 * Open the network device in exclusive mode. Without this
 		 * we will be racing with the UEFI network stack. It will
 		 * pull packets off the network leading to lost packets.
 		 */
-		status = BS->OpenProtocol(h, &sn_guid, (void **)&net,
+		status = BS->OpenProtocol(handles[i], &sn_guid, (void **)&net,
 		    IH, 0, EFI_OPEN_PROTOCOL_EXCLUSIVE);
 		if (status != EFI_SUCCESS) {
 			printf("Unable to open network interface %d for "
-			    "exclusive access\n", i);
+			    "exclusive access: %d\n", i, EFI_ERROR(status));
 		}
 
+		handles2[nifs] = handles[i];
+		nifs++;
+	}
+	free(handles);
+	if (nifs == 0) {
+		free(handles2);
+		return (ENOENT);
+	}
+
+	err = efi_register_handles(&efinet_dev, handles2, NULL, nifs);
+	if (err != 0) {
+		free(handles2);
+		return (err);
+	}
+
+	efinetif.netif_nifs = nifs;
+	efinetif.netif_ifs = calloc(nifs, sizeof(struct netif_dif));
+
+	stats = calloc(nifs, sizeof(struct netif_stats));
+
+	for (i = 0; i < nifs; i++) {
+
+		dif = &efinetif.netif_ifs[i];
 		dif->dif_unit = i;
 		dif->dif_nsel = 1;
 		dif->dif_stats = &stats[i];
-		dif->dif_private = h;
+		dif->dif_private = handles2[i];
 	}
+	free(handles2);
 
 	return (0);
 }

Modified: head/sys/boot/efi/loader/main.c
==============================================================================
--- head/sys/boot/efi/loader/main.c	Thu May 26 23:27:08 2016	(r300809)
+++ head/sys/boot/efi/loader/main.c	Thu May 26 23:32:28 2016	(r300810)
@@ -183,6 +183,47 @@ out:
 	return retval;
 }
 
+static int
+find_currdev(EFI_LOADED_IMAGE *img, struct devsw **dev, int *unit,
+    uint64_t *extra)
+{
+	EFI_DEVICE_PATH *devpath, *copy;
+	EFI_HANDLE h;
+
+	/*
+	 * Try the device handle from our loaded image first.  If that
+	 * fails, use the device path from the loaded image and see if
+	 * any of the nodes in that path match one of the enumerated
+	 * handles.
+	 */
+	if (efi_handle_lookup(img->DeviceHandle, dev, unit, extra) == 0)
+		return (0);
+
+	copy = NULL;
+	devpath = efi_lookup_image_devpath(IH);
+	while (devpath != NULL) {
+		h = efi_devpath_handle(devpath);
+		if (h == NULL)
+			break;
+
+		if (efi_handle_lookup(h, dev, unit, extra) == 0) {
+			if (copy != NULL)
+				free(copy);
+			return (0);
+		}
+
+		if (copy != NULL)
+			free(copy);
+		devpath = efi_lookup_devpath(h);
+		if (devpath != NULL) {
+			copy = efi_devpath_trim(devpath);
+			devpath = copy;
+		}
+	}
+
+	return (ENOENT);
+}
+
 EFI_STATUS
 main(int argc, CHAR16 *argv[])
 {
@@ -358,7 +399,7 @@ main(int argc, CHAR16 *argv[])
 	 */
 	BS->SetWatchdogTimer(0, 0, 0, NULL);
 
-	if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &pool_guid) != 0)
+	if (find_currdev(img, &dev, &unit, &pool_guid) != 0)
 		return (EFI_NOT_FOUND);
 
 	switch (dev->dv_type) {


More information about the svn-src-all mailing list