Lei YU | 3c341df | 2020-09-27 13:24:53 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
John Wang | ef3194d | 2024-06-04 15:00:27 +0800 | [diff] [blame] | 3 | #include "types.hpp" |
Lei YU | 6505e9d | 2020-09-21 17:34:32 +0800 | [diff] [blame] | 4 | |
George Liu | 131de1e | 2024-06-04 11:07:47 +0800 | [diff] [blame] | 5 | #include <ipmid/api.hpp> |
| 6 | #include <ipmid/utils.hpp> |
George Liu | 666f96a | 2024-06-03 19:41:40 +0800 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
George Liu | 131de1e | 2024-06-04 11:07:47 +0800 | [diff] [blame] | 8 | #include <sdbusplus/bus.hpp> |
Lei YU | 8a454c5 | 2020-09-23 16:52:46 +0800 | [diff] [blame] | 9 | |
| 10 | #include <optional> |
| 11 | |
Lei YU | 17bc93d | 2020-09-23 19:55:07 +0800 | [diff] [blame] | 12 | namespace |
| 13 | { |
| 14 | constexpr auto FIRMWARE_TYPE_OFFSET = 0; |
| 15 | constexpr auto FIRMWARE_TYPE_SIZE = 1; |
George Liu | 93b6a33 | 2024-08-19 08:30:52 +0800 | [diff] [blame^] | 16 | constexpr auto FIRMWARE_VERSION_OFFSET = |
| 17 | FIRMWARE_TYPE_OFFSET + FIRMWARE_TYPE_SIZE; |
Lei YU | 17bc93d | 2020-09-23 19:55:07 +0800 | [diff] [blame] | 18 | constexpr auto FIRMWARE_VERSION_SIZE = 15; |
George Liu | 93b6a33 | 2024-08-19 08:30:52 +0800 | [diff] [blame^] | 19 | constexpr auto FIRMWARE_BUILDTIME_OFFSET = |
| 20 | FIRMWARE_VERSION_OFFSET + FIRMWARE_VERSION_SIZE; |
Lei YU | 17bc93d | 2020-09-23 19:55:07 +0800 | [diff] [blame] | 21 | constexpr auto FIRMWARE_BUILDTIME_SIZE = 20; |
Lei YU | ebd3d09 | 2020-09-27 18:11:48 +0800 | [diff] [blame] | 22 | constexpr auto FIRMWARE_MIN_SIZE = FIRMWARE_BUILDTIME_OFFSET; |
Lei YU | 17bc93d | 2020-09-23 19:55:07 +0800 | [diff] [blame] | 23 | |
| 24 | static_assert(FIRMWARE_VERSION_OFFSET == 1); |
| 25 | static_assert(FIRMWARE_BUILDTIME_OFFSET == 16); |
| 26 | } // namespace |
| 27 | |
Lei YU | 6505e9d | 2020-09-21 17:34:32 +0800 | [diff] [blame] | 28 | namespace ipmi |
| 29 | { |
| 30 | |
Lei YU | 8a454c5 | 2020-09-23 16:52:46 +0800 | [diff] [blame] | 31 | static void registerOEMFunctions() __attribute__((constructor)); |
Lei YU | 8a454c5 | 2020-09-23 16:52:46 +0800 | [diff] [blame] | 32 | |
John Wang | ef3194d | 2024-06-04 15:00:27 +0800 | [diff] [blame] | 33 | struct AssetInfoHeader |
| 34 | { |
| 35 | uint8_t rwFlag; |
| 36 | uint8_t deviceType; |
| 37 | uint8_t infoType; |
| 38 | uint8_t maskAllPresentLen; |
| 39 | uint8_t enableStatus; |
| 40 | uint8_t maskPresent; |
| 41 | uint8_t maskAllPresent; |
| 42 | uint8_t allInfoDone; |
| 43 | uint16_t totalMessageLen; |
| 44 | } __attribute__((packed)); |
| 45 | |
| 46 | enum class bios_version_devname |
| 47 | { |
| 48 | BIOS = 0, |
| 49 | ME = 1, |
| 50 | IE = 2, |
| 51 | PCH = 3, |
| 52 | BOARD = 4, |
| 53 | MRC = 5, |
| 54 | CUSTOM_ID = 6, |
| 55 | PCH_STEPPING = 7, |
| 56 | }; |
| 57 | |
| 58 | constexpr std::array<std::string_view, 8> bios_devname{ |
| 59 | "BIOS", "ME", "IE", "PCH", "BOARD", "MRC", "CUSTOM_ID", "PCH_STEPPING", |
| 60 | }; |
| 61 | |
Lei YU | 8a454c5 | 2020-09-23 16:52:46 +0800 | [diff] [blame] | 62 | struct ParsedAssetInfo |
Lei YU | 6505e9d | 2020-09-21 17:34:32 +0800 | [diff] [blame] | 63 | { |
Lei YU | 8a454c5 | 2020-09-23 16:52:46 +0800 | [diff] [blame] | 64 | uint8_t rwFlag; |
| 65 | uint8_t deviceType; |
| 66 | uint8_t infoType; |
| 67 | uint8_t maskAllPresentLen; |
| 68 | std::vector<uint8_t> enableStatus; |
| 69 | std::vector<uint8_t> maskPresent; |
| 70 | std::vector<uint8_t> maskAllPresent; |
| 71 | uint8_t allInfoDone; |
| 72 | uint16_t totalMessageLen; |
| 73 | std::vector<uint8_t> data; |
| 74 | }; |
| 75 | |
| 76 | void dumpAssetInfo(const ParsedAssetInfo& info) |
| 77 | { |
Lei YU | 6505e9d | 2020-09-21 17:34:32 +0800 | [diff] [blame] | 78 | fprintf(stderr, |
Lei YU | 8a454c5 | 2020-09-23 16:52:46 +0800 | [diff] [blame] | 79 | "AssetInfo: rw %d, deviceType 0x%02x, infoType 0x%02x, " |
| 80 | "maskAllPresentLen %u\n", |
| 81 | info.rwFlag, info.deviceType, info.infoType, |
| 82 | info.maskAllPresentLen); |
| 83 | fprintf(stderr, "enableStatus "); |
| 84 | for (const auto& d : info.enableStatus) |
| 85 | { |
| 86 | fprintf(stderr, "0x%02x ", d); |
| 87 | } |
| 88 | fprintf(stderr, "\nmaskPresent "); |
| 89 | for (const auto& d : info.maskPresent) |
| 90 | { |
| 91 | fprintf(stderr, "0x%02x ", d); |
| 92 | } |
| 93 | fprintf(stderr, "\nmaskAllPresent "); |
| 94 | for (const auto& d : info.maskAllPresent) |
| 95 | { |
| 96 | fprintf(stderr, "0x%02x ", d); |
| 97 | } |
| 98 | fprintf(stderr, "\nallInfoDone %d, totalMessageLen %u\n", info.allInfoDone, |
| 99 | info.totalMessageLen); |
| 100 | fprintf(stderr, "data: "); |
| 101 | for (const auto& d : info.data) |
| 102 | { |
| 103 | fprintf(stderr, "0x%02x ", d); |
| 104 | } |
| 105 | fprintf(stderr, "\n"); |
| 106 | } |
| 107 | |
| 108 | std::optional<ParsedAssetInfo> parseAssetInfo(const AssetInfoHeader* h) |
| 109 | { |
| 110 | auto len = h->maskAllPresentLen; |
| 111 | if (len == 0) |
| 112 | { |
| 113 | // This is invalid |
| 114 | return {}; |
| 115 | } |
| 116 | |
| 117 | ParsedAssetInfo info; |
| 118 | info.rwFlag = h->rwFlag; |
| 119 | info.deviceType = h->deviceType; |
| 120 | info.infoType = h->infoType; |
| 121 | info.maskAllPresentLen = len; |
| 122 | info.enableStatus.resize(len); |
| 123 | info.maskPresent.resize(len); |
| 124 | info.maskAllPresent.resize(len); |
| 125 | const uint8_t* p = &h->enableStatus; |
| 126 | memcpy(info.enableStatus.data(), p, len); |
| 127 | p += len; |
| 128 | memcpy(info.maskPresent.data(), p, len); |
| 129 | p += len; |
| 130 | memcpy(info.maskAllPresent.data(), p, len); |
| 131 | p += len; |
| 132 | info.allInfoDone = *p++; |
| 133 | info.totalMessageLen = *reinterpret_cast<const uint16_t*>(p); |
| 134 | p += sizeof(uint16_t); |
| 135 | auto dataLen = info.totalMessageLen - (sizeof(AssetInfoHeader) + 3 * len); |
| 136 | info.data.resize(dataLen); |
| 137 | memcpy(info.data.data(), p, dataLen); |
| 138 | |
Lei YU | 17bc93d | 2020-09-23 19:55:07 +0800 | [diff] [blame] | 139 | // dumpAssetInfo(info); |
Lei YU | 8a454c5 | 2020-09-23 16:52:46 +0800 | [diff] [blame] | 140 | return info; |
| 141 | } |
| 142 | |
Lei YU | 17bc93d | 2020-09-23 19:55:07 +0800 | [diff] [blame] | 143 | void parseBIOSInfo(const std::vector<uint8_t>& data) |
| 144 | { |
Lei YU | ebd3d09 | 2020-09-27 18:11:48 +0800 | [diff] [blame] | 145 | if (data.size() < FIRMWARE_MIN_SIZE) |
| 146 | { |
| 147 | return; |
| 148 | } |
Lei YU | 17bc93d | 2020-09-23 19:55:07 +0800 | [diff] [blame] | 149 | bios_version_devname dev = static_cast<bios_version_devname>(data[0]); |
| 150 | std::string version{data.data() + FIRMWARE_VERSION_OFFSET, |
| 151 | data.data() + FIRMWARE_VERSION_SIZE}; |
| 152 | std::string buildTime; |
| 153 | if (dev == bios_version_devname::BIOS) |
| 154 | { |
| 155 | buildTime.assign(reinterpret_cast<const char*>( |
| 156 | data.data() + FIRMWARE_BUILDTIME_OFFSET), |
| 157 | FIRMWARE_BUILDTIME_SIZE); |
Lei YU | 3c341df | 2020-09-27 13:24:53 +0800 | [diff] [blame] | 158 | |
George Liu | 131de1e | 2024-06-04 11:07:47 +0800 | [diff] [blame] | 159 | try |
| 160 | { |
| 161 | // Set BIOS version |
| 162 | std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus(); |
| 163 | auto service = getService(*dbus, VERSION_IFACE, BIOS_OBJPATH); |
| 164 | setDbusProperty(*dbus, service, BIOS_OBJPATH, VERSION_IFACE, |
| 165 | VERSION, version); |
| 166 | } |
| 167 | catch (const std::exception& e) |
| 168 | { |
| 169 | lg2::error("Failed to set BIOS version: {VERSION}, error: {ERROR}", |
| 170 | "VERSION", version, "ERROR", e); |
| 171 | } |
Lei YU | 17bc93d | 2020-09-23 19:55:07 +0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | printf("Dev %s, version %s, build time %s\n", |
| 175 | bios_devname[static_cast<int>(dev)].data(), version.c_str(), |
| 176 | buildTime.c_str()); |
| 177 | } |
| 178 | |
George Liu | 93b6a33 | 2024-08-19 08:30:52 +0800 | [diff] [blame^] | 179 | ipmi_ret_t ipmiOemIEIAssetInfo( |
| 180 | ipmi_netfn_t /* netfn */, ipmi_cmd_t /* cmd */, ipmi_request_t request, |
| 181 | ipmi_response_t /* response */, ipmi_data_len_t /* data_len */, |
| 182 | ipmi_context_t /* context */) |
Lei YU | 8a454c5 | 2020-09-23 16:52:46 +0800 | [diff] [blame] | 183 | { |
| 184 | auto header = reinterpret_cast<AssetInfoHeader*>(request); |
Lei YU | 8a454c5 | 2020-09-23 16:52:46 +0800 | [diff] [blame] | 185 | |
| 186 | auto info = parseAssetInfo(header); |
| 187 | auto deviceType = info->deviceType; |
| 188 | if (deviceType != 0x05) |
| 189 | { |
George Liu | 666f96a | 2024-06-03 19:41:40 +0800 | [diff] [blame] | 190 | lg2::info("Device type ({DEVICE_TYPE}) not supported yet", |
| 191 | "DEVICE_TYPE", deviceType); |
Lei YU | 8a454c5 | 2020-09-23 16:52:46 +0800 | [diff] [blame] | 192 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 193 | } |
| 194 | |
| 195 | // For now we only support BIOS type |
Lei YU | 17bc93d | 2020-09-23 19:55:07 +0800 | [diff] [blame] | 196 | parseBIOSInfo(info->data); |
Lei YU | 8a454c5 | 2020-09-23 16:52:46 +0800 | [diff] [blame] | 197 | |
Lei YU | 17bc93d | 2020-09-23 19:55:07 +0800 | [diff] [blame] | 198 | return IPMI_CC_OK; |
Lei YU | 6505e9d | 2020-09-21 17:34:32 +0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void registerOEMFunctions(void) |
| 202 | { |
John Wang | 0917ac4 | 2024-06-04 16:52:07 +0800 | [diff] [blame] | 203 | ipmi_register_callback(netFnIEI, iei::cmdSetAssetInfo, nullptr, |
George Liu | a3a9d2a | 2024-04-03 09:30:45 +0800 | [diff] [blame] | 204 | ipmiOemIEIAssetInfo, SYSTEM_INTERFACE); |
Lei YU | 6505e9d | 2020-09-21 17:34:32 +0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | } // namespace ipmi |