Skip to content

Commit

Permalink
xtask/starfive/visionfive2: simplify header creation
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Maslowski <info@orangecms.org>
  • Loading branch information
orangecms committed Jan 29, 2024
1 parent 30ee2fc commit 9be390d
Showing 1 changed file with 30 additions and 56 deletions.
86 changes: 30 additions & 56 deletions xtask/src/starfive/visionfive2_hdr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const CRC_IV: u32 = 0xffff_ffff;
const CRC_SV: u32 = 0x04c1_1db7;

// It is not clear what CRC starfive had in mind, they wrote their own ...
fn crc32_reverse(x: u32) -> u32 {
let mut x = x;
Expand All @@ -20,40 +23,31 @@ fn crc32(iv: u32, sv: u32, data: Vec<u8>) -> u32 {
byte <<= 1;
}
}

crc
}

fn crc32_final(iv: u32) -> u32 {
crc32_reverse(iv ^ !0u32)
crc32_reverse(crc ^ iv)
}

/* version: shall be 0x01010101
* (from https://doc-en.rvspace.org/VisionFive2/SWTRM/VisionFive2_SW_TRM/create_spl.html) */
const VERSION: u32 = 0x0101_0101;
// Offset of backup SBL from Flash info start (from input_sbl_normal.cfg)
const BACKUP: u32 = 0x20_0000;
// offset of spl header: 64+256+256 = 0x240
const HEADER_OFFSET: u32 = 0x0240;
const HEADER_OFFSET_BYTES: [u8; 4] = HEADER_OFFSET.to_le_bytes();
// Offset of backup SBL from Flash info start (from input_sbl_normal.cfg)
const BACKUP_OFFSET: u32 = 0x20_0000;
const BACKUP_OFFSET_BYTES: [u8; 4] = BACKUP_OFFSET.to_le_bytes();
/* Offset from HDR to SPL_IMAGE, 0x400 (00 04 00 00) currently */
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 + 0x0044;

const CRC_IV: u32 = !0;
const CRC_SV: u32 = 0x04c11db7;
/* 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();

// The use of a packed struct is kind of pointless. Just emit the proper things in the proper order.
// 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.
// uint32_t sofs; /* offset of spl header: 64+256+256 = 0x240 */
// uint32_t bofs; /* SBL_BAK_OFFSET: Offset of backup SBL from Flash info start (from input_sbl_normal.cfg) */
// uint8_t zro2[636];
// uint32_t vers; /* version: shall be 0x01010101
// * (from https://doc-en.rvspace.org/VisionFive2/SWTRM/VisionFive2_SW_TRM/create_spl.html) */
// uint32_t fsiz; /* u-boot-spl.bin size in bytes */
// uint32_t res1; /* Offset from HDR to SPL_IMAGE, 0x400 (00 04 00 00) currently */
// uint32_t crcs; /* CRC32 of u-boot-spl.bin */
// uint8_t zro3[364];

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 @@ -65,40 +59,20 @@ pub fn spl_create_hdr(dat: Vec<u8>) -> Vec<u8> {
}
*/
let v = crc32(CRC_IV, CRC_SV, dat.clone());
let fv = crc32_final(v);

let mut hdr = vec![];

let spl_header_offset: [u8; 4] = HEADER_OFFSET.to_le_bytes();
hdr.extend_from_slice(&spl_header_offset);

let backup: [u8; 4] = BACKUP.to_le_bytes();
hdr.extend_from_slice(&backup);

hdr.resize(hdr.len() + 636, 0);

let version: [u8; 4] = VERSION.to_le_bytes();
hdr.extend_from_slice(&version);

let checksum: [u8; 4] = v.to_le_bytes();
let data_len: [u8; 4] = (dat.len() as u32).to_le_bytes();
hdr.extend_from_slice(&data_len); /* u-boot-spl.bin size in bytes */

println!("boot blob size: {}", dat.len());

let data_offset: [u8; 4] = HEADER_SIZE.to_le_bytes();
hdr.extend_from_slice(&data_offset);

/* CRC32 of dat */
let l: [u8; 4] = fv.to_le_bytes();
hdr.extend_from_slice(&l);
// fill up to HEADER_SIZE
hdr.resize(hdr.len() + 364, 0);

assert!(
hdr.len() == HEADER_SIZE as usize,
"hdr is {:x} bytes, not {HEADER_SIZE}",
hdr.len()
);
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);
hdr.resize(HEADER_SIZE as usize, 0);
hdr.extend(&dat);
hdr
}
Expand Down

0 comments on commit 9be390d

Please sign in to comment.