blob: 2265497ead6a500e69bcb8c10996ae44cfd9c3e0 [file] [log] [blame]
Matt Spinlerd3335df2019-07-10 11:04:21 -05001#include "private_header.hpp"
2
Matt Spinler289aa472019-09-20 12:33:29 -05003#include "log_id.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -05004#include "pel_types.hpp"
5
Matt Spinlerd3335df2019-07-10 11:04:21 -05006#include <phosphor-logging/log.hpp>
7
8namespace openpower
9{
10namespace pels
11{
12
13using namespace phosphor::logging;
14
Matt Spinler289aa472019-09-20 12:33:29 -050015PrivateHeader::PrivateHeader(uint16_t componentID, uint32_t obmcLogID,
16 uint64_t timestamp)
17{
18 _header.id = static_cast<uint16_t>(SectionID::privateHeader);
19 _header.size = PrivateHeader::flattenedSize();
20 _header.version = privateHeaderVersion;
21 _header.subType = 0;
22 _header.componentID = componentID;
23
24 _createTimestamp = getBCDTime(timestamp);
25
26 auto now = std::chrono::system_clock::now();
27 _commitTimestamp = getBCDTime(now);
28
29 _creatorID = static_cast<uint8_t>(CreatorID::openBMC);
30
31 // Add support for reminder and telemetry log types here if
32 // ever necessary.
33 _logType = 0;
34
35 _reservedByte = 0;
36
37 // the final section count will be updated later
38 _sectionCount = 1;
39
40 _obmcLogID = obmcLogID;
41
42 _id = generatePELID();
43
44 _plid = _id;
45
46 // Leave _creatorVersion at 0
47
48 _valid = true;
49}
50
Matt Spinlerd3335df2019-07-10 11:04:21 -050051PrivateHeader::PrivateHeader(Stream& pel)
52{
53 try
54 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -050055 unflatten(pel);
Matt Spinlerd3335df2019-07-10 11:04:21 -050056 validate();
57 }
58 catch (const std::exception& e)
59 {
60 log<level::ERR>("Cannot unflatten private header",
61 entry("ERROR=%s", e.what()));
62 _valid = false;
63 }
64}
65
66void PrivateHeader::validate()
67{
68 bool failed = false;
69
Matt Spinler1a94cc32019-09-11 13:32:12 -050070 if (header().id != static_cast<uint16_t>(SectionID::privateHeader))
Matt Spinlerd3335df2019-07-10 11:04:21 -050071 {
72 log<level::ERR>("Invalid private header section ID",
73 entry("ID=0x%X", header().id));
74 failed = true;
75 }
76
77 if (header().version != privateHeaderVersion)
78 {
79 log<level::ERR>("Invalid private header version",
80 entry("VERSION=0x%X", header().version));
81 failed = true;
82 }
83
84 if (_sectionCount < minSectionCount)
85 {
86 log<level::ERR>("Invalid section count in private header",
87 entry("SECTION_COUNT=0x%X", _sectionCount));
88 failed = true;
89 }
90
91 _valid = (failed) ? false : true;
92}
93
Matt Spinlercf5a8d02019-09-05 12:58:53 -050094void PrivateHeader::unflatten(Stream& stream)
Matt Spinlerd3335df2019-07-10 11:04:21 -050095{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050096 stream >> _header >> _createTimestamp >> _commitTimestamp >> _creatorID >>
97 _logType >> _reservedByte >> _sectionCount >> _obmcLogID >>
98 _creatorVersion >> _plid >> _id;
Matt Spinlerd3335df2019-07-10 11:04:21 -050099}
100
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500101void PrivateHeader::flatten(Stream& stream)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500102{
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500103 stream << _header << _createTimestamp << _commitTimestamp << _creatorID
104 << _logType << _reservedByte << _sectionCount << _obmcLogID
105 << _creatorVersion << _plid << _id;
Matt Spinlerd3335df2019-07-10 11:04:21 -0500106}
107
108Stream& operator>>(Stream& s, CreatorVersion& cv)
109{
110 for (size_t i = 0; i < sizeof(CreatorVersion); i++)
111 {
112 s >> cv.version[i];
113 }
114 return s;
115}
116
117Stream& operator<<(Stream& s, CreatorVersion& cv)
118{
119 for (size_t i = 0; i < sizeof(CreatorVersion); i++)
120 {
121 s << cv.version[i];
122 }
123 return s;
124}
125
126} // namespace pels
127} // namespace openpower