blob: 27ea6e23d85d86f399bd477ba748d3418925c726 [file] [log] [blame]
Ratan Gupta0b77cfa2017-01-31 17:32:31 +05301#include <exception>
Vishwa4be4b7a2015-10-31 22:55:50 -05002#include <vector>
3#include <stdlib.h>
4#include <dlfcn.h>
5#include <errno.h>
6#include <stdio.h>
Vishwa4be4b7a2015-10-31 22:55:50 -05007#include <systemd/sd-bus.h>
Chris Austenb45c4cb2015-11-01 06:34:56 -06008#include <unistd.h>
vishwa13555bd2015-11-10 12:10:38 -06009#include <host-ipmid/ipmid-api.h>
vishwac93d6d42015-12-16 11:55:16 -060010#include <iostream>
11#include <memory>
12#include <algorithm>
13#include <fstream>
Yi Li75c2d462016-04-11 16:57:46 +080014#include <sstream>
Brad Bishopde6a3792016-07-25 17:09:12 -040015#include <mapper.h>
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060016#include "types.hpp"
Ratan Gupta19c617b2017-02-10 15:39:40 +053017#include "frup.hpp"
Matthew Barth155c34f2016-10-18 14:33:17 -050018#include "fru-area.hpp"
Ratan Gupta0b77cfa2017-01-31 17:32:31 +053019#include <sdbusplus/server.hpp>
Vishwa4be4b7a2015-10-31 22:55:50 -050020
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060021using namespace ipmi::vpd;
22
Ratan Guptacb0d4e52016-12-22 19:05:57 +053023extern const FruMap frus;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060024extern const std::map<Path, InterfaceMap> extras;
Ratan Guptacb0d4e52016-12-22 19:05:57 +053025
vishwac93d6d42015-12-16 11:55:16 -060026//----------------------------------------------------------------
27// Constructor
28//----------------------------------------------------------------
29ipmi_fru::ipmi_fru(const uint8_t fruid, const ipmi_fru_area_type type,
30 sd_bus *bus_type, bool bmc_fru)
31{
32 iv_fruid = fruid;
33 iv_type = type;
34 iv_bmc_fru = bmc_fru;
35 iv_bus_type = bus_type;
36 iv_valid = false;
37 iv_data = NULL;
38 iv_present = false;
39
40 if(iv_type == IPMI_FRU_AREA_INTERNAL_USE)
41 {
42 iv_name = "INTERNAL_";
43 }
44 else if(iv_type == IPMI_FRU_AREA_CHASSIS_INFO)
45 {
46 iv_name = "CHASSIS_";
47 }
48 else if(iv_type == IPMI_FRU_AREA_BOARD_INFO)
49 {
50 iv_name = "BOARD_";
51 }
52 else if(iv_type == IPMI_FRU_AREA_PRODUCT_INFO)
53 {
54 iv_name = "PRODUCT_";
55 }
56 else if(iv_type == IPMI_FRU_AREA_MULTI_RECORD)
57 {
58 iv_name = "MULTI_";
59 }
60 else
61 {
62 iv_name = IPMI_FRU_AREA_TYPE_MAX;
63 fprintf(stderr, "ERROR: Invalid Area type :[%d]\n",iv_type);
64 }
65}
66
67//-----------------------------------------------------
68// For a FRU area type, accepts the data and updates
69// area specific data.
70//-----------------------------------------------------
71void ipmi_fru::set_data(const uint8_t *data, const size_t len)
72{
73 iv_len = len;
74 iv_data = new uint8_t[len];
75 memcpy(iv_data, data, len);
76}
77
78//-----------------------------------------------------
79// Sets the dbus parameters
80//-----------------------------------------------------
81void ipmi_fru::update_dbus_paths(const char *bus_name,
82 const char *obj_path, const char *intf_name)
83{
84 iv_bus_name = bus_name;
85 iv_obj_path = obj_path;
86 iv_intf_name = intf_name;
87}
88
89//-------------------
90// Destructor
91//-------------------
92ipmi_fru::~ipmi_fru()
93{
vishwac93d6d42015-12-16 11:55:16 -060094 if(iv_data != NULL)
95 {
96 delete [] iv_data;
97 iv_data = NULL;
98 }
vishwac93d6d42015-12-16 11:55:16 -060099}
100
Vishwa4be4b7a2015-10-31 22:55:50 -0500101//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600102// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600103// and returns the 8 bit checksum
104// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500105//-------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600106unsigned char calculate_crc(const unsigned char *data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500107{
108 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600109 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500110
111 for(byte = 0; byte < len; byte++)
112 {
113 crc += *data++;
114 }
vishwaf3ca3522015-12-02 10:35:13 -0600115
Vishwa4be4b7a2015-10-31 22:55:50 -0500116 return(-crc);
117}
118
119//---------------------------------------------------------------------
120// Accepts a fru area offset in commom hdr and tells which area it is.
121//---------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600122ipmi_fru_area_type get_fru_area_type(uint8_t area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500123{
124 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
125
126 switch(area_offset)
127 {
128 case IPMI_FRU_INTERNAL_OFFSET:
129 type = IPMI_FRU_AREA_INTERNAL_USE;
130 break;
131
132 case IPMI_FRU_CHASSIS_OFFSET:
133 type = IPMI_FRU_AREA_CHASSIS_INFO;
134 break;
135
136 case IPMI_FRU_BOARD_OFFSET:
137 type = IPMI_FRU_AREA_BOARD_INFO;
138 break;
139
140 case IPMI_FRU_PRODUCT_OFFSET:
141 type = IPMI_FRU_AREA_PRODUCT_INFO;
142 break;
143
144 case IPMI_FRU_MULTI_OFFSET:
145 type = IPMI_FRU_AREA_MULTI_RECORD;
146 break;
147
148 default:
149 type = IPMI_FRU_AREA_TYPE_MAX;
150 }
151
152 return type;
153}
154
vishwac93d6d42015-12-16 11:55:16 -0600155///-----------------------------------------------
156// Validates the data for crc and mandatory fields
157///-----------------------------------------------
158int verify_fru_data(const uint8_t *data, const size_t len)
159{
160 uint8_t checksum = 0;
161 int rc = -1;
162
163 // Validate for first byte to always have a value of [1]
164 if(data[0] != IPMI_FRU_HDR_BYTE_ZERO)
165 {
166 fprintf(stderr, "Invalid entry:[%d] in byte-0\n",data[0]);
167 return rc;
168 }
169#ifdef __IPMI_DEBUG__
170 else
171 {
172 printf("SUCCESS: Validated [0x%X] in entry_1 of fru_data\n",data[0]);
173 }
174#endif
175
176 // See if the calculated CRC matches with the embedded one.
177 // CRC to be calculated on all except the last one that is CRC itself.
178 checksum = calculate_crc(data, len - 1);
179 if(checksum != data[len-1])
180 {
181#ifdef __IPMI_DEBUG__
182 fprintf(stderr, "Checksum mismatch."
183 " Calculated:[0x%X], Embedded:[0x%X]\n",
184 checksum, data[len]);
185#endif
186 return rc;
187 }
188#ifdef __IPMI_DEBUG__
189 else
190 {
191 printf("SUCCESS: Checksum matches:[0x%X]\n",checksum);
192 }
193#endif
194
195 return EXIT_SUCCESS;
196}
197
Vishwa4be4b7a2015-10-31 22:55:50 -0500198//------------------------------------------------------------------------
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530199// Gets the value of the key from the fru dictionary of the given section.
200// FRU dictionary is parsed fru data for all the sections.
201//------------------------------------------------------------------------
202
203std::string getFRUValue(const std::string& section,
204 const std::string& key,
Ratan Guptacdde23d2017-02-15 09:29:54 +0530205 const std::string& delimiter,
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530206 IPMIFruInfo& fruData)
207{
208
209 auto minIndexValue = 0;
210 auto maxIndexValue = 0;
211 std::string fruValue = "";
212 if (section == "Board")
213 {
214 minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
215 maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
216 }
217 else if (section == "Product")
218 {
219 minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
220 maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
221
222 }
223 else if (section == "Chassis")
224 {
225 minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
226 maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
227 }
228
229 auto first = fruData.cbegin() + minIndexValue;
230 auto last = first + (maxIndexValue - minIndexValue) + 1;
231
232 auto itr = std::find_if(first, last,
233 [&key](auto& e){ return key == e.first; });
234
235 if (itr != last)
236 {
237 fruValue = itr->second;
238 }
Ratan Guptacdde23d2017-02-15 09:29:54 +0530239
240 //if the key is custom property then the value could be in two formats.
241 //1) custom field 2 = "value".
242 //2) custom field 2 = "key:value".
243 //if delimeter length = 0 i.e custom field 2 = "value"
244
245 constexpr auto customProp = "Custom Field";
246 if (key.find(customProp) != std::string::npos)
247 {
248 if (delimiter.length() > 0)
249 {
250 size_t delimiterpos = fruValue.find(delimiter);
251 if (delimiterpos != std::string::npos)
252 fruValue = fruValue.substr(delimiterpos + 1);
253 }
254 }
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530255 return fruValue;
256
257}
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530258//Get the inventory service from the mapper.
259auto getService(sdbusplus::bus::bus& bus,
260 const std::string& intf,
261 const std::string& path)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530262{
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530263 auto mapperCall = bus.new_method_call(
264 "xyz.openbmc_project.ObjectMapper",
Leonel Gonzalezc0e51342017-03-16 13:47:44 -0500265 "/xyz/openbmc_project/object_mapper",
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530266 "xyz.openbmc_project.ObjectMapper",
267 "GetObject");
268
269 mapperCall.append(path);
270 mapperCall.append(std::vector<std::string>({intf}));
271
272 auto mapperResponseMsg = bus.call(mapperCall);
273 if (mapperResponseMsg.is_method_error())
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530274 {
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530275 throw std::runtime_error("ERROR in mapper call");
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530276 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530277
278 std::map<std::string, std::vector<std::string>> mapperResponse;
279 mapperResponseMsg.read(mapperResponse);
280
281 if (mapperResponse.begin() == mapperResponse.end())
282 {
283 throw std::runtime_error("ERROR in reading the mapper response");
284 }
285
286 return mapperResponse.begin()->first;
287}
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530288
289//------------------------------------------------------------------------
Vishwa4be4b7a2015-10-31 22:55:50 -0500290// Takes FRU data, invokes Parser for each fru record area and updates
291// Inventory
292//------------------------------------------------------------------------
Patrick Williams21eb0432017-03-31 11:40:48 -0500293int ipmi_update_inventory(fru_area_vec_t& area_vec, sd_bus* bus_sd)
Vishwa4be4b7a2015-10-31 22:55:50 -0500294{
vishwac93d6d42015-12-16 11:55:16 -0600295 // Generic error reporter
Vishwa4be4b7a2015-10-31 22:55:50 -0500296 int rc = 0;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530297 uint8_t fruid = 0;
298 IPMIFruInfo fruData;
Vishwa4be4b7a2015-10-31 22:55:50 -0500299
Vishwa4be4b7a2015-10-31 22:55:50 -0500300 // For each FRU area, extract the needed data , get it parsed and update
301 // the Inventory.
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530302 for (const auto& fruArea : area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500303 {
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530304 fruid = fruArea->get_fruid();
Vishwa4be4b7a2015-10-31 22:55:50 -0500305 // Fill the container with information
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530306 rc = parse_fru_area((fruArea)->get_type(), (void*)(fruArea)->get_data(),
307 (fruArea)->get_len(), fruData);
308 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500309 {
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530310 std::cerr << "ERROR parsing FRU records\n";
311 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500312 }
313 } // END walking the vector of areas and updating
314
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530315 // For each Fru we have the list of instances which needs to be updated.
316 // Each instance object implements certain interfaces.
317 // Each Interface is having Dbus properties.
318 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
319
320 // Here we are just printing the object,interface and the properties.
321 // which needs to be called with the new inventory manager implementation.
Patrick Williams49a6fcd2017-04-18 11:49:07 -0500322 sdbusplus::bus::bus bus{bus_sd};
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530323 using namespace std::string_literals;
324 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
Brad Bishopec7648d2017-02-23 11:30:16 -0500325 static const auto path = "/xyz/openbmc_project/inventory"s;
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530326 std::string service;
327 try
328 {
329 service = getService(bus,intf,path);
330 }
331 catch (const std::runtime_error& e)
332 {
333 std::cerr << e.what() << "\n";
334 return -1;
335 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530336
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530337 auto iter = frus.find(fruid);
338 if (iter == frus.end())
339 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530340 std::cerr << "ERROR Unable to get the fru info for FRU="
341 << static_cast<int>(fruid) << "\n";
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530342 return -1;
343 }
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530344
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530345 auto& instanceList = iter->second;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530346 if (instanceList.size() <= 0)
347 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530348 std::cout << "Object List empty for this FRU="
349 << static_cast<int>(fruid) << "\n";
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530350 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530351
352 ObjectMap objects;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530353 for (auto& instance : instanceList)
354 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530355 InterfaceMap interfaces;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600356 const auto& extrasIter = extras.find(instance.first);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530357
358 for (auto& interfaceList : instance.second)
359 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530360 PropertyMap props;//store all the properties
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530361 for (auto& properties : interfaceList.second)
362 {
Ratan Guptacdde23d2017-02-15 09:29:54 +0530363 std::string section, property, delimiter, value;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530364 for (auto& info : properties.second)
365 {
366 if (info.first == "IPMIFruSection")
367 {
368 section = std::move(info.second);
369 }
370 if (info.first == "IPMIFruProperty")
371 {
372 property = std::move(info.second);
373 }
Ratan Guptacdde23d2017-02-15 09:29:54 +0530374 if (info.first == "IPMIFruValueDelimiter")
375 {
376 //Read the delimeter as ascii value
377 //convert it into char
378 if( info.second.length() > 0 )
379 {
380 char dlm = ' ';
381 rc = sscanf(info.second.c_str(),"%hhd",&dlm);
382 if (rc > 0)
383 {
384 delimiter = std::string(1,dlm);
385 }
386 }
387 }
388
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530389 }
390
391 if (!section.empty() && !property.empty())
392 {
Ratan Guptacdde23d2017-02-15 09:29:54 +0530393 value = getFRUValue(section, property, delimiter, fruData);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530394 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530395 props.emplace(std::move(properties.first), std::move(value));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530396 }
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600397 // Check and update extra properties
398 if(extras.end() != extrasIter)
399 {
400 const auto& propsIter =
401 (extrasIter->second).find(interfaceList.first);
402 if((extrasIter->second).end() != propsIter)
403 {
404 for(const auto& map : propsIter->second)
405 {
406 props.emplace(map.first, map.second);
407 }
408 }
409 }
410 interfaces.emplace(std::move(interfaceList.first),
411 std::move(props));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530412 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500413
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600414 // Call the inventory manager
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530415 sdbusplus::message::object_path path = instance.first;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600416 // Check and update extra properties
417 if(extras.end() != extrasIter)
418 {
419 for(const auto& entry : extrasIter->second)
420 {
421 if(interfaces.end() == interfaces.find(entry.first))
422 {
423 interfaces.emplace(entry.first, entry.second);
424 }
425 }
426 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530427 objects.emplace(path,interfaces);
428 }
429
430 auto pimMsg = bus.new_method_call(
431 service.c_str(),
432 path.c_str(),
433 intf.c_str(),
434 "Notify");
435 pimMsg.append(std::move(objects));
436 auto inventoryMgrResponseMsg = bus.call(pimMsg);
437 if (inventoryMgrResponseMsg.is_method_error())
438 {
439 std::cerr << "Error in notify call\n";
440 return -1;
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530441 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500442 return rc;
443}
444
vishwac93d6d42015-12-16 11:55:16 -0600445///----------------------------------------------------
446// Checks if a particular fru area is populated or not
447///----------------------------------------------------
448bool remove_invalid_area(const std::unique_ptr<ipmi_fru> &fru_area)
Vishwa4be4b7a2015-10-31 22:55:50 -0500449{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600450 // Filter the ones that are empty
451 if(!(fru_area->get_len()))
vishwac93d6d42015-12-16 11:55:16 -0600452 {
453 return true;
454 }
455 return false;
456}
Vishwa4be4b7a2015-10-31 22:55:50 -0500457
vishwac93d6d42015-12-16 11:55:16 -0600458///----------------------------------------------------------------------------------
459// Populates various FRU areas
460// @prereq : This must be called only after validating common header.
461///----------------------------------------------------------------------------------
462int ipmi_populate_fru_areas(uint8_t *fru_data, const size_t data_len,
463 fru_area_vec_t & fru_area_vec)
464{
vishwa13555bd2015-11-10 12:10:38 -0600465 size_t area_offset = 0;
vishwac93d6d42015-12-16 11:55:16 -0600466 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500467
vishwac93d6d42015-12-16 11:55:16 -0600468 // Now walk the common header and see if the file size has atleast the last
469 // offset mentioned by the common_hdr. If the file size is less than the
470 // offset of any if the fru areas mentioned in the common header, then we do
471 // not have a complete file.
472 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
473 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
474 {
Yi Li75c2d462016-04-11 16:57:46 +0800475 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600476 // Actual offset in the payload is the offset mentioned in common header
477 // multipled by 8. Common header is always the first 8 bytes.
478 area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
479 if(area_offset && (data_len < (area_offset + 2)))
480 {
481 // Our file size is less than what it needs to be. +2 because we are
482 // using area len that is at 2 byte off area_offset
Patrick Williams3365ec82016-08-17 17:45:18 -0500483 fprintf(stderr, "fru file is incomplete. Size:[%zd]\n",data_len);
vishwac93d6d42015-12-16 11:55:16 -0600484 return rc;
485 }
486 else if(area_offset)
487 {
488 // Read 2 bytes to know the actual size of area.
489 uint8_t area_hdr[2] = {0};
490 memcpy(area_hdr, &((uint8_t *)fru_data)[area_offset], sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500491
vishwac93d6d42015-12-16 11:55:16 -0600492 // Size of this area will be the 2nd byte in the fru area header.
493 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
494 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500495
Patrick Williams3365ec82016-08-17 17:45:18 -0500496 printf("fru data size:[%zd], area offset:[%zd], area_size:[%zd]\n",
vishwac93d6d42015-12-16 11:55:16 -0600497 data_len, area_offset, area_len);
Vishwa4be4b7a2015-10-31 22:55:50 -0500498
vishwac93d6d42015-12-16 11:55:16 -0600499 // See if we really have that much buffer. We have area offset amd
500 // from there, the actual len.
501 if(data_len < (area_len + area_offset))
502 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500503 fprintf(stderr, "Incomplete Fru file.. Size:[%zd]\n",data_len);
vishwac93d6d42015-12-16 11:55:16 -0600504 return rc;
505 }
506
507 // Save off the data.
508 memcpy(area_data, &((uint8_t *)fru_data)[area_offset], area_len);
509
510 // Validate the crc
511 rc = verify_fru_data(area_data, area_len);
512 if(rc < 0)
513 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500514 fprintf(stderr, "Error validating fru area. offset:[%zd]\n",area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600515 return rc;
516 }
517 else
518 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500519 printf("Successfully verified area checksum. offset:[%zd]\n",area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600520 }
521
522 // We already have a vector that is passed to us containing all
523 // of the fields populated. Update the data portion now.
524 for(auto& iter : fru_area_vec)
525 {
526 if((iter)->get_type() == get_fru_area_type(fru_entry))
527 {
528 (iter)->set_data(area_data, area_len);
529 }
530 }
531 } // If we have fru data present
532 } // Walk common_hdr
533
534 // Not all the fields will be populated in a fru data. Mostly all cases will
535 // not have more than 2 or 3.
536 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
537 remove_invalid_area), fru_area_vec.end());
538
539 return EXIT_SUCCESS;
540}
541
542///---------------------------------------------------------
543// Validates the fru data per ipmi common header constructs.
544// Returns with updated common_hdr and also file_size
545//----------------------------------------------------------
546int ipmi_validate_common_hdr(const uint8_t *fru_data, const size_t data_len)
547{
548 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500549
550 uint8_t common_hdr[sizeof(struct common_header)] = {0};
vishwac93d6d42015-12-16 11:55:16 -0600551 if(data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500552 {
vishwac93d6d42015-12-16 11:55:16 -0600553 memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500554 }
555 else
556 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500557 fprintf(stderr, "Incomplete fru data file. Size:[%zd]\n", data_len);
vishwac93d6d42015-12-16 11:55:16 -0600558 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500559 }
560
vishwac93d6d42015-12-16 11:55:16 -0600561 // Verify the crc and size
562 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
563 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500564 {
vishwac93d6d42015-12-16 11:55:16 -0600565 fprintf(stderr, "Failed to validate common header\n");
566 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500567 }
568
vishwac93d6d42015-12-16 11:55:16 -0600569 return EXIT_SUCCESS;
570}
Vishwa4be4b7a2015-10-31 22:55:50 -0500571
vishwac93d6d42015-12-16 11:55:16 -0600572//------------------------------------------------------------
573// Cleanup routine
574//------------------------------------------------------------
575int cleanup_error(FILE *fru_fp, fru_area_vec_t & fru_area_vec)
576{
577 if(fru_fp != NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500578 {
vishwac93d6d42015-12-16 11:55:16 -0600579 fclose(fru_fp);
580 fru_fp = NULL;
581 }
vishwaf3ca3522015-12-02 10:35:13 -0600582
Vishwa4be4b7a2015-10-31 22:55:50 -0500583 if(!(fru_area_vec.empty()))
584 {
vishwac93d6d42015-12-16 11:55:16 -0600585 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500586 }
vishwaf3ca3522015-12-02 10:35:13 -0600587
vishwac93d6d42015-12-16 11:55:16 -0600588 return -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500589}
590
591///-----------------------------------------------------
592// Accepts the filename and validates per IPMI FRU spec
593//----------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600594int ipmi_validate_fru_area(const uint8_t fruid, const char *fru_file_name,
vishwac93d6d42015-12-16 11:55:16 -0600595 sd_bus *bus_type, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500596{
vishwac93d6d42015-12-16 11:55:16 -0600597 size_t data_len = 0;
598 size_t bytes_read = 0;
599 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500600
vishwac93d6d42015-12-16 11:55:16 -0600601 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
602 // are not used, keeping it here for completeness.
603 fru_area_vec_t fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800604 std::vector<std::string> defined_fru_area;
605
vishwac93d6d42015-12-16 11:55:16 -0600606 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
607 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
608 {
609 // Create an object and push onto a vector.
610 std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru>
611 (fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru);
612
613 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500614 bool present = access(fru_file_name, F_OK) == 0;
vishwac93d6d42015-12-16 11:55:16 -0600615 fru_area->set_present(present);
616
vishwac93d6d42015-12-16 11:55:16 -0600617 fru_area_vec.emplace_back(std::move(fru_area));
618 }
619
620 FILE *fru_fp = fopen(fru_file_name,"rb");
621 if(fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500622 {
623 fprintf(stderr, "ERROR: opening:[%s]\n",fru_file_name);
624 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600625 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500626 }
627
vishwac93d6d42015-12-16 11:55:16 -0600628 // Get the size of the file to see if it meets minimum requirement
629 if(fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500630 {
631 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600632 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500633 }
634
vishwac93d6d42015-12-16 11:55:16 -0600635 // Allocate a buffer to hold entire file content
636 data_len = ftell(fru_fp);
637 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500638
vishwac93d6d42015-12-16 11:55:16 -0600639 rewind(fru_fp);
640 bytes_read = fread(fru_data, data_len, 1, fru_fp);
Vishwa4be4b7a2015-10-31 22:55:50 -0500641 if(bytes_read != 1)
642 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500643 fprintf(stderr, "Failed reading fru data. Bytes_read=[%zd]\n",bytes_read);
Vishwa4be4b7a2015-10-31 22:55:50 -0500644 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600645 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500646 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500647
vishwac93d6d42015-12-16 11:55:16 -0600648 // We are done reading.
649 fclose(fru_fp);
650 fru_fp = NULL;
651
652 rc = ipmi_validate_common_hdr(fru_data, data_len);
vishwaf3ca3522015-12-02 10:35:13 -0600653 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500654 {
vishwac93d6d42015-12-16 11:55:16 -0600655 return cleanup_error(fru_fp, fru_area_vec);
656 }
657
658 // Now that we validated the common header, populate various fru sections if we have them here.
659 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
660 if(rc < 0)
661 {
662 fprintf(stderr,"Populating FRU areas failed for:[%d]\n",fruid);
663 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500664 }
665 else
666 {
vishwac93d6d42015-12-16 11:55:16 -0600667 printf("SUCCESS: Populated FRU areas for:[%s]\n",fru_file_name);
Vishwa4be4b7a2015-10-31 22:55:50 -0500668 }
669
vishwac93d6d42015-12-16 11:55:16 -0600670#ifdef __IPMI_DEBUG__
671 for(auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500672 {
vishwac93d6d42015-12-16 11:55:16 -0600673 printf("FRU ID : [%d]\n",(iter)->get_fruid());
674 printf("AREA NAME : [%s]\n",(iter)->get_name());
675 printf("TYPE : [%d]\n",(iter)->get_type());
676 printf("LEN : [%d]\n",(iter)->get_len());
677 printf("BUS NAME : [%s]\n", (iter)->get_bus_name());
678 printf("OBJ PATH : [%s]\n", (iter)->get_obj_path());
679 printf("INTF NAME :[%s]\n", (iter)->get_intf_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500680 }
vishwac93d6d42015-12-16 11:55:16 -0600681#endif
682
683 // If the vector is populated with everything, then go ahead and update the
684 // inventory.
685 if(!(fru_area_vec.empty()))
686 {
687
688#ifdef __IPMI_DEBUG__
689 printf("\n SIZE of vector is : [%d] \n",fru_area_vec.size());
690#endif
Patrick Williams21eb0432017-03-31 11:40:48 -0500691 rc = ipmi_update_inventory(fru_area_vec, bus_type);
vishwac93d6d42015-12-16 11:55:16 -0600692 if(rc <0)
693 {
694 fprintf(stderr, "Error updating inventory\n");
695 }
696 }
697
698 // we are done with all that we wanted to do. This will do the job of
699 // calling any destructors too.
700 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500701
702 return rc;
703}