opentitanlib/chip/
device_id.rs

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