summaryrefslogtreecommitdiffstats
path: root/tools/ghidra/create_gsp_project.py
blob: 2d9a5887392315aea7d5068e66c65b70ad1715d1 (plain)
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
69
#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2026 Open-IOV contributors
"""
Create Ghidra project for GSP firmware analysis.
Usage: python3 create_gsp_project.py <firmware_bin> <project_dir> <project_name>
"""

import sys
import os
import subprocess
import shutil

def create_ghidra_project(firmware_path, project_dir, project_name):
    """Create a Ghidra project and import the GSP firmware."""
    
    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")
        sys.exit(1)
    
    # Create project directory
    os.makedirs(project_dir, exist_ok=True)
    
    # Import the firmware binary
    cmd = [
        analyze_headless,
        project_dir,
        project_name,
        "-import", firmware_path,
        "-processor", "RISCV:LE:64:default",
        "-cspec", "gcc",
        "-overwrite",
        "-scriptPath", os.path.join(project_dir, "scripts"),
        "-postScript", "extract_fwimage.py"
    ]
    
    print(f"Running: {' '.join(cmd)}")
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    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: create_gsp_project.py <firmware_bin> <project_dir> <project_name>")
        sys.exit(1)
    
    firmware_path = sys.argv[1]
    project_dir = sys.argv[2]
    project_name = sys.argv[3]
    
    if not os.path.exists(firmware_path):
        print(f"ERROR: Firmware not found: {firmware_path}")
        sys.exit(1)
    
    success = create_ghidra_project(firmware_path, project_dir, project_name)
    sys.exit(0 if success else 1)

if __name__ == "__main__":
    main()