xref: /linux/drivers/gpu/nova-core/firmware.rs (revision 4257d3179384f8ccb085b64f5ca1373be637b77c)
1 // SPDX-License-Identifier: GPL-2.0
2 // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 
4 //! Contains structures and functions dedicated to the parsing, building and patching of firmwares
5 //! to be loaded into a given execution unit.
6 
7 use core::marker::PhantomData;
8 use core::ops::Deref;
9 
10 use kernel::{
11     device,
12     firmware,
13     prelude::*,
14     str::CString,
15     transmute::FromBytes, //
16 };
17 
18 use crate::{
19     falcon::{
20         FalconDmaLoadTarget,
21         FalconFirmware, //
22     },
23     gpu,
24     num::{
25         FromSafeCast,
26         IntoSafeCast, //
27     },
28 };
29 
30 pub(crate) mod booter;
31 pub(crate) mod fsp;
32 pub(crate) mod fwsec;
33 pub(crate) mod gsp;
34 pub(crate) mod riscv;
35 
36 pub(crate) const FIRMWARE_VERSION: &str = "570.144";
37 
38 /// Requests the GPU firmware `name` suitable for `chipset`, with version `ver`.
39 fn request_firmware(
40     dev: &device::Device,
41     chipset: gpu::Chipset,
42     name: &str,
43     ver: &str,
44 ) -> Result<firmware::Firmware> {
45     let chip_name = chipset.name();
46 
47     CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{name}-{ver}.bin"))
48         .and_then(|path| firmware::Firmware::request(&path, dev))
49 }
50 
51 /// Structure used to describe some firmwares, notably FWSEC-FRTS.
52 #[repr(C)]
53 #[derive(Debug, Clone)]
54 pub(crate) struct FalconUCodeDescV2 {
55     /// Header defined by 'NV_BIT_FALCON_UCODE_DESC_HEADER_VDESC*' in OpenRM.
56     hdr: u32,
57     /// Stored size of the ucode after the header, compressed or uncompressed
58     stored_size: u32,
59     /// Uncompressed size of the ucode.  If store_size == uncompressed_size, then the ucode
60     /// is not compressed.
61     pub(crate) uncompressed_size: u32,
62     /// Code entry point
63     pub(crate) virtual_entry: u32,
64     /// Offset after the code segment at which the Application Interface Table headers are located.
65     pub(crate) interface_offset: u32,
66     /// Base address at which to load the code segment into 'IMEM'.
67     pub(crate) imem_phys_base: u32,
68     /// Size in bytes of the code to copy into 'IMEM' (includes both secure and non-secure
69     /// segments).
70     pub(crate) imem_load_size: u32,
71     /// Virtual 'IMEM' address (i.e. 'tag') at which the code should start.
72     pub(crate) imem_virt_base: u32,
73     /// Virtual address of secure IMEM segment.
74     pub(crate) imem_sec_base: u32,
75     /// Size of secure IMEM segment.
76     pub(crate) imem_sec_size: u32,
77     /// Offset into stored (uncompressed) image at which DMEM begins.
78     pub(crate) dmem_offset: u32,
79     /// Base address at which to load the data segment into 'DMEM'.
80     pub(crate) dmem_phys_base: u32,
81     /// Size in bytes of the data to copy into 'DMEM'.
82     pub(crate) dmem_load_size: u32,
83     /// "Alternate" Size of data to load into IMEM.
84     pub(crate) alt_imem_load_size: u32,
85     /// "Alternate" Size of data to load into DMEM.
86     pub(crate) alt_dmem_load_size: u32,
87 }
88 
89 // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.
90 unsafe impl FromBytes for FalconUCodeDescV2 {}
91 
92 /// Structure used to describe some firmwares, notably FWSEC-FRTS.
93 #[repr(C)]
94 #[derive(Debug, Clone)]
95 pub(crate) struct FalconUCodeDescV3 {
96     /// Header defined by `NV_BIT_FALCON_UCODE_DESC_HEADER_VDESC*` in OpenRM.
97     hdr: u32,
98     /// Stored size of the ucode after the header.
99     stored_size: u32,
100     /// Offset in `DMEM` at which the signature is expected to be found.
101     pub(crate) pkc_data_offset: u32,
102     /// Offset after the code segment at which the app headers are located.
103     pub(crate) interface_offset: u32,
104     /// Base address at which to load the code segment into `IMEM`.
105     pub(crate) imem_phys_base: u32,
106     /// Size in bytes of the code to copy into `IMEM`.
107     pub(crate) imem_load_size: u32,
108     /// Virtual `IMEM` address (i.e. `tag`) at which the code should start.
109     pub(crate) imem_virt_base: u32,
110     /// Base address at which to load the data segment into `DMEM`.
111     pub(crate) dmem_phys_base: u32,
112     /// Size in bytes of the data to copy into `DMEM`.
113     pub(crate) dmem_load_size: u32,
114     /// Mask of the falcon engines on which this firmware can run.
115     pub(crate) engine_id_mask: u16,
116     /// ID of the ucode used to infer a fuse register to validate the signature.
117     pub(crate) ucode_id: u8,
118     /// Number of signatures in this firmware.
119     pub(crate) signature_count: u8,
120     /// Versions of the signatures, used to infer a valid signature to use.
121     pub(crate) signature_versions: u16,
122     _reserved: u16,
123 }
124 
125 // SAFETY: all bit patterns are valid for this type, and it doesn't use
126 // interior mutability.
127 unsafe impl FromBytes for FalconUCodeDescV3 {}
128 
129 /// Enum wrapping the different versions of Falcon microcode descriptors.
130 ///
131 /// This allows handling both V2 and V3 descriptor formats through a
132 /// unified type, providing version-agnostic access to firmware metadata
133 /// via the [`FalconUCodeDescriptor`] trait.
134 #[derive(Debug, Clone)]
135 pub(crate) enum FalconUCodeDesc {
136     V2(FalconUCodeDescV2),
137     V3(FalconUCodeDescV3),
138 }
139 
140 impl Deref for FalconUCodeDesc {
141     type Target = dyn FalconUCodeDescriptor;
142 
143     fn deref(&self) -> &Self::Target {
144         match self {
145             FalconUCodeDesc::V2(v2) => v2,
146             FalconUCodeDesc::V3(v3) => v3,
147         }
148     }
149 }
150 
151 /// Trait providing a common interface for accessing Falcon microcode descriptor fields.
152 ///
153 /// This trait abstracts over the different descriptor versions ([`FalconUCodeDescV2`] and
154 /// [`FalconUCodeDescV3`]), allowing code to work with firmware metadata without needing to
155 /// know the specific descriptor version. Fields not present return zero.
156 pub(crate) trait FalconUCodeDescriptor {
157     fn hdr(&self) -> u32;
158     fn imem_load_size(&self) -> u32;
159     fn interface_offset(&self) -> u32;
160     fn dmem_load_size(&self) -> u32;
161     fn pkc_data_offset(&self) -> u32;
162     fn engine_id_mask(&self) -> u16;
163     fn ucode_id(&self) -> u8;
164     fn signature_count(&self) -> u8;
165     fn signature_versions(&self) -> u16;
166 
167     /// Returns the size in bytes of the header.
168     fn size(&self) -> usize {
169         let hdr = self.hdr();
170 
171         const HDR_SIZE_SHIFT: u32 = 16;
172         const HDR_SIZE_MASK: u32 = 0xffff0000;
173         ((hdr & HDR_SIZE_MASK) >> HDR_SIZE_SHIFT).into_safe_cast()
174     }
175 
176     fn imem_sec_load_params(&self) -> FalconDmaLoadTarget;
177     fn imem_ns_load_params(&self) -> Option<FalconDmaLoadTarget>;
178     fn dmem_load_params(&self) -> FalconDmaLoadTarget;
179 }
180 
181 impl FalconUCodeDescriptor for FalconUCodeDescV2 {
182     fn hdr(&self) -> u32 {
183         self.hdr
184     }
185     fn imem_load_size(&self) -> u32 {
186         self.imem_load_size
187     }
188     fn interface_offset(&self) -> u32 {
189         self.interface_offset
190     }
191     fn dmem_load_size(&self) -> u32 {
192         self.dmem_load_size
193     }
194     fn pkc_data_offset(&self) -> u32 {
195         0
196     }
197     fn engine_id_mask(&self) -> u16 {
198         0
199     }
200     fn ucode_id(&self) -> u8 {
201         0
202     }
203     fn signature_count(&self) -> u8 {
204         0
205     }
206     fn signature_versions(&self) -> u16 {
207         0
208     }
209 
210     fn imem_sec_load_params(&self) -> FalconDmaLoadTarget {
211         // `imem_sec_base` is the *virtual* start address of the secure IMEM segment, so subtract
212         // `imem_virt_base` to get its physical offset.
213         let imem_sec_start = self.imem_sec_base.saturating_sub(self.imem_virt_base);
214 
215         FalconDmaLoadTarget {
216             src_start: imem_sec_start,
217             dst_start: self.imem_phys_base.saturating_add(imem_sec_start),
218             len: self.imem_sec_size,
219         }
220     }
221 
222     fn imem_ns_load_params(&self) -> Option<FalconDmaLoadTarget> {
223         Some(FalconDmaLoadTarget {
224             // Non-secure code always starts at offset 0.
225             src_start: 0,
226             dst_start: self.imem_phys_base,
227             // `imem_load_size` includes the size of the secure segment, so subtract it to
228             // get the correct amount of data to copy.
229             len: self.imem_load_size.saturating_sub(self.imem_sec_size),
230         })
231     }
232 
233     fn dmem_load_params(&self) -> FalconDmaLoadTarget {
234         FalconDmaLoadTarget {
235             src_start: self.dmem_offset,
236             dst_start: self.dmem_phys_base,
237             len: self.dmem_load_size,
238         }
239     }
240 }
241 
242 impl FalconUCodeDescriptor for FalconUCodeDescV3 {
243     fn hdr(&self) -> u32 {
244         self.hdr
245     }
246     fn imem_load_size(&self) -> u32 {
247         self.imem_load_size
248     }
249     fn interface_offset(&self) -> u32 {
250         self.interface_offset
251     }
252     fn dmem_load_size(&self) -> u32 {
253         self.dmem_load_size
254     }
255     fn pkc_data_offset(&self) -> u32 {
256         self.pkc_data_offset
257     }
258     fn engine_id_mask(&self) -> u16 {
259         self.engine_id_mask
260     }
261     fn ucode_id(&self) -> u8 {
262         self.ucode_id
263     }
264     fn signature_count(&self) -> u8 {
265         self.signature_count
266     }
267     fn signature_versions(&self) -> u16 {
268         self.signature_versions
269     }
270 
271     fn imem_sec_load_params(&self) -> FalconDmaLoadTarget {
272         FalconDmaLoadTarget {
273             // IMEM segment always starts at offset 0.
274             src_start: 0,
275             dst_start: self.imem_phys_base,
276             len: self.imem_load_size,
277         }
278     }
279 
280     fn imem_ns_load_params(&self) -> Option<FalconDmaLoadTarget> {
281         // Not used on V3 platforms
282         None
283     }
284 
285     fn dmem_load_params(&self) -> FalconDmaLoadTarget {
286         FalconDmaLoadTarget {
287             // DMEM segment starts right after the IMEM one.
288             src_start: self.imem_load_size,
289             dst_start: self.dmem_phys_base,
290             len: self.dmem_load_size,
291         }
292     }
293 }
294 
295 /// Trait implemented by types defining the signed state of a firmware.
296 trait SignedState {}
297 
298 /// Type indicating that the firmware must be signed before it can be used.
299 struct Unsigned;
300 impl SignedState for Unsigned {}
301 
302 /// Type indicating that the firmware is signed and ready to be loaded.
303 struct Signed;
304 impl SignedState for Signed {}
305 
306 /// Microcode to be loaded into a specific falcon.
307 ///
308 /// This is module-local and meant for sub-modules to use internally.
309 ///
310 /// After construction, a firmware is [`Unsigned`], and must generally be patched with a signature
311 /// before it can be loaded (with an exception for development hardware). The
312 /// [`Self::patch_signature`] and [`Self::no_patch_signature`] methods are used to transition the
313 /// firmware to its [`Signed`] state.
314 // TODO: Consider replacing this with a coherent memory object once `CoherentAllocation` supports
315 // temporary CPU-exclusive access to the object without unsafe methods.
316 struct FirmwareObject<F: FalconFirmware, S: SignedState>(KVVec<u8>, PhantomData<(F, S)>);
317 
318 /// Trait for signatures to be patched directly into a given firmware.
319 ///
320 /// This is module-local and meant for sub-modules to use internally.
321 trait FirmwareSignature<F: FalconFirmware>: AsRef<[u8]> {}
322 
323 impl<F: FalconFirmware> FirmwareObject<F, Unsigned> {
324     /// Patches the firmware at offset `signature_start` with `signature`.
325     fn patch_signature<S: FirmwareSignature<F>>(
326         mut self,
327         signature: &S,
328         signature_start: usize,
329     ) -> Result<FirmwareObject<F, Signed>> {
330         let signature_bytes = signature.as_ref();
331         let signature_end = signature_start
332             .checked_add(signature_bytes.len())
333             .ok_or(EOVERFLOW)?;
334         let dst = self
335             .0
336             .get_mut(signature_start..signature_end)
337             .ok_or(EINVAL)?;
338 
339         // PANIC: `dst` and `signature_bytes` have the same length.
340         dst.copy_from_slice(signature_bytes);
341 
342         Ok(FirmwareObject(self.0, PhantomData))
343     }
344 
345     /// Mark the firmware as signed without patching it.
346     ///
347     /// This method is used to explicitly confirm that we do not need to sign the firmware, while
348     /// allowing us to continue as if it was. This is typically only needed for development
349     /// hardware.
350     fn no_patch_signature(self) -> FirmwareObject<F, Signed> {
351         FirmwareObject(self.0, PhantomData)
352     }
353 }
354 
355 /// Header common to most firmware files.
356 #[repr(C)]
357 #[derive(Debug, Clone)]
358 struct BinHdr {
359     /// Magic number, must be `0x10de`.
360     bin_magic: u32,
361     /// Version of the header.
362     bin_ver: u32,
363     /// Size in bytes of the binary (to be ignored).
364     bin_size: u32,
365     /// Offset of the start of the application-specific header.
366     header_offset: u32,
367     /// Offset of the start of the data payload.
368     data_offset: u32,
369     /// Size in bytes of the data payload.
370     data_size: u32,
371 }
372 
373 // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.
374 unsafe impl FromBytes for BinHdr {}
375 
376 // A firmware blob starting with a `BinHdr`.
377 struct BinFirmware<'a> {
378     hdr: BinHdr,
379     fw: &'a [u8],
380 }
381 
382 impl<'a> BinFirmware<'a> {
383     /// Interpret `fw` as a firmware image starting with a [`BinHdr`], and returns the
384     /// corresponding [`BinFirmware`] that can be used to extract its payload.
385     fn new(fw: &'a firmware::Firmware) -> Result<Self> {
386         const BIN_MAGIC: u32 = 0x10de;
387         let fw = fw.data();
388 
389         fw.get(0..size_of::<BinHdr>())
390             // Extract header.
391             .and_then(BinHdr::from_bytes_copy)
392             // Validate header.
393             .filter(|hdr| hdr.bin_magic == BIN_MAGIC)
394             .map(|hdr| Self { hdr, fw })
395             .ok_or(EINVAL)
396     }
397 
398     /// Returns the data payload of the firmware, or `None` if the data range is out of bounds of
399     /// the firmware image.
400     fn data(&self) -> Option<&[u8]> {
401         let fw_start = usize::from_safe_cast(self.hdr.data_offset);
402         let fw_size = usize::from_safe_cast(self.hdr.data_size);
403         let fw_end = fw_start.checked_add(fw_size)?;
404 
405         self.fw.get(fw_start..fw_end)
406     }
407 }
408 
409 pub(crate) struct ModInfoBuilder<const N: usize>(firmware::ModInfoBuilder<N>);
410 
411 impl<const N: usize> ModInfoBuilder<N> {
412     const fn make_entry_file(self, chipset: &str, fw: &str) -> Self {
413         ModInfoBuilder(
414             self.0
415                 .new_entry()
416                 .push("nvidia/")
417                 .push(chipset)
418                 .push("/gsp/")
419                 .push(fw)
420                 .push("-")
421                 .push(FIRMWARE_VERSION)
422                 .push(".bin"),
423         )
424     }
425 
426     const fn make_entry_chipset(self, chipset: gpu::Chipset) -> Self {
427         let name = chipset.name();
428 
429         let this = self
430             .make_entry_file(name, "booter_load")
431             .make_entry_file(name, "booter_unload")
432             .make_entry_file(name, "bootloader")
433             .make_entry_file(name, "gsp");
434 
435         let this = if chipset.needs_fwsec_bootloader() {
436             this.make_entry_file(name, "gen_bootloader")
437         } else {
438             this
439         };
440 
441         if chipset.uses_fsp() {
442             this.make_entry_file(name, "fmc")
443         } else {
444             this
445         }
446     }
447 
448     pub(crate) const fn create(
449         module_name: &'static core::ffi::CStr,
450     ) -> firmware::ModInfoBuilder<N> {
451         let mut this = Self(firmware::ModInfoBuilder::new(module_name));
452         let mut i = 0;
453 
454         while i < gpu::Chipset::ALL.len() {
455             this = this.make_entry_chipset(gpu::Chipset::ALL[i]);
456             i += 1;
457         }
458 
459         this.0
460     }
461 }
462 
463 /// Ad-hoc and temporary module to extract sections from ELF images.
464 ///
465 /// Some firmware images are currently packaged as ELF files, where sections names are used as keys
466 /// to specific and related bits of data. Future firmware versions are scheduled to move away from
467 /// that scheme before nova-core becomes stable, which means this module will eventually be
468 /// removed.
469 mod elf {
470     use core::mem::size_of;
471 
472     use kernel::{
473         bindings,
474         str::CStr,
475         transmute::FromBytes, //
476     };
477 
478     /// Trait to abstract over ELF header differences.
479     trait ElfHeader: FromBytes {
480         fn shnum(&self) -> u16;
481         fn shoff(&self) -> u64;
482         fn shstrndx(&self) -> u16;
483     }
484 
485     /// Trait to abstract over ELF section-header differences.
486     trait ElfSectionHeader: FromBytes {
487         fn name(&self) -> u32;
488         fn offset(&self) -> u64;
489         fn size(&self) -> u64;
490     }
491 
492     /// Trait describing a matching ELF header and section-header format.
493     trait ElfFormat {
494         type Header: ElfHeader;
495         type SectionHeader: ElfSectionHeader;
496     }
497 
498     /// Newtype to provide a [`FromBytes`] implementation.
499     #[repr(transparent)]
500     struct Elf64Hdr(bindings::elf64_hdr);
501     // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.
502     unsafe impl FromBytes for Elf64Hdr {}
503 
504     impl ElfHeader for Elf64Hdr {
505         fn shnum(&self) -> u16 {
506             self.0.e_shnum
507         }
508 
509         fn shoff(&self) -> u64 {
510             self.0.e_shoff
511         }
512 
513         fn shstrndx(&self) -> u16 {
514             self.0.e_shstrndx
515         }
516     }
517 
518     #[repr(transparent)]
519     struct Elf64SHdr(bindings::elf64_shdr);
520     // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.
521     unsafe impl FromBytes for Elf64SHdr {}
522 
523     impl ElfSectionHeader for Elf64SHdr {
524         fn name(&self) -> u32 {
525             self.0.sh_name
526         }
527 
528         fn offset(&self) -> u64 {
529             self.0.sh_offset
530         }
531 
532         fn size(&self) -> u64 {
533             self.0.sh_size
534         }
535     }
536 
537     struct Elf64Format;
538 
539     impl ElfFormat for Elf64Format {
540         type Header = Elf64Hdr;
541         type SectionHeader = Elf64SHdr;
542     }
543 
544     /// Newtype to provide [`FromBytes`] and [`ElfHeader`] implementations for ELF32.
545     #[repr(transparent)]
546     struct Elf32Hdr(bindings::elf32_hdr);
547     // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.
548     unsafe impl FromBytes for Elf32Hdr {}
549 
550     impl ElfHeader for Elf32Hdr {
551         fn shnum(&self) -> u16 {
552             self.0.e_shnum
553         }
554 
555         fn shoff(&self) -> u64 {
556             u64::from(self.0.e_shoff)
557         }
558 
559         fn shstrndx(&self) -> u16 {
560             self.0.e_shstrndx
561         }
562     }
563 
564     /// Newtype to provide [`FromBytes`] and [`ElfSectionHeader`] implementations for ELF32.
565     #[repr(transparent)]
566     struct Elf32SHdr(bindings::elf32_shdr);
567     // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.
568     unsafe impl FromBytes for Elf32SHdr {}
569 
570     impl ElfSectionHeader for Elf32SHdr {
571         fn name(&self) -> u32 {
572             self.0.sh_name
573         }
574 
575         fn offset(&self) -> u64 {
576             u64::from(self.0.sh_offset)
577         }
578 
579         fn size(&self) -> u64 {
580             u64::from(self.0.sh_size)
581         }
582     }
583 
584     struct Elf32Format;
585 
586     impl ElfFormat for Elf32Format {
587         type Header = Elf32Hdr;
588         type SectionHeader = Elf32SHdr;
589     }
590 
591     /// Returns a NULL-terminated string from the ELF image at `offset`.
592     fn elf_str(elf: &[u8], offset: u64) -> Option<&str> {
593         let idx = usize::try_from(offset).ok()?;
594         let bytes = elf.get(idx..)?;
595         CStr::from_bytes_until_nul(bytes).ok()?.to_str().ok()
596     }
597 
598     fn elf_section_generic<'a, F>(elf: &'a [u8], name: &str) -> Option<&'a [u8]>
599     where
600         F: ElfFormat,
601     {
602         let hdr = F::Header::from_bytes(elf.get(0..size_of::<F::Header>())?)?;
603 
604         let shdr_num = usize::from(hdr.shnum());
605         let shdr_start = usize::try_from(hdr.shoff()).ok()?;
606         let shdr_end = shdr_num
607             .checked_mul(size_of::<F::SectionHeader>())
608             .and_then(|v| v.checked_add(shdr_start))?;
609 
610         // Get all the section headers as an iterator over byte chunks.
611         let shdr_bytes = elf.get(shdr_start..shdr_end)?;
612         let mut shdr_iter = shdr_bytes.chunks_exact(size_of::<F::SectionHeader>());
613 
614         // Get the strings table.
615         let strhdr = shdr_iter
616             .clone()
617             .nth(usize::from(hdr.shstrndx()))
618             .and_then(F::SectionHeader::from_bytes)?;
619 
620         // Find the section which name matches `name` and return it.
621         shdr_iter.find_map(|sh_bytes| {
622             let sh = F::SectionHeader::from_bytes(sh_bytes)?;
623             let name_offset = strhdr.offset().checked_add(u64::from(sh.name()))?;
624             let section_name = elf_str(elf, name_offset)?;
625 
626             if section_name != name {
627                 return None;
628             }
629 
630             let start = usize::try_from(sh.offset()).ok()?;
631             let end = usize::try_from(sh.size())
632                 .ok()
633                 .and_then(|sz| start.checked_add(sz))?;
634 
635             elf.get(start..end)
636         })
637     }
638 
639     /// Extract the section with name `name` from the ELF64 image `elf`.
640     fn elf64_section<'a>(elf: &'a [u8], name: &str) -> Option<&'a [u8]> {
641         elf_section_generic::<Elf64Format>(elf, name)
642     }
643 
644     /// Extract the section with name `name` from the ELF32 image `elf`.
645     fn elf32_section<'a>(elf: &'a [u8], name: &str) -> Option<&'a [u8]> {
646         elf_section_generic::<Elf32Format>(elf, name)
647     }
648 
649     /// Automatically detects ELF32 vs ELF64 based on the ELF header.
650     pub(super) fn elf_section<'a>(elf: &'a [u8], name: &str) -> Option<&'a [u8]> {
651         // ELF identification: a 4-byte magic followed by a class byte (32- vs 64-bit).
652         const ELFMAG: &[u8] = b"\x7fELF";
653         const SELFMAG: usize = ELFMAG.len();
654         const EI_CLASS: usize = 4;
655         const ELFCLASS32: u8 = 1;
656         const ELFCLASS64: u8 = 2;
657 
658         if elf.get(0..SELFMAG) != Some(ELFMAG) {
659             return None;
660         }
661 
662         match *elf.get(EI_CLASS)? {
663             ELFCLASS32 => elf32_section(elf, name),
664             ELFCLASS64 => elf64_section(elf, name),
665             _ => None,
666         }
667     }
668 }
669