blob: f846ccd8b003c3aa0485fb98c34fe9b31bf108d1 [file] [log] [blame]
Patrick Williams768730d2023-09-06 16:27:20 -05001#include "config.h"
2
Patrick Venture3a5071a2018-09-12 13:27:42 -07003#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 <systemd/sd-bus.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070011
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050012#include <ipmid/api.hpp>
Vernon Mauery9cf08382023-04-28 14:00:11 -070013#include <ipmid/entity_map_json.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050014#include <ipmid/utils.hpp>
15#include <phosphor-logging/elog-errors.hpp>
16#include <phosphor-logging/elog.hpp>
George Liu51856e62024-07-17 19:46:03 +080017#include <phosphor-logging/lg2.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050018#include <sdbusplus/server.hpp>
19#include <xyz/openbmc_project/Common/error.hpp>
20#include <xyz/openbmc_project/Logging/SEL/error.hpp>
21
Patrick Venture0b02be92018-08-31 11:55:55 -070022#include <algorithm>
Lei YU52d91242017-10-17 22:52:28 +080023#include <chrono>
Jayanth Othayoth5f9ed922024-12-08 09:33:58 -060024#include <cstdint>
Lei YU52d91242017-10-17 22:52:28 +080025#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";
Patrick Venture0b02be92018-08-31 11:55:55 -070055} // namespace
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +053056
Tom Joseph6f7deaa2017-06-30 19:03:54 +053057using InternalFailure =
Willy Tu523e2d12023-09-05 11:36:48 -070058 sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
Tom Joseph6f7deaa2017-06-30 19:03:54 +053059using namespace phosphor::logging;
Marri Devender Raocac383b2017-07-03 13:24:27 -050060using namespace ipmi::fru;
Willy Tu523e2d12023-09-05 11:36:48 -070061using namespace xyz::openbmc_project::logging::sel;
Lotus Xu57d35572021-01-24 11:13:13 +080062using SELCreated =
Willy Tu523e2d12023-09-05 11:36:48 -070063 sdbusplus::error::xyz::openbmc_project::logging::sel::Created;
Marri Devender Raocac383b2017-07-03 13:24:27 -050064
Lei YUd9e57662021-09-14 18:06:28 +080065using SELRecordID = uint16_t;
66using SELEntry = ipmi::sel::SELEventRecordFormat;
67using SELCacheMap = std::map<SELRecordID, SELEntry>;
68
69SELCacheMap selCacheMap __attribute__((init_priority(101)));
Lei YU3df36612021-09-15 11:41:11 +080070bool selCacheMapInitialized;
Patrick Williams5d82f472022-07-22 19:26:53 -050071std::unique_ptr<sdbusplus::bus::match_t> selAddedMatch
Lei YUd9555252021-09-14 20:30:41 +080072 __attribute__((init_priority(101)));
Patrick Williams5d82f472022-07-22 19:26:53 -050073std::unique_ptr<sdbusplus::bus::match_t> selRemovedMatch
Lei YUb6b72b02021-09-14 21:06:05 +080074 __attribute__((init_priority(101)));
Patrick Williams5d82f472022-07-22 19:26:53 -050075std::unique_ptr<sdbusplus::bus::match_t> selUpdatedMatch
Lei YU4106e422021-09-15 10:55:51 +080076 __attribute__((init_priority(101)));
Lei YUd9e57662021-09-14 18:06:28 +080077
Lei YUb6b72b02021-09-14 21:06:05 +080078static inline uint16_t getLoggingId(const std::string& p)
Lei YUd9e57662021-09-14 18:06:28 +080079{
80 namespace fs = std::filesystem;
81 fs::path entryPath(p);
Lei YUb6b72b02021-09-14 21:06:05 +080082 return std::stoul(entryPath.filename().string());
83}
84
Lei YUeba8e9a2021-09-15 13:16:17 +080085static inline std::string getLoggingObjPath(uint16_t id)
86{
87 return std::string(ipmi::sel::logBasePath) + "/" + std::to_string(id);
88}
89
Patrick Williams56c04e22025-03-03 11:19:13 -050090std::optional<std::pair<uint16_t, SELEntry>> parseLoggingEntry(
91 const std::string& p)
Lei YUb6b72b02021-09-14 21:06:05 +080092{
Lei YU3df36612021-09-15 11:41:11 +080093 try
94 {
Lei YUa0bb2a32021-09-24 11:03:56 +080095 auto id = getLoggingId(p);
96 ipmi::sel::GetSELEntryResponse record{};
Lei YU3df36612021-09-15 11:41:11 +080097 record = ipmi::sel::convertLogEntrytoSEL(p);
Lei YUa0bb2a32021-09-24 11:03:56 +080098 return std::pair<uint16_t, SELEntry>({id, std::move(record.event)});
Lei YU3df36612021-09-15 11:41:11 +080099 }
100 catch (const std::exception& e)
101 {
102 fprintf(stderr, "Failed to convert %s to SEL: %s\n", p.c_str(),
103 e.what());
104 }
Lei YUa0bb2a32021-09-24 11:03:56 +0800105 return std::nullopt;
Lei YUd9e57662021-09-14 18:06:28 +0800106}
107
Patrick Williams5d82f472022-07-22 19:26:53 -0500108static void selAddedCallback(sdbusplus::message_t& m)
Lei YUd9555252021-09-14 20:30:41 +0800109{
110 sdbusplus::message::object_path objPath;
111 try
112 {
113 m.read(objPath);
114 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500115 catch (const sdbusplus::exception_t& e)
Lei YUd9555252021-09-14 20:30:41 +0800116 {
George Liu51856e62024-07-17 19:46:03 +0800117 lg2::error("Failed to read object path");
Lei YUd9555252021-09-14 20:30:41 +0800118 return;
119 }
120 std::string p = objPath;
Lei YUa0bb2a32021-09-24 11:03:56 +0800121 auto entry = parseLoggingEntry(p);
122 if (entry)
123 {
124 selCacheMap.insert(std::move(*entry));
125 }
Lei YUd9555252021-09-14 20:30:41 +0800126}
127
Patrick Williams5d82f472022-07-22 19:26:53 -0500128static void selRemovedCallback(sdbusplus::message_t& m)
Lei YUb6b72b02021-09-14 21:06:05 +0800129{
130 sdbusplus::message::object_path objPath;
131 try
132 {
133 m.read(objPath);
134 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500135 catch (const sdbusplus::exception_t& e)
Lei YUb6b72b02021-09-14 21:06:05 +0800136 {
George Liu51856e62024-07-17 19:46:03 +0800137 lg2::error("Failed to read object path");
Lei YUb6b72b02021-09-14 21:06:05 +0800138 }
Lei YUa0bb2a32021-09-24 11:03:56 +0800139 try
140 {
141 std::string p = objPath;
142 selCacheMap.erase(getLoggingId(p));
143 }
144 catch (const std::invalid_argument& e)
145 {
George Liu51856e62024-07-17 19:46:03 +0800146 lg2::error("Invalid logging entry ID");
Lei YUa0bb2a32021-09-24 11:03:56 +0800147 }
Lei YUb6b72b02021-09-14 21:06:05 +0800148}
149
Patrick Williams5d82f472022-07-22 19:26:53 -0500150static void selUpdatedCallback(sdbusplus::message_t& m)
Lei YU4106e422021-09-15 10:55:51 +0800151{
152 std::string p = m.get_path();
153 auto entry = parseLoggingEntry(p);
Lei YUa0bb2a32021-09-24 11:03:56 +0800154 if (entry)
155 {
156 selCacheMap.insert_or_assign(entry->first, std::move(entry->second));
157 }
Lei YU4106e422021-09-15 10:55:51 +0800158}
159
Lei YUd9555252021-09-14 20:30:41 +0800160void registerSelCallbackHandler()
161{
162 using namespace sdbusplus::bus::match::rules;
Patrick Williams5d82f472022-07-22 19:26:53 -0500163 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Lei YUd9555252021-09-14 20:30:41 +0800164 if (!selAddedMatch)
165 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500166 selAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
George Liu656ae3c2024-08-23 16:44:22 +0800167 bus, interfacesAdded(ipmi::sel::logWatchPath),
Lei YUd9555252021-09-14 20:30:41 +0800168 std::bind(selAddedCallback, std::placeholders::_1));
169 }
Lei YUb6b72b02021-09-14 21:06:05 +0800170 if (!selRemovedMatch)
171 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500172 selRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
George Liu656ae3c2024-08-23 16:44:22 +0800173 bus, interfacesRemoved(ipmi::sel::logWatchPath),
Lei YUb6b72b02021-09-14 21:06:05 +0800174 std::bind(selRemovedCallback, std::placeholders::_1));
175 }
Lei YU4106e422021-09-15 10:55:51 +0800176 if (!selUpdatedMatch)
177 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500178 selUpdatedMatch = std::make_unique<sdbusplus::bus::match_t>(
Lei YU4106e422021-09-15 10:55:51 +0800179 bus,
180 type::signal() + member("PropertiesChanged"s) +
181 interface("org.freedesktop.DBus.Properties"s) +
George Liu656ae3c2024-08-23 16:44:22 +0800182 argN(0, ipmi::sel::logEntryIntf),
Lei YU4106e422021-09-15 10:55:51 +0800183 std::bind(selUpdatedCallback, std::placeholders::_1));
184 }
Lei YUd9555252021-09-14 20:30:41 +0800185}
186
Lei YUd9e57662021-09-14 18:06:28 +0800187void initSELCache()
188{
Lei YUc10cda12022-01-06 16:01:57 +0800189 registerSelCallbackHandler();
Lei YUeba8e9a2021-09-15 13:16:17 +0800190 ipmi::sel::ObjectPaths paths;
Lei YUd9e57662021-09-14 18:06:28 +0800191 try
192 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800193 ipmi::sel::readLoggingObjectPaths(paths);
Lei YUd9e57662021-09-14 18:06:28 +0800194 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500195 catch (const sdbusplus::exception_t& e)
Lei YUd9e57662021-09-14 18:06:28 +0800196 {
George Liu51856e62024-07-17 19:46:03 +0800197 lg2::error("Failed to get logging object paths");
Lei YUd9e57662021-09-14 18:06:28 +0800198 return;
199 }
Lei YUeba8e9a2021-09-15 13:16:17 +0800200 for (const auto& p : paths)
Lei YUd9e57662021-09-14 18:06:28 +0800201 {
Lei YUa0bb2a32021-09-24 11:03:56 +0800202 auto entry = parseLoggingEntry(p);
203 if (entry)
204 {
205 selCacheMap.insert(std::move(*entry));
206 }
Lei YUd9e57662021-09-14 18:06:28 +0800207 }
Lei YU3df36612021-09-15 11:41:11 +0800208 selCacheMapInitialized = true;
Lei YUd9e57662021-09-14 18:06:28 +0800209}
210
Marri Devender Raocac383b2017-07-03 13:24:27 -0500211/**
212 * @enum Device access mode
213 */
214enum class AccessMode
215{
216 bytes, ///< Device is accessed by bytes
217 words ///< Device is accessed by words
218};
219
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000220/** @brief implements the get SEL Info command
221 * @returns IPMI completion code plus response data
222 * - selVersion - SEL revision
223 * - entries - Number of log entries in SEL.
224 * - freeSpace - Free Space in bytes.
225 * - addTimeStamp - Most recent addition timestamp
226 * - eraseTimeStamp - Most recent erase timestamp
227 * - operationSupport - Reserve & Delete SEL operations supported
228 */
229
230ipmi::RspType<uint8_t, // SEL revision.
231 uint16_t, // number of log entries in SEL.
232 uint16_t, // free Space in bytes.
233 uint32_t, // most recent addition timestamp
234 uint32_t, // most recent erase timestamp.
235
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500236 bool, // SEL allocation info supported
237 bool, // reserve SEL supported
238 bool, // partial Add SEL Entry supported
239 bool, // delete SEL supported
240 uint3_t, // reserved
241 bool // overflow flag
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000242 >
243 ipmiStorageGetSelInfo()
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530244{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000245 uint16_t entries = 0;
246 // Most recent addition timestamp.
247 uint32_t addTimeStamp = ipmi::sel::invalidTimeStamp;
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530248
Lei YUeba8e9a2021-09-15 13:16:17 +0800249 if (!selCacheMapInitialized)
Tom Josephe59abfb2018-08-06 18:46:27 +0530250 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800251 // In case the initSELCache() fails, try it again
252 initSELCache();
Tom Josephe59abfb2018-08-06 18:46:27 +0530253 }
Lei YUeba8e9a2021-09-15 13:16:17 +0800254 if (!selCacheMap.empty())
Tom Josephe59abfb2018-08-06 18:46:27 +0530255 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800256 entries = static_cast<uint16_t>(selCacheMap.size());
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530257
258 try
259 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800260 auto objPath = getLoggingObjPath(selCacheMap.rbegin()->first);
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000261 addTimeStamp = static_cast<uint32_t>(
Lei YUeba8e9a2021-09-15 13:16:17 +0800262 (ipmi::sel::getEntryTimeStamp(objPath).count()));
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530263 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500264 catch (const InternalFailure& e)
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500265 {}
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530266 catch (const std::runtime_error& e)
267 {
George Liu51856e62024-07-17 19:46:03 +0800268 lg2::error("runtime error: {ERROR}", "ERROR", e);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530269 }
270 }
271
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000272 constexpr uint8_t selVersion = ipmi::sel::selVersion;
273 constexpr uint16_t freeSpace = 0xFFFF;
274 constexpr uint32_t eraseTimeStamp = ipmi::sel::invalidTimeStamp;
275 constexpr uint3_t reserved{0};
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530276
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000277 return ipmi::responseSuccess(
278 selVersion, entries, freeSpace, addTimeStamp, eraseTimeStamp,
279 ipmi::sel::operationSupport::getSelAllocationInfo,
280 ipmi::sel::operationSupport::reserveSel,
281 ipmi::sel::operationSupport::partialAddSelEntry,
282 ipmi::sel::operationSupport::deleteSel, reserved,
283 ipmi::sel::operationSupport::overflow);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530284}
285
Willy Tu11d68892022-01-20 10:37:34 -0800286ipmi_ret_t getSELEntry(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request,
287 ipmi_response_t response, ipmi_data_len_t data_len,
288 ipmi_context_t)
Tom Josepha4953392017-06-30 19:09:47 +0530289{
Jason M. Bills851acb12019-02-04 14:06:57 -0800290 if (*data_len != sizeof(ipmi::sel::GetSELEntryRequest))
291 {
292 *data_len = 0;
293 return IPMI_CC_REQ_DATA_LEN_INVALID;
294 }
295
Patrick Venture0b02be92018-08-31 11:55:55 -0700296 auto requestData =
297 reinterpret_cast<const ipmi::sel::GetSELEntryRequest*>(request);
Tom Josepha4953392017-06-30 19:09:47 +0530298
299 if (requestData->reservationID != 0)
300 {
Jason M. Bills13e67c82018-09-10 14:12:16 -0700301 if (!checkSELReservation(requestData->reservationID))
Tom Josepha4953392017-06-30 19:09:47 +0530302 {
303 *data_len = 0;
304 return IPMI_CC_INVALID_RESERVATION_ID;
305 }
306 }
307
Lei YU3df36612021-09-15 11:41:11 +0800308 if (!selCacheMapInitialized)
309 {
310 // In case the initSELCache() fails, try it again
311 initSELCache();
312 }
313
314 if (selCacheMap.empty())
Tom Josepha4953392017-06-30 19:09:47 +0530315 {
316 *data_len = 0;
317 return IPMI_CC_SENSOR_INVALID;
318 }
319
Lei YU3df36612021-09-15 11:41:11 +0800320 SELCacheMap::const_iterator iter;
Tom Josepha4953392017-06-30 19:09:47 +0530321
322 // Check for the requested SEL Entry.
323 if (requestData->selRecordID == ipmi::sel::firstEntry)
324 {
Lei YU3df36612021-09-15 11:41:11 +0800325 iter = selCacheMap.begin();
Tom Josepha4953392017-06-30 19:09:47 +0530326 }
327 else if (requestData->selRecordID == ipmi::sel::lastEntry)
328 {
Lei YU3df36612021-09-15 11:41:11 +0800329 if (selCacheMap.size() > 1)
330 {
331 iter = selCacheMap.end();
332 --iter;
333 }
334 else
335 {
336 // Only one entry exists, return the first
337 iter = selCacheMap.begin();
338 }
Tom Josepha4953392017-06-30 19:09:47 +0530339 }
340 else
341 {
Lei YU3df36612021-09-15 11:41:11 +0800342 iter = selCacheMap.find(requestData->selRecordID);
343 if (iter == selCacheMap.end())
Tom Josepha4953392017-06-30 19:09:47 +0530344 {
345 *data_len = 0;
346 return IPMI_CC_SENSOR_INVALID;
347 }
348 }
349
Lei YU3df36612021-09-15 11:41:11 +0800350 ipmi::sel::GetSELEntryResponse record{0, iter->second};
Tom Josepha4953392017-06-30 19:09:47 +0530351 // Identify the next SEL record ID
Lei YU3df36612021-09-15 11:41:11 +0800352 ++iter;
353 if (iter == selCacheMap.end())
Tom Josepha4953392017-06-30 19:09:47 +0530354 {
Lei YU3df36612021-09-15 11:41:11 +0800355 record.nextRecordID = ipmi::sel::lastEntry;
Tom Josepha4953392017-06-30 19:09:47 +0530356 }
357 else
358 {
Lei YU3df36612021-09-15 11:41:11 +0800359 record.nextRecordID = iter->first;
Tom Josepha4953392017-06-30 19:09:47 +0530360 }
361
362 if (requestData->readLength == ipmi::sel::entireRecord)
363 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700364 std::memcpy(response, &record, sizeof(record));
Tom Josepha4953392017-06-30 19:09:47 +0530365 *data_len = sizeof(record);
366 }
367 else
368 {
369 if (requestData->offset >= ipmi::sel::selRecordSize ||
370 requestData->readLength > ipmi::sel::selRecordSize)
371 {
372 *data_len = 0;
373 return IPMI_CC_INVALID_FIELD_REQUEST;
374 }
375
376 auto diff = ipmi::sel::selRecordSize - requestData->offset;
Patrick Williams1318a5e2024-08-16 15:19:54 -0400377 auto readLength =
378 std::min(diff, static_cast<int>(requestData->readLength));
Tom Josepha4953392017-06-30 19:09:47 +0530379
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700380 std::memcpy(response, &record.nextRecordID,
381 sizeof(record.nextRecordID));
Jayanth Othayoth5f9ed922024-12-08 09:33:58 -0600382 auto tempRecordID = htole16(record.event.eventRecord.recordID);
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700383 std::memcpy(static_cast<uint8_t*>(response) +
384 sizeof(record.nextRecordID),
Jayanth Othayoth5f9ed922024-12-08 09:33:58 -0600385 reinterpret_cast<const uint8_t*>(&tempRecordID) +
386 requestData->offset,
Lei YUaf378fa2020-12-02 16:28:57 +0800387 readLength);
Tom Josepha4953392017-06-30 19:09:47 +0530388 *data_len = sizeof(record.nextRecordID) + readLength;
389 }
390
391 return IPMI_CC_OK;
392}
393
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000394/** @brief implements the delete SEL entry command
395 * @request
396 * - reservationID; // reservation ID.
397 * - selRecordID; // SEL record ID.
398 *
399 * @returns ipmi completion code plus response data
400 * - Record ID of the deleted record
401 */
402ipmi::RspType<uint16_t // deleted record ID
403 >
404 deleteSELEntry(uint16_t reservationID, uint16_t selRecordID)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530405{
Brad Bishop1a4117b2018-11-21 15:48:18 -0500406 namespace fs = std::filesystem;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530407
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000408 if (!checkSELReservation(reservationID))
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530409 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000410 return ipmi::responseInvalidReservationId();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530411 }
412
Jason M. Bills13e67c82018-09-10 14:12:16 -0700413 // Per the IPMI spec, need to cancel the reservation when a SEL entry is
414 // deleted
415 cancelSELReservation();
416
Lei YUeba8e9a2021-09-15 13:16:17 +0800417 if (!selCacheMapInitialized)
Tom Josephe59abfb2018-08-06 18:46:27 +0530418 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800419 // In case the initSELCache() fails, try it again
420 initSELCache();
Tom Josephe59abfb2018-08-06 18:46:27 +0530421 }
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530422
Lei YUeba8e9a2021-09-15 13:16:17 +0800423 if (selCacheMap.empty())
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530424 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000425 return ipmi::responseSensorInvalid();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530426 }
427
Lei YUeba8e9a2021-09-15 13:16:17 +0800428 SELCacheMap::const_iterator iter;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530429 uint16_t delRecordID = 0;
430
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000431 if (selRecordID == ipmi::sel::firstEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530432 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800433 delRecordID = selCacheMap.begin()->first;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530434 }
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000435 else if (selRecordID == ipmi::sel::lastEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530436 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800437 delRecordID = selCacheMap.rbegin()->first;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530438 }
439 else
440 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000441 delRecordID = selRecordID;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530442 }
443
Jian7d5f4812022-02-23 19:30:27 +0800444 iter = selCacheMap.find(delRecordID);
445 if (iter == selCacheMap.end())
446 {
447 return ipmi::responseSensorInvalid();
448 }
449
Patrick Williams5d82f472022-07-22 19:26:53 -0500450 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530451 std::string service;
452
Lei YUeba8e9a2021-09-15 13:16:17 +0800453 auto objPath = getLoggingObjPath(iter->first);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530454 try
455 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800456 service = ipmi::getService(bus, ipmi::sel::logDeleteIntf, objPath);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530457 }
458 catch (const std::runtime_error& e)
459 {
George Liu51856e62024-07-17 19:46:03 +0800460 lg2::error("runtime error: {ERROR}", "ERROR", e);
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000461 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530462 }
463
Lei YUeba8e9a2021-09-15 13:16:17 +0800464 auto methodCall = bus.new_method_call(service.c_str(), objPath.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700465 ipmi::sel::logDeleteIntf, "Delete");
George Liu3e3cc352023-07-26 15:59:31 +0800466 try
467 {
468 auto reply = bus.call(methodCall);
469 }
470 catch (const std::exception& e)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530471 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000472 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530473 }
474
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000475 return ipmi::responseSuccess(delRecordID);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530476}
477
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000478/** @brief implements the Clear SEL command
479 * @request
480 * - reservationID // Reservation ID.
481 * - clr // char array { 'C'(0x43h), 'L'(0x4Ch), 'R'(0x52h) }
482 * - eraseOperation; // requested operation.
483 *
484 * @returns ipmi completion code plus response data
485 * - erase status
486 */
487
488ipmi::RspType<uint8_t // erase status
489 >
490 clearSEL(uint16_t reservationID, const std::array<char, 3>& clr,
491 uint8_t eraseOperation)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530492{
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000493 static constexpr std::array<char, 3> clrOk = {'C', 'L', 'R'};
494 if (clr != clrOk)
Jason M. Bills851acb12019-02-04 14:06:57 -0800495 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000496 return ipmi::responseInvalidFieldRequest();
Jason M. Bills851acb12019-02-04 14:06:57 -0800497 }
498
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000499 if (!checkSELReservation(reservationID))
Tom Joseph2f05bb52017-06-30 19:14:49 +0530500 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000501 return ipmi::responseInvalidReservationId();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530502 }
503
Tom Joseph2f05bb52017-06-30 19:14:49 +0530504 /*
505 * Erasure status cannot be fetched from DBUS, so always return erasure
506 * status as `erase completed`.
507 */
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000508 if (eraseOperation == ipmi::sel::getEraseStatus)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530509 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000510 return ipmi::responseSuccess(
511 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530512 }
513
Xiaoyu Ma73d07772024-02-04 10:31:37 +0800514 // Check that initiate erase is correct
515 if (eraseOperation != ipmi::sel::initiateErase)
516 {
517 return ipmi::responseInvalidFieldRequest();
518 }
519
Jason M. Bills13e67c82018-09-10 14:12:16 -0700520 // Per the IPMI spec, need to cancel any reservation when the SEL is cleared
521 cancelSELReservation();
522
Patrick Williams5d82f472022-07-22 19:26:53 -0500523 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Lotus Xu78fe1b12021-05-23 17:26:56 +0800524 auto service = ipmi::getService(bus, ipmi::sel::logIntf, ipmi::sel::logObj);
Patrick Williams1318a5e2024-08-16 15:19:54 -0400525 auto method =
526 bus.new_method_call(service.c_str(), ipmi::sel::logObj,
527 ipmi::sel::logIntf, ipmi::sel::logDeleteAllMethod);
Tom Josephe59abfb2018-08-06 18:46:27 +0530528 try
Tom Joseph2f05bb52017-06-30 19:14:49 +0530529 {
Lotus Xu78fe1b12021-05-23 17:26:56 +0800530 bus.call_noreply(method);
Tom Josephe59abfb2018-08-06 18:46:27 +0530531 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500532 catch (const sdbusplus::exception_t& e)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530533 {
George Liu51856e62024-07-17 19:46:03 +0800534 lg2::error("Error eraseAll: {ERROR}", "ERROR", e);
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000535 return ipmi::responseUnspecifiedError();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530536 }
537
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000538 return ipmi::responseSuccess(
539 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530540}
541
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000542/** @brief implements the get SEL time command
543 * @returns IPMI completion code plus response data
544 * -current time
545 */
546ipmi::RspType<uint32_t> // current time
547 ipmiStorageGetSelTime()
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500548{
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530549 using namespace std::chrono;
George Liu4d623e92020-05-25 16:51:57 +0800550 uint64_t bmc_time_usec = 0;
551 std::stringstream bmcTime;
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500552
Lei YUe8939392017-06-15 10:45:05 +0800553 try
554 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500555 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
George Liu4d623e92020-05-25 16:51:57 +0800556 auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
George Liu42f64ef2024-02-05 15:03:18 +0800557 auto propValue = ipmi::getDbusProperty(
558 bus, service, BMC_TIME_PATH, TIME_INTERFACE, PROPERTY_ELAPSED);
559 bmc_time_usec = std::get<uint64_t>(propValue);
Lei YUe8939392017-06-15 10:45:05 +0800560 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500561 catch (const InternalFailure& e)
Lei YUe8939392017-06-15 10:45:05 +0800562 {
George Liu51856e62024-07-17 19:46:03 +0800563 lg2::error("Internal Failure: {ERROR}", "ERROR", e);
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000564 return ipmi::responseUnspecifiedError();
Lei YUe8939392017-06-15 10:45:05 +0800565 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530566 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800567 {
George Liu51856e62024-07-17 19:46:03 +0800568 lg2::error("exception message: {ERROR}", "ERROR", e);
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000569 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530570 }
571
George Liu51856e62024-07-17 19:46:03 +0800572 lg2::debug("BMC time: {BMC_TIME}", "BMC_TIME",
573 duration_cast<seconds>(microseconds(bmc_time_usec)).count());
Nagaraju Goruganti8960b7c2018-04-29 22:38:40 -0500574
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530575 // Time is really long int but IPMI wants just uint32. This works okay until
576 // the number of seconds since 1970 overflows uint32 size.. Still a whole
577 // lot of time here to even think about that.
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000578 return ipmi::responseSuccess(
George Liu4d623e92020-05-25 16:51:57 +0800579 duration_cast<seconds>(microseconds(bmc_time_usec)).count());
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500580}
581
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000582/** @brief implements the set SEL time command
583 * @param selDeviceTime - epoch time
584 * -local time as the number of seconds from 00:00:00, January 1, 1970
585 * @returns IPMI completion code
586 */
587ipmi::RspType<> ipmiStorageSetSelTime(uint32_t selDeviceTime)
Chris Austenb4f5b922015-10-13 12:44:43 -0500588{
Lei YUe8939392017-06-15 10:45:05 +0800589 using namespace std::chrono;
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000590 microseconds usec{seconds(selDeviceTime)};
Norman James82330442015-11-19 16:53:26 -0600591
Lei YUe8939392017-06-15 10:45:05 +0800592 try
593 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500594 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
shamim ali071d00c2022-03-02 15:42:46 +0530595 bool ntp = std::get<bool>(
596 ipmi::getDbusProperty(bus, SystemdTimeService, SystemdTimePath,
597 SystemdTimeInterface, "NTP"));
598 if (ntp)
599 {
600 return ipmi::responseCommandNotAvailable();
601 }
602
George Liu4d623e92020-05-25 16:51:57 +0800603 auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
Andrew Geissler6467ed22020-05-16 16:03:53 -0500604 std::variant<uint64_t> value{(uint64_t)usec.count()};
Lei YUe8939392017-06-15 10:45:05 +0800605
George Liu4d623e92020-05-25 16:51:57 +0800606 // Set bmc time
607 auto method = bus.new_method_call(service.c_str(), BMC_TIME_PATH,
Patrick Venture0b02be92018-08-31 11:55:55 -0700608 DBUS_PROPERTIES, "Set");
Lei YUe8939392017-06-15 10:45:05 +0800609
610 method.append(TIME_INTERFACE, PROPERTY_ELAPSED, value);
611 auto reply = bus.call(method);
Norman James82330442015-11-19 16:53:26 -0600612 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500613 catch (const InternalFailure& e)
Lei YUe8939392017-06-15 10:45:05 +0800614 {
George Liu51856e62024-07-17 19:46:03 +0800615 lg2::error("Internal Failure: {ERROR}", "ERROR", e);
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000616 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530617 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530618 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800619 {
George Liu51856e62024-07-17 19:46:03 +0800620 lg2::error("exception message: {ERROR}", "ERROR", e);
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000621 return ipmi::responseUnspecifiedError();
Norman James82330442015-11-19 16:53:26 -0600622 }
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530623
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000624 return ipmi::responseSuccess();
Chris Austenb4f5b922015-10-13 12:44:43 -0500625}
626
Xiaochao Maf020c442022-10-31 11:09:38 +0800627/** @brief implements the get SEL timezone command
628 * @returns IPMI completion code plus response data
629 * -current timezone
630 */
631ipmi::RspType<int16_t> ipmiStorageGetSelTimeUtcOffset()
632{
633 time_t timep;
634 struct tm* gmTime;
635 struct tm* localTime;
636
637 time(&timep);
638 localTime = localtime(&timep);
639 auto validLocalTime = mktime(localTime);
640 gmTime = gmtime(&timep);
641 auto validGmTime = mktime(gmTime);
642 auto timeEquation = (validLocalTime - validGmTime) / 60;
643
644 return ipmi::responseSuccess(timeEquation);
645}
646
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000647/** @brief implements the reserve SEL command
648 * @returns IPMI completion code plus response data
649 * - SEL reservation ID.
650 */
651ipmi::RspType<uint16_t> ipmiStorageReserveSel()
Chris Austenb4f5b922015-10-13 12:44:43 -0500652{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000653 return ipmi::responseSuccess(reserveSel());
Chris Austenb4f5b922015-10-13 12:44:43 -0500654}
655
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000656/** @brief implements the Add SEL entry command
657 * @request
658 *
659 * - recordID ID used for SEL Record access
660 * - recordType Record Type
661 * - timeStamp Time when event was logged. LS byte first
662 * - generatorID software ID if event was generated from
663 * system software
664 * - evmRev event message format version
665 * - sensorType sensor type code for service that generated
666 * the event
667 * - sensorNumber number of sensors that generated the event
668 * - eventDir event dir
669 * - eventData event data field contents
670 *
671 * @returns ipmi completion code plus response data
672 * - RecordID of the Added SEL entry
673 */
674ipmi::RspType<uint16_t // recordID of the Added SEL entry
675 >
Willy Tu11d68892022-01-20 10:37:34 -0800676 ipmiStorageAddSEL(uint16_t recordID, uint8_t recordType,
677 [[maybe_unused]] uint32_t timeStamp, uint16_t generatorID,
Patrick Williams32aece12025-01-24 10:06:55 -0500678 [[maybe_unused]] uint8_t evmRev,
679 [[maybe_unused]] uint8_t sensorType, uint8_t sensorNumber,
680 uint8_t eventDir,
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000681 std::array<uint8_t, eventDataSize> eventData)
Chris Austenb4f5b922015-10-13 12:44:43 -0500682{
Lotus Xu57d35572021-01-24 11:13:13 +0800683 std::string objpath;
684 static constexpr auto systemRecordType = 0x02;
Patrick Williams32aece12025-01-24 10:06:55 -0500685#ifdef OPEN_POWER_SUPPORT
Tom Josephb647d5b2017-10-31 17:25:33 +0530686 // Hostboot sends SEL with OEM record type 0xDE to indicate that there is
687 // a maintenance procedure associated with eSEL record.
688 static constexpr auto procedureType = 0xDE;
Patrick Williams32aece12025-01-24 10:06:55 -0500689#endif
Lotus Xu57d35572021-01-24 11:13:13 +0800690 cancelSELReservation();
691 if (recordType == systemRecordType)
692 {
Lotus Xu57d35572021-01-24 11:13:13 +0800693 for (const auto& it : invSensors)
694 {
695 if (it.second.sensorID == sensorNumber)
696 {
697 objpath = it.first;
698 break;
699 }
700 }
701 auto selDataStr = ipmi::sel::toHexStr(eventData);
702
703 bool assert = (eventDir & 0x80) ? false : true;
704
Patrick Williams1318a5e2024-08-16 15:19:54 -0400705 recordID = report<SELCreated>(
706 Created::RECORD_TYPE(recordType),
707 Created::GENERATOR_ID(generatorID),
708 Created::SENSOR_DATA(selDataStr.c_str()),
709 Created::EVENT_DIR(assert), Created::SENSOR_PATH(objpath.c_str()));
Lotus Xu57d35572021-01-24 11:13:13 +0800710 }
Patrick Williams768730d2023-09-06 16:27:20 -0500711#ifdef OPEN_POWER_SUPPORT
Lotus Xu57d35572021-01-24 11:13:13 +0800712 else if (recordType == procedureType)
Tom Josephb647d5b2017-10-31 17:25:33 +0530713 {
714 // In the OEM record type 0xDE, byte 11 in the SEL record indicate the
715 // procedure number.
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000716 createProcedureLogEntry(sensorType);
Tom Josephb647d5b2017-10-31 17:25:33 +0530717 }
Patrick Williams768730d2023-09-06 16:27:20 -0500718#endif
Chris Austenb4f5b922015-10-13 12:44:43 -0500719
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000720 return ipmi::responseSuccess(recordID);
Chris Austenb4f5b922015-10-13 12:44:43 -0500721}
722
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300723bool isFruPresent(ipmi::Context::ptr& ctx, const std::string& fruPath)
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300724{
725 using namespace ipmi::fru;
726
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300727 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -0400728 boost::system::error_code ec =
729 getService(ctx, invItemInterface, invObjPath + fruPath, service);
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300730 if (!ec)
731 {
732 bool result;
733 ec = ipmi::getDbusProperty(ctx, service, invObjPath + fruPath,
734 invItemInterface, itemPresentProp, result);
735 if (!ec)
736 {
737 return result;
738 }
739 }
Konstantin Aladyshevc7e4b042021-12-24 15:30:47 +0300740
741 ipmi::ObjectValueTree managedObjects;
Nan Zhou947da1b2022-09-20 20:40:59 +0000742 ec = getManagedObjects(ctx, "xyz.openbmc_project.EntityManager",
743 "/xyz/openbmc_project/inventory", managedObjects);
Konstantin Aladyshevc7e4b042021-12-24 15:30:47 +0300744 if (!ec)
745 {
746 auto connection = managedObjects.find(fruPath);
747 if (connection != managedObjects.end())
748 {
749 return true;
750 }
751 }
752
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300753 return false;
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300754}
755
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000756/** @brief implements the get FRU Inventory Area Info command
757 *
758 * @returns IPMI completion code plus response data
759 * - FRU Inventory area size in bytes,
760 * - access bit
761 **/
762ipmi::RspType<uint16_t, // FRU Inventory area size in bytes,
763 uint8_t // access size (bytes / words)
764 >
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300765 ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruID)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500766{
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000767 auto iter = frus.find(fruID);
Tom Joseph187f5642018-03-29 13:49:06 +0530768 if (iter == frus.end())
769 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000770 return ipmi::responseSensorInvalid();
Tom Joseph187f5642018-03-29 13:49:06 +0530771 }
772
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300773 auto path = iter->second[0].path;
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300774 if (!isFruPresent(ctx, path))
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300775 {
776 return ipmi::responseSensorInvalid();
777 }
778
Marri Devender Raocac383b2017-07-03 13:24:27 -0500779 try
780 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000781 return ipmi::responseSuccess(
782 static_cast<uint16_t>(getFruAreaData(fruID).size()),
783 static_cast<uint8_t>(AccessMode::bytes));
Marri Devender Raocac383b2017-07-03 13:24:27 -0500784 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700785 catch (const InternalFailure& e)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500786 {
George Liu51856e62024-07-17 19:46:03 +0800787 lg2::error("Internal Failure: {ERROR}", "ERROR", e);
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000788 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500789 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500790}
791
anil kumar appana5b7c3262019-05-27 18:10:23 +0000792/**@brief implements the Read FRU Data command
793 * @param fruDeviceId - FRU device ID. FFh = reserved
794 * @param offset - FRU inventory offset to read
795 * @param readCount - count to read
796 *
797 * @return IPMI completion code plus response data
798 * - returnCount - response data count.
799 * - data - response data
800 */
801ipmi::RspType<uint8_t, // count returned
802 std::vector<uint8_t>> // FRU data
803 ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t offset,
804 uint8_t readCount)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500805{
anil kumar appana5b7c3262019-05-27 18:10:23 +0000806 if (fruDeviceId == 0xFF)
Tom Joseph187f5642018-03-29 13:49:06 +0530807 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000808 return ipmi::responseInvalidFieldRequest();
Tom Joseph187f5642018-03-29 13:49:06 +0530809 }
810
anil kumar appana5b7c3262019-05-27 18:10:23 +0000811 auto iter = frus.find(fruDeviceId);
812 if (iter == frus.end())
813 {
814 return ipmi::responseSensorInvalid();
815 }
816
Marri Devender Raocac383b2017-07-03 13:24:27 -0500817 try
818 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000819 const auto& fruArea = getFruAreaData(fruDeviceId);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500820 auto size = fruArea.size();
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500821
Tom Josephefcd68b2018-04-26 18:46:27 +0530822 if (offset >= size)
823 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000824 return ipmi::responseParmOutOfRange();
Tom Josephefcd68b2018-04-26 18:46:27 +0530825 }
826
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500827 // Write the count of response data.
anil kumar appana5b7c3262019-05-27 18:10:23 +0000828 uint8_t returnCount;
829 if ((offset + readCount) <= size)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500830 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000831 returnCount = readCount;
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500832 }
833 else
834 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000835 returnCount = size - offset;
Marri Devender Raocac383b2017-07-03 13:24:27 -0500836 }
Ratan Gupta2848d602018-01-31 20:39:20 +0530837
anil kumar appana5b7c3262019-05-27 18:10:23 +0000838 std::vector<uint8_t> fruData((fruArea.begin() + offset),
839 (fruArea.begin() + offset + returnCount));
Ratan Gupta2848d602018-01-31 20:39:20 +0530840
anil kumar appana5b7c3262019-05-27 18:10:23 +0000841 return ipmi::responseSuccess(returnCount, fruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500842 }
843 catch (const InternalFailure& e)
844 {
George Liu51856e62024-07-17 19:46:03 +0800845 lg2::error("Internal Failure: {ERROR}", "ERROR", e);
anil kumar appana5b7c3262019-05-27 18:10:23 +0000846 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500847 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500848}
849
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000850ipmi::RspType<uint8_t, // SDR version
851 uint16_t, // record count LS first
852 uint16_t, // free space in bytes, LS first
853 uint32_t, // addition timestamp LS first
854 uint32_t, // deletion timestamp LS first
855 uint8_t> // operation Support
856 ipmiGetRepositoryInfo()
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600857{
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000858 constexpr uint8_t sdrVersion = 0x51;
859 constexpr uint16_t freeSpace = 0xFFFF;
860 constexpr uint32_t additionTimestamp = 0x0;
861 constexpr uint32_t deletionTimestamp = 0x0;
862 constexpr uint8_t operationSupport = 0;
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600863
Willy Tu0ca3bfe2022-01-28 09:40:18 -0800864 // Get SDR count. This returns the total number of SDRs in the device.
865 const auto& entityRecords =
866 ipmi::sensor::EntityInfoMapContainer::getContainer()
867 ->getIpmiEntityRecords();
Patrick Williams1318a5e2024-08-16 15:19:54 -0400868 uint16_t records =
869 ipmi::sensor::sensors.size() + frus.size() + entityRecords.size();
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600870
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000871 return ipmi::responseSuccess(sdrVersion, records, freeSpace,
872 additionTimestamp, deletionTimestamp,
873 operationSupport);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600874}
Chris Austenb4f5b922015-10-13 12:44:43 -0500875
Chris Austenb4f5b922015-10-13 12:44:43 -0500876void register_netfn_storage_functions()
877{
Lei YU3df36612021-09-15 11:41:11 +0800878 selCacheMapInitialized = false;
Lei YUd9e57662021-09-14 18:06:28 +0800879 initSELCache();
Willy Tud351a722021-08-12 14:33:40 -0700880 // Handlers with dbus-sdr handler implementation.
881 // Do not register the hander if it dynamic sensors stack is used.
882
883#ifndef FEATURE_DYNAMIC_SENSORS
Thang Tran292c9172023-06-26 10:03:04 +0700884
885#ifndef FEATURE_DYNAMIC_STORAGES_ONLY
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530886 // <Get SEL Info>
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000887 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
888 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
889 ipmiStorageGetSelInfo);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530890
Xiaochao Maf020c442022-10-31 11:09:38 +0800891 // <Get SEL Timezone>
892 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
893 ipmi::storage::cmdGetSelTimeUtcOffset,
894 ipmi::Privilege::User,
895 ipmiStorageGetSelTimeUtcOffset);
896
Tom Josepha4953392017-06-30 19:09:47 +0530897 // <Get SEL Entry>
George Liue6cd4df2024-12-05 17:07:59 +0800898 ipmi_register_callback(NETFUN_STORAGE, ipmi::storage::cmdGetSelEntry, NULL,
Patrick Venture0b02be92018-08-31 11:55:55 -0700899 getSELEntry, PRIVILEGE_USER);
Tom Josepha4953392017-06-30 19:09:47 +0530900
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530901 // <Delete SEL Entry>
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000902 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
903 ipmi::storage::cmdDeleteSelEntry,
904 ipmi::Privilege::Operator, deleteSELEntry);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530905
Tom05732372016-09-06 17:21:23 +0530906 // <Add SEL Entry>
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000907 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
908 ipmi::storage::cmdAddSelEntry,
909 ipmi::Privilege::Operator, ipmiStorageAddSEL);
910
Tom Joseph2f05bb52017-06-30 19:14:49 +0530911 // <Clear SEL>
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000912 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
913 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
914 clearSEL);
915
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500916 // <Get FRU Inventory Area Info>
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000917 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
918 ipmi::storage::cmdGetFruInventoryAreaInfo,
919 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500920
Jason M. Billsb5248c92019-06-24 15:53:08 -0700921 // <READ FRU Data>
anil kumar appana5b7c3262019-05-27 18:10:23 +0000922 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
923 ipmi::storage::cmdReadFruData,
924 ipmi::Privilege::Operator, ipmiStorageReadFruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500925
Thang Tran292c9172023-06-26 10:03:04 +0700926#endif // FEATURE_DYNAMIC_STORAGES_ONLY
927
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600928 // <Get Repository Info>
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000929 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
930 ipmi::storage::cmdGetSdrRepositoryInfo,
931 ipmi::Privilege::User, ipmiGetRepositoryInfo);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600932
Tom Joseph5ca50952018-02-22 00:33:38 +0530933 // <Reserve SDR Repository>
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000934 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
935 ipmi::storage::cmdReserveSdrRepository,
936 ipmi::Privilege::User, ipmiSensorReserveSdr);
Tom Joseph5ca50952018-02-22 00:33:38 +0530937
938 // <Get SDR>
George Liue6cd4df2024-12-05 17:07:59 +0800939 ipmi_register_callback(NETFUN_STORAGE, ipmi::storage::cmdGetSdr, nullptr,
Patrick Venture0b02be92018-08-31 11:55:55 -0700940 ipmi_sen_get_sdr, PRIVILEGE_USER);
Tom Joseph5ca50952018-02-22 00:33:38 +0530941
Willy Tud351a722021-08-12 14:33:40 -0700942#endif
943
944 // Common Handers used by both implementation.
945
946 // <Reserve SEL>
947 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
948 ipmi::storage::cmdReserveSel, ipmi::Privilege::User,
949 ipmiStorageReserveSel);
950
Thang Trandbf66e12023-09-21 13:07:24 +0700951 // <Get SEL Time>
952 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
953 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
954 ipmiStorageGetSelTime);
955
956 // <Set SEL Time>
957 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
958 ipmi::storage::cmdSetSelTime,
959 ipmi::Privilege::Operator, ipmiStorageSetSelTime);
960
Marri Devender Rao908f7502017-07-10 01:49:54 -0500961 ipmi::fru::registerCallbackHandler();
Chris Austenb4f5b922015-10-13 12:44:43 -0500962 return;
963}