1 // SPDX-License-Identifier: GPL-2.0 2 3 //! Contains structures and functions dedicated to the parsing, building and patching of firmwares 4 //! to be loaded into a given execution unit. 5 6 use core::marker::PhantomData; 7 use core::mem::size_of; 8 9 use kernel::device; 10 use kernel::firmware; 11 use kernel::prelude::*; 12 use kernel::str::CString; 13 use kernel::transmute::FromBytes; 14 15 use crate::dma::DmaObject; 16 use crate::falcon::FalconFirmware; 17 use crate::gpu; 18 use crate::gpu::Chipset; 19 20 pub(crate) mod booter; 21 pub(crate) mod fwsec; 22 23 pub(crate) const FIRMWARE_VERSION: &str = "535.113.01"; 24 25 /// Requests the GPU firmware `name` suitable for `chipset`, with version `ver`. 26 fn request_firmware( 27 dev: &device::Device, 28 chipset: gpu::Chipset, 29 name: &str, 30 ver: &str, 31 ) -> Result<firmware::Firmware> { 32 let chip_name = chipset.name(); 33 34 CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{name}-{ver}.bin")) 35 .and_then(|path| firmware::Firmware::request(&path, dev)) 36 } 37 38 /// Structure encapsulating the firmware blobs required for the GPU to operate. 39 #[expect(dead_code)] 40 pub(crate) struct Firmware { 41 bootloader: firmware::Firmware, 42 gsp: firmware::Firmware, 43 } 44 45 impl Firmware { 46 pub(crate) fn new(dev: &device::Device, chipset: Chipset, ver: &str) -> Result<Firmware> { 47 let request = |name| request_firmware(dev, chipset, name, ver); 48 49 Ok(Firmware { 50 bootloader: request("bootloader")?, 51 gsp: request("gsp")?, 52 }) 53 } 54 } 55 56 /// Structure used to describe some firmwares, notably FWSEC-FRTS. 57 #[repr(C)] 58 #[derive(Debug, Clone)] 59 pub(crate) struct FalconUCodeDescV3 { 60 /// Header defined by `NV_BIT_FALCON_UCODE_DESC_HEADER_VDESC*` in OpenRM. 61 hdr: u32, 62 /// Stored size of the ucode after the header. 63 stored_size: u32, 64 /// Offset in `DMEM` at which the signature is expected to be found. 65 pub(crate) pkc_data_offset: u32, 66 /// Offset after the code segment at which the app headers are located. 67 pub(crate) interface_offset: u32, 68 /// Base address at which to load the code segment into `IMEM`. 69 pub(crate) imem_phys_base: u32, 70 /// Size in bytes of the code to copy into `IMEM`. 71 pub(crate) imem_load_size: u32, 72 /// Virtual `IMEM` address (i.e. `tag`) at which the code should start. 73 pub(crate) imem_virt_base: u32, 74 /// Base address at which to load the data segment into `DMEM`. 75 pub(crate) dmem_phys_base: u32, 76 /// Size in bytes of the data to copy into `DMEM`. 77 pub(crate) dmem_load_size: u32, 78 /// Mask of the falcon engines on which this firmware can run. 79 pub(crate) engine_id_mask: u16, 80 /// ID of the ucode used to infer a fuse register to validate the signature. 81 pub(crate) ucode_id: u8, 82 /// Number of signatures in this firmware. 83 pub(crate) signature_count: u8, 84 /// Versions of the signatures, used to infer a valid signature to use. 85 pub(crate) signature_versions: u16, 86 _reserved: u16, 87 } 88 89 impl FalconUCodeDescV3 { 90 /// Returns the size in bytes of the header. 91 pub(crate) fn size(&self) -> usize { 92 const HDR_SIZE_SHIFT: u32 = 16; 93 const HDR_SIZE_MASK: u32 = 0xffff0000; 94 95 ((self.hdr & HDR_SIZE_MASK) >> HDR_SIZE_SHIFT) as usize 96 } 97 } 98 99 /// Trait implemented by types defining the signed state of a firmware. 100 trait SignedState {} 101 102 /// Type indicating that the firmware must be signed before it can be used. 103 struct Unsigned; 104 impl SignedState for Unsigned {} 105 106 /// Type indicating that the firmware is signed and ready to be loaded. 107 struct Signed; 108 impl SignedState for Signed {} 109 110 /// A [`DmaObject`] containing a specific microcode ready to be loaded into a falcon. 111 /// 112 /// This is module-local and meant for sub-modules to use internally. 113 /// 114 /// After construction, a firmware is [`Unsigned`], and must generally be patched with a signature 115 /// before it can be loaded (with an exception for development hardware). The 116 /// [`Self::patch_signature`] and [`Self::no_patch_signature`] methods are used to transition the 117 /// firmware to its [`Signed`] state. 118 struct FirmwareDmaObject<F: FalconFirmware, S: SignedState>(DmaObject, PhantomData<(F, S)>); 119 120 /// Trait for signatures to be patched directly into a given firmware. 121 /// 122 /// This is module-local and meant for sub-modules to use internally. 123 trait FirmwareSignature<F: FalconFirmware>: AsRef<[u8]> {} 124 125 impl<F: FalconFirmware> FirmwareDmaObject<F, Unsigned> { 126 /// Patches the firmware at offset `sig_base_img` with `signature`. 127 fn patch_signature<S: FirmwareSignature<F>>( 128 mut self, 129 signature: &S, 130 sig_base_img: usize, 131 ) -> Result<FirmwareDmaObject<F, Signed>> { 132 let signature_bytes = signature.as_ref(); 133 if sig_base_img + signature_bytes.len() > self.0.size() { 134 return Err(EINVAL); 135 } 136 137 // SAFETY: We are the only user of this object, so there cannot be any race. 138 let dst = unsafe { self.0.start_ptr_mut().add(sig_base_img) }; 139 140 // SAFETY: `signature` and `dst` are valid, properly aligned, and do not overlap. 141 unsafe { 142 core::ptr::copy_nonoverlapping(signature_bytes.as_ptr(), dst, signature_bytes.len()) 143 }; 144 145 Ok(FirmwareDmaObject(self.0, PhantomData)) 146 } 147 148 /// Mark the firmware as signed without patching it. 149 /// 150 /// This method is used to explicitly confirm that we do not need to sign the firmware, while 151 /// allowing us to continue as if it was. This is typically only needed for development 152 /// hardware. 153 fn no_patch_signature(self) -> FirmwareDmaObject<F, Signed> { 154 FirmwareDmaObject(self.0, PhantomData) 155 } 156 } 157 158 /// Header common to most firmware files. 159 #[repr(C)] 160 #[derive(Debug, Clone)] 161 struct BinHdr { 162 /// Magic number, must be `0x10de`. 163 bin_magic: u32, 164 /// Version of the header. 165 bin_ver: u32, 166 /// Size in bytes of the binary (to be ignored). 167 bin_size: u32, 168 /// Offset of the start of the application-specific header. 169 header_offset: u32, 170 /// Offset of the start of the data payload. 171 data_offset: u32, 172 /// Size in bytes of the data payload. 173 data_size: u32, 174 } 175 176 // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability. 177 unsafe impl FromBytes for BinHdr {} 178 179 // A firmware blob starting with a `BinHdr`. 180 struct BinFirmware<'a> { 181 hdr: BinHdr, 182 fw: &'a [u8], 183 } 184 185 impl<'a> BinFirmware<'a> { 186 /// Interpret `fw` as a firmware image starting with a [`BinHdr`], and returns the 187 /// corresponding [`BinFirmware`] that can be used to extract its payload. 188 fn new(fw: &'a firmware::Firmware) -> Result<Self> { 189 const BIN_MAGIC: u32 = 0x10de; 190 let fw = fw.data(); 191 192 fw.get(0..size_of::<BinHdr>()) 193 // Extract header. 194 .and_then(BinHdr::from_bytes_copy) 195 // Validate header. 196 .and_then(|hdr| { 197 if hdr.bin_magic == BIN_MAGIC { 198 Some(hdr) 199 } else { 200 None 201 } 202 }) 203 .map(|hdr| Self { hdr, fw }) 204 .ok_or(EINVAL) 205 } 206 207 /// Returns the data payload of the firmware, or `None` if the data range is out of bounds of 208 /// the firmware image. 209 fn data(&self) -> Option<&[u8]> { 210 let fw_start = self.hdr.data_offset as usize; 211 let fw_size = self.hdr.data_size as usize; 212 213 self.fw.get(fw_start..fw_start + fw_size) 214 } 215 } 216 217 pub(crate) struct ModInfoBuilder<const N: usize>(firmware::ModInfoBuilder<N>); 218 219 impl<const N: usize> ModInfoBuilder<N> { 220 const fn make_entry_file(self, chipset: &str, fw: &str) -> Self { 221 ModInfoBuilder( 222 self.0 223 .new_entry() 224 .push("nvidia/") 225 .push(chipset) 226 .push("/gsp/") 227 .push(fw) 228 .push("-") 229 .push(FIRMWARE_VERSION) 230 .push(".bin"), 231 ) 232 } 233 234 const fn make_entry_chipset(self, chipset: &str) -> Self { 235 self.make_entry_file(chipset, "booter_load") 236 .make_entry_file(chipset, "booter_unload") 237 .make_entry_file(chipset, "bootloader") 238 .make_entry_file(chipset, "gsp") 239 } 240 241 pub(crate) const fn create( 242 module_name: &'static kernel::str::CStr, 243 ) -> firmware::ModInfoBuilder<N> { 244 let mut this = Self(firmware::ModInfoBuilder::new(module_name)); 245 let mut i = 0; 246 247 while i < gpu::Chipset::ALL.len() { 248 this = this.make_entry_chipset(gpu::Chipset::ALL[i].name()); 249 i += 1; 250 } 251 252 this.0 253 } 254 } 255