Commit ade19c50 authored by Alexandre Courbot's avatar Alexandre Courbot
Browse files

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: default avatarJoel Fernandes <joelagnelf@nvidia.com>
Signed-off-by: default avatarAlexandre Courbot <acourbot@nvidia.com>
Message-ID: <20251029-nova-vbios-frombytes-v1-5-ac441ebc1de3@nvidia.com>
parent 46768644
Loading
Loading
Loading
Loading
+12 −18
Original line number Diff line number Diff line
@@ -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.