blob: 1b8acba96387f8e603d568d5f953c3f95f6dc7ef [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".
Gunnar Millsc19b8132018-06-14 08:56:17 -0500243 //if delimiter length = 0 i.e custom field 2 = "value"
Ratan Guptacdde23d2017-02-15 09:29:54 +0530244
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;
Ratan Guptac19c0542018-02-04 23:24:44 +0530356 const auto& extrasIter = extras.find(instance.path);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530357
Ratan Guptac19c0542018-02-04 23:24:44 +0530358 for (auto& interfaceList : instance.interfaces)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530359 {
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 {
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500363 std::string value;
364 decltype(auto) pdata = properties.second;
Ratan Guptacdde23d2017-02-15 09:29:54 +0530365
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500366 if (!pdata.section.empty() && !pdata.property.empty())
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530367 {
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500368 value = getFRUValue(pdata.section, pdata.property,
369 pdata.delimiter, fruData);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530370 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530371 props.emplace(std::move(properties.first), std::move(value));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530372 }
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600373 // Check and update extra properties
374 if(extras.end() != extrasIter)
375 {
376 const auto& propsIter =
377 (extrasIter->second).find(interfaceList.first);
378 if((extrasIter->second).end() != propsIter)
379 {
380 for(const auto& map : propsIter->second)
381 {
382 props.emplace(map.first, map.second);
383 }
384 }
385 }
386 interfaces.emplace(std::move(interfaceList.first),
387 std::move(props));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530388 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500389
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600390 // Call the inventory manager
Ratan Guptac19c0542018-02-04 23:24:44 +0530391 sdbusplus::message::object_path path = instance.path;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600392 // Check and update extra properties
393 if(extras.end() != extrasIter)
394 {
395 for(const auto& entry : extrasIter->second)
396 {
397 if(interfaces.end() == interfaces.find(entry.first))
398 {
399 interfaces.emplace(entry.first, entry.second);
400 }
401 }
402 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530403 objects.emplace(path,interfaces);
404 }
405
406 auto pimMsg = bus.new_method_call(
407 service.c_str(),
408 path.c_str(),
409 intf.c_str(),
410 "Notify");
411 pimMsg.append(std::move(objects));
412 auto inventoryMgrResponseMsg = bus.call(pimMsg);
413 if (inventoryMgrResponseMsg.is_method_error())
414 {
415 std::cerr << "Error in notify call\n";
416 return -1;
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530417 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500418 return rc;
419}
420
vishwac93d6d42015-12-16 11:55:16 -0600421///----------------------------------------------------
422// Checks if a particular fru area is populated or not
423///----------------------------------------------------
424bool remove_invalid_area(const std::unique_ptr<ipmi_fru> &fru_area)
Vishwa4be4b7a2015-10-31 22:55:50 -0500425{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600426 // Filter the ones that are empty
427 if(!(fru_area->get_len()))
vishwac93d6d42015-12-16 11:55:16 -0600428 {
429 return true;
430 }
431 return false;
432}
Vishwa4be4b7a2015-10-31 22:55:50 -0500433
vishwac93d6d42015-12-16 11:55:16 -0600434///----------------------------------------------------------------------------------
435// Populates various FRU areas
436// @prereq : This must be called only after validating common header.
437///----------------------------------------------------------------------------------
438int ipmi_populate_fru_areas(uint8_t *fru_data, const size_t data_len,
439 fru_area_vec_t & fru_area_vec)
440{
vishwa13555bd2015-11-10 12:10:38 -0600441 size_t area_offset = 0;
vishwac93d6d42015-12-16 11:55:16 -0600442 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500443
vishwac93d6d42015-12-16 11:55:16 -0600444 // Now walk the common header and see if the file size has atleast the last
445 // offset mentioned by the common_hdr. If the file size is less than the
446 // offset of any if the fru areas mentioned in the common header, then we do
447 // not have a complete file.
448 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
449 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
450 {
Yi Li75c2d462016-04-11 16:57:46 +0800451 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600452 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500453 // multiplied by 8. Common header is always the first 8 bytes.
vishwac93d6d42015-12-16 11:55:16 -0600454 area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
455 if(area_offset && (data_len < (area_offset + 2)))
456 {
457 // Our file size is less than what it needs to be. +2 because we are
458 // using area len that is at 2 byte off area_offset
Patrick Williams3365ec82016-08-17 17:45:18 -0500459 fprintf(stderr, "fru file is incomplete. Size:[%zd]\n",data_len);
vishwac93d6d42015-12-16 11:55:16 -0600460 return rc;
461 }
462 else if(area_offset)
463 {
464 // Read 2 bytes to know the actual size of area.
465 uint8_t area_hdr[2] = {0};
466 memcpy(area_hdr, &((uint8_t *)fru_data)[area_offset], sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500467
vishwac93d6d42015-12-16 11:55:16 -0600468 // Size of this area will be the 2nd byte in the fru area header.
469 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
470 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500471
Patrick Williams3365ec82016-08-17 17:45:18 -0500472 printf("fru data size:[%zd], area offset:[%zd], area_size:[%zd]\n",
vishwac93d6d42015-12-16 11:55:16 -0600473 data_len, area_offset, area_len);
Vishwa4be4b7a2015-10-31 22:55:50 -0500474
vishwac93d6d42015-12-16 11:55:16 -0600475 // See if we really have that much buffer. We have area offset amd
476 // from there, the actual len.
477 if(data_len < (area_len + area_offset))
478 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500479 fprintf(stderr, "Incomplete Fru file.. Size:[%zd]\n",data_len);
vishwac93d6d42015-12-16 11:55:16 -0600480 return rc;
481 }
482
483 // Save off the data.
484 memcpy(area_data, &((uint8_t *)fru_data)[area_offset], area_len);
485
486 // Validate the crc
487 rc = verify_fru_data(area_data, area_len);
488 if(rc < 0)
489 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500490 fprintf(stderr, "Error validating fru area. offset:[%zd]\n",area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600491 return rc;
492 }
493 else
494 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500495 printf("Successfully verified area checksum. offset:[%zd]\n",area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600496 }
497
498 // We already have a vector that is passed to us containing all
499 // of the fields populated. Update the data portion now.
500 for(auto& iter : fru_area_vec)
501 {
502 if((iter)->get_type() == get_fru_area_type(fru_entry))
503 {
504 (iter)->set_data(area_data, area_len);
505 }
506 }
507 } // If we have fru data present
508 } // Walk common_hdr
509
510 // Not all the fields will be populated in a fru data. Mostly all cases will
511 // not have more than 2 or 3.
512 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
513 remove_invalid_area), fru_area_vec.end());
514
515 return EXIT_SUCCESS;
516}
517
518///---------------------------------------------------------
519// Validates the fru data per ipmi common header constructs.
520// Returns with updated common_hdr and also file_size
521//----------------------------------------------------------
522int ipmi_validate_common_hdr(const uint8_t *fru_data, const size_t data_len)
523{
524 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500525
526 uint8_t common_hdr[sizeof(struct common_header)] = {0};
vishwac93d6d42015-12-16 11:55:16 -0600527 if(data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500528 {
vishwac93d6d42015-12-16 11:55:16 -0600529 memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500530 }
531 else
532 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500533 fprintf(stderr, "Incomplete fru data file. Size:[%zd]\n", data_len);
vishwac93d6d42015-12-16 11:55:16 -0600534 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500535 }
536
vishwac93d6d42015-12-16 11:55:16 -0600537 // Verify the crc and size
538 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
539 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500540 {
vishwac93d6d42015-12-16 11:55:16 -0600541 fprintf(stderr, "Failed to validate common header\n");
542 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500543 }
544
vishwac93d6d42015-12-16 11:55:16 -0600545 return EXIT_SUCCESS;
546}
Vishwa4be4b7a2015-10-31 22:55:50 -0500547
vishwac93d6d42015-12-16 11:55:16 -0600548//------------------------------------------------------------
549// Cleanup routine
550//------------------------------------------------------------
551int cleanup_error(FILE *fru_fp, fru_area_vec_t & fru_area_vec)
552{
553 if(fru_fp != NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500554 {
vishwac93d6d42015-12-16 11:55:16 -0600555 fclose(fru_fp);
556 fru_fp = NULL;
557 }
vishwaf3ca3522015-12-02 10:35:13 -0600558
Vishwa4be4b7a2015-10-31 22:55:50 -0500559 if(!(fru_area_vec.empty()))
560 {
vishwac93d6d42015-12-16 11:55:16 -0600561 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500562 }
vishwaf3ca3522015-12-02 10:35:13 -0600563
vishwac93d6d42015-12-16 11:55:16 -0600564 return -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500565}
566
567///-----------------------------------------------------
568// Accepts the filename and validates per IPMI FRU spec
569//----------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600570int ipmi_validate_fru_area(const uint8_t fruid, const char *fru_file_name,
vishwac93d6d42015-12-16 11:55:16 -0600571 sd_bus *bus_type, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500572{
vishwac93d6d42015-12-16 11:55:16 -0600573 size_t data_len = 0;
574 size_t bytes_read = 0;
575 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500576
vishwac93d6d42015-12-16 11:55:16 -0600577 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
578 // are not used, keeping it here for completeness.
579 fru_area_vec_t fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800580 std::vector<std::string> defined_fru_area;
581
vishwac93d6d42015-12-16 11:55:16 -0600582 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
583 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
584 {
585 // Create an object and push onto a vector.
586 std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru>
587 (fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru);
588
589 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500590 bool present = access(fru_file_name, F_OK) == 0;
vishwac93d6d42015-12-16 11:55:16 -0600591 fru_area->set_present(present);
592
vishwac93d6d42015-12-16 11:55:16 -0600593 fru_area_vec.emplace_back(std::move(fru_area));
594 }
595
596 FILE *fru_fp = fopen(fru_file_name,"rb");
597 if(fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500598 {
599 fprintf(stderr, "ERROR: opening:[%s]\n",fru_file_name);
600 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600601 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500602 }
603
vishwac93d6d42015-12-16 11:55:16 -0600604 // Get the size of the file to see if it meets minimum requirement
605 if(fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500606 {
607 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600608 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500609 }
610
vishwac93d6d42015-12-16 11:55:16 -0600611 // Allocate a buffer to hold entire file content
612 data_len = ftell(fru_fp);
613 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500614
vishwac93d6d42015-12-16 11:55:16 -0600615 rewind(fru_fp);
616 bytes_read = fread(fru_data, data_len, 1, fru_fp);
Vishwa4be4b7a2015-10-31 22:55:50 -0500617 if(bytes_read != 1)
618 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500619 fprintf(stderr, "Failed reading fru data. Bytes_read=[%zd]\n",bytes_read);
Vishwa4be4b7a2015-10-31 22:55:50 -0500620 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600621 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500622 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500623
vishwac93d6d42015-12-16 11:55:16 -0600624 // We are done reading.
625 fclose(fru_fp);
626 fru_fp = NULL;
627
628 rc = ipmi_validate_common_hdr(fru_data, data_len);
vishwaf3ca3522015-12-02 10:35:13 -0600629 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500630 {
vishwac93d6d42015-12-16 11:55:16 -0600631 return cleanup_error(fru_fp, fru_area_vec);
632 }
633
634 // Now that we validated the common header, populate various fru sections if we have them here.
635 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
636 if(rc < 0)
637 {
638 fprintf(stderr,"Populating FRU areas failed for:[%d]\n",fruid);
639 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500640 }
641 else
642 {
vishwac93d6d42015-12-16 11:55:16 -0600643 printf("SUCCESS: Populated FRU areas for:[%s]\n",fru_file_name);
Vishwa4be4b7a2015-10-31 22:55:50 -0500644 }
645
vishwac93d6d42015-12-16 11:55:16 -0600646#ifdef __IPMI_DEBUG__
647 for(auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500648 {
vishwac93d6d42015-12-16 11:55:16 -0600649 printf("FRU ID : [%d]\n",(iter)->get_fruid());
650 printf("AREA NAME : [%s]\n",(iter)->get_name());
651 printf("TYPE : [%d]\n",(iter)->get_type());
652 printf("LEN : [%d]\n",(iter)->get_len());
653 printf("BUS NAME : [%s]\n", (iter)->get_bus_name());
654 printf("OBJ PATH : [%s]\n", (iter)->get_obj_path());
655 printf("INTF NAME :[%s]\n", (iter)->get_intf_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500656 }
vishwac93d6d42015-12-16 11:55:16 -0600657#endif
658
659 // If the vector is populated with everything, then go ahead and update the
660 // inventory.
661 if(!(fru_area_vec.empty()))
662 {
663
664#ifdef __IPMI_DEBUG__
665 printf("\n SIZE of vector is : [%d] \n",fru_area_vec.size());
666#endif
Patrick Williams21eb0432017-03-31 11:40:48 -0500667 rc = ipmi_update_inventory(fru_area_vec, bus_type);
vishwac93d6d42015-12-16 11:55:16 -0600668 if(rc <0)
669 {
670 fprintf(stderr, "Error updating inventory\n");
671 }
672 }
673
674 // we are done with all that we wanted to do. This will do the job of
675 // calling any destructors too.
676 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500677
678 return rc;
679}