]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: Remove some device path helper macros
authorJan Janssen <medhefgo@web.de>
Sat, 7 Jan 2023 10:46:41 +0000 (11:46 +0100)
committerJan Janssen <medhefgo@web.de>
Wed, 22 Feb 2023 20:54:11 +0000 (21:54 +0100)
In gnu-efi/EDK2 device paths are not marked as packed and instead the
Length field is split into 2 bytes. Accessing those requires these
helper macros as device paths may be unaligned.

Since our own efi headers define device path structs as packed, we can
access these directly, making code much more readable.

src/boot/efi/cpio.c
src/boot/efi/device-path-util.c
src/boot/efi/part-discovery.c
src/boot/efi/proto/device-path.h

index acfefbb001576067fc1374ba82a8ac41fe795c5c..c53f19b25c32b7de502ff53e1e31c681bfa714a4 100644 (file)
@@ -312,7 +312,7 @@ static char16_t *get_dropin_dir(const EFI_DEVICE_PATH *file_path) {
 
         /* Make sure we really only got file paths. */
         for (const EFI_DEVICE_PATH *node = file_path; !IsDevicePathEnd(node); node = NextDevicePathNode(node))
-                if (DevicePathType(node) != MEDIA_DEVICE_PATH || DevicePathSubType(node) != MEDIA_FILEPATH_DP)
+                if (node->Type != MEDIA_DEVICE_PATH || node->SubType != MEDIA_FILEPATH_DP)
                         return NULL;
 
         _cleanup_free_ char16_t *file_path_str = NULL;
index 4e0b3db6110519129b3a022d618b3f352f436893..a693c3f2962fe47f04edb54be915eb10a31c2148 100644 (file)
@@ -55,11 +55,10 @@ EFI_STATUS device_path_to_str(const EFI_DEVICE_PATH *dp, char16_t **ret) {
                 for (const EFI_DEVICE_PATH *node = dp; !IsDevicePathEnd(node);
                      node = NextDevicePathNode(node)) {
 
-                        if (DevicePathType(node) != MEDIA_DEVICE_PATH ||
-                            DevicePathSubType(node) != MEDIA_FILEPATH_DP)
+                        if (node->Type != MEDIA_DEVICE_PATH || node->SubType != MEDIA_FILEPATH_DP)
                                 return err;
 
-                        size_t path_size = DevicePathNodeLength(node);
+                        size_t path_size = node->Length;
                         if (path_size <= offsetof(FILEPATH_DEVICE_PATH, PathName) || path_size % sizeof(char16_t))
                                 return EFI_INVALID_PARAMETER;
                         path_size -= offsetof(FILEPATH_DEVICE_PATH, PathName);
@@ -99,11 +98,9 @@ bool device_path_startswith(const EFI_DEVICE_PATH *dp, const EFI_DEVICE_PATH *st
                         return true;
                 if (IsDevicePathEnd(dp))
                         return false;
-                size_t l1 = DevicePathNodeLength(start);
-                size_t l2 = DevicePathNodeLength(dp);
-                if (l1 != l2)
+                if (start->Length != dp->Length)
                         return false;
-                if (memcmp(dp, start, l1) != 0)
+                if (memcmp(dp, start, start->Length) != 0)
                         return false;
                 start = NextDevicePathNode(start);
                 dp = NextDevicePathNode(dp);
@@ -119,15 +116,12 @@ EFI_DEVICE_PATH *device_path_replace_node(
         assert(path);
         assert(node);
 
-        size_t len = (uint8_t *) node - (uint8_t *) path, new_node_len = 0;
-        if (new_node)
-                new_node_len = DevicePathNodeLength(new_node);
-
-        EFI_DEVICE_PATH *ret = xmalloc(len + new_node_len + sizeof(EFI_DEVICE_PATH));
+        size_t len = (uint8_t *) node - (uint8_t *) path;
+        EFI_DEVICE_PATH *ret = xmalloc(len + (new_node ? new_node->Length : 0) + sizeof(EFI_DEVICE_PATH));
         EFI_DEVICE_PATH *end = mempcpy(ret, path, len);
 
         if (new_node)
-                end = mempcpy(end, new_node, new_node_len);
+                end = mempcpy(end, new_node, new_node->Length);
 
         SetDevicePathEndNode(end);
         return ret;
index e71daf0382d47912575d7426c3938981a8bdde51..2524eb6fd0e49790205a149e72d6038f3cc2dbe2 100644 (file)
@@ -166,10 +166,7 @@ static EFI_STATUS find_device(const EFI_GUID *type, EFI_HANDLE *device, EFI_DEVI
         /* Find the (last) partition node itself. */
         EFI_DEVICE_PATH *part_node = NULL;
         for (EFI_DEVICE_PATH *node = partition_path; !IsDevicePathEnd(node); node = NextDevicePathNode(node)) {
-                if (DevicePathType(node) != MEDIA_DEVICE_PATH)
-                        continue;
-
-                if (DevicePathSubType(node) != MEDIA_HARDDRIVE_DP)
+                if (node->Type != MEDIA_DEVICE_PATH || node->SubType != MEDIA_HARDDRIVE_DP)
                         continue;
 
                 part_node = node;
@@ -286,14 +283,12 @@ char16_t *disk_get_part_uuid(EFI_HANDLE *handle) {
                 return NULL;
 
         for (; !IsDevicePathEnd(dp); dp = NextDevicePathNode(dp)) {
-                if (DevicePathType(dp) != MEDIA_DEVICE_PATH)
-                        continue;
-                if (DevicePathSubType(dp) != MEDIA_HARDDRIVE_DP)
+                if (dp->Type != MEDIA_DEVICE_PATH || dp->SubType != MEDIA_HARDDRIVE_DP)
                         continue;
 
                 /* The HD device path may be misaligned. */
                 HARDDRIVE_DEVICE_PATH hd;
-                memcpy(&hd, dp, MIN(sizeof(hd), (size_t) DevicePathNodeLength(dp)));
+                memcpy(&hd, dp, MIN(sizeof(hd), dp->Length));
 
                 if (hd.SignatureType != SIGNATURE_TYPE_GUID)
                         continue;
index 8d8bd0c3f77e074089600bd9aa6a6c7adb93a668..f6583b3697d0d9bb1d03c2cc9e5fd0b3730879a8 100644 (file)
@@ -82,10 +82,6 @@ typedef struct {
                         const char16_t *ConvertTextToDevicPath);
 } EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL;
 
-#define DevicePathType(dp) ((dp)->Type)
-#define DevicePathSubType(dp) ((dp)->SubType)
-#define DevicePathNodeLength(dp) ((dp)->Length)
-
 static inline EFI_DEVICE_PATH *NextDevicePathNode(const EFI_DEVICE_PATH *dp) {
         assert(dp);
         return (EFI_DEVICE_PATH *) ((uint8_t *) dp + dp->Length);