blob: ef1b638d296832597674aa372a355b45b01116f1 [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>
George Liu7acfcf02024-07-17 20:14:56 +08009#include <phosphor-logging/lg2.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070010#include <xyz/openbmc_project/Common/error.hpp>
Patrick Venture46470a32018-09-07 19:26:25 -070011
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050012#include <charconv>
13#include <chrono>
14#include <filesystem>
15#include <vector>
16
Tom Joseph6b7a1432017-05-19 10:43:36 +053017extern const ipmi::sensor::InvObjectIDMap invSensors;
18using namespace phosphor::logging;
19using InternalFailure =
Willy Tu523e2d12023-09-05 11:36:48 -070020 sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
Tom Joseph6b7a1432017-05-19 10:43:36 +053021
Lei YU5015e952020-12-03 14:01:31 +080022namespace
23{
24
25constexpr auto systemEventRecord = 0x02;
Lei YU07c15b72020-12-06 15:08:06 +080026constexpr auto generatorID = 0x2000;
27constexpr auto eventMsgRevision = 0x04;
28constexpr auto assertEvent = 0x00;
29constexpr auto deassertEvent = 0x80;
30constexpr auto selDataSize = 3;
31constexpr auto oemCDDataSize = 9;
32constexpr auto oemEFDataSize = 13;
Lei YU5015e952020-12-03 14:01:31 +080033
Patrick Williams39deff22024-12-11 21:12:59 -050034constexpr auto propAdditionalData = "AdditionalData";
Lei YU07c15b72020-12-06 15:08:06 +080035constexpr auto propResolved = "Resolved";
36
Lei YU719a41c2020-12-03 17:50:44 +080037constexpr auto strEventDir = "EVENT_DIR";
38constexpr auto strGenerateId = "GENERATOR_ID";
39constexpr auto strRecordType = "RECORD_TYPE";
40constexpr auto strSensorData = "SENSOR_DATA";
41constexpr auto strSensorPath = "SENSOR_PATH";
42
Lei YU5015e952020-12-03 14:01:31 +080043} // namespace
44
Tom Joseph6b7a1432017-05-19 10:43:36 +053045namespace ipmi
46{
47
48namespace sel
49{
50
51namespace internal
52{
53
Lei YU5015e952020-12-03 14:01:31 +080054inline bool isRecordOEM(uint8_t recordType)
55{
56 return recordType != systemEventRecord;
57}
58
Lei YU719a41c2020-12-03 17:50:44 +080059using additionalDataMap = std::map<std::string, std::string>;
Lei YU07c15b72020-12-06 15:08:06 +080060using entryDataMap = std::map<PropertyName, PropertyType>;
Lei YU5015e952020-12-03 14:01:31 +080061
Lei YU07c15b72020-12-06 15:08:06 +080062int convert(const std::string_view& str, int base = 10)
Lei YU5015e952020-12-03 14:01:31 +080063{
William A. Kennington III4c521022023-07-28 13:07:30 -070064 int ret = 0;
Lei YU5015e952020-12-03 14:01:31 +080065 std::from_chars(str.data(), str.data() + str.size(), ret, base);
Lei YU07c15b72020-12-06 15:08:06 +080066 return ret;
Lei YU5015e952020-12-03 14:01:31 +080067}
68
69// Convert the string to a vector of uint8_t, where the str is formatted as hex
70std::vector<uint8_t> convertVec(const std::string_view& str)
71{
72 std::vector<uint8_t> ret;
73 auto len = str.size() / 2;
74 ret.reserve(len);
75 for (size_t i = 0; i < len; ++i)
76 {
Lei YU07c15b72020-12-06 15:08:06 +080077 ret.emplace_back(
78 static_cast<uint8_t>(convert(str.substr(i * 2, 2), 16)));
Lei YU5015e952020-12-03 14:01:31 +080079 }
80 return ret;
81}
82
Lei YU719a41c2020-12-03 17:50:44 +080083/** Construct OEM SEL record according to IPMI spec 32.2, 32.3. */
Lei YU07c15b72020-12-06 15:08:06 +080084void constructOEMSEL(uint8_t recordType, std::chrono::milliseconds timestamp,
Lei YU719a41c2020-12-03 17:50:44 +080085 const additionalDataMap& m, GetSELEntryResponse& record)
86{
87 auto dataIter = m.find(strSensorData);
88 assert(dataIter != m.end());
89 auto sensorData = convertVec(dataIter->second);
90 if (recordType >= 0xC0 && recordType < 0xE0)
91 {
92 record.event.oemCD.timeStamp = static_cast<uint32_t>(
93 std::chrono::duration_cast<std::chrono::seconds>(timestamp)
94 .count());
95 record.event.oemCD.recordType = recordType;
96 // The ManufactureID and OEM Defined are packed in the sensor data
97 // Fill the 9 bytes of Manufacture ID and oemDefined
98 memcpy(&record.event.oemCD.manufacturerID, sensorData.data(),
Lei YU07c15b72020-12-06 15:08:06 +080099 std::min(sensorData.size(), static_cast<size_t>(oemCDDataSize)));
Lei YU719a41c2020-12-03 17:50:44 +0800100 }
101 else if (recordType >= 0xE0)
102 {
103 record.event.oemEF.recordType = recordType;
104 // The remaining 13 bytes are the OEM Defined data
105 memcpy(&record.event.oemEF.oemDefined, sensorData.data(),
Lei YU07c15b72020-12-06 15:08:06 +0800106 std::min(sensorData.size(), static_cast<size_t>(oemEFDataSize)));
Lei YU719a41c2020-12-03 17:50:44 +0800107 }
108}
109
Lei YU07c15b72020-12-06 15:08:06 +0800110void constructSEL(uint8_t recordType, std::chrono::milliseconds timestamp,
Willy Tu11d68892022-01-20 10:37:34 -0800111 const additionalDataMap& m, const entryDataMap&,
Lei YU07c15b72020-12-06 15:08:06 +0800112 GetSELEntryResponse& record)
113{
114 if (recordType != systemEventRecord)
115 {
George Liu7acfcf02024-07-17 20:14:56 +0800116 lg2::error("Invalid recordType");
Lei YU07c15b72020-12-06 15:08:06 +0800117 elog<InternalFailure>();
118 }
119
120 // Default values when there is no matched sensor
121 record.event.eventRecord.sensorType = 0;
122 record.event.eventRecord.sensorNum = 0xFF;
123 record.event.eventRecord.eventType = 0;
124
125 auto iter = m.find(strSensorPath);
126 assert(iter != m.end());
127 const auto& sensorPath = iter->second;
128 auto sensorIter = invSensors.find(sensorPath);
129
130 if (sensorIter != invSensors.end())
131 {
132 // There is a matched sensor
133 record.event.eventRecord.sensorType = sensorIter->second.sensorType;
134 record.event.eventRecord.sensorNum = sensorIter->second.sensorID;
135
136 iter = m.find(strEventDir);
137 assert(iter != m.end());
138 auto eventDir = static_cast<uint8_t>(convert(iter->second));
139 uint8_t assert = eventDir ? assertEvent : deassertEvent;
140 record.event.eventRecord.eventType =
141 assert | sensorIter->second.eventReadingType;
142 }
143 record.event.eventRecord.recordType = recordType;
144 record.event.eventRecord.timeStamp = static_cast<uint32_t>(
145 std::chrono::duration_cast<std::chrono::seconds>(timestamp).count());
146 iter = m.find(strGenerateId);
147 assert(iter != m.end());
148 record.event.eventRecord.generatorID =
149 static_cast<uint16_t>(convert(iter->second));
150 record.event.eventRecord.eventMsgRevision = eventMsgRevision;
151 iter = m.find(strSensorData);
152 assert(iter != m.end());
153 auto sensorData = convertVec(iter->second);
154 // The remaining 3 bytes are the sensor data
155 memcpy(&record.event.eventRecord.eventData1, sensorData.data(),
156 std::min(sensorData.size(), static_cast<size_t>(selDataSize)));
157}
158
Patrick Venture0b02be92018-08-31 11:55:55 -0700159GetSELEntryResponse
160 prepareSELEntry(const std::string& objPath,
161 ipmi::sensor::InvObjectIDMap::const_iterator iter)
Tom Joseph6b7a1432017-05-19 10:43:36 +0530162{
Patrick Venture0b02be92018-08-31 11:55:55 -0700163 GetSELEntryResponse record{};
Tom Joseph6b7a1432017-05-19 10:43:36 +0530164
Patrick Williams5d82f472022-07-22 19:26:53 -0500165 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph6b7a1432017-05-19 10:43:36 +0530166 auto service = ipmi::getService(bus, logEntryIntf, objPath);
167
168 // Read all the log entry properties.
Patrick Venture0b02be92018-08-31 11:55:55 -0700169 auto methodCall = bus.new_method_call(service.c_str(), objPath.c_str(),
170 propIntf, "GetAll");
Tom Joseph6b7a1432017-05-19 10:43:36 +0530171 methodCall.append(logEntryIntf);
172
George Liu3e3cc352023-07-26 15:59:31 +0800173 entryDataMap entryData;
174 try
Tom Joseph6b7a1432017-05-19 10:43:36 +0530175 {
George Liu3e3cc352023-07-26 15:59:31 +0800176 auto reply = bus.call(methodCall);
177 reply.read(entryData);
178 }
179 catch (const std::exception& e)
180 {
George Liu7acfcf02024-07-17 20:14:56 +0800181 lg2::error("Error in reading logging property entries: {ERROR}",
182 "ERROR", e);
Tom Joseph6b7a1432017-05-19 10:43:36 +0530183 elog<InternalFailure>();
184 }
185
Tom Joseph6b7a1432017-05-19 10:43:36 +0530186 // Read Id from the log entry.
187 static constexpr auto propId = "Id";
188 auto iterId = entryData.find(propId);
189 if (iterId == entryData.end())
190 {
George Liu7acfcf02024-07-17 20:14:56 +0800191 lg2::error("Error in reading Id of logging entry");
Tom Joseph6b7a1432017-05-19 10:43:36 +0530192 elog<InternalFailure>();
193 }
194
Tom Joseph6b7a1432017-05-19 10:43:36 +0530195 // Read Timestamp from the log entry.
196 static constexpr auto propTimeStamp = "Timestamp";
197 auto iterTimeStamp = entryData.find(propTimeStamp);
198 if (iterTimeStamp == entryData.end())
199 {
George Liu7acfcf02024-07-17 20:14:56 +0800200 lg2::error("Error in reading Timestamp of logging entry");
Tom Joseph6b7a1432017-05-19 10:43:36 +0530201 elog<InternalFailure>();
202 }
Tom Joseph6b7a1432017-05-19 10:43:36 +0530203 std::chrono::milliseconds chronoTimeStamp(
Vernon Maueryf442e112019-04-09 11:44:36 -0700204 std::get<uint64_t>(iterTimeStamp->second));
Tom Joseph6b7a1432017-05-19 10:43:36 +0530205
Lei YU690f4d52021-04-28 19:05:24 +0800206 bool isFromSELLogger = false;
207 additionalDataMap m;
208
209 // The recordID are with the same offset between different types,
210 // so we are safe to set the recordID here
211 record.event.eventRecord.recordID =
212 static_cast<uint16_t>(std::get<uint32_t>(iterId->second));
213
214 iterId = entryData.find(propAdditionalData);
215 if (iterId != entryData.end())
216 {
217 // Check if it's a SEL from phosphor-sel-logger which shall contain
218 // the record ID, etc
Patrick Williams39deff22024-12-11 21:12:59 -0500219 const auto& addData = std::get<AdditionalData>(iterId->second);
Patrick Williamsbe5a2e02024-11-22 11:23:17 -0500220 auto recordTypeIter = addData.find(strRecordType);
221 if (recordTypeIter != addData.end())
Lei YU690f4d52021-04-28 19:05:24 +0800222 {
223 // It is a SEL from phosphor-sel-logger
224 isFromSELLogger = true;
225 }
226 else
227 {
228 // Not a SEL from phosphor-sel-logger, it shall have a valid
229 // invSensor
230 if (iter == invSensors.end())
231 {
George Liu7acfcf02024-07-17 20:14:56 +0800232 lg2::error("System event sensor not found");
Lei YU690f4d52021-04-28 19:05:24 +0800233 elog<InternalFailure>();
234 }
235 }
236 }
237
238 if (isFromSELLogger)
Tom Joseph6b7a1432017-05-19 10:43:36 +0530239 {
Lei YUaf378fa2020-12-02 16:28:57 +0800240 // It is expected to be a custom SEL entry
Lei YU07c15b72020-12-06 15:08:06 +0800241 auto recordType = static_cast<uint8_t>(convert(m[strRecordType]));
Lei YU5015e952020-12-03 14:01:31 +0800242 auto isOEM = isRecordOEM(recordType);
243 if (isOEM)
244 {
Lei YU07c15b72020-12-06 15:08:06 +0800245 constructOEMSEL(recordType, chronoTimeStamp, m, record);
Lei YU5015e952020-12-03 14:01:31 +0800246 }
247 else
248 {
Lei YU07c15b72020-12-06 15:08:06 +0800249 constructSEL(recordType, chronoTimeStamp, m, entryData, record);
Lei YU5015e952020-12-03 14:01:31 +0800250 }
Tom Joseph6b7a1432017-05-19 10:43:36 +0530251 }
252 else
253 {
Lei YUaf378fa2020-12-02 16:28:57 +0800254 record.event.eventRecord.timeStamp = static_cast<uint32_t>(
255 std::chrono::duration_cast<std::chrono::seconds>(chronoTimeStamp)
256 .count());
257
Lei YUaf378fa2020-12-02 16:28:57 +0800258 record.event.eventRecord.recordType = systemEventRecord;
259 record.event.eventRecord.generatorID = generatorID;
260 record.event.eventRecord.eventMsgRevision = eventMsgRevision;
261
262 record.event.eventRecord.sensorType = iter->second.sensorType;
263 record.event.eventRecord.sensorNum = iter->second.sensorID;
264 record.event.eventRecord.eventData1 = iter->second.eventOffset;
265
266 // Read Resolved from the log entry.
Lei YUaf378fa2020-12-02 16:28:57 +0800267 auto iterResolved = entryData.find(propResolved);
268 if (iterResolved == entryData.end())
269 {
George Liu7acfcf02024-07-17 20:14:56 +0800270 lg2::error("Error in reading Resolved field of logging entry");
Lei YUaf378fa2020-12-02 16:28:57 +0800271 elog<InternalFailure>();
272 }
273
Lei YUaf378fa2020-12-02 16:28:57 +0800274 // Evaluate if the event is assertion or deassertion event
275 if (std::get<bool>(iterResolved->second))
276 {
Patrick Williams1318a5e2024-08-16 15:19:54 -0400277 record.event.eventRecord.eventType =
278 deassertEvent | iter->second.eventReadingType;
Lei YUaf378fa2020-12-02 16:28:57 +0800279 }
280 else
281 {
282 record.event.eventRecord.eventType = iter->second.eventReadingType;
283 }
Tom Joseph6b7a1432017-05-19 10:43:36 +0530284 }
285
286 return record;
287}
288
289} // namespace internal
290
Tom Joseph6edc8a02017-06-30 18:52:56 +0530291GetSELEntryResponse convertLogEntrytoSEL(const std::string& objPath)
292{
Patrick Williams5d82f472022-07-22 19:26:53 -0500293 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph6edc8a02017-06-30 18:52:56 +0530294
Vernon Mauerya7f81cc2019-11-06 12:51:43 -0800295 static constexpr auto assocIntf =
296 "xyz.openbmc_project.Association.Definitions";
297 static constexpr auto assocProp = "Associations";
Tom Joseph6edc8a02017-06-30 18:52:56 +0530298
George Liu42f64ef2024-02-05 15:03:18 +0800299 std::vector<ipmi::Association> assocs;
George Liu3e3cc352023-07-26 15:59:31 +0800300 try
301 {
George Liu42f64ef2024-02-05 15:03:18 +0800302 auto service = ipmi::getService(bus, assocIntf, objPath);
Patrick Williams1318a5e2024-08-16 15:19:54 -0400303 auto propValue =
304 ipmi::getDbusProperty(bus, service, objPath, assocIntf, assocProp);
George Liu42f64ef2024-02-05 15:03:18 +0800305 assocs = std::get<std::vector<ipmi::Association>>(propValue);
George Liu3e3cc352023-07-26 15:59:31 +0800306 }
307 catch (const std::exception& e)
308 {
George Liu7acfcf02024-07-17 20:14:56 +0800309 lg2::error("Error in reading Associations interface: {ERROR}", "ERROR",
310 e);
George Liu3e3cc352023-07-26 15:59:31 +0800311 elog<InternalFailure>();
312 }
Tom Joseph6edc8a02017-06-30 18:52:56 +0530313
Tom Joseph6edc8a02017-06-30 18:52:56 +0530314 /*
315 * Check if the log entry has any callout associations, if there is a
316 * callout association try to match the inventory path to the corresponding
317 * IPMI sensor.
318 */
319 for (const auto& item : assocs)
320 {
321 if (std::get<0>(item).compare(CALLOUT_FWD_ASSOCIATION) == 0)
322 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700323 auto iter = invSensors.find(std::get<2>(item));
324 if (iter == invSensors.end())
325 {
326 iter = invSensors.find(BOARD_SENSOR);
327 if (iter == invSensors.end())
328 {
George Liu7acfcf02024-07-17 20:14:56 +0800329 lg2::error("Motherboard sensor not found");
Patrick Venture0b02be92018-08-31 11:55:55 -0700330 elog<InternalFailure>();
331 }
332 }
Tom Joseph6edc8a02017-06-30 18:52:56 +0530333
Patrick Venture0b02be92018-08-31 11:55:55 -0700334 return internal::prepareSELEntry(objPath, iter);
Tom Joseph6edc8a02017-06-30 18:52:56 +0530335 }
336 }
337
338 // If there are no callout associations link the log entry to system event
339 // sensor
340 auto iter = invSensors.find(SYSTEM_SENSOR);
Tom Joseph6edc8a02017-06-30 18:52:56 +0530341 return internal::prepareSELEntry(objPath, iter);
342}
343
Tom Joseph399fd922017-06-30 18:40:30 +0530344std::chrono::seconds getEntryTimeStamp(const std::string& objPath)
345{
Patrick Williams5d82f472022-07-22 19:26:53 -0500346 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph399fd922017-06-30 18:40:30 +0530347
George Liu42f64ef2024-02-05 15:03:18 +0800348 static constexpr auto propTimeStamp = "Timestamp";
Tom Joseph399fd922017-06-30 18:40:30 +0530349
George Liu42f64ef2024-02-05 15:03:18 +0800350 uint64_t timeStamp;
George Liu3e3cc352023-07-26 15:59:31 +0800351 try
Tom Joseph399fd922017-06-30 18:40:30 +0530352 {
George Liu42f64ef2024-02-05 15:03:18 +0800353 auto service = ipmi::getService(bus, logEntryIntf, objPath);
354 auto propValue = ipmi::getDbusProperty(bus, service, objPath,
355 logEntryIntf, propTimeStamp);
356 timeStamp = std::get<uint64_t>(propValue);
George Liu3e3cc352023-07-26 15:59:31 +0800357 }
358 catch (const std::exception& e)
359 {
George Liu7acfcf02024-07-17 20:14:56 +0800360 lg2::error("Error in reading Timestamp from Entry interface: {ERROR}",
361 "ERROR", e);
Tom Joseph399fd922017-06-30 18:40:30 +0530362 elog<InternalFailure>();
363 }
364
George Liu42f64ef2024-02-05 15:03:18 +0800365 std::chrono::milliseconds chronoTimeStamp(timeStamp);
Tom Joseph399fd922017-06-30 18:40:30 +0530366
367 return std::chrono::duration_cast<std::chrono::seconds>(chronoTimeStamp);
368}
369
Tom Joseph232f5292017-07-07 20:14:02 +0530370void readLoggingObjectPaths(ObjectPaths& paths)
371{
Patrick Williams5d82f472022-07-22 19:26:53 -0500372 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph232f5292017-07-07 20:14:02 +0530373 auto depth = 0;
374 paths.clear();
375
Patrick Venture0b02be92018-08-31 11:55:55 -0700376 auto mapperCall = bus.new_method_call(mapperBusName, mapperObjPath,
377 mapperIntf, "GetSubTreePaths");
Tom Joseph232f5292017-07-07 20:14:02 +0530378 mapperCall.append(logBasePath);
379 mapperCall.append(depth);
380 mapperCall.append(ObjectPaths({logEntryIntf}));
381
Konstantin Aladyshev49434b62022-01-10 16:58:47 +0300382 try
Tom Joseph232f5292017-07-07 20:14:02 +0530383 {
Konstantin Aladyshev49434b62022-01-10 16:58:47 +0300384 auto reply = bus.call(mapperCall);
Tom Joseph232f5292017-07-07 20:14:02 +0530385 reply.read(paths);
Tom Joseph232f5292017-07-07 20:14:02 +0530386 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500387 catch (const sdbusplus::exception_t& e)
Konstantin Aladyshev49434b62022-01-10 16:58:47 +0300388 {
389 if (strcmp(e.name(),
390 "xyz.openbmc_project.Common.Error.ResourceNotFound"))
391 {
392 throw;
393 }
394 }
395
396 std::sort(paths.begin(), paths.end(),
397 [](const std::string& a, const std::string& b) {
Patrick Williams1318a5e2024-08-16 15:19:54 -0400398 namespace fs = std::filesystem;
399 fs::path pathA(a);
400 fs::path pathB(b);
401 auto idA = std::stoul(pathA.filename().string());
402 auto idB = std::stoul(pathB.filename().string());
Konstantin Aladyshev49434b62022-01-10 16:58:47 +0300403
Patrick Williams1318a5e2024-08-16 15:19:54 -0400404 return idA < idB;
405 });
Tom Joseph232f5292017-07-07 20:14:02 +0530406}
407
Tom Joseph6b7a1432017-05-19 10:43:36 +0530408} // namespace sel
409
410} // namespace ipmi