summaryrefslogtreecommitdiff
path: root/plugins/svd/src/lib.rs
blob: 34bb0832c694cd7f986b47e3d65483f5b5916912 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
pub mod mapper;
pub mod settings;

use crate::mapper::DeviceMapper;
use crate::settings::LoadSettings;
use binaryninja::binary_view::{BinaryView, BinaryViewBase, BinaryViewExt};
use binaryninja::command::Command;
use binaryninja::logger::Logger;
use binaryninja::workflow::{Activity, AnalysisContext, Workflow};
use log::LevelFilter;

pub const LOADER_ACTIVITY_NAME: &str = "analysis.svd.loader";
const LOADER_ACTIVITY_CONFIG: &str = r#"{
    "name": "analysis.svd.loader",
    "title" : "SVD Loader",
    "description": "This analysis step applies SVD info to the view...",
    "eligibility": {
        "auto": {},
        "runOnce": true
    }
}"#;

struct LoadSVDFile;

impl Command for LoadSVDFile {
    fn action(&self, view: &BinaryView) {
        let Some(file) =
            binaryninja::interaction::get_open_filename_input("Select a .svd file", "*.svd")
        else {
            return;
        };

        let file_content = match std::fs::read_to_string(&file) {
            Ok(content) => content,
            Err(e) => {
                log::error!("Failed to read file: {}", e);
                return;
            }
        };

        match svd_parser::parse(&file_content) {
            Ok(device) => {
                // We have a supported svd device. map it!
                let load_settings = LoadSettings::from_view_settings(view);
                let address_size = view.address_size();
                let mapper = DeviceMapper::new(load_settings, address_size, device);
                mapper.map_to_view(view);
                view.update_analysis();
            }
            Err(e) => {
                log::error!("Failed to parse SVD file: {}", e);
            }
        }
    }

    fn valid(&self, _view: &BinaryView) -> bool {
        true
    }
}

#[no_mangle]
#[allow(non_snake_case)]
pub extern "C" fn CorePluginInit() -> bool {
    Logger::new("SVD").with_level(LevelFilter::Debug).init();

    binaryninja::command::register_command(
        "Load SVD File",
        "Loads an SVD file into the current view.",
        LoadSVDFile {},
    );

    // Register settings globally.
    LoadSettings::register();

    let loader_activity = |ctx: &AnalysisContext| {
        let view = ctx.view();
        let load_settings = LoadSettings::from_view_settings(&view);
        let Some(file) = &load_settings.auto_load_file else {
            log::debug!("No SVD file specified, skipping...");
            return;
        };
        let file_content = match std::fs::read_to_string(file) {
            Ok(content) => content,
            Err(e) => {
                log::error!("Failed to read file: {}", e);
                return;
            }
        };
        match svd_parser::parse(&file_content) {
            Ok(device) => {
                let address_size = view.address_size();
                let mapper = DeviceMapper::new(load_settings, address_size, device);
                mapper.map_to_view(&view);
            }
            Err(e) => {
                log::error!("Failed to parse SVD file: {}", e);
            }
        }
    };

    // Register new workflow activity to load svd information.
    let old_module_meta_workflow = Workflow::instance("core.module.metaAnalysis");
    let module_meta_workflow = old_module_meta_workflow.clone("core.module.metaAnalysis");
    let loader_activity = Activity::new_with_action(LOADER_ACTIVITY_CONFIG, loader_activity);
    module_meta_workflow
        .register_activity(&loader_activity)
        .unwrap();
    module_meta_workflow.insert("core.module.loadDebugInfo", [LOADER_ACTIVITY_NAME]);
    module_meta_workflow.register().unwrap();

    true
}