Skip to content

Commit

Permalink
Remove static device UUID
Browse files Browse the repository at this point in the history
This patch removes the static mut device UUID.  This has multiple
benefits:  It removes some unsafe code, it makes the initialization
order explicit and it makes the code compatible with Rust edition 2024.
  • Loading branch information
robin-nitrokey committed Apr 17, 2024
1 parent 08dec77 commit b9b1b64
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 36 deletions.
2 changes: 2 additions & 0 deletions components/boards/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub fn init_usb_nfc<B: Board>(
}

pub fn init_apps<B: Board>(
soc: &B::Soc,
trussed: &mut Trussed<B>,
init_status: InitStatus,
store: &RunnerStore<B>,
Expand Down Expand Up @@ -177,6 +178,7 @@ pub fn init_apps<B: Board>(
};

let runner = Runner {
uuid: *soc.uuid(),
is_efs_available: !nfc_powered,
_marker: Default::default(),
};
Expand Down
5 changes: 3 additions & 2 deletions components/boards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use rand_chacha::ChaCha8Rng;
use trussed::{client::Syscall, Platform};

use crate::{
soc::Soc,
soc::{Soc, Uuid},
store::{RunnerStore, StoragePointers},
ui::{buttons::UserPresence, rgb_led::RgbLed, UserInterface},
};
Expand Down Expand Up @@ -76,6 +76,7 @@ pub trait Board: StoragePointers {
}

pub struct Runner<B> {
pub uuid: Uuid,
pub is_efs_available: bool,
pub _marker: PhantomData<B>,
}
Expand All @@ -90,7 +91,7 @@ impl<B: Board> apps::Runner for Runner<B> {
type Se050Timer = B::Se050Timer;

fn uuid(&self) -> [u8; 16] {
*B::Soc::device_uuid()
self.uuid
}

fn is_efs_available(&self) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion components/boards/src/soc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ pub trait Soc: Reboot + 'static {
const SOC_NAME: &'static str;
const VARIANT: Variant;

fn device_uuid() -> &'static Uuid;
fn uuid(&self) -> &Uuid;
}
21 changes: 13 additions & 8 deletions components/boards/src/soc/lpc55.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ use lpc55_hal::{
pub mod clock_controller;
pub mod monotonic;

pub static mut DEVICE_UUID: Uuid = [0u8; 16];

type UsbPeripheral = lpc55_hal::peripherals::usbhs::EnabledUsbhsDevice;

pub struct Lpc55;
pub struct Lpc55 {
uuid: Uuid,
}

impl Lpc55 {
pub fn new() -> Self {
Self {
uuid: lpc55_hal::uuid(),
}
}
}

impl Soc for Lpc55 {
type UsbBus = lpc55_hal::drivers::UsbBus<UsbPeripheral>;
Expand All @@ -33,11 +41,8 @@ impl Soc for Lpc55 {
const SOC_NAME: &'static str = "lpc55";
const VARIANT: Variant = Variant::Lpc55;

fn device_uuid() -> &'static Uuid {
#[allow(static_mut_refs)]
unsafe {
&DEVICE_UUID
}
fn uuid(&self) -> &Uuid {
&self.uuid
}
}

Expand Down
25 changes: 12 additions & 13 deletions components/boards/src/soc/nrf52.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use rtic_monotonic::{RtcDuration, RtcMonotonic};
pub mod flash;
pub mod rtic_monotonic;

static mut DEVICE_UUID: Uuid = [0u8; 16];

pub struct Nrf52;
pub struct Nrf52 {
uuid: Uuid,
}

impl Soc for Nrf52 {
type UsbBus = Usbd<UsbPeripheral<'static>>;
Expand All @@ -27,11 +27,8 @@ impl Soc for Nrf52 {
const SOC_NAME: &'static str = "nrf52";
const VARIANT: Variant = Variant::Nrf52;

fn device_uuid() -> &'static Uuid {
#[allow(static_mut_refs)]
unsafe {
&DEVICE_UUID
}
fn uuid(&self) -> &Uuid {
&self.uuid
}
}

Expand Down Expand Up @@ -60,13 +57,13 @@ pub fn init_bootup(
ficr: &nrf52840_pac::FICR,
uicr: &nrf52840_pac::UICR,
power: &mut nrf52840_pac::POWER,
) {
) -> Nrf52 {
let deviceid0 = ficr.deviceid[0].read().bits();
let deviceid1 = ficr.deviceid[1].read().bits();
unsafe {
DEVICE_UUID[0..4].copy_from_slice(&deviceid0.to_be_bytes());
DEVICE_UUID[4..8].copy_from_slice(&deviceid1.to_be_bytes());
}

let mut uuid = Uuid::default();
uuid[0..4].copy_from_slice(&deviceid0.to_be_bytes());
uuid[4..8].copy_from_slice(&deviceid1.to_be_bytes());

info!("RESET Reason: {:x}", power.resetreas.read().bits());
power.resetreas.write(|w| w);
Expand Down Expand Up @@ -109,6 +106,8 @@ pub fn init_bootup(
} else {
info!("UICR APPROTECT is DISABLED!");
};

Nrf52 { uuid }
}

type UsbClockType = Clocks<
Expand Down
3 changes: 2 additions & 1 deletion runners/embedded/src/bin/app-nrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod app {

boards::init::init_logger::<Board>(VERSION_STRING);

nrf52::init_bootup(&ctx.device.FICR, &ctx.device.UICR, &mut ctx.device.POWER);
let soc = nrf52::init_bootup(&ctx.device.FICR, &ctx.device.UICR, &mut ctx.device.POWER);

let mut board_gpio = nk3am::init_pins(ctx.device.GPIOTE, ctx.device.P0, ctx.device.P1);

Expand Down Expand Up @@ -103,6 +103,7 @@ mod app {
);

let apps = boards::init::init_apps(
&soc,
&mut trussed,
init_status,
&store,
Expand Down
12 changes: 2 additions & 10 deletions runners/embedded/src/nk3xn/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use boards::{
ButtonsTimer, InternalFlashStorage, NK3xN, PwmTimer, I2C,
},
soc::{
lpc55::{clock_controller::DynamicClockController, Lpc55, DEVICE_UUID},
lpc55::{clock_controller::DynamicClockController, Lpc55},
Soc,
},
store::{self, RunnerStore},
Expand Down Expand Up @@ -141,15 +141,6 @@ impl Stage0 {

#[inline(never)]
pub fn next(mut self, iocon: hal::Iocon<Unknown>, gpio: hal::Gpio<Unknown>) -> Stage1 {
unsafe {
DEVICE_UUID.copy_from_slice(&hal::uuid());
#[cfg(feature = "alpha")]
{
DEVICE_UUID[14] = 0xa1;
DEVICE_UUID[15] = 0xfa;
}
};

let mut iocon = iocon.enabled(&mut self.peripherals.syscon);
let mut gpio = gpio.enabled(&mut self.peripherals.syscon);

Expand Down Expand Up @@ -801,6 +792,7 @@ impl Stage6 {
pub fn next(mut self, usbhs: Usbhs<Unknown>) -> All {
self.perform_data_migrations();
let apps = init::init_apps(
&Lpc55::new(),
&mut self.trussed,
self.status,
&self.store,
Expand Down
3 changes: 2 additions & 1 deletion runners/nkpk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod app {

boards::init::init_logger::<Board>(VERSION_STRING);

nrf52::init_bootup(&ctx.device.FICR, &ctx.device.UICR, &mut ctx.device.POWER);
let soc = nrf52::init_bootup(&ctx.device.FICR, &ctx.device.UICR, &mut ctx.device.POWER);

let board_gpio = nkpk::init_pins(ctx.device.GPIOTE, ctx.device.P0, ctx.device.P1);

Expand Down Expand Up @@ -92,6 +92,7 @@ mod app {
boards::init::init_trussed(&mut dev_rng, store, user_interface, &mut init_status);

let apps = boards::init::init_apps(
&soc,
&mut trussed,
init_status,
&store,
Expand Down

0 comments on commit b9b1b64

Please sign in to comment.