Add elf_section() which checks the ELF magic and class byte to automatically dispatch to elf32_section() or elf64_section().
Update existing callers to use elf_section() instead of calling elf64_section() directly. Signed-off-by: John Hubbard <[email protected]> --- drivers/gpu/nova-core/firmware.rs | 20 +++++++++++++++++--- drivers/gpu/nova-core/firmware/gsp.rs | 4 ++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/nova-core/firmware.rs b/drivers/gpu/nova-core/firmware.rs index 5f3f878eef71..43a4e70aeedc 100644 --- a/drivers/gpu/nova-core/firmware.rs +++ b/drivers/gpu/nova-core/firmware.rs @@ -596,13 +596,27 @@ fn elf_section_generic<'a, H, S>(elf: &'a [u8], name: &str) -> Option<&'a [u8]> } /// Extract the section with name `name` from the ELF64 image `elf`. - pub(super) fn elf64_section<'a>(elf: &'a [u8], name: &str) -> Option<&'a [u8]> { + fn elf64_section<'a>(elf: &'a [u8], name: &str) -> Option<&'a [u8]> { elf_section_generic::<Elf64Hdr, Elf64SHdr>(elf, name) } /// Extract section with name `name` from the ELF32 image `elf`. - #[expect(dead_code)] - pub(super) fn elf32_section<'a>(elf: &'a [u8], name: &str) -> Option<&'a [u8]> { + fn elf32_section<'a>(elf: &'a [u8], name: &str) -> Option<&'a [u8]> { elf_section_generic::<Elf32Hdr, Elf32SHdr>(elf, name) } + + /// Automatically detects ELF32 vs ELF64 based on the ELF header. + pub(super) fn elf_section<'a>(elf: &'a [u8], name: &str) -> Option<&'a [u8]> { + // Check ELF magic. + if elf.len() < 5 || elf.get(0..4)? != b"\x7fELF" { + return None; + } + + // Check ELF class: 1 = 32-bit, 2 = 64-bit. + match elf.get(4)? { + 1 => elf32_section(elf, name), + 2 => elf64_section(elf, name), + _ => None, + } + } } diff --git a/drivers/gpu/nova-core/firmware/gsp.rs b/drivers/gpu/nova-core/firmware/gsp.rs index 173b16cdfb16..f100b5675b7e 100644 --- a/drivers/gpu/nova-core/firmware/gsp.rs +++ b/drivers/gpu/nova-core/firmware/gsp.rs @@ -105,7 +105,7 @@ pub(crate) fn new<'a>( pin_init::pin_init_scope(move || { let firmware = super::request_firmware(dev, chipset, "gsp", ver)?; - let fw_section = elf::elf64_section(firmware.data(), ".fwimage").ok_or(EINVAL)?; + let fw_section = elf::elf_section(firmware.data(), ".fwimage").ok_or(EINVAL)?; let size = fw_section.len(); @@ -162,7 +162,7 @@ pub(crate) fn new<'a>( signatures: { let sigs_section = Self::get_gsp_sigs_section(chipset).ok_or(ENOTSUPP)?; - elf::elf64_section(firmware.data(), sigs_section) + elf::elf_section(firmware.data(), sigs_section) .ok_or(EINVAL) .and_then(|data| DmaObject::from_data(dev, data))? }, -- 2.52.0
