blob: 4a97b478e3baaf07520fa9bec771affd301b4420 [file] [log] [blame]
Patrick Williams768730d2023-09-06 16:27:20 -05001#include "config.h"
2
Patrick Venture3a5071a2018-09-12 13:27:42 -07003#include "storagehandler.hpp"
4
5#include "fruread.hpp"
6#include "read_fru_data.hpp"
7#include "selutility.hpp"
8#include "sensorhandler.hpp"
9#include "storageaddsel.hpp"
Patrick Venture3a5071a2018-09-12 13:27:42 -070010
Lei YU52d91242017-10-17 22:52:28 +080011#include <arpa/inet.h>
Patrick Venture3a5071a2018-09-12 13:27:42 -070012#include <systemd/sd-bus.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070013
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050014#include <ipmid/api.hpp>
Vernon Mauery9cf08382023-04-28 14:00:11 -070015#include <ipmid/entity_map_json.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050016#include <ipmid/utils.hpp>
17#include <phosphor-logging/elog-errors.hpp>
18#include <phosphor-logging/elog.hpp>
George Liu51856e62024-07-17 19:46:03 +080019#include <phosphor-logging/lg2.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050020#include <sdbusplus/server.hpp>
21#include <xyz/openbmc_project/Common/error.hpp>
22#include <xyz/openbmc_project/Logging/SEL/error.hpp>
23
Patrick Venture0b02be92018-08-31 11:55:55 -070024#include <algorithm>
Lei YU52d91242017-10-17 22:52:28 +080025#include <chrono>
26#include <cstdio>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070027#include <cstring>
Vernon Mauerybdda8002019-02-26 10:18:51 -080028#include <filesystem>
Lei YUa0bb2a32021-09-24 11:03:56 +080029#include <optional>
Patrick Venture3a5071a2018-09-12 13:27:42 -070030#include <string>
Vernon Mauery16b86932019-05-01 08:36:11 -070031#include <variant>
Patrick Venture3a5071a2018-09-12 13:27:42 -070032
Chris Austenb4f5b922015-10-13 12:44:43 -050033void register_netfn_storage_functions() __attribute__((constructor));
34
Patrick Venture0b02be92018-08-31 11:55:55 -070035unsigned int g_sel_time = 0xFFFFFFFF;
Patrick Venturedb0cbe62019-09-09 14:47:22 -070036namespace ipmi
37{
38namespace sensor
39{
40extern const IdInfoMap sensors;
41} // namespace sensor
42} // namespace ipmi
Lotus Xu57d35572021-01-24 11:13:13 +080043extern const ipmi::sensor::InvObjectIDMap invSensors;
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -060044extern const FruMap frus;
anil kumar appana2c7db1d2019-05-28 11:20:19 +000045constexpr uint8_t eventDataSize = 3;
Patrick Venture0b02be92018-08-31 11:55:55 -070046namespace
47{
shamim ali071d00c2022-03-02 15:42:46 +053048constexpr auto SystemdTimeService = "org.freedesktop.timedate1";
49constexpr auto SystemdTimePath = "/org/freedesktop/timedate1";
50constexpr auto SystemdTimeInterface = "org.freedesktop.timedate1";
51
Lei YUe8939392017-06-15 10:45:05 +080052constexpr auto TIME_INTERFACE = "xyz.openbmc_project.Time.EpochTime";
George Liu4d623e92020-05-25 16:51:57 +080053constexpr auto BMC_TIME_PATH = "/xyz/openbmc_project/time/bmc";
Lei YUe8939392017-06-15 10:45:05 +080054constexpr auto DBUS_PROPERTIES = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070055constexpr auto PROPERTY_ELAPSED = "Elapsed";
Patrick Venture0b02be92018-08-31 11:55:55 -070056} // namespace
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +053057
Tom Joseph6f7deaa2017-06-30 19:03:54 +053058using InternalFailure =
Willy Tu523e2d12023-09-05 11:36:48 -070059 sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
Tom Joseph6f7deaa2017-06-30 19:03:54 +053060using namespace phosphor::logging;
Marri Devender Raocac383b2017-07-03 13:24:27 -050061using namespace ipmi::fru;
Willy Tu523e2d12023-09-05 11:36:48 -070062using namespace xyz::openbmc_project::logging::sel;
Lotus Xu57d35572021-01-24 11:13:13 +080063using SELCreated =
Willy Tu523e2d12023-09-05 11:36:48 -070064 sdbusplus::error::xyz::openbmc_project::logging::sel::Created;
Marri Devender Raocac383b2017-07-03 13:24:27 -050065
Lei YUd9e57662021-09-14 18:06:28 +080066using SELRecordID = uint16_t;
67using SELEntry = ipmi::sel::SELEventRecordFormat;
68using SELCacheMap = std::map<SELRecordID, SELEntry>;
69
70SELCacheMap selCacheMap __attribute__((init_priority(101)));
Lei YU3df36612021-09-15 11:41:11 +080071bool selCacheMapInitialized;
Patrick Williams5d82f472022-07-22 19:26:53 -050072std::unique_ptr<sdbusplus::bus::match_t> selAddedMatch
Lei YUd9555252021-09-14 20:30:41 +080073 __attribute__((init_priority(101)));
Patrick Williams5d82f472022-07-22 19:26:53 -050074std::unique_ptr<sdbusplus::bus::match_t> selRemovedMatch
Lei YUb6b72b02021-09-14 21:06:05 +080075 __attribute__((init_priority(101)));
Patrick Williams5d82f472022-07-22 19:26:53 -050076std::unique_ptr<sdbusplus::bus::match_t> selUpdatedMatch
Lei YU4106e422021-09-15 10:55:51 +080077 __attribute__((init_priority(101)));
Lei YUd9e57662021-09-14 18:06:28 +080078
Lei YUb6b72b02021-09-14 21:06:05 +080079static inline uint16_t getLoggingId(const std::string& p)
Lei YUd9e57662021-09-14 18:06:28 +080080{
81 namespace fs = std::filesystem;
82 fs::path entryPath(p);
Lei YUb6b72b02021-09-14 21:06:05 +080083 return std::stoul(entryPath.filename().string());
84}
85
Lei YUeba8e9a2021-09-15 13:16:17 +080086static inline std::string getLoggingObjPath(uint16_t id)
87{
88 return std::string(ipmi::sel::logBasePath) + "/" + std::to_string(id);
89}
90
Lei YUa0bb2a32021-09-24 11:03:56 +080091std::optional<std::pair<uint16_t, SELEntry>>
92 parseLoggingEntry(const std::string& p)
Lei YUb6b72b02021-09-14 21:06:05 +080093{
Lei YU3df36612021-09-15 11:41:11 +080094 try
95 {
Lei YUa0bb2a32021-09-24 11:03:56 +080096 auto id = getLoggingId(p);
97 ipmi::sel::GetSELEntryResponse record{};
Lei YU3df36612021-09-15 11:41:11 +080098 record = ipmi::sel::convertLogEntrytoSEL(p);
Lei YUa0bb2a32021-09-24 11:03:56 +080099 return std::pair<uint16_t, SELEntry>({id, std::move(record.event)});
Lei YU3df36612021-09-15 11:41:11 +0800100 }
101 catch (const std::exception& e)
102 {
103 fprintf(stderr, "Failed to convert %s to SEL: %s\n", p.c_str(),
104 e.what());
105 }
Lei YUa0bb2a32021-09-24 11:03:56 +0800106 return std::nullopt;
Lei YUd9e57662021-09-14 18:06:28 +0800107}
108
Patrick Williams5d82f472022-07-22 19:26:53 -0500109static void selAddedCallback(sdbusplus::message_t& m)
Lei YUd9555252021-09-14 20:30:41 +0800110{
111 sdbusplus::message::object_path objPath;
112 try
113 {
114 m.read(objPath);
115 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500116 catch (const sdbusplus::exception_t& e)
Lei YUd9555252021-09-14 20:30:41 +0800117 {
George Liu51856e62024-07-17 19:46:03 +0800118 lg2::error("Failed to read object path");
Lei YUd9555252021-09-14 20:30:41 +0800119 return;
120 }
121 std::string p = objPath;
Lei YUa0bb2a32021-09-24 11:03:56 +0800122 auto entry = parseLoggingEntry(p);
123 if (entry)
124 {
125 selCacheMap.insert(std::move(*entry));
126 }
Lei YUd9555252021-09-14 20:30:41 +0800127}
128
Patrick Williams5d82f472022-07-22 19:26:53 -0500129static void selRemovedCallback(sdbusplus::message_t& m)
Lei YUb6b72b02021-09-14 21:06:05 +0800130{
131 sdbusplus::message::object_path objPath;
132 try
133 {
134 m.read(objPath);
135 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500136 catch (const sdbusplus::exception_t& e)
Lei YUb6b72b02021-09-14 21:06:05 +0800137 {
George Liu51856e62024-07-17 19:46:03 +0800138 lg2::error("Failed to read object path");
Lei YUb6b72b02021-09-14 21:06:05 +0800139 }
Lei YUa0bb2a32021-09-24 11:03:56 +0800140 try
141 {
142 std::string p = objPath;
143 selCacheMap.erase(getLoggingId(p));
144 }
145 catch (const std::invalid_argument& e)
146 {
George Liu51856e62024-07-17 19:46:03 +0800147 lg2::error("Invalid logging entry ID");
Lei YUa0bb2a32021-09-24 11:03:56 +0800148 }
Lei YUb6b72b02021-09-14 21:06:05 +0800149}
150
Patrick Williams5d82f472022-07-22 19:26:53 -0500151static void selUpdatedCallback(sdbusplus::message_t& m)
Lei YU4106e422021-09-15 10:55:51 +0800152{
153 std::string p = m.get_path();
154 auto entry = parseLoggingEntry(p);
Lei YUa0bb2a32021-09-24 11:03:56 +0800155 if (entry)
156 {
157 selCacheMap.insert_or_assign(entry->first, std::move(entry->second));
158 }
Lei YU4106e422021-09-15 10:55:51 +0800159}
160
Lei YUd9555252021-09-14 20:30:41 +0800161void registerSelCallbackHandler()
162{
163 using namespace sdbusplus::bus::match::rules;
Patrick Williams5d82f472022-07-22 19:26:53 -0500164 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Lei YUd9555252021-09-14 20:30:41 +0800165 if (!selAddedMatch)
166 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500167 selAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
George Liu656ae3c2024-08-23 16:44:22 +0800168 bus, interfacesAdded(ipmi::sel::logWatchPath),
Lei YUd9555252021-09-14 20:30:41 +0800169 std::bind(selAddedCallback, std::placeholders::_1));
170 }
Lei YUb6b72b02021-09-14 21:06:05 +0800171 if (!selRemovedMatch)
172 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500173 selRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
George Liu656ae3c2024-08-23 16:44:22 +0800174 bus, interfacesRemoved(ipmi::sel::logWatchPath),
Lei YUb6b72b02021-09-14 21:06:05 +0800175 std::bind(selRemovedCallback, std::placeholders::_1));
176 }
Lei YU4106e422021-09-15 10:55:51 +0800177 if (!selUpdatedMatch)
178 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500179 selUpdatedMatch = std::make_unique<sdbusplus::bus::match_t>(
Lei YU4106e422021-09-15 10:55:51 +0800180 bus,
181 type::signal() + member("PropertiesChanged"s) +
182 interface("org.freedesktop.DBus.Properties"s) +
George Liu656ae3c2024-08-23 16:44:22 +0800183 argN(0, ipmi::sel::logEntryIntf),
Lei YU4106e422021-09-15 10:55:51 +0800184 std::bind(selUpdatedCallback, std::placeholders::_1));
185 }
Lei YUd9555252021-09-14 20:30:41 +0800186}
187
Lei YUd9e57662021-09-14 18:06:28 +0800188void initSELCache()
189{
Lei YUc10cda12022-01-06 16:01:57 +0800190 registerSelCallbackHandler();
Lei YUeba8e9a2021-09-15 13:16:17 +0800191 ipmi::sel::ObjectPaths paths;
Lei YUd9e57662021-09-14 18:06:28 +0800192 try
193 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800194 ipmi::sel::readLoggingObjectPaths(paths);
Lei YUd9e57662021-09-14 18:06:28 +0800195 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500196 catch (const sdbusplus::exception_t& e)
Lei YUd9e57662021-09-14 18:06:28 +0800197 {
George Liu51856e62024-07-17 19:46:03 +0800198 lg2::error("Failed to get logging object paths");
Lei YUd9e57662021-09-14 18:06:28 +0800199 return;
200 }
Lei YUeba8e9a2021-09-15 13:16:17 +0800201 for (const auto& p : paths)
Lei YUd9e57662021-09-14 18:06:28 +0800202 {
Lei YUa0bb2a32021-09-24 11:03:56 +0800203 auto entry = parseLoggingEntry(p);
204 if (entry)
205 {
206 selCacheMap.insert(std::move(*entry));
207 }
Lei YUd9e57662021-09-14 18:06:28 +0800208 }
Lei YU3df36612021-09-15 11:41:11 +0800209 selCacheMapInitialized = true;
Lei YUd9e57662021-09-14 18:06:28 +0800210}
211
Marri Devender Raocac383b2017-07-03 13:24:27 -0500212/**
213 * @enum Device access mode
214 */
215enum class AccessMode
216{
217 bytes, ///< Device is accessed by bytes
218 words ///< Device is accessed by words
219};
220
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000221/** @brief implements the get SEL Info command
222 * @returns IPMI completion code plus response data
223 * - selVersion - SEL revision
224 * - entries - Number of log entries in SEL.
225 * - freeSpace - Free Space in bytes.
226 * - addTimeStamp - Most recent addition timestamp
227 * - eraseTimeStamp - Most recent erase timestamp
228 * - operationSupport - Reserve & Delete SEL operations supported
229 */
230
231ipmi::RspType<uint8_t, // SEL revision.
232 uint16_t, // number of log entries in SEL.
233 uint16_t, // free Space in bytes.
234 uint32_t, // most recent addition timestamp
235 uint32_t, // most recent erase timestamp.
236
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500237 bool, // SEL allocation info supported
238 bool, // reserve SEL supported
239 bool, // partial Add SEL Entry supported
240 bool, // delete SEL supported
241 uint3_t, // reserved
242 bool // overflow flag
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000243 >
244 ipmiStorageGetSelInfo()
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530245{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000246 uint16_t entries = 0;
247 // Most recent addition timestamp.
248 uint32_t addTimeStamp = ipmi::sel::invalidTimeStamp;
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530249
Lei YUeba8e9a2021-09-15 13:16:17 +0800250 if (!selCacheMapInitialized)
Tom Josephe59abfb2018-08-06 18:46:27 +0530251 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800252 // In case the initSELCache() fails, try it again
253 initSELCache();
Tom Josephe59abfb2018-08-06 18:46:27 +0530254 }
Lei YUeba8e9a2021-09-15 13:16:17 +0800255 if (!selCacheMap.empty())
Tom Josephe59abfb2018-08-06 18:46:27 +0530256 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800257 entries = static_cast<uint16_t>(selCacheMap.size());
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530258
259 try
260 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800261 auto objPath = getLoggingObjPath(selCacheMap.rbegin()->first);
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000262 addTimeStamp = static_cast<uint32_t>(
Lei YUeba8e9a2021-09-15 13:16:17 +0800263 (ipmi::sel::getEntryTimeStamp(objPath).count()));
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530264 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500265 catch (const InternalFailure& e)
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500266 {}
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530267 catch (const std::runtime_error& e)
268 {
George Liu51856e62024-07-17 19:46:03 +0800269 lg2::error("runtime error: {ERROR}", "ERROR", e);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530270 }
271 }
272
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000273 constexpr uint8_t selVersion = ipmi::sel::selVersion;
274 constexpr uint16_t freeSpace = 0xFFFF;
275 constexpr uint32_t eraseTimeStamp = ipmi::sel::invalidTimeStamp;
276 constexpr uint3_t reserved{0};
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530277
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000278 return ipmi::responseSuccess(
279 selVersion, entries, freeSpace, addTimeStamp, eraseTimeStamp,
280 ipmi::sel::operationSupport::getSelAllocationInfo,
281 ipmi::sel::operationSupport::reserveSel,
282 ipmi::sel::operationSupport::partialAddSelEntry,
283 ipmi::sel::operationSupport::deleteSel, reserved,
284 ipmi::sel::operationSupport::overflow);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530285}
286
Willy Tu11d68892022-01-20 10:37:34 -0800287ipmi_ret_t getSELEntry(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request,
288 ipmi_response_t response, ipmi_data_len_t data_len,
289 ipmi_context_t)
Tom Josepha4953392017-06-30 19:09:47 +0530290{
Jason M. Bills851acb12019-02-04 14:06:57 -0800291 if (*data_len != sizeof(ipmi::sel::GetSELEntryRequest))
292 {
293 *data_len = 0;
294 return IPMI_CC_REQ_DATA_LEN_INVALID;
295 }
296
Patrick Venture0b02be92018-08-31 11:55:55 -0700297 auto requestData =
298 reinterpret_cast<const ipmi::sel::GetSELEntryRequest*>(request);
Tom Josepha4953392017-06-30 19:09:47 +0530299
300 if (requestData->reservationID != 0)
301 {
Jason M. Bills13e67c82018-09-10 14:12:16 -0700302 if (!checkSELReservation(requestData->reservationID))
Tom Josepha4953392017-06-30 19:09:47 +0530303 {
304 *data_len = 0;
305 return IPMI_CC_INVALID_RESERVATION_ID;
306 }
307 }
308
Lei YU3df36612021-09-15 11:41:11 +0800309 if (!selCacheMapInitialized)
310 {
311 // In case the initSELCache() fails, try it again
312 initSELCache();
313 }
314
315 if (selCacheMap.empty())
Tom Josepha4953392017-06-30 19:09:47 +0530316 {
317 *data_len = 0;
318 return IPMI_CC_SENSOR_INVALID;
319 }
320
Lei YU3df36612021-09-15 11:41:11 +0800321 SELCacheMap::const_iterator iter;
Tom Josepha4953392017-06-30 19:09:47 +0530322
323 // Check for the requested SEL Entry.
324 if (requestData->selRecordID == ipmi::sel::firstEntry)
325 {
Lei YU3df36612021-09-15 11:41:11 +0800326 iter = selCacheMap.begin();
Tom Josepha4953392017-06-30 19:09:47 +0530327 }
328 else if (requestData->selRecordID == ipmi::sel::lastEntry)
329 {
Lei YU3df36612021-09-15 11:41:11 +0800330 if (selCacheMap.size() > 1)
331 {
332 iter = selCacheMap.end();
333 --iter;
334 }
335 else
336 {
337 // Only one entry exists, return the first
338 iter = selCacheMap.begin();
339 }
Tom Josepha4953392017-06-30 19:09:47 +0530340 }
341 else
342 {
Lei YU3df36612021-09-15 11:41:11 +0800343 iter = selCacheMap.find(requestData->selRecordID);
344 if (iter == selCacheMap.end())
Tom Josepha4953392017-06-30 19:09:47 +0530345 {
346 *data_len = 0;
347 return IPMI_CC_SENSOR_INVALID;
348 }
349 }
350
Lei YU3df36612021-09-15 11:41:11 +0800351 ipmi::sel::GetSELEntryResponse record{0, iter->second};
Tom Josepha4953392017-06-30 19:09:47 +0530352 // Identify the next SEL record ID
Lei YU3df36612021-09-15 11:41:11 +0800353 ++iter;
354 if (iter == selCacheMap.end())
Tom Josepha4953392017-06-30 19:09:47 +0530355 {
Lei YU3df36612021-09-15 11:41:11 +0800356 record.nextRecordID = ipmi::sel::lastEntry;
Tom Josepha4953392017-06-30 19:09:47 +0530357 }
358 else
359 {
Lei YU3df36612021-09-15 11:41:11 +0800360 record.nextRecordID = iter->first;
Tom Josepha4953392017-06-30 19:09:47 +0530361 }
362
363 if (requestData->readLength == ipmi::sel::entireRecord)
364 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700365 std::memcpy(response, &record, sizeof(record));
Tom Josepha4953392017-06-30 19:09:47 +0530366 *data_len = sizeof(record);
367 }
368 else
369 {
370 if (requestData->offset >= ipmi::sel::selRecordSize ||
371 requestData->readLength > ipmi::sel::selRecordSize)
372 {
373 *data_len = 0;
374 return IPMI_CC_INVALID_FIELD_REQUEST;
375 }
376
377 auto diff = ipmi::sel::selRecordSize - requestData->offset;
Patrick Williams1318a5e2024-08-16 15:19:54 -0400378 auto readLength =
379 std::min(diff, static_cast<int>(requestData->readLength));
Tom Josepha4953392017-06-30 19:09:47 +0530380
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700381 std::memcpy(response, &record.nextRecordID,
382 sizeof(record.nextRecordID));
383 std::memcpy(static_cast<uint8_t*>(response) +
384 sizeof(record.nextRecordID),
Lei YUaf378fa2020-12-02 16:28:57 +0800385 &record.event.eventRecord.recordID + requestData->offset,
386 readLength);
Tom Josepha4953392017-06-30 19:09:47 +0530387 *data_len = sizeof(record.nextRecordID) + readLength;
388 }
389
390 return IPMI_CC_OK;
391}
392
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000393/** @brief implements the delete SEL entry command
394 * @request
395 * - reservationID; // reservation ID.
396 * - selRecordID; // SEL record ID.
397 *
398 * @returns ipmi completion code plus response data
399 * - Record ID of the deleted record
400 */
401ipmi::RspType<uint16_t // deleted record ID
402 >
403 deleteSELEntry(uint16_t reservationID, uint16_t selRecordID)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530404{
Brad Bishop1a4117b2018-11-21 15:48:18 -0500405 namespace fs = std::filesystem;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530406
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000407 if (!checkSELReservation(reservationID))
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530408 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000409 return ipmi::responseInvalidReservationId();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530410 }
411
Jason M. Bills13e67c82018-09-10 14:12:16 -0700412 // Per the IPMI spec, need to cancel the reservation when a SEL entry is
413 // deleted
414 cancelSELReservation();
415
Lei YUeba8e9a2021-09-15 13:16:17 +0800416 if (!selCacheMapInitialized)
Tom Josephe59abfb2018-08-06 18:46:27 +0530417 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800418 // In case the initSELCache() fails, try it again
419 initSELCache();
Tom Josephe59abfb2018-08-06 18:46:27 +0530420 }
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530421
Lei YUeba8e9a2021-09-15 13:16:17 +0800422 if (selCacheMap.empty())
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530423 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000424 return ipmi::responseSensorInvalid();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530425 }
426
Lei YUeba8e9a2021-09-15 13:16:17 +0800427 SELCacheMap::const_iterator iter;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530428 uint16_t delRecordID = 0;
429
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000430 if (selRecordID == ipmi::sel::firstEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530431 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800432 delRecordID = selCacheMap.begin()->first;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530433 }
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000434 else if (selRecordID == ipmi::sel::lastEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530435 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800436 delRecordID = selCacheMap.rbegin()->first;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530437 }
438 else
439 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000440 delRecordID = selRecordID;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530441 }
442
Jian7d5f4812022-02-23 19:30:27 +0800443 iter = selCacheMap.find(delRecordID);
444 if (iter == selCacheMap.end())
445 {
446 return ipmi::responseSensorInvalid();
447 }
448
Patrick Williams5d82f472022-07-22 19:26:53 -0500449 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530450 std::string service;
451
Lei YUeba8e9a2021-09-15 13:16:17 +0800452 auto objPath = getLoggingObjPath(iter->first);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530453 try
454 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800455 service = ipmi::getService(bus, ipmi::sel::logDeleteIntf, objPath);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530456 }
457 catch (const std::runtime_error& e)
458 {
George Liu51856e62024-07-17 19:46:03 +0800459 lg2::error("runtime error: {ERROR}", "ERROR", e);
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000460 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530461 }
462
Lei YUeba8e9a2021-09-15 13:16:17 +0800463 auto methodCall = bus.new_method_call(service.c_str(), objPath.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700464 ipmi::sel::logDeleteIntf, "Delete");
George Liu3e3cc352023-07-26 15:59:31 +0800465 try
466 {
467 auto reply = bus.call(methodCall);
468 }
469 catch (const std::exception& e)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530470 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000471 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530472 }
473
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000474 return ipmi::responseSuccess(delRecordID);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530475}
476
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000477/** @brief implements the Clear SEL command
478 * @request
479 * - reservationID // Reservation ID.
480 * - clr // char array { 'C'(0x43h), 'L'(0x4Ch), 'R'(0x52h) }
481 * - eraseOperation; // requested operation.
482 *
483 * @returns ipmi completion code plus response data
484 * - erase status
485 */
486
487ipmi::RspType<uint8_t // erase status
488 >
489 clearSEL(uint16_t reservationID, const std::array<char, 3>& clr,
490 uint8_t eraseOperation)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530491{
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000492 static constexpr std::array<char, 3> clrOk = {'C', 'L', 'R'};
493 if (clr != clrOk)
Jason M. Bills851acb12019-02-04 14:06:57 -0800494 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000495 return ipmi::responseInvalidFieldRequest();
Jason M. Bills851acb12019-02-04 14:06:57 -0800496 }
497
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000498 if (!checkSELReservation(reservationID))
Tom Joseph2f05bb52017-06-30 19:14:49 +0530499 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000500 return ipmi::responseInvalidReservationId();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530501 }
502
Tom Joseph2f05bb52017-06-30 19:14:49 +0530503 /*
504 * Erasure status cannot be fetched from DBUS, so always return erasure
505 * status as `erase completed`.
506 */
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000507 if (eraseOperation == ipmi::sel::getEraseStatus)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530508 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000509 return ipmi::responseSuccess(
510 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530511 }
512
Xiaoyu Ma73d07772024-02-04 10:31:37 +0800513 // Check that initiate erase is correct
514 if (eraseOperation != ipmi::sel::initiateErase)
515 {
516 return ipmi::responseInvalidFieldRequest();
517 }
518
Jason M. Bills13e67c82018-09-10 14:12:16 -0700519 // Per the IPMI spec, need to cancel any reservation when the SEL is cleared
520 cancelSELReservation();
521
Patrick Williams5d82f472022-07-22 19:26:53 -0500522 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Lotus Xu78fe1b12021-05-23 17:26:56 +0800523 auto service = ipmi::getService(bus, ipmi::sel::logIntf, ipmi::sel::logObj);
Patrick Williams1318a5e2024-08-16 15:19:54 -0400524 auto method =
525 bus.new_method_call(service.c_str(), ipmi::sel::logObj,
526 ipmi::sel::logIntf, ipmi::sel::logDeleteAllMethod);
Tom Josephe59abfb2018-08-06 18:46:27 +0530527 try
Tom Joseph2f05bb52017-06-30 19:14:49 +0530528 {
Lotus Xu78fe1b12021-05-23 17:26:56 +0800529 bus.call_noreply(method);
Tom Josephe59abfb2018-08-06 18:46:27 +0530530 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500531 catch (const sdbusplus::exception_t& e)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530532 {
George Liu51856e62024-07-17 19:46:03 +0800533 lg2::error("Error eraseAll: {ERROR}", "ERROR", e);
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000534 return ipmi::responseUnspecifiedError();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530535 }
536
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000537 return ipmi::responseSuccess(
538 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530539}
540
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000541/** @brief implements the get SEL time command
542 * @returns IPMI completion code plus response data
543 * -current time
544 */
545ipmi::RspType<uint32_t> // current time
546 ipmiStorageGetSelTime()
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500547{
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530548 using namespace std::chrono;
George Liu4d623e92020-05-25 16:51:57 +0800549 uint64_t bmc_time_usec = 0;
550 std::stringstream bmcTime;
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500551
Lei YUe8939392017-06-15 10:45:05 +0800552 try
553 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500554 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
George Liu4d623e92020-05-25 16:51:57 +0800555 auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
George Liu42f64ef2024-02-05 15:03:18 +0800556 auto propValue = ipmi::getDbusProperty(
557 bus, service, BMC_TIME_PATH, TIME_INTERFACE, PROPERTY_ELAPSED);
558 bmc_time_usec = std::get<uint64_t>(propValue);
Lei YUe8939392017-06-15 10:45:05 +0800559 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500560 catch (const InternalFailure& e)
Lei YUe8939392017-06-15 10:45:05 +0800561 {
George Liu51856e62024-07-17 19:46:03 +0800562 lg2::error("Internal Failure: {ERROR}", "ERROR", e);
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000563 return ipmi::responseUnspecifiedError();
Lei YUe8939392017-06-15 10:45:05 +0800564 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530565 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800566 {
George Liu51856e62024-07-17 19:46:03 +0800567 lg2::error("exception message: {ERROR}", "ERROR", e);
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000568 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530569 }
570
George Liu51856e62024-07-17 19:46:03 +0800571 lg2::debug("BMC time: {BMC_TIME}", "BMC_TIME",
572 duration_cast<seconds>(microseconds(bmc_time_usec)).count());
Nagaraju Goruganti8960b7c2018-04-29 22:38:40 -0500573
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530574 // Time is really long int but IPMI wants just uint32. This works okay until
575 // the number of seconds since 1970 overflows uint32 size.. Still a whole
576 // lot of time here to even think about that.
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000577 return ipmi::responseSuccess(
George Liu4d623e92020-05-25 16:51:57 +0800578 duration_cast<seconds>(microseconds(bmc_time_usec)).count());
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500579}
580
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000581/** @brief implements the set SEL time command
582 * @param selDeviceTime - epoch time
583 * -local time as the number of seconds from 00:00:00, January 1, 1970
584 * @returns IPMI completion code
585 */
586ipmi::RspType<> ipmiStorageSetSelTime(uint32_t selDeviceTime)
Chris Austenb4f5b922015-10-13 12:44:43 -0500587{
Lei YUe8939392017-06-15 10:45:05 +0800588 using namespace std::chrono;
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000589 microseconds usec{seconds(selDeviceTime)};
Norman James82330442015-11-19 16:53:26 -0600590
Lei YUe8939392017-06-15 10:45:05 +0800591 try
592 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500593 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
shamim ali071d00c2022-03-02 15:42:46 +0530594 bool ntp = std::get<bool>(
595 ipmi::getDbusProperty(bus, SystemdTimeService, SystemdTimePath,
596 SystemdTimeInterface, "NTP"));
597 if (ntp)
598 {
599 return ipmi::responseCommandNotAvailable();
600 }
601
George Liu4d623e92020-05-25 16:51:57 +0800602 auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
Andrew Geissler6467ed22020-05-16 16:03:53 -0500603 std::variant<uint64_t> value{(uint64_t)usec.count()};
Lei YUe8939392017-06-15 10:45:05 +0800604
George Liu4d623e92020-05-25 16:51:57 +0800605 // Set bmc time
606 auto method = bus.new_method_call(service.c_str(), BMC_TIME_PATH,
Patrick Venture0b02be92018-08-31 11:55:55 -0700607 DBUS_PROPERTIES, "Set");
Lei YUe8939392017-06-15 10:45:05 +0800608
609 method.append(TIME_INTERFACE, PROPERTY_ELAPSED, value);
610 auto reply = bus.call(method);
Norman James82330442015-11-19 16:53:26 -0600611 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500612 catch (const InternalFailure& e)
Lei YUe8939392017-06-15 10:45:05 +0800613 {
George Liu51856e62024-07-17 19:46:03 +0800614 lg2::error("Internal Failure: {ERROR}", "ERROR", e);
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000615 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530616 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530617 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800618 {
George Liu51856e62024-07-17 19:46:03 +0800619 lg2::error("exception message: {ERROR}", "ERROR", e);
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000620 return ipmi::responseUnspecifiedError();
Norman James82330442015-11-19 16:53:26 -0600621 }
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530622
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000623 return ipmi::responseSuccess();
Chris Austenb4f5b922015-10-13 12:44:43 -0500624}
625
Xiaochao Maf020c442022-10-31 11:09:38 +0800626/** @brief implements the get SEL timezone command
627 * @returns IPMI completion code plus response data
628 * -current timezone
629 */
630ipmi::RspType<int16_t> ipmiStorageGetSelTimeUtcOffset()
631{
632 time_t timep;
633 struct tm* gmTime;
634 struct tm* localTime;
635
636 time(&timep);
637 localTime = localtime(&timep);
638 auto validLocalTime = mktime(localTime);
639 gmTime = gmtime(&timep);
640 auto validGmTime = mktime(gmTime);
641 auto timeEquation = (validLocalTime - validGmTime) / 60;
642
643 return ipmi::responseSuccess(timeEquation);
644}
645
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000646/** @brief implements the reserve SEL command
647 * @returns IPMI completion code plus response data
648 * - SEL reservation ID.
649 */
650ipmi::RspType<uint16_t> ipmiStorageReserveSel()
Chris Austenb4f5b922015-10-13 12:44:43 -0500651{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000652 return ipmi::responseSuccess(reserveSel());
Chris Austenb4f5b922015-10-13 12:44:43 -0500653}
654
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000655/** @brief implements the Add SEL entry command
656 * @request
657 *
658 * - recordID ID used for SEL Record access
659 * - recordType Record Type
660 * - timeStamp Time when event was logged. LS byte first
661 * - generatorID software ID if event was generated from
662 * system software
663 * - evmRev event message format version
664 * - sensorType sensor type code for service that generated
665 * the event
666 * - sensorNumber number of sensors that generated the event
667 * - eventDir event dir
668 * - eventData event data field contents
669 *
670 * @returns ipmi completion code plus response data
671 * - RecordID of the Added SEL entry
672 */
673ipmi::RspType<uint16_t // recordID of the Added SEL entry
674 >
Willy Tu11d68892022-01-20 10:37:34 -0800675 ipmiStorageAddSEL(uint16_t recordID, uint8_t recordType,
676 [[maybe_unused]] uint32_t timeStamp, uint16_t generatorID,
677 [[maybe_unused]] uint8_t evmRev, uint8_t sensorType,
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000678 uint8_t sensorNumber, uint8_t eventDir,
679 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;
Tom Josephb647d5b2017-10-31 17:25:33 +0530683 // Hostboot sends SEL with OEM record type 0xDE to indicate that there is
684 // a maintenance procedure associated with eSEL record.
685 static constexpr auto procedureType = 0xDE;
Lotus Xu57d35572021-01-24 11:13:13 +0800686 cancelSELReservation();
687 if (recordType == systemRecordType)
688 {
Lotus Xu57d35572021-01-24 11:13:13 +0800689 for (const auto& it : invSensors)
690 {
691 if (it.second.sensorID == sensorNumber)
692 {
693 objpath = it.first;
694 break;
695 }
696 }
697 auto selDataStr = ipmi::sel::toHexStr(eventData);
698
699 bool assert = (eventDir & 0x80) ? false : true;
700
Patrick Williams1318a5e2024-08-16 15:19:54 -0400701 recordID = report<SELCreated>(
702 Created::RECORD_TYPE(recordType),
703 Created::GENERATOR_ID(generatorID),
704 Created::SENSOR_DATA(selDataStr.c_str()),
705 Created::EVENT_DIR(assert), Created::SENSOR_PATH(objpath.c_str()));
Lotus Xu57d35572021-01-24 11:13:13 +0800706 }
Patrick Williams768730d2023-09-06 16:27:20 -0500707#ifdef OPEN_POWER_SUPPORT
Lotus Xu57d35572021-01-24 11:13:13 +0800708 else if (recordType == procedureType)
Tom Josephb647d5b2017-10-31 17:25:33 +0530709 {
710 // In the OEM record type 0xDE, byte 11 in the SEL record indicate the
711 // procedure number.
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000712 createProcedureLogEntry(sensorType);
Tom Josephb647d5b2017-10-31 17:25:33 +0530713 }
Patrick Williams768730d2023-09-06 16:27:20 -0500714#endif
Chris Austenb4f5b922015-10-13 12:44:43 -0500715
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000716 return ipmi::responseSuccess(recordID);
Chris Austenb4f5b922015-10-13 12:44:43 -0500717}
718
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300719bool isFruPresent(ipmi::Context::ptr& ctx, const std::string& fruPath)
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300720{
721 using namespace ipmi::fru;
722
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300723 std::string service;
Patrick Williams1318a5e2024-08-16 15:19:54 -0400724 boost::system::error_code ec =
725 getService(ctx, invItemInterface, invObjPath + fruPath, service);
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300726 if (!ec)
727 {
728 bool result;
729 ec = ipmi::getDbusProperty(ctx, service, invObjPath + fruPath,
730 invItemInterface, itemPresentProp, result);
731 if (!ec)
732 {
733 return result;
734 }
735 }
Konstantin Aladyshevc7e4b042021-12-24 15:30:47 +0300736
737 ipmi::ObjectValueTree managedObjects;
Nan Zhou947da1b2022-09-20 20:40:59 +0000738 ec = getManagedObjects(ctx, "xyz.openbmc_project.EntityManager",
739 "/xyz/openbmc_project/inventory", managedObjects);
Konstantin Aladyshevc7e4b042021-12-24 15:30:47 +0300740 if (!ec)
741 {
742 auto connection = managedObjects.find(fruPath);
743 if (connection != managedObjects.end())
744 {
745 return true;
746 }
747 }
748
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300749 return false;
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300750}
751
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000752/** @brief implements the get FRU Inventory Area Info command
753 *
754 * @returns IPMI completion code plus response data
755 * - FRU Inventory area size in bytes,
756 * - access bit
757 **/
758ipmi::RspType<uint16_t, // FRU Inventory area size in bytes,
759 uint8_t // access size (bytes / words)
760 >
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300761 ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruID)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500762{
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000763 auto iter = frus.find(fruID);
Tom Joseph187f5642018-03-29 13:49:06 +0530764 if (iter == frus.end())
765 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000766 return ipmi::responseSensorInvalid();
Tom Joseph187f5642018-03-29 13:49:06 +0530767 }
768
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300769 auto path = iter->second[0].path;
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300770 if (!isFruPresent(ctx, path))
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300771 {
772 return ipmi::responseSensorInvalid();
773 }
774
Marri Devender Raocac383b2017-07-03 13:24:27 -0500775 try
776 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000777 return ipmi::responseSuccess(
778 static_cast<uint16_t>(getFruAreaData(fruID).size()),
779 static_cast<uint8_t>(AccessMode::bytes));
Marri Devender Raocac383b2017-07-03 13:24:27 -0500780 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700781 catch (const InternalFailure& e)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500782 {
George Liu51856e62024-07-17 19:46:03 +0800783 lg2::error("Internal Failure: {ERROR}", "ERROR", e);
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000784 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500785 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500786}
787
anil kumar appana5b7c3262019-05-27 18:10:23 +0000788/**@brief implements the Read FRU Data command
789 * @param fruDeviceId - FRU device ID. FFh = reserved
790 * @param offset - FRU inventory offset to read
791 * @param readCount - count to read
792 *
793 * @return IPMI completion code plus response data
794 * - returnCount - response data count.
795 * - data - response data
796 */
797ipmi::RspType<uint8_t, // count returned
798 std::vector<uint8_t>> // FRU data
799 ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t offset,
800 uint8_t readCount)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500801{
anil kumar appana5b7c3262019-05-27 18:10:23 +0000802 if (fruDeviceId == 0xFF)
Tom Joseph187f5642018-03-29 13:49:06 +0530803 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000804 return ipmi::responseInvalidFieldRequest();
Tom Joseph187f5642018-03-29 13:49:06 +0530805 }
806
anil kumar appana5b7c3262019-05-27 18:10:23 +0000807 auto iter = frus.find(fruDeviceId);
808 if (iter == frus.end())
809 {
810 return ipmi::responseSensorInvalid();
811 }
812
Marri Devender Raocac383b2017-07-03 13:24:27 -0500813 try
814 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000815 const auto& fruArea = getFruAreaData(fruDeviceId);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500816 auto size = fruArea.size();
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500817
Tom Josephefcd68b2018-04-26 18:46:27 +0530818 if (offset >= size)
819 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000820 return ipmi::responseParmOutOfRange();
Tom Josephefcd68b2018-04-26 18:46:27 +0530821 }
822
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500823 // Write the count of response data.
anil kumar appana5b7c3262019-05-27 18:10:23 +0000824 uint8_t returnCount;
825 if ((offset + readCount) <= size)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500826 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000827 returnCount = readCount;
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500828 }
829 else
830 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000831 returnCount = size - offset;
Marri Devender Raocac383b2017-07-03 13:24:27 -0500832 }
Ratan Gupta2848d602018-01-31 20:39:20 +0530833
anil kumar appana5b7c3262019-05-27 18:10:23 +0000834 std::vector<uint8_t> fruData((fruArea.begin() + offset),
835 (fruArea.begin() + offset + returnCount));
Ratan Gupta2848d602018-01-31 20:39:20 +0530836
anil kumar appana5b7c3262019-05-27 18:10:23 +0000837 return ipmi::responseSuccess(returnCount, fruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500838 }
839 catch (const InternalFailure& e)
840 {
George Liu51856e62024-07-17 19:46:03 +0800841 lg2::error("Internal Failure: {ERROR}", "ERROR", e);
anil kumar appana5b7c3262019-05-27 18:10:23 +0000842 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500843 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500844}
845
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000846ipmi::RspType<uint8_t, // SDR version
847 uint16_t, // record count LS first
848 uint16_t, // free space in bytes, LS first
849 uint32_t, // addition timestamp LS first
850 uint32_t, // deletion timestamp LS first
851 uint8_t> // operation Support
852 ipmiGetRepositoryInfo()
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600853{
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000854 constexpr uint8_t sdrVersion = 0x51;
855 constexpr uint16_t freeSpace = 0xFFFF;
856 constexpr uint32_t additionTimestamp = 0x0;
857 constexpr uint32_t deletionTimestamp = 0x0;
858 constexpr uint8_t operationSupport = 0;
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600859
Willy Tu0ca3bfe2022-01-28 09:40:18 -0800860 // Get SDR count. This returns the total number of SDRs in the device.
861 const auto& entityRecords =
862 ipmi::sensor::EntityInfoMapContainer::getContainer()
863 ->getIpmiEntityRecords();
Patrick Williams1318a5e2024-08-16 15:19:54 -0400864 uint16_t records =
865 ipmi::sensor::sensors.size() + frus.size() + entityRecords.size();
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600866
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000867 return ipmi::responseSuccess(sdrVersion, records, freeSpace,
868 additionTimestamp, deletionTimestamp,
869 operationSupport);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600870}
Chris Austenb4f5b922015-10-13 12:44:43 -0500871
Chris Austenb4f5b922015-10-13 12:44:43 -0500872void register_netfn_storage_functions()
873{
Lei YU3df36612021-09-15 11:41:11 +0800874 selCacheMapInitialized = false;
Lei YUd9e57662021-09-14 18:06:28 +0800875 initSELCache();
Willy Tud351a722021-08-12 14:33:40 -0700876 // Handlers with dbus-sdr handler implementation.
877 // Do not register the hander if it dynamic sensors stack is used.
878
879#ifndef FEATURE_DYNAMIC_SENSORS
Thang Tran292c9172023-06-26 10:03:04 +0700880
881#ifndef FEATURE_DYNAMIC_STORAGES_ONLY
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530882 // <Get SEL Info>
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000883 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
884 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
885 ipmiStorageGetSelInfo);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530886
Xiaochao Maf020c442022-10-31 11:09:38 +0800887 // <Get SEL Timezone>
888 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
889 ipmi::storage::cmdGetSelTimeUtcOffset,
890 ipmi::Privilege::User,
891 ipmiStorageGetSelTimeUtcOffset);
892
Tom Josepha4953392017-06-30 19:09:47 +0530893 // <Get SEL Entry>
Patrick Venture0b02be92018-08-31 11:55:55 -0700894 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_ENTRY, NULL,
895 getSELEntry, PRIVILEGE_USER);
Tom Josepha4953392017-06-30 19:09:47 +0530896
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530897 // <Delete SEL Entry>
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000898 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
899 ipmi::storage::cmdDeleteSelEntry,
900 ipmi::Privilege::Operator, deleteSELEntry);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530901
Tom05732372016-09-06 17:21:23 +0530902 // <Add SEL Entry>
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000903 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
904 ipmi::storage::cmdAddSelEntry,
905 ipmi::Privilege::Operator, ipmiStorageAddSEL);
906
Tom Joseph2f05bb52017-06-30 19:14:49 +0530907 // <Clear SEL>
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000908 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
909 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
910 clearSEL);
911
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500912 // <Get FRU Inventory Area Info>
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000913 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
914 ipmi::storage::cmdGetFruInventoryAreaInfo,
915 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500916
Jason M. Billsb5248c92019-06-24 15:53:08 -0700917 // <READ FRU Data>
anil kumar appana5b7c3262019-05-27 18:10:23 +0000918 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
919 ipmi::storage::cmdReadFruData,
920 ipmi::Privilege::Operator, ipmiStorageReadFruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500921
Thang Tran292c9172023-06-26 10:03:04 +0700922#endif // FEATURE_DYNAMIC_STORAGES_ONLY
923
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600924 // <Get Repository Info>
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000925 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
926 ipmi::storage::cmdGetSdrRepositoryInfo,
927 ipmi::Privilege::User, ipmiGetRepositoryInfo);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600928
Tom Joseph5ca50952018-02-22 00:33:38 +0530929 // <Reserve SDR Repository>
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000930 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
931 ipmi::storage::cmdReserveSdrRepository,
932 ipmi::Privilege::User, ipmiSensorReserveSdr);
Tom Joseph5ca50952018-02-22 00:33:38 +0530933
934 // <Get SDR>
Patrick Venture0b02be92018-08-31 11:55:55 -0700935 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SDR, nullptr,
936 ipmi_sen_get_sdr, PRIVILEGE_USER);
Tom Joseph5ca50952018-02-22 00:33:38 +0530937
Willy Tud351a722021-08-12 14:33:40 -0700938#endif
939
940 // Common Handers used by both implementation.
941
942 // <Reserve SEL>
943 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
944 ipmi::storage::cmdReserveSel, ipmi::Privilege::User,
945 ipmiStorageReserveSel);
946
Thang Trandbf66e12023-09-21 13:07:24 +0700947 // <Get SEL Time>
948 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
949 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
950 ipmiStorageGetSelTime);
951
952 // <Set SEL Time>
953 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
954 ipmi::storage::cmdSetSelTime,
955 ipmi::Privilege::Operator, ipmiStorageSetSelTime);
956
Marri Devender Rao908f7502017-07-10 01:49:54 -0500957 ipmi::fru::registerCallbackHandler();
Chris Austenb4f5b922015-10-13 12:44:43 -0500958 return;
959}