Update initrd to use 32 bit sizes

This commit is contained in:
pjht 2023-03-30 09:57:28 -05:00
parent 9d077d1f3b
commit 34d3493946
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A

View File

@ -5,16 +5,15 @@ use std::{
path::Path, path::Path,
}; };
const BLOCK_SIZE: u16 = 512; const BLOCK_SIZE: u32 = 512;
const MAX_NAME_LEN: u16 = BLOCK_SIZE - 8; const MAX_NAME_LEN: u32 = BLOCK_SIZE - 4 - 4 - 2 - 1;
fn main() { fn main() {
let mut archive = File::create("initrd").expect("Could not open initrd"); let mut archive = File::create("initrd").expect("Could not open initrd");
for file_path in args().skip(1) { for file_path in args().skip(1) {
let file_path = Path::new(&file_path); let file_path = Path::new(&file_path);
let file_name = file_path.file_name().unwrap(); let file_name = file_path.file_name().unwrap();
let file_name_len = let file_name_len = u32::try_from(file_name.len()).unwrap();
u16::try_from(file_name.len()).expect("File name length greater than 256 bytes");
assert!( assert!(
file_name_len <= MAX_NAME_LEN, file_name_len <= MAX_NAME_LEN,
"File {:?} has name longer than {} bytes", "File {:?} has name longer than {} bytes",
@ -22,8 +21,8 @@ fn main() {
MAX_NAME_LEN MAX_NAME_LEN
); );
let mut file = File::open(file_path).expect("File did not exist"); let mut file = File::open(file_path).expect("File did not exist");
let length = u16::try_from(file.metadata().expect("Could not get file metadata").len()) let length = u32::try_from(file.metadata().expect("Could not get file metadata").len())
.expect("File size greater than 64 KiB"); .expect("File size greater than 4 GiB");
let file_num_blocks = (length / BLOCK_SIZE) + if length % BLOCK_SIZE == 0 { 0 } else { 1 }; let file_num_blocks = (length / BLOCK_SIZE) + if length % BLOCK_SIZE == 0 { 0 } else { 1 };
let mut header_block = Vec::new(); let mut header_block = Vec::new();
header_block.extend_from_slice(&length.to_be_bytes()); header_block.extend_from_slice(&length.to_be_bytes());
@ -37,7 +36,7 @@ fn main() {
.expect("Could not write to initrd"); .expect("Could not write to initrd");
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
let bytes_written = io::copy(&mut file, &mut archive) let bytes_written = io::copy(&mut file, &mut archive)
.expect("Could not read from file/write to initrd") as u16; .expect("Could not read from file/write to initrd") as u32;
if (bytes_written % BLOCK_SIZE) != 0 { if (bytes_written % BLOCK_SIZE) != 0 {
let bytes_padding = (BLOCK_SIZE) - (bytes_written % BLOCK_SIZE); let bytes_padding = (BLOCK_SIZE) - (bytes_written % BLOCK_SIZE);
archive archive