John Wang | 0917ac4 | 2024-06-04 16:52:07 +0800 | [diff] [blame] | 1 | #include "types.hpp" |
| 2 | #include "utils.hpp" |
| 3 | |
| 4 | #include <ipmid/api.hpp> |
| 5 | |
| 6 | #include <regex> |
| 7 | |
| 8 | namespace ipmi |
| 9 | { |
| 10 | namespace iei |
| 11 | { |
| 12 | namespace firmware_version |
| 13 | { |
| 14 | |
| 15 | void registerIEIFirmwareVersionFunctions() __attribute__((constructor)); |
| 16 | |
| 17 | static constexpr auto versionInterface = "xyz.openbmc_project.Software.Version"; |
| 18 | |
| 19 | std::string getBMCVersionValue(ipmi::Context::ptr ctx, |
| 20 | const std::string& objPath, |
| 21 | const std::string& interface) |
| 22 | { |
| 23 | std::string value{}; |
| 24 | auto cc = iei::getDbusProperty(ctx, objPath, interface, "Version", value); |
| 25 | if (cc) |
| 26 | { |
| 27 | return ""; |
| 28 | } |
| 29 | |
| 30 | const std::regex patternId("v[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"); |
| 31 | std::smatch result; |
| 32 | if (!std::regex_search(value, result, patternId)) |
| 33 | { |
| 34 | return ""; |
| 35 | } |
| 36 | std::string versionStr = result[0]; |
| 37 | return versionStr.substr(1); |
| 38 | } |
| 39 | |
| 40 | std::string getFWVersionValue(ipmi::Context::ptr ctx, |
| 41 | const std::string& objPath, |
| 42 | const std::string& interface) |
| 43 | { |
| 44 | std::string value{}; |
| 45 | auto cc = iei::getDbusProperty(ctx, objPath, interface, "Version", value); |
| 46 | if (cc) |
| 47 | { |
| 48 | return ""; |
| 49 | } |
| 50 | return value; |
| 51 | } |
| 52 | |
| 53 | enum FirmwareType : uint8_t |
| 54 | { |
| 55 | BMC = 0x01, |
| 56 | BIOS = 0x03, |
| 57 | }; |
| 58 | |
George Liu | 93b6a33 | 2024-08-19 08:30:52 +0800 | [diff] [blame^] | 59 | RspType<std::string> ipmiIEIGetFWVersion( |
| 60 | ipmi::Context::ptr ctx, uint8_t fwIndex, uint8_t /* devIndex */) |
John Wang | 0917ac4 | 2024-06-04 16:52:07 +0800 | [diff] [blame] | 61 | { |
| 62 | std::string fwPath{}; |
| 63 | std::string value{}; |
| 64 | |
| 65 | switch (fwIndex) |
| 66 | { |
| 67 | case BMC: |
| 68 | { |
| 69 | fwPath = "/xyz/openbmc_project/software/bmc_active"; |
| 70 | value = getBMCVersionValue(ctx, fwPath, versionInterface); |
| 71 | break; |
| 72 | } |
| 73 | case BIOS: |
| 74 | { |
| 75 | fwPath = "/xyz/openbmc_project/software/bios_active"; |
| 76 | value = getFWVersionValue(ctx, fwPath, versionInterface); |
| 77 | break; |
| 78 | } |
| 79 | default: |
| 80 | return ipmi::responseParmOutOfRange(); |
| 81 | } |
| 82 | |
| 83 | return ipmi::responseSuccess(value); |
| 84 | } |
| 85 | |
| 86 | void registerIEIFirmwareVersionFunctions() |
| 87 | { |
| 88 | ipmi::registerHandler(prioOemBase, netFnIEI, cmdGetFWVersion, |
| 89 | ipmi::Privilege::User, ipmiIEIGetFWVersion); |
| 90 | } |
| 91 | |
| 92 | } // namespace firmware_version |
| 93 | } // namespace iei |
| 94 | } // namespace ipmi |