blob: 26944995de9717616a42659e229579f790a93b9e [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
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 */
Matt Spinler4e8078c2019-07-09 13:22:32 -050016#include "manager.hpp"
17
18#include "additional_data.hpp"
Matt Spinler3387eac2023-07-06 14:56:02 -050019#include "elog_serialize.hpp"
Matt Spinler05c2c6c2019-12-18 14:02:09 -060020#include "json_utils.hpp"
Matt Spinler89fa0822019-07-17 13:54:30 -050021#include "pel.hpp"
Vijay Lobo2fb10212021-08-22 23:24:16 -050022#include "pel_entry.hpp"
Matt Spinler1962e082020-08-05 13:44:53 -050023#include "service_indicators.hpp"
Matt Spinler8b81ec02022-07-12 13:25:37 -050024#include "severity.hpp"
Matt Spinler89fa0822019-07-17 13:54:30 -050025
Matt Spinlerff9cec22020-07-15 13:06:35 -050026#include <sys/inotify.h>
Matt Spinler6b1a5c82020-01-07 08:48:53 -060027#include <unistd.h>
28
Matt Spinlerfbdfc762023-06-30 15:15:44 -050029#include <phosphor-logging/lg2.hpp>
Patrick Williams2544b412022-10-04 08:41:06 -050030#include <xyz/openbmc_project/Common/error.hpp>
31#include <xyz/openbmc_project/Logging/Create/server.hpp>
32
Matt Spinler89fa0822019-07-17 13:54:30 -050033#include <filesystem>
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050034#include <format>
Matt Spinler89fa0822019-07-17 13:54:30 -050035#include <fstream>
Matt Spinler0003af12022-06-08 10:46:17 -050036#include <locale>
Matt Spinler4e8078c2019-07-09 13:22:32 -050037
38namespace openpower
39{
40namespace pels
41{
42
43using namespace phosphor::logging;
Matt Spinler89fa0822019-07-17 13:54:30 -050044namespace fs = std::filesystem;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080045namespace rg = openpower::pels::message;
Matt Spinler4e8078c2019-07-09 13:22:32 -050046
Matt Spinlera34ab722019-12-16 10:39:32 -060047namespace common_error = sdbusplus::xyz::openbmc_project::Common::Error;
48
Willy Tu6ddbf692023-09-05 10:54:16 -070049using Create = sdbusplus::server::xyz::openbmc_project::logging::Create;
Matt Spinler56ad2a02020-03-26 14:00:52 -050050
Matt Spinler4e8078c2019-07-09 13:22:32 -050051namespace additional_data
52{
53constexpr auto rawPEL = "RAWPEL";
Matt Spinler19e72902020-01-24 11:05:20 -060054constexpr auto esel = "ESEL";
Matt Spinler30ddc9f2020-07-16 15:39:59 -050055constexpr auto error = "ERROR_NAME";
Matt Spinler19e72902020-01-24 11:05:20 -060056} // namespace additional_data
Matt Spinler4e8078c2019-07-09 13:22:32 -050057
Matt Spinler30ddc9f2020-07-16 15:39:59 -050058constexpr auto defaultLogMessage = "xyz.openbmc_project.Logging.Error.Default";
Matt Spinler0dd22c82023-05-04 15:28:12 -050059constexpr uint32_t bmcThermalCompID = 0x2700;
60constexpr uint32_t bmcFansCompID = 0x2800;
Matt Spinler30ddc9f2020-07-16 15:39:59 -050061
Matt Spinlerff9cec22020-07-15 13:06:35 -050062Manager::~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
Matt Spinler4e8078c2019-07-09 13:22:32 -050074void Manager::create(const std::string& message, uint32_t obmcLogID,
75 uint64_t timestamp, Entry::Level severity,
Patrick Williamse5940632024-11-22 20:47:58 -050076 const std::map<std::string, std::string>& additionalData,
Matt Spinler56ad2a02020-03-26 14:00:52 -050077 const std::vector<std::string>& associations,
78 const FFDCEntries& ffdc)
Matt Spinler4e8078c2019-07-09 13:22:32 -050079{
80 AdditionalData ad{additionalData};
81
Matt Spinler19e72902020-01-24 11:05:20 -060082 // If a PEL was passed in via a filename or in an ESEL,
83 // use that. Otherwise, create one.
Matt Spinler4e8078c2019-07-09 13:22:32 -050084 auto rawPelPath = ad.getValue(additional_data::rawPEL);
85 if (rawPelPath)
86 {
87 addRawPEL(*rawPelPath, obmcLogID);
88 }
89 else
90 {
Matt Spinler19e72902020-01-24 11:05:20 -060091 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,
Matt Spinler56ad2a02020-03-26 14:00:52 -050099 associations, ffdc);
Matt Spinler19e72902020-01-24 11:05:20 -0600100 }
Matt Spinler4e8078c2019-07-09 13:22:32 -0500101 }
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600102
103 setEntryPath(obmcLogID);
Vijay Lobocbc93a42021-05-20 19:04:07 -0500104 setServiceProviderNotifyFlag(obmcLogID);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500105}
106
107void Manager::addRawPEL(const std::string& rawPelPath, uint32_t obmcLogID)
108{
Matt Spinler89fa0822019-07-17 13:54:30 -0500109 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 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500117 lg2::error(
118 "Filesystem error reading a raw PEL. File = {FILE}, obmcLogID = {LOGID}",
119 "FILE", rawPelPath, "LOGID", obmcLogID);
Matt Spinler89fa0822019-07-17 13:54:30 -0500120 // TODO, Decide what to do here. Maybe nothing.
121 return;
122 }
123
124 file.close();
125
Matt Spinler19e72902020-01-24 11:05:20 -0600126 addPEL(data, obmcLogID);
Matt Spinler67416922021-07-19 12:34:57 -0600127
128 std::error_code ec;
129 fs::remove(rawPelPath, ec);
Matt Spinler89fa0822019-07-17 13:54:30 -0500130 }
131 else
132 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500133 lg2::error(
134 "Raw PEL file from BMC event log does not exit. File = {FILE}, obmcLogID = {LOGID}",
135 "FILE", rawPelPath, "LOGID", obmcLogID);
Matt Spinler89fa0822019-07-17 13:54:30 -0500136 }
Matt Spinler4e8078c2019-07-09 13:22:32 -0500137}
138
Matt Spinler19e72902020-01-24 11:05:20 -0600139void Manager::addPEL(std::vector<uint8_t>& pelData, uint32_t obmcLogID)
140{
Matt Spinler19e72902020-01-24 11:05:20 -0600141 auto pel = std::make_unique<openpower::pels::PEL>(pelData, obmcLogID);
142 if (pel->valid())
143 {
Sumit Kumar8ec41562021-10-29 05:39:37 -0500144 // PELs created by others still need this field set by us.
145 pel->setCommitTime();
146
Sumit Kumara1e40842021-06-23 09:52:25 -0500147 // Assign Id other than to Hostbot PEL
148 if ((pel->privateHeader()).creatorID() !=
149 static_cast<uint8_t>(CreatorID::hostboot))
150 {
151 pel->assignID();
152 }
Sumit Kumar2ccdcef2021-07-31 10:04:58 -0500153 else
154 {
155 const Repository::LogID id{Repository::LogID::Pel(pel->id())};
156 auto result = _repo.hasPEL(id);
157 if (result)
158 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500159 lg2::warning(
160 "Duplicate HostBoot PEL ID {ID} found, moving it to archive folder",
161 "ID", lg2::hex, pel->id());
Sumit Kumar2ccdcef2021-07-31 10:04:58 -0500162
163 _repo.archivePEL(*pel);
Matt Spinlerd8fb5ba2022-01-25 13:01:14 -0600164
165 // No need to keep around the openBMC event log entry
166 scheduleObmcLogDelete(obmcLogID);
Sumit Kumar2ccdcef2021-07-31 10:04:58 -0500167 return;
168 }
169 }
Sumit Kumara1e40842021-06-23 09:52:25 -0500170
Sumit Kumar3160a542021-04-26 08:07:04 -0500171 // Update System Info to Extended User Data
172 pel->updateSysInfoInExtendedUserDataSection(*_dataIface);
173
Sumit Kumar3e274432021-09-14 06:37:56 -0500174 // Check for severity 0x51 and update boot progress SRC
175 updateProgressSRC(pel);
176
Matt Spinler19e72902020-01-24 11:05:20 -0600177 try
178 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500179 lg2::debug("Adding external PEL {ID} (BMC ID {BMCID}) to repo",
180 "ID", lg2::hex, pel->id(), "BMCID", obmcLogID);
Matt Spinler19e72902020-01-24 11:05:20 -0600181 _repo.add(pel);
Matt Spinler7e727a32020-07-07 15:00:17 -0500182
183 if (_repo.sizeWarning())
184 {
185 scheduleRepoPrune();
186 }
Matt Spinler1962e082020-08-05 13:44:53 -0500187
188 // Activate any resulting service indicators if necessary
189 auto policy = service_indicators::getPolicy(*_dataIface);
190 policy->activate(*pel);
Matt Spinler19e72902020-01-24 11:05:20 -0600191 }
Patrick Williams66491c62021-10-06 12:23:37 -0500192 catch (const std::exception& e)
Matt Spinler19e72902020-01-24 11:05:20 -0600193 {
194 // Probably a full or r/o filesystem, not much we can do.
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500195 lg2::error("Unable to add PEL {ID} to Repository", "ID", lg2::hex,
196 pel->id());
Matt Spinler19e72902020-01-24 11:05:20 -0600197 }
Andrew Geissler44fc3162020-07-09 09:21:31 -0500198
Vijay Lobod354a392021-06-01 16:21:02 -0500199 updateEventId(pel);
Matt Spinler28d6ae22022-03-18 11:18:27 -0500200 updateResolution(*pel);
Matt Spinler3387eac2023-07-06 14:56:02 -0500201 serializeLogEntry(obmcLogID);
Vijay Loboafb1b462021-07-21 23:29:13 -0500202 createPELEntry(obmcLogID);
Matt Spinlerdf5cb832022-07-12 12:47:26 -0500203
204 // Check if firmware should quiesce system due to error
205 checkPelAndQuiesce(pel);
Matt Spinler19e72902020-01-24 11:05:20 -0600206 }
207 else
208 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500209 lg2::error("Invalid PEL received from the host. BMC ID = {ID}", "ID",
210 obmcLogID);
Matt Spinler19e72902020-01-24 11:05:20 -0600211
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);
Matt Spinlerfe721892020-04-02 10:28:08 -0500228
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());
Matt Spinlerd8fb5ba2022-01-25 13:01:14 -0600235
236 // No need to keep around the openBMC event log entry
237 scheduleObmcLogDelete(obmcLogID);
Matt Spinler19e72902020-01-24 11:05:20 -0600238 }
239}
240
241void Manager::addESELPEL(const std::string& esel, uint32_t obmcLogID)
242{
243 std::vector<uint8_t> data;
244
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500245 lg2::debug("Adding PEL from ESEL. BMC ID = {ID}", "ID", obmcLogID);
Matt Spinler5f5352e2020-03-05 16:23:27 -0600246
Matt Spinler19e72902020-01-24 11:05:20 -0600247 try
248 {
249 data = std::move(eselToRawData(esel));
250 }
Patrick Williams66491c62021-10-06 12:23:37 -0500251 catch (const std::exception& e)
Matt Spinler19e72902020-01-24 11:05:20 -0600252 {
253 // Try to add it below anyway, so it follows the usual bad data path.
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500254 lg2::error("Problems converting ESEL string to a byte vector");
Matt Spinler19e72902020-01-24 11:05:20 -0600255 }
256
257 addPEL(data, obmcLogID);
258}
259
260std::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 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500273 lg2::error("ESEL data too short, length = {LEN}", "LEN", esel.size());
Matt Spinler19e72902020-01-24 11:05:20 -0600274 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 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500286 lg2::error("ESEL data too short, length = {LEN}", "LEN",
287 esel.size());
Matt Spinler19e72902020-01-24 11:05:20 -0600288 throw std::length_error("ESEL data too short");
289 }
290 }
291
292 return data;
293}
294
Matt Spinler4e8078c2019-07-09 13:22:32 -0500295void Manager::erase(uint32_t obmcLogID)
296{
Matt Spinler475e5742019-07-18 16:09:49 -0500297 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
298
Vijay Loboafb1b462021-07-21 23:29:13 -0500299 auto path = std::string(OBJ_ENTRY) + '/' + std::to_string(obmcLogID);
300 _pelEntries.erase(path);
Matt Spinler475e5742019-07-18 16:09:49 -0500301 _repo.remove(id);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500302}
303
harsh-agarwal1d763db32024-09-03 09:18:50 -0500304void Manager::getLogIDWithHwIsolation(std::vector<uint32_t>& idsWithHwIsoEntry)
Matt Spinler4e8078c2019-07-09 13:22:32 -0500305{
harsh-agarwal1d763db32024-09-03 09:18:50 -0500306 idsWithHwIsoEntry = _dataIface->getLogIDWithHwIsolation();
307}
308
309bool Manager::isDeleteProhibited(uint32_t obmcLogID)
310{
harsh-agarwal1760b9812025-03-18 23:23:24 -0500311 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
312 if (!_repo.hasPEL(id))
313 {
314 return false;
315 }
harsh-agarwal1d763db32024-09-03 09:18:50 -0500316 auto entryPath{std::string(OBJ_ENTRY) + '/' + std::to_string(obmcLogID)};
317 auto entry = _pelEntries.find(entryPath);
318 if (entry != _pelEntries.end())
319 {
320 if (entry->second->guard())
321 {
322 auto hwIsolationAssocPaths = _dataIface->getAssociatedPaths(
323 entryPath += "/isolated_hw_entry", "/", 0,
324 {"xyz.openbmc_project.HardwareIsolation.Entry"});
325 if (!hwIsolationAssocPaths.empty())
326 {
327 return true;
328 }
329 }
330 }
Matt Spinler4e8078c2019-07-09 13:22:32 -0500331 return false;
332}
333
Matt Spinler56ad2a02020-03-26 14:00:52 -0500334PelFFDC Manager::convertToPelFFDC(const FFDCEntries& ffdc)
335{
336 PelFFDC pelFFDC;
337
338 std::for_each(ffdc.begin(), ffdc.end(), [&pelFFDC](const auto& f) {
339 PelFFDCfile pf;
340 pf.subType = std::get<ffdcSubtypePos>(f);
341 pf.version = std::get<ffdcVersionPos>(f);
342 pf.fd = std::get<ffdcFDPos>(f);
343
344 switch (std::get<ffdcFormatPos>(f))
345 {
346 case Create::FFDCFormat::JSON:
347 pf.format = UserDataFormat::json;
348 break;
349 case Create::FFDCFormat::CBOR:
350 pf.format = UserDataFormat::cbor;
351 break;
352 case Create::FFDCFormat::Text:
353 pf.format = UserDataFormat::text;
354 break;
355 case Create::FFDCFormat::Custom:
356 pf.format = UserDataFormat::custom;
357 break;
358 }
359
360 pelFFDC.push_back(pf);
361 });
362
363 return pelFFDC;
364}
365
Patrick Williams075c7922024-08-16 15:19:49 -0400366void Manager::createPEL(
367 const std::string& message, uint32_t obmcLogID, uint64_t timestamp,
368 phosphor::logging::Entry::Level severity,
Patrick Williamse5940632024-11-22 20:47:58 -0500369 const std::map<std::string, std::string>& additionalData,
Patrick Williams075c7922024-08-16 15:19:49 -0400370 const std::vector<std::string>& /*associations*/, const FFDCEntries& ffdc)
Matt Spinler4e8078c2019-07-09 13:22:32 -0500371{
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800372 auto entry = _registry.lookup(message, rg::LookupType::name);
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500373 auto pelFFDC = convertToPelFFDC(ffdc);
374 AdditionalData ad{additionalData};
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600375 std::string msg;
Matt Spinler67456c22019-10-21 12:22:49 -0500376
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500377 if (!entry)
Matt Spinler67456c22019-10-21 12:22:49 -0500378 {
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500379 // Instead, get the default entry that means there is no
380 // other matching entry. This error will still use the
381 // AdditionalData values of the original error, and this
382 // code will add the error message value that wasn't found
383 // to this AD. This way, there will at least be a PEL,
384 // possibly with callouts, to allow users to debug the
385 // issue that caused the error even without its own PEL.
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500386 lg2::error("Event not found in PEL message registry: {MSG}", "MSG",
387 message);
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500388
389 entry = _registry.lookup(defaultLogMessage, rg::LookupType::name);
390 if (!entry)
391 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500392 lg2::error("Default event not found in PEL message registry");
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500393 return;
394 }
395
396 ad.add(additional_data::error, message);
397 }
398
399 auto pel = std::make_unique<openpower::pels::PEL>(
Matt Spinler9d921092022-12-15 11:54:49 -0600400 *entry, obmcLogID, timestamp, severity, ad, pelFFDC, *_dataIface,
401 *_journal);
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500402
403 _repo.add(pel);
404
405 if (_repo.sizeWarning())
406 {
407 scheduleRepoPrune();
408 }
409
410 auto src = pel->primarySRC();
411 if (src)
412 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500413 auto asciiString = (*src)->asciiString();
414 while (asciiString.back() == ' ')
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500415 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500416 asciiString.pop_back();
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500417 }
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500418 lg2::info("Created PEL {ID} (BMC ID {BMCID}) with SRC {SRC}", "ID",
419 lg2::hex, pel->id(), "BMCID", pel->obmcLogID(), "SRC",
420 asciiString);
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600421 }
Matt Spinler1962e082020-08-05 13:44:53 -0500422
Sumit Kumar3e274432021-09-14 06:37:56 -0500423 // Check for severity 0x51 and update boot progress SRC
424 updateProgressSRC(pel);
425
Matt Spinler1962e082020-08-05 13:44:53 -0500426 // Activate any resulting service indicators if necessary
427 auto policy = service_indicators::getPolicy(*_dataIface);
428 policy->activate(*pel);
Andrew Geissler44fc3162020-07-09 09:21:31 -0500429
Matt Spinler8b81ec02022-07-12 13:25:37 -0500430 updateDBusSeverity(*pel);
Vijay Lobod354a392021-06-01 16:21:02 -0500431 updateEventId(pel);
Matt Spinler28d6ae22022-03-18 11:18:27 -0500432 updateResolution(*pel);
Matt Spinler3387eac2023-07-06 14:56:02 -0500433 serializeLogEntry(obmcLogID);
Vijay Loboafb1b462021-07-21 23:29:13 -0500434 createPELEntry(obmcLogID);
Matt Spinlerdf5cb832022-07-12 12:47:26 -0500435
436 // Check if firmware should quiesce system due to error
437 checkPelAndQuiesce(pel);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500438}
439
Matt Spinlera34ab722019-12-16 10:39:32 -0600440sdbusplus::message::unix_fd Manager::getPEL(uint32_t pelID)
441{
442 Repository::LogID id{Repository::LogID::Pel(pelID)};
443 std::optional<int> fd;
444
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500445 lg2::debug("getPEL {ID}", "ID", lg2::hex, pelID);
Matt Spinler5f5352e2020-03-05 16:23:27 -0600446
Matt Spinlera34ab722019-12-16 10:39:32 -0600447 try
448 {
449 fd = _repo.getPELFD(id);
450 }
Patrick Williams66491c62021-10-06 12:23:37 -0500451 catch (const std::exception& e)
Matt Spinlera34ab722019-12-16 10:39:32 -0600452 {
453 throw common_error::InternalFailure();
454 }
455
456 if (!fd)
457 {
458 throw common_error::InvalidArgument();
459 }
460
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600461 scheduleFDClose(*fd);
462
Matt Spinlera34ab722019-12-16 10:39:32 -0600463 return *fd;
464}
465
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600466void Manager::scheduleFDClose(int fd)
467{
468 _fdCloserEventSource = std::make_unique<sdeventplus::source::Defer>(
Matt Spinlerff9cec22020-07-15 13:06:35 -0500469 _event, std::bind(std::mem_fn(&Manager::closeFD), this, fd,
470 std::placeholders::_1));
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600471}
472
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500473void Manager::closeFD(int fd, sdeventplus::source::EventBase& /*source*/)
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600474{
475 close(fd);
476 _fdCloserEventSource.reset();
477}
478
Matt Spinlera34ab722019-12-16 10:39:32 -0600479std::vector<uint8_t> Manager::getPELFromOBMCID(uint32_t obmcLogID)
480{
481 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
482 std::optional<std::vector<uint8_t>> data;
483
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500484 lg2::debug("getPELFromOBMCID {BMCID}", "BMCID", obmcLogID);
Matt Spinler5f5352e2020-03-05 16:23:27 -0600485
Matt Spinlera34ab722019-12-16 10:39:32 -0600486 try
487 {
488 data = _repo.getPELData(id);
489 }
Patrick Williams66491c62021-10-06 12:23:37 -0500490 catch (const std::exception& e)
Matt Spinlera34ab722019-12-16 10:39:32 -0600491 {
492 throw common_error::InternalFailure();
493 }
494
495 if (!data)
496 {
497 throw common_error::InvalidArgument();
498 }
499
500 return *data;
501}
502
503void Manager::hostAck(uint32_t pelID)
504{
505 Repository::LogID id{Repository::LogID::Pel(pelID)};
506
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500507 lg2::debug("HostHack {ID}", "ID", lg2::hex, pelID);
Matt Spinler5f5352e2020-03-05 16:23:27 -0600508
Matt Spinlera34ab722019-12-16 10:39:32 -0600509 if (!_repo.hasPEL(id))
510 {
511 throw common_error::InvalidArgument();
512 }
513
514 if (_hostNotifier)
515 {
516 _hostNotifier->ackPEL(pelID);
517 }
518}
519
520void Manager::hostReject(uint32_t pelID, RejectionReason reason)
521{
522 Repository::LogID id{Repository::LogID::Pel(pelID)};
523
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500524 lg2::debug("HostReject {ID}, reason = {REASON}", "ID", lg2::hex, pelID,
525 "REASON", reason);
Matt Spinler5f5352e2020-03-05 16:23:27 -0600526
Matt Spinlera34ab722019-12-16 10:39:32 -0600527 if (!_repo.hasPEL(id))
528 {
529 throw common_error::InvalidArgument();
530 }
531
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600532 if (reason == RejectionReason::BadPEL)
Matt Spinlera34ab722019-12-16 10:39:32 -0600533 {
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600534 AdditionalData data;
535 data.add("BAD_ID", getNumberString("0x%08X", pelID));
536 _eventLogger.log("org.open_power.Logging.Error.SentBadPELToHost",
537 Entry::Level::Informational, data);
538 if (_hostNotifier)
Matt Spinlera34ab722019-12-16 10:39:32 -0600539 {
540 _hostNotifier->setBadPEL(pelID);
541 }
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600542 }
543 else if ((reason == RejectionReason::HostFull) && _hostNotifier)
544 {
545 _hostNotifier->setHostFull(pelID);
Matt Spinlera34ab722019-12-16 10:39:32 -0600546 }
547}
548
Matt Spinler7e727a32020-07-07 15:00:17 -0500549void Manager::scheduleRepoPrune()
550{
Matt Spinler7e727a32020-07-07 15:00:17 -0500551 _repoPrunerEventSource = std::make_unique<sdeventplus::source::Defer>(
Matt Spinlerff9cec22020-07-15 13:06:35 -0500552 _event, std::bind(std::mem_fn(&Manager::pruneRepo), this,
553 std::placeholders::_1));
Matt Spinler7e727a32020-07-07 15:00:17 -0500554}
555
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500556void Manager::pruneRepo(sdeventplus::source::EventBase& /*source*/)
Matt Spinler7e727a32020-07-07 15:00:17 -0500557{
Sumit Kumar027bf282022-01-24 11:25:19 -0600558 auto idsWithHwIsoEntry = _dataIface->getLogIDWithHwIsolation();
559
560 auto idsToDelete = _repo.prune(idsWithHwIsoEntry);
Matt Spinler7e727a32020-07-07 15:00:17 -0500561
562 // Remove the OpenBMC event logs for the PELs that were just removed.
563 std::for_each(idsToDelete.begin(), idsToDelete.end(),
564 [this](auto id) { this->_logManager.erase(id); });
565
566 _repoPrunerEventSource.reset();
567}
568
Matt Spinlerff9cec22020-07-15 13:06:35 -0500569void Manager::setupPELDeleteWatch()
570{
571 _pelFileDeleteFD = inotify_init1(IN_NONBLOCK);
572 if (-1 == _pelFileDeleteFD)
573 {
574 auto e = errno;
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500575 lg2::error("inotify_init1 failed with errno {ERRNO}", "ERRNO", e);
Matt Spinlerff9cec22020-07-15 13:06:35 -0500576 abort();
577 }
578
579 _pelFileDeleteWatchFD = inotify_add_watch(
580 _pelFileDeleteFD, _repo.repoPath().c_str(), IN_DELETE);
581 if (-1 == _pelFileDeleteWatchFD)
582 {
583 auto e = errno;
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500584 lg2::error("inotify_add_watch failed with errno {ERRNO}", "ERRNO", e);
Matt Spinlerff9cec22020-07-15 13:06:35 -0500585 abort();
586 }
587
588 _pelFileDeleteEventSource = std::make_unique<sdeventplus::source::IO>(
589 _event, _pelFileDeleteFD, EPOLLIN,
590 std::bind(std::mem_fn(&Manager::pelFileDeleted), this,
591 std::placeholders::_1, std::placeholders::_2,
592 std::placeholders::_3));
593}
594
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500595void Manager::pelFileDeleted(sdeventplus::source::IO& /*io*/, int /*fd*/,
Matt Spinlerff9cec22020-07-15 13:06:35 -0500596 uint32_t revents)
597{
598 if (!(revents & EPOLLIN))
599 {
600 return;
601 }
602
603 // An event for 1 PEL uses 48B. When all PELs are deleted at once,
604 // as many events as there is room for can be handled in one callback.
605 // A size of 2000 will allow 41 to be processed, with additional
606 // callbacks being needed to process the remaining ones.
Matt Spinler9d59d582021-05-19 07:57:10 -0600607 std::array<uint8_t, 2000> data{};
Matt Spinlerff9cec22020-07-15 13:06:35 -0500608 auto bytesRead = read(_pelFileDeleteFD, data.data(), data.size());
609 if (bytesRead < 0)
610 {
611 auto e = errno;
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500612 lg2::error("Failed reading data from inotify event, errno = {ERRNO}",
613 "ERRNO", e);
Matt Spinlerff9cec22020-07-15 13:06:35 -0500614 abort();
615 }
616
617 auto offset = 0;
618 while (offset < bytesRead)
619 {
620 auto event = reinterpret_cast<inotify_event*>(&data[offset]);
621 if (event->mask & IN_DELETE)
622 {
623 std::string filename{event->name};
624
625 // Get the PEL ID from the filename and tell the
626 // repo it's been removed, and then delete the BMC
627 // event log if it's there.
628 auto pos = filename.find_first_of('_');
629 if (pos != std::string::npos)
630 {
631 try
632 {
633 auto idString = filename.substr(pos + 1);
634 auto pelID = std::stoul(idString, nullptr, 16);
635
636 Repository::LogID id{Repository::LogID::Pel(pelID)};
637 auto removedLogID = _repo.remove(id);
638 if (removedLogID)
639 {
640 _logManager.erase(removedLogID->obmcID.id);
641 }
642 }
643 catch (const std::exception& e)
644 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500645 lg2::info("Could not find PEL ID from its filename {NAME}",
646 "NAME", filename);
Matt Spinlerff9cec22020-07-15 13:06:35 -0500647 }
648 }
649 }
650
651 offset += offsetof(inotify_event, name) + event->len;
652 }
653}
Matt Spinler9cc30072020-09-16 15:39:34 -0500654
655std::tuple<uint32_t, uint32_t> Manager::createPELWithFFDCFiles(
656 std::string message, Entry::Level severity,
657 std::map<std::string, std::string> additionalData,
658 std::vector<std::tuple<
Willy Tu6ddbf692023-09-05 10:54:16 -0700659 sdbusplus::server::xyz::openbmc_project::logging::Create::FFDCFormat,
Matt Spinler9cc30072020-09-16 15:39:34 -0500660 uint8_t, uint8_t, sdbusplus::message::unix_fd>>
661 fFDC)
662{
Paul Fertser221b79b2024-03-04 15:40:23 +0000663 _logManager.create(message, severity, additionalData, fFDC);
Matt Spinler44893cc2020-08-26 11:34:17 -0500664
665 return {_logManager.lastEntryID(), _repo.lastPelID()};
Matt Spinler9cc30072020-09-16 15:39:34 -0500666}
667
Matt Spinler8bd4ca42022-04-01 16:06:06 -0500668std::string Manager::getPELJSON(uint32_t obmcLogID)
Matt Spinleraa85a072022-03-23 11:26:41 -0500669{
Matt Spinler8bd4ca42022-04-01 16:06:06 -0500670 // Throws InvalidArgument if not found
671 auto pelID = getPELIdFromBMCLogId(obmcLogID);
672
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -0500673 auto cmd = std::format("/usr/bin/peltool -i {:#x}", pelID);
Matt Spinler8bd4ca42022-04-01 16:06:06 -0500674
675 FILE* pipe = popen(cmd.c_str(), "r");
676 if (!pipe)
677 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500678 lg2::error("Error running cmd: {CMD}", "CMD", cmd);
Matt Spinler8bd4ca42022-04-01 16:06:06 -0500679 throw common_error::InternalFailure();
680 }
681
682 std::string output;
683 std::array<char, 1024> buffer;
684 while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
685 {
686 output.append(buffer.data());
687 }
688
689 int rc = pclose(pipe);
690 if (WEXITSTATUS(rc) != 0)
691 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500692 lg2::error("Error running cmd: {CMD}, rc = {RC}", "CMD", cmd, "RC", rc);
Matt Spinler8bd4ca42022-04-01 16:06:06 -0500693 throw common_error::InternalFailure();
694 }
695
696 return output;
Matt Spinleraa85a072022-03-23 11:26:41 -0500697}
698
Andrew Geissler44fc3162020-07-09 09:21:31 -0500699void Manager::checkPelAndQuiesce(std::unique_ptr<openpower::pels::PEL>& pel)
700{
Matt Spinlerb2abc042021-05-17 15:32:50 -0600701 if ((pel->userHeader().severity() ==
702 static_cast<uint8_t>(SeverityType::nonError)) ||
703 (pel->userHeader().severity() ==
704 static_cast<uint8_t>(SeverityType::recovered)))
Andrew Geissler44fc3162020-07-09 09:21:31 -0500705 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500706 lg2::debug(
Matt Spinlerb2abc042021-05-17 15:32:50 -0600707 "PEL severity informational or recovered. no quiesce needed");
Andrew Geissler44fc3162020-07-09 09:21:31 -0500708 return;
709 }
710 if (!_logManager.isQuiesceOnErrorEnabled())
711 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500712 lg2::debug("QuiesceOnHwError not enabled, no quiesce needed");
Andrew Geissler44fc3162020-07-09 09:21:31 -0500713 return;
714 }
715
Matt Spinler845c6242022-03-01 16:45:08 -0600716 CreatorID creatorID{pel->privateHeader().creatorID()};
717
718 if ((creatorID != CreatorID::openBMC) &&
719 (creatorID != CreatorID::hostboot) &&
720 (creatorID != CreatorID::ioDrawer) && (creatorID != CreatorID::occ) &&
721 (creatorID != CreatorID::phyp))
722 {
723 return;
724 }
725
Andrew Geissler44fc3162020-07-09 09:21:31 -0500726 // Now check if it has any type of callout
Andrew Geisslerf8e750d2022-01-14 14:56:13 -0600727 if (pel->isHwCalloutPresent())
Andrew Geissler44fc3162020-07-09 09:21:31 -0500728 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500729 lg2::info(
Matt Spinlerb2abc042021-05-17 15:32:50 -0600730 "QuiesceOnHwError enabled, PEL severity not nonError or recovered, "
731 "and callout is present");
Andrew Geissler44fc3162020-07-09 09:21:31 -0500732
733 _logManager.quiesceOnError(pel->obmcLogID());
734 }
735}
736
Vijay Lobod354a392021-06-01 16:21:02 -0500737std::string Manager::getEventId(const openpower::pels::PEL& pel) const
738{
739 std::string str;
740 auto src = pel.primarySRC();
741 if (src)
742 {
743 const auto& hexwords = (*src)->hexwordData();
744
745 std::string refcode = (*src)->asciiString();
746 size_t pos = refcode.find_last_not_of(0x20);
747 if (pos != std::string::npos)
748 {
749 refcode.erase(pos + 1);
750 }
751 str = refcode;
752
753 for (auto& value : hexwords)
754 {
755 str += " ";
756 str += getNumberString("%08X", value);
757 }
758 }
Matt Spinler0003af12022-06-08 10:46:17 -0500759 return sanitizeFieldForDBus(str);
Vijay Lobod354a392021-06-01 16:21:02 -0500760}
761
762void Manager::updateEventId(std::unique_ptr<openpower::pels::PEL>& pel)
763{
764 std::string eventIdStr = getEventId(*pel);
765
766 auto entryN = _logManager.entries.find(pel->obmcLogID());
767 if (entryN != _logManager.entries.end())
768 {
Matt Spinlerb25e8a32023-06-07 16:05:36 -0500769 entryN->second->eventId(eventIdStr, true);
Vijay Lobod354a392021-06-01 16:21:02 -0500770 }
771}
772
Matt Spinler0003af12022-06-08 10:46:17 -0500773std::string Manager::sanitizeFieldForDBus(std::string field)
774{
775 std::for_each(field.begin(), field.end(), [](char& ch) {
776 if (((ch < ' ') || (ch > '~')) && (ch != '\n') && (ch != '\t'))
777 {
778 ch = ' ';
779 }
780 });
781 return field;
782}
783
Vijay Lobo593a4c62021-06-16 14:25:26 -0500784std::string Manager::getResolution(const openpower::pels::PEL& pel) const
785{
786 std::string str;
787 std::string resolution;
788 auto src = pel.primarySRC();
789 if (src)
790 {
791 // First extract the callout pointer and then go through
792 const auto& callouts = (*src)->callouts();
793 namespace pv = openpower::pels::pel_values;
794 // All PELs dont have callout, check before parsing callout data
795 if (callouts)
796 {
797 const auto& entries = callouts->callouts();
798 // Entry starts with index 1
799 uint8_t index = 1;
800 for (auto& entry : entries)
801 {
802 resolution += std::to_string(index) + ". ";
803 // Adding Location code to resolution
804 if (!entry->locationCode().empty())
Patrick Williams2544b412022-10-04 08:41:06 -0500805 resolution += "Location Code: " + entry->locationCode() +
806 ", ";
Vijay Lobo593a4c62021-06-16 14:25:26 -0500807 if (entry->fruIdentity())
808 {
809 // Get priority and set the resolution string
810 str = pv::getValue(entry->priority(),
811 pel_values::calloutPriorityValues,
812 pel_values::registryNamePos);
813 str[0] = toupper(str[0]);
814 resolution += "Priority: " + str + ", ";
815 if (entry->fruIdentity()->getPN().has_value())
816 {
817 resolution +=
818 "PN: " + entry->fruIdentity()->getPN().value() +
819 ", ";
820 }
821 if (entry->fruIdentity()->getSN().has_value())
822 {
823 resolution +=
824 "SN: " + entry->fruIdentity()->getSN().value() +
825 ", ";
826 }
827 if (entry->fruIdentity()->getCCIN().has_value())
828 {
829 resolution +=
830 "CCIN: " + entry->fruIdentity()->getCCIN().value() +
831 ", ";
832 }
833 // Add the maintenance procedure
834 if (entry->fruIdentity()->getMaintProc().has_value())
835 {
836 resolution +=
837 "Procedure: " +
838 entry->fruIdentity()->getMaintProc().value() + ", ";
839 }
840 }
841 resolution.resize(resolution.size() - 2);
842 resolution += "\n";
843 index++;
844 }
845 }
846 }
Matt Spinler0003af12022-06-08 10:46:17 -0500847 return sanitizeFieldForDBus(resolution);
Vijay Lobo593a4c62021-06-16 14:25:26 -0500848}
849
Matt Spinler28d6ae22022-03-18 11:18:27 -0500850bool Manager::updateResolution(const openpower::pels::PEL& pel)
Vijay Lobo593a4c62021-06-16 14:25:26 -0500851{
Matt Spinler28d6ae22022-03-18 11:18:27 -0500852 std::string callouts = getResolution(pel);
853 auto entryN = _logManager.entries.find(pel.obmcLogID());
Vijay Lobo593a4c62021-06-16 14:25:26 -0500854 if (entryN != _logManager.entries.end())
855 {
Matt Spinler734ed2b2022-01-21 09:31:46 -0600856 entryN->second->resolution(callouts, true);
Vijay Lobo593a4c62021-06-16 14:25:26 -0500857 }
Matt Spinler28d6ae22022-03-18 11:18:27 -0500858
859 return false;
Vijay Lobo593a4c62021-06-16 14:25:26 -0500860}
861
Matt Spinler3387eac2023-07-06 14:56:02 -0500862void Manager::serializeLogEntry(uint32_t obmcLogID)
863{
864 auto entryN = _logManager.entries.find(obmcLogID);
865 if (entryN != _logManager.entries.end())
866 {
867 serialize(*entryN->second);
868 }
869}
870
Matt Spinler8b81ec02022-07-12 13:25:37 -0500871void Manager::updateDBusSeverity(const openpower::pels::PEL& pel)
872{
873 // The final severity of the PEL may not agree with the
874 // original severity of the D-Bus event log. Update the
875 // D-Bus property to match in some cases. This is to
876 // ensure there isn't a Critical or Warning Redfish event
877 // log for an informational or recovered PEL (or vice versa).
878 // This doesn't make an explicit call to serialize the new
879 // event log property value because updateEventId() is called
880 // right after this and will do it.
881 auto sevType =
882 static_cast<SeverityType>(pel.userHeader().severity() & 0xF0);
883
884 auto entryN = _logManager.entries.find(pel.obmcLogID());
885 if (entryN != _logManager.entries.end())
886 {
Patrick Williams075c7922024-08-16 15:19:49 -0400887 auto newSeverity =
888 fixupLogSeverity(entryN->second->severity(), sevType);
Matt Spinler8b81ec02022-07-12 13:25:37 -0500889 if (newSeverity)
890 {
Matt Spinlerfbdfc762023-06-30 15:15:44 -0500891 lg2::info("Changing event log {ID} severity from {OLD} "
892 "to {NEW} to match PEL",
893 "ID", lg2::hex, entryN->second->id(), "OLD",
894 Entry::convertLevelToString(entryN->second->severity()),
895 "NEW", Entry::convertLevelToString(*newSeverity));
Matt Spinler8b81ec02022-07-12 13:25:37 -0500896
897 entryN->second->severity(*newSeverity, true);
898 }
899 }
900}
901
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600902void Manager::setEntryPath(uint32_t obmcLogID)
903{
904 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
905 if (auto attributes = _repo.getPELAttributes(id); attributes)
906 {
907 auto& attr = attributes.value().get();
908 auto entry = _logManager.entries.find(obmcLogID);
909 if (entry != _logManager.entries.end())
910 {
Matt Spinler734ed2b2022-01-21 09:31:46 -0600911 entry->second->path(attr.path, true);
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600912 }
913 }
914}
915
Vijay Lobocbc93a42021-05-20 19:04:07 -0500916void Manager::setServiceProviderNotifyFlag(uint32_t obmcLogID)
917{
918 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
919 if (auto attributes = _repo.getPELAttributes(id); attributes)
920 {
921 auto& attr = attributes.value().get();
922 auto entry = _logManager.entries.find(obmcLogID);
923 if (entry != _logManager.entries.end())
924 {
Lakshmi Yadlapati7a3ede52022-11-18 13:26:17 -0600925 if (attr.actionFlags.test(callHomeFlagBit))
926 {
Matt Spinlerb25e8a32023-06-07 16:05:36 -0500927 entry->second->serviceProviderNotify(Entry::Notify::Notify,
928 true);
Lakshmi Yadlapati7a3ede52022-11-18 13:26:17 -0600929 }
930 else
931 {
Matt Spinlerb25e8a32023-06-07 16:05:36 -0500932 entry->second->serviceProviderNotify(Entry::Notify::Inhibit,
933 true);
Lakshmi Yadlapati7a3ede52022-11-18 13:26:17 -0600934 }
Vijay Lobocbc93a42021-05-20 19:04:07 -0500935 }
936 }
937}
938
Matt Spinler734ed2b2022-01-21 09:31:46 -0600939void Manager::createPELEntry(uint32_t obmcLogID, bool skipIaSignal)
Vijay Loboafb1b462021-07-21 23:29:13 -0500940{
941 std::map<std::string, PropertiesVariant> varData;
942 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
943 if (auto attributes = _repo.getPELAttributes(id); attributes)
944 {
945 namespace pv = openpower::pels::pel_values;
946 auto& attr = attributes.value().get();
Vijay Lobob2e541e2021-08-31 23:12:47 -0500947
948 // get the hidden flag values
949 auto sevType = static_cast<SeverityType>(attr.severity & 0xF0);
950 auto isHidden = true;
951 if (((sevType != SeverityType::nonError) &&
952 attr.actionFlags.test(reportFlagBit) &&
953 !attr.actionFlags.test(hiddenFlagBit)) ||
954 ((sevType == SeverityType::nonError) &&
955 attr.actionFlags.test(serviceActionFlagBit)))
956 {
957 isHidden = false;
958 }
959 varData.emplace(std::string("Hidden"), isHidden);
Vijay Loboafb1b462021-07-21 23:29:13 -0500960 varData.emplace(
961 std::string("Subsystem"),
962 pv::getValue(attr.subsystem, pel_values::subsystemValues));
Vijay Lobo2fb10212021-08-22 23:24:16 -0500963
964 varData.emplace(
965 std::string("ManagementSystemAck"),
966 (attr.hmcState == TransmissionState::acked ? true : false));
967
Matt Spinler8e65f4e2023-05-02 13:40:08 -0500968 varData.emplace("PlatformLogID", attr.plid);
969 varData.emplace("Deconfig", attr.deconfig);
970 varData.emplace("Guard", attr.guard);
971 varData.emplace("Timestamp", attr.creationTime);
972
Vijay Loboafb1b462021-07-21 23:29:13 -0500973 // Path to create PELEntry Interface is same as PEL
974 auto path = std::string(OBJ_ENTRY) + '/' + std::to_string(obmcLogID);
975 // Create Interface for PELEntry and set properties
Vijay Lobo2fb10212021-08-22 23:24:16 -0500976 auto pelEntry = std::make_unique<PELEntry>(_logManager.getBus(), path,
977 varData, obmcLogID, &_repo);
Matt Spinler734ed2b2022-01-21 09:31:46 -0600978 if (!skipIaSignal)
979 {
980 pelEntry->emit_added();
981 }
Vijay Loboafb1b462021-07-21 23:29:13 -0500982 _pelEntries.emplace(std::move(path), std::move(pelEntry));
983 }
984}
985
Ramesh Iyyarf4203c42021-06-24 06:09:23 -0500986uint32_t Manager::getPELIdFromBMCLogId(uint32_t bmcLogId)
987{
988 Repository::LogID id{Repository::LogID::Obmc(bmcLogId)};
989 if (auto logId = _repo.getLogID(id); !logId.has_value())
990 {
991 throw common_error::InvalidArgument();
992 }
993 else
994 {
995 return logId->pelID.id;
996 }
997}
998
Ramesh Iyyar530efbf2021-06-24 06:22:22 -0500999uint32_t Manager::getBMCLogIdFromPELId(uint32_t pelId)
1000{
1001 Repository::LogID id{Repository::LogID::Pel(pelId)};
1002 if (auto logId = _repo.getLogID(id); !logId.has_value())
1003 {
1004 throw common_error::InvalidArgument();
1005 }
1006 else
1007 {
1008 return logId->obmcID.id;
1009 }
1010}
1011
Patrick Williams25291152025-02-01 08:21:42 -05001012void Manager::updateProgressSRC(
1013 std::unique_ptr<openpower::pels::PEL>& pel) const
Sumit Kumar3e274432021-09-14 06:37:56 -05001014{
1015 // Check for pel severity of type - 0x51 = critical error, system
1016 // termination
1017 if (pel->userHeader().severity() == 0x51)
1018 {
1019 auto src = pel->primarySRC();
1020 if (src)
1021 {
1022 std::vector<uint8_t> asciiSRC = (*src)->getSrcStruct();
1023 uint64_t srcRefCode = 0;
1024
1025 // Read bytes from offset [40-47] e.g. BD8D1001
1026 for (int i = 0; i < 8; i++)
1027 {
Patrick Williams075c7922024-08-16 15:19:49 -04001028 srcRefCode |=
1029 (static_cast<uint64_t>(asciiSRC[40 + i]) << (8 * i));
Sumit Kumar3e274432021-09-14 06:37:56 -05001030 }
1031
1032 try
1033 {
1034 _dataIface->createProgressSRC(srcRefCode, asciiSRC);
1035 }
Matt Spinler87f39242023-05-01 11:36:18 -05001036 catch (const std::exception&)
Sumit Kumar3e274432021-09-14 06:37:56 -05001037 {
1038 // Exception - may be no boot progress interface on dbus
1039 }
1040 }
1041 }
1042}
1043
Matt Spinlerd8fb5ba2022-01-25 13:01:14 -06001044void Manager::scheduleObmcLogDelete(uint32_t obmcLogID)
1045{
1046 _obmcLogDeleteEventSource = std::make_unique<sdeventplus::source::Defer>(
1047 _event, std::bind(std::mem_fn(&Manager::deleteObmcLog), this,
1048 std::placeholders::_1, obmcLogID));
1049}
1050
1051void Manager::deleteObmcLog(sdeventplus::source::EventBase&, uint32_t obmcLogID)
1052{
Matt Spinlerfbdfc762023-06-30 15:15:44 -05001053 lg2::info("Removing event log with no PEL: {BMCID}", "BMCID", obmcLogID);
Matt Spinlerd8fb5ba2022-01-25 13:01:14 -06001054 _logManager.erase(obmcLogID);
1055 _obmcLogDeleteEventSource.reset();
1056}
1057
Matt Spinler0dd22c82023-05-04 15:28:12 -05001058bool Manager::clearPowerThermalDeconfigFlag(const std::string& locationCode,
1059 openpower::pels::PEL& pel)
1060{
1061 // The requirements state that only power-thermal or
1062 // fan PELs need their deconfig flag cleared.
1063 static const std::vector<uint32_t> compIDs{bmcThermalCompID, bmcFansCompID};
1064
1065 if (std::find(compIDs.begin(), compIDs.end(),
1066 pel.privateHeader().header().componentID) == compIDs.end())
1067 {
1068 return false;
1069 }
1070
1071 auto src = pel.primarySRC();
1072 const auto& callouts = (*src)->callouts();
1073 if (!callouts)
1074 {
1075 return false;
1076 }
1077
1078 for (const auto& callout : callouts->callouts())
1079 {
1080 // Look for the passed in location code in a callout that
1081 // is either a normal HW callout or a symbolic FRU with
1082 // a trusted location code callout.
1083 if ((callout->locationCode() != locationCode) ||
1084 !callout->fruIdentity())
1085 {
1086 continue;
1087 }
1088
1089 if ((callout->fruIdentity()->failingComponentType() !=
1090 src::FRUIdentity::hardwareFRU) &&
1091 (callout->fruIdentity()->failingComponentType() !=
1092 src::FRUIdentity::symbolicFRUTrustedLocCode))
1093 {
1094 continue;
1095 }
1096
Matt Spinlerfbdfc762023-06-30 15:15:44 -05001097 lg2::info(
1098 "Clearing deconfig flag in PEL {ID} with SRC {SRC} because {LOC} was replaced",
1099 "ID", lg2::hex, pel.id(), "SRC", (*src)->asciiString().substr(0, 8),
1100 "LOC", locationCode);
Matt Spinler0dd22c82023-05-04 15:28:12 -05001101 (*src)->clearErrorStatusFlag(SRC::ErrorStatusFlags::deconfigured);
1102 return true;
1103 }
1104 return false;
1105}
1106
1107void Manager::hardwarePresent(const std::string& locationCode)
1108{
1109 Repository::PELUpdateFunc handlePowerThermalHardwarePresent =
1110 [locationCode](openpower::pels::PEL& pel) {
Patrick Williams075c7922024-08-16 15:19:49 -04001111 return Manager::clearPowerThermalDeconfigFlag(locationCode, pel);
1112 };
Matt Spinler0dd22c82023-05-04 15:28:12 -05001113
1114 // If the PEL was created by the BMC and has the deconfig flag set,
1115 // it's a candidate to have the deconfig flag cleared.
1116 for (const auto& [id, attributes] : _repo.getAttributesMap())
1117 {
1118 if ((attributes.creator == static_cast<uint8_t>(CreatorID::openBMC)) &&
1119 attributes.deconfig)
1120 {
Matt Spinler1cb59f72023-07-20 09:49:50 -05001121 auto updated = _repo.updatePEL(attributes.path,
1122 handlePowerThermalHardwarePresent);
1123
1124 if (updated)
1125 {
1126 // Also update the property on D-Bus
1127 auto objPath = std::string(OBJ_ENTRY) + '/' +
1128 std::to_string(id.obmcID.id);
1129 auto entryN = _pelEntries.find(objPath);
1130 if (entryN != _pelEntries.end())
1131 {
1132 entryN->second->deconfig(false);
1133 }
1134 else
1135 {
1136 lg2::error(
1137 "Could not find PEL Entry D-Bus object for {PATH}",
1138 "PATH", objPath);
1139 }
1140 }
Matt Spinler0dd22c82023-05-04 15:28:12 -05001141 }
1142 }
1143}
1144
Matt Spinler4e8078c2019-07-09 13:22:32 -05001145} // namespace pels
1146} // namespace openpower