80 lines
2.9 KiB
Rust
80 lines
2.9 KiB
Rust
// SPDX-License-Identifier: MIT
|
|
//! Exercise the SDK's host-only BM1366 HAL simulator with a captured job.
|
|
|
|
use alkane_os::hal_wire::HalClient;
|
|
use alkane_os::pow::verify_sha256_report;
|
|
use alkane_os::HardwareBackend;
|
|
use std::error::Error;
|
|
|
|
const HEADER: [u8; 80] = [
|
|
0x00, 0x00, 0x00, 0x20, 0xfc, 0x3a, 0xb8, 0xd4, 0x30, 0x21, 0xf4, 0x5e, 0xb6, 0x62, 0x38, 0x9d,
|
|
0x0f, 0x79, 0x1f, 0x65, 0x7d, 0xbf, 0x75, 0xa5, 0xcb, 0x93, 0x67, 0x0b, 0x57, 0xb6, 0xc2, 0x25,
|
|
0x03, 0xb4, 0x21, 0x3a, 0x43, 0xae, 0x10, 0x52, 0xeb, 0xd4, 0x44, 0x43, 0x06, 0x8f, 0xe1, 0x78,
|
|
0xe2, 0xc6, 0x1c, 0xa4, 0x99, 0x48, 0x46, 0xfd, 0x13, 0x3a, 0xdd, 0xb0, 0x4a, 0xab, 0x2b, 0x38,
|
|
0x2c, 0x5b, 0x4c, 0x74, 0xc1, 0x01, 0x2e, 0x6a, 0x8f, 0x06, 0x02, 0x17, 0x00, 0x00, 0x00, 0x00,
|
|
];
|
|
|
|
fn crc16(bytes: &[u8]) -> u16 {
|
|
let mut value = 0xffff_u16;
|
|
for byte in bytes {
|
|
value ^= u16::from(*byte) << 8;
|
|
for _ in 0..8 {
|
|
value = if value & 0x8000 != 0 {
|
|
(value << 1) ^ 0x1021
|
|
} else {
|
|
value << 1
|
|
};
|
|
}
|
|
}
|
|
value
|
|
}
|
|
|
|
fn reverse_words(input: &[u8]) -> [u8; 32] {
|
|
let mut output = [0_u8; 32];
|
|
for index in 0..8 {
|
|
output[index * 4..index * 4 + 4].copy_from_slice(&input[(7 - index) * 4..(8 - index) * 4]);
|
|
}
|
|
output
|
|
}
|
|
|
|
fn captured_job() -> [u8; 88] {
|
|
let mut frame = [0_u8; 88];
|
|
frame[..4].copy_from_slice(&[0x55, 0xaa, 0x21, 0x54]);
|
|
frame[4] = 0x18;
|
|
frame[10..14].copy_from_slice(&HEADER[72..76]);
|
|
frame[14..18].copy_from_slice(&HEADER[68..72]);
|
|
frame[18..50].copy_from_slice(&reverse_words(&HEADER[36..68]));
|
|
frame[50..82].copy_from_slice(&reverse_words(&HEADER[4..36]));
|
|
frame[82..86].copy_from_slice(&HEADER[..4]);
|
|
let crc = crc16(&frame[2..86]);
|
|
frame[86..88].copy_from_slice(&crc.to_be_bytes());
|
|
frame
|
|
}
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
let path = std::env::args()
|
|
.nth(1)
|
|
.ok_or("usage: simulator_replay_probe SOCKET_PATH")?;
|
|
let mut backend = HalClient::connect(path)?;
|
|
backend.set_psu_enabled(true)?;
|
|
backend.set_chain_reset(0, true)?;
|
|
backend.set_chain_reset(0, false)?;
|
|
backend.send_frame(0, &captured_job())?;
|
|
let mut buffer = [0_u8; 11];
|
|
let length = backend.receive_frame(0, &mut buffer)?;
|
|
if length != 11 || buffer[..2] != [0xaa, 0x55] || buffer[10] & 0x80 == 0 {
|
|
return Err("missing BM1366 nonce report".into());
|
|
}
|
|
let nonce = u32::from_le_bytes(buffer[2..6].try_into()?);
|
|
let version_field = u16::from_be_bytes(buffer[8..10].try_into()?);
|
|
let mut target = [0_u8; 32];
|
|
target[26..28].fill(0xff);
|
|
let result = verify_sha256_report(&HEADER, nonce, version_field, &target);
|
|
backend.set_psu_enabled(false)?;
|
|
if !result.meets_target {
|
|
return Err("captured nonce does not meet difficulty 1".into());
|
|
}
|
|
println!("BM1366 captured report verified at difficulty 1");
|
|
Ok(())
|
|
}
|