blob: 6dda65c75048b01b9e0e8033ec5492aa695887f8 [file] [log] [blame]
Sridevi Rameshd4489752019-12-08 09:03:29 -06001#include "pldm_fru_cmd.hpp"
2
3#include "pldm_cmd_helper.hpp"
4
Sridevi Rameshb8baffa2020-07-22 06:44:39 -05005#ifdef OEM_IBM
6#include "oem/ibm/libpldm/fru.h"
7#endif
8
John Wang5bdde3a2020-06-16 15:02:33 +08009#include <endian.h>
10
11#include <functional>
12#include <tuple>
13
Sridevi Rameshd4489752019-12-08 09:03:29 -060014namespace pldmtool
15{
16
17namespace fru
18{
19
20namespace
21{
22
23using namespace pldmtool::helper;
24
25std::vector<std::unique_ptr<CommandInterface>> commands;
26
27} // namespace
28
29class GetFruRecordTableMetadata : public CommandInterface
30{
31 public:
32 ~GetFruRecordTableMetadata() = default;
33 GetFruRecordTableMetadata() = delete;
34 GetFruRecordTableMetadata(const GetFruRecordTableMetadata&) = delete;
35 GetFruRecordTableMetadata(GetFruRecordTableMetadata&&) = default;
36 GetFruRecordTableMetadata&
37 operator=(const GetFruRecordTableMetadata&) = delete;
38 GetFruRecordTableMetadata& operator=(GetFruRecordTableMetadata&&) = default;
39
40 using CommandInterface::CommandInterface;
41
42 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
43 {
44 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
45 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
46
Christian Geddes3bdb3c22020-05-01 14:55:39 -050047 auto rc = encode_get_fru_record_table_metadata_req(
48 instanceId, request, PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES);
Sridevi Rameshd4489752019-12-08 09:03:29 -060049 return {rc, requestMsg};
50 }
51
52 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
53 {
54 uint8_t cc = 0;
55 uint8_t fru_data_major_version, fru_data_minor_version;
56 uint32_t fru_table_maximum_size, fru_table_length;
57 uint16_t total_record_set_identifiers, total_table_records;
58 uint32_t checksum;
59
60 auto rc = decode_get_fru_record_table_metadata_resp(
61 responsePtr, payloadLength, &cc, &fru_data_major_version,
62 &fru_data_minor_version, &fru_table_maximum_size, &fru_table_length,
63 &total_record_set_identifiers, &total_table_records, &checksum);
64 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
65 {
66 std::cerr << "Response Message Error: "
67 << "rc=" << rc << ",cc=" << (int)cc << std::endl;
68 return;
69 }
Sridevi Rameshd4489752019-12-08 09:03:29 -060070 std::cout << "FRUDATAMajorVersion : "
71 << static_cast<uint32_t>(fru_data_major_version) << std::endl;
72 std::cout << "FRUDATAMinorVersion : "
73 << static_cast<uint32_t>(fru_data_minor_version) << std::endl;
74 std::cout << "FRUTableMaximumSize : " << fru_table_maximum_size
75 << std::endl;
76 std::cout << "FRUTableLength : " << fru_table_length << std::endl;
77 std::cout << "Total number of Record Set Identifiers in table : "
78 << total_record_set_identifiers << std::endl;
79 std::cout << "Total number of records in table : "
80 << total_table_records << std::endl;
81 std::cout << "FRU DATAStructureTableIntegrityChecksum : " << checksum
82 << std::endl;
83 }
84};
85
John Wang5bdde3a2020-06-16 15:02:33 +080086class FRUTablePrint
87{
88 public:
89 explicit FRUTablePrint(const uint8_t* table, size_t table_size) :
90 table(table), table_size(table_size)
91 {}
92
93 void print()
94 {
95 auto p = table;
96 while (!isTableEnd(p))
97 {
98 auto record =
99 reinterpret_cast<const pldm_fru_record_data_format*>(p);
100 std::cout << "FRU Record Set Identifier: "
101 << (int)le16toh(record->record_set_id) << std::endl;
102 std::cout << "FRU Record Type: "
103 << typeToString(fruRecordTypes, record->record_type)
104 << std::endl;
105 std::cout << "Number of FRU fields: " << (int)record->num_fru_fields
106 << std::endl;
107 std::cout << "Encoding Type for FRU fields: "
108 << typeToString(fruEncodingType, record->encoding_type)
109 << std::endl;
110
John Wang5bdde3a2020-06-16 15:02:33 +0800111 p += sizeof(pldm_fru_record_data_format) -
112 sizeof(pldm_fru_record_tlv);
Sridevi Rameshb8baffa2020-07-22 06:44:39 -0500113
114 auto isIPZ = false;
115 std::map<uint8_t, std::string> FruFieldTypeMap;
116 std::string fruFieldValue;
117
John Wang5bdde3a2020-06-16 15:02:33 +0800118 for (int i = 0; i < record->num_fru_fields; i++)
119 {
120 auto tlv = reinterpret_cast<const pldm_fru_record_tlv*>(p);
Sridevi Rameshb8baffa2020-07-22 06:44:39 -0500121 if (record->record_type == PLDM_FRU_RECORD_TYPE_GENERAL)
John Wang5bdde3a2020-06-16 15:02:33 +0800122 {
Sridevi Rameshb8baffa2020-07-22 06:44:39 -0500123 FruFieldTypeMap.insert(fruGeneralFieldTypes.begin(),
124 fruGeneralFieldTypes.end());
125 if (tlv->type == PLDM_FRU_FIELD_TYPE_IANA)
126 {
127 fruFieldValue =
128 fruFieldParserU32(tlv->value, tlv->length);
129 }
130 else if (tlv->type == PLDM_FRU_FIELD_TYPE_MANUFAC_DATE)
131 {
132 fruFieldValue =
133 fruFieldParserTimestamp(tlv->value, tlv->length);
134 }
John Wang5bdde3a2020-06-16 15:02:33 +0800135 }
Sridevi Rameshb8baffa2020-07-22 06:44:39 -0500136 else
137 {
138#ifdef OEM_IBM
139 if (tlv->type == PLDM_OEM_FRU_FIELD_TYPE_RT)
140 {
141 auto oemIPZValue =
142 fruFieldValuestring(tlv->value, tlv->length);
143
144 if (populateMaps.find(oemIPZValue) !=
145 populateMaps.end())
146 {
147 isIPZ = true;
148 const std::map<uint8_t, std::string> IPZTypes =
149 populateMaps.at(oemIPZValue);
150 FruFieldTypeMap.insert(IPZTypes.begin(),
151 IPZTypes.end());
152 }
153 }
154 else
155 {
156 FruFieldTypeMap.insert(fruOemFieldTypes.begin(),
157 fruOemFieldTypes.end());
158 }
159 if (tlv->type == PLDM_OEM_FRU_FIELD_TYPE_IANA)
160 {
161 fruFieldValue =
162 fruFieldParserU32(tlv->value, tlv->length);
163 }
164#endif
165 }
166
167 if (isIPZ && (tlv->type != 2))
168 {
169 fruFieldValue = fruFieldIPZParser(tlv->value, tlv->length);
170 }
171 else
172 {
173 fruFieldValue =
174 fruFieldValuestring(tlv->value, tlv->length);
175 }
176
177 std::cout << "\tFRU Field Type: "
178 << typeToString(FruFieldTypeMap, tlv->type)
179 << std::endl;
180 std::cout << "\tFRU Field Length: " << (int)(tlv->length)
181 << std::endl;
182 std::cout << "\tFRU Field Value: " << fruFieldValue
183 << std::endl;
184
John Wang5bdde3a2020-06-16 15:02:33 +0800185 p += sizeof(pldm_fru_record_tlv) - 1 + tlv->length;
186 }
187 }
188 }
189
190 private:
191 const uint8_t* table;
192 size_t table_size;
193
194 bool isTableEnd(const uint8_t* p)
195 {
196 auto offset = p - table;
197 return (table_size - offset) <= 7;
198 }
199
Sridevi Rameshb8baffa2020-07-22 06:44:39 -0500200 static inline const std::map<uint8_t, std::string> fruEncodingType{
John Wang5bdde3a2020-06-16 15:02:33 +0800201 {PLDM_FRU_ENCODING_UNSPECIFIED, "Unspecified"},
202 {PLDM_FRU_ENCODING_ASCII, "ASCII"},
203 {PLDM_FRU_ENCODING_UTF8, "UTF8"},
204 {PLDM_FRU_ENCODING_UTF16, "UTF16"},
205 {PLDM_FRU_ENCODING_UTF16LE, "UTF16LE"},
206 {PLDM_FRU_ENCODING_UTF16BE, "UTF16BE"}};
207
Sridevi Rameshb8baffa2020-07-22 06:44:39 -0500208 static inline const std::map<uint8_t, std::string> fruGeneralFieldTypes{
209 {PLDM_FRU_FIELD_TYPE_CHASSIS, "Chassis"},
210 {PLDM_FRU_FIELD_TYPE_MODEL, "Model"},
211 {PLDM_FRU_FIELD_TYPE_PN, "Part Number"},
212 {PLDM_FRU_FIELD_TYPE_SN, "Serial Number"},
213 {PLDM_FRU_FIELD_TYPE_MANUFAC, "Manufacturer"},
214 {PLDM_FRU_FIELD_TYPE_MANUFAC_DATE, "Manufacture Date"},
215 {PLDM_FRU_FIELD_TYPE_VENDOR, "Vendor"},
216 {PLDM_FRU_FIELD_TYPE_NAME, "Name"},
217 {PLDM_FRU_FIELD_TYPE_SKU, "SKU"},
218 {PLDM_FRU_FIELD_TYPE_VERSION, "Version"},
219 {PLDM_FRU_FIELD_TYPE_ASSET_TAG, "Asset Tag"},
220 {PLDM_FRU_FIELD_TYPE_DESC, "Description"},
221 {PLDM_FRU_FIELD_TYPE_EC_LVL, "Engineering Change Level"},
222 {PLDM_FRU_FIELD_TYPE_OTHER, "Other Information"},
223 {PLDM_FRU_FIELD_TYPE_IANA, "Vendor IANA"}};
224
225 static inline const std::map<uint8_t, std::string> fruRecordTypes{
John Wang5bdde3a2020-06-16 15:02:33 +0800226 {PLDM_FRU_RECORD_TYPE_GENERAL, "General"},
227 {PLDM_FRU_RECORD_TYPE_OEM, "OEM"}};
228
Sridevi Rameshb8baffa2020-07-22 06:44:39 -0500229#ifdef OEM_IBM
230 static inline const std::map<uint8_t, std::string> fruOemFieldTypes{
231 {PLDM_OEM_FRU_FIELD_TYPE_IANA, "Vendor IANA"},
232 {PLDM_OEM_FRU_FIELD_TYPE_RT, "RT"},
233 {PLDM_OEM_FRU_FIELD_TYPE_LOCATION_CODE, "Location Code"}};
234
235 static inline const std::map<uint8_t, std::string> VINIFieldTypes{
236 {2, "RT"}, {3, "B3"}, {4, "B4"}, {5, "B7"}, {6, "CC"}, {7, "CE"},
237 {8, "CT"}, {9, "DR"}, {10, "FG"}, {11, "FN"}, {12, "HE"}, {13, "HW"},
238 {14, "HX"}, {15, "PN"}, {16, "SN"}, {17, "TS"}, {18, "VZ"}};
239
240 static inline const std::map<uint8_t, std::string> VSYSFieldTypes{
241 {2, "RT"}, {3, "BR"}, {4, "DR"}, {5, "FV"}, {6, "ID"},
242 {7, "MN"}, {8, "NN"}, {9, "RB"}, {10, "RG"}, {11, "SE"},
243 {12, "SG"}, {13, "SU"}, {14, "TM"}, {15, "TN"}, {16, "WN"}};
244
245 static inline const std::map<uint8_t, std::string> LXR0FieldTypes{
246 {2, "RT"}, {3, "LX"}, {4, "VZ"}};
247
248 static inline const std::map<uint8_t, std::string> VW10FieldTypes{
249 {2, "RT"}, {3, "DR"}, {4, "GD"}};
250
251 static inline const std::map<uint8_t, std::string> VR10FieldTypes{
252 {2, "RT"}, {3, "DC"}, {4, "DR"}, {5, "FL"}, {6, "WA"}};
253
254 static inline const std::map<std::string,
255 const std::map<uint8_t, std::string>>
256 populateMaps{{"VINI", VINIFieldTypes},
257 {"VSYS", VSYSFieldTypes},
258 {"LXR0", LXR0FieldTypes},
259 {"VWX10", VW10FieldTypes},
260 {"VR10", VR10FieldTypes}};
261#endif
262
263 std::string typeToString(std::map<uint8_t, std::string> typeMap,
John Wang5bdde3a2020-06-16 15:02:33 +0800264 uint8_t type)
265 {
266 auto typeString = std::to_string(type);
267 try
268 {
Sridevi Rameshb8baffa2020-07-22 06:44:39 -0500269 return std::string(typeMap.at(type)) + "(" + typeString + ")";
John Wang5bdde3a2020-06-16 15:02:33 +0800270 }
271 catch (const std::out_of_range& e)
272 {
273 return typeString;
274 }
275 }
276
Sridevi Rameshb8baffa2020-07-22 06:44:39 -0500277 std::string fruFieldValuestring(const uint8_t* value, uint8_t length)
John Wang5bdde3a2020-06-16 15:02:33 +0800278 {
279 return std::string(reinterpret_cast<const char*>(value), length);
280 }
281
John Wang5bdde3a2020-06-16 15:02:33 +0800282 static std::string fruFieldParserU32(const uint8_t* value, uint8_t length)
283 {
284 assert(length == 4);
285 uint32_t v;
286 std::memcpy(&v, value, length);
287 return std::to_string(le32toh(v));
288 }
289
Sridevi Rameshb8baffa2020-07-22 06:44:39 -0500290 static std::string fruFieldParserTimestamp(const uint8_t*, uint8_t)
John Wang5bdde3a2020-06-16 15:02:33 +0800291 {
Sridevi Rameshb8baffa2020-07-22 06:44:39 -0500292 return std::string("TODO");
293 }
John Wang5bdde3a2020-06-16 15:02:33 +0800294
Sridevi Rameshb8baffa2020-07-22 06:44:39 -0500295 static std::string fruFieldIPZParser(const uint8_t* value, uint8_t length)
296 {
297 std::ostringstream tempStream;
298 for (int i = 0; i < int(length); ++i)
299 {
300 tempStream << "0x" << std::setfill('0') << std::setw(2) << std::hex
301 << (unsigned)value[i] << " ";
302 }
303 return tempStream.str();
John Wang5bdde3a2020-06-16 15:02:33 +0800304 }
305};
306
307class GetFRURecordByOption : public CommandInterface
308{
309 public:
310 ~GetFRURecordByOption() = default;
311 GetFRURecordByOption() = delete;
312 GetFRURecordByOption(const GetFRURecordByOption&) = delete;
313 GetFRURecordByOption(GetFruRecordTableMetadata&&) = delete;
314 GetFRURecordByOption& operator=(const GetFRURecordByOption&) = delete;
315 GetFRURecordByOption& operator=(GetFRURecordByOption&&) = delete;
316
317 explicit GetFRURecordByOption(const char* type, const char* name,
318 CLI::App* app) :
319 CommandInterface(type, name, app)
320 {
321 app->add_option("-i, --identifier", recordSetIdentifier,
322 "Record Set Identifier\n"
323 "Possible values: {All record sets = 0, Specific "
324 "record set = 1 – 65535}")
325 ->required();
326 app->add_option("-r, --record", recordType,
327 "Record Type\n"
328 "Possible values: {All record types = 0, Specific "
329 "record types = 1 – 255}")
330 ->required();
331 app->add_option("-f, --field", fieldType,
332 "Field Type\n"
333 "Possible values: {All record field types = 0, "
334 "Specific field types = 1 – 15}")
335 ->required();
336 }
337
338 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
339 {
340 if (fieldType != 0 && recordType == 0)
341 {
342 throw std::invalid_argument("if field type is non-zero, the record "
343 "type shall also be non-zero");
344 }
Sridevi Rameshb8baffa2020-07-22 06:44:39 -0500345 if (recordType == 254 && (fieldType > 2 && fieldType < 254))
346 {
347 throw std::invalid_argument(
348 "GetFRURecordByOption is not supported for recordType : 254 "
349 "and fieldType > 2");
350 }
John Wang5bdde3a2020-06-16 15:02:33 +0800351
352 auto payloadLength = sizeof(pldm_get_fru_record_by_option_req);
353
354 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + payloadLength,
355 0);
356 auto reqMsg = reinterpret_cast<pldm_msg*>(requestMsg.data());
357
358 auto rc = encode_get_fru_record_by_option_req(
359 instanceId, 0 /* DataTransferHandle */, 0 /* FRUTableHandle */,
360 recordSetIdentifier, recordType, fieldType, PLDM_GET_FIRSTPART,
361 reqMsg, payloadLength);
362
363 return {rc, requestMsg};
364 }
365
366 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
367 {
368 uint8_t cc;
369 uint32_t dataTransferHandle;
370 uint8_t transferFlag;
371 variable_field fruData;
372
373 auto rc = decode_get_fru_record_by_option_resp(
374 responsePtr, payloadLength, &cc, &dataTransferHandle, &transferFlag,
375 &fruData);
376
377 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
378 {
379 std::cerr << "Response Message Error: "
380 << "rc=" << rc << ",cc=" << (int)cc << std::endl;
381 return;
382 }
383
384 FRUTablePrint tablePrint(fruData.ptr, fruData.length);
385 tablePrint.print();
386 }
387
388 private:
389 uint16_t recordSetIdentifier;
390 uint8_t recordType;
391 uint8_t fieldType;
392};
393
Sridevi Rameshbd9440b2020-03-24 02:14:36 -0500394class GetFruRecordTable : public CommandInterface
395{
396 public:
397 ~GetFruRecordTable() = default;
398 GetFruRecordTable() = delete;
399 GetFruRecordTable(const GetFruRecordTable&) = delete;
400 GetFruRecordTable(GetFruRecordTable&&) = default;
401 GetFruRecordTable& operator=(const GetFruRecordTable&) = delete;
402 GetFruRecordTable& operator=(GetFruRecordTable&&) = default;
403
404 using CommandInterface::CommandInterface;
405 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
406 {
407 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
408 PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES);
409 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
410
411 auto rc = encode_get_fru_record_table_req(
412 instanceId, 0, PLDM_START_AND_END, request,
413 requestMsg.size() - sizeof(pldm_msg_hdr));
414 return {rc, requestMsg};
415 }
416 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
417 {
418 uint8_t cc = 0;
419 uint32_t next_data_transfer_handle = 0;
420 uint8_t transfer_flag = 0;
421 size_t fru_record_table_length = 0;
422 std::vector<uint8_t> fru_record_table_data(payloadLength);
423
424 auto rc = decode_get_fru_record_table_resp(
425 responsePtr, payloadLength, &cc, &next_data_transfer_handle,
426 &transfer_flag, fru_record_table_data.data(),
427 &fru_record_table_length);
428
429 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
430 {
431 std::cerr << "Response Message Error: "
432 << "rc=" << rc << ",cc=" << (int)cc << std::endl;
433 return;
434 }
435
436 FRUTablePrint tablePrint(fru_record_table_data.data(),
437 fru_record_table_length);
438 tablePrint.print();
439 }
440};
441
Sridevi Rameshd4489752019-12-08 09:03:29 -0600442void registerCommand(CLI::App& app)
443{
444 auto fru = app.add_subcommand("fru", "FRU type command");
445 fru->require_subcommand(1);
446 auto getFruRecordTableMetadata = fru->add_subcommand(
447 "GetFruRecordTableMetadata", "get FRU record table metadata");
448 commands.push_back(std::make_unique<GetFruRecordTableMetadata>(
449 "fru", "GetFruRecordTableMetadata", getFruRecordTableMetadata));
John Wang5bdde3a2020-06-16 15:02:33 +0800450
451 auto getFRURecordByOption =
452 fru->add_subcommand("GetFRURecordByOption", "get FRU Record By Option");
453 commands.push_back(std::make_unique<GetFRURecordByOption>(
454 "fru", "GetFRURecordByOption", getFRURecordByOption));
Sridevi Rameshbd9440b2020-03-24 02:14:36 -0500455
456 auto getFruRecordTable =
457 fru->add_subcommand("GetFruRecordTable", "get FRU Record Table");
458 commands.push_back(std::make_unique<GetFruRecordTable>(
459 "fru", "GetFruRecordTable", getFruRecordTable));
Sridevi Rameshd4489752019-12-08 09:03:29 -0600460}
461
462} // namespace fru
463
464} // namespace pldmtool