boot: Remove some device path helper macros

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.
This commit is contained in:
Jan Janssen
2023-02-22 21:54:11 +01:00
parent e6d7dc9ada
commit d755ac6214
4 changed files with 11 additions and 26 deletions
+1 -1
View 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;
+7 -13
View 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;
+3 -8
View 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;
-4
View 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);