blob: 392362121259eac7e365e0735708c15395c74bf2 [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;
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -060030extern const ipmi::sensor::IdInfoMap sensors;
31extern const FruMap frus;
Chris Austenb4f5b922015-10-13 12:44:43 -050032
Patrick Venture0b02be92018-08-31 11:55:55 -070033namespace
34{
Lei YUe8939392017-06-15 10:45:05 +080035constexpr auto TIME_INTERFACE = "xyz.openbmc_project.Time.EpochTime";
36constexpr auto HOST_TIME_PATH = "/xyz/openbmc_project/time/host";
37constexpr auto DBUS_PROPERTIES = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070038constexpr auto PROPERTY_ELAPSED = "Elapsed";
Lei YUe8939392017-06-15 10:45:05 +080039
Patrick Venture0b02be92018-08-31 11:55:55 -070040} // namespace
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +053041
Tom Joseph6f7deaa2017-06-30 19:03:54 +053042namespace cache
43{
Patrick Venture0b02be92018-08-31 11:55:55 -070044/*
45 * This cache contains the object paths of the logging entries sorted in the
46 * order of the filename(numeric order). The cache is initialized by
47 * invoking readLoggingObjectPaths with the cache as the parameter. The
48 * cache is invoked in the execution of the Get SEL info and Delete SEL
49 * entry command. The Get SEL Info command is typically invoked before the
50 * Get SEL entry command, so the cache is utilized for responding to Get SEL
51 * entry command. The cache is invalidated by clearing after Delete SEL
52 * entry and Clear SEL command.
53 */
54ipmi::sel::ObjectPaths paths;
Tom Joseph6f7deaa2017-06-30 19:03:54 +053055
Patrick Venture0b02be92018-08-31 11:55:55 -070056} // namespace cache
Tom Joseph6f7deaa2017-06-30 19:03:54 +053057
58using InternalFailure =
Patrick Venture0b02be92018-08-31 11:55:55 -070059 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Tom Joseph6f7deaa2017-06-30 19:03:54 +053060using namespace phosphor::logging;
Marri Devender Raocac383b2017-07-03 13:24:27 -050061using namespace ipmi::fru;
62
63/**
64 * @enum Device access mode
65 */
66enum class AccessMode
67{
68 bytes, ///< Device is accessed by bytes
69 words ///< Device is accessed by words
70};
71
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -050072ipmi_ret_t ipmi_storage_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -070073 ipmi_request_t request,
74 ipmi_response_t response,
75 ipmi_data_len_t data_len,
76 ipmi_context_t context)
Chris Austenb4f5b922015-10-13 12:44:43 -050077{
Chris Austenb4f5b922015-10-13 12:44:43 -050078 // Status code.
Nan Li70aa8d92016-08-29 00:11:10 +080079 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenb4f5b922015-10-13 12:44:43 -050080 *data_len = 0;
81 return rc;
82}
83
Tom Joseph6f7deaa2017-06-30 19:03:54 +053084ipmi_ret_t getSELInfo(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
85 ipmi_request_t request, ipmi_response_t response,
86 ipmi_data_len_t data_len, ipmi_context_t context)
87{
Jason M. Bills851acb12019-02-04 14:06:57 -080088 if (*data_len != 0)
89 {
90 *data_len = 0;
91 return IPMI_CC_REQ_DATA_LEN_INVALID;
92 }
93
Tom Joseph6f7deaa2017-06-30 19:03:54 +053094 std::vector<uint8_t> outPayload(sizeof(ipmi::sel::GetSELInfoResponse));
Patrick Venture0b02be92018-08-31 11:55:55 -070095 auto responseData =
96 reinterpret_cast<ipmi::sel::GetSELInfoResponse*>(outPayload.data());
Tom Joseph6f7deaa2017-06-30 19:03:54 +053097
98 responseData->selVersion = ipmi::sel::selVersion;
99 // Last erase timestamp is not available from log manager.
100 responseData->eraseTimeStamp = ipmi::sel::invalidTimeStamp;
101 responseData->operationSupport = ipmi::sel::operationSupport;
102
Tom Josephe59abfb2018-08-06 18:46:27 +0530103 try
104 {
105 ipmi::sel::readLoggingObjectPaths(cache::paths);
106 }
107 catch (const sdbusplus::exception::SdBusError& e)
108 {
109 // No action if reading log objects have failed for this command.
110 // readLoggingObjectPaths will throw exception if there are no log
111 // entries. The command will be responded with number of SEL entries
112 // as 0.
113 }
114
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530115 responseData->entries = 0;
116 responseData->addTimeStamp = ipmi::sel::invalidTimeStamp;
117
118 if (!cache::paths.empty())
119 {
120 responseData->entries = static_cast<uint16_t>(cache::paths.size());
121
122 try
123 {
124 responseData->addTimeStamp = static_cast<uint32_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700125 (ipmi::sel::getEntryTimeStamp(cache::paths.back()).count()));
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530126 }
127 catch (InternalFailure& e)
128 {
129 }
130 catch (const std::runtime_error& e)
131 {
132 log<level::ERR>(e.what());
133 }
134 }
135
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700136 std::memcpy(response, outPayload.data(), outPayload.size());
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530137 *data_len = outPayload.size();
138
139 return IPMI_CC_OK;
140}
141
Tom Josepha4953392017-06-30 19:09:47 +0530142ipmi_ret_t getSELEntry(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
143 ipmi_request_t request, ipmi_response_t response,
144 ipmi_data_len_t data_len, ipmi_context_t context)
145{
Jason M. Bills851acb12019-02-04 14:06:57 -0800146 if (*data_len != sizeof(ipmi::sel::GetSELEntryRequest))
147 {
148 *data_len = 0;
149 return IPMI_CC_REQ_DATA_LEN_INVALID;
150 }
151
Patrick Venture0b02be92018-08-31 11:55:55 -0700152 auto requestData =
153 reinterpret_cast<const ipmi::sel::GetSELEntryRequest*>(request);
Tom Josepha4953392017-06-30 19:09:47 +0530154
155 if (requestData->reservationID != 0)
156 {
Jason M. Bills13e67c82018-09-10 14:12:16 -0700157 if (!checkSELReservation(requestData->reservationID))
Tom Josepha4953392017-06-30 19:09:47 +0530158 {
159 *data_len = 0;
160 return IPMI_CC_INVALID_RESERVATION_ID;
161 }
162 }
163
164 if (cache::paths.empty())
165 {
166 *data_len = 0;
167 return IPMI_CC_SENSOR_INVALID;
168 }
169
170 ipmi::sel::ObjectPaths::const_iterator iter;
171
172 // Check for the requested SEL Entry.
173 if (requestData->selRecordID == ipmi::sel::firstEntry)
174 {
175 iter = cache::paths.begin();
176 }
177 else if (requestData->selRecordID == ipmi::sel::lastEntry)
178 {
179 iter = cache::paths.end();
180 }
181 else
182 {
183 std::string objPath = std::string(ipmi::sel::logBasePath) + "/" +
184 std::to_string(requestData->selRecordID);
185
186 iter = std::find(cache::paths.begin(), cache::paths.end(), objPath);
187 if (iter == cache::paths.end())
188 {
189 *data_len = 0;
190 return IPMI_CC_SENSOR_INVALID;
191 }
192 }
193
Patrick Venture0b02be92018-08-31 11:55:55 -0700194 ipmi::sel::GetSELEntryResponse record{};
Tom Josepha4953392017-06-30 19:09:47 +0530195
196 // Convert the log entry into SEL record.
197 try
198 {
199 record = ipmi::sel::convertLogEntrytoSEL(*iter);
200 }
201 catch (InternalFailure& e)
202 {
203 *data_len = 0;
204 return IPMI_CC_UNSPECIFIED_ERROR;
205 }
206 catch (const std::runtime_error& e)
207 {
208 log<level::ERR>(e.what());
209 *data_len = 0;
210 return IPMI_CC_UNSPECIFIED_ERROR;
211 }
212
Tom Josepha4953392017-06-30 19:09:47 +0530213 // Identify the next SEL record ID
Patrick Venture0b02be92018-08-31 11:55:55 -0700214 if (iter != cache::paths.end())
Tom Josepha4953392017-06-30 19:09:47 +0530215 {
216 ++iter;
217 if (iter == cache::paths.end())
218 {
219 record.nextRecordID = ipmi::sel::lastEntry;
220 }
221 else
222 {
Vernon Mauery185b9f82018-07-20 10:52:36 -0700223 namespace fs = std::filesystem;
Tom Josepha4953392017-06-30 19:09:47 +0530224 fs::path path(*iter);
Patrick Venture0b02be92018-08-31 11:55:55 -0700225 record.nextRecordID = static_cast<uint16_t>(
226 std::stoul(std::string(path.filename().c_str())));
Tom Josepha4953392017-06-30 19:09:47 +0530227 }
228 }
229 else
230 {
231 record.nextRecordID = ipmi::sel::lastEntry;
232 }
233
234 if (requestData->readLength == ipmi::sel::entireRecord)
235 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700236 std::memcpy(response, &record, sizeof(record));
Tom Josepha4953392017-06-30 19:09:47 +0530237 *data_len = sizeof(record);
238 }
239 else
240 {
241 if (requestData->offset >= ipmi::sel::selRecordSize ||
242 requestData->readLength > ipmi::sel::selRecordSize)
243 {
244 *data_len = 0;
245 return IPMI_CC_INVALID_FIELD_REQUEST;
246 }
247
248 auto diff = ipmi::sel::selRecordSize - requestData->offset;
Patrick Venture0b02be92018-08-31 11:55:55 -0700249 auto readLength =
250 std::min(diff, static_cast<int>(requestData->readLength));
Tom Josepha4953392017-06-30 19:09:47 +0530251
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700252 std::memcpy(response, &record.nextRecordID,
253 sizeof(record.nextRecordID));
254 std::memcpy(static_cast<uint8_t*>(response) +
255 sizeof(record.nextRecordID),
256 &record.recordID + requestData->offset, readLength);
Tom Josepha4953392017-06-30 19:09:47 +0530257 *data_len = sizeof(record.nextRecordID) + readLength;
258 }
259
260 return IPMI_CC_OK;
261}
262
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000263/** @brief implements the delete SEL entry command
264 * @request
265 * - reservationID; // reservation ID.
266 * - selRecordID; // SEL record ID.
267 *
268 * @returns ipmi completion code plus response data
269 * - Record ID of the deleted record
270 */
271ipmi::RspType<uint16_t // deleted record ID
272 >
273 deleteSELEntry(uint16_t reservationID, uint16_t selRecordID)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530274{
Jason M. Bills851acb12019-02-04 14:06:57 -0800275
Brad Bishop1a4117b2018-11-21 15:48:18 -0500276 namespace fs = std::filesystem;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530277
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000278 if (!checkSELReservation(reservationID))
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530279 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000280 return ipmi::responseInvalidReservationId();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530281 }
282
Jason M. Bills13e67c82018-09-10 14:12:16 -0700283 // Per the IPMI spec, need to cancel the reservation when a SEL entry is
284 // deleted
285 cancelSELReservation();
286
Tom Josephe59abfb2018-08-06 18:46:27 +0530287 try
288 {
289 ipmi::sel::readLoggingObjectPaths(cache::paths);
290 }
291 catch (const sdbusplus::exception::SdBusError& e)
292 {
293 // readLoggingObjectPaths will throw exception if there are no error
294 // log entries.
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000295 return ipmi::responseSensorInvalid();
Tom Josephe59abfb2018-08-06 18:46:27 +0530296 }
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530297
298 if (cache::paths.empty())
299 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000300 return ipmi::responseSensorInvalid();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530301 }
302
303 ipmi::sel::ObjectPaths::const_iterator iter;
304 uint16_t delRecordID = 0;
305
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000306 if (selRecordID == ipmi::sel::firstEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530307 {
308 iter = cache::paths.begin();
309 fs::path path(*iter);
Patrick Venture0b02be92018-08-31 11:55:55 -0700310 delRecordID = static_cast<uint16_t>(
311 std::stoul(std::string(path.filename().c_str())));
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530312 }
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000313 else if (selRecordID == ipmi::sel::lastEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530314 {
315 iter = cache::paths.end();
316 fs::path path(*iter);
Patrick Venture0b02be92018-08-31 11:55:55 -0700317 delRecordID = static_cast<uint16_t>(
318 std::stoul(std::string(path.filename().c_str())));
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530319 }
320 else
321 {
322 std::string objPath = std::string(ipmi::sel::logBasePath) + "/" +
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000323 std::to_string(selRecordID);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530324
325 iter = std::find(cache::paths.begin(), cache::paths.end(), objPath);
326 if (iter == cache::paths.end())
327 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000328 return ipmi::responseSensorInvalid();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530329 }
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000330 delRecordID = selRecordID;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530331 }
332
333 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
334 std::string service;
335
336 try
337 {
338 service = ipmi::getService(bus, ipmi::sel::logDeleteIntf, *iter);
339 }
340 catch (const std::runtime_error& e)
341 {
342 log<level::ERR>(e.what());
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000343 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530344 }
345
Patrick Venture0b02be92018-08-31 11:55:55 -0700346 auto methodCall = bus.new_method_call(service.c_str(), (*iter).c_str(),
347 ipmi::sel::logDeleteIntf, "Delete");
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530348 auto reply = bus.call(methodCall);
349 if (reply.is_method_error())
350 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000351 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530352 }
353
354 // Invalidate the cache of dbus entry objects.
355 cache::paths.clear();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530356
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000357 return ipmi::responseSuccess(delRecordID);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530358}
359
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000360/** @brief implements the Clear SEL command
361 * @request
362 * - reservationID // Reservation ID.
363 * - clr // char array { 'C'(0x43h), 'L'(0x4Ch), 'R'(0x52h) }
364 * - eraseOperation; // requested operation.
365 *
366 * @returns ipmi completion code plus response data
367 * - erase status
368 */
369
370ipmi::RspType<uint8_t // erase status
371 >
372 clearSEL(uint16_t reservationID, const std::array<char, 3>& clr,
373 uint8_t eraseOperation)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530374{
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000375 static constexpr std::array<char, 3> clrOk = {'C', 'L', 'R'};
376 if (clr != clrOk)
Jason M. Bills851acb12019-02-04 14:06:57 -0800377 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000378 return ipmi::responseInvalidFieldRequest();
Jason M. Bills851acb12019-02-04 14:06:57 -0800379 }
380
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000381 if (!checkSELReservation(reservationID))
Tom Joseph2f05bb52017-06-30 19:14:49 +0530382 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000383 return ipmi::responseInvalidReservationId();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530384 }
385
Tom Joseph2f05bb52017-06-30 19:14:49 +0530386 /*
387 * Erasure status cannot be fetched from DBUS, so always return erasure
388 * status as `erase completed`.
389 */
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000390 if (eraseOperation == ipmi::sel::getEraseStatus)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530391 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000392 return ipmi::responseSuccess(
393 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530394 }
395
Jason M. Bills13e67c82018-09-10 14:12:16 -0700396 // Per the IPMI spec, need to cancel any reservation when the SEL is cleared
397 cancelSELReservation();
398
Tom Joseph2f05bb52017-06-30 19:14:49 +0530399 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Tom Josephe59abfb2018-08-06 18:46:27 +0530400 ipmi::sel::ObjectPaths objectPaths;
Tom Joseph2f05bb52017-06-30 19:14:49 +0530401 auto depth = 0;
402
Patrick Venture0b02be92018-08-31 11:55:55 -0700403 auto mapperCall =
404 bus.new_method_call(ipmi::sel::mapperBusName, ipmi::sel::mapperObjPath,
405 ipmi::sel::mapperIntf, "GetSubTreePaths");
Tom Joseph2f05bb52017-06-30 19:14:49 +0530406 mapperCall.append(ipmi::sel::logBasePath);
407 mapperCall.append(depth);
408 mapperCall.append(ipmi::sel::ObjectPaths({ipmi::sel::logEntryIntf}));
409
Tom Josephe59abfb2018-08-06 18:46:27 +0530410 try
Tom Joseph2f05bb52017-06-30 19:14:49 +0530411 {
Tom Josephe59abfb2018-08-06 18:46:27 +0530412 auto reply = bus.call(mapperCall);
413 if (reply.is_method_error())
414 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000415 return ipmi::responseSuccess(
416 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Josephe59abfb2018-08-06 18:46:27 +0530417 }
Tom Joseph2f05bb52017-06-30 19:14:49 +0530418
Tom Josephe59abfb2018-08-06 18:46:27 +0530419 reply.read(objectPaths);
420 if (objectPaths.empty())
421 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000422 return ipmi::responseSuccess(
423 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Josephe59abfb2018-08-06 18:46:27 +0530424 }
425 }
426 catch (const sdbusplus::exception::SdBusError& e)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530427 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000428 return ipmi::responseSuccess(
429 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530430 }
431
432 std::string service;
433
434 try
435 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700436 service = ipmi::getService(bus, ipmi::sel::logDeleteIntf,
Tom Joseph2f05bb52017-06-30 19:14:49 +0530437 objectPaths.front());
438 }
439 catch (const std::runtime_error& e)
440 {
441 log<level::ERR>(e.what());
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000442 return ipmi::responseUnspecifiedError();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530443 }
444
445 for (const auto& iter : objectPaths)
446 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700447 auto methodCall = bus.new_method_call(
448 service.c_str(), iter.c_str(), ipmi::sel::logDeleteIntf, "Delete");
Tom Joseph2f05bb52017-06-30 19:14:49 +0530449
450 auto reply = bus.call(methodCall);
451 if (reply.is_method_error())
452 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000453 return ipmi::responseUnspecifiedError();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530454 }
455 }
456
457 // Invalidate the cache of dbus entry objects.
458 cache::paths.clear();
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000459 return ipmi::responseSuccess(
460 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530461}
462
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000463/** @brief implements the get SEL time command
464 * @returns IPMI completion code plus response data
465 * -current time
466 */
467ipmi::RspType<uint32_t> // current time
468 ipmiStorageGetSelTime()
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500469{
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530470 using namespace std::chrono;
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530471 uint64_t host_time_usec = 0;
Nagaraju Goruganti8960b7c2018-04-29 22:38:40 -0500472 std::stringstream hostTime;
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500473
Lei YUe8939392017-06-15 10:45:05 +0800474 try
475 {
476 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
477 auto service = ipmi::getService(bus, TIME_INTERFACE, HOST_TIME_PATH);
Vernon Mauery16b86932019-05-01 08:36:11 -0700478 std::variant<uint64_t> value;
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530479
Lei YUe8939392017-06-15 10:45:05 +0800480 // Get host time
Patrick Venture0b02be92018-08-31 11:55:55 -0700481 auto method = bus.new_method_call(service.c_str(), HOST_TIME_PATH,
482 DBUS_PROPERTIES, "Get");
Lei YUe8939392017-06-15 10:45:05 +0800483
484 method.append(TIME_INTERFACE, PROPERTY_ELAPSED);
485 auto reply = bus.call(method);
486 if (reply.is_method_error())
487 {
488 log<level::ERR>("Error getting time",
489 entry("SERVICE=%s", service.c_str()),
490 entry("PATH=%s", HOST_TIME_PATH));
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000491 return ipmi::responseUnspecifiedError();
Lei YUe8939392017-06-15 10:45:05 +0800492 }
493 reply.read(value);
Vernon Maueryf442e112019-04-09 11:44:36 -0700494 host_time_usec = std::get<uint64_t>(value);
Lei YUe8939392017-06-15 10:45:05 +0800495 }
496 catch (InternalFailure& e)
497 {
498 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000499 return ipmi::responseUnspecifiedError();
Lei YUe8939392017-06-15 10:45:05 +0800500 }
501 catch (const std::runtime_error& e)
502 {
503 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000504 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530505 }
506
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000507 hostTime << "Host time:"
508 << duration_cast<seconds>(microseconds(host_time_usec)).count();
Nagaraju Goruganti8960b7c2018-04-29 22:38:40 -0500509 log<level::DEBUG>(hostTime.str().c_str());
510
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530511 // Time is really long int but IPMI wants just uint32. This works okay until
512 // the number of seconds since 1970 overflows uint32 size.. Still a whole
513 // lot of time here to even think about that.
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000514 return ipmi::responseSuccess(
515 duration_cast<seconds>(microseconds(host_time_usec)).count());
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500516}
517
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000518/** @brief implements the set SEL time command
519 * @param selDeviceTime - epoch time
520 * -local time as the number of seconds from 00:00:00, January 1, 1970
521 * @returns IPMI completion code
522 */
523ipmi::RspType<> ipmiStorageSetSelTime(uint32_t selDeviceTime)
Chris Austenb4f5b922015-10-13 12:44:43 -0500524{
Lei YUe8939392017-06-15 10:45:05 +0800525 using namespace std::chrono;
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000526 microseconds usec{seconds(selDeviceTime)};
Norman James82330442015-11-19 16:53:26 -0600527
Lei YUe8939392017-06-15 10:45:05 +0800528 try
529 {
530 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
531 auto service = ipmi::getService(bus, TIME_INTERFACE, HOST_TIME_PATH);
Vernon Mauery16b86932019-05-01 08:36:11 -0700532 std::variant<uint64_t> value{usec.count()};
Lei YUe8939392017-06-15 10:45:05 +0800533
534 // Set host time
Patrick Venture0b02be92018-08-31 11:55:55 -0700535 auto method = bus.new_method_call(service.c_str(), HOST_TIME_PATH,
536 DBUS_PROPERTIES, "Set");
Lei YUe8939392017-06-15 10:45:05 +0800537
538 method.append(TIME_INTERFACE, PROPERTY_ELAPSED, value);
539 auto reply = bus.call(method);
540 if (reply.is_method_error())
541 {
542 log<level::ERR>("Error setting time",
543 entry("SERVICE=%s", service.c_str()),
544 entry("PATH=%s", HOST_TIME_PATH));
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000545 return ipmi::responseUnspecifiedError();
Lei YUe8939392017-06-15 10:45:05 +0800546 }
Norman James82330442015-11-19 16:53:26 -0600547 }
Lei YUe8939392017-06-15 10:45:05 +0800548 catch (InternalFailure& e)
549 {
550 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000551 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530552 }
Lei YUe8939392017-06-15 10:45:05 +0800553 catch (const std::runtime_error& e)
554 {
555 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000556 return ipmi::responseUnspecifiedError();
Norman James82330442015-11-19 16:53:26 -0600557 }
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530558
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000559 return ipmi::responseSuccess();
Chris Austenb4f5b922015-10-13 12:44:43 -0500560}
561
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500562ipmi_ret_t ipmi_storage_reserve_sel(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700563 ipmi_request_t request,
564 ipmi_response_t response,
565 ipmi_data_len_t data_len,
566 ipmi_context_t context)
Chris Austenb4f5b922015-10-13 12:44:43 -0500567{
Jason M. Bills851acb12019-02-04 14:06:57 -0800568 if (*data_len != 0)
569 {
570 *data_len = 0;
571 return IPMI_CC_REQ_DATA_LEN_INVALID;
572 }
573
Chris Austenb4f5b922015-10-13 12:44:43 -0500574 ipmi_ret_t rc = IPMI_CC_OK;
Jason M. Bills13e67c82018-09-10 14:12:16 -0700575 unsigned short selResID = reserveSel();
Chris Austenb4f5b922015-10-13 12:44:43 -0500576
Jason M. Bills13e67c82018-09-10 14:12:16 -0700577 *data_len = sizeof(selResID);
Chris Austenb4f5b922015-10-13 12:44:43 -0500578
579 // Pack the actual response
Jason M. Bills13e67c82018-09-10 14:12:16 -0700580 std::memcpy(response, &selResID, *data_len);
Chris Austenb4f5b922015-10-13 12:44:43 -0500581
582 return rc;
583}
584
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500585ipmi_ret_t ipmi_storage_add_sel(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700586 ipmi_request_t request,
587 ipmi_response_t response,
588 ipmi_data_len_t data_len,
589 ipmi_context_t context)
Chris Austenb4f5b922015-10-13 12:44:43 -0500590{
Jason M. Bills851acb12019-02-04 14:06:57 -0800591 if (*data_len != sizeof(ipmi_add_sel_request_t))
592 {
593 *data_len = 0;
594 return IPMI_CC_REQ_DATA_LEN_INVALID;
595 }
Chris Austenb4f5b922015-10-13 12:44:43 -0500596
597 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -0700598 ipmi_add_sel_request_t* p = (ipmi_add_sel_request_t*)request;
Chris Austen41a4b312015-10-25 03:45:42 -0500599 uint16_t recordid;
Chris Austenb4f5b922015-10-13 12:44:43 -0500600
Jason M. Bills13e67c82018-09-10 14:12:16 -0700601 // Per the IPMI spec, need to cancel the reservation when a SEL entry is
602 // added
603 cancelSELReservation();
604
Chris Austen313d95b2015-10-31 12:55:30 -0500605 recordid = ((uint16_t)p->eventdata[1] << 8) | p->eventdata[2];
Chris Austen41a4b312015-10-25 03:45:42 -0500606
Jason M. Bills13e67c82018-09-10 14:12:16 -0700607 *data_len = sizeof(recordid);
Chris Austenb4f5b922015-10-13 12:44:43 -0500608
609 // Pack the actual response
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700610 std::memcpy(response, &p->eventdata[1], 2);
Chris Austenb4f5b922015-10-13 12:44:43 -0500611
Tom Josephb647d5b2017-10-31 17:25:33 +0530612 // Hostboot sends SEL with OEM record type 0xDE to indicate that there is
613 // a maintenance procedure associated with eSEL record.
614 static constexpr auto procedureType = 0xDE;
615 if (p->recordtype == procedureType)
616 {
617 // In the OEM record type 0xDE, byte 11 in the SEL record indicate the
618 // procedure number.
619 createProcedureLogEntry(p->sensortype);
620 }
Chris Austenb4f5b922015-10-13 12:44:43 -0500621
622 return rc;
623}
624
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000625/** @brief implements the get FRU Inventory Area Info command
626 *
627 * @returns IPMI completion code plus response data
628 * - FRU Inventory area size in bytes,
629 * - access bit
630 **/
631ipmi::RspType<uint16_t, // FRU Inventory area size in bytes,
632 uint8_t // access size (bytes / words)
633 >
634 ipmiStorageGetFruInvAreaInfo(uint8_t fruID)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500635{
Tom Joseph187f5642018-03-29 13:49:06 +0530636
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000637 auto iter = frus.find(fruID);
Tom Joseph187f5642018-03-29 13:49:06 +0530638 if (iter == frus.end())
639 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000640 return ipmi::responseSensorInvalid();
Tom Joseph187f5642018-03-29 13:49:06 +0530641 }
642
Marri Devender Raocac383b2017-07-03 13:24:27 -0500643 try
644 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000645 return ipmi::responseSuccess(
646 static_cast<uint16_t>(getFruAreaData(fruID).size()),
647 static_cast<uint8_t>(AccessMode::bytes));
Marri Devender Raocac383b2017-07-03 13:24:27 -0500648 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700649 catch (const InternalFailure& e)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500650 {
Marri Devender Raocac383b2017-07-03 13:24:27 -0500651 log<level::ERR>(e.what());
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000652 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500653 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500654}
655
Patrick Venture0b02be92018-08-31 11:55:55 -0700656// Read FRU data
657ipmi_ret_t ipmi_storage_read_fru_data(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
658 ipmi_request_t request,
659 ipmi_response_t response,
660 ipmi_data_len_t data_len,
661 ipmi_context_t context)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500662{
663 ipmi_ret_t rc = IPMI_CC_OK;
Marri Devender Raocac383b2017-07-03 13:24:27 -0500664 const ReadFruDataRequest* reqptr =
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500665 reinterpret_cast<const ReadFruDataRequest*>(request);
Patrick Venture0b02be92018-08-31 11:55:55 -0700666 auto resptr = reinterpret_cast<ReadFruDataResponse*>(response);
Tom Joseph187f5642018-03-29 13:49:06 +0530667
668 auto iter = frus.find(reqptr->fruID);
669 if (iter == frus.end())
670 {
671 *data_len = 0;
672 return IPMI_CC_SENSOR_INVALID;
673 }
674
Marri Devender Raocac383b2017-07-03 13:24:27 -0500675 auto offset =
676 static_cast<uint16_t>(reqptr->offsetMS << 8 | reqptr->offsetLS);
677 try
678 {
679 const auto& fruArea = getFruAreaData(reqptr->fruID);
680 auto size = fruArea.size();
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500681
Tom Josephefcd68b2018-04-26 18:46:27 +0530682 if (offset >= size)
683 {
684 return IPMI_CC_PARM_OUT_OF_RANGE;
685 }
686
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500687 // Write the count of response data.
688 if ((offset + reqptr->count) <= size)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500689 {
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500690 resptr->count = reqptr->count;
691 }
692 else
693 {
694 resptr->count = size - offset;
Marri Devender Raocac383b2017-07-03 13:24:27 -0500695 }
Ratan Gupta2848d602018-01-31 20:39:20 +0530696
Ratan Gupta2848d602018-01-31 20:39:20 +0530697 std::copy((fruArea.begin() + offset),
Patrick Venture0b02be92018-08-31 11:55:55 -0700698 (fruArea.begin() + offset + resptr->count), resptr->data);
Ratan Gupta2848d602018-01-31 20:39:20 +0530699
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500700 *data_len = resptr->count + 1; // additional one byte for count
Marri Devender Raocac383b2017-07-03 13:24:27 -0500701 }
702 catch (const InternalFailure& e)
703 {
704 rc = IPMI_CC_UNSPECIFIED_ERROR;
705 *data_len = 0;
706 log<level::ERR>(e.what());
Marri Devender Raocac383b2017-07-03 13:24:27 -0500707 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500708 return rc;
709}
710
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600711ipmi_ret_t ipmi_get_repository_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700712 ipmi_request_t request,
713 ipmi_response_t response,
714 ipmi_data_len_t data_len,
715 ipmi_context_t context)
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600716{
717 constexpr auto sdrVersion = 0x51;
Patrick Venture0b02be92018-08-31 11:55:55 -0700718 auto responseData = reinterpret_cast<GetRepositoryInfoResponse*>(response);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600719
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700720 std::memset(responseData, 0, sizeof(GetRepositoryInfoResponse));
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600721
722 responseData->sdrVersion = sdrVersion;
723
724 uint16_t records = frus.size() + sensors.size();
725 responseData->recordCountMs = records >> 8;
726 responseData->recordCountLs = records;
727
728 responseData->freeSpace[0] = 0xFF;
729 responseData->freeSpace[1] = 0xFF;
730
731 *data_len = sizeof(GetRepositoryInfoResponse);
732
733 return IPMI_CC_OK;
734}
Chris Austenb4f5b922015-10-13 12:44:43 -0500735
Chris Austenb4f5b922015-10-13 12:44:43 -0500736void register_netfn_storage_functions()
737{
Tom05732372016-09-06 17:21:23 +0530738 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -0700739 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_WILDCARD, NULL,
740 ipmi_storage_wildcard, PRIVILEGE_USER);
Chris Austenb4f5b922015-10-13 12:44:43 -0500741
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530742 // <Get SEL Info>
Patrick Venture0b02be92018-08-31 11:55:55 -0700743 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_INFO, NULL,
744 getSELInfo, PRIVILEGE_USER);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530745
Tom05732372016-09-06 17:21:23 +0530746 // <Get SEL Time>
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000747 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
748 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
749 ipmiStorageGetSelTime);
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500750
Tom05732372016-09-06 17:21:23 +0530751 // <Set SEL Time>
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000752 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
753 ipmi::storage::cmdSetSelTime,
754 ipmi::Privilege::Operator, ipmiStorageSetSelTime);
Chris Austenb4f5b922015-10-13 12:44:43 -0500755
Tom05732372016-09-06 17:21:23 +0530756 // <Reserve SEL>
Patrick Venture0b02be92018-08-31 11:55:55 -0700757 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_RESERVE_SEL, NULL,
758 ipmi_storage_reserve_sel, PRIVILEGE_USER);
Chris Austenb4f5b922015-10-13 12:44:43 -0500759
Tom Josepha4953392017-06-30 19:09:47 +0530760 // <Get SEL Entry>
Patrick Venture0b02be92018-08-31 11:55:55 -0700761 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_ENTRY, NULL,
762 getSELEntry, PRIVILEGE_USER);
Tom Josepha4953392017-06-30 19:09:47 +0530763
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530764 // <Delete SEL Entry>
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000765 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
766 ipmi::storage::cmdDeleteSelEntry,
767 ipmi::Privilege::Operator, deleteSELEntry);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530768
Tom05732372016-09-06 17:21:23 +0530769 // <Add SEL Entry>
Patrick Venture0b02be92018-08-31 11:55:55 -0700770 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_ADD_SEL, NULL,
771 ipmi_storage_add_sel, PRIVILEGE_OPERATOR);
Tom Joseph2f05bb52017-06-30 19:14:49 +0530772 // <Clear SEL>
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000773 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
774 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
775 clearSEL);
776
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500777 // <Get FRU Inventory Area Info>
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000778 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
779 ipmi::storage::cmdGetFruInventoryAreaInfo,
780 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500781
782 // <Add READ FRU Data
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500783 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_READ_FRU_DATA, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -0700784 ipmi_storage_read_fru_data, PRIVILEGE_OPERATOR);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500785
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600786 // <Get Repository Info>
787 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_REPOSITORY_INFO,
Patrick Venture0b02be92018-08-31 11:55:55 -0700788 nullptr, ipmi_get_repository_info, PRIVILEGE_USER);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600789
Tom Joseph5ca50952018-02-22 00:33:38 +0530790 // <Reserve SDR Repository>
Patrick Venture0b02be92018-08-31 11:55:55 -0700791 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_RESERVE_SDR, nullptr,
792 ipmi_sen_reserve_sdr, PRIVILEGE_USER);
Tom Joseph5ca50952018-02-22 00:33:38 +0530793
794 // <Get SDR>
Patrick Venture0b02be92018-08-31 11:55:55 -0700795 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SDR, nullptr,
796 ipmi_sen_get_sdr, PRIVILEGE_USER);
Tom Joseph5ca50952018-02-22 00:33:38 +0530797
Marri Devender Rao908f7502017-07-10 01:49:54 -0500798 ipmi::fru::registerCallbackHandler();
Chris Austenb4f5b922015-10-13 12:44:43 -0500799 return;
800}