Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2017-2019 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "dbus-sdr/storagecommands.hpp" |
| 18 | |
| 19 | #include "dbus-sdr/sdrutils.hpp" |
| 20 | #include "selutility.hpp" |
| 21 | |
| 22 | #include <boost/algorithm/string.hpp> |
Ed Tanous | 9860505 | 2025-02-13 16:57:13 -0800 | [diff] [blame] | 23 | #include <boost/asio/detached.hpp> |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 24 | #include <boost/container/flat_map.hpp> |
| 25 | #include <boost/process.hpp> |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 26 | #include <ipmid/api.hpp> |
| 27 | #include <ipmid/message.hpp> |
| 28 | #include <ipmid/types.hpp> |
George Liu | de1420d | 2025-03-03 15:14:25 +0800 | [diff] [blame] | 29 | #include <ipmid/utils.hpp> |
George Liu | de6694e | 2024-07-17 15:22:25 +0800 | [diff] [blame] | 30 | #include <phosphor-logging/lg2.hpp> |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 31 | #include <sdbusplus/message/types.hpp> |
| 32 | #include <sdbusplus/timer.hpp> |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 33 | |
| 34 | #include <filesystem> |
| 35 | #include <fstream> |
| 36 | #include <functional> |
| 37 | #include <iostream> |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 38 | #include <optional> |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 39 | #include <stdexcept> |
| 40 | #include <string_view> |
| 41 | |
| 42 | static constexpr bool DEBUG = false; |
| 43 | |
| 44 | namespace dynamic_sensors::ipmi::sel |
| 45 | { |
| 46 | static const std::filesystem::path selLogDir = "/var/log"; |
| 47 | static const std::string selLogFilename = "ipmi_sel"; |
| 48 | |
| 49 | static int getFileTimestamp(const std::filesystem::path& file) |
| 50 | { |
| 51 | struct stat st; |
| 52 | |
| 53 | if (stat(file.c_str(), &st) >= 0) |
| 54 | { |
| 55 | return st.st_mtime; |
| 56 | } |
| 57 | return ::ipmi::sel::invalidTimeStamp; |
| 58 | } |
| 59 | |
| 60 | namespace erase_time |
| 61 | { |
| 62 | static constexpr const char* selEraseTimestamp = "/var/lib/ipmi/sel_erase_time"; |
| 63 | |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 64 | int get() |
| 65 | { |
| 66 | return getFileTimestamp(selEraseTimestamp); |
| 67 | } |
| 68 | } // namespace erase_time |
| 69 | } // namespace dynamic_sensors::ipmi::sel |
| 70 | |
| 71 | namespace ipmi |
| 72 | { |
| 73 | |
| 74 | namespace storage |
| 75 | { |
| 76 | |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 77 | constexpr static const size_t maxFruSdrNameSize = 16; |
| 78 | using ObjectType = |
| 79 | boost::container::flat_map<std::string, |
| 80 | boost::container::flat_map<std::string, Value>>; |
| 81 | using ManagedObjectType = |
| 82 | boost::container::flat_map<sdbusplus::message::object_path, ObjectType>; |
| 83 | using ManagedEntry = std::pair<sdbusplus::message::object_path, ObjectType>; |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 84 | using Paths = std::vector<std::string>; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 85 | |
| 86 | constexpr static const char* fruDeviceServiceName = |
| 87 | "xyz.openbmc_project.FruDevice"; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 88 | constexpr static const size_t writeTimeoutSeconds = 10; |
| 89 | constexpr static const char* chassisTypeRackMount = "23"; |
Zev Weiss | f38f9d1 | 2021-05-21 13:30:16 -0500 | [diff] [blame] | 90 | constexpr static const char* chassisTypeMainServer = "17"; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 91 | |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 92 | static std::vector<uint8_t> fruCache; |
krishnar4 | d90b3f0 | 2022-11-11 16:18:32 +0530 | [diff] [blame] | 93 | static constexpr uint16_t invalidBus = 0xFFFF; |
| 94 | static constexpr uint8_t invalidAddr = 0xFF; |
Johnathan Mantey | b99de18 | 2023-12-21 08:28:18 -0800 | [diff] [blame] | 95 | static constexpr uint8_t typeASCIILatin8 = 0xC0; |
krishnar4 | d90b3f0 | 2022-11-11 16:18:32 +0530 | [diff] [blame] | 96 | static uint16_t cacheBus = invalidBus; |
| 97 | static uint8_t cacheAddr = invalidAddr; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 98 | static uint8_t lastDevId = 0xFF; |
| 99 | |
krishnar4 | d90b3f0 | 2022-11-11 16:18:32 +0530 | [diff] [blame] | 100 | static uint16_t writeBus = invalidBus; |
| 101 | static uint8_t writeAddr = invalidAddr; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 102 | |
Patrick Williams | 9565522 | 2023-12-05 12:45:02 -0600 | [diff] [blame] | 103 | std::unique_ptr<sdbusplus::Timer> writeTimer = nullptr; |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 104 | static std::vector<sdbusplus::bus::match_t> fruMatches; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 105 | |
| 106 | ManagedObjectType frus; |
| 107 | |
| 108 | // we unfortunately have to build a map of hashes in case there is a |
| 109 | // collision to verify our dev-id |
krishnar4 | d90b3f0 | 2022-11-11 16:18:32 +0530 | [diff] [blame] | 110 | boost::container::flat_map<uint8_t, std::pair<uint16_t, uint8_t>> deviceHashes; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 111 | void registerStorageFunctions() __attribute__((constructor)); |
| 112 | |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 113 | bool writeFru(const std::vector<uint8_t>& fru) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 114 | { |
krishnar4 | d90b3f0 | 2022-11-11 16:18:32 +0530 | [diff] [blame] | 115 | if (writeBus == invalidBus && writeAddr == invalidAddr) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 116 | { |
| 117 | return true; |
| 118 | } |
Thang Tran | d934be9 | 2021-12-08 10:13:50 +0700 | [diff] [blame] | 119 | lastDevId = 0xFF; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 120 | std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus(); |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 121 | sdbusplus::message_t writeFru = dbus->new_method_call( |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 122 | fruDeviceServiceName, "/xyz/openbmc_project/FruDevice", |
| 123 | "xyz.openbmc_project.FruDeviceManager", "WriteFru"); |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 124 | writeFru.append(writeBus, writeAddr, fru); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 125 | try |
| 126 | { |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 127 | sdbusplus::message_t writeFruResp = dbus->call(writeFru); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 128 | } |
Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 129 | catch (const sdbusplus::exception_t&) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 130 | { |
| 131 | // todo: log sel? |
George Liu | de6694e | 2024-07-17 15:22:25 +0800 | [diff] [blame] | 132 | lg2::error("error writing fru"); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 133 | return false; |
| 134 | } |
krishnar4 | d90b3f0 | 2022-11-11 16:18:32 +0530 | [diff] [blame] | 135 | writeBus = invalidBus; |
| 136 | writeAddr = invalidAddr; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | |
William A. Kennington III | 5253562 | 2022-11-28 18:28:22 -0800 | [diff] [blame] | 140 | void writeFruCache() |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 141 | { |
William A. Kennington III | 5253562 | 2022-11-28 18:28:22 -0800 | [diff] [blame] | 142 | writeFru(fruCache); |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 145 | void createTimers() |
| 146 | { |
Patrick Williams | 9565522 | 2023-12-05 12:45:02 -0600 | [diff] [blame] | 147 | writeTimer = std::make_unique<sdbusplus::Timer>(writeFruCache); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void recalculateHashes() |
| 151 | { |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 152 | deviceHashes.clear(); |
| 153 | // hash the object paths to create unique device id's. increment on |
| 154 | // collision |
| 155 | std::hash<std::string> hasher; |
| 156 | for (const auto& fru : frus) |
| 157 | { |
| 158 | auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice"); |
| 159 | if (fruIface == fru.second.end()) |
| 160 | { |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | auto busFind = fruIface->second.find("BUS"); |
| 165 | auto addrFind = fruIface->second.find("ADDRESS"); |
| 166 | if (busFind == fruIface->second.end() || |
| 167 | addrFind == fruIface->second.end()) |
| 168 | { |
George Liu | de6694e | 2024-07-17 15:22:25 +0800 | [diff] [blame] | 169 | lg2::info("fru device missing Bus or Address, fru: {FRU}", "FRU", |
| 170 | fru.first.str); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 171 | continue; |
| 172 | } |
| 173 | |
krishnar4 | d90b3f0 | 2022-11-11 16:18:32 +0530 | [diff] [blame] | 174 | uint16_t fruBus = std::get<uint32_t>(busFind->second); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 175 | uint8_t fruAddr = std::get<uint32_t>(addrFind->second); |
| 176 | auto chassisFind = fruIface->second.find("CHASSIS_TYPE"); |
| 177 | std::string chassisType; |
| 178 | if (chassisFind != fruIface->second.end()) |
| 179 | { |
| 180 | chassisType = std::get<std::string>(chassisFind->second); |
| 181 | } |
| 182 | |
| 183 | uint8_t fruHash = 0; |
Zev Weiss | f38f9d1 | 2021-05-21 13:30:16 -0500 | [diff] [blame] | 184 | if (chassisType.compare(chassisTypeRackMount) != 0 && |
| 185 | chassisType.compare(chassisTypeMainServer) != 0) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 186 | { |
| 187 | fruHash = hasher(fru.first.str); |
| 188 | // can't be 0xFF based on spec, and 0 is reserved for baseboard |
| 189 | if (fruHash == 0 || fruHash == 0xFF) |
| 190 | { |
| 191 | fruHash = 1; |
| 192 | } |
| 193 | } |
krishnar4 | d90b3f0 | 2022-11-11 16:18:32 +0530 | [diff] [blame] | 194 | std::pair<uint16_t, uint8_t> newDev(fruBus, fruAddr); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 195 | |
| 196 | bool emplacePassed = false; |
| 197 | while (!emplacePassed) |
| 198 | { |
| 199 | auto resp = deviceHashes.emplace(fruHash, newDev); |
| 200 | emplacePassed = resp.second; |
| 201 | if (!emplacePassed) |
| 202 | { |
| 203 | fruHash++; |
| 204 | // can't be 0xFF based on spec, and 0 is reserved for |
| 205 | // baseboard |
| 206 | if (fruHash == 0XFF) |
| 207 | { |
| 208 | fruHash = 0x1; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 215 | void replaceCacheFru( |
| 216 | const std::shared_ptr<sdbusplus::asio::connection>& bus, |
| 217 | boost::asio::yield_context& yield, |
| 218 | [[maybe_unused]] const std::optional<std::string>& path = std::nullopt) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 219 | { |
| 220 | boost::system::error_code ec; |
| 221 | |
| 222 | frus = bus->yield_method_call<ManagedObjectType>( |
| 223 | yield, ec, fruDeviceServiceName, "/", |
| 224 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 225 | if (ec) |
| 226 | { |
George Liu | de6694e | 2024-07-17 15:22:25 +0800 | [diff] [blame] | 227 | lg2::error("GetMangagedObjects for replaceCacheFru failed: {ERROR}", |
| 228 | "ERROR", ec.message()); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 229 | |
| 230 | return; |
| 231 | } |
| 232 | recalculateHashes(); |
| 233 | } |
| 234 | |
Patrick Williams | 69b4c28 | 2025-03-03 11:19:13 -0500 | [diff] [blame] | 235 | std::pair<ipmi::Cc, std::vector<uint8_t>> getFru(ipmi::Context::ptr ctx, |
| 236 | uint8_t devId) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 237 | { |
| 238 | if (lastDevId == devId && devId != 0xFF) |
| 239 | { |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 240 | return {ipmi::ccSuccess, fruCache}; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 243 | auto deviceFind = deviceHashes.find(devId); |
| 244 | if (deviceFind == deviceHashes.end()) |
| 245 | { |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 246 | return {IPMI_CC_SENSOR_INVALID, {}}; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 249 | cacheBus = deviceFind->second.first; |
| 250 | cacheAddr = deviceFind->second.second; |
| 251 | |
| 252 | boost::system::error_code ec; |
George Liu | de1420d | 2025-03-03 15:14:25 +0800 | [diff] [blame] | 253 | std::vector<uint8_t> fru = ipmi::callDbusMethod<std::vector<uint8_t>>( |
| 254 | ctx, ec, fruDeviceServiceName, "/xyz/openbmc_project/FruDevice", |
| 255 | "xyz.openbmc_project.FruDeviceManager", "GetRawFru", cacheBus, |
| 256 | cacheAddr); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 257 | |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 258 | if (ec) |
| 259 | { |
George Liu | de6694e | 2024-07-17 15:22:25 +0800 | [diff] [blame] | 260 | lg2::error("Couldn't get raw fru: {ERROR}", "ERROR", ec.message()); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 261 | |
krishnar4 | d90b3f0 | 2022-11-11 16:18:32 +0530 | [diff] [blame] | 262 | cacheBus = invalidBus; |
| 263 | cacheAddr = invalidAddr; |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 264 | return {ipmi::ccResponseError, {}}; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 267 | fruCache.clear(); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 268 | lastDevId = devId; |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 269 | fruCache = fru; |
| 270 | |
| 271 | return {ipmi::ccSuccess, fru}; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void writeFruIfRunning() |
| 275 | { |
| 276 | if (!writeTimer->isRunning()) |
| 277 | { |
| 278 | return; |
| 279 | } |
| 280 | writeTimer->stop(); |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 281 | writeFruCache(); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | void startMatch(void) |
| 285 | { |
| 286 | if (fruMatches.size()) |
| 287 | { |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | fruMatches.reserve(2); |
| 292 | |
| 293 | auto bus = getSdBus(); |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 294 | fruMatches.emplace_back( |
| 295 | *bus, |
| 296 | "type='signal',arg0path='/xyz/openbmc_project/" |
| 297 | "FruDevice/',member='InterfacesAdded'", |
| 298 | [](sdbusplus::message_t& message) { |
| 299 | sdbusplus::message::object_path path; |
| 300 | ObjectType object; |
| 301 | try |
| 302 | { |
| 303 | message.read(path, object); |
| 304 | } |
| 305 | catch (const sdbusplus::exception_t&) |
| 306 | { |
| 307 | return; |
| 308 | } |
| 309 | auto findType = object.find("xyz.openbmc_project.FruDevice"); |
| 310 | if (findType == object.end()) |
| 311 | { |
| 312 | return; |
| 313 | } |
| 314 | writeFruIfRunning(); |
| 315 | frus[path] = object; |
| 316 | recalculateHashes(); |
| 317 | lastDevId = 0xFF; |
| 318 | }); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 319 | |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 320 | fruMatches.emplace_back( |
| 321 | *bus, |
| 322 | "type='signal',arg0path='/xyz/openbmc_project/" |
| 323 | "FruDevice/',member='InterfacesRemoved'", |
| 324 | [](sdbusplus::message_t& message) { |
| 325 | sdbusplus::message::object_path path; |
| 326 | std::set<std::string> interfaces; |
| 327 | try |
| 328 | { |
| 329 | message.read(path, interfaces); |
| 330 | } |
| 331 | catch (const sdbusplus::exception_t&) |
| 332 | { |
| 333 | return; |
| 334 | } |
| 335 | auto findType = interfaces.find("xyz.openbmc_project.FruDevice"); |
| 336 | if (findType == interfaces.end()) |
| 337 | { |
| 338 | return; |
| 339 | } |
| 340 | writeFruIfRunning(); |
| 341 | frus.erase(path); |
| 342 | recalculateHashes(); |
| 343 | lastDevId = 0xFF; |
| 344 | }); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 345 | |
| 346 | // call once to populate |
Ed Tanous | 9860505 | 2025-02-13 16:57:13 -0800 | [diff] [blame] | 347 | boost::asio::spawn( |
| 348 | *getIoContext(), |
| 349 | [](boost::asio::yield_context yield) { |
| 350 | replaceCacheFru(getSdBus(), yield); |
| 351 | }, |
| 352 | boost::asio::detached); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /** @brief implements the read FRU data command |
| 356 | * @param fruDeviceId - FRU Device ID |
| 357 | * @param fruInventoryOffset - FRU Inventory Offset to write |
| 358 | * @param countToRead - Count to read |
| 359 | * |
| 360 | * @returns ipmi completion code plus response data |
| 361 | * - countWritten - Count written |
| 362 | */ |
| 363 | ipmi::RspType<uint8_t, // Count |
| 364 | std::vector<uint8_t> // Requested data |
| 365 | > |
| 366 | ipmiStorageReadFruData(ipmi::Context::ptr ctx, uint8_t fruDeviceId, |
| 367 | uint16_t fruInventoryOffset, uint8_t countToRead) |
| 368 | { |
| 369 | if (fruDeviceId == 0xFF) |
| 370 | { |
| 371 | return ipmi::responseInvalidFieldRequest(); |
| 372 | } |
| 373 | |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 374 | auto [status, fru] = getFru(ctx, fruDeviceId); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 375 | if (status != ipmi::ccSuccess) |
| 376 | { |
| 377 | return ipmi::response(status); |
| 378 | } |
| 379 | |
| 380 | size_t fromFruByteLen = 0; |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 381 | if (countToRead + fruInventoryOffset < fru.size()) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 382 | { |
| 383 | fromFruByteLen = countToRead; |
| 384 | } |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 385 | else if (fru.size() > fruInventoryOffset) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 386 | { |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 387 | fromFruByteLen = fru.size() - fruInventoryOffset; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 388 | } |
| 389 | else |
| 390 | { |
| 391 | return ipmi::responseReqDataLenExceeded(); |
| 392 | } |
| 393 | |
| 394 | std::vector<uint8_t> requestedData; |
| 395 | |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 396 | requestedData.insert(requestedData.begin(), |
| 397 | fru.begin() + fruInventoryOffset, |
| 398 | fru.begin() + fruInventoryOffset + fromFruByteLen); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 399 | |
| 400 | return ipmi::responseSuccess(static_cast<uint8_t>(requestedData.size()), |
| 401 | requestedData); |
| 402 | } |
| 403 | |
| 404 | /** @brief implements the write FRU data command |
| 405 | * @param fruDeviceId - FRU Device ID |
| 406 | * @param fruInventoryOffset - FRU Inventory Offset to write |
| 407 | * @param dataToWrite - Data to write |
| 408 | * |
| 409 | * @returns ipmi completion code plus response data |
| 410 | * - countWritten - Count written |
| 411 | */ |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 412 | ipmi::RspType<uint8_t> ipmiStorageWriteFruData( |
| 413 | ipmi::Context::ptr ctx, uint8_t fruDeviceId, uint16_t fruInventoryOffset, |
| 414 | std::vector<uint8_t>& dataToWrite) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 415 | { |
| 416 | if (fruDeviceId == 0xFF) |
| 417 | { |
| 418 | return ipmi::responseInvalidFieldRequest(); |
| 419 | } |
| 420 | |
| 421 | size_t writeLen = dataToWrite.size(); |
| 422 | |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 423 | auto [status, fru] = getFru(ctx, fruDeviceId); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 424 | if (status != ipmi::ccSuccess) |
| 425 | { |
| 426 | return ipmi::response(status); |
| 427 | } |
| 428 | size_t lastWriteAddr = fruInventoryOffset + writeLen; |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 429 | if (fru.size() < lastWriteAddr) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 430 | { |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 431 | fru.resize(fruInventoryOffset + writeLen); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | std::copy(dataToWrite.begin(), dataToWrite.begin() + writeLen, |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 435 | fru.begin() + fruInventoryOffset); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 436 | |
| 437 | bool atEnd = false; |
| 438 | |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 439 | if (fru.size() >= sizeof(FRUHeader)) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 440 | { |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 441 | FRUHeader* header = reinterpret_cast<FRUHeader*>(fru.data()); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 442 | |
| 443 | size_t areaLength = 0; |
| 444 | size_t lastRecordStart = std::max( |
| 445 | {header->internalOffset, header->chassisOffset, header->boardOffset, |
| 446 | header->productOffset, header->multiRecordOffset}); |
| 447 | lastRecordStart *= 8; // header starts in are multiples of 8 bytes |
| 448 | |
| 449 | if (header->multiRecordOffset) |
| 450 | { |
| 451 | // This FRU has a MultiRecord Area |
| 452 | uint8_t endOfList = 0; |
| 453 | // Walk the MultiRecord headers until the last record |
| 454 | while (!endOfList) |
| 455 | { |
| 456 | // The MSB in the second byte of the MultiRecord header signals |
| 457 | // "End of list" |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 458 | endOfList = fru[lastRecordStart + 1] & 0x80; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 459 | // Third byte in the MultiRecord header is the length |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 460 | areaLength = fru[lastRecordStart + 2]; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 461 | // This length is in bytes (not 8 bytes like other headers) |
| 462 | areaLength += 5; // The length omits the 5 byte header |
| 463 | if (!endOfList) |
| 464 | { |
| 465 | // Next MultiRecord header |
| 466 | lastRecordStart += areaLength; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | // This FRU does not have a MultiRecord Area |
| 473 | // Get the length of the area in multiples of 8 bytes |
| 474 | if (lastWriteAddr > (lastRecordStart + 1)) |
| 475 | { |
| 476 | // second byte in record area is the length |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 477 | areaLength = fru[lastRecordStart + 1]; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 478 | areaLength *= 8; // it is in multiples of 8 bytes |
| 479 | } |
| 480 | } |
| 481 | if (lastWriteAddr >= (areaLength + lastRecordStart)) |
| 482 | { |
| 483 | atEnd = true; |
| 484 | } |
| 485 | } |
| 486 | uint8_t countWritten = 0; |
| 487 | |
| 488 | writeBus = cacheBus; |
| 489 | writeAddr = cacheAddr; |
| 490 | if (atEnd) |
| 491 | { |
| 492 | // cancel timer, we're at the end so might as well send it |
| 493 | writeTimer->stop(); |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 494 | if (!writeFru(fru)) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 495 | { |
| 496 | return ipmi::responseInvalidFieldRequest(); |
| 497 | } |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 498 | countWritten = std::min(fru.size(), static_cast<size_t>(0xFF)); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 499 | } |
| 500 | else |
| 501 | { |
Sui Chen | 548d1a2 | 2022-09-14 07:41:17 -0700 | [diff] [blame] | 502 | fruCache = fru; // Write-back |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 503 | // start a timer, if no further data is sent to check to see if it is |
| 504 | // valid |
| 505 | writeTimer->start(std::chrono::duration_cast<std::chrono::microseconds>( |
| 506 | std::chrono::seconds(writeTimeoutSeconds))); |
| 507 | countWritten = 0; |
| 508 | } |
| 509 | |
| 510 | return ipmi::responseSuccess(countWritten); |
| 511 | } |
| 512 | |
| 513 | /** @brief implements the get FRU inventory area info command |
| 514 | * @param fruDeviceId - FRU Device ID |
| 515 | * |
| 516 | * @returns IPMI completion code plus response data |
| 517 | * - inventorySize - Number of possible allocation units |
| 518 | * - accessType - Allocation unit size in bytes. |
| 519 | */ |
| 520 | ipmi::RspType<uint16_t, // inventorySize |
| 521 | uint8_t> // accessType |
| 522 | ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruDeviceId) |
| 523 | { |
| 524 | if (fruDeviceId == 0xFF) |
| 525 | { |
| 526 | return ipmi::responseInvalidFieldRequest(); |
| 527 | } |
| 528 | |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 529 | auto [ret, fru] = getFru(ctx, fruDeviceId); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 530 | if (ret != ipmi::ccSuccess) |
| 531 | { |
| 532 | return ipmi::response(ret); |
| 533 | } |
| 534 | |
| 535 | constexpr uint8_t accessType = |
| 536 | static_cast<uint8_t>(GetFRUAreaAccessType::byte); |
| 537 | |
Willy Tu | 48fe64e | 2022-08-01 23:23:46 +0000 | [diff] [blame] | 538 | return ipmi::responseSuccess(fru.size(), accessType); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 541 | ipmi_ret_t getFruSdrCount(ipmi::Context::ptr, size_t& count) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 542 | { |
| 543 | count = deviceHashes.size(); |
| 544 | return IPMI_CC_OK; |
| 545 | } |
| 546 | |
Johnathan Mantey | 23a722c | 2023-05-12 08:18:54 -0700 | [diff] [blame] | 547 | ipmi_ret_t getFruSdrs([[maybe_unused]] ipmi::Context::ptr ctx, size_t index, |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 548 | get_sdr::SensorDataFruRecord& resp) |
| 549 | { |
| 550 | if (deviceHashes.size() < index) |
| 551 | { |
| 552 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 553 | } |
| 554 | auto device = deviceHashes.begin() + index; |
krishnar4 | d90b3f0 | 2022-11-11 16:18:32 +0530 | [diff] [blame] | 555 | uint16_t& bus = device->second.first; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 556 | uint8_t& address = device->second.second; |
| 557 | |
| 558 | boost::container::flat_map<std::string, Value>* fruData = nullptr; |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 559 | auto fru = std::find_if( |
| 560 | frus.begin(), frus.end(), |
| 561 | [bus, address, &fruData](ManagedEntry& entry) { |
| 562 | auto findFruDevice = |
| 563 | entry.second.find("xyz.openbmc_project.FruDevice"); |
| 564 | if (findFruDevice == entry.second.end()) |
| 565 | { |
| 566 | return false; |
| 567 | } |
| 568 | fruData = &(findFruDevice->second); |
| 569 | auto findBus = findFruDevice->second.find("BUS"); |
| 570 | auto findAddress = findFruDevice->second.find("ADDRESS"); |
| 571 | if (findBus == findFruDevice->second.end() || |
| 572 | findAddress == findFruDevice->second.end()) |
| 573 | { |
| 574 | return false; |
| 575 | } |
| 576 | if (std::get<uint32_t>(findBus->second) != bus) |
| 577 | { |
| 578 | return false; |
| 579 | } |
| 580 | if (std::get<uint32_t>(findAddress->second) != address) |
| 581 | { |
| 582 | return false; |
| 583 | } |
| 584 | return true; |
| 585 | }); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 586 | if (fru == frus.end()) |
| 587 | { |
| 588 | return IPMI_CC_RESPONSE_ERROR; |
| 589 | } |
Shakeeb Pasha | eacad3c | 2021-06-28 20:25:17 +0530 | [diff] [blame] | 590 | std::string name; |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 591 | uint8_t entityID = 0; |
| 592 | uint8_t entityInstance = 0x1; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 593 | |
| 594 | #ifdef USING_ENTITY_MANAGER_DECORATORS |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 595 | boost::system::error_code ec; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 596 | |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 597 | Paths subtreePaths = ipmi::callDbusMethod<Paths>( |
| 598 | ctx, ec, "xyz.openbmc_project.ObjectMapper", |
| 599 | "/xyz/openbmc_project/object_mapper", |
| 600 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", |
| 601 | "/xyz/openbmc_project/inventory", 0, |
| 602 | std::array<const char*, 2>{ |
| 603 | "xyz.openbmc_project.Inventory.Decorator.I2CDevice", |
| 604 | "xyz.openbmc_project.Inventory.Decorator.Ipmi", |
| 605 | }); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 606 | if (ec) |
| 607 | { |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 608 | lg2::error( |
| 609 | "GetSubTreePaths for ipmiStorageGetFruInvAreaInfo failed: {ERROR}", |
| 610 | "ERROR", ec.message()); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 611 | return ipmi::ccResponseError; |
| 612 | } |
| 613 | |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 614 | bool foundDevice = false; |
| 615 | for (const auto& path : subtreePaths) |
| 616 | { |
| 617 | ipmi::PropertyMap i2cProperties; |
| 618 | boost::system::error_code ec = ipmi::getAllDbusProperties( |
| 619 | ctx, "xyz.openbmc_project.EntityManager", path, |
| 620 | "xyz.openbmc_project.Inventory.Decorator.I2CDevice", i2cProperties); |
| 621 | if (ec) |
| 622 | { |
| 623 | continue; |
| 624 | } |
| 625 | |
| 626 | std::optional<uint64_t> maybeBus; |
| 627 | std::optional<uint64_t> maybeAddress; |
| 628 | std::optional<std::string> maybeName; |
| 629 | for (const auto& [key, val] : i2cProperties) |
| 630 | { |
| 631 | if (key == "Bus") |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 632 | { |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 633 | maybeBus = std::get<uint64_t>(val); |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 634 | } |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 635 | else if (key == "Address") |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 636 | { |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 637 | maybeAddress = std::get<uint64_t>(val); |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 638 | } |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 639 | else if (key == "Name") |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 640 | { |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 641 | maybeName = std::get<std::string>(val); |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 642 | } |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 643 | } |
| 644 | if (!maybeBus || *maybeBus != bus || !maybeAddress || |
| 645 | *maybeAddress != address) |
| 646 | { |
| 647 | continue; |
| 648 | } |
| 649 | // At this point we found the device entry and will populate the |
| 650 | // information if exist. |
| 651 | foundDevice = true; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 652 | |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 653 | if (maybeName.has_value()) |
| 654 | { |
| 655 | name = *maybeName; |
| 656 | } |
| 657 | |
| 658 | ipmi::PropertyMap entityData; |
| 659 | ec = ipmi::getAllDbusProperties( |
| 660 | ctx, "xyz.openbmc_project.EntityManager", path, |
| 661 | "xyz.openbmc_project.Inventory.Decorator.Ipmi", entityData); |
| 662 | if (!ec) |
| 663 | { |
| 664 | for (const auto& [key, val] : entityData) |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 665 | { |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 666 | if (key == "EntityId") |
| 667 | { |
| 668 | entityID = std::get<uint64_t>(val); |
| 669 | } |
| 670 | else if (key == "EntityInstance") |
| 671 | { |
| 672 | entityInstance = std::get<uint64_t>(val); |
| 673 | } |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 674 | } |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 675 | } |
| 676 | break; |
| 677 | } |
Shakeeb Pasha | eacad3c | 2021-06-28 20:25:17 +0530 | [diff] [blame] | 678 | |
Willy Tu | 06ed9da | 2025-04-07 00:44:20 +0000 | [diff] [blame^] | 679 | if (!foundDevice) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 680 | { |
| 681 | if constexpr (DEBUG) |
| 682 | { |
| 683 | std::fprintf(stderr, "Ipmi or FruDevice Decorator interface " |
| 684 | "not found for Fru\n"); |
| 685 | } |
| 686 | } |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 687 | #endif |
| 688 | |
Alexander Hansen | ea46f3c | 2023-09-04 11:27:54 +0200 | [diff] [blame] | 689 | std::vector<std::string> nameProperties = { |
Jitendra Tripathy | 1d881b5 | 2025-05-16 10:08:15 +0000 | [diff] [blame] | 690 | "BOARD_PRODUCT_NAME", "PRODUCT_PRODUCT_NAME", "PRODUCT_PART_NUMBER", |
Alexander Hansen | ea46f3c | 2023-09-04 11:27:54 +0200 | [diff] [blame] | 691 | "BOARD_PART_NUMBER", "PRODUCT_MANUFACTURER", "BOARD_MANUFACTURER", |
| 692 | "PRODUCT_SERIAL_NUMBER", "BOARD_SERIAL_NUMBER"}; |
| 693 | |
| 694 | for (const std::string& prop : nameProperties) |
| 695 | { |
| 696 | auto findProp = fruData->find(prop); |
| 697 | if (findProp != fruData->end()) |
| 698 | { |
| 699 | name = std::get<std::string>(findProp->second); |
| 700 | break; |
| 701 | } |
| 702 | } |
| 703 | |
Shakeeb Pasha | eacad3c | 2021-06-28 20:25:17 +0530 | [diff] [blame] | 704 | if (name.empty()) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 705 | { |
| 706 | name = "UNKNOWN"; |
| 707 | } |
| 708 | if (name.size() > maxFruSdrNameSize) |
| 709 | { |
| 710 | name = name.substr(0, maxFruSdrNameSize); |
| 711 | } |
| 712 | size_t sizeDiff = maxFruSdrNameSize - name.size(); |
| 713 | |
| 714 | resp.header.record_id_lsb = 0x0; // calling code is to implement these |
| 715 | resp.header.record_id_msb = 0x0; |
| 716 | resp.header.sdr_version = ipmiSdrVersion; |
| 717 | resp.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD; |
| 718 | resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff; |
| 719 | resp.key.deviceAddress = 0x20; |
| 720 | resp.key.fruID = device->first; |
| 721 | resp.key.accessLun = 0x80; // logical / physical fru device |
| 722 | resp.key.channelNumber = 0x0; |
| 723 | resp.body.reserved = 0x0; |
| 724 | resp.body.deviceType = 0x10; |
| 725 | resp.body.deviceTypeModifier = 0x0; |
| 726 | |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 727 | resp.body.entityID = entityID; |
| 728 | resp.body.entityInstance = entityInstance; |
| 729 | |
| 730 | resp.body.oem = 0x0; |
Johnathan Mantey | b99de18 | 2023-12-21 08:28:18 -0800 | [diff] [blame] | 731 | resp.body.deviceIDLen = ipmi::storage::typeASCIILatin8 | name.size(); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 732 | name.copy(resp.body.deviceID, name.size()); |
| 733 | |
| 734 | return IPMI_CC_OK; |
| 735 | } |
| 736 | |
| 737 | static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles) |
| 738 | { |
| 739 | // Loop through the directory looking for ipmi_sel log files |
| 740 | for (const std::filesystem::directory_entry& dirEnt : |
| 741 | std::filesystem::directory_iterator( |
| 742 | dynamic_sensors::ipmi::sel::selLogDir)) |
| 743 | { |
| 744 | std::string filename = dirEnt.path().filename(); |
| 745 | if (boost::starts_with(filename, |
| 746 | dynamic_sensors::ipmi::sel::selLogFilename)) |
| 747 | { |
| 748 | // If we find an ipmi_sel log file, save the path |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 749 | selLogFiles.emplace_back( |
| 750 | dynamic_sensors::ipmi::sel::selLogDir / filename); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | // As the log files rotate, they are appended with a ".#" that is higher for |
| 754 | // the older logs. Since we don't expect more than 10 log files, we |
| 755 | // can just sort the list to get them in order from newest to oldest |
| 756 | std::sort(selLogFiles.begin(), selLogFiles.end()); |
| 757 | |
| 758 | return !selLogFiles.empty(); |
| 759 | } |
| 760 | |
| 761 | static int countSELEntries() |
| 762 | { |
| 763 | // Get the list of ipmi_sel log files |
| 764 | std::vector<std::filesystem::path> selLogFiles; |
| 765 | if (!getSELLogFiles(selLogFiles)) |
| 766 | { |
| 767 | return 0; |
| 768 | } |
| 769 | int numSELEntries = 0; |
| 770 | // Loop through each log file and count the number of logs |
| 771 | for (const std::filesystem::path& file : selLogFiles) |
| 772 | { |
| 773 | std::ifstream logStream(file); |
| 774 | if (!logStream.is_open()) |
| 775 | { |
| 776 | continue; |
| 777 | } |
| 778 | |
| 779 | std::string line; |
| 780 | while (std::getline(logStream, line)) |
| 781 | { |
| 782 | numSELEntries++; |
| 783 | } |
| 784 | } |
| 785 | return numSELEntries; |
| 786 | } |
| 787 | |
| 788 | static bool findSELEntry(const int recordID, |
| 789 | const std::vector<std::filesystem::path>& selLogFiles, |
| 790 | std::string& entry) |
| 791 | { |
| 792 | // Record ID is the first entry field following the timestamp. It is |
| 793 | // preceded by a space and followed by a comma |
| 794 | std::string search = " " + std::to_string(recordID) + ","; |
| 795 | |
| 796 | // Loop through the ipmi_sel log entries |
| 797 | for (const std::filesystem::path& file : selLogFiles) |
| 798 | { |
| 799 | std::ifstream logStream(file); |
| 800 | if (!logStream.is_open()) |
| 801 | { |
| 802 | continue; |
| 803 | } |
| 804 | |
| 805 | while (std::getline(logStream, entry)) |
| 806 | { |
| 807 | // Check if the record ID matches |
| 808 | if (entry.find(search) != std::string::npos) |
| 809 | { |
| 810 | return true; |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | return false; |
| 815 | } |
| 816 | |
Patrick Williams | 69b4c28 | 2025-03-03 11:19:13 -0500 | [diff] [blame] | 817 | static uint16_t getNextRecordID( |
| 818 | const uint16_t recordID, |
| 819 | const std::vector<std::filesystem::path>& selLogFiles) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 820 | { |
| 821 | uint16_t nextRecordID = recordID + 1; |
| 822 | std::string entry; |
| 823 | if (findSELEntry(nextRecordID, selLogFiles, entry)) |
| 824 | { |
| 825 | return nextRecordID; |
| 826 | } |
| 827 | else |
| 828 | { |
| 829 | return ipmi::sel::lastEntry; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | static int fromHexStr(const std::string& hexStr, std::vector<uint8_t>& data) |
| 834 | { |
| 835 | for (unsigned int i = 0; i < hexStr.size(); i += 2) |
| 836 | { |
| 837 | try |
| 838 | { |
| 839 | data.push_back(static_cast<uint8_t>( |
| 840 | std::stoul(hexStr.substr(i, 2), nullptr, 16))); |
| 841 | } |
Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 842 | catch (const std::invalid_argument& e) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 843 | { |
George Liu | de6694e | 2024-07-17 15:22:25 +0800 | [diff] [blame] | 844 | lg2::error("Invalid argument: {ERROR}", "ERROR", e); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 845 | return -1; |
| 846 | } |
Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 847 | catch (const std::out_of_range& e) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 848 | { |
George Liu | de6694e | 2024-07-17 15:22:25 +0800 | [diff] [blame] | 849 | lg2::error("Out of range: {ERROR}", "ERROR", e); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 850 | return -1; |
| 851 | } |
| 852 | } |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | ipmi::RspType<uint8_t, // SEL version |
| 857 | uint16_t, // SEL entry count |
| 858 | uint16_t, // free space |
| 859 | uint32_t, // last add timestamp |
| 860 | uint32_t, // last erase timestamp |
| 861 | uint8_t> // operation support |
| 862 | ipmiStorageGetSELInfo() |
| 863 | { |
| 864 | constexpr uint8_t selVersion = ipmi::sel::selVersion; |
| 865 | uint16_t entries = countSELEntries(); |
| 866 | uint32_t addTimeStamp = dynamic_sensors::ipmi::sel::getFileTimestamp( |
| 867 | dynamic_sensors::ipmi::sel::selLogDir / |
| 868 | dynamic_sensors::ipmi::sel::selLogFilename); |
| 869 | uint32_t eraseTimeStamp = dynamic_sensors::ipmi::sel::erase_time::get(); |
| 870 | constexpr uint8_t operationSupport = |
| 871 | dynamic_sensors::ipmi::sel::selOperationSupport; |
| 872 | constexpr uint16_t freeSpace = |
| 873 | 0xffff; // Spec indicates that more than 64kB is free |
| 874 | |
| 875 | return ipmi::responseSuccess(selVersion, entries, freeSpace, addTimeStamp, |
| 876 | eraseTimeStamp, operationSupport); |
| 877 | } |
| 878 | |
| 879 | using systemEventType = std::tuple< |
| 880 | uint32_t, // Timestamp |
| 881 | uint16_t, // Generator ID |
| 882 | uint8_t, // EvM Rev |
| 883 | uint8_t, // Sensor Type |
| 884 | uint8_t, // Sensor Number |
| 885 | uint7_t, // Event Type |
| 886 | bool, // Event Direction |
| 887 | std::array<uint8_t, dynamic_sensors::ipmi::sel::systemEventSize>>; // Event |
| 888 | // Data |
| 889 | using oemTsEventType = std::tuple< |
| 890 | uint32_t, // Timestamp |
| 891 | std::array<uint8_t, dynamic_sensors::ipmi::sel::oemTsEventSize>>; // Event |
| 892 | // Data |
| 893 | using oemEventType = |
| 894 | std::array<uint8_t, dynamic_sensors::ipmi::sel::oemEventSize>; // Event Data |
| 895 | |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 896 | ipmi::RspType<uint16_t, // Next Record ID |
| 897 | uint16_t, // Record ID |
| 898 | uint8_t, // Record Type |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 899 | std::variant<systemEventType, oemTsEventType, |
| 900 | oemEventType>> // Record Content |
| 901 | ipmiStorageGetSELEntry(uint16_t reservationID, uint16_t targetID, |
| 902 | uint8_t offset, uint8_t size) |
| 903 | { |
| 904 | // Only support getting the entire SEL record. If a partial size or non-zero |
| 905 | // offset is requested, return an error |
| 906 | if (offset != 0 || size != ipmi::sel::entireRecord) |
| 907 | { |
| 908 | return ipmi::responseRetBytesUnavailable(); |
| 909 | } |
| 910 | |
| 911 | // Check the reservation ID if one is provided or required (only if the |
| 912 | // offset is non-zero) |
| 913 | if (reservationID != 0 || offset != 0) |
| 914 | { |
| 915 | if (!checkSELReservation(reservationID)) |
| 916 | { |
| 917 | return ipmi::responseInvalidReservationId(); |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | // Get the ipmi_sel log files |
| 922 | std::vector<std::filesystem::path> selLogFiles; |
| 923 | if (!getSELLogFiles(selLogFiles)) |
| 924 | { |
| 925 | return ipmi::responseSensorInvalid(); |
| 926 | } |
| 927 | |
| 928 | std::string targetEntry; |
| 929 | |
| 930 | if (targetID == ipmi::sel::firstEntry) |
| 931 | { |
| 932 | // The first entry will be at the top of the oldest log file |
| 933 | std::ifstream logStream(selLogFiles.back()); |
| 934 | if (!logStream.is_open()) |
| 935 | { |
| 936 | return ipmi::responseUnspecifiedError(); |
| 937 | } |
| 938 | |
| 939 | if (!std::getline(logStream, targetEntry)) |
| 940 | { |
| 941 | return ipmi::responseUnspecifiedError(); |
| 942 | } |
| 943 | } |
| 944 | else if (targetID == ipmi::sel::lastEntry) |
| 945 | { |
| 946 | // The last entry will be at the bottom of the newest log file |
| 947 | std::ifstream logStream(selLogFiles.front()); |
| 948 | if (!logStream.is_open()) |
| 949 | { |
| 950 | return ipmi::responseUnspecifiedError(); |
| 951 | } |
| 952 | |
| 953 | std::string line; |
| 954 | while (std::getline(logStream, line)) |
| 955 | { |
| 956 | targetEntry = line; |
| 957 | } |
| 958 | } |
| 959 | else |
| 960 | { |
| 961 | if (!findSELEntry(targetID, selLogFiles, targetEntry)) |
| 962 | { |
| 963 | return ipmi::responseSensorInvalid(); |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | // The format of the ipmi_sel message is "<Timestamp> |
| 968 | // <ID>,<Type>,<EventData>,[<Generator ID>,<Path>,<Direction>]". |
| 969 | // First get the Timestamp |
| 970 | size_t space = targetEntry.find_first_of(" "); |
| 971 | if (space == std::string::npos) |
| 972 | { |
| 973 | return ipmi::responseUnspecifiedError(); |
| 974 | } |
| 975 | std::string entryTimestamp = targetEntry.substr(0, space); |
| 976 | // Then get the log contents |
| 977 | size_t entryStart = targetEntry.find_first_not_of(" ", space); |
| 978 | if (entryStart == std::string::npos) |
| 979 | { |
| 980 | return ipmi::responseUnspecifiedError(); |
| 981 | } |
| 982 | std::string_view entry(targetEntry); |
| 983 | entry.remove_prefix(entryStart); |
| 984 | // Use split to separate the entry into its fields |
| 985 | std::vector<std::string> targetEntryFields; |
| 986 | boost::split(targetEntryFields, entry, boost::is_any_of(","), |
| 987 | boost::token_compress_on); |
| 988 | if (targetEntryFields.size() < 3) |
| 989 | { |
| 990 | return ipmi::responseUnspecifiedError(); |
| 991 | } |
| 992 | std::string& recordIDStr = targetEntryFields[0]; |
| 993 | std::string& recordTypeStr = targetEntryFields[1]; |
| 994 | std::string& eventDataStr = targetEntryFields[2]; |
| 995 | |
| 996 | uint16_t recordID; |
| 997 | uint8_t recordType; |
| 998 | try |
| 999 | { |
| 1000 | recordID = std::stoul(recordIDStr); |
| 1001 | recordType = std::stoul(recordTypeStr, nullptr, 16); |
| 1002 | } |
| 1003 | catch (const std::invalid_argument&) |
| 1004 | { |
| 1005 | return ipmi::responseUnspecifiedError(); |
| 1006 | } |
| 1007 | uint16_t nextRecordID = getNextRecordID(recordID, selLogFiles); |
| 1008 | std::vector<uint8_t> eventDataBytes; |
| 1009 | if (fromHexStr(eventDataStr, eventDataBytes) < 0) |
| 1010 | { |
| 1011 | return ipmi::responseUnspecifiedError(); |
| 1012 | } |
| 1013 | |
| 1014 | if (recordType == dynamic_sensors::ipmi::sel::systemEvent) |
| 1015 | { |
| 1016 | // Get the timestamp |
| 1017 | std::tm timeStruct = {}; |
| 1018 | std::istringstream entryStream(entryTimestamp); |
| 1019 | |
| 1020 | uint32_t timestamp = ipmi::sel::invalidTimeStamp; |
| 1021 | if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) |
| 1022 | { |
Willy Tu | 7bb412f | 2023-09-25 11:30:45 -0700 | [diff] [blame] | 1023 | timeStruct.tm_isdst = -1; |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1024 | timestamp = std::mktime(&timeStruct); |
| 1025 | } |
| 1026 | |
| 1027 | // Set the event message revision |
| 1028 | uint8_t evmRev = dynamic_sensors::ipmi::sel::eventMsgRev; |
| 1029 | |
| 1030 | uint16_t generatorID = 0; |
| 1031 | uint8_t sensorType = 0; |
| 1032 | uint16_t sensorAndLun = 0; |
| 1033 | uint8_t sensorNum = 0xFF; |
| 1034 | uint7_t eventType = 0; |
| 1035 | bool eventDir = 0; |
| 1036 | // System type events should have six fields |
| 1037 | if (targetEntryFields.size() >= 6) |
| 1038 | { |
| 1039 | std::string& generatorIDStr = targetEntryFields[3]; |
| 1040 | std::string& sensorPath = targetEntryFields[4]; |
| 1041 | std::string& eventDirStr = targetEntryFields[5]; |
| 1042 | |
| 1043 | // Get the generator ID |
| 1044 | try |
| 1045 | { |
| 1046 | generatorID = std::stoul(generatorIDStr, nullptr, 16); |
| 1047 | } |
| 1048 | catch (const std::invalid_argument&) |
| 1049 | { |
| 1050 | std::cerr << "Invalid Generator ID\n"; |
| 1051 | } |
| 1052 | |
| 1053 | // Get the sensor type, sensor number, and event type for the sensor |
| 1054 | sensorType = getSensorTypeFromPath(sensorPath); |
| 1055 | sensorAndLun = getSensorNumberFromPath(sensorPath); |
| 1056 | sensorNum = static_cast<uint8_t>(sensorAndLun); |
Harvey.Wu | 4376cdf | 2021-11-16 19:40:55 +0800 | [diff] [blame] | 1057 | if ((generatorID & 0x0001) == 0) |
| 1058 | { |
| 1059 | // IPMB Address |
| 1060 | generatorID |= sensorAndLun & 0x0300; |
| 1061 | } |
| 1062 | else |
| 1063 | { |
| 1064 | // system software |
| 1065 | generatorID |= sensorAndLun >> 8; |
| 1066 | } |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1067 | eventType = getSensorEventTypeFromPath(sensorPath); |
| 1068 | |
| 1069 | // Get the event direction |
| 1070 | try |
| 1071 | { |
| 1072 | eventDir = std::stoul(eventDirStr) ? 0 : 1; |
| 1073 | } |
| 1074 | catch (const std::invalid_argument&) |
| 1075 | { |
| 1076 | std::cerr << "Invalid Event Direction\n"; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | // Only keep the eventData bytes that fit in the record |
| 1081 | std::array<uint8_t, dynamic_sensors::ipmi::sel::systemEventSize> |
| 1082 | eventData{}; |
| 1083 | std::copy_n(eventDataBytes.begin(), |
| 1084 | std::min(eventDataBytes.size(), eventData.size()), |
| 1085 | eventData.begin()); |
| 1086 | |
| 1087 | return ipmi::responseSuccess( |
| 1088 | nextRecordID, recordID, recordType, |
| 1089 | systemEventType{timestamp, generatorID, evmRev, sensorType, |
| 1090 | sensorNum, eventType, eventDir, eventData}); |
| 1091 | } |
| 1092 | |
Thang Tran | e70c59b | 2023-09-21 13:54:28 +0700 | [diff] [blame] | 1093 | if (recordType >= dynamic_sensors::ipmi::sel::oemTsEventFirst && |
| 1094 | recordType <= dynamic_sensors::ipmi::sel::oemTsEventLast) |
| 1095 | { |
| 1096 | // Get the timestamp |
| 1097 | std::tm timeStruct = {}; |
| 1098 | std::istringstream entryStream(entryTimestamp); |
| 1099 | |
| 1100 | uint32_t timestamp = ipmi::sel::invalidTimeStamp; |
| 1101 | if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) |
| 1102 | { |
| 1103 | timeStruct.tm_isdst = -1; |
| 1104 | timestamp = std::mktime(&timeStruct); |
| 1105 | } |
| 1106 | |
| 1107 | // Only keep the bytes that fit in the record |
| 1108 | std::array<uint8_t, dynamic_sensors::ipmi::sel::oemTsEventSize> |
| 1109 | eventData{}; |
| 1110 | std::copy_n(eventDataBytes.begin(), |
| 1111 | std::min(eventDataBytes.size(), eventData.size()), |
| 1112 | eventData.begin()); |
| 1113 | |
| 1114 | return ipmi::responseSuccess(nextRecordID, recordID, recordType, |
| 1115 | oemTsEventType{timestamp, eventData}); |
| 1116 | } |
| 1117 | |
| 1118 | if (recordType >= dynamic_sensors::ipmi::sel::oemEventFirst) |
| 1119 | { |
| 1120 | // Only keep the bytes that fit in the record |
| 1121 | std::array<uint8_t, dynamic_sensors::ipmi::sel::oemEventSize> |
| 1122 | eventData{}; |
| 1123 | std::copy_n(eventDataBytes.begin(), |
| 1124 | std::min(eventDataBytes.size(), eventData.size()), |
| 1125 | eventData.begin()); |
| 1126 | |
| 1127 | return ipmi::responseSuccess(nextRecordID, recordID, recordType, |
| 1128 | eventData); |
| 1129 | } |
| 1130 | |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1131 | return ipmi::responseUnspecifiedError(); |
| 1132 | } |
| 1133 | |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 1134 | /* |
| 1135 | Unused arguments |
| 1136 | uint16_t recordID, uint8_t recordType, uint32_t timestamp, |
| 1137 | uint16_t generatorID, uint8_t evmRev, uint8_t sensorType, uint8_t sensorNum, |
| 1138 | uint8_t eventType, uint8_t eventData1, uint8_t eventData2, |
| 1139 | uint8_t eventData3 |
| 1140 | */ |
Patrick Williams | 69b4c28 | 2025-03-03 11:19:13 -0500 | [diff] [blame] | 1141 | ipmi::RspType<uint16_t> ipmiStorageAddSELEntry( |
| 1142 | uint16_t, uint8_t, uint32_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t, |
| 1143 | uint8_t, uint8_t, uint8_t) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1144 | { |
| 1145 | // Per the IPMI spec, need to cancel any reservation when a SEL entry is |
| 1146 | // added |
| 1147 | cancelSELReservation(); |
| 1148 | |
| 1149 | uint16_t responseID = 0xFFFF; |
| 1150 | return ipmi::responseSuccess(responseID); |
| 1151 | } |
| 1152 | |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 1153 | ipmi::RspType<uint8_t> ipmiStorageClearSEL( |
| 1154 | ipmi::Context::ptr ctx, uint16_t reservationID, |
| 1155 | const std::array<uint8_t, 3>& clr, uint8_t eraseOperation) |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1156 | { |
| 1157 | if (!checkSELReservation(reservationID)) |
| 1158 | { |
| 1159 | return ipmi::responseInvalidReservationId(); |
| 1160 | } |
| 1161 | |
| 1162 | static constexpr std::array<uint8_t, 3> clrExpected = {'C', 'L', 'R'}; |
| 1163 | if (clr != clrExpected) |
| 1164 | { |
| 1165 | return ipmi::responseInvalidFieldRequest(); |
| 1166 | } |
| 1167 | |
| 1168 | // Erasure status cannot be fetched, so always return erasure status as |
| 1169 | // `erase completed`. |
| 1170 | if (eraseOperation == ipmi::sel::getEraseStatus) |
| 1171 | { |
| 1172 | return ipmi::responseSuccess(ipmi::sel::eraseComplete); |
| 1173 | } |
| 1174 | |
| 1175 | // Check that initiate erase is correct |
| 1176 | if (eraseOperation != ipmi::sel::initiateErase) |
| 1177 | { |
| 1178 | return ipmi::responseInvalidFieldRequest(); |
| 1179 | } |
| 1180 | |
| 1181 | // Per the IPMI spec, need to cancel any reservation when the SEL is |
| 1182 | // cleared |
| 1183 | cancelSELReservation(); |
| 1184 | |
George Liu | de1420d | 2025-03-03 15:14:25 +0800 | [diff] [blame] | 1185 | boost::system::error_code ec = |
| 1186 | ipmi::callDbusMethod(ctx, "xyz.openbmc_project.Logging.IPMI", |
| 1187 | "/xyz/openbmc_project/Logging/IPMI", |
| 1188 | "xyz.openbmc_project.Logging.IPMI", "Clear"); |
Charles Boyer | 818bea1 | 2021-09-20 16:56:36 -0500 | [diff] [blame] | 1189 | if (ec) |
| 1190 | { |
George Liu | de1420d | 2025-03-03 15:14:25 +0800 | [diff] [blame] | 1191 | std::cerr << "error in clear SEL: " << ec.message() << std::endl; |
Charles Boyer | 818bea1 | 2021-09-20 16:56:36 -0500 | [diff] [blame] | 1192 | return ipmi::responseUnspecifiedError(); |
| 1193 | } |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1194 | |
| 1195 | return ipmi::responseSuccess(ipmi::sel::eraseComplete); |
| 1196 | } |
| 1197 | |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 1198 | std::vector<uint8_t> getType8SDRs( |
| 1199 | ipmi::sensor::EntityInfoMap::const_iterator& entity, uint16_t recordId) |
Harvey Wu | 05d17c0 | 2021-09-15 08:46:59 +0800 | [diff] [blame] | 1200 | { |
| 1201 | std::vector<uint8_t> resp; |
| 1202 | get_sdr::SensorDataEntityRecord data{}; |
| 1203 | |
| 1204 | /* Header */ |
| 1205 | get_sdr::header::set_record_id(recordId, &(data.header)); |
| 1206 | // Based on IPMI Spec v2.0 rev 1.1 |
| 1207 | data.header.sdr_version = SDR_VERSION; |
| 1208 | data.header.record_type = 0x08; |
| 1209 | data.header.record_length = sizeof(data.key) + sizeof(data.body); |
| 1210 | |
| 1211 | /* Key */ |
| 1212 | data.key.containerEntityId = entity->second.containerEntityId; |
| 1213 | data.key.containerEntityInstance = entity->second.containerEntityInstance; |
| 1214 | get_sdr::key::set_flags(entity->second.isList, entity->second.isLinked, |
| 1215 | &(data.key)); |
| 1216 | data.key.entityId1 = entity->second.containedEntities[0].first; |
| 1217 | data.key.entityInstance1 = entity->second.containedEntities[0].second; |
| 1218 | |
| 1219 | /* Body */ |
| 1220 | data.body.entityId2 = entity->second.containedEntities[1].first; |
| 1221 | data.body.entityInstance2 = entity->second.containedEntities[1].second; |
| 1222 | data.body.entityId3 = entity->second.containedEntities[2].first; |
| 1223 | data.body.entityInstance3 = entity->second.containedEntities[2].second; |
| 1224 | data.body.entityId4 = entity->second.containedEntities[3].first; |
| 1225 | data.body.entityInstance4 = entity->second.containedEntities[3].second; |
| 1226 | |
| 1227 | resp.insert(resp.end(), (uint8_t*)&data, ((uint8_t*)&data) + sizeof(data)); |
| 1228 | |
| 1229 | return resp; |
| 1230 | } |
| 1231 | |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1232 | std::vector<uint8_t> getType12SDRs(uint16_t index, uint16_t recordId) |
| 1233 | { |
| 1234 | std::vector<uint8_t> resp; |
| 1235 | if (index == 0) |
| 1236 | { |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1237 | std::string bmcName = "Basbrd Mgmt Ctlr"; |
Johnathan Mantey | cd1c496 | 2021-09-22 12:58:08 -0700 | [diff] [blame] | 1238 | Type12Record bmc(recordId, 0x20, 0, 0, 0xbf, 0x2e, 1, 0, bmcName); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1239 | uint8_t* bmcPtr = reinterpret_cast<uint8_t*>(&bmc); |
| 1240 | resp.insert(resp.end(), bmcPtr, bmcPtr + sizeof(Type12Record)); |
| 1241 | } |
| 1242 | else if (index == 1) |
| 1243 | { |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1244 | std::string meName = "Mgmt Engine"; |
Johnathan Mantey | cd1c496 | 2021-09-22 12:58:08 -0700 | [diff] [blame] | 1245 | Type12Record me(recordId, 0x2c, 6, 0x24, 0x21, 0x2e, 2, 0, meName); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1246 | uint8_t* mePtr = reinterpret_cast<uint8_t*>(&me); |
| 1247 | resp.insert(resp.end(), mePtr, mePtr + sizeof(Type12Record)); |
| 1248 | } |
| 1249 | else |
| 1250 | { |
Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame] | 1251 | throw std::runtime_error( |
| 1252 | "getType12SDRs:: Illegal index " + std::to_string(index)); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | return resp; |
| 1256 | } |
| 1257 | |
| 1258 | void registerStorageFunctions() |
| 1259 | { |
| 1260 | createTimers(); |
| 1261 | startMatch(); |
| 1262 | |
| 1263 | // <Get FRU Inventory Area Info> |
Willy Tu | d351a72 | 2021-08-12 14:33:40 -0700 | [diff] [blame] | 1264 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage, |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1265 | ipmi::storage::cmdGetFruInventoryAreaInfo, |
| 1266 | ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo); |
| 1267 | // <READ FRU Data> |
| 1268 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage, |
| 1269 | ipmi::storage::cmdReadFruData, ipmi::Privilege::User, |
| 1270 | ipmiStorageReadFruData); |
| 1271 | |
| 1272 | // <WRITE FRU Data> |
| 1273 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage, |
| 1274 | ipmi::storage::cmdWriteFruData, |
| 1275 | ipmi::Privilege::Operator, ipmiStorageWriteFruData); |
| 1276 | |
| 1277 | // <Get SEL Info> |
| 1278 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage, |
| 1279 | ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User, |
| 1280 | ipmiStorageGetSELInfo); |
| 1281 | |
| 1282 | // <Get SEL Entry> |
| 1283 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage, |
| 1284 | ipmi::storage::cmdGetSelEntry, ipmi::Privilege::User, |
| 1285 | ipmiStorageGetSELEntry); |
| 1286 | |
| 1287 | // <Add SEL Entry> |
| 1288 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage, |
| 1289 | ipmi::storage::cmdAddSelEntry, |
| 1290 | ipmi::Privilege::Operator, ipmiStorageAddSELEntry); |
| 1291 | |
| 1292 | // <Clear SEL> |
| 1293 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage, |
| 1294 | ipmi::storage::cmdClearSel, ipmi::Privilege::Operator, |
| 1295 | ipmiStorageClearSEL); |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1296 | } |
| 1297 | } // namespace storage |
| 1298 | } // namespace ipmi |