| Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 1 | /** | 
|  | 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 Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 16 | #include "private_header.hpp" | 
|  | 17 |  | 
| Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 18 | #include "log_id.hpp" | 
| Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 19 | #include "pel_types.hpp" | 
|  | 20 |  | 
| Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 21 | #include <phosphor-logging/log.hpp> | 
|  | 22 |  | 
|  | 23 | namespace openpower | 
|  | 24 | { | 
|  | 25 | namespace pels | 
|  | 26 | { | 
|  | 27 |  | 
|  | 28 | using namespace phosphor::logging; | 
|  | 29 |  | 
| Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 30 | PrivateHeader::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 Spinler | 31eed99 | 2019-10-09 14:07:52 -0500 | [diff] [blame] | 66 | PrivateHeader::PrivateHeader(Stream& pel) : | 
|  | 67 | _creatorID(0), _logType(0), _reservedByte(0), _sectionCount(0), | 
|  | 68 | _obmcLogID(0), _plid(0), _id(0) | 
| Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 69 | { | 
|  | 70 | try | 
|  | 71 | { | 
| Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 72 | unflatten(pel); | 
| Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 73 | 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 |  | 
|  | 83 | void PrivateHeader::validate() | 
|  | 84 | { | 
|  | 85 | bool failed = false; | 
|  | 86 |  | 
| Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 87 | if (header().id != static_cast<uint16_t>(SectionID::privateHeader)) | 
| Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 88 | { | 
|  | 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 Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 111 | void PrivateHeader::unflatten(Stream& stream) | 
| Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 112 | { | 
| Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 113 | stream >> _header >> _createTimestamp >> _commitTimestamp >> _creatorID >> | 
|  | 114 | _logType >> _reservedByte >> _sectionCount >> _obmcLogID >> | 
|  | 115 | _creatorVersion >> _plid >> _id; | 
| Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
| Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 118 | void PrivateHeader::flatten(Stream& stream) | 
| Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 119 | { | 
| Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 120 | stream << _header << _createTimestamp << _commitTimestamp << _creatorID | 
|  | 121 | << _logType << _reservedByte << _sectionCount << _obmcLogID | 
|  | 122 | << _creatorVersion << _plid << _id; | 
| Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 123 | } | 
|  | 124 |  | 
|  | 125 | Stream& 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 |  | 
|  | 134 | Stream& operator<<(Stream& s, CreatorVersion& cv) | 
|  | 135 | { | 
|  | 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 |