blob: 0a7ac1cd76d091ba94356da985a263349d91c54e [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
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050013#include <ipmid/api.hpp>
Vernon Mauery9cf08382023-04-28 14:00:11 -070014#include <ipmid/entity_map_json.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050015#include <ipmid/utils.hpp>
16#include <phosphor-logging/elog-errors.hpp>
17#include <phosphor-logging/elog.hpp>
18#include <phosphor-logging/log.hpp>
19#include <sdbusplus/server.hpp>
20#include <xyz/openbmc_project/Common/error.hpp>
21#include <xyz/openbmc_project/Logging/SEL/error.hpp>
22
Patrick Venture0b02be92018-08-31 11:55:55 -070023#include <algorithm>
Lei YU52d91242017-10-17 22:52:28 +080024#include <chrono>
25#include <cstdio>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070026#include <cstring>
Vernon Mauerybdda8002019-02-26 10:18:51 -080027#include <filesystem>
Lei YUa0bb2a32021-09-24 11:03:56 +080028#include <optional>
Patrick Venture3a5071a2018-09-12 13:27:42 -070029#include <string>
Vernon Mauery16b86932019-05-01 08:36:11 -070030#include <variant>
Patrick Venture3a5071a2018-09-12 13:27:42 -070031
Chris Austenb4f5b922015-10-13 12:44:43 -050032void register_netfn_storage_functions() __attribute__((constructor));
33
Patrick Venture0b02be92018-08-31 11:55:55 -070034unsigned int g_sel_time = 0xFFFFFFFF;
Patrick Venturedb0cbe62019-09-09 14:47:22 -070035namespace ipmi
36{
37namespace sensor
38{
39extern const IdInfoMap sensors;
40} // namespace sensor
41} // namespace ipmi
Lotus Xu57d35572021-01-24 11:13:13 +080042extern const ipmi::sensor::InvObjectIDMap invSensors;
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -060043extern const FruMap frus;
anil kumar appana2c7db1d2019-05-28 11:20:19 +000044constexpr uint8_t eventDataSize = 3;
Patrick Venture0b02be92018-08-31 11:55:55 -070045namespace
46{
shamim ali071d00c2022-03-02 15:42:46 +053047constexpr auto SystemdTimeService = "org.freedesktop.timedate1";
48constexpr auto SystemdTimePath = "/org/freedesktop/timedate1";
49constexpr auto SystemdTimeInterface = "org.freedesktop.timedate1";
50
Lei YUe8939392017-06-15 10:45:05 +080051constexpr auto TIME_INTERFACE = "xyz.openbmc_project.Time.EpochTime";
George Liu4d623e92020-05-25 16:51:57 +080052constexpr auto BMC_TIME_PATH = "/xyz/openbmc_project/time/bmc";
Lei YUe8939392017-06-15 10:45:05 +080053constexpr auto DBUS_PROPERTIES = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070054constexpr auto PROPERTY_ELAPSED = "Elapsed";
Lei YUe8939392017-06-15 10:45:05 +080055
Lei YUd9555252021-09-14 20:30:41 +080056constexpr auto logWatchPath = "/xyz/openbmc_project/logging";
Lei YUd9e57662021-09-14 18:06:28 +080057constexpr auto logBasePath = "/xyz/openbmc_project/logging/entry";
58constexpr auto logEntryIntf = "xyz.openbmc_project.Logging.Entry";
59constexpr auto logDeleteIntf = "xyz.openbmc_project.Object.Delete";
Patrick Venture0b02be92018-08-31 11:55:55 -070060} // namespace
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +053061
Tom Joseph6f7deaa2017-06-30 19:03:54 +053062using InternalFailure =
Patrick Venture0b02be92018-08-31 11:55:55 -070063 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Tom Joseph6f7deaa2017-06-30 19:03:54 +053064using namespace phosphor::logging;
Marri Devender Raocac383b2017-07-03 13:24:27 -050065using namespace ipmi::fru;
Lotus Xu57d35572021-01-24 11:13:13 +080066using namespace xyz::openbmc_project::Logging::SEL;
67using SELCreated =
68 sdbusplus::xyz::openbmc_project::Logging::SEL::Error::Created;
Marri Devender Raocac383b2017-07-03 13:24:27 -050069
Lei YUd9e57662021-09-14 18:06:28 +080070using SELRecordID = uint16_t;
71using SELEntry = ipmi::sel::SELEventRecordFormat;
72using SELCacheMap = std::map<SELRecordID, SELEntry>;
73
74SELCacheMap selCacheMap __attribute__((init_priority(101)));
Lei YU3df36612021-09-15 11:41:11 +080075bool selCacheMapInitialized;
Patrick Williams5d82f472022-07-22 19:26:53 -050076std::unique_ptr<sdbusplus::bus::match_t> selAddedMatch
Lei YUd9555252021-09-14 20:30:41 +080077 __attribute__((init_priority(101)));
Patrick Williams5d82f472022-07-22 19:26:53 -050078std::unique_ptr<sdbusplus::bus::match_t> selRemovedMatch
Lei YUb6b72b02021-09-14 21:06:05 +080079 __attribute__((init_priority(101)));
Patrick Williams5d82f472022-07-22 19:26:53 -050080std::unique_ptr<sdbusplus::bus::match_t> selUpdatedMatch
Lei YU4106e422021-09-15 10:55:51 +080081 __attribute__((init_priority(101)));
Lei YUd9e57662021-09-14 18:06:28 +080082
Lei YUb6b72b02021-09-14 21:06:05 +080083static inline uint16_t getLoggingId(const std::string& p)
Lei YUd9e57662021-09-14 18:06:28 +080084{
85 namespace fs = std::filesystem;
86 fs::path entryPath(p);
Lei YUb6b72b02021-09-14 21:06:05 +080087 return std::stoul(entryPath.filename().string());
88}
89
Lei YUeba8e9a2021-09-15 13:16:17 +080090static inline std::string getLoggingObjPath(uint16_t id)
91{
92 return std::string(ipmi::sel::logBasePath) + "/" + std::to_string(id);
93}
94
Lei YUa0bb2a32021-09-24 11:03:56 +080095std::optional<std::pair<uint16_t, SELEntry>>
96 parseLoggingEntry(const std::string& p)
Lei YUb6b72b02021-09-14 21:06:05 +080097{
Lei YU3df36612021-09-15 11:41:11 +080098 try
99 {
Lei YUa0bb2a32021-09-24 11:03:56 +0800100 auto id = getLoggingId(p);
101 ipmi::sel::GetSELEntryResponse record{};
Lei YU3df36612021-09-15 11:41:11 +0800102 record = ipmi::sel::convertLogEntrytoSEL(p);
Lei YUa0bb2a32021-09-24 11:03:56 +0800103 return std::pair<uint16_t, SELEntry>({id, std::move(record.event)});
Lei YU3df36612021-09-15 11:41:11 +0800104 }
105 catch (const std::exception& e)
106 {
107 fprintf(stderr, "Failed to convert %s to SEL: %s\n", p.c_str(),
108 e.what());
109 }
Lei YUa0bb2a32021-09-24 11:03:56 +0800110 return std::nullopt;
Lei YUd9e57662021-09-14 18:06:28 +0800111}
112
Patrick Williams5d82f472022-07-22 19:26:53 -0500113static void selAddedCallback(sdbusplus::message_t& m)
Lei YUd9555252021-09-14 20:30:41 +0800114{
115 sdbusplus::message::object_path objPath;
116 try
117 {
118 m.read(objPath);
119 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500120 catch (const sdbusplus::exception_t& e)
Lei YUd9555252021-09-14 20:30:41 +0800121 {
122 log<level::ERR>("Failed to read object path");
123 return;
124 }
125 std::string p = objPath;
Lei YUa0bb2a32021-09-24 11:03:56 +0800126 auto entry = parseLoggingEntry(p);
127 if (entry)
128 {
129 selCacheMap.insert(std::move(*entry));
130 }
Lei YUd9555252021-09-14 20:30:41 +0800131}
132
Patrick Williams5d82f472022-07-22 19:26:53 -0500133static void selRemovedCallback(sdbusplus::message_t& m)
Lei YUb6b72b02021-09-14 21:06:05 +0800134{
135 sdbusplus::message::object_path objPath;
136 try
137 {
138 m.read(objPath);
139 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500140 catch (const sdbusplus::exception_t& e)
Lei YUb6b72b02021-09-14 21:06:05 +0800141 {
142 log<level::ERR>("Failed to read object path");
Lei YUb6b72b02021-09-14 21:06:05 +0800143 }
Lei YUa0bb2a32021-09-24 11:03:56 +0800144 try
145 {
146 std::string p = objPath;
147 selCacheMap.erase(getLoggingId(p));
148 }
149 catch (const std::invalid_argument& e)
150 {
151 log<level::ERR>("Invalid logging entry ID");
152 }
Lei YUb6b72b02021-09-14 21:06:05 +0800153}
154
Patrick Williams5d82f472022-07-22 19:26:53 -0500155static void selUpdatedCallback(sdbusplus::message_t& m)
Lei YU4106e422021-09-15 10:55:51 +0800156{
157 std::string p = m.get_path();
158 auto entry = parseLoggingEntry(p);
Lei YUa0bb2a32021-09-24 11:03:56 +0800159 if (entry)
160 {
161 selCacheMap.insert_or_assign(entry->first, std::move(entry->second));
162 }
Lei YU4106e422021-09-15 10:55:51 +0800163}
164
Lei YUd9555252021-09-14 20:30:41 +0800165void registerSelCallbackHandler()
166{
167 using namespace sdbusplus::bus::match::rules;
Patrick Williams5d82f472022-07-22 19:26:53 -0500168 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Lei YUd9555252021-09-14 20:30:41 +0800169 if (!selAddedMatch)
170 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500171 selAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
Lei YUd9555252021-09-14 20:30:41 +0800172 bus, interfacesAdded(logWatchPath),
173 std::bind(selAddedCallback, std::placeholders::_1));
174 }
Lei YUb6b72b02021-09-14 21:06:05 +0800175 if (!selRemovedMatch)
176 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500177 selRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
Lei YUb6b72b02021-09-14 21:06:05 +0800178 bus, interfacesRemoved(logWatchPath),
179 std::bind(selRemovedCallback, std::placeholders::_1));
180 }
Lei YU4106e422021-09-15 10:55:51 +0800181 if (!selUpdatedMatch)
182 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500183 selUpdatedMatch = std::make_unique<sdbusplus::bus::match_t>(
Lei YU4106e422021-09-15 10:55:51 +0800184 bus,
185 type::signal() + member("PropertiesChanged"s) +
186 interface("org.freedesktop.DBus.Properties"s) +
187 argN(0, logEntryIntf),
188 std::bind(selUpdatedCallback, std::placeholders::_1));
189 }
Lei YUd9555252021-09-14 20:30:41 +0800190}
191
Lei YUd9e57662021-09-14 18:06:28 +0800192void initSELCache()
193{
Lei YUc10cda12022-01-06 16:01:57 +0800194 registerSelCallbackHandler();
Lei YUeba8e9a2021-09-15 13:16:17 +0800195 ipmi::sel::ObjectPaths paths;
Lei YUd9e57662021-09-14 18:06:28 +0800196 try
197 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800198 ipmi::sel::readLoggingObjectPaths(paths);
Lei YUd9e57662021-09-14 18:06:28 +0800199 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500200 catch (const sdbusplus::exception_t& e)
Lei YUd9e57662021-09-14 18:06:28 +0800201 {
202 log<level::ERR>("Failed to get logging object paths");
203 return;
204 }
Lei YUeba8e9a2021-09-15 13:16:17 +0800205 for (const auto& p : paths)
Lei YUd9e57662021-09-14 18:06:28 +0800206 {
Lei YUa0bb2a32021-09-24 11:03:56 +0800207 auto entry = parseLoggingEntry(p);
208 if (entry)
209 {
210 selCacheMap.insert(std::move(*entry));
211 }
Lei YUd9e57662021-09-14 18:06:28 +0800212 }
Lei YU3df36612021-09-15 11:41:11 +0800213 selCacheMapInitialized = true;
Lei YUd9e57662021-09-14 18:06:28 +0800214}
215
Marri Devender Raocac383b2017-07-03 13:24:27 -0500216/**
217 * @enum Device access mode
218 */
219enum class AccessMode
220{
221 bytes, ///< Device is accessed by bytes
222 words ///< Device is accessed by words
223};
224
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000225/** @brief implements the get SEL Info command
226 * @returns IPMI completion code plus response data
227 * - selVersion - SEL revision
228 * - entries - Number of log entries in SEL.
229 * - freeSpace - Free Space in bytes.
230 * - addTimeStamp - Most recent addition timestamp
231 * - eraseTimeStamp - Most recent erase timestamp
232 * - operationSupport - Reserve & Delete SEL operations supported
233 */
234
235ipmi::RspType<uint8_t, // SEL revision.
236 uint16_t, // number of log entries in SEL.
237 uint16_t, // free Space in bytes.
238 uint32_t, // most recent addition timestamp
239 uint32_t, // most recent erase timestamp.
240
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500241 bool, // SEL allocation info supported
242 bool, // reserve SEL supported
243 bool, // partial Add SEL Entry supported
244 bool, // delete SEL supported
245 uint3_t, // reserved
246 bool // overflow flag
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000247 >
248 ipmiStorageGetSelInfo()
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530249{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000250 uint16_t entries = 0;
251 // Most recent addition timestamp.
252 uint32_t addTimeStamp = ipmi::sel::invalidTimeStamp;
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530253
Lei YUeba8e9a2021-09-15 13:16:17 +0800254 if (!selCacheMapInitialized)
Tom Josephe59abfb2018-08-06 18:46:27 +0530255 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800256 // In case the initSELCache() fails, try it again
257 initSELCache();
Tom Josephe59abfb2018-08-06 18:46:27 +0530258 }
Lei YUeba8e9a2021-09-15 13:16:17 +0800259 if (!selCacheMap.empty())
Tom Josephe59abfb2018-08-06 18:46:27 +0530260 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800261 entries = static_cast<uint16_t>(selCacheMap.size());
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530262
263 try
264 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800265 auto objPath = getLoggingObjPath(selCacheMap.rbegin()->first);
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000266 addTimeStamp = static_cast<uint32_t>(
Lei YUeba8e9a2021-09-15 13:16:17 +0800267 (ipmi::sel::getEntryTimeStamp(objPath).count()));
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530268 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500269 catch (const InternalFailure& e)
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500270 {}
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530271 catch (const std::runtime_error& e)
272 {
273 log<level::ERR>(e.what());
274 }
275 }
276
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000277 constexpr uint8_t selVersion = ipmi::sel::selVersion;
278 constexpr uint16_t freeSpace = 0xFFFF;
279 constexpr uint32_t eraseTimeStamp = ipmi::sel::invalidTimeStamp;
280 constexpr uint3_t reserved{0};
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530281
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000282 return ipmi::responseSuccess(
283 selVersion, entries, freeSpace, addTimeStamp, eraseTimeStamp,
284 ipmi::sel::operationSupport::getSelAllocationInfo,
285 ipmi::sel::operationSupport::reserveSel,
286 ipmi::sel::operationSupport::partialAddSelEntry,
287 ipmi::sel::operationSupport::deleteSel, reserved,
288 ipmi::sel::operationSupport::overflow);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530289}
290
Willy Tu11d68892022-01-20 10:37:34 -0800291ipmi_ret_t getSELEntry(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request,
292 ipmi_response_t response, ipmi_data_len_t data_len,
293 ipmi_context_t)
Tom Josepha4953392017-06-30 19:09:47 +0530294{
Jason M. Bills851acb12019-02-04 14:06:57 -0800295 if (*data_len != sizeof(ipmi::sel::GetSELEntryRequest))
296 {
297 *data_len = 0;
298 return IPMI_CC_REQ_DATA_LEN_INVALID;
299 }
300
Patrick Venture0b02be92018-08-31 11:55:55 -0700301 auto requestData =
302 reinterpret_cast<const ipmi::sel::GetSELEntryRequest*>(request);
Tom Josepha4953392017-06-30 19:09:47 +0530303
304 if (requestData->reservationID != 0)
305 {
Jason M. Bills13e67c82018-09-10 14:12:16 -0700306 if (!checkSELReservation(requestData->reservationID))
Tom Josepha4953392017-06-30 19:09:47 +0530307 {
308 *data_len = 0;
309 return IPMI_CC_INVALID_RESERVATION_ID;
310 }
311 }
312
Lei YU3df36612021-09-15 11:41:11 +0800313 if (!selCacheMapInitialized)
314 {
315 // In case the initSELCache() fails, try it again
316 initSELCache();
317 }
318
319 if (selCacheMap.empty())
Tom Josepha4953392017-06-30 19:09:47 +0530320 {
321 *data_len = 0;
322 return IPMI_CC_SENSOR_INVALID;
323 }
324
Lei YU3df36612021-09-15 11:41:11 +0800325 SELCacheMap::const_iterator iter;
Tom Josepha4953392017-06-30 19:09:47 +0530326
327 // Check for the requested SEL Entry.
328 if (requestData->selRecordID == ipmi::sel::firstEntry)
329 {
Lei YU3df36612021-09-15 11:41:11 +0800330 iter = selCacheMap.begin();
Tom Josepha4953392017-06-30 19:09:47 +0530331 }
332 else if (requestData->selRecordID == ipmi::sel::lastEntry)
333 {
Lei YU3df36612021-09-15 11:41:11 +0800334 if (selCacheMap.size() > 1)
335 {
336 iter = selCacheMap.end();
337 --iter;
338 }
339 else
340 {
341 // Only one entry exists, return the first
342 iter = selCacheMap.begin();
343 }
Tom Josepha4953392017-06-30 19:09:47 +0530344 }
345 else
346 {
Lei YU3df36612021-09-15 11:41:11 +0800347 iter = selCacheMap.find(requestData->selRecordID);
348 if (iter == selCacheMap.end())
Tom Josepha4953392017-06-30 19:09:47 +0530349 {
350 *data_len = 0;
351 return IPMI_CC_SENSOR_INVALID;
352 }
353 }
354
Lei YU3df36612021-09-15 11:41:11 +0800355 ipmi::sel::GetSELEntryResponse record{0, iter->second};
Tom Josepha4953392017-06-30 19:09:47 +0530356 // Identify the next SEL record ID
Lei YU3df36612021-09-15 11:41:11 +0800357 ++iter;
358 if (iter == selCacheMap.end())
Tom Josepha4953392017-06-30 19:09:47 +0530359 {
Lei YU3df36612021-09-15 11:41:11 +0800360 record.nextRecordID = ipmi::sel::lastEntry;
Tom Josepha4953392017-06-30 19:09:47 +0530361 }
362 else
363 {
Lei YU3df36612021-09-15 11:41:11 +0800364 record.nextRecordID = iter->first;
Tom Josepha4953392017-06-30 19:09:47 +0530365 }
366
367 if (requestData->readLength == ipmi::sel::entireRecord)
368 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700369 std::memcpy(response, &record, sizeof(record));
Tom Josepha4953392017-06-30 19:09:47 +0530370 *data_len = sizeof(record);
371 }
372 else
373 {
374 if (requestData->offset >= ipmi::sel::selRecordSize ||
375 requestData->readLength > ipmi::sel::selRecordSize)
376 {
377 *data_len = 0;
378 return IPMI_CC_INVALID_FIELD_REQUEST;
379 }
380
381 auto diff = ipmi::sel::selRecordSize - requestData->offset;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500382 auto readLength = std::min(diff,
383 static_cast<int>(requestData->readLength));
Tom Josepha4953392017-06-30 19:09:47 +0530384
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700385 std::memcpy(response, &record.nextRecordID,
386 sizeof(record.nextRecordID));
387 std::memcpy(static_cast<uint8_t*>(response) +
388 sizeof(record.nextRecordID),
Lei YUaf378fa2020-12-02 16:28:57 +0800389 &record.event.eventRecord.recordID + requestData->offset,
390 readLength);
Tom Josepha4953392017-06-30 19:09:47 +0530391 *data_len = sizeof(record.nextRecordID) + readLength;
392 }
393
394 return IPMI_CC_OK;
395}
396
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000397/** @brief implements the delete SEL entry command
398 * @request
399 * - reservationID; // reservation ID.
400 * - selRecordID; // SEL record ID.
401 *
402 * @returns ipmi completion code plus response data
403 * - Record ID of the deleted record
404 */
405ipmi::RspType<uint16_t // deleted record ID
406 >
407 deleteSELEntry(uint16_t reservationID, uint16_t selRecordID)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530408{
Brad Bishop1a4117b2018-11-21 15:48:18 -0500409 namespace fs = std::filesystem;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530410
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000411 if (!checkSELReservation(reservationID))
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530412 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000413 return ipmi::responseInvalidReservationId();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530414 }
415
Jason M. Bills13e67c82018-09-10 14:12:16 -0700416 // Per the IPMI spec, need to cancel the reservation when a SEL entry is
417 // deleted
418 cancelSELReservation();
419
Lei YUeba8e9a2021-09-15 13:16:17 +0800420 if (!selCacheMapInitialized)
Tom Josephe59abfb2018-08-06 18:46:27 +0530421 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800422 // In case the initSELCache() fails, try it again
423 initSELCache();
Tom Josephe59abfb2018-08-06 18:46:27 +0530424 }
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530425
Lei YUeba8e9a2021-09-15 13:16:17 +0800426 if (selCacheMap.empty())
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530427 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000428 return ipmi::responseSensorInvalid();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530429 }
430
Lei YUeba8e9a2021-09-15 13:16:17 +0800431 SELCacheMap::const_iterator iter;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530432 uint16_t delRecordID = 0;
433
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000434 if (selRecordID == ipmi::sel::firstEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530435 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800436 delRecordID = selCacheMap.begin()->first;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530437 }
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000438 else if (selRecordID == ipmi::sel::lastEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530439 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800440 delRecordID = selCacheMap.rbegin()->first;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530441 }
442 else
443 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000444 delRecordID = selRecordID;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530445 }
446
Jian7d5f4812022-02-23 19:30:27 +0800447 iter = selCacheMap.find(delRecordID);
448 if (iter == selCacheMap.end())
449 {
450 return ipmi::responseSensorInvalid();
451 }
452
Patrick Williams5d82f472022-07-22 19:26:53 -0500453 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530454 std::string service;
455
Lei YUeba8e9a2021-09-15 13:16:17 +0800456 auto objPath = getLoggingObjPath(iter->first);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530457 try
458 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800459 service = ipmi::getService(bus, ipmi::sel::logDeleteIntf, objPath);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530460 }
461 catch (const std::runtime_error& e)
462 {
463 log<level::ERR>(e.what());
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000464 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530465 }
466
Lei YUeba8e9a2021-09-15 13:16:17 +0800467 auto methodCall = bus.new_method_call(service.c_str(), objPath.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700468 ipmi::sel::logDeleteIntf, "Delete");
George Liu3e3cc352023-07-26 15:59:31 +0800469 try
470 {
471 auto reply = bus.call(methodCall);
472 }
473 catch (const std::exception& e)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530474 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000475 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530476 }
477
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000478 return ipmi::responseSuccess(delRecordID);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530479}
480
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000481/** @brief implements the Clear SEL command
482 * @request
483 * - reservationID // Reservation ID.
484 * - clr // char array { 'C'(0x43h), 'L'(0x4Ch), 'R'(0x52h) }
485 * - eraseOperation; // requested operation.
486 *
487 * @returns ipmi completion code plus response data
488 * - erase status
489 */
490
491ipmi::RspType<uint8_t // erase status
492 >
493 clearSEL(uint16_t reservationID, const std::array<char, 3>& clr,
494 uint8_t eraseOperation)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530495{
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000496 static constexpr std::array<char, 3> clrOk = {'C', 'L', 'R'};
497 if (clr != clrOk)
Jason M. Bills851acb12019-02-04 14:06:57 -0800498 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000499 return ipmi::responseInvalidFieldRequest();
Jason M. Bills851acb12019-02-04 14:06:57 -0800500 }
501
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000502 if (!checkSELReservation(reservationID))
Tom Joseph2f05bb52017-06-30 19:14:49 +0530503 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000504 return ipmi::responseInvalidReservationId();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530505 }
506
Tom Joseph2f05bb52017-06-30 19:14:49 +0530507 /*
508 * Erasure status cannot be fetched from DBUS, so always return erasure
509 * status as `erase completed`.
510 */
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000511 if (eraseOperation == ipmi::sel::getEraseStatus)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530512 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000513 return ipmi::responseSuccess(
514 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530515 }
516
Jason M. Bills13e67c82018-09-10 14:12:16 -0700517 // Per the IPMI spec, need to cancel any reservation when the SEL is cleared
518 cancelSELReservation();
519
Patrick Williams5d82f472022-07-22 19:26:53 -0500520 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Lotus Xu78fe1b12021-05-23 17:26:56 +0800521 auto service = ipmi::getService(bus, ipmi::sel::logIntf, ipmi::sel::logObj);
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500522 auto method = bus.new_method_call(service.c_str(), ipmi::sel::logObj,
523 ipmi::sel::logIntf,
524 ipmi::sel::logDeleteAllMethod);
Tom Josephe59abfb2018-08-06 18:46:27 +0530525 try
Tom Joseph2f05bb52017-06-30 19:14:49 +0530526 {
Lotus Xu78fe1b12021-05-23 17:26:56 +0800527 bus.call_noreply(method);
Tom Josephe59abfb2018-08-06 18:46:27 +0530528 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500529 catch (const sdbusplus::exception_t& e)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530530 {
Lotus Xu78fe1b12021-05-23 17:26:56 +0800531 log<level::ERR>("Error eraseAll ", entry("ERROR=%s", e.what()));
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000532 return ipmi::responseUnspecifiedError();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530533 }
534
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000535 return ipmi::responseSuccess(
536 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530537}
538
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000539/** @brief implements the get SEL time command
540 * @returns IPMI completion code plus response data
541 * -current time
542 */
543ipmi::RspType<uint32_t> // current time
544 ipmiStorageGetSelTime()
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500545{
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530546 using namespace std::chrono;
George Liu4d623e92020-05-25 16:51:57 +0800547 uint64_t bmc_time_usec = 0;
548 std::stringstream bmcTime;
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500549
Lei YUe8939392017-06-15 10:45:05 +0800550 try
551 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500552 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
George Liu4d623e92020-05-25 16:51:57 +0800553 auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
Vernon Mauery16b86932019-05-01 08:36:11 -0700554 std::variant<uint64_t> value;
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530555
George Liu4d623e92020-05-25 16:51:57 +0800556 // Get bmc time
557 auto method = bus.new_method_call(service.c_str(), BMC_TIME_PATH,
Patrick Venture0b02be92018-08-31 11:55:55 -0700558 DBUS_PROPERTIES, "Get");
Lei YUe8939392017-06-15 10:45:05 +0800559
560 method.append(TIME_INTERFACE, PROPERTY_ELAPSED);
561 auto reply = bus.call(method);
Lei YUe8939392017-06-15 10:45:05 +0800562 reply.read(value);
George Liu4d623e92020-05-25 16:51:57 +0800563 bmc_time_usec = std::get<uint64_t>(value);
Lei YUe8939392017-06-15 10:45:05 +0800564 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500565 catch (const InternalFailure& e)
Lei YUe8939392017-06-15 10:45:05 +0800566 {
567 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000568 return ipmi::responseUnspecifiedError();
Lei YUe8939392017-06-15 10:45:05 +0800569 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530570 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800571 {
572 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000573 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530574 }
575
George Liu4d623e92020-05-25 16:51:57 +0800576 bmcTime << "BMC time:"
577 << duration_cast<seconds>(microseconds(bmc_time_usec)).count();
578 log<level::DEBUG>(bmcTime.str().c_str());
Nagaraju Goruganti8960b7c2018-04-29 22:38:40 -0500579
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530580 // Time is really long int but IPMI wants just uint32. This works okay until
581 // the number of seconds since 1970 overflows uint32 size.. Still a whole
582 // lot of time here to even think about that.
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000583 return ipmi::responseSuccess(
George Liu4d623e92020-05-25 16:51:57 +0800584 duration_cast<seconds>(microseconds(bmc_time_usec)).count());
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500585}
586
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000587/** @brief implements the set SEL time command
588 * @param selDeviceTime - epoch time
589 * -local time as the number of seconds from 00:00:00, January 1, 1970
590 * @returns IPMI completion code
591 */
592ipmi::RspType<> ipmiStorageSetSelTime(uint32_t selDeviceTime)
Chris Austenb4f5b922015-10-13 12:44:43 -0500593{
Lei YUe8939392017-06-15 10:45:05 +0800594 using namespace std::chrono;
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000595 microseconds usec{seconds(selDeviceTime)};
Norman James82330442015-11-19 16:53:26 -0600596
Lei YUe8939392017-06-15 10:45:05 +0800597 try
598 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500599 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
shamim ali071d00c2022-03-02 15:42:46 +0530600 bool ntp = std::get<bool>(
601 ipmi::getDbusProperty(bus, SystemdTimeService, SystemdTimePath,
602 SystemdTimeInterface, "NTP"));
603 if (ntp)
604 {
605 return ipmi::responseCommandNotAvailable();
606 }
607
George Liu4d623e92020-05-25 16:51:57 +0800608 auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
Andrew Geissler6467ed22020-05-16 16:03:53 -0500609 std::variant<uint64_t> value{(uint64_t)usec.count()};
Lei YUe8939392017-06-15 10:45:05 +0800610
George Liu4d623e92020-05-25 16:51:57 +0800611 // Set bmc time
612 auto method = bus.new_method_call(service.c_str(), BMC_TIME_PATH,
Patrick Venture0b02be92018-08-31 11:55:55 -0700613 DBUS_PROPERTIES, "Set");
Lei YUe8939392017-06-15 10:45:05 +0800614
615 method.append(TIME_INTERFACE, PROPERTY_ELAPSED, value);
616 auto reply = bus.call(method);
Norman James82330442015-11-19 16:53:26 -0600617 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500618 catch (const InternalFailure& e)
Lei YUe8939392017-06-15 10:45:05 +0800619 {
620 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000621 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530622 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530623 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800624 {
625 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000626 return ipmi::responseUnspecifiedError();
Norman James82330442015-11-19 16:53:26 -0600627 }
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530628
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000629 return ipmi::responseSuccess();
Chris Austenb4f5b922015-10-13 12:44:43 -0500630}
631
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000632/** @brief implements the reserve SEL command
633 * @returns IPMI completion code plus response data
634 * - SEL reservation ID.
635 */
636ipmi::RspType<uint16_t> ipmiStorageReserveSel()
Chris Austenb4f5b922015-10-13 12:44:43 -0500637{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000638 return ipmi::responseSuccess(reserveSel());
Chris Austenb4f5b922015-10-13 12:44:43 -0500639}
640
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000641/** @brief implements the Add SEL entry command
642 * @request
643 *
644 * - recordID ID used for SEL Record access
645 * - recordType Record Type
646 * - timeStamp Time when event was logged. LS byte first
647 * - generatorID software ID if event was generated from
648 * system software
649 * - evmRev event message format version
650 * - sensorType sensor type code for service that generated
651 * the event
652 * - sensorNumber number of sensors that generated the event
653 * - eventDir event dir
654 * - eventData event data field contents
655 *
656 * @returns ipmi completion code plus response data
657 * - RecordID of the Added SEL entry
658 */
659ipmi::RspType<uint16_t // recordID of the Added SEL entry
660 >
Willy Tu11d68892022-01-20 10:37:34 -0800661 ipmiStorageAddSEL(uint16_t recordID, uint8_t recordType,
662 [[maybe_unused]] uint32_t timeStamp, uint16_t generatorID,
663 [[maybe_unused]] uint8_t evmRev, uint8_t sensorType,
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000664 uint8_t sensorNumber, uint8_t eventDir,
665 std::array<uint8_t, eventDataSize> eventData)
Chris Austenb4f5b922015-10-13 12:44:43 -0500666{
Lotus Xu57d35572021-01-24 11:13:13 +0800667 std::string objpath;
668 static constexpr auto systemRecordType = 0x02;
Tom Josephb647d5b2017-10-31 17:25:33 +0530669 // Hostboot sends SEL with OEM record type 0xDE to indicate that there is
670 // a maintenance procedure associated with eSEL record.
671 static constexpr auto procedureType = 0xDE;
Lotus Xu57d35572021-01-24 11:13:13 +0800672 cancelSELReservation();
673 if (recordType == systemRecordType)
674 {
Lotus Xu57d35572021-01-24 11:13:13 +0800675 for (const auto& it : invSensors)
676 {
677 if (it.second.sensorID == sensorNumber)
678 {
679 objpath = it.first;
680 break;
681 }
682 }
683 auto selDataStr = ipmi::sel::toHexStr(eventData);
684
685 bool assert = (eventDir & 0x80) ? false : true;
686
687 recordID = report<SELCreated>(Created::RECORD_TYPE(recordType),
688 Created::GENERATOR_ID(generatorID),
689 Created::SENSOR_DATA(selDataStr.c_str()),
690 Created::EVENT_DIR(assert),
691 Created::SENSOR_PATH(objpath.c_str()));
692 }
693 else if (recordType == procedureType)
Tom Josephb647d5b2017-10-31 17:25:33 +0530694 {
695 // In the OEM record type 0xDE, byte 11 in the SEL record indicate the
696 // procedure number.
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000697 createProcedureLogEntry(sensorType);
Tom Josephb647d5b2017-10-31 17:25:33 +0530698 }
Chris Austenb4f5b922015-10-13 12:44:43 -0500699
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000700 return ipmi::responseSuccess(recordID);
Chris Austenb4f5b922015-10-13 12:44:43 -0500701}
702
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300703bool isFruPresent(ipmi::Context::ptr& ctx, const std::string& fruPath)
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300704{
705 using namespace ipmi::fru;
706
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300707 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500708 boost::system::error_code ec = getService(ctx, invItemInterface,
709 invObjPath + fruPath, service);
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300710 if (!ec)
711 {
712 bool result;
713 ec = ipmi::getDbusProperty(ctx, service, invObjPath + fruPath,
714 invItemInterface, itemPresentProp, result);
715 if (!ec)
716 {
717 return result;
718 }
719 }
Konstantin Aladyshevc7e4b042021-12-24 15:30:47 +0300720
721 ipmi::ObjectValueTree managedObjects;
Nan Zhou947da1b2022-09-20 20:40:59 +0000722 ec = getManagedObjects(ctx, "xyz.openbmc_project.EntityManager",
723 "/xyz/openbmc_project/inventory", managedObjects);
Konstantin Aladyshevc7e4b042021-12-24 15:30:47 +0300724 if (!ec)
725 {
726 auto connection = managedObjects.find(fruPath);
727 if (connection != managedObjects.end())
728 {
729 return true;
730 }
731 }
732
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300733 return false;
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300734}
735
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000736/** @brief implements the get FRU Inventory Area Info command
737 *
738 * @returns IPMI completion code plus response data
739 * - FRU Inventory area size in bytes,
740 * - access bit
741 **/
742ipmi::RspType<uint16_t, // FRU Inventory area size in bytes,
743 uint8_t // access size (bytes / words)
744 >
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300745 ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruID)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500746{
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000747 auto iter = frus.find(fruID);
Tom Joseph187f5642018-03-29 13:49:06 +0530748 if (iter == frus.end())
749 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000750 return ipmi::responseSensorInvalid();
Tom Joseph187f5642018-03-29 13:49:06 +0530751 }
752
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300753 auto path = iter->second[0].path;
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300754 if (!isFruPresent(ctx, path))
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300755 {
756 return ipmi::responseSensorInvalid();
757 }
758
Marri Devender Raocac383b2017-07-03 13:24:27 -0500759 try
760 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000761 return ipmi::responseSuccess(
762 static_cast<uint16_t>(getFruAreaData(fruID).size()),
763 static_cast<uint8_t>(AccessMode::bytes));
Marri Devender Raocac383b2017-07-03 13:24:27 -0500764 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700765 catch (const InternalFailure& e)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500766 {
Marri Devender Raocac383b2017-07-03 13:24:27 -0500767 log<level::ERR>(e.what());
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000768 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500769 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500770}
771
anil kumar appana5b7c3262019-05-27 18:10:23 +0000772/**@brief implements the Read FRU Data command
773 * @param fruDeviceId - FRU device ID. FFh = reserved
774 * @param offset - FRU inventory offset to read
775 * @param readCount - count to read
776 *
777 * @return IPMI completion code plus response data
778 * - returnCount - response data count.
779 * - data - response data
780 */
781ipmi::RspType<uint8_t, // count returned
782 std::vector<uint8_t>> // FRU data
783 ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t offset,
784 uint8_t readCount)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500785{
anil kumar appana5b7c3262019-05-27 18:10:23 +0000786 if (fruDeviceId == 0xFF)
Tom Joseph187f5642018-03-29 13:49:06 +0530787 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000788 return ipmi::responseInvalidFieldRequest();
Tom Joseph187f5642018-03-29 13:49:06 +0530789 }
790
anil kumar appana5b7c3262019-05-27 18:10:23 +0000791 auto iter = frus.find(fruDeviceId);
792 if (iter == frus.end())
793 {
794 return ipmi::responseSensorInvalid();
795 }
796
Marri Devender Raocac383b2017-07-03 13:24:27 -0500797 try
798 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000799 const auto& fruArea = getFruAreaData(fruDeviceId);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500800 auto size = fruArea.size();
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500801
Tom Josephefcd68b2018-04-26 18:46:27 +0530802 if (offset >= size)
803 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000804 return ipmi::responseParmOutOfRange();
Tom Josephefcd68b2018-04-26 18:46:27 +0530805 }
806
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500807 // Write the count of response data.
anil kumar appana5b7c3262019-05-27 18:10:23 +0000808 uint8_t returnCount;
809 if ((offset + readCount) <= size)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500810 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000811 returnCount = readCount;
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500812 }
813 else
814 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000815 returnCount = size - offset;
Marri Devender Raocac383b2017-07-03 13:24:27 -0500816 }
Ratan Gupta2848d602018-01-31 20:39:20 +0530817
anil kumar appana5b7c3262019-05-27 18:10:23 +0000818 std::vector<uint8_t> fruData((fruArea.begin() + offset),
819 (fruArea.begin() + offset + returnCount));
Ratan Gupta2848d602018-01-31 20:39:20 +0530820
anil kumar appana5b7c3262019-05-27 18:10:23 +0000821 return ipmi::responseSuccess(returnCount, fruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500822 }
823 catch (const InternalFailure& e)
824 {
Marri Devender Raocac383b2017-07-03 13:24:27 -0500825 log<level::ERR>(e.what());
anil kumar appana5b7c3262019-05-27 18:10:23 +0000826 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500827 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500828}
829
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000830ipmi::RspType<uint8_t, // SDR version
831 uint16_t, // record count LS first
832 uint16_t, // free space in bytes, LS first
833 uint32_t, // addition timestamp LS first
834 uint32_t, // deletion timestamp LS first
835 uint8_t> // operation Support
836 ipmiGetRepositoryInfo()
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600837{
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000838 constexpr uint8_t sdrVersion = 0x51;
839 constexpr uint16_t freeSpace = 0xFFFF;
840 constexpr uint32_t additionTimestamp = 0x0;
841 constexpr uint32_t deletionTimestamp = 0x0;
842 constexpr uint8_t operationSupport = 0;
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600843
Willy Tu0ca3bfe2022-01-28 09:40:18 -0800844 // Get SDR count. This returns the total number of SDRs in the device.
845 const auto& entityRecords =
846 ipmi::sensor::EntityInfoMapContainer::getContainer()
847 ->getIpmiEntityRecords();
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500848 uint16_t records = ipmi::sensor::sensors.size() + frus.size() +
849 entityRecords.size();
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600850
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000851 return ipmi::responseSuccess(sdrVersion, records, freeSpace,
852 additionTimestamp, deletionTimestamp,
853 operationSupport);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600854}
Chris Austenb4f5b922015-10-13 12:44:43 -0500855
Chris Austenb4f5b922015-10-13 12:44:43 -0500856void register_netfn_storage_functions()
857{
Lei YU3df36612021-09-15 11:41:11 +0800858 selCacheMapInitialized = false;
Lei YUd9e57662021-09-14 18:06:28 +0800859 initSELCache();
Willy Tud351a722021-08-12 14:33:40 -0700860 // Handlers with dbus-sdr handler implementation.
861 // Do not register the hander if it dynamic sensors stack is used.
862
863#ifndef FEATURE_DYNAMIC_SENSORS
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530864 // <Get SEL Info>
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000865 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
866 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
867 ipmiStorageGetSelInfo);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530868
Tom05732372016-09-06 17:21:23 +0530869 // <Get SEL Time>
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000870 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
871 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
872 ipmiStorageGetSelTime);
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500873
Tom05732372016-09-06 17:21:23 +0530874 // <Set SEL Time>
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000875 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
876 ipmi::storage::cmdSetSelTime,
877 ipmi::Privilege::Operator, ipmiStorageSetSelTime);
Chris Austenb4f5b922015-10-13 12:44:43 -0500878
Tom Josepha4953392017-06-30 19:09:47 +0530879 // <Get SEL Entry>
Patrick Venture0b02be92018-08-31 11:55:55 -0700880 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_ENTRY, NULL,
881 getSELEntry, PRIVILEGE_USER);
Tom Josepha4953392017-06-30 19:09:47 +0530882
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530883 // <Delete SEL Entry>
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000884 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
885 ipmi::storage::cmdDeleteSelEntry,
886 ipmi::Privilege::Operator, deleteSELEntry);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530887
Tom05732372016-09-06 17:21:23 +0530888 // <Add SEL Entry>
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000889 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
890 ipmi::storage::cmdAddSelEntry,
891 ipmi::Privilege::Operator, ipmiStorageAddSEL);
892
Tom Joseph2f05bb52017-06-30 19:14:49 +0530893 // <Clear SEL>
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000894 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
895 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
896 clearSEL);
897
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500898 // <Get FRU Inventory Area Info>
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000899 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
900 ipmi::storage::cmdGetFruInventoryAreaInfo,
901 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500902
Jason M. Billsb5248c92019-06-24 15:53:08 -0700903 // <READ FRU Data>
anil kumar appana5b7c3262019-05-27 18:10:23 +0000904 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
905 ipmi::storage::cmdReadFruData,
906 ipmi::Privilege::Operator, ipmiStorageReadFruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500907
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600908 // <Get Repository Info>
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000909 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
910 ipmi::storage::cmdGetSdrRepositoryInfo,
911 ipmi::Privilege::User, ipmiGetRepositoryInfo);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600912
Tom Joseph5ca50952018-02-22 00:33:38 +0530913 // <Reserve SDR Repository>
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000914 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
915 ipmi::storage::cmdReserveSdrRepository,
916 ipmi::Privilege::User, ipmiSensorReserveSdr);
Tom Joseph5ca50952018-02-22 00:33:38 +0530917
918 // <Get SDR>
Patrick Venture0b02be92018-08-31 11:55:55 -0700919 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SDR, nullptr,
920 ipmi_sen_get_sdr, PRIVILEGE_USER);
Tom Joseph5ca50952018-02-22 00:33:38 +0530921
Willy Tud351a722021-08-12 14:33:40 -0700922#endif
923
924 // Common Handers used by both implementation.
925
926 // <Reserve SEL>
927 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
928 ipmi::storage::cmdReserveSel, ipmi::Privilege::User,
929 ipmiStorageReserveSel);
930
Marri Devender Rao908f7502017-07-10 01:49:54 -0500931 ipmi::fru::registerCallbackHandler();
Chris Austenb4f5b922015-10-13 12:44:43 -0500932 return;
933}