summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexandre Courbot <acourbot@nvidia.com>2025-10-29 00:07:40 +0900
committerAlexandre Courbot <acourbot@nvidia.com>2025-11-05 20:29:47 +0900
commitade19c5060dfa39b84a9475a4a6b05e2a8a2b3ac (patch)
treea2133b693109950dcfd7b17f5014ee6bc35c5700
parent46768644a164f0f5eaa06fdf93718edcbbc47b64 (diff)
gpu: nova-core: vbios: use FromBytes for NpdeStruct
Use `from_bytes_copy_prefix` to create `NpdeStruct` instead of building it ourselves from the bytes stream. This lets us remove a few array accesses and results in shorter code. Reviewed-by: Joel Fernandes <joelagnelf@nvidia.com> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com> Message-ID: <20251029-nova-vbios-frombytes-v1-5-ac441ebc1de3@nvidia.com>
-rw-r--r--drivers/gpu/nova-core/vbios.rs30
1 files changed, 12 insertions, 18 deletions
diff --git a/drivers/gpu/nova-core/vbios.rs b/drivers/gpu/nova-core/vbios.rs
index 0efd2502c230..aec9166ffb45 100644
--- a/drivers/gpu/nova-core/vbios.rs
+++ b/drivers/gpu/nova-core/vbios.rs
@@ -537,35 +537,29 @@ struct NpdeStruct {
last_image: u8,
}
+// SAFETY: all bit patterns are valid for `NpdeStruct`.
+unsafe impl FromBytes for NpdeStruct {}
+
impl NpdeStruct {
fn new(dev: &device::Device, data: &[u8]) -> Option<Self> {
- if data.len() < core::mem::size_of::<Self>() {
- dev_dbg!(dev, "Not enough data for NpdeStruct\n");
- return None;
- }
-
- let mut signature = [0u8; 4];
- signature.copy_from_slice(&data[0..4]);
+ let (npde, _) = NpdeStruct::from_bytes_copy_prefix(data)?;
// Signature should be "NPDE" (0x4544504E).
- if &signature != b"NPDE" {
- dev_dbg!(dev, "Invalid signature for NpdeStruct: {:?}\n", signature);
+ if &npde.signature != b"NPDE" {
+ dev_dbg!(
+ dev,
+ "Invalid signature for NpdeStruct: {:?}\n",
+ npde.signature
+ );
return None;
}
- let subimage_len = u16::from_le_bytes([data[8], data[9]]);
- if subimage_len == 0 {
+ if npde.subimage_len == 0 {
dev_dbg!(dev, "Invalid subimage length: 0\n");
return None;
}
- Some(NpdeStruct {
- signature,
- npci_data_ext_rev: u16::from_le_bytes([data[4], data[5]]),
- npci_data_ext_len: u16::from_le_bytes([data[6], data[7]]),
- subimage_len,
- last_image: data[10],
- })
+ Some(npde)
}
/// Check if this is the last image in the ROM.