blob: edaa7a61f4aadabf6964f9eae471377cbd104318 [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
21// OpenBMC System Manager dbus framework
vishwa13555bd2015-11-10 12:10:38 -060022const char *sys_object_name = "/org/openbmc/managers/System";
23const char *sys_intf_name = "org.openbmc.managers.System";
Vishwa4be4b7a2015-10-31 22:55:50 -050024
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060025using namespace ipmi::vpd;
26
Ratan Guptacb0d4e52016-12-22 19:05:57 +053027extern const FruMap frus;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060028extern const std::map<Path, InterfaceMap> extras;
Ratan Guptacb0d4e52016-12-22 19:05:57 +053029
vishwac93d6d42015-12-16 11:55:16 -060030//----------------------------------------------------------------
31// Constructor
32//----------------------------------------------------------------
33ipmi_fru::ipmi_fru(const uint8_t fruid, const ipmi_fru_area_type type,
34 sd_bus *bus_type, bool bmc_fru)
35{
36 iv_fruid = fruid;
37 iv_type = type;
38 iv_bmc_fru = bmc_fru;
39 iv_bus_type = bus_type;
40 iv_valid = false;
41 iv_data = NULL;
42 iv_present = false;
43
44 if(iv_type == IPMI_FRU_AREA_INTERNAL_USE)
45 {
46 iv_name = "INTERNAL_";
47 }
48 else if(iv_type == IPMI_FRU_AREA_CHASSIS_INFO)
49 {
50 iv_name = "CHASSIS_";
51 }
52 else if(iv_type == IPMI_FRU_AREA_BOARD_INFO)
53 {
54 iv_name = "BOARD_";
55 }
56 else if(iv_type == IPMI_FRU_AREA_PRODUCT_INFO)
57 {
58 iv_name = "PRODUCT_";
59 }
60 else if(iv_type == IPMI_FRU_AREA_MULTI_RECORD)
61 {
62 iv_name = "MULTI_";
63 }
64 else
65 {
66 iv_name = IPMI_FRU_AREA_TYPE_MAX;
67 fprintf(stderr, "ERROR: Invalid Area type :[%d]\n",iv_type);
68 }
69}
70
71//-----------------------------------------------------
72// For a FRU area type, accepts the data and updates
73// area specific data.
74//-----------------------------------------------------
75void ipmi_fru::set_data(const uint8_t *data, const size_t len)
76{
77 iv_len = len;
78 iv_data = new uint8_t[len];
79 memcpy(iv_data, data, len);
80}
81
82//-----------------------------------------------------
83// Sets the dbus parameters
84//-----------------------------------------------------
85void ipmi_fru::update_dbus_paths(const char *bus_name,
86 const char *obj_path, const char *intf_name)
87{
88 iv_bus_name = bus_name;
89 iv_obj_path = obj_path;
90 iv_intf_name = intf_name;
91}
92
93//-------------------
94// Destructor
95//-------------------
96ipmi_fru::~ipmi_fru()
97{
98 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
99 sd_bus_message *response = NULL;
100 int rc = 0;
101
102 if(iv_data != NULL)
103 {
104 delete [] iv_data;
105 iv_data = NULL;
106 }
107
108 // If we have not been successful in doing some updates and we are a BMC
109 // fru, then need to set the fault bits.
110 bool valid_dbus = !(iv_bus_name.empty()) &&
111 !(iv_obj_path.empty()) &&
112 !(iv_intf_name.empty());
113
114 // Based on bmc_fru, success in updating the FRU inventory we need to set
115 // some special bits.
116 if(iv_bmc_fru && valid_dbus)
117 {
118 // Set the Fault bit if we did not successfully process the fru
119 const char *fault_bit = iv_valid ? "False" : "True";
120
121 rc = sd_bus_call_method(iv_bus_type, // On the System Bus
122 iv_bus_name.c_str(), // Service to contact
123 iv_obj_path.c_str(), // Object path
124 iv_intf_name.c_str(), // Interface name
125 "setFault", // Method to be called
126 &bus_error, // object to return error
127 &response, // Response message on success
128 "s", // input message (string)
129 fault_bit); // First argument to setFault
130
131 if(rc <0)
132 {
133 fprintf(stderr,"Failed to set Fault bit, value:[%s] for fruid:[%d], path:[%s]\n",
134 fault_bit, iv_fruid, iv_obj_path.c_str());
135 }
136 else
137 {
138 printf("Fault bit set to :[%s] for fruid:[%d], Path:[%s]\n",
139 fault_bit, iv_fruid,iv_obj_path.c_str());
140 }
141
142 sd_bus_error_free(&bus_error);
143 sd_bus_message_unref(response);
144
145 // Set the Present bits
146 const char *present_bit = iv_present ? "True" : "False";
147
148 rc = sd_bus_call_method(iv_bus_type, // On the System Bus
149 iv_bus_name.c_str(), // Service to contact
150 iv_obj_path.c_str(), // Object path
151 iv_intf_name.c_str(), // Interface name
152 "setPresent", // Method to be called
153 &bus_error, // object to return error
154 &response, // Response message on success
155 "s", // input message (string)
156 present_bit); // First argument to setPresent
157 if(rc < 0)
158 {
159 fprintf(stderr,"Failed to set Present bit for fruid:[%d], path:[%s]\n",
160 iv_fruid, iv_obj_path.c_str());
161 }
162 else
163 {
Yi Li75c2d462016-04-11 16:57:46 +0800164 printf("Present bit set to :[%s] for fruid:[%d], Path[%s]:\n",
165 present_bit, iv_fruid, iv_obj_path.c_str());
vishwac93d6d42015-12-16 11:55:16 -0600166 }
167
168 sd_bus_error_free(&bus_error);
169 sd_bus_message_unref(response);
170 }
171}
172
173// Sets up the sd_bus structures for the given fru type
174int ipmi_fru::setup_sd_bus_paths(void)
175{
176 // Need this to get respective DBUS objects
177 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
178 sd_bus_message *response = NULL;
179 int rc = 0;
180
181 // What we need is BOARD_1, PRODUCT_1, CHASSIS_1 etc..
Patrick Williams867da972016-08-17 17:40:05 -0500182 char *inv_bus_name = NULL;
183 const char *inv_obj_path = NULL,
Patrick Williams804077d2016-07-29 15:26:18 -0500184 *inv_intf_name = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600185 char fru_area_name[16] = {0};
Brad Bishopde6a3792016-07-25 17:09:12 -0400186 char *sys_bus_name = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600187 sprintf(fru_area_name,"%s%d",iv_name.c_str(), iv_fruid);
188
189#ifdef __IPMI_DEBUG__
190 printf("Getting sd_bus for :[%s]\n",fru_area_name);
191#endif
192
Brad Bishopde6a3792016-07-25 17:09:12 -0400193 rc = mapper_get_service(iv_bus_type, sys_object_name, &sys_bus_name);
194 if(rc < 0)
195 {
196 fprintf(stderr, "Failed to get system manager service:[%s]\n",
197 strerror(-rc));
198 goto exit;
199 }
200
vishwac93d6d42015-12-16 11:55:16 -0600201 // We want to call a method "getObjectFromId" on System Bus that is
202 // made available over OpenBmc system services.
Yi Li75c2d462016-04-11 16:57:46 +0800203
vishwac93d6d42015-12-16 11:55:16 -0600204 rc = sd_bus_call_method(iv_bus_type, // On the System Bus
205 sys_bus_name, // Service to contact
206 sys_object_name, // Object path
207 sys_intf_name, // Interface name
208 "getObjectFromId", // Method to be called
209 &bus_error, // object to return error
210 &response, // Response message on success
211 "ss", // input message (string,string)
212 "FRU_STR", // First argument to getObjectFromId
213 fru_area_name); // Second Argument
vishwac93d6d42015-12-16 11:55:16 -0600214 if(rc < 0)
215 {
216 fprintf(stderr, "Failed to resolve fruid:[%d] to dbus: [%s]\n", iv_fruid, bus_error.message);
217 }
218 else
219 {
Brad Bishopde6a3792016-07-25 17:09:12 -0400220 // Method getObjectFromId returns 2 parameters and all are strings, namely
221 // object_path and interface name for accessing that particular
222 // FRU over Inventory SDBUS manager. 'ss' here mentions that format.
223 rc = sd_bus_message_read(response, "(ss)", &inv_obj_path, &inv_intf_name);
vishwac93d6d42015-12-16 11:55:16 -0600224 if(rc < 0)
225 {
226 fprintf(stderr, "Failed to parse response message:[%s]\n", strerror(-rc));
227 }
228 else
229 {
Brad Bishopde6a3792016-07-25 17:09:12 -0400230 rc = mapper_get_service(iv_bus_type, inv_obj_path, &inv_bus_name);
231 if(rc < 0)
232 {
233 fprintf(stderr, "Failed to get inventory item service:[%s]\n",
234 strerror(-rc));
235 goto exit;
236 }
vishwac93d6d42015-12-16 11:55:16 -0600237 // Update the paths in the area object
238 update_dbus_paths(inv_bus_name, inv_obj_path, inv_intf_name);
239 }
240 }
241
Brad Bishopde6a3792016-07-25 17:09:12 -0400242exit:
vishwac93d6d42015-12-16 11:55:16 -0600243#ifdef __IPMI_DEBUG__
244 printf("fru_area=[%s], inv_bus_name=[%s], inv_obj_path=[%s], inv_intf_name=[%s]\n",
245 fru_area_name, inv_bus_name, inv_obj_path, inv_intf_name);
246#endif
247
Brad Bishopde6a3792016-07-25 17:09:12 -0400248 free(sys_bus_name);
Patrick Williams867da972016-08-17 17:40:05 -0500249 free(inv_bus_name);
vishwac93d6d42015-12-16 11:55:16 -0600250 sd_bus_error_free(&bus_error);
251 sd_bus_message_unref(response);
252
253 return rc;
254}
255
Vishwa4be4b7a2015-10-31 22:55:50 -0500256//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600257// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600258// and returns the 8 bit checksum
259// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500260//-------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600261unsigned char calculate_crc(const unsigned char *data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500262{
263 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600264 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500265
266 for(byte = 0; byte < len; byte++)
267 {
268 crc += *data++;
269 }
vishwaf3ca3522015-12-02 10:35:13 -0600270
Vishwa4be4b7a2015-10-31 22:55:50 -0500271 return(-crc);
272}
273
274//---------------------------------------------------------------------
275// Accepts a fru area offset in commom hdr and tells which area it is.
276//---------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600277ipmi_fru_area_type get_fru_area_type(uint8_t area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500278{
279 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
280
281 switch(area_offset)
282 {
283 case IPMI_FRU_INTERNAL_OFFSET:
284 type = IPMI_FRU_AREA_INTERNAL_USE;
285 break;
286
287 case IPMI_FRU_CHASSIS_OFFSET:
288 type = IPMI_FRU_AREA_CHASSIS_INFO;
289 break;
290
291 case IPMI_FRU_BOARD_OFFSET:
292 type = IPMI_FRU_AREA_BOARD_INFO;
293 break;
294
295 case IPMI_FRU_PRODUCT_OFFSET:
296 type = IPMI_FRU_AREA_PRODUCT_INFO;
297 break;
298
299 case IPMI_FRU_MULTI_OFFSET:
300 type = IPMI_FRU_AREA_MULTI_RECORD;
301 break;
302
303 default:
304 type = IPMI_FRU_AREA_TYPE_MAX;
305 }
306
307 return type;
308}
309
vishwac93d6d42015-12-16 11:55:16 -0600310///-----------------------------------------------
311// Validates the data for crc and mandatory fields
312///-----------------------------------------------
313int verify_fru_data(const uint8_t *data, const size_t len)
314{
315 uint8_t checksum = 0;
316 int rc = -1;
317
318 // Validate for first byte to always have a value of [1]
319 if(data[0] != IPMI_FRU_HDR_BYTE_ZERO)
320 {
321 fprintf(stderr, "Invalid entry:[%d] in byte-0\n",data[0]);
322 return rc;
323 }
324#ifdef __IPMI_DEBUG__
325 else
326 {
327 printf("SUCCESS: Validated [0x%X] in entry_1 of fru_data\n",data[0]);
328 }
329#endif
330
331 // See if the calculated CRC matches with the embedded one.
332 // CRC to be calculated on all except the last one that is CRC itself.
333 checksum = calculate_crc(data, len - 1);
334 if(checksum != data[len-1])
335 {
336#ifdef __IPMI_DEBUG__
337 fprintf(stderr, "Checksum mismatch."
338 " Calculated:[0x%X], Embedded:[0x%X]\n",
339 checksum, data[len]);
340#endif
341 return rc;
342 }
343#ifdef __IPMI_DEBUG__
344 else
345 {
346 printf("SUCCESS: Checksum matches:[0x%X]\n",checksum);
347 }
348#endif
349
350 return EXIT_SUCCESS;
351}
352
Vishwa4be4b7a2015-10-31 22:55:50 -0500353//------------------------------------------------------------------------
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530354// Gets the value of the key from the fru dictionary of the given section.
355// FRU dictionary is parsed fru data for all the sections.
356//------------------------------------------------------------------------
357
358std::string getFRUValue(const std::string& section,
359 const std::string& key,
Ratan Guptacdde23d2017-02-15 09:29:54 +0530360 const std::string& delimiter,
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530361 IPMIFruInfo& fruData)
362{
363
364 auto minIndexValue = 0;
365 auto maxIndexValue = 0;
366 std::string fruValue = "";
367 if (section == "Board")
368 {
369 minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
370 maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
371 }
372 else if (section == "Product")
373 {
374 minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
375 maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
376
377 }
378 else if (section == "Chassis")
379 {
380 minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
381 maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
382 }
383
384 auto first = fruData.cbegin() + minIndexValue;
385 auto last = first + (maxIndexValue - minIndexValue) + 1;
386
387 auto itr = std::find_if(first, last,
388 [&key](auto& e){ return key == e.first; });
389
390 if (itr != last)
391 {
392 fruValue = itr->second;
393 }
Ratan Guptacdde23d2017-02-15 09:29:54 +0530394
395 //if the key is custom property then the value could be in two formats.
396 //1) custom field 2 = "value".
397 //2) custom field 2 = "key:value".
398 //if delimeter length = 0 i.e custom field 2 = "value"
399
400 constexpr auto customProp = "Custom Field";
401 if (key.find(customProp) != std::string::npos)
402 {
403 if (delimiter.length() > 0)
404 {
405 size_t delimiterpos = fruValue.find(delimiter);
406 if (delimiterpos != std::string::npos)
407 fruValue = fruValue.substr(delimiterpos + 1);
408 }
409 }
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530410 return fruValue;
411
412}
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530413//Get the inventory service from the mapper.
414auto getService(sdbusplus::bus::bus& bus,
415 const std::string& intf,
416 const std::string& path)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530417{
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530418 auto mapperCall = bus.new_method_call(
419 "xyz.openbmc_project.ObjectMapper",
420 "/xyz/openbmc_project/ObjectMapper",
421 "xyz.openbmc_project.ObjectMapper",
422 "GetObject");
423
424 mapperCall.append(path);
425 mapperCall.append(std::vector<std::string>({intf}));
426
427 auto mapperResponseMsg = bus.call(mapperCall);
428 if (mapperResponseMsg.is_method_error())
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530429 {
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530430 throw std::runtime_error("ERROR in mapper call");
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530431 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530432
433 std::map<std::string, std::vector<std::string>> mapperResponse;
434 mapperResponseMsg.read(mapperResponse);
435
436 if (mapperResponse.begin() == mapperResponse.end())
437 {
438 throw std::runtime_error("ERROR in reading the mapper response");
439 }
440
441 return mapperResponse.begin()->first;
442}
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530443
444//------------------------------------------------------------------------
Vishwa4be4b7a2015-10-31 22:55:50 -0500445// Takes FRU data, invokes Parser for each fru record area and updates
446// Inventory
447//------------------------------------------------------------------------
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530448int ipmi_update_inventory(fru_area_vec_t& area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500449{
vishwac93d6d42015-12-16 11:55:16 -0600450 // Generic error reporter
Vishwa4be4b7a2015-10-31 22:55:50 -0500451 int rc = 0;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530452 uint8_t fruid = 0;
453 IPMIFruInfo fruData;
Vishwa4be4b7a2015-10-31 22:55:50 -0500454
Vishwa4be4b7a2015-10-31 22:55:50 -0500455 // For each FRU area, extract the needed data , get it parsed and update
456 // the Inventory.
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530457 for (const auto& fruArea : area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500458 {
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530459 fruid = fruArea->get_fruid();
Vishwa4be4b7a2015-10-31 22:55:50 -0500460 // Fill the container with information
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530461 rc = parse_fru_area((fruArea)->get_type(), (void*)(fruArea)->get_data(),
462 (fruArea)->get_len(), fruData);
463 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500464 {
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530465 std::cerr << "ERROR parsing FRU records\n";
466 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500467 }
468 } // END walking the vector of areas and updating
469
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530470 // For each Fru we have the list of instances which needs to be updated.
471 // Each instance object implements certain interfaces.
472 // Each Interface is having Dbus properties.
473 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
474
475 // Here we are just printing the object,interface and the properties.
476 // which needs to be called with the new inventory manager implementation.
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530477 auto bus = sdbusplus::bus::new_default();
478 using namespace std::string_literals;
479 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
Brad Bishopec7648d2017-02-23 11:30:16 -0500480 static const auto path = "/xyz/openbmc_project/inventory"s;
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530481 std::string service;
482 try
483 {
484 service = getService(bus,intf,path);
485 }
486 catch (const std::runtime_error& e)
487 {
488 std::cerr << e.what() << "\n";
489 return -1;
490 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530491
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530492 auto iter = frus.find(fruid);
493 if (iter == frus.end())
494 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530495 std::cerr << "ERROR Unable to get the fru info for FRU="
496 << static_cast<int>(fruid) << "\n";
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530497 return -1;
498 }
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530499
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530500 auto& instanceList = iter->second;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530501 if (instanceList.size() <= 0)
502 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530503 std::cout << "Object List empty for this FRU="
504 << static_cast<int>(fruid) << "\n";
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530505 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530506
507 ObjectMap objects;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530508 for (auto& instance : instanceList)
509 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530510 InterfaceMap interfaces;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600511 const auto& extrasIter = extras.find(instance.first);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530512
513 for (auto& interfaceList : instance.second)
514 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530515 PropertyMap props;//store all the properties
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530516 for (auto& properties : interfaceList.second)
517 {
Ratan Guptacdde23d2017-02-15 09:29:54 +0530518 std::string section, property, delimiter, value;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530519 for (auto& info : properties.second)
520 {
521 if (info.first == "IPMIFruSection")
522 {
523 section = std::move(info.second);
524 }
525 if (info.first == "IPMIFruProperty")
526 {
527 property = std::move(info.second);
528 }
Ratan Guptacdde23d2017-02-15 09:29:54 +0530529 if (info.first == "IPMIFruValueDelimiter")
530 {
531 //Read the delimeter as ascii value
532 //convert it into char
533 if( info.second.length() > 0 )
534 {
535 char dlm = ' ';
536 rc = sscanf(info.second.c_str(),"%hhd",&dlm);
537 if (rc > 0)
538 {
539 delimiter = std::string(1,dlm);
540 }
541 }
542 }
543
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530544 }
545
546 if (!section.empty() && !property.empty())
547 {
Ratan Guptacdde23d2017-02-15 09:29:54 +0530548 value = getFRUValue(section, property, delimiter, fruData);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530549 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530550 props.emplace(std::move(properties.first), std::move(value));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530551 }
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600552 // Check and update extra properties
553 if(extras.end() != extrasIter)
554 {
555 const auto& propsIter =
556 (extrasIter->second).find(interfaceList.first);
557 if((extrasIter->second).end() != propsIter)
558 {
559 for(const auto& map : propsIter->second)
560 {
561 props.emplace(map.first, map.second);
562 }
563 }
564 }
565 interfaces.emplace(std::move(interfaceList.first),
566 std::move(props));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530567 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500568
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600569 // Call the inventory manager
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530570 sdbusplus::message::object_path path = instance.first;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600571 // Check and update extra properties
572 if(extras.end() != extrasIter)
573 {
574 for(const auto& entry : extrasIter->second)
575 {
576 if(interfaces.end() == interfaces.find(entry.first))
577 {
578 interfaces.emplace(entry.first, entry.second);
579 }
580 }
581 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530582 objects.emplace(path,interfaces);
583 }
584
585 auto pimMsg = bus.new_method_call(
586 service.c_str(),
587 path.c_str(),
588 intf.c_str(),
589 "Notify");
590 pimMsg.append(std::move(objects));
591 auto inventoryMgrResponseMsg = bus.call(pimMsg);
592 if (inventoryMgrResponseMsg.is_method_error())
593 {
594 std::cerr << "Error in notify call\n";
595 return -1;
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530596 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500597 return rc;
598}
599
vishwac93d6d42015-12-16 11:55:16 -0600600///----------------------------------------------------
601// Checks if a particular fru area is populated or not
602///----------------------------------------------------
603bool remove_invalid_area(const std::unique_ptr<ipmi_fru> &fru_area)
Vishwa4be4b7a2015-10-31 22:55:50 -0500604{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600605 // Filter the ones that are empty
606 if(!(fru_area->get_len()))
vishwac93d6d42015-12-16 11:55:16 -0600607 {
608 return true;
609 }
610 return false;
611}
Vishwa4be4b7a2015-10-31 22:55:50 -0500612
vishwac93d6d42015-12-16 11:55:16 -0600613///----------------------------------------------------------------------------------
614// Populates various FRU areas
615// @prereq : This must be called only after validating common header.
616///----------------------------------------------------------------------------------
617int ipmi_populate_fru_areas(uint8_t *fru_data, const size_t data_len,
618 fru_area_vec_t & fru_area_vec)
619{
vishwa13555bd2015-11-10 12:10:38 -0600620 size_t area_offset = 0;
vishwac93d6d42015-12-16 11:55:16 -0600621 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500622
vishwac93d6d42015-12-16 11:55:16 -0600623 // Now walk the common header and see if the file size has atleast the last
624 // offset mentioned by the common_hdr. If the file size is less than the
625 // offset of any if the fru areas mentioned in the common header, then we do
626 // not have a complete file.
627 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
628 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
629 {
Yi Li75c2d462016-04-11 16:57:46 +0800630 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600631 // Actual offset in the payload is the offset mentioned in common header
632 // multipled by 8. Common header is always the first 8 bytes.
633 area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
634 if(area_offset && (data_len < (area_offset + 2)))
635 {
636 // Our file size is less than what it needs to be. +2 because we are
637 // using area len that is at 2 byte off area_offset
Patrick Williams3365ec82016-08-17 17:45:18 -0500638 fprintf(stderr, "fru file is incomplete. Size:[%zd]\n",data_len);
vishwac93d6d42015-12-16 11:55:16 -0600639 return rc;
640 }
641 else if(area_offset)
642 {
643 // Read 2 bytes to know the actual size of area.
644 uint8_t area_hdr[2] = {0};
645 memcpy(area_hdr, &((uint8_t *)fru_data)[area_offset], sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500646
vishwac93d6d42015-12-16 11:55:16 -0600647 // Size of this area will be the 2nd byte in the fru area header.
648 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
649 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500650
Patrick Williams3365ec82016-08-17 17:45:18 -0500651 printf("fru data size:[%zd], area offset:[%zd], area_size:[%zd]\n",
vishwac93d6d42015-12-16 11:55:16 -0600652 data_len, area_offset, area_len);
Vishwa4be4b7a2015-10-31 22:55:50 -0500653
vishwac93d6d42015-12-16 11:55:16 -0600654 // See if we really have that much buffer. We have area offset amd
655 // from there, the actual len.
656 if(data_len < (area_len + area_offset))
657 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500658 fprintf(stderr, "Incomplete Fru file.. Size:[%zd]\n",data_len);
vishwac93d6d42015-12-16 11:55:16 -0600659 return rc;
660 }
661
662 // Save off the data.
663 memcpy(area_data, &((uint8_t *)fru_data)[area_offset], area_len);
664
665 // Validate the crc
666 rc = verify_fru_data(area_data, area_len);
667 if(rc < 0)
668 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500669 fprintf(stderr, "Error validating fru area. offset:[%zd]\n",area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600670 return rc;
671 }
672 else
673 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500674 printf("Successfully verified area checksum. offset:[%zd]\n",area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600675 }
676
677 // We already have a vector that is passed to us containing all
678 // of the fields populated. Update the data portion now.
679 for(auto& iter : fru_area_vec)
680 {
681 if((iter)->get_type() == get_fru_area_type(fru_entry))
682 {
683 (iter)->set_data(area_data, area_len);
684 }
685 }
686 } // If we have fru data present
687 } // Walk common_hdr
688
689 // Not all the fields will be populated in a fru data. Mostly all cases will
690 // not have more than 2 or 3.
691 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
692 remove_invalid_area), fru_area_vec.end());
693
694 return EXIT_SUCCESS;
695}
696
697///---------------------------------------------------------
698// Validates the fru data per ipmi common header constructs.
699// Returns with updated common_hdr and also file_size
700//----------------------------------------------------------
701int ipmi_validate_common_hdr(const uint8_t *fru_data, const size_t data_len)
702{
703 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500704
705 uint8_t common_hdr[sizeof(struct common_header)] = {0};
vishwac93d6d42015-12-16 11:55:16 -0600706 if(data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500707 {
vishwac93d6d42015-12-16 11:55:16 -0600708 memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500709 }
710 else
711 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500712 fprintf(stderr, "Incomplete fru data file. Size:[%zd]\n", data_len);
vishwac93d6d42015-12-16 11:55:16 -0600713 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500714 }
715
vishwac93d6d42015-12-16 11:55:16 -0600716 // Verify the crc and size
717 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
718 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500719 {
vishwac93d6d42015-12-16 11:55:16 -0600720 fprintf(stderr, "Failed to validate common header\n");
721 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500722 }
723
vishwac93d6d42015-12-16 11:55:16 -0600724 return EXIT_SUCCESS;
725}
Vishwa4be4b7a2015-10-31 22:55:50 -0500726
vishwac93d6d42015-12-16 11:55:16 -0600727//------------------------------------------------------------
728// Cleanup routine
729//------------------------------------------------------------
730int cleanup_error(FILE *fru_fp, fru_area_vec_t & fru_area_vec)
731{
732 if(fru_fp != NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500733 {
vishwac93d6d42015-12-16 11:55:16 -0600734 fclose(fru_fp);
735 fru_fp = NULL;
736 }
vishwaf3ca3522015-12-02 10:35:13 -0600737
Vishwa4be4b7a2015-10-31 22:55:50 -0500738 if(!(fru_area_vec.empty()))
739 {
vishwac93d6d42015-12-16 11:55:16 -0600740 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500741 }
vishwaf3ca3522015-12-02 10:35:13 -0600742
vishwac93d6d42015-12-16 11:55:16 -0600743 return -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500744}
745
Yi Li75c2d462016-04-11 16:57:46 +0800746
747///-----------------------------------------------------
748// Get the fru area names defined in BMC for a given @fruid.
749//----------------------------------------------------
750int get_defined_fru_area(sd_bus *bus_type, const uint8_t fruid,
751 std::vector<std::string> &defined_fru_area)
752{
753 // Need this to get respective DBUS objects
754 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
755 sd_bus_message *response = NULL;
756 int rc = 0;
Patrick Williams804077d2016-07-29 15:26:18 -0500757 const char *areas = NULL;
Brad Bishopde6a3792016-07-25 17:09:12 -0400758 char *sys_bus_name = NULL;
Yi Li75c2d462016-04-11 16:57:46 +0800759
760#ifdef __IPMI_DEBUG__
761 printf("Getting fru areas defined in Skeleton for :[%d]\n", fruid);
762#endif
763
Brad Bishopde6a3792016-07-25 17:09:12 -0400764 rc = mapper_get_service(bus_type, sys_object_name, &sys_bus_name);
765 if(rc < 0)
766 {
767 fprintf(stderr, "Failed to get system manager service:[%s]\n",
768 strerror(-rc));
769 goto exit;
770 }
771
Yi Li75c2d462016-04-11 16:57:46 +0800772 // We want to call a method "getFRUArea" on System Bus that is
773 // made available over OpenBmc system services.
774 rc = sd_bus_call_method(bus_type, // On the System Bus
775 sys_bus_name, // Service to contact
776 sys_object_name, // Object path
777 sys_intf_name, // Interface name
778 "getFRUArea", // Method to be called
779 &bus_error, // object to return error
780 &response, // Response message on success
781 "y", // input message (integer)
782 fruid); // Argument
783
784 if(rc < 0)
785 {
786 fprintf(stderr, "Failed to get fru area for fruid:[%d] to dbus: [%s]\n",
787 fruid, bus_error.message);
788 }
789 else
790 {
791 // if several fru area names are defined, the names are combined to
792 // a string seperated by ','
793 rc = sd_bus_message_read(response, "s", &areas);
794 if(rc < 0)
795 {
796 fprintf(stderr, "Failed to parse response message from getFRUArea:[%s]\n",
797 strerror(-rc));
798 }
799 else
800 {
801#ifdef __IPMI_DEBUG__
802 printf("get defined fru area: id: %d, areas: %s\n", fruid, areas);
803#endif
804 std::string area_name;
805 std::stringstream ss(areas);
806 // fru area names string is seperated by ',', parse it into tokens
807 while (std::getline(ss, area_name, ','))
808 {
809 if (!area_name.empty())
810 defined_fru_area.emplace_back(area_name);
811 }
812 }
813 }
814
Brad Bishopde6a3792016-07-25 17:09:12 -0400815exit:
816
817 free(sys_bus_name);
Yi Li75c2d462016-04-11 16:57:46 +0800818 sd_bus_error_free(&bus_error);
819 sd_bus_message_unref(response);
820
821 return rc;
822}
823
824
Vishwa4be4b7a2015-10-31 22:55:50 -0500825///-----------------------------------------------------
826// Accepts the filename and validates per IPMI FRU spec
827//----------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600828int ipmi_validate_fru_area(const uint8_t fruid, const char *fru_file_name,
vishwac93d6d42015-12-16 11:55:16 -0600829 sd_bus *bus_type, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500830{
vishwac93d6d42015-12-16 11:55:16 -0600831 size_t data_len = 0;
832 size_t bytes_read = 0;
833 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500834
vishwac93d6d42015-12-16 11:55:16 -0600835 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
836 // are not used, keeping it here for completeness.
837 fru_area_vec_t fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800838 std::vector<std::string> defined_fru_area;
839
840 // BMC defines fru areas that should be present in Skeleton
841 rc = get_defined_fru_area(bus_type, fruid, defined_fru_area);
842 if(rc < 0)
843 {
844 fprintf(stderr, "ERROR: cannot get defined fru area\n");
845 return rc;
846 }
vishwac93d6d42015-12-16 11:55:16 -0600847 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
848 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
849 {
850 // Create an object and push onto a vector.
851 std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru>
852 (fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru);
853
854 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500855 bool present = access(fru_file_name, F_OK) == 0;
vishwac93d6d42015-12-16 11:55:16 -0600856 fru_area->set_present(present);
857
Yi Li75c2d462016-04-11 16:57:46 +0800858 // Only setup dbus path for areas defined in BMC.
859 // Otherwise Skeleton will report 'not found' error
860 std::string fru_area_name = fru_area->get_name() + std::to_string(fruid);
861 auto iter = std::find(defined_fru_area.begin(), defined_fru_area.end(),
862 fru_area_name);
863 if (iter != defined_fru_area.end())
864 {
865 fru_area->setup_sd_bus_paths();
866 }
vishwac93d6d42015-12-16 11:55:16 -0600867 fru_area_vec.emplace_back(std::move(fru_area));
868 }
869
870 FILE *fru_fp = fopen(fru_file_name,"rb");
871 if(fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500872 {
873 fprintf(stderr, "ERROR: opening:[%s]\n",fru_file_name);
874 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600875 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500876 }
877
vishwac93d6d42015-12-16 11:55:16 -0600878 // Get the size of the file to see if it meets minimum requirement
879 if(fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500880 {
881 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600882 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500883 }
884
vishwac93d6d42015-12-16 11:55:16 -0600885 // Allocate a buffer to hold entire file content
886 data_len = ftell(fru_fp);
887 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500888
vishwac93d6d42015-12-16 11:55:16 -0600889 rewind(fru_fp);
890 bytes_read = fread(fru_data, data_len, 1, fru_fp);
Vishwa4be4b7a2015-10-31 22:55:50 -0500891 if(bytes_read != 1)
892 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500893 fprintf(stderr, "Failed reading fru data. Bytes_read=[%zd]\n",bytes_read);
Vishwa4be4b7a2015-10-31 22:55:50 -0500894 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600895 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500896 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500897
vishwac93d6d42015-12-16 11:55:16 -0600898 // We are done reading.
899 fclose(fru_fp);
900 fru_fp = NULL;
901
902 rc = ipmi_validate_common_hdr(fru_data, data_len);
vishwaf3ca3522015-12-02 10:35:13 -0600903 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500904 {
vishwac93d6d42015-12-16 11:55:16 -0600905 return cleanup_error(fru_fp, fru_area_vec);
906 }
907
908 // Now that we validated the common header, populate various fru sections if we have them here.
909 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
910 if(rc < 0)
911 {
912 fprintf(stderr,"Populating FRU areas failed for:[%d]\n",fruid);
913 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500914 }
915 else
916 {
vishwac93d6d42015-12-16 11:55:16 -0600917 printf("SUCCESS: Populated FRU areas for:[%s]\n",fru_file_name);
Vishwa4be4b7a2015-10-31 22:55:50 -0500918 }
919
vishwac93d6d42015-12-16 11:55:16 -0600920#ifdef __IPMI_DEBUG__
921 for(auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500922 {
vishwac93d6d42015-12-16 11:55:16 -0600923 printf("FRU ID : [%d]\n",(iter)->get_fruid());
924 printf("AREA NAME : [%s]\n",(iter)->get_name());
925 printf("TYPE : [%d]\n",(iter)->get_type());
926 printf("LEN : [%d]\n",(iter)->get_len());
927 printf("BUS NAME : [%s]\n", (iter)->get_bus_name());
928 printf("OBJ PATH : [%s]\n", (iter)->get_obj_path());
929 printf("INTF NAME :[%s]\n", (iter)->get_intf_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500930 }
vishwac93d6d42015-12-16 11:55:16 -0600931#endif
932
933 // If the vector is populated with everything, then go ahead and update the
934 // inventory.
935 if(!(fru_area_vec.empty()))
936 {
937
938#ifdef __IPMI_DEBUG__
939 printf("\n SIZE of vector is : [%d] \n",fru_area_vec.size());
940#endif
941 rc = ipmi_update_inventory(fru_area_vec);
942 if(rc <0)
943 {
944 fprintf(stderr, "Error updating inventory\n");
945 }
946 }
947
948 // we are done with all that we wanted to do. This will do the job of
949 // calling any destructors too.
950 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500951
952 return rc;
953}