xref: /openbmc/smbios-mdr/src/tpm.cpp (revision 6a84b4c81021742b79f5219a8925dfd98a29b9eb)
1 #include "tpm.hpp"
2 
3 #include "mdrv2.hpp"
4 
5 #include <fstream>
6 #include <iomanip>
7 #include <iostream>
8 #include <sstream>
9 
10 namespace phosphor
11 {
12 namespace smbios
13 {
14 
tpmInfoUpdate(uint8_t * smbiosTableStorage,const std::string & motherboard)15 void Tpm::tpmInfoUpdate(uint8_t* smbiosTableStorage,
16                         const std::string& motherboard)
17 {
18     storage = smbiosTableStorage;
19     motherboardPath = motherboard;
20 
21     uint8_t* dataIn = storage;
22     dataIn = getSMBIOSTypePtr(dataIn, tpmDeviceType);
23     if (dataIn == nullptr)
24     {
25         return;
26     }
27     for (uint8_t index = 0; index < tpmId; index++)
28     {
29         dataIn = smbiosNextPtr(dataIn);
30         if (dataIn == nullptr)
31         {
32             return;
33         }
34         dataIn = getSMBIOSTypePtr(dataIn, tpmDeviceType);
35         if (dataIn == nullptr)
36         {
37             return;
38         }
39     }
40     auto tpmInfo = reinterpret_cast<struct TPMInfo*>(dataIn);
41 
42     present(true);
43     purpose(softwareversion::VersionPurpose::Other);
44     tpmVendor(tpmInfo);
45     tpmFirmwareVersion(tpmInfo);
46     tpmDescription(tpmInfo->description, tpmInfo->length, dataIn);
47     trustedComponentType(trustedComponent::ComponentAttachType::Discrete);
48 }
49 
tpmVendor(const struct TPMInfo * tpmInfo)50 void Tpm::tpmVendor(const struct TPMInfo* tpmInfo)
51 {
52     constexpr int vendorIdLength = 4;
53     // Specified as four ASCII characters, as defined by TCG Vendor ID
54     char vendorId[vendorIdLength + 1];
55     int i;
56     for (i = 0; i < vendorIdLength && tpmInfo->vendor[i] != '\0'; i++)
57     {
58         if (std::isprint(tpmInfo->vendor[i]))
59         {
60             vendorId[i] = tpmInfo->vendor[i];
61         }
62         else
63         {
64             vendorId[i] = '.';
65         }
66     }
67     vendorId[i] = '\0';
68     manufacturer(vendorId);
69 }
70 
tpmFirmwareVersion(const struct TPMInfo * tpmInfo)71 void Tpm::tpmFirmwareVersion(const struct TPMInfo* tpmInfo)
72 {
73     std::stringstream stream;
74 
75     if (tpmInfo->specMajor == tpmMajorVerion1)
76     {
77         auto ver = reinterpret_cast<const struct TPMVersionSpec1*>(
78             &tpmInfo->firmwareVersion1);
79         stream << ver->revMajor << "." << ver->revMinor;
80     }
81     else if (tpmInfo->specMajor == tpmMajorVerion2)
82     {
83         auto ver = reinterpret_cast<const struct TPMVersionSpec2*>(
84             &tpmInfo->firmwareVersion1);
85         stream << ver->revMajor << "." << ver->revMinor;
86     }
87     version(stream.str());
88 }
89 
tpmDescription(const uint8_t positionNum,const uint8_t structLen,uint8_t * dataIn)90 void Tpm::tpmDescription(const uint8_t positionNum, const uint8_t structLen,
91                          uint8_t* dataIn)
92 {
93     std::string result = positionToString(positionNum, structLen, dataIn);
94     prettyName(result);
95 }
96 } // namespace smbios
97 } // namespace phosphor
98