From afba3040b30515aa4f1d6462ae2bf3d485f02f25 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 25 Sep 2025 16:13:25 +0800 Subject: [PATCH] cleanup print debugs in sha2 for bootloader --- bao1x-boot/sha2-bao1x/Cargo.toml | 1 + bao1x-boot/sha2-bao1x/src/lib.rs | 1 + bao1x-boot/sha2-bao1x/src/sha256.rs | 16 +++++++++++----- bao1x-boot/sha2-bao1x/src/sha512.rs | 16 +++++++++++----- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/bao1x-boot/sha2-bao1x/Cargo.toml b/bao1x-boot/sha2-bao1x/Cargo.toml index 2dbdeb32f..cc4026a65 100644 --- a/bao1x-boot/sha2-bao1x/Cargo.toml +++ b/bao1x-boot/sha2-bao1x/Cargo.toml @@ -21,5 +21,6 @@ oid = [] precursor = ["utralib/precursor"] renode = ["utralib/renode"] std = [] +debug = [] # This crate is specific to the bao1x. default = ["bao1x"] diff --git a/bao1x-boot/sha2-bao1x/src/lib.rs b/bao1x-boot/sha2-bao1x/src/lib.rs index 25f36cb47..d030cd23c 100644 --- a/bao1x-boot/sha2-bao1x/src/lib.rs +++ b/bao1x-boot/sha2-bao1x/src/lib.rs @@ -17,6 +17,7 @@ use digest::{ }; mod core_api; +#[cfg(feature = "debug")] mod debug; mod sha256; mod sha512; diff --git a/bao1x-boot/sha2-bao1x/src/sha256.rs b/bao1x-boot/sha2-bao1x/src/sha256.rs index 47a02ea5b..0faa02f8c 100644 --- a/bao1x-boot/sha2-bao1x/src/sha256.rs +++ b/bao1x-boot/sha2-bao1x/src/sha256.rs @@ -29,11 +29,13 @@ fn compress(csr: &mut CSR, blocks: &[[u8; BLOCK_LEN]]) { const BUF_BLOCKS: usize = utralib::HW_SEG_MSG_MEM_LEN / BLOCK_LEN; // safety: this is the actual location of the message buffer and its length according to the hardware // spec. only safe because this is machine mode and no-std (no threads, no concurrency) + #[cfg(feature = "debug")] let msg_blocks: &mut [[u8; BLOCK_LEN]; BUF_BLOCKS] = unsafe { &mut *(utralib::HW_SEG_MSG_MEM as *mut [[u8; BLOCK_LEN]; BUF_BLOCKS]) }; for block_chunk in blocks.chunks(BUF_BLOCKS) { // block_chunk has a length equal to or less than msg_buf due to .chunks() iterator above + #[cfg(feature = "debug")] for (src, dst) in block_chunk.iter().zip(msg_blocks.iter_mut()) { crate::println!(" {:x?}", src); dst.copy_from_slice(src); @@ -58,9 +60,9 @@ fn compress(csr: &mut CSR, blocks: &[[u8; BLOCK_LEN]]) { .with_seg_result(true) .raw_value(), ); - // csr.wo(utra::combohash::SFR_OPT3, 0); // debug dump + #[cfg(feature = "debug")] for i in 0..utra::combohash::COMBOHASH_NUMREGS { crate::println!("{:x}: {:x}", i * 4, unsafe { (utra::combohash::HW_COMBOHASH_BASE as *const u32).add(i).read_volatile() @@ -78,10 +80,14 @@ fn compress(csr: &mut CSR, blocks: &[[u8; BLOCK_LEN]]) { // clear the flag on exit csr.rmwf(utra::combohash::SFR_FR_MFSM_DONE, 1); - let state = unsafe { core::slice::from_raw_parts(utralib::HW_SEG_HOUT_MEM as *const u32, 16) }; - let state_byte = unsafe { core::slice::from_raw_parts(utralib::HW_SEG_HOUT_MEM as *const u8, 32) }; - crate::println!("state: {:x?}", state); - crate::println!("state_byte: {:x?}", state_byte); + #[cfg(feature = "debug")] + { + let state = unsafe { core::slice::from_raw_parts(utralib::HW_SEG_HOUT_MEM as *const u32, 16) }; + let state_byte = + unsafe { core::slice::from_raw_parts(utralib::HW_SEG_HOUT_MEM as *const u8, 32) }; + crate::println!("state: {:x?}", state); + crate::println!("state_byte: {:x?}", state_byte); + } // clear the first block setting - this was set by the new() function // it does not hurt to clear it every successive block diff --git a/bao1x-boot/sha2-bao1x/src/sha512.rs b/bao1x-boot/sha2-bao1x/src/sha512.rs index 1b9dfb43c..aaa989459 100644 --- a/bao1x-boot/sha2-bao1x/src/sha512.rs +++ b/bao1x-boot/sha2-bao1x/src/sha512.rs @@ -29,11 +29,13 @@ fn compress(csr: &mut CSR, blocks: &[[u8; BLOCK_LEN]]) { const BUF_BLOCKS: usize = utralib::HW_SEG_MSG_MEM_LEN / BLOCK_LEN; // safety: this is the actual location of the message buffer and its length according to the hardware // spec. only safe because this is machine mode and no-std (no threads, no concurrency) + #[cfg(feature = "debug")] let msg_blocks: &mut [[u8; BLOCK_LEN]; BUF_BLOCKS] = unsafe { &mut *(utralib::HW_SEG_MSG_MEM as *mut [[u8; BLOCK_LEN]; BUF_BLOCKS]) }; for block_chunk in blocks.chunks(BUF_BLOCKS) { // block_chunk has a length equal to or less than msg_buf due to .chunks() iterator above + #[cfg(feature = "debug")] for (src, dst) in block_chunk.iter().zip(msg_blocks.iter_mut()) { crate::println!(" {:x?}", src); dst.copy_from_slice(src); @@ -58,9 +60,9 @@ fn compress(csr: &mut CSR, blocks: &[[u8; BLOCK_LEN]]) { .with_seg_result(true) .raw_value(), ); - // csr.wo(utra::combohash::SFR_OPT3, 0); // debug dump + #[cfg(feature = "debug")] for i in 0..utra::combohash::COMBOHASH_NUMREGS { crate::println!("{:x}: {:x}", i * 4, unsafe { (utra::combohash::HW_COMBOHASH_BASE as *const u32).add(i).read_volatile() @@ -78,10 +80,14 @@ fn compress(csr: &mut CSR, blocks: &[[u8; BLOCK_LEN]]) { // clear the flag on exit csr.rmwf(utra::combohash::SFR_FR_MFSM_DONE, 1); - let state = unsafe { core::slice::from_raw_parts(utralib::HW_SEG_HOUT_MEM as *const u32, 16) }; - let state_byte = unsafe { core::slice::from_raw_parts(utralib::HW_SEG_HOUT_MEM as *const u8, 32) }; - crate::println!("state: {:x?}", state); - crate::println!("state_byte: {:x?}", state_byte); + #[cfg(feature = "debug")] + { + let state = unsafe { core::slice::from_raw_parts(utralib::HW_SEG_HOUT_MEM as *const u32, 16) }; + let state_byte = + unsafe { core::slice::from_raw_parts(utralib::HW_SEG_HOUT_MEM as *const u8, 32) }; + crate::println!("state: {:x?}", state); + crate::println!("state_byte: {:x?}", state_byte); + } // clear the first block setting - this was set by the new() function // it does not hurt to clear it every successive block