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 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 17 | #include <boost/algorithm/string.hpp> |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 18 | #include <boost/container/flat_map.hpp> |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 19 | #include <boost/process.hpp> |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 20 | #include <commandutils.hpp> |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 21 | #include <filesystem> |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 22 | #include <iostream> |
Jason M. Bills | 99b78ec | 2019-01-18 10:42:18 -0800 | [diff] [blame] | 23 | #include <ipmi_to_redfish_hooks.hpp> |
James Feist | 2a265d5 | 2019-04-08 11:16:27 -0700 | [diff] [blame] | 24 | #include <ipmid/api.hpp> |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 25 | #include <phosphor-ipmi-host/selutility.hpp> |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 26 | #include <phosphor-logging/log.hpp> |
| 27 | #include <sdbusplus/message/types.hpp> |
| 28 | #include <sdbusplus/timer.hpp> |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 29 | #include <sdrutils.hpp> |
| 30 | #include <stdexcept> |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 31 | #include <storagecommands.hpp> |
Jason M. Bills | 52aaa7d | 2019-05-08 15:21:39 -0700 | [diff] [blame] | 32 | #include <string_view> |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 33 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 34 | namespace intel_oem::ipmi::sel |
| 35 | { |
| 36 | static const std::filesystem::path selLogDir = "/var/log"; |
| 37 | static const std::string selLogFilename = "ipmi_sel"; |
| 38 | |
| 39 | static int getFileTimestamp(const std::filesystem::path& file) |
| 40 | { |
| 41 | struct stat st; |
| 42 | |
| 43 | if (stat(file.c_str(), &st) >= 0) |
| 44 | { |
| 45 | return st.st_mtime; |
| 46 | } |
| 47 | return ::ipmi::sel::invalidTimeStamp; |
| 48 | } |
| 49 | |
| 50 | namespace erase_time |
Jason M. Bills | 7944c30 | 2019-03-20 15:24:05 -0700 | [diff] [blame] | 51 | { |
| 52 | static constexpr const char* selEraseTimestamp = "/var/lib/ipmi/sel_erase_time"; |
| 53 | |
| 54 | void save() |
| 55 | { |
| 56 | // open the file, creating it if necessary |
| 57 | int fd = open(selEraseTimestamp, O_WRONLY | O_CREAT | O_CLOEXEC, 0644); |
| 58 | if (fd < 0) |
| 59 | { |
| 60 | std::cerr << "Failed to open file\n"; |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | // update the file timestamp to the current time |
| 65 | if (futimens(fd, NULL) < 0) |
| 66 | { |
| 67 | std::cerr << "Failed to update timestamp: " |
| 68 | << std::string(strerror(errno)); |
| 69 | } |
| 70 | close(fd); |
| 71 | } |
| 72 | |
| 73 | int get() |
| 74 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 75 | return getFileTimestamp(selEraseTimestamp); |
Jason M. Bills | 7944c30 | 2019-03-20 15:24:05 -0700 | [diff] [blame] | 76 | } |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 77 | } // namespace erase_time |
| 78 | } // namespace intel_oem::ipmi::sel |
Jason M. Bills | 7944c30 | 2019-03-20 15:24:05 -0700 | [diff] [blame] | 79 | |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 80 | namespace ipmi |
| 81 | { |
| 82 | |
| 83 | namespace storage |
| 84 | { |
| 85 | |
Jason M. Bills | e2d1aee | 2018-10-03 15:57:18 -0700 | [diff] [blame] | 86 | constexpr static const size_t maxMessageSize = 64; |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 87 | constexpr static const size_t maxFruSdrNameSize = 16; |
| 88 | using ManagedObjectType = boost::container::flat_map< |
| 89 | sdbusplus::message::object_path, |
| 90 | boost::container::flat_map< |
| 91 | std::string, boost::container::flat_map<std::string, DbusVariant>>>; |
| 92 | using ManagedEntry = std::pair< |
| 93 | sdbusplus::message::object_path, |
| 94 | boost::container::flat_map< |
| 95 | std::string, boost::container::flat_map<std::string, DbusVariant>>>; |
| 96 | |
James Feist | 3bcba45 | 2018-12-20 12:31:03 -0800 | [diff] [blame] | 97 | constexpr static const char* fruDeviceServiceName = |
| 98 | "xyz.openbmc_project.FruDevice"; |
Jason M. Bills | e2d1aee | 2018-10-03 15:57:18 -0700 | [diff] [blame] | 99 | constexpr static const size_t cacheTimeoutSeconds = 10; |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 100 | |
Jason M. Bills | 4ed6f2c | 2019-04-02 12:21:25 -0700 | [diff] [blame] | 101 | // event direction is bit[7] of eventType where 1b = Deassertion event |
| 102 | constexpr static const uint8_t deassertionEvent = 0x80; |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 103 | |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 104 | static std::vector<uint8_t> fruCache; |
| 105 | static uint8_t cacheBus = 0xFF; |
| 106 | static uint8_t cacheAddr = 0XFF; |
| 107 | |
| 108 | std::unique_ptr<phosphor::Timer> cacheTimer = nullptr; |
| 109 | |
| 110 | // we unfortunately have to build a map of hashes in case there is a |
| 111 | // collision to verify our dev-id |
| 112 | boost::container::flat_map<uint8_t, std::pair<uint8_t, uint8_t>> deviceHashes; |
| 113 | |
Jason M. Bills | e2d1aee | 2018-10-03 15:57:18 -0700 | [diff] [blame] | 114 | void registerStorageFunctions() __attribute__((constructor)); |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 115 | static sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection()); |
| 116 | |
| 117 | bool writeFru() |
| 118 | { |
| 119 | sdbusplus::message::message writeFru = dbus.new_method_call( |
| 120 | fruDeviceServiceName, "/xyz/openbmc_project/FruDevice", |
| 121 | "xyz.openbmc_project.FruDeviceManager", "WriteFru"); |
| 122 | writeFru.append(cacheBus, cacheAddr, fruCache); |
| 123 | try |
| 124 | { |
| 125 | sdbusplus::message::message writeFruResp = dbus.call(writeFru); |
| 126 | } |
| 127 | catch (sdbusplus::exception_t&) |
| 128 | { |
| 129 | // todo: log sel? |
| 130 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 131 | "error writing fru"); |
| 132 | return false; |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
Jason M. Bills | e2d1aee | 2018-10-03 15:57:18 -0700 | [diff] [blame] | 137 | void createTimer() |
| 138 | { |
| 139 | if (cacheTimer == nullptr) |
| 140 | { |
| 141 | cacheTimer = std::make_unique<phosphor::Timer>(writeFru); |
| 142 | } |
| 143 | } |
| 144 | |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 145 | ipmi_ret_t replaceCacheFru(uint8_t devId) |
| 146 | { |
| 147 | static uint8_t lastDevId = 0xFF; |
| 148 | |
| 149 | bool timerRunning = (cacheTimer != nullptr) && !cacheTimer->isExpired(); |
| 150 | if (lastDevId == devId && timerRunning) |
| 151 | { |
| 152 | return IPMI_CC_OK; // cache already up to date |
| 153 | } |
| 154 | // if timer is running, stop it and writeFru manually |
| 155 | else if (timerRunning) |
| 156 | { |
| 157 | cacheTimer->stop(); |
| 158 | writeFru(); |
| 159 | } |
| 160 | |
| 161 | sdbusplus::message::message getObjects = dbus.new_method_call( |
| 162 | fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager", |
| 163 | "GetManagedObjects"); |
| 164 | ManagedObjectType frus; |
| 165 | try |
| 166 | { |
| 167 | sdbusplus::message::message resp = dbus.call(getObjects); |
| 168 | resp.read(frus); |
| 169 | } |
| 170 | catch (sdbusplus::exception_t&) |
| 171 | { |
| 172 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 173 | "replaceCacheFru: error getting managed objects"); |
| 174 | return IPMI_CC_RESPONSE_ERROR; |
| 175 | } |
| 176 | |
| 177 | deviceHashes.clear(); |
| 178 | |
| 179 | // hash the object paths to create unique device id's. increment on |
| 180 | // collision |
| 181 | std::hash<std::string> hasher; |
| 182 | for (const auto& fru : frus) |
| 183 | { |
| 184 | auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice"); |
| 185 | if (fruIface == fru.second.end()) |
| 186 | { |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | auto busFind = fruIface->second.find("BUS"); |
| 191 | auto addrFind = fruIface->second.find("ADDRESS"); |
| 192 | if (busFind == fruIface->second.end() || |
| 193 | addrFind == fruIface->second.end()) |
| 194 | { |
| 195 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 196 | "fru device missing Bus or Address", |
| 197 | phosphor::logging::entry("FRU=%s", fru.first.str.c_str())); |
| 198 | continue; |
| 199 | } |
| 200 | |
Vernon Mauery | 8166c8d | 2019-05-23 11:22:30 -0700 | [diff] [blame^] | 201 | uint8_t fruBus = std::get<uint32_t>(busFind->second); |
| 202 | uint8_t fruAddr = std::get<uint32_t>(addrFind->second); |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 203 | |
| 204 | uint8_t fruHash = 0; |
| 205 | if (fruBus != 0 || fruAddr != 0) |
| 206 | { |
| 207 | fruHash = hasher(fru.first.str); |
| 208 | // can't be 0xFF based on spec, and 0 is reserved for baseboard |
| 209 | if (fruHash == 0 || fruHash == 0xFF) |
| 210 | { |
| 211 | fruHash = 1; |
| 212 | } |
| 213 | } |
| 214 | std::pair<uint8_t, uint8_t> newDev(fruBus, fruAddr); |
| 215 | |
| 216 | bool emplacePassed = false; |
| 217 | while (!emplacePassed) |
| 218 | { |
| 219 | auto resp = deviceHashes.emplace(fruHash, newDev); |
| 220 | emplacePassed = resp.second; |
| 221 | if (!emplacePassed) |
| 222 | { |
| 223 | fruHash++; |
| 224 | // can't be 0xFF based on spec, and 0 is reserved for |
| 225 | // baseboard |
| 226 | if (fruHash == 0XFF) |
| 227 | { |
| 228 | fruHash = 0x1; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | auto deviceFind = deviceHashes.find(devId); |
| 234 | if (deviceFind == deviceHashes.end()) |
| 235 | { |
| 236 | return IPMI_CC_SENSOR_INVALID; |
| 237 | } |
| 238 | |
| 239 | fruCache.clear(); |
| 240 | sdbusplus::message::message getRawFru = dbus.new_method_call( |
| 241 | fruDeviceServiceName, "/xyz/openbmc_project/FruDevice", |
| 242 | "xyz.openbmc_project.FruDeviceManager", "GetRawFru"); |
| 243 | cacheBus = deviceFind->second.first; |
| 244 | cacheAddr = deviceFind->second.second; |
| 245 | getRawFru.append(cacheBus, cacheAddr); |
| 246 | try |
| 247 | { |
| 248 | sdbusplus::message::message getRawResp = dbus.call(getRawFru); |
| 249 | getRawResp.read(fruCache); |
| 250 | } |
| 251 | catch (sdbusplus::exception_t&) |
| 252 | { |
| 253 | lastDevId = 0xFF; |
| 254 | cacheBus = 0xFF; |
| 255 | cacheAddr = 0xFF; |
| 256 | return IPMI_CC_RESPONSE_ERROR; |
| 257 | } |
| 258 | |
| 259 | lastDevId = devId; |
| 260 | return IPMI_CC_OK; |
| 261 | } |
| 262 | |
Jason M. Bills | e2d1aee | 2018-10-03 15:57:18 -0700 | [diff] [blame] | 263 | ipmi_ret_t ipmiStorageReadFRUData(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 264 | ipmi_request_t request, |
| 265 | ipmi_response_t response, |
| 266 | ipmi_data_len_t dataLen, |
| 267 | ipmi_context_t context) |
| 268 | { |
| 269 | if (*dataLen != 4) |
| 270 | { |
| 271 | *dataLen = 0; |
| 272 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 273 | } |
| 274 | *dataLen = 0; // default to 0 in case of an error |
| 275 | |
| 276 | auto req = static_cast<GetFRUAreaReq*>(request); |
| 277 | |
| 278 | if (req->countToRead > maxMessageSize - 1) |
| 279 | { |
| 280 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 281 | } |
| 282 | ipmi_ret_t status = replaceCacheFru(req->fruDeviceID); |
| 283 | |
| 284 | if (status != IPMI_CC_OK) |
| 285 | { |
| 286 | return status; |
| 287 | } |
| 288 | |
| 289 | size_t fromFRUByteLen = 0; |
| 290 | if (req->countToRead + req->fruInventoryOffset < fruCache.size()) |
| 291 | { |
| 292 | fromFRUByteLen = req->countToRead; |
| 293 | } |
| 294 | else if (fruCache.size() > req->fruInventoryOffset) |
| 295 | { |
| 296 | fromFRUByteLen = fruCache.size() - req->fruInventoryOffset; |
| 297 | } |
| 298 | size_t padByteLen = req->countToRead - fromFRUByteLen; |
| 299 | uint8_t* respPtr = static_cast<uint8_t*>(response); |
| 300 | *respPtr = req->countToRead; |
| 301 | std::copy(fruCache.begin() + req->fruInventoryOffset, |
| 302 | fruCache.begin() + req->fruInventoryOffset + fromFRUByteLen, |
| 303 | ++respPtr); |
| 304 | // if longer than the fru is requested, fill with 0xFF |
| 305 | if (padByteLen) |
| 306 | { |
| 307 | respPtr += fromFRUByteLen; |
| 308 | std::fill(respPtr, respPtr + padByteLen, 0xFF); |
| 309 | } |
| 310 | *dataLen = fromFRUByteLen + 1; |
| 311 | |
| 312 | return IPMI_CC_OK; |
| 313 | } |
| 314 | |
| 315 | ipmi_ret_t ipmiStorageWriteFRUData(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 316 | ipmi_request_t request, |
| 317 | ipmi_response_t response, |
| 318 | ipmi_data_len_t dataLen, |
| 319 | ipmi_context_t context) |
| 320 | { |
| 321 | if (*dataLen < 4 || |
| 322 | *dataLen >= |
| 323 | 0xFF + 3) // count written return is one byte, so limit to one byte |
| 324 | // of data after the three request data bytes |
| 325 | { |
| 326 | *dataLen = 0; |
| 327 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 328 | } |
| 329 | |
| 330 | auto req = static_cast<WriteFRUDataReq*>(request); |
| 331 | size_t writeLen = *dataLen - 3; |
| 332 | *dataLen = 0; // default to 0 in case of an error |
| 333 | |
| 334 | ipmi_ret_t status = replaceCacheFru(req->fruDeviceID); |
| 335 | if (status != IPMI_CC_OK) |
| 336 | { |
| 337 | return status; |
| 338 | } |
| 339 | int lastWriteAddr = req->fruInventoryOffset + writeLen; |
| 340 | if (fruCache.size() < lastWriteAddr) |
| 341 | { |
| 342 | fruCache.resize(req->fruInventoryOffset + writeLen); |
| 343 | } |
| 344 | |
| 345 | std::copy(req->data, req->data + writeLen, |
| 346 | fruCache.begin() + req->fruInventoryOffset); |
| 347 | |
| 348 | bool atEnd = false; |
| 349 | |
| 350 | if (fruCache.size() >= sizeof(FRUHeader)) |
| 351 | { |
| 352 | |
| 353 | FRUHeader* header = reinterpret_cast<FRUHeader*>(fruCache.data()); |
| 354 | |
| 355 | int lastRecordStart = std::max( |
| 356 | header->internalOffset, |
| 357 | std::max(header->chassisOffset, |
| 358 | std::max(header->boardOffset, header->productOffset))); |
| 359 | // TODO: Handle Multi-Record FRUs? |
| 360 | |
| 361 | lastRecordStart *= 8; // header starts in are multiples of 8 bytes |
| 362 | |
| 363 | // get the length of the area in multiples of 8 bytes |
| 364 | if (lastWriteAddr > (lastRecordStart + 1)) |
| 365 | { |
| 366 | // second byte in record area is the length |
| 367 | int areaLength(fruCache[lastRecordStart + 1]); |
| 368 | areaLength *= 8; // it is in multiples of 8 bytes |
| 369 | |
| 370 | if (lastWriteAddr >= (areaLength + lastRecordStart)) |
| 371 | { |
| 372 | atEnd = true; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | uint8_t* respPtr = static_cast<uint8_t*>(response); |
| 377 | if (atEnd) |
| 378 | { |
| 379 | // cancel timer, we're at the end so might as well send it |
| 380 | cacheTimer->stop(); |
| 381 | if (!writeFru()) |
| 382 | { |
| 383 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 384 | } |
| 385 | *respPtr = std::min(fruCache.size(), static_cast<size_t>(0xFF)); |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | // start a timer, if no further data is sent in cacheTimeoutSeconds |
| 390 | // seconds, check to see if it is valid |
| 391 | createTimer(); |
| 392 | cacheTimer->start(std::chrono::duration_cast<std::chrono::microseconds>( |
| 393 | std::chrono::seconds(cacheTimeoutSeconds))); |
| 394 | *respPtr = 0; |
| 395 | } |
| 396 | |
| 397 | *dataLen = 1; |
| 398 | |
| 399 | return IPMI_CC_OK; |
| 400 | } |
| 401 | |
| 402 | ipmi_ret_t ipmiStorageGetFRUInvAreaInfo(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 403 | ipmi_request_t request, |
| 404 | ipmi_response_t response, |
| 405 | ipmi_data_len_t dataLen, |
| 406 | ipmi_context_t context) |
| 407 | { |
| 408 | if (*dataLen != 1) |
| 409 | { |
| 410 | *dataLen = 0; |
| 411 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 412 | } |
| 413 | *dataLen = 0; // default to 0 in case of an error |
| 414 | |
| 415 | uint8_t reqDev = *(static_cast<uint8_t*>(request)); |
| 416 | if (reqDev == 0xFF) |
| 417 | { |
| 418 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 419 | } |
| 420 | ipmi_ret_t status = replaceCacheFru(reqDev); |
| 421 | |
| 422 | if (status != IPMI_CC_OK) |
| 423 | { |
| 424 | return status; |
| 425 | } |
| 426 | |
| 427 | GetFRUAreaResp* respPtr = static_cast<GetFRUAreaResp*>(response); |
| 428 | respPtr->inventorySizeLSB = fruCache.size() & 0xFF; |
| 429 | respPtr->inventorySizeMSB = fruCache.size() >> 8; |
| 430 | respPtr->accessType = static_cast<uint8_t>(GetFRUAreaAccessType::byte); |
| 431 | |
| 432 | *dataLen = sizeof(GetFRUAreaResp); |
| 433 | return IPMI_CC_OK; |
| 434 | } |
| 435 | |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 436 | ipmi_ret_t getFruSdrCount(size_t& count) |
| 437 | { |
| 438 | ipmi_ret_t ret = replaceCacheFru(0); |
| 439 | if (ret != IPMI_CC_OK) |
| 440 | { |
| 441 | return ret; |
| 442 | } |
| 443 | count = deviceHashes.size(); |
| 444 | return IPMI_CC_OK; |
| 445 | } |
| 446 | |
| 447 | ipmi_ret_t getFruSdrs(size_t index, get_sdr::SensorDataFruRecord& resp) |
| 448 | { |
| 449 | ipmi_ret_t ret = replaceCacheFru(0); // this will update the hash list |
| 450 | if (ret != IPMI_CC_OK) |
| 451 | { |
| 452 | return ret; |
| 453 | } |
| 454 | if (deviceHashes.size() < index) |
| 455 | { |
| 456 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 457 | } |
| 458 | auto device = deviceHashes.begin() + index; |
| 459 | uint8_t& bus = device->second.first; |
| 460 | uint8_t& address = device->second.second; |
| 461 | |
| 462 | ManagedObjectType frus; |
| 463 | |
| 464 | sdbusplus::message::message getObjects = dbus.new_method_call( |
| 465 | fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager", |
| 466 | "GetManagedObjects"); |
| 467 | try |
| 468 | { |
| 469 | sdbusplus::message::message resp = dbus.call(getObjects); |
| 470 | resp.read(frus); |
| 471 | } |
| 472 | catch (sdbusplus::exception_t&) |
| 473 | { |
| 474 | return IPMI_CC_RESPONSE_ERROR; |
| 475 | } |
| 476 | boost::container::flat_map<std::string, DbusVariant>* fruData = nullptr; |
| 477 | auto fru = |
| 478 | std::find_if(frus.begin(), frus.end(), |
| 479 | [bus, address, &fruData](ManagedEntry& entry) { |
| 480 | auto findFruDevice = |
| 481 | entry.second.find("xyz.openbmc_project.FruDevice"); |
| 482 | if (findFruDevice == entry.second.end()) |
| 483 | { |
| 484 | return false; |
| 485 | } |
| 486 | fruData = &(findFruDevice->second); |
| 487 | auto findBus = findFruDevice->second.find("BUS"); |
| 488 | auto findAddress = |
| 489 | findFruDevice->second.find("ADDRESS"); |
| 490 | if (findBus == findFruDevice->second.end() || |
| 491 | findAddress == findFruDevice->second.end()) |
| 492 | { |
| 493 | return false; |
| 494 | } |
Vernon Mauery | 8166c8d | 2019-05-23 11:22:30 -0700 | [diff] [blame^] | 495 | if (std::get<uint32_t>(findBus->second) != bus) |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 496 | { |
| 497 | return false; |
| 498 | } |
Vernon Mauery | 8166c8d | 2019-05-23 11:22:30 -0700 | [diff] [blame^] | 499 | if (std::get<uint32_t>(findAddress->second) != address) |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 500 | { |
| 501 | return false; |
| 502 | } |
| 503 | return true; |
| 504 | }); |
| 505 | if (fru == frus.end()) |
| 506 | { |
| 507 | return IPMI_CC_RESPONSE_ERROR; |
| 508 | } |
| 509 | std::string name; |
| 510 | auto findProductName = fruData->find("BOARD_PRODUCT_NAME"); |
| 511 | auto findBoardName = fruData->find("PRODUCT_PRODUCT_NAME"); |
| 512 | if (findProductName != fruData->end()) |
| 513 | { |
Vernon Mauery | 8166c8d | 2019-05-23 11:22:30 -0700 | [diff] [blame^] | 514 | name = std::get<std::string>(findProductName->second); |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 515 | } |
| 516 | else if (findBoardName != fruData->end()) |
| 517 | { |
Vernon Mauery | 8166c8d | 2019-05-23 11:22:30 -0700 | [diff] [blame^] | 518 | name = std::get<std::string>(findBoardName->second); |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 519 | } |
| 520 | else |
| 521 | { |
| 522 | name = "UNKNOWN"; |
| 523 | } |
| 524 | if (name.size() > maxFruSdrNameSize) |
| 525 | { |
| 526 | name = name.substr(0, maxFruSdrNameSize); |
| 527 | } |
| 528 | size_t sizeDiff = maxFruSdrNameSize - name.size(); |
| 529 | |
| 530 | resp.header.record_id_lsb = 0x0; // calling code is to implement these |
| 531 | resp.header.record_id_msb = 0x0; |
| 532 | resp.header.sdr_version = ipmiSdrVersion; |
| 533 | resp.header.record_type = 0x11; // FRU Device Locator |
| 534 | resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff; |
| 535 | resp.key.deviceAddress = 0x20; |
| 536 | resp.key.fruID = device->first; |
| 537 | resp.key.accessLun = 0x80; // logical / physical fru device |
| 538 | resp.key.channelNumber = 0x0; |
| 539 | resp.body.reserved = 0x0; |
| 540 | resp.body.deviceType = 0x10; |
James Feist | 4f86d1f | 2019-04-03 10:30:26 -0700 | [diff] [blame] | 541 | resp.body.deviceTypeModifier = 0x0; |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 542 | resp.body.entityID = 0x0; |
| 543 | resp.body.entityInstance = 0x1; |
| 544 | resp.body.oem = 0x0; |
| 545 | resp.body.deviceIDLen = name.size(); |
| 546 | name.copy(resp.body.deviceID, name.size()); |
| 547 | |
| 548 | return IPMI_CC_OK; |
| 549 | } |
Jason M. Bills | e2d1aee | 2018-10-03 15:57:18 -0700 | [diff] [blame] | 550 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 551 | static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 552 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 553 | // Loop through the directory looking for ipmi_sel log files |
| 554 | for (const std::filesystem::directory_entry& dirEnt : |
| 555 | std::filesystem::directory_iterator(intel_oem::ipmi::sel::selLogDir)) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 556 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 557 | std::string filename = dirEnt.path().filename(); |
| 558 | if (boost::starts_with(filename, intel_oem::ipmi::sel::selLogFilename)) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 559 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 560 | // If we find an ipmi_sel log file, save the path |
| 561 | selLogFiles.emplace_back(intel_oem::ipmi::sel::selLogDir / |
| 562 | filename); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 563 | } |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 564 | } |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 565 | // As the log files rotate, they are appended with a ".#" that is higher for |
| 566 | // the older logs. Since we don't expect more than 10 log files, we |
| 567 | // can just sort the list to get them in order from newest to oldest |
| 568 | std::sort(selLogFiles.begin(), selLogFiles.end()); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 569 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 570 | return !selLogFiles.empty(); |
| 571 | } |
| 572 | |
| 573 | static int countSELEntries() |
| 574 | { |
| 575 | // Get the list of ipmi_sel log files |
| 576 | std::vector<std::filesystem::path> selLogFiles; |
| 577 | if (!getSELLogFiles(selLogFiles)) |
| 578 | { |
| 579 | return 0; |
| 580 | } |
| 581 | int numSELEntries = 0; |
| 582 | // Loop through each log file and count the number of logs |
| 583 | for (const std::filesystem::path& file : selLogFiles) |
| 584 | { |
| 585 | std::ifstream logStream(file); |
| 586 | if (!logStream.is_open()) |
| 587 | { |
| 588 | continue; |
| 589 | } |
| 590 | |
| 591 | std::string line; |
| 592 | while (std::getline(logStream, line)) |
| 593 | { |
| 594 | numSELEntries++; |
| 595 | } |
| 596 | } |
| 597 | return numSELEntries; |
| 598 | } |
| 599 | |
| 600 | static bool findSELEntry(const int recordID, |
| 601 | const std::vector<std::filesystem::path> selLogFiles, |
| 602 | std::string& entry) |
| 603 | { |
| 604 | // Record ID is the first entry field following the timestamp. It is |
| 605 | // preceded by a space and followed by a comma |
| 606 | std::string search = " " + std::to_string(recordID) + ","; |
| 607 | |
| 608 | // Loop through the ipmi_sel log entries |
| 609 | for (const std::filesystem::path& file : selLogFiles) |
| 610 | { |
| 611 | std::ifstream logStream(file); |
| 612 | if (!logStream.is_open()) |
| 613 | { |
| 614 | continue; |
| 615 | } |
| 616 | |
| 617 | while (std::getline(logStream, entry)) |
| 618 | { |
| 619 | // Check if the record ID matches |
| 620 | if (entry.find(search) != std::string::npos) |
| 621 | { |
| 622 | return true; |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | static uint16_t |
| 630 | getNextRecordID(const uint16_t recordID, |
| 631 | const std::vector<std::filesystem::path> selLogFiles) |
| 632 | { |
| 633 | uint16_t nextRecordID = recordID + 1; |
| 634 | std::string entry; |
| 635 | if (findSELEntry(nextRecordID, selLogFiles, entry)) |
| 636 | { |
| 637 | return nextRecordID; |
| 638 | } |
| 639 | else |
| 640 | { |
| 641 | return ipmi::sel::lastEntry; |
| 642 | } |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | static int fromHexStr(const std::string hexStr, std::vector<uint8_t>& data) |
| 646 | { |
| 647 | for (unsigned int i = 0; i < hexStr.size(); i += 2) |
| 648 | { |
| 649 | try |
| 650 | { |
| 651 | data.push_back(static_cast<uint8_t>( |
| 652 | std::stoul(hexStr.substr(i, 2), nullptr, 16))); |
| 653 | } |
| 654 | catch (std::invalid_argument& e) |
| 655 | { |
| 656 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 657 | return -1; |
| 658 | } |
| 659 | catch (std::out_of_range& e) |
| 660 | { |
| 661 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 662 | return -1; |
| 663 | } |
| 664 | } |
| 665 | return 0; |
| 666 | } |
| 667 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 668 | ipmi::RspType<uint8_t, // SEL version |
| 669 | uint16_t, // SEL entry count |
| 670 | uint16_t, // free space |
| 671 | uint32_t, // last add timestamp |
| 672 | uint32_t, // last erase timestamp |
| 673 | uint8_t> // operation support |
| 674 | ipmiStorageGetSELInfo() |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 675 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 676 | constexpr uint8_t selVersion = ipmi::sel::selVersion; |
| 677 | uint16_t entries = countSELEntries(); |
| 678 | uint32_t addTimeStamp = intel_oem::ipmi::sel::getFileTimestamp( |
| 679 | intel_oem::ipmi::sel::selLogDir / intel_oem::ipmi::sel::selLogFilename); |
| 680 | uint32_t eraseTimeStamp = intel_oem::ipmi::sel::erase_time::get(); |
| 681 | constexpr uint8_t operationSupport = |
| 682 | intel_oem::ipmi::sel::selOperationSupport; |
| 683 | constexpr uint16_t freeSpace = |
| 684 | 0xffff; // Spec indicates that more than 64kB is free |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 685 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 686 | return ipmi::responseSuccess(selVersion, entries, freeSpace, addTimeStamp, |
| 687 | eraseTimeStamp, operationSupport); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 690 | using systemEventType = std::tuple< |
| 691 | uint32_t, // Timestamp |
| 692 | uint16_t, // Generator ID |
| 693 | uint8_t, // EvM Rev |
| 694 | uint8_t, // Sensor Type |
| 695 | uint8_t, // Sensor Number |
| 696 | uint7_t, // Event Type |
| 697 | bool, // Event Direction |
| 698 | std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize>>; // Event Data |
| 699 | using oemTsEventType = std::tuple< |
| 700 | uint32_t, // Timestamp |
| 701 | std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize>>; // Event Data |
| 702 | using oemEventType = |
| 703 | std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize>; // Event Data |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 704 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 705 | ipmi::RspType<uint16_t, // Next Record ID |
| 706 | uint16_t, // Record ID |
| 707 | uint8_t, // Record Type |
| 708 | std::variant<systemEventType, oemTsEventType, |
| 709 | oemEventType>> // Record Content |
| 710 | ipmiStorageGetSELEntry(uint16_t reservationID, uint16_t targetID, |
| 711 | uint8_t offset, uint8_t size) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 712 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 713 | // Only support getting the entire SEL record. If a partial size or non-zero |
| 714 | // offset is requested, return an error |
| 715 | if (offset != 0 || size != ipmi::sel::entireRecord) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 716 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 717 | return ipmi::responseRetBytesUnavailable(); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 718 | } |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 719 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 720 | // Check the reservation ID if one is provided or required (only if the |
| 721 | // offset is non-zero) |
| 722 | if (reservationID != 0 || offset != 0) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 723 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 724 | if (!checkSELReservation(reservationID)) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 725 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 726 | return ipmi::responseInvalidReservationId(); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 730 | // Get the ipmi_sel log files |
| 731 | std::vector<std::filesystem::path> selLogFiles; |
| 732 | if (!getSELLogFiles(selLogFiles)) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 733 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 734 | return ipmi::responseSensorInvalid(); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 735 | } |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 736 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 737 | std::string targetEntry; |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 738 | |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 739 | if (targetID == ipmi::sel::firstEntry) |
| 740 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 741 | // The first entry will be at the top of the oldest log file |
| 742 | std::ifstream logStream(selLogFiles.back()); |
| 743 | if (!logStream.is_open()) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 744 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 745 | return ipmi::responseUnspecifiedError(); |
| 746 | } |
| 747 | |
| 748 | if (!std::getline(logStream, targetEntry)) |
| 749 | { |
| 750 | return ipmi::responseUnspecifiedError(); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | else if (targetID == ipmi::sel::lastEntry) |
| 754 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 755 | // The last entry will be at the bottom of the newest log file |
| 756 | std::ifstream logStream(selLogFiles.front()); |
| 757 | if (!logStream.is_open()) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 758 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 759 | return ipmi::responseUnspecifiedError(); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 760 | } |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 761 | |
| 762 | std::string line; |
| 763 | while (std::getline(logStream, line)) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 764 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 765 | targetEntry = line; |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 766 | } |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 767 | } |
| 768 | else |
| 769 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 770 | if (!findSELEntry(targetID, selLogFiles, targetEntry)) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 771 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 772 | return ipmi::responseSensorInvalid(); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 773 | } |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Jason M. Bills | 52aaa7d | 2019-05-08 15:21:39 -0700 | [diff] [blame] | 776 | // The format of the ipmi_sel message is "<Timestamp> |
| 777 | // <ID>,<Type>,<EventData>,[<Generator ID>,<Path>,<Direction>]". |
| 778 | // First get the Timestamp |
| 779 | size_t space = targetEntry.find_first_of(" "); |
| 780 | if (space == std::string::npos) |
| 781 | { |
| 782 | return ipmi::responseUnspecifiedError(); |
| 783 | } |
| 784 | std::string entryTimestamp = targetEntry.substr(0, space); |
| 785 | // Then get the log contents |
| 786 | size_t entryStart = targetEntry.find_first_not_of(" ", space); |
| 787 | if (entryStart == std::string::npos) |
| 788 | { |
| 789 | return ipmi::responseUnspecifiedError(); |
| 790 | } |
| 791 | std::string_view entry(targetEntry); |
| 792 | entry.remove_prefix(entryStart); |
| 793 | // Use split to separate the entry into its fields |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 794 | std::vector<std::string> targetEntryFields; |
Jason M. Bills | 52aaa7d | 2019-05-08 15:21:39 -0700 | [diff] [blame] | 795 | boost::split(targetEntryFields, entry, boost::is_any_of(","), |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 796 | boost::token_compress_on); |
Jason M. Bills | 52aaa7d | 2019-05-08 15:21:39 -0700 | [diff] [blame] | 797 | if (targetEntryFields.size() < 3) |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 798 | { |
| 799 | return ipmi::responseUnspecifiedError(); |
| 800 | } |
Jason M. Bills | 1a2fbdd | 2019-05-10 09:05:37 -0700 | [diff] [blame] | 801 | std::string& recordIDStr = targetEntryFields[0]; |
| 802 | std::string& recordTypeStr = targetEntryFields[1]; |
| 803 | std::string& eventDataStr = targetEntryFields[2]; |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 804 | |
Jason M. Bills | 1a2fbdd | 2019-05-10 09:05:37 -0700 | [diff] [blame] | 805 | uint16_t recordID; |
| 806 | uint8_t recordType; |
| 807 | try |
| 808 | { |
| 809 | recordID = std::stoul(recordIDStr); |
| 810 | recordType = std::stoul(recordTypeStr, nullptr, 16); |
| 811 | } |
| 812 | catch (const std::invalid_argument&) |
| 813 | { |
| 814 | return ipmi::responseUnspecifiedError(); |
| 815 | } |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 816 | uint16_t nextRecordID = getNextRecordID(recordID, selLogFiles); |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 817 | std::vector<uint8_t> eventDataBytes; |
Jason M. Bills | 1a2fbdd | 2019-05-10 09:05:37 -0700 | [diff] [blame] | 818 | if (fromHexStr(eventDataStr, eventDataBytes) < 0) |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 819 | { |
| 820 | return ipmi::responseUnspecifiedError(); |
| 821 | } |
| 822 | |
| 823 | if (recordType == intel_oem::ipmi::sel::systemEvent) |
| 824 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 825 | // Get the timestamp |
| 826 | std::tm timeStruct = {}; |
Jason M. Bills | 52aaa7d | 2019-05-08 15:21:39 -0700 | [diff] [blame] | 827 | std::istringstream entryStream(entryTimestamp); |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 828 | |
| 829 | uint32_t timestamp = ipmi::sel::invalidTimeStamp; |
| 830 | if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) |
| 831 | { |
| 832 | timestamp = std::mktime(&timeStruct); |
| 833 | } |
| 834 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 835 | // Set the event message revision |
| 836 | uint8_t evmRev = intel_oem::ipmi::sel::eventMsgRev; |
| 837 | |
Jason M. Bills | 1a2fbdd | 2019-05-10 09:05:37 -0700 | [diff] [blame] | 838 | uint16_t generatorID = 0; |
| 839 | uint8_t sensorType = 0; |
| 840 | uint8_t sensorNum = 0xFF; |
| 841 | uint7_t eventType = 0; |
| 842 | bool eventDir = 0; |
| 843 | // System type events should have six fields |
| 844 | if (targetEntryFields.size() >= 6) |
| 845 | { |
| 846 | std::string& generatorIDStr = targetEntryFields[3]; |
| 847 | std::string& sensorPath = targetEntryFields[4]; |
| 848 | std::string& eventDirStr = targetEntryFields[5]; |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 849 | |
Jason M. Bills | 1a2fbdd | 2019-05-10 09:05:37 -0700 | [diff] [blame] | 850 | // Get the generator ID |
| 851 | try |
| 852 | { |
| 853 | generatorID = std::stoul(generatorIDStr, nullptr, 16); |
| 854 | } |
| 855 | catch (const std::invalid_argument&) |
| 856 | { |
| 857 | std::cerr << "Invalid Generator ID\n"; |
| 858 | } |
| 859 | |
| 860 | // Get the sensor type, sensor number, and event type for the sensor |
| 861 | sensorType = getSensorTypeFromPath(sensorPath); |
| 862 | sensorNum = getSensorNumberFromPath(sensorPath); |
| 863 | eventType = getSensorEventTypeFromPath(sensorPath); |
| 864 | |
| 865 | // Get the event direction |
| 866 | try |
| 867 | { |
| 868 | eventDir = std::stoul(eventDirStr) ? 0 : 1; |
| 869 | } |
| 870 | catch (const std::invalid_argument&) |
| 871 | { |
| 872 | std::cerr << "Invalid Event Direction\n"; |
| 873 | } |
| 874 | } |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 875 | |
| 876 | // Only keep the eventData bytes that fit in the record |
| 877 | std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize> eventData{}; |
| 878 | std::copy_n(eventDataBytes.begin(), |
| 879 | std::min(eventDataBytes.size(), eventData.size()), |
| 880 | eventData.begin()); |
| 881 | |
| 882 | return ipmi::responseSuccess( |
| 883 | nextRecordID, recordID, recordType, |
| 884 | systemEventType{timestamp, generatorID, evmRev, sensorType, |
| 885 | sensorNum, eventType, eventDir, eventData}); |
| 886 | } |
| 887 | else if (recordType >= intel_oem::ipmi::sel::oemTsEventFirst && |
| 888 | recordType <= intel_oem::ipmi::sel::oemTsEventLast) |
| 889 | { |
| 890 | // Get the timestamp |
| 891 | std::tm timeStruct = {}; |
Jason M. Bills | 52aaa7d | 2019-05-08 15:21:39 -0700 | [diff] [blame] | 892 | std::istringstream entryStream(entryTimestamp); |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 893 | |
| 894 | uint32_t timestamp = ipmi::sel::invalidTimeStamp; |
| 895 | if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) |
| 896 | { |
| 897 | timestamp = std::mktime(&timeStruct); |
| 898 | } |
| 899 | |
| 900 | // Only keep the bytes that fit in the record |
| 901 | std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize> eventData{}; |
| 902 | std::copy_n(eventDataBytes.begin(), |
| 903 | std::min(eventDataBytes.size(), eventData.size()), |
| 904 | eventData.begin()); |
| 905 | |
| 906 | return ipmi::responseSuccess(nextRecordID, recordID, recordType, |
| 907 | oemTsEventType{timestamp, eventData}); |
| 908 | } |
| 909 | else if (recordType >= intel_oem::ipmi::sel::oemEventFirst && |
| 910 | recordType <= intel_oem::ipmi::sel::oemEventLast) |
| 911 | { |
| 912 | // Only keep the bytes that fit in the record |
| 913 | std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize> eventData{}; |
| 914 | std::copy_n(eventDataBytes.begin(), |
| 915 | std::min(eventDataBytes.size(), eventData.size()), |
| 916 | eventData.begin()); |
| 917 | |
| 918 | return ipmi::responseSuccess(nextRecordID, recordID, recordType, |
| 919 | eventData); |
| 920 | } |
| 921 | |
| 922 | return ipmi::responseUnspecifiedError(); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 923 | } |
| 924 | |
Jason M. Bills | 6dd8f04 | 2019-04-11 10:39:02 -0700 | [diff] [blame] | 925 | ipmi::RspType<uint16_t> ipmiStorageAddSELEntry( |
| 926 | uint16_t recordID, uint8_t recordType, uint32_t timestamp, |
| 927 | uint16_t generatorID, uint8_t evmRev, uint8_t sensorType, uint8_t sensorNum, |
| 928 | uint8_t eventType, uint8_t eventData1, uint8_t eventData2, |
| 929 | uint8_t eventData3) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 930 | { |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 931 | // Per the IPMI spec, need to cancel any reservation when a SEL entry is |
| 932 | // added |
| 933 | cancelSELReservation(); |
| 934 | |
Jason M. Bills | 6dd8f04 | 2019-04-11 10:39:02 -0700 | [diff] [blame] | 935 | // Send this request to the Redfish hooks to log it as a Redfish message |
| 936 | // instead. There is no need to add it to the SEL, so just return success. |
| 937 | intel_oem::ipmi::sel::checkRedfishHooks( |
| 938 | recordID, recordType, timestamp, generatorID, evmRev, sensorType, |
| 939 | sensorNum, eventType, eventData1, eventData2, eventData3); |
Jason M. Bills | 99b78ec | 2019-01-18 10:42:18 -0800 | [diff] [blame] | 940 | |
Jason M. Bills | 6dd8f04 | 2019-04-11 10:39:02 -0700 | [diff] [blame] | 941 | uint16_t responseID = 0xFFFF; |
| 942 | return ipmi::responseSuccess(responseID); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 943 | } |
| 944 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 945 | ipmi::RspType<uint8_t> ipmiStorageClearSEL(ipmi::Context::ptr ctx, |
| 946 | uint16_t reservationID, |
| 947 | const std::array<uint8_t, 3>& clr, |
| 948 | uint8_t eraseOperation) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 949 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 950 | if (!checkSELReservation(reservationID)) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 951 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 952 | return ipmi::responseInvalidReservationId(); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 953 | } |
| 954 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 955 | static constexpr std::array<uint8_t, 3> clrExpected = {'C', 'L', 'R'}; |
| 956 | if (clr != clrExpected) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 957 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 958 | return ipmi::responseInvalidFieldRequest(); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 959 | } |
| 960 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 961 | // Erasure status cannot be fetched, so always return erasure status as |
| 962 | // `erase completed`. |
| 963 | if (eraseOperation == ipmi::sel::getEraseStatus) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 964 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 965 | return ipmi::responseSuccess(ipmi::sel::eraseComplete); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 966 | } |
| 967 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 968 | // Check that initiate erase is correct |
| 969 | if (eraseOperation != ipmi::sel::initiateErase) |
| 970 | { |
| 971 | return ipmi::responseInvalidFieldRequest(); |
| 972 | } |
| 973 | |
| 974 | // Per the IPMI spec, need to cancel any reservation when the SEL is |
| 975 | // cleared |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 976 | cancelSELReservation(); |
| 977 | |
Jason M. Bills | 7944c30 | 2019-03-20 15:24:05 -0700 | [diff] [blame] | 978 | // Save the erase time |
| 979 | intel_oem::ipmi::sel::erase_time::save(); |
| 980 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 981 | // Clear the SEL by deleting the log files |
| 982 | std::vector<std::filesystem::path> selLogFiles; |
| 983 | if (getSELLogFiles(selLogFiles)) |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 984 | { |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 985 | for (const std::filesystem::path& file : selLogFiles) |
| 986 | { |
| 987 | std::error_code ec; |
| 988 | std::filesystem::remove(file, ec); |
| 989 | } |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 990 | } |
| 991 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 992 | // Reload rsyslog so it knows to start new log files |
| 993 | sdbusplus::message::message rsyslogReload = dbus.new_method_call( |
| 994 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 995 | "org.freedesktop.systemd1.Manager", "ReloadUnit"); |
| 996 | rsyslogReload.append("rsyslog.service", "replace"); |
| 997 | try |
| 998 | { |
| 999 | sdbusplus::message::message reloadResponse = dbus.call(rsyslogReload); |
| 1000 | } |
| 1001 | catch (sdbusplus::exception_t& e) |
| 1002 | { |
| 1003 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 1004 | } |
| 1005 | |
| 1006 | return ipmi::responseSuccess(ipmi::sel::eraseComplete); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
Jason M. Bills | 1a47462 | 2019-06-14 14:51:33 -0700 | [diff] [blame] | 1009 | ipmi::RspType<uint32_t> ipmiStorageGetSELTime() |
| 1010 | { |
| 1011 | struct timespec selTime = {}; |
| 1012 | |
| 1013 | if (clock_gettime(CLOCK_REALTIME, &selTime) < 0) |
| 1014 | { |
| 1015 | return ipmi::responseUnspecifiedError(); |
| 1016 | } |
| 1017 | |
| 1018 | return ipmi::responseSuccess(selTime.tv_sec); |
| 1019 | } |
| 1020 | |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 1021 | ipmi::RspType<> ipmiStorageSetSELTime(uint32_t selTime) |
Jason M. Bills | cac97a5 | 2019-01-30 14:43:46 -0800 | [diff] [blame] | 1022 | { |
| 1023 | // Set SEL Time is not supported |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 1024 | return ipmi::responseInvalidCommand(); |
Jason M. Bills | cac97a5 | 2019-01-30 14:43:46 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
Jason M. Bills | e2d1aee | 2018-10-03 15:57:18 -0700 | [diff] [blame] | 1027 | void registerStorageFunctions() |
| 1028 | { |
| 1029 | // <Get FRU Inventory Area Info> |
| 1030 | ipmiPrintAndRegister( |
| 1031 | NETFUN_STORAGE, |
| 1032 | static_cast<ipmi_cmd_t>(IPMINetfnStorageCmds::ipmiCmdGetFRUInvAreaInfo), |
| 1033 | NULL, ipmiStorageGetFRUInvAreaInfo, PRIVILEGE_OPERATOR); |
| 1034 | |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 1035 | // <READ FRU Data> |
Jason M. Bills | e2d1aee | 2018-10-03 15:57:18 -0700 | [diff] [blame] | 1036 | ipmiPrintAndRegister( |
| 1037 | NETFUN_STORAGE, |
| 1038 | static_cast<ipmi_cmd_t>(IPMINetfnStorageCmds::ipmiCmdReadFRUData), NULL, |
| 1039 | ipmiStorageReadFRUData, PRIVILEGE_OPERATOR); |
| 1040 | |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 1041 | // <WRITE FRU Data> |
Jason M. Bills | e2d1aee | 2018-10-03 15:57:18 -0700 | [diff] [blame] | 1042 | ipmiPrintAndRegister( |
| 1043 | NETFUN_STORAGE, |
| 1044 | static_cast<ipmi_cmd_t>(IPMINetfnStorageCmds::ipmiCmdWriteFRUData), |
| 1045 | NULL, ipmiStorageWriteFRUData, PRIVILEGE_OPERATOR); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 1046 | |
| 1047 | // <Get SEL Info> |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 1048 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage, |
| 1049 | ipmi::storage::cmdGetSelInfo, |
| 1050 | ipmi::Privilege::Operator, ipmiStorageGetSELInfo); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 1051 | |
| 1052 | // <Get SEL Entry> |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 1053 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage, |
| 1054 | ipmi::storage::cmdGetSelEntry, |
| 1055 | ipmi::Privilege::Operator, ipmiStorageGetSELEntry); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 1056 | |
| 1057 | // <Add SEL Entry> |
Jason M. Bills | 6dd8f04 | 2019-04-11 10:39:02 -0700 | [diff] [blame] | 1058 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage, |
| 1059 | static_cast<ipmi::Cmd>(ipmi::storage::cmdAddSelEntry), |
| 1060 | ipmi::Privilege::Operator, ipmiStorageAddSELEntry); |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 1061 | |
| 1062 | // <Clear SEL> |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 1063 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage, |
| 1064 | ipmi::storage::cmdClearSel, ipmi::Privilege::Operator, |
| 1065 | ipmiStorageClearSEL); |
Jason M. Bills | cac97a5 | 2019-01-30 14:43:46 -0800 | [diff] [blame] | 1066 | |
Jason M. Bills | 1a47462 | 2019-06-14 14:51:33 -0700 | [diff] [blame] | 1067 | // <Get SEL Time> |
| 1068 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage, |
| 1069 | ipmi::storage::cmdGetSelTime, |
| 1070 | ipmi::Privilege::Operator, ipmiStorageGetSELTime); |
| 1071 | |
Jason M. Bills | cac97a5 | 2019-01-30 14:43:46 -0800 | [diff] [blame] | 1072 | // <Set SEL Time> |
Jason M. Bills | 1d4d54d | 2019-04-23 11:26:11 -0700 | [diff] [blame] | 1073 | ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage, |
| 1074 | ipmi::storage::cmdSetSelTime, |
| 1075 | ipmi::Privilege::Operator, ipmiStorageSetSELTime); |
Jason M. Bills | e2d1aee | 2018-10-03 15:57:18 -0700 | [diff] [blame] | 1076 | } |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 1077 | } // namespace storage |
Jason M. Bills | c04e2e7 | 2018-11-28 15:15:56 -0800 | [diff] [blame] | 1078 | } // namespace ipmi |