]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/boot/efi/util.h
Merge pull request #21981 from medhefgo/boot-cleanup
[thirdparty/systemd.git] / src / boot / efi / util.h
index e925f1728bddb252c941afd4a8754267dd977bec..58ca44443da656b0c0d049a96236e2b373af1615 100644 (file)
@@ -13,7 +13,7 @@
 /* This TPM PCR is where most Linux infrastructure extends the initrd binary images into, and so do we. */
 #define TPM_PCR_INDEX_INITRD 4
 
-#define OFFSETOF(x,y) __builtin_offsetof(x,y)
+#define offsetof(type, member) __builtin_offsetof(type, member)
 
 #define UINTN_MAX (~(UINTN)0)
 #define INTN_MAX ((INTN)(UINTN_MAX>>1))
 #define UINT64_MAX ((UINT64) -1)
 #endif
 
+#define assert_alloc_ret(p)     \
+        ({                      \
+                void *_p = (p); \
+                assert(_p);     \
+                _p;             \
+        })
+
+#define xnew_alloc(type, n, alloc)                                           \
+        ({                                                                   \
+                UINTN _alloc_size;                                           \
+                if (__builtin_mul_overflow(sizeof(type), (n), &_alloc_size)) \
+                        assert_not_reached();                                \
+                (type *) alloc(_alloc_size);                                 \
+        })
+
+#define xallocate_pool(size) assert_alloc_ret(AllocatePool(size))
+#define xallocate_zero_pool(size) assert_alloc_ret(AllocateZeroPool(size))
+#define xreallocate_pool(p, old_size, new_size) assert_alloc_ret(ReallocatePool((p), (old_size), (new_size)))
+#define xpool_print(fmt, ...) ((CHAR16 *) assert_alloc_ret(PoolPrint((fmt), ##__VA_ARGS__)))
+#define xstrdup(str) ((CHAR16 *) assert_alloc_ret(StrDuplicate(str)))
+#define xnew(type, n) xnew_alloc(type, (n), xallocate_pool)
+#define xnew0(type, n) xnew_alloc(type, (n), xallocate_zero_pool)
+
 EFI_STATUS parse_boolean(const CHAR8 *v, BOOLEAN *b);
 
 UINT64 ticks_read(void);
@@ -45,12 +68,12 @@ EFI_STATUS efivar_get_uint64_le(const EFI_GUID *vendor, const CHAR16 *name, UINT
 EFI_STATUS efivar_get_boolean_u8(const EFI_GUID *vendor, const CHAR16 *name, BOOLEAN *ret);
 
 CHAR8 *strchra(const CHAR8 *s, CHAR8 c);
-CHAR16 *stra_to_path(const CHAR8 *stra);
-CHAR16 *stra_to_str(const CHAR8 *stra);
+CHAR16 *xstra_to_path(const CHAR8 *stra);
+CHAR16 *xstra_to_str(const CHAR8 *stra);
 
 EFI_STATUS file_read(EFI_FILE_HANDLE dir, const CHAR16 *name, UINTN off, UINTN size, CHAR8 **content, UINTN *content_size);
 
-static inline void FreePoolp(void *p) {
+static inline void free_poolp(void *p) {
         void *q = *(void**) p;
 
         if (!q)
@@ -59,9 +82,9 @@ static inline void FreePoolp(void *p) {
         FreePool(q);
 }
 
-#define _cleanup_freepool_ _cleanup_(FreePoolp)
+#define _cleanup_freepool_ _cleanup_(free_poolp)
 
-static inline void FileHandleClosep(EFI_FILE_HANDLE *handle) {
+static inline void file_handle_closep(EFI_FILE_HANDLE *handle) {
         if (!*handle)
                 return;
 
@@ -88,13 +111,6 @@ EFI_STATUS log_oom(void);
                 err; \
         })
 
-void *memmem_safe(const void *haystack, UINTN haystack_len, const void *needle, UINTN needle_len);
-
-static inline void *mempmem_safe(const void *haystack, UINTN haystack_len, const void *needle, UINTN needle_len) {
-        CHAR8 *p = memmem_safe(haystack, haystack_len, needle, needle_len);
-        return p ? p + needle_len : NULL;
-}
-
 void print_at(UINTN x, UINTN y, UINTN attr, const CHAR16 *str);
 void clear_screen(UINTN attr);
 
@@ -106,7 +122,11 @@ EFI_STATUS get_file_info_harder(EFI_FILE_HANDLE handle, EFI_FILE_INFO **ret, UIN
 EFI_STATUS readdir_harder(EFI_FILE_HANDLE handle, EFI_FILE_INFO **buffer, UINTN *buffer_size);
 
 UINTN strnlena(const CHAR8 *p, UINTN maxlen);
-CHAR8 *strndup8(const CHAR8 *p, UINTN sz);
+CHAR8 *xstrndup8(const CHAR8 *p, UINTN sz);
+INTN strncasecmpa(const CHAR8 *a, const CHAR8 *b, UINTN maxlen);
+static inline BOOLEAN strncaseeqa(const CHAR8 *a, const CHAR8 *b, UINTN maxlen) {
+        return strncasecmpa(a, b, maxlen) == 0;
+}
 
 BOOLEAN is_ascii(const CHAR16 *f);
 
@@ -139,3 +159,13 @@ static inline void *PHYSICAL_ADDRESS_TO_POINTER(EFI_PHYSICAL_ADDRESS addr) {
 }
 
 UINT64 get_os_indications_supported(void);
+
+#ifdef EFI_DEBUG
+void debug_break(void);
+extern UINT8 _text, _data;
+/* Report the relocated position of text and data sections so that a debugger
+ * can attach to us. See debug-sd-boot.sh for how this can be done. */
+#  define debug_hook(identity) Print(identity L"@0x%x,0x%x\n", &_text, &_data)
+#else
+#  define debug_hook(identity)
+#endif