From 4c6bdee4e882bbd384ac4358a994bf50146aa6e1 Mon Sep 17 00:00:00 2001 From: kicer Date: Mon, 22 Dec 2025 01:05:43 +0800 Subject: [PATCH] args add exec --- scripts/boot2.bin | Bin 402 -> 402 bytes scripts/boot2.s | 2 +- scripts/stm8loader.py | 39 +++++++++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/scripts/boot2.bin b/scripts/boot2.bin index 807b531b83f0c4d4321983074d80a49823d5a0b9..baf8cc1001ab607b5f180c18d3a3269f129dc6e2 100644 GIT binary patch delta 12 TcmbQlJc)UNH6#B [file]", "ERROR") continue @@ -825,7 +825,7 @@ class STM8Bootloader: except Exception as e: self.log(f"Error: {e}", "ERROR") - elif cmd == 'write': + elif cmd == 'write' or cmd == 'w': if len(args) < 3: self.log("Usage: write ", "ERROR") self.log("Example: write 0x8000 firmware.bin", "INFO") @@ -854,7 +854,7 @@ class STM8Bootloader: except Exception as e: self.log(f"Error: {e}", "ERROR") - elif cmd == 'exec': + elif cmd == 'exec' or cmd == 'x': if len(args) < 2: self.log("Usage: exec ", "ERROR") self.log("Example: exec 4F9D (CLR A; NOP)", "INFO") @@ -880,7 +880,7 @@ class STM8Bootloader: except Exception as e: self.log(f"Error: {e}", "ERROR") - elif cmd == 'go': + elif cmd == 'go' or cmd == 'g': if len(args) < 2: self.log("Usage: go ", "ERROR") continue @@ -916,28 +916,28 @@ class STM8Bootloader: """Display help information""" help_text = """ Command List: - read [file] - Read memory, optionally save to file + r/read [file] - Read memory, optionally save to file Example: read 0x8000 256 dump.bin - write - Write memory, supports file or hex string + w/write - Write memory, supports file or hex string Example: write 0x8000 firmware.bin Example: write 0x8000 AABBCCDDEEFF - exec - Execute machine code at specified address + x/exec - Execute machine code at *fixed* address Example: exec 4F9D (CLR A; NOP;) - go - Jump to specified address for execution + g/go - Jump to specified address for execution Example: go 0x8000 info - Display MCU information - ls [path] - List directory contents, files show size, directories only names + ls [path] - List directory contents, files show size reload [file] - Reset MCU and upload boot2 program help - Display this help information - exit / quit - Exit interactive mode + exit/quit - Exit interactive mode """ print(help_text) @@ -991,6 +991,8 @@ Examples: help='Write memory: ADDR is start address, FILE/HEX is file or hex string') parser.add_argument('-g', '--go', metavar='ADDR', help='Jump to address for execution') + parser.add_argument('-x', '--exec', metavar='HEX', + help='Execute machine code') # Other options parser.add_argument('--list-ports', action='store_true', @@ -1104,6 +1106,23 @@ Examples: loader.close() return 1 + elif args.exec: + command_executed = True + try: + addr = 0 + hex_str = args.exec.replace('0x', '').replace(' ', '') + if len(hex_str) % 2 != 0: + raise ValueError("Hex string length must be even") + machine_code = bytes.fromhex(hex_str) + if len(machine_code) > MAX_DATA_SIZE: + raise ValueError("Machine code too long (max {MAX_DATA_SIZE} bytes)") + if loader.exec_machine_code(addr, machine_code): + print(f"[INFO] Exec {len(machine_code)} bytes") + except Exception as e: + print(f"[ERROR] Exec failed: {e}") + loader.close() + return 1 + # If no command specified or need to enter interactive mode if not command_executed or args.interactive: loader.interactive_mode()