1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2026 Open-IOV contributors
"""
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()
|