blob: 7ec87f32d076bd9caa6a1741f0e9cd710077bc57 [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"
20
Matt Spinlerd3335df2019-07-10 11:04:21 -050021#include <phosphor-logging/log.hpp>
22
23namespace openpower
24{
25namespace pels
26{
27
28using namespace phosphor::logging;
29
Matt Spinler289aa472019-09-20 12:33:29 -050030PrivateHeader::PrivateHeader(uint16_t componentID, uint32_t obmcLogID,
31 uint64_t timestamp)
32{
33 _header.id = static_cast<uint16_t>(SectionID::privateHeader);
34 _header.size = PrivateHeader::flattenedSize();
35 _header.version = privateHeaderVersion;
36 _header.subType = 0;
37 _header.componentID = componentID;
38
39 _createTimestamp = getBCDTime(timestamp);
40
41 auto now = std::chrono::system_clock::now();
42 _commitTimestamp = getBCDTime(now);
43
44 _creatorID = static_cast<uint8_t>(CreatorID::openBMC);
45
46 // Add support for reminder and telemetry log types here if
47 // ever necessary.
48 _logType = 0;
49
50 _reservedByte = 0;
51
52 // the final section count will be updated later
53 _sectionCount = 1;
54
55 _obmcLogID = obmcLogID;
56
57 _id = generatePELID();
58
59 _plid = _id;
60
61 // Leave _creatorVersion at 0
62
63 _valid = true;
64}
65
Matt Spinler31eed992019-10-09 14:07:52 -050066PrivateHeader::PrivateHeader(Stream& pel) :
67 _creatorID(0), _logType(0), _reservedByte(0), _sectionCount(0),
68 _obmcLogID(0), _plid(0), _id(0)
Matt Spinlerd3335df2019-07-10 11:04:21 -050069{
70 try
71 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -050072 unflatten(pel);
Matt Spinlerd3335df2019-07-10 11:04:21 -050073 validate();
74 }
75 catch (const std::exception& e)
76 {
77 log<level::ERR>("Cannot unflatten private header",
78 entry("ERROR=%s", e.what()));
79 _valid = false;
80 }
81}
82
83void PrivateHeader::validate()
84{
85 bool failed = false;
86
Matt Spinler1a94cc32019-09-11 13:32:12 -050087 if (header().id != static_cast<uint16_t>(SectionID::privateHeader))
Matt Spinlerd3335df2019-07-10 11:04:21 -050088 {
89 log<level::ERR>("Invalid private header section ID",
90 entry("ID=0x%X", header().id));
91 failed = true;
92 }
93
94 if (header().version != privateHeaderVersion)
95 {
96 log<level::ERR>("Invalid private header version",
97 entry("VERSION=0x%X", header().version));
98 failed = true;
99 }
100
101 if (_sectionCount < minSectionCount)
102 {
103 log<level::ERR>("Invalid section count in private header",
104 entry("SECTION_COUNT=0x%X", _sectionCount));
105 failed = true;
106 }
107
108 _valid = (failed) ? false : true;
109}
110
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500111void PrivateHeader::unflatten(Stream& stream)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500112{
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500113 stream >> _header >> _createTimestamp >> _commitTimestamp >> _creatorID >>
114 _logType >> _reservedByte >> _sectionCount >> _obmcLogID >>
115 _creatorVersion >> _plid >> _id;
Matt Spinlerd3335df2019-07-10 11:04:21 -0500116}
117
Matt Spinler06885452019-11-06 10:35:42 -0600118void PrivateHeader::flatten(Stream& stream) const
Matt Spinlerd3335df2019-07-10 11:04:21 -0500119{
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500120 stream << _header << _createTimestamp << _commitTimestamp << _creatorID
121 << _logType << _reservedByte << _sectionCount << _obmcLogID
122 << _creatorVersion << _plid << _id;
Matt Spinlerd3335df2019-07-10 11:04:21 -0500123}
124
125Stream& operator>>(Stream& s, CreatorVersion& cv)
126{
127 for (size_t i = 0; i < sizeof(CreatorVersion); i++)
128 {
129 s >> cv.version[i];
130 }
131 return s;
132}
133
Matt Spinler06885452019-11-06 10:35:42 -0600134Stream& operator<<(Stream& s, const CreatorVersion& cv)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500135{
136 for (size_t i = 0; i < sizeof(CreatorVersion); i++)
137 {
138 s << cv.version[i];
139 }
140 return s;
141}
142
143} // namespace pels
144} // namespace openpower