]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
efi: check if all sections of our EFI binaries are properly aligned 31907/head
authorFrantisek Sumsal <frantisek@sumsal.cz>
Fri, 22 Mar 2024 12:35:38 +0000 (13:35 +0100)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Fri, 22 Mar 2024 13:02:03 +0000 (14:02 +0100)
meson.build
src/boot/efi/meson.build
tools/check-efi-alignment.py [new file with mode: 0755]

index 8d1cd8a9ed0ee7a0c153a941257ba975463fac64..1bedbd56056c29d8268d7c687cd0f95bab0dd9c6 100644 (file)
@@ -1828,6 +1828,7 @@ conf.set10('ENABLE_UKIFY', want_ukify)
 
 #####################################################################
 
+check_efi_alignment_py = find_program('tools/check-efi-alignment.py')
 check_version_history_py = find_program('tools/check-version-history.py')
 elf2efi_py = find_program('tools/elf2efi.py')
 export_dbus_interfaces_py = find_program('tools/dbus_exporter.py')
index 786fc8038fbca2d618958fc1ddac18eb6ef66412..7a60b0ec7ed1be70cfff6fa12cadf8e8f43f3dcb 100644 (file)
@@ -407,6 +407,11 @@ foreach efi_elf_binary : efi_elf_binaries
         if name == 'addon@0@.efi.stub'.format(efi_arch)
                 efi_addon = exe.full_path()
         endif
+
+        test('check-alignment-@0@'.format(name),
+             check_efi_alignment_py,
+             args : exe.full_path(),
+             suite : 'efi')
 endforeach
 
 alias_target('systemd-boot', boot_targets)
diff --git a/tools/check-efi-alignment.py b/tools/check-efi-alignment.py
new file mode 100755 (executable)
index 0000000..bb33ac0
--- /dev/null
@@ -0,0 +1,32 @@
+#!/usr/bin/python3
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# vi: set tw=110 sw=4 ts=4 et:
+
+import sys
+
+import pefile
+
+
+def main():
+    pe = pefile.PE(sys.argv[1], fast_load=True)
+
+    for section in pe.sections:
+        name = section.Name.rstrip(b"\x00").decode()
+        file_addr = section.PointerToRawData
+        virt_addr = section.VirtualAddress
+        print(f"{name:10s} file=0x{file_addr:08x} virt=0x{virt_addr:08x}")
+
+        if file_addr % 512 != 0:
+            print(f"File address of {name} section is not aligned to 512 bytes", file=sys.stderr)
+            return 1
+
+        if virt_addr % 512 != 0:
+            print(f"Virt address of {name} section is not aligned to 512 bytes", file=sys.stderr)
+            return 1
+
+if __name__ == '__main__':
+    if len(sys.argv) != 2:
+        print(f"Usage: {sys.argv[0]} pe-image")
+        sys.exit(1)
+
+    sys.exit(main())