Add align/erased_val params per flash device

The previous c/rust ffi functions were hardcoding the values of align
and erased_val before each run through static globals. This adds new sim
flash functions that get the align/erased_val from the sim flash device
that is being run on, allowing that later multiple flash devices can
each use its own params.

Signed-off-by: Fabio Utzig <utzig@apache.org>
This commit is contained in:
Fabio Utzig 2018-10-24 21:49:09 -03:00 committed by Fabio Utzig
parent 269d28621d
commit 73ffc4458d
4 changed files with 44 additions and 17 deletions

View File

@ -34,6 +34,8 @@ extern int sim_flash_read(uint8_t flash_id, uint32_t offset, uint8_t *dest,
uint32_t size);
extern int sim_flash_write(uint8_t flash_id, uint32_t offset, const uint8_t *src,
uint32_t size);
extern uint8_t sim_flash_align(uint8_t flash_id);
extern uint8_t sim_flash_erased_val(uint8_t flash_id);
static jmp_buf boot_jmpbuf;
int flash_counter;
@ -205,18 +207,14 @@ done:
#endif
}
uint8_t sim_flash_align = 1;
uint8_t flash_area_align(const struct flash_area *area)
{
(void)area;
return sim_flash_align;
return sim_flash_align(area->fa_device_id);
}
uint8_t sim_flash_erased_val = 0xff;
uint8_t flash_area_erased_val(const struct flash_area *area)
{
(void)area;
return sim_flash_erased_val;
return sim_flash_erased_val(area->fa_device_id);
}
struct area {
@ -337,7 +335,7 @@ int flash_area_read_is_empty(const struct flash_area *area, uint32_t off,
}
for (i = 0, u8dst = (uint8_t *)dst; i < len; i++) {
if (u8dst[i] != sim_flash_erased_val) {
if (u8dst[i] != sim_flash_erased_val(area->fa_device_id)) {
return 0;
}
}

View File

@ -5,12 +5,31 @@ use libc;
use log::LogLevel;
use std::mem;
use std::slice;
use std::collections::HashMap;
use std::sync::Mutex;
// The current active flash device. The 'static is a lie, and we manage the lifetime ourselves.
static mut FLASH: Option<*mut Flash> = None;
struct FlashParams {
align: u8,
erased_val: u8,
}
lazy_static! {
static ref FLASH_PARAMS: Mutex<HashMap<u8, FlashParams>> = {
Mutex::new(HashMap::new())
};
}
// Set the flash device to be used by the simulation. The pointer is unsafely stashed away.
pub unsafe fn set_flash(dev: &mut Flash) {
pub unsafe fn set_flash(dev_id: u8, dev: &mut Flash) {
let mut flash_params = FLASH_PARAMS.lock().unwrap();
flash_params.insert(dev_id, FlashParams {
align: dev.align() as u8,
erased_val: dev.erased_val(),
});
let dev: &'static mut Flash = mem::transmute(dev);
FLASH = Some(dev as *mut Flash);
}
@ -52,6 +71,20 @@ pub extern fn sim_flash_write(_id: u8, offset: u32, src: *const u8, size: u32) -
map_err(dev.write(offset as usize, &buf))
}
#[no_mangle]
pub extern fn sim_flash_align(id: u8) -> u8 {
let flash_params = FLASH_PARAMS.lock().unwrap();
let params = flash_params.get(&id).unwrap();
params.align
}
#[no_mangle]
pub extern fn sim_flash_erased_val(id: u8) -> u8 {
let flash_params = FLASH_PARAMS.lock().unwrap();
let params = flash_params.get(&id).unwrap();
params.erased_val
}
fn map_err(err: Result<()>) -> libc::c_int {
match err {
Ok(()) => 0,

View File

@ -54,7 +54,7 @@ impl AreaDesc {
area.push(FlashArea {
flash_id: id,
device_id: 42,
device_id: 0,
pad16: 0,
off: sector.base as u32,
size: sector.size as u32,
@ -71,7 +71,7 @@ impl AreaDesc {
self.areas.push(area);
self.whole.push(FlashArea {
flash_id: id,
device_id: 42,
device_id: 0,
pad16: 0,
off: orig_base as u32,
size: orig_len as u32,
@ -85,7 +85,7 @@ impl AreaDesc {
pub fn add_simple_image(&mut self, base: usize, len: usize, id: FlashId) {
let area = vec![FlashArea {
flash_id: id,
device_id: 42,
device_id: 0,
pad16: 0,
off: base as u32,
size: len as u32,
@ -94,7 +94,7 @@ impl AreaDesc {
self.areas.push(area);
self.whole.push(FlashArea {
flash_id: id,
device_id: 42,
device_id: 0,
pad16: 0,
off: base as u32,
size: len as u32,

View File

@ -18,11 +18,9 @@ pub fn boot_go(flash: &mut Flash, areadesc: &AreaDesc, counter: Option<&mut i32>
let _lock = BOOT_LOCK.lock().unwrap();
unsafe {
api::set_flash(flash);
api::set_flash(0, flash);
raw::c_catch_asserts = if catch_asserts { 1 } else { 0 };
raw::c_asserts = 0u8;
raw::sim_flash_align = flash.align() as u8;
raw::sim_flash_erased_val = flash.erased_val();
raw::flash_counter = match counter {
None => 0,
Some(ref c) => **c as libc::c_int
@ -95,8 +93,6 @@ mod raw {
pub static mut c_asserts: u8;
pub static mut c_catch_asserts: u8;
pub static mut sim_flash_align: u8;
pub static mut sim_flash_erased_val: u8;
pub fn boot_slots_trailer_sz(min_write_sz: u8) -> u32;
pub static BOOT_MAGIC_SZ: u32;