From 704682b0ae31f908ed4467815ec0fae5f2ca668c Mon Sep 17 00:00:00 2001 From: ede1998 Date: Thu, 27 Aug 2026 21:59:30 +0200 Subject: [PATCH] Ensure RawCard size matches flash alignment requirements, add debug logs --- Cargo.lock | 1 + Cargo.toml | 1 + src/card/decoder.rs | 1 + src/card/model.rs | 3 +++ src/storage/cardstore.rs | 21 +++++++++++---------- src/storage/store.rs | 1 + 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8038235..b278b60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -375,6 +375,7 @@ dependencies = [ "log", "num_enum", "pn532", + "static_assertions", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index d0f6ded..7ac5eb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,7 @@ ch1115 = { version = "0.1.2", features = ["graphics"] } esp-storage = "0.9.0" embedded-storage = "0.3.1" binary_serde = "1.0.25" +static_assertions = { version = "1.1.0", default-features = false } [build-dependencies] log = "0.4.27" diff --git a/src/card/decoder.rs b/src/card/decoder.rs index b99b23d..2889684 100644 --- a/src/card/decoder.rs +++ b/src/card/decoder.rs @@ -17,6 +17,7 @@ pub fn split_nfc_hex(payload: &[u8]) -> Option { packed_card_text: payload[0x2DB..0x311].try_into().ok()?, secret: payload[0x311..0x329].try_into().ok()?, _opaque_trailer: payload[0x329..0x35A].try_into().ok()?, + _padding: Default::default(), }) } diff --git a/src/card/model.rs b/src/card/model.rs index 4feaa86..56debfb 100644 --- a/src/card/model.rs +++ b/src/card/model.rs @@ -19,8 +19,11 @@ pub struct RawCard { pub packed_card_text: [u8; 54], pub secret: [u8; 24], pub _opaque_trailer: [u8; 49], + pub _padding: [u8; 2], } +static_assertions::const_assert!(RawCard::SERIALIZED_SIZE.is_multiple_of(4)); + #[derive(Debug, Clone, Eq, PartialEq, FromPrimitive)] #[repr(u8)] pub enum CardType { diff --git a/src/storage/cardstore.rs b/src/storage/cardstore.rs index 8b69c54..15ce0f9 100644 --- a/src/storage/cardstore.rs +++ b/src/storage/cardstore.rs @@ -17,6 +17,17 @@ const COUNT_REGION: MemoryRegion = MemoryRegion { size: size_of::(), }; +const ALLOCATION_TABLE_REGION: MemoryRegion = MemoryRegion { + offset: COUNT_REGION.end() as u32, + size: MAX_CARDS * AllocationTableEntry::SERIALIZED_SIZE, +}; + +const CARDS_REGION: MemoryRegion = MemoryRegion { + offset: ALLOCATION_TABLE_REGION.end() as u32, + size: CARDSTORE_REGION.size - ALLOCATION_TABLE_REGION.end(), +}; + + #[derive(Debug, BinarySerde, PartialEq, Eq, Clone)] #[repr(C)] pub struct AllocationTableEntry { @@ -52,16 +63,6 @@ impl AllocationTableEntry { } } -const ALLOCATION_TABLE_REGION: MemoryRegion = MemoryRegion { - offset: COUNT_REGION.end() as u32, - size: MAX_CARDS * AllocationTableEntry::SERIALIZED_SIZE, -}; - -const CARDS_REGION: MemoryRegion = MemoryRegion { - offset: ALLOCATION_TABLE_REGION.end() as u32, - size: CARDSTORE_REGION.size - ALLOCATION_TABLE_REGION.end(), -}; - #[derive(Debug)] pub enum CardStoreError { Store(StoreError), diff --git a/src/storage/store.rs b/src/storage/store.rs index fdd0a02..37d3563 100644 --- a/src/storage/store.rs +++ b/src/storage/store.rs @@ -39,6 +39,7 @@ impl Store { } pub fn write(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), StoreError> { + log::debug!("Writing {} bytes at offset 0x{offset:08x}", bytes.len()); self.storage.write(FLASH_ADDR + offset, bytes) } }