#!/usr/bin/env python3 """ Analyze GSP firmware .fwimage binary in Ghidra. Usage: python3 analyze_fwimage.py """ import sys import os import subprocess def analyze_fwimage(fwimage_path, project_dir, project_name): """Analyze the .fwimage binary in Ghidra.""" ghidra_path = "/snap/bin/ghidra" analyze_headless = "/snap/ghidra/47/ghidra/support/analyzeHeadless" if not os.path.exists(analyze_headless): analyze_headless = "/opt/ghidra/support/analyzeHeadless" if not os.path.exists(analyze_headless): print("ERROR: Ghidra analyzeHeadless not found") return False # Create project directory os.makedirs(project_dir, exist_ok=True) # Import the .fwimage binary cmd = [ analyze_headless, project_dir, project_name, "-import", fwimage_path, "-processor", "RISCV:LE:64:default", "-cspec", "gcc", "-overwrite", "-scriptPath", os.path.join(project_dir, "scripts"), "-postScript", "analyze_gsp_fwimage.py" ] print(f"Running: {' '.join(cmd)}") result = subprocess.run(cmd, capture_output=True, text=True, timeout=3600) print(result.stdout) if result.stderr: print(result.stderr, file=sys.stderr) return result.returncode == 0 def main(): if len(sys.argv) < 4: print("Usage: analyze_fwimage.py ") sys.exit(1) fwimage_path = sys.argv[1] project_dir = sys.argv[2] project_name = sys.argv[3] if not os.path.exists(fwimage_path): print(f"ERROR: Firmware image not found: {fwimage_path}") sys.exit(1) success = analyze_fwimage(fwimage_path, project_dir, project_name) sys.exit(0 if success else 1) if __name__ == "__main__": main()