Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 106 additions & 9 deletions boards/components/src/tickv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@
//! components::flash_user_component_static!(lowrisc::flash_ctrl::FlashCtrl),
//! );
//!
//! let kvstore = components::tickv::TicKVComponent::new(
//! // SipHash
//! let sip_hash = static_init!(
//! capsules_extra::sip_hash::SipHasher24,
//! capsules_extra::sip_hash::SipHasher24::new()
//! );
//! sip_hash.register();
//!
//! let tickv = components::tickv::TicKVComponent::new(
//! sip_hash,
//! &mux_flash,
//! 0x20040000 / lowrisc::flash_ctrl::PAGE_SIZE,
//! 0x40000,
//! flash_ctrl_read_buf,
//! page_buffer,
//! )
//! .finalize(components::tickv_component_static!(
//! lowrisc::flash_ctrl::FlashCtrl
//! lowrisc::flash_ctrl::FlashCtrl,
//! capsules_extra::sip_hash::SipHasher24
//! ));
//! hil::flash::HasClient::set_client(&peripherals.flash_ctrl, mux_flash);
//! ```
Expand All @@ -50,44 +59,58 @@ use kernel::hil::hasher::Hasher;
// Setup static space for the objects.
#[macro_export]
macro_rules! tickv_component_static {
($F:ty, $H:ty) => {{
($F:ty, $H:ty, $PAGE_SIZE:expr $(,)?) => {{
let flash =
kernel::static_buf!(capsules_core::virtualizers::virtual_flash::FlashUser<'static, $F>);
let tickv = kernel::static_buf!(
capsules_extra::tickv::TicKVStore<
'static,
capsules_core::virtualizers::virtual_flash::FlashUser<'static, $F>,
$H,
$PAGE_SIZE,
>
);

(flash, tickv)
};};
}

#[macro_export]
macro_rules! tickv_dedicated_flash_component_static {
($F:ty, $H:ty, $PAGE_SIZE:expr $(,)?) => {{
let tickfs_read_buffer = kernel::static_buf!([u8; $PAGE_SIZE]);
let tickv =
kernel::static_buf!(capsules_extra::tickv::TicKVStore<'static, $F, $H, $PAGE_SIZE>);

(tickv, tickfs_read_buffer)
};};
}

pub struct TicKVComponent<
F: 'static + hil::flash::Flash + hil::flash::HasClient<'static, MuxFlash<'static, F>>,
H: 'static + Hasher<'static, 8>,
const PAGE_SIZE: usize,
> {
mux_flash: &'static MuxFlash<'static, F>,
hasher: &'static H,
region_offset: usize,
flash_size: usize,
tickfs_read_buf: &'static mut [u8; 2048],
tickfs_read_buf: &'static mut [u8; PAGE_SIZE],
flash_read_buffer: &'static mut F::Page,
}

impl<
F: 'static + hil::flash::Flash + hil::flash::HasClient<'static, MuxFlash<'static, F>>,
H: Hasher<'static, 8>,
> TicKVComponent<F, H>
const PAGE_SIZE: usize,
> TicKVComponent<F, H, PAGE_SIZE>
{
pub fn new(
hasher: &'static H,
mux_flash: &'static MuxFlash<'static, F>,
region_offset: usize,
flash_size: usize,
tickfs_read_buf: &'static mut [u8; 2048],
tickfs_read_buf: &'static mut [u8; PAGE_SIZE],
flash_read_buffer: &'static mut F::Page,
) -> Self {
Self {
Expand All @@ -104,13 +127,14 @@ impl<
impl<
F: 'static + hil::flash::Flash + hil::flash::HasClient<'static, MuxFlash<'static, F>>,
H: 'static + Hasher<'static, 8>,
> Component for TicKVComponent<F, H>
const PAGE_SIZE: usize,
> Component for TicKVComponent<F, H, PAGE_SIZE>
{
type StaticInput = (
&'static mut MaybeUninit<FlashUser<'static, F>>,
&'static mut MaybeUninit<TicKVStore<'static, FlashUser<'static, F>, H>>,
&'static mut MaybeUninit<TicKVStore<'static, FlashUser<'static, F>, H, PAGE_SIZE>>,
);
type Output = &'static TicKVStore<'static, FlashUser<'static, F>, H>;
type Output = &'static TicKVStore<'static, FlashUser<'static, F>, H, PAGE_SIZE>;

fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let _grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
Expand All @@ -130,3 +154,76 @@ impl<
driver
}
}

pub struct TicKVDedicatedFlashComponent<
F: 'static
+ hil::flash::Flash
+ hil::flash::HasClient<'static, TicKVStore<'static, F, H, PAGE_SIZE>>,
H: 'static + Hasher<'static, 8>,
const PAGE_SIZE: usize,
> {
flash: &'static F,
hasher: &'static H,
region_offset: usize,
flash_size: usize,
flash_read_buffer: &'static mut F::Page,
}

impl<
F: 'static
+ hil::flash::Flash
+ hil::flash::HasClient<'static, TicKVStore<'static, F, H, PAGE_SIZE>>,
H: Hasher<'static, 8>,
const PAGE_SIZE: usize,
> TicKVDedicatedFlashComponent<F, H, PAGE_SIZE>
{
pub fn new(
hasher: &'static H,
flash: &'static F,
region_offset: usize,
flash_size: usize,
flash_read_buffer: &'static mut F::Page,
) -> Self {
Self {
hasher,
flash,
region_offset,
flash_size,
flash_read_buffer,
}
}
}

impl<
F: 'static
+ hil::flash::Flash
+ hil::flash::HasClient<'static, TicKVStore<'static, F, H, PAGE_SIZE>>,
H: 'static + Hasher<'static, 8>,
const PAGE_SIZE: usize,
> Component for TicKVDedicatedFlashComponent<F, H, PAGE_SIZE>
{
type StaticInput = (
&'static mut MaybeUninit<TicKVStore<'static, F, H, PAGE_SIZE>>,
&'static mut MaybeUninit<[u8; PAGE_SIZE]>,
);
type Output = &'static TicKVStore<'static, F, H, PAGE_SIZE>;

fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let _grant_cap = create_capability!(capabilities::MemoryAllocationCapability);

let tickfs_read_buf = static_buffer.1.write([0; PAGE_SIZE]);

let tickv = static_buffer.0.write(TicKVStore::new(
self.flash,
self.hasher,
tickfs_read_buf,
self.flash_read_buffer,
self.region_offset,
self.flash_size,
));
self.flash.set_client(tickv);
self.hasher.set_client(tickv);
tickv.initialise();
tickv
}
}
8 changes: 7 additions & 1 deletion boards/opentitan/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static mut TICKV: Option<
lowrisc::flash_ctrl::FlashCtrl<'static>,
>,
capsules_extra::sip_hash::SipHasher24<'static>,
2048,
>,
> = None;
// Test access to AES CCM
Expand Down Expand Up @@ -169,6 +170,7 @@ struct EarlGrey {
lowrisc::flash_ctrl::FlashCtrl<'static>,
>,
capsules_extra::sip_hash::SipHasher24<'static>,
2048,
>,
[u8; 8],
>,
Expand Down Expand Up @@ -534,7 +536,8 @@ unsafe fn setup() -> (
)
.finalize(components::tickv_component_static!(
lowrisc::flash_ctrl::FlashCtrl,
capsules_extra::sip_hash::SipHasher24
capsules_extra::sip_hash::SipHasher24,
2048
));
hil::flash::HasClient::set_client(&peripherals.flash_ctrl, mux_flash);
sip_hash.set_client(tickv);
Expand All @@ -547,6 +550,7 @@ unsafe fn setup() -> (
lowrisc::flash_ctrl::FlashCtrl,
>,
capsules_extra::sip_hash::SipHasher24<'static>,
2048,
>,
capsules_extra::tickv::TicKVKeyType,
),
Expand All @@ -559,6 +563,7 @@ unsafe fn setup() -> (
lowrisc::flash_ctrl::FlashCtrl,
>,
capsules_extra::sip_hash::SipHasher24<'static>,
2048,
>,
capsules_extra::tickv::TicKVKeyType,
),
Expand All @@ -574,6 +579,7 @@ unsafe fn setup() -> (
capsules_extra::tickv::TicKVStore<
capsules_core::virtualizers::virtual_flash::FlashUser<lowrisc::flash_ctrl::FlashCtrl>,
capsules_extra::sip_hash::SipHasher24<'static>,
2048,
>,
capsules_extra::tickv::TicKVKeyType,
));
Expand Down
1 change: 1 addition & 0 deletions boards/opentitan/src/tests/tickv_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn tickv_append_key() {
'static,
FlashUser<'static, lowrisc::flash_ctrl::FlashCtrl<'static>>,
capsules_extra::sip_hash::SipHasher24,
2048,
>,
TicKVKeyType,
>,
Expand Down
34 changes: 21 additions & 13 deletions capsules/extra/src/tickv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ impl<'a, F: Flash> TickFSFlashCtrl<'a, F> {
}
}

impl<'a, F: Flash> tickv::flash_controller::FlashController<2048> for TickFSFlashCtrl<'a, F> {
impl<'a, F: Flash, const PAGE_SIZE: usize> tickv::flash_controller::FlashController<PAGE_SIZE>
for TickFSFlashCtrl<'a, F>
{
fn read_region(
&self,
region_number: usize,
_offset: usize,
_buf: &mut [u8; 2048],
_buf: &mut [u8; PAGE_SIZE],
) -> Result<(), tickv::error_codes::ErrorCode> {
if self
.flash
Expand All @@ -100,12 +102,12 @@ impl<'a, F: Flash> tickv::flash_controller::FlashController<2048> for TickFSFlas
let data_buf = self.flash_read_buffer.take().unwrap();

for (i, d) in buf.iter().enumerate() {
data_buf.as_mut()[i + (address % 2048)] = *d;
data_buf.as_mut()[i + (address % PAGE_SIZE)] = *d;
}

if self
.flash
.write_page(self.region_offset + (address / 2048), data_buf)
.write_page(self.region_offset + (address / PAGE_SIZE), data_buf)
.is_err()
{
return Err(tickv::error_codes::ErrorCode::WriteFail);
Expand All @@ -123,8 +125,8 @@ impl<'a, F: Flash> tickv::flash_controller::FlashController<2048> for TickFSFlas

pub type TicKVKeyType = [u8; 8];

pub struct TicKVStore<'a, F: Flash + 'static, H: Hasher<'a, 8>> {
tickv: AsyncTicKV<'a, TickFSFlashCtrl<'a, F>, 2048>,
pub struct TicKVStore<'a, F: Flash + 'static, H: Hasher<'a, 8>, const PAGE_SIZE: usize> {
tickv: AsyncTicKV<'a, TickFSFlashCtrl<'a, F>, PAGE_SIZE>,
hasher: &'a H,
operation: Cell<Operation>,
next_operation: Cell<Operation>,
Expand All @@ -138,16 +140,16 @@ pub struct TicKVStore<'a, F: Flash + 'static, H: Hasher<'a, 8>> {
client: OptionalCell<&'a dyn kv_system::Client<TicKVKeyType>>,
}

impl<'a, F: Flash, H: Hasher<'a, 8>> TicKVStore<'a, F, H> {
impl<'a, F: Flash, H: Hasher<'a, 8>, const PAGE_SIZE: usize> TicKVStore<'a, F, H, PAGE_SIZE> {
pub fn new(
flash: &'a F,
hasher: &'a H,
tickfs_read_buf: &'static mut [u8; 2048],
tickfs_read_buf: &'static mut [u8; PAGE_SIZE],
flash_read_buffer: &'static mut F::Page,
region_offset: usize,
flash_size: usize,
) -> TicKVStore<'a, F, H> {
let tickv = AsyncTicKV::<TickFSFlashCtrl<F>, 2048>::new(
) -> TicKVStore<'a, F, H, PAGE_SIZE> {
let tickv = AsyncTicKV::<TickFSFlashCtrl<F>, PAGE_SIZE>::new(
TickFSFlashCtrl::new(flash, flash_read_buffer, region_offset),
tickfs_read_buf,
flash_size,
Expand Down Expand Up @@ -225,7 +227,9 @@ impl<'a, F: Flash, H: Hasher<'a, 8>> TicKVStore<'a, F, H> {
}
}

impl<'a, F: Flash, H: Hasher<'a, 8>> hasher::Client<8> for TicKVStore<'a, F, H> {
impl<'a, F: Flash, H: Hasher<'a, 8>, const PAGE_SIZE: usize> hasher::Client<8>
for TicKVStore<'a, F, H, PAGE_SIZE>
{
fn add_mut_data_done(&self, _result: Result<(), ErrorCode>, data: &'static mut [u8]) {
self.unhashed_key_buf.replace(data);
self.hasher.run(self.key_buf.take().unwrap()).unwrap();
Expand All @@ -242,7 +246,9 @@ impl<'a, F: Flash, H: Hasher<'a, 8>> hasher::Client<8> for TicKVStore<'a, F, H>
}
}

impl<'a, F: Flash, H: Hasher<'a, 8>> flash::Client<F> for TicKVStore<'a, F, H> {
impl<'a, F: Flash, H: Hasher<'a, 8>, const PAGE_SIZE: usize> flash::Client<F>
for TicKVStore<'a, F, H, PAGE_SIZE>
{
fn read_complete(&self, pagebuffer: &'static mut F::Page, _error: flash::Error) {
self.tickv.set_read_buffer(pagebuffer.as_mut());
self.tickv
Expand Down Expand Up @@ -377,7 +383,9 @@ impl<'a, F: Flash, H: Hasher<'a, 8>> flash::Client<F> for TicKVStore<'a, F, H> {
}
}

impl<'a, F: Flash, H: Hasher<'a, 8>> KVSystem<'a> for TicKVStore<'a, F, H> {
impl<'a, F: Flash, H: Hasher<'a, 8>, const PAGE_SIZE: usize> KVSystem<'a>
for TicKVStore<'a, F, H, PAGE_SIZE>
{
type K = TicKVKeyType;

fn set_client(&self, client: &'a dyn kv_system::Client<Self::K>) {
Expand Down