blob: d85673f3aa1ccbd46cf782a142933127f9a61ed2 [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>
24#include <cstdio>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070025#include <cstring>
Vernon Mauerybdda8002019-02-26 10:18:51 -080026#include <filesystem>
Lei YUa0bb2a32021-09-24 11:03:56 +080027#include <optional>
Patrick Venture3a5071a2018-09-12 13:27:42 -070028#include <string>
Vernon Mauery16b86932019-05-01 08:36:11 -070029#include <variant>
Patrick Venture3a5071a2018-09-12 13:27:42 -070030
George Liu5087b072025-03-11 19:28:17 +080031void registerNetFnStorageFunctions() __attribute__((constructor));
Chris Austenb4f5b922015-10-13 12:44:43 -050032
Patrick Venture0b02be92018-08-31 11:55:55 -070033unsigned int g_sel_time = 0xFFFFFFFF;
Patrick Venturedb0cbe62019-09-09 14:47:22 -070034namespace ipmi
35{
36namespace sensor
37{
38extern const IdInfoMap sensors;
39} // namespace sensor
40} // namespace ipmi
Lotus Xu57d35572021-01-24 11:13:13 +080041extern const ipmi::sensor::InvObjectIDMap invSensors;
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -060042extern const FruMap frus;
anil kumar appana2c7db1d2019-05-28 11:20:19 +000043constexpr uint8_t eventDataSize = 3;
Patrick Venture0b02be92018-08-31 11:55:55 -070044namespace
45{
shamim ali071d00c2022-03-02 15:42:46 +053046constexpr auto SystemdTimeService = "org.freedesktop.timedate1";
47constexpr auto SystemdTimePath = "/org/freedesktop/timedate1";
48constexpr auto SystemdTimeInterface = "org.freedesktop.timedate1";
49
Lei YUe8939392017-06-15 10:45:05 +080050constexpr auto TIME_INTERFACE = "xyz.openbmc_project.Time.EpochTime";
George Liu4d623e92020-05-25 16:51:57 +080051constexpr auto BMC_TIME_PATH = "/xyz/openbmc_project/time/bmc";
Lei YUe8939392017-06-15 10:45:05 +080052constexpr auto DBUS_PROPERTIES = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070053constexpr auto PROPERTY_ELAPSED = "Elapsed";
Patrick Venture0b02be92018-08-31 11:55:55 -070054} // namespace
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +053055
Tom Joseph6f7deaa2017-06-30 19:03:54 +053056using InternalFailure =
Willy Tu523e2d12023-09-05 11:36:48 -070057 sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
Tom Joseph6f7deaa2017-06-30 19:03:54 +053058using namespace phosphor::logging;
Marri Devender Raocac383b2017-07-03 13:24:27 -050059using namespace ipmi::fru;
Willy Tu523e2d12023-09-05 11:36:48 -070060using namespace xyz::openbmc_project::logging::sel;
Lotus Xu57d35572021-01-24 11:13:13 +080061using SELCreated =
Willy Tu523e2d12023-09-05 11:36:48 -070062 sdbusplus::error::xyz::openbmc_project::logging::sel::Created;
Marri Devender Raocac383b2017-07-03 13:24:27 -050063
Lei YUd9e57662021-09-14 18:06:28 +080064using SELRecordID = uint16_t;
65using SELEntry = ipmi::sel::SELEventRecordFormat;
66using SELCacheMap = std::map<SELRecordID, SELEntry>;
67
68SELCacheMap selCacheMap __attribute__((init_priority(101)));
Lei YU3df36612021-09-15 11:41:11 +080069bool selCacheMapInitialized;
Patrick Williams5d82f472022-07-22 19:26:53 -050070std::unique_ptr<sdbusplus::bus::match_t> selAddedMatch
Lei YUd9555252021-09-14 20:30:41 +080071 __attribute__((init_priority(101)));
Patrick Williams5d82f472022-07-22 19:26:53 -050072std::unique_ptr<sdbusplus::bus::match_t> selRemovedMatch
Lei YUb6b72b02021-09-14 21:06:05 +080073 __attribute__((init_priority(101)));
Patrick Williams5d82f472022-07-22 19:26:53 -050074std::unique_ptr<sdbusplus::bus::match_t> selUpdatedMatch
Lei YU4106e422021-09-15 10:55:51 +080075 __attribute__((init_priority(101)));
Lei YUd9e57662021-09-14 18:06:28 +080076
Lei YUb6b72b02021-09-14 21:06:05 +080077static inline uint16_t getLoggingId(const std::string& p)
Lei YUd9e57662021-09-14 18:06:28 +080078{
79 namespace fs = std::filesystem;
80 fs::path entryPath(p);
Lei YUb6b72b02021-09-14 21:06:05 +080081 return std::stoul(entryPath.filename().string());
82}
83
Lei YUeba8e9a2021-09-15 13:16:17 +080084static inline std::string getLoggingObjPath(uint16_t id)
85{
86 return std::string(ipmi::sel::logBasePath) + "/" + std::to_string(id);
87}
88
Patrick Williams69b4c282025-03-03 11:19:13 -050089std::optional<std::pair<uint16_t, SELEntry>> parseLoggingEntry(
90 const std::string& p)
Lei YUb6b72b02021-09-14 21:06:05 +080091{
Lei YU3df36612021-09-15 11:41:11 +080092 try
93 {
Lei YUa0bb2a32021-09-24 11:03:56 +080094 auto id = getLoggingId(p);
95 ipmi::sel::GetSELEntryResponse record{};
Lei YU3df36612021-09-15 11:41:11 +080096 record = ipmi::sel::convertLogEntrytoSEL(p);
Lei YUa0bb2a32021-09-24 11:03:56 +080097 return std::pair<uint16_t, SELEntry>({id, std::move(record.event)});
Lei YU3df36612021-09-15 11:41:11 +080098 }
99 catch (const std::exception& e)
100 {
101 fprintf(stderr, "Failed to convert %s to SEL: %s\n", p.c_str(),
102 e.what());
103 }
Lei YUa0bb2a32021-09-24 11:03:56 +0800104 return std::nullopt;
Lei YUd9e57662021-09-14 18:06:28 +0800105}
106
Patrick Williams5d82f472022-07-22 19:26:53 -0500107static void selAddedCallback(sdbusplus::message_t& m)
Lei YUd9555252021-09-14 20:30:41 +0800108{
109 sdbusplus::message::object_path objPath;
110 try
111 {
112 m.read(objPath);
113 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500114 catch (const sdbusplus::exception_t& e)
Lei YUd9555252021-09-14 20:30:41 +0800115 {
George Liu51856e62024-07-17 19:46:03 +0800116 lg2::error("Failed to read object path");
Lei YUd9555252021-09-14 20:30:41 +0800117 return;
118 }
119 std::string p = objPath;
Lei YUa0bb2a32021-09-24 11:03:56 +0800120 auto entry = parseLoggingEntry(p);
121 if (entry)
122 {
123 selCacheMap.insert(std::move(*entry));
124 }
Lei YUd9555252021-09-14 20:30:41 +0800125}
126
Patrick Williams5d82f472022-07-22 19:26:53 -0500127static void selRemovedCallback(sdbusplus::message_t& m)
Lei YUb6b72b02021-09-14 21:06:05 +0800128{
129 sdbusplus::message::object_path objPath;
130 try
131 {
132 m.read(objPath);
133 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500134 catch (const sdbusplus::exception_t& e)
Lei YUb6b72b02021-09-14 21:06:05 +0800135 {
George Liu51856e62024-07-17 19:46:03 +0800136 lg2::error("Failed to read object path");
Lei YUb6b72b02021-09-14 21:06:05 +0800137 }
Lei YUa0bb2a32021-09-24 11:03:56 +0800138 try
139 {
140 std::string p = objPath;
141 selCacheMap.erase(getLoggingId(p));
142 }
143 catch (const std::invalid_argument& e)
144 {
George Liu51856e62024-07-17 19:46:03 +0800145 lg2::error("Invalid logging entry ID");
Lei YUa0bb2a32021-09-24 11:03:56 +0800146 }
Lei YUb6b72b02021-09-14 21:06:05 +0800147}
148
Patrick Williams5d82f472022-07-22 19:26:53 -0500149static void selUpdatedCallback(sdbusplus::message_t& m)
Lei YU4106e422021-09-15 10:55:51 +0800150{
151 std::string p = m.get_path();
152 auto entry = parseLoggingEntry(p);
Lei YUa0bb2a32021-09-24 11:03:56 +0800153 if (entry)
154 {
155 selCacheMap.insert_or_assign(entry->first, std::move(entry->second));
156 }
Lei YU4106e422021-09-15 10:55:51 +0800157}
158
Lei YUd9555252021-09-14 20:30:41 +0800159void registerSelCallbackHandler()
160{
161 using namespace sdbusplus::bus::match::rules;
Patrick Williams5d82f472022-07-22 19:26:53 -0500162 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Lei YUd9555252021-09-14 20:30:41 +0800163 if (!selAddedMatch)
164 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500165 selAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
George Liu656ae3c2024-08-23 16:44:22 +0800166 bus, interfacesAdded(ipmi::sel::logWatchPath),
Lei YUd9555252021-09-14 20:30:41 +0800167 std::bind(selAddedCallback, std::placeholders::_1));
168 }
Lei YUb6b72b02021-09-14 21:06:05 +0800169 if (!selRemovedMatch)
170 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500171 selRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
George Liu656ae3c2024-08-23 16:44:22 +0800172 bus, interfacesRemoved(ipmi::sel::logWatchPath),
Lei YUb6b72b02021-09-14 21:06:05 +0800173 std::bind(selRemovedCallback, std::placeholders::_1));
174 }
Lei YU4106e422021-09-15 10:55:51 +0800175 if (!selUpdatedMatch)
176 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500177 selUpdatedMatch = std::make_unique<sdbusplus::bus::match_t>(
Lei YU4106e422021-09-15 10:55:51 +0800178 bus,
179 type::signal() + member("PropertiesChanged"s) +
180 interface("org.freedesktop.DBus.Properties"s) +
George Liu656ae3c2024-08-23 16:44:22 +0800181 argN(0, ipmi::sel::logEntryIntf),
Lei YU4106e422021-09-15 10:55:51 +0800182 std::bind(selUpdatedCallback, std::placeholders::_1));
183 }
Lei YUd9555252021-09-14 20:30:41 +0800184}
185
Lei YUd9e57662021-09-14 18:06:28 +0800186void initSELCache()
187{
Lei YUc10cda12022-01-06 16:01:57 +0800188 registerSelCallbackHandler();
Lei YUeba8e9a2021-09-15 13:16:17 +0800189 ipmi::sel::ObjectPaths paths;
Lei YUd9e57662021-09-14 18:06:28 +0800190 try
191 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800192 ipmi::sel::readLoggingObjectPaths(paths);
Lei YUd9e57662021-09-14 18:06:28 +0800193 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500194 catch (const sdbusplus::exception_t& e)
Lei YUd9e57662021-09-14 18:06:28 +0800195 {
George Liu51856e62024-07-17 19:46:03 +0800196 lg2::error("Failed to get logging object paths");
Lei YUd9e57662021-09-14 18:06:28 +0800197 return;
198 }
Lei YUeba8e9a2021-09-15 13:16:17 +0800199 for (const auto& p : paths)
Lei YUd9e57662021-09-14 18:06:28 +0800200 {
Lei YUa0bb2a32021-09-24 11:03:56 +0800201 auto entry = parseLoggingEntry(p);
202 if (entry)
203 {
204 selCacheMap.insert(std::move(*entry));
205 }
Lei YUd9e57662021-09-14 18:06:28 +0800206 }
Lei YU3df36612021-09-15 11:41:11 +0800207 selCacheMapInitialized = true;
Lei YUd9e57662021-09-14 18:06:28 +0800208}
209
Marri Devender Raocac383b2017-07-03 13:24:27 -0500210/**
211 * @enum Device access mode
212 */
213enum class AccessMode
214{
215 bytes, ///< Device is accessed by bytes
216 words ///< Device is accessed by words
217};
218
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000219/** @brief implements the get SEL Info command
220 * @returns IPMI completion code plus response data
221 * - selVersion - SEL revision
222 * - entries - Number of log entries in SEL.
223 * - freeSpace - Free Space in bytes.
224 * - addTimeStamp - Most recent addition timestamp
225 * - eraseTimeStamp - Most recent erase timestamp
226 * - operationSupport - Reserve & Delete SEL operations supported
227 */
228
229ipmi::RspType<uint8_t, // SEL revision.
230 uint16_t, // number of log entries in SEL.
231 uint16_t, // free Space in bytes.
232 uint32_t, // most recent addition timestamp
233 uint32_t, // most recent erase timestamp.
234
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500235 bool, // SEL allocation info supported
236 bool, // reserve SEL supported
237 bool, // partial Add SEL Entry supported
238 bool, // delete SEL supported
239 uint3_t, // reserved
240 bool // overflow flag
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000241 >
242 ipmiStorageGetSelInfo()
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530243{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000244 uint16_t entries = 0;
245 // Most recent addition timestamp.
246 uint32_t addTimeStamp = ipmi::sel::invalidTimeStamp;
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530247
Lei YUeba8e9a2021-09-15 13:16:17 +0800248 if (!selCacheMapInitialized)
Tom Josephe59abfb2018-08-06 18:46:27 +0530249 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800250 // In case the initSELCache() fails, try it again
251 initSELCache();
Tom Josephe59abfb2018-08-06 18:46:27 +0530252 }
Lei YUeba8e9a2021-09-15 13:16:17 +0800253 if (!selCacheMap.empty())
Tom Josephe59abfb2018-08-06 18:46:27 +0530254 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800255 entries = static_cast<uint16_t>(selCacheMap.size());
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530256
257 try
258 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800259 auto objPath = getLoggingObjPath(selCacheMap.rbegin()->first);
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000260 addTimeStamp = static_cast<uint32_t>(
Lei YUeba8e9a2021-09-15 13:16:17 +0800261 (ipmi::sel::getEntryTimeStamp(objPath).count()));
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530262 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500263 catch (const InternalFailure& e)
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500264 {}
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530265 catch (const std::runtime_error& e)
266 {
George Liu51856e62024-07-17 19:46:03 +0800267 lg2::error("runtime error: {ERROR}", "ERROR", e);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530268 }
269 }
270
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000271 constexpr uint8_t selVersion = ipmi::sel::selVersion;
272 constexpr uint16_t freeSpace = 0xFFFF;
273 constexpr uint32_t eraseTimeStamp = ipmi::sel::invalidTimeStamp;
274 constexpr uint3_t reserved{0};
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530275
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000276 return ipmi::responseSuccess(
277 selVersion, entries, freeSpace, addTimeStamp, eraseTimeStamp,
278 ipmi::sel::operationSupport::getSelAllocationInfo,
279 ipmi::sel::operationSupport::reserveSel,
280 ipmi::sel::operationSupport::partialAddSelEntry,
281 ipmi::sel::operationSupport::deleteSel, reserved,
282 ipmi::sel::operationSupport::overflow);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530283}
284
Willy Tu11d68892022-01-20 10:37:34 -0800285ipmi_ret_t getSELEntry(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request,
286 ipmi_response_t response, ipmi_data_len_t data_len,
287 ipmi_context_t)
Tom Josepha4953392017-06-30 19:09:47 +0530288{
Jason M. Bills851acb12019-02-04 14:06:57 -0800289 if (*data_len != sizeof(ipmi::sel::GetSELEntryRequest))
290 {
291 *data_len = 0;
292 return IPMI_CC_REQ_DATA_LEN_INVALID;
293 }
294
Patrick Venture0b02be92018-08-31 11:55:55 -0700295 auto requestData =
296 reinterpret_cast<const ipmi::sel::GetSELEntryRequest*>(request);
Tom Josepha4953392017-06-30 19:09:47 +0530297
298 if (requestData->reservationID != 0)
299 {
Jason M. Bills13e67c82018-09-10 14:12:16 -0700300 if (!checkSELReservation(requestData->reservationID))
Tom Josepha4953392017-06-30 19:09:47 +0530301 {
302 *data_len = 0;
303 return IPMI_CC_INVALID_RESERVATION_ID;
304 }
305 }
306
Lei YU3df36612021-09-15 11:41:11 +0800307 if (!selCacheMapInitialized)
308 {
309 // In case the initSELCache() fails, try it again
310 initSELCache();
311 }
312
313 if (selCacheMap.empty())
Tom Josepha4953392017-06-30 19:09:47 +0530314 {
315 *data_len = 0;
316 return IPMI_CC_SENSOR_INVALID;
317 }
318
Lei YU3df36612021-09-15 11:41:11 +0800319 SELCacheMap::const_iterator iter;
Tom Josepha4953392017-06-30 19:09:47 +0530320
321 // Check for the requested SEL Entry.
322 if (requestData->selRecordID == ipmi::sel::firstEntry)
323 {
Lei YU3df36612021-09-15 11:41:11 +0800324 iter = selCacheMap.begin();
Tom Josepha4953392017-06-30 19:09:47 +0530325 }
326 else if (requestData->selRecordID == ipmi::sel::lastEntry)
327 {
Lei YU3df36612021-09-15 11:41:11 +0800328 if (selCacheMap.size() > 1)
329 {
330 iter = selCacheMap.end();
331 --iter;
332 }
333 else
334 {
335 // Only one entry exists, return the first
336 iter = selCacheMap.begin();
337 }
Tom Josepha4953392017-06-30 19:09:47 +0530338 }
339 else
340 {
Lei YU3df36612021-09-15 11:41:11 +0800341 iter = selCacheMap.find(requestData->selRecordID);
342 if (iter == selCacheMap.end())
Tom Josepha4953392017-06-30 19:09:47 +0530343 {
344 *data_len = 0;
345 return IPMI_CC_SENSOR_INVALID;
346 }
347 }
348
Lei YU3df36612021-09-15 11:41:11 +0800349 ipmi::sel::GetSELEntryResponse record{0, iter->second};
Tom Josepha4953392017-06-30 19:09:47 +0530350 // Identify the next SEL record ID
Lei YU3df36612021-09-15 11:41:11 +0800351 ++iter;
352 if (iter == selCacheMap.end())
Tom Josepha4953392017-06-30 19:09:47 +0530353 {
Lei YU3df36612021-09-15 11:41:11 +0800354 record.nextRecordID = ipmi::sel::lastEntry;
Tom Josepha4953392017-06-30 19:09:47 +0530355 }
356 else
357 {
Lei YU3df36612021-09-15 11:41:11 +0800358 record.nextRecordID = iter->first;
Tom Josepha4953392017-06-30 19:09:47 +0530359 }
360
361 if (requestData->readLength == ipmi::sel::entireRecord)
362 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700363 std::memcpy(response, &record, sizeof(record));
Tom Josepha4953392017-06-30 19:09:47 +0530364 *data_len = sizeof(record);
365 }
366 else
367 {
368 if (requestData->offset >= ipmi::sel::selRecordSize ||
369 requestData->readLength > ipmi::sel::selRecordSize)
370 {
371 *data_len = 0;
372 return IPMI_CC_INVALID_FIELD_REQUEST;
373 }
374
375 auto diff = ipmi::sel::selRecordSize - requestData->offset;
Patrick Williams1318a5e2024-08-16 15:19:54 -0400376 auto readLength =
377 std::min(diff, static_cast<int>(requestData->readLength));
Tom Josepha4953392017-06-30 19:09:47 +0530378
Lei YU3ad19712025-01-27 05:23:03 +0000379 uint16_t nextRecordID = record.nextRecordID;
380 std::memcpy(response, &nextRecordID, sizeof(nextRecordID));
381
382 const ipmi::sel::SELEventRecordFormat* evt = &record.event;
383 std::memcpy(static_cast<uint8_t*>(response) + sizeof(nextRecordID),
384 reinterpret_cast<const uint8_t*>(evt) + requestData->offset,
Lei YUaf378fa2020-12-02 16:28:57 +0800385 readLength);
Lei YU3ad19712025-01-27 05:23:03 +0000386 *data_len = sizeof(nextRecordID) + readLength;
Tom Josepha4953392017-06-30 19:09:47 +0530387 }
388
389 return IPMI_CC_OK;
390}
391
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000392/** @brief implements the delete SEL entry command
393 * @request
394 * - reservationID; // reservation ID.
395 * - selRecordID; // SEL record ID.
396 *
397 * @returns ipmi completion code plus response data
398 * - Record ID of the deleted record
399 */
400ipmi::RspType<uint16_t // deleted record ID
401 >
402 deleteSELEntry(uint16_t reservationID, uint16_t selRecordID)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530403{
Brad Bishop1a4117b2018-11-21 15:48:18 -0500404 namespace fs = std::filesystem;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530405
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000406 if (!checkSELReservation(reservationID))
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530407 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000408 return ipmi::responseInvalidReservationId();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530409 }
410
Jason M. Bills13e67c82018-09-10 14:12:16 -0700411 // Per the IPMI spec, need to cancel the reservation when a SEL entry is
412 // deleted
413 cancelSELReservation();
414
Lei YUeba8e9a2021-09-15 13:16:17 +0800415 if (!selCacheMapInitialized)
Tom Josephe59abfb2018-08-06 18:46:27 +0530416 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800417 // In case the initSELCache() fails, try it again
418 initSELCache();
Tom Josephe59abfb2018-08-06 18:46:27 +0530419 }
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530420
Lei YUeba8e9a2021-09-15 13:16:17 +0800421 if (selCacheMap.empty())
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530422 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000423 return ipmi::responseSensorInvalid();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530424 }
425
Lei YUeba8e9a2021-09-15 13:16:17 +0800426 SELCacheMap::const_iterator iter;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530427 uint16_t delRecordID = 0;
428
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000429 if (selRecordID == ipmi::sel::firstEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530430 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800431 delRecordID = selCacheMap.begin()->first;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530432 }
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000433 else if (selRecordID == ipmi::sel::lastEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530434 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800435 delRecordID = selCacheMap.rbegin()->first;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530436 }
437 else
438 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000439 delRecordID = selRecordID;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530440 }
441
Jian7d5f4812022-02-23 19:30:27 +0800442 iter = selCacheMap.find(delRecordID);
443 if (iter == selCacheMap.end())
444 {
445 return ipmi::responseSensorInvalid();
446 }
447
Patrick Williams5d82f472022-07-22 19:26:53 -0500448 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530449 std::string service;
450
Lei YUeba8e9a2021-09-15 13:16:17 +0800451 auto objPath = getLoggingObjPath(iter->first);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530452 try
453 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800454 service = ipmi::getService(bus, ipmi::sel::logDeleteIntf, objPath);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530455 }
456 catch (const std::runtime_error& e)
457 {
George Liu51856e62024-07-17 19:46:03 +0800458 lg2::error("runtime error: {ERROR}", "ERROR", e);
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000459 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530460 }
461
Lei YUeba8e9a2021-09-15 13:16:17 +0800462 auto methodCall = bus.new_method_call(service.c_str(), objPath.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700463 ipmi::sel::logDeleteIntf, "Delete");
George Liu3e3cc352023-07-26 15:59:31 +0800464 try
465 {
466 auto reply = bus.call(methodCall);
467 }
468 catch (const std::exception& e)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530469 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000470 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530471 }
472
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000473 return ipmi::responseSuccess(delRecordID);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530474}
475
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000476/** @brief implements the Clear SEL command
477 * @request
478 * - reservationID // Reservation ID.
479 * - clr // char array { 'C'(0x43h), 'L'(0x4Ch), 'R'(0x52h) }
480 * - eraseOperation; // requested operation.
481 *
482 * @returns ipmi completion code plus response data
483 * - erase status
484 */
485
486ipmi::RspType<uint8_t // erase status
487 >
488 clearSEL(uint16_t reservationID, const std::array<char, 3>& clr,
489 uint8_t eraseOperation)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530490{
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000491 static constexpr std::array<char, 3> clrOk = {'C', 'L', 'R'};
492 if (clr != clrOk)
Jason M. Bills851acb12019-02-04 14:06:57 -0800493 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000494 return ipmi::responseInvalidFieldRequest();
Jason M. Bills851acb12019-02-04 14:06:57 -0800495 }
496
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000497 if (!checkSELReservation(reservationID))
Tom Joseph2f05bb52017-06-30 19:14:49 +0530498 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000499 return ipmi::responseInvalidReservationId();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530500 }
501
Tom Joseph2f05bb52017-06-30 19:14:49 +0530502 /*
503 * Erasure status cannot be fetched from DBUS, so always return erasure
504 * status as `erase completed`.
505 */
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000506 if (eraseOperation == ipmi::sel::getEraseStatus)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530507 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000508 return ipmi::responseSuccess(
509 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530510 }
511
Xiaoyu Ma73d07772024-02-04 10:31:37 +0800512 // Check that initiate erase is correct
513 if (eraseOperation != ipmi::sel::initiateErase)
514 {
515 return ipmi::responseInvalidFieldRequest();
516 }
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 Williams1318a5e2024-08-16 15:19:54 -0400523 auto method =
524 bus.new_method_call(service.c_str(), ipmi::sel::logObj,
525 ipmi::sel::logIntf, 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 {
George Liu51856e62024-07-17 19:46:03 +0800532 lg2::error("Error eraseAll: {ERROR}", "ERROR", e);
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);
George Liu42f64ef2024-02-05 15:03:18 +0800555 auto propValue = ipmi::getDbusProperty(
556 bus, service, BMC_TIME_PATH, TIME_INTERFACE, PROPERTY_ELAPSED);
557 bmc_time_usec = std::get<uint64_t>(propValue);
Lei YUe8939392017-06-15 10:45:05 +0800558 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500559 catch (const InternalFailure& e)
Lei YUe8939392017-06-15 10:45:05 +0800560 {
George Liu51856e62024-07-17 19:46:03 +0800561 lg2::error("Internal Failure: {ERROR}", "ERROR", e);
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000562 return ipmi::responseUnspecifiedError();
Lei YUe8939392017-06-15 10:45:05 +0800563 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530564 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800565 {
George Liu51856e62024-07-17 19:46:03 +0800566 lg2::error("exception message: {ERROR}", "ERROR", e);
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000567 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530568 }
569
George Liu51856e62024-07-17 19:46:03 +0800570 lg2::debug("BMC time: {BMC_TIME}", "BMC_TIME",
571 duration_cast<seconds>(microseconds(bmc_time_usec)).count());
Nagaraju Goruganti8960b7c2018-04-29 22:38:40 -0500572
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530573 // Time is really long int but IPMI wants just uint32. This works okay until
574 // the number of seconds since 1970 overflows uint32 size.. Still a whole
575 // lot of time here to even think about that.
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000576 return ipmi::responseSuccess(
George Liu4d623e92020-05-25 16:51:57 +0800577 duration_cast<seconds>(microseconds(bmc_time_usec)).count());
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500578}
579
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000580/** @brief implements the set SEL time command
581 * @param selDeviceTime - epoch time
582 * -local time as the number of seconds from 00:00:00, January 1, 1970
583 * @returns IPMI completion code
584 */
585ipmi::RspType<> ipmiStorageSetSelTime(uint32_t selDeviceTime)
Chris Austenb4f5b922015-10-13 12:44:43 -0500586{
Lei YUe8939392017-06-15 10:45:05 +0800587 using namespace std::chrono;
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000588 microseconds usec{seconds(selDeviceTime)};
Norman James82330442015-11-19 16:53:26 -0600589
Lei YUe8939392017-06-15 10:45:05 +0800590 try
591 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500592 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
shamim ali071d00c2022-03-02 15:42:46 +0530593 bool ntp = std::get<bool>(
594 ipmi::getDbusProperty(bus, SystemdTimeService, SystemdTimePath,
595 SystemdTimeInterface, "NTP"));
596 if (ntp)
597 {
598 return ipmi::responseCommandNotAvailable();
599 }
600
George Liu4d623e92020-05-25 16:51:57 +0800601 auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
Andrew Geissler6467ed22020-05-16 16:03:53 -0500602 std::variant<uint64_t> value{(uint64_t)usec.count()};
Lei YUe8939392017-06-15 10:45:05 +0800603
George Liu4d623e92020-05-25 16:51:57 +0800604 // Set bmc time
605 auto method = bus.new_method_call(service.c_str(), BMC_TIME_PATH,
Patrick Venture0b02be92018-08-31 11:55:55 -0700606 DBUS_PROPERTIES, "Set");
Lei YUe8939392017-06-15 10:45:05 +0800607
608 method.append(TIME_INTERFACE, PROPERTY_ELAPSED, value);
609 auto reply = bus.call(method);
Norman James82330442015-11-19 16:53:26 -0600610 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500611 catch (const InternalFailure& e)
Lei YUe8939392017-06-15 10:45:05 +0800612 {
George Liu51856e62024-07-17 19:46:03 +0800613 lg2::error("Internal Failure: {ERROR}", "ERROR", e);
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000614 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530615 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530616 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800617 {
George Liu51856e62024-07-17 19:46:03 +0800618 lg2::error("exception message: {ERROR}", "ERROR", e);
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000619 return ipmi::responseUnspecifiedError();
Norman James82330442015-11-19 16:53:26 -0600620 }
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530621
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000622 return ipmi::responseSuccess();
Chris Austenb4f5b922015-10-13 12:44:43 -0500623}
624
Xiaochao Maf020c442022-10-31 11:09:38 +0800625/** @brief implements the get SEL timezone command
626 * @returns IPMI completion code plus response data
627 * -current timezone
628 */
629ipmi::RspType<int16_t> ipmiStorageGetSelTimeUtcOffset()
630{
631 time_t timep;
632 struct tm* gmTime;
633 struct tm* localTime;
634
635 time(&timep);
636 localTime = localtime(&timep);
637 auto validLocalTime = mktime(localTime);
638 gmTime = gmtime(&timep);
639 auto validGmTime = mktime(gmTime);
640 auto timeEquation = (validLocalTime - validGmTime) / 60;
641
642 return ipmi::responseSuccess(timeEquation);
643}
644
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000645/** @brief implements the reserve SEL command
646 * @returns IPMI completion code plus response data
647 * - SEL reservation ID.
648 */
649ipmi::RspType<uint16_t> ipmiStorageReserveSel()
Chris Austenb4f5b922015-10-13 12:44:43 -0500650{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000651 return ipmi::responseSuccess(reserveSel());
Chris Austenb4f5b922015-10-13 12:44:43 -0500652}
653
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000654/** @brief implements the Add SEL entry command
655 * @request
656 *
657 * - recordID ID used for SEL Record access
658 * - recordType Record Type
659 * - timeStamp Time when event was logged. LS byte first
660 * - generatorID software ID if event was generated from
661 * system software
662 * - evmRev event message format version
663 * - sensorType sensor type code for service that generated
664 * the event
665 * - sensorNumber number of sensors that generated the event
666 * - eventDir event dir
667 * - eventData event data field contents
668 *
669 * @returns ipmi completion code plus response data
670 * - RecordID of the Added SEL entry
671 */
672ipmi::RspType<uint16_t // recordID of the Added SEL entry
673 >
Willy Tu11d68892022-01-20 10:37:34 -0800674 ipmiStorageAddSEL(uint16_t recordID, uint8_t recordType,
675 [[maybe_unused]] uint32_t timeStamp, uint16_t generatorID,
Patrick Williams32aece12025-01-24 10:06:55 -0500676 [[maybe_unused]] uint8_t evmRev,
677 [[maybe_unused]] uint8_t sensorType, uint8_t sensorNumber,
678 uint8_t eventDir,
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000679 std::array<uint8_t, eventDataSize> eventData)
Chris Austenb4f5b922015-10-13 12:44:43 -0500680{
Lotus Xu57d35572021-01-24 11:13:13 +0800681 std::string objpath;
682 static constexpr auto systemRecordType = 0x02;
Patrick Williams32aece12025-01-24 10:06:55 -0500683#ifdef OPEN_POWER_SUPPORT
Tom Josephb647d5b2017-10-31 17:25:33 +0530684 // Hostboot sends SEL with OEM record type 0xDE to indicate that there is
685 // a maintenance procedure associated with eSEL record.
686 static constexpr auto procedureType = 0xDE;
Patrick Williams32aece12025-01-24 10:06:55 -0500687#endif
Lotus Xu57d35572021-01-24 11:13:13 +0800688 cancelSELReservation();
689 if (recordType == systemRecordType)
690 {
Lotus Xu57d35572021-01-24 11:13:13 +0800691 for (const auto& it : invSensors)
692 {
693 if (it.second.sensorID == sensorNumber)
694 {
695 objpath = it.first;
696 break;
697 }
698 }
699 auto selDataStr = ipmi::sel::toHexStr(eventData);
700
701 bool assert = (eventDir & 0x80) ? false : true;
702
Patrick Williams1318a5e2024-08-16 15:19:54 -0400703 recordID = report<SELCreated>(
704 Created::RECORD_TYPE(recordType),
705 Created::GENERATOR_ID(generatorID),
706 Created::SENSOR_DATA(selDataStr.c_str()),
707 Created::EVENT_DIR(assert), Created::SENSOR_PATH(objpath.c_str()));
Lotus Xu57d35572021-01-24 11:13:13 +0800708 }
Patrick Williams768730d2023-09-06 16:27:20 -0500709#ifdef OPEN_POWER_SUPPORT
Lotus Xu57d35572021-01-24 11:13:13 +0800710 else if (recordType == procedureType)
Tom Josephb647d5b2017-10-31 17:25:33 +0530711 {
712 // In the OEM record type 0xDE, byte 11 in the SEL record indicate the
713 // procedure number.
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000714 createProcedureLogEntry(sensorType);
Tom Josephb647d5b2017-10-31 17:25:33 +0530715 }
Patrick Williams768730d2023-09-06 16:27:20 -0500716#endif
Chris Austenb4f5b922015-10-13 12:44:43 -0500717
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000718 return ipmi::responseSuccess(recordID);
Chris Austenb4f5b922015-10-13 12:44:43 -0500719}
720
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300721bool isFruPresent(ipmi::Context::ptr& ctx, const std::string& fruPath)
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300722{
723 using namespace ipmi::fru;
724
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300725 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -0400726 boost::system::error_code ec =
727 getService(ctx, invItemInterface, invObjPath + fruPath, service);
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300728 if (!ec)
729 {
730 bool result;
731 ec = ipmi::getDbusProperty(ctx, service, invObjPath + fruPath,
732 invItemInterface, itemPresentProp, result);
733 if (!ec)
734 {
735 return result;
736 }
737 }
Konstantin Aladyshevc7e4b042021-12-24 15:30:47 +0300738
739 ipmi::ObjectValueTree managedObjects;
Nan Zhou947da1b2022-09-20 20:40:59 +0000740 ec = getManagedObjects(ctx, "xyz.openbmc_project.EntityManager",
741 "/xyz/openbmc_project/inventory", managedObjects);
Konstantin Aladyshevc7e4b042021-12-24 15:30:47 +0300742 if (!ec)
743 {
744 auto connection = managedObjects.find(fruPath);
745 if (connection != managedObjects.end())
746 {
747 return true;
748 }
749 }
750
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300751 return false;
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300752}
753
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000754/** @brief implements the get FRU Inventory Area Info command
755 *
756 * @returns IPMI completion code plus response data
757 * - FRU Inventory area size in bytes,
758 * - access bit
759 **/
760ipmi::RspType<uint16_t, // FRU Inventory area size in bytes,
761 uint8_t // access size (bytes / words)
762 >
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300763 ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruID)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500764{
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000765 auto iter = frus.find(fruID);
Tom Joseph187f5642018-03-29 13:49:06 +0530766 if (iter == frus.end())
767 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000768 return ipmi::responseSensorInvalid();
Tom Joseph187f5642018-03-29 13:49:06 +0530769 }
770
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300771 auto path = iter->second[0].path;
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300772 if (!isFruPresent(ctx, path))
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300773 {
774 return ipmi::responseSensorInvalid();
775 }
776
Marri Devender Raocac383b2017-07-03 13:24:27 -0500777 try
778 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000779 return ipmi::responseSuccess(
780 static_cast<uint16_t>(getFruAreaData(fruID).size()),
781 static_cast<uint8_t>(AccessMode::bytes));
Marri Devender Raocac383b2017-07-03 13:24:27 -0500782 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700783 catch (const InternalFailure& e)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500784 {
George Liu51856e62024-07-17 19:46:03 +0800785 lg2::error("Internal Failure: {ERROR}", "ERROR", e);
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000786 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500787 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500788}
789
anil kumar appana5b7c3262019-05-27 18:10:23 +0000790/**@brief implements the Read FRU Data command
791 * @param fruDeviceId - FRU device ID. FFh = reserved
792 * @param offset - FRU inventory offset to read
793 * @param readCount - count to read
794 *
795 * @return IPMI completion code plus response data
796 * - returnCount - response data count.
797 * - data - response data
798 */
799ipmi::RspType<uint8_t, // count returned
800 std::vector<uint8_t>> // FRU data
801 ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t offset,
802 uint8_t readCount)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500803{
anil kumar appana5b7c3262019-05-27 18:10:23 +0000804 if (fruDeviceId == 0xFF)
Tom Joseph187f5642018-03-29 13:49:06 +0530805 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000806 return ipmi::responseInvalidFieldRequest();
Tom Joseph187f5642018-03-29 13:49:06 +0530807 }
808
anil kumar appana5b7c3262019-05-27 18:10:23 +0000809 auto iter = frus.find(fruDeviceId);
810 if (iter == frus.end())
811 {
812 return ipmi::responseSensorInvalid();
813 }
814
Marri Devender Raocac383b2017-07-03 13:24:27 -0500815 try
816 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000817 const auto& fruArea = getFruAreaData(fruDeviceId);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500818 auto size = fruArea.size();
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500819
Tom Josephefcd68b2018-04-26 18:46:27 +0530820 if (offset >= size)
821 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000822 return ipmi::responseParmOutOfRange();
Tom Josephefcd68b2018-04-26 18:46:27 +0530823 }
824
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500825 // Write the count of response data.
anil kumar appana5b7c3262019-05-27 18:10:23 +0000826 uint8_t returnCount;
827 if ((offset + readCount) <= size)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500828 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000829 returnCount = readCount;
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500830 }
831 else
832 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000833 returnCount = size - offset;
Marri Devender Raocac383b2017-07-03 13:24:27 -0500834 }
Ratan Gupta2848d602018-01-31 20:39:20 +0530835
anil kumar appana5b7c3262019-05-27 18:10:23 +0000836 std::vector<uint8_t> fruData((fruArea.begin() + offset),
837 (fruArea.begin() + offset + returnCount));
Ratan Gupta2848d602018-01-31 20:39:20 +0530838
anil kumar appana5b7c3262019-05-27 18:10:23 +0000839 return ipmi::responseSuccess(returnCount, fruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500840 }
841 catch (const InternalFailure& e)
842 {
George Liu51856e62024-07-17 19:46:03 +0800843 lg2::error("Internal Failure: {ERROR}", "ERROR", e);
anil kumar appana5b7c3262019-05-27 18:10:23 +0000844 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500845 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500846}
847
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000848ipmi::RspType<uint8_t, // SDR version
849 uint16_t, // record count LS first
850 uint16_t, // free space in bytes, LS first
851 uint32_t, // addition timestamp LS first
852 uint32_t, // deletion timestamp LS first
853 uint8_t> // operation Support
854 ipmiGetRepositoryInfo()
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600855{
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000856 constexpr uint8_t sdrVersion = 0x51;
857 constexpr uint16_t freeSpace = 0xFFFF;
858 constexpr uint32_t additionTimestamp = 0x0;
859 constexpr uint32_t deletionTimestamp = 0x0;
860 constexpr uint8_t operationSupport = 0;
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600861
Willy Tu0ca3bfe2022-01-28 09:40:18 -0800862 // Get SDR count. This returns the total number of SDRs in the device.
863 const auto& entityRecords =
864 ipmi::sensor::EntityInfoMapContainer::getContainer()
865 ->getIpmiEntityRecords();
Patrick Williams1318a5e2024-08-16 15:19:54 -0400866 uint16_t records =
867 ipmi::sensor::sensors.size() + frus.size() + entityRecords.size();
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600868
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000869 return ipmi::responseSuccess(sdrVersion, records, freeSpace,
870 additionTimestamp, deletionTimestamp,
871 operationSupport);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600872}
Chris Austenb4f5b922015-10-13 12:44:43 -0500873
George Liu5087b072025-03-11 19:28:17 +0800874void registerNetFnStorageFunctions()
Chris Austenb4f5b922015-10-13 12:44:43 -0500875{
Lei YU3df36612021-09-15 11:41:11 +0800876 selCacheMapInitialized = false;
Lei YUd9e57662021-09-14 18:06:28 +0800877 initSELCache();
Willy Tud351a722021-08-12 14:33:40 -0700878 // Handlers with dbus-sdr handler implementation.
879 // Do not register the hander if it dynamic sensors stack is used.
880
881#ifndef FEATURE_DYNAMIC_SENSORS
Thang Tran292c9172023-06-26 10:03:04 +0700882
883#ifndef FEATURE_DYNAMIC_STORAGES_ONLY
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530884 // <Get SEL Info>
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000885 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
886 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
887 ipmiStorageGetSelInfo);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530888
Xiaochao Maf020c442022-10-31 11:09:38 +0800889 // <Get SEL Timezone>
890 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
891 ipmi::storage::cmdGetSelTimeUtcOffset,
892 ipmi::Privilege::User,
893 ipmiStorageGetSelTimeUtcOffset);
894
Tom Josepha4953392017-06-30 19:09:47 +0530895 // <Get SEL Entry>
Jayanth Othayotha6fb32d2024-12-15 10:55:22 -0600896 ipmi_register_callback(NETFUN_STORAGE, ipmi::storage::cmdGetSelEntry,
897 nullptr, getSELEntry, PRIVILEGE_USER);
Tom Josepha4953392017-06-30 19:09:47 +0530898
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530899 // <Delete SEL Entry>
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000900 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
901 ipmi::storage::cmdDeleteSelEntry,
902 ipmi::Privilege::Operator, deleteSELEntry);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530903
Tom05732372016-09-06 17:21:23 +0530904 // <Add SEL Entry>
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000905 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
906 ipmi::storage::cmdAddSelEntry,
907 ipmi::Privilege::Operator, ipmiStorageAddSEL);
908
Tom Joseph2f05bb52017-06-30 19:14:49 +0530909 // <Clear SEL>
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000910 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
911 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
912 clearSEL);
913
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500914 // <Get FRU Inventory Area Info>
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000915 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
916 ipmi::storage::cmdGetFruInventoryAreaInfo,
917 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500918
Jason M. Billsb5248c92019-06-24 15:53:08 -0700919 // <READ FRU Data>
anil kumar appana5b7c3262019-05-27 18:10:23 +0000920 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
921 ipmi::storage::cmdReadFruData,
922 ipmi::Privilege::Operator, ipmiStorageReadFruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500923
Thang Tran292c9172023-06-26 10:03:04 +0700924#endif // FEATURE_DYNAMIC_STORAGES_ONLY
925
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600926 // <Get Repository Info>
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000927 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
928 ipmi::storage::cmdGetSdrRepositoryInfo,
929 ipmi::Privilege::User, ipmiGetRepositoryInfo);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600930
Tom Joseph5ca50952018-02-22 00:33:38 +0530931 // <Reserve SDR Repository>
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000932 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
933 ipmi::storage::cmdReserveSdrRepository,
934 ipmi::Privilege::User, ipmiSensorReserveSdr);
Tom Joseph5ca50952018-02-22 00:33:38 +0530935
936 // <Get SDR>
George Liue6cd4df2024-12-05 17:07:59 +0800937 ipmi_register_callback(NETFUN_STORAGE, ipmi::storage::cmdGetSdr, nullptr,
Patrick Venture0b02be92018-08-31 11:55:55 -0700938 ipmi_sen_get_sdr, PRIVILEGE_USER);
Tom Joseph5ca50952018-02-22 00:33:38 +0530939
Willy Tud351a722021-08-12 14:33:40 -0700940#endif
941
942 // Common Handers used by both implementation.
943
944 // <Reserve SEL>
945 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
946 ipmi::storage::cmdReserveSel, ipmi::Privilege::User,
947 ipmiStorageReserveSel);
948
Thang Trandbf66e12023-09-21 13:07:24 +0700949 // <Get SEL Time>
950 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
951 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
952 ipmiStorageGetSelTime);
953
954 // <Set SEL Time>
955 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
956 ipmi::storage::cmdSetSelTime,
957 ipmi::Privilege::Operator, ipmiStorageSetSelTime);
958
Marri Devender Rao908f7502017-07-10 01:49:54 -0500959 ipmi::fru::registerCallbackHandler();
Chris Austenb4f5b922015-10-13 12:44:43 -0500960 return;
961}