Skip to content

Commit

Permalink
xtask/starfive/visionfive2: rework header to using structs
Browse files Browse the repository at this point in the history
This is based on what the vendor tool does.
No clue how the secure boot flow or setup really works.
Reference: starfive-tech/Tools#8

Signed-off-by: Daniel Maslowski <[email protected]>
  • Loading branch information
orangecms committed Feb 9, 2024
1 parent 53a8983 commit d100b0a
Showing 1 changed file with 93 additions and 22 deletions.
115 changes: 93 additions & 22 deletions xtask/src/starfive/visionfive2_hdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,49 @@ const BACKUP_OFFSET_BYTES: [u8; 4] = BACKUP_OFFSET.to_le_bytes();
const HEADER_SIZE: u32 = 0x0400;
const HEADER_SIZE_BYTES: [u8; 4] = HEADER_SIZE.to_le_bytes();

/* this is a shortcut */
const VERSION_OFFSET: usize = HEADER_OFFSET as usize + 0x0044;
#[repr(C)]
struct Version {
major: u8,
minor: u8,
revision: u16,
}

#[repr(C)]
struct JH7110CommonHeader {
// the offset to the other header
size: u32,
backup_offset: u32,
_unknown1: u64,
_skip1: [u8; 48],
// 0x040
ec_param_p: [u8; 32],
ec_param_a: [u8; 32],
ec_param_b: [u8; 32],
ec_param_gx: [u8; 32],
ec_param_gy: [u8; 32],
ec_param_n: [u8; 32],
_skip2: [u8; 64],
// 0x140
ec_key_1: [u8; 64],
ec_key_2: [u8; 64],
ec_key_3: [u8; 64],
ec_key_4: [u8; 64],
}

/* version: shall be 0x01010101
* (from https://doc-en.rvspace.org/VisionFive2/SWTRM/VisionFive2_SW_TRM/create_spl.html) */
const VERSION: u32 = 0x0101_0101;
const VERSION_BYTES: [u8; 4] = VERSION.to_le_bytes();
#[repr(C)]
struct JH7110SBLHeader {
// NOTE: there may be other optional fields here; unclear
_skip1: [u8; 0x40],
ec_key_select: u32,
version: Version,
payload_size: u32,
header_size: u32,
checksum: u32,
aes_iv: [u8; 16],
ec_key_revoke: [u8; 32],
sbl_ver_cipher: [u8; 16],
}

// The use of a packed struct is kind of pointless.
// Just emit the proper things in the proper order.
// Push them into the output.
// Also, let's get real: there are no big-endian machines left. Assume LE.
pub fn spl_create_hdr(dat: Vec<u8>) -> Vec<u8> {
/*
// need to find out which one to use, but it's not this one.
Expand All @@ -58,20 +89,60 @@ pub fn spl_create_hdr(dat: Vec<u8>) -> Vec<u8> {
let crcout = digest.finalize();
}
*/
let v = crc32(CRC_IV, CRC_SV, dat.clone());
let checksum: [u8; 4] = v.to_le_bytes();
let data_len: [u8; 4] = (dat.len() as u32).to_le_bytes();
let checksum = crc32(CRC_IV, CRC_SV, dat.clone());

let common_header = JH7110CommonHeader {
size: HEADER_OFFSET,
backup_offset: BACKUP_OFFSET,
_unknown1: 0,
_skip1: [0u8; 0x30],
ec_param_p: [0u8; 0x20],
ec_param_a: [0u8; 0x20],
ec_param_b: [0u8; 0x20],
ec_param_gx: [0u8; 0x20],
ec_param_gy: [0u8; 0x20],
ec_param_n: [0u8; 0x20],
_skip2: [0u8; 0x40],
ec_key_1: [0u8; 0x40],
ec_key_2: [0u8; 0x40],
ec_key_3: [0u8; 0x40],
ec_key_4: [0u8; 0x40],
};

let sbl_header = JH7110SBLHeader {
_skip1: [0u8; 0x40],
// TODO: This needs to be a parameter for "secure boot" setup.
// 4 slots are available; 0 means use none, 1 means first, 4 is maximum.
ec_key_select: 0,
/* version: shall be 0x01010101
* https://doc-en.rvspace.org/VisionFive2/SWTRM/VisionFive2_SW_TRM/create_spl.html */
version: Version {
major: 1,
minor: 1,
revision: 0x101,
},
payload_size: dat.len() as u32,
header_size: HEADER_SIZE,
checksum,
aes_iv: [0u8; 16],
ec_key_revoke: [0u8; 32],
sbl_ver_cipher: [0u8; 16],
};

let mut hdr = vec![];
hdr.extend_from_slice(&HEADER_OFFSET_BYTES);
hdr.extend_from_slice(&BACKUP_OFFSET_BYTES);
hdr.resize(HEADER_OFFSET as usize, 0);
// NOTE: there are apparently other optional fields, unused here as of now
hdr.resize(VERSION_OFFSET, 0);
hdr.extend_from_slice(&VERSION_BYTES);
hdr.extend_from_slice(&data_len);
hdr.extend_from_slice(&HEADER_SIZE_BYTES);
hdr.extend_from_slice(&checksum);

let ch = unsafe {
std::slice::from_raw_parts(
(&common_header as *const JH7110CommonHeader) as *const u8,
0x240,
)
};
let sh = unsafe {
std::slice::from_raw_parts((&sbl_header as *const JH7110SBLHeader) as *const u8, 0x94)
};
hdr.extend_from_slice(ch);
hdr.extend_from_slice(sh);

hdr.resize(HEADER_SIZE as usize, 0);
hdr.extend(&dat);
hdr
Expand Down

0 comments on commit d100b0a

Please sign in to comment.