1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (c) 2020 Takashi Sakamoto

//! Parser for Configuration ROM according to specification defined by TCAT for ASICs of DICE.
//!
//! The module includes structure, enumeration, and trait and its implementaion to parse
//! configuration ROM according to specification defined by TCAT for ASICs of DICE.

use ieee1212_config_rom::*;

/// Identifier in bus information block of Configuration ROM.
#[derive(Default, Debug, Clone, Copy)]
pub struct Identifier {
    /// The numeric identifier of vendor, usually Organizationally Unique Identifier (OUI)
    /// registered in IEEE.
    pub vendor_id: u32,
    /// The numeric value of category.
    pub category: u8,
    /// The numeric identifier of product.
    pub product_id: u16,
    /// The serial number.
    pub serial: u32,
}

/// Data in root directory block of Configuration ROM.
#[derive(Default, Debug, Clone, Copy)]
pub struct RootData<'a> {
    /// The numeric identifier of vendor, usually Organizationally Unique Identifier (OUI)
    /// registered in IEEE.
    pub vendor_id: u32,
    /// The name of vendor.
    pub vendor_name: &'a str,
    /// The numeric identifier of product.
    pub product_id: u32,
    /// The name of product.
    pub product_name: &'a str,
}

/// Data in unit directory block of Configuration ROM.
#[derive(Default, Debug, Clone, Copy)]
pub struct UnitData<'a> {
    /// The numeric identifier of model.
    pub model_id: u32,
    /// The name of model.
    pub model_name: &'a str,
    /// The specifier identifier.
    pub specifier_id: u32,
    /// Version.
    pub version: u32,
}

const VENDOR_ID_MASK: u32 = 0xffffff00;
const VENDOR_ID_SHIFT: usize = 8;
const CATEGORY_MASK: u32 = 0x000000ff;
const CATEGORY_SHIFT: usize = 0;
const PRODUCT_ID_MASK: u32 = 0xffc00000;
const PRODUCT_ID_SHIFT: usize = 22;
const SERIAL_MASK: u32 = 0x003fffff;
const SERIAL_SHIFT: usize = 0;

/// Parser of configuration ROM which has layout specific to DICE.
pub trait DiceConfigRom<'a> {
    /// Get identifier.
    fn get_identifier(&self) -> Option<Identifier>;
    /// Get data in root directory.
    fn get_root_data(&'a self) -> Option<RootData<'a>>;
    /// Get data in unit directory.
    fn get_unit_data(&'a self) -> Option<UnitData<'a>>;
}

impl<'a> DiceConfigRom<'a> for ConfigRom<'a> {
    fn get_identifier(&self) -> Option<Identifier> {
        if self.bus_info.len() < 12 {
            None
        } else {
            let mut quadlet = [0; 4];

            quadlet.copy_from_slice(&self.bus_info[8..12]);
            let val = u32::from_be_bytes(quadlet);
            let vendor_id = (val & VENDOR_ID_MASK) >> VENDOR_ID_SHIFT;
            let category = ((val & CATEGORY_MASK) >> CATEGORY_SHIFT) as u8;

            quadlet.copy_from_slice(&self.bus_info[12..16]);
            let val = u32::from_be_bytes(quadlet);
            let product_id = ((val & PRODUCT_ID_MASK) >> PRODUCT_ID_SHIFT) as u16;
            let serial = (val & SERIAL_MASK) >> SERIAL_SHIFT;

            Some(Identifier {
                vendor_id,
                category,
                product_id,
                serial,
            })
        }
    }

    fn get_root_data(&'a self) -> Option<RootData<'a>> {
        detect_desc_text(&self.root, KeyType::Vendor).and_then(|(vendor_id, vendor_name)| {
            detect_desc_text(&self.root, KeyType::Model).map(|(product_id, product_name)| {
                RootData {
                    vendor_id,
                    vendor_name,
                    product_id,
                    product_name,
                }
            })
        })
    }

    fn get_unit_data(&'a self) -> Option<UnitData<'a>> {
        self.root
            .iter()
            .find_map(|entry| EntryDataAccess::<&[Entry]>::get(entry, KeyType::Unit))
            .and_then(|entries| {
                entries
                    .iter()
                    .find_map(|entry| EntryDataAccess::<u32>::get(entry, KeyType::SpecifierId))
                    .and_then(|specifier_id| {
                        entries
                            .iter()
                            .find_map(|entry| EntryDataAccess::<u32>::get(entry, KeyType::Version))
                            .and_then(|version| {
                                detect_desc_text(entries, KeyType::Model).map(
                                    |(model_id, model_name)| UnitData {
                                        model_id,
                                        model_name,
                                        specifier_id,
                                        version,
                                    },
                                )
                            })
                    })
            })
    }
}

fn detect_desc_text<'a>(entries: &'a [Entry], key_type: KeyType) -> Option<(u32, &'a str)> {
    let mut peekable = entries.iter().peekable();

    while let Some(entry) = peekable.next() {
        let result = EntryDataAccess::<u32>::get(entry, key_type).and_then(|value| {
            peekable.peek().and_then(|&next| {
                EntryDataAccess::<&str>::get(next, KeyType::Descriptor).map(|name| (value, name))
            })
        });

        if result.is_some() {
            return result;
        }
    }

    None
}