David Brown | e2acfae | 2020-01-21 16:45:01 -0700 | [diff] [blame] | 1 | // Copyright (c) 2017-2019 Linaro LTD |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | |
David Brown | f52272c | 2017-07-12 09:56:16 -0600 | [diff] [blame] | 5 | mod area; |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 6 | pub mod c; |
David Brown | f52272c | 2017-07-12 09:56:16 -0600 | [diff] [blame] | 7 | |
| 8 | // The API needs to be public, even though it isn't intended to be called by Rust code, but the |
| 9 | // functions are exported to C code. |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 10 | pub mod api; |
David Brown | f52272c | 2017-07-12 09:56:16 -0600 | [diff] [blame] | 11 | |
David Brown | 65de6d1 | 2019-01-02 11:38:38 -0700 | [diff] [blame] | 12 | pub use crate::area::{AreaDesc, FlashId}; |
David Brown | 8a4e23b | 2021-06-11 10:29:01 -0600 | [diff] [blame] | 13 | |
| 14 | /// For testing the ram load feature, we need to emulate a block of RAM and be able to pass that |
| 15 | /// down to the C code. The call down to boot_go should go through this object so that the buffer |
| 16 | /// itself is managed properly. |
| 17 | pub struct RamBlock { |
| 18 | ram: Vec<u8>, |
| 19 | offset: u32, // 32-bit offset. |
| 20 | } |
| 21 | |
| 22 | impl RamBlock { |
| 23 | pub fn new(size: u32, offset: u32) -> RamBlock { |
| 24 | RamBlock { |
| 25 | ram: vec![0; size as usize], |
| 26 | offset: offset, |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /// Borrow the RAM buffer, with 'offset' being the beginning of the buffer. |
| 31 | pub fn borrow(&self) -> &[u8] { |
| 32 | &self.ram |
| 33 | } |
| 34 | |
David Brown | f17d391 | 2021-06-23 16:10:51 -0600 | [diff] [blame^] | 35 | /// Borrow a piece of the ram, with 'offset' being the beginning of the buffer. |
| 36 | pub fn borrow_part(&self, base: usize, size: usize) -> &[u8] { |
| 37 | &self.ram[base..base+size] |
| 38 | } |
| 39 | |
David Brown | 8a4e23b | 2021-06-11 10:29:01 -0600 | [diff] [blame] | 40 | pub fn invoke<F, R>(&self, act: F) -> R |
| 41 | where F: FnOnce() -> R |
| 42 | { |
| 43 | api::set_ram_info(api::BootsimRamInfo { |
| 44 | start: self.offset, |
| 45 | size: self.ram.len() as u32, |
| 46 | base: &self.ram[0] as *const u8 as usize - self.offset as usize, |
| 47 | }); |
| 48 | let result = act(); |
| 49 | api::clear_ram_info(); |
| 50 | result |
| 51 | } |
| 52 | } |