blob: 6e8d8703250c46916a6097c7993e8500389c5ad6 [file] [log] [blame]
Alexander Hansen40fb5492025-10-28 17:56:12 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2019 IBM Corporation
3
Matt Spinlerd3335df2019-07-10 11:04:21 -05004#include "private_header.hpp"
5
Harisuddin Mohamed Isa2ecb4652020-01-10 10:51:02 +08006#include "json_utils.hpp"
Matt Spinler289aa472019-09-20 12:33:29 -05007#include "log_id.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -05008#include "pel_types.hpp"
Aatirc92b4eb2019-11-18 13:44:51 -06009#include "pel_values.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050010
Arya K Padman5bc26532024-04-10 06:19:25 -050011#include <phosphor-logging/lg2.hpp>
Matt Spinlerd3335df2019-07-10 11:04:21 -050012
13namespace openpower
14{
15namespace pels
16{
17
Aatirc92b4eb2019-11-18 13:44:51 -060018namespace pv = openpower::pels::pel_values;
Matt Spinlerd3335df2019-07-10 11:04:21 -050019
Matt Spinler289aa472019-09-20 12:33:29 -050020PrivateHeader::PrivateHeader(uint16_t componentID, uint32_t obmcLogID,
21 uint64_t timestamp)
22{
23 _header.id = static_cast<uint16_t>(SectionID::privateHeader);
24 _header.size = PrivateHeader::flattenedSize();
25 _header.version = privateHeaderVersion;
26 _header.subType = 0;
27 _header.componentID = componentID;
28
29 _createTimestamp = getBCDTime(timestamp);
30
31 auto now = std::chrono::system_clock::now();
32 _commitTimestamp = getBCDTime(now);
33
34 _creatorID = static_cast<uint8_t>(CreatorID::openBMC);
35
36 // Add support for reminder and telemetry log types here if
37 // ever necessary.
38 _logType = 0;
39
40 _reservedByte = 0;
41
42 // the final section count will be updated later
43 _sectionCount = 1;
44
45 _obmcLogID = obmcLogID;
46
47 _id = generatePELID();
48
49 _plid = _id;
50
51 // Leave _creatorVersion at 0
52
53 _valid = true;
54}
55
Matt Spinler31eed992019-10-09 14:07:52 -050056PrivateHeader::PrivateHeader(Stream& pel) :
57 _creatorID(0), _logType(0), _reservedByte(0), _sectionCount(0),
58 _obmcLogID(0), _plid(0), _id(0)
Matt Spinlerd3335df2019-07-10 11:04:21 -050059{
60 try
61 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -050062 unflatten(pel);
Matt Spinlerd3335df2019-07-10 11:04:21 -050063 validate();
64 }
65 catch (const std::exception& e)
66 {
Arya K Padman5bc26532024-04-10 06:19:25 -050067 lg2::error("Cannot unflatten private header: {EXCEPTION}", "EXCEPTION",
68 e);
Matt Spinlerd3335df2019-07-10 11:04:21 -050069 _valid = false;
70 }
71}
Matt Spinlerb832aa52023-03-21 15:32:34 -050072std::optional<std::string> PrivateHeader::getJSON(uint8_t creatorID) const
Aatirc92b4eb2019-11-18 13:44:51 -060073{
74 char tmpPhVal[50];
Harisuddin Mohamed Isa160c51c2020-02-13 23:42:20 +080075 sprintf(tmpPhVal, "%02X/%02X/%02X%02X %02X:%02X:%02X",
Aatirc92b4eb2019-11-18 13:44:51 -060076 _createTimestamp.month, _createTimestamp.day,
77 _createTimestamp.yearMSB, _createTimestamp.yearLSB,
78 _createTimestamp.hour, _createTimestamp.minutes,
79 _createTimestamp.seconds);
80 std::string phCreateTStr(tmpPhVal);
Harisuddin Mohamed Isa160c51c2020-02-13 23:42:20 +080081 sprintf(tmpPhVal, "%02X/%02X/%02X%02X %02X:%02X:%02X",
Aatirc92b4eb2019-11-18 13:44:51 -060082 _commitTimestamp.month, _commitTimestamp.day,
Sumit Kumar74348c62021-10-26 05:48:28 -050083 _commitTimestamp.yearMSB, _commitTimestamp.yearLSB,
Aatirc92b4eb2019-11-18 13:44:51 -060084 _commitTimestamp.hour, _commitTimestamp.minutes,
85 _commitTimestamp.seconds);
86 std::string phCommitTStr(tmpPhVal);
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +080087 std::string creator = getNumberString("%c", _creatorID);
Aatirc92b4eb2019-11-18 13:44:51 -060088 creator = pv::creatorIDs.count(creator) ? pv::creatorIDs.at(creator)
89 : "Unknown CreatorID";
90 std::string phCreatorVersionStr =
91 std::string(reinterpret_cast<const char*>(_creatorVersion.version));
Matt Spinlerd3335df2019-07-10 11:04:21 -050092
Harisuddin Mohamed Isa2ecb4652020-01-10 10:51:02 +080093 std::string ph;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +080094 jsonInsert(ph, pv::sectionVer, getNumberString("%d", privateHeaderVersion),
95 1);
96 jsonInsert(ph, pv::subSection, getNumberString("%d", _header.subType), 1);
Matt Spinlerb832aa52023-03-21 15:32:34 -050097 jsonInsert(ph, pv::createdBy,
98 getComponentName(_header.componentID, creatorID), 1);
Harisuddin Mohamed Isa2ecb4652020-01-10 10:51:02 +080099 jsonInsert(ph, "Created at", phCreateTStr, 1);
100 jsonInsert(ph, "Committed at", phCommitTStr, 1);
101 jsonInsert(ph, "Creator Subsystem", creator, 1);
102 jsonInsert(ph, "CSSVER", phCreatorVersionStr, 1);
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800103 jsonInsert(ph, "Platform Log Id", getNumberString("0x%X", _plid), 1);
104 jsonInsert(ph, "Entry Id", getNumberString("0x%X", _id), 1);
105 jsonInsert(ph, "BMC Event Log Id", std::to_string(_obmcLogID), 1);
Harisuddin Mohamed Isa2ecb4652020-01-10 10:51:02 +0800106 ph.erase(ph.size() - 2);
Aatirc92b4eb2019-11-18 13:44:51 -0600107
108 return ph;
109}
Matt Spinlerd3335df2019-07-10 11:04:21 -0500110void PrivateHeader::validate()
111{
112 bool failed = false;
113
Matt Spinler1a94cc32019-09-11 13:32:12 -0500114 if (header().id != static_cast<uint16_t>(SectionID::privateHeader))
Matt Spinlerd3335df2019-07-10 11:04:21 -0500115 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500116 lg2::error("Invalid private header section ID: {HEADER_ID}",
117 "HEADER_ID", lg2::hex, header().id);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500118 failed = true;
119 }
120
121 if (header().version != privateHeaderVersion)
122 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500123 lg2::error("Invalid private header version: {HEADER_VERSION}",
124 "HEADER_VERSION", lg2::hex, header().version);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500125 failed = true;
126 }
127
128 if (_sectionCount < minSectionCount)
129 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500130 lg2::error("Invalid section count in private header: {SECTION_COUNT}",
131 "SECTION_COUNT", lg2::hex, _sectionCount);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500132 failed = true;
133 }
134
135 _valid = (failed) ? false : true;
136}
137
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500138void PrivateHeader::unflatten(Stream& stream)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500139{
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500140 stream >> _header >> _createTimestamp >> _commitTimestamp >> _creatorID >>
141 _logType >> _reservedByte >> _sectionCount >> _obmcLogID >>
142 _creatorVersion >> _plid >> _id;
Matt Spinlerd3335df2019-07-10 11:04:21 -0500143}
144
Matt Spinler06885452019-11-06 10:35:42 -0600145void PrivateHeader::flatten(Stream& stream) const
Matt Spinlerd3335df2019-07-10 11:04:21 -0500146{
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500147 stream << _header << _createTimestamp << _commitTimestamp << _creatorID
148 << _logType << _reservedByte << _sectionCount << _obmcLogID
149 << _creatorVersion << _plid << _id;
Matt Spinlerd3335df2019-07-10 11:04:21 -0500150}
151
152Stream& operator>>(Stream& s, CreatorVersion& cv)
153{
154 for (size_t i = 0; i < sizeof(CreatorVersion); i++)
155 {
156 s >> cv.version[i];
157 }
158 return s;
159}
160
Matt Spinler06885452019-11-06 10:35:42 -0600161Stream& operator<<(Stream& s, const CreatorVersion& cv)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500162{
163 for (size_t i = 0; i < sizeof(CreatorVersion); i++)
164 {
165 s << cv.version[i];
166 }
167 return s;
168}
169
170} // namespace pels
171} // namespace openpower