opentitanlib/chip/
device_id.rs1use anyhow::Result;
6use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
7use serde::Serialize;
8use serde_annotate::Annotate;
9use std::io::{Read, Write};
10
11#[derive(Debug, Default, Serialize, Annotate)]
12pub struct DeviceId {
13 #[annotate(format=hex)]
14 pub creator: u16,
15 #[annotate(format=hex)]
16 pub product: u16,
17 #[annotate(format=hex)]
18 pub din: u64,
19 #[annotate(format=hex)]
20 pub crc32: u32,
21 #[annotate(format=hex)]
22 pub sku_specific: [u32; 4],
23}
24
25impl DeviceId {
26 pub fn read(src: &mut impl Read) -> Result<Self> {
27 let creator = src.read_u16::<LittleEndian>()?;
28 let product = src.read_u16::<LittleEndian>()?;
29 let din = src.read_u64::<LittleEndian>()?;
30 let crc32 = src.read_u32::<LittleEndian>()?;
31 let mut sku_specific = [0u32; 4];
32 src.read_u32_into::<LittleEndian>(&mut sku_specific)?;
33 Ok(Self {
34 creator,
35 product,
36 din,
37 crc32,
38 sku_specific,
39 })
40 }
41
42 pub fn write(&self, dest: &mut impl Write) -> Result<()> {
43 dest.write_u16::<LittleEndian>(self.creator)?;
44 dest.write_u16::<LittleEndian>(self.product)?;
45 dest.write_u64::<LittleEndian>(self.din)?;
46 dest.write_u32::<LittleEndian>(self.crc32)?;
47 for sku_specific in &self.sku_specific {
48 dest.write_u32::<LittleEndian>(*sku_specific)?;
49 }
50 Ok(())
51 }
52}