blob: a99c1c85ef61e8f10e0be0aa276cd9ac05f3f5c6 [file] [log] [blame]
David Browne2acfae2020-01-21 16:45:01 -07001// Copyright (c) 2017-2019 Linaro LTD
2// Copyright (c) 2018-2019 JUUL Labs
3//
4// SPDX-License-Identifier: Apache-2.0
5
David Brownde7729e2017-01-09 10:41:35 -07006//! HAL api for MyNewt applications
7
Fabio Utzig8000e322019-08-05 08:14:32 -03008use crate::area::CAreaDesc;
David Brown28215642019-01-02 11:42:39 -07009use log::{Level, log_enabled, warn};
Fabio Utzig8000e322019-08-05 08:14:32 -030010use simflash::{Result, Flash, FlashPtr};
David Brown28215642019-01-02 11:42:39 -070011use std::{
Fabio Utzig8000e322019-08-05 08:14:32 -030012 cell::RefCell,
David Brown28215642019-01-02 11:42:39 -070013 collections::HashMap,
14 mem,
Fabio Utzig8000e322019-08-05 08:14:32 -030015 ptr,
David Brown28215642019-01-02 11:42:39 -070016 slice,
David Brown28215642019-01-02 11:42:39 -070017};
David Brownde7729e2017-01-09 10:41:35 -070018
Fabio Utzig1c9aea52018-11-15 10:36:07 -020019/// A FlashMap maintain a table of [device_id -> Flash trait]
Fabio Utzigafb2bc92018-11-19 16:11:52 -020020pub type FlashMap = HashMap<u8, FlashPtr>;
Fabio Utzig1c9aea52018-11-15 10:36:07 -020021
Fabio Utzig8000e322019-08-05 08:14:32 -030022pub struct FlashParamsStruct {
Fabio Utzig1edb7882020-10-04 11:51:53 -030023 align: u16,
Fabio Utzig73ffc442018-10-24 21:49:09 -030024 erased_val: u8,
25}
26
Fabio Utzig8000e322019-08-05 08:14:32 -030027pub type FlashParams = HashMap<u8, FlashParamsStruct>;
28
David Brownd216b202021-06-04 10:14:33 -060029/// The `boot_rsp` structure used by boot_go.
30#[repr(C)]
31#[derive(Debug)]
32pub struct BootRsp {
33 pub br_hdr: *const ImageHeader,
34 pub flash_dev_id: u8,
35 pub image_off: u32,
36}
37
38// TODO: Don't duplicate this image header declaration.
39#[repr(C)]
40#[derive(Debug)]
41pub struct ImageHeader {
42 magic: u32,
43 load_addr: u32,
44 hdr_size: u16,
45 protect_tlv_size: u16,
46 img_size: u32,
47 flags: u32,
48 ver: ImageVersion,
49 _pad2: u32,
50}
51
52#[repr(C)]
53#[derive(Debug)]
54pub struct ImageVersion {
55 pub major: u8,
56 pub minor: u8,
57 pub revision: u16,
58 pub build_num: u32,
59}
60
Fabio Utzig8000e322019-08-05 08:14:32 -030061pub struct CAreaDescPtr {
62 pub ptr: *const CAreaDesc,
63}
64
65pub struct FlashContext {
66 flash_map: FlashMap,
67 flash_params: FlashParams,
68 flash_areas: CAreaDescPtr,
69}
70
71impl FlashContext {
72 pub fn new() -> FlashContext {
73 FlashContext {
74 flash_map: HashMap::new(),
75 flash_params: HashMap::new(),
76 flash_areas: CAreaDescPtr{ptr: ptr::null()},
77 }
78 }
79}
80
David Brownfc8e3c52021-03-10 05:11:26 -070081impl Default for FlashContext {
82 fn default() -> FlashContext {
83 FlashContext {
84 flash_map: HashMap::new(),
85 flash_params: HashMap::new(),
86 flash_areas: CAreaDescPtr{ptr: ptr::null()},
87 }
88 }
89}
90
Fabio Utzig8000e322019-08-05 08:14:32 -030091#[repr(C)]
92#[derive(Debug, Default)]
93pub struct CSimContext {
94 pub flash_counter: libc::c_int,
95 pub jumped: libc::c_int,
96 pub c_asserts: u8,
97 pub c_catch_asserts: u8,
98 // NOTE: Always leave boot_jmpbuf declaration at the end; this should
99 // store a "jmp_buf" which is arch specific and not defined by libc crate.
100 // The size below is enough to store data on a x86_64 machine.
101 pub boot_jmpbuf: [u64; 16],
102}
103
104pub struct CSimContextPtr {
105 pub ptr: *const CSimContext,
106}
107
108impl CSimContextPtr {
109 pub fn new() -> CSimContextPtr {
110 CSimContextPtr {
111 ptr: ptr::null(),
112 }
113 }
114}
115
David Brownfc8e3c52021-03-10 05:11:26 -0700116impl Default for CSimContextPtr {
117 fn default() -> CSimContextPtr {
118 CSimContextPtr {
119 ptr: ptr::null(),
120 }
121 }
122}
123
Fabio Utzig8000e322019-08-05 08:14:32 -0300124thread_local! {
125 pub static THREAD_CTX: RefCell<FlashContext> = RefCell::new(FlashContext::new());
126 pub static SIM_CTX: RefCell<CSimContextPtr> = RefCell::new(CSimContextPtr::new());
Fabio Utzig73ffc442018-10-24 21:49:09 -0300127}
128
David Brown7cc45262021-03-10 05:13:44 -0700129/// Set the flash device to be used by the simulation. The pointer is unsafely stashed away.
130///
131/// # Safety
132///
133/// This uses mem::transmute to stash a Rust pointer into a C value to
134/// retrieve later. It should be safe to use this.
135pub fn set_flash(dev_id: u8, dev: &mut dyn Flash) {
Fabio Utzig8000e322019-08-05 08:14:32 -0300136 THREAD_CTX.with(|ctx| {
137 ctx.borrow_mut().flash_params.insert(dev_id, FlashParamsStruct {
Fabio Utzig1edb7882020-10-04 11:51:53 -0300138 align: dev.align() as u16,
Fabio Utzig8000e322019-08-05 08:14:32 -0300139 erased_val: dev.erased_val(),
140 });
David Brown7cc45262021-03-10 05:13:44 -0700141 unsafe {
142 let dev: &'static mut dyn Flash = mem::transmute(dev);
143 ctx.borrow_mut().flash_map.insert(
144 dev_id, FlashPtr{ptr: dev as *mut dyn Flash});
145 }
Fabio Utzig73ffc442018-10-24 21:49:09 -0300146 });
David Brownbdb6db72017-07-06 10:14:37 -0600147}
148
David Brown7cc45262021-03-10 05:13:44 -0700149pub fn clear_flash(dev_id: u8) {
Fabio Utzig8000e322019-08-05 08:14:32 -0300150 THREAD_CTX.with(|ctx| {
151 ctx.borrow_mut().flash_map.remove(&dev_id);
152 });
David Brownbdb6db72017-07-06 10:14:37 -0600153}
154
David Brownde7729e2017-01-09 10:41:35 -0700155// This isn't meant to call directly, but by a wrapper.
156
157#[no_mangle]
Fabio Utzig8000e322019-08-05 08:14:32 -0300158pub extern fn sim_get_flash_areas() -> *const CAreaDesc {
159 THREAD_CTX.with(|ctx| {
160 ctx.borrow().flash_areas.ptr
161 })
162}
163
164#[no_mangle]
165pub extern fn sim_set_flash_areas(areas: *const CAreaDesc) {
166 THREAD_CTX.with(|ctx| {
167 ctx.borrow_mut().flash_areas.ptr = areas;
168 });
169}
170
171#[no_mangle]
172pub extern fn sim_reset_flash_areas() {
173 THREAD_CTX.with(|ctx| {
174 ctx.borrow_mut().flash_areas.ptr = ptr::null();
175 });
176}
177
178#[no_mangle]
179pub extern fn sim_get_context() -> *const CSimContext {
180 SIM_CTX.with(|ctx| {
181 ctx.borrow().ptr
182 })
183}
184
185#[no_mangle]
186pub extern fn sim_set_context(ptr: *const CSimContext) {
187 SIM_CTX.with(|ctx| {
188 ctx.borrow_mut().ptr = ptr;
189 });
190}
191
192#[no_mangle]
193pub extern fn sim_reset_context() {
194 SIM_CTX.with(|ctx| {
195 ctx.borrow_mut().ptr = ptr::null();
196 });
197}
198
199#[no_mangle]
Fabio Utzig1c9aea52018-11-15 10:36:07 -0200200pub extern fn sim_flash_erase(dev_id: u8, offset: u32, size: u32) -> libc::c_int {
Fabio Utzig8000e322019-08-05 08:14:32 -0300201 let mut rc: libc::c_int = -19;
202 THREAD_CTX.with(|ctx| {
203 if let Some(flash) = ctx.borrow().flash_map.get(&dev_id) {
Fabio Utzig1c9aea52018-11-15 10:36:07 -0200204 let dev = unsafe { &mut *(flash.ptr) };
Fabio Utzig8000e322019-08-05 08:14:32 -0300205 rc = map_err(dev.erase(offset as usize, size as usize));
Fabio Utzig1c9aea52018-11-15 10:36:07 -0200206 }
Fabio Utzig8000e322019-08-05 08:14:32 -0300207 });
208 rc
David Brownde7729e2017-01-09 10:41:35 -0700209}
210
211#[no_mangle]
Fabio Utzig1c9aea52018-11-15 10:36:07 -0200212pub extern fn sim_flash_read(dev_id: u8, offset: u32, dest: *mut u8, size: u32) -> libc::c_int {
Fabio Utzig8000e322019-08-05 08:14:32 -0300213 let mut rc: libc::c_int = -19;
214 THREAD_CTX.with(|ctx| {
215 if let Some(flash) = ctx.borrow().flash_map.get(&dev_id) {
Fabio Utzig1c9aea52018-11-15 10:36:07 -0200216 let mut buf: &mut[u8] = unsafe { slice::from_raw_parts_mut(dest, size as usize) };
217 let dev = unsafe { &mut *(flash.ptr) };
Fabio Utzig8000e322019-08-05 08:14:32 -0300218 rc = map_err(dev.read(offset as usize, &mut buf));
Fabio Utzig1c9aea52018-11-15 10:36:07 -0200219 }
Fabio Utzig8000e322019-08-05 08:14:32 -0300220 });
221 rc
David Brownde7729e2017-01-09 10:41:35 -0700222}
223
224#[no_mangle]
Fabio Utzig1c9aea52018-11-15 10:36:07 -0200225pub extern fn sim_flash_write(dev_id: u8, offset: u32, src: *const u8, size: u32) -> libc::c_int {
Fabio Utzig8000e322019-08-05 08:14:32 -0300226 let mut rc: libc::c_int = -19;
227 THREAD_CTX.with(|ctx| {
228 if let Some(flash) = ctx.borrow().flash_map.get(&dev_id) {
Fabio Utzig1c9aea52018-11-15 10:36:07 -0200229 let buf: &[u8] = unsafe { slice::from_raw_parts(src, size as usize) };
230 let dev = unsafe { &mut *(flash.ptr) };
Fabio Utzig8000e322019-08-05 08:14:32 -0300231 rc = map_err(dev.write(offset as usize, &buf));
Fabio Utzig1c9aea52018-11-15 10:36:07 -0200232 }
Fabio Utzig8000e322019-08-05 08:14:32 -0300233 });
234 rc
David Brownde7729e2017-01-09 10:41:35 -0700235}
236
Fabio Utzig73ffc442018-10-24 21:49:09 -0300237#[no_mangle]
Fabio Utzig1edb7882020-10-04 11:51:53 -0300238pub extern fn sim_flash_align(id: u8) -> u16 {
Fabio Utzig8000e322019-08-05 08:14:32 -0300239 THREAD_CTX.with(|ctx| {
240 ctx.borrow().flash_params.get(&id).unwrap().align
241 })
Fabio Utzig73ffc442018-10-24 21:49:09 -0300242}
243
244#[no_mangle]
245pub extern fn sim_flash_erased_val(id: u8) -> u8 {
Fabio Utzig8000e322019-08-05 08:14:32 -0300246 THREAD_CTX.with(|ctx| {
247 ctx.borrow().flash_params.get(&id).unwrap().erased_val
248 })
Fabio Utzig73ffc442018-10-24 21:49:09 -0300249}
250
David Brownde7729e2017-01-09 10:41:35 -0700251fn map_err(err: Result<()>) -> libc::c_int {
252 match err {
253 Ok(()) => 0,
Fabio Utzig19b2c1a2017-04-20 07:32:44 -0300254 Err(e) => {
255 warn!("{}", e);
256 -1
257 },
David Brownde7729e2017-01-09 10:41:35 -0700258 }
259}
David Brown2d1d7cf2017-05-10 08:55:09 -0600260
261/// Called by C code to determine if we should log at this level. Levels are defined in
262/// bootutil/bootutil_log.h. This makes the logging from the C code controlled by bootsim::api, so
263/// for example, it can be enabled with something like:
264/// RUST_LOG=bootsim::api=info cargo run --release runall
265/// or
266/// RUST_LOG=bootsim=info cargo run --release runall
267#[no_mangle]
268pub extern fn sim_log_enabled(level: libc::c_int) -> libc::c_int {
269 let res = match level {
David Brown28215642019-01-02 11:42:39 -0700270 1 => log_enabled!(Level::Error),
271 2 => log_enabled!(Level::Warn),
272 3 => log_enabled!(Level::Info),
Fabio Utzige92df932019-12-10 14:29:17 -0300273 4 => log_enabled!(Level::Debug),
274 5 => log_enabled!(Level::Trace), // log level == SIM
David Brown2d1d7cf2017-05-10 08:55:09 -0600275 _ => false,
276 };
277 if res {
278 1
279 } else {
280 0
281 }
282}