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