]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/efi/util.h
Merge pull request #18423 from DaanDeMeyer/sd-boot-no-include
[thirdparty/systemd.git] / src / boot / efi / util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <efi.h>
5 #include <efilib.h>
6
7 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
8 #define OFFSETOF(x,y) __builtin_offsetof(x,y)
9
10 static inline UINTN ALIGN_TO(UINTN l, UINTN ali) {
11 return ((l + ali - 1) & ~(ali - 1));
12 }
13
14 static inline const CHAR16 *yes_no(BOOLEAN b) {
15 return b ? L"yes" : L"no";
16 }
17
18 EFI_STATUS parse_boolean(const CHAR8 *v, BOOLEAN *b);
19
20 UINT64 ticks_read(void);
21 UINT64 ticks_freq(void);
22 UINT64 time_usec(void);
23
24 EFI_STATUS efivar_set(const EFI_GUID *vendor, const CHAR16 *name, const CHAR16 *value, BOOLEAN persistent);
25 EFI_STATUS efivar_set_raw(const EFI_GUID *vendor, const CHAR16 *name, const VOID *buf, UINTN size, BOOLEAN persistent);
26 EFI_STATUS efivar_set_uint_string(const EFI_GUID *vendor, CHAR16 *name, UINTN i, BOOLEAN persistent);
27 EFI_STATUS efivar_set_uint32_le(const EFI_GUID *vendor, CHAR16 *NAME, UINT32 value, BOOLEAN persistent);
28 EFI_STATUS efivar_set_uint64_le(const EFI_GUID *vendor, CHAR16 *name, UINT64 value, BOOLEAN persistent);
29 VOID efivar_set_time_usec(const EFI_GUID *vendor, CHAR16 *name, UINT64 usec);
30
31 EFI_STATUS efivar_get(const EFI_GUID *vendor, const CHAR16 *name, CHAR16 **value);
32 EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, const CHAR16 *name, CHAR8 **buffer, UINTN *size);
33 EFI_STATUS efivar_get_uint_string(const EFI_GUID *vendor, const CHAR16 *name, UINTN *i);
34 EFI_STATUS efivar_get_uint32_le(const EFI_GUID *vendor, const CHAR16 *name, UINT32 *ret);
35 EFI_STATUS efivar_get_uint64_le(const EFI_GUID *vendor, const CHAR16 *name, UINT64 *ret);
36 EFI_STATUS efivar_get_boolean_u8(const EFI_GUID *vendor, const CHAR16 *name, BOOLEAN *ret);
37
38 CHAR8 *strchra(CHAR8 *s, CHAR8 c);
39 CHAR16 *stra_to_path(CHAR8 *stra);
40 CHAR16 *stra_to_str(CHAR8 *stra);
41
42 const CHAR16 *startswith(const CHAR16 *s, const CHAR16 *prefix);
43 const CHAR16 *endswith(const CHAR16 *s, const CHAR16 *postfix);
44
45 const CHAR16 *startswith_no_case(const CHAR16 *s, const CHAR16 *prefix);
46 const CHAR16 *endswith_no_case(const CHAR16 *s, const CHAR16 *postfix);
47
48 EFI_STATUS file_read(EFI_FILE_HANDLE dir, const CHAR16 *name, UINTN off, UINTN size, CHAR8 **content, UINTN *content_size);
49
50 static inline void FreePoolp(void *p) {
51 void *q = *(void**) p;
52
53 if (!q)
54 return;
55
56 FreePool(q);
57 }
58
59 #define _cleanup_(x) __attribute__((__cleanup__(x)))
60 #define _cleanup_freepool_ _cleanup_(FreePoolp)
61
62 static inline void FileHandleClosep(EFI_FILE_HANDLE *handle) {
63 if (!*handle)
64 return;
65
66 uefi_call_wrapper((*handle)->Close, 1, *handle);
67 }
68
69 /*
70 * Allocated random UUID, intended to be shared across tools that implement
71 * the (ESP)\loader\entries\<vendor>-<revision>.conf convention and the
72 * associated EFI variables.
73 */
74 #define LOADER_GUID \
75 &(const EFI_GUID) { 0x4a67b082, 0x0a4c, 0x41cf, { 0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f } }
76 #define EFI_GLOBAL_GUID &(const EFI_GUID) EFI_GLOBAL_VARIABLE
77
78 #define UINTN_MAX (~(UINTN)0)
79 #define INTN_MAX ((INTN)(UINTN_MAX>>1))
80
81 #define TAKE_PTR(ptr) \
82 ({ \
83 typeof(ptr) _ptr_ = (ptr); \
84 (ptr) = NULL; \
85 _ptr_; \
86 })
87
88 EFI_STATUS log_oom(void);