blob: 0af9bf567780a7cc0633934b94807881486717a7 [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 Spinler42828bd2019-10-11 10:39:30 -050069const std::vector<uint8_t> srcFRUIdentityCallout{
70 'I', 'D', 0x1C, 0x1D, // type, size, flags
71 '1', '2', '3', '4', // PN
72 '5', '6', '7', 0x00, 'A', 'A', 'A', 'A', // CCIN
73 '1', '2', '3', '4', '5', '6', '7', '8', // SN
74 '9', 'A', 'B', 'C'};
75
76const std::vector<uint8_t> srcPCEIdentityCallout{
77 'P', 'E', 0x24, 0x00, // type, size, flags
78 'T', 'T', 'T', 'T', '-', 'M', 'M', 'M', // MTM
79 '1', '2', '3', '4', '5', '6', '7', // SN
80 '8', '9', 'A', 'B', 'C', 'P', 'C', 'E', // Name + null padded
81 'N', 'A', 'M', 'E', '1', '2', 0x00, 0x00, 0x00};
82
83const std::vector<uint8_t> srcMRUCallout{
84 'M', 'R', 0x28, 0x04, // ID, size, flags
85 0x00, 0x00, 0x00, 0x00, // Reserved
86 0x00, 0x00, 0x00, 'H', // priority 0
87 0x01, 0x01, 0x01, 0x01, // MRU ID 0
88 0x00, 0x00, 0x00, 'M', // priority 1
89 0x02, 0x02, 0x02, 0x02, // MRU ID 1
90 0x00, 0x00, 0x00, 'L', // priority 2
91 0x03, 0x03, 0x03, 0x03, // MRU ID 2
92 0x00, 0x00, 0x00, 'H', // priority 3
93 0x04, 0x04, 0x04, 0x04, // MRU ID 3
94};
95
96constexpr size_t sectionCountOffset = 27;
97
98std::vector<uint8_t> pelDataFactory(TestPELType type)
Matt Spinlerd3335df2019-07-10 11:04:21 -050099{
Matt Spinler42828bd2019-10-11 10:39:30 -0500100 std::vector<uint8_t> data;
101
Matt Spinlerd3335df2019-07-10 11:04:21 -0500102 switch (type)
103 {
Matt Spinler42828bd2019-10-11 10:39:30 -0500104 case TestPELType::pelSimple:
105 data.insert(data.end(), privateHeaderSection.begin(),
106 privateHeaderSection.end());
107 data.insert(data.end(), userHeaderSection.begin(),
108 userHeaderSection.end());
109 data.at(sectionCountOffset) = 2;
Matt Spinlerd3335df2019-07-10 11:04:21 -0500110 break;
Matt Spinler42828bd2019-10-11 10:39:30 -0500111 case TestPELType::privateHeaderSection:
112 data.insert(data.end(), privateHeaderSection.begin(),
113 privateHeaderSection.end());
Matt Spinlerd3335df2019-07-10 11:04:21 -0500114 break;
Matt Spinler42828bd2019-10-11 10:39:30 -0500115 case TestPELType::userHeaderSection:
116 data.insert(data.end(), userHeaderSection.begin(),
117 userHeaderSection.end());
Matt Spinler03c1d912019-07-10 14:12:15 -0500118 break;
Matt Spinler42828bd2019-10-11 10:39:30 -0500119 case TestPELType::primarySRCSection:
120 data.insert(data.end(), srcSectionNoCallouts.begin(),
121 srcSectionNoCallouts.end());
122 break;
123 case TestPELType::primarySRCSection2Callouts:
124 {
125 // Start with the no-callouts SRC, and add the callouts section
126 // from above.
127 auto src = srcSectionNoCallouts;
128 auto callouts =
129 srcDataFactory(TestSRCType::calloutSection2Callouts);
130
131 src.insert(src.end(), callouts.begin(), callouts.end());
132
133 // Set the flag that says there are callouts
134 // One byte after the 8B header
135 src[8 + 1] |= 0x01;
136
137 // Set the new sizes
138 uint16_t size = src.size();
139 Stream stream{src};
140
141 stream.offset(2); // In the header
142 stream << size;
143
144 // In the SRC - the size field doesn't include the header
145 size -= 8;
146 stream.offset(8 + 6);
147 stream << size;
148
149 data.insert(data.end(), src.begin(), src.end());
150 break;
151 }
Matt Spinlerd3335df2019-07-10 11:04:21 -0500152 }
153 return data;
154}
155
Matt Spinler6c9662c2019-10-09 11:27:20 -0500156std::vector<uint8_t> srcDataFactory(TestSRCType type)
157{
158 switch (type)
159 {
160 case TestSRCType::fruIdentityStructure:
161 return srcFRUIdentityCallout;
162
163 case TestSRCType::pceIdentityStructure:
164 return srcPCEIdentityCallout;
165
166 case TestSRCType::mruStructure:
167 return srcMRUCallout;
Matt Spinler32f13c92019-10-09 12:48:25 -0500168
169 case TestSRCType::calloutStructureA:
170 {
171 // Add just the FRU identity substructure to the base structure
172 std::vector<uint8_t> data{
173 0xFF, 0x28, 'H', 4, // size, flags, priority, LC length
174 'U', '4', '2', 0x00 // LC
175 };
176
177 data.insert(data.end(), srcFRUIdentityCallout.begin(),
178 srcFRUIdentityCallout.end());
179
180 // The final size
181 data[0] = data.size();
182 return data;
183 }
184 case TestSRCType::calloutStructureB:
185 {
186 // Add all 3 substructures to the base structure
187
188 std::vector<uint8_t> data{
189 0xFF, 0x2F, 'L', 8, // size, flags, priority, LC length
190 'U', '1', '2', '-', 'P', '1', 0x00, 0x00 // LC
191 };
192 data.insert(data.end(), srcFRUIdentityCallout.begin(),
193 srcFRUIdentityCallout.end());
194 data.insert(data.end(), srcPCEIdentityCallout.begin(),
195 srcPCEIdentityCallout.end());
196 data.insert(data.end(), srcMRUCallout.begin(), srcMRUCallout.end());
197
198 // The final size
199 data[0] = data.size();
200 return data;
201 }
Matt Spinlerf9bae182019-10-09 13:37:38 -0500202 case TestSRCType::calloutSection2Callouts:
203 {
204 std::vector<uint8_t> data{0xC0, 0x00, 0x00,
205 0x00}; // ID, flags, length in words
206
207 // Add 2 callouts
208 auto callout = srcDataFactory(TestSRCType::calloutStructureA);
209 data.insert(data.end(), callout.begin(), callout.end());
210
211 callout = srcDataFactory(TestSRCType::calloutStructureB);
212 data.insert(data.end(), callout.begin(), callout.end());
213
214 // Set the actual word length value at offset 2
215 Stream stream{data};
216 uint16_t wordLength = data.size() / 4;
217 stream.offset(2);
218 stream << wordLength;
219 stream.offset(0);
220
221 return data;
222 }
Matt Spinler6c9662c2019-10-09 11:27:20 -0500223 }
224 return {};
225}
226
Matt Spinlerd3335df2019-07-10 11:04:21 -0500227std::unique_ptr<std::vector<uint8_t>> readPELFile(const fs::path& path)
228{
229 std::ifstream file{path};
230
231 auto pel = std::make_unique<std::vector<uint8_t>>(
232 std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
233 return pel;
234}