blob: 43f88536830d782c0a25b94d47d6c2782a77828d [file] [log] [blame]
Kevin Tungc5387272025-07-28 18:10:43 +08001#include "tpm_device.hpp"
2
3#include "tpm2/tpm2.hpp"
4
5#include <phosphor-logging/lg2.hpp>
6#include <sdbusplus/async.hpp>
7
8PHOSPHOR_LOG2_USING;
9
10TPMDevice::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
28sdbusplus::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
50sdbusplus::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}