blob: 64b1f2c59e2460a5bc355324e5348beb93ee59b0 [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 Spinler31eed992019-10-09 14:07:52 -050051PrivateHeader::PrivateHeader(Stream& pel) :
52 _creatorID(0), _logType(0), _reservedByte(0), _sectionCount(0),
53 _obmcLogID(0), _plid(0), _id(0)
Matt Spinlerd3335df2019-07-10 11:04:21 -050054{
55 try
56 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -050057 unflatten(pel);
Matt Spinlerd3335df2019-07-10 11:04:21 -050058 validate();
59 }
60 catch (const std::exception& e)
61 {
62 log<level::ERR>("Cannot unflatten private header",
63 entry("ERROR=%s", e.what()));
64 _valid = false;
65 }
66}
67
68void PrivateHeader::validate()
69{
70 bool failed = false;
71
Matt Spinler1a94cc32019-09-11 13:32:12 -050072 if (header().id != static_cast<uint16_t>(SectionID::privateHeader))
Matt Spinlerd3335df2019-07-10 11:04:21 -050073 {
74 log<level::ERR>("Invalid private header section ID",
75 entry("ID=0x%X", header().id));
76 failed = true;
77 }
78
79 if (header().version != privateHeaderVersion)
80 {
81 log<level::ERR>("Invalid private header version",
82 entry("VERSION=0x%X", header().version));
83 failed = true;
84 }
85
86 if (_sectionCount < minSectionCount)
87 {
88 log<level::ERR>("Invalid section count in private header",
89 entry("SECTION_COUNT=0x%X", _sectionCount));
90 failed = true;
91 }
92
93 _valid = (failed) ? false : true;
94}
95
Matt Spinlercf5a8d02019-09-05 12:58:53 -050096void PrivateHeader::unflatten(Stream& stream)
Matt Spinlerd3335df2019-07-10 11:04:21 -050097{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050098 stream >> _header >> _createTimestamp >> _commitTimestamp >> _creatorID >>
99 _logType >> _reservedByte >> _sectionCount >> _obmcLogID >>
100 _creatorVersion >> _plid >> _id;
Matt Spinlerd3335df2019-07-10 11:04:21 -0500101}
102
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500103void PrivateHeader::flatten(Stream& stream)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500104{
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500105 stream << _header << _createTimestamp << _commitTimestamp << _creatorID
106 << _logType << _reservedByte << _sectionCount << _obmcLogID
107 << _creatorVersion << _plid << _id;
Matt Spinlerd3335df2019-07-10 11:04:21 -0500108}
109
110Stream& operator>>(Stream& s, CreatorVersion& cv)
111{
112 for (size_t i = 0; i < sizeof(CreatorVersion); i++)
113 {
114 s >> cv.version[i];
115 }
116 return s;
117}
118
119Stream& operator<<(Stream& s, CreatorVersion& cv)
120{
121 for (size_t i = 0; i < sizeof(CreatorVersion); i++)
122 {
123 s << cv.version[i];
124 }
125 return s;
126}
127
128} // namespace pels
129} // namespace openpower