Tock V2 port - rebased and updated (#620)
* Changes from #580 * fixes USB cancel panic * style fixes * Update src/env/tock/storage.rs Co-authored-by: Zach Halvorsen <zhalvorsen@google.com> --------- Co-authored-by: Zach Halvorsen <zhalvorsen@google.com>
This commit is contained in:
95
third_party/libtock-drivers/src/rng.rs
vendored
95
third_party/libtock-drivers/src/rng.rs
vendored
@@ -1,45 +1,78 @@
|
||||
use crate::util;
|
||||
use core::cell::Cell;
|
||||
use libtock_core::{callback, syscalls};
|
||||
//! Userspace interface for easy access to the random number generator
|
||||
|
||||
const DRIVER_NUMBER: usize = 0x40001;
|
||||
use crate::util::Util;
|
||||
use core::cell::Cell;
|
||||
use core::convert::TryInto;
|
||||
use libtock_platform as platform;
|
||||
use libtock_platform::{share, AllowRw, DefaultConfig, Subscribe, Syscalls};
|
||||
use platform::ErrorCode;
|
||||
|
||||
/// Driver number for the random number generator
|
||||
const DRIVER_NUMBER: u32 = 0x40001;
|
||||
|
||||
mod command_nr {
|
||||
pub const REQUEST_RNG: usize = 1;
|
||||
pub const REQUEST_RNG: u32 = 1;
|
||||
}
|
||||
|
||||
mod subscribe_nr {
|
||||
pub const BUFFER_FILLED: usize = 0;
|
||||
pub const BUFFER_FILLED: u32 = 0;
|
||||
}
|
||||
|
||||
mod allow_nr {
|
||||
pub const SHARE_BUFFER: usize = 0;
|
||||
pub const SHARE_BUFFER: u32 = 0;
|
||||
}
|
||||
|
||||
pub fn fill_buffer(buf: &mut [u8]) -> bool {
|
||||
let buf_len = buf.len();
|
||||
/// System call configuration trait for `Rng`
|
||||
pub trait Config: platform::allow_rw::Config + platform::subscribe::Config {}
|
||||
|
||||
let result = syscalls::allow(DRIVER_NUMBER, allow_nr::SHARE_BUFFER, buf);
|
||||
if result.is_err() {
|
||||
return false;
|
||||
impl<T: platform::allow_rw::Config + platform::subscribe::Config> Config for T {}
|
||||
|
||||
pub struct Rng<S: Syscalls, C: Config = DefaultConfig>(S, C);
|
||||
|
||||
impl<S: Syscalls, C: Config> Rng<S, C> {
|
||||
pub fn fill_buffer(buf: &mut [u8]) -> bool {
|
||||
let buf_len = buf.len();
|
||||
let is_filled = Cell::new(false);
|
||||
|
||||
share::scope::<
|
||||
(
|
||||
AllowRw<_, DRIVER_NUMBER, { allow_nr::SHARE_BUFFER }>,
|
||||
Subscribe<_, DRIVER_NUMBER, { subscribe_nr::BUFFER_FILLED }>,
|
||||
),
|
||||
_,
|
||||
_,
|
||||
>(|handle| {
|
||||
let (allow_rw, subscribe) = handle.split();
|
||||
let result = S::allow_rw::<C, DRIVER_NUMBER, { allow_nr::SHARE_BUFFER }>(allow_rw, buf);
|
||||
if result.is_err() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Automatically sets `is_filled` to true as soon as the buffer is filled.
|
||||
let subscription =
|
||||
S::subscribe::<_, _, C, DRIVER_NUMBER, { subscribe_nr::BUFFER_FILLED }>(
|
||||
subscribe, &is_filled,
|
||||
);
|
||||
if subscription.is_err() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Requests the random number generator to fill the buffer.
|
||||
let result_code: Result<(), ErrorCode> = S::command(
|
||||
DRIVER_NUMBER,
|
||||
command_nr::REQUEST_RNG,
|
||||
buf_len.try_into().unwrap(),
|
||||
0,
|
||||
)
|
||||
.to_result();
|
||||
if result_code.is_err() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Yields until the buffer is filled.
|
||||
Util::<S>::yieldk_for(|| is_filled.get());
|
||||
|
||||
true
|
||||
})
|
||||
}
|
||||
|
||||
let is_filled = Cell::new(false);
|
||||
let mut is_filled_alarm = || is_filled.set(true);
|
||||
let subscription = syscalls::subscribe::<callback::Identity0Consumer, _>(
|
||||
DRIVER_NUMBER,
|
||||
subscribe_nr::BUFFER_FILLED,
|
||||
&mut is_filled_alarm,
|
||||
);
|
||||
if subscription.is_err() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let result_code = syscalls::command(DRIVER_NUMBER, command_nr::REQUEST_RNG, buf_len, 0);
|
||||
if result_code.is_err() {
|
||||
return false;
|
||||
}
|
||||
|
||||
util::yieldk_for(|| is_filled.get());
|
||||
true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user