|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) The mldsa-native project authors |
| 3 | +# Copyright (c) The mlkem-native project authors |
| 4 | +# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 5 | + |
| 6 | +"""QEMU wrapper for executing Cortex-M33 bare-metal ELF binaries (mps3-an524).""" |
| 7 | + |
| 8 | +import struct as st |
| 9 | +import sys |
| 10 | +import subprocess |
| 11 | +import tempfile |
| 12 | +import os |
| 13 | + |
| 14 | + |
| 15 | +def err(msg, **kwargs): |
| 16 | + print(msg, file=sys.stderr, **kwargs) |
| 17 | + |
| 18 | + |
| 19 | +binpath = sys.argv[1] |
| 20 | +args = sys.argv[1:] |
| 21 | + |
| 22 | +# Memory layout: [argc] [offset1] [offset2] ... [string1\0] [string2\0] ... |
| 23 | +# M33-AN524 RAM: 0x20000000-0x2001FFFF (128KB) |
| 24 | +# Heap ends at: ~0x20000b20 |
| 25 | +# Stack: 0x20008000-0x2001FFFF (96KB, grows downward) |
| 26 | +# Use address after heap but before stack |
| 27 | +# cmdline.c CMDLINE_ADDR must match this value |
| 28 | +cmdline_offset = 0x20007000 |
| 29 | +arg0_offset = cmdline_offset + 4 + len(args) * 4 |
| 30 | +arg_offsets = [sum(map(len, args[:i])) + i + arg0_offset for i in range(len(args))] |
| 31 | + |
| 32 | +binargs = st.pack( |
| 33 | + f"<{1+len(args)}I" + "".join(f"{len(a)+1}s" for a in args), |
| 34 | + len(args), |
| 35 | + *arg_offsets, |
| 36 | + *map(lambda x: x.encode("utf-8"), args), |
| 37 | +) |
| 38 | + |
| 39 | +with tempfile.NamedTemporaryFile(mode="wb", delete=False, suffix=".bin") as fd: |
| 40 | + args_file = fd.name |
| 41 | + fd.write(binargs) |
| 42 | + |
| 43 | +try: |
| 44 | + qemu_cmd = f"qemu-system-arm -M mps3-an524 -cpu cortex-m33 -nographic -semihosting -kernel {binpath} -device loader,file={args_file},addr=0x{cmdline_offset:x}".split() |
| 45 | + result = subprocess.run(qemu_cmd, encoding="utf-8", capture_output=True) |
| 46 | +finally: |
| 47 | + os.unlink(args_file) |
| 48 | +if result.returncode != 0: |
| 49 | + err("FAIL!") |
| 50 | + err(f"{qemu_cmd} failed with error code {result.returncode}") |
| 51 | + err(result.stderr) |
| 52 | + exit(1) |
| 53 | + |
| 54 | +for line in result.stdout.splitlines(): |
| 55 | + print(line) |
0 commit comments