1 /*
2 // Copyright (c) 2017-2019 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 
17 #include "dbus-sdr/storagecommands.hpp"
18 
19 #include "dbus-sdr/sdrutils.hpp"
20 #include "selutility.hpp"
21 
22 #include <boost/algorithm/string.hpp>
23 #include <boost/container/flat_map.hpp>
24 #include <boost/process.hpp>
25 #include <ipmid/api.hpp>
26 #include <ipmid/message.hpp>
27 #include <ipmid/types.hpp>
28 #include <phosphor-logging/log.hpp>
29 #include <sdbusplus/message/types.hpp>
30 #include <sdbusplus/timer.hpp>
31 
32 #include <filesystem>
33 #include <fstream>
34 #include <functional>
35 #include <iostream>
36 #include <stdexcept>
37 #include <string_view>
38 
39 static constexpr bool DEBUG = false;
40 
41 namespace dynamic_sensors::ipmi::sel
42 {
43 static const std::filesystem::path selLogDir = "/var/log";
44 static const std::string selLogFilename = "ipmi_sel";
45 
46 static int getFileTimestamp(const std::filesystem::path& file)
47 {
48     struct stat st;
49 
50     if (stat(file.c_str(), &st) >= 0)
51     {
52         return st.st_mtime;
53     }
54     return ::ipmi::sel::invalidTimeStamp;
55 }
56 
57 namespace erase_time
58 {
59 static constexpr const char* selEraseTimestamp = "/var/lib/ipmi/sel_erase_time";
60 
61 int get()
62 {
63     return getFileTimestamp(selEraseTimestamp);
64 }
65 } // namespace erase_time
66 } // namespace dynamic_sensors::ipmi::sel
67 
68 namespace ipmi
69 {
70 
71 namespace storage
72 {
73 
74 constexpr static const size_t maxMessageSize = 64;
75 constexpr static const size_t maxFruSdrNameSize = 16;
76 using ObjectType =
77     boost::container::flat_map<std::string,
78                                boost::container::flat_map<std::string, Value>>;
79 using ManagedObjectType =
80     boost::container::flat_map<sdbusplus::message::object_path, ObjectType>;
81 using ManagedEntry = std::pair<sdbusplus::message::object_path, ObjectType>;
82 
83 constexpr static const char* selLoggerServiceName =
84     "xyz.openbmc_project.Logging.IPMI";
85 constexpr static const char* fruDeviceServiceName =
86     "xyz.openbmc_project.FruDevice";
87 constexpr static const char* entityManagerServiceName =
88     "xyz.openbmc_project.EntityManager";
89 constexpr static const size_t writeTimeoutSeconds = 10;
90 constexpr static const char* chassisTypeRackMount = "23";
91 constexpr static const char* chassisTypeMainServer = "17";
92 
93 // event direction is bit[7] of eventType where 1b = Deassertion event
94 constexpr static const uint8_t deassertionEvent = 0x80;
95 
96 static std::vector<uint8_t> fruCache;
97 static constexpr uint16_t invalidBus = 0xFFFF;
98 static constexpr uint8_t invalidAddr = 0xFF;
99 static uint16_t cacheBus = invalidBus;
100 static uint8_t cacheAddr = invalidAddr;
101 static uint8_t lastDevId = 0xFF;
102 
103 static uint16_t writeBus = invalidBus;
104 static uint8_t writeAddr = invalidAddr;
105 
106 std::unique_ptr<phosphor::Timer> writeTimer = nullptr;
107 static std::vector<sdbusplus::bus::match_t> fruMatches;
108 
109 ManagedObjectType frus;
110 
111 // we unfortunately have to build a map of hashes in case there is a
112 // collision to verify our dev-id
113 boost::container::flat_map<uint8_t, std::pair<uint16_t, uint8_t>> deviceHashes;
114 void registerStorageFunctions() __attribute__((constructor));
115 
116 bool writeFru(const std::vector<uint8_t>& fru)
117 {
118     if (writeBus == invalidBus && writeAddr == invalidAddr)
119     {
120         return true;
121     }
122     lastDevId = 0xFF;
123     std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
124     sdbusplus::message_t writeFru = dbus->new_method_call(
125         fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
126         "xyz.openbmc_project.FruDeviceManager", "WriteFru");
127     writeFru.append(writeBus, writeAddr, fru);
128     try
129     {
130         sdbusplus::message_t writeFruResp = dbus->call(writeFru);
131     }
132     catch (const sdbusplus::exception_t&)
133     {
134         // todo: log sel?
135         phosphor::logging::log<phosphor::logging::level::ERR>(
136             "error writing fru");
137         return false;
138     }
139     writeBus = invalidBus;
140     writeAddr = invalidAddr;
141     return true;
142 }
143 
144 void writeFruCache()
145 {
146     writeFru(fruCache);
147 }
148 
149 void createTimers()
150 {
151     writeTimer = std::make_unique<phosphor::Timer>(writeFruCache);
152 }
153 
154 void recalculateHashes()
155 {
156     deviceHashes.clear();
157     // hash the object paths to create unique device id's. increment on
158     // collision
159     std::hash<std::string> hasher;
160     for (const auto& fru : frus)
161     {
162         auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice");
163         if (fruIface == fru.second.end())
164         {
165             continue;
166         }
167 
168         auto busFind = fruIface->second.find("BUS");
169         auto addrFind = fruIface->second.find("ADDRESS");
170         if (busFind == fruIface->second.end() ||
171             addrFind == fruIface->second.end())
172         {
173             phosphor::logging::log<phosphor::logging::level::INFO>(
174                 "fru device missing Bus or Address",
175                 phosphor::logging::entry("FRU=%s", fru.first.str.c_str()));
176             continue;
177         }
178 
179         uint16_t fruBus = std::get<uint32_t>(busFind->second);
180         uint8_t fruAddr = std::get<uint32_t>(addrFind->second);
181         auto chassisFind = fruIface->second.find("CHASSIS_TYPE");
182         std::string chassisType;
183         if (chassisFind != fruIface->second.end())
184         {
185             chassisType = std::get<std::string>(chassisFind->second);
186         }
187 
188         uint8_t fruHash = 0;
189         if (chassisType.compare(chassisTypeRackMount) != 0 &&
190             chassisType.compare(chassisTypeMainServer) != 0)
191         {
192             fruHash = hasher(fru.first.str);
193             // can't be 0xFF based on spec, and 0 is reserved for baseboard
194             if (fruHash == 0 || fruHash == 0xFF)
195             {
196                 fruHash = 1;
197             }
198         }
199         std::pair<uint16_t, uint8_t> newDev(fruBus, fruAddr);
200 
201         bool emplacePassed = false;
202         while (!emplacePassed)
203         {
204             auto resp = deviceHashes.emplace(fruHash, newDev);
205             emplacePassed = resp.second;
206             if (!emplacePassed)
207             {
208                 fruHash++;
209                 // can't be 0xFF based on spec, and 0 is reserved for
210                 // baseboard
211                 if (fruHash == 0XFF)
212                 {
213                     fruHash = 0x1;
214                 }
215             }
216         }
217     }
218 }
219 
220 void replaceCacheFru(
221     const std::shared_ptr<sdbusplus::asio::connection>& bus,
222     boost::asio::yield_context& yield,
223     [[maybe_unused]] const std::optional<std::string>& path = std::nullopt)
224 {
225     boost::system::error_code ec;
226 
227     frus = bus->yield_method_call<ManagedObjectType>(
228         yield, ec, fruDeviceServiceName, "/",
229         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
230     if (ec)
231     {
232         phosphor::logging::log<phosphor::logging::level::ERR>(
233             "GetMangagedObjects for replaceCacheFru failed",
234             phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
235 
236         return;
237     }
238     recalculateHashes();
239 }
240 
241 std::pair<ipmi::Cc, std::vector<uint8_t>> getFru(ipmi::Context::ptr ctx,
242                                                  uint8_t devId)
243 {
244     if (lastDevId == devId && devId != 0xFF)
245     {
246         return {ipmi::ccSuccess, fruCache};
247     }
248 
249     auto deviceFind = deviceHashes.find(devId);
250     if (deviceFind == deviceHashes.end())
251     {
252         return {IPMI_CC_SENSOR_INVALID, {}};
253     }
254 
255     cacheBus = deviceFind->second.first;
256     cacheAddr = deviceFind->second.second;
257 
258     boost::system::error_code ec;
259 
260     std::vector<uint8_t> fru =
261         ctx->bus->yield_method_call<std::vector<uint8_t>>(
262             ctx->yield, ec, fruDeviceServiceName,
263             "/xyz/openbmc_project/FruDevice",
264             "xyz.openbmc_project.FruDeviceManager", "GetRawFru", cacheBus,
265             cacheAddr);
266     if (ec)
267     {
268         phosphor::logging::log<phosphor::logging::level::ERR>(
269             "Couldn't get raw fru",
270             phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
271 
272         cacheBus = invalidBus;
273         cacheAddr = invalidAddr;
274         return {ipmi::ccResponseError, {}};
275     }
276 
277     fruCache.clear();
278     lastDevId = devId;
279     fruCache = fru;
280 
281     return {ipmi::ccSuccess, fru};
282 }
283 
284 void writeFruIfRunning()
285 {
286     if (!writeTimer->isRunning())
287     {
288         return;
289     }
290     writeTimer->stop();
291     writeFruCache();
292 }
293 
294 void startMatch(void)
295 {
296     if (fruMatches.size())
297     {
298         return;
299     }
300 
301     fruMatches.reserve(2);
302 
303     auto bus = getSdBus();
304     fruMatches.emplace_back(*bus,
305                             "type='signal',arg0path='/xyz/openbmc_project/"
306                             "FruDevice/',member='InterfacesAdded'",
307                             [](sdbusplus::message_t& message) {
308         sdbusplus::message::object_path path;
309         ObjectType object;
310         try
311         {
312             message.read(path, object);
313         }
314         catch (const sdbusplus::exception_t&)
315         {
316             return;
317         }
318         auto findType = object.find("xyz.openbmc_project.FruDevice");
319         if (findType == object.end())
320         {
321             return;
322         }
323         writeFruIfRunning();
324         frus[path] = object;
325         recalculateHashes();
326         lastDevId = 0xFF;
327     });
328 
329     fruMatches.emplace_back(*bus,
330                             "type='signal',arg0path='/xyz/openbmc_project/"
331                             "FruDevice/',member='InterfacesRemoved'",
332                             [](sdbusplus::message_t& message) {
333         sdbusplus::message::object_path path;
334         std::set<std::string> interfaces;
335         try
336         {
337             message.read(path, interfaces);
338         }
339         catch (const sdbusplus::exception_t&)
340         {
341             return;
342         }
343         auto findType = interfaces.find("xyz.openbmc_project.FruDevice");
344         if (findType == interfaces.end())
345         {
346             return;
347         }
348         writeFruIfRunning();
349         frus.erase(path);
350         recalculateHashes();
351         lastDevId = 0xFF;
352     });
353 
354     // call once to populate
355     boost::asio::spawn(*getIoContext(), [](boost::asio::yield_context yield) {
356         replaceCacheFru(getSdBus(), yield);
357     });
358 }
359 
360 /** @brief implements the read FRU data command
361  *  @param fruDeviceId        - FRU Device ID
362  *  @param fruInventoryOffset - FRU Inventory Offset to write
363  *  @param countToRead        - Count to read
364  *
365  *  @returns ipmi completion code plus response data
366  *   - countWritten  - Count written
367  */
368 ipmi::RspType<uint8_t,             // Count
369               std::vector<uint8_t> // Requested data
370               >
371     ipmiStorageReadFruData(ipmi::Context::ptr ctx, uint8_t fruDeviceId,
372                            uint16_t fruInventoryOffset, uint8_t countToRead)
373 {
374     if (fruDeviceId == 0xFF)
375     {
376         return ipmi::responseInvalidFieldRequest();
377     }
378 
379     auto [status, fru] = getFru(ctx, fruDeviceId);
380     if (status != ipmi::ccSuccess)
381     {
382         return ipmi::response(status);
383     }
384 
385     size_t fromFruByteLen = 0;
386     if (countToRead + fruInventoryOffset < fru.size())
387     {
388         fromFruByteLen = countToRead;
389     }
390     else if (fru.size() > fruInventoryOffset)
391     {
392         fromFruByteLen = fru.size() - fruInventoryOffset;
393     }
394     else
395     {
396         return ipmi::responseReqDataLenExceeded();
397     }
398 
399     std::vector<uint8_t> requestedData;
400 
401     requestedData.insert(requestedData.begin(),
402                          fru.begin() + fruInventoryOffset,
403                          fru.begin() + fruInventoryOffset + fromFruByteLen);
404 
405     return ipmi::responseSuccess(static_cast<uint8_t>(requestedData.size()),
406                                  requestedData);
407 }
408 
409 /** @brief implements the write FRU data command
410  *  @param fruDeviceId        - FRU Device ID
411  *  @param fruInventoryOffset - FRU Inventory Offset to write
412  *  @param dataToWrite        - Data to write
413  *
414  *  @returns ipmi completion code plus response data
415  *   - countWritten  - Count written
416  */
417 ipmi::RspType<uint8_t>
418     ipmiStorageWriteFruData(ipmi::Context::ptr ctx, uint8_t fruDeviceId,
419                             uint16_t fruInventoryOffset,
420                             std::vector<uint8_t>& dataToWrite)
421 {
422     if (fruDeviceId == 0xFF)
423     {
424         return ipmi::responseInvalidFieldRequest();
425     }
426 
427     size_t writeLen = dataToWrite.size();
428 
429     auto [status, fru] = getFru(ctx, fruDeviceId);
430     if (status != ipmi::ccSuccess)
431     {
432         return ipmi::response(status);
433     }
434     size_t lastWriteAddr = fruInventoryOffset + writeLen;
435     if (fru.size() < lastWriteAddr)
436     {
437         fru.resize(fruInventoryOffset + writeLen);
438     }
439 
440     std::copy(dataToWrite.begin(), dataToWrite.begin() + writeLen,
441               fru.begin() + fruInventoryOffset);
442 
443     bool atEnd = false;
444 
445     if (fru.size() >= sizeof(FRUHeader))
446     {
447         FRUHeader* header = reinterpret_cast<FRUHeader*>(fru.data());
448 
449         size_t areaLength = 0;
450         size_t lastRecordStart = std::max(
451             {header->internalOffset, header->chassisOffset, header->boardOffset,
452              header->productOffset, header->multiRecordOffset});
453         lastRecordStart *= 8; // header starts in are multiples of 8 bytes
454 
455         if (header->multiRecordOffset)
456         {
457             // This FRU has a MultiRecord Area
458             uint8_t endOfList = 0;
459             // Walk the MultiRecord headers until the last record
460             while (!endOfList)
461             {
462                 // The MSB in the second byte of the MultiRecord header signals
463                 // "End of list"
464                 endOfList = fru[lastRecordStart + 1] & 0x80;
465                 // Third byte in the MultiRecord header is the length
466                 areaLength = fru[lastRecordStart + 2];
467                 // This length is in bytes (not 8 bytes like other headers)
468                 areaLength += 5; // The length omits the 5 byte header
469                 if (!endOfList)
470                 {
471                     // Next MultiRecord header
472                     lastRecordStart += areaLength;
473                 }
474             }
475         }
476         else
477         {
478             // This FRU does not have a MultiRecord Area
479             // Get the length of the area in multiples of 8 bytes
480             if (lastWriteAddr > (lastRecordStart + 1))
481             {
482                 // second byte in record area is the length
483                 areaLength = fru[lastRecordStart + 1];
484                 areaLength *= 8; // it is in multiples of 8 bytes
485             }
486         }
487         if (lastWriteAddr >= (areaLength + lastRecordStart))
488         {
489             atEnd = true;
490         }
491     }
492     uint8_t countWritten = 0;
493 
494     writeBus = cacheBus;
495     writeAddr = cacheAddr;
496     if (atEnd)
497     {
498         // cancel timer, we're at the end so might as well send it
499         writeTimer->stop();
500         if (!writeFru(fru))
501         {
502             return ipmi::responseInvalidFieldRequest();
503         }
504         countWritten = std::min(fru.size(), static_cast<size_t>(0xFF));
505     }
506     else
507     {
508         fruCache = fru; // Write-back
509         // start a timer, if no further data is sent  to check to see if it is
510         // valid
511         writeTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
512             std::chrono::seconds(writeTimeoutSeconds)));
513         countWritten = 0;
514     }
515 
516     return ipmi::responseSuccess(countWritten);
517 }
518 
519 /** @brief implements the get FRU inventory area info command
520  *  @param fruDeviceId  - FRU Device ID
521  *
522  *  @returns IPMI completion code plus response data
523  *   - inventorySize - Number of possible allocation units
524  *   - accessType    - Allocation unit size in bytes.
525  */
526 ipmi::RspType<uint16_t, // inventorySize
527               uint8_t>  // accessType
528     ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruDeviceId)
529 {
530     if (fruDeviceId == 0xFF)
531     {
532         return ipmi::responseInvalidFieldRequest();
533     }
534 
535     auto [ret, fru] = getFru(ctx, fruDeviceId);
536     if (ret != ipmi::ccSuccess)
537     {
538         return ipmi::response(ret);
539     }
540 
541     constexpr uint8_t accessType =
542         static_cast<uint8_t>(GetFRUAreaAccessType::byte);
543 
544     return ipmi::responseSuccess(fru.size(), accessType);
545 }
546 
547 ipmi_ret_t getFruSdrCount(ipmi::Context::ptr, size_t& count)
548 {
549     count = deviceHashes.size();
550     return IPMI_CC_OK;
551 }
552 
553 ipmi_ret_t getFruSdrs([[maybe_unused]] ipmi::Context::ptr ctx, size_t index,
554                       get_sdr::SensorDataFruRecord& resp)
555 {
556     if (deviceHashes.size() < index)
557     {
558         return IPMI_CC_INVALID_FIELD_REQUEST;
559     }
560     auto device = deviceHashes.begin() + index;
561     uint16_t& bus = device->second.first;
562     uint8_t& address = device->second.second;
563 
564     boost::container::flat_map<std::string, Value>* fruData = nullptr;
565     auto fru = std::find_if(frus.begin(), frus.end(),
566                             [bus, address, &fruData](ManagedEntry& entry) {
567         auto findFruDevice = entry.second.find("xyz.openbmc_project.FruDevice");
568         if (findFruDevice == entry.second.end())
569         {
570             return false;
571         }
572         fruData = &(findFruDevice->second);
573         auto findBus = findFruDevice->second.find("BUS");
574         auto findAddress = findFruDevice->second.find("ADDRESS");
575         if (findBus == findFruDevice->second.end() ||
576             findAddress == findFruDevice->second.end())
577         {
578             return false;
579         }
580         if (std::get<uint32_t>(findBus->second) != bus)
581         {
582             return false;
583         }
584         if (std::get<uint32_t>(findAddress->second) != address)
585         {
586             return false;
587         }
588         return true;
589     });
590     if (fru == frus.end())
591     {
592         return IPMI_CC_RESPONSE_ERROR;
593     }
594     std::string name;
595 
596 #ifdef USING_ENTITY_MANAGER_DECORATORS
597 
598     boost::container::flat_map<std::string, Value>* entityData = nullptr;
599 
600     // todo: this should really use caching, this is a very inefficient lookup
601     boost::system::error_code ec;
602 
603     ManagedObjectType entities = ctx->bus->yield_method_call<ManagedObjectType>(
604         ctx->yield, ec, entityManagerServiceName,
605         "/xyz/openbmc_project/inventory", "org.freedesktop.DBus.ObjectManager",
606         "GetManagedObjects");
607 
608     if (ec)
609     {
610         phosphor::logging::log<phosphor::logging::level::ERR>(
611             "GetMangagedObjects for ipmiStorageGetFruInvAreaInfo failed",
612             phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
613 
614         return ipmi::ccResponseError;
615     }
616 
617     auto entity =
618         std::find_if(entities.begin(), entities.end(),
619                      [bus, address, &entityData, &name](ManagedEntry& entry) {
620         auto findFruDevice = entry.second.find(
621             "xyz.openbmc_project.Inventory.Decorator.I2CDevice");
622         if (findFruDevice == entry.second.end())
623         {
624             return false;
625         }
626 
627         // Integer fields added via Entity-Manager json are uint64_ts by
628         // default.
629         auto findBus = findFruDevice->second.find("Bus");
630         auto findAddress = findFruDevice->second.find("Address");
631 
632         if (findBus == findFruDevice->second.end() ||
633             findAddress == findFruDevice->second.end())
634         {
635             return false;
636         }
637         if ((std::get<uint64_t>(findBus->second) != bus) ||
638             (std::get<uint64_t>(findAddress->second) != address))
639         {
640             return false;
641         }
642 
643         auto fruName = findFruDevice->second.find("Name");
644         if (fruName != findFruDevice->second.end())
645         {
646             name = std::get<std::string>(fruName->second);
647         }
648 
649         // At this point we found the device entry and should return
650         // true.
651         auto findIpmiDevice =
652             entry.second.find("xyz.openbmc_project.Inventory.Decorator.Ipmi");
653         if (findIpmiDevice != entry.second.end())
654         {
655             entityData = &(findIpmiDevice->second);
656         }
657 
658         return true;
659         });
660 
661     if (entity == entities.end())
662     {
663         if constexpr (DEBUG)
664         {
665             std::fprintf(stderr, "Ipmi or FruDevice Decorator interface "
666                                  "not found for Fru\n");
667         }
668     }
669 
670 #endif
671 
672     if (name.empty())
673     {
674         name = "UNKNOWN";
675     }
676     if (name.size() > maxFruSdrNameSize)
677     {
678         name = name.substr(0, maxFruSdrNameSize);
679     }
680     size_t sizeDiff = maxFruSdrNameSize - name.size();
681 
682     resp.header.record_id_lsb = 0x0; // calling code is to implement these
683     resp.header.record_id_msb = 0x0;
684     resp.header.sdr_version = ipmiSdrVersion;
685     resp.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
686     resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff;
687     resp.key.deviceAddress = 0x20;
688     resp.key.fruID = device->first;
689     resp.key.accessLun = 0x80; // logical / physical fru device
690     resp.key.channelNumber = 0x0;
691     resp.body.reserved = 0x0;
692     resp.body.deviceType = 0x10;
693     resp.body.deviceTypeModifier = 0x0;
694 
695     uint8_t entityID = 0;
696     uint8_t entityInstance = 0x1;
697 
698 #ifdef USING_ENTITY_MANAGER_DECORATORS
699     if (entityData)
700     {
701         auto entityIdProperty = entityData->find("EntityId");
702         auto entityInstanceProperty = entityData->find("EntityInstance");
703 
704         if (entityIdProperty != entityData->end())
705         {
706             entityID = static_cast<uint8_t>(
707                 std::get<uint64_t>(entityIdProperty->second));
708         }
709         if (entityInstanceProperty != entityData->end())
710         {
711             entityInstance = static_cast<uint8_t>(
712                 std::get<uint64_t>(entityInstanceProperty->second));
713         }
714     }
715 #endif
716 
717     resp.body.entityID = entityID;
718     resp.body.entityInstance = entityInstance;
719 
720     resp.body.oem = 0x0;
721     resp.body.deviceIDLen = name.size();
722     name.copy(resp.body.deviceID, name.size());
723 
724     return IPMI_CC_OK;
725 }
726 
727 static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles)
728 {
729     // Loop through the directory looking for ipmi_sel log files
730     for (const std::filesystem::directory_entry& dirEnt :
731          std::filesystem::directory_iterator(
732              dynamic_sensors::ipmi::sel::selLogDir))
733     {
734         std::string filename = dirEnt.path().filename();
735         if (boost::starts_with(filename,
736                                dynamic_sensors::ipmi::sel::selLogFilename))
737         {
738             // If we find an ipmi_sel log file, save the path
739             selLogFiles.emplace_back(dynamic_sensors::ipmi::sel::selLogDir /
740                                      filename);
741         }
742     }
743     // As the log files rotate, they are appended with a ".#" that is higher for
744     // the older logs. Since we don't expect more than 10 log files, we
745     // can just sort the list to get them in order from newest to oldest
746     std::sort(selLogFiles.begin(), selLogFiles.end());
747 
748     return !selLogFiles.empty();
749 }
750 
751 static int countSELEntries()
752 {
753     // Get the list of ipmi_sel log files
754     std::vector<std::filesystem::path> selLogFiles;
755     if (!getSELLogFiles(selLogFiles))
756     {
757         return 0;
758     }
759     int numSELEntries = 0;
760     // Loop through each log file and count the number of logs
761     for (const std::filesystem::path& file : selLogFiles)
762     {
763         std::ifstream logStream(file);
764         if (!logStream.is_open())
765         {
766             continue;
767         }
768 
769         std::string line;
770         while (std::getline(logStream, line))
771         {
772             numSELEntries++;
773         }
774     }
775     return numSELEntries;
776 }
777 
778 static bool findSELEntry(const int recordID,
779                          const std::vector<std::filesystem::path>& selLogFiles,
780                          std::string& entry)
781 {
782     // Record ID is the first entry field following the timestamp. It is
783     // preceded by a space and followed by a comma
784     std::string search = " " + std::to_string(recordID) + ",";
785 
786     // Loop through the ipmi_sel log entries
787     for (const std::filesystem::path& file : selLogFiles)
788     {
789         std::ifstream logStream(file);
790         if (!logStream.is_open())
791         {
792             continue;
793         }
794 
795         while (std::getline(logStream, entry))
796         {
797             // Check if the record ID matches
798             if (entry.find(search) != std::string::npos)
799             {
800                 return true;
801             }
802         }
803     }
804     return false;
805 }
806 
807 static uint16_t
808     getNextRecordID(const uint16_t recordID,
809                     const std::vector<std::filesystem::path>& selLogFiles)
810 {
811     uint16_t nextRecordID = recordID + 1;
812     std::string entry;
813     if (findSELEntry(nextRecordID, selLogFiles, entry))
814     {
815         return nextRecordID;
816     }
817     else
818     {
819         return ipmi::sel::lastEntry;
820     }
821 }
822 
823 static int fromHexStr(const std::string& hexStr, std::vector<uint8_t>& data)
824 {
825     for (unsigned int i = 0; i < hexStr.size(); i += 2)
826     {
827         try
828         {
829             data.push_back(static_cast<uint8_t>(
830                 std::stoul(hexStr.substr(i, 2), nullptr, 16)));
831         }
832         catch (const std::invalid_argument& e)
833         {
834             phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
835             return -1;
836         }
837         catch (const std::out_of_range& e)
838         {
839             phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
840             return -1;
841         }
842     }
843     return 0;
844 }
845 
846 ipmi::RspType<uint8_t,  // SEL version
847               uint16_t, // SEL entry count
848               uint16_t, // free space
849               uint32_t, // last add timestamp
850               uint32_t, // last erase timestamp
851               uint8_t>  // operation support
852     ipmiStorageGetSELInfo()
853 {
854     constexpr uint8_t selVersion = ipmi::sel::selVersion;
855     uint16_t entries = countSELEntries();
856     uint32_t addTimeStamp = dynamic_sensors::ipmi::sel::getFileTimestamp(
857         dynamic_sensors::ipmi::sel::selLogDir /
858         dynamic_sensors::ipmi::sel::selLogFilename);
859     uint32_t eraseTimeStamp = dynamic_sensors::ipmi::sel::erase_time::get();
860     constexpr uint8_t operationSupport =
861         dynamic_sensors::ipmi::sel::selOperationSupport;
862     constexpr uint16_t freeSpace =
863         0xffff; // Spec indicates that more than 64kB is free
864 
865     return ipmi::responseSuccess(selVersion, entries, freeSpace, addTimeStamp,
866                                  eraseTimeStamp, operationSupport);
867 }
868 
869 using systemEventType = std::tuple<
870     uint32_t, // Timestamp
871     uint16_t, // Generator ID
872     uint8_t,  // EvM Rev
873     uint8_t,  // Sensor Type
874     uint8_t,  // Sensor Number
875     uint7_t,  // Event Type
876     bool,     // Event Direction
877     std::array<uint8_t, dynamic_sensors::ipmi::sel::systemEventSize>>; // Event
878                                                                        // Data
879 using oemTsEventType = std::tuple<
880     uint32_t, // Timestamp
881     std::array<uint8_t, dynamic_sensors::ipmi::sel::oemTsEventSize>>; // Event
882                                                                       // Data
883 using oemEventType =
884     std::array<uint8_t, dynamic_sensors::ipmi::sel::oemEventSize>; // Event Data
885 
886 ipmi::RspType<uint16_t,                   // Next Record ID
887               uint16_t,                   // Record ID
888               uint8_t,                    // Record Type
889               std::variant<systemEventType, oemTsEventType,
890                            oemEventType>> // Record Content
891     ipmiStorageGetSELEntry(uint16_t reservationID, uint16_t targetID,
892                            uint8_t offset, uint8_t size)
893 {
894     // Only support getting the entire SEL record. If a partial size or non-zero
895     // offset is requested, return an error
896     if (offset != 0 || size != ipmi::sel::entireRecord)
897     {
898         return ipmi::responseRetBytesUnavailable();
899     }
900 
901     // Check the reservation ID if one is provided or required (only if the
902     // offset is non-zero)
903     if (reservationID != 0 || offset != 0)
904     {
905         if (!checkSELReservation(reservationID))
906         {
907             return ipmi::responseInvalidReservationId();
908         }
909     }
910 
911     // Get the ipmi_sel log files
912     std::vector<std::filesystem::path> selLogFiles;
913     if (!getSELLogFiles(selLogFiles))
914     {
915         return ipmi::responseSensorInvalid();
916     }
917 
918     std::string targetEntry;
919 
920     if (targetID == ipmi::sel::firstEntry)
921     {
922         // The first entry will be at the top of the oldest log file
923         std::ifstream logStream(selLogFiles.back());
924         if (!logStream.is_open())
925         {
926             return ipmi::responseUnspecifiedError();
927         }
928 
929         if (!std::getline(logStream, targetEntry))
930         {
931             return ipmi::responseUnspecifiedError();
932         }
933     }
934     else if (targetID == ipmi::sel::lastEntry)
935     {
936         // The last entry will be at the bottom of the newest log file
937         std::ifstream logStream(selLogFiles.front());
938         if (!logStream.is_open())
939         {
940             return ipmi::responseUnspecifiedError();
941         }
942 
943         std::string line;
944         while (std::getline(logStream, line))
945         {
946             targetEntry = line;
947         }
948     }
949     else
950     {
951         if (!findSELEntry(targetID, selLogFiles, targetEntry))
952         {
953             return ipmi::responseSensorInvalid();
954         }
955     }
956 
957     // The format of the ipmi_sel message is "<Timestamp>
958     // <ID>,<Type>,<EventData>,[<Generator ID>,<Path>,<Direction>]".
959     // First get the Timestamp
960     size_t space = targetEntry.find_first_of(" ");
961     if (space == std::string::npos)
962     {
963         return ipmi::responseUnspecifiedError();
964     }
965     std::string entryTimestamp = targetEntry.substr(0, space);
966     // Then get the log contents
967     size_t entryStart = targetEntry.find_first_not_of(" ", space);
968     if (entryStart == std::string::npos)
969     {
970         return ipmi::responseUnspecifiedError();
971     }
972     std::string_view entry(targetEntry);
973     entry.remove_prefix(entryStart);
974     // Use split to separate the entry into its fields
975     std::vector<std::string> targetEntryFields;
976     boost::split(targetEntryFields, entry, boost::is_any_of(","),
977                  boost::token_compress_on);
978     if (targetEntryFields.size() < 3)
979     {
980         return ipmi::responseUnspecifiedError();
981     }
982     std::string& recordIDStr = targetEntryFields[0];
983     std::string& recordTypeStr = targetEntryFields[1];
984     std::string& eventDataStr = targetEntryFields[2];
985 
986     uint16_t recordID;
987     uint8_t recordType;
988     try
989     {
990         recordID = std::stoul(recordIDStr);
991         recordType = std::stoul(recordTypeStr, nullptr, 16);
992     }
993     catch (const std::invalid_argument&)
994     {
995         return ipmi::responseUnspecifiedError();
996     }
997     uint16_t nextRecordID = getNextRecordID(recordID, selLogFiles);
998     std::vector<uint8_t> eventDataBytes;
999     if (fromHexStr(eventDataStr, eventDataBytes) < 0)
1000     {
1001         return ipmi::responseUnspecifiedError();
1002     }
1003 
1004     if (recordType == dynamic_sensors::ipmi::sel::systemEvent)
1005     {
1006         // Get the timestamp
1007         std::tm timeStruct = {};
1008         std::istringstream entryStream(entryTimestamp);
1009 
1010         uint32_t timestamp = ipmi::sel::invalidTimeStamp;
1011         if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
1012         {
1013             timestamp = std::mktime(&timeStruct);
1014         }
1015 
1016         // Set the event message revision
1017         uint8_t evmRev = dynamic_sensors::ipmi::sel::eventMsgRev;
1018 
1019         uint16_t generatorID = 0;
1020         uint8_t sensorType = 0;
1021         uint16_t sensorAndLun = 0;
1022         uint8_t sensorNum = 0xFF;
1023         uint7_t eventType = 0;
1024         bool eventDir = 0;
1025         // System type events should have six fields
1026         if (targetEntryFields.size() >= 6)
1027         {
1028             std::string& generatorIDStr = targetEntryFields[3];
1029             std::string& sensorPath = targetEntryFields[4];
1030             std::string& eventDirStr = targetEntryFields[5];
1031 
1032             // Get the generator ID
1033             try
1034             {
1035                 generatorID = std::stoul(generatorIDStr, nullptr, 16);
1036             }
1037             catch (const std::invalid_argument&)
1038             {
1039                 std::cerr << "Invalid Generator ID\n";
1040             }
1041 
1042             // Get the sensor type, sensor number, and event type for the sensor
1043             sensorType = getSensorTypeFromPath(sensorPath);
1044             sensorAndLun = getSensorNumberFromPath(sensorPath);
1045             sensorNum = static_cast<uint8_t>(sensorAndLun);
1046             if ((generatorID & 0x0001) == 0)
1047             {
1048                 // IPMB Address
1049                 generatorID |= sensorAndLun & 0x0300;
1050             }
1051             else
1052             {
1053                 // system software
1054                 generatorID |= sensorAndLun >> 8;
1055             }
1056             eventType = getSensorEventTypeFromPath(sensorPath);
1057 
1058             // Get the event direction
1059             try
1060             {
1061                 eventDir = std::stoul(eventDirStr) ? 0 : 1;
1062             }
1063             catch (const std::invalid_argument&)
1064             {
1065                 std::cerr << "Invalid Event Direction\n";
1066             }
1067         }
1068 
1069         // Only keep the eventData bytes that fit in the record
1070         std::array<uint8_t, dynamic_sensors::ipmi::sel::systemEventSize>
1071             eventData{};
1072         std::copy_n(eventDataBytes.begin(),
1073                     std::min(eventDataBytes.size(), eventData.size()),
1074                     eventData.begin());
1075 
1076         return ipmi::responseSuccess(
1077             nextRecordID, recordID, recordType,
1078             systemEventType{timestamp, generatorID, evmRev, sensorType,
1079                             sensorNum, eventType, eventDir, eventData});
1080     }
1081 
1082     return ipmi::responseUnspecifiedError();
1083 }
1084 
1085 /*
1086 Unused arguments
1087   uint16_t recordID, uint8_t recordType, uint32_t timestamp,
1088   uint16_t generatorID, uint8_t evmRev, uint8_t sensorType, uint8_t sensorNum,
1089   uint8_t eventType, uint8_t eventData1, uint8_t eventData2,
1090   uint8_t eventData3
1091 */
1092 ipmi::RspType<uint16_t> ipmiStorageAddSELEntry(uint16_t, uint8_t, uint32_t,
1093                                                uint16_t, uint8_t, uint8_t,
1094                                                uint8_t, uint8_t, uint8_t,
1095                                                uint8_t, uint8_t)
1096 {
1097     // Per the IPMI spec, need to cancel any reservation when a SEL entry is
1098     // added
1099     cancelSELReservation();
1100 
1101     uint16_t responseID = 0xFFFF;
1102     return ipmi::responseSuccess(responseID);
1103 }
1104 
1105 ipmi::RspType<uint8_t> ipmiStorageClearSEL(ipmi::Context::ptr ctx,
1106                                            uint16_t reservationID,
1107                                            const std::array<uint8_t, 3>& clr,
1108                                            uint8_t eraseOperation)
1109 {
1110     if (!checkSELReservation(reservationID))
1111     {
1112         return ipmi::responseInvalidReservationId();
1113     }
1114 
1115     static constexpr std::array<uint8_t, 3> clrExpected = {'C', 'L', 'R'};
1116     if (clr != clrExpected)
1117     {
1118         return ipmi::responseInvalidFieldRequest();
1119     }
1120 
1121     // Erasure status cannot be fetched, so always return erasure status as
1122     // `erase completed`.
1123     if (eraseOperation == ipmi::sel::getEraseStatus)
1124     {
1125         return ipmi::responseSuccess(ipmi::sel::eraseComplete);
1126     }
1127 
1128     // Check that initiate erase is correct
1129     if (eraseOperation != ipmi::sel::initiateErase)
1130     {
1131         return ipmi::responseInvalidFieldRequest();
1132     }
1133 
1134     // Per the IPMI spec, need to cancel any reservation when the SEL is
1135     // cleared
1136     cancelSELReservation();
1137 
1138     boost::system::error_code ec;
1139     ctx->bus->yield_method_call<>(ctx->yield, ec, selLoggerServiceName,
1140                                   "/xyz/openbmc_project/Logging/IPMI",
1141                                   "xyz.openbmc_project.Logging.IPMI", "Clear");
1142     if (ec)
1143     {
1144         std::cerr << "error in clear SEL: " << ec << std::endl;
1145         return ipmi::responseUnspecifiedError();
1146     }
1147 
1148     return ipmi::responseSuccess(ipmi::sel::eraseComplete);
1149 }
1150 
1151 ipmi::RspType<uint32_t> ipmiStorageGetSELTime()
1152 {
1153     struct timespec selTime = {};
1154 
1155     if (clock_gettime(CLOCK_REALTIME, &selTime) < 0)
1156     {
1157         return ipmi::responseUnspecifiedError();
1158     }
1159 
1160     return ipmi::responseSuccess(selTime.tv_sec);
1161 }
1162 
1163 ipmi::RspType<> ipmiStorageSetSELTime(uint32_t)
1164 {
1165     // Set SEL Time is not supported
1166     return ipmi::responseInvalidCommand();
1167 }
1168 
1169 std::vector<uint8_t>
1170     getType8SDRs(ipmi::sensor::EntityInfoMap::const_iterator& entity,
1171                  uint16_t recordId)
1172 {
1173     std::vector<uint8_t> resp;
1174     get_sdr::SensorDataEntityRecord data{};
1175 
1176     /* Header */
1177     get_sdr::header::set_record_id(recordId, &(data.header));
1178     // Based on IPMI Spec v2.0 rev 1.1
1179     data.header.sdr_version = SDR_VERSION;
1180     data.header.record_type = 0x08;
1181     data.header.record_length = sizeof(data.key) + sizeof(data.body);
1182 
1183     /* Key */
1184     data.key.containerEntityId = entity->second.containerEntityId;
1185     data.key.containerEntityInstance = entity->second.containerEntityInstance;
1186     get_sdr::key::set_flags(entity->second.isList, entity->second.isLinked,
1187                             &(data.key));
1188     data.key.entityId1 = entity->second.containedEntities[0].first;
1189     data.key.entityInstance1 = entity->second.containedEntities[0].second;
1190 
1191     /* Body */
1192     data.body.entityId2 = entity->second.containedEntities[1].first;
1193     data.body.entityInstance2 = entity->second.containedEntities[1].second;
1194     data.body.entityId3 = entity->second.containedEntities[2].first;
1195     data.body.entityInstance3 = entity->second.containedEntities[2].second;
1196     data.body.entityId4 = entity->second.containedEntities[3].first;
1197     data.body.entityInstance4 = entity->second.containedEntities[3].second;
1198 
1199     resp.insert(resp.end(), (uint8_t*)&data, ((uint8_t*)&data) + sizeof(data));
1200 
1201     return resp;
1202 }
1203 
1204 std::vector<uint8_t> getType12SDRs(uint16_t index, uint16_t recordId)
1205 {
1206     std::vector<uint8_t> resp;
1207     if (index == 0)
1208     {
1209         std::string bmcName = "Basbrd Mgmt Ctlr";
1210         Type12Record bmc(recordId, 0x20, 0, 0, 0xbf, 0x2e, 1, 0, bmcName);
1211         uint8_t* bmcPtr = reinterpret_cast<uint8_t*>(&bmc);
1212         resp.insert(resp.end(), bmcPtr, bmcPtr + sizeof(Type12Record));
1213     }
1214     else if (index == 1)
1215     {
1216         std::string meName = "Mgmt Engine";
1217         Type12Record me(recordId, 0x2c, 6, 0x24, 0x21, 0x2e, 2, 0, meName);
1218         uint8_t* mePtr = reinterpret_cast<uint8_t*>(&me);
1219         resp.insert(resp.end(), mePtr, mePtr + sizeof(Type12Record));
1220     }
1221     else
1222     {
1223         throw std::runtime_error("getType12SDRs:: Illegal index " +
1224                                  std::to_string(index));
1225     }
1226 
1227     return resp;
1228 }
1229 
1230 void registerStorageFunctions()
1231 {
1232     createTimers();
1233     startMatch();
1234 
1235     // <Get FRU Inventory Area Info>
1236     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1237                           ipmi::storage::cmdGetFruInventoryAreaInfo,
1238                           ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
1239     // <READ FRU Data>
1240     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1241                           ipmi::storage::cmdReadFruData, ipmi::Privilege::User,
1242                           ipmiStorageReadFruData);
1243 
1244     // <WRITE FRU Data>
1245     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1246                           ipmi::storage::cmdWriteFruData,
1247                           ipmi::Privilege::Operator, ipmiStorageWriteFruData);
1248 
1249     // <Get SEL Info>
1250     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1251                           ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
1252                           ipmiStorageGetSELInfo);
1253 
1254     // <Get SEL Entry>
1255     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1256                           ipmi::storage::cmdGetSelEntry, ipmi::Privilege::User,
1257                           ipmiStorageGetSELEntry);
1258 
1259     // <Add SEL Entry>
1260     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1261                           ipmi::storage::cmdAddSelEntry,
1262                           ipmi::Privilege::Operator, ipmiStorageAddSELEntry);
1263 
1264     // <Clear SEL>
1265     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1266                           ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
1267                           ipmiStorageClearSEL);
1268 
1269     // <Get SEL Time>
1270     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1271                           ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
1272                           ipmiStorageGetSELTime);
1273 
1274     // <Set SEL Time>
1275     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1276                           ipmi::storage::cmdSetSelTime,
1277                           ipmi::Privilege::Operator, ipmiStorageSetSELTime);
1278 }
1279 } // namespace storage
1280 } // namespace ipmi
1281