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