os/initrd_maker.rs

52 lines
2.0 KiB
Rust
Raw Normal View History

2022-10-07 18:05:26 -05:00
use std::{
env::args,
fs::File,
io::{self, Write},
path::Path,
};
2022-11-18 16:05:13 -06:00
const BLOCK_SIZE: u16 = 512;
const MAX_NAME_LEN: u16 = BLOCK_SIZE - 8;
2022-10-07 18:05:26 -05:00
fn main() {
let mut archive = File::create("initrd").expect("Could not open initrd");
for file_path in args().skip(1) {
let file_path = Path::new(&file_path);
let file_name = file_path.file_name().unwrap();
let file_name_len =
2022-11-18 16:05:13 -06:00
u16::try_from(file_name.len()).expect("File name length greater than 256 bytes");
2022-11-18 16:29:58 -06:00
assert!(
file_name_len <= MAX_NAME_LEN,
"File {:?} has name longer than {} bytes",
file_name,
MAX_NAME_LEN
);
2022-10-07 18:05:26 -05:00
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())
.expect("File size greater than 64 KiB");
2022-11-18 16:29:58 -06:00
let file_num_blocks = (length / BLOCK_SIZE) + if length % BLOCK_SIZE == 0 { 0 } else { 1 };
2022-10-07 18:05:26 -05:00
let mut header_block = Vec::new();
2022-10-08 17:55:21 -05:00
header_block.extend_from_slice(&length.to_be_bytes());
header_block.extend_from_slice(&file_num_blocks.to_be_bytes());
2022-11-18 16:05:13 -06:00
header_block.extend_from_slice(&file_name_len.to_be_bytes());
2022-10-07 18:05:26 -05:00
header_block.extend_from_slice(file_name.to_str().unwrap().as_bytes());
header_block.push(0);
header_block.resize(BLOCK_SIZE as usize, 0);
archive
.write_all(&header_block)
.expect("Could not write to initrd");
2022-11-18 16:29:58 -06:00
#[allow(clippy::cast_possible_truncation)]
let bytes_written = io::copy(&mut file, &mut archive)
.expect("Could not read from file/write to initrd") as u16;
if (bytes_written % BLOCK_SIZE) != 0 {
let bytes_padding = (BLOCK_SIZE) - (bytes_written % BLOCK_SIZE);
archive
.write_all(&vec![0; bytes_padding as usize])
.expect("Could not write to initrd");
2022-10-07 18:05:26 -05:00
}
}
archive
2022-11-18 16:05:13 -06:00
.write_all(&[0; BLOCK_SIZE as usize])
2022-10-07 18:05:26 -05:00
.expect("Could not write to initrd");
}