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