blob: a6e220eaf7d7837ae0f0fcc31b922b34960547b3 [file] [log] [blame]
Kevin Tungc5387272025-07-28 18:10:43 +08001#pragma once
2
3#include "common/include/device.hpp"
4#include "common/include/software_manager.hpp"
5
6#include <sdbusplus/async.hpp>
7
8#include <unordered_map>
9
10namespace SoftwareInf = phosphor::software;
11namespace ManagerInf = SoftwareInf::manager;
12
13enum class TPMType
14{
15 TPM2,
16};
17
18class TPMInterface
19{
20 public:
21 TPMInterface(sdbusplus::async::context& ctx, uint8_t tpmIndex) :
22 ctx(ctx), tpmIndex(tpmIndex)
23 {}
24 virtual ~TPMInterface() = default;
25 TPMInterface(const TPMInterface&) = delete;
26 TPMInterface& operator=(const TPMInterface&) = delete;
27 TPMInterface(TPMInterface&&) = delete;
28 TPMInterface& operator=(TPMInterface&&) = delete;
29
30 virtual bool isUpdateSupported() const = 0;
31 virtual sdbusplus::async::task<bool> getVersion(std::string& version) = 0;
32 virtual sdbusplus::async::task<bool> updateFirmware(const uint8_t* image,
33 size_t imageSize) = 0;
34
35 protected:
36 sdbusplus::async::context& ctx;
37 uint8_t tpmIndex;
38};
39
40class TPMDevice : public Device
41{
42 public:
43 using Device::softwareCurrent;
44
45 TPMDevice(sdbusplus::async::context& ctx, enum TPMType tpmType,
46 uint8_t tpmIndex, SoftwareConfig& config,
47 ManagerInf::SoftwareManager* parent);
48
49 sdbusplus::async::task<bool> updateDevice(const uint8_t* image,
50 size_t image_size) final;
51
52 sdbusplus::async::task<std::string> getVersion();
53
54 bool isUpdateSupported() const
55 {
56 return tpmInterface ? tpmInterface->isUpdateSupported() : false;
57 }
58
59 private:
60 std::unique_ptr<TPMInterface> tpmInterface;
61};
62
63inline bool stringToTPMType(const std::string& type, TPMType& tpmType)
64{
65 static const std::unordered_map<std::string, TPMType> tpmTypeMap = {
66 {"TPM2Firmware", TPMType::TPM2},
67 };
68
69 auto it = tpmTypeMap.find(type);
70 if (it != tpmTypeMap.end())
71 {
72 tpmType = it->second;
73 return true;
74 }
75 return false;
76}