|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Decode Titan errc flash log dumps into readable entries.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import struct |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +TI_LOG_MAGIC = 0xD1A60001 |
| 11 | +TI_LOG_ENTRY_SIZE = 128 |
| 12 | + |
| 13 | +ERRC_NAMES = { |
| 14 | + 0: "TI_ERRC_NONE", |
| 15 | + 1: "TI_ERRC_UNKNOWN", |
| 16 | + 2: "TI_ERRC_INVALID_ARG", |
| 17 | + 3: "TI_ERRC_BUSY", |
| 18 | + 4: "TI_ERRC_TIMEOUT", |
| 19 | + 5: "TI_ERRC_OVERFLOW", |
| 20 | + 6: "TI_ERRC_NOT_FOUND", |
| 21 | + 7: "TI_ERRC_INTERNAL", |
| 22 | + 8: "TI_ERRC_DEVICE", |
| 23 | + 9: "TI_ERRC_BUS", |
| 24 | + 10: "TI_ERRC_CHECKSUM", |
| 25 | + 11: "TI_ERRC_PROTOCOL", |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +def cstr(buf: bytes) -> str: |
| 30 | + return buf.split(b"\x00", 1)[0].decode("utf-8", errors="replace") |
| 31 | + |
| 32 | + |
| 33 | +def decode_entry(entry: bytes) -> tuple[int, int, int, str, str, str]: |
| 34 | + magic = struct.unpack_from("<I", entry, 0)[0] |
| 35 | + errc = entry[4] |
| 36 | + line = struct.unpack_from("<I", entry, 8)[0] |
| 37 | + func = cstr(entry[12:40]) |
| 38 | + file = cstr(entry[40:80]) |
| 39 | + msg = cstr(entry[80:128]) |
| 40 | + return (magic, errc, line, func, file, msg) |
| 41 | + |
| 42 | +# Ex command |
| 43 | +# ./build.sh test_errc |
| 44 | +# openocd -f interface/stlink.cfg -f target/stm32h7x_dual_bank.cfg -c "init; reset halt; dump_image errc_log.bin 0x081E0000 0x20000; shutdown" |
| 45 | +# python3 tools/decode_errc_log.py errc_log.bin --limit 20 |
| 46 | +def main() -> int: |
| 47 | + parser = argparse.ArgumentParser(description="Decode Titan errc_log.bin") |
| 48 | + parser.add_argument("bin_file", type=Path, help="Path to errc_log.bin") |
| 49 | + parser.add_argument("--base", default="0x081E0000", help="Flash base address for display") |
| 50 | + parser.add_argument("--limit", type=int, default=32, help="Max entries to print") |
| 51 | + args = parser.parse_args() |
| 52 | + |
| 53 | + base_addr = int(args.base, 0) |
| 54 | + data = args.bin_file.read_bytes() |
| 55 | + |
| 56 | + printed = 0 |
| 57 | + for offset in range(0, len(data), TI_LOG_ENTRY_SIZE): |
| 58 | + entry = data[offset : offset + TI_LOG_ENTRY_SIZE] |
| 59 | + if len(entry) < TI_LOG_ENTRY_SIZE: |
| 60 | + break |
| 61 | + |
| 62 | + magic, errc, line, func, file, msg = decode_entry(entry) |
| 63 | + |
| 64 | + if magic == 0xFFFFFFFF: |
| 65 | + continue |
| 66 | + if magic != TI_LOG_MAGIC: |
| 67 | + continue |
| 68 | + |
| 69 | + errc_name = ERRC_NAMES.get(errc, f"UNKNOWN({errc})") |
| 70 | + abs_addr = base_addr + offset |
| 71 | + print( |
| 72 | + f"0x{abs_addr:08X} {errc_name:<18} line={line:<5} " |
| 73 | + f"file={file:<20} func={func:<24} msg={msg}" |
| 74 | + ) |
| 75 | + printed += 1 |
| 76 | + if printed >= args.limit: |
| 77 | + break |
| 78 | + |
| 79 | + if printed == 0: |
| 80 | + print("No valid log entries found (magic mismatch or region empty).") |
| 81 | + return 0 |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + raise SystemExit(main()) |
0 commit comments