xref: /openbmc/phosphor-host-ipmid/storagehandler.cpp (revision 3ad197160a5845de3760aa82aa586228f61d19f5)
1768730d2SPatrick Williams #include "config.h"
2768730d2SPatrick Williams 
33a5071a9SPatrick Venture #include "fruread.hpp"
43a5071a9SPatrick Venture #include "read_fru_data.hpp"
53a5071a9SPatrick Venture #include "selutility.hpp"
63a5071a9SPatrick Venture #include "sensorhandler.hpp"
73a5071a9SPatrick Venture #include "storageaddsel.hpp"
83a5071a9SPatrick Venture 
952d91242SLei YU #include <arpa/inet.h>
103a5071a9SPatrick Venture #include <systemd/sd-bus.h>
110b02be92SPatrick Venture 
12fbc6c9d7SPatrick Williams #include <ipmid/api.hpp>
139cf0838aSVernon Mauery #include <ipmid/entity_map_json.hpp>
14fbc6c9d7SPatrick Williams #include <ipmid/utils.hpp>
15fbc6c9d7SPatrick Williams #include <phosphor-logging/elog-errors.hpp>
16fbc6c9d7SPatrick Williams #include <phosphor-logging/elog.hpp>
1751856e68SGeorge Liu #include <phosphor-logging/lg2.hpp>
18fbc6c9d7SPatrick Williams #include <sdbusplus/server.hpp>
19fbc6c9d7SPatrick Williams #include <xyz/openbmc_project/Common/error.hpp>
20fbc6c9d7SPatrick Williams #include <xyz/openbmc_project/Logging/SEL/error.hpp>
21fbc6c9d7SPatrick Williams 
220b02be92SPatrick Venture #include <algorithm>
2352d91242SLei YU #include <chrono>
2452d91242SLei YU #include <cstdio>
25b51bf9c8SPatrick Venture #include <cstring>
26bdda8008SVernon Mauery #include <filesystem>
27a0bb2a33SLei YU #include <optional>
283a5071a9SPatrick Venture #include <string>
2916b8693dSVernon Mauery #include <variant>
303a5071a9SPatrick Venture 
315087b075SGeorge Liu void registerNetFnStorageFunctions() __attribute__((constructor));
3298a23840SMatthew Barth 
3398a23840SMatthew Barth unsigned int g_sel_time = 0xFFFFFFFF;
34db0cbe64SPatrick Venture namespace ipmi
35db0cbe64SPatrick Venture {
36db0cbe64SPatrick Venture namespace sensor
37db0cbe64SPatrick Venture {
38db0cbe64SPatrick Venture extern const IdInfoMap sensors;
39db0cbe64SPatrick Venture } // namespace sensor
40db0cbe64SPatrick Venture } // namespace ipmi
4157d35579SLotus Xu extern const ipmi::sensor::InvObjectIDMap invSensors;
42e66c3b04SDhruvaraj Subhashchandran extern const FruMap frus;
432c7db1d3Sanil kumar appana constexpr uint8_t eventDataSize = 3;
440b02be92SPatrick Venture namespace
450b02be92SPatrick Venture {
46071d00c5Sshamim ali constexpr auto SystemdTimeService = "org.freedesktop.timedate1";
47071d00c5Sshamim ali constexpr auto SystemdTimePath = "/org/freedesktop/timedate1";
48071d00c5Sshamim ali constexpr auto SystemdTimeInterface = "org.freedesktop.timedate1";
49071d00c5Sshamim ali 
50e8939395SLei YU constexpr auto TIME_INTERFACE = "xyz.openbmc_project.Time.EpochTime";
514d623e98SGeorge Liu constexpr auto BMC_TIME_PATH = "/xyz/openbmc_project/time/bmc";
52e8939395SLei YU constexpr auto DBUS_PROPERTIES = "org.freedesktop.DBus.Properties";
53e8939395SLei YU constexpr auto PROPERTY_ELAPSED = "Elapsed";
540b02be92SPatrick Venture } // namespace
555fba7a65SVishwanatha Subbanna 
566f7deaa0STom Joseph using InternalFailure =
57523e2d1bSWilly Tu     sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
586f7deaa0STom Joseph using namespace phosphor::logging;
59cac383bfSMarri Devender Rao using namespace ipmi::fru;
60523e2d1bSWilly Tu using namespace xyz::openbmc_project::logging::sel;
6157d35579SLotus Xu using SELCreated =
62523e2d1bSWilly Tu     sdbusplus::error::xyz::openbmc_project::logging::sel::Created;
63cac383bfSMarri Devender Rao 
64d9e5766cSLei YU using SELRecordID = uint16_t;
65d9e5766cSLei YU using SELEntry = ipmi::sel::SELEventRecordFormat;
66d9e5766cSLei YU using SELCacheMap = std::map<SELRecordID, SELEntry>;
67d9e5766cSLei YU 
68d9e5766cSLei YU SELCacheMap selCacheMap __attribute__((init_priority(101)));
693df3661eSLei YU bool selCacheMapInitialized;
705d82f474SPatrick Williams std::unique_ptr<sdbusplus::bus::match_t> selAddedMatch
71d955525cSLei YU     __attribute__((init_priority(101)));
725d82f474SPatrick Williams std::unique_ptr<sdbusplus::bus::match_t> selRemovedMatch
73b6b72b03SLei YU     __attribute__((init_priority(101)));
745d82f474SPatrick Williams std::unique_ptr<sdbusplus::bus::match_t> selUpdatedMatch
754106e427SLei YU     __attribute__((init_priority(101)));
76d9e5766cSLei YU 
getLoggingId(const std::string & p)77b6b72b03SLei YU static inline uint16_t getLoggingId(const std::string& p)
78d9e5766cSLei YU {
79d9e5766cSLei YU     namespace fs = std::filesystem;
80d9e5766cSLei YU     fs::path entryPath(p);
81b6b72b03SLei YU     return std::stoul(entryPath.filename().string());
82b6b72b03SLei YU }
83b6b72b03SLei YU 
getLoggingObjPath(uint16_t id)84eba8e9a2SLei YU static inline std::string getLoggingObjPath(uint16_t id)
85eba8e9a2SLei YU {
86eba8e9a2SLei YU     return std::string(ipmi::sel::logBasePath) + "/" + std::to_string(id);
87eba8e9a2SLei YU }
88eba8e9a2SLei YU 
parseLoggingEntry(const std::string & p)8969b4c281SPatrick Williams std::optional<std::pair<uint16_t, SELEntry>> parseLoggingEntry(
9069b4c281SPatrick Williams     const std::string& p)
91a0bb2a33SLei YU {
92a0bb2a33SLei YU     try
93b6b72b03SLei YU     {
94b6b72b03SLei YU         auto id = getLoggingId(p);
953df3661eSLei YU         ipmi::sel::GetSELEntryResponse record{};
963df3661eSLei YU         record = ipmi::sel::convertLogEntrytoSEL(p);
97a0bb2a33SLei YU         return std::pair<uint16_t, SELEntry>({id, std::move(record.event)});
983df3661eSLei YU     }
993df3661eSLei YU     catch (const std::exception& e)
1003df3661eSLei YU     {
1013df3661eSLei YU         fprintf(stderr, "Failed to convert %s to SEL: %s\n", p.c_str(),
1023df3661eSLei YU                 e.what());
1033df3661eSLei YU     }
104a0bb2a33SLei YU     return std::nullopt;
105d9e5766cSLei YU }
106d9e5766cSLei YU 
selAddedCallback(sdbusplus::message_t & m)1075d82f474SPatrick Williams static void selAddedCallback(sdbusplus::message_t& m)
108d955525cSLei YU {
109d955525cSLei YU     sdbusplus::message::object_path objPath;
110d955525cSLei YU     try
111d955525cSLei YU     {
112d955525cSLei YU         m.read(objPath);
113d955525cSLei YU     }
1145d82f474SPatrick Williams     catch (const sdbusplus::exception_t& e)
115d955525cSLei YU     {
11651856e68SGeorge Liu         lg2::error("Failed to read object path");
117d955525cSLei YU         return;
118d955525cSLei YU     }
119d955525cSLei YU     std::string p = objPath;
120a0bb2a33SLei YU     auto entry = parseLoggingEntry(p);
121a0bb2a33SLei YU     if (entry)
122a0bb2a33SLei YU     {
123a0bb2a33SLei YU         selCacheMap.insert(std::move(*entry));
124a0bb2a33SLei YU     }
125d955525cSLei YU }
126d955525cSLei YU 
selRemovedCallback(sdbusplus::message_t & m)1275d82f474SPatrick Williams static void selRemovedCallback(sdbusplus::message_t& m)
128b6b72b03SLei YU {
129b6b72b03SLei YU     sdbusplus::message::object_path objPath;
130b6b72b03SLei YU     try
131b6b72b03SLei YU     {
132b6b72b03SLei YU         m.read(objPath);
133b6b72b03SLei YU     }
1345d82f474SPatrick Williams     catch (const sdbusplus::exception_t& e)
135b6b72b03SLei YU     {
13651856e68SGeorge Liu         lg2::error("Failed to read object path");
137b6b72b03SLei YU     }
138a0bb2a33SLei YU     try
139a0bb2a33SLei YU     {
140b6b72b03SLei YU         std::string p = objPath;
141b6b72b03SLei YU         selCacheMap.erase(getLoggingId(p));
142b6b72b03SLei YU     }
143a0bb2a33SLei YU     catch (const std::invalid_argument& e)
144a0bb2a33SLei YU     {
14551856e68SGeorge Liu         lg2::error("Invalid logging entry ID");
146a0bb2a33SLei YU     }
147a0bb2a33SLei YU }
148b6b72b03SLei YU 
selUpdatedCallback(sdbusplus::message_t & m)1495d82f474SPatrick Williams static void selUpdatedCallback(sdbusplus::message_t& m)
1504106e427SLei YU {
1514106e427SLei YU     std::string p = m.get_path();
1524106e427SLei YU     auto entry = parseLoggingEntry(p);
153a0bb2a33SLei YU     if (entry)
154a0bb2a33SLei YU     {
155a0bb2a33SLei YU         selCacheMap.insert_or_assign(entry->first, std::move(entry->second));
156a0bb2a33SLei YU     }
1574106e427SLei YU }
1584106e427SLei YU 
registerSelCallbackHandler()159d955525cSLei YU void registerSelCallbackHandler()
160d955525cSLei YU {
161d955525cSLei YU     using namespace sdbusplus::bus::match::rules;
1625d82f474SPatrick Williams     sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
163d955525cSLei YU     if (!selAddedMatch)
164d955525cSLei YU     {
1655d82f474SPatrick Williams         selAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
166656ae3c5SGeorge Liu             bus, interfacesAdded(ipmi::sel::logWatchPath),
167d955525cSLei YU             std::bind(selAddedCallback, std::placeholders::_1));
168d955525cSLei YU     }
169b6b72b03SLei YU     if (!selRemovedMatch)
170b6b72b03SLei YU     {
1715d82f474SPatrick Williams         selRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
172656ae3c5SGeorge Liu             bus, interfacesRemoved(ipmi::sel::logWatchPath),
173b6b72b03SLei YU             std::bind(selRemovedCallback, std::placeholders::_1));
174b6b72b03SLei YU     }
1754106e427SLei YU     if (!selUpdatedMatch)
1764106e427SLei YU     {
1775d82f474SPatrick Williams         selUpdatedMatch = std::make_unique<sdbusplus::bus::match_t>(
1784106e427SLei YU             bus,
1794106e427SLei YU             type::signal() + member("PropertiesChanged"s) +
1804106e427SLei YU                 interface("org.freedesktop.DBus.Properties"s) +
181656ae3c5SGeorge Liu                 argN(0, ipmi::sel::logEntryIntf),
1824106e427SLei YU             std::bind(selUpdatedCallback, std::placeholders::_1));
1834106e427SLei YU     }
184d955525cSLei YU }
185d955525cSLei YU 
initSELCache()186d9e5766cSLei YU void initSELCache()
187d9e5766cSLei YU {
188c10cda1dSLei YU     registerSelCallbackHandler();
189eba8e9a2SLei YU     ipmi::sel::ObjectPaths paths;
190d9e5766cSLei YU     try
191d9e5766cSLei YU     {
192eba8e9a2SLei YU         ipmi::sel::readLoggingObjectPaths(paths);
193d9e5766cSLei YU     }
1945d82f474SPatrick Williams     catch (const sdbusplus::exception_t& e)
195d9e5766cSLei YU     {
19651856e68SGeorge Liu         lg2::error("Failed to get logging object paths");
197d9e5766cSLei YU         return;
198d9e5766cSLei YU     }
199eba8e9a2SLei YU     for (const auto& p : paths)
200d9e5766cSLei YU     {
201a0bb2a33SLei YU         auto entry = parseLoggingEntry(p);
202a0bb2a33SLei YU         if (entry)
203a0bb2a33SLei YU         {
204a0bb2a33SLei YU             selCacheMap.insert(std::move(*entry));
205a0bb2a33SLei YU         }
206d9e5766cSLei YU     }
2073df3661eSLei YU     selCacheMapInitialized = true;
208d9e5766cSLei YU }
209d9e5766cSLei YU 
210cac383bfSMarri Devender Rao /**
211cac383bfSMarri Devender Rao  * @enum Device access mode
212cac383bfSMarri Devender Rao  */
213cac383bfSMarri Devender Rao enum class AccessMode
214cac383bfSMarri Devender Rao {
215cac383bfSMarri Devender Rao     bytes, ///< Device is accessed by bytes
216cac383bfSMarri Devender Rao     words  ///< Device is accessed by words
217cac383bfSMarri Devender Rao };
218cac383bfSMarri Devender Rao 
219b755772fSjayaprakash Mutyala /** @brief implements the get SEL Info command
220b755772fSjayaprakash Mutyala  *  @returns IPMI completion code plus response data
221b755772fSjayaprakash Mutyala  *   - selVersion - SEL revision
222b755772fSjayaprakash Mutyala  *   - entries    - Number of log entries in SEL.
223b755772fSjayaprakash Mutyala  *   - freeSpace  - Free Space in bytes.
224b755772fSjayaprakash Mutyala  *   - addTimeStamp - Most recent addition timestamp
225b755772fSjayaprakash Mutyala  *   - eraseTimeStamp - Most recent erase timestamp
226b755772fSjayaprakash Mutyala  *   - operationSupport - Reserve & Delete SEL operations supported
227b755772fSjayaprakash Mutyala  */
228851acb19SJason M. Bills 
229b755772fSjayaprakash Mutyala ipmi::RspType<uint8_t,  // SEL revision.
230b755772fSjayaprakash Mutyala               uint16_t, // number of log entries in SEL.
231b755772fSjayaprakash Mutyala               uint16_t, // free Space in bytes.
232b755772fSjayaprakash Mutyala               uint32_t, // most recent addition timestamp
233b755772fSjayaprakash Mutyala               uint32_t, // most recent erase timestamp.
2346f7deaa0STom Joseph 
235b755772fSjayaprakash Mutyala               bool,     // SEL allocation info supported
236b755772fSjayaprakash Mutyala               bool,     // reserve SEL supported
237b755772fSjayaprakash Mutyala               bool,     // partial Add SEL Entry supported
238b755772fSjayaprakash Mutyala               bool,     // delete SEL supported
239b755772fSjayaprakash Mutyala               uint3_t,  // reserved
240b755772fSjayaprakash Mutyala               bool      // overflow flag
241b755772fSjayaprakash Mutyala               >
ipmiStorageGetSelInfo()242b755772fSjayaprakash Mutyala     ipmiStorageGetSelInfo()
243b755772fSjayaprakash Mutyala {
244b755772fSjayaprakash Mutyala     uint16_t entries = 0;
245b755772fSjayaprakash Mutyala     // Most recent addition timestamp.
246b755772fSjayaprakash Mutyala     uint32_t addTimeStamp = ipmi::sel::invalidTimeStamp;
2476f7deaa0STom Joseph 
248eba8e9a2SLei YU     if (!selCacheMapInitialized)
249e59abfbaSTom Joseph     {
250eba8e9a2SLei YU         // In case the initSELCache() fails, try it again
251eba8e9a2SLei YU         initSELCache();
252e59abfbaSTom Joseph     }
253eba8e9a2SLei YU     if (!selCacheMap.empty())
254e59abfbaSTom Joseph     {
255eba8e9a2SLei YU         entries = static_cast<uint16_t>(selCacheMap.size());
2566f7deaa0STom Joseph 
2576f7deaa0STom Joseph         try
2586f7deaa0STom Joseph         {
259eba8e9a2SLei YU             auto objPath = getLoggingObjPath(selCacheMap.rbegin()->first);
260b755772fSjayaprakash Mutyala             addTimeStamp = static_cast<uint32_t>(
261eba8e9a2SLei YU                 (ipmi::sel::getEntryTimeStamp(objPath).count()));
2626f7deaa0STom Joseph         }
263a2ad2da8SPatrick Williams         catch (const InternalFailure& e)
264fbc6c9d7SPatrick Williams         {}
2656f7deaa0STom Joseph         catch (const std::runtime_error& e)
2666f7deaa0STom Joseph         {
26751856e68SGeorge Liu             lg2::error("runtime error: {ERROR}", "ERROR", e);
2686f7deaa0STom Joseph         }
2696f7deaa0STom Joseph     }
2706f7deaa0STom Joseph 
271b755772fSjayaprakash Mutyala     constexpr uint8_t selVersion = ipmi::sel::selVersion;
272b755772fSjayaprakash Mutyala     constexpr uint16_t freeSpace = 0xFFFF;
273b755772fSjayaprakash Mutyala     constexpr uint32_t eraseTimeStamp = ipmi::sel::invalidTimeStamp;
274b755772fSjayaprakash Mutyala     constexpr uint3_t reserved{0};
2756f7deaa0STom Joseph 
276b755772fSjayaprakash Mutyala     return ipmi::responseSuccess(
277b755772fSjayaprakash Mutyala         selVersion, entries, freeSpace, addTimeStamp, eraseTimeStamp,
278b755772fSjayaprakash Mutyala         ipmi::sel::operationSupport::getSelAllocationInfo,
279b755772fSjayaprakash Mutyala         ipmi::sel::operationSupport::reserveSel,
280b755772fSjayaprakash Mutyala         ipmi::sel::operationSupport::partialAddSelEntry,
281b755772fSjayaprakash Mutyala         ipmi::sel::operationSupport::deleteSel, reserved,
282b755772fSjayaprakash Mutyala         ipmi::sel::operationSupport::overflow);
2836f7deaa0STom Joseph }
2846f7deaa0STom Joseph 
getSELEntry(ipmi_netfn_t,ipmi_cmd_t,ipmi_request_t request,ipmi_response_t response,ipmi_data_len_t data_len,ipmi_context_t)28511d68897SWilly Tu ipmi_ret_t getSELEntry(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request,
28611d68897SWilly Tu                        ipmi_response_t response, ipmi_data_len_t data_len,
28711d68897SWilly Tu                        ipmi_context_t)
288a495339fSTom Joseph {
289851acb19SJason M. Bills     if (*data_len != sizeof(ipmi::sel::GetSELEntryRequest))
290851acb19SJason M. Bills     {
291851acb19SJason M. Bills         *data_len = 0;
292851acb19SJason M. Bills         return IPMI_CC_REQ_DATA_LEN_INVALID;
293851acb19SJason M. Bills     }
294851acb19SJason M. Bills 
2950b02be92SPatrick Venture     auto requestData =
2960b02be92SPatrick Venture         reinterpret_cast<const ipmi::sel::GetSELEntryRequest*>(request);
297a495339fSTom Joseph 
298a495339fSTom Joseph     if (requestData->reservationID != 0)
299a495339fSTom Joseph     {
30013e67c8dSJason M. Bills         if (!checkSELReservation(requestData->reservationID))
301a495339fSTom Joseph         {
302a495339fSTom Joseph             *data_len = 0;
303a495339fSTom Joseph             return IPMI_CC_INVALID_RESERVATION_ID;
304a495339fSTom Joseph         }
305a495339fSTom Joseph     }
306a495339fSTom Joseph 
3073df3661eSLei YU     if (!selCacheMapInitialized)
3083df3661eSLei YU     {
3093df3661eSLei YU         // In case the initSELCache() fails, try it again
3103df3661eSLei YU         initSELCache();
3113df3661eSLei YU     }
3123df3661eSLei YU 
3133df3661eSLei YU     if (selCacheMap.empty())
314a495339fSTom Joseph     {
315a495339fSTom Joseph         *data_len = 0;
316a495339fSTom Joseph         return IPMI_CC_SENSOR_INVALID;
317a495339fSTom Joseph     }
318a495339fSTom Joseph 
3193df3661eSLei YU     SELCacheMap::const_iterator iter;
320a495339fSTom Joseph 
321a495339fSTom Joseph     // Check for the requested SEL Entry.
322a495339fSTom Joseph     if (requestData->selRecordID == ipmi::sel::firstEntry)
323a495339fSTom Joseph     {
3243df3661eSLei YU         iter = selCacheMap.begin();
325a495339fSTom Joseph     }
326a495339fSTom Joseph     else if (requestData->selRecordID == ipmi::sel::lastEntry)
327a495339fSTom Joseph     {
3283df3661eSLei YU         if (selCacheMap.size() > 1)
3293df3661eSLei YU         {
3303df3661eSLei YU             iter = selCacheMap.end();
3313df3661eSLei YU             --iter;
332a495339fSTom Joseph         }
333a495339fSTom Joseph         else
334a495339fSTom Joseph         {
3353df3661eSLei YU             // Only one entry exists, return the first
3363df3661eSLei YU             iter = selCacheMap.begin();
3373df3661eSLei YU         }
3383df3661eSLei YU     }
3393df3661eSLei YU     else
3403df3661eSLei YU     {
3413df3661eSLei YU         iter = selCacheMap.find(requestData->selRecordID);
3423df3661eSLei YU         if (iter == selCacheMap.end())
343a495339fSTom Joseph         {
344a495339fSTom Joseph             *data_len = 0;
345a495339fSTom Joseph             return IPMI_CC_SENSOR_INVALID;
346a495339fSTom Joseph         }
347a495339fSTom Joseph     }
348a495339fSTom Joseph 
3493df3661eSLei YU     ipmi::sel::GetSELEntryResponse record{0, iter->second};
350a495339fSTom Joseph     // Identify the next SEL record ID
351a495339fSTom Joseph     ++iter;
3523df3661eSLei YU     if (iter == selCacheMap.end())
353a495339fSTom Joseph     {
354a495339fSTom Joseph         record.nextRecordID = ipmi::sel::lastEntry;
355a495339fSTom Joseph     }
356a495339fSTom Joseph     else
357a495339fSTom Joseph     {
3583df3661eSLei YU         record.nextRecordID = iter->first;
359a495339fSTom Joseph     }
360a495339fSTom Joseph 
361a495339fSTom Joseph     if (requestData->readLength == ipmi::sel::entireRecord)
362a495339fSTom Joseph     {
363b51bf9c8SPatrick Venture         std::memcpy(response, &record, sizeof(record));
364a495339fSTom Joseph         *data_len = sizeof(record);
365a495339fSTom Joseph     }
366a495339fSTom Joseph     else
367a495339fSTom Joseph     {
368a495339fSTom Joseph         if (requestData->offset >= ipmi::sel::selRecordSize ||
369a495339fSTom Joseph             requestData->readLength > ipmi::sel::selRecordSize)
370a495339fSTom Joseph         {
371a495339fSTom Joseph             *data_len = 0;
372a495339fSTom Joseph             return IPMI_CC_INVALID_FIELD_REQUEST;
373a495339fSTom Joseph         }
374a495339fSTom Joseph 
375a495339fSTom Joseph         auto diff = ipmi::sel::selRecordSize - requestData->offset;
3761318a5edSPatrick Williams         auto readLength =
3771318a5edSPatrick Williams             std::min(diff, static_cast<int>(requestData->readLength));
378a495339fSTom Joseph 
379*3ad19716SLei YU         uint16_t nextRecordID = record.nextRecordID;
380*3ad19716SLei YU         std::memcpy(response, &nextRecordID, sizeof(nextRecordID));
381*3ad19716SLei YU 
382*3ad19716SLei YU         const ipmi::sel::SELEventRecordFormat* evt = &record.event;
383*3ad19716SLei YU         std::memcpy(static_cast<uint8_t*>(response) + sizeof(nextRecordID),
384*3ad19716SLei YU                     reinterpret_cast<const uint8_t*>(evt) + requestData->offset,
385af378fa3SLei YU                     readLength);
386*3ad19716SLei YU         *data_len = sizeof(nextRecordID) + readLength;
387a495339fSTom Joseph     }
388a495339fSTom Joseph 
389a495339fSTom Joseph     return IPMI_CC_OK;
390a495339fSTom Joseph }
391a495339fSTom Joseph 
39200a18d04SPradeep Kumar /** @brief implements the delete SEL entry command
39300a18d04SPradeep Kumar  * @request
39400a18d04SPradeep Kumar  *   - reservationID; // reservation ID.
39500a18d04SPradeep Kumar  *   - selRecordID;   // SEL record ID.
39600a18d04SPradeep Kumar  *
39700a18d04SPradeep Kumar  *  @returns ipmi completion code plus response data
39800a18d04SPradeep Kumar  *   - Record ID of the deleted record
39900a18d04SPradeep Kumar  */
40000a18d04SPradeep Kumar ipmi::RspType<uint16_t // deleted record ID
40100a18d04SPradeep Kumar               >
deleteSELEntry(uint16_t reservationID,uint16_t selRecordID)40200a18d04SPradeep Kumar     deleteSELEntry(uint16_t reservationID, uint16_t selRecordID)
4038f4a2aa8STom Joseph {
4041a4117b8SBrad Bishop     namespace fs = std::filesystem;
4058f4a2aa8STom Joseph 
40600a18d04SPradeep Kumar     if (!checkSELReservation(reservationID))
4078f4a2aa8STom Joseph     {
40800a18d04SPradeep Kumar         return ipmi::responseInvalidReservationId();
4098f4a2aa8STom Joseph     }
4108f4a2aa8STom Joseph 
41113e67c8dSJason M. Bills     // Per the IPMI spec, need to cancel the reservation when a SEL entry is
41213e67c8dSJason M. Bills     // deleted
41313e67c8dSJason M. Bills     cancelSELReservation();
41413e67c8dSJason M. Bills 
415eba8e9a2SLei YU     if (!selCacheMapInitialized)
416e59abfbaSTom Joseph     {
417eba8e9a2SLei YU         // In case the initSELCache() fails, try it again
418eba8e9a2SLei YU         initSELCache();
419e59abfbaSTom Joseph     }
4208f4a2aa8STom Joseph 
421eba8e9a2SLei YU     if (selCacheMap.empty())
4228f4a2aa8STom Joseph     {
42300a18d04SPradeep Kumar         return ipmi::responseSensorInvalid();
4248f4a2aa8STom Joseph     }
4258f4a2aa8STom Joseph 
426eba8e9a2SLei YU     SELCacheMap::const_iterator iter;
4278f4a2aa8STom Joseph     uint16_t delRecordID = 0;
4288f4a2aa8STom Joseph 
42900a18d04SPradeep Kumar     if (selRecordID == ipmi::sel::firstEntry)
4308f4a2aa8STom Joseph     {
431eba8e9a2SLei YU         delRecordID = selCacheMap.begin()->first;
4328f4a2aa8STom Joseph     }
43300a18d04SPradeep Kumar     else if (selRecordID == ipmi::sel::lastEntry)
4348f4a2aa8STom Joseph     {
435eba8e9a2SLei YU         delRecordID = selCacheMap.rbegin()->first;
4368f4a2aa8STom Joseph     }
4378f4a2aa8STom Joseph     else
4388f4a2aa8STom Joseph     {
4397d5f481aSJian         delRecordID = selRecordID;
4407d5f481aSJian     }
4417d5f481aSJian 
4427d5f481aSJian     iter = selCacheMap.find(delRecordID);
443eba8e9a2SLei YU     if (iter == selCacheMap.end())
4448f4a2aa8STom Joseph     {
44500a18d04SPradeep Kumar         return ipmi::responseSensorInvalid();
4468f4a2aa8STom Joseph     }
4478f4a2aa8STom Joseph 
4485d82f474SPatrick Williams     sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
4498f4a2aa8STom Joseph     std::string service;
4508f4a2aa8STom Joseph 
451eba8e9a2SLei YU     auto objPath = getLoggingObjPath(iter->first);
4528f4a2aa8STom Joseph     try
4538f4a2aa8STom Joseph     {
454eba8e9a2SLei YU         service = ipmi::getService(bus, ipmi::sel::logDeleteIntf, objPath);
4558f4a2aa8STom Joseph     }
4568f4a2aa8STom Joseph     catch (const std::runtime_error& e)
4578f4a2aa8STom Joseph     {
45851856e68SGeorge Liu         lg2::error("runtime error: {ERROR}", "ERROR", e);
45900a18d04SPradeep Kumar         return ipmi::responseUnspecifiedError();
4608f4a2aa8STom Joseph     }
4618f4a2aa8STom Joseph 
462eba8e9a2SLei YU     auto methodCall = bus.new_method_call(service.c_str(), objPath.c_str(),
4630b02be92SPatrick Venture                                           ipmi::sel::logDeleteIntf, "Delete");
4643e3cc35bSGeorge Liu     try
4653e3cc35bSGeorge Liu     {
4668f4a2aa8STom Joseph         auto reply = bus.call(methodCall);
4673e3cc35bSGeorge Liu     }
4683e3cc35bSGeorge Liu     catch (const std::exception& e)
4698f4a2aa8STom Joseph     {
47000a18d04SPradeep Kumar         return ipmi::responseUnspecifiedError();
4718f4a2aa8STom Joseph     }
4728f4a2aa8STom Joseph 
47300a18d04SPradeep Kumar     return ipmi::responseSuccess(delRecordID);
4748f4a2aa8STom Joseph }
4758f4a2aa8STom Joseph 
4764a5a99afSPradeep Kumar /** @brief implements the Clear SEL command
4774a5a99afSPradeep Kumar  * @request
4784a5a99afSPradeep Kumar  *   - reservationID   // Reservation ID.
4794a5a99afSPradeep Kumar  *   - clr             // char array { 'C'(0x43h), 'L'(0x4Ch), 'R'(0x52h) }
4804a5a99afSPradeep Kumar  *   - eraseOperation; // requested operation.
4814a5a99afSPradeep Kumar  *
4824a5a99afSPradeep Kumar  *  @returns ipmi completion code plus response data
4834a5a99afSPradeep Kumar  *   - erase status
4844a5a99afSPradeep Kumar  */
4854a5a99afSPradeep Kumar 
4864a5a99afSPradeep Kumar ipmi::RspType<uint8_t // erase status
4874a5a99afSPradeep Kumar               >
clearSEL(uint16_t reservationID,const std::array<char,3> & clr,uint8_t eraseOperation)4884a5a99afSPradeep Kumar     clearSEL(uint16_t reservationID, const std::array<char, 3>& clr,
4894a5a99afSPradeep Kumar              uint8_t eraseOperation)
4902f05bb56STom Joseph {
4914a5a99afSPradeep Kumar     static constexpr std::array<char, 3> clrOk = {'C', 'L', 'R'};
4924a5a99afSPradeep Kumar     if (clr != clrOk)
493851acb19SJason M. Bills     {
4944a5a99afSPradeep Kumar         return ipmi::responseInvalidFieldRequest();
495851acb19SJason M. Bills     }
496851acb19SJason M. Bills 
4974a5a99afSPradeep Kumar     if (!checkSELReservation(reservationID))
4982f05bb56STom Joseph     {
4994a5a99afSPradeep Kumar         return ipmi::responseInvalidReservationId();
5002f05bb56STom Joseph     }
5012f05bb56STom Joseph 
5022f05bb56STom Joseph     /*
5032f05bb56STom Joseph      * Erasure status cannot be fetched from DBUS, so always return erasure
5042f05bb56STom Joseph      * status as `erase completed`.
5052f05bb56STom Joseph      */
5064a5a99afSPradeep Kumar     if (eraseOperation == ipmi::sel::getEraseStatus)
5072f05bb56STom Joseph     {
5084a5a99afSPradeep Kumar         return ipmi::responseSuccess(
5094a5a99afSPradeep Kumar             static_cast<uint8_t>(ipmi::sel::eraseComplete));
5102f05bb56STom Joseph     }
5112f05bb56STom Joseph 
51273d07778SXiaoyu Ma     // Check that initiate erase is correct
51373d07778SXiaoyu Ma     if (eraseOperation != ipmi::sel::initiateErase)
51473d07778SXiaoyu Ma     {
51573d07778SXiaoyu Ma         return ipmi::responseInvalidFieldRequest();
51673d07778SXiaoyu Ma     }
51773d07778SXiaoyu Ma 
51813e67c8dSJason M. Bills     // Per the IPMI spec, need to cancel any reservation when the SEL is cleared
51913e67c8dSJason M. Bills     cancelSELReservation();
52013e67c8dSJason M. Bills 
5215d82f474SPatrick Williams     sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
52278fe1b14SLotus Xu     auto service = ipmi::getService(bus, ipmi::sel::logIntf, ipmi::sel::logObj);
5231318a5edSPatrick Williams     auto method =
5241318a5edSPatrick Williams         bus.new_method_call(service.c_str(), ipmi::sel::logObj,
5251318a5edSPatrick Williams                             ipmi::sel::logIntf, ipmi::sel::logDeleteAllMethod);
526e59abfbaSTom Joseph     try
527e59abfbaSTom Joseph     {
52878fe1b14SLotus Xu         bus.call_noreply(method);
529e59abfbaSTom Joseph     }
5305d82f474SPatrick Williams     catch (const sdbusplus::exception_t& e)
531e59abfbaSTom Joseph     {
53251856e68SGeorge Liu         lg2::error("Error eraseAll: {ERROR}", "ERROR", e);
5334a5a99afSPradeep Kumar         return ipmi::responseUnspecifiedError();
5342f05bb56STom Joseph     }
5352f05bb56STom Joseph 
5364a5a99afSPradeep Kumar     return ipmi::responseSuccess(
5374a5a99afSPradeep Kumar         static_cast<uint8_t>(ipmi::sel::eraseComplete));
5382f05bb56STom Joseph }
5392f05bb56STom Joseph 
540db2e8c45Sjayaprakash Mutyala /** @brief implements the get SEL time command
541db2e8c45Sjayaprakash Mutyala  *  @returns IPMI completion code plus response data
542db2e8c45Sjayaprakash Mutyala  *   -current time
543db2e8c45Sjayaprakash Mutyala  */
544db2e8c45Sjayaprakash Mutyala ipmi::RspType<uint32_t> // current time
ipmiStorageGetSelTime()545db2e8c45Sjayaprakash Mutyala     ipmiStorageGetSelTime()
54698a23840SMatthew Barth {
5475fba7a65SVishwanatha Subbanna     using namespace std::chrono;
5484d623e98SGeorge Liu     uint64_t bmc_time_usec = 0;
5494d623e98SGeorge Liu     std::stringstream bmcTime;
55098a23840SMatthew Barth 
551e8939395SLei YU     try
552e8939395SLei YU     {
5535d82f474SPatrick Williams         sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
5544d623e98SGeorge Liu         auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
55542f64efcSGeorge Liu         auto propValue = ipmi::getDbusProperty(
55642f64efcSGeorge Liu             bus, service, BMC_TIME_PATH, TIME_INTERFACE, PROPERTY_ELAPSED);
55742f64efcSGeorge Liu         bmc_time_usec = std::get<uint64_t>(propValue);
558e8939395SLei YU     }
559a2ad2da8SPatrick Williams     catch (const InternalFailure& e)
560e8939395SLei YU     {
56151856e68SGeorge Liu         lg2::error("Internal Failure: {ERROR}", "ERROR", e);
562db2e8c45Sjayaprakash Mutyala         return ipmi::responseUnspecifiedError();
563e8939395SLei YU     }
56430fd0a18STom Joseph     catch (const std::exception& e)
565e8939395SLei YU     {
56651856e68SGeorge Liu         lg2::error("exception message: {ERROR}", "ERROR", e);
567db2e8c45Sjayaprakash Mutyala         return ipmi::responseUnspecifiedError();
5685fba7a65SVishwanatha Subbanna     }
5695fba7a65SVishwanatha Subbanna 
57051856e68SGeorge Liu     lg2::debug("BMC time: {BMC_TIME}", "BMC_TIME",
57151856e68SGeorge Liu                duration_cast<seconds>(microseconds(bmc_time_usec)).count());
5728960b7c2SNagaraju Goruganti 
5735fba7a65SVishwanatha Subbanna     // Time is really long int but IPMI wants just uint32. This works okay until
5745fba7a65SVishwanatha Subbanna     // the number of seconds since 1970 overflows uint32 size.. Still a whole
5755fba7a65SVishwanatha Subbanna     // lot of time here to even think about that.
576db2e8c45Sjayaprakash Mutyala     return ipmi::responseSuccess(
5774d623e98SGeorge Liu         duration_cast<seconds>(microseconds(bmc_time_usec)).count());
57898a23840SMatthew Barth }
57998a23840SMatthew Barth 
580db2e8c45Sjayaprakash Mutyala /** @brief implements the set SEL time command
581db2e8c45Sjayaprakash Mutyala  *  @param selDeviceTime - epoch time
582db2e8c45Sjayaprakash Mutyala  *        -local time as the number of seconds from 00:00:00, January 1, 1970
583db2e8c45Sjayaprakash Mutyala  *  @returns IPMI completion code
584db2e8c45Sjayaprakash Mutyala  */
ipmiStorageSetSelTime(uint32_t selDeviceTime)585db2e8c45Sjayaprakash Mutyala ipmi::RspType<> ipmiStorageSetSelTime(uint32_t selDeviceTime)
58698a23840SMatthew Barth {
587e8939395SLei YU     using namespace std::chrono;
588db2e8c45Sjayaprakash Mutyala     microseconds usec{seconds(selDeviceTime)};
58998a23840SMatthew Barth 
590e8939395SLei YU     try
591e8939395SLei YU     {
5925d82f474SPatrick Williams         sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
593071d00c5Sshamim ali         bool ntp = std::get<bool>(
594071d00c5Sshamim ali             ipmi::getDbusProperty(bus, SystemdTimeService, SystemdTimePath,
595071d00c5Sshamim ali                                   SystemdTimeInterface, "NTP"));
596071d00c5Sshamim ali         if (ntp)
597071d00c5Sshamim ali         {
598071d00c5Sshamim ali             return ipmi::responseCommandNotAvailable();
599071d00c5Sshamim ali         }
600071d00c5Sshamim ali 
6014d623e98SGeorge Liu         auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
6026467ed24SAndrew Geissler         std::variant<uint64_t> value{(uint64_t)usec.count()};
603e8939395SLei YU 
6044d623e98SGeorge Liu         // Set bmc time
6054d623e98SGeorge Liu         auto method = bus.new_method_call(service.c_str(), BMC_TIME_PATH,
6060b02be92SPatrick Venture                                           DBUS_PROPERTIES, "Set");
607e8939395SLei YU 
608e8939395SLei YU         method.append(TIME_INTERFACE, PROPERTY_ELAPSED, value);
609e8939395SLei YU         auto reply = bus.call(method);
610e8939395SLei YU     }
611a2ad2da8SPatrick Williams     catch (const InternalFailure& e)
612e8939395SLei YU     {
61351856e68SGeorge Liu         lg2::error("Internal Failure: {ERROR}", "ERROR", e);
614db2e8c45Sjayaprakash Mutyala         return ipmi::responseUnspecifiedError();
6155fba7a65SVishwanatha Subbanna     }
61630fd0a18STom Joseph     catch (const std::exception& e)
617e8939395SLei YU     {
61851856e68SGeorge Liu         lg2::error("exception message: {ERROR}", "ERROR", e);
619db2e8c45Sjayaprakash Mutyala         return ipmi::responseUnspecifiedError();
62098a23840SMatthew Barth     }
6215fba7a65SVishwanatha Subbanna 
622db2e8c45Sjayaprakash Mutyala     return ipmi::responseSuccess();
62398a23840SMatthew Barth }
62498a23840SMatthew Barth 
625f020c444SXiaochao Ma /** @brief implements the get SEL timezone command
626f020c444SXiaochao Ma  *  @returns IPMI completion code plus response data
627f020c444SXiaochao Ma  *   -current timezone
628f020c444SXiaochao Ma  */
ipmiStorageGetSelTimeUtcOffset()629f020c444SXiaochao Ma ipmi::RspType<int16_t> ipmiStorageGetSelTimeUtcOffset()
630f020c444SXiaochao Ma {
631f020c444SXiaochao Ma     time_t timep;
632f020c444SXiaochao Ma     struct tm* gmTime;
633f020c444SXiaochao Ma     struct tm* localTime;
634f020c444SXiaochao Ma 
635f020c444SXiaochao Ma     time(&timep);
636f020c444SXiaochao Ma     localTime = localtime(&timep);
637f020c444SXiaochao Ma     auto validLocalTime = mktime(localTime);
638f020c444SXiaochao Ma     gmTime = gmtime(&timep);
639f020c444SXiaochao Ma     auto validGmTime = mktime(gmTime);
640f020c444SXiaochao Ma     auto timeEquation = (validLocalTime - validGmTime) / 60;
641f020c444SXiaochao Ma 
642f020c444SXiaochao Ma     return ipmi::responseSuccess(timeEquation);
643f020c444SXiaochao Ma }
644f020c444SXiaochao Ma 
645b755772fSjayaprakash Mutyala /** @brief implements the reserve SEL command
646b755772fSjayaprakash Mutyala  *  @returns IPMI completion code plus response data
647b755772fSjayaprakash Mutyala  *   - SEL reservation ID.
648b755772fSjayaprakash Mutyala  */
ipmiStorageReserveSel()649b755772fSjayaprakash Mutyala ipmi::RspType<uint16_t> ipmiStorageReserveSel()
65098a23840SMatthew Barth {
651b755772fSjayaprakash Mutyala     return ipmi::responseSuccess(reserveSel());
65298a23840SMatthew Barth }
65398a23840SMatthew Barth 
6542c7db1d3Sanil kumar appana /** @brief implements the Add SEL entry command
6552c7db1d3Sanil kumar appana  * @request
6562c7db1d3Sanil kumar appana  *
6572c7db1d3Sanil kumar appana  *   - recordID      ID used for SEL Record access
6582c7db1d3Sanil kumar appana  *   - recordType    Record Type
6592c7db1d3Sanil kumar appana  *   - timeStamp     Time when event was logged. LS byte first
6602c7db1d3Sanil kumar appana  *   - generatorID   software ID if event was generated from
6612c7db1d3Sanil kumar appana  *                   system software
6622c7db1d3Sanil kumar appana  *   - evmRev        event message format version
6632c7db1d3Sanil kumar appana  *   - sensorType    sensor type code for service that generated
6642c7db1d3Sanil kumar appana  *                   the event
6652c7db1d3Sanil kumar appana  *   - sensorNumber  number of sensors that generated the event
6662c7db1d3Sanil kumar appana  *   - eventDir     event dir
6672c7db1d3Sanil kumar appana  *   - eventData    event data field contents
6682c7db1d3Sanil kumar appana  *
6692c7db1d3Sanil kumar appana  *  @returns ipmi completion code plus response data
6702c7db1d3Sanil kumar appana  *   - RecordID of the Added SEL entry
6712c7db1d3Sanil kumar appana  */
6722c7db1d3Sanil kumar appana ipmi::RspType<uint16_t // recordID of the Added SEL entry
6732c7db1d3Sanil kumar appana               >
ipmiStorageAddSEL(uint16_t recordID,uint8_t recordType,uint32_t timeStamp,uint16_t generatorID,uint8_t evmRev,uint8_t sensorType,uint8_t sensorNumber,uint8_t eventDir,std::array<uint8_t,eventDataSize> eventData)67411d68897SWilly Tu     ipmiStorageAddSEL(uint16_t recordID, uint8_t recordType,
67511d68897SWilly Tu                       [[maybe_unused]] uint32_t timeStamp, uint16_t generatorID,
67632aece1aSPatrick Williams                       [[maybe_unused]] uint8_t evmRev,
67732aece1aSPatrick Williams                       [[maybe_unused]] uint8_t sensorType, uint8_t sensorNumber,
67832aece1aSPatrick Williams                       uint8_t eventDir,
6792c7db1d3Sanil kumar appana                       std::array<uint8_t, eventDataSize> eventData)
68098a23840SMatthew Barth {
68157d35579SLotus Xu     std::string objpath;
68257d35579SLotus Xu     static constexpr auto systemRecordType = 0x02;
68332aece1aSPatrick Williams #ifdef OPEN_POWER_SUPPORT
684b647d5beSTom Joseph     // Hostboot sends SEL with OEM record type 0xDE to indicate that there is
685b647d5beSTom Joseph     // a maintenance procedure associated with eSEL record.
686b647d5beSTom Joseph     static constexpr auto procedureType = 0xDE;
68732aece1aSPatrick Williams #endif
68857d35579SLotus Xu     cancelSELReservation();
68957d35579SLotus Xu     if (recordType == systemRecordType)
69057d35579SLotus Xu     {
69157d35579SLotus Xu         for (const auto& it : invSensors)
69257d35579SLotus Xu         {
69357d35579SLotus Xu             if (it.second.sensorID == sensorNumber)
69457d35579SLotus Xu             {
69557d35579SLotus Xu                 objpath = it.first;
69657d35579SLotus Xu                 break;
69757d35579SLotus Xu             }
69857d35579SLotus Xu         }
69957d35579SLotus Xu         auto selDataStr = ipmi::sel::toHexStr(eventData);
70057d35579SLotus Xu 
70157d35579SLotus Xu         bool assert = (eventDir & 0x80) ? false : true;
70257d35579SLotus Xu 
7031318a5edSPatrick Williams         recordID = report<SELCreated>(
7041318a5edSPatrick Williams             Created::RECORD_TYPE(recordType),
70557d35579SLotus Xu             Created::GENERATOR_ID(generatorID),
70657d35579SLotus Xu             Created::SENSOR_DATA(selDataStr.c_str()),
7071318a5edSPatrick Williams             Created::EVENT_DIR(assert), Created::SENSOR_PATH(objpath.c_str()));
70857d35579SLotus Xu     }
709768730d2SPatrick Williams #ifdef OPEN_POWER_SUPPORT
71057d35579SLotus Xu     else if (recordType == procedureType)
711b647d5beSTom Joseph     {
712b647d5beSTom Joseph         // In the OEM record type 0xDE, byte 11 in the SEL record indicate the
713b647d5beSTom Joseph         // procedure number.
7142c7db1d3Sanil kumar appana         createProcedureLogEntry(sensorType);
715b647d5beSTom Joseph     }
716768730d2SPatrick Williams #endif
71798a23840SMatthew Barth 
7182c7db1d3Sanil kumar appana     return ipmi::responseSuccess(recordID);
71998a23840SMatthew Barth }
72098a23840SMatthew Barth 
isFruPresent(ipmi::Context::ptr & ctx,const std::string & fruPath)72169e3cd40SKonstantin Aladyshev bool isFruPresent(ipmi::Context::ptr& ctx, const std::string& fruPath)
722fa6e2092SKirill Pakhomov {
723fa6e2092SKirill Pakhomov     using namespace ipmi::fru;
724fa6e2092SKirill Pakhomov 
72569e3cd40SKonstantin Aladyshev     std::string service;
7261318a5edSPatrick Williams     boost::system::error_code ec =
7271318a5edSPatrick Williams         getService(ctx, invItemInterface, invObjPath + fruPath, service);
72869e3cd40SKonstantin Aladyshev     if (!ec)
72969e3cd40SKonstantin Aladyshev     {
73069e3cd40SKonstantin Aladyshev         bool result;
73169e3cd40SKonstantin Aladyshev         ec = ipmi::getDbusProperty(ctx, service, invObjPath + fruPath,
73269e3cd40SKonstantin Aladyshev                                    invItemInterface, itemPresentProp, result);
73369e3cd40SKonstantin Aladyshev         if (!ec)
73469e3cd40SKonstantin Aladyshev         {
73569e3cd40SKonstantin Aladyshev             return result;
73669e3cd40SKonstantin Aladyshev         }
73769e3cd40SKonstantin Aladyshev     }
738c7e4b04eSKonstantin Aladyshev 
739c7e4b04eSKonstantin Aladyshev     ipmi::ObjectValueTree managedObjects;
740947da1b0SNan Zhou     ec = getManagedObjects(ctx, "xyz.openbmc_project.EntityManager",
741947da1b0SNan Zhou                            "/xyz/openbmc_project/inventory", managedObjects);
742c7e4b04eSKonstantin Aladyshev     if (!ec)
743c7e4b04eSKonstantin Aladyshev     {
744c7e4b04eSKonstantin Aladyshev         auto connection = managedObjects.find(fruPath);
745c7e4b04eSKonstantin Aladyshev         if (connection != managedObjects.end())
746c7e4b04eSKonstantin Aladyshev         {
747c7e4b04eSKonstantin Aladyshev             return true;
748c7e4b04eSKonstantin Aladyshev         }
749c7e4b04eSKonstantin Aladyshev     }
750c7e4b04eSKonstantin Aladyshev 
75169e3cd40SKonstantin Aladyshev     return false;
752fa6e2092SKirill Pakhomov }
753fa6e2092SKirill Pakhomov 
754b0c794d8SPradeep Kumar /** @brief implements the get FRU Inventory Area Info command
755b0c794d8SPradeep Kumar  *
756b0c794d8SPradeep Kumar  *  @returns IPMI completion code plus response data
757b0c794d8SPradeep Kumar  *   - FRU Inventory area size in bytes,
758b0c794d8SPradeep Kumar  *   - access bit
759b0c794d8SPradeep Kumar  **/
760b0c794d8SPradeep Kumar ipmi::RspType<uint16_t, // FRU Inventory area size in bytes,
761b0c794d8SPradeep Kumar               uint8_t   // access size (bytes / words)
762b0c794d8SPradeep Kumar               >
ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx,uint8_t fruID)76369e3cd40SKonstantin Aladyshev     ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruID)
764fa7b4e2eSMarri Devender Rao {
765b0c794d8SPradeep Kumar     auto iter = frus.find(fruID);
766187f564aSTom Joseph     if (iter == frus.end())
767187f564aSTom Joseph     {
768b0c794d8SPradeep Kumar         return ipmi::responseSensorInvalid();
769187f564aSTom Joseph     }
770187f564aSTom Joseph 
771fa6e2092SKirill Pakhomov     auto path = iter->second[0].path;
77269e3cd40SKonstantin Aladyshev     if (!isFruPresent(ctx, path))
773fa6e2092SKirill Pakhomov     {
774fa6e2092SKirill Pakhomov         return ipmi::responseSensorInvalid();
775fa6e2092SKirill Pakhomov     }
776fa6e2092SKirill Pakhomov 
777cac383bfSMarri Devender Rao     try
778cac383bfSMarri Devender Rao     {
779b0c794d8SPradeep Kumar         return ipmi::responseSuccess(
780b0c794d8SPradeep Kumar             static_cast<uint16_t>(getFruAreaData(fruID).size()),
781b0c794d8SPradeep Kumar             static_cast<uint8_t>(AccessMode::bytes));
782cac383bfSMarri Devender Rao     }
783cac383bfSMarri Devender Rao     catch (const InternalFailure& e)
784cac383bfSMarri Devender Rao     {
78551856e68SGeorge Liu         lg2::error("Internal Failure: {ERROR}", "ERROR", e);
786b0c794d8SPradeep Kumar         return ipmi::responseUnspecifiedError();
787cac383bfSMarri Devender Rao     }
788fa7b4e2eSMarri Devender Rao }
789fa7b4e2eSMarri Devender Rao 
7905b7c3266Sanil kumar appana /**@brief implements the Read FRU Data command
7915b7c3266Sanil kumar appana  * @param fruDeviceId - FRU device ID. FFh = reserved
7925b7c3266Sanil kumar appana  * @param offset      - FRU inventory offset to read
7935b7c3266Sanil kumar appana  * @param readCount   - count to read
7945b7c3266Sanil kumar appana  *
7955b7c3266Sanil kumar appana  * @return IPMI completion code plus response data
7965b7c3266Sanil kumar appana  * - returnCount - response data count.
7975b7c3266Sanil kumar appana  * - data        -  response data
7985b7c3266Sanil kumar appana  */
7995b7c3266Sanil kumar appana ipmi::RspType<uint8_t,              // count returned
8005b7c3266Sanil kumar appana               std::vector<uint8_t>> // FRU data
ipmiStorageReadFruData(uint8_t fruDeviceId,uint16_t offset,uint8_t readCount)8015b7c3266Sanil kumar appana     ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t offset,
8025b7c3266Sanil kumar appana                            uint8_t readCount)
803fa7b4e2eSMarri Devender Rao {
8045b7c3266Sanil kumar appana     if (fruDeviceId == 0xFF)
805187f564aSTom Joseph     {
8065b7c3266Sanil kumar appana         return ipmi::responseInvalidFieldRequest();
807187f564aSTom Joseph     }
808187f564aSTom Joseph 
8095b7c3266Sanil kumar appana     auto iter = frus.find(fruDeviceId);
8105b7c3266Sanil kumar appana     if (iter == frus.end())
8115b7c3266Sanil kumar appana     {
8125b7c3266Sanil kumar appana         return ipmi::responseSensorInvalid();
8135b7c3266Sanil kumar appana     }
8145b7c3266Sanil kumar appana 
815cac383bfSMarri Devender Rao     try
816cac383bfSMarri Devender Rao     {
8175b7c3266Sanil kumar appana         const auto& fruArea = getFruAreaData(fruDeviceId);
818cac383bfSMarri Devender Rao         auto size = fruArea.size();
8197f2d7c90SNagaraju Goruganti 
820efcd68b2STom Joseph         if (offset >= size)
821efcd68b2STom Joseph         {
8225b7c3266Sanil kumar appana             return ipmi::responseParmOutOfRange();
823efcd68b2STom Joseph         }
824efcd68b2STom Joseph 
8257f2d7c90SNagaraju Goruganti         // Write the count of response data.
8265b7c3266Sanil kumar appana         uint8_t returnCount;
8275b7c3266Sanil kumar appana         if ((offset + readCount) <= size)
828cac383bfSMarri Devender Rao         {
8295b7c3266Sanil kumar appana             returnCount = readCount;
8307f2d7c90SNagaraju Goruganti         }
8317f2d7c90SNagaraju Goruganti         else
8327f2d7c90SNagaraju Goruganti         {
8335b7c3266Sanil kumar appana             returnCount = size - offset;
834cac383bfSMarri Devender Rao         }
8352848d604SRatan Gupta 
8365b7c3266Sanil kumar appana         std::vector<uint8_t> fruData((fruArea.begin() + offset),
8375b7c3266Sanil kumar appana                                      (fruArea.begin() + offset + returnCount));
8382848d604SRatan Gupta 
8395b7c3266Sanil kumar appana         return ipmi::responseSuccess(returnCount, fruData);
840cac383bfSMarri Devender Rao     }
841cac383bfSMarri Devender Rao     catch (const InternalFailure& e)
842cac383bfSMarri Devender Rao     {
84351856e68SGeorge Liu         lg2::error("Internal Failure: {ERROR}", "ERROR", e);
8445b7c3266Sanil kumar appana         return ipmi::responseUnspecifiedError();
845cac383bfSMarri Devender Rao     }
846fa7b4e2eSMarri Devender Rao }
847fa7b4e2eSMarri Devender Rao 
848b60e840aSPradeep Kumar ipmi::RspType<uint8_t,  // SDR version
849b60e840aSPradeep Kumar               uint16_t, // record count LS first
850b60e840aSPradeep Kumar               uint16_t, // free space in bytes, LS first
851b60e840aSPradeep Kumar               uint32_t, // addition timestamp LS first
852b60e840aSPradeep Kumar               uint32_t, // deletion timestamp LS first
853b60e840aSPradeep Kumar               uint8_t>  // operation Support
ipmiGetRepositoryInfo()854b60e840aSPradeep Kumar     ipmiGetRepositoryInfo()
855e66c3b04SDhruvaraj Subhashchandran {
856b60e840aSPradeep Kumar     constexpr uint8_t sdrVersion = 0x51;
857b60e840aSPradeep Kumar     constexpr uint16_t freeSpace = 0xFFFF;
858b60e840aSPradeep Kumar     constexpr uint32_t additionTimestamp = 0x0;
859b60e840aSPradeep Kumar     constexpr uint32_t deletionTimestamp = 0x0;
860b60e840aSPradeep Kumar     constexpr uint8_t operationSupport = 0;
861e66c3b04SDhruvaraj Subhashchandran 
8620ca3bfedSWilly Tu     // Get SDR count. This returns the total number of SDRs in the device.
8630ca3bfedSWilly Tu     const auto& entityRecords =
8640ca3bfedSWilly Tu         ipmi::sensor::EntityInfoMapContainer::getContainer()
8650ca3bfedSWilly Tu             ->getIpmiEntityRecords();
8661318a5edSPatrick Williams     uint16_t records =
8671318a5edSPatrick Williams         ipmi::sensor::sensors.size() + frus.size() + entityRecords.size();
868e66c3b04SDhruvaraj Subhashchandran 
869b60e840aSPradeep Kumar     return ipmi::responseSuccess(sdrVersion, records, freeSpace,
870b60e840aSPradeep Kumar                                  additionTimestamp, deletionTimestamp,
871b60e840aSPradeep Kumar                                  operationSupport);
872e66c3b04SDhruvaraj Subhashchandran }
87398a23840SMatthew Barth 
registerNetFnStorageFunctions()8745087b075SGeorge Liu void registerNetFnStorageFunctions()
87598a23840SMatthew Barth {
8763df3661eSLei YU     selCacheMapInitialized = false;
877d9e5766cSLei YU     initSELCache();
878d351a729SWilly Tu     // Handlers with dbus-sdr handler implementation.
879d351a729SWilly Tu     // Do not register the hander if it dynamic sensors stack is used.
880d351a729SWilly Tu 
881d351a729SWilly Tu #ifndef FEATURE_DYNAMIC_SENSORS
882292c917eSThang Tran 
883292c917eSThang Tran #ifndef FEATURE_DYNAMIC_STORAGES_ONLY
8846f7deaa0STom Joseph     // <Get SEL Info>
885b755772fSjayaprakash Mutyala     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
886b755772fSjayaprakash Mutyala                           ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
887b755772fSjayaprakash Mutyala                           ipmiStorageGetSelInfo);
8886f7deaa0STom Joseph 
889f020c444SXiaochao Ma     // <Get SEL Timezone>
890f020c444SXiaochao Ma     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
891f020c444SXiaochao Ma                           ipmi::storage::cmdGetSelTimeUtcOffset,
892f020c444SXiaochao Ma                           ipmi::Privilege::User,
893f020c444SXiaochao Ma                           ipmiStorageGetSelTimeUtcOffset);
894f020c444SXiaochao Ma 
895a495339fSTom Joseph     // <Get SEL Entry>
896a6fb32d8SJayanth Othayoth     ipmi_register_callback(NETFUN_STORAGE, ipmi::storage::cmdGetSelEntry,
897a6fb32d8SJayanth Othayoth                            nullptr, getSELEntry, PRIVILEGE_USER);
898a495339fSTom Joseph 
8998f4a2aa8STom Joseph     // <Delete SEL Entry>
90000a18d04SPradeep Kumar     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
90100a18d04SPradeep Kumar                           ipmi::storage::cmdDeleteSelEntry,
90200a18d04SPradeep Kumar                           ipmi::Privilege::Operator, deleteSELEntry);
9038f4a2aa8STom Joseph 
9040573237fSTom     // <Add SEL Entry>
9052c7db1d3Sanil kumar appana     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
9062c7db1d3Sanil kumar appana                           ipmi::storage::cmdAddSelEntry,
9072c7db1d3Sanil kumar appana                           ipmi::Privilege::Operator, ipmiStorageAddSEL);
9082c7db1d3Sanil kumar appana 
9092f05bb56STom Joseph     // <Clear SEL>
9104a5a99afSPradeep Kumar     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
9114a5a99afSPradeep Kumar                           ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
9124a5a99afSPradeep Kumar                           clearSEL);
9134a5a99afSPradeep Kumar 
914fa7b4e2eSMarri Devender Rao     // <Get FRU Inventory Area Info>
915b0c794d8SPradeep Kumar     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
916b0c794d8SPradeep Kumar                           ipmi::storage::cmdGetFruInventoryAreaInfo,
917b0c794d8SPradeep Kumar                           ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
918fa7b4e2eSMarri Devender Rao 
919b5248c95SJason M. Bills     // <READ FRU Data>
9205b7c3266Sanil kumar appana     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
9215b7c3266Sanil kumar appana                           ipmi::storage::cmdReadFruData,
9225b7c3266Sanil kumar appana                           ipmi::Privilege::Operator, ipmiStorageReadFruData);
923cac383bfSMarri Devender Rao 
924292c917eSThang Tran #endif // FEATURE_DYNAMIC_STORAGES_ONLY
925292c917eSThang Tran 
926e66c3b04SDhruvaraj Subhashchandran     // <Get Repository Info>
927b60e840aSPradeep Kumar     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
928b60e840aSPradeep Kumar                           ipmi::storage::cmdGetSdrRepositoryInfo,
929b60e840aSPradeep Kumar                           ipmi::Privilege::User, ipmiGetRepositoryInfo);
930e66c3b04SDhruvaraj Subhashchandran 
9315ca50959STom Joseph     // <Reserve SDR Repository>
932d957823eSjayaprakash Mutyala     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
933d957823eSjayaprakash Mutyala                           ipmi::storage::cmdReserveSdrRepository,
934d957823eSjayaprakash Mutyala                           ipmi::Privilege::User, ipmiSensorReserveSdr);
9355ca50959STom Joseph 
9365ca50959STom Joseph     // <Get SDR>
937e6cd4dfdSGeorge Liu     ipmi_register_callback(NETFUN_STORAGE, ipmi::storage::cmdGetSdr, nullptr,
9380b02be92SPatrick Venture                            ipmi_sen_get_sdr, PRIVILEGE_USER);
9395ca50959STom Joseph 
940d351a729SWilly Tu #endif
941d351a729SWilly Tu 
942d351a729SWilly Tu     // Common Handers used by both implementation.
943d351a729SWilly Tu 
944d351a729SWilly Tu     // <Reserve SEL>
945d351a729SWilly Tu     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
946d351a729SWilly Tu                           ipmi::storage::cmdReserveSel, ipmi::Privilege::User,
947d351a729SWilly Tu                           ipmiStorageReserveSel);
948d351a729SWilly Tu 
949dbf66e1bSThang Tran     // <Get SEL Time>
950dbf66e1bSThang Tran     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
951dbf66e1bSThang Tran                           ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
952dbf66e1bSThang Tran                           ipmiStorageGetSelTime);
953dbf66e1bSThang Tran 
954dbf66e1bSThang Tran     // <Set SEL Time>
955dbf66e1bSThang Tran     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
956dbf66e1bSThang Tran                           ipmi::storage::cmdSetSelTime,
957dbf66e1bSThang Tran                           ipmi::Privilege::Operator, ipmiStorageSetSelTime);
958dbf66e1bSThang Tran 
959908f7507SMarri Devender Rao     ipmi::fru::registerCallbackHandler();
96098a23840SMatthew Barth     return;
96198a23840SMatthew Barth }
962