xref: /openbmc/phosphor-logging/extensions/openpower-pels/private_header.cpp (revision 40fb54935ce7367636a7156039396ee91cc4d5e2)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2019 IBM Corporation
3 
4 #include "private_header.hpp"
5 
6 #include "json_utils.hpp"
7 #include "log_id.hpp"
8 #include "pel_types.hpp"
9 #include "pel_values.hpp"
10 
11 #include <phosphor-logging/lg2.hpp>
12 
13 namespace openpower
14 {
15 namespace pels
16 {
17 
18 namespace pv = openpower::pels::pel_values;
19 
PrivateHeader(uint16_t componentID,uint32_t obmcLogID,uint64_t timestamp)20 PrivateHeader::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 
PrivateHeader(Stream & pel)56 PrivateHeader::PrivateHeader(Stream& pel) :
57     _creatorID(0), _logType(0), _reservedByte(0), _sectionCount(0),
58     _obmcLogID(0), _plid(0), _id(0)
59 {
60     try
61     {
62         unflatten(pel);
63         validate();
64     }
65     catch (const std::exception& e)
66     {
67         lg2::error("Cannot unflatten private header: {EXCEPTION}", "EXCEPTION",
68                    e);
69         _valid = false;
70     }
71 }
getJSON(uint8_t creatorID) const72 std::optional<std::string> PrivateHeader::getJSON(uint8_t creatorID) const
73 {
74     char tmpPhVal[50];
75     sprintf(tmpPhVal, "%02X/%02X/%02X%02X %02X:%02X:%02X",
76             _createTimestamp.month, _createTimestamp.day,
77             _createTimestamp.yearMSB, _createTimestamp.yearLSB,
78             _createTimestamp.hour, _createTimestamp.minutes,
79             _createTimestamp.seconds);
80     std::string phCreateTStr(tmpPhVal);
81     sprintf(tmpPhVal, "%02X/%02X/%02X%02X %02X:%02X:%02X",
82             _commitTimestamp.month, _commitTimestamp.day,
83             _commitTimestamp.yearMSB, _commitTimestamp.yearLSB,
84             _commitTimestamp.hour, _commitTimestamp.minutes,
85             _commitTimestamp.seconds);
86     std::string phCommitTStr(tmpPhVal);
87     std::string creator = getNumberString("%c", _creatorID);
88     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));
92 
93     std::string ph;
94     jsonInsert(ph, pv::sectionVer, getNumberString("%d", privateHeaderVersion),
95                1);
96     jsonInsert(ph, pv::subSection, getNumberString("%d", _header.subType), 1);
97     jsonInsert(ph, pv::createdBy,
98                getComponentName(_header.componentID, creatorID), 1);
99     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);
103     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);
106     ph.erase(ph.size() - 2);
107 
108     return ph;
109 }
validate()110 void PrivateHeader::validate()
111 {
112     bool failed = false;
113 
114     if (header().id != static_cast<uint16_t>(SectionID::privateHeader))
115     {
116         lg2::error("Invalid private header section ID: {HEADER_ID}",
117                    "HEADER_ID", lg2::hex, header().id);
118         failed = true;
119     }
120 
121     if (header().version != privateHeaderVersion)
122     {
123         lg2::error("Invalid private header version: {HEADER_VERSION}",
124                    "HEADER_VERSION", lg2::hex, header().version);
125         failed = true;
126     }
127 
128     if (_sectionCount < minSectionCount)
129     {
130         lg2::error("Invalid section count in private header: {SECTION_COUNT}",
131                    "SECTION_COUNT", lg2::hex, _sectionCount);
132         failed = true;
133     }
134 
135     _valid = (failed) ? false : true;
136 }
137 
unflatten(Stream & stream)138 void PrivateHeader::unflatten(Stream& stream)
139 {
140     stream >> _header >> _createTimestamp >> _commitTimestamp >> _creatorID >>
141         _logType >> _reservedByte >> _sectionCount >> _obmcLogID >>
142         _creatorVersion >> _plid >> _id;
143 }
144 
flatten(Stream & stream) const145 void PrivateHeader::flatten(Stream& stream) const
146 {
147     stream << _header << _createTimestamp << _commitTimestamp << _creatorID
148            << _logType << _reservedByte << _sectionCount << _obmcLogID
149            << _creatorVersion << _plid << _id;
150 }
151 
operator >>(Stream & s,CreatorVersion & cv)152 Stream& 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 
operator <<(Stream & s,const CreatorVersion & cv)161 Stream& operator<<(Stream& s, const CreatorVersion& cv)
162 {
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
172