blob: 2c1a8606fc7d406247a9e5c2c020ee4acc10464c [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
Matt Spinler289aa472019-09-20 12:33:29 -050018#include "log_id.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050019#include "pel_types.hpp"
Aatirc92b4eb2019-11-18 13:44:51 -060020#include "pel_values.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050021
Matt Spinlerd3335df2019-07-10 11:04:21 -050022#include <phosphor-logging/log.hpp>
23
24namespace openpower
25{
26namespace pels
27{
28
Aatirc92b4eb2019-11-18 13:44:51 -060029namespace pv = openpower::pels::pel_values;
Matt Spinlerd3335df2019-07-10 11:04:21 -050030using namespace phosphor::logging;
31
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 {
79 log<level::ERR>("Cannot unflatten private header",
80 entry("ERROR=%s", e.what()));
81 _valid = false;
82 }
83}
Aatirc92b4eb2019-11-18 13:44:51 -060084std::optional<std::string> PrivateHeader::getJSON() const
85{
86 char tmpPhVal[50];
87 sprintf(tmpPhVal, "%02X/%02X/%02X%02X %02X:%02X:%02X",
88 _createTimestamp.month, _createTimestamp.day,
89 _createTimestamp.yearMSB, _createTimestamp.yearLSB,
90 _createTimestamp.hour, _createTimestamp.minutes,
91 _createTimestamp.seconds);
92 std::string phCreateTStr(tmpPhVal);
93 sprintf(tmpPhVal, "%02X/%02X/%02X%02X %02X:%02X:%02X",
94 _commitTimestamp.month, _commitTimestamp.day,
95 _createTimestamp.yearMSB, _commitTimestamp.yearLSB,
96 _commitTimestamp.hour, _commitTimestamp.minutes,
97 _commitTimestamp.seconds);
98 std::string phCommitTStr(tmpPhVal);
99 sprintf(tmpPhVal, "%c", _creatorID);
100 std::string creator(tmpPhVal);
101 creator = pv::creatorIDs.count(creator) ? pv::creatorIDs.at(creator)
102 : "Unknown CreatorID";
103 std::string phCreatorVersionStr =
104 std::string(reinterpret_cast<const char*>(_creatorVersion.version));
Matt Spinlerd3335df2019-07-10 11:04:21 -0500105
Aatirc92b4eb2019-11-18 13:44:51 -0600106 sprintf(tmpPhVal, "0x%X", _header.componentID);
107 std::string phCbStr(tmpPhVal);
108 sprintf(tmpPhVal, "%d", _header.subType);
109 std::string phStStr(tmpPhVal);
110 sprintf(tmpPhVal, "%d", privateHeaderVersion);
111 std::string phVerStr(tmpPhVal);
112 sprintf(tmpPhVal, "0x%X", _plid);
113 std::string phPlatformIDStr(tmpPhVal);
114 sprintf(tmpPhVal, "0x%X", _id);
115 std::string phLogEntryIDStr(tmpPhVal);
116 std::string ph = "{\"Section Version\": \"" + phVerStr +
117 "\"}, \n {\"Sub-section type\": \"" + phStStr +
118 "\"}, \n "
119 "{\"Log Committed by\": \"" +
120 phCbStr + "\"}, \n {\"Entry Creation\": \"" +
121 phCreateTStr + "\"}, \n {\"Entry Commit\": \"" +
122 phCommitTStr + "\"}, \n {\"Creator ID\": \"" + creator +
123 "\"}, \n {\"Creator Implementation\": \"" +
124 phCreatorVersionStr + "\"},\n {\"Platform Log ID\": \"" +
125 phPlatformIDStr + "\"},\n {\"Log Entry ID\": \"" +
126 phLogEntryIDStr + "\"}";
127
128 return ph;
129}
Matt Spinlerd3335df2019-07-10 11:04:21 -0500130void PrivateHeader::validate()
131{
132 bool failed = false;
133
Matt Spinler1a94cc32019-09-11 13:32:12 -0500134 if (header().id != static_cast<uint16_t>(SectionID::privateHeader))
Matt Spinlerd3335df2019-07-10 11:04:21 -0500135 {
136 log<level::ERR>("Invalid private header section ID",
137 entry("ID=0x%X", header().id));
138 failed = true;
139 }
140
141 if (header().version != privateHeaderVersion)
142 {
143 log<level::ERR>("Invalid private header version",
144 entry("VERSION=0x%X", header().version));
145 failed = true;
146 }
147
148 if (_sectionCount < minSectionCount)
149 {
150 log<level::ERR>("Invalid section count in private header",
151 entry("SECTION_COUNT=0x%X", _sectionCount));
152 failed = true;
153 }
154
155 _valid = (failed) ? false : true;
156}
157
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500158void PrivateHeader::unflatten(Stream& stream)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500159{
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500160 stream >> _header >> _createTimestamp >> _commitTimestamp >> _creatorID >>
161 _logType >> _reservedByte >> _sectionCount >> _obmcLogID >>
162 _creatorVersion >> _plid >> _id;
Matt Spinlerd3335df2019-07-10 11:04:21 -0500163}
164
Matt Spinler06885452019-11-06 10:35:42 -0600165void PrivateHeader::flatten(Stream& stream) const
Matt Spinlerd3335df2019-07-10 11:04:21 -0500166{
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500167 stream << _header << _createTimestamp << _commitTimestamp << _creatorID
168 << _logType << _reservedByte << _sectionCount << _obmcLogID
169 << _creatorVersion << _plid << _id;
Matt Spinlerd3335df2019-07-10 11:04:21 -0500170}
171
172Stream& operator>>(Stream& s, CreatorVersion& cv)
173{
174 for (size_t i = 0; i < sizeof(CreatorVersion); i++)
175 {
176 s >> cv.version[i];
177 }
178 return s;
179}
180
Matt Spinler06885452019-11-06 10:35:42 -0600181Stream& operator<<(Stream& s, const CreatorVersion& cv)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500182{
183 for (size_t i = 0; i < sizeof(CreatorVersion); i++)
184 {
185 s << cv.version[i];
186 }
187 return s;
188}
189
190} // namespace pels
191} // namespace openpower