blob: 4c94758fdd2b8cc0792a2488d53c0e315473ba5e [file] [log] [blame]
Matt Spinlerd3335df2019-07-10 11:04:21 -05001#include "pel_utils.hpp"
2
3#include "extensions/openpower-pels/private_header.hpp"
Matt Spinler03c1d912019-07-10 14:12:15 -05004#include "extensions/openpower-pels/user_header.hpp"
Matt Spinlerd3335df2019-07-10 11:04:21 -05005
6#include <fstream>
7
8#include <gtest/gtest.h>
9
10namespace fs = std::filesystem;
11using namespace openpower::pels;
12
Matt Spinlercb6b0592019-07-16 15:58:51 -050013std::filesystem::path CleanLogID::pelIDFile{};
Matt Spinler89fa0822019-07-17 13:54:30 -050014std::filesystem::path CleanPELFiles::pelIDFile{};
15std::filesystem::path CleanPELFiles::repoPath{};
Matt Spinler367144c2019-09-19 15:33:52 -050016std::filesystem::path CleanPELFiles::registryPath{};
Matt Spinlercb6b0592019-07-16 15:58:51 -050017
Matt Spinler42828bd2019-10-11 10:39:30 -050018const std::vector<uint8_t> privateHeaderSection{
19 // section header
Matt Spinlerd3335df2019-07-10 11:04:21 -050020 0x50, 0x48, // ID 'PH'
21 0x00, 0x30, // Size
22 0x01, 0x02, // version, subtype
23 0x03, 0x04, // comp ID
24
Matt Spinler42828bd2019-10-11 10:39:30 -050025 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63, // create timestamp
Matt Spinlerd3335df2019-07-10 11:04:21 -050026 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 Spinler42828bd2019-10-11 10:39:30 -050034 0x80, 0x81, 0x82, 0x83};
Matt Spinlerd3335df2019-07-10 11:04:21 -050035
Matt Spinler42828bd2019-10-11 10:39:30 -050036const std::vector<uint8_t> userHeaderSection{
37 // section header
Matt Spinler03c1d912019-07-10 14:12:15 -050038 0x55, 0x48, // ID 'UH'
39 0x00, 0x18, // Size
40 0x01, 0x0A, // version, subtype
41 0x0B, 0x0C, // comp ID
42
Matt Spinler03c1d912019-07-10 14:12:15 -050043 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 Spinlerd3335df2019-07-10 11:04:21 -050049};
50
Matt Spinler42828bd2019-10-11 10:39:30 -050051const std::vector<uint8_t> srcSectionNoCallouts{
Matt Spinlerf9bae182019-10-09 13:37:38 -050052
53 // Header
54 'P', 'S', 0x00, 0x80, 0x01, 0x01, 0x02, 0x02,
55
Matt Spinlerf9bae182019-10-09 13:37:38 -050056 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 Spinler213e5c12019-10-11 10:57:49 -050069std::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
76std::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 Spinler42828bd2019-10-11 10:39:30 -050082const 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
89const 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
96const 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
109constexpr size_t sectionCountOffset = 27;
110
111std::vector<uint8_t> pelDataFactory(TestPELType type)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500112{
Matt Spinler42828bd2019-10-11 10:39:30 -0500113 std::vector<uint8_t> data;
114
Matt Spinlerd3335df2019-07-10 11:04:21 -0500115 switch (type)
116 {
Matt Spinler42828bd2019-10-11 10:39:30 -0500117 case TestPELType::pelSimple:
118 data.insert(data.end(), privateHeaderSection.begin(),
119 privateHeaderSection.end());
120 data.insert(data.end(), userHeaderSection.begin(),
121 userHeaderSection.end());
Matt Spinler213e5c12019-10-11 10:57:49 -0500122 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 Spinlerd3335df2019-07-10 11:04:21 -0500129 break;
Matt Spinler42828bd2019-10-11 10:39:30 -0500130 case TestPELType::privateHeaderSection:
131 data.insert(data.end(), privateHeaderSection.begin(),
132 privateHeaderSection.end());
Matt Spinlerd3335df2019-07-10 11:04:21 -0500133 break;
Matt Spinler42828bd2019-10-11 10:39:30 -0500134 case TestPELType::userHeaderSection:
135 data.insert(data.end(), userHeaderSection.begin(),
136 userHeaderSection.end());
Matt Spinler03c1d912019-07-10 14:12:15 -0500137 break;
Matt Spinler42828bd2019-10-11 10:39:30 -0500138 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 Spinler213e5c12019-10-11 10:57:49 -0500171 case TestPELType::failingMTMSSection:
172 data.insert(data.end(), failingMTMSSection.begin(),
173 failingMTMSSection.end());
Matt Spinlerd3335df2019-07-10 11:04:21 -0500174 }
175 return data;
176}
177
Matt Spinler6c9662c2019-10-09 11:27:20 -0500178std::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 Spinler32f13c92019-10-09 12:48:25 -0500190
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 Spinlerf9bae182019-10-09 13:37:38 -0500224 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 Spinler6c9662c2019-10-09 11:27:20 -0500245 }
246 return {};
247}
248
Matt Spinlerd3335df2019-07-10 11:04:21 -0500249std::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}