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