blob: 04df3fb69b613ce18886fed49d27efde183dff64 [file] [log] [blame]
Lei YU3c341df2020-09-27 13:24:53 +08001#include "config.h"
2
Lei YU6505e9d2020-09-21 17:34:32 +08003#include "inspur_oem.hpp"
4
Lei YU3c341df2020-09-27 13:24:53 +08005#include "utils.hpp"
6
Lei YU6505e9d2020-09-21 17:34:32 +08007#include <ipmid/api.h>
8
Lei YU8a454c52020-09-23 16:52:46 +08009#include <phosphor-logging/log.hpp>
10#include <sdbusplus/bus.hpp>
11
12#include <optional>
13
Lei YU17bc93d2020-09-23 19:55:07 +080014namespace
15{
16constexpr auto FIRMWARE_TYPE_OFFSET = 0;
17constexpr auto FIRMWARE_TYPE_SIZE = 1;
18constexpr auto FIRMWARE_VERSION_OFFSET =
19 FIRMWARE_TYPE_OFFSET + FIRMWARE_TYPE_SIZE;
20constexpr auto FIRMWARE_VERSION_SIZE = 15;
21constexpr auto FIRMWARE_BUILDTIME_OFFSET =
22 FIRMWARE_VERSION_OFFSET + FIRMWARE_VERSION_SIZE;
23constexpr auto FIRMWARE_BUILDTIME_SIZE = 20;
24
25static_assert(FIRMWARE_VERSION_OFFSET == 1);
26static_assert(FIRMWARE_BUILDTIME_OFFSET == 16);
27} // namespace
28
Lei YU6505e9d2020-09-21 17:34:32 +080029namespace ipmi
30{
31
Lei YU8a454c52020-09-23 16:52:46 +080032#define UNUSED(x) (void)(x)
Lei YU6505e9d2020-09-21 17:34:32 +080033
Lei YU8a454c52020-09-23 16:52:46 +080034using namespace phosphor::logging;
Lei YU17bc93d2020-09-23 19:55:07 +080035using namespace inspur;
Lei YU8a454c52020-09-23 16:52:46 +080036
37static void registerOEMFunctions() __attribute__((constructor));
38sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
39
40struct ParsedAssetInfo
Lei YU6505e9d2020-09-21 17:34:32 +080041{
Lei YU8a454c52020-09-23 16:52:46 +080042 uint8_t rwFlag;
43 uint8_t deviceType;
44 uint8_t infoType;
45 uint8_t maskAllPresentLen;
46 std::vector<uint8_t> enableStatus;
47 std::vector<uint8_t> maskPresent;
48 std::vector<uint8_t> maskAllPresent;
49 uint8_t allInfoDone;
50 uint16_t totalMessageLen;
51 std::vector<uint8_t> data;
52};
53
54void dumpAssetInfo(const ParsedAssetInfo& info)
55{
Lei YU6505e9d2020-09-21 17:34:32 +080056 fprintf(stderr,
Lei YU8a454c52020-09-23 16:52:46 +080057 "AssetInfo: rw %d, deviceType 0x%02x, infoType 0x%02x, "
58 "maskAllPresentLen %u\n",
59 info.rwFlag, info.deviceType, info.infoType,
60 info.maskAllPresentLen);
61 fprintf(stderr, "enableStatus ");
62 for (const auto& d : info.enableStatus)
63 {
64 fprintf(stderr, "0x%02x ", d);
65 }
66 fprintf(stderr, "\nmaskPresent ");
67 for (const auto& d : info.maskPresent)
68 {
69 fprintf(stderr, "0x%02x ", d);
70 }
71 fprintf(stderr, "\nmaskAllPresent ");
72 for (const auto& d : info.maskAllPresent)
73 {
74 fprintf(stderr, "0x%02x ", d);
75 }
76 fprintf(stderr, "\nallInfoDone %d, totalMessageLen %u\n", info.allInfoDone,
77 info.totalMessageLen);
78 fprintf(stderr, "data: ");
79 for (const auto& d : info.data)
80 {
81 fprintf(stderr, "0x%02x ", d);
82 }
83 fprintf(stderr, "\n");
84}
85
86std::optional<ParsedAssetInfo> parseAssetInfo(const AssetInfoHeader* h)
87{
88 auto len = h->maskAllPresentLen;
89 if (len == 0)
90 {
91 // This is invalid
92 return {};
93 }
94
95 ParsedAssetInfo info;
96 info.rwFlag = h->rwFlag;
97 info.deviceType = h->deviceType;
98 info.infoType = h->infoType;
99 info.maskAllPresentLen = len;
100 info.enableStatus.resize(len);
101 info.maskPresent.resize(len);
102 info.maskAllPresent.resize(len);
103 const uint8_t* p = &h->enableStatus;
104 memcpy(info.enableStatus.data(), p, len);
105 p += len;
106 memcpy(info.maskPresent.data(), p, len);
107 p += len;
108 memcpy(info.maskAllPresent.data(), p, len);
109 p += len;
110 info.allInfoDone = *p++;
111 info.totalMessageLen = *reinterpret_cast<const uint16_t*>(p);
112 p += sizeof(uint16_t);
113 auto dataLen = info.totalMessageLen - (sizeof(AssetInfoHeader) + 3 * len);
114 info.data.resize(dataLen);
115 memcpy(info.data.data(), p, dataLen);
116
Lei YU17bc93d2020-09-23 19:55:07 +0800117 // dumpAssetInfo(info);
Lei YU8a454c52020-09-23 16:52:46 +0800118 return info;
119}
120
Lei YU17bc93d2020-09-23 19:55:07 +0800121void parseBIOSInfo(const std::vector<uint8_t>& data)
122{
123 bios_version_devname dev = static_cast<bios_version_devname>(data[0]);
124 std::string version{data.data() + FIRMWARE_VERSION_OFFSET,
125 data.data() + FIRMWARE_VERSION_SIZE};
126 std::string buildTime;
127 if (dev == bios_version_devname::BIOS)
128 {
129 buildTime.assign(reinterpret_cast<const char*>(
130 data.data() + FIRMWARE_BUILDTIME_OFFSET),
131 FIRMWARE_BUILDTIME_SIZE);
Lei YU3c341df2020-09-27 13:24:53 +0800132
133 // Set BIOS version
134 auto service = utils::getService(bus, BIOS_OBJPATH, VERSION_IFACE);
135 utils::setProperty(bus, service.c_str(), BIOS_OBJPATH, VERSION_IFACE,
136 VERSION, version);
Lei YU17bc93d2020-09-23 19:55:07 +0800137 }
138
139 printf("Dev %s, version %s, build time %s\n",
140 bios_devname[static_cast<int>(dev)].data(), version.c_str(),
141 buildTime.c_str());
142}
143
Lei YU8a454c52020-09-23 16:52:46 +0800144ipmi_ret_t ipmiOemInspurAssetInfo(ipmi_netfn_t /* netfn */,
145 ipmi_cmd_t /* cmd */, ipmi_request_t request,
146 ipmi_response_t response,
147 ipmi_data_len_t /* data_len */,
148 ipmi_context_t /* context */)
149{
150 auto header = reinterpret_cast<AssetInfoHeader*>(request);
151 uint8_t* res = reinterpret_cast<uint8_t*>(response);
152 UNUSED(res);
153
154 auto info = parseAssetInfo(header);
155 auto deviceType = info->deviceType;
156 if (deviceType != 0x05)
157 {
158 log<level::WARNING>("Device type not supported yet");
159 return IPMI_CC_UNSPECIFIED_ERROR;
160 }
161
162 // For now we only support BIOS type
Lei YU17bc93d2020-09-23 19:55:07 +0800163 parseBIOSInfo(info->data);
Lei YU8a454c52020-09-23 16:52:46 +0800164
Lei YU17bc93d2020-09-23 19:55:07 +0800165 return IPMI_CC_OK;
Lei YU6505e9d2020-09-21 17:34:32 +0800166}
167
168void registerOEMFunctions(void)
169{
170 ipmi_register_callback(NETFN_OEM_INSPUR, CMD_OEM_ASSET_INFO, nullptr,
171 ipmiOemInspurAssetInfo, SYSTEM_INTERFACE);
172}
173
174} // namespace ipmi