Skip to content

Commit 258a6f9

Browse files
johnhubbardGnurou
authored andcommitted
gpu: nova-core: add support for 32-bit firmware images
Some GPU firmware images are packaged as 32-bit ELF rather than 64-bit. Add a 32-bit implementation of the shared ELF section-parsing abstraction so those images can be parsed alongside the existing 64-bit path. Signed-off-by: John Hubbard <jhubbard@nvidia.com> Reviewed-by: Eliot Courtney <ecourtney@nvidia.com> Link: https://patch.msgid.link/20260602032111.224790-9-jhubbard@nvidia.com Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
1 parent bd581ff commit 258a6f9

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

drivers/gpu/nova-core/firmware.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,53 @@ mod elf {
534534
type SectionHeader = Elf64SHdr;
535535
}
536536

537+
/// Newtype to provide [`FromBytes`] and [`ElfHeader`] implementations for ELF32.
538+
#[repr(transparent)]
539+
struct Elf32Hdr(bindings::elf32_hdr);
540+
// SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.
541+
unsafe impl FromBytes for Elf32Hdr {}
542+
543+
impl ElfHeader for Elf32Hdr {
544+
fn shnum(&self) -> u16 {
545+
self.0.e_shnum
546+
}
547+
548+
fn shoff(&self) -> u64 {
549+
u64::from(self.0.e_shoff)
550+
}
551+
552+
fn shstrndx(&self) -> u16 {
553+
self.0.e_shstrndx
554+
}
555+
}
556+
557+
/// Newtype to provide [`FromBytes`] and [`ElfSectionHeader`] implementations for ELF32.
558+
#[repr(transparent)]
559+
struct Elf32SHdr(bindings::elf32_shdr);
560+
// SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.
561+
unsafe impl FromBytes for Elf32SHdr {}
562+
563+
impl ElfSectionHeader for Elf32SHdr {
564+
fn name(&self) -> u32 {
565+
self.0.sh_name
566+
}
567+
568+
fn offset(&self) -> u64 {
569+
u64::from(self.0.sh_offset)
570+
}
571+
572+
fn size(&self) -> u64 {
573+
u64::from(self.0.sh_size)
574+
}
575+
}
576+
577+
struct Elf32Format;
578+
579+
impl ElfFormat for Elf32Format {
580+
type Header = Elf32Hdr;
581+
type SectionHeader = Elf32SHdr;
582+
}
583+
537584
/// Returns a NULL-terminated string from the ELF image at `offset`.
538585
fn elf_str(elf: &[u8], offset: u64) -> Option<&str> {
539586
let idx = usize::try_from(offset).ok()?;
@@ -586,4 +633,10 @@ mod elf {
586633
pub(super) fn elf64_section<'a>(elf: &'a [u8], name: &str) -> Option<&'a [u8]> {
587634
elf_section_generic::<Elf64Format>(elf, name)
588635
}
636+
637+
/// Extract the section with name `name` from the ELF32 image `elf`.
638+
#[expect(dead_code)]
639+
pub(super) fn elf32_section<'a>(elf: &'a [u8], name: &str) -> Option<&'a [u8]> {
640+
elf_section_generic::<Elf32Format>(elf, name)
641+
}
589642
}

0 commit comments

Comments
 (0)