Skip to main content

crash/
lib.rs

1//! The XCR1 crash report format. Capture writes to caller-provided storage.
2
3#![no_std]
4
5mod boot_guard;
6mod record;
7
8pub use boot_guard::{BOOT_GUARD_SIZE, BootGuard, CRASH_LOOP_LIMIT};
9pub use record::*;
10
11#[cfg(feature = "persistence")]
12mod persistence;
13#[cfg(feature = "persistence")]
14pub use persistence::*;
15
16use core::sync::atomic::{Ordering, compiler_fence, fence};
17
18fn checksum(bytes: &[u8]) -> u32 {
19    let mut checksum = u32::MAX;
20    for &byte in bytes {
21        checksum ^= u32::from(byte);
22        for _ in 0..8 {
23            checksum = (checksum >> 1) ^ (0xEDB8_8320 & 0u32.wrapping_sub(checksum & 1));
24        }
25    }
26    !checksum
27}
28
29fn write_marker(output: &mut [u8], marker: [u8; 4]) {
30    compiler_fence(Ordering::SeqCst);
31    fence(Ordering::SeqCst);
32    for (byte, value) in output[..4].iter_mut().zip(marker) {
33        // SAFETY: The byte is within the exclusive output slice.
34        unsafe { core::ptr::write_volatile(byte, value) };
35    }
36    fence(Ordering::SeqCst);
37    compiler_fence(Ordering::SeqCst);
38}
39
40#[cfg(test)]
41mod tests {
42    #[test]
43    fn ieee_crc_32_check_value() {
44        assert_eq!(super::checksum(b"123456789"), 0xCBF4_3926);
45    }
46}