summaryrefslogtreecommitdiffstats
path: root/tools/ghidra/analyze_fwimage.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/ghidra/analyze_fwimage.py')
-rw-r--r--tools/ghidra/analyze_fwimage.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/tools/ghidra/analyze_fwimage.py b/tools/ghidra/analyze_fwimage.py
new file mode 100644
index 0000000..5d98504
--- /dev/null
+++ b/tools/ghidra/analyze_fwimage.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python3
+"""
+Analyze GSP firmware .fwimage binary in Ghidra.
+Usage: python3 analyze_fwimage.py <fwimage_bin> <project_dir> <project_name>
+"""
+
+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 <fwimage_bin> <project_dir> <project_name>")
+ 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() \ No newline at end of file