1 #include "storagehandler.hpp"
2 
3 #include "fruread.hpp"
4 #include "read_fru_data.hpp"
5 #include "selutility.hpp"
6 #include "sensorhandler.hpp"
7 #include "storageaddsel.hpp"
8 
9 #include <arpa/inet.h>
10 #include <mapper.h>
11 #include <systemd/sd-bus.h>
12 
13 #include <ipmid/api.hpp>
14 #include <ipmid/entity_map_json.hpp>
15 #include <ipmid/utils.hpp>
16 #include <phosphor-logging/elog-errors.hpp>
17 #include <phosphor-logging/elog.hpp>
18 #include <phosphor-logging/log.hpp>
19 #include <sdbusplus/server.hpp>
20 #include <xyz/openbmc_project/Common/error.hpp>
21 #include <xyz/openbmc_project/Logging/SEL/error.hpp>
22 
23 #include <algorithm>
24 #include <chrono>
25 #include <cstdio>
26 #include <cstring>
27 #include <filesystem>
28 #include <optional>
29 #include <string>
30 #include <variant>
31 
32 void register_netfn_storage_functions() __attribute__((constructor));
33 
34 unsigned int g_sel_time = 0xFFFFFFFF;
35 namespace ipmi
36 {
37 namespace sensor
38 {
39 extern const IdInfoMap sensors;
40 } // namespace sensor
41 } // namespace ipmi
42 extern const ipmi::sensor::InvObjectIDMap invSensors;
43 extern const FruMap frus;
44 constexpr uint8_t eventDataSize = 3;
45 namespace
46 {
47 constexpr auto SystemdTimeService = "org.freedesktop.timedate1";
48 constexpr auto SystemdTimePath = "/org/freedesktop/timedate1";
49 constexpr auto SystemdTimeInterface = "org.freedesktop.timedate1";
50 
51 constexpr auto TIME_INTERFACE = "xyz.openbmc_project.Time.EpochTime";
52 constexpr auto BMC_TIME_PATH = "/xyz/openbmc_project/time/bmc";
53 constexpr auto DBUS_PROPERTIES = "org.freedesktop.DBus.Properties";
54 constexpr auto PROPERTY_ELAPSED = "Elapsed";
55 
56 constexpr auto logWatchPath = "/xyz/openbmc_project/logging";
57 constexpr auto logBasePath = "/xyz/openbmc_project/logging/entry";
58 constexpr auto logEntryIntf = "xyz.openbmc_project.Logging.Entry";
59 constexpr auto logDeleteIntf = "xyz.openbmc_project.Object.Delete";
60 } // namespace
61 
62 using InternalFailure =
63     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
64 using namespace phosphor::logging;
65 using namespace ipmi::fru;
66 using namespace xyz::openbmc_project::Logging::SEL;
67 using SELCreated =
68     sdbusplus::xyz::openbmc_project::Logging::SEL::Error::Created;
69 
70 using SELRecordID = uint16_t;
71 using SELEntry = ipmi::sel::SELEventRecordFormat;
72 using SELCacheMap = std::map<SELRecordID, SELEntry>;
73 
74 SELCacheMap selCacheMap __attribute__((init_priority(101)));
75 bool selCacheMapInitialized;
76 std::unique_ptr<sdbusplus::bus::match_t> selAddedMatch
77     __attribute__((init_priority(101)));
78 std::unique_ptr<sdbusplus::bus::match_t> selRemovedMatch
79     __attribute__((init_priority(101)));
80 std::unique_ptr<sdbusplus::bus::match_t> selUpdatedMatch
81     __attribute__((init_priority(101)));
82 
83 static inline uint16_t getLoggingId(const std::string& p)
84 {
85     namespace fs = std::filesystem;
86     fs::path entryPath(p);
87     return std::stoul(entryPath.filename().string());
88 }
89 
90 static inline std::string getLoggingObjPath(uint16_t id)
91 {
92     return std::string(ipmi::sel::logBasePath) + "/" + std::to_string(id);
93 }
94 
95 std::optional<std::pair<uint16_t, SELEntry>>
96     parseLoggingEntry(const std::string& p)
97 {
98     try
99     {
100         auto id = getLoggingId(p);
101         ipmi::sel::GetSELEntryResponse record{};
102         record = ipmi::sel::convertLogEntrytoSEL(p);
103         return std::pair<uint16_t, SELEntry>({id, std::move(record.event)});
104     }
105     catch (const std::exception& e)
106     {
107         fprintf(stderr, "Failed to convert %s to SEL: %s\n", p.c_str(),
108                 e.what());
109     }
110     return std::nullopt;
111 }
112 
113 static void selAddedCallback(sdbusplus::message_t& m)
114 {
115     sdbusplus::message::object_path objPath;
116     try
117     {
118         m.read(objPath);
119     }
120     catch (const sdbusplus::exception_t& e)
121     {
122         log<level::ERR>("Failed to read object path");
123         return;
124     }
125     std::string p = objPath;
126     auto entry = parseLoggingEntry(p);
127     if (entry)
128     {
129         selCacheMap.insert(std::move(*entry));
130     }
131 }
132 
133 static void selRemovedCallback(sdbusplus::message_t& m)
134 {
135     sdbusplus::message::object_path objPath;
136     try
137     {
138         m.read(objPath);
139     }
140     catch (const sdbusplus::exception_t& e)
141     {
142         log<level::ERR>("Failed to read object path");
143     }
144     try
145     {
146         std::string p = objPath;
147         selCacheMap.erase(getLoggingId(p));
148     }
149     catch (const std::invalid_argument& e)
150     {
151         log<level::ERR>("Invalid logging entry ID");
152     }
153 }
154 
155 static void selUpdatedCallback(sdbusplus::message_t& m)
156 {
157     std::string p = m.get_path();
158     auto entry = parseLoggingEntry(p);
159     if (entry)
160     {
161         selCacheMap.insert_or_assign(entry->first, std::move(entry->second));
162     }
163 }
164 
165 void registerSelCallbackHandler()
166 {
167     using namespace sdbusplus::bus::match::rules;
168     sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
169     if (!selAddedMatch)
170     {
171         selAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
172             bus, interfacesAdded(logWatchPath),
173             std::bind(selAddedCallback, std::placeholders::_1));
174     }
175     if (!selRemovedMatch)
176     {
177         selRemovedMatch = std::make_unique<sdbusplus::bus::match_t>(
178             bus, interfacesRemoved(logWatchPath),
179             std::bind(selRemovedCallback, std::placeholders::_1));
180     }
181     if (!selUpdatedMatch)
182     {
183         selUpdatedMatch = std::make_unique<sdbusplus::bus::match_t>(
184             bus,
185             type::signal() + member("PropertiesChanged"s) +
186                 interface("org.freedesktop.DBus.Properties"s) +
187                 argN(0, logEntryIntf),
188             std::bind(selUpdatedCallback, std::placeholders::_1));
189     }
190 }
191 
192 void initSELCache()
193 {
194     registerSelCallbackHandler();
195     ipmi::sel::ObjectPaths paths;
196     try
197     {
198         ipmi::sel::readLoggingObjectPaths(paths);
199     }
200     catch (const sdbusplus::exception_t& e)
201     {
202         log<level::ERR>("Failed to get logging object paths");
203         return;
204     }
205     for (const auto& p : paths)
206     {
207         auto entry = parseLoggingEntry(p);
208         if (entry)
209         {
210             selCacheMap.insert(std::move(*entry));
211         }
212     }
213     selCacheMapInitialized = true;
214 }
215 
216 /**
217  * @enum Device access mode
218  */
219 enum class AccessMode
220 {
221     bytes, ///< Device is accessed by bytes
222     words  ///< Device is accessed by words
223 };
224 
225 /** @brief implements the get SEL Info command
226  *  @returns IPMI completion code plus response data
227  *   - selVersion - SEL revision
228  *   - entries    - Number of log entries in SEL.
229  *   - freeSpace  - Free Space in bytes.
230  *   - addTimeStamp - Most recent addition timestamp
231  *   - eraseTimeStamp - Most recent erase timestamp
232  *   - operationSupport - Reserve & Delete SEL operations supported
233  */
234 
235 ipmi::RspType<uint8_t,  // SEL revision.
236               uint16_t, // number of log entries in SEL.
237               uint16_t, // free Space in bytes.
238               uint32_t, // most recent addition timestamp
239               uint32_t, // most recent erase timestamp.
240 
241               bool,     // SEL allocation info supported
242               bool,     // reserve SEL supported
243               bool,     // partial Add SEL Entry supported
244               bool,     // delete SEL supported
245               uint3_t,  // reserved
246               bool      // overflow flag
247               >
248     ipmiStorageGetSelInfo()
249 {
250     uint16_t entries = 0;
251     // Most recent addition timestamp.
252     uint32_t addTimeStamp = ipmi::sel::invalidTimeStamp;
253 
254     if (!selCacheMapInitialized)
255     {
256         // In case the initSELCache() fails, try it again
257         initSELCache();
258     }
259     if (!selCacheMap.empty())
260     {
261         entries = static_cast<uint16_t>(selCacheMap.size());
262 
263         try
264         {
265             auto objPath = getLoggingObjPath(selCacheMap.rbegin()->first);
266             addTimeStamp = static_cast<uint32_t>(
267                 (ipmi::sel::getEntryTimeStamp(objPath).count()));
268         }
269         catch (const InternalFailure& e)
270         {}
271         catch (const std::runtime_error& e)
272         {
273             log<level::ERR>(e.what());
274         }
275     }
276 
277     constexpr uint8_t selVersion = ipmi::sel::selVersion;
278     constexpr uint16_t freeSpace = 0xFFFF;
279     constexpr uint32_t eraseTimeStamp = ipmi::sel::invalidTimeStamp;
280     constexpr uint3_t reserved{0};
281 
282     return ipmi::responseSuccess(
283         selVersion, entries, freeSpace, addTimeStamp, eraseTimeStamp,
284         ipmi::sel::operationSupport::getSelAllocationInfo,
285         ipmi::sel::operationSupport::reserveSel,
286         ipmi::sel::operationSupport::partialAddSelEntry,
287         ipmi::sel::operationSupport::deleteSel, reserved,
288         ipmi::sel::operationSupport::overflow);
289 }
290 
291 ipmi_ret_t getSELEntry(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request,
292                        ipmi_response_t response, ipmi_data_len_t data_len,
293                        ipmi_context_t)
294 {
295     if (*data_len != sizeof(ipmi::sel::GetSELEntryRequest))
296     {
297         *data_len = 0;
298         return IPMI_CC_REQ_DATA_LEN_INVALID;
299     }
300 
301     auto requestData =
302         reinterpret_cast<const ipmi::sel::GetSELEntryRequest*>(request);
303 
304     if (requestData->reservationID != 0)
305     {
306         if (!checkSELReservation(requestData->reservationID))
307         {
308             *data_len = 0;
309             return IPMI_CC_INVALID_RESERVATION_ID;
310         }
311     }
312 
313     if (!selCacheMapInitialized)
314     {
315         // In case the initSELCache() fails, try it again
316         initSELCache();
317     }
318 
319     if (selCacheMap.empty())
320     {
321         *data_len = 0;
322         return IPMI_CC_SENSOR_INVALID;
323     }
324 
325     SELCacheMap::const_iterator iter;
326 
327     // Check for the requested SEL Entry.
328     if (requestData->selRecordID == ipmi::sel::firstEntry)
329     {
330         iter = selCacheMap.begin();
331     }
332     else if (requestData->selRecordID == ipmi::sel::lastEntry)
333     {
334         if (selCacheMap.size() > 1)
335         {
336             iter = selCacheMap.end();
337             --iter;
338         }
339         else
340         {
341             // Only one entry exists, return the first
342             iter = selCacheMap.begin();
343         }
344     }
345     else
346     {
347         iter = selCacheMap.find(requestData->selRecordID);
348         if (iter == selCacheMap.end())
349         {
350             *data_len = 0;
351             return IPMI_CC_SENSOR_INVALID;
352         }
353     }
354 
355     ipmi::sel::GetSELEntryResponse record{0, iter->second};
356     // Identify the next SEL record ID
357     ++iter;
358     if (iter == selCacheMap.end())
359     {
360         record.nextRecordID = ipmi::sel::lastEntry;
361     }
362     else
363     {
364         record.nextRecordID = iter->first;
365     }
366 
367     if (requestData->readLength == ipmi::sel::entireRecord)
368     {
369         std::memcpy(response, &record, sizeof(record));
370         *data_len = sizeof(record);
371     }
372     else
373     {
374         if (requestData->offset >= ipmi::sel::selRecordSize ||
375             requestData->readLength > ipmi::sel::selRecordSize)
376         {
377             *data_len = 0;
378             return IPMI_CC_INVALID_FIELD_REQUEST;
379         }
380 
381         auto diff = ipmi::sel::selRecordSize - requestData->offset;
382         auto readLength = std::min(diff,
383                                    static_cast<int>(requestData->readLength));
384 
385         std::memcpy(response, &record.nextRecordID,
386                     sizeof(record.nextRecordID));
387         std::memcpy(static_cast<uint8_t*>(response) +
388                         sizeof(record.nextRecordID),
389                     &record.event.eventRecord.recordID + requestData->offset,
390                     readLength);
391         *data_len = sizeof(record.nextRecordID) + readLength;
392     }
393 
394     return IPMI_CC_OK;
395 }
396 
397 /** @brief implements the delete SEL entry command
398  * @request
399  *   - reservationID; // reservation ID.
400  *   - selRecordID;   // SEL record ID.
401  *
402  *  @returns ipmi completion code plus response data
403  *   - Record ID of the deleted record
404  */
405 ipmi::RspType<uint16_t // deleted record ID
406               >
407     deleteSELEntry(uint16_t reservationID, uint16_t selRecordID)
408 {
409     namespace fs = std::filesystem;
410 
411     if (!checkSELReservation(reservationID))
412     {
413         return ipmi::responseInvalidReservationId();
414     }
415 
416     // Per the IPMI spec, need to cancel the reservation when a SEL entry is
417     // deleted
418     cancelSELReservation();
419 
420     if (!selCacheMapInitialized)
421     {
422         // In case the initSELCache() fails, try it again
423         initSELCache();
424     }
425 
426     if (selCacheMap.empty())
427     {
428         return ipmi::responseSensorInvalid();
429     }
430 
431     SELCacheMap::const_iterator iter;
432     uint16_t delRecordID = 0;
433 
434     if (selRecordID == ipmi::sel::firstEntry)
435     {
436         delRecordID = selCacheMap.begin()->first;
437     }
438     else if (selRecordID == ipmi::sel::lastEntry)
439     {
440         delRecordID = selCacheMap.rbegin()->first;
441     }
442     else
443     {
444         delRecordID = selRecordID;
445     }
446 
447     iter = selCacheMap.find(delRecordID);
448     if (iter == selCacheMap.end())
449     {
450         return ipmi::responseSensorInvalid();
451     }
452 
453     sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
454     std::string service;
455 
456     auto objPath = getLoggingObjPath(iter->first);
457     try
458     {
459         service = ipmi::getService(bus, ipmi::sel::logDeleteIntf, objPath);
460     }
461     catch (const std::runtime_error& e)
462     {
463         log<level::ERR>(e.what());
464         return ipmi::responseUnspecifiedError();
465     }
466 
467     auto methodCall = bus.new_method_call(service.c_str(), objPath.c_str(),
468                                           ipmi::sel::logDeleteIntf, "Delete");
469     try
470     {
471         auto reply = bus.call(methodCall);
472     }
473     catch (const std::exception& e)
474     {
475         return ipmi::responseUnspecifiedError();
476     }
477 
478     return ipmi::responseSuccess(delRecordID);
479 }
480 
481 /** @brief implements the Clear SEL command
482  * @request
483  *   - reservationID   // Reservation ID.
484  *   - clr             // char array { 'C'(0x43h), 'L'(0x4Ch), 'R'(0x52h) }
485  *   - eraseOperation; // requested operation.
486  *
487  *  @returns ipmi completion code plus response data
488  *   - erase status
489  */
490 
491 ipmi::RspType<uint8_t // erase status
492               >
493     clearSEL(uint16_t reservationID, const std::array<char, 3>& clr,
494              uint8_t eraseOperation)
495 {
496     static constexpr std::array<char, 3> clrOk = {'C', 'L', 'R'};
497     if (clr != clrOk)
498     {
499         return ipmi::responseInvalidFieldRequest();
500     }
501 
502     if (!checkSELReservation(reservationID))
503     {
504         return ipmi::responseInvalidReservationId();
505     }
506 
507     /*
508      * Erasure status cannot be fetched from DBUS, so always return erasure
509      * status as `erase completed`.
510      */
511     if (eraseOperation == ipmi::sel::getEraseStatus)
512     {
513         return ipmi::responseSuccess(
514             static_cast<uint8_t>(ipmi::sel::eraseComplete));
515     }
516 
517     // Per the IPMI spec, need to cancel any reservation when the SEL is cleared
518     cancelSELReservation();
519 
520     sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
521     auto service = ipmi::getService(bus, ipmi::sel::logIntf, ipmi::sel::logObj);
522     auto method = bus.new_method_call(service.c_str(), ipmi::sel::logObj,
523                                       ipmi::sel::logIntf,
524                                       ipmi::sel::logDeleteAllMethod);
525     try
526     {
527         bus.call_noreply(method);
528     }
529     catch (const sdbusplus::exception_t& e)
530     {
531         log<level::ERR>("Error eraseAll ", entry("ERROR=%s", e.what()));
532         return ipmi::responseUnspecifiedError();
533     }
534 
535     return ipmi::responseSuccess(
536         static_cast<uint8_t>(ipmi::sel::eraseComplete));
537 }
538 
539 /** @brief implements the get SEL time command
540  *  @returns IPMI completion code plus response data
541  *   -current time
542  */
543 ipmi::RspType<uint32_t> // current time
544     ipmiStorageGetSelTime()
545 {
546     using namespace std::chrono;
547     uint64_t bmc_time_usec = 0;
548     std::stringstream bmcTime;
549 
550     try
551     {
552         sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
553         auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
554         std::variant<uint64_t> value;
555 
556         // Get bmc time
557         auto method = bus.new_method_call(service.c_str(), BMC_TIME_PATH,
558                                           DBUS_PROPERTIES, "Get");
559 
560         method.append(TIME_INTERFACE, PROPERTY_ELAPSED);
561         auto reply = bus.call(method);
562         reply.read(value);
563         bmc_time_usec = std::get<uint64_t>(value);
564     }
565     catch (const InternalFailure& e)
566     {
567         log<level::ERR>(e.what());
568         return ipmi::responseUnspecifiedError();
569     }
570     catch (const std::exception& e)
571     {
572         log<level::ERR>(e.what());
573         return ipmi::responseUnspecifiedError();
574     }
575 
576     bmcTime << "BMC time:"
577             << duration_cast<seconds>(microseconds(bmc_time_usec)).count();
578     log<level::DEBUG>(bmcTime.str().c_str());
579 
580     // Time is really long int but IPMI wants just uint32. This works okay until
581     // the number of seconds since 1970 overflows uint32 size.. Still a whole
582     // lot of time here to even think about that.
583     return ipmi::responseSuccess(
584         duration_cast<seconds>(microseconds(bmc_time_usec)).count());
585 }
586 
587 /** @brief implements the set SEL time command
588  *  @param selDeviceTime - epoch time
589  *        -local time as the number of seconds from 00:00:00, January 1, 1970
590  *  @returns IPMI completion code
591  */
592 ipmi::RspType<> ipmiStorageSetSelTime(uint32_t selDeviceTime)
593 {
594     using namespace std::chrono;
595     microseconds usec{seconds(selDeviceTime)};
596 
597     try
598     {
599         sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
600         bool ntp = std::get<bool>(
601             ipmi::getDbusProperty(bus, SystemdTimeService, SystemdTimePath,
602                                   SystemdTimeInterface, "NTP"));
603         if (ntp)
604         {
605             return ipmi::responseCommandNotAvailable();
606         }
607 
608         auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
609         std::variant<uint64_t> value{(uint64_t)usec.count()};
610 
611         // Set bmc time
612         auto method = bus.new_method_call(service.c_str(), BMC_TIME_PATH,
613                                           DBUS_PROPERTIES, "Set");
614 
615         method.append(TIME_INTERFACE, PROPERTY_ELAPSED, value);
616         auto reply = bus.call(method);
617     }
618     catch (const InternalFailure& e)
619     {
620         log<level::ERR>(e.what());
621         return ipmi::responseUnspecifiedError();
622     }
623     catch (const std::exception& e)
624     {
625         log<level::ERR>(e.what());
626         return ipmi::responseUnspecifiedError();
627     }
628 
629     return ipmi::responseSuccess();
630 }
631 
632 /** @brief implements the reserve SEL command
633  *  @returns IPMI completion code plus response data
634  *   - SEL reservation ID.
635  */
636 ipmi::RspType<uint16_t> ipmiStorageReserveSel()
637 {
638     return ipmi::responseSuccess(reserveSel());
639 }
640 
641 /** @brief implements the Add SEL entry command
642  * @request
643  *
644  *   - recordID      ID used for SEL Record access
645  *   - recordType    Record Type
646  *   - timeStamp     Time when event was logged. LS byte first
647  *   - generatorID   software ID if event was generated from
648  *                   system software
649  *   - evmRev        event message format version
650  *   - sensorType    sensor type code for service that generated
651  *                   the event
652  *   - sensorNumber  number of sensors that generated the event
653  *   - eventDir     event dir
654  *   - eventData    event data field contents
655  *
656  *  @returns ipmi completion code plus response data
657  *   - RecordID of the Added SEL entry
658  */
659 ipmi::RspType<uint16_t // recordID of the Added SEL entry
660               >
661     ipmiStorageAddSEL(uint16_t recordID, uint8_t recordType,
662                       [[maybe_unused]] uint32_t timeStamp, uint16_t generatorID,
663                       [[maybe_unused]] uint8_t evmRev, uint8_t sensorType,
664                       uint8_t sensorNumber, uint8_t eventDir,
665                       std::array<uint8_t, eventDataSize> eventData)
666 {
667     std::string objpath;
668     static constexpr auto systemRecordType = 0x02;
669     // Hostboot sends SEL with OEM record type 0xDE to indicate that there is
670     // a maintenance procedure associated with eSEL record.
671     static constexpr auto procedureType = 0xDE;
672     cancelSELReservation();
673     if (recordType == systemRecordType)
674     {
675         for (const auto& it : invSensors)
676         {
677             if (it.second.sensorID == sensorNumber)
678             {
679                 objpath = it.first;
680                 break;
681             }
682         }
683         auto selDataStr = ipmi::sel::toHexStr(eventData);
684 
685         bool assert = (eventDir & 0x80) ? false : true;
686 
687         recordID = report<SELCreated>(Created::RECORD_TYPE(recordType),
688                                       Created::GENERATOR_ID(generatorID),
689                                       Created::SENSOR_DATA(selDataStr.c_str()),
690                                       Created::EVENT_DIR(assert),
691                                       Created::SENSOR_PATH(objpath.c_str()));
692     }
693     else if (recordType == procedureType)
694     {
695         // In the OEM record type 0xDE, byte 11 in the SEL record indicate the
696         // procedure number.
697         createProcedureLogEntry(sensorType);
698     }
699 
700     return ipmi::responseSuccess(recordID);
701 }
702 
703 bool isFruPresent(ipmi::Context::ptr& ctx, const std::string& fruPath)
704 {
705     using namespace ipmi::fru;
706 
707     std::string service;
708     boost::system::error_code ec = getService(ctx, invItemInterface,
709                                               invObjPath + fruPath, service);
710     if (!ec)
711     {
712         bool result;
713         ec = ipmi::getDbusProperty(ctx, service, invObjPath + fruPath,
714                                    invItemInterface, itemPresentProp, result);
715         if (!ec)
716         {
717             return result;
718         }
719     }
720 
721     ipmi::ObjectValueTree managedObjects;
722     ec = getManagedObjects(ctx, "xyz.openbmc_project.EntityManager",
723                            "/xyz/openbmc_project/inventory", managedObjects);
724     if (!ec)
725     {
726         auto connection = managedObjects.find(fruPath);
727         if (connection != managedObjects.end())
728         {
729             return true;
730         }
731     }
732 
733     return false;
734 }
735 
736 /** @brief implements the get FRU Inventory Area Info command
737  *
738  *  @returns IPMI completion code plus response data
739  *   - FRU Inventory area size in bytes,
740  *   - access bit
741  **/
742 ipmi::RspType<uint16_t, // FRU Inventory area size in bytes,
743               uint8_t   // access size (bytes / words)
744               >
745     ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruID)
746 {
747     auto iter = frus.find(fruID);
748     if (iter == frus.end())
749     {
750         return ipmi::responseSensorInvalid();
751     }
752 
753     auto path = iter->second[0].path;
754     if (!isFruPresent(ctx, path))
755     {
756         return ipmi::responseSensorInvalid();
757     }
758 
759     try
760     {
761         return ipmi::responseSuccess(
762             static_cast<uint16_t>(getFruAreaData(fruID).size()),
763             static_cast<uint8_t>(AccessMode::bytes));
764     }
765     catch (const InternalFailure& e)
766     {
767         log<level::ERR>(e.what());
768         return ipmi::responseUnspecifiedError();
769     }
770 }
771 
772 /**@brief implements the Read FRU Data command
773  * @param fruDeviceId - FRU device ID. FFh = reserved
774  * @param offset      - FRU inventory offset to read
775  * @param readCount   - count to read
776  *
777  * @return IPMI completion code plus response data
778  * - returnCount - response data count.
779  * - data        -  response data
780  */
781 ipmi::RspType<uint8_t,              // count returned
782               std::vector<uint8_t>> // FRU data
783     ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t offset,
784                            uint8_t readCount)
785 {
786     if (fruDeviceId == 0xFF)
787     {
788         return ipmi::responseInvalidFieldRequest();
789     }
790 
791     auto iter = frus.find(fruDeviceId);
792     if (iter == frus.end())
793     {
794         return ipmi::responseSensorInvalid();
795     }
796 
797     try
798     {
799         const auto& fruArea = getFruAreaData(fruDeviceId);
800         auto size = fruArea.size();
801 
802         if (offset >= size)
803         {
804             return ipmi::responseParmOutOfRange();
805         }
806 
807         // Write the count of response data.
808         uint8_t returnCount;
809         if ((offset + readCount) <= size)
810         {
811             returnCount = readCount;
812         }
813         else
814         {
815             returnCount = size - offset;
816         }
817 
818         std::vector<uint8_t> fruData((fruArea.begin() + offset),
819                                      (fruArea.begin() + offset + returnCount));
820 
821         return ipmi::responseSuccess(returnCount, fruData);
822     }
823     catch (const InternalFailure& e)
824     {
825         log<level::ERR>(e.what());
826         return ipmi::responseUnspecifiedError();
827     }
828 }
829 
830 ipmi::RspType<uint8_t,  // SDR version
831               uint16_t, // record count LS first
832               uint16_t, // free space in bytes, LS first
833               uint32_t, // addition timestamp LS first
834               uint32_t, // deletion timestamp LS first
835               uint8_t>  // operation Support
836     ipmiGetRepositoryInfo()
837 {
838     constexpr uint8_t sdrVersion = 0x51;
839     constexpr uint16_t freeSpace = 0xFFFF;
840     constexpr uint32_t additionTimestamp = 0x0;
841     constexpr uint32_t deletionTimestamp = 0x0;
842     constexpr uint8_t operationSupport = 0;
843 
844     // Get SDR count. This returns the total number of SDRs in the device.
845     const auto& entityRecords =
846         ipmi::sensor::EntityInfoMapContainer::getContainer()
847             ->getIpmiEntityRecords();
848     uint16_t records = ipmi::sensor::sensors.size() + frus.size() +
849                        entityRecords.size();
850 
851     return ipmi::responseSuccess(sdrVersion, records, freeSpace,
852                                  additionTimestamp, deletionTimestamp,
853                                  operationSupport);
854 }
855 
856 void register_netfn_storage_functions()
857 {
858     selCacheMapInitialized = false;
859     initSELCache();
860     // Handlers with dbus-sdr handler implementation.
861     // Do not register the hander if it dynamic sensors stack is used.
862 
863 #ifndef FEATURE_DYNAMIC_SENSORS
864     // <Get SEL Info>
865     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
866                           ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
867                           ipmiStorageGetSelInfo);
868 
869     // <Get SEL Time>
870     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
871                           ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
872                           ipmiStorageGetSelTime);
873 
874     // <Set SEL Time>
875     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
876                           ipmi::storage::cmdSetSelTime,
877                           ipmi::Privilege::Operator, ipmiStorageSetSelTime);
878 
879     // <Get SEL Entry>
880     ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SEL_ENTRY, NULL,
881                            getSELEntry, PRIVILEGE_USER);
882 
883     // <Delete SEL Entry>
884     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
885                           ipmi::storage::cmdDeleteSelEntry,
886                           ipmi::Privilege::Operator, deleteSELEntry);
887 
888     // <Add SEL Entry>
889     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
890                           ipmi::storage::cmdAddSelEntry,
891                           ipmi::Privilege::Operator, ipmiStorageAddSEL);
892 
893     // <Clear SEL>
894     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
895                           ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
896                           clearSEL);
897 
898     // <Get FRU Inventory Area Info>
899     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
900                           ipmi::storage::cmdGetFruInventoryAreaInfo,
901                           ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
902 
903     // <READ FRU Data>
904     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
905                           ipmi::storage::cmdReadFruData,
906                           ipmi::Privilege::Operator, ipmiStorageReadFruData);
907 
908     // <Get Repository Info>
909     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
910                           ipmi::storage::cmdGetSdrRepositoryInfo,
911                           ipmi::Privilege::User, ipmiGetRepositoryInfo);
912 
913     // <Reserve SDR Repository>
914     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
915                           ipmi::storage::cmdReserveSdrRepository,
916                           ipmi::Privilege::User, ipmiSensorReserveSdr);
917 
918     // <Get SDR>
919     ipmi_register_callback(NETFUN_STORAGE, IPMI_CMD_GET_SDR, nullptr,
920                            ipmi_sen_get_sdr, PRIVILEGE_USER);
921 
922 #endif
923 
924     // Common Handers used by both implementation.
925 
926     // <Reserve SEL>
927     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
928                           ipmi::storage::cmdReserveSel, ipmi::Privilege::User,
929                           ipmiStorageReserveSel);
930 
931     ipmi::fru::registerCallbackHandler();
932     return;
933 }
934