Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 1 | #include "pel_utils.hpp" |
| 2 | |
| 3 | #include "extensions/openpower-pels/private_header.hpp" |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 4 | #include "extensions/openpower-pels/user_header.hpp" |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 5 | |
| 6 | #include <fstream> |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | namespace fs = std::filesystem; |
| 11 | using namespace openpower::pels; |
| 12 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 13 | std::filesystem::path CleanLogID::pelIDFile{}; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 14 | std::filesystem::path CleanPELFiles::pelIDFile{}; |
| 15 | std::filesystem::path CleanPELFiles::repoPath{}; |
Matt Spinler | 367144c | 2019-09-19 15:33:52 -0500 | [diff] [blame] | 16 | std::filesystem::path CleanPELFiles::registryPath{}; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 17 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 18 | const std::vector<uint8_t> privateHeaderSection{ |
| 19 | // section header |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 20 | 0x50, 0x48, // ID 'PH' |
| 21 | 0x00, 0x30, // Size |
| 22 | 0x01, 0x02, // version, subtype |
| 23 | 0x03, 0x04, // comp ID |
| 24 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 25 | 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63, // create timestamp |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 26 | 0x20, 0x31, 0x06, 0x0F, 0x09, 0x22, 0x3A, 0x00, // commit timestamp |
| 27 | 0xAA, // creatorID |
| 28 | 0x00, // logtype |
| 29 | 0x00, // reserved |
| 30 | 0x02, // section count |
| 31 | 0x90, 0x91, 0x92, 0x93, // OpenBMC log ID |
| 32 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0, // creator version |
| 33 | 0x50, 0x51, 0x52, 0x53, // plid |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 34 | 0x80, 0x81, 0x82, 0x83}; |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 35 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 36 | const std::vector<uint8_t> userHeaderSection{ |
| 37 | // section header |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 38 | 0x55, 0x48, // ID 'UH' |
| 39 | 0x00, 0x18, // Size |
| 40 | 0x01, 0x0A, // version, subtype |
| 41 | 0x0B, 0x0C, // comp ID |
| 42 | |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 43 | 0x10, 0x04, // subsystem, scope |
| 44 | 0x20, 0x00, // severity, type |
| 45 | 0x00, 0x00, 0x00, 0x00, // reserved |
| 46 | 0x03, 0x04, // problem domain, vector |
| 47 | 0x80, 0xC0, // action flags |
| 48 | 0x00, 0x00, 0x00, 0x00 // reserved |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 49 | }; |
| 50 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 51 | const std::vector<uint8_t> srcSectionNoCallouts{ |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 52 | |
| 53 | // Header |
| 54 | 'P', 'S', 0x00, 0x80, 0x01, 0x01, 0x02, 0x02, |
| 55 | |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 56 | 0x02, 0x00, 0x00, // version, flags, reserved |
| 57 | 0x09, 0x00, 0x00, // hex word count, reserved2B |
| 58 | 0x00, 0x48, // SRC structure size |
| 59 | |
| 60 | // Hex words 2 - 9 |
| 61 | 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, |
| 62 | 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x07, |
| 63 | 0x08, 0x08, 0x08, 0x08, 0x09, 0x09, 0x09, 0x09, |
| 64 | // ASCII string |
| 65 | 'B', 'D', '8', 'D', '5', '6', '7', '8', ' ', ' ', ' ', ' ', ' ', ' ', ' ', |
| 66 | ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', |
| 67 | ' ', ' '}; |
| 68 | |
Matt Spinler | 213e5c1 | 2019-10-11 10:57:49 -0500 | [diff] [blame] | 69 | std::vector<uint8_t> failingMTMSSection{ |
| 70 | // Header |
| 71 | 0x4D, 0x54, 0x00, 0x1C, 0x01, 0x00, 0x20, 0x00, |
| 72 | |
| 73 | 'T', 'T', 'T', 'T', '-', 'M', 'M', 'M', '1', '2', |
| 74 | '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C'}; |
| 75 | |
| 76 | std::vector<uint8_t> UserDataSection{ |
| 77 | // Header |
| 78 | 0x55, 0x44, 0x00, 0x10, 0x00, 0x00, 0x20, 0x00, |
| 79 | |
| 80 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; |
| 81 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 82 | const std::vector<uint8_t> srcFRUIdentityCallout{ |
| 83 | 'I', 'D', 0x1C, 0x1D, // type, size, flags |
| 84 | '1', '2', '3', '4', // PN |
| 85 | '5', '6', '7', 0x00, 'A', 'A', 'A', 'A', // CCIN |
| 86 | '1', '2', '3', '4', '5', '6', '7', '8', // SN |
| 87 | '9', 'A', 'B', 'C'}; |
| 88 | |
| 89 | const std::vector<uint8_t> srcPCEIdentityCallout{ |
| 90 | 'P', 'E', 0x24, 0x00, // type, size, flags |
| 91 | 'T', 'T', 'T', 'T', '-', 'M', 'M', 'M', // MTM |
| 92 | '1', '2', '3', '4', '5', '6', '7', // SN |
| 93 | '8', '9', 'A', 'B', 'C', 'P', 'C', 'E', // Name + null padded |
| 94 | 'N', 'A', 'M', 'E', '1', '2', 0x00, 0x00, 0x00}; |
| 95 | |
| 96 | const std::vector<uint8_t> srcMRUCallout{ |
| 97 | 'M', 'R', 0x28, 0x04, // ID, size, flags |
| 98 | 0x00, 0x00, 0x00, 0x00, // Reserved |
| 99 | 0x00, 0x00, 0x00, 'H', // priority 0 |
| 100 | 0x01, 0x01, 0x01, 0x01, // MRU ID 0 |
| 101 | 0x00, 0x00, 0x00, 'M', // priority 1 |
| 102 | 0x02, 0x02, 0x02, 0x02, // MRU ID 1 |
| 103 | 0x00, 0x00, 0x00, 'L', // priority 2 |
| 104 | 0x03, 0x03, 0x03, 0x03, // MRU ID 2 |
| 105 | 0x00, 0x00, 0x00, 'H', // priority 3 |
| 106 | 0x04, 0x04, 0x04, 0x04, // MRU ID 3 |
| 107 | }; |
| 108 | |
| 109 | constexpr size_t sectionCountOffset = 27; |
| 110 | |
| 111 | std::vector<uint8_t> pelDataFactory(TestPELType type) |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 112 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 113 | std::vector<uint8_t> data; |
| 114 | |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 115 | switch (type) |
| 116 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 117 | case TestPELType::pelSimple: |
| 118 | data.insert(data.end(), privateHeaderSection.begin(), |
| 119 | privateHeaderSection.end()); |
| 120 | data.insert(data.end(), userHeaderSection.begin(), |
| 121 | userHeaderSection.end()); |
Matt Spinler | 213e5c1 | 2019-10-11 10:57:49 -0500 | [diff] [blame] | 122 | data.insert(data.end(), srcSectionNoCallouts.begin(), |
| 123 | srcSectionNoCallouts.end()); |
| 124 | data.insert(data.end(), failingMTMSSection.begin(), |
| 125 | failingMTMSSection.end()); |
| 126 | data.insert(data.end(), UserDataSection.begin(), |
| 127 | UserDataSection.end()); |
| 128 | data.at(sectionCountOffset) = 5; |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 129 | break; |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 130 | case TestPELType::privateHeaderSection: |
| 131 | data.insert(data.end(), privateHeaderSection.begin(), |
| 132 | privateHeaderSection.end()); |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 133 | break; |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 134 | case TestPELType::userHeaderSection: |
| 135 | data.insert(data.end(), userHeaderSection.begin(), |
| 136 | userHeaderSection.end()); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 137 | break; |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 138 | case TestPELType::primarySRCSection: |
| 139 | data.insert(data.end(), srcSectionNoCallouts.begin(), |
| 140 | srcSectionNoCallouts.end()); |
| 141 | break; |
| 142 | case TestPELType::primarySRCSection2Callouts: |
| 143 | { |
| 144 | // Start with the no-callouts SRC, and add the callouts section |
| 145 | // from above. |
| 146 | auto src = srcSectionNoCallouts; |
| 147 | auto callouts = |
| 148 | srcDataFactory(TestSRCType::calloutSection2Callouts); |
| 149 | |
| 150 | src.insert(src.end(), callouts.begin(), callouts.end()); |
| 151 | |
| 152 | // Set the flag that says there are callouts |
| 153 | // One byte after the 8B header |
| 154 | src[8 + 1] |= 0x01; |
| 155 | |
| 156 | // Set the new sizes |
| 157 | uint16_t size = src.size(); |
| 158 | Stream stream{src}; |
| 159 | |
| 160 | stream.offset(2); // In the header |
| 161 | stream << size; |
| 162 | |
| 163 | // In the SRC - the size field doesn't include the header |
| 164 | size -= 8; |
| 165 | stream.offset(8 + 6); |
| 166 | stream << size; |
| 167 | |
| 168 | data.insert(data.end(), src.begin(), src.end()); |
| 169 | break; |
| 170 | } |
Matt Spinler | 213e5c1 | 2019-10-11 10:57:49 -0500 | [diff] [blame] | 171 | case TestPELType::failingMTMSSection: |
| 172 | data.insert(data.end(), failingMTMSSection.begin(), |
| 173 | failingMTMSSection.end()); |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 174 | } |
| 175 | return data; |
| 176 | } |
| 177 | |
Matt Spinler | 6c9662c | 2019-10-09 11:27:20 -0500 | [diff] [blame] | 178 | std::vector<uint8_t> srcDataFactory(TestSRCType type) |
| 179 | { |
| 180 | switch (type) |
| 181 | { |
| 182 | case TestSRCType::fruIdentityStructure: |
| 183 | return srcFRUIdentityCallout; |
| 184 | |
| 185 | case TestSRCType::pceIdentityStructure: |
| 186 | return srcPCEIdentityCallout; |
| 187 | |
| 188 | case TestSRCType::mruStructure: |
| 189 | return srcMRUCallout; |
Matt Spinler | 32f13c9 | 2019-10-09 12:48:25 -0500 | [diff] [blame] | 190 | |
| 191 | case TestSRCType::calloutStructureA: |
| 192 | { |
| 193 | // Add just the FRU identity substructure to the base structure |
| 194 | std::vector<uint8_t> data{ |
| 195 | 0xFF, 0x28, 'H', 4, // size, flags, priority, LC length |
| 196 | 'U', '4', '2', 0x00 // LC |
| 197 | }; |
| 198 | |
| 199 | data.insert(data.end(), srcFRUIdentityCallout.begin(), |
| 200 | srcFRUIdentityCallout.end()); |
| 201 | |
| 202 | // The final size |
| 203 | data[0] = data.size(); |
| 204 | return data; |
| 205 | } |
| 206 | case TestSRCType::calloutStructureB: |
| 207 | { |
| 208 | // Add all 3 substructures to the base structure |
| 209 | |
| 210 | std::vector<uint8_t> data{ |
| 211 | 0xFF, 0x2F, 'L', 8, // size, flags, priority, LC length |
| 212 | 'U', '1', '2', '-', 'P', '1', 0x00, 0x00 // LC |
| 213 | }; |
| 214 | data.insert(data.end(), srcFRUIdentityCallout.begin(), |
| 215 | srcFRUIdentityCallout.end()); |
| 216 | data.insert(data.end(), srcPCEIdentityCallout.begin(), |
| 217 | srcPCEIdentityCallout.end()); |
| 218 | data.insert(data.end(), srcMRUCallout.begin(), srcMRUCallout.end()); |
| 219 | |
| 220 | // The final size |
| 221 | data[0] = data.size(); |
| 222 | return data; |
| 223 | } |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 224 | case TestSRCType::calloutSection2Callouts: |
| 225 | { |
| 226 | std::vector<uint8_t> data{0xC0, 0x00, 0x00, |
| 227 | 0x00}; // ID, flags, length in words |
| 228 | |
| 229 | // Add 2 callouts |
| 230 | auto callout = srcDataFactory(TestSRCType::calloutStructureA); |
| 231 | data.insert(data.end(), callout.begin(), callout.end()); |
| 232 | |
| 233 | callout = srcDataFactory(TestSRCType::calloutStructureB); |
| 234 | data.insert(data.end(), callout.begin(), callout.end()); |
| 235 | |
| 236 | // Set the actual word length value at offset 2 |
| 237 | Stream stream{data}; |
| 238 | uint16_t wordLength = data.size() / 4; |
| 239 | stream.offset(2); |
| 240 | stream << wordLength; |
| 241 | stream.offset(0); |
| 242 | |
| 243 | return data; |
| 244 | } |
Matt Spinler | 6c9662c | 2019-10-09 11:27:20 -0500 | [diff] [blame] | 245 | } |
| 246 | return {}; |
| 247 | } |
| 248 | |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 249 | std::unique_ptr<std::vector<uint8_t>> readPELFile(const fs::path& path) |
| 250 | { |
| 251 | std::ifstream file{path}; |
| 252 | |
| 253 | auto pel = std::make_unique<std::vector<uint8_t>>( |
| 254 | std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()); |
| 255 | return pel; |
| 256 | } |