Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 1 | #include "guid.hpp" |
| 2 | |
William A. Kennington III | 4f09eae | 2019-02-12 17:10:35 -0800 | [diff] [blame] | 3 | #include <ipmid/api.h> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 4 | |
Xie Ning | 94b0d13 | 2023-01-13 16:53:56 +0800 | [diff] [blame] | 5 | #include <ipmid/utils.hpp> |
| 6 | #include <phosphor-logging/elog-errors.hpp> |
George Liu | 7b7f25f | 2022-07-04 17:07:32 +0800 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
Xie Ning | 94b0d13 | 2023-01-13 16:53:56 +0800 | [diff] [blame] | 8 | #include <xyz/openbmc_project/Common/error.hpp> |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame] | 9 | |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 10 | #include <sstream> |
| 11 | #include <string> |
| 12 | |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 13 | using namespace phosphor::logging; |
Xie Ning | 94b0d13 | 2023-01-13 16:53:56 +0800 | [diff] [blame] | 14 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 15 | |
Vernon Mauery | 36e3c53 | 2023-07-31 19:28:18 -0700 | [diff] [blame] | 16 | static std::optional<command::Guid> guid; |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 17 | |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 18 | namespace command |
| 19 | { |
| 20 | |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 21 | std::unique_ptr<sdbusplus::bus::match_t> matchPtr(nullptr); |
| 22 | |
Xie Ning | 94b0d13 | 2023-01-13 16:53:56 +0800 | [diff] [blame] | 23 | static constexpr auto propInterface = "xyz.openbmc_project.Common.UUID"; |
| 24 | static constexpr auto uuidProperty = "UUID"; |
Vernon Mauery | 36e3c53 | 2023-07-31 19:28:18 -0700 | [diff] [blame] | 25 | static constexpr auto subtreePath = "/xyz/openbmc_project/inventory"; |
Xie Ning | 94b0d13 | 2023-01-13 16:53:56 +0800 | [diff] [blame] | 26 | |
| 27 | static 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 Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 64 | |
Vernon Mauery | 36e3c53 | 2023-07-31 19:28:18 -0700 | [diff] [blame] | 65 | // Canned System GUID for when the Chassis DBUS object is not populated |
| 66 | static constexpr Guid fakeGuid = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 67 | 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, |
| 68 | 0x0D, 0x0E, 0x0F, 0x10}; |
| 69 | const Guid& getSystemGUID() |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 70 | { |
Vernon Mauery | 36e3c53 | 2023-07-31 19:28:18 -0700 | [diff] [blame] | 71 | if (guid.has_value()) |
| 72 | { |
| 73 | return guid.value(); |
| 74 | } |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 75 | |
Xie Ning | 94b0d13 | 2023-01-13 16:53:56 +0800 | [diff] [blame] | 76 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 77 | |
Xie Ning | 94b0d13 | 2023-01-13 16:53:56 +0800 | [diff] [blame] | 78 | ipmi::Value propValue; |
| 79 | try |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 80 | { |
Vernon Mauery | 36e3c53 | 2023-07-31 19:28:18 -0700 | [diff] [blame] | 81 | const auto& [objPath, service] = ipmi::getDbusObject(bus, propInterface, |
| 82 | subtreePath); |
Xie Ning | 94b0d13 | 2023-01-13 16:53:56 +0800 | [diff] [blame] | 83 | // Read UUID property value from bmcObject |
| 84 | // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223 |
Vernon Mauery | 36e3c53 | 2023-07-31 19:28:18 -0700 | [diff] [blame] | 85 | propValue = ipmi::getDbusProperty(bus, service, objPath, propInterface, |
Xie Ning | 94b0d13 | 2023-01-13 16:53:56 +0800 | [diff] [blame] | 86 | uuidProperty); |
| 87 | } |
| 88 | catch (const sdbusplus::exception_t& e) |
| 89 | { |
| 90 | lg2::error("Failed in reading BMC UUID property: {ERROR}", "ERROR", e); |
Vernon Mauery | 36e3c53 | 2023-07-31 19:28:18 -0700 | [diff] [blame] | 91 | return fakeGuid; |
Xie Ning | 94b0d13 | 2023-01-13 16:53:56 +0800 | [diff] [blame] | 92 | } |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 93 | |
Xie Ning | 94b0d13 | 2023-01-13 16:53:56 +0800 | [diff] [blame] | 94 | std::string rfc4122Uuid = std::get<std::string>(propValue); |
| 95 | try |
| 96 | { |
| 97 | // convert to IPMI format |
Vernon Mauery | 36e3c53 | 2023-07-31 19:28:18 -0700 | [diff] [blame] | 98 | Guid tmpGuid{}; |
| 99 | rfcToGuid(rfc4122Uuid, tmpGuid); |
| 100 | guid = tmpGuid; |
Xie Ning | 94b0d13 | 2023-01-13 16:53:56 +0800 | [diff] [blame] | 101 | } |
| 102 | catch (const InvalidArgument& e) |
| 103 | { |
| 104 | lg2::error("Failed in parsing BMC UUID property: {VALUE}", "VALUE", |
| 105 | rfc4122Uuid.c_str()); |
Vernon Mauery | 36e3c53 | 2023-07-31 19:28:18 -0700 | [diff] [blame] | 106 | return fakeGuid; |
Xie Ning | 94b0d13 | 2023-01-13 16:53:56 +0800 | [diff] [blame] | 107 | } |
Vernon Mauery | 36e3c53 | 2023-07-31 19:28:18 -0700 | [diff] [blame] | 108 | return guid.value(); |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 109 | } |
| 110 | |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 111 | void registerGUIDChangeCallback() |
| 112 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 113 | if (matchPtr == nullptr) |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 114 | { |
| 115 | using namespace sdbusplus::bus::match::rules; |
Patrick Williams | 0a59062 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 116 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 117 | |
Vernon Mauery | 36e3c53 | 2023-07-31 19:28:18 -0700 | [diff] [blame] | 118 | 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 Williams | 7b53409 | 2023-10-20 11:18:45 -0500 | [diff] [blame] | 144 | }); |
Vernon Mauery | 36e3c53 | 2023-07-31 19:28:18 -0700 | [diff] [blame] | 145 | } |
| 146 | catch (const std::exception& e) |
| 147 | { |
| 148 | lg2::error("Failed to create dbus match: {ERROR}", "ERROR", e); |
| 149 | } |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 153 | } // namespace command |