// SPDX-License-Identifier: GPL-2.012//! Contains structures and functions dedicated to the parsing, building and patching of firmwares3//! to be loaded into a given execution unit.45use core::marker::PhantomData;67use kernel::{8device,9firmware,10prelude::*,11str::CString,12transmute::FromBytes, //13};1415use crate::{16dma::DmaObject,17falcon::FalconFirmware,18gpu,19num::{20FromSafeCast,21IntoSafeCast, //22},23};2425pub(crate) mod booter;26pub(crate) mod fwsec;27pub(crate) mod gsp;28pub(crate) mod riscv;2930pub(crate) const FIRMWARE_VERSION: &str = "570.144";3132/// Requests the GPU firmware `name` suitable for `chipset`, with version `ver`.33fn request_firmware(34dev: &device::Device,35chipset: gpu::Chipset,36name: &str,37ver: &str,38) -> Result<firmware::Firmware> {39let chip_name = chipset.name();4041CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{name}-{ver}.bin"))42.and_then(|path| firmware::Firmware::request(&path, dev))43}4445/// Structure used to describe some firmwares, notably FWSEC-FRTS.46#[repr(C)]47#[derive(Debug, Clone)]48pub(crate) struct FalconUCodeDescV3 {49/// Header defined by `NV_BIT_FALCON_UCODE_DESC_HEADER_VDESC*` in OpenRM.50hdr: u32,51/// Stored size of the ucode after the header.52stored_size: u32,53/// Offset in `DMEM` at which the signature is expected to be found.54pub(crate) pkc_data_offset: u32,55/// Offset after the code segment at which the app headers are located.56pub(crate) interface_offset: u32,57/// Base address at which to load the code segment into `IMEM`.58pub(crate) imem_phys_base: u32,59/// Size in bytes of the code to copy into `IMEM`.60pub(crate) imem_load_size: u32,61/// Virtual `IMEM` address (i.e. `tag`) at which the code should start.62pub(crate) imem_virt_base: u32,63/// Base address at which to load the data segment into `DMEM`.64pub(crate) dmem_phys_base: u32,65/// Size in bytes of the data to copy into `DMEM`.66pub(crate) dmem_load_size: u32,67/// Mask of the falcon engines on which this firmware can run.68pub(crate) engine_id_mask: u16,69/// ID of the ucode used to infer a fuse register to validate the signature.70pub(crate) ucode_id: u8,71/// Number of signatures in this firmware.72pub(crate) signature_count: u8,73/// Versions of the signatures, used to infer a valid signature to use.74pub(crate) signature_versions: u16,75_reserved: u16,76}7778impl FalconUCodeDescV3 {79/// Returns the size in bytes of the header.80pub(crate) fn size(&self) -> usize {81const HDR_SIZE_SHIFT: u32 = 16;82const HDR_SIZE_MASK: u32 = 0xffff0000;8384((self.hdr & HDR_SIZE_MASK) >> HDR_SIZE_SHIFT).into_safe_cast()85}86}8788/// Trait implemented by types defining the signed state of a firmware.89trait SignedState {}9091/// Type indicating that the firmware must be signed before it can be used.92struct Unsigned;93impl SignedState for Unsigned {}9495/// Type indicating that the firmware is signed and ready to be loaded.96struct Signed;97impl SignedState for Signed {}9899/// A [`DmaObject`] containing a specific microcode ready to be loaded into a falcon.100///101/// This is module-local and meant for sub-modules to use internally.102///103/// After construction, a firmware is [`Unsigned`], and must generally be patched with a signature104/// before it can be loaded (with an exception for development hardware). The105/// [`Self::patch_signature`] and [`Self::no_patch_signature`] methods are used to transition the106/// firmware to its [`Signed`] state.107struct FirmwareDmaObject<F: FalconFirmware, S: SignedState>(DmaObject, PhantomData<(F, S)>);108109/// Trait for signatures to be patched directly into a given firmware.110///111/// This is module-local and meant for sub-modules to use internally.112trait FirmwareSignature<F: FalconFirmware>: AsRef<[u8]> {}113114impl<F: FalconFirmware> FirmwareDmaObject<F, Unsigned> {115/// Patches the firmware at offset `sig_base_img` with `signature`.116fn patch_signature<S: FirmwareSignature<F>>(117mut self,118signature: &S,119sig_base_img: usize,120) -> Result<FirmwareDmaObject<F, Signed>> {121let signature_bytes = signature.as_ref();122if sig_base_img + signature_bytes.len() > self.0.size() {123return Err(EINVAL);124}125126// SAFETY: We are the only user of this object, so there cannot be any race.127let dst = unsafe { self.0.start_ptr_mut().add(sig_base_img) };128129// SAFETY: `signature` and `dst` are valid, properly aligned, and do not overlap.130unsafe {131core::ptr::copy_nonoverlapping(signature_bytes.as_ptr(), dst, signature_bytes.len())132};133134Ok(FirmwareDmaObject(self.0, PhantomData))135}136137/// Mark the firmware as signed without patching it.138///139/// This method is used to explicitly confirm that we do not need to sign the firmware, while140/// allowing us to continue as if it was. This is typically only needed for development141/// hardware.142fn no_patch_signature(self) -> FirmwareDmaObject<F, Signed> {143FirmwareDmaObject(self.0, PhantomData)144}145}146147/// Header common to most firmware files.148#[repr(C)]149#[derive(Debug, Clone)]150struct BinHdr {151/// Magic number, must be `0x10de`.152bin_magic: u32,153/// Version of the header.154bin_ver: u32,155/// Size in bytes of the binary (to be ignored).156bin_size: u32,157/// Offset of the start of the application-specific header.158header_offset: u32,159/// Offset of the start of the data payload.160data_offset: u32,161/// Size in bytes of the data payload.162data_size: u32,163}164165// SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.166unsafe impl FromBytes for BinHdr {}167168// A firmware blob starting with a `BinHdr`.169struct BinFirmware<'a> {170hdr: BinHdr,171fw: &'a [u8],172}173174impl<'a> BinFirmware<'a> {175/// Interpret `fw` as a firmware image starting with a [`BinHdr`], and returns the176/// corresponding [`BinFirmware`] that can be used to extract its payload.177fn new(fw: &'a firmware::Firmware) -> Result<Self> {178const BIN_MAGIC: u32 = 0x10de;179let fw = fw.data();180181fw.get(0..size_of::<BinHdr>())182// Extract header.183.and_then(BinHdr::from_bytes_copy)184// Validate header.185.and_then(|hdr| {186if hdr.bin_magic == BIN_MAGIC {187Some(hdr)188} else {189None190}191})192.map(|hdr| Self { hdr, fw })193.ok_or(EINVAL)194}195196/// Returns the data payload of the firmware, or `None` if the data range is out of bounds of197/// the firmware image.198fn data(&self) -> Option<&[u8]> {199let fw_start = usize::from_safe_cast(self.hdr.data_offset);200let fw_size = usize::from_safe_cast(self.hdr.data_size);201202self.fw.get(fw_start..fw_start + fw_size)203}204}205206pub(crate) struct ModInfoBuilder<const N: usize>(firmware::ModInfoBuilder<N>);207208impl<const N: usize> ModInfoBuilder<N> {209const fn make_entry_file(self, chipset: &str, fw: &str) -> Self {210ModInfoBuilder(211self.0212.new_entry()213.push("nvidia/")214.push(chipset)215.push("/gsp/")216.push(fw)217.push("-")218.push(FIRMWARE_VERSION)219.push(".bin"),220)221}222223const fn make_entry_chipset(self, chipset: &str) -> Self {224self.make_entry_file(chipset, "booter_load")225.make_entry_file(chipset, "booter_unload")226.make_entry_file(chipset, "bootloader")227.make_entry_file(chipset, "gsp")228}229230pub(crate) const fn create(231module_name: &'static kernel::str::CStr,232) -> firmware::ModInfoBuilder<N> {233let mut this = Self(firmware::ModInfoBuilder::new(module_name));234let mut i = 0;235236while i < gpu::Chipset::ALL.len() {237this = this.make_entry_chipset(gpu::Chipset::ALL[i].name());238i += 1;239}240241this.0242}243}244245246