blob: a963f029a980c0ec83867c0267ffba100956a494 [file] [log] [blame]
Tom Josephf8da32a2016-12-06 16:56:04 +05301#include "guid.hpp"
2
William A. Kennington III4f09eae2019-02-12 17:10:35 -08003#include <ipmid/api.h>
Vernon Mauery9e801a22018-10-12 13:20:49 -07004
Xie Ning94b0d132023-01-13 16:53:56 +08005#include <ipmid/utils.hpp>
6#include <phosphor-logging/elog-errors.hpp>
George Liu7b7f25f2022-07-04 17:07:32 +08007#include <phosphor-logging/lg2.hpp>
Xie Ning94b0d132023-01-13 16:53:56 +08008#include <xyz/openbmc_project/Common/error.hpp>
George Liubc8958f2022-07-04 09:29:49 +08009
Tom Josephf8da32a2016-12-06 16:56:04 +053010#include <sstream>
11#include <string>
12
Vernon Maueryfc37e592018-12-19 14:55:15 -080013using namespace phosphor::logging;
Xie Ning94b0d132023-01-13 16:53:56 +080014using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Vernon Maueryfc37e592018-12-19 14:55:15 -080015
Vernon Mauery36e3c532023-07-31 19:28:18 -070016static std::optional<command::Guid> guid;
Tom Joseph83029cb2017-09-01 16:37:31 +053017
Tom Josephf8da32a2016-12-06 16:56:04 +053018namespace command
19{
20
Tom Joseph83029cb2017-09-01 16:37:31 +053021std::unique_ptr<sdbusplus::bus::match_t> matchPtr(nullptr);
22
Xie Ning94b0d132023-01-13 16:53:56 +080023static constexpr auto propInterface = "xyz.openbmc_project.Common.UUID";
24static constexpr auto uuidProperty = "UUID";
Vernon Mauery36e3c532023-07-31 19:28:18 -070025static constexpr auto subtreePath = "/xyz/openbmc_project/inventory";
Xie Ning94b0d132023-01-13 16:53:56 +080026
27static void rfcToGuid(std::string rfc4122, Guid& uuid)
28{
29 using Argument = xyz::openbmc_project::Common::InvalidArgument;
30 // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
31 // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte
32 // order
33 // Ex: 0x2332fc2c40e66298e511f2782395a361
34 constexpr size_t uuidHexLength = (2 * BMC_GUID_LEN);
35 constexpr size_t uuidRfc4122Length = (uuidHexLength + 4);
36
37 if (rfc4122.size() == uuidRfc4122Length)
38 {
39 rfc4122.erase(std::remove(rfc4122.begin(), rfc4122.end(), '-'),
40 rfc4122.end());
41 }
42 if (rfc4122.size() != uuidHexLength)
43 {
44 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
45 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
46 }
47 for (size_t ind = 0; ind < uuidHexLength; ind += 2)
48 {
49 long b;
50 try
51 {
52 b = std::stoul(rfc4122.substr(ind, 2), nullptr, 16);
53 }
54 catch (const std::exception& e)
55 {
56 elog<InvalidArgument>(Argument::ARGUMENT_NAME("rfc4122"),
57 Argument::ARGUMENT_VALUE(rfc4122.c_str()));
58 }
59
60 uuid[BMC_GUID_LEN - (ind / 2) - 1] = static_cast<uint8_t>(b);
61 }
62 return;
63}
Tom Joseph83029cb2017-09-01 16:37:31 +053064
Vernon Mauery36e3c532023-07-31 19:28:18 -070065// Canned System GUID for when the Chassis DBUS object is not populated
66static constexpr Guid fakeGuid = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
67 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
68 0x0D, 0x0E, 0x0F, 0x10};
69const Guid& getSystemGUID()
Tom Josephf8da32a2016-12-06 16:56:04 +053070{
Vernon Mauery36e3c532023-07-31 19:28:18 -070071 if (guid.has_value())
72 {
73 return guid.value();
74 }
Tom Josephf8da32a2016-12-06 16:56:04 +053075
Xie Ning94b0d132023-01-13 16:53:56 +080076 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Josephf8da32a2016-12-06 16:56:04 +053077
Xie Ning94b0d132023-01-13 16:53:56 +080078 ipmi::Value propValue;
79 try
Tom Josephf8da32a2016-12-06 16:56:04 +053080 {
Vernon Mauery36e3c532023-07-31 19:28:18 -070081 const auto& [objPath, service] = ipmi::getDbusObject(bus, propInterface,
82 subtreePath);
Xie Ning94b0d132023-01-13 16:53:56 +080083 // Read UUID property value from bmcObject
84 // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
Vernon Mauery36e3c532023-07-31 19:28:18 -070085 propValue = ipmi::getDbusProperty(bus, service, objPath, propInterface,
Xie Ning94b0d132023-01-13 16:53:56 +080086 uuidProperty);
87 }
88 catch (const sdbusplus::exception_t& e)
89 {
90 lg2::error("Failed in reading BMC UUID property: {ERROR}", "ERROR", e);
Vernon Mauery36e3c532023-07-31 19:28:18 -070091 return fakeGuid;
Xie Ning94b0d132023-01-13 16:53:56 +080092 }
Tom Josephf8da32a2016-12-06 16:56:04 +053093
Xie Ning94b0d132023-01-13 16:53:56 +080094 std::string rfc4122Uuid = std::get<std::string>(propValue);
95 try
96 {
97 // convert to IPMI format
Vernon Mauery36e3c532023-07-31 19:28:18 -070098 Guid tmpGuid{};
99 rfcToGuid(rfc4122Uuid, tmpGuid);
100 guid = tmpGuid;
Xie Ning94b0d132023-01-13 16:53:56 +0800101 }
102 catch (const InvalidArgument& e)
103 {
104 lg2::error("Failed in parsing BMC UUID property: {VALUE}", "VALUE",
105 rfc4122Uuid.c_str());
Vernon Mauery36e3c532023-07-31 19:28:18 -0700106 return fakeGuid;
Xie Ning94b0d132023-01-13 16:53:56 +0800107 }
Vernon Mauery36e3c532023-07-31 19:28:18 -0700108 return guid.value();
Tom Josephf8da32a2016-12-06 16:56:04 +0530109}
110
Tom Joseph83029cb2017-09-01 16:37:31 +0530111void registerGUIDChangeCallback()
112{
Vernon Mauery9e801a22018-10-12 13:20:49 -0700113 if (matchPtr == nullptr)
Tom Joseph83029cb2017-09-01 16:37:31 +0530114 {
115 using namespace sdbusplus::bus::match::rules;
Patrick Williams0a590622022-07-22 19:26:53 -0500116 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph83029cb2017-09-01 16:37:31 +0530117
Vernon Mauery36e3c532023-07-31 19:28:18 -0700118 try
119 {
120 matchPtr = std::make_unique<sdbusplus::bus::match_t>(
121 bus, propertiesChangedNamespace(subtreePath, propInterface),
122 [](sdbusplus::message_t& m) {
123 try
124 {
125 std::string iface{};
126 std::map<std::string, ipmi::Value> pdict{};
127 m.read(iface, pdict);
128 if (iface != propInterface)
129 {
130 return;
131 }
132 auto guidStr = std::get<std::string>(pdict.at("UUID"));
133 Guid tmpGuid{};
134 rfcToGuid(guidStr, tmpGuid);
135 guid = tmpGuid;
136 }
137 catch (const std::exception& e)
138 {
139 // signal contained invalid guid; ignore it
140 lg2::error(
141 "Failed to parse propertiesChanged signal: {ERROR}",
142 "ERROR", e);
143 }
Patrick Williams7b534092023-10-20 11:18:45 -0500144 });
Vernon Mauery36e3c532023-07-31 19:28:18 -0700145 }
146 catch (const std::exception& e)
147 {
148 lg2::error("Failed to create dbus match: {ERROR}", "ERROR", e);
149 }
Tom Joseph83029cb2017-09-01 16:37:31 +0530150 }
151}
152
Tom Josephf8da32a2016-12-06 16:56:04 +0530153} // namespace command