blob: d38469319ec1a5124d326aefd629c6c0795a3e87 [file] [log] [blame]
Patrick Williams768730d2023-09-06 16:27:20 -05001#include "config.h"
2
Patrick Venture3a5071a2018-09-12 13:27:42 -07003#include "storagehandler.hpp"
4
5#include "fruread.hpp"
6#include "read_fru_data.hpp"
7#include "selutility.hpp"
8#include "sensorhandler.hpp"
9#include "storageaddsel.hpp"
Patrick Venture3a5071a2018-09-12 13:27:42 -070010
Lei YU52d91242017-10-17 22:52:28 +080011#include <arpa/inet.h>
Patrick Venture3a5071a2018-09-12 13:27:42 -070012#include <systemd/sd-bus.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070013
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050014#include <ipmid/api.hpp>
Vernon Mauery9cf08382023-04-28 14:00:11 -070015#include <ipmid/entity_map_json.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050016#include <ipmid/utils.hpp>
17#include <phosphor-logging/elog-errors.hpp>
18#include <phosphor-logging/elog.hpp>
19#include <phosphor-logging/log.hpp>
20#include <sdbusplus/server.hpp>
21#include <xyz/openbmc_project/Common/error.hpp>
22#include <xyz/openbmc_project/Logging/SEL/error.hpp>
23
Patrick Venture0b02be92018-08-31 11:55:55 -070024#include <algorithm>
Lei YU52d91242017-10-17 22:52:28 +080025#include <chrono>
26#include <cstdio>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070027#include <cstring>
Vernon Mauerybdda8002019-02-26 10:18:51 -080028#include <filesystem>
Lei YUa0bb2a32021-09-24 11:03:56 +080029#include <optional>
Patrick Venture3a5071a2018-09-12 13:27:42 -070030#include <string>
Vernon Mauery16b86932019-05-01 08:36:11 -070031#include <variant>
Patrick Venture3a5071a2018-09-12 13:27:42 -070032
Chris Austenb4f5b922015-10-13 12:44:43 -050033void register_netfn_storage_functions() __attribute__((constructor));
34
Patrick Venture0b02be92018-08-31 11:55:55 -070035unsigned int g_sel_time = 0xFFFFFFFF;
Patrick Venturedb0cbe62019-09-09 14:47:22 -070036namespace ipmi
37{
38namespace sensor
39{
40extern const IdInfoMap sensors;
41} // namespace sensor
42} // namespace ipmi
Lotus Xu57d35572021-01-24 11:13:13 +080043extern const ipmi::sensor::InvObjectIDMap invSensors;
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -060044extern const FruMap frus;
anil kumar appana2c7db1d2019-05-28 11:20:19 +000045constexpr uint8_t eventDataSize = 3;
Patrick Venture0b02be92018-08-31 11:55:55 -070046namespace
47{
shamim ali071d00c2022-03-02 15:42:46 +053048constexpr auto SystemdTimeService = "org.freedesktop.timedate1";
49constexpr auto SystemdTimePath = "/org/freedesktop/timedate1";
50constexpr auto SystemdTimeInterface = "org.freedesktop.timedate1";
51
Lei YUe8939392017-06-15 10:45:05 +080052constexpr auto TIME_INTERFACE = "xyz.openbmc_project.Time.EpochTime";
George Liu4d623e92020-05-25 16:51:57 +080053constexpr auto BMC_TIME_PATH = "/xyz/openbmc_project/time/bmc";
Lei YUe8939392017-06-15 10:45:05 +080054constexpr auto DBUS_PROPERTIES = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070055constexpr auto PROPERTY_ELAPSED = "Elapsed";
Lei YUe8939392017-06-15 10:45:05 +080056
Lei YUd9555252021-09-14 20:30:41 +080057constexpr auto logWatchPath = "/xyz/openbmc_project/logging";
Lei YUd9e57662021-09-14 18:06:28 +080058constexpr auto logBasePath = "/xyz/openbmc_project/logging/entry";
59constexpr auto logEntryIntf = "xyz.openbmc_project.Logging.Entry";
60constexpr auto logDeleteIntf = "xyz.openbmc_project.Object.Delete";
Patrick Venture0b02be92018-08-31 11:55:55 -070061} // namespace
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +053062
Tom Joseph6f7deaa2017-06-30 19:03:54 +053063using InternalFailure =
Willy Tu523e2d12023-09-05 11:36:48 -070064 sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
Tom Joseph6f7deaa2017-06-30 19:03:54 +053065using namespace phosphor::logging;
Marri Devender Raocac383b2017-07-03 13:24:27 -050066using namespace ipmi::fru;
Willy Tu523e2d12023-09-05 11:36:48 -070067using namespace xyz::openbmc_project::logging::sel;
Lotus Xu57d35572021-01-24 11:13:13 +080068using SELCreated =
Willy Tu523e2d12023-09-05 11:36:48 -070069 sdbusplus::error::xyz::openbmc_project::logging::sel::Created;
Marri Devender Raocac383b2017-07-03 13:24:27 -050070
Lei YUd9e57662021-09-14 18:06:28 +080071using SELRecordID = uint16_t;
72using SELEntry = ipmi::sel::SELEventRecordFormat;
73using SELCacheMap = std::map<SELRecordID, SELEntry>;
74
75SELCacheMap selCacheMap __attribute__((init_priority(101)));
Lei YU3df36612021-09-15 11:41:11 +080076bool selCacheMapInitialized;
Patrick Williams5d82f472022-07-22 19:26:53 -050077std::unique_ptr<sdbusplus::bus::match_t> selAddedMatch
Lei YUd9555252021-09-14 20:30:41 +080078 __attribute__((init_priority(101)));
Patrick Williams5d82f472022-07-22 19:26:53 -050079std::unique_ptr<sdbusplus::bus::match_t> selRemovedMatch
Lei YUb6b72b02021-09-14 21:06:05 +080080 __attribute__((init_priority(101)));
Patrick Williams5d82f472022-07-22 19:26:53 -050081std::unique_ptr<sdbusplus::bus::match_t> selUpdatedMatch
Lei YU4106e422021-09-15 10:55:51 +080082 __attribute__((init_priority(101)));
Lei YUd9e57662021-09-14 18:06:28 +080083
Lei YUb6b72b02021-09-14 21:06:05 +080084static inline uint16_t getLoggingId(const std::string& p)
Lei YUd9e57662021-09-14 18:06:28 +080085{
86 namespace fs = std::filesystem;
87 fs::path entryPath(p);
Lei YUb6b72b02021-09-14 21:06:05 +080088 return std::stoul(entryPath.filename().string());
89}
90
Lei YUeba8e9a2021-09-15 13:16:17 +080091static inline std::string getLoggingObjPath(uint16_t id)
92{
93 return std::string(ipmi::sel::logBasePath) + "/" + std::to_string(id);
94}
95
Lei YUa0bb2a32021-09-24 11:03:56 +080096std::optional<std::pair<uint16_t, SELEntry>>
97 parseLoggingEntry(const std::string& p)
Lei YUb6b72b02021-09-14 21:06:05 +080098{
Lei YU3df36612021-09-15 11:41:11 +080099 try
100 {
Lei YUa0bb2a32021-09-24 11:03:56 +0800101 auto id = getLoggingId(p);
102 ipmi::sel::GetSELEntryResponse record{};
Lei YU3df36612021-09-15 11:41:11 +0800103 record = ipmi::sel::convertLogEntrytoSEL(p);
Lei YUa0bb2a32021-09-24 11:03:56 +0800104 return std::pair<uint16_t, SELEntry>({id, std::move(record.event)});
Lei YU3df36612021-09-15 11:41:11 +0800105 }
106 catch (const std::exception& e)
107 {
108 fprintf(stderr, "Failed to convert %s to SEL: %s\n", p.c_str(),
109 e.what());
110 }
Lei YUa0bb2a32021-09-24 11:03:56 +0800111 return std::nullopt;
Lei YUd9e57662021-09-14 18:06:28 +0800112}
113
Patrick Williams5d82f472022-07-22 19:26:53 -0500114static void selAddedCallback(sdbusplus::message_t& m)
Lei YUd9555252021-09-14 20:30:41 +0800115{
116 sdbusplus::message::object_path objPath;
117 try
118 {
119 m.read(objPath);
120 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500121 catch (const sdbusplus::exception_t& e)
Lei YUd9555252021-09-14 20:30:41 +0800122 {
123 log<level::ERR>("Failed to read object path");
124 return;
125 }
126 std::string p = objPath;
Lei YUa0bb2a32021-09-24 11:03:56 +0800127 auto entry = parseLoggingEntry(p);
128 if (entry)
129 {
130 selCacheMap.insert(std::move(*entry));
131 }
Lei YUd9555252021-09-14 20:30:41 +0800132}
133
Patrick Williams5d82f472022-07-22 19:26:53 -0500134static void selRemovedCallback(sdbusplus::message_t& m)
Lei YUb6b72b02021-09-14 21:06:05 +0800135{
136 sdbusplus::message::object_path objPath;
137 try
138 {
139 m.read(objPath);
140 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500141 catch (const sdbusplus::exception_t& e)
Lei YUb6b72b02021-09-14 21:06:05 +0800142 {
143 log<level::ERR>("Failed to read object path");
Lei YUb6b72b02021-09-14 21:06:05 +0800144 }
Lei YUa0bb2a32021-09-24 11:03:56 +0800145 try
146 {
147 std::string p = objPath;
148 selCacheMap.erase(getLoggingId(p));
149 }
150 catch (const std::invalid_argument& e)
151 {
152 log<level::ERR>("Invalid logging entry ID");
153 }
Lei YUb6b72b02021-09-14 21:06:05 +0800154}
155
Patrick Williams5d82f472022-07-22 19:26:53 -0500156static void selUpdatedCallback(sdbusplus::message_t& m)
Lei YU4106e422021-09-15 10:55:51 +0800157{
158 std::string p = m.get_path();
159 auto entry = parseLoggingEntry(p);
Lei YUa0bb2a32021-09-24 11:03:56 +0800160 if (entry)
161 {
162 selCacheMap.insert_or_assign(entry->first, std::move(entry->second));
163 }
Lei YU4106e422021-09-15 10:55:51 +0800164}
165
Lei YUd9555252021-09-14 20:30:41 +0800166void registerSelCallbackHandler()
167{
168 using namespace sdbusplus::bus::match::rules;
Patrick Williams5d82f472022-07-22 19:26:53 -0500169 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Lei YUd9555252021-09-14 20:30:41 +0800170 if (!selAddedMatch)
171 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500172 selAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
Lei YUd9555252021-09-14 20:30:41 +0800173 bus, interfacesAdded(logWatchPath),
174 std::bind(selAddedCallback, std::placeholders::_1));
175 }
Lei YUb6b72b02021-09-14 21:06:05 +0800176 if (!selRemovedMatch)
177 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500178 selRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
Lei YUb6b72b02021-09-14 21:06:05 +0800179 bus, interfacesRemoved(logWatchPath),
180 std::bind(selRemovedCallback, std::placeholders::_1));
181 }
Lei YU4106e422021-09-15 10:55:51 +0800182 if (!selUpdatedMatch)
183 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500184 selUpdatedMatch = std::make_unique<sdbusplus::bus::match_t>(
Lei YU4106e422021-09-15 10:55:51 +0800185 bus,
186 type::signal() + member("PropertiesChanged"s) +
187 interface("org.freedesktop.DBus.Properties"s) +
188 argN(0, logEntryIntf),
189 std::bind(selUpdatedCallback, std::placeholders::_1));
190 }
Lei YUd9555252021-09-14 20:30:41 +0800191}
192
Lei YUd9e57662021-09-14 18:06:28 +0800193void initSELCache()
194{
Lei YUc10cda12022-01-06 16:01:57 +0800195 registerSelCallbackHandler();
Lei YUeba8e9a2021-09-15 13:16:17 +0800196 ipmi::sel::ObjectPaths paths;
Lei YUd9e57662021-09-14 18:06:28 +0800197 try
198 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800199 ipmi::sel::readLoggingObjectPaths(paths);
Lei YUd9e57662021-09-14 18:06:28 +0800200 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500201 catch (const sdbusplus::exception_t& e)
Lei YUd9e57662021-09-14 18:06:28 +0800202 {
203 log<level::ERR>("Failed to get logging object paths");
204 return;
205 }
Lei YUeba8e9a2021-09-15 13:16:17 +0800206 for (const auto& p : paths)
Lei YUd9e57662021-09-14 18:06:28 +0800207 {
Lei YUa0bb2a32021-09-24 11:03:56 +0800208 auto entry = parseLoggingEntry(p);
209 if (entry)
210 {
211 selCacheMap.insert(std::move(*entry));
212 }
Lei YUd9e57662021-09-14 18:06:28 +0800213 }
Lei YU3df36612021-09-15 11:41:11 +0800214 selCacheMapInitialized = true;
Lei YUd9e57662021-09-14 18:06:28 +0800215}
216
Marri Devender Raocac383b2017-07-03 13:24:27 -0500217/**
218 * @enum Device access mode
219 */
220enum class AccessMode
221{
222 bytes, ///< Device is accessed by bytes
223 words ///< Device is accessed by words
224};
225
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000226/** @brief implements the get SEL Info command
227 * @returns IPMI completion code plus response data
228 * - selVersion - SEL revision
229 * - entries - Number of log entries in SEL.
230 * - freeSpace - Free Space in bytes.
231 * - addTimeStamp - Most recent addition timestamp
232 * - eraseTimeStamp - Most recent erase timestamp
233 * - operationSupport - Reserve & Delete SEL operations supported
234 */
235
236ipmi::RspType<uint8_t, // SEL revision.
237 uint16_t, // number of log entries in SEL.
238 uint16_t, // free Space in bytes.
239 uint32_t, // most recent addition timestamp
240 uint32_t, // most recent erase timestamp.
241
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500242 bool, // SEL allocation info supported
243 bool, // reserve SEL supported
244 bool, // partial Add SEL Entry supported
245 bool, // delete SEL supported
246 uint3_t, // reserved
247 bool // overflow flag
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000248 >
249 ipmiStorageGetSelInfo()
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530250{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000251 uint16_t entries = 0;
252 // Most recent addition timestamp.
253 uint32_t addTimeStamp = ipmi::sel::invalidTimeStamp;
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530254
Lei YUeba8e9a2021-09-15 13:16:17 +0800255 if (!selCacheMapInitialized)
Tom Josephe59abfb2018-08-06 18:46:27 +0530256 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800257 // In case the initSELCache() fails, try it again
258 initSELCache();
Tom Josephe59abfb2018-08-06 18:46:27 +0530259 }
Lei YUeba8e9a2021-09-15 13:16:17 +0800260 if (!selCacheMap.empty())
Tom Josephe59abfb2018-08-06 18:46:27 +0530261 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800262 entries = static_cast<uint16_t>(selCacheMap.size());
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530263
264 try
265 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800266 auto objPath = getLoggingObjPath(selCacheMap.rbegin()->first);
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000267 addTimeStamp = static_cast<uint32_t>(
Lei YUeba8e9a2021-09-15 13:16:17 +0800268 (ipmi::sel::getEntryTimeStamp(objPath).count()));
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530269 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500270 catch (const InternalFailure& e)
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500271 {}
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530272 catch (const std::runtime_error& e)
273 {
274 log<level::ERR>(e.what());
275 }
276 }
277
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000278 constexpr uint8_t selVersion = ipmi::sel::selVersion;
279 constexpr uint16_t freeSpace = 0xFFFF;
280 constexpr uint32_t eraseTimeStamp = ipmi::sel::invalidTimeStamp;
281 constexpr uint3_t reserved{0};
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530282
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000283 return ipmi::responseSuccess(
284 selVersion, entries, freeSpace, addTimeStamp, eraseTimeStamp,
285 ipmi::sel::operationSupport::getSelAllocationInfo,
286 ipmi::sel::operationSupport::reserveSel,
287 ipmi::sel::operationSupport::partialAddSelEntry,
288 ipmi::sel::operationSupport::deleteSel, reserved,
289 ipmi::sel::operationSupport::overflow);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530290}
291
Willy Tu11d68892022-01-20 10:37:34 -0800292ipmi_ret_t getSELEntry(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request,
293 ipmi_response_t response, ipmi_data_len_t data_len,
294 ipmi_context_t)
Tom Josepha4953392017-06-30 19:09:47 +0530295{
Jason M. Bills851acb12019-02-04 14:06:57 -0800296 if (*data_len != sizeof(ipmi::sel::GetSELEntryRequest))
297 {
298 *data_len = 0;
299 return IPMI_CC_REQ_DATA_LEN_INVALID;
300 }
301
Patrick Venture0b02be92018-08-31 11:55:55 -0700302 auto requestData =
303 reinterpret_cast<const ipmi::sel::GetSELEntryRequest*>(request);
Tom Josepha4953392017-06-30 19:09:47 +0530304
305 if (requestData->reservationID != 0)
306 {
Jason M. Bills13e67c82018-09-10 14:12:16 -0700307 if (!checkSELReservation(requestData->reservationID))
Tom Josepha4953392017-06-30 19:09:47 +0530308 {
309 *data_len = 0;
310 return IPMI_CC_INVALID_RESERVATION_ID;
311 }
312 }
313
Lei YU3df36612021-09-15 11:41:11 +0800314 if (!selCacheMapInitialized)
315 {
316 // In case the initSELCache() fails, try it again
317 initSELCache();
318 }
319
320 if (selCacheMap.empty())
Tom Josepha4953392017-06-30 19:09:47 +0530321 {
322 *data_len = 0;
323 return IPMI_CC_SENSOR_INVALID;
324 }
325
Lei YU3df36612021-09-15 11:41:11 +0800326 SELCacheMap::const_iterator iter;
Tom Josepha4953392017-06-30 19:09:47 +0530327
328 // Check for the requested SEL Entry.
329 if (requestData->selRecordID == ipmi::sel::firstEntry)
330 {
Lei YU3df36612021-09-15 11:41:11 +0800331 iter = selCacheMap.begin();
Tom Josepha4953392017-06-30 19:09:47 +0530332 }
333 else if (requestData->selRecordID == ipmi::sel::lastEntry)
334 {
Lei YU3df36612021-09-15 11:41:11 +0800335 if (selCacheMap.size() > 1)
336 {
337 iter = selCacheMap.end();
338 --iter;
339 }
340 else
341 {
342 // Only one entry exists, return the first
343 iter = selCacheMap.begin();
344 }
Tom Josepha4953392017-06-30 19:09:47 +0530345 }
346 else
347 {
Lei YU3df36612021-09-15 11:41:11 +0800348 iter = selCacheMap.find(requestData->selRecordID);
349 if (iter == selCacheMap.end())
Tom Josepha4953392017-06-30 19:09:47 +0530350 {
351 *data_len = 0;
352 return IPMI_CC_SENSOR_INVALID;
353 }
354 }
355
Lei YU3df36612021-09-15 11:41:11 +0800356 ipmi::sel::GetSELEntryResponse record{0, iter->second};
Tom Josepha4953392017-06-30 19:09:47 +0530357 // Identify the next SEL record ID
Lei YU3df36612021-09-15 11:41:11 +0800358 ++iter;
359 if (iter == selCacheMap.end())
Tom Josepha4953392017-06-30 19:09:47 +0530360 {
Lei YU3df36612021-09-15 11:41:11 +0800361 record.nextRecordID = ipmi::sel::lastEntry;
Tom Josepha4953392017-06-30 19:09:47 +0530362 }
363 else
364 {
Lei YU3df36612021-09-15 11:41:11 +0800365 record.nextRecordID = iter->first;
Tom Josepha4953392017-06-30 19:09:47 +0530366 }
367
368 if (requestData->readLength == ipmi::sel::entireRecord)
369 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700370 std::memcpy(response, &record, sizeof(record));
Tom Josepha4953392017-06-30 19:09:47 +0530371 *data_len = sizeof(record);
372 }
373 else
374 {
375 if (requestData->offset >= ipmi::sel::selRecordSize ||
376 requestData->readLength > ipmi::sel::selRecordSize)
377 {
378 *data_len = 0;
379 return IPMI_CC_INVALID_FIELD_REQUEST;
380 }
381
382 auto diff = ipmi::sel::selRecordSize - requestData->offset;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500383 auto readLength = std::min(diff,
384 static_cast<int>(requestData->readLength));
Tom Josepha4953392017-06-30 19:09:47 +0530385
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700386 std::memcpy(response, &record.nextRecordID,
387 sizeof(record.nextRecordID));
388 std::memcpy(static_cast<uint8_t*>(response) +
389 sizeof(record.nextRecordID),
Lei YUaf378fa2020-12-02 16:28:57 +0800390 &record.event.eventRecord.recordID + requestData->offset,
391 readLength);
Tom Josepha4953392017-06-30 19:09:47 +0530392 *data_len = sizeof(record.nextRecordID) + readLength;
393 }
394
395 return IPMI_CC_OK;
396}
397
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000398/** @brief implements the delete SEL entry command
399 * @request
400 * - reservationID; // reservation ID.
401 * - selRecordID; // SEL record ID.
402 *
403 * @returns ipmi completion code plus response data
404 * - Record ID of the deleted record
405 */
406ipmi::RspType<uint16_t // deleted record ID
407 >
408 deleteSELEntry(uint16_t reservationID, uint16_t selRecordID)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530409{
Brad Bishop1a4117b2018-11-21 15:48:18 -0500410 namespace fs = std::filesystem;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530411
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000412 if (!checkSELReservation(reservationID))
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530413 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000414 return ipmi::responseInvalidReservationId();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530415 }
416
Jason M. Bills13e67c82018-09-10 14:12:16 -0700417 // Per the IPMI spec, need to cancel the reservation when a SEL entry is
418 // deleted
419 cancelSELReservation();
420
Lei YUeba8e9a2021-09-15 13:16:17 +0800421 if (!selCacheMapInitialized)
Tom Josephe59abfb2018-08-06 18:46:27 +0530422 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800423 // In case the initSELCache() fails, try it again
424 initSELCache();
Tom Josephe59abfb2018-08-06 18:46:27 +0530425 }
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530426
Lei YUeba8e9a2021-09-15 13:16:17 +0800427 if (selCacheMap.empty())
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530428 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000429 return ipmi::responseSensorInvalid();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530430 }
431
Lei YUeba8e9a2021-09-15 13:16:17 +0800432 SELCacheMap::const_iterator iter;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530433 uint16_t delRecordID = 0;
434
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000435 if (selRecordID == ipmi::sel::firstEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530436 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800437 delRecordID = selCacheMap.begin()->first;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530438 }
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000439 else if (selRecordID == ipmi::sel::lastEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530440 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800441 delRecordID = selCacheMap.rbegin()->first;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530442 }
443 else
444 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000445 delRecordID = selRecordID;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530446 }
447
Jian7d5f4812022-02-23 19:30:27 +0800448 iter = selCacheMap.find(delRecordID);
449 if (iter == selCacheMap.end())
450 {
451 return ipmi::responseSensorInvalid();
452 }
453
Patrick Williams5d82f472022-07-22 19:26:53 -0500454 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530455 std::string service;
456
Lei YUeba8e9a2021-09-15 13:16:17 +0800457 auto objPath = getLoggingObjPath(iter->first);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530458 try
459 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800460 service = ipmi::getService(bus, ipmi::sel::logDeleteIntf, objPath);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530461 }
462 catch (const std::runtime_error& e)
463 {
464 log<level::ERR>(e.what());
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000465 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530466 }
467
Lei YUeba8e9a2021-09-15 13:16:17 +0800468 auto methodCall = bus.new_method_call(service.c_str(), objPath.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700469 ipmi::sel::logDeleteIntf, "Delete");
George Liu3e3cc352023-07-26 15:59:31 +0800470 try
471 {
472 auto reply = bus.call(methodCall);
473 }
474 catch (const std::exception& e)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530475 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000476 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530477 }
478
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000479 return ipmi::responseSuccess(delRecordID);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530480}
481
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000482/** @brief implements the Clear SEL command
483 * @request
484 * - reservationID // Reservation ID.
485 * - clr // char array { 'C'(0x43h), 'L'(0x4Ch), 'R'(0x52h) }
486 * - eraseOperation; // requested operation.
487 *
488 * @returns ipmi completion code plus response data
489 * - erase status
490 */
491
492ipmi::RspType<uint8_t // erase status
493 >
494 clearSEL(uint16_t reservationID, const std::array<char, 3>& clr,
495 uint8_t eraseOperation)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530496{
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000497 static constexpr std::array<char, 3> clrOk = {'C', 'L', 'R'};
498 if (clr != clrOk)
Jason M. Bills851acb12019-02-04 14:06:57 -0800499 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000500 return ipmi::responseInvalidFieldRequest();
Jason M. Bills851acb12019-02-04 14:06:57 -0800501 }
502
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000503 if (!checkSELReservation(reservationID))
Tom Joseph2f05bb52017-06-30 19:14:49 +0530504 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000505 return ipmi::responseInvalidReservationId();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530506 }
507
Tom Joseph2f05bb52017-06-30 19:14:49 +0530508 /*
509 * Erasure status cannot be fetched from DBUS, so always return erasure
510 * status as `erase completed`.
511 */
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000512 if (eraseOperation == ipmi::sel::getEraseStatus)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530513 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000514 return ipmi::responseSuccess(
515 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530516 }
517
Jason M. Bills13e67c82018-09-10 14:12:16 -0700518 // Per the IPMI spec, need to cancel any reservation when the SEL is cleared
519 cancelSELReservation();
520
Patrick Williams5d82f472022-07-22 19:26:53 -0500521 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Lotus Xu78fe1b12021-05-23 17:26:56 +0800522 auto service = ipmi::getService(bus, ipmi::sel::logIntf, ipmi::sel::logObj);
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500523 auto method = bus.new_method_call(service.c_str(), ipmi::sel::logObj,
524 ipmi::sel::logIntf,
525 ipmi::sel::logDeleteAllMethod);
Tom Josephe59abfb2018-08-06 18:46:27 +0530526 try
Tom Joseph2f05bb52017-06-30 19:14:49 +0530527 {
Lotus Xu78fe1b12021-05-23 17:26:56 +0800528 bus.call_noreply(method);
Tom Josephe59abfb2018-08-06 18:46:27 +0530529 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500530 catch (const sdbusplus::exception_t& e)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530531 {
Lotus Xu78fe1b12021-05-23 17:26:56 +0800532 log<level::ERR>("Error eraseAll ", entry("ERROR=%s", e.what()));
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000533 return ipmi::responseUnspecifiedError();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530534 }
535
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000536 return ipmi::responseSuccess(
537 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530538}
539
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000540/** @brief implements the get SEL time command
541 * @returns IPMI completion code plus response data
542 * -current time
543 */
544ipmi::RspType<uint32_t> // current time
545 ipmiStorageGetSelTime()
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500546{
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530547 using namespace std::chrono;
George Liu4d623e92020-05-25 16:51:57 +0800548 uint64_t bmc_time_usec = 0;
549 std::stringstream bmcTime;
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500550
Lei YUe8939392017-06-15 10:45:05 +0800551 try
552 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500553 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
George Liu4d623e92020-05-25 16:51:57 +0800554 auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
Vernon Mauery16b86932019-05-01 08:36:11 -0700555 std::variant<uint64_t> value;
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530556
George Liu4d623e92020-05-25 16:51:57 +0800557 // Get bmc time
558 auto method = bus.new_method_call(service.c_str(), BMC_TIME_PATH,
Patrick Venture0b02be92018-08-31 11:55:55 -0700559 DBUS_PROPERTIES, "Get");
Lei YUe8939392017-06-15 10:45:05 +0800560
561 method.append(TIME_INTERFACE, PROPERTY_ELAPSED);
562 auto reply = bus.call(method);
Lei YUe8939392017-06-15 10:45:05 +0800563 reply.read(value);
George Liu4d623e92020-05-25 16:51:57 +0800564 bmc_time_usec = std::get<uint64_t>(value);
Lei YUe8939392017-06-15 10:45:05 +0800565 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500566 catch (const InternalFailure& e)
Lei YUe8939392017-06-15 10:45:05 +0800567 {
568 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000569 return ipmi::responseUnspecifiedError();
Lei YUe8939392017-06-15 10:45:05 +0800570 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530571 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800572 {
573 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000574 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530575 }
576
George Liu4d623e92020-05-25 16:51:57 +0800577 bmcTime << "BMC time:"
578 << duration_cast<seconds>(microseconds(bmc_time_usec)).count();
579 log<level::DEBUG>(bmcTime.str().c_str());
Nagaraju Goruganti8960b7c2018-04-29 22:38:40 -0500580
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530581 // Time is really long int but IPMI wants just uint32. This works okay until
582 // the number of seconds since 1970 overflows uint32 size.. Still a whole
583 // lot of time here to even think about that.
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000584 return ipmi::responseSuccess(
George Liu4d623e92020-05-25 16:51:57 +0800585 duration_cast<seconds>(microseconds(bmc_time_usec)).count());
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500586}
587
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000588/** @brief implements the set SEL time command
589 * @param selDeviceTime - epoch time
590 * -local time as the number of seconds from 00:00:00, January 1, 1970
591 * @returns IPMI completion code
592 */
593ipmi::RspType<> ipmiStorageSetSelTime(uint32_t selDeviceTime)
Chris Austenb4f5b922015-10-13 12:44:43 -0500594{
Lei YUe8939392017-06-15 10:45:05 +0800595 using namespace std::chrono;
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000596 microseconds usec{seconds(selDeviceTime)};
Norman James82330442015-11-19 16:53:26 -0600597
Lei YUe8939392017-06-15 10:45:05 +0800598 try
599 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500600 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
shamim ali071d00c2022-03-02 15:42:46 +0530601 bool ntp = std::get<bool>(
602 ipmi::getDbusProperty(bus, SystemdTimeService, SystemdTimePath,
603 SystemdTimeInterface, "NTP"));
604 if (ntp)
605 {
606 return ipmi::responseCommandNotAvailable();
607 }
608
George Liu4d623e92020-05-25 16:51:57 +0800609 auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
Andrew Geissler6467ed22020-05-16 16:03:53 -0500610 std::variant<uint64_t> value{(uint64_t)usec.count()};
Lei YUe8939392017-06-15 10:45:05 +0800611
George Liu4d623e92020-05-25 16:51:57 +0800612 // Set bmc time
613 auto method = bus.new_method_call(service.c_str(), BMC_TIME_PATH,
Patrick Venture0b02be92018-08-31 11:55:55 -0700614 DBUS_PROPERTIES, "Set");
Lei YUe8939392017-06-15 10:45:05 +0800615
616 method.append(TIME_INTERFACE, PROPERTY_ELAPSED, value);
617 auto reply = bus.call(method);
Norman James82330442015-11-19 16:53:26 -0600618 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500619 catch (const InternalFailure& e)
Lei YUe8939392017-06-15 10:45:05 +0800620 {
621 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000622 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530623 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530624 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800625 {
626 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000627 return ipmi::responseUnspecifiedError();
Norman James82330442015-11-19 16:53:26 -0600628 }
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530629
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000630 return ipmi::responseSuccess();
Chris Austenb4f5b922015-10-13 12:44:43 -0500631}
632
Xiaochao Maf020c442022-10-31 11:09:38 +0800633/** @brief implements the get SEL timezone command
634 * @returns IPMI completion code plus response data
635 * -current timezone
636 */
637ipmi::RspType<int16_t> ipmiStorageGetSelTimeUtcOffset()
638{
639 time_t timep;
640 struct tm* gmTime;
641 struct tm* localTime;
642
643 time(&timep);
644 localTime = localtime(&timep);
645 auto validLocalTime = mktime(localTime);
646 gmTime = gmtime(&timep);
647 auto validGmTime = mktime(gmTime);
648 auto timeEquation = (validLocalTime - validGmTime) / 60;
649
650 return ipmi::responseSuccess(timeEquation);
651}
652
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000653/** @brief implements the reserve SEL command
654 * @returns IPMI completion code plus response data
655 * - SEL reservation ID.
656 */
657ipmi::RspType<uint16_t> ipmiStorageReserveSel()
Chris Austenb4f5b922015-10-13 12:44:43 -0500658{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000659 return ipmi::responseSuccess(reserveSel());
Chris Austenb4f5b922015-10-13 12:44:43 -0500660}
661
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000662/** @brief implements the Add SEL entry command
663 * @request
664 *
665 * - recordID ID used for SEL Record access
666 * - recordType Record Type
667 * - timeStamp Time when event was logged. LS byte first
668 * - generatorID software ID if event was generated from
669 * system software
670 * - evmRev event message format version
671 * - sensorType sensor type code for service that generated
672 * the event
673 * - sensorNumber number of sensors that generated the event
674 * - eventDir event dir
675 * - eventData event data field contents
676 *
677 * @returns ipmi completion code plus response data
678 * - RecordID of the Added SEL entry
679 */
680ipmi::RspType<uint16_t // recordID of the Added SEL entry
681 >
Willy Tu11d68892022-01-20 10:37:34 -0800682 ipmiStorageAddSEL(uint16_t recordID, uint8_t recordType,
683 [[maybe_unused]] uint32_t timeStamp, uint16_t generatorID,
684 [[maybe_unused]] uint8_t evmRev, uint8_t sensorType,
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000685 uint8_t sensorNumber, uint8_t eventDir,
686 std::array<uint8_t, eventDataSize> eventData)
Chris Austenb4f5b922015-10-13 12:44:43 -0500687{
Lotus Xu57d35572021-01-24 11:13:13 +0800688 std::string objpath;
689 static constexpr auto systemRecordType = 0x02;
Tom Josephb647d5b2017-10-31 17:25:33 +0530690 // Hostboot sends SEL with OEM record type 0xDE to indicate that there is
691 // a maintenance procedure associated with eSEL record.
692 static constexpr auto procedureType = 0xDE;
Lotus Xu57d35572021-01-24 11:13:13 +0800693 cancelSELReservation();
694 if (recordType == systemRecordType)
695 {
Lotus Xu57d35572021-01-24 11:13:13 +0800696 for (const auto& it : invSensors)
697 {
698 if (it.second.sensorID == sensorNumber)
699 {
700 objpath = it.first;
701 break;
702 }
703 }
704 auto selDataStr = ipmi::sel::toHexStr(eventData);
705
706 bool assert = (eventDir & 0x80) ? false : true;
707
708 recordID = report<SELCreated>(Created::RECORD_TYPE(recordType),
709 Created::GENERATOR_ID(generatorID),
710 Created::SENSOR_DATA(selDataStr.c_str()),
711 Created::EVENT_DIR(assert),
712 Created::SENSOR_PATH(objpath.c_str()));
713 }
Patrick Williams768730d2023-09-06 16:27:20 -0500714#ifdef OPEN_POWER_SUPPORT
Lotus Xu57d35572021-01-24 11:13:13 +0800715 else if (recordType == procedureType)
Tom Josephb647d5b2017-10-31 17:25:33 +0530716 {
717 // In the OEM record type 0xDE, byte 11 in the SEL record indicate the
718 // procedure number.
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000719 createProcedureLogEntry(sensorType);
Tom Josephb647d5b2017-10-31 17:25:33 +0530720 }
Patrick Williams768730d2023-09-06 16:27:20 -0500721#endif
Chris Austenb4f5b922015-10-13 12:44:43 -0500722
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000723 return ipmi::responseSuccess(recordID);
Chris Austenb4f5b922015-10-13 12:44:43 -0500724}
725
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300726bool isFruPresent(ipmi::Context::ptr& ctx, const std::string& fruPath)
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300727{
728 using namespace ipmi::fru;
729
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300730 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500731 boost::system::error_code ec = getService(ctx, invItemInterface,
732 invObjPath + fruPath, service);
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300733 if (!ec)
734 {
735 bool result;
736 ec = ipmi::getDbusProperty(ctx, service, invObjPath + fruPath,
737 invItemInterface, itemPresentProp, result);
738 if (!ec)
739 {
740 return result;
741 }
742 }
Konstantin Aladyshevc7e4b042021-12-24 15:30:47 +0300743
744 ipmi::ObjectValueTree managedObjects;
Nan Zhou947da1b2022-09-20 20:40:59 +0000745 ec = getManagedObjects(ctx, "xyz.openbmc_project.EntityManager",
746 "/xyz/openbmc_project/inventory", managedObjects);
Konstantin Aladyshevc7e4b042021-12-24 15:30:47 +0300747 if (!ec)
748 {
749 auto connection = managedObjects.find(fruPath);
750 if (connection != managedObjects.end())
751 {
752 return true;
753 }
754 }
755
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300756 return false;
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300757}
758
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000759/** @brief implements the get FRU Inventory Area Info command
760 *
761 * @returns IPMI completion code plus response data
762 * - FRU Inventory area size in bytes,
763 * - access bit
764 **/
765ipmi::RspType<uint16_t, // FRU Inventory area size in bytes,
766 uint8_t // access size (bytes / words)
767 >
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300768 ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruID)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500769{
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000770 auto iter = frus.find(fruID);
Tom Joseph187f5642018-03-29 13:49:06 +0530771 if (iter == frus.end())
772 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000773 return ipmi::responseSensorInvalid();
Tom Joseph187f5642018-03-29 13:49:06 +0530774 }
775
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300776 auto path = iter->second[0].path;
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300777 if (!isFruPresent(ctx, path))
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300778 {
779 return ipmi::responseSensorInvalid();
780 }
781
Marri Devender Raocac383b2017-07-03 13:24:27 -0500782 try
783 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000784 return ipmi::responseSuccess(
785 static_cast<uint16_t>(getFruAreaData(fruID).size()),
786 static_cast<uint8_t>(AccessMode::bytes));
Marri Devender Raocac383b2017-07-03 13:24:27 -0500787 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700788 catch (const InternalFailure& e)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500789 {
Marri Devender Raocac383b2017-07-03 13:24:27 -0500790 log<level::ERR>(e.what());
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000791 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500792 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500793}
794
anil kumar appana5b7c3262019-05-27 18:10:23 +0000795/**@brief implements the Read FRU Data command
796 * @param fruDeviceId - FRU device ID. FFh = reserved
797 * @param offset - FRU inventory offset to read
798 * @param readCount - count to read
799 *
800 * @return IPMI completion code plus response data
801 * - returnCount - response data count.
802 * - data - response data
803 */
804ipmi::RspType<uint8_t, // count returned
805 std::vector<uint8_t>> // FRU data
806 ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t offset,
807 uint8_t readCount)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500808{
anil kumar appana5b7c3262019-05-27 18:10:23 +0000809 if (fruDeviceId == 0xFF)
Tom Joseph187f5642018-03-29 13:49:06 +0530810 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000811 return ipmi::responseInvalidFieldRequest();
Tom Joseph187f5642018-03-29 13:49:06 +0530812 }
813
anil kumar appana5b7c3262019-05-27 18:10:23 +0000814 auto iter = frus.find(fruDeviceId);
815 if (iter == frus.end())
816 {
817 return ipmi::responseSensorInvalid();
818 }
819
Marri Devender Raocac383b2017-07-03 13:24:27 -0500820 try
821 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000822 const auto& fruArea = getFruAreaData(fruDeviceId);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500823 auto size = fruArea.size();
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500824
Tom Josephefcd68b2018-04-26 18:46:27 +0530825 if (offset >= size)
826 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000827 return ipmi::responseParmOutOfRange();
Tom Josephefcd68b2018-04-26 18:46:27 +0530828 }
829
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500830 // Write the count of response data.
anil kumar appana5b7c3262019-05-27 18:10:23 +0000831 uint8_t returnCount;
832 if ((offset + readCount) <= size)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500833 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000834 returnCount = readCount;
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500835 }
836 else
837 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000838 returnCount = size - offset;
Marri Devender Raocac383b2017-07-03 13:24:27 -0500839 }
Ratan Gupta2848d602018-01-31 20:39:20 +0530840
anil kumar appana5b7c3262019-05-27 18:10:23 +0000841 std::vector<uint8_t> fruData((fruArea.begin() + offset),
842 (fruArea.begin() + offset + returnCount));
Ratan Gupta2848d602018-01-31 20:39:20 +0530843
anil kumar appana5b7c3262019-05-27 18:10:23 +0000844 return ipmi::responseSuccess(returnCount, fruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500845 }
846 catch (const InternalFailure& e)
847 {
Marri Devender Raocac383b2017-07-03 13:24:27 -0500848 log<level::ERR>(e.what());
anil kumar appana5b7c3262019-05-27 18:10:23 +0000849 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500850 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500851}
852
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000853ipmi::RspType<uint8_t, // SDR version
854 uint16_t, // record count LS first
855 uint16_t, // free space in bytes, LS first
856 uint32_t, // addition timestamp LS first
857 uint32_t, // deletion timestamp LS first
858 uint8_t> // operation Support
859 ipmiGetRepositoryInfo()
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600860{
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000861 constexpr uint8_t sdrVersion = 0x51;
862 constexpr uint16_t freeSpace = 0xFFFF;
863 constexpr uint32_t additionTimestamp = 0x0;
864 constexpr uint32_t deletionTimestamp = 0x0;
865 constexpr uint8_t operationSupport = 0;
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600866
Willy Tu0ca3bfe2022-01-28 09:40:18 -0800867 // Get SDR count. This returns the total number of SDRs in the device.
868 const auto& entityRecords =
869 ipmi::sensor::EntityInfoMapContainer::getContainer()
870 ->getIpmiEntityRecords();
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500871 uint16_t records = ipmi::sensor::sensors.size() + frus.size() +
872 entityRecords.size();
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600873
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000874 return ipmi::responseSuccess(sdrVersion, records, freeSpace,
875 additionTimestamp, deletionTimestamp,
876 operationSupport);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600877}
Chris Austenb4f5b922015-10-13 12:44:43 -0500878
Chris Austenb4f5b922015-10-13 12:44:43 -0500879void register_netfn_storage_functions()
880{
Lei YU3df36612021-09-15 11:41:11 +0800881 selCacheMapInitialized = false;
Lei YUd9e57662021-09-14 18:06:28 +0800882 initSELCache();
Willy Tud351a722021-08-12 14:33:40 -0700883 // Handlers with dbus-sdr handler implementation.
884 // Do not register the hander if it dynamic sensors stack is used.
885
886#ifndef FEATURE_DYNAMIC_SENSORS
Thang Tran292c9172023-06-26 10:03:04 +0700887
888#ifndef FEATURE_DYNAMIC_STORAGES_ONLY
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530889 // <Get SEL Info>
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000890 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
891 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
892 ipmiStorageGetSelInfo);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530893
Xiaochao Maf020c442022-10-31 11:09:38 +0800894 // <Get SEL Timezone>
895 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
896 ipmi::storage::cmdGetSelTimeUtcOffset,
897 ipmi::Privilege::User,
898 ipmiStorageGetSelTimeUtcOffset);
899
Tom Josepha4953392017-06-30 19:09:47 +0530900 // <Get SEL Entry>
Patrick Venture0b02be92018-08-31 11:55:55 -0700901 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_ENTRY, NULL,
902 getSELEntry, PRIVILEGE_USER);
Tom Josepha4953392017-06-30 19:09:47 +0530903
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530904 // <Delete SEL Entry>
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000905 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
906 ipmi::storage::cmdDeleteSelEntry,
907 ipmi::Privilege::Operator, deleteSELEntry);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530908
Tom05732372016-09-06 17:21:23 +0530909 // <Add SEL Entry>
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000910 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
911 ipmi::storage::cmdAddSelEntry,
912 ipmi::Privilege::Operator, ipmiStorageAddSEL);
913
Tom Joseph2f05bb52017-06-30 19:14:49 +0530914 // <Clear SEL>
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000915 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
916 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
917 clearSEL);
918
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500919 // <Get FRU Inventory Area Info>
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000920 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
921 ipmi::storage::cmdGetFruInventoryAreaInfo,
922 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500923
Jason M. Billsb5248c92019-06-24 15:53:08 -0700924 // <READ FRU Data>
anil kumar appana5b7c3262019-05-27 18:10:23 +0000925 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
926 ipmi::storage::cmdReadFruData,
927 ipmi::Privilege::Operator, ipmiStorageReadFruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500928
Thang Tran292c9172023-06-26 10:03:04 +0700929#endif // FEATURE_DYNAMIC_STORAGES_ONLY
930
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600931 // <Get Repository Info>
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000932 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
933 ipmi::storage::cmdGetSdrRepositoryInfo,
934 ipmi::Privilege::User, ipmiGetRepositoryInfo);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600935
Tom Joseph5ca50952018-02-22 00:33:38 +0530936 // <Reserve SDR Repository>
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000937 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
938 ipmi::storage::cmdReserveSdrRepository,
939 ipmi::Privilege::User, ipmiSensorReserveSdr);
Tom Joseph5ca50952018-02-22 00:33:38 +0530940
941 // <Get SDR>
Patrick Venture0b02be92018-08-31 11:55:55 -0700942 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SDR, nullptr,
943 ipmi_sen_get_sdr, PRIVILEGE_USER);
Tom Joseph5ca50952018-02-22 00:33:38 +0530944
Willy Tud351a722021-08-12 14:33:40 -0700945#endif
946
947 // Common Handers used by both implementation.
948
949 // <Reserve SEL>
950 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
951 ipmi::storage::cmdReserveSel, ipmi::Privilege::User,
952 ipmiStorageReserveSel);
953
Thang Trandbf66e12023-09-21 13:07:24 +0700954 // <Get SEL Time>
955 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
956 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
957 ipmiStorageGetSelTime);
958
959 // <Set SEL Time>
960 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
961 ipmi::storage::cmdSetSelTime,
962 ipmi::Privilege::Operator, ipmiStorageSetSelTime);
963
Marri Devender Rao908f7502017-07-10 01:49:54 -0500964 ipmi::fru::registerCallbackHandler();
Chris Austenb4f5b922015-10-13 12:44:43 -0500965 return;
966}