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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
pub mod mapper;
pub mod settings;
use crate::mapper::DeviceMapper;
use crate::settings::LoadSettings;
use binaryninja::binary_view::{BinaryView, BinaryViewBase};
use binaryninja::command::Command;
use binaryninja::interaction::{Form, FormInputField};
use binaryninja::workflow::{activity, Activity, AnalysisContext, Workflow};
use std::path::PathBuf;
use svd_parser::ValidateLevel;
pub struct LoadFileField;
impl LoadFileField {
pub fn field() -> FormInputField {
FormInputField::OpenFileName {
prompt: "File Path".to_string(),
// TODO: This is called extension but is really a filter.
extension: Some("*.svd".to_string()),
default: None,
value: None,
}
}
pub fn from_form(form: &Form) -> Option<PathBuf> {
let field = form.get_field_with_name("File Path")?;
let field_value = field.try_value_string()?;
Some(PathBuf::from(field_value))
}
}
pub struct AddCommentsField;
impl AddCommentsField {
pub fn field(default: bool) -> FormInputField {
FormInputField::Checkbox {
prompt: "Add Comments".to_string(),
default: Some(default),
value: false,
}
}
pub fn from_form(form: &Form) -> Option<bool> {
let field = form.get_field_with_name("Add Comments")?;
let field_value = field.try_value_int()?;
match field_value {
1 => Some(true),
_ => Some(false),
}
}
}
pub struct AddMemoryRegionsField;
impl AddMemoryRegionsField {
pub fn field(default: bool) -> FormInputField {
FormInputField::Checkbox {
prompt: "Add Memory Regions".to_string(),
default: Some(default),
value: false,
}
}
pub fn from_form(form: &Form) -> Option<bool> {
let field = form.get_field_with_name("Add Memory Regions")?;
let field_value = field.try_value_int()?;
match field_value {
1 => Some(true),
_ => Some(false),
}
}
}
struct LoadSVDFile;
impl Command for LoadSVDFile {
fn action(&self, view: &BinaryView) {
let mut form = Form::new("Load SVD File");
let mut load_settings = LoadSettings::from_view_settings(view);
form.add_field(LoadFileField::field());
form.add_field(AddCommentsField::field(load_settings.add_comments));
form.add_field(AddMemoryRegionsField::field(
load_settings.add_backing_regions,
));
if !form.prompt() {
return;
}
let Some(file_path) = LoadFileField::from_form(&form) else {
return;
};
load_settings.add_comments = AddCommentsField::from_form(&form).unwrap_or(true);
load_settings.add_backing_regions = AddMemoryRegionsField::from_form(&form).unwrap_or(true);
let file_content = match std::fs::read_to_string(&file_path) {
Ok(content) => content,
Err(e) => {
tracing::error!("Failed to read file: {:?}", e);
return;
}
};
// Disabling validation since vendors are lazy and don't follow the spec.
let mut config = svd_parser::Config::default();
config.validate_level = ValidateLevel::Disabled;
match svd_parser::parse_with_config(&file_content, &config) {
Ok(device) => {
// We have a supported svd device. map it!
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) => {
tracing::error!("Failed to parse SVD file: {:?}", e);
}
}
}
fn valid(&self, _view: &BinaryView) -> bool {
true
}
}
#[no_mangle]
#[allow(non_snake_case)]
#[cfg(not(feature = "demo"))]
pub extern "C" fn CorePluginInit() -> bool {
if plugin_init().is_err() {
tracing::error!("Failed to initialize SVD plug-in");
return false;
}
true
}
#[no_mangle]
#[allow(non_snake_case)]
#[cfg(feature = "demo")]
pub extern "C" fn SVDPluginInit() -> bool {
if plugin_init().is_err() {
tracing::error!("Failed to initialize SVD plug-in");
return false;
}
true
}
fn plugin_init() -> Result<(), ()> {
binaryninja::tracing_init!("SVD");
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 {
tracing::debug!("No SVD file specified, skipping...");
return;
};
let file_content = match std::fs::read_to_string(file) {
Ok(content) => content,
Err(e) => {
tracing::error!("Failed to read file: {}", e);
return;
}
};
let mut config = svd_parser::Config::default();
config.validate_level = ValidateLevel::Disabled;
match svd_parser::parse_with_config(&file_content, &config) {
Ok(device) => {
let address_size = view.address_size();
let mapper = DeviceMapper::new(load_settings, address_size, device);
mapper.map_to_view(&view);
}
Err(e) => {
tracing::error!("Failed to parse SVD file: {:?}", e);
}
}
};
// Register new workflow activity to load svd information.
let loader_config = activity::Config::action(
"analysis.svd.loader",
"SVD Loader",
"This analysis step applies SVD info to the view...",
)
.eligibility(activity::Eligibility::auto().run_once(true));
let loader_activity = Activity::new_with_action(loader_config, loader_activity);
Workflow::cloned("core.module.metaAnalysis")
.ok_or(())?
.activity_before(&loader_activity, "core.module.loadDebugInfo")?
.register()?;
Ok(())
}
|