git: 965f85271c11 - main - libefivar: Handle USBxxx device path when optional para is not specified

From: Warner Losh <imp_at_FreeBSD.org>
Date: Sun, 27 Feb 2022 16:47:51 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=965f85271c11e6c5901c09984472fa74d2266e1c

commit 965f85271c11e6c5901c09984472fa74d2266e1c
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2022-02-25 17:54:51 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2022-02-27 16:13:24 +0000

    libefivar: Handle USBxxx device path when optional para is not specified
    
    According to UEFI spec,
    for the Messaging Device Path with USB Class SubType, some paras
    are optional in the text device path.
    Take UsbClass(VID,PID,Class,SubClass,Protocol) for example,
    The VID is an integer between 0 and 65535 and is optional. The
    default value is 0xFFFF.
    The PID is an integer between 0 and 65535 and is optional. The
    default value is 0xFFFF.
    The Class is an integer between 0 and 255 and is optional. The
    default value is 0xFF.
    The SubClass is an integer between 0 and 255 and is optional. The
    default value is 0xFF.
    The Protocol is an integer between 0 and 255 and is optional. The
    default value is 0xFF.
    So if any the optional para is not specified in the text device,
    we should set related para in the node structure to default value.
    
    This commit is to do the enhancement for USB Class device path
    when optional para is not specified.
    
    Upstream Bug:   https://bugzilla.tianocore.org/show_bug.cgi?id=1243
    Obtained from:  https://github.com/tianocore/edk2/commit/3874108034eb3f1d5d5180df33a5dfdd5fab5d25
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/581
---
 lib/libefivar/efivar-dp-parse.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/lib/libefivar/efivar-dp-parse.c b/lib/libefivar/efivar-dp-parse.c
index 303fc9676425..a4c7cff57c06 100644
--- a/lib/libefivar/efivar-dp-parse.c
+++ b/lib/libefivar/efivar-dp-parse.c
@@ -2185,22 +2185,42 @@ ConvertFromTextUsbClass (
   PIDStr      = GetNextParamStr (&TextDeviceNode);
   if (UsbClassText->ClassExist) {
     ClassStr = GetNextParamStr (&TextDeviceNode);
-    UsbClass->DeviceClass = (UINT8) Strtoi (ClassStr);
+    if (*ClassStr == '\0') {
+      UsbClass->DeviceClass = 0xFF;
+    } else {
+      UsbClass->DeviceClass = (UINT8) Strtoi (ClassStr);
+    }
   } else {
     UsbClass->DeviceClass = UsbClassText->Class;
   }
   if (UsbClassText->SubClassExist) {
     SubClassStr = GetNextParamStr (&TextDeviceNode);
-    UsbClass->DeviceSubClass = (UINT8) Strtoi (SubClassStr);
+    if (*SubClassStr == '\0') {
+      UsbClass->DeviceSubClass = 0xFF;
+    } else {
+      UsbClass->DeviceSubClass = (UINT8) Strtoi (SubClassStr);
+    }
   } else {
     UsbClass->DeviceSubClass = UsbClassText->SubClass;
   }
 
   ProtocolStr = GetNextParamStr (&TextDeviceNode);
 
-  UsbClass->VendorId        = (UINT16) Strtoi (VIDStr);
-  UsbClass->ProductId       = (UINT16) Strtoi (PIDStr);
-  UsbClass->DeviceProtocol  = (UINT8) Strtoi (ProtocolStr);
+  if (*VIDStr == '\0') {
+    UsbClass->VendorId        = 0xFFFF;
+  } else {
+    UsbClass->VendorId        = (UINT16) Strtoi (VIDStr);
+  }
+  if (*PIDStr == '\0') {
+    UsbClass->ProductId       = 0xFFFF;
+  } else {
+    UsbClass->ProductId       = (UINT16) Strtoi (PIDStr);
+  }
+  if (*ProtocolStr == '\0') {
+    UsbClass->DeviceProtocol  = 0xFF;
+  } else {
+    UsbClass->DeviceProtocol  = (UINT8) Strtoi (ProtocolStr);
+  }
 
   return (EFI_DEVICE_PATH_PROTOCOL *) UsbClass;
 }