1 /**
2  * Copyright © 2019 IBM 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 #include "manager.hpp"
17 
18 #include "additional_data.hpp"
19 #include "elog_serialize.hpp"
20 #include "json_utils.hpp"
21 #include "pel.hpp"
22 #include "pel_entry.hpp"
23 #include "service_indicators.hpp"
24 #include "severity.hpp"
25 
26 #include <sys/inotify.h>
27 #include <unistd.h>
28 
29 #include <phosphor-logging/lg2.hpp>
30 #include <xyz/openbmc_project/Common/error.hpp>
31 #include <xyz/openbmc_project/Logging/Create/server.hpp>
32 
33 #include <filesystem>
34 #include <format>
35 #include <fstream>
36 #include <locale>
37 
38 namespace openpower
39 {
40 namespace pels
41 {
42 
43 using namespace phosphor::logging;
44 namespace fs = std::filesystem;
45 namespace rg = openpower::pels::message;
46 
47 namespace common_error = sdbusplus::xyz::openbmc_project::Common::Error;
48 
49 using Create = sdbusplus::server::xyz::openbmc_project::logging::Create;
50 
51 namespace additional_data
52 {
53 constexpr auto rawPEL = "RAWPEL";
54 constexpr auto esel = "ESEL";
55 constexpr auto error = "ERROR_NAME";
56 } // namespace additional_data
57 
58 constexpr auto defaultLogMessage = "xyz.openbmc_project.Logging.Error.Default";
59 constexpr uint32_t bmcThermalCompID = 0x2700;
60 constexpr uint32_t bmcFansCompID = 0x2800;
61 
~Manager()62 Manager::~Manager()
63 {
64     if (_pelFileDeleteFD != -1)
65     {
66         if (_pelFileDeleteWatchFD != -1)
67         {
68             inotify_rm_watch(_pelFileDeleteFD, _pelFileDeleteWatchFD);
69         }
70         close(_pelFileDeleteFD);
71     }
72 }
73 
create(const std::string & message,uint32_t obmcLogID,uint64_t timestamp,Entry::Level severity,const std::vector<std::string> & additionalData,const std::vector<std::string> & associations,const FFDCEntries & ffdc)74 void Manager::create(const std::string& message, uint32_t obmcLogID,
75                      uint64_t timestamp, Entry::Level severity,
76                      const std::vector<std::string>& additionalData,
77                      const std::vector<std::string>& associations,
78                      const FFDCEntries& ffdc)
79 {
80     AdditionalData ad{additionalData};
81 
82     // If a PEL was passed in via a filename or in an ESEL,
83     // use that.  Otherwise, create one.
84     auto rawPelPath = ad.getValue(additional_data::rawPEL);
85     if (rawPelPath)
86     {
87         addRawPEL(*rawPelPath, obmcLogID);
88     }
89     else
90     {
91         auto esel = ad.getValue(additional_data::esel);
92         if (esel)
93         {
94             addESELPEL(*esel, obmcLogID);
95         }
96         else
97         {
98             createPEL(message, obmcLogID, timestamp, severity, additionalData,
99                       associations, ffdc);
100         }
101     }
102 
103     setEntryPath(obmcLogID);
104     setServiceProviderNotifyFlag(obmcLogID);
105 }
106 
addRawPEL(const std::string & rawPelPath,uint32_t obmcLogID)107 void Manager::addRawPEL(const std::string& rawPelPath, uint32_t obmcLogID)
108 {
109     if (fs::exists(rawPelPath))
110     {
111         std::ifstream file(rawPelPath, std::ios::in | std::ios::binary);
112 
113         auto data = std::vector<uint8_t>(std::istreambuf_iterator<char>(file),
114                                          std::istreambuf_iterator<char>());
115         if (file.fail())
116         {
117             lg2::error(
118                 "Filesystem error reading a raw PEL. File = {FILE}, obmcLogID = {LOGID}",
119                 "FILE", rawPelPath, "LOGID", obmcLogID);
120             // TODO, Decide what to do here. Maybe nothing.
121             return;
122         }
123 
124         file.close();
125 
126         addPEL(data, obmcLogID);
127 
128         std::error_code ec;
129         fs::remove(rawPelPath, ec);
130     }
131     else
132     {
133         lg2::error(
134             "Raw PEL file from BMC event log does not exit. File = {FILE}, obmcLogID = {LOGID}",
135             "FILE", rawPelPath, "LOGID", obmcLogID);
136     }
137 }
138 
addPEL(std::vector<uint8_t> & pelData,uint32_t obmcLogID)139 void Manager::addPEL(std::vector<uint8_t>& pelData, uint32_t obmcLogID)
140 {
141     auto pel = std::make_unique<openpower::pels::PEL>(pelData, obmcLogID);
142     if (pel->valid())
143     {
144         // PELs created by others still need this field set by us.
145         pel->setCommitTime();
146 
147         // Assign Id other than to Hostbot PEL
148         if ((pel->privateHeader()).creatorID() !=
149             static_cast<uint8_t>(CreatorID::hostboot))
150         {
151             pel->assignID();
152         }
153         else
154         {
155             const Repository::LogID id{Repository::LogID::Pel(pel->id())};
156             auto result = _repo.hasPEL(id);
157             if (result)
158             {
159                 lg2::warning(
160                     "Duplicate HostBoot PEL ID {ID} found, moving it to archive folder",
161                     "ID", lg2::hex, pel->id());
162 
163                 _repo.archivePEL(*pel);
164 
165                 // No need to keep around the openBMC event log entry
166                 scheduleObmcLogDelete(obmcLogID);
167                 return;
168             }
169         }
170 
171         // Update System Info to Extended User Data
172         pel->updateSysInfoInExtendedUserDataSection(*_dataIface);
173 
174         // Check for severity 0x51 and update boot progress SRC
175         updateProgressSRC(pel);
176 
177         try
178         {
179             lg2::debug("Adding external PEL {ID} (BMC ID {BMCID}) to repo",
180                        "ID", lg2::hex, pel->id(), "BMCID", obmcLogID);
181             _repo.add(pel);
182 
183             if (_repo.sizeWarning())
184             {
185                 scheduleRepoPrune();
186             }
187 
188             // Activate any resulting service indicators if necessary
189             auto policy = service_indicators::getPolicy(*_dataIface);
190             policy->activate(*pel);
191         }
192         catch (const std::exception& e)
193         {
194             // Probably a full or r/o filesystem, not much we can do.
195             lg2::error("Unable to add PEL {ID} to Repository", "ID", lg2::hex,
196                        pel->id());
197         }
198 
199         updateEventId(pel);
200         updateResolution(*pel);
201         serializeLogEntry(obmcLogID);
202         createPELEntry(obmcLogID);
203 
204         // Check if firmware should quiesce system due to error
205         checkPelAndQuiesce(pel);
206     }
207     else
208     {
209         lg2::error("Invalid PEL received from the host. BMC ID = {ID}", "ID",
210                    obmcLogID);
211 
212         AdditionalData ad;
213         ad.add("PLID", getNumberString("0x%08X", pel->plid()));
214         ad.add("OBMC_LOG_ID", std::to_string(obmcLogID));
215         ad.add("PEL_SIZE", std::to_string(pelData.size()));
216 
217         std::string asciiString;
218         auto src = pel->primarySRC();
219         if (src)
220         {
221             asciiString = (*src)->asciiString();
222         }
223 
224         ad.add("SRC", asciiString);
225 
226         _eventLogger.log("org.open_power.Logging.Error.BadHostPEL",
227                          Entry::Level::Error, ad);
228 
229         // Save it to a file for debug in the lab.  Just keep the latest.
230         // Not adding it to the PEL because it could already be max size
231         // and don't want to truncate an already invalid PEL.
232         std::ofstream pelFile{getPELRepoPath() / "badPEL"};
233         pelFile.write(reinterpret_cast<const char*>(pelData.data()),
234                       pelData.size());
235 
236         // No need to keep around the openBMC event log entry
237         scheduleObmcLogDelete(obmcLogID);
238     }
239 }
240 
addESELPEL(const std::string & esel,uint32_t obmcLogID)241 void Manager::addESELPEL(const std::string& esel, uint32_t obmcLogID)
242 {
243     std::vector<uint8_t> data;
244 
245     lg2::debug("Adding PEL from ESEL. BMC ID = {ID}", "ID", obmcLogID);
246 
247     try
248     {
249         data = std::move(eselToRawData(esel));
250     }
251     catch (const std::exception& e)
252     {
253         // Try to add it below anyway, so it follows the usual bad data path.
254         lg2::error("Problems converting ESEL string to a byte vector");
255     }
256 
257     addPEL(data, obmcLogID);
258 }
259 
eselToRawData(const std::string & esel)260 std::vector<uint8_t> Manager::eselToRawData(const std::string& esel)
261 {
262     std::vector<uint8_t> data;
263     std::string byteString;
264 
265     // As the eSEL string looks like: "50 48 00 ab ..." there are 3
266     // characters per raw byte, and since the actual PEL data starts
267     // at the 16th byte, the code will grab the PEL data starting at
268     // offset 48 in the string.
269     static constexpr size_t pelStart = 16 * 3;
270 
271     if (esel.size() <= pelStart)
272     {
273         lg2::error("ESEL data too short, length = {LEN}", "LEN", esel.size());
274         throw std::length_error("ESEL data too short");
275     }
276 
277     for (size_t i = pelStart; i < esel.size(); i += 3)
278     {
279         if (i + 1 < esel.size())
280         {
281             byteString = esel.substr(i, 2);
282             data.push_back(std::stoi(byteString, nullptr, 16));
283         }
284         else
285         {
286             lg2::error("ESEL data too short, length = {LEN}", "LEN",
287                        esel.size());
288             throw std::length_error("ESEL data too short");
289         }
290     }
291 
292     return data;
293 }
294 
erase(uint32_t obmcLogID)295 void Manager::erase(uint32_t obmcLogID)
296 {
297     Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
298 
299     auto path = std::string(OBJ_ENTRY) + '/' + std::to_string(obmcLogID);
300     _pelEntries.erase(path);
301     _repo.remove(id);
302 }
303 
isDeleteProhibited(uint32_t)304 bool Manager::isDeleteProhibited(uint32_t /*obmcLogID*/)
305 {
306     return false;
307 }
308 
convertToPelFFDC(const FFDCEntries & ffdc)309 PelFFDC Manager::convertToPelFFDC(const FFDCEntries& ffdc)
310 {
311     PelFFDC pelFFDC;
312 
313     std::for_each(ffdc.begin(), ffdc.end(), [&pelFFDC](const auto& f) {
314         PelFFDCfile pf;
315         pf.subType = std::get<ffdcSubtypePos>(f);
316         pf.version = std::get<ffdcVersionPos>(f);
317         pf.fd = std::get<ffdcFDPos>(f);
318 
319         switch (std::get<ffdcFormatPos>(f))
320         {
321             case Create::FFDCFormat::JSON:
322                 pf.format = UserDataFormat::json;
323                 break;
324             case Create::FFDCFormat::CBOR:
325                 pf.format = UserDataFormat::cbor;
326                 break;
327             case Create::FFDCFormat::Text:
328                 pf.format = UserDataFormat::text;
329                 break;
330             case Create::FFDCFormat::Custom:
331                 pf.format = UserDataFormat::custom;
332                 break;
333         }
334 
335         pelFFDC.push_back(pf);
336     });
337 
338     return pelFFDC;
339 }
340 
createPEL(const std::string & message,uint32_t obmcLogID,uint64_t timestamp,phosphor::logging::Entry::Level severity,const std::vector<std::string> & additionalData,const std::vector<std::string> &,const FFDCEntries & ffdc)341 void Manager::createPEL(
342     const std::string& message, uint32_t obmcLogID, uint64_t timestamp,
343     phosphor::logging::Entry::Level severity,
344     const std::vector<std::string>& additionalData,
345     const std::vector<std::string>& /*associations*/, const FFDCEntries& ffdc)
346 {
347     auto entry = _registry.lookup(message, rg::LookupType::name);
348     auto pelFFDC = convertToPelFFDC(ffdc);
349     AdditionalData ad{additionalData};
350     std::string msg;
351 
352     if (!entry)
353     {
354         // Instead, get the default entry that means there is no
355         // other matching entry.  This error will still use the
356         // AdditionalData values of the original error, and this
357         // code will add the error message value that wasn't found
358         // to this AD.  This way, there will at least be a PEL,
359         // possibly with callouts, to allow users to debug the
360         // issue that caused the error even without its own PEL.
361         lg2::error("Event not found in PEL message registry: {MSG}", "MSG",
362                    message);
363 
364         entry = _registry.lookup(defaultLogMessage, rg::LookupType::name);
365         if (!entry)
366         {
367             lg2::error("Default event not found in PEL message registry");
368             return;
369         }
370 
371         ad.add(additional_data::error, message);
372     }
373 
374     auto pel = std::make_unique<openpower::pels::PEL>(
375         *entry, obmcLogID, timestamp, severity, ad, pelFFDC, *_dataIface,
376         *_journal);
377 
378     _repo.add(pel);
379 
380     if (_repo.sizeWarning())
381     {
382         scheduleRepoPrune();
383     }
384 
385     auto src = pel->primarySRC();
386     if (src)
387     {
388         auto asciiString = (*src)->asciiString();
389         while (asciiString.back() == ' ')
390         {
391             asciiString.pop_back();
392         }
393         lg2::info("Created PEL {ID} (BMC ID {BMCID}) with SRC {SRC}", "ID",
394                   lg2::hex, pel->id(), "BMCID", pel->obmcLogID(), "SRC",
395                   asciiString);
396     }
397 
398     // Check for severity 0x51 and update boot progress SRC
399     updateProgressSRC(pel);
400 
401     // Activate any resulting service indicators if necessary
402     auto policy = service_indicators::getPolicy(*_dataIface);
403     policy->activate(*pel);
404 
405     updateDBusSeverity(*pel);
406     updateEventId(pel);
407     updateResolution(*pel);
408     serializeLogEntry(obmcLogID);
409     createPELEntry(obmcLogID);
410 
411     // Check if firmware should quiesce system due to error
412     checkPelAndQuiesce(pel);
413 }
414 
getPEL(uint32_t pelID)415 sdbusplus::message::unix_fd Manager::getPEL(uint32_t pelID)
416 {
417     Repository::LogID id{Repository::LogID::Pel(pelID)};
418     std::optional<int> fd;
419 
420     lg2::debug("getPEL {ID}", "ID", lg2::hex, pelID);
421 
422     try
423     {
424         fd = _repo.getPELFD(id);
425     }
426     catch (const std::exception& e)
427     {
428         throw common_error::InternalFailure();
429     }
430 
431     if (!fd)
432     {
433         throw common_error::InvalidArgument();
434     }
435 
436     scheduleFDClose(*fd);
437 
438     return *fd;
439 }
440 
scheduleFDClose(int fd)441 void Manager::scheduleFDClose(int fd)
442 {
443     _fdCloserEventSource = std::make_unique<sdeventplus::source::Defer>(
444         _event, std::bind(std::mem_fn(&Manager::closeFD), this, fd,
445                           std::placeholders::_1));
446 }
447 
closeFD(int fd,sdeventplus::source::EventBase &)448 void Manager::closeFD(int fd, sdeventplus::source::EventBase& /*source*/)
449 {
450     close(fd);
451     _fdCloserEventSource.reset();
452 }
453 
getPELFromOBMCID(uint32_t obmcLogID)454 std::vector<uint8_t> Manager::getPELFromOBMCID(uint32_t obmcLogID)
455 {
456     Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
457     std::optional<std::vector<uint8_t>> data;
458 
459     lg2::debug("getPELFromOBMCID  {BMCID}", "BMCID", obmcLogID);
460 
461     try
462     {
463         data = _repo.getPELData(id);
464     }
465     catch (const std::exception& e)
466     {
467         throw common_error::InternalFailure();
468     }
469 
470     if (!data)
471     {
472         throw common_error::InvalidArgument();
473     }
474 
475     return *data;
476 }
477 
hostAck(uint32_t pelID)478 void Manager::hostAck(uint32_t pelID)
479 {
480     Repository::LogID id{Repository::LogID::Pel(pelID)};
481 
482     lg2::debug("HostHack {ID}", "ID", lg2::hex, pelID);
483 
484     if (!_repo.hasPEL(id))
485     {
486         throw common_error::InvalidArgument();
487     }
488 
489     if (_hostNotifier)
490     {
491         _hostNotifier->ackPEL(pelID);
492     }
493 }
494 
hostReject(uint32_t pelID,RejectionReason reason)495 void Manager::hostReject(uint32_t pelID, RejectionReason reason)
496 {
497     Repository::LogID id{Repository::LogID::Pel(pelID)};
498 
499     lg2::debug("HostReject {ID}, reason = {REASON}", "ID", lg2::hex, pelID,
500                "REASON", reason);
501 
502     if (!_repo.hasPEL(id))
503     {
504         throw common_error::InvalidArgument();
505     }
506 
507     if (reason == RejectionReason::BadPEL)
508     {
509         AdditionalData data;
510         data.add("BAD_ID", getNumberString("0x%08X", pelID));
511         _eventLogger.log("org.open_power.Logging.Error.SentBadPELToHost",
512                          Entry::Level::Informational, data);
513         if (_hostNotifier)
514         {
515             _hostNotifier->setBadPEL(pelID);
516         }
517     }
518     else if ((reason == RejectionReason::HostFull) && _hostNotifier)
519     {
520         _hostNotifier->setHostFull(pelID);
521     }
522 }
523 
scheduleRepoPrune()524 void Manager::scheduleRepoPrune()
525 {
526     _repoPrunerEventSource = std::make_unique<sdeventplus::source::Defer>(
527         _event, std::bind(std::mem_fn(&Manager::pruneRepo), this,
528                           std::placeholders::_1));
529 }
530 
pruneRepo(sdeventplus::source::EventBase &)531 void Manager::pruneRepo(sdeventplus::source::EventBase& /*source*/)
532 {
533     auto idsWithHwIsoEntry = _dataIface->getLogIDWithHwIsolation();
534 
535     auto idsToDelete = _repo.prune(idsWithHwIsoEntry);
536 
537     // Remove the OpenBMC event logs for the PELs that were just removed.
538     std::for_each(idsToDelete.begin(), idsToDelete.end(),
539                   [this](auto id) { this->_logManager.erase(id); });
540 
541     _repoPrunerEventSource.reset();
542 }
543 
setupPELDeleteWatch()544 void Manager::setupPELDeleteWatch()
545 {
546     _pelFileDeleteFD = inotify_init1(IN_NONBLOCK);
547     if (-1 == _pelFileDeleteFD)
548     {
549         auto e = errno;
550         lg2::error("inotify_init1 failed with errno {ERRNO}", "ERRNO", e);
551         abort();
552     }
553 
554     _pelFileDeleteWatchFD = inotify_add_watch(
555         _pelFileDeleteFD, _repo.repoPath().c_str(), IN_DELETE);
556     if (-1 == _pelFileDeleteWatchFD)
557     {
558         auto e = errno;
559         lg2::error("inotify_add_watch failed with errno {ERRNO}", "ERRNO", e);
560         abort();
561     }
562 
563     _pelFileDeleteEventSource = std::make_unique<sdeventplus::source::IO>(
564         _event, _pelFileDeleteFD, EPOLLIN,
565         std::bind(std::mem_fn(&Manager::pelFileDeleted), this,
566                   std::placeholders::_1, std::placeholders::_2,
567                   std::placeholders::_3));
568 }
569 
pelFileDeleted(sdeventplus::source::IO &,int,uint32_t revents)570 void Manager::pelFileDeleted(sdeventplus::source::IO& /*io*/, int /*fd*/,
571                              uint32_t revents)
572 {
573     if (!(revents & EPOLLIN))
574     {
575         return;
576     }
577 
578     // An event for 1 PEL uses 48B. When all PELs are deleted at once,
579     // as many events as there is room for can be handled in one callback.
580     // A size of 2000 will allow 41 to be processed, with additional
581     // callbacks being needed to process the remaining ones.
582     std::array<uint8_t, 2000> data{};
583     auto bytesRead = read(_pelFileDeleteFD, data.data(), data.size());
584     if (bytesRead < 0)
585     {
586         auto e = errno;
587         lg2::error("Failed reading data from inotify event, errno = {ERRNO}",
588                    "ERRNO", e);
589         abort();
590     }
591 
592     auto offset = 0;
593     while (offset < bytesRead)
594     {
595         auto event = reinterpret_cast<inotify_event*>(&data[offset]);
596         if (event->mask & IN_DELETE)
597         {
598             std::string filename{event->name};
599 
600             // Get the PEL ID from the filename and tell the
601             // repo it's been removed, and then delete the BMC
602             // event log if it's there.
603             auto pos = filename.find_first_of('_');
604             if (pos != std::string::npos)
605             {
606                 try
607                 {
608                     auto idString = filename.substr(pos + 1);
609                     auto pelID = std::stoul(idString, nullptr, 16);
610 
611                     Repository::LogID id{Repository::LogID::Pel(pelID)};
612                     auto removedLogID = _repo.remove(id);
613                     if (removedLogID)
614                     {
615                         _logManager.erase(removedLogID->obmcID.id);
616                     }
617                 }
618                 catch (const std::exception& e)
619                 {
620                     lg2::info("Could not find PEL ID from its filename {NAME}",
621                               "NAME", filename);
622                 }
623             }
624         }
625 
626         offset += offsetof(inotify_event, name) + event->len;
627     }
628 }
629 
createPELWithFFDCFiles(std::string message,Entry::Level severity,std::map<std::string,std::string> additionalData,std::vector<std::tuple<sdbusplus::server::xyz::openbmc_project::logging::Create::FFDCFormat,uint8_t,uint8_t,sdbusplus::message::unix_fd>> fFDC)630 std::tuple<uint32_t, uint32_t> Manager::createPELWithFFDCFiles(
631     std::string message, Entry::Level severity,
632     std::map<std::string, std::string> additionalData,
633     std::vector<std::tuple<
634         sdbusplus::server::xyz::openbmc_project::logging::Create::FFDCFormat,
635         uint8_t, uint8_t, sdbusplus::message::unix_fd>>
636         fFDC)
637 {
638     _logManager.create(message, severity, additionalData, fFDC);
639 
640     return {_logManager.lastEntryID(), _repo.lastPelID()};
641 }
642 
getPELJSON(uint32_t obmcLogID)643 std::string Manager::getPELJSON(uint32_t obmcLogID)
644 {
645     // Throws InvalidArgument if not found
646     auto pelID = getPELIdFromBMCLogId(obmcLogID);
647 
648     auto cmd = std::format("/usr/bin/peltool -i {:#x}", pelID);
649 
650     FILE* pipe = popen(cmd.c_str(), "r");
651     if (!pipe)
652     {
653         lg2::error("Error running cmd: {CMD}", "CMD", cmd);
654         throw common_error::InternalFailure();
655     }
656 
657     std::string output;
658     std::array<char, 1024> buffer;
659     while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
660     {
661         output.append(buffer.data());
662     }
663 
664     int rc = pclose(pipe);
665     if (WEXITSTATUS(rc) != 0)
666     {
667         lg2::error("Error running cmd: {CMD}, rc = {RC}", "CMD", cmd, "RC", rc);
668         throw common_error::InternalFailure();
669     }
670 
671     return output;
672 }
673 
checkPelAndQuiesce(std::unique_ptr<openpower::pels::PEL> & pel)674 void Manager::checkPelAndQuiesce(std::unique_ptr<openpower::pels::PEL>& pel)
675 {
676     if ((pel->userHeader().severity() ==
677          static_cast<uint8_t>(SeverityType::nonError)) ||
678         (pel->userHeader().severity() ==
679          static_cast<uint8_t>(SeverityType::recovered)))
680     {
681         lg2::debug(
682             "PEL severity informational or recovered. no quiesce needed");
683         return;
684     }
685     if (!_logManager.isQuiesceOnErrorEnabled())
686     {
687         lg2::debug("QuiesceOnHwError not enabled, no quiesce needed");
688         return;
689     }
690 
691     CreatorID creatorID{pel->privateHeader().creatorID()};
692 
693     if ((creatorID != CreatorID::openBMC) &&
694         (creatorID != CreatorID::hostboot) &&
695         (creatorID != CreatorID::ioDrawer) && (creatorID != CreatorID::occ) &&
696         (creatorID != CreatorID::phyp))
697     {
698         return;
699     }
700 
701     // Now check if it has any type of callout
702     if (pel->isHwCalloutPresent())
703     {
704         lg2::info(
705             "QuiesceOnHwError enabled, PEL severity not nonError or recovered, "
706             "and callout is present");
707 
708         _logManager.quiesceOnError(pel->obmcLogID());
709     }
710 }
711 
getEventId(const openpower::pels::PEL & pel) const712 std::string Manager::getEventId(const openpower::pels::PEL& pel) const
713 {
714     std::string str;
715     auto src = pel.primarySRC();
716     if (src)
717     {
718         const auto& hexwords = (*src)->hexwordData();
719 
720         std::string refcode = (*src)->asciiString();
721         size_t pos = refcode.find_last_not_of(0x20);
722         if (pos != std::string::npos)
723         {
724             refcode.erase(pos + 1);
725         }
726         str = refcode;
727 
728         for (auto& value : hexwords)
729         {
730             str += " ";
731             str += getNumberString("%08X", value);
732         }
733     }
734     return sanitizeFieldForDBus(str);
735 }
736 
updateEventId(std::unique_ptr<openpower::pels::PEL> & pel)737 void Manager::updateEventId(std::unique_ptr<openpower::pels::PEL>& pel)
738 {
739     std::string eventIdStr = getEventId(*pel);
740 
741     auto entryN = _logManager.entries.find(pel->obmcLogID());
742     if (entryN != _logManager.entries.end())
743     {
744         entryN->second->eventId(eventIdStr, true);
745     }
746 }
747 
sanitizeFieldForDBus(std::string field)748 std::string Manager::sanitizeFieldForDBus(std::string field)
749 {
750     std::for_each(field.begin(), field.end(), [](char& ch) {
751         if (((ch < ' ') || (ch > '~')) && (ch != '\n') && (ch != '\t'))
752         {
753             ch = ' ';
754         }
755     });
756     return field;
757 }
758 
getResolution(const openpower::pels::PEL & pel) const759 std::string Manager::getResolution(const openpower::pels::PEL& pel) const
760 {
761     std::string str;
762     std::string resolution;
763     auto src = pel.primarySRC();
764     if (src)
765     {
766         // First extract the callout pointer and then go through
767         const auto& callouts = (*src)->callouts();
768         namespace pv = openpower::pels::pel_values;
769         // All PELs dont have callout, check before parsing callout data
770         if (callouts)
771         {
772             const auto& entries = callouts->callouts();
773             // Entry starts with index 1
774             uint8_t index = 1;
775             for (auto& entry : entries)
776             {
777                 resolution += std::to_string(index) + ". ";
778                 // Adding Location code to resolution
779                 if (!entry->locationCode().empty())
780                     resolution += "Location Code: " + entry->locationCode() +
781                                   ", ";
782                 if (entry->fruIdentity())
783                 {
784                     // Get priority and set the resolution string
785                     str = pv::getValue(entry->priority(),
786                                        pel_values::calloutPriorityValues,
787                                        pel_values::registryNamePos);
788                     str[0] = toupper(str[0]);
789                     resolution += "Priority: " + str + ", ";
790                     if (entry->fruIdentity()->getPN().has_value())
791                     {
792                         resolution +=
793                             "PN: " + entry->fruIdentity()->getPN().value() +
794                             ", ";
795                     }
796                     if (entry->fruIdentity()->getSN().has_value())
797                     {
798                         resolution +=
799                             "SN: " + entry->fruIdentity()->getSN().value() +
800                             ", ";
801                     }
802                     if (entry->fruIdentity()->getCCIN().has_value())
803                     {
804                         resolution +=
805                             "CCIN: " + entry->fruIdentity()->getCCIN().value() +
806                             ", ";
807                     }
808                     // Add the maintenance procedure
809                     if (entry->fruIdentity()->getMaintProc().has_value())
810                     {
811                         resolution +=
812                             "Procedure: " +
813                             entry->fruIdentity()->getMaintProc().value() + ", ";
814                     }
815                 }
816                 resolution.resize(resolution.size() - 2);
817                 resolution += "\n";
818                 index++;
819             }
820         }
821     }
822     return sanitizeFieldForDBus(resolution);
823 }
824 
updateResolution(const openpower::pels::PEL & pel)825 bool Manager::updateResolution(const openpower::pels::PEL& pel)
826 {
827     std::string callouts = getResolution(pel);
828     auto entryN = _logManager.entries.find(pel.obmcLogID());
829     if (entryN != _logManager.entries.end())
830     {
831         entryN->second->resolution(callouts, true);
832     }
833 
834     return false;
835 }
836 
serializeLogEntry(uint32_t obmcLogID)837 void Manager::serializeLogEntry(uint32_t obmcLogID)
838 {
839     auto entryN = _logManager.entries.find(obmcLogID);
840     if (entryN != _logManager.entries.end())
841     {
842         serialize(*entryN->second);
843     }
844 }
845 
updateDBusSeverity(const openpower::pels::PEL & pel)846 void Manager::updateDBusSeverity(const openpower::pels::PEL& pel)
847 {
848     // The final severity of the PEL may not agree with the
849     // original severity of the D-Bus event log.  Update the
850     // D-Bus property to match in some cases.  This is to
851     // ensure there isn't a Critical or Warning Redfish event
852     // log for an informational or recovered PEL (or vice versa).
853     // This doesn't make an explicit call to serialize the new
854     // event log property value because updateEventId() is called
855     // right after this and will do it.
856     auto sevType =
857         static_cast<SeverityType>(pel.userHeader().severity() & 0xF0);
858 
859     auto entryN = _logManager.entries.find(pel.obmcLogID());
860     if (entryN != _logManager.entries.end())
861     {
862         auto newSeverity =
863             fixupLogSeverity(entryN->second->severity(), sevType);
864         if (newSeverity)
865         {
866             lg2::info("Changing event log {ID} severity from {OLD} "
867                       "to {NEW} to match PEL",
868                       "ID", lg2::hex, entryN->second->id(), "OLD",
869                       Entry::convertLevelToString(entryN->second->severity()),
870                       "NEW", Entry::convertLevelToString(*newSeverity));
871 
872             entryN->second->severity(*newSeverity, true);
873         }
874     }
875 }
876 
setEntryPath(uint32_t obmcLogID)877 void Manager::setEntryPath(uint32_t obmcLogID)
878 {
879     Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
880     if (auto attributes = _repo.getPELAttributes(id); attributes)
881     {
882         auto& attr = attributes.value().get();
883         auto entry = _logManager.entries.find(obmcLogID);
884         if (entry != _logManager.entries.end())
885         {
886             entry->second->path(attr.path, true);
887         }
888     }
889 }
890 
setServiceProviderNotifyFlag(uint32_t obmcLogID)891 void Manager::setServiceProviderNotifyFlag(uint32_t obmcLogID)
892 {
893     Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
894     if (auto attributes = _repo.getPELAttributes(id); attributes)
895     {
896         auto& attr = attributes.value().get();
897         auto entry = _logManager.entries.find(obmcLogID);
898         if (entry != _logManager.entries.end())
899         {
900             if (attr.actionFlags.test(callHomeFlagBit))
901             {
902                 entry->second->serviceProviderNotify(Entry::Notify::Notify,
903                                                      true);
904             }
905             else
906             {
907                 entry->second->serviceProviderNotify(Entry::Notify::Inhibit,
908                                                      true);
909             }
910         }
911     }
912 }
913 
createPELEntry(uint32_t obmcLogID,bool skipIaSignal)914 void Manager::createPELEntry(uint32_t obmcLogID, bool skipIaSignal)
915 {
916     std::map<std::string, PropertiesVariant> varData;
917     Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
918     if (auto attributes = _repo.getPELAttributes(id); attributes)
919     {
920         namespace pv = openpower::pels::pel_values;
921         auto& attr = attributes.value().get();
922 
923         // get the hidden flag values
924         auto sevType = static_cast<SeverityType>(attr.severity & 0xF0);
925         auto isHidden = true;
926         if (((sevType != SeverityType::nonError) &&
927              attr.actionFlags.test(reportFlagBit) &&
928              !attr.actionFlags.test(hiddenFlagBit)) ||
929             ((sevType == SeverityType::nonError) &&
930              attr.actionFlags.test(serviceActionFlagBit)))
931         {
932             isHidden = false;
933         }
934         varData.emplace(std::string("Hidden"), isHidden);
935         varData.emplace(
936             std::string("Subsystem"),
937             pv::getValue(attr.subsystem, pel_values::subsystemValues));
938 
939         varData.emplace(
940             std::string("ManagementSystemAck"),
941             (attr.hmcState == TransmissionState::acked ? true : false));
942 
943         varData.emplace("PlatformLogID", attr.plid);
944         varData.emplace("Deconfig", attr.deconfig);
945         varData.emplace("Guard", attr.guard);
946         varData.emplace("Timestamp", attr.creationTime);
947 
948         // Path to create PELEntry Interface is same as PEL
949         auto path = std::string(OBJ_ENTRY) + '/' + std::to_string(obmcLogID);
950         // Create Interface for PELEntry and set properties
951         auto pelEntry = std::make_unique<PELEntry>(_logManager.getBus(), path,
952                                                    varData, obmcLogID, &_repo);
953         if (!skipIaSignal)
954         {
955             pelEntry->emit_added();
956         }
957         _pelEntries.emplace(std::move(path), std::move(pelEntry));
958     }
959 }
960 
getPELIdFromBMCLogId(uint32_t bmcLogId)961 uint32_t Manager::getPELIdFromBMCLogId(uint32_t bmcLogId)
962 {
963     Repository::LogID id{Repository::LogID::Obmc(bmcLogId)};
964     if (auto logId = _repo.getLogID(id); !logId.has_value())
965     {
966         throw common_error::InvalidArgument();
967     }
968     else
969     {
970         return logId->pelID.id;
971     }
972 }
973 
getBMCLogIdFromPELId(uint32_t pelId)974 uint32_t Manager::getBMCLogIdFromPELId(uint32_t pelId)
975 {
976     Repository::LogID id{Repository::LogID::Pel(pelId)};
977     if (auto logId = _repo.getLogID(id); !logId.has_value())
978     {
979         throw common_error::InvalidArgument();
980     }
981     else
982     {
983         return logId->obmcID.id;
984     }
985 }
986 
updateProgressSRC(std::unique_ptr<openpower::pels::PEL> & pel) const987 void Manager::updateProgressSRC(
988     std::unique_ptr<openpower::pels::PEL>& pel) const
989 {
990     // Check for pel severity of type - 0x51 = critical error, system
991     // termination
992     if (pel->userHeader().severity() == 0x51)
993     {
994         auto src = pel->primarySRC();
995         if (src)
996         {
997             std::vector<uint8_t> asciiSRC = (*src)->getSrcStruct();
998             uint64_t srcRefCode = 0;
999 
1000             // Read bytes from offset [40-47] e.g. BD8D1001
1001             for (int i = 0; i < 8; i++)
1002             {
1003                 srcRefCode |=
1004                     (static_cast<uint64_t>(asciiSRC[40 + i]) << (8 * i));
1005             }
1006 
1007             try
1008             {
1009                 _dataIface->createProgressSRC(srcRefCode, asciiSRC);
1010             }
1011             catch (const std::exception&)
1012             {
1013                 // Exception - may be no boot progress interface on dbus
1014             }
1015         }
1016     }
1017 }
1018 
scheduleObmcLogDelete(uint32_t obmcLogID)1019 void Manager::scheduleObmcLogDelete(uint32_t obmcLogID)
1020 {
1021     _obmcLogDeleteEventSource = std::make_unique<sdeventplus::source::Defer>(
1022         _event, std::bind(std::mem_fn(&Manager::deleteObmcLog), this,
1023                           std::placeholders::_1, obmcLogID));
1024 }
1025 
deleteObmcLog(sdeventplus::source::EventBase &,uint32_t obmcLogID)1026 void Manager::deleteObmcLog(sdeventplus::source::EventBase&, uint32_t obmcLogID)
1027 {
1028     lg2::info("Removing event log with no PEL: {BMCID}", "BMCID", obmcLogID);
1029     _logManager.erase(obmcLogID);
1030     _obmcLogDeleteEventSource.reset();
1031 }
1032 
clearPowerThermalDeconfigFlag(const std::string & locationCode,openpower::pels::PEL & pel)1033 bool Manager::clearPowerThermalDeconfigFlag(const std::string& locationCode,
1034                                             openpower::pels::PEL& pel)
1035 {
1036     // The requirements state that only power-thermal or
1037     // fan PELs need their deconfig flag cleared.
1038     static const std::vector<uint32_t> compIDs{bmcThermalCompID, bmcFansCompID};
1039 
1040     if (std::find(compIDs.begin(), compIDs.end(),
1041                   pel.privateHeader().header().componentID) == compIDs.end())
1042     {
1043         return false;
1044     }
1045 
1046     auto src = pel.primarySRC();
1047     const auto& callouts = (*src)->callouts();
1048     if (!callouts)
1049     {
1050         return false;
1051     }
1052 
1053     for (const auto& callout : callouts->callouts())
1054     {
1055         // Look for the passed in location code in a callout that
1056         // is either a normal HW callout or a symbolic FRU with
1057         // a trusted location code callout.
1058         if ((callout->locationCode() != locationCode) ||
1059             !callout->fruIdentity())
1060         {
1061             continue;
1062         }
1063 
1064         if ((callout->fruIdentity()->failingComponentType() !=
1065              src::FRUIdentity::hardwareFRU) &&
1066             (callout->fruIdentity()->failingComponentType() !=
1067              src::FRUIdentity::symbolicFRUTrustedLocCode))
1068         {
1069             continue;
1070         }
1071 
1072         lg2::info(
1073             "Clearing deconfig flag in PEL {ID} with SRC {SRC} because {LOC} was replaced",
1074             "ID", lg2::hex, pel.id(), "SRC", (*src)->asciiString().substr(0, 8),
1075             "LOC", locationCode);
1076         (*src)->clearErrorStatusFlag(SRC::ErrorStatusFlags::deconfigured);
1077         return true;
1078     }
1079     return false;
1080 }
1081 
hardwarePresent(const std::string & locationCode)1082 void Manager::hardwarePresent(const std::string& locationCode)
1083 {
1084     Repository::PELUpdateFunc handlePowerThermalHardwarePresent =
1085         [locationCode](openpower::pels::PEL& pel) {
1086             return Manager::clearPowerThermalDeconfigFlag(locationCode, pel);
1087         };
1088 
1089     // If the PEL was created by the BMC and has the deconfig flag set,
1090     // it's a candidate to have the deconfig flag cleared.
1091     for (const auto& [id, attributes] : _repo.getAttributesMap())
1092     {
1093         if ((attributes.creator == static_cast<uint8_t>(CreatorID::openBMC)) &&
1094             attributes.deconfig)
1095         {
1096             auto updated = _repo.updatePEL(attributes.path,
1097                                            handlePowerThermalHardwarePresent);
1098 
1099             if (updated)
1100             {
1101                 // Also update the property on D-Bus
1102                 auto objPath = std::string(OBJ_ENTRY) + '/' +
1103                                std::to_string(id.obmcID.id);
1104                 auto entryN = _pelEntries.find(objPath);
1105                 if (entryN != _pelEntries.end())
1106                 {
1107                     entryN->second->deconfig(false);
1108                 }
1109                 else
1110                 {
1111                     lg2::error(
1112                         "Could not find PEL Entry D-Bus object for {PATH}",
1113                         "PATH", objPath);
1114                 }
1115             }
1116         }
1117     }
1118 }
1119 
1120 } // namespace pels
1121 } // namespace openpower
1122