blob: 4c02a5055fd4102ff98ab09a6d0e99e714279a93 [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 <mapper.h>
13#include <systemd/sd-bus.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070014
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050015#include <ipmid/api.hpp>
Vernon Mauery9cf08382023-04-28 14:00:11 -070016#include <ipmid/entity_map_json.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050017#include <ipmid/utils.hpp>
18#include <phosphor-logging/elog-errors.hpp>
19#include <phosphor-logging/elog.hpp>
20#include <phosphor-logging/log.hpp>
21#include <sdbusplus/server.hpp>
22#include <xyz/openbmc_project/Common/error.hpp>
23#include <xyz/openbmc_project/Logging/SEL/error.hpp>
24
Patrick Venture0b02be92018-08-31 11:55:55 -070025#include <algorithm>
Lei YU52d91242017-10-17 22:52:28 +080026#include <chrono>
27#include <cstdio>
Patrick Ventureb51bf9c2018-09-10 15:53:14 -070028#include <cstring>
Vernon Mauerybdda8002019-02-26 10:18:51 -080029#include <filesystem>
Lei YUa0bb2a32021-09-24 11:03:56 +080030#include <optional>
Patrick Venture3a5071a2018-09-12 13:27:42 -070031#include <string>
Vernon Mauery16b86932019-05-01 08:36:11 -070032#include <variant>
Patrick Venture3a5071a2018-09-12 13:27:42 -070033
Chris Austenb4f5b922015-10-13 12:44:43 -050034void register_netfn_storage_functions() __attribute__((constructor));
35
Patrick Venture0b02be92018-08-31 11:55:55 -070036unsigned int g_sel_time = 0xFFFFFFFF;
Patrick Venturedb0cbe62019-09-09 14:47:22 -070037namespace ipmi
38{
39namespace sensor
40{
41extern const IdInfoMap sensors;
42} // namespace sensor
43} // namespace ipmi
Lotus Xu57d35572021-01-24 11:13:13 +080044extern const ipmi::sensor::InvObjectIDMap invSensors;
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -060045extern const FruMap frus;
anil kumar appana2c7db1d2019-05-28 11:20:19 +000046constexpr uint8_t eventDataSize = 3;
Patrick Venture0b02be92018-08-31 11:55:55 -070047namespace
48{
shamim ali071d00c2022-03-02 15:42:46 +053049constexpr auto SystemdTimeService = "org.freedesktop.timedate1";
50constexpr auto SystemdTimePath = "/org/freedesktop/timedate1";
51constexpr auto SystemdTimeInterface = "org.freedesktop.timedate1";
52
Lei YUe8939392017-06-15 10:45:05 +080053constexpr auto TIME_INTERFACE = "xyz.openbmc_project.Time.EpochTime";
George Liu4d623e92020-05-25 16:51:57 +080054constexpr auto BMC_TIME_PATH = "/xyz/openbmc_project/time/bmc";
Lei YUe8939392017-06-15 10:45:05 +080055constexpr auto DBUS_PROPERTIES = "org.freedesktop.DBus.Properties";
Patrick Venture0b02be92018-08-31 11:55:55 -070056constexpr auto PROPERTY_ELAPSED = "Elapsed";
Lei YUe8939392017-06-15 10:45:05 +080057
Lei YUd9555252021-09-14 20:30:41 +080058constexpr auto logWatchPath = "/xyz/openbmc_project/logging";
Lei YUd9e57662021-09-14 18:06:28 +080059constexpr auto logBasePath = "/xyz/openbmc_project/logging/entry";
60constexpr auto logEntryIntf = "xyz.openbmc_project.Logging.Entry";
61constexpr auto logDeleteIntf = "xyz.openbmc_project.Object.Delete";
Patrick Venture0b02be92018-08-31 11:55:55 -070062} // namespace
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +053063
Tom Joseph6f7deaa2017-06-30 19:03:54 +053064using InternalFailure =
Willy Tu523e2d12023-09-05 11:36:48 -070065 sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
Tom Joseph6f7deaa2017-06-30 19:03:54 +053066using namespace phosphor::logging;
Marri Devender Raocac383b2017-07-03 13:24:27 -050067using namespace ipmi::fru;
Willy Tu523e2d12023-09-05 11:36:48 -070068using namespace xyz::openbmc_project::logging::sel;
Lotus Xu57d35572021-01-24 11:13:13 +080069using SELCreated =
Willy Tu523e2d12023-09-05 11:36:48 -070070 sdbusplus::error::xyz::openbmc_project::logging::sel::Created;
Marri Devender Raocac383b2017-07-03 13:24:27 -050071
Lei YUd9e57662021-09-14 18:06:28 +080072using SELRecordID = uint16_t;
73using SELEntry = ipmi::sel::SELEventRecordFormat;
74using SELCacheMap = std::map<SELRecordID, SELEntry>;
75
76SELCacheMap selCacheMap __attribute__((init_priority(101)));
Lei YU3df36612021-09-15 11:41:11 +080077bool selCacheMapInitialized;
Patrick Williams5d82f472022-07-22 19:26:53 -050078std::unique_ptr<sdbusplus::bus::match_t> selAddedMatch
Lei YUd9555252021-09-14 20:30:41 +080079 __attribute__((init_priority(101)));
Patrick Williams5d82f472022-07-22 19:26:53 -050080std::unique_ptr<sdbusplus::bus::match_t> selRemovedMatch
Lei YUb6b72b02021-09-14 21:06:05 +080081 __attribute__((init_priority(101)));
Patrick Williams5d82f472022-07-22 19:26:53 -050082std::unique_ptr<sdbusplus::bus::match_t> selUpdatedMatch
Lei YU4106e422021-09-15 10:55:51 +080083 __attribute__((init_priority(101)));
Lei YUd9e57662021-09-14 18:06:28 +080084
Lei YUb6b72b02021-09-14 21:06:05 +080085static inline uint16_t getLoggingId(const std::string& p)
Lei YUd9e57662021-09-14 18:06:28 +080086{
87 namespace fs = std::filesystem;
88 fs::path entryPath(p);
Lei YUb6b72b02021-09-14 21:06:05 +080089 return std::stoul(entryPath.filename().string());
90}
91
Lei YUeba8e9a2021-09-15 13:16:17 +080092static inline std::string getLoggingObjPath(uint16_t id)
93{
94 return std::string(ipmi::sel::logBasePath) + "/" + std::to_string(id);
95}
96
Lei YUa0bb2a32021-09-24 11:03:56 +080097std::optional<std::pair<uint16_t, SELEntry>>
98 parseLoggingEntry(const std::string& p)
Lei YUb6b72b02021-09-14 21:06:05 +080099{
Lei YU3df36612021-09-15 11:41:11 +0800100 try
101 {
Lei YUa0bb2a32021-09-24 11:03:56 +0800102 auto id = getLoggingId(p);
103 ipmi::sel::GetSELEntryResponse record{};
Lei YU3df36612021-09-15 11:41:11 +0800104 record = ipmi::sel::convertLogEntrytoSEL(p);
Lei YUa0bb2a32021-09-24 11:03:56 +0800105 return std::pair<uint16_t, SELEntry>({id, std::move(record.event)});
Lei YU3df36612021-09-15 11:41:11 +0800106 }
107 catch (const std::exception& e)
108 {
109 fprintf(stderr, "Failed to convert %s to SEL: %s\n", p.c_str(),
110 e.what());
111 }
Lei YUa0bb2a32021-09-24 11:03:56 +0800112 return std::nullopt;
Lei YUd9e57662021-09-14 18:06:28 +0800113}
114
Patrick Williams5d82f472022-07-22 19:26:53 -0500115static void selAddedCallback(sdbusplus::message_t& m)
Lei YUd9555252021-09-14 20:30:41 +0800116{
117 sdbusplus::message::object_path objPath;
118 try
119 {
120 m.read(objPath);
121 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500122 catch (const sdbusplus::exception_t& e)
Lei YUd9555252021-09-14 20:30:41 +0800123 {
124 log<level::ERR>("Failed to read object path");
125 return;
126 }
127 std::string p = objPath;
Lei YUa0bb2a32021-09-24 11:03:56 +0800128 auto entry = parseLoggingEntry(p);
129 if (entry)
130 {
131 selCacheMap.insert(std::move(*entry));
132 }
Lei YUd9555252021-09-14 20:30:41 +0800133}
134
Patrick Williams5d82f472022-07-22 19:26:53 -0500135static void selRemovedCallback(sdbusplus::message_t& m)
Lei YUb6b72b02021-09-14 21:06:05 +0800136{
137 sdbusplus::message::object_path objPath;
138 try
139 {
140 m.read(objPath);
141 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500142 catch (const sdbusplus::exception_t& e)
Lei YUb6b72b02021-09-14 21:06:05 +0800143 {
144 log<level::ERR>("Failed to read object path");
Lei YUb6b72b02021-09-14 21:06:05 +0800145 }
Lei YUa0bb2a32021-09-24 11:03:56 +0800146 try
147 {
148 std::string p = objPath;
149 selCacheMap.erase(getLoggingId(p));
150 }
151 catch (const std::invalid_argument& e)
152 {
153 log<level::ERR>("Invalid logging entry ID");
154 }
Lei YUb6b72b02021-09-14 21:06:05 +0800155}
156
Patrick Williams5d82f472022-07-22 19:26:53 -0500157static void selUpdatedCallback(sdbusplus::message_t& m)
Lei YU4106e422021-09-15 10:55:51 +0800158{
159 std::string p = m.get_path();
160 auto entry = parseLoggingEntry(p);
Lei YUa0bb2a32021-09-24 11:03:56 +0800161 if (entry)
162 {
163 selCacheMap.insert_or_assign(entry->first, std::move(entry->second));
164 }
Lei YU4106e422021-09-15 10:55:51 +0800165}
166
Lei YUd9555252021-09-14 20:30:41 +0800167void registerSelCallbackHandler()
168{
169 using namespace sdbusplus::bus::match::rules;
Patrick Williams5d82f472022-07-22 19:26:53 -0500170 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Lei YUd9555252021-09-14 20:30:41 +0800171 if (!selAddedMatch)
172 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500173 selAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
Lei YUd9555252021-09-14 20:30:41 +0800174 bus, interfacesAdded(logWatchPath),
175 std::bind(selAddedCallback, std::placeholders::_1));
176 }
Lei YUb6b72b02021-09-14 21:06:05 +0800177 if (!selRemovedMatch)
178 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500179 selRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
Lei YUb6b72b02021-09-14 21:06:05 +0800180 bus, interfacesRemoved(logWatchPath),
181 std::bind(selRemovedCallback, std::placeholders::_1));
182 }
Lei YU4106e422021-09-15 10:55:51 +0800183 if (!selUpdatedMatch)
184 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500185 selUpdatedMatch = std::make_unique<sdbusplus::bus::match_t>(
Lei YU4106e422021-09-15 10:55:51 +0800186 bus,
187 type::signal() + member("PropertiesChanged"s) +
188 interface("org.freedesktop.DBus.Properties"s) +
189 argN(0, logEntryIntf),
190 std::bind(selUpdatedCallback, std::placeholders::_1));
191 }
Lei YUd9555252021-09-14 20:30:41 +0800192}
193
Lei YUd9e57662021-09-14 18:06:28 +0800194void initSELCache()
195{
Lei YUc10cda12022-01-06 16:01:57 +0800196 registerSelCallbackHandler();
Lei YUeba8e9a2021-09-15 13:16:17 +0800197 ipmi::sel::ObjectPaths paths;
Lei YUd9e57662021-09-14 18:06:28 +0800198 try
199 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800200 ipmi::sel::readLoggingObjectPaths(paths);
Lei YUd9e57662021-09-14 18:06:28 +0800201 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500202 catch (const sdbusplus::exception_t& e)
Lei YUd9e57662021-09-14 18:06:28 +0800203 {
204 log<level::ERR>("Failed to get logging object paths");
205 return;
206 }
Lei YUeba8e9a2021-09-15 13:16:17 +0800207 for (const auto& p : paths)
Lei YUd9e57662021-09-14 18:06:28 +0800208 {
Lei YUa0bb2a32021-09-24 11:03:56 +0800209 auto entry = parseLoggingEntry(p);
210 if (entry)
211 {
212 selCacheMap.insert(std::move(*entry));
213 }
Lei YUd9e57662021-09-14 18:06:28 +0800214 }
Lei YU3df36612021-09-15 11:41:11 +0800215 selCacheMapInitialized = true;
Lei YUd9e57662021-09-14 18:06:28 +0800216}
217
Marri Devender Raocac383b2017-07-03 13:24:27 -0500218/**
219 * @enum Device access mode
220 */
221enum class AccessMode
222{
223 bytes, ///< Device is accessed by bytes
224 words ///< Device is accessed by words
225};
226
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000227/** @brief implements the get SEL Info command
228 * @returns IPMI completion code plus response data
229 * - selVersion - SEL revision
230 * - entries - Number of log entries in SEL.
231 * - freeSpace - Free Space in bytes.
232 * - addTimeStamp - Most recent addition timestamp
233 * - eraseTimeStamp - Most recent erase timestamp
234 * - operationSupport - Reserve & Delete SEL operations supported
235 */
236
237ipmi::RspType<uint8_t, // SEL revision.
238 uint16_t, // number of log entries in SEL.
239 uint16_t, // free Space in bytes.
240 uint32_t, // most recent addition timestamp
241 uint32_t, // most recent erase timestamp.
242
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500243 bool, // SEL allocation info supported
244 bool, // reserve SEL supported
245 bool, // partial Add SEL Entry supported
246 bool, // delete SEL supported
247 uint3_t, // reserved
248 bool // overflow flag
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000249 >
250 ipmiStorageGetSelInfo()
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530251{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000252 uint16_t entries = 0;
253 // Most recent addition timestamp.
254 uint32_t addTimeStamp = ipmi::sel::invalidTimeStamp;
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530255
Lei YUeba8e9a2021-09-15 13:16:17 +0800256 if (!selCacheMapInitialized)
Tom Josephe59abfb2018-08-06 18:46:27 +0530257 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800258 // In case the initSELCache() fails, try it again
259 initSELCache();
Tom Josephe59abfb2018-08-06 18:46:27 +0530260 }
Lei YUeba8e9a2021-09-15 13:16:17 +0800261 if (!selCacheMap.empty())
Tom Josephe59abfb2018-08-06 18:46:27 +0530262 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800263 entries = static_cast<uint16_t>(selCacheMap.size());
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530264
265 try
266 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800267 auto objPath = getLoggingObjPath(selCacheMap.rbegin()->first);
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000268 addTimeStamp = static_cast<uint32_t>(
Lei YUeba8e9a2021-09-15 13:16:17 +0800269 (ipmi::sel::getEntryTimeStamp(objPath).count()));
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530270 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500271 catch (const InternalFailure& e)
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500272 {}
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530273 catch (const std::runtime_error& e)
274 {
275 log<level::ERR>(e.what());
276 }
277 }
278
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000279 constexpr uint8_t selVersion = ipmi::sel::selVersion;
280 constexpr uint16_t freeSpace = 0xFFFF;
281 constexpr uint32_t eraseTimeStamp = ipmi::sel::invalidTimeStamp;
282 constexpr uint3_t reserved{0};
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530283
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000284 return ipmi::responseSuccess(
285 selVersion, entries, freeSpace, addTimeStamp, eraseTimeStamp,
286 ipmi::sel::operationSupport::getSelAllocationInfo,
287 ipmi::sel::operationSupport::reserveSel,
288 ipmi::sel::operationSupport::partialAddSelEntry,
289 ipmi::sel::operationSupport::deleteSel, reserved,
290 ipmi::sel::operationSupport::overflow);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530291}
292
Willy Tu11d68892022-01-20 10:37:34 -0800293ipmi_ret_t getSELEntry(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request,
294 ipmi_response_t response, ipmi_data_len_t data_len,
295 ipmi_context_t)
Tom Josepha4953392017-06-30 19:09:47 +0530296{
Jason M. Bills851acb12019-02-04 14:06:57 -0800297 if (*data_len != sizeof(ipmi::sel::GetSELEntryRequest))
298 {
299 *data_len = 0;
300 return IPMI_CC_REQ_DATA_LEN_INVALID;
301 }
302
Patrick Venture0b02be92018-08-31 11:55:55 -0700303 auto requestData =
304 reinterpret_cast<const ipmi::sel::GetSELEntryRequest*>(request);
Tom Josepha4953392017-06-30 19:09:47 +0530305
306 if (requestData->reservationID != 0)
307 {
Jason M. Bills13e67c82018-09-10 14:12:16 -0700308 if (!checkSELReservation(requestData->reservationID))
Tom Josepha4953392017-06-30 19:09:47 +0530309 {
310 *data_len = 0;
311 return IPMI_CC_INVALID_RESERVATION_ID;
312 }
313 }
314
Lei YU3df36612021-09-15 11:41:11 +0800315 if (!selCacheMapInitialized)
316 {
317 // In case the initSELCache() fails, try it again
318 initSELCache();
319 }
320
321 if (selCacheMap.empty())
Tom Josepha4953392017-06-30 19:09:47 +0530322 {
323 *data_len = 0;
324 return IPMI_CC_SENSOR_INVALID;
325 }
326
Lei YU3df36612021-09-15 11:41:11 +0800327 SELCacheMap::const_iterator iter;
Tom Josepha4953392017-06-30 19:09:47 +0530328
329 // Check for the requested SEL Entry.
330 if (requestData->selRecordID == ipmi::sel::firstEntry)
331 {
Lei YU3df36612021-09-15 11:41:11 +0800332 iter = selCacheMap.begin();
Tom Josepha4953392017-06-30 19:09:47 +0530333 }
334 else if (requestData->selRecordID == ipmi::sel::lastEntry)
335 {
Lei YU3df36612021-09-15 11:41:11 +0800336 if (selCacheMap.size() > 1)
337 {
338 iter = selCacheMap.end();
339 --iter;
340 }
341 else
342 {
343 // Only one entry exists, return the first
344 iter = selCacheMap.begin();
345 }
Tom Josepha4953392017-06-30 19:09:47 +0530346 }
347 else
348 {
Lei YU3df36612021-09-15 11:41:11 +0800349 iter = selCacheMap.find(requestData->selRecordID);
350 if (iter == selCacheMap.end())
Tom Josepha4953392017-06-30 19:09:47 +0530351 {
352 *data_len = 0;
353 return IPMI_CC_SENSOR_INVALID;
354 }
355 }
356
Lei YU3df36612021-09-15 11:41:11 +0800357 ipmi::sel::GetSELEntryResponse record{0, iter->second};
Tom Josepha4953392017-06-30 19:09:47 +0530358 // Identify the next SEL record ID
Lei YU3df36612021-09-15 11:41:11 +0800359 ++iter;
360 if (iter == selCacheMap.end())
Tom Josepha4953392017-06-30 19:09:47 +0530361 {
Lei YU3df36612021-09-15 11:41:11 +0800362 record.nextRecordID = ipmi::sel::lastEntry;
Tom Josepha4953392017-06-30 19:09:47 +0530363 }
364 else
365 {
Lei YU3df36612021-09-15 11:41:11 +0800366 record.nextRecordID = iter->first;
Tom Josepha4953392017-06-30 19:09:47 +0530367 }
368
369 if (requestData->readLength == ipmi::sel::entireRecord)
370 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700371 std::memcpy(response, &record, sizeof(record));
Tom Josepha4953392017-06-30 19:09:47 +0530372 *data_len = sizeof(record);
373 }
374 else
375 {
376 if (requestData->offset >= ipmi::sel::selRecordSize ||
377 requestData->readLength > ipmi::sel::selRecordSize)
378 {
379 *data_len = 0;
380 return IPMI_CC_INVALID_FIELD_REQUEST;
381 }
382
383 auto diff = ipmi::sel::selRecordSize - requestData->offset;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500384 auto readLength = std::min(diff,
385 static_cast<int>(requestData->readLength));
Tom Josepha4953392017-06-30 19:09:47 +0530386
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700387 std::memcpy(response, &record.nextRecordID,
388 sizeof(record.nextRecordID));
389 std::memcpy(static_cast<uint8_t*>(response) +
390 sizeof(record.nextRecordID),
Lei YUaf378fa2020-12-02 16:28:57 +0800391 &record.event.eventRecord.recordID + requestData->offset,
392 readLength);
Tom Josepha4953392017-06-30 19:09:47 +0530393 *data_len = sizeof(record.nextRecordID) + readLength;
394 }
395
396 return IPMI_CC_OK;
397}
398
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000399/** @brief implements the delete SEL entry command
400 * @request
401 * - reservationID; // reservation ID.
402 * - selRecordID; // SEL record ID.
403 *
404 * @returns ipmi completion code plus response data
405 * - Record ID of the deleted record
406 */
407ipmi::RspType<uint16_t // deleted record ID
408 >
409 deleteSELEntry(uint16_t reservationID, uint16_t selRecordID)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530410{
Brad Bishop1a4117b2018-11-21 15:48:18 -0500411 namespace fs = std::filesystem;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530412
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000413 if (!checkSELReservation(reservationID))
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530414 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000415 return ipmi::responseInvalidReservationId();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530416 }
417
Jason M. Bills13e67c82018-09-10 14:12:16 -0700418 // Per the IPMI spec, need to cancel the reservation when a SEL entry is
419 // deleted
420 cancelSELReservation();
421
Lei YUeba8e9a2021-09-15 13:16:17 +0800422 if (!selCacheMapInitialized)
Tom Josephe59abfb2018-08-06 18:46:27 +0530423 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800424 // In case the initSELCache() fails, try it again
425 initSELCache();
Tom Josephe59abfb2018-08-06 18:46:27 +0530426 }
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530427
Lei YUeba8e9a2021-09-15 13:16:17 +0800428 if (selCacheMap.empty())
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530429 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000430 return ipmi::responseSensorInvalid();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530431 }
432
Lei YUeba8e9a2021-09-15 13:16:17 +0800433 SELCacheMap::const_iterator iter;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530434 uint16_t delRecordID = 0;
435
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000436 if (selRecordID == ipmi::sel::firstEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530437 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800438 delRecordID = selCacheMap.begin()->first;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530439 }
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000440 else if (selRecordID == ipmi::sel::lastEntry)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530441 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800442 delRecordID = selCacheMap.rbegin()->first;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530443 }
444 else
445 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000446 delRecordID = selRecordID;
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530447 }
448
Jian7d5f4812022-02-23 19:30:27 +0800449 iter = selCacheMap.find(delRecordID);
450 if (iter == selCacheMap.end())
451 {
452 return ipmi::responseSensorInvalid();
453 }
454
Patrick Williams5d82f472022-07-22 19:26:53 -0500455 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530456 std::string service;
457
Lei YUeba8e9a2021-09-15 13:16:17 +0800458 auto objPath = getLoggingObjPath(iter->first);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530459 try
460 {
Lei YUeba8e9a2021-09-15 13:16:17 +0800461 service = ipmi::getService(bus, ipmi::sel::logDeleteIntf, objPath);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530462 }
463 catch (const std::runtime_error& e)
464 {
465 log<level::ERR>(e.what());
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000466 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530467 }
468
Lei YUeba8e9a2021-09-15 13:16:17 +0800469 auto methodCall = bus.new_method_call(service.c_str(), objPath.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700470 ipmi::sel::logDeleteIntf, "Delete");
George Liu3e3cc352023-07-26 15:59:31 +0800471 try
472 {
473 auto reply = bus.call(methodCall);
474 }
475 catch (const std::exception& e)
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530476 {
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000477 return ipmi::responseUnspecifiedError();
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530478 }
479
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000480 return ipmi::responseSuccess(delRecordID);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530481}
482
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000483/** @brief implements the Clear SEL command
484 * @request
485 * - reservationID // Reservation ID.
486 * - clr // char array { 'C'(0x43h), 'L'(0x4Ch), 'R'(0x52h) }
487 * - eraseOperation; // requested operation.
488 *
489 * @returns ipmi completion code plus response data
490 * - erase status
491 */
492
493ipmi::RspType<uint8_t // erase status
494 >
495 clearSEL(uint16_t reservationID, const std::array<char, 3>& clr,
496 uint8_t eraseOperation)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530497{
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000498 static constexpr std::array<char, 3> clrOk = {'C', 'L', 'R'};
499 if (clr != clrOk)
Jason M. Bills851acb12019-02-04 14:06:57 -0800500 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000501 return ipmi::responseInvalidFieldRequest();
Jason M. Bills851acb12019-02-04 14:06:57 -0800502 }
503
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000504 if (!checkSELReservation(reservationID))
Tom Joseph2f05bb52017-06-30 19:14:49 +0530505 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000506 return ipmi::responseInvalidReservationId();
Tom Joseph2f05bb52017-06-30 19:14:49 +0530507 }
508
Tom Joseph2f05bb52017-06-30 19:14:49 +0530509 /*
510 * Erasure status cannot be fetched from DBUS, so always return erasure
511 * status as `erase completed`.
512 */
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000513 if (eraseOperation == ipmi::sel::getEraseStatus)
Tom Joseph2f05bb52017-06-30 19:14:49 +0530514 {
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000515 return ipmi::responseSuccess(
516 static_cast<uint8_t>(ipmi::sel::eraseComplete));
Tom Joseph2f05bb52017-06-30 19:14:49 +0530517 }
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 Williamsfbc6c9d2023-05-10 07:50:16 -0500524 auto method = bus.new_method_call(service.c_str(), ipmi::sel::logObj,
525 ipmi::sel::logIntf,
526 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 {
Lotus Xu78fe1b12021-05-23 17:26:56 +0800533 log<level::ERR>("Error eraseAll ", entry("ERROR=%s", e.what()));
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);
Vernon Mauery16b86932019-05-01 08:36:11 -0700556 std::variant<uint64_t> value;
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530557
George Liu4d623e92020-05-25 16:51:57 +0800558 // Get bmc time
559 auto method = bus.new_method_call(service.c_str(), BMC_TIME_PATH,
Patrick Venture0b02be92018-08-31 11:55:55 -0700560 DBUS_PROPERTIES, "Get");
Lei YUe8939392017-06-15 10:45:05 +0800561
562 method.append(TIME_INTERFACE, PROPERTY_ELAPSED);
563 auto reply = bus.call(method);
Lei YUe8939392017-06-15 10:45:05 +0800564 reply.read(value);
George Liu4d623e92020-05-25 16:51:57 +0800565 bmc_time_usec = std::get<uint64_t>(value);
Lei YUe8939392017-06-15 10:45:05 +0800566 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500567 catch (const InternalFailure& e)
Lei YUe8939392017-06-15 10:45:05 +0800568 {
569 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000570 return ipmi::responseUnspecifiedError();
Lei YUe8939392017-06-15 10:45:05 +0800571 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530572 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800573 {
574 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000575 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530576 }
577
George Liu4d623e92020-05-25 16:51:57 +0800578 bmcTime << "BMC time:"
579 << duration_cast<seconds>(microseconds(bmc_time_usec)).count();
580 log<level::DEBUG>(bmcTime.str().c_str());
Nagaraju Goruganti8960b7c2018-04-29 22:38:40 -0500581
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530582 // Time is really long int but IPMI wants just uint32. This works okay until
583 // the number of seconds since 1970 overflows uint32 size.. Still a whole
584 // lot of time here to even think about that.
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000585 return ipmi::responseSuccess(
George Liu4d623e92020-05-25 16:51:57 +0800586 duration_cast<seconds>(microseconds(bmc_time_usec)).count());
Adriana Kobylak8e30f2a2015-10-20 10:23:51 -0500587}
588
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000589/** @brief implements the set SEL time command
590 * @param selDeviceTime - epoch time
591 * -local time as the number of seconds from 00:00:00, January 1, 1970
592 * @returns IPMI completion code
593 */
594ipmi::RspType<> ipmiStorageSetSelTime(uint32_t selDeviceTime)
Chris Austenb4f5b922015-10-13 12:44:43 -0500595{
Lei YUe8939392017-06-15 10:45:05 +0800596 using namespace std::chrono;
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000597 microseconds usec{seconds(selDeviceTime)};
Norman James82330442015-11-19 16:53:26 -0600598
Lei YUe8939392017-06-15 10:45:05 +0800599 try
600 {
Patrick Williams5d82f472022-07-22 19:26:53 -0500601 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
shamim ali071d00c2022-03-02 15:42:46 +0530602 bool ntp = std::get<bool>(
603 ipmi::getDbusProperty(bus, SystemdTimeService, SystemdTimePath,
604 SystemdTimeInterface, "NTP"));
605 if (ntp)
606 {
607 return ipmi::responseCommandNotAvailable();
608 }
609
George Liu4d623e92020-05-25 16:51:57 +0800610 auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
Andrew Geissler6467ed22020-05-16 16:03:53 -0500611 std::variant<uint64_t> value{(uint64_t)usec.count()};
Lei YUe8939392017-06-15 10:45:05 +0800612
George Liu4d623e92020-05-25 16:51:57 +0800613 // Set bmc time
614 auto method = bus.new_method_call(service.c_str(), BMC_TIME_PATH,
Patrick Venture0b02be92018-08-31 11:55:55 -0700615 DBUS_PROPERTIES, "Set");
Lei YUe8939392017-06-15 10:45:05 +0800616
617 method.append(TIME_INTERFACE, PROPERTY_ELAPSED, value);
618 auto reply = bus.call(method);
Norman James82330442015-11-19 16:53:26 -0600619 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500620 catch (const InternalFailure& e)
Lei YUe8939392017-06-15 10:45:05 +0800621 {
622 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000623 return ipmi::responseUnspecifiedError();
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530624 }
Tom Joseph30fd0a12019-09-24 06:53:22 +0530625 catch (const std::exception& e)
Lei YUe8939392017-06-15 10:45:05 +0800626 {
627 log<level::ERR>(e.what());
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000628 return ipmi::responseUnspecifiedError();
Norman James82330442015-11-19 16:53:26 -0600629 }
Vishwanatha Subbanna5fba7a62016-09-01 14:06:07 +0530630
jayaprakash Mutyaladb2e8c42019-05-03 01:38:01 +0000631 return ipmi::responseSuccess();
Chris Austenb4f5b922015-10-13 12:44:43 -0500632}
633
Xiaochao Maf020c442022-10-31 11:09:38 +0800634/** @brief implements the get SEL timezone command
635 * @returns IPMI completion code plus response data
636 * -current timezone
637 */
638ipmi::RspType<int16_t> ipmiStorageGetSelTimeUtcOffset()
639{
640 time_t timep;
641 struct tm* gmTime;
642 struct tm* localTime;
643
644 time(&timep);
645 localTime = localtime(&timep);
646 auto validLocalTime = mktime(localTime);
647 gmTime = gmtime(&timep);
648 auto validGmTime = mktime(gmTime);
649 auto timeEquation = (validLocalTime - validGmTime) / 60;
650
651 return ipmi::responseSuccess(timeEquation);
652}
653
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000654/** @brief implements the reserve SEL command
655 * @returns IPMI completion code plus response data
656 * - SEL reservation ID.
657 */
658ipmi::RspType<uint16_t> ipmiStorageReserveSel()
Chris Austenb4f5b922015-10-13 12:44:43 -0500659{
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000660 return ipmi::responseSuccess(reserveSel());
Chris Austenb4f5b922015-10-13 12:44:43 -0500661}
662
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000663/** @brief implements the Add SEL entry command
664 * @request
665 *
666 * - recordID ID used for SEL Record access
667 * - recordType Record Type
668 * - timeStamp Time when event was logged. LS byte first
669 * - generatorID software ID if event was generated from
670 * system software
671 * - evmRev event message format version
672 * - sensorType sensor type code for service that generated
673 * the event
674 * - sensorNumber number of sensors that generated the event
675 * - eventDir event dir
676 * - eventData event data field contents
677 *
678 * @returns ipmi completion code plus response data
679 * - RecordID of the Added SEL entry
680 */
681ipmi::RspType<uint16_t // recordID of the Added SEL entry
682 >
Willy Tu11d68892022-01-20 10:37:34 -0800683 ipmiStorageAddSEL(uint16_t recordID, uint8_t recordType,
684 [[maybe_unused]] uint32_t timeStamp, uint16_t generatorID,
685 [[maybe_unused]] uint8_t evmRev, uint8_t sensorType,
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000686 uint8_t sensorNumber, uint8_t eventDir,
687 std::array<uint8_t, eventDataSize> eventData)
Chris Austenb4f5b922015-10-13 12:44:43 -0500688{
Lotus Xu57d35572021-01-24 11:13:13 +0800689 std::string objpath;
690 static constexpr auto systemRecordType = 0x02;
Tom Josephb647d5b2017-10-31 17:25:33 +0530691 // Hostboot sends SEL with OEM record type 0xDE to indicate that there is
692 // a maintenance procedure associated with eSEL record.
693 static constexpr auto procedureType = 0xDE;
Lotus Xu57d35572021-01-24 11:13:13 +0800694 cancelSELReservation();
695 if (recordType == systemRecordType)
696 {
Lotus Xu57d35572021-01-24 11:13:13 +0800697 for (const auto& it : invSensors)
698 {
699 if (it.second.sensorID == sensorNumber)
700 {
701 objpath = it.first;
702 break;
703 }
704 }
705 auto selDataStr = ipmi::sel::toHexStr(eventData);
706
707 bool assert = (eventDir & 0x80) ? false : true;
708
709 recordID = report<SELCreated>(Created::RECORD_TYPE(recordType),
710 Created::GENERATOR_ID(generatorID),
711 Created::SENSOR_DATA(selDataStr.c_str()),
712 Created::EVENT_DIR(assert),
713 Created::SENSOR_PATH(objpath.c_str()));
714 }
Patrick Williams768730d2023-09-06 16:27:20 -0500715#ifdef OPEN_POWER_SUPPORT
Lotus Xu57d35572021-01-24 11:13:13 +0800716 else if (recordType == procedureType)
Tom Josephb647d5b2017-10-31 17:25:33 +0530717 {
718 // In the OEM record type 0xDE, byte 11 in the SEL record indicate the
719 // procedure number.
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000720 createProcedureLogEntry(sensorType);
Tom Josephb647d5b2017-10-31 17:25:33 +0530721 }
Patrick Williams768730d2023-09-06 16:27:20 -0500722#endif
Chris Austenb4f5b922015-10-13 12:44:43 -0500723
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000724 return ipmi::responseSuccess(recordID);
Chris Austenb4f5b922015-10-13 12:44:43 -0500725}
726
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300727bool isFruPresent(ipmi::Context::ptr& ctx, const std::string& fruPath)
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300728{
729 using namespace ipmi::fru;
730
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300731 std::string service;
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500732 boost::system::error_code ec = getService(ctx, invItemInterface,
733 invObjPath + fruPath, service);
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300734 if (!ec)
735 {
736 bool result;
737 ec = ipmi::getDbusProperty(ctx, service, invObjPath + fruPath,
738 invItemInterface, itemPresentProp, result);
739 if (!ec)
740 {
741 return result;
742 }
743 }
Konstantin Aladyshevc7e4b042021-12-24 15:30:47 +0300744
745 ipmi::ObjectValueTree managedObjects;
Nan Zhou947da1b2022-09-20 20:40:59 +0000746 ec = getManagedObjects(ctx, "xyz.openbmc_project.EntityManager",
747 "/xyz/openbmc_project/inventory", managedObjects);
Konstantin Aladyshevc7e4b042021-12-24 15:30:47 +0300748 if (!ec)
749 {
750 auto connection = managedObjects.find(fruPath);
751 if (connection != managedObjects.end())
752 {
753 return true;
754 }
755 }
756
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300757 return false;
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300758}
759
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000760/** @brief implements the get FRU Inventory Area Info command
761 *
762 * @returns IPMI completion code plus response data
763 * - FRU Inventory area size in bytes,
764 * - access bit
765 **/
766ipmi::RspType<uint16_t, // FRU Inventory area size in bytes,
767 uint8_t // access size (bytes / words)
768 >
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300769 ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruID)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500770{
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000771 auto iter = frus.find(fruID);
Tom Joseph187f5642018-03-29 13:49:06 +0530772 if (iter == frus.end())
773 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000774 return ipmi::responseSensorInvalid();
Tom Joseph187f5642018-03-29 13:49:06 +0530775 }
776
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300777 auto path = iter->second[0].path;
Konstantin Aladyshev69e3cd42021-11-26 16:46:17 +0300778 if (!isFruPresent(ctx, path))
Kirill Pakhomovfa6e2092020-04-24 18:57:15 +0300779 {
780 return ipmi::responseSensorInvalid();
781 }
782
Marri Devender Raocac383b2017-07-03 13:24:27 -0500783 try
784 {
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000785 return ipmi::responseSuccess(
786 static_cast<uint16_t>(getFruAreaData(fruID).size()),
787 static_cast<uint8_t>(AccessMode::bytes));
Marri Devender Raocac383b2017-07-03 13:24:27 -0500788 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700789 catch (const InternalFailure& e)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500790 {
Marri Devender Raocac383b2017-07-03 13:24:27 -0500791 log<level::ERR>(e.what());
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000792 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500793 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500794}
795
anil kumar appana5b7c3262019-05-27 18:10:23 +0000796/**@brief implements the Read FRU Data command
797 * @param fruDeviceId - FRU device ID. FFh = reserved
798 * @param offset - FRU inventory offset to read
799 * @param readCount - count to read
800 *
801 * @return IPMI completion code plus response data
802 * - returnCount - response data count.
803 * - data - response data
804 */
805ipmi::RspType<uint8_t, // count returned
806 std::vector<uint8_t>> // FRU data
807 ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t offset,
808 uint8_t readCount)
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500809{
anil kumar appana5b7c3262019-05-27 18:10:23 +0000810 if (fruDeviceId == 0xFF)
Tom Joseph187f5642018-03-29 13:49:06 +0530811 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000812 return ipmi::responseInvalidFieldRequest();
Tom Joseph187f5642018-03-29 13:49:06 +0530813 }
814
anil kumar appana5b7c3262019-05-27 18:10:23 +0000815 auto iter = frus.find(fruDeviceId);
816 if (iter == frus.end())
817 {
818 return ipmi::responseSensorInvalid();
819 }
820
Marri Devender Raocac383b2017-07-03 13:24:27 -0500821 try
822 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000823 const auto& fruArea = getFruAreaData(fruDeviceId);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500824 auto size = fruArea.size();
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500825
Tom Josephefcd68b2018-04-26 18:46:27 +0530826 if (offset >= size)
827 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000828 return ipmi::responseParmOutOfRange();
Tom Josephefcd68b2018-04-26 18:46:27 +0530829 }
830
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500831 // Write the count of response data.
anil kumar appana5b7c3262019-05-27 18:10:23 +0000832 uint8_t returnCount;
833 if ((offset + readCount) <= size)
Marri Devender Raocac383b2017-07-03 13:24:27 -0500834 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000835 returnCount = readCount;
Nagaraju Goruganti7f2d7c92018-03-21 11:18:30 -0500836 }
837 else
838 {
anil kumar appana5b7c3262019-05-27 18:10:23 +0000839 returnCount = size - offset;
Marri Devender Raocac383b2017-07-03 13:24:27 -0500840 }
Ratan Gupta2848d602018-01-31 20:39:20 +0530841
anil kumar appana5b7c3262019-05-27 18:10:23 +0000842 std::vector<uint8_t> fruData((fruArea.begin() + offset),
843 (fruArea.begin() + offset + returnCount));
Ratan Gupta2848d602018-01-31 20:39:20 +0530844
anil kumar appana5b7c3262019-05-27 18:10:23 +0000845 return ipmi::responseSuccess(returnCount, fruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500846 }
847 catch (const InternalFailure& e)
848 {
Marri Devender Raocac383b2017-07-03 13:24:27 -0500849 log<level::ERR>(e.what());
anil kumar appana5b7c3262019-05-27 18:10:23 +0000850 return ipmi::responseUnspecifiedError();
Marri Devender Raocac383b2017-07-03 13:24:27 -0500851 }
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500852}
853
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000854ipmi::RspType<uint8_t, // SDR version
855 uint16_t, // record count LS first
856 uint16_t, // free space in bytes, LS first
857 uint32_t, // addition timestamp LS first
858 uint32_t, // deletion timestamp LS first
859 uint8_t> // operation Support
860 ipmiGetRepositoryInfo()
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600861{
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000862 constexpr uint8_t sdrVersion = 0x51;
863 constexpr uint16_t freeSpace = 0xFFFF;
864 constexpr uint32_t additionTimestamp = 0x0;
865 constexpr uint32_t deletionTimestamp = 0x0;
866 constexpr uint8_t operationSupport = 0;
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600867
Willy Tu0ca3bfe2022-01-28 09:40:18 -0800868 // Get SDR count. This returns the total number of SDRs in the device.
869 const auto& entityRecords =
870 ipmi::sensor::EntityInfoMapContainer::getContainer()
871 ->getIpmiEntityRecords();
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500872 uint16_t records = ipmi::sensor::sensors.size() + frus.size() +
873 entityRecords.size();
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600874
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000875 return ipmi::responseSuccess(sdrVersion, records, freeSpace,
876 additionTimestamp, deletionTimestamp,
877 operationSupport);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600878}
Chris Austenb4f5b922015-10-13 12:44:43 -0500879
Chris Austenb4f5b922015-10-13 12:44:43 -0500880void register_netfn_storage_functions()
881{
Lei YU3df36612021-09-15 11:41:11 +0800882 selCacheMapInitialized = false;
Lei YUd9e57662021-09-14 18:06:28 +0800883 initSELCache();
Willy Tud351a722021-08-12 14:33:40 -0700884 // Handlers with dbus-sdr handler implementation.
885 // Do not register the hander if it dynamic sensors stack is used.
886
887#ifndef FEATURE_DYNAMIC_SENSORS
Thang Tran292c9172023-06-26 10:03:04 +0700888
889#ifndef FEATURE_DYNAMIC_STORAGES_ONLY
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530890 // <Get SEL Info>
jayaprakash Mutyalab7557722019-05-02 21:13:30 +0000891 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
892 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
893 ipmiStorageGetSelInfo);
Tom Joseph6f7deaa2017-06-30 19:03:54 +0530894
Xiaochao Maf020c442022-10-31 11:09:38 +0800895 // <Get SEL Timezone>
896 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
897 ipmi::storage::cmdGetSelTimeUtcOffset,
898 ipmi::Privilege::User,
899 ipmiStorageGetSelTimeUtcOffset);
900
Tom Josepha4953392017-06-30 19:09:47 +0530901 // <Get SEL Entry>
Patrick Venture0b02be92018-08-31 11:55:55 -0700902 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_ENTRY, NULL,
903 getSELEntry, PRIVILEGE_USER);
Tom Josepha4953392017-06-30 19:09:47 +0530904
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530905 // <Delete SEL Entry>
Pradeep Kumar00a18d02019-04-26 17:04:28 +0000906 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
907 ipmi::storage::cmdDeleteSelEntry,
908 ipmi::Privilege::Operator, deleteSELEntry);
Tom Joseph8f4a2aa2017-06-30 19:12:49 +0530909
Tom05732372016-09-06 17:21:23 +0530910 // <Add SEL Entry>
anil kumar appana2c7db1d2019-05-28 11:20:19 +0000911 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
912 ipmi::storage::cmdAddSelEntry,
913 ipmi::Privilege::Operator, ipmiStorageAddSEL);
914
Tom Joseph2f05bb52017-06-30 19:14:49 +0530915 // <Clear SEL>
Pradeep Kumar4a5a99a2019-04-26 15:22:39 +0000916 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
917 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
918 clearSEL);
919
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500920 // <Get FRU Inventory Area Info>
Pradeep Kumarb0c794d2019-05-02 13:09:14 +0000921 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
922 ipmi::storage::cmdGetFruInventoryAreaInfo,
923 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Marri Devender Raofa7b4e22017-07-03 00:52:20 -0500924
Jason M. Billsb5248c92019-06-24 15:53:08 -0700925 // <READ FRU Data>
anil kumar appana5b7c3262019-05-27 18:10:23 +0000926 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
927 ipmi::storage::cmdReadFruData,
928 ipmi::Privilege::Operator, ipmiStorageReadFruData);
Marri Devender Raocac383b2017-07-03 13:24:27 -0500929
Thang Tran292c9172023-06-26 10:03:04 +0700930#endif // FEATURE_DYNAMIC_STORAGES_ONLY
931
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600932 // <Get Repository Info>
Pradeep Kumarb60e8402019-05-06 15:17:01 +0000933 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
934 ipmi::storage::cmdGetSdrRepositoryInfo,
935 ipmi::Privilege::User, ipmiGetRepositoryInfo);
Dhruvaraj Subhashchandrane66c3b02018-02-07 01:21:56 -0600936
Tom Joseph5ca50952018-02-22 00:33:38 +0530937 // <Reserve SDR Repository>
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000938 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
939 ipmi::storage::cmdReserveSdrRepository,
940 ipmi::Privilege::User, ipmiSensorReserveSdr);
Tom Joseph5ca50952018-02-22 00:33:38 +0530941
942 // <Get SDR>
Patrick Venture0b02be92018-08-31 11:55:55 -0700943 ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SDR, nullptr,
944 ipmi_sen_get_sdr, PRIVILEGE_USER);
Tom Joseph5ca50952018-02-22 00:33:38 +0530945
Willy Tud351a722021-08-12 14:33:40 -0700946#endif
947
948 // Common Handers used by both implementation.
949
950 // <Reserve SEL>
951 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
952 ipmi::storage::cmdReserveSel, ipmi::Privilege::User,
953 ipmiStorageReserveSel);
954
Thang Trandbf66e12023-09-21 13:07:24 +0700955 // <Get SEL Time>
956 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
957 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
958 ipmiStorageGetSelTime);
959
960 // <Set SEL Time>
961 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
962 ipmi::storage::cmdSetSelTime,
963 ipmi::Privilege::Operator, ipmiStorageSetSelTime);
964
Marri Devender Rao908f7502017-07-10 01:49:54 -0500965 ipmi::fru::registerCallbackHandler();
Chris Austenb4f5b922015-10-13 12:44:43 -0500966 return;
967}