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