Remove Overlayhader and support loading overlays

This commit is contained in:
2023-04-10 17:01:28 +02:00
parent 7a373a38ff
commit 1a0d175779
8 changed files with 47 additions and 65 deletions

View File

@@ -58,13 +58,23 @@ fn write_section(output: &mut Output, sections: &Vec<OverlaySection>, add_end_na
for section in sections {
writeln!(output, "\t\t.{} {{", section.name)?;
writeln!(output, "\t\t\t__{}_start = .;", section.name)?;
section.file_pattern.iter().try_for_each(|patr| writeln!(output, "\t\t\tKEEP({}(.header))", patr))?;
writeln!(output, "\t\t\t__{}_lbas = .;", section.name)?;
section.file_pattern.iter().try_for_each(|patr| writeln!(output, "\t\t\tKEEP({}(.header.lbas))", patr))?;
writeln!(output, "\t\t\t__{}_ctor = .;", section.name)?;
for section_type in [".text.startup._GLOBAL__*", ".ctors", ".text.*", ".rodata*", ".sdata*", ".data*", ".sbss*", ".bss*"] {
section.file_pattern.iter().try_for_each(|patr| writeln!(output, "\t\t\t{}({})", patr, section_type))?;
for (section_type, keep) in [(".text.startup._GLOBAL__*", false), (".ctors", false), (".text.*", false), (".rodata*", false), (".sdata*", false), (".data*", false), (".sbss*", false), (".bss*", false), (".keep.dummy", true)] {
section.file_pattern.iter().try_for_each(|patr| {
let section_entry = format!("{}({})", patr, section_type);
write!(output, "\t\t\t")?;
if keep {
writeln!(output, "KEEP({})", section_entry)
}
else {
writeln!(output, "{}", section_entry)
}
})?;
}
if add_end_name {

View File

@@ -8,18 +8,6 @@ pub type LBANameVec = Vec<String>;
mod main;
#[repr(packed)]
struct OverlayHeader {
_start_adr: u32,
lba_count: u16,
}
impl OverlayHeader {
pub fn read_lba_count(&self) -> usize {
u16::from_le(self.lba_count) as usize
}
}
#[repr(packed)]
struct LBAEntry {
lba: u16,
@@ -51,7 +39,7 @@ impl LBAEntry {
}
pub fn load_from(file_name: &str, file_path: PathBuf, lba_source: PathBuf) -> Result<File, Error> {
let content = load_content(&file_path)?;
let content = read_file(&file_path)?;
let lba_names = load_lba_names(lba_source)?;
let content_size = format_if_error!(tool_helper::compress::psx_default::lz4(&content), "Compressing {} failed with \"{error_text}\"", file_path.to_string_lossy())?.len();
@@ -63,14 +51,10 @@ pub fn load_for_main(file_name: &str, content: Vec<u8>, lba_source: PathBuf) ->
}
pub fn update_content(content: &mut Vec<u8>, lba_names: &LBANameVec, file_map: &FileSystemMap, length_func: LengthCalculatorFunction) -> Result<Vec<u8>, Error> {
let (lba_header, lba_count) = skip_to_lba_header(content);
let lba_header = unsafe{std::slice::from_raw_parts_mut(lba_header.as_mut_ptr() as *mut LBAEntry, lba_count)};
let lba_header = skip_to_lba_header(content);
let lba_header = unsafe{std::slice::from_raw_parts_mut(lba_header.as_mut_ptr() as *mut LBAEntry, lba_names.len())};
for_each_lba_name(lba_names, file_map, length_func, |idx, (lba, bytes)| {
if idx >= lba_count {
return Err(Error::from_text(format!("Trying to write more LBAs then there is space!")));
}
lba_header[idx].write_entry(lba, bytes)
})?;
@@ -105,34 +89,10 @@ fn for_each_lba_name<F: FnMut(usize, (u16, usize)) -> Result<(), Error>>(lba_nam
Ok(())
}
fn load_content(file_path: &PathBuf) -> Result<Vec<u8>, Error> {
let mut content = read_file(&file_path)?;
fn skip_to_lba_header(content: &mut Vec<u8>) -> &mut [u8] {
let overlay_header_size = 0;
if content.len() < std::mem::size_of::<OverlayHeader>() {
return Err(Error::from_text(format!("Overlay {} has no header!", file_path.to_string_lossy())));
}
let (lba_header, lba_count) = skip_to_lba_header(&mut content);
if lba_header.is_empty() {
return Err(Error::from_text(format!("LBA header of overlay {} is smaller then {} elements", file_path.to_string_lossy(), lba_count)));
}
let mut count = 0;
for byte in lba_header {
*byte = count as u8;
count += 1;
}
Ok(content)
}
fn skip_to_lba_header(content: &mut Vec<u8>) -> (&mut [u8], usize) {
let overlay_header_size = std::mem::size_of::<OverlayHeader>();
let lba_count = unsafe{std::mem::transmute::<&u8, &OverlayHeader>(&content[0])}.read_lba_count();
let lba_header_size = lba_count*std::mem::size_of::<LBAEntry>();
(&mut content[overlay_header_size..(overlay_header_size+lba_header_size)], lba_count)
&mut content[overlay_header_size..]
}
fn load_lba_names(lba_source: PathBuf) -> Result<LBANameVec, Error> {