1
0
mirror of https://github.com/systemd/systemd synced 2024-07-05 17:39:42 +00:00
systemd/tools/check-efi-alignment.py
Antonio Alvarez Feijoo f0896ca557 efi: skip check-alignment-* tests if pefile is not installed
Otherwise, when building with ukify disabled, no one else requires pefile, and
if it is not installed, those tests fail:

```
================================== 12/1212 ===================================
test:         systemd:efi / check-alignment-systemd-bootx64.efi
start time:   14:21:54
duration:     0.08s
result:       exit status 1
command:      /mnt/work/systemd/upstream-fork/main/tools/check-efi-alignment.py /mnt/work/systemd/upstream-fork/main/build/src/boot/efi/systemd-bootx64.efi
----------------------------------- stderr -----------------------------------
Traceback (most recent call last):
  File "/mnt/work/systemd/upstream-fork/main/tools/check-efi-alignment.py", line 7, in <module>
    import pefile
ModuleNotFoundError: No module named 'pefile'
==============================================================================

================================== 13/1212 ===================================
test:         systemd:efi / check-alignment-linuxx64.efi.stub
start time:   14:21:54
duration:     0.08s
result:       exit status 1
command:      /mnt/work/systemd/upstream-fork/main/tools/check-efi-alignment.py /mnt/work/systemd/upstream-fork/main/build/src/boot/efi/linuxx64.efi.stub
----------------------------------- stderr -----------------------------------
Traceback (most recent call last):
  File "/mnt/work/systemd/upstream-fork/main/tools/check-efi-alignment.py", line 7, in <module>
    import pefile
ModuleNotFoundError: No module named 'pefile'
==============================================================================

================================== 14/1212 ===================================
test:         systemd:efi / check-alignment-addonx64.efi.stub
start time:   14:21:54
duration:     0.07s
result:       exit status 1
command:      /mnt/work/systemd/upstream-fork/main/tools/check-efi-alignment.py /mnt/work/systemd/upstream-fork/main/build/src/boot/efi/addonx64.efi.stub
----------------------------------- stderr -----------------------------------
Traceback (most recent call last):
  File "/mnt/work/systemd/upstream-fork/main/tools/check-efi-alignment.py", line 7, in <module>
    import pefile
ModuleNotFoundError: No module named 'pefile'
==============================================================================
```

Follow-up for 7ff3b88396
2024-03-27 02:32:42 +09:00

37 lines
970 B
Python
Executable File

#!/usr/bin/python3
# SPDX-License-Identifier: LGPL-2.1-or-later
# vi: set tw=110 sw=4 ts=4 et:
import sys
try:
import pefile
except ImportError as e:
print(str(e), file=sys.stderr)
sys.exit(77)
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())