xref: /openbmc/smbios-mdr/src/tpm.cpp (revision 5af42bbf374907888c09d7aaf2cbf74d74c7707d)
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     if (!motherboardPath.empty())
48     {
49         std::vector<std::tuple<std::string, std::string, std::string>> assocs;
50         assocs.emplace_back("chassis", "trusted_components", motherboardPath);
51         association::associations(assocs);
52     }
53 }
54 
tpmVendor(const struct TPMInfo * tpmInfo)55 void Tpm::tpmVendor(const struct TPMInfo* tpmInfo)
56 {
57     constexpr int vendorIdLength = 4;
58     // Specified as four ASCII characters, as defined by TCG Vendor ID
59     char vendorId[vendorIdLength + 1];
60     int i;
61     for (i = 0; i < vendorIdLength && tpmInfo->vendor[i] != '\0'; i++)
62     {
63         if (std::isprint(tpmInfo->vendor[i]))
64         {
65             vendorId[i] = tpmInfo->vendor[i];
66         }
67         else
68         {
69             vendorId[i] = '.';
70         }
71     }
72     vendorId[i] = '\0';
73     manufacturer(vendorId);
74 }
75 
tpmFirmwareVersion(const struct TPMInfo * tpmInfo)76 void Tpm::tpmFirmwareVersion(const struct TPMInfo* tpmInfo)
77 {
78     std::stringstream stream;
79 
80     if (tpmInfo->specMajor == tpmMajorVerion1)
81     {
82         auto ver = reinterpret_cast<const struct TPMVersionSpec1*>(
83             &tpmInfo->firmwareVersion1);
84         stream << ver->revMajor << "." << ver->revMinor;
85     }
86     else if (tpmInfo->specMajor == tpmMajorVerion2)
87     {
88         auto ver = reinterpret_cast<const struct TPMVersionSpec2*>(
89             &tpmInfo->firmwareVersion1);
90         stream << ver->revMajor << "." << ver->revMinor;
91     }
92     version(stream.str());
93 }
94 
tpmDescription(const uint8_t positionNum,const uint8_t structLen,uint8_t * dataIn)95 void Tpm::tpmDescription(const uint8_t positionNum, const uint8_t structLen,
96                          uint8_t* dataIn)
97 {
98     std::string result = positionToString(positionNum, structLen, dataIn);
99     prettyName(result);
100 }
101 } // namespace smbios
102 } // namespace phosphor
103