Skip to content

Commit

Permalink
Merge pull request #597 from betrusted-io/cram-dev
Browse files Browse the repository at this point in the history
more cleanup for cram dev target
  • Loading branch information
bunnie authored Dec 1, 2024
2 parents cfc1114 + 33f3e2f commit 4cf9c96
Show file tree
Hide file tree
Showing 25 changed files with 16,110 additions and 21,233 deletions.
16 changes: 0 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ members = [
"libs/perflib",
"libs/tls",
"libs/userprefs",
"libs/xous-pio",
# "libs/xous-pio",
"libs/xous-bio",
"libs/xous-bio-bdma",
"libs/xous-pl230",
Expand Down
1 change: 1 addition & 0 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(ci)', 'cfg(baremetal)'] }

[features]
cramium-soc = ["utralib/cramium-soc", "cramium-hal", "rand_chacha", "raw-trng"]
verilator-only = ["cramium-hal/verilator-only"]
cramium-fpga = ["utralib/cramium-fpga"]
board-baosec = ["cramium-hal/board-baosec"]
board-baosor = ["cramium-hal/board-baosor"]
Expand Down
1 change: 1 addition & 0 deletions kernel/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ impl MemoryManager {
self.ram_size
);
};
#[cfg(feature = "swap")]
for o in 0..self.ram_size / PAGE_SIZE {
unsafe {
println!(
Expand Down
16 changes: 12 additions & 4 deletions libs/cramium-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,17 @@ either = { version = "1.9.0", default-features = false }
xous = { version = "0.9.64", features = ["v2p"] }

[features]
board-baosec = [] # USB form factor token
board-baosor = [] # Precursor form factor
board-dabao = [] # Dev board form factor
camera-ov2640 = []
display-sh1107 = []
axp2101 = []
board-baosec = [
"camera-ov2640",
"display-sh1107",
"axp2101",
] # USB form factor token
board-baosor = ["camera-ov2640", "axp2101"] # Precursor form factor
board-dabao = [] # Dev board form factor
verilator-only = []

udma-stress-test = [
] # For RTL simulation testing - aggravates a key corner case
Expand All @@ -39,4 +47,4 @@ compress-entropy = []
magic-manual = []
std = ["log", "xous-api-names", "usb-device", "xous-api-ticktimer"]
derive-rkyv = ["rkyv"]
default = ["board-baosec"]
default = []
69 changes: 69 additions & 0 deletions libs/cramium-hal/src/board/dabao.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Constants that define pin locations, RAM offsets, etc. for the DaBao basic breakout board
use crate::iox::IoSetup;
use crate::iox::*;

// console uart buffer
pub const UART_DMA_TX_BUF_PHYS: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_MEM_LEN - 4096;

// RAM needs two buffers of 1k + 16 bytes = 2048 + 16 = 2064 bytes; round up to one page
pub const SPIM_RAM_IFRAM_ADDR: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_MEM_LEN - 2 * 4096;

// app uart buffer
pub const APP_UART_IFRAM_ADDR: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_MEM_LEN - 3 * 4096;

// one page for the I2C driver
pub const I2C_IFRAM_ADDR: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_MEM_LEN - 4 * 4096;

// USB pages - USB subsystem is a hog, needs a lot of pages, note this is mutually exclusive with camera,
// can't use both at once
pub const CRG_IFRAM_PAGES: usize = 23; // +1 for extended application buffer by 4k
pub const CRG_UDC_MEMBASE: usize =
utralib::HW_IFRAM1_MEM + utralib::HW_IFRAM1_MEM_LEN - CRG_IFRAM_PAGES * 0x1000;

// MANUALLY SYNCED TO ALLOCATIONS ABOVE
// inclusive numbering - we allocate pages from the top-down, so the last number should generally be 31
pub const IFRAM0_RESERVED_PAGE_RANGE: [usize; 2] = [31 - 4, 31];
pub const IFRAM1_RESERVED_PAGE_RANGE: [usize; 2] = [31 - 0, 31];

/// returns the USB SE0 port and pin number
const SE0_PIN: u8 = 14;
pub fn setup_usb_pins<T: IoSetup + IoGpio>(iox: &T) -> (IoxPort, u8) {
iox.setup_pin(
IoxPort::PB,
SE0_PIN,
Some(IoxDir::Output),
Some(IoxFunction::Gpio),
None,
None,
Some(IoxEnable::Enable),
Some(IoxDriveStrength::Drive2mA),
);
iox.set_gpio_pin_value(IoxPort::PB, SE0_PIN, IoxValue::Low);
(IoxPort::PB, SE0_PIN)
}

pub fn setup_i2c_pins(iox: &dyn IoSetup) -> crate::udma::I2cChannel {
// I2C_SCL_B[0]
iox.setup_pin(
IoxPort::PB,
11,
Some(IoxDir::Output),
Some(IoxFunction::AF1),
None,
None,
Some(IoxEnable::Enable),
Some(IoxDriveStrength::Drive2mA),
);
// I2C_SDA_B[0]
iox.setup_pin(
IoxPort::PB,
12,
Some(IoxDir::Output),
Some(IoxFunction::AF1),
Some(IoxEnable::Enable),
None,
Some(IoxEnable::Enable),
Some(IoxDriveStrength::Drive2mA),
);
crate::udma::I2cChannel::Channel0
}
4 changes: 4 additions & 0 deletions libs/cramium-hal/src/board/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ pub use baosec::*;
pub mod baosor;
#[cfg(feature = "board-baosor")]
pub use baosor::*;
#[cfg(feature = "board-dabao")]
pub mod dabao;
#[cfg(feature = "board-dabao")]
pub use dabao::*;
3 changes: 3 additions & 0 deletions libs/cramium-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

#[macro_use]
pub mod debug;
#[cfg(feature = "axp2101")]
pub mod axp2101;
pub mod board;
pub mod ifram;
pub mod iox;
#[cfg(feature = "camera-ov2640")]
pub mod ov2640;
pub mod sce;
#[cfg(feature = "display-sh1107")]
pub mod sh1107;
pub mod shared_csr;
pub mod udma;
Expand Down
16 changes: 16 additions & 0 deletions libs/cramium-hal/src/sce/trng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const RAW_ENTRIES: usize = 16;
/// for at least this many cycles before the next sample is taken, thus
/// making it more difficult for any adversary to reason about the current
/// state of the TRNG given the QC samples.
#[allow(dead_code)]
const RAW_GUARDBAND: usize = 32;

pub struct Trng {
Expand All @@ -92,6 +93,9 @@ pub struct Trng {
mode: Mode,
/// Buffer some raw entropy inside the kernel, so we can "burst out" entropy
/// for reseed operations without having to wait for the TRNG to regenerate data.
///
/// This generates an unused warning when `verilator-only` is selected. We leave this
/// warning in place to remind ourselves that the TRNG is not real.
raw: [Option<u32>; RAW_ENTRIES],
#[cfg(feature = "compress-entropy")]
rng_var: u8,
Expand All @@ -110,6 +114,7 @@ impl Trng {
}
}

#[cfg(not(feature = "verilator-only"))]
pub fn setup_raw_generation(&mut self, count: u16) {
self._count = count;
self.mode = Mode::Raw;
Expand Down Expand Up @@ -137,6 +142,10 @@ impl Trng {
self.csr.wo(utra::trng::SFR_OPT, 0);
}

#[cfg(feature = "verilator-only")]
pub fn setup_raw_generation(&mut self, _count: u16) { self.mode = Mode::Raw; }

#[cfg(not(feature = "verilator-only"))]
pub fn get_raw(&mut self) -> u32 {
// Pull from the buffered entropy pool, until it's empty.
for d in self.raw.iter_mut() {
Expand Down Expand Up @@ -217,6 +226,13 @@ impl Trng {
self.raw[0].take().unwrap()
}

#[cfg(feature = "verilator-only")]
pub fn get_raw(&mut self) -> u32 {
// guaranteed random by fair dice roll
// (in all seriousness, this is intentional in verilator because there is no TRNG in the RTL model)
4
}

pub fn get_u32(&mut self) -> Option<u32> {
match self.mode {
Mode::Uninit => None,
Expand Down
8 changes: 4 additions & 4 deletions libs/xous-pl230/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
log = "0.4.14"
utralib = { version = "0.1.25", optional = true, default-features = false }
bitfield = "0.13.2"
xous-pio = { path = "../xous-pio", optional = true, default-features = false }
# xous-pio = { path = "../xous-pio", optional = true, default-features = false }
pio-proc = "0.2.2"
pio = "0.2.1"
cramium-hal = { path = "../cramium-hal", optional = true, default-features = false }
Expand All @@ -23,10 +23,10 @@ cramium-fpga = ["utralib/cramium-fpga"]
precursor = []
hosted = []
renode = []
pio = ["xous-pio"]
pio = []
dma-mainram = []
baremetal = []
debug-print = []

tests = ["pio", "cramium-hal"]
default = ["tests", "pio"]
tests = ["cramium-hal"]
default = ["tests"]
32 changes: 15 additions & 17 deletions loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ xous-pl230 = { path = "../libs/xous-pl230", optional = true, features = [
"baremetal",
# "dma-mainram", # PL-230 option - this fails on MPW
] }
xous-pio = { path = "../libs/xous-pio", optional = true, features = [
"tests",
"cramium-soc",
"baremetal",
] }
# xous-pio = { path = "../libs/xous-pio", optional = true, features = [
# "tests",
# "cramium-soc",
# "baremetal",
# ] }
aes-gcm-siv = { version = "0.11.1", default-features = false, features = [
"aes",
], optional = true }
Expand Down Expand Up @@ -79,29 +79,25 @@ renode = [
cramium-soc = [
"utralib/cramium-soc",
"debug-print",
"simulation-only",
"cramium-hal",
"xous-pio",
"xous-pl230",
"rand_chacha",
"sram-margin",
# "boot-delay",
]
board-baosec = [
"cramium-hal/board-baosec",
"updates",
"usb",
"libm",
"simple-fatfs",
"linked_list_allocator",
"ed25519-dalek",
"sha2",
"digest",
# "boot-delay",
]
board-baosec = ["cramium-hal/board-baosec"]
board-baosor = ["cramium-hal/board-baosor"]
board-dabao = ["cramium-hal/board-dabao"]
cramium-fpga = [
"utralib/cramium-fpga",
"debug-print",
"simulation-only",
"cramium-hal",
]
cramium-fpga = ["utralib/cramium-fpga", "debug-print", "cramium-hal"]
atsama5d27 = ["utralib/atsama5d27", "armv7", "dep:atsama5d27"]

# precursor flags
Expand All @@ -118,6 +114,7 @@ board-bringup = []
sram-margin = []
boot-delay = []
simulation-only = []
verilator-only = []
quantum-timer-test = ["pio", "pio-proc"]
spim-test = []
irq-test = []
Expand All @@ -129,10 +126,11 @@ clock-tests = []
reset-debug = []
usb = []
qr = ["nalgebra"]
updates = []

# general flags
debug-print = []
earlyprintk = []
resume = [] # suspend/resume pathway code

default = ["board-bringup", "dump-trng", "usb"]
default = []
2 changes: 1 addition & 1 deletion loader/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub extern "C" fn _start(_kernel_args: usize, loader_sig: usize) {
// Stub for clearing IFRAM & RAM on Cramium target. This is required
// to clear the parity check bits, which are randomly set on boot. System will
// eventually hang if these bits aren't cleared.
#[cfg(any(feature = "cramium-soc", feature = "cramium-fpga"))]
#[cfg(all(any(feature = "cramium-soc", feature = "cramium-fpga"), not(feature = "simulation-only")))]
unsafe {
#[rustfmt::skip]
asm! (
Expand Down
9 changes: 7 additions & 2 deletions loader/src/bootconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,15 @@ impl BootConfig {
(self.sram_size - self.init_size - self.extra_pages * PAGE_SIZE) / mem::size_of::<usize>(),
)
};
assert!((val as usize) >= (self.sram_start as usize));
assert!(
(val as usize) >= (self.sram_start as usize),
"val {:x} ! >= sram_start {:x}",
val as usize,
self.sram_start as usize
);
assert!(
(val as usize) < (self.sram_start as usize) + self.sram_size,
"top address {:08x} > (start + size) {:08x} + {:08x} = {:08x}",
"top address {:08x} ! < (start + size) {:08x} + {:08x} = {:08x}",
val as usize,
self.sram_start as usize,
self.sram_size,
Expand Down
Loading

0 comments on commit 4cf9c96

Please sign in to comment.