summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2021-02-01 21:56:30 +0000
committerDaan De Meyer <daan.j.demeyer@gmail.com>2021-02-02 21:03:58 +0000
commit987d0a12507d7ba41803c26de679be902ba43a05 (patch)
tree4054750a62d6103a65122bfa31827945506022c6
parenta7308a266b38a9cf3eae7e9c0b86ec27e0707f45 (diff)
downloadsystemd-987d0a12507d7ba41803c26de679be902ba43a05.tar.gz
boot: Add efivar_get/set_uint64_le() functions
These are implemented as bit-shifting functions that allow reading and writing UEFI variables stored as little endian 64-bit unsigned values.
-rw-r--r--src/boot/efi/util.c61
-rw-r--r--src/boot/efi/util.h4
2 files changed, 65 insertions, 0 deletions
diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c
index dea4872158..50ad757565 100644
--- a/src/boot/efi/util.c
+++ b/src/boot/efi/util.c
@@ -97,6 +97,32 @@ EFI_STATUS efivar_set_uint_string(const EFI_GUID *vendor, CHAR16 *name, UINTN i,
return efivar_set(vendor, name, str, persistent);
}
+EFI_STATUS efivar_set_uint32_le(const EFI_GUID *vendor, CHAR16 *name, UINT32 value, BOOLEAN persistent) {
+ UINT8 buf[4];
+
+ buf[0] = (UINT8)(value >> 0U & 0xFF);
+ buf[1] = (UINT8)(value >> 8U & 0xFF);
+ buf[2] = (UINT8)(value >> 16U & 0xFF);
+ buf[3] = (UINT8)(value >> 24U & 0xFF);
+
+ return efivar_set_raw(vendor, name, buf, sizeof(buf), persistent);
+}
+
+EFI_STATUS efivar_set_uint64_le(const EFI_GUID *vendor, CHAR16 *name, UINT64 value, BOOLEAN persistent) {
+ UINT8 buf[8];
+
+ buf[0] = (UINT8)(value >> 0U & 0xFF);
+ buf[1] = (UINT8)(value >> 8U & 0xFF);
+ buf[2] = (UINT8)(value >> 16U & 0xFF);
+ buf[3] = (UINT8)(value >> 24U & 0xFF);
+ buf[4] = (UINT8)(value >> 32U & 0xFF);
+ buf[5] = (UINT8)(value >> 40U & 0xFF);
+ buf[6] = (UINT8)(value >> 48U & 0xFF);
+ buf[7] = (UINT8)(value >> 56U & 0xFF);
+
+ return efivar_set_raw(vendor, name, buf, sizeof(buf), persistent);
+}
+
EFI_STATUS efivar_get(const EFI_GUID *vendor, const CHAR16 *name, CHAR16 **value) {
_cleanup_freepool_ CHAR8 *buf = NULL;
EFI_STATUS err;
@@ -143,6 +169,41 @@ EFI_STATUS efivar_get_uint_string(const EFI_GUID *vendor, const CHAR16 *name, UI
return err;
}
+EFI_STATUS efivar_get_uint32_le(const EFI_GUID *vendor, const CHAR16 *name, UINT32 *ret) {
+ _cleanup_freepool_ CHAR8 *buf = NULL;
+ UINTN size;
+ EFI_STATUS err;
+
+ err = efivar_get_raw(vendor, name, &buf, &size);
+ if (!EFI_ERROR(err) && ret) {
+ if (size != sizeof(UINT32))
+ return EFI_BUFFER_TOO_SMALL;
+
+ *ret = (UINT32) buf[0] << 0U | (UINT32) buf[1] << 8U | (UINT32) buf[2] << 16U |
+ (UINT32) buf[3] << 24U;
+ }
+
+ return err;
+}
+
+EFI_STATUS efivar_get_uint64_le(const EFI_GUID *vendor, const CHAR16 *name, UINT64 *ret) {
+ _cleanup_freepool_ CHAR8 *buf = NULL;
+ UINTN size;
+ EFI_STATUS err;
+
+ err = efivar_get_raw(vendor, name, &buf, &size);
+ if (!EFI_ERROR(err) && ret) {
+ if (size != sizeof(UINT64))
+ return EFI_BUFFER_TOO_SMALL;
+
+ *ret = (UINT64) buf[0] << 0U | (UINT64) buf[1] << 8U | (UINT64) buf[2] << 16U |
+ (UINT64) buf[3] << 24U | (UINT64) buf[4] << 32U | (UINT64) buf[5] << 40U |
+ (UINT64) buf[6] << 48U | (UINT64) buf[7] << 56U;
+ }
+
+ return err;
+}
+
EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, const CHAR16 *name, CHAR8 **buffer, UINTN *size) {
_cleanup_freepool_ CHAR8 *buf = NULL;
UINTN l;
diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h
index f0e9610629..313d6697d2 100644
--- a/src/boot/efi/util.h
+++ b/src/boot/efi/util.h
@@ -24,11 +24,15 @@ UINT64 time_usec(void);
EFI_STATUS efivar_set(const EFI_GUID *vendor, const CHAR16 *name, const CHAR16 *value, BOOLEAN persistent);
EFI_STATUS efivar_set_raw(const EFI_GUID *vendor, const CHAR16 *name, const VOID *buf, UINTN size, BOOLEAN persistent);
EFI_STATUS efivar_set_uint_string(const EFI_GUID *vendor, CHAR16 *name, UINTN i, BOOLEAN persistent);
+EFI_STATUS efivar_set_uint32_le(const EFI_GUID *vendor, CHAR16 *NAME, UINT32 value, BOOLEAN persistent);
+EFI_STATUS efivar_set_uint64_le(const EFI_GUID *vendor, CHAR16 *name, UINT64 value, BOOLEAN persistent);
VOID efivar_set_time_usec(const EFI_GUID *vendor, CHAR16 *name, UINT64 usec);
EFI_STATUS efivar_get(const EFI_GUID *vendor, const CHAR16 *name, CHAR16 **value);
EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, const CHAR16 *name, CHAR8 **buffer, UINTN *size);
EFI_STATUS efivar_get_uint_string(const EFI_GUID *vendor, const CHAR16 *name, UINTN *i);
+EFI_STATUS efivar_get_uint32_le(const EFI_GUID *vendor, const CHAR16 *name, UINT32 *ret);
+EFI_STATUS efivar_get_uint64_le(const EFI_GUID *vendor, const CHAR16 *name, UINT64 *ret);
EFI_STATUS efivar_get_boolean_u8(const EFI_GUID *vendor, const CHAR16 *name, BOOLEAN *ret);
CHAR8 *strchra(CHAR8 *s, CHAR8 c);