blob: 905d0b7d5fdd2c4f79318932c24d8e0a52492121 [file] [log] [blame]
Patrick Venture3a5071a2018-09-12 13:27:42 -07001#include "storagehandler.hpp"
2
3#include "fruread.hpp"
4#include "read_fru_data.hpp"
5#include "selutility.hpp"
6#include "sensorhandler.hpp"
7#include "storageaddsel.hpp"
Patrick Venture3a5071a2018-09-12 13:27:42 -07008
Lei YU52d91242017-10-17 22:52:28 +08009#include <arpa/inet.h>
Patrick Venture3a5071a2018-09-12 13:27:42 -070010#include <mapper.h>
11#include <systemd/sd-bus.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070012
13#include <algorithm>
Lei YU52d91242017-10-17 22:52:28 +080014#include <chrono>
15#include <cstdio>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070016#include <cstring>
Vernon Mauerybdda8002019-02-26 10:18:51 -080017#include <filesystem>
Vernon Mauerye08fbff2019-04-03 09:19:34 -070018#include <ipmid/api.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -070019#include <ipmid/utils.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070020#include <phosphor-logging/elog-errors.hpp>
21#include <phosphor-logging/log.hpp>
22#include <sdbusplus/server.hpp>
23#include <string>
Vernon Mauery16b86932019-05-01 08:36:11 -070024#include <variant>
Patrick Venture3a5071a2018-09-12 13:27:42 -070025#include <xyz/openbmc_project/Common/error.hpp>
26
Chris Austenb4f5b922015-10-13 12:44:43 -050027void register_netfn_storage_functions() __attribute__((constructor));
28
Patrick Venture0b02be92018-08-31 11:55:55 -070029unsigned int g_sel_time = 0xFFFFFFFF;
Patrick Venturedb0cbe62019-09-09 14:47:22 -070030namespace ipmi
31{
32namespace sensor
33{
34extern const IdInfoMap sensors;
35} // namespace sensor
36} // namespace ipmi
37
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -060038extern const FruMap frus;
anil kumar appana2c7db1d2019-05-28 11:20:19 +000039constexpr uint8_t eventDataSize = 3;
Patrick Venture0b02be92018-08-31 11:55:55 -070040namespace
41{
Lei YUe8939392017-06-15 10:45:05 +080042constexpr auto TIME_INTERFACE = "xyz.openbmc_project.Time.EpochTime";
43constexpr auto HOST_TIME_PATH = "/xyz/openbmc_project/time/host";
44constexpr auto DBUS_PROPERTIES = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070045constexpr auto PROPERTY_ELAPSED = "Elapsed";
Lei YUe8939392017-06-15 10:45:05 +080046
Patrick Venture0b02be92018-08-31 11:55:55 -070047} // namespace
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +053048
Tom Joseph6f7deaa2017-06-30 19:03:54 +053049namespace cache
50{
Patrick Venture0b02be92018-08-31 11:55:55 -070051/*
52 * This cache contains the object paths of the logging entries sorted in the
53 * order of the filename(numeric order). The cache is initialized by
54 * invoking readLoggingObjectPaths with the cache as the parameter. The
55 * cache is invoked in the execution of the Get SEL info and Delete SEL
56 * entry command. The Get SEL Info command is typically invoked before the
57 * Get SEL entry command, so the cache is utilized for responding to Get SEL
58 * entry command. The cache is invalidated by clearing after Delete SEL
59 * entry and Clear SEL command.
60 */
61ipmi::sel::ObjectPaths paths;
Tom Joseph6f7deaa2017-06-30 19:03:54 +053062
Patrick Venture0b02be92018-08-31 11:55:55 -070063} // namespace cache
Tom Joseph6f7deaa2017-06-30 19:03:54 +053064
65using InternalFailure =
Patrick Venture0b02be92018-08-31 11:55:55 -070066 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Tom Joseph6f7deaa2017-06-30 19:03:54 +053067using namespace phosphor::logging;
Marri Devender Raocac383b2017-07-03 13:24:27 -050068using namespace ipmi::fru;
69
70/**
71 * @enum Device access mode
72 */
73enum class AccessMode
74{
75 bytes, ///< Device is accessed by bytes
76 words ///< Device is accessed by words
77};
78
jayaprakash Mutyalab7557722019-05-02 21:13:30 +000079/** @brief implements the get SEL Info command
80 * @returns IPMI completion code plus response data
81 * - selVersion - SEL revision
82 * - entries - Number of log entries in SEL.
83 * - freeSpace - Free Space in bytes.
84 * - addTimeStamp - Most recent addition timestamp
85 * - eraseTimeStamp - Most recent erase timestamp
86 * - operationSupport - Reserve & Delete SEL operations supported
87 */
88
89ipmi::RspType<uint8_t, // SEL revision.
90 uint16_t, // number of log entries in SEL.
91 uint16_t, // free Space in bytes.
92 uint32_t, // most recent addition timestamp
93 uint32_t, // most recent erase timestamp.
94
95 bool, // SEL allocation info supported
96 bool, // reserve SEL supported
97 bool, // partial Add SEL Entry supported
98 bool, // delete SEL supported
99 uint3_t, // reserved
100 bool // overflow flag
101 >
102 ipmiStorageGetSelInfo()
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530103{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000104 uint16_t entries = 0;
105 // Most recent addition timestamp.
106 uint32_t addTimeStamp = ipmi::sel::invalidTimeStamp;
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530107
Tom Josephe59abfb2018-08-06 18:46:27 +0530108 try
109 {
110 ipmi::sel::readLoggingObjectPaths(cache::paths);
111 }
112 catch (const sdbusplus::exception::SdBusError& e)
113 {
114 // No action if reading log objects have failed for this command.
115 // readLoggingObjectPaths will throw exception if there are no log
116 // entries. The command will be responded with number of SEL entries
117 // as 0.
118 }
119
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530120 if (!cache::paths.empty())
121 {
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000122 entries = static_cast<uint16_t>(cache::paths.size());
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530123
124 try
125 {
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000126 addTimeStamp = static_cast<uint32_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700127 (ipmi::sel::getEntryTimeStamp(cache::paths.back()).count()));
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530128 }
129 catch (InternalFailure& e)
130 {
131 }
132 catch (const std::runtime_error& e)
133 {
134 log<level::ERR>(e.what());
135 }
136 }
137
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000138 constexpr uint8_t selVersion = ipmi::sel::selVersion;
139 constexpr uint16_t freeSpace = 0xFFFF;
140 constexpr uint32_t eraseTimeStamp = ipmi::sel::invalidTimeStamp;
141 constexpr uint3_t reserved{0};
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530142
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000143 return ipmi::responseSuccess(
144 selVersion, entries, freeSpace, addTimeStamp, eraseTimeStamp,
145 ipmi::sel::operationSupport::getSelAllocationInfo,
146 ipmi::sel::operationSupport::reserveSel,
147 ipmi::sel::operationSupport::partialAddSelEntry,
148 ipmi::sel::operationSupport::deleteSel, reserved,
149 ipmi::sel::operationSupport::overflow);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530150}
151
Tom Josepha4953392017-06-30 19:09:47 +0530152ipmi_ret_t getSELEntry(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
153 ipmi_request_t request, ipmi_response_t response,
154 ipmi_data_len_t data_len, ipmi_context_t context)
155{
Jason M. Bills851acb12019-02-04 14:06:57 -0800156 if (*data_len != sizeof(ipmi::sel::GetSELEntryRequest))
157 {
158 *data_len = 0;
159 return IPMI_CC_REQ_DATA_LEN_INVALID;
160 }
161
Patrick Venture0b02be92018-08-31 11:55:55 -0700162 auto requestData =
163 reinterpret_cast<const ipmi::sel::GetSELEntryRequest*>(request);
Tom Josepha4953392017-06-30 19:09:47 +0530164
165 if (requestData->reservationID != 0)
166 {
Jason M. Bills13e67c82018-09-10 14:12:16 -0700167 if (!checkSELReservation(requestData->reservationID))
Tom Josepha4953392017-06-30 19:09:47 +0530168 {
169 *data_len = 0;
170 return IPMI_CC_INVALID_RESERVATION_ID;
171 }
172 }
173
174 if (cache::paths.empty())
175 {
176 *data_len = 0;
177 return IPMI_CC_SENSOR_INVALID;
178 }
179
180 ipmi::sel::ObjectPaths::const_iterator iter;
181
182 // Check for the requested SEL Entry.
183 if (requestData->selRecordID == ipmi::sel::firstEntry)
184 {
185 iter = cache::paths.begin();
186 }
187 else if (requestData->selRecordID == ipmi::sel::lastEntry)
188 {
189 iter = cache::paths.end();
190 }
191 else
192 {
193 std::string objPath = std::string(ipmi::sel::logBasePath) + "/" +
194 std::to_string(requestData->selRecordID);
195
196 iter = std::find(cache::paths.begin(), cache::paths.end(), objPath);
197 if (iter == cache::paths.end())
198 {
199 *data_len = 0;
200 return IPMI_CC_SENSOR_INVALID;
201 }
202 }
203
Patrick Venture0b02be92018-08-31 11:55:55 -0700204 ipmi::sel::GetSELEntryResponse record{};
Tom Josepha4953392017-06-30 19:09:47 +0530205
206 // Convert the log entry into SEL record.
207 try
208 {
209 record = ipmi::sel::convertLogEntrytoSEL(*iter);
210 }
211 catch (InternalFailure& e)
212 {
213 *data_len = 0;
214 return IPMI_CC_UNSPECIFIED_ERROR;
215 }
216 catch (const std::runtime_error& e)
217 {
218 log<level::ERR>(e.what());
219 *data_len = 0;
220 return IPMI_CC_UNSPECIFIED_ERROR;
221 }
222
Tom Josepha4953392017-06-30 19:09:47 +0530223 // Identify the next SEL record ID
Patrick Venture0b02be92018-08-31 11:55:55 -0700224 if (iter != cache::paths.end())
Tom Josepha4953392017-06-30 19:09:47 +0530225 {
226 ++iter;
227 if (iter == cache::paths.end())
228 {
229 record.nextRecordID = ipmi::sel::lastEntry;
230 }
231 else
232 {
Vernon Mauery185b9f82018-07-20 10:52:36 -0700233 namespace fs = std::filesystem;
Tom Josepha4953392017-06-30 19:09:47 +0530234 fs::path path(*iter);
Patrick Venture0b02be92018-08-31 11:55:55 -0700235 record.nextRecordID = static_cast<uint16_t>(
236 std::stoul(std::string(path.filename().c_str())));
Tom Josepha4953392017-06-30 19:09:47 +0530237 }
238 }
239 else
240 {
241 record.nextRecordID = ipmi::sel::lastEntry;
242 }
243
244 if (requestData->readLength == ipmi::sel::entireRecord)
245 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700246 std::memcpy(response, &record, sizeof(record));
Tom Josepha4953392017-06-30 19:09:47 +0530247 *data_len = sizeof(record);
248 }
249 else
250 {
251 if (requestData->offset >= ipmi::sel::selRecordSize ||
252 requestData->readLength > ipmi::sel::selRecordSize)
253 {
254 *data_len = 0;
255 return IPMI_CC_INVALID_FIELD_REQUEST;
256 }
257
258 auto diff = ipmi::sel::selRecordSize - requestData->offset;
Patrick Venture0b02be92018-08-31 11:55:55 -0700259 auto readLength =
260 std::min(diff, static_cast<int>(requestData->readLength));
Tom Josepha4953392017-06-30 19:09:47 +0530261
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700262 std::memcpy(response, &record.nextRecordID,
263 sizeof(record.nextRecordID));
264 std::memcpy(static_cast<uint8_t*>(response) +
265 sizeof(record.nextRecordID),
266 &record.recordID + requestData->offset, readLength);
Tom Josepha4953392017-06-30 19:09:47 +0530267 *data_len = sizeof(record.nextRecordID) + readLength;
268 }
269
270 return IPMI_CC_OK;
271}
272
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000273/** @brief implements the delete SEL entry command
274 * @request
275 * - reservationID; // reservation ID.
276 * - selRecordID; // SEL record ID.
277 *
278 * @returns ipmi completion code plus response data
279 * - Record ID of the deleted record
280 */
281ipmi::RspType<uint16_t // deleted record ID
282 >
283 deleteSELEntry(uint16_t reservationID, uint16_t selRecordID)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530284{
Jason M. Bills851acb12019-02-04 14:06:57 -0800285
Brad Bishop1a4117b2018-11-21 15:48:18 -0500286 namespace fs = std::filesystem;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530287
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000288 if (!checkSELReservation(reservationID))
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530289 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000290 return ipmi::responseInvalidReservationId();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530291 }
292
Jason M. Bills13e67c82018-09-10 14:12:16 -0700293 // Per the IPMI spec, need to cancel the reservation when a SEL entry is
294 // deleted
295 cancelSELReservation();
296
Tom Josephe59abfb2018-08-06 18:46:27 +0530297 try
298 {
299 ipmi::sel::readLoggingObjectPaths(cache::paths);
300 }
301 catch (const sdbusplus::exception::SdBusError& e)
302 {
303 // readLoggingObjectPaths will throw exception if there are no error
304 // log entries.
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000305 return ipmi::responseSensorInvalid();
Tom Josephe59abfb2018-08-06 18:46:27 +0530306 }
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530307
308 if (cache::paths.empty())
309 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000310 return ipmi::responseSensorInvalid();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530311 }
312
313 ipmi::sel::ObjectPaths::const_iterator iter;
314 uint16_t delRecordID = 0;
315
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000316 if (selRecordID == ipmi::sel::firstEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530317 {
318 iter = cache::paths.begin();
319 fs::path path(*iter);
Patrick Venture0b02be92018-08-31 11:55:55 -0700320 delRecordID = static_cast<uint16_t>(
321 std::stoul(std::string(path.filename().c_str())));
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530322 }
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000323 else if (selRecordID == ipmi::sel::lastEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530324 {
325 iter = cache::paths.end();
326 fs::path path(*iter);
Patrick Venture0b02be92018-08-31 11:55:55 -0700327 delRecordID = static_cast<uint16_t>(
328 std::stoul(std::string(path.filename().c_str())));
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530329 }
330 else
331 {
332 std::string objPath = std::string(ipmi::sel::logBasePath) + "/" +
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000333 std::to_string(selRecordID);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530334
335 iter = std::find(cache::paths.begin(), cache::paths.end(), objPath);
336 if (iter == cache::paths.end())
337 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000338 return ipmi::responseSensorInvalid();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530339 }
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000340 delRecordID = selRecordID;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530341 }
342
343 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
344 std::string service;
345
346 try
347 {
348 service = ipmi::getService(bus, ipmi::sel::logDeleteIntf, *iter);
349 }
350 catch (const std::runtime_error& e)
351 {
352 log<level::ERR>(e.what());
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000353 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530354 }
355
Patrick Venture0b02be92018-08-31 11:55:55 -0700356 auto methodCall = bus.new_method_call(service.c_str(), (*iter).c_str(),
357 ipmi::sel::logDeleteIntf, "Delete");
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530358 auto reply = bus.call(methodCall);
359 if (reply.is_method_error())
360 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000361 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530362 }
363
364 // Invalidate the cache of dbus entry objects.
365 cache::paths.clear();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530366
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000367 return ipmi::responseSuccess(delRecordID);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530368}
369
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000370/** @brief implements the Clear SEL command
371 * @request
372 * - reservationID // Reservation ID.
373 * - clr // char array { 'C'(0x43h), 'L'(0x4Ch), 'R'(0x52h) }
374 * - eraseOperation; // requested operation.
375 *
376 * @returns ipmi completion code plus response data
377 * - erase status
378 */
379
380ipmi::RspType<uint8_t // erase status
381 >
382 clearSEL(uint16_t reservationID, const std::array<char, 3>& clr,
383 uint8_t eraseOperation)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530384{
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000385 static constexpr std::array<char, 3> clrOk = {'C', 'L', 'R'};
386 if (clr != clrOk)
Jason M. Bills851acb12019-02-04 14:06:57 -0800387 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000388 return ipmi::responseInvalidFieldRequest();
Jason M. Bills851acb12019-02-04 14:06:57 -0800389 }
390
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000391 if (!checkSELReservation(reservationID))
Tom Joseph2f05bb52017-06-30 19:14:49 +0530392 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000393 return ipmi::responseInvalidReservationId();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530394 }
395
Tom Joseph2f05bb52017-06-30 19:14:49 +0530396 /*
397 * Erasure status cannot be fetched from DBUS, so always return erasure
398 * status as `erase completed`.
399 */
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000400 if (eraseOperation == ipmi::sel::getEraseStatus)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530401 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000402 return ipmi::responseSuccess(
403 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530404 }
405
Jason M. Bills13e67c82018-09-10 14:12:16 -0700406 // Per the IPMI spec, need to cancel any reservation when the SEL is cleared
407 cancelSELReservation();
408
Tom Joseph2f05bb52017-06-30 19:14:49 +0530409 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Tom Josephe59abfb2018-08-06 18:46:27 +0530410 ipmi::sel::ObjectPaths objectPaths;
Tom Joseph2f05bb52017-06-30 19:14:49 +0530411 auto depth = 0;
412
Patrick Venture0b02be92018-08-31 11:55:55 -0700413 auto mapperCall =
414 bus.new_method_call(ipmi::sel::mapperBusName, ipmi::sel::mapperObjPath,
415 ipmi::sel::mapperIntf, "GetSubTreePaths");
Tom Joseph2f05bb52017-06-30 19:14:49 +0530416 mapperCall.append(ipmi::sel::logBasePath);
417 mapperCall.append(depth);
418 mapperCall.append(ipmi::sel::ObjectPaths({ipmi::sel::logEntryIntf}));
419
Tom Josephe59abfb2018-08-06 18:46:27 +0530420 try
Tom Joseph2f05bb52017-06-30 19:14:49 +0530421 {
Tom Josephe59abfb2018-08-06 18:46:27 +0530422 auto reply = bus.call(mapperCall);
423 if (reply.is_method_error())
424 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000425 return ipmi::responseSuccess(
426 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Josephe59abfb2018-08-06 18:46:27 +0530427 }
Tom Joseph2f05bb52017-06-30 19:14:49 +0530428
Tom Josephe59abfb2018-08-06 18:46:27 +0530429 reply.read(objectPaths);
430 if (objectPaths.empty())
431 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000432 return ipmi::responseSuccess(
433 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Josephe59abfb2018-08-06 18:46:27 +0530434 }
435 }
436 catch (const sdbusplus::exception::SdBusError& e)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530437 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000438 return ipmi::responseSuccess(
439 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530440 }
441
442 std::string service;
443
444 try
445 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700446 service = ipmi::getService(bus, ipmi::sel::logDeleteIntf,
Tom Joseph2f05bb52017-06-30 19:14:49 +0530447 objectPaths.front());
448 }
449 catch (const std::runtime_error& e)
450 {
451 log<level::ERR>(e.what());
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000452 return ipmi::responseUnspecifiedError();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530453 }
454
455 for (const auto& iter : objectPaths)
456 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700457 auto methodCall = bus.new_method_call(
458 service.c_str(), iter.c_str(), ipmi::sel::logDeleteIntf, "Delete");
Tom Joseph2f05bb52017-06-30 19:14:49 +0530459
460 auto reply = bus.call(methodCall);
461 if (reply.is_method_error())
462 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000463 return ipmi::responseUnspecifiedError();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530464 }
465 }
466
467 // Invalidate the cache of dbus entry objects.
468 cache::paths.clear();
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000469 return ipmi::responseSuccess(
470 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530471}
472
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000473/** @brief implements the get SEL time command
474 * @returns IPMI completion code plus response data
475 * -current time
476 */
477ipmi::RspType<uint32_t> // current time
478 ipmiStorageGetSelTime()
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500479{
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530480 using namespace std::chrono;
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530481 uint64_t host_time_usec = 0;
Nagaraju Goruganti8960b7c2018-04-29 22:38:40 -0500482 std::stringstream hostTime;
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500483
Lei YUe8939392017-06-15 10:45:05 +0800484 try
485 {
486 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
487 auto service = ipmi::getService(bus, TIME_INTERFACE, HOST_TIME_PATH);
Vernon Mauery16b86932019-05-01 08:36:11 -0700488 std::variant<uint64_t> value;
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530489
Lei YUe8939392017-06-15 10:45:05 +0800490 // Get host time
Patrick Venture0b02be92018-08-31 11:55:55 -0700491 auto method = bus.new_method_call(service.c_str(), HOST_TIME_PATH,
492 DBUS_PROPERTIES, "Get");
Lei YUe8939392017-06-15 10:45:05 +0800493
494 method.append(TIME_INTERFACE, PROPERTY_ELAPSED);
495 auto reply = bus.call(method);
496 if (reply.is_method_error())
497 {
498 log<level::ERR>("Error getting time",
499 entry("SERVICE=%s", service.c_str()),
500 entry("PATH=%s", HOST_TIME_PATH));
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000501 return ipmi::responseUnspecifiedError();
Lei YUe8939392017-06-15 10:45:05 +0800502 }
503 reply.read(value);
Vernon Maueryf442e112019-04-09 11:44:36 -0700504 host_time_usec = std::get<uint64_t>(value);
Lei YUe8939392017-06-15 10:45:05 +0800505 }
506 catch (InternalFailure& e)
507 {
508 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000509 return ipmi::responseUnspecifiedError();
Lei YUe8939392017-06-15 10:45:05 +0800510 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530511 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800512 {
513 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000514 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530515 }
516
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000517 hostTime << "Host time:"
518 << duration_cast<seconds>(microseconds(host_time_usec)).count();
Nagaraju Goruganti8960b7c2018-04-29 22:38:40 -0500519 log<level::DEBUG>(hostTime.str().c_str());
520
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530521 // Time is really long int but IPMI wants just uint32. This works okay until
522 // the number of seconds since 1970 overflows uint32 size.. Still a whole
523 // lot of time here to even think about that.
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000524 return ipmi::responseSuccess(
525 duration_cast<seconds>(microseconds(host_time_usec)).count());
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500526}
527
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000528/** @brief implements the set SEL time command
529 * @param selDeviceTime - epoch time
530 * -local time as the number of seconds from 00:00:00, January 1, 1970
531 * @returns IPMI completion code
532 */
533ipmi::RspType<> ipmiStorageSetSelTime(uint32_t selDeviceTime)
Chris Austenb4f5b922015-10-13 12:44:43 -0500534{
Lei YUe8939392017-06-15 10:45:05 +0800535 using namespace std::chrono;
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000536 microseconds usec{seconds(selDeviceTime)};
Norman James82330442015-11-19 16:53:26 -0600537
Lei YUe8939392017-06-15 10:45:05 +0800538 try
539 {
540 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
541 auto service = ipmi::getService(bus, TIME_INTERFACE, HOST_TIME_PATH);
Vernon Mauery16b86932019-05-01 08:36:11 -0700542 std::variant<uint64_t> value{usec.count()};
Lei YUe8939392017-06-15 10:45:05 +0800543
544 // Set host time
Patrick Venture0b02be92018-08-31 11:55:55 -0700545 auto method = bus.new_method_call(service.c_str(), HOST_TIME_PATH,
546 DBUS_PROPERTIES, "Set");
Lei YUe8939392017-06-15 10:45:05 +0800547
548 method.append(TIME_INTERFACE, PROPERTY_ELAPSED, value);
549 auto reply = bus.call(method);
550 if (reply.is_method_error())
551 {
552 log<level::ERR>("Error setting time",
553 entry("SERVICE=%s", service.c_str()),
554 entry("PATH=%s", HOST_TIME_PATH));
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000555 return ipmi::responseUnspecifiedError();
Lei YUe8939392017-06-15 10:45:05 +0800556 }
Norman James82330442015-11-19 16:53:26 -0600557 }
Lei YUe8939392017-06-15 10:45:05 +0800558 catch (InternalFailure& e)
559 {
560 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000561 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530562 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530563 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800564 {
565 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000566 return ipmi::responseUnspecifiedError();
Norman James82330442015-11-19 16:53:26 -0600567 }
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530568
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000569 return ipmi::responseSuccess();
Chris Austenb4f5b922015-10-13 12:44:43 -0500570}
571
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000572/** @brief implements the reserve SEL command
573 * @returns IPMI completion code plus response data
574 * - SEL reservation ID.
575 */
576ipmi::RspType<uint16_t> ipmiStorageReserveSel()
Chris Austenb4f5b922015-10-13 12:44:43 -0500577{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000578 return ipmi::responseSuccess(reserveSel());
Chris Austenb4f5b922015-10-13 12:44:43 -0500579}
580
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000581/** @brief implements the Add SEL entry command
582 * @request
583 *
584 * - recordID ID used for SEL Record access
585 * - recordType Record Type
586 * - timeStamp Time when event was logged. LS byte first
587 * - generatorID software ID if event was generated from
588 * system software
589 * - evmRev event message format version
590 * - sensorType sensor type code for service that generated
591 * the event
592 * - sensorNumber number of sensors that generated the event
593 * - eventDir event dir
594 * - eventData event data field contents
595 *
596 * @returns ipmi completion code plus response data
597 * - RecordID of the Added SEL entry
598 */
599ipmi::RspType<uint16_t // recordID of the Added SEL entry
600 >
601 ipmiStorageAddSEL(uint16_t recordID, uint8_t recordType, uint32_t timeStamp,
602 uint16_t generatorID, uint8_t evmRev, uint8_t sensorType,
603 uint8_t sensorNumber, uint8_t eventDir,
604 std::array<uint8_t, eventDataSize> eventData)
Chris Austenb4f5b922015-10-13 12:44:43 -0500605{
Jason M. Bills13e67c82018-09-10 14:12:16 -0700606 // Per the IPMI spec, need to cancel the reservation when a SEL entry is
607 // added
608 cancelSELReservation();
Tom Josephb647d5b2017-10-31 17:25:33 +0530609 // Hostboot sends SEL with OEM record type 0xDE to indicate that there is
610 // a maintenance procedure associated with eSEL record.
611 static constexpr auto procedureType = 0xDE;
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000612 if (recordType == procedureType)
Tom Josephb647d5b2017-10-31 17:25:33 +0530613 {
614 // In the OEM record type 0xDE, byte 11 in the SEL record indicate the
615 // procedure number.
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000616 createProcedureLogEntry(sensorType);
Tom Josephb647d5b2017-10-31 17:25:33 +0530617 }
Chris Austenb4f5b922015-10-13 12:44:43 -0500618
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000619 return ipmi::responseSuccess(recordID);
Chris Austenb4f5b922015-10-13 12:44:43 -0500620}
621
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300622bool isFruPresent(const std::string& fruPath)
623{
624 using namespace ipmi::fru;
625
626 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
627
628 auto propValue =
629 ipmi::getDbusProperty(bus, invMgrInterface, invObjPath + fruPath,
630 invItemInterface, itemPresentProp);
631
632 return std::get<bool>(propValue);
633}
634
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000635/** @brief implements the get FRU Inventory Area Info command
636 *
637 * @returns IPMI completion code plus response data
638 * - FRU Inventory area size in bytes,
639 * - access bit
640 **/
641ipmi::RspType<uint16_t, // FRU Inventory area size in bytes,
642 uint8_t // access size (bytes / words)
643 >
644 ipmiStorageGetFruInvAreaInfo(uint8_t fruID)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500645{
Tom Joseph187f5642018-03-29 13:49:06 +0530646
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000647 auto iter = frus.find(fruID);
Tom Joseph187f5642018-03-29 13:49:06 +0530648 if (iter == frus.end())
649 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000650 return ipmi::responseSensorInvalid();
Tom Joseph187f5642018-03-29 13:49:06 +0530651 }
652
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300653 auto path = iter->second[0].path;
654 if (!isFruPresent(path))
655 {
656 return ipmi::responseSensorInvalid();
657 }
658
Marri Devender Raocac383b2017-07-03 13:24:27 -0500659 try
660 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000661 return ipmi::responseSuccess(
662 static_cast<uint16_t>(getFruAreaData(fruID).size()),
663 static_cast<uint8_t>(AccessMode::bytes));
Marri Devender Raocac383b2017-07-03 13:24:27 -0500664 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700665 catch (const InternalFailure& e)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500666 {
Marri Devender Raocac383b2017-07-03 13:24:27 -0500667 log<level::ERR>(e.what());
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000668 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500669 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500670}
671
anil kumar appana5b7c3262019-05-27 18:10:23 +0000672/**@brief implements the Read FRU Data command
673 * @param fruDeviceId - FRU device ID. FFh = reserved
674 * @param offset - FRU inventory offset to read
675 * @param readCount - count to read
676 *
677 * @return IPMI completion code plus response data
678 * - returnCount - response data count.
679 * - data - response data
680 */
681ipmi::RspType<uint8_t, // count returned
682 std::vector<uint8_t>> // FRU data
683 ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t offset,
684 uint8_t readCount)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500685{
anil kumar appana5b7c3262019-05-27 18:10:23 +0000686 if (fruDeviceId == 0xFF)
Tom Joseph187f5642018-03-29 13:49:06 +0530687 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000688 return ipmi::responseInvalidFieldRequest();
Tom Joseph187f5642018-03-29 13:49:06 +0530689 }
690
anil kumar appana5b7c3262019-05-27 18:10:23 +0000691 auto iter = frus.find(fruDeviceId);
692 if (iter == frus.end())
693 {
694 return ipmi::responseSensorInvalid();
695 }
696
Marri Devender Raocac383b2017-07-03 13:24:27 -0500697 try
698 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000699 const auto& fruArea = getFruAreaData(fruDeviceId);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500700 auto size = fruArea.size();
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500701
Tom Josephefcd68b2018-04-26 18:46:27 +0530702 if (offset >= size)
703 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000704 return ipmi::responseParmOutOfRange();
Tom Josephefcd68b2018-04-26 18:46:27 +0530705 }
706
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500707 // Write the count of response data.
anil kumar appana5b7c3262019-05-27 18:10:23 +0000708 uint8_t returnCount;
709 if ((offset + readCount) <= size)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500710 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000711 returnCount = readCount;
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500712 }
713 else
714 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000715 returnCount = size - offset;
Marri Devender Raocac383b2017-07-03 13:24:27 -0500716 }
Ratan Gupta2848d602018-01-31 20:39:20 +0530717
anil kumar appana5b7c3262019-05-27 18:10:23 +0000718 std::vector<uint8_t> fruData((fruArea.begin() + offset),
719 (fruArea.begin() + offset + returnCount));
Ratan Gupta2848d602018-01-31 20:39:20 +0530720
anil kumar appana5b7c3262019-05-27 18:10:23 +0000721 return ipmi::responseSuccess(returnCount, fruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500722 }
723 catch (const InternalFailure& e)
724 {
Marri Devender Raocac383b2017-07-03 13:24:27 -0500725 log<level::ERR>(e.what());
anil kumar appana5b7c3262019-05-27 18:10:23 +0000726 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500727 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500728}
729
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000730ipmi::RspType<uint8_t, // SDR version
731 uint16_t, // record count LS first
732 uint16_t, // free space in bytes, LS first
733 uint32_t, // addition timestamp LS first
734 uint32_t, // deletion timestamp LS first
735 uint8_t> // operation Support
736 ipmiGetRepositoryInfo()
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600737{
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600738
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000739 constexpr uint8_t sdrVersion = 0x51;
740 constexpr uint16_t freeSpace = 0xFFFF;
741 constexpr uint32_t additionTimestamp = 0x0;
742 constexpr uint32_t deletionTimestamp = 0x0;
743 constexpr uint8_t operationSupport = 0;
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600744
Patrick Venturedb0cbe62019-09-09 14:47:22 -0700745 uint16_t records = frus.size() + ipmi::sensor::sensors.size();
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600746
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000747 return ipmi::responseSuccess(sdrVersion, records, freeSpace,
748 additionTimestamp, deletionTimestamp,
749 operationSupport);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600750}
Chris Austenb4f5b922015-10-13 12:44:43 -0500751
Chris Austenb4f5b922015-10-13 12:44:43 -0500752void register_netfn_storage_functions()
753{
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530754 // <Get SEL Info>
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000755 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
756 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
757 ipmiStorageGetSelInfo);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530758
Tom05732372016-09-06 17:21:23 +0530759 // <Get SEL Time>
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000760 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
761 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
762 ipmiStorageGetSelTime);
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500763
Tom05732372016-09-06 17:21:23 +0530764 // <Set SEL Time>
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000765 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
766 ipmi::storage::cmdSetSelTime,
767 ipmi::Privilege::Operator, ipmiStorageSetSelTime);
Chris Austenb4f5b922015-10-13 12:44:43 -0500768
Tom05732372016-09-06 17:21:23 +0530769 // <Reserve SEL>
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000770 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
771 ipmi::storage::cmdReserveSel, ipmi::Privilege::User,
772 ipmiStorageReserveSel);
Tom Josepha4953392017-06-30 19:09:47 +0530773 // <Get SEL Entry>
Patrick Venture0b02be92018-08-31 11:55:55 -0700774 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_ENTRY, NULL,
775 getSELEntry, PRIVILEGE_USER);
Tom Josepha4953392017-06-30 19:09:47 +0530776
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530777 // <Delete SEL Entry>
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000778 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
779 ipmi::storage::cmdDeleteSelEntry,
780 ipmi::Privilege::Operator, deleteSELEntry);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530781
Tom05732372016-09-06 17:21:23 +0530782 // <Add SEL Entry>
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000783 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
784 ipmi::storage::cmdAddSelEntry,
785 ipmi::Privilege::Operator, ipmiStorageAddSEL);
786
Tom Joseph2f05bb52017-06-30 19:14:49 +0530787 // <Clear SEL>
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000788 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
789 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
790 clearSEL);
791
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500792 // <Get FRU Inventory Area Info>
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000793 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
794 ipmi::storage::cmdGetFruInventoryAreaInfo,
795 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500796
Jason M. Billsb5248c92019-06-24 15:53:08 -0700797 // <READ FRU Data>
anil kumar appana5b7c3262019-05-27 18:10:23 +0000798 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
799 ipmi::storage::cmdReadFruData,
800 ipmi::Privilege::Operator, ipmiStorageReadFruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500801
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600802 // <Get Repository Info>
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000803 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
804 ipmi::storage::cmdGetSdrRepositoryInfo,
805 ipmi::Privilege::User, ipmiGetRepositoryInfo);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600806
Tom Joseph5ca50952018-02-22 00:33:38 +0530807 // <Reserve SDR Repository>
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000808 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
809 ipmi::storage::cmdReserveSdrRepository,
810 ipmi::Privilege::User, ipmiSensorReserveSdr);
Tom Joseph5ca50952018-02-22 00:33:38 +0530811
812 // <Get SDR>
Patrick Venture0b02be92018-08-31 11:55:55 -0700813 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SDR, nullptr,
814 ipmi_sen_get_sdr, PRIVILEGE_USER);
Tom Joseph5ca50952018-02-22 00:33:38 +0530815
Marri Devender Rao908f7502017-07-10 01:49:54 -0500816 ipmi::fru::registerCallbackHandler();
Chris Austenb4f5b922015-10-13 12:44:43 -0500817 return;
818}