// 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;6use core::mem::size_of;78use kernel::device;9use kernel::firmware;10use kernel::prelude::*;11use kernel::str::CString;12use kernel::transmute::FromBytes;1314use crate::dma::DmaObject;15use crate::falcon::FalconFirmware;16use crate::gpu;1718pub(crate) mod booter;19pub(crate) mod fwsec;20pub(crate) mod gsp;21pub(crate) mod riscv;2223pub(crate) const FIRMWARE_VERSION: &str = "570.144";2425/// Requests the GPU firmware `name` suitable for `chipset`, with version `ver`.26fn request_firmware(27dev: &device::Device,28chipset: gpu::Chipset,29name: &str,30ver: &str,31) -> Result<firmware::Firmware> {32let chip_name = chipset.name();3334CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{name}-{ver}.bin"))35.and_then(|path| firmware::Firmware::request(&path, dev))36}3738/// Structure used to describe some firmwares, notably FWSEC-FRTS.39#[repr(C)]40#[derive(Debug, Clone)]41pub(crate) struct FalconUCodeDescV3 {42/// Header defined by `NV_BIT_FALCON_UCODE_DESC_HEADER_VDESC*` in OpenRM.43hdr: u32,44/// Stored size of the ucode after the header.45stored_size: u32,46/// Offset in `DMEM` at which the signature is expected to be found.47pub(crate) pkc_data_offset: u32,48/// Offset after the code segment at which the app headers are located.49pub(crate) interface_offset: u32,50/// Base address at which to load the code segment into `IMEM`.51pub(crate) imem_phys_base: u32,52/// Size in bytes of the code to copy into `IMEM`.53pub(crate) imem_load_size: u32,54/// Virtual `IMEM` address (i.e. `tag`) at which the code should start.55pub(crate) imem_virt_base: u32,56/// Base address at which to load the data segment into `DMEM`.57pub(crate) dmem_phys_base: u32,58/// Size in bytes of the data to copy into `DMEM`.59pub(crate) dmem_load_size: u32,60/// Mask of the falcon engines on which this firmware can run.61pub(crate) engine_id_mask: u16,62/// ID of the ucode used to infer a fuse register to validate the signature.63pub(crate) ucode_id: u8,64/// Number of signatures in this firmware.65pub(crate) signature_count: u8,66/// Versions of the signatures, used to infer a valid signature to use.67pub(crate) signature_versions: u16,68_reserved: u16,69}7071impl FalconUCodeDescV3 {72/// Returns the size in bytes of the header.73pub(crate) fn size(&self) -> usize {74const HDR_SIZE_SHIFT: u32 = 16;75const HDR_SIZE_MASK: u32 = 0xffff0000;7677((self.hdr & HDR_SIZE_MASK) >> HDR_SIZE_SHIFT) as usize78}79}8081/// Trait implemented by types defining the signed state of a firmware.82trait SignedState {}8384/// Type indicating that the firmware must be signed before it can be used.85struct Unsigned;86impl SignedState for Unsigned {}8788/// Type indicating that the firmware is signed and ready to be loaded.89struct Signed;90impl SignedState for Signed {}9192/// A [`DmaObject`] containing a specific microcode ready to be loaded into a falcon.93///94/// This is module-local and meant for sub-modules to use internally.95///96/// After construction, a firmware is [`Unsigned`], and must generally be patched with a signature97/// before it can be loaded (with an exception for development hardware). The98/// [`Self::patch_signature`] and [`Self::no_patch_signature`] methods are used to transition the99/// firmware to its [`Signed`] state.100struct FirmwareDmaObject<F: FalconFirmware, S: SignedState>(DmaObject, PhantomData<(F, S)>);101102/// Trait for signatures to be patched directly into a given firmware.103///104/// This is module-local and meant for sub-modules to use internally.105trait FirmwareSignature<F: FalconFirmware>: AsRef<[u8]> {}106107impl<F: FalconFirmware> FirmwareDmaObject<F, Unsigned> {108/// Patches the firmware at offset `sig_base_img` with `signature`.109fn patch_signature<S: FirmwareSignature<F>>(110mut self,111signature: &S,112sig_base_img: usize,113) -> Result<FirmwareDmaObject<F, Signed>> {114let signature_bytes = signature.as_ref();115if sig_base_img + signature_bytes.len() > self.0.size() {116return Err(EINVAL);117}118119// SAFETY: We are the only user of this object, so there cannot be any race.120let dst = unsafe { self.0.start_ptr_mut().add(sig_base_img) };121122// SAFETY: `signature` and `dst` are valid, properly aligned, and do not overlap.123unsafe {124core::ptr::copy_nonoverlapping(signature_bytes.as_ptr(), dst, signature_bytes.len())125};126127Ok(FirmwareDmaObject(self.0, PhantomData))128}129130/// Mark the firmware as signed without patching it.131///132/// This method is used to explicitly confirm that we do not need to sign the firmware, while133/// allowing us to continue as if it was. This is typically only needed for development134/// hardware.135fn no_patch_signature(self) -> FirmwareDmaObject<F, Signed> {136FirmwareDmaObject(self.0, PhantomData)137}138}139140/// Header common to most firmware files.141#[repr(C)]142#[derive(Debug, Clone)]143struct BinHdr {144/// Magic number, must be `0x10de`.145bin_magic: u32,146/// Version of the header.147bin_ver: u32,148/// Size in bytes of the binary (to be ignored).149bin_size: u32,150/// Offset of the start of the application-specific header.151header_offset: u32,152/// Offset of the start of the data payload.153data_offset: u32,154/// Size in bytes of the data payload.155data_size: u32,156}157158// SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.159unsafe impl FromBytes for BinHdr {}160161// A firmware blob starting with a `BinHdr`.162struct BinFirmware<'a> {163hdr: BinHdr,164fw: &'a [u8],165}166167impl<'a> BinFirmware<'a> {168/// Interpret `fw` as a firmware image starting with a [`BinHdr`], and returns the169/// corresponding [`BinFirmware`] that can be used to extract its payload.170fn new(fw: &'a firmware::Firmware) -> Result<Self> {171const BIN_MAGIC: u32 = 0x10de;172let fw = fw.data();173174fw.get(0..size_of::<BinHdr>())175// Extract header.176.and_then(BinHdr::from_bytes_copy)177// Validate header.178.and_then(|hdr| {179if hdr.bin_magic == BIN_MAGIC {180Some(hdr)181} else {182None183}184})185.map(|hdr| Self { hdr, fw })186.ok_or(EINVAL)187}188189/// Returns the data payload of the firmware, or `None` if the data range is out of bounds of190/// the firmware image.191fn data(&self) -> Option<&[u8]> {192let fw_start = self.hdr.data_offset as usize;193let fw_size = self.hdr.data_size as usize;194195self.fw.get(fw_start..fw_start + fw_size)196}197}198199pub(crate) struct ModInfoBuilder<const N: usize>(firmware::ModInfoBuilder<N>);200201impl<const N: usize> ModInfoBuilder<N> {202const fn make_entry_file(self, chipset: &str, fw: &str) -> Self {203ModInfoBuilder(204self.0205.new_entry()206.push("nvidia/")207.push(chipset)208.push("/gsp/")209.push(fw)210.push("-")211.push(FIRMWARE_VERSION)212.push(".bin"),213)214}215216const fn make_entry_chipset(self, chipset: &str) -> Self {217self.make_entry_file(chipset, "booter_load")218.make_entry_file(chipset, "booter_unload")219.make_entry_file(chipset, "bootloader")220.make_entry_file(chipset, "gsp")221}222223pub(crate) const fn create(224module_name: &'static kernel::str::CStr,225) -> firmware::ModInfoBuilder<N> {226let mut this = Self(firmware::ModInfoBuilder::new(module_name));227let mut i = 0;228229while i < gpu::Chipset::ALL.len() {230this = this.make_entry_chipset(gpu::Chipset::ALL[i].name());231i += 1;232}233234this.0235}236}237238239