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