xref: /openbmc/phosphor-bmc-code-mgmt/tpm/tpm_device.cpp (revision c538727d70f3673771f18e559d7ecab203abf6d3)
1 #include "tpm_device.hpp"
2 
3 #include "tpm2/tpm2.hpp"
4 
5 #include <phosphor-logging/lg2.hpp>
6 #include <sdbusplus/async.hpp>
7 
8 PHOSPHOR_LOG2_USING;
9 
TPMDevice(sdbusplus::async::context & ctx,enum TPMType tpmType,uint8_t tpmIndex,SoftwareConfig & config,ManagerInf::SoftwareManager * parent)10 TPMDevice::TPMDevice(sdbusplus::async::context& ctx, enum TPMType tpmType,
11                      uint8_t tpmIndex, SoftwareConfig& config,
12                      ManagerInf::SoftwareManager* parent) :
13     Device(ctx, config, parent, {RequestedApplyTimes::OnReset})
14 {
15     switch (tpmType)
16     {
17         case TPMType::TPM2:
18             tpmInterface = std::make_unique<TPM2Interface>(ctx, tpmIndex);
19             break;
20         default:
21             tpmInterface = nullptr;
22             error("Unsupported TPM type: {TYPE}", "TYPE",
23                   static_cast<int>(tpmType));
24             break;
25     }
26 }
27 
updateDevice(const uint8_t * image,size_t imageSize)28 sdbusplus::async::task<bool> TPMDevice::updateDevice(const uint8_t* image,
29                                                      size_t imageSize)
30 {
31     if (tpmInterface == nullptr)
32     {
33         error("TPM interface is not initialized");
34         co_return false;
35     }
36 
37     setUpdateProgress(10);
38 
39     if (!co_await tpmInterface->updateFirmware(image, imageSize))
40     {
41         error("Failed to update TPM firmware");
42         co_return false;
43     }
44 
45     setUpdateProgress(100);
46     debug("Successfully updated TPM");
47     co_return true;
48 }
49 
getVersion()50 sdbusplus::async::task<std::string> TPMDevice::getVersion()
51 {
52     std::string version = "Unknown";
53 
54     if (tpmInterface == nullptr)
55     {
56         error("TPM interface is not initialized");
57         co_return version;
58     }
59 
60     if (!co_await tpmInterface->getVersion(version))
61     {
62         error("Failed to get TPM version");
63     }
64 
65     co_return version;
66 }
67