blob: c43a660095d24f7dd44229d3a234c709611e421e [file] [log] [blame]
Patrick Venture46470a32018-09-07 19:26:25 -07001#include "config.h"
2
3#include "selutility.hpp"
4
Vernon Mauerye08fbff2019-04-03 09:19:34 -07005#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -07006#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -07007#include <ipmid/utils.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -07008#include <phosphor-logging/elog-errors.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -07009#include <xyz/openbmc_project/Common/error.hpp>
Patrick Venture46470a32018-09-07 19:26:25 -070010
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050011#include <charconv>
12#include <chrono>
13#include <filesystem>
14#include <vector>
15
Tom Joseph6b7a1432017-05-19 10:43:36 +053016extern const ipmi::sensor::InvObjectIDMap invSensors;
17using namespace phosphor::logging;
18using InternalFailure =
Willy Tu523e2d12023-09-05 11:36:48 -070019 sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
Tom Joseph6b7a1432017-05-19 10:43:36 +053020
Lei YU5015e952020-12-03 14:01:31 +080021namespace
22{
23
24constexpr auto systemEventRecord = 0x02;
Lei YU07c15b72020-12-06 15:08:06 +080025constexpr auto generatorID = 0x2000;
26constexpr auto eventMsgRevision = 0x04;
27constexpr auto assertEvent = 0x00;
28constexpr auto deassertEvent = 0x80;
29constexpr auto selDataSize = 3;
30constexpr auto oemCDDataSize = 9;
31constexpr auto oemEFDataSize = 13;
Lei YU5015e952020-12-03 14:01:31 +080032
Lei YU719a41c2020-12-03 17:50:44 +080033constexpr auto propAdditionalData = "AdditionalData";
Lei YU07c15b72020-12-06 15:08:06 +080034constexpr auto propResolved = "Resolved";
35
Lei YU719a41c2020-12-03 17:50:44 +080036constexpr auto strEventDir = "EVENT_DIR";
37constexpr auto strGenerateId = "GENERATOR_ID";
38constexpr auto strRecordType = "RECORD_TYPE";
39constexpr auto strSensorData = "SENSOR_DATA";
40constexpr auto strSensorPath = "SENSOR_PATH";
41
Lei YU5015e952020-12-03 14:01:31 +080042} // namespace
43
Tom Joseph6b7a1432017-05-19 10:43:36 +053044namespace ipmi
45{
46
47namespace sel
48{
49
50namespace internal
51{
52
Lei YU5015e952020-12-03 14:01:31 +080053inline bool isRecordOEM(uint8_t recordType)
54{
55 return recordType != systemEventRecord;
56}
57
Lei YU719a41c2020-12-03 17:50:44 +080058using additionalDataMap = std::map<std::string, std::string>;
Lei YU07c15b72020-12-06 15:08:06 +080059using entryDataMap = std::map<PropertyName, PropertyType>;
Lei YU5015e952020-12-03 14:01:31 +080060/** Parse the entry with format like key=val */
61std::pair<std::string, std::string> parseEntry(const std::string& entry)
62{
63 constexpr auto equalSign = "=";
64 auto pos = entry.find(equalSign);
65 assert(pos != std::string::npos);
66 auto key = entry.substr(0, pos);
67 auto val = entry.substr(pos + 1);
68 return {key, val};
69}
70
Lei YU719a41c2020-12-03 17:50:44 +080071additionalDataMap parseAdditionalData(const AdditionalData& data)
Lei YU5015e952020-12-03 14:01:31 +080072{
73 std::map<std::string, std::string> ret;
74
75 for (const auto& d : data)
76 {
77 ret.insert(parseEntry(d));
78 }
79 return ret;
80}
81
Lei YU07c15b72020-12-06 15:08:06 +080082int convert(const std::string_view& str, int base = 10)
Lei YU5015e952020-12-03 14:01:31 +080083{
William A. Kennington III4c521022023-07-28 13:07:30 -070084 int ret = 0;
Lei YU5015e952020-12-03 14:01:31 +080085 std::from_chars(str.data(), str.data() + str.size(), ret, base);
Lei YU07c15b72020-12-06 15:08:06 +080086 return ret;
Lei YU5015e952020-12-03 14:01:31 +080087}
88
89// Convert the string to a vector of uint8_t, where the str is formatted as hex
90std::vector<uint8_t> convertVec(const std::string_view& str)
91{
92 std::vector<uint8_t> ret;
93 auto len = str.size() / 2;
94 ret.reserve(len);
95 for (size_t i = 0; i < len; ++i)
96 {
Lei YU07c15b72020-12-06 15:08:06 +080097 ret.emplace_back(
98 static_cast<uint8_t>(convert(str.substr(i * 2, 2), 16)));
Lei YU5015e952020-12-03 14:01:31 +080099 }
100 return ret;
101}
102
Lei YU719a41c2020-12-03 17:50:44 +0800103/** Construct OEM SEL record according to IPMI spec 32.2, 32.3. */
Lei YU07c15b72020-12-06 15:08:06 +0800104void constructOEMSEL(uint8_t recordType, std::chrono::milliseconds timestamp,
Lei YU719a41c2020-12-03 17:50:44 +0800105 const additionalDataMap& m, GetSELEntryResponse& record)
106{
107 auto dataIter = m.find(strSensorData);
108 assert(dataIter != m.end());
109 auto sensorData = convertVec(dataIter->second);
110 if (recordType >= 0xC0 && recordType < 0xE0)
111 {
112 record.event.oemCD.timeStamp = static_cast<uint32_t>(
113 std::chrono::duration_cast<std::chrono::seconds>(timestamp)
114 .count());
115 record.event.oemCD.recordType = recordType;
116 // The ManufactureID and OEM Defined are packed in the sensor data
117 // Fill the 9 bytes of Manufacture ID and oemDefined
118 memcpy(&record.event.oemCD.manufacturerID, sensorData.data(),
Lei YU07c15b72020-12-06 15:08:06 +0800119 std::min(sensorData.size(), static_cast<size_t>(oemCDDataSize)));
Lei YU719a41c2020-12-03 17:50:44 +0800120 }
121 else if (recordType >= 0xE0)
122 {
123 record.event.oemEF.recordType = recordType;
124 // The remaining 13 bytes are the OEM Defined data
125 memcpy(&record.event.oemEF.oemDefined, sensorData.data(),
Lei YU07c15b72020-12-06 15:08:06 +0800126 std::min(sensorData.size(), static_cast<size_t>(oemEFDataSize)));
Lei YU719a41c2020-12-03 17:50:44 +0800127 }
128}
129
Lei YU07c15b72020-12-06 15:08:06 +0800130void constructSEL(uint8_t recordType, std::chrono::milliseconds timestamp,
Willy Tu11d68892022-01-20 10:37:34 -0800131 const additionalDataMap& m, const entryDataMap&,
Lei YU07c15b72020-12-06 15:08:06 +0800132 GetSELEntryResponse& record)
133{
134 if (recordType != systemEventRecord)
135 {
136 log<level::ERR>("Invalid recordType");
137 elog<InternalFailure>();
138 }
139
140 // Default values when there is no matched sensor
141 record.event.eventRecord.sensorType = 0;
142 record.event.eventRecord.sensorNum = 0xFF;
143 record.event.eventRecord.eventType = 0;
144
145 auto iter = m.find(strSensorPath);
146 assert(iter != m.end());
147 const auto& sensorPath = iter->second;
148 auto sensorIter = invSensors.find(sensorPath);
149
150 if (sensorIter != invSensors.end())
151 {
152 // There is a matched sensor
153 record.event.eventRecord.sensorType = sensorIter->second.sensorType;
154 record.event.eventRecord.sensorNum = sensorIter->second.sensorID;
155
156 iter = m.find(strEventDir);
157 assert(iter != m.end());
158 auto eventDir = static_cast<uint8_t>(convert(iter->second));
159 uint8_t assert = eventDir ? assertEvent : deassertEvent;
160 record.event.eventRecord.eventType =
161 assert | sensorIter->second.eventReadingType;
162 }
163 record.event.eventRecord.recordType = recordType;
164 record.event.eventRecord.timeStamp = static_cast<uint32_t>(
165 std::chrono::duration_cast<std::chrono::seconds>(timestamp).count());
166 iter = m.find(strGenerateId);
167 assert(iter != m.end());
168 record.event.eventRecord.generatorID =
169 static_cast<uint16_t>(convert(iter->second));
170 record.event.eventRecord.eventMsgRevision = eventMsgRevision;
171 iter = m.find(strSensorData);
172 assert(iter != m.end());
173 auto sensorData = convertVec(iter->second);
174 // The remaining 3 bytes are the sensor data
175 memcpy(&record.event.eventRecord.eventData1, sensorData.data(),
176 std::min(sensorData.size(), static_cast<size_t>(selDataSize)));
177}
178
Patrick Venture0b02be92018-08-31 11:55:55 -0700179GetSELEntryResponse
180 prepareSELEntry(const std::string& objPath,
181 ipmi::sensor::InvObjectIDMap::const_iterator iter)
Tom Joseph6b7a1432017-05-19 10:43:36 +0530182{
Patrick Venture0b02be92018-08-31 11:55:55 -0700183 GetSELEntryResponse record{};
Tom Joseph6b7a1432017-05-19 10:43:36 +0530184
Patrick Williams5d82f472022-07-22 19:26:53 -0500185 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph6b7a1432017-05-19 10:43:36 +0530186 auto service = ipmi::getService(bus, logEntryIntf, objPath);
187
188 // Read all the log entry properties.
Patrick Venture0b02be92018-08-31 11:55:55 -0700189 auto methodCall = bus.new_method_call(service.c_str(), objPath.c_str(),
190 propIntf, "GetAll");
Tom Joseph6b7a1432017-05-19 10:43:36 +0530191 methodCall.append(logEntryIntf);
192
George Liu3e3cc352023-07-26 15:59:31 +0800193 entryDataMap entryData;
194 try
Tom Joseph6b7a1432017-05-19 10:43:36 +0530195 {
George Liu3e3cc352023-07-26 15:59:31 +0800196 auto reply = bus.call(methodCall);
197 reply.read(entryData);
198 }
199 catch (const std::exception& e)
200 {
201 log<level::ERR>("Error in reading logging property entries",
202 entry("ERROR=%s", e.what()));
Tom Joseph6b7a1432017-05-19 10:43:36 +0530203 elog<InternalFailure>();
204 }
205
Tom Joseph6b7a1432017-05-19 10:43:36 +0530206 // Read Id from the log entry.
207 static constexpr auto propId = "Id";
208 auto iterId = entryData.find(propId);
209 if (iterId == entryData.end())
210 {
211 log<level::ERR>("Error in reading Id of logging entry");
212 elog<InternalFailure>();
213 }
214
Tom Joseph6b7a1432017-05-19 10:43:36 +0530215 // Read Timestamp from the log entry.
216 static constexpr auto propTimeStamp = "Timestamp";
217 auto iterTimeStamp = entryData.find(propTimeStamp);
218 if (iterTimeStamp == entryData.end())
219 {
220 log<level::ERR>("Error in reading Timestamp of logging entry");
221 elog<InternalFailure>();
222 }
Tom Joseph6b7a1432017-05-19 10:43:36 +0530223 std::chrono::milliseconds chronoTimeStamp(
Vernon Maueryf442e112019-04-09 11:44:36 -0700224 std::get<uint64_t>(iterTimeStamp->second));
Tom Joseph6b7a1432017-05-19 10:43:36 +0530225
Lei YU690f4d52021-04-28 19:05:24 +0800226 bool isFromSELLogger = false;
227 additionalDataMap m;
228
229 // The recordID are with the same offset between different types,
230 // so we are safe to set the recordID here
231 record.event.eventRecord.recordID =
232 static_cast<uint16_t>(std::get<uint32_t>(iterId->second));
233
234 iterId = entryData.find(propAdditionalData);
235 if (iterId != entryData.end())
236 {
237 // Check if it's a SEL from phosphor-sel-logger which shall contain
238 // the record ID, etc
239 const auto& addData = std::get<AdditionalData>(iterId->second);
240 m = parseAdditionalData(addData);
241 auto recordTypeIter = m.find(strRecordType);
242 if (recordTypeIter != m.end())
243 {
244 // It is a SEL from phosphor-sel-logger
245 isFromSELLogger = true;
246 }
247 else
248 {
249 // Not a SEL from phosphor-sel-logger, it shall have a valid
250 // invSensor
251 if (iter == invSensors.end())
252 {
253 log<level::ERR>("System event sensor not found");
254 elog<InternalFailure>();
255 }
256 }
257 }
258
259 if (isFromSELLogger)
Tom Joseph6b7a1432017-05-19 10:43:36 +0530260 {
Lei YUaf378fa2020-12-02 16:28:57 +0800261 // It is expected to be a custom SEL entry
Lei YU07c15b72020-12-06 15:08:06 +0800262 auto recordType = static_cast<uint8_t>(convert(m[strRecordType]));
Lei YU5015e952020-12-03 14:01:31 +0800263 auto isOEM = isRecordOEM(recordType);
264 if (isOEM)
265 {
Lei YU07c15b72020-12-06 15:08:06 +0800266 constructOEMSEL(recordType, chronoTimeStamp, m, record);
Lei YU5015e952020-12-03 14:01:31 +0800267 }
268 else
269 {
Lei YU07c15b72020-12-06 15:08:06 +0800270 constructSEL(recordType, chronoTimeStamp, m, entryData, record);
Lei YU5015e952020-12-03 14:01:31 +0800271 }
Tom Joseph6b7a1432017-05-19 10:43:36 +0530272 }
273 else
274 {
Lei YUaf378fa2020-12-02 16:28:57 +0800275 record.event.eventRecord.timeStamp = static_cast<uint32_t>(
276 std::chrono::duration_cast<std::chrono::seconds>(chronoTimeStamp)
277 .count());
278
Lei YUaf378fa2020-12-02 16:28:57 +0800279 record.event.eventRecord.recordType = systemEventRecord;
280 record.event.eventRecord.generatorID = generatorID;
281 record.event.eventRecord.eventMsgRevision = eventMsgRevision;
282
283 record.event.eventRecord.sensorType = iter->second.sensorType;
284 record.event.eventRecord.sensorNum = iter->second.sensorID;
285 record.event.eventRecord.eventData1 = iter->second.eventOffset;
286
287 // Read Resolved from the log entry.
Lei YUaf378fa2020-12-02 16:28:57 +0800288 auto iterResolved = entryData.find(propResolved);
289 if (iterResolved == entryData.end())
290 {
291 log<level::ERR>("Error in reading Resolved field of logging entry");
292 elog<InternalFailure>();
293 }
294
Lei YUaf378fa2020-12-02 16:28:57 +0800295 // Evaluate if the event is assertion or deassertion event
296 if (std::get<bool>(iterResolved->second))
297 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500298 record.event.eventRecord.eventType = deassertEvent |
299 iter->second.eventReadingType;
Lei YUaf378fa2020-12-02 16:28:57 +0800300 }
301 else
302 {
303 record.event.eventRecord.eventType = iter->second.eventReadingType;
304 }
Tom Joseph6b7a1432017-05-19 10:43:36 +0530305 }
306
307 return record;
308}
309
310} // namespace internal
311
Tom Joseph6edc8a02017-06-30 18:52:56 +0530312GetSELEntryResponse convertLogEntrytoSEL(const std::string& objPath)
313{
Patrick Williams5d82f472022-07-22 19:26:53 -0500314 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph6edc8a02017-06-30 18:52:56 +0530315
Vernon Mauerya7f81cc2019-11-06 12:51:43 -0800316 static constexpr auto assocIntf =
317 "xyz.openbmc_project.Association.Definitions";
318 static constexpr auto assocProp = "Associations";
Tom Joseph6edc8a02017-06-30 18:52:56 +0530319
George Liu42f64ef2024-02-05 15:03:18 +0800320 std::vector<ipmi::Association> assocs;
George Liu3e3cc352023-07-26 15:59:31 +0800321 try
322 {
George Liu42f64ef2024-02-05 15:03:18 +0800323 auto service = ipmi::getService(bus, assocIntf, objPath);
324 auto propValue = ipmi::getDbusProperty(bus, service, objPath, assocIntf,
325 assocProp);
326 assocs = std::get<std::vector<ipmi::Association>>(propValue);
George Liu3e3cc352023-07-26 15:59:31 +0800327 }
328 catch (const std::exception& e)
329 {
330 log<level::ERR>("Error in reading Associations interface",
331 entry("ERROR=%s", e.what()));
332 elog<InternalFailure>();
333 }
Tom Joseph6edc8a02017-06-30 18:52:56 +0530334
Tom Joseph6edc8a02017-06-30 18:52:56 +0530335 /*
336 * Check if the log entry has any callout associations, if there is a
337 * callout association try to match the inventory path to the corresponding
338 * IPMI sensor.
339 */
340 for (const auto& item : assocs)
341 {
342 if (std::get<0>(item).compare(CALLOUT_FWD_ASSOCIATION) == 0)
343 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700344 auto iter = invSensors.find(std::get<2>(item));
345 if (iter == invSensors.end())
346 {
347 iter = invSensors.find(BOARD_SENSOR);
348 if (iter == invSensors.end())
349 {
350 log<level::ERR>("Motherboard sensor not found");
351 elog<InternalFailure>();
352 }
353 }
Tom Joseph6edc8a02017-06-30 18:52:56 +0530354
Patrick Venture0b02be92018-08-31 11:55:55 -0700355 return internal::prepareSELEntry(objPath, iter);
Tom Joseph6edc8a02017-06-30 18:52:56 +0530356 }
357 }
358
359 // If there are no callout associations link the log entry to system event
360 // sensor
361 auto iter = invSensors.find(SYSTEM_SENSOR);
Tom Joseph6edc8a02017-06-30 18:52:56 +0530362 return internal::prepareSELEntry(objPath, iter);
363}
364
Tom Joseph399fd922017-06-30 18:40:30 +0530365std::chrono::seconds getEntryTimeStamp(const std::string& objPath)
366{
Patrick Williams5d82f472022-07-22 19:26:53 -0500367 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph399fd922017-06-30 18:40:30 +0530368
George Liu42f64ef2024-02-05 15:03:18 +0800369 static constexpr auto propTimeStamp = "Timestamp";
Tom Joseph399fd922017-06-30 18:40:30 +0530370
George Liu42f64ef2024-02-05 15:03:18 +0800371 uint64_t timeStamp;
George Liu3e3cc352023-07-26 15:59:31 +0800372 try
Tom Joseph399fd922017-06-30 18:40:30 +0530373 {
George Liu42f64ef2024-02-05 15:03:18 +0800374 auto service = ipmi::getService(bus, logEntryIntf, objPath);
375 auto propValue = ipmi::getDbusProperty(bus, service, objPath,
376 logEntryIntf, propTimeStamp);
377 timeStamp = std::get<uint64_t>(propValue);
George Liu3e3cc352023-07-26 15:59:31 +0800378 }
379 catch (const std::exception& e)
380 {
381 log<level::ERR>("Error in reading Timestamp from Entry interface",
382 entry("ERROR=%s", e.what()));
Tom Joseph399fd922017-06-30 18:40:30 +0530383 elog<InternalFailure>();
384 }
385
George Liu42f64ef2024-02-05 15:03:18 +0800386 std::chrono::milliseconds chronoTimeStamp(timeStamp);
Tom Joseph399fd922017-06-30 18:40:30 +0530387
388 return std::chrono::duration_cast<std::chrono::seconds>(chronoTimeStamp);
389}
390
Tom Joseph232f5292017-07-07 20:14:02 +0530391void readLoggingObjectPaths(ObjectPaths& paths)
392{
Patrick Williams5d82f472022-07-22 19:26:53 -0500393 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph232f5292017-07-07 20:14:02 +0530394 auto depth = 0;
395 paths.clear();
396
Patrick Venture0b02be92018-08-31 11:55:55 -0700397 auto mapperCall = bus.new_method_call(mapperBusName, mapperObjPath,
398 mapperIntf, "GetSubTreePaths");
Tom Joseph232f5292017-07-07 20:14:02 +0530399 mapperCall.append(logBasePath);
400 mapperCall.append(depth);
401 mapperCall.append(ObjectPaths({logEntryIntf}));
402
Konstantin Aladyshev49434b62022-01-10 16:58:47 +0300403 try
Tom Joseph232f5292017-07-07 20:14:02 +0530404 {
Konstantin Aladyshev49434b62022-01-10 16:58:47 +0300405 auto reply = bus.call(mapperCall);
Tom Joseph232f5292017-07-07 20:14:02 +0530406 reply.read(paths);
Tom Joseph232f5292017-07-07 20:14:02 +0530407 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500408 catch (const sdbusplus::exception_t& e)
Konstantin Aladyshev49434b62022-01-10 16:58:47 +0300409 {
410 if (strcmp(e.name(),
411 "xyz.openbmc_project.Common.Error.ResourceNotFound"))
412 {
413 throw;
414 }
415 }
416
417 std::sort(paths.begin(), paths.end(),
418 [](const std::string& a, const std::string& b) {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500419 namespace fs = std::filesystem;
420 fs::path pathA(a);
421 fs::path pathB(b);
422 auto idA = std::stoul(pathA.filename().string());
423 auto idB = std::stoul(pathB.filename().string());
Konstantin Aladyshev49434b62022-01-10 16:58:47 +0300424
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500425 return idA < idB;
426 });
Tom Joseph232f5292017-07-07 20:14:02 +0530427}
428
Tom Joseph6b7a1432017-05-19 10:43:36 +0530429} // namespace sel
430
431} // namespace ipmi