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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
use std::collections::{BTreeMap, HashMap};
use std::path::PathBuf;
use std::str::FromStr;
use crate::utils::prelude::*;
use failure::{format_err, Error};
use minidom::Element;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Core {
CortexM0,
CortexM0Plus,
CortexM1,
CortexM3,
CortexM4,
CortexM7,
CortexM23,
CortexM33,
SC000,
SC300,
ARMV8MBL,
ARMV8MML,
CortexR4,
CortexR5,
CortexR7,
CortexR8,
CortexA5,
CortexA7,
CortexA8,
CortexA9,
CortexA15,
CortexA17,
CortexA32,
CortexA35,
CortexA53,
CortexA57,
CortexA72,
CortexA73,
}
impl FromStr for Core {
type Err = Error;
fn from_str(from: &str) -> Result<Self, Error> {
match from {
"Cortex-M0" => Ok(Core::CortexM0),
"Cortex-M0+" => Ok(Core::CortexM0Plus),
"Cortex-M1" => Ok(Core::CortexM1),
"Cortex-M3" => Ok(Core::CortexM3),
"Cortex-M4" => Ok(Core::CortexM4),
"Cortex-M7" => Ok(Core::CortexM7),
"Cortex-M23" => Ok(Core::CortexM23),
"Cortex-M33" => Ok(Core::CortexM33),
"SC000" => Ok(Core::SC000),
"SC300" => Ok(Core::SC300),
"ARMV8MBL" => Ok(Core::ARMV8MBL),
"ARMV8MML" => Ok(Core::ARMV8MML),
"Cortex-R4" => Ok(Core::CortexR4),
"Cortex-R5" => Ok(Core::CortexR5),
"Cortex-R7" => Ok(Core::CortexR7),
"Cortex-R8" => Ok(Core::CortexR8),
"Cortex-A5" => Ok(Core::CortexA5),
"Cortex-A7" => Ok(Core::CortexA7),
"Cortex-A8" => Ok(Core::CortexA8),
"Cortex-A9" => Ok(Core::CortexA9),
"Cortex-A15" => Ok(Core::CortexA15),
"Cortex-A17" => Ok(Core::CortexA17),
"Cortex-A32" => Ok(Core::CortexA32),
"Cortex-A35" => Ok(Core::CortexA35),
"Cortex-A53" => Ok(Core::CortexA53),
"Cortex-A57" => Ok(Core::CortexA57),
"Cortex-A72" => Ok(Core::CortexA72),
"Cortex-A73" => Ok(Core::CortexA73),
unknown => Err(format_err!("Unknown core {}", unknown)),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FPU {
None,
SinglePrecision,
DoublePrecision,
}
impl FromStr for FPU {
type Err = Error;
fn from_str(from: &str) -> Result<Self, Error> {
match from {
"FPU" => Ok(FPU::SinglePrecision),
"SP_FPU" => Ok(FPU::SinglePrecision),
"1" => Ok(FPU::SinglePrecision),
"None" => Ok(FPU::None),
"0" => Ok(FPU::None),
"DP_FPU" => Ok(FPU::DoublePrecision),
"2" => Ok(FPU::DoublePrecision),
unknown => Err(format_err!("Unknown fpu {}", unknown)),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MPU {
NotPresent,
Present,
}
impl FromStr for MPU {
type Err = Error;
fn from_str(from: &str) -> Result<Self, Error> {
match from {
"MPU" => Ok(MPU::Present),
"1" => Ok(MPU::Present),
"None" => Ok(MPU::NotPresent),
"0" => Ok(MPU::NotPresent),
unknown => Err(format_err!("Unknown fpu {}", unknown)),
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Processor {
units: u8,
pub core: Core,
fpu: FPU,
mpu: MPU,
}
#[derive(Debug, Clone)]
struct ProcessorBuilder {
core: Option<Core>,
units: Option<u8>,
fpu: Option<FPU>,
mpu: Option<MPU>,
}
impl ProcessorBuilder {
fn merge(self, parent: &Self) -> Self {
ProcessorBuilder {
core: self.core.or_else(|| parent.core.clone()),
units: self.units.or_else(|| parent.units),
fpu: self.fpu.or_else(|| parent.fpu.clone()),
mpu: self.mpu.or_else(|| parent.mpu.clone()),
}
}
fn build(self) -> Result<Processor, Error> {
Ok(Processor {
core: self.core.ok_or_else(|| format_err!("No Core found!"))?,
units: self.units.unwrap_or(1u8),
fpu: self.fpu.unwrap_or(FPU::None),
mpu: self.mpu.unwrap_or(MPU::NotPresent),
})
}
}
impl FromElem for ProcessorBuilder {
fn from_elem(e: &Element) -> Result<Self, Error> {
Ok(ProcessorBuilder {
core: attr_parse(e, "Dcore", "processor").ok(),
units: attr_parse(e, "Punits", "processor").ok(),
fpu: attr_parse(e, "Dfpu", "processor").ok(),
mpu: attr_parse(e, "Dmpu", "processor").ok(),
})
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum Processors {
Symmetric(Processor),
Asymmetric(BTreeMap<String, Processor>),
}
#[derive(Debug, Clone)]
enum ProcessorsBuilder {
Symmetric(ProcessorBuilder),
Asymmetric(BTreeMap<String, ProcessorBuilder>),
}
impl ProcessorsBuilder {
fn merge(self, parent: &Option<Self>) -> Result<Self, Error> {
match self {
ProcessorsBuilder::Symmetric(me) => match parent {
Some(ProcessorsBuilder::Symmetric(ref single_core)) => {
Ok(ProcessorsBuilder::Symmetric(me.merge(single_core)))
}
Some(ProcessorsBuilder::Asymmetric(_)) => Err(format_err!(
"Tried to merge symmetric and asymmetric processors"
)),
None => Ok(ProcessorsBuilder::Symmetric(me)),
},
ProcessorsBuilder::Asymmetric(mut me) => match parent {
Some(ProcessorsBuilder::Symmetric(_)) => Err(format_err!(
"Tried to merge asymmetric and symmetric processors"
)),
Some(ProcessorsBuilder::Asymmetric(ref par_map)) => {
me.extend(par_map.iter().map(|(k, v)| (k.clone(), v.clone())));
Ok(ProcessorsBuilder::Asymmetric(me))
}
None => Ok(ProcessorsBuilder::Asymmetric(me)),
},
}
}
fn merge_into(&mut self, other: Self) {
match self {
ProcessorsBuilder::Symmetric(_) => (),
ProcessorsBuilder::Asymmetric(ref mut me) => match other {
ProcessorsBuilder::Symmetric(_) => (),
ProcessorsBuilder::Asymmetric(more) => me.extend(more.into_iter()),
},
}
}
fn build(self) -> Result<Processors, Error> {
match self {
ProcessorsBuilder::Symmetric(prc) => prc.build().map(Processors::Symmetric),
ProcessorsBuilder::Asymmetric(map) => {
let new_map: Result<BTreeMap<String, Processor>, Error> = map
.into_iter()
.map(|(name, prc)| match prc.build() {
Ok(new_prc) => Ok((name, new_prc)),
Err(e) => Err(e),
})
.collect();
Ok(Processors::Asymmetric(new_map?))
}
}
}
}
impl FromElem for ProcessorsBuilder {
fn from_elem(e: &Element) -> Result<Self, Error> {
Ok(match e.attr("Pname") {
Some(name) => ProcessorsBuilder::Asymmetric(
Some((name.to_string(), ProcessorBuilder::from_elem(e)?))
.into_iter()
.collect(),
),
None => ProcessorsBuilder::Symmetric(ProcessorBuilder::from_elem(e)?),
})
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryPermissions {
pub read: bool,
pub write: bool,
pub execute: bool,
pub peripheral: bool,
pub secure: bool,
pub non_secure: bool,
pub non_secure_callable: bool,
}
impl MemoryPermissions {
fn from_str(input: &str) -> Self {
let mut ret = MemoryPermissions {
read: false,
write: false,
execute: false,
peripheral: false,
secure: false,
non_secure: false,
non_secure_callable: false,
};
for c in input.chars() {
match c {
'r' => ret.read = true,
'w' => ret.write = true,
'x' => ret.execute = true,
'p' => ret.peripheral = true,
's' => ret.secure = true,
'n' => ret.non_secure = true,
'c' => ret.non_secure_callable = true,
_ => (),
}
}
ret
}
}
enum NumberBool {
False,
True,
}
impl Into<bool> for NumberBool {
fn into(self) -> bool {
match self {
NumberBool::True => true,
NumberBool::False => false,
}
}
}
impl FromStr for NumberBool {
type Err = Error;
fn from_str(from: &str) -> Result<Self, Error> {
match from {
"true" => Ok(NumberBool::True),
"1" => Ok(NumberBool::True),
"false" => Ok(NumberBool::False),
"0" => Ok(NumberBool::False),
unknown => Err(format_err!(
"unkown boolean found in merory startup {}",
unknown
)),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Memory {
pub access: MemoryPermissions,
pub start: u64,
pub size: u64,
pub startup: bool,
pub default: bool,
}
struct MemElem(String, Memory);
impl FromElem for MemElem {
fn from_elem(e: &Element) -> Result<Self, Error> {
let access = MemoryPermissions::from_str(e.attr("access").unwrap_or_else(|| {
let memtype = e.attr("id").unwrap_or_default();
if memtype.contains("ROM") {
"rx"
} else if memtype.contains("RAM") {
"rw"
} else {
""
}
}));
let name = e
.attr("id")
.or_else(|| e.attr("name"))
.map(|s| s.to_string())
.ok_or_else(|| format_err!("No name found for memory"))?;
let start = attr_parse_hex(e, "start", "memory")?;
let size = attr_parse_hex(e, "size", "memory")?;
let startup = attr_parse(e, "startup", "memory")
.map(|nb: NumberBool| nb.into())
.unwrap_or_default();
let default = attr_parse(e, "default", "memory")
.map(|nb: NumberBool| nb.into())
.unwrap_or_default();
Ok(MemElem(
name,
Memory {
access,
start,
size,
startup,
default,
},
))
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Memories(pub HashMap<String, Memory>);
fn merge_memories(lhs: Memories, rhs: &Memories) -> Memories {
let rhs: Vec<_> = rhs
.0
.iter()
.filter_map(|(k, v)| {
if lhs.0.contains_key(k) {
None
} else {
Some((k.clone(), v.clone()))
}
})
.collect();
let mut lhs = lhs;
lhs.0.extend(rhs);
lhs
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Algorithm {
pub file_name: PathBuf,
pub start: u64,
pub size: u64,
pub default: bool,
pub ram_start: Option<u64>,
pub ram_size: Option<u64>,
}
impl FromElem for Algorithm {
fn from_elem(e: &Element) -> Result<Self, Error> {
let default = attr_parse(e, "default", "memory")
.map(|nb: NumberBool| nb.into())
.unwrap_or_default();
let file_name: &str = attr_map(e, "name", "algorithm")?;
Ok(Self {
file_name: file_name.replace("\\", "/").into(),
start: attr_parse_hex(e, "start", "algorithm")?,
size: attr_parse_hex(e, "size", "algorithm")?,
ram_start: attr_parse_hex(e, "RAMstart", "algorithm").ok(),
ram_size: attr_parse_hex(e, "RAMsize", "algorithm").ok(),
default,
})
}
}
#[derive(Debug)]
struct DeviceBuilder<'dom> {
name: Option<&'dom str>,
algorithms: Vec<Algorithm>,
memories: Memories,
processor: Option<ProcessorsBuilder>,
vendor: Option<&'dom str>,
family: Option<&'dom str>,
sub_family: Option<&'dom str>,
}
#[derive(Debug, Serialize)]
pub struct Device {
pub name: String,
pub memories: Memories,
pub algorithms: Vec<Algorithm>,
pub processor: Processors,
pub vendor: Option<String>,
pub family: String,
pub sub_family: Option<String>,
}
impl<'dom> DeviceBuilder<'dom> {
fn from_elem(e: &'dom Element) -> Self {
let memories = Memories(HashMap::new());
let mut family = None;
let mut sub_family = None;
if e.name() == "family" {
family = e.attr("Dfamily");
}
if e.name() == "subFamily" {
sub_family = e.attr("DsubFamily");
}
DeviceBuilder {
name: e.attr("Dname").or_else(|| e.attr("Dvariant")),
vendor: e.attr("Dvendor"),
memories,
algorithms: Vec::new(),
processor: None,
family,
sub_family,
}
}
fn build(self) -> Result<Device, Error> {
let name = self
.name
.map(|s| s.into())
.ok_or_else(|| format_err!("Device found without a name"))?;
let family = self
.family
.map(|s| s.into())
.ok_or_else(|| format_err!("Device found without a family"))?;
Ok(Device {
processor: match self.processor {
Some(pb) => pb.build()?,
None => return Err(format_err!("Device found without a processor {}", name)),
},
name,
memories: self.memories,
algorithms: self.algorithms,
vendor: self.vendor.map(str::to_string),
family,
sub_family: self.sub_family.map(str::to_string),
})
}
fn add_parent(mut self, parent: &Self) -> Result<Self, Error> {
self.algorithms.extend_from_slice(&parent.algorithms);
Ok(Self {
name: self.name.or(parent.name),
algorithms: self.algorithms,
memories: merge_memories(self.memories, &parent.memories),
processor: match self.processor {
Some(old_proc) => Some(old_proc.merge(&parent.processor)?),
None => parent.processor.clone(),
},
vendor: self.vendor.or(parent.vendor),
family: self.family.or(parent.family),
sub_family: self.sub_family.or(parent.sub_family),
})
}
fn add_processor(&mut self, processor: ProcessorsBuilder) -> &mut Self {
match self.processor {
None => self.processor = Some(processor),
Some(ref mut origin) => origin.merge_into(processor),
};
self
}
fn add_memory(&mut self, MemElem(name, mem): MemElem) -> &mut Self {
self.memories.0.insert(name, mem);
self
}
fn add_algorithm(&mut self, alg: Algorithm) -> &mut Self {
self.algorithms.push(alg);
self
}
}
fn parse_device<'dom>(e: &'dom Element) -> Vec<DeviceBuilder<'dom>> {
let mut device = DeviceBuilder::from_elem(e);
let variants = e
.children()
.filter_map(|child| match child.name() {
"variant" => Some(DeviceBuilder::from_elem(child)),
"memory" => {
FromElem::from_elem(child)
.ok_warn()
.map(|mem| device.add_memory(mem));
None
}
"algorithm" => {
FromElem::from_elem(child)
.ok_warn()
.map(|alg| device.add_algorithm(alg));
None
}
"processor" => {
FromElem::from_elem(child)
.ok_warn()
.map(|prc| device.add_processor(prc));
None
}
_ => None,
})
.collect::<Vec<_>>();
if variants.is_empty() {
vec![device]
} else {
variants
.into_iter()
.flat_map(|bld| bld.add_parent(&device).ok_warn())
.collect()
}
}
fn parse_sub_family<'dom>(e: &'dom Element) -> Vec<DeviceBuilder<'dom>> {
let mut sub_family_device = DeviceBuilder::from_elem(e);
let devices = e
.children()
.flat_map(|child| match child.name() {
"device" => parse_device(child),
"memory" => {
FromElem::from_elem(child)
.ok_warn()
.map(|mem| sub_family_device.add_memory(mem));
Vec::new()
}
"algorithm" => {
FromElem::from_elem(child)
.ok_warn()
.map(|alg| sub_family_device.add_algorithm(alg));
Vec::new()
}
"processor" => {
FromElem::from_elem(child)
.ok_warn()
.map(|prc| sub_family_device.add_processor(prc));
Vec::new()
}
_ => Vec::new(),
})
.collect::<Vec<_>>();
devices
.into_iter()
.flat_map(|bldr| bldr.add_parent(&sub_family_device).ok_warn())
.collect()
}
fn parse_family(e: &Element) -> Result<Vec<Device>, Error> {
let mut family_device = DeviceBuilder::from_elem(e);
let all_devices = e
.children()
.flat_map(|child| match child.name() {
"subFamily" => parse_sub_family(child),
"device" => parse_device(child),
"memory" => {
FromElem::from_elem(child)
.ok_warn()
.map(|mem| family_device.add_memory(mem));
Vec::new()
}
"algorithm" => {
FromElem::from_elem(child)
.ok_warn()
.map(|alg| family_device.add_algorithm(alg));
Vec::new()
}
"processor" => {
FromElem::from_elem(child)
.ok_warn()
.map(|prc| family_device.add_processor(prc));
Vec::new()
}
_ => Vec::new(),
})
.collect::<Vec<_>>();
all_devices
.into_iter()
.map(|bldr| bldr.add_parent(&family_device).and_then(|dev| dev.build()))
.collect()
}
#[derive(Default, Serialize)]
pub struct Devices(pub HashMap<String, Device>);
impl FromElem for Devices {
fn from_elem(e: &Element) -> Result<Self, Error> {
e.children()
.fold(Ok(HashMap::new()), |res, c| match (res, parse_family(c)) {
(Ok(mut devs), Ok(add_this)) => {
devs.extend(add_this.into_iter().map(|dev| (dev.name.clone(), dev)));
Ok(devs)
}
(Ok(_), Err(e)) => Err(e),
(Err(e), Ok(_)) => Err(e),
(Err(e), Err(_)) => Err(e),
})
.map(Devices)
}
}