Marri Devender Rao | cac383b | 2017-07-03 13:24:27 -0500 | [diff] [blame] | 1 | #include <algorithm> |
Lei YU | 52d9124 | 2017-10-17 22:52:28 +0800 | [diff] [blame] | 2 | #include <arpa/inet.h> |
| 3 | #include <chrono> |
| 4 | #include <cstdio> |
Tom Joseph | a495339 | 2017-06-30 19:09:47 +0530 | [diff] [blame] | 5 | #include <experimental/filesystem> |
Lei YU | 52d9124 | 2017-10-17 22:52:28 +0800 | [diff] [blame] | 6 | #include <mapper.h> |
| 7 | #include <string> |
| 8 | #include <systemd/sd-bus.h> |
| 9 | |
Tom Joseph | 6f7deaa | 2017-06-30 19:03:54 +0530 | [diff] [blame] | 10 | #include <phosphor-logging/log.hpp> |
Marri Devender Rao | cac383b | 2017-07-03 13:24:27 -0500 | [diff] [blame] | 11 | #include <phosphor-logging/elog-errors.hpp> |
Lei YU | 52d9124 | 2017-10-17 22:52:28 +0800 | [diff] [blame] | 12 | #include <sdbusplus/server.hpp> |
| 13 | |
| 14 | #include "host-ipmid/ipmid-api.h" |
| 15 | #include "read_fru_data.hpp" |
| 16 | #include "selutility.hpp" |
| 17 | #include "storageaddsel.h" |
| 18 | #include "storagehandler.h" |
| 19 | #include "utils.hpp" |
| 20 | #include "xyz/openbmc_project/Common/error.hpp" |
| 21 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 22 | |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 23 | void register_netfn_storage_functions() __attribute__((constructor)); |
| 24 | |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 25 | unsigned int g_sel_time = 0xFFFFFFFF; |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 26 | extern unsigned short g_sel_reserve; |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 27 | |
Lei YU | e893939 | 2017-06-15 10:45:05 +0800 | [diff] [blame] | 28 | namespace { |
| 29 | constexpr auto TIME_INTERFACE = "xyz.openbmc_project.Time.EpochTime"; |
| 30 | constexpr auto HOST_TIME_PATH = "/xyz/openbmc_project/time/host"; |
| 31 | constexpr auto DBUS_PROPERTIES = "org.freedesktop.DBus.Properties"; |
| 32 | constexpr auto PROPERTY_ELAPSED= "Elapsed"; |
| 33 | |
| 34 | const char* getTimeString(const uint64_t& usecSinceEpoch) |
| 35 | { |
| 36 | using namespace std::chrono; |
| 37 | system_clock::time_point tp{microseconds(usecSinceEpoch)}; |
| 38 | auto t = system_clock::to_time_t(tp); |
| 39 | return std::ctime(&t); |
| 40 | } |
| 41 | } |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 42 | |
Tom Joseph | 6f7deaa | 2017-06-30 19:03:54 +0530 | [diff] [blame] | 43 | namespace cache |
| 44 | { |
| 45 | /* |
| 46 | * This cache contains the object paths of the logging entries sorted in the |
| 47 | * order of the filename(numeric order). The cache is initialized by |
| 48 | * invoking readLoggingObjectPaths with the cache as the parameter. The |
| 49 | * cache is invoked in the execution of the Get SEL info and Delete SEL |
| 50 | * entry command. The Get SEL Info command is typically invoked before the |
| 51 | * Get SEL entry command, so the cache is utilized for responding to Get SEL |
| 52 | * entry command. The cache is invalidated by clearing after Delete SEL |
| 53 | * entry and Clear SEL command. |
| 54 | */ |
| 55 | ipmi::sel::ObjectPaths paths; |
| 56 | |
| 57 | } // namespace objectPathsCache |
| 58 | |
| 59 | using InternalFailure = |
| 60 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 61 | using namespace phosphor::logging; |
Marri Devender Rao | cac383b | 2017-07-03 13:24:27 -0500 | [diff] [blame] | 62 | using namespace ipmi::fru; |
| 63 | |
| 64 | /** |
| 65 | * @enum Device access mode |
| 66 | */ |
| 67 | enum class AccessMode |
| 68 | { |
| 69 | bytes, ///< Device is accessed by bytes |
| 70 | words ///< Device is accessed by words |
| 71 | }; |
| 72 | |
Tom Joseph | 6f7deaa | 2017-06-30 19:03:54 +0530 | [diff] [blame] | 73 | |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 74 | ipmi_ret_t ipmi_storage_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 75 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 76 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 77 | { |
| 78 | printf("Handling STORAGE WILDCARD Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd); |
| 79 | // Status code. |
Nan Li | 70aa8d9 | 2016-08-29 00:11:10 +0800 | [diff] [blame] | 80 | ipmi_ret_t rc = IPMI_CC_INVALID; |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 81 | *data_len = 0; |
| 82 | return rc; |
| 83 | } |
| 84 | |
Tom Joseph | 6f7deaa | 2017-06-30 19:03:54 +0530 | [diff] [blame] | 85 | ipmi_ret_t getSELInfo(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 86 | ipmi_request_t request, ipmi_response_t response, |
| 87 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 88 | { |
| 89 | std::vector<uint8_t> outPayload(sizeof(ipmi::sel::GetSELInfoResponse)); |
| 90 | auto responseData = reinterpret_cast<ipmi::sel::GetSELInfoResponse*> |
| 91 | (outPayload.data()); |
| 92 | |
| 93 | responseData->selVersion = ipmi::sel::selVersion; |
| 94 | // Last erase timestamp is not available from log manager. |
| 95 | responseData->eraseTimeStamp = ipmi::sel::invalidTimeStamp; |
| 96 | responseData->operationSupport = ipmi::sel::operationSupport; |
| 97 | |
| 98 | ipmi::sel::readLoggingObjectPaths(cache::paths); |
| 99 | responseData->entries = 0; |
| 100 | responseData->addTimeStamp = ipmi::sel::invalidTimeStamp; |
| 101 | |
| 102 | if (!cache::paths.empty()) |
| 103 | { |
| 104 | responseData->entries = static_cast<uint16_t>(cache::paths.size()); |
| 105 | |
| 106 | try |
| 107 | { |
| 108 | responseData->addTimeStamp = static_cast<uint32_t>( |
| 109 | (ipmi::sel::getEntryTimeStamp(cache::paths.back()) |
| 110 | .count())); |
| 111 | } |
| 112 | catch (InternalFailure& e) |
| 113 | { |
| 114 | } |
| 115 | catch (const std::runtime_error& e) |
| 116 | { |
| 117 | log<level::ERR>(e.what()); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | memcpy(response, outPayload.data(), outPayload.size()); |
| 122 | *data_len = outPayload.size(); |
| 123 | |
| 124 | return IPMI_CC_OK; |
| 125 | } |
| 126 | |
Tom Joseph | a495339 | 2017-06-30 19:09:47 +0530 | [diff] [blame] | 127 | ipmi_ret_t getSELEntry(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 128 | ipmi_request_t request, ipmi_response_t response, |
| 129 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 130 | { |
| 131 | auto requestData = reinterpret_cast<const ipmi::sel::GetSELEntryRequest*> |
| 132 | (request); |
| 133 | |
| 134 | if (requestData->reservationID != 0) |
| 135 | { |
| 136 | if (g_sel_reserve != requestData->reservationID) |
| 137 | { |
| 138 | *data_len = 0; |
| 139 | return IPMI_CC_INVALID_RESERVATION_ID; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (cache::paths.empty()) |
| 144 | { |
| 145 | *data_len = 0; |
| 146 | return IPMI_CC_SENSOR_INVALID; |
| 147 | } |
| 148 | |
| 149 | ipmi::sel::ObjectPaths::const_iterator iter; |
| 150 | |
| 151 | // Check for the requested SEL Entry. |
| 152 | if (requestData->selRecordID == ipmi::sel::firstEntry) |
| 153 | { |
| 154 | iter = cache::paths.begin(); |
| 155 | } |
| 156 | else if (requestData->selRecordID == ipmi::sel::lastEntry) |
| 157 | { |
| 158 | iter = cache::paths.end(); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | std::string objPath = std::string(ipmi::sel::logBasePath) + "/" + |
| 163 | std::to_string(requestData->selRecordID); |
| 164 | |
| 165 | iter = std::find(cache::paths.begin(), cache::paths.end(), objPath); |
| 166 | if (iter == cache::paths.end()) |
| 167 | { |
| 168 | *data_len = 0; |
| 169 | return IPMI_CC_SENSOR_INVALID; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | ipmi::sel::GetSELEntryResponse record {}; |
| 174 | |
| 175 | // Convert the log entry into SEL record. |
| 176 | try |
| 177 | { |
| 178 | record = ipmi::sel::convertLogEntrytoSEL(*iter); |
| 179 | } |
| 180 | catch (InternalFailure& e) |
| 181 | { |
| 182 | *data_len = 0; |
| 183 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 184 | } |
| 185 | catch (const std::runtime_error& e) |
| 186 | { |
| 187 | log<level::ERR>(e.what()); |
| 188 | *data_len = 0; |
| 189 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | // Identify the next SEL record ID |
| 194 | if(iter != cache::paths.end()) |
| 195 | { |
| 196 | ++iter; |
| 197 | if (iter == cache::paths.end()) |
| 198 | { |
| 199 | record.nextRecordID = ipmi::sel::lastEntry; |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | namespace fs = std::experimental::filesystem; |
| 204 | fs::path path(*iter); |
| 205 | record.nextRecordID = static_cast<uint16_t> |
| 206 | (std::stoul(std::string(path.filename().c_str()))); |
| 207 | } |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | record.nextRecordID = ipmi::sel::lastEntry; |
| 212 | } |
| 213 | |
| 214 | if (requestData->readLength == ipmi::sel::entireRecord) |
| 215 | { |
| 216 | memcpy(response, &record, sizeof(record)); |
| 217 | *data_len = sizeof(record); |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | if (requestData->offset >= ipmi::sel::selRecordSize || |
| 222 | requestData->readLength > ipmi::sel::selRecordSize) |
| 223 | { |
| 224 | *data_len = 0; |
| 225 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 226 | } |
| 227 | |
| 228 | auto diff = ipmi::sel::selRecordSize - requestData->offset; |
| 229 | auto readLength = std::min(diff, |
| 230 | static_cast<int>(requestData->readLength)); |
| 231 | |
| 232 | memcpy(response, &record.nextRecordID, sizeof(record.nextRecordID)); |
| 233 | memcpy(static_cast<uint8_t*>(response) + sizeof(record.nextRecordID), |
| 234 | &record.recordID + requestData->offset, readLength); |
| 235 | *data_len = sizeof(record.nextRecordID) + readLength; |
| 236 | } |
| 237 | |
| 238 | return IPMI_CC_OK; |
| 239 | } |
| 240 | |
Tom Joseph | 8f4a2aa | 2017-06-30 19:12:49 +0530 | [diff] [blame] | 241 | ipmi_ret_t deleteSELEntry(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 242 | ipmi_request_t request, ipmi_response_t response, |
| 243 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 244 | { |
| 245 | namespace fs = std::experimental::filesystem; |
| 246 | auto requestData = reinterpret_cast<const ipmi::sel::DeleteSELEntryRequest*> |
| 247 | (request); |
| 248 | |
| 249 | if (g_sel_reserve != requestData->reservationID) |
| 250 | { |
| 251 | *data_len = 0; |
| 252 | return IPMI_CC_INVALID_RESERVATION_ID; |
| 253 | } |
| 254 | |
| 255 | ipmi::sel::readLoggingObjectPaths(cache::paths); |
| 256 | |
| 257 | if (cache::paths.empty()) |
| 258 | { |
| 259 | *data_len = 0; |
| 260 | return IPMI_CC_SENSOR_INVALID; |
| 261 | } |
| 262 | |
| 263 | ipmi::sel::ObjectPaths::const_iterator iter; |
| 264 | uint16_t delRecordID = 0; |
| 265 | |
| 266 | if (requestData->selRecordID == ipmi::sel::firstEntry) |
| 267 | { |
| 268 | iter = cache::paths.begin(); |
| 269 | fs::path path(*iter); |
| 270 | delRecordID = static_cast<uint16_t> |
| 271 | (std::stoul(std::string(path.filename().c_str()))); |
| 272 | } |
| 273 | else if (requestData->selRecordID == ipmi::sel::lastEntry) |
| 274 | { |
| 275 | iter = cache::paths.end(); |
| 276 | fs::path path(*iter); |
| 277 | delRecordID = static_cast<uint16_t> |
| 278 | (std::stoul(std::string(path.filename().c_str()))); |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | std::string objPath = std::string(ipmi::sel::logBasePath) + "/" + |
| 283 | std::to_string(requestData->selRecordID); |
| 284 | |
| 285 | iter = std::find(cache::paths.begin(), cache::paths.end(), objPath); |
| 286 | if (iter == cache::paths.end()) |
| 287 | { |
| 288 | *data_len = 0; |
| 289 | return IPMI_CC_SENSOR_INVALID; |
| 290 | } |
| 291 | delRecordID = requestData->selRecordID; |
| 292 | } |
| 293 | |
| 294 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 295 | std::string service; |
| 296 | |
| 297 | try |
| 298 | { |
| 299 | service = ipmi::getService(bus, ipmi::sel::logDeleteIntf, *iter); |
| 300 | } |
| 301 | catch (const std::runtime_error& e) |
| 302 | { |
| 303 | log<level::ERR>(e.what()); |
| 304 | *data_len = 0; |
| 305 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 306 | } |
| 307 | |
| 308 | auto methodCall = bus.new_method_call(service.c_str(), |
| 309 | (*iter).c_str(), |
| 310 | ipmi::sel::logDeleteIntf, |
| 311 | "Delete"); |
| 312 | auto reply = bus.call(methodCall); |
| 313 | if (reply.is_method_error()) |
| 314 | { |
| 315 | *data_len = 0; |
| 316 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 317 | } |
| 318 | |
| 319 | // Invalidate the cache of dbus entry objects. |
| 320 | cache::paths.clear(); |
| 321 | memcpy(response, &delRecordID, sizeof(delRecordID)); |
| 322 | *data_len = sizeof(delRecordID); |
| 323 | |
| 324 | return IPMI_CC_OK; |
| 325 | } |
| 326 | |
Tom Joseph | 2f05bb5 | 2017-06-30 19:14:49 +0530 | [diff] [blame] | 327 | ipmi_ret_t clearSEL(ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request, |
| 328 | ipmi_response_t response, ipmi_data_len_t data_len, |
| 329 | ipmi_context_t context) |
| 330 | { |
| 331 | auto requestData = reinterpret_cast<const ipmi::sel::ClearSELRequest*> |
| 332 | (request); |
| 333 | |
| 334 | if (g_sel_reserve != requestData->reservationID) |
| 335 | { |
| 336 | *data_len = 0; |
| 337 | return IPMI_CC_INVALID_RESERVATION_ID; |
| 338 | } |
| 339 | |
| 340 | if (requestData->charC != 'C' || |
| 341 | requestData->charL != 'L' || |
| 342 | requestData->charR != 'R') |
| 343 | { |
| 344 | *data_len = 0; |
| 345 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 346 | } |
| 347 | |
| 348 | uint8_t eraseProgress = ipmi::sel::eraseComplete; |
| 349 | |
| 350 | /* |
| 351 | * Erasure status cannot be fetched from DBUS, so always return erasure |
| 352 | * status as `erase completed`. |
| 353 | */ |
| 354 | if (requestData->eraseOperation == ipmi::sel::getEraseStatus) |
| 355 | { |
| 356 | memcpy(response, &eraseProgress, sizeof(eraseProgress)); |
| 357 | *data_len = sizeof(eraseProgress); |
| 358 | return IPMI_CC_OK; |
| 359 | } |
| 360 | |
| 361 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 362 | auto depth = 0; |
| 363 | |
| 364 | auto mapperCall = bus.new_method_call(ipmi::sel::mapperBusName, |
| 365 | ipmi::sel::mapperObjPath, |
| 366 | ipmi::sel::mapperIntf, |
| 367 | "GetSubTreePaths"); |
| 368 | mapperCall.append(ipmi::sel::logBasePath); |
| 369 | mapperCall.append(depth); |
| 370 | mapperCall.append(ipmi::sel::ObjectPaths({ipmi::sel::logEntryIntf})); |
| 371 | |
| 372 | auto reply = bus.call(mapperCall); |
| 373 | if (reply.is_method_error()) |
| 374 | { |
| 375 | memcpy(response, &eraseProgress, sizeof(eraseProgress)); |
| 376 | *data_len = sizeof(eraseProgress); |
| 377 | return IPMI_CC_OK; |
| 378 | } |
| 379 | |
| 380 | ipmi::sel::ObjectPaths objectPaths; |
| 381 | reply.read(objectPaths); |
| 382 | if (objectPaths.empty()) |
| 383 | { |
| 384 | memcpy(response, &eraseProgress, sizeof(eraseProgress)); |
| 385 | *data_len = sizeof(eraseProgress); |
| 386 | return IPMI_CC_OK; |
| 387 | } |
| 388 | |
| 389 | std::string service; |
| 390 | |
| 391 | try |
| 392 | { |
| 393 | service = ipmi::getService(bus, |
| 394 | ipmi::sel::logDeleteIntf, |
| 395 | objectPaths.front()); |
| 396 | } |
| 397 | catch (const std::runtime_error& e) |
| 398 | { |
| 399 | log<level::ERR>(e.what()); |
| 400 | *data_len = 0; |
| 401 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 402 | } |
| 403 | |
| 404 | for (const auto& iter : objectPaths) |
| 405 | { |
| 406 | auto methodCall = bus.new_method_call(service.c_str(), |
| 407 | iter.c_str(), |
| 408 | ipmi::sel::logDeleteIntf, |
| 409 | "Delete"); |
| 410 | |
| 411 | auto reply = bus.call(methodCall); |
| 412 | if (reply.is_method_error()) |
| 413 | { |
| 414 | *data_len = 0; |
| 415 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // Invalidate the cache of dbus entry objects. |
| 420 | cache::paths.clear(); |
| 421 | memcpy(response, &eraseProgress, sizeof(eraseProgress)); |
| 422 | *data_len = sizeof(eraseProgress); |
| 423 | return IPMI_CC_OK; |
| 424 | } |
| 425 | |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 426 | ipmi_ret_t ipmi_storage_get_sel_time(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 427 | ipmi_request_t request, ipmi_response_t response, |
| 428 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 429 | { |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 430 | using namespace std::chrono; |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 431 | uint64_t host_time_usec = 0; |
| 432 | uint32_t resp = 0; |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 433 | |
| 434 | printf("IPMI Handling GET-SEL-TIME\n"); |
| 435 | |
Lei YU | e893939 | 2017-06-15 10:45:05 +0800 | [diff] [blame] | 436 | try |
| 437 | { |
| 438 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 439 | auto service = ipmi::getService(bus, TIME_INTERFACE, HOST_TIME_PATH); |
| 440 | sdbusplus::message::variant<uint64_t> value; |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 441 | |
Lei YU | e893939 | 2017-06-15 10:45:05 +0800 | [diff] [blame] | 442 | // Get host time |
| 443 | auto method = bus.new_method_call(service.c_str(), |
| 444 | HOST_TIME_PATH, |
| 445 | DBUS_PROPERTIES, |
| 446 | "Get"); |
| 447 | |
| 448 | method.append(TIME_INTERFACE, PROPERTY_ELAPSED); |
| 449 | auto reply = bus.call(method); |
| 450 | if (reply.is_method_error()) |
| 451 | { |
| 452 | log<level::ERR>("Error getting time", |
| 453 | entry("SERVICE=%s", service.c_str()), |
| 454 | entry("PATH=%s", HOST_TIME_PATH)); |
| 455 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 456 | } |
| 457 | reply.read(value); |
| 458 | host_time_usec = value.get<uint64_t>(); |
| 459 | } |
| 460 | catch (InternalFailure& e) |
| 461 | { |
| 462 | log<level::ERR>(e.what()); |
| 463 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 464 | } |
| 465 | catch (const std::runtime_error& e) |
| 466 | { |
| 467 | log<level::ERR>(e.what()); |
| 468 | return IPMI_CC_UNSPECIFIED_ERROR; |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 469 | } |
| 470 | |
Lei YU | e893939 | 2017-06-15 10:45:05 +0800 | [diff] [blame] | 471 | printf("Host time: %" PRIu64 ", %s", |
| 472 | host_time_usec, getTimeString(host_time_usec)); |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 473 | |
| 474 | // Time is really long int but IPMI wants just uint32. This works okay until |
| 475 | // the number of seconds since 1970 overflows uint32 size.. Still a whole |
| 476 | // lot of time here to even think about that. |
| 477 | resp = duration_cast<seconds>(microseconds(host_time_usec)).count(); |
| 478 | resp = htole32(resp); |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 479 | |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 480 | // From the IPMI Spec 2.0, response should be a 32-bit value |
| 481 | *data_len = sizeof(resp); |
| 482 | |
| 483 | // Pack the actual response |
| 484 | memcpy(response, &resp, *data_len); |
| 485 | |
Lei YU | e893939 | 2017-06-15 10:45:05 +0800 | [diff] [blame] | 486 | return IPMI_CC_OK; |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | ipmi_ret_t ipmi_storage_set_sel_time(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 490 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 491 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 492 | { |
Lei YU | e893939 | 2017-06-15 10:45:05 +0800 | [diff] [blame] | 493 | using namespace std::chrono; |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 494 | ipmi_ret_t rc = IPMI_CC_OK; |
Lei YU | e893939 | 2017-06-15 10:45:05 +0800 | [diff] [blame] | 495 | uint32_t secs = *static_cast<uint32_t*>(request); |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 496 | *data_len = 0; |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 497 | |
Lei YU | e893939 | 2017-06-15 10:45:05 +0800 | [diff] [blame] | 498 | printf("Handling Set-SEL-Time:[0x%X], Cmd:[0x%X], Data:[0x%X]\n", |
| 499 | netfn, cmd, secs); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 500 | |
Lei YU | e893939 | 2017-06-15 10:45:05 +0800 | [diff] [blame] | 501 | secs = le32toh(secs); |
| 502 | microseconds usec{seconds(secs)}; |
Norman James | 8233044 | 2015-11-19 16:53:26 -0600 | [diff] [blame] | 503 | |
Lei YU | e893939 | 2017-06-15 10:45:05 +0800 | [diff] [blame] | 504 | printf("To Set host time: %" PRIu64 ", %s", |
| 505 | usec.count(), getTimeString(usec.count())); |
| 506 | |
| 507 | try |
| 508 | { |
| 509 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 510 | auto service = ipmi::getService(bus, TIME_INTERFACE, HOST_TIME_PATH); |
| 511 | sdbusplus::message::variant<uint64_t> value{usec.count()}; |
| 512 | |
| 513 | // Set host time |
| 514 | auto method = bus.new_method_call(service.c_str(), |
| 515 | HOST_TIME_PATH, |
| 516 | DBUS_PROPERTIES, |
| 517 | "Set"); |
| 518 | |
| 519 | method.append(TIME_INTERFACE, PROPERTY_ELAPSED, value); |
| 520 | auto reply = bus.call(method); |
| 521 | if (reply.is_method_error()) |
| 522 | { |
| 523 | log<level::ERR>("Error setting time", |
| 524 | entry("SERVICE=%s", service.c_str()), |
| 525 | entry("PATH=%s", HOST_TIME_PATH)); |
| 526 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 527 | } |
Norman James | 8233044 | 2015-11-19 16:53:26 -0600 | [diff] [blame] | 528 | } |
Lei YU | e893939 | 2017-06-15 10:45:05 +0800 | [diff] [blame] | 529 | catch (InternalFailure& e) |
| 530 | { |
| 531 | log<level::ERR>(e.what()); |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 532 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 533 | } |
Lei YU | e893939 | 2017-06-15 10:45:05 +0800 | [diff] [blame] | 534 | catch (const std::runtime_error& e) |
| 535 | { |
| 536 | log<level::ERR>(e.what()); |
Norman James | 8233044 | 2015-11-19 16:53:26 -0600 | [diff] [blame] | 537 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 538 | } |
Vishwanatha Subbanna | 5fba7a6 | 2016-09-01 14:06:07 +0530 | [diff] [blame] | 539 | |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 540 | return rc; |
| 541 | } |
| 542 | |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 543 | ipmi_ret_t ipmi_storage_reserve_sel(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 544 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 545 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 546 | { |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 547 | ipmi_ret_t rc = IPMI_CC_OK; |
| 548 | |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 549 | // IPMI spec, Reservation ID, the value simply increases against each execution of reserve_sel command. |
| 550 | if( ++g_sel_reserve == 0) |
| 551 | g_sel_reserve = 1; |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 552 | |
Nan Li | 36c0cb6 | 2016-03-31 11:16:08 +0800 | [diff] [blame] | 553 | printf("IPMI Handling RESERVE-SEL 0x%04x\n", g_sel_reserve); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 554 | |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 555 | *data_len = sizeof(g_sel_reserve); |
| 556 | |
| 557 | // Pack the actual response |
| 558 | memcpy(response, &g_sel_reserve, *data_len); |
| 559 | |
| 560 | return rc; |
| 561 | } |
| 562 | |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 563 | ipmi_ret_t ipmi_storage_add_sel(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 564 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 565 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 566 | { |
| 567 | |
| 568 | ipmi_ret_t rc = IPMI_CC_OK; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 569 | ipmi_add_sel_request_t *p = (ipmi_add_sel_request_t*) request; |
| 570 | uint16_t recordid; |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 571 | |
Chris Austen | 313d95b | 2015-10-31 12:55:30 -0500 | [diff] [blame] | 572 | recordid = ((uint16_t)p->eventdata[1] << 8) | p->eventdata[2]; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 573 | |
| 574 | printf("IPMI Handling ADD-SEL for record 0x%04x\n", recordid); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 575 | |
| 576 | *data_len = sizeof(g_sel_reserve); |
| 577 | |
| 578 | // Pack the actual response |
Chris Austen | 7cc3332 | 2015-11-11 00:20:22 -0600 | [diff] [blame] | 579 | memcpy(response, &p->eventdata[1], 2); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 580 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 581 | send_esel(recordid); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 582 | |
| 583 | return rc; |
| 584 | } |
| 585 | |
Marri Devender Rao | fa7b4e2 | 2017-07-03 00:52:20 -0500 | [diff] [blame] | 586 | //Read FRU info area |
| 587 | ipmi_ret_t ipmi_storage_get_fru_inv_area_info( |
| 588 | ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request, |
| 589 | ipmi_response_t response, ipmi_data_len_t data_len, |
| 590 | ipmi_context_t context) |
| 591 | { |
| 592 | ipmi_ret_t rc = IPMI_CC_OK; |
Marri Devender Rao | cac383b | 2017-07-03 13:24:27 -0500 | [diff] [blame] | 593 | const FruInvenAreaInfoRequest* reqptr = |
| 594 | reinterpret_cast<const FruInvenAreaInfoRequest*>(request); |
| 595 | try |
| 596 | { |
| 597 | const auto& fruArea = getFruAreaData(reqptr->fruID); |
| 598 | auto size = static_cast<uint16_t>(fruArea.size()); |
| 599 | FruInvenAreaInfoResponse resp; |
| 600 | resp.sizems = size >> 8; |
| 601 | resp.sizels = size; |
| 602 | resp.access = static_cast<uint8_t>(AccessMode::bytes); |
| 603 | |
| 604 | *data_len = sizeof(resp); |
| 605 | |
| 606 | // Pack the actual response |
| 607 | memcpy(response, &resp, *data_len); |
| 608 | } |
| 609 | catch(const InternalFailure& e) |
| 610 | { |
| 611 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 612 | *data_len = 0; |
| 613 | log<level::ERR>(e.what()); |
| 614 | report<InternalFailure>(); |
| 615 | } |
Marri Devender Rao | fa7b4e2 | 2017-07-03 00:52:20 -0500 | [diff] [blame] | 616 | return rc; |
| 617 | } |
| 618 | |
| 619 | //Read FRU data |
| 620 | ipmi_ret_t ipmi_storage_read_fru_data( |
| 621 | ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_request_t request, |
| 622 | ipmi_response_t response, ipmi_data_len_t data_len, |
| 623 | ipmi_context_t context) |
| 624 | { |
| 625 | ipmi_ret_t rc = IPMI_CC_OK; |
Marri Devender Rao | cac383b | 2017-07-03 13:24:27 -0500 | [diff] [blame] | 626 | const ReadFruDataRequest* reqptr = |
| 627 | reinterpret_cast<const ReadFruDataRequest*>(request); |
| 628 | auto offset = |
| 629 | static_cast<uint16_t>(reqptr->offsetMS << 8 | reqptr->offsetLS); |
| 630 | try |
| 631 | { |
| 632 | const auto& fruArea = getFruAreaData(reqptr->fruID); |
| 633 | auto size = fruArea.size(); |
| 634 | if ((offset + reqptr->count) > size) |
| 635 | { |
| 636 | log<level::ERR>("Invalid offset and count", |
Gunnar Mills | e4050f2 | 2017-10-19 16:36:45 -0500 | [diff] [blame^] | 637 | entry("OFFSET=%s", offset), |
| 638 | entry("COUNT=%s", reqptr->count), |
| 639 | entry("SIZE_OF_FRU_AREA=%s", size)); |
Marri Devender Rao | cac383b | 2017-07-03 13:24:27 -0500 | [diff] [blame] | 640 | return IPMI_CC_INVALID; |
| 641 | } |
| 642 | std::copy((fruArea.begin() + offset), (fruArea.begin() + reqptr->count), |
| 643 | (static_cast<uint8_t*>(response))); |
| 644 | *data_len = reqptr->count; |
| 645 | } |
| 646 | catch (const InternalFailure& e) |
| 647 | { |
| 648 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 649 | *data_len = 0; |
| 650 | log<level::ERR>(e.what()); |
| 651 | report<InternalFailure>(); |
| 652 | } |
Marri Devender Rao | fa7b4e2 | 2017-07-03 00:52:20 -0500 | [diff] [blame] | 653 | return rc; |
| 654 | } |
| 655 | |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 656 | |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 657 | void register_netfn_storage_functions() |
| 658 | { |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 659 | // <Wildcard Command> |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 660 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_WILDCARD); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 661 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WILDCARD, NULL, ipmi_storage_wildcard, |
| 662 | PRIVILEGE_USER); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 663 | |
Tom Joseph | 6f7deaa | 2017-06-30 19:03:54 +0530 | [diff] [blame] | 664 | // <Get SEL Info> |
| 665 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_GET_SEL_INFO); |
| 666 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_INFO, NULL, getSELInfo, |
| 667 | PRIVILEGE_USER); |
| 668 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 669 | // <Get SEL Time> |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 670 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_GET_SEL_TIME); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 671 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_TIME, NULL, ipmi_storage_get_sel_time, |
| 672 | PRIVILEGE_USER); |
Adriana Kobylak | 8e30f2a | 2015-10-20 10:23:51 -0500 | [diff] [blame] | 673 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 674 | // <Set SEL Time> |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 675 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_SET_SEL_TIME); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 676 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_SET_SEL_TIME, NULL, ipmi_storage_set_sel_time, |
| 677 | PRIVILEGE_OPERATOR); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 678 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 679 | // <Reserve SEL> |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 680 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_RESERVE_SEL); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 681 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_RESERVE_SEL, NULL, ipmi_storage_reserve_sel, |
| 682 | PRIVILEGE_USER); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 683 | |
Tom Joseph | a495339 | 2017-06-30 19:09:47 +0530 | [diff] [blame] | 684 | // <Get SEL Entry> |
| 685 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_GET_SEL_ENTRY); |
| 686 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_ENTRY, NULL, getSELEntry, |
| 687 | PRIVILEGE_USER); |
| 688 | |
Tom Joseph | 8f4a2aa | 2017-06-30 19:12:49 +0530 | [diff] [blame] | 689 | // <Delete SEL Entry> |
| 690 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_DELETE_SEL); |
| 691 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_DELETE_SEL, NULL, deleteSELEntry, |
| 692 | PRIVILEGE_OPERATOR); |
| 693 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 694 | // <Add SEL Entry> |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 695 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_ADD_SEL); |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 696 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_ADD_SEL, NULL, ipmi_storage_add_sel, |
| 697 | PRIVILEGE_OPERATOR); |
Tom Joseph | 2f05bb5 | 2017-06-30 19:14:49 +0530 | [diff] [blame] | 698 | // <Clear SEL> |
| 699 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_STORAGE, IPMI_CMD_CLEAR_SEL); |
| 700 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_CLEAR_SEL, NULL, clearSEL, |
| 701 | PRIVILEGE_OPERATOR); |
Marri Devender Rao | fa7b4e2 | 2017-07-03 00:52:20 -0500 | [diff] [blame] | 702 | // <Get FRU Inventory Area Info> |
| 703 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_STORAGE, |
| 704 | IPMI_CMD_GET_FRU_INV_AREA_INFO); |
| 705 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_FRU_INV_AREA_INFO, NULL, |
| 706 | ipmi_storage_get_fru_inv_area_info, PRIVILEGE_OPERATOR); |
| 707 | |
| 708 | // <Add READ FRU Data |
| 709 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_STORAGE, |
| 710 | IPMI_CMD_READ_FRU_DATA); |
Marri Devender Rao | fa7b4e2 | 2017-07-03 00:52:20 -0500 | [diff] [blame] | 711 | ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_READ_FRU_DATA, NULL, |
| 712 | ipmi_storage_read_fru_data, PRIVILEGE_OPERATOR); |
Marri Devender Rao | cac383b | 2017-07-03 13:24:27 -0500 | [diff] [blame] | 713 | |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 714 | ipmi::fru::registerCallbackHandler(); |
Chris Austen | b4f5b92 | 2015-10-13 12:44:43 -0500 | [diff] [blame] | 715 | return; |
| 716 | } |
| 717 | |