xref: /openbmc/phosphor-bmc-code-mgmt/tpm/tpm_software_manager.cpp (revision c538727d70f3673771f18e559d7ecab203abf6d3)
1 #include "tpm_software_manager.hpp"
2 
3 #include "common/include/dbus_helper.hpp"
4 #include "tpm_device.hpp"
5 
6 #include <phosphor-logging/lg2.hpp>
7 
8 PHOSPHOR_LOG2_USING;
9 
10 namespace SoftwareInf = phosphor::software;
11 
start()12 void TPMSoftwareManager::start()
13 {
14     std::vector<std::string> configIntfs = {
15         "xyz.openbmc_project.Configuration.TPM2Firmware",
16     };
17 
18     ctx.spawn(initDevices(configIntfs));
19     ctx.run();
20 }
21 
initDevice(const std::string & service,const std::string & path,SoftwareConfig & config)22 sdbusplus::async::task<bool> TPMSoftwareManager::initDevice(
23     const std::string& service, const std::string& path, SoftwareConfig& config)
24 {
25     const std::string configIface =
26         "xyz.openbmc_project.Configuration." + config.configType;
27 
28     std::optional<uint8_t> tpmIndex = co_await dbusGetRequiredProperty<uint8_t>(
29         ctx, service, path, configIface, "TPMIndex");
30 
31     if (!tpmIndex.has_value())
32     {
33         error("Missing property: TPMIndex");
34         co_return false;
35     }
36 
37     std::optional<std::string> type =
38         co_await dbusGetRequiredProperty<std::string>(ctx, service, path,
39                                                       configIface, "Type");
40     if (!type.has_value())
41     {
42         error("Missing property: Type");
43         co_return false;
44     }
45 
46     TPMType tpmType;
47     if (!stringToTPMType(type.value(), tpmType))
48     {
49         error("Invalid TPM type: {TYPE}", "TYPE", type.value());
50         co_return false;
51     }
52 
53     debug("TPM device: TPM Index={INDEX}, Type={TYPE}", "INDEX",
54           tpmIndex.value(), "TYPE", type.value());
55 
56     auto tpmDevice = std::make_unique<TPMDevice>(ctx, tpmType, tpmIndex.value(),
57                                                  config, this);
58 
59     std::unique_ptr<SoftwareInf::Software> software =
60         std::make_unique<SoftwareInf::Software>(ctx, *tpmDevice);
61 
62     software->setVersion(co_await tpmDevice->getVersion());
63 
64     if (tpmDevice->isUpdateSupported())
65     {
66         std::set<RequestedApplyTimes> allowedApplyTimes = {
67             RequestedApplyTimes::OnReset};
68 
69         software->enableUpdate(allowedApplyTimes);
70     }
71 
72     tpmDevice->softwareCurrent = std::move(software);
73 
74     devices.insert({config.objectPath, std::move(tpmDevice)});
75 
76     co_return true;
77 }
78 
main()79 int main()
80 {
81     sdbusplus::async::context ctx;
82 
83     TPMSoftwareManager tpmSoftwareManager(ctx);
84 
85     tpmSoftwareManager.start();
86     return 0;
87 }
88