xref: /openbmc/phosphor-bmc-code-mgmt/tpm/tpm_device.hpp (revision c538727d70f3673771f18e559d7ecab203abf6d3)
1 #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 
10 namespace SoftwareInf = phosphor::software;
11 namespace ManagerInf = SoftwareInf::manager;
12 
13 enum class TPMType
14 {
15     TPM2,
16 };
17 
18 class TPMInterface
19 {
20   public:
TPMInterface(sdbusplus::async::context & ctx,uint8_t tpmIndex)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 
40 class 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 
isUpdateSupported() const54     bool isUpdateSupported() const
55     {
56         return tpmInterface ? tpmInterface->isUpdateSupported() : false;
57     }
58 
59   private:
60     std::unique_ptr<TPMInterface> tpmInterface;
61 };
62 
stringToTPMType(const std::string & type,TPMType & tpmType)63 inline 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 }
77