]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
efi-api: export UUID converter calls
authorLennart Poettering <lennart@poettering.net>
Mon, 14 Aug 2023 11:29:07 +0000 (13:29 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Mon, 23 Oct 2023 10:23:56 +0000 (11:23 +0100)
(while exporting, do some minor simplifications)

src/shared/efi-api.c
src/shared/efi-api.h

index d533d1ba7e31949bbcb24cae1c7bfffdf50720e6..4cd1091e9ac37a0c816760a47e6d0be10daeb7bd 100644 (file)
@@ -189,32 +189,6 @@ static ssize_t utf16_size(const uint16_t *s, size_t buf_len_bytes) {
         return -EINVAL; /* The terminator was not found */
 }
 
-struct guid {
-        uint32_t u1;
-        uint16_t u2;
-        uint16_t u3;
-        uint8_t u4[8];
-} _packed_;
-
-static void efi_guid_to_id128(const void *guid, sd_id128_t *id128) {
-        uint32_t u1;
-        uint16_t u2, u3;
-        const struct guid *uuid = guid;
-
-        memcpy(&u1, &uuid->u1, sizeof(uint32_t));
-        id128->bytes[0] = (u1 >> 24) & 0xff;
-        id128->bytes[1] = (u1 >> 16) & 0xff;
-        id128->bytes[2] = (u1 >> 8) & 0xff;
-        id128->bytes[3] = u1 & 0xff;
-        memcpy(&u2, &uuid->u2, sizeof(uint16_t));
-        id128->bytes[4] = (u2 >> 8) & 0xff;
-        id128->bytes[5] = u2 & 0xff;
-        memcpy(&u3, &uuid->u3, sizeof(uint16_t));
-        id128->bytes[6] = (u3 >> 8) & 0xff;
-        id128->bytes[7] = u3 & 0xff;
-        memcpy(&id128->bytes[8], uuid->u4, sizeof(uuid->u4));
-}
-
 int efi_get_boot_option(
                 uint16_t id,
                 char **ret_title,
@@ -290,7 +264,7 @@ int efi_get_boot_option(
                                         continue;
 
                                 if (ret_part_uuid)
-                                        efi_guid_to_id128(dpath->drive.signature, &p_uuid);
+                                        p_uuid = efi_guid_to_id128(dpath->drive.signature);
                                 continue;
                         }
 
@@ -326,16 +300,6 @@ static void to_utf16(uint16_t *dest, const char *src) {
         dest[i] = '\0';
 }
 
-static void id128_to_efi_guid(sd_id128_t id, void *guid) {
-        struct guid uuid = {
-                .u1 = id.bytes[0] << 24 | id.bytes[1] << 16 | id.bytes[2] << 8 | id.bytes[3],
-                .u2 = id.bytes[4] << 8 | id.bytes[5],
-                .u3 = id.bytes[6] << 8 | id.bytes[7],
-        };
-        memcpy(uuid.u4, id.bytes+8, sizeof(uuid.u4));
-        memcpy(guid, &uuid, sizeof(uuid));
-}
-
 static uint16_t *tilt_slashes(uint16_t *s) {
         for (uint16_t *p = s; *p; p++)
                 if (*p == '/')
@@ -388,7 +352,7 @@ int efi_add_boot_option(
         memcpy(&devicep->drive.part_nr, &part, sizeof(uint32_t));
         memcpy(&devicep->drive.part_start, &pstart, sizeof(uint64_t));
         memcpy(&devicep->drive.part_size, &psize, sizeof(uint64_t));
-        id128_to_efi_guid(part_uuid, devicep->drive.signature);
+        efi_id128_to_guid(part_uuid, devicep->drive.signature);
         devicep->drive.mbr_type = MBR_TYPE_EFI_PARTITION_TABLE_HEADER;
         devicep->drive.signature_type = SIGNATURE_TYPE_GUID;
         size += devicep->length;
@@ -551,3 +515,42 @@ bool efi_has_tpm2(void) {
 }
 
 #endif
+
+struct efi_guid {
+        uint32_t u1;
+        uint16_t u2;
+        uint16_t u3;
+        uint8_t u4[8];
+} _packed_;
+
+sd_id128_t efi_guid_to_id128(const void *guid) {
+        const struct efi_guid *uuid = ASSERT_PTR(guid); /* cast is safe, because struct efi_guid is packed */
+        sd_id128_t id128;
+
+        id128.bytes[0] = (uuid->u1 >> 24) & 0xff;
+        id128.bytes[1] = (uuid->u1 >> 16) & 0xff;
+        id128.bytes[2] = (uuid->u1 >> 8) & 0xff;
+        id128.bytes[3] = uuid->u1 & 0xff;
+
+        id128.bytes[4] = (uuid->u2 >> 8) & 0xff;
+        id128.bytes[5] = uuid->u2 & 0xff;
+
+        id128.bytes[6] = (uuid->u3 >> 8) & 0xff;
+        id128.bytes[7] = uuid->u3 & 0xff;
+
+        memcpy(&id128.bytes[8], uuid->u4, sizeof(uuid->u4));
+
+        return id128;
+}
+
+void efi_id128_to_guid(sd_id128_t id, void *ret_guid) {
+        assert(ret_guid);
+
+        struct efi_guid uuid = {
+                .u1 = id.bytes[0] << 24 | id.bytes[1] << 16 | id.bytes[2] << 8 | id.bytes[3],
+                .u2 = id.bytes[4] << 8 | id.bytes[5],
+                .u3 = id.bytes[6] << 8 | id.bytes[7],
+        };
+        memcpy(uuid.u4, id.bytes+8, sizeof(uuid.u4));
+        memcpy(ret_guid, &uuid, sizeof(uuid));
+}
index c36524f4042ead7711c59db6b5d478d9a3da80a9..09071b22c99dcd5e6b985336647b42998966bc5c 100644 (file)
@@ -69,3 +69,6 @@ static inline bool efi_has_tpm2(void) {
 static inline char *efi_tilt_backslashes(char *s) {
         return string_replace_char(s, '\\', '/');
 }
+
+sd_id128_t efi_guid_to_id128(const void *guid);
+void efi_id128_to_guid(sd_id128_t id, void *ret_guid);