blob: 40ea311148845779187e215d429ef43fc6391d2b [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 Spinlerd3335df2019-07-10 11:04:21 -050016#include "private_header.hpp"
17
Harisuddin Mohamed Isa2ecb4652020-01-10 10:51:02 +080018#include "json_utils.hpp"
Matt Spinler289aa472019-09-20 12:33:29 -050019#include "log_id.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050020#include "pel_types.hpp"
Aatirc92b4eb2019-11-18 13:44:51 -060021#include "pel_values.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050022
Arya K Padman5bc26532024-04-10 06:19:25 -050023#include <phosphor-logging/lg2.hpp>
Matt Spinlerd3335df2019-07-10 11:04:21 -050024
25namespace openpower
26{
27namespace pels
28{
29
Aatirc92b4eb2019-11-18 13:44:51 -060030namespace pv = openpower::pels::pel_values;
Matt Spinlerd3335df2019-07-10 11:04:21 -050031
Matt Spinler289aa472019-09-20 12:33:29 -050032PrivateHeader::PrivateHeader(uint16_t componentID, uint32_t obmcLogID,
33 uint64_t timestamp)
34{
35 _header.id = static_cast<uint16_t>(SectionID::privateHeader);
36 _header.size = PrivateHeader::flattenedSize();
37 _header.version = privateHeaderVersion;
38 _header.subType = 0;
39 _header.componentID = componentID;
40
41 _createTimestamp = getBCDTime(timestamp);
42
43 auto now = std::chrono::system_clock::now();
44 _commitTimestamp = getBCDTime(now);
45
46 _creatorID = static_cast<uint8_t>(CreatorID::openBMC);
47
48 // Add support for reminder and telemetry log types here if
49 // ever necessary.
50 _logType = 0;
51
52 _reservedByte = 0;
53
54 // the final section count will be updated later
55 _sectionCount = 1;
56
57 _obmcLogID = obmcLogID;
58
59 _id = generatePELID();
60
61 _plid = _id;
62
63 // Leave _creatorVersion at 0
64
65 _valid = true;
66}
67
Matt Spinler31eed992019-10-09 14:07:52 -050068PrivateHeader::PrivateHeader(Stream& pel) :
69 _creatorID(0), _logType(0), _reservedByte(0), _sectionCount(0),
70 _obmcLogID(0), _plid(0), _id(0)
Matt Spinlerd3335df2019-07-10 11:04:21 -050071{
72 try
73 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -050074 unflatten(pel);
Matt Spinlerd3335df2019-07-10 11:04:21 -050075 validate();
76 }
77 catch (const std::exception& e)
78 {
Arya K Padman5bc26532024-04-10 06:19:25 -050079 lg2::error("Cannot unflatten private header: {EXCEPTION}", "EXCEPTION",
80 e);
Matt Spinlerd3335df2019-07-10 11:04:21 -050081 _valid = false;
82 }
83}
Matt Spinlerb832aa52023-03-21 15:32:34 -050084std::optional<std::string> PrivateHeader::getJSON(uint8_t creatorID) const
Aatirc92b4eb2019-11-18 13:44:51 -060085{
86 char tmpPhVal[50];
Harisuddin Mohamed Isa160c51c2020-02-13 23:42:20 +080087 sprintf(tmpPhVal, "%02X/%02X/%02X%02X %02X:%02X:%02X",
Aatirc92b4eb2019-11-18 13:44:51 -060088 _createTimestamp.month, _createTimestamp.day,
89 _createTimestamp.yearMSB, _createTimestamp.yearLSB,
90 _createTimestamp.hour, _createTimestamp.minutes,
91 _createTimestamp.seconds);
92 std::string phCreateTStr(tmpPhVal);
Harisuddin Mohamed Isa160c51c2020-02-13 23:42:20 +080093 sprintf(tmpPhVal, "%02X/%02X/%02X%02X %02X:%02X:%02X",
Aatirc92b4eb2019-11-18 13:44:51 -060094 _commitTimestamp.month, _commitTimestamp.day,
Sumit Kumar74348c62021-10-26 05:48:28 -050095 _commitTimestamp.yearMSB, _commitTimestamp.yearLSB,
Aatirc92b4eb2019-11-18 13:44:51 -060096 _commitTimestamp.hour, _commitTimestamp.minutes,
97 _commitTimestamp.seconds);
98 std::string phCommitTStr(tmpPhVal);
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +080099 std::string creator = getNumberString("%c", _creatorID);
Aatirc92b4eb2019-11-18 13:44:51 -0600100 creator = pv::creatorIDs.count(creator) ? pv::creatorIDs.at(creator)
101 : "Unknown CreatorID";
102 std::string phCreatorVersionStr =
103 std::string(reinterpret_cast<const char*>(_creatorVersion.version));
Matt Spinlerd3335df2019-07-10 11:04:21 -0500104
Harisuddin Mohamed Isa2ecb4652020-01-10 10:51:02 +0800105 std::string ph;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800106 jsonInsert(ph, pv::sectionVer, getNumberString("%d", privateHeaderVersion),
107 1);
108 jsonInsert(ph, pv::subSection, getNumberString("%d", _header.subType), 1);
Matt Spinlerb832aa52023-03-21 15:32:34 -0500109 jsonInsert(ph, pv::createdBy,
110 getComponentName(_header.componentID, creatorID), 1);
Harisuddin Mohamed Isa2ecb4652020-01-10 10:51:02 +0800111 jsonInsert(ph, "Created at", phCreateTStr, 1);
112 jsonInsert(ph, "Committed at", phCommitTStr, 1);
113 jsonInsert(ph, "Creator Subsystem", creator, 1);
114 jsonInsert(ph, "CSSVER", phCreatorVersionStr, 1);
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800115 jsonInsert(ph, "Platform Log Id", getNumberString("0x%X", _plid), 1);
116 jsonInsert(ph, "Entry Id", getNumberString("0x%X", _id), 1);
117 jsonInsert(ph, "BMC Event Log Id", std::to_string(_obmcLogID), 1);
Harisuddin Mohamed Isa2ecb4652020-01-10 10:51:02 +0800118 ph.erase(ph.size() - 2);
Aatirc92b4eb2019-11-18 13:44:51 -0600119
120 return ph;
121}
Matt Spinlerd3335df2019-07-10 11:04:21 -0500122void PrivateHeader::validate()
123{
124 bool failed = false;
125
Matt Spinler1a94cc32019-09-11 13:32:12 -0500126 if (header().id != static_cast<uint16_t>(SectionID::privateHeader))
Matt Spinlerd3335df2019-07-10 11:04:21 -0500127 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500128 lg2::error("Invalid private header section ID: {HEADER_ID}",
129 "HEADER_ID", lg2::hex, header().id);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500130 failed = true;
131 }
132
133 if (header().version != privateHeaderVersion)
134 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500135 lg2::error("Invalid private header version: {HEADER_VERSION}",
136 "HEADER_VERSION", lg2::hex, header().version);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500137 failed = true;
138 }
139
140 if (_sectionCount < minSectionCount)
141 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500142 lg2::error("Invalid section count in private header: {SECTION_COUNT}",
143 "SECTION_COUNT", lg2::hex, _sectionCount);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500144 failed = true;
145 }
146
147 _valid = (failed) ? false : true;
148}
149
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500150void PrivateHeader::unflatten(Stream& stream)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500151{
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500152 stream >> _header >> _createTimestamp >> _commitTimestamp >> _creatorID >>
153 _logType >> _reservedByte >> _sectionCount >> _obmcLogID >>
154 _creatorVersion >> _plid >> _id;
Matt Spinlerd3335df2019-07-10 11:04:21 -0500155}
156
Matt Spinler06885452019-11-06 10:35:42 -0600157void PrivateHeader::flatten(Stream& stream) const
Matt Spinlerd3335df2019-07-10 11:04:21 -0500158{
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500159 stream << _header << _createTimestamp << _commitTimestamp << _creatorID
160 << _logType << _reservedByte << _sectionCount << _obmcLogID
161 << _creatorVersion << _plid << _id;
Matt Spinlerd3335df2019-07-10 11:04:21 -0500162}
163
164Stream& operator>>(Stream& s, CreatorVersion& cv)
165{
166 for (size_t i = 0; i < sizeof(CreatorVersion); i++)
167 {
168 s >> cv.version[i];
169 }
170 return s;
171}
172
Matt Spinler06885452019-11-06 10:35:42 -0600173Stream& operator<<(Stream& s, const CreatorVersion& cv)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500174{
175 for (size_t i = 0; i < sizeof(CreatorVersion); i++)
176 {
177 s << cv.version[i];
178 }
179 return s;
180}
181
182} // namespace pels
183} // namespace openpower