blob: 85a28b61d8462ca2c51c08b29864afcfd54258f7 [file] [log] [blame]
John Wang0917ac42024-06-04 16:52:07 +08001#include "types.hpp"
2#include "utils.hpp"
3
4#include <ipmid/api.hpp>
5
6#include <regex>
7
8namespace ipmi
9{
10namespace iei
11{
12namespace firmware_version
13{
14
15void registerIEIFirmwareVersionFunctions() __attribute__((constructor));
16
17static constexpr auto versionInterface = "xyz.openbmc_project.Software.Version";
18
19std::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
40std::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
53enum FirmwareType : uint8_t
54{
55 BMC = 0x01,
56 BIOS = 0x03,
57};
58
George Liu93b6a332024-08-19 08:30:52 +080059RspType<std::string> ipmiIEIGetFWVersion(
60 ipmi::Context::ptr ctx, uint8_t fwIndex, uint8_t /* devIndex */)
John Wang0917ac42024-06-04 16:52:07 +080061{
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
86void 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