/
flash_algorithm.rs
541 lines (464 loc) · 18.2 KB
/
flash_algorithm.rs
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
use super::FlashError;
use crate::{architecture::riscv, core::Architecture, Target};
use probe_rs_target::{
FlashProperties, PageInfo, RamRegion, RawFlashAlgorithm, SectorInfo, TransferEncoding,
};
use std::mem::size_of_val;
/// A flash algorithm, which has been assembled for a specific
/// chip.
///
/// To create a [FlashAlgorithm], call the [`assemble_from_raw`] function.
///
/// [`assemble_from_raw`]: FlashAlgorithm::assemble_from_raw
#[derive(Debug, Default, Clone)]
pub struct FlashAlgorithm {
/// The name of the flash algorithm.
pub name: String,
/// Whether this flash algorithm is the default one or not.
pub default: bool,
/// Memory address where the flash algo instructions will be loaded to.
pub load_address: u64,
/// List of 32-bit words containing the position-independent code for the algo.
pub instructions: Vec<u32>,
/// Address of the `Init()` entry point. Optional.
pub pc_init: Option<u64>,
/// Address of the `UnInit()` entry point. Optional.
pub pc_uninit: Option<u64>,
/// Address of the `ProgramPage()` entry point.
pub pc_program_page: u64,
/// Address of the `EraseSector()` entry point.
pub pc_erase_sector: u64,
/// Address of the `EraseAll()` entry point. Optional.
pub pc_erase_all: Option<u64>,
/// Initial value of the R9 register for calling flash algo entry points, which
/// determines where the position-independent data resides.
pub static_base: u64,
/// Initial value of the stack pointer when calling any flash algo API.
pub begin_stack: u64,
/// A list of base addresses for page buffers. The buffers must be at
/// least as large as the region's `page_size` attribute. If at least 2 buffers are included in
/// the list, then double buffered programming will be enabled.
pub page_buffers: Vec<u64>,
/// Location of optional RTT control block.
///
/// If this is present, the flash algorithm supports debug output over RTT.
pub rtt_control_block: Option<u64>,
/// The properties of the flash on the device.
pub flash_properties: FlashProperties,
/// The encoding format accepted by the flash algorithm.
pub transfer_encoding: TransferEncoding,
}
impl FlashAlgorithm {
/// Try to retrieve the information about the flash sector
/// which contains `address`.
///
/// If the `address` is not part of the flash, None will
/// be returned.
pub fn sector_info(&self, address: u64) -> Option<SectorInfo> {
if !self.flash_properties.address_range.contains(&address) {
tracing::trace!("Address {:08x} not contained in this flash device", address);
return None;
}
let offset_address = address - self.flash_properties.address_range.start;
let containing_sector = self
.flash_properties
.sectors
.iter()
.rfind(|s| s.address <= offset_address)?;
let sector_index = (offset_address - containing_sector.address) / containing_sector.size;
let sector_address = self.flash_properties.address_range.start
+ containing_sector.address
+ sector_index * containing_sector.size;
Some(SectorInfo {
base_address: sector_address,
size: containing_sector.size,
})
}
/// Returns the necessary information about the page which `address` resides in
/// if the address is inside the flash region.
pub fn page_info(&self, address: u64) -> Option<PageInfo> {
if !self.flash_properties.address_range.contains(&address) {
return None;
}
Some(PageInfo {
base_address: address - (address % self.flash_properties.page_size as u64),
size: self.flash_properties.page_size,
})
}
/// Iterate over all the sectors of the flash.
pub fn iter_sectors(&self) -> impl Iterator<Item = SectorInfo> + '_ {
let props = &self.flash_properties;
assert!(!props.sectors.is_empty());
assert!(props.sectors[0].address == 0);
let mut addr = props.address_range.start;
let mut desc_idx = 0;
std::iter::from_fn(move || {
if addr >= props.address_range.end {
return None;
}
// Advance desc_idx if needed
if let Some(next_desc) = props.sectors.get(desc_idx + 1) {
if props.address_range.start + next_desc.address <= addr {
desc_idx += 1;
}
}
let size = props.sectors[desc_idx].size;
let sector = SectorInfo {
base_address: addr,
size,
};
addr += size;
Some(sector)
})
}
/// Iterate over all the pages of the flash.
pub fn iter_pages(&self) -> impl Iterator<Item = PageInfo> + '_ {
let props = &self.flash_properties;
let mut addr = props.address_range.start;
std::iter::from_fn(move || {
if addr >= props.address_range.end {
return None;
}
let page = PageInfo {
base_address: addr,
size: props.page_size,
};
addr += props.page_size as u64;
Some(page)
})
}
/// Returns true if the entire contents of the argument array equal the erased byte value.
pub fn is_erased(&self, data: &[u8]) -> bool {
for b in data {
if *b != self.flash_properties.erased_byte_value {
return false;
}
}
true
}
const FLASH_ALGO_STACK_SIZE: u64 = 512;
// Header for RISC-V Flash Algorithms
const RISCV_FLASH_BLOB_HEADER: [u32; 2] = [riscv::assembly::EBREAK, riscv::assembly::EBREAK];
const ARM_FLASH_BLOB_HEADER: [u32; 8] = [
0xE00A_BE00,
0x062D_780D,
0x2408_4068,
0xD300_0040,
0x1E64_4058,
0x1C49_D1FA,
0x2A00_1E52,
0x0477_0D1F,
];
const XTENSA_FLASH_BLOB_HEADER: [u32; 0] = [];
/// When the target architecture is not known, and we need to allocate space for the header,
/// this function returns the maximum size of the header of supported architectures.
pub fn get_max_algorithm_header_size() -> u64 {
let algos = [
Self::algorithm_header(Architecture::Arm),
Self::algorithm_header(Architecture::Riscv),
Self::algorithm_header(Architecture::Xtensa),
];
algos.iter().copied().map(size_of_val).max().unwrap() as u64
}
fn algorithm_header(architecture: Architecture) -> &'static [u32] {
match architecture {
Architecture::Arm => &Self::ARM_FLASH_BLOB_HEADER,
Architecture::Riscv => &Self::RISCV_FLASH_BLOB_HEADER,
Architecture::Xtensa => &Self::XTENSA_FLASH_BLOB_HEADER,
}
}
/// Constructs a complete flash algorithm, tailored to the flash and RAM sizes given.
pub fn assemble_from_raw(
raw: &RawFlashAlgorithm,
ram_region: &RamRegion,
target: &Target,
) -> Result<Self, FlashError> {
Self::assemble_from_raw_with_data(raw, ram_region, ram_region, target)
}
/// Constructs a complete flash algorithm, tailored to the flash and RAM sizes given.
pub fn assemble_from_raw_with_data(
raw: &RawFlashAlgorithm,
ram_region: &RamRegion,
data_ram_region: &RamRegion,
target: &Target,
) -> Result<Self, FlashError> {
use std::mem::size_of;
if raw.flash_properties.page_size % 4 != 0 {
// TODO move to yaml validation
return Err(FlashError::InvalidPageSize {
size: raw.flash_properties.page_size,
});
}
let assembled_instructions = raw.instructions.chunks_exact(size_of::<u32>());
let remainder = assembled_instructions.remainder();
let last_elem = if !remainder.is_empty() {
let word = u32::from_le_bytes(
remainder
.iter()
.cloned()
// Pad with up to three bytes
.chain([0u8, 0u8, 0u8])
.take(4)
.collect::<Vec<u8>>()
.try_into()
.unwrap(),
);
Some(word)
} else {
None
};
let header = Self::algorithm_header(target.architecture());
let instructions: Vec<u32> = header
.iter()
.copied()
.chain(
assembled_instructions.map(|bytes| u32::from_le_bytes(bytes.try_into().unwrap())),
)
.chain(last_elem)
.collect();
let header_size = size_of_val(header) as u64;
// The start address where we try to load the flash algorithm.
let addr_load = match raw.load_address {
Some(address) => {
// adjust the raw load address to account for the algo header
address
.checked_sub(header_size)
.ok_or(FlashError::InvalidFlashAlgorithmLoadAddress { address })?
}
None => {
// assume position independent code
ram_region.range.start
}
};
if addr_load < ram_region.range.start {
return Err(FlashError::InvalidFlashAlgorithmLoadAddress { address: addr_load });
}
let code_start = addr_load + header_size;
let code_size_bytes = (instructions.len() * size_of::<u32>()) as u64;
let code_end = code_start + code_size_bytes;
let buffer_page_size = raw.flash_properties.page_size as u64;
let remaining_ram = ram_region.range.end - code_end;
let buffer_page_size_in_instr_region = if ram_region == data_ram_region {
buffer_page_size
} else {
0
};
// Try to find a stack size that fits with at least one page of data.
let stack_size = if let Some(configured_stack) = raw.stack_size {
let stack_size = configured_stack as u64;
// Make sure at least one data page fits into RAM.
if buffer_page_size_in_instr_region + stack_size > remaining_ram {
// The configured stack size is too large. Let's not try to be too clever about it.
return Err(FlashError::InvalidFlashAlgorithmStackSize);
}
stack_size
} else {
// Make sure at least one data page fits into RAM, and also
// avoid a panic if the RAM region is too small.
if buffer_page_size_in_instr_region >= remaining_ram {
// We don't have any space for a stack
return Err(FlashError::InvalidFlashAlgorithmStackSize);
}
// Use up to 512 bytes of RAM out of the remaining for stack.
(remaining_ram - buffer_page_size_in_instr_region).min(Self::FLASH_ALGO_STACK_SIZE)
};
let stack_top_addr = code_end + stack_size;
tracing::debug!("The flash algorithm will be configured with {stack_size} bytes of stack below {stack_top_addr:08x}");
// Determine the bounds of the data region.
let data_start_addr = if let Some(data_load_addr) = raw.data_load_address {
// Specified, use what the user gave us
data_load_addr
} else if ram_region == data_ram_region {
// Not specified, same region, place above stack
stack_top_addr
} else {
// Not specified, different region, place at start of data RAM region
data_ram_region.range.start
};
let data_region_end_addr = data_ram_region.range.end;
// Data buffer 1
let first_buffer_start = data_start_addr;
// Data buffer 2
let second_buffer_start = first_buffer_start + buffer_page_size;
let second_buffer_end = second_buffer_start + buffer_page_size;
// Determine whether we can use double buffering or not by the remaining RAM region size.
let page_buffers = if second_buffer_end <= data_region_end_addr {
vec![first_buffer_start, second_buffer_start]
} else {
vec![first_buffer_start]
};
tracing::debug!("Page buffers: {:08x?}", page_buffers);
let name = raw.name.clone();
Ok(FlashAlgorithm {
name,
default: raw.default,
load_address: addr_load,
instructions,
pc_init: raw.pc_init.map(|v| code_start + v),
pc_uninit: raw.pc_uninit.map(|v| code_start + v),
pc_program_page: code_start + raw.pc_program_page,
pc_erase_sector: code_start + raw.pc_erase_sector,
pc_erase_all: raw.pc_erase_all.map(|v| code_start + v),
static_base: code_start + raw.data_section_offset,
begin_stack: stack_top_addr,
page_buffers,
rtt_control_block: raw.rtt_location,
flash_properties: raw.flash_properties.clone(),
transfer_encoding: raw.transfer_encoding.unwrap_or_default(),
})
}
}
#[cfg(test)]
mod test {
use probe_rs_target::{FlashProperties, SectorDescription, SectorInfo};
use crate::flashing::FlashAlgorithm;
#[test]
fn flash_sector_single_size() {
let config = FlashAlgorithm {
flash_properties: FlashProperties {
sectors: vec![SectorDescription {
size: 0x100,
address: 0x0,
}],
address_range: 0x1000..0x1000 + 0x1000,
page_size: 0x10,
..Default::default()
},
..Default::default()
};
let expected_first = SectorInfo {
base_address: 0x1000,
size: 0x100,
};
assert!(config.sector_info(0x1000 - 1).is_none());
assert_eq!(Some(expected_first), config.sector_info(0x1000));
assert_eq!(Some(expected_first), config.sector_info(0x10ff));
assert_eq!(Some(expected_first), config.sector_info(0x100b));
assert_eq!(Some(expected_first), config.sector_info(0x10ea));
}
#[test]
fn flash_sector_single_size_weird_sector_size() {
let config = FlashAlgorithm {
flash_properties: FlashProperties {
sectors: vec![SectorDescription {
size: 258,
address: 0x0,
}],
address_range: 0x800_0000..0x800_0000 + 258 * 10,
page_size: 0x10,
..Default::default()
},
..Default::default()
};
let expected_first = SectorInfo {
base_address: 0x800_0000,
size: 258,
};
assert!(config.sector_info(0x800_0000 - 1).is_none());
assert_eq!(Some(expected_first), config.sector_info(0x800_0000));
assert_eq!(Some(expected_first), config.sector_info(0x800_0000 + 257));
assert_eq!(Some(expected_first), config.sector_info(0x800_000b));
assert_eq!(Some(expected_first), config.sector_info(0x800_00e0));
}
#[test]
fn flash_sector_multiple_sizes() {
let config = FlashAlgorithm {
flash_properties: FlashProperties {
sectors: vec![
SectorDescription {
size: 0x4000,
address: 0x0,
},
SectorDescription {
size: 0x1_0000,
address: 0x1_0000,
},
SectorDescription {
size: 0x2_0000,
address: 0x2_0000,
},
],
address_range: 0x800_0000..0x800_0000 + 0x10_0000,
page_size: 0x10,
..Default::default()
},
..Default::default()
};
let expected_a = SectorInfo {
base_address: 0x800_4000,
size: 0x4000,
};
let expected_b = SectorInfo {
base_address: 0x801_0000,
size: 0x1_0000,
};
let expected_c = SectorInfo {
base_address: 0x80A_0000,
size: 0x2_0000,
};
assert_eq!(Some(expected_a), config.sector_info(0x800_4000));
assert_eq!(Some(expected_b), config.sector_info(0x801_0000));
assert_eq!(Some(expected_c), config.sector_info(0x80A_0000));
}
#[test]
fn flash_sector_multiple_sizes_iter() {
let config = FlashAlgorithm {
flash_properties: FlashProperties {
sectors: vec![
SectorDescription {
size: 0x4000,
address: 0x0,
},
SectorDescription {
size: 0x1_0000,
address: 0x1_0000,
},
SectorDescription {
size: 0x2_0000,
address: 0x2_0000,
},
],
address_range: 0x800_0000..0x800_0000 + 0x8_0000,
page_size: 0x10,
..Default::default()
},
..Default::default()
};
let got: Vec<SectorInfo> = config.iter_sectors().collect();
let expected = &[
SectorInfo {
base_address: 0x800_0000,
size: 0x4000,
},
SectorInfo {
base_address: 0x800_4000,
size: 0x4000,
},
SectorInfo {
base_address: 0x800_8000,
size: 0x4000,
},
SectorInfo {
base_address: 0x800_c000,
size: 0x4000,
},
SectorInfo {
base_address: 0x801_0000,
size: 0x1_0000,
},
SectorInfo {
base_address: 0x802_0000,
size: 0x2_0000,
},
SectorInfo {
base_address: 0x804_0000,
size: 0x2_0000,
},
SectorInfo {
base_address: 0x806_0000,
size: 0x2_0000,
},
];
assert_eq!(&got, expected);
}
}