[PATCH 4/7] drm/nova: use `zerocopy` in fwsec.rs
Pedro Yudi Honda
niyudi.honda em usp.br
Seg Jun 22 15:40:24 -03 2026
From: Pedro Yudi Honda <niyudi.honda em usp.br>
In firmware/fwsec.rs, replace the following `transmute` traits with
their `zerocopy` equivalents:
- `transmute::FromBytes` -> `zerocopy::FromBytes`
- `transmute::AsBytes` -> `zerocopy::IntoBytes`
- add `zerocopy::KnownLayout` where necessary
Update call sites accordingly.
Signed-off-by: Pedro Yudi Honda <niyudi.honda em usp.br>
---
drivers/gpu/nova-core/firmware/fwsec.rs | 47 ++++++-------------------
1 file changed, 11 insertions(+), 36 deletions(-)
diff --git a/drivers/gpu/nova-core/firmware/fwsec.rs b/drivers/gpu/nova-core/firmware/fwsec.rs
index 8810cb49db67..b2a8785658fb 100644
--- a/drivers/gpu/nova-core/firmware/fwsec.rs
+++ b/drivers/gpu/nova-core/firmware/fwsec.rs
@@ -20,10 +20,6 @@
Device, //
},
prelude::*,
- transmute::{
- AsBytes,
- FromBytes, //
- },
};
use crate::{
@@ -50,26 +46,22 @@
const NVFW_FALCON_APPIF_ID_DMEMMAPPER: u32 = 0x4;
#[repr(C)]
-#[derive(Debug)]
+#[derive(Debug, FromBytes)]
struct FalconAppifHdrV1 {
version: u8,
header_size: u8,
entry_size: u8,
entry_count: u8,
}
-// SAFETY: Any byte sequence is valid for this struct.
-unsafe impl FromBytes for FalconAppifHdrV1 {}
#[repr(C, packed)]
-#[derive(Debug)]
+#[derive(Debug, FromBytes)]
struct FalconAppifV1 {
id: u32,
dmem_base: u32,
}
-// SAFETY: Any byte sequence is valid for this struct.
-unsafe impl FromBytes for FalconAppifV1 {}
-#[derive(Debug)]
+#[derive(Debug, FromBytes, IntoBytes, zerocopy_derive::KnownLayout)]
#[repr(C, packed)]
struct FalconAppifDmemmapperV3 {
signature: u32,
@@ -90,12 +82,8 @@ struct FalconAppifDmemmapperV3 {
ucode_cmd_mask1: u32,
multi_tgt_tbl: u32,
}
-// SAFETY: Any byte sequence is valid for this struct.
-unsafe impl FromBytes for FalconAppifDmemmapperV3 {}
-// SAFETY: This struct doesn't contain uninitialized bytes and doesn't have interior mutability.
-unsafe impl AsBytes for FalconAppifDmemmapperV3 {}
-#[derive(Debug)]
+#[derive(Debug, FromBytes, IntoBytes, zerocopy_derive::KnownLayout)]
#[repr(C, packed)]
struct ReadVbios {
ver: u32,
@@ -104,12 +92,8 @@ struct ReadVbios {
size: u32,
flags: u32,
}
-// SAFETY: Any byte sequence is valid for this struct.
-unsafe impl FromBytes for ReadVbios {}
-// SAFETY: This struct doesn't contain uninitialized bytes and doesn't have interior mutability.
-unsafe impl AsBytes for ReadVbios {}
-#[derive(Debug)]
+#[derive(Debug, FromBytes, IntoBytes, zerocopy_derive::KnownLayout)]
#[repr(C, packed)]
struct FrtsRegion {
ver: u32,
@@ -118,22 +102,15 @@ struct FrtsRegion {
size: u32,
ftype: u32,
}
-// SAFETY: Any byte sequence is valid for this struct.
-unsafe impl FromBytes for FrtsRegion {}
-// SAFETY: This struct doesn't contain uninitialized bytes and doesn't have interior mutability.
-unsafe impl AsBytes for FrtsRegion {}
const NVFW_FRTS_CMD_REGION_TYPE_FB: u32 = 2;
#[repr(C, packed)]
+#[derive(FromBytes, IntoBytes, zerocopy_derive::KnownLayout)]
struct FrtsCmd {
read_vbios: ReadVbios,
frts_region: FrtsRegion,
}
-// SAFETY: Any byte sequence is valid for this struct.
-unsafe impl FromBytes for FrtsCmd {}
-// SAFETY: This struct doesn't contain uninitialized bytes and doesn't have interior mutability.
-unsafe impl AsBytes for FrtsCmd {}
const NVFW_FALCON_APPIF_DMEMMAPPER_CMD_FRTS: u32 = 0x15;
const NVFW_FALCON_APPIF_DMEMMAPPER_CMD_SB: u32 = 0x19;
@@ -153,11 +130,9 @@ pub(crate) enum FwsecCommand {
/// A single signature that can be patched into a FWSEC image.
#[repr(transparent)]
+#[derive(FromBytes)]
pub(crate) struct Bcrt30Rsa3kSignature([u8; BCRT30_RSA3K_SIG_SIZE]);
-/// SAFETY: A signature is just an array of bytes.
-unsafe impl FromBytes for Bcrt30Rsa3kSignature {}
-
impl From<[u8; BCRT30_RSA3K_SIG_SIZE]> for Bcrt30Rsa3kSignature {
fn from(sig: [u8; BCRT30_RSA3K_SIG_SIZE]) -> Self {
Self(sig)
@@ -230,7 +205,7 @@ fn new_fwsec(bios: &Vbios, cmd: FwsecCommand) -> Result<Self> {
let hdr = ucode
.get(hdr_offset..)
- .and_then(FalconAppifHdrV1::from_bytes_prefix)
+ .and_then(|b| FalconAppifHdrV1::read_from_prefix(b).ok())
.ok_or(EINVAL)?
.0;
@@ -248,7 +223,7 @@ fn new_fwsec(bios: &Vbios, cmd: FwsecCommand) -> Result<Self> {
let app = ucode
.get(entry_offset..)
- .and_then(FalconAppifV1::from_bytes_prefix)
+ .and_then(|b| FalconAppifV1::read_from_prefix(b).ok())
.ok_or(EINVAL)?
.0;
@@ -265,7 +240,7 @@ fn new_fwsec(bios: &Vbios, cmd: FwsecCommand) -> Result<Self> {
let dmem_mapper = ucode
.get_mut(dmem_mapper_offset..)
- .and_then(FalconAppifDmemmapperV3::from_bytes_mut_prefix)
+ .and_then(|b| FalconAppifDmemmapperV3::mut_from_prefix(b).ok())
.ok_or(EINVAL)?
.0;
@@ -283,7 +258,7 @@ fn new_fwsec(bios: &Vbios, cmd: FwsecCommand) -> Result<Self> {
let frts_cmd = ucode
.get_mut(frts_cmd_offset..)
- .and_then(FrtsCmd::from_bytes_mut_prefix)
+ .and_then(|b| FrtsCmd::mut_from_prefix(b).ok())
.ok_or(EINVAL)?
.0;
--
2.34.1
Mais detalhes sobre a lista de discussão kernel