blob: bf9ae7ff4e936fe9b3c5079588cb06cca11f291d [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>
vishwac93d6d42015-12-16 11:55:16 -060016#include "frup.h"
Matthew Barth155c34f2016-10-18 14:33:17 -050017#include "fru-area.hpp"
Ratan Guptacb0d4e52016-12-22 19:05:57 +053018#include "fru-gen.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
Ratan Guptacb0d4e52016-12-22 19:05:57 +053025extern const FruMap frus;
26
27// Association between interface and the dbus property
28using InterfaceList = std::map<std::string,
29 std::map<std::string, std::string>>;
30
31// Association between property and its value
32using PropertiesList = std::map<std::string, std::string>;
33
34
vishwac93d6d42015-12-16 11:55:16 -060035//----------------------------------------------------------------
36// Constructor
37//----------------------------------------------------------------
38ipmi_fru::ipmi_fru(const uint8_t fruid, const ipmi_fru_area_type type,
39 sd_bus *bus_type, bool bmc_fru)
40{
41 iv_fruid = fruid;
42 iv_type = type;
43 iv_bmc_fru = bmc_fru;
44 iv_bus_type = bus_type;
45 iv_valid = false;
46 iv_data = NULL;
47 iv_present = false;
48
49 if(iv_type == IPMI_FRU_AREA_INTERNAL_USE)
50 {
51 iv_name = "INTERNAL_";
52 }
53 else if(iv_type == IPMI_FRU_AREA_CHASSIS_INFO)
54 {
55 iv_name = "CHASSIS_";
56 }
57 else if(iv_type == IPMI_FRU_AREA_BOARD_INFO)
58 {
59 iv_name = "BOARD_";
60 }
61 else if(iv_type == IPMI_FRU_AREA_PRODUCT_INFO)
62 {
63 iv_name = "PRODUCT_";
64 }
65 else if(iv_type == IPMI_FRU_AREA_MULTI_RECORD)
66 {
67 iv_name = "MULTI_";
68 }
69 else
70 {
71 iv_name = IPMI_FRU_AREA_TYPE_MAX;
72 fprintf(stderr, "ERROR: Invalid Area type :[%d]\n",iv_type);
73 }
74}
75
76//-----------------------------------------------------
77// For a FRU area type, accepts the data and updates
78// area specific data.
79//-----------------------------------------------------
80void ipmi_fru::set_data(const uint8_t *data, const size_t len)
81{
82 iv_len = len;
83 iv_data = new uint8_t[len];
84 memcpy(iv_data, data, len);
85}
86
87//-----------------------------------------------------
88// Sets the dbus parameters
89//-----------------------------------------------------
90void ipmi_fru::update_dbus_paths(const char *bus_name,
91 const char *obj_path, const char *intf_name)
92{
93 iv_bus_name = bus_name;
94 iv_obj_path = obj_path;
95 iv_intf_name = intf_name;
96}
97
98//-------------------
99// Destructor
100//-------------------
101ipmi_fru::~ipmi_fru()
102{
103 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
104 sd_bus_message *response = NULL;
105 int rc = 0;
106
107 if(iv_data != NULL)
108 {
109 delete [] iv_data;
110 iv_data = NULL;
111 }
112
113 // If we have not been successful in doing some updates and we are a BMC
114 // fru, then need to set the fault bits.
115 bool valid_dbus = !(iv_bus_name.empty()) &&
116 !(iv_obj_path.empty()) &&
117 !(iv_intf_name.empty());
118
119 // Based on bmc_fru, success in updating the FRU inventory we need to set
120 // some special bits.
121 if(iv_bmc_fru && valid_dbus)
122 {
123 // Set the Fault bit if we did not successfully process the fru
124 const char *fault_bit = iv_valid ? "False" : "True";
125
126 rc = sd_bus_call_method(iv_bus_type, // On the System Bus
127 iv_bus_name.c_str(), // Service to contact
128 iv_obj_path.c_str(), // Object path
129 iv_intf_name.c_str(), // Interface name
130 "setFault", // Method to be called
131 &bus_error, // object to return error
132 &response, // Response message on success
133 "s", // input message (string)
134 fault_bit); // First argument to setFault
135
136 if(rc <0)
137 {
138 fprintf(stderr,"Failed to set Fault bit, value:[%s] for fruid:[%d], path:[%s]\n",
139 fault_bit, iv_fruid, iv_obj_path.c_str());
140 }
141 else
142 {
143 printf("Fault bit set to :[%s] for fruid:[%d], Path:[%s]\n",
144 fault_bit, iv_fruid,iv_obj_path.c_str());
145 }
146
147 sd_bus_error_free(&bus_error);
148 sd_bus_message_unref(response);
149
150 // Set the Present bits
151 const char *present_bit = iv_present ? "True" : "False";
152
153 rc = sd_bus_call_method(iv_bus_type, // On the System Bus
154 iv_bus_name.c_str(), // Service to contact
155 iv_obj_path.c_str(), // Object path
156 iv_intf_name.c_str(), // Interface name
157 "setPresent", // Method to be called
158 &bus_error, // object to return error
159 &response, // Response message on success
160 "s", // input message (string)
161 present_bit); // First argument to setPresent
162 if(rc < 0)
163 {
164 fprintf(stderr,"Failed to set Present bit for fruid:[%d], path:[%s]\n",
165 iv_fruid, iv_obj_path.c_str());
166 }
167 else
168 {
Yi Li75c2d462016-04-11 16:57:46 +0800169 printf("Present bit set to :[%s] for fruid:[%d], Path[%s]:\n",
170 present_bit, iv_fruid, iv_obj_path.c_str());
vishwac93d6d42015-12-16 11:55:16 -0600171 }
172
173 sd_bus_error_free(&bus_error);
174 sd_bus_message_unref(response);
175 }
176}
177
178// Sets up the sd_bus structures for the given fru type
179int ipmi_fru::setup_sd_bus_paths(void)
180{
181 // Need this to get respective DBUS objects
182 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
183 sd_bus_message *response = NULL;
184 int rc = 0;
185
186 // What we need is BOARD_1, PRODUCT_1, CHASSIS_1 etc..
Patrick Williams867da972016-08-17 17:40:05 -0500187 char *inv_bus_name = NULL;
188 const char *inv_obj_path = NULL,
Patrick Williams804077d2016-07-29 15:26:18 -0500189 *inv_intf_name = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600190 char fru_area_name[16] = {0};
Brad Bishopde6a3792016-07-25 17:09:12 -0400191 char *sys_bus_name = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600192 sprintf(fru_area_name,"%s%d",iv_name.c_str(), iv_fruid);
193
194#ifdef __IPMI_DEBUG__
195 printf("Getting sd_bus for :[%s]\n",fru_area_name);
196#endif
197
Brad Bishopde6a3792016-07-25 17:09:12 -0400198 rc = mapper_get_service(iv_bus_type, sys_object_name, &sys_bus_name);
199 if(rc < 0)
200 {
201 fprintf(stderr, "Failed to get system manager service:[%s]\n",
202 strerror(-rc));
203 goto exit;
204 }
205
vishwac93d6d42015-12-16 11:55:16 -0600206 // We want to call a method "getObjectFromId" on System Bus that is
207 // made available over OpenBmc system services.
Yi Li75c2d462016-04-11 16:57:46 +0800208
vishwac93d6d42015-12-16 11:55:16 -0600209 rc = sd_bus_call_method(iv_bus_type, // On the System Bus
210 sys_bus_name, // Service to contact
211 sys_object_name, // Object path
212 sys_intf_name, // Interface name
213 "getObjectFromId", // Method to be called
214 &bus_error, // object to return error
215 &response, // Response message on success
216 "ss", // input message (string,string)
217 "FRU_STR", // First argument to getObjectFromId
218 fru_area_name); // Second Argument
vishwac93d6d42015-12-16 11:55:16 -0600219 if(rc < 0)
220 {
221 fprintf(stderr, "Failed to resolve fruid:[%d] to dbus: [%s]\n", iv_fruid, bus_error.message);
222 }
223 else
224 {
Brad Bishopde6a3792016-07-25 17:09:12 -0400225 // Method getObjectFromId returns 2 parameters and all are strings, namely
226 // object_path and interface name for accessing that particular
227 // FRU over Inventory SDBUS manager. 'ss' here mentions that format.
228 rc = sd_bus_message_read(response, "(ss)", &inv_obj_path, &inv_intf_name);
vishwac93d6d42015-12-16 11:55:16 -0600229 if(rc < 0)
230 {
231 fprintf(stderr, "Failed to parse response message:[%s]\n", strerror(-rc));
232 }
233 else
234 {
Brad Bishopde6a3792016-07-25 17:09:12 -0400235 rc = mapper_get_service(iv_bus_type, inv_obj_path, &inv_bus_name);
236 if(rc < 0)
237 {
238 fprintf(stderr, "Failed to get inventory item service:[%s]\n",
239 strerror(-rc));
240 goto exit;
241 }
vishwac93d6d42015-12-16 11:55:16 -0600242 // Update the paths in the area object
243 update_dbus_paths(inv_bus_name, inv_obj_path, inv_intf_name);
244 }
245 }
246
Brad Bishopde6a3792016-07-25 17:09:12 -0400247exit:
vishwac93d6d42015-12-16 11:55:16 -0600248#ifdef __IPMI_DEBUG__
249 printf("fru_area=[%s], inv_bus_name=[%s], inv_obj_path=[%s], inv_intf_name=[%s]\n",
250 fru_area_name, inv_bus_name, inv_obj_path, inv_intf_name);
251#endif
252
Brad Bishopde6a3792016-07-25 17:09:12 -0400253 free(sys_bus_name);
Patrick Williams867da972016-08-17 17:40:05 -0500254 free(inv_bus_name);
vishwac93d6d42015-12-16 11:55:16 -0600255 sd_bus_error_free(&bus_error);
256 sd_bus_message_unref(response);
257
258 return rc;
259}
260
Vishwa4be4b7a2015-10-31 22:55:50 -0500261//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600262// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600263// and returns the 8 bit checksum
264// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500265//-------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600266unsigned char calculate_crc(const unsigned char *data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500267{
268 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600269 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500270
271 for(byte = 0; byte < len; byte++)
272 {
273 crc += *data++;
274 }
vishwaf3ca3522015-12-02 10:35:13 -0600275
Vishwa4be4b7a2015-10-31 22:55:50 -0500276 return(-crc);
277}
278
279//---------------------------------------------------------------------
280// Accepts a fru area offset in commom hdr and tells which area it is.
281//---------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600282ipmi_fru_area_type get_fru_area_type(uint8_t area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500283{
284 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
285
286 switch(area_offset)
287 {
288 case IPMI_FRU_INTERNAL_OFFSET:
289 type = IPMI_FRU_AREA_INTERNAL_USE;
290 break;
291
292 case IPMI_FRU_CHASSIS_OFFSET:
293 type = IPMI_FRU_AREA_CHASSIS_INFO;
294 break;
295
296 case IPMI_FRU_BOARD_OFFSET:
297 type = IPMI_FRU_AREA_BOARD_INFO;
298 break;
299
300 case IPMI_FRU_PRODUCT_OFFSET:
301 type = IPMI_FRU_AREA_PRODUCT_INFO;
302 break;
303
304 case IPMI_FRU_MULTI_OFFSET:
305 type = IPMI_FRU_AREA_MULTI_RECORD;
306 break;
307
308 default:
309 type = IPMI_FRU_AREA_TYPE_MAX;
310 }
311
312 return type;
313}
314
vishwac93d6d42015-12-16 11:55:16 -0600315///-----------------------------------------------
316// Validates the data for crc and mandatory fields
317///-----------------------------------------------
318int verify_fru_data(const uint8_t *data, const size_t len)
319{
320 uint8_t checksum = 0;
321 int rc = -1;
322
323 // Validate for first byte to always have a value of [1]
324 if(data[0] != IPMI_FRU_HDR_BYTE_ZERO)
325 {
326 fprintf(stderr, "Invalid entry:[%d] in byte-0\n",data[0]);
327 return rc;
328 }
329#ifdef __IPMI_DEBUG__
330 else
331 {
332 printf("SUCCESS: Validated [0x%X] in entry_1 of fru_data\n",data[0]);
333 }
334#endif
335
336 // See if the calculated CRC matches with the embedded one.
337 // CRC to be calculated on all except the last one that is CRC itself.
338 checksum = calculate_crc(data, len - 1);
339 if(checksum != data[len-1])
340 {
341#ifdef __IPMI_DEBUG__
342 fprintf(stderr, "Checksum mismatch."
343 " Calculated:[0x%X], Embedded:[0x%X]\n",
344 checksum, data[len]);
345#endif
346 return rc;
347 }
348#ifdef __IPMI_DEBUG__
349 else
350 {
351 printf("SUCCESS: Checksum matches:[0x%X]\n",checksum);
352 }
353#endif
354
355 return EXIT_SUCCESS;
356}
357
Vishwa4be4b7a2015-10-31 22:55:50 -0500358//------------------------------------------------------------------------
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530359// Gets the value of the key from the fru dictionary of the given section.
360// FRU dictionary is parsed fru data for all the sections.
361//------------------------------------------------------------------------
362
363std::string getFRUValue(const std::string& section,
364 const std::string& key,
365 IPMIFruInfo& fruData)
366{
367
368 auto minIndexValue = 0;
369 auto maxIndexValue = 0;
370 std::string fruValue = "";
371 if (section == "Board")
372 {
373 minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
374 maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
375 }
376 else if (section == "Product")
377 {
378 minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
379 maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
380
381 }
382 else if (section == "Chassis")
383 {
384 minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
385 maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
386 }
387
388 auto first = fruData.cbegin() + minIndexValue;
389 auto last = first + (maxIndexValue - minIndexValue) + 1;
390
391 auto itr = std::find_if(first, last,
392 [&key](auto& e){ return key == e.first; });
393
394 if (itr != last)
395 {
396 fruValue = itr->second;
397 }
398 return fruValue;
399
400}
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530401//Get the inventory service from the mapper.
402auto getService(sdbusplus::bus::bus& bus,
403 const std::string& intf,
404 const std::string& path)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530405{
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530406 auto mapperCall = bus.new_method_call(
407 "xyz.openbmc_project.ObjectMapper",
408 "/xyz/openbmc_project/ObjectMapper",
409 "xyz.openbmc_project.ObjectMapper",
410 "GetObject");
411
412 mapperCall.append(path);
413 mapperCall.append(std::vector<std::string>({intf}));
414
415 auto mapperResponseMsg = bus.call(mapperCall);
416 if (mapperResponseMsg.is_method_error())
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530417 {
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530418 throw std::runtime_error("ERROR in mapper call");
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530419 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530420
421 std::map<std::string, std::vector<std::string>> mapperResponse;
422 mapperResponseMsg.read(mapperResponse);
423
424 if (mapperResponse.begin() == mapperResponse.end())
425 {
426 throw std::runtime_error("ERROR in reading the mapper response");
427 }
428
429 return mapperResponse.begin()->first;
430}
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530431
432//------------------------------------------------------------------------
Vishwa4be4b7a2015-10-31 22:55:50 -0500433// Takes FRU data, invokes Parser for each fru record area and updates
434// Inventory
435//------------------------------------------------------------------------
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530436int ipmi_update_inventory(fru_area_vec_t& area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500437{
vishwac93d6d42015-12-16 11:55:16 -0600438 // Generic error reporter
Vishwa4be4b7a2015-10-31 22:55:50 -0500439 int rc = 0;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530440 uint8_t fruid = 0;
441 IPMIFruInfo fruData;
Vishwa4be4b7a2015-10-31 22:55:50 -0500442
Vishwa4be4b7a2015-10-31 22:55:50 -0500443 // For each FRU area, extract the needed data , get it parsed and update
444 // the Inventory.
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530445 for (const auto& fruArea : area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500446 {
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530447 fruid = fruArea->get_fruid();
Vishwa4be4b7a2015-10-31 22:55:50 -0500448 // Fill the container with information
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530449 rc = parse_fru_area((fruArea)->get_type(), (void*)(fruArea)->get_data(),
450 (fruArea)->get_len(), fruData);
451 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500452 {
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530453 std::cerr << "ERROR parsing FRU records\n";
454 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500455 }
456 } // END walking the vector of areas and updating
457
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530458 // For each Fru we have the list of instances which needs to be updated.
459 // Each instance object implements certain interfaces.
460 // Each Interface is having Dbus properties.
461 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
462
463 // Here we are just printing the object,interface and the properties.
464 // which needs to be called with the new inventory manager implementation.
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530465 auto bus = sdbusplus::bus::new_default();
466 using namespace std::string_literals;
467 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
468 static const auto path = "/xyz/openbmc_project/Inventory"s;
469 std::string service;
470 try
471 {
472 service = getService(bus,intf,path);
473 }
474 catch (const std::runtime_error& e)
475 {
476 std::cerr << e.what() << "\n";
477 return -1;
478 }
479 auto notify = [&]()
480 {
481 return bus.new_method_call(
482 service.c_str(),
483 path.c_str(),
484 intf.c_str(),
485 "Notify");
486 };
487
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530488 auto iter = frus.find(fruid);
489 if (iter == frus.end())
490 {
491 std::cerr << "ERROR Unable to get the fru info for FRU=" << fruid << "\n";
492 return -1;
493 }
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530494
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530495 auto& instanceList = iter->second;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530496 if (instanceList.size() <= 0)
497 {
498 std::cout << "Object List empty for this FRU=" << fruid << "\n";
499 }
500 for (auto& instance : instanceList)
501 {
502 InterfaceList interfaces;
503
504 for (auto& interfaceList : instance.second)
505 {
506 PropertiesList prop;//store all the properties
507 for (auto& properties : interfaceList.second)
508 {
509 std::string section, property, value;
510 for (auto& info : properties.second)
511 {
512 if (info.first == "IPMIFruSection")
513 {
514 section = std::move(info.second);
515 }
516 if (info.first == "IPMIFruProperty")
517 {
518 property = std::move(info.second);
519 }
520 }
521
522 if (!section.empty() && !property.empty())
523 {
524 value = getFRUValue(section, property, fruData);
525 }
526 prop.emplace(std::move(properties.first), std::move(value));
527 }
528 interfaces.emplace(std::move(interfaceList.first), std::move(prop));
529 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530530 //Call the inventory manager
531 sdbusplus::message::object_path relPath = instance.first;
Vishwa4be4b7a2015-10-31 22:55:50 -0500532
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530533 auto m = notify();
534 m.append(relPath);
535 m.append(interfaces);
536 auto inventoryMgrResponseMsg = bus.call(m);
537 if (inventoryMgrResponseMsg.is_method_error())
538 {
539 std::cerr << "Error in notify call\n";
540 return -1;
541 }
542 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500543 return rc;
544}
545
vishwac93d6d42015-12-16 11:55:16 -0600546///----------------------------------------------------
547// Checks if a particular fru area is populated or not
548///----------------------------------------------------
549bool remove_invalid_area(const std::unique_ptr<ipmi_fru> &fru_area)
Vishwa4be4b7a2015-10-31 22:55:50 -0500550{
vishwac93d6d42015-12-16 11:55:16 -0600551 // Filter the ones that do not have dbus reference.
552 if((strlen((fru_area)->get_bus_name()) == 0) ||
553 (strlen((fru_area)->get_obj_path()) == 0) ||
554 (strlen((fru_area)->get_intf_name()) == 0))
555 {
556 return true;
557 }
558 return false;
559}
Vishwa4be4b7a2015-10-31 22:55:50 -0500560
vishwac93d6d42015-12-16 11:55:16 -0600561///----------------------------------------------------------------------------------
562// Populates various FRU areas
563// @prereq : This must be called only after validating common header.
564///----------------------------------------------------------------------------------
565int ipmi_populate_fru_areas(uint8_t *fru_data, const size_t data_len,
566 fru_area_vec_t & fru_area_vec)
567{
vishwa13555bd2015-11-10 12:10:38 -0600568 size_t area_offset = 0;
vishwac93d6d42015-12-16 11:55:16 -0600569 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500570
vishwac93d6d42015-12-16 11:55:16 -0600571 // Now walk the common header and see if the file size has atleast the last
572 // offset mentioned by the common_hdr. If the file size is less than the
573 // offset of any if the fru areas mentioned in the common header, then we do
574 // not have a complete file.
575 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
576 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
577 {
Yi Li75c2d462016-04-11 16:57:46 +0800578 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600579 // Actual offset in the payload is the offset mentioned in common header
580 // multipled by 8. Common header is always the first 8 bytes.
581 area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
582 if(area_offset && (data_len < (area_offset + 2)))
583 {
584 // Our file size is less than what it needs to be. +2 because we are
585 // using area len that is at 2 byte off area_offset
Patrick Williams3365ec82016-08-17 17:45:18 -0500586 fprintf(stderr, "fru file is incomplete. Size:[%zd]\n",data_len);
vishwac93d6d42015-12-16 11:55:16 -0600587 return rc;
588 }
589 else if(area_offset)
590 {
591 // Read 2 bytes to know the actual size of area.
592 uint8_t area_hdr[2] = {0};
593 memcpy(area_hdr, &((uint8_t *)fru_data)[area_offset], sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500594
vishwac93d6d42015-12-16 11:55:16 -0600595 // Size of this area will be the 2nd byte in the fru area header.
596 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
597 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500598
Patrick Williams3365ec82016-08-17 17:45:18 -0500599 printf("fru data size:[%zd], area offset:[%zd], area_size:[%zd]\n",
vishwac93d6d42015-12-16 11:55:16 -0600600 data_len, area_offset, area_len);
Vishwa4be4b7a2015-10-31 22:55:50 -0500601
vishwac93d6d42015-12-16 11:55:16 -0600602 // See if we really have that much buffer. We have area offset amd
603 // from there, the actual len.
604 if(data_len < (area_len + area_offset))
605 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500606 fprintf(stderr, "Incomplete Fru file.. Size:[%zd]\n",data_len);
vishwac93d6d42015-12-16 11:55:16 -0600607 return rc;
608 }
609
610 // Save off the data.
611 memcpy(area_data, &((uint8_t *)fru_data)[area_offset], area_len);
612
613 // Validate the crc
614 rc = verify_fru_data(area_data, area_len);
615 if(rc < 0)
616 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500617 fprintf(stderr, "Error validating fru area. offset:[%zd]\n",area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600618 return rc;
619 }
620 else
621 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500622 printf("Successfully verified area checksum. offset:[%zd]\n",area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600623 }
624
625 // We already have a vector that is passed to us containing all
626 // of the fields populated. Update the data portion now.
627 for(auto& iter : fru_area_vec)
628 {
629 if((iter)->get_type() == get_fru_area_type(fru_entry))
630 {
631 (iter)->set_data(area_data, area_len);
632 }
633 }
634 } // If we have fru data present
635 } // Walk common_hdr
636
637 // Not all the fields will be populated in a fru data. Mostly all cases will
638 // not have more than 2 or 3.
639 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
640 remove_invalid_area), fru_area_vec.end());
641
642 return EXIT_SUCCESS;
643}
644
645///---------------------------------------------------------
646// Validates the fru data per ipmi common header constructs.
647// Returns with updated common_hdr and also file_size
648//----------------------------------------------------------
649int ipmi_validate_common_hdr(const uint8_t *fru_data, const size_t data_len)
650{
651 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500652
653 uint8_t common_hdr[sizeof(struct common_header)] = {0};
vishwac93d6d42015-12-16 11:55:16 -0600654 if(data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500655 {
vishwac93d6d42015-12-16 11:55:16 -0600656 memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500657 }
658 else
659 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500660 fprintf(stderr, "Incomplete fru data file. Size:[%zd]\n", data_len);
vishwac93d6d42015-12-16 11:55:16 -0600661 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500662 }
663
vishwac93d6d42015-12-16 11:55:16 -0600664 // Verify the crc and size
665 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
666 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500667 {
vishwac93d6d42015-12-16 11:55:16 -0600668 fprintf(stderr, "Failed to validate common header\n");
669 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500670 }
671
vishwac93d6d42015-12-16 11:55:16 -0600672 return EXIT_SUCCESS;
673}
Vishwa4be4b7a2015-10-31 22:55:50 -0500674
vishwac93d6d42015-12-16 11:55:16 -0600675//------------------------------------------------------------
676// Cleanup routine
677//------------------------------------------------------------
678int cleanup_error(FILE *fru_fp, fru_area_vec_t & fru_area_vec)
679{
680 if(fru_fp != NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500681 {
vishwac93d6d42015-12-16 11:55:16 -0600682 fclose(fru_fp);
683 fru_fp = NULL;
684 }
vishwaf3ca3522015-12-02 10:35:13 -0600685
Vishwa4be4b7a2015-10-31 22:55:50 -0500686 if(!(fru_area_vec.empty()))
687 {
vishwac93d6d42015-12-16 11:55:16 -0600688 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500689 }
vishwaf3ca3522015-12-02 10:35:13 -0600690
vishwac93d6d42015-12-16 11:55:16 -0600691 return -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500692}
693
Yi Li75c2d462016-04-11 16:57:46 +0800694
695///-----------------------------------------------------
696// Get the fru area names defined in BMC for a given @fruid.
697//----------------------------------------------------
698int get_defined_fru_area(sd_bus *bus_type, const uint8_t fruid,
699 std::vector<std::string> &defined_fru_area)
700{
701 // Need this to get respective DBUS objects
702 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
703 sd_bus_message *response = NULL;
704 int rc = 0;
Patrick Williams804077d2016-07-29 15:26:18 -0500705 const char *areas = NULL;
Brad Bishopde6a3792016-07-25 17:09:12 -0400706 char *sys_bus_name = NULL;
Yi Li75c2d462016-04-11 16:57:46 +0800707
708#ifdef __IPMI_DEBUG__
709 printf("Getting fru areas defined in Skeleton for :[%d]\n", fruid);
710#endif
711
Brad Bishopde6a3792016-07-25 17:09:12 -0400712 rc = mapper_get_service(bus_type, sys_object_name, &sys_bus_name);
713 if(rc < 0)
714 {
715 fprintf(stderr, "Failed to get system manager service:[%s]\n",
716 strerror(-rc));
717 goto exit;
718 }
719
Yi Li75c2d462016-04-11 16:57:46 +0800720 // We want to call a method "getFRUArea" on System Bus that is
721 // made available over OpenBmc system services.
722 rc = sd_bus_call_method(bus_type, // On the System Bus
723 sys_bus_name, // Service to contact
724 sys_object_name, // Object path
725 sys_intf_name, // Interface name
726 "getFRUArea", // Method to be called
727 &bus_error, // object to return error
728 &response, // Response message on success
729 "y", // input message (integer)
730 fruid); // Argument
731
732 if(rc < 0)
733 {
734 fprintf(stderr, "Failed to get fru area for fruid:[%d] to dbus: [%s]\n",
735 fruid, bus_error.message);
736 }
737 else
738 {
739 // if several fru area names are defined, the names are combined to
740 // a string seperated by ','
741 rc = sd_bus_message_read(response, "s", &areas);
742 if(rc < 0)
743 {
744 fprintf(stderr, "Failed to parse response message from getFRUArea:[%s]\n",
745 strerror(-rc));
746 }
747 else
748 {
749#ifdef __IPMI_DEBUG__
750 printf("get defined fru area: id: %d, areas: %s\n", fruid, areas);
751#endif
752 std::string area_name;
753 std::stringstream ss(areas);
754 // fru area names string is seperated by ',', parse it into tokens
755 while (std::getline(ss, area_name, ','))
756 {
757 if (!area_name.empty())
758 defined_fru_area.emplace_back(area_name);
759 }
760 }
761 }
762
Brad Bishopde6a3792016-07-25 17:09:12 -0400763exit:
764
765 free(sys_bus_name);
Yi Li75c2d462016-04-11 16:57:46 +0800766 sd_bus_error_free(&bus_error);
767 sd_bus_message_unref(response);
768
769 return rc;
770}
771
772
Vishwa4be4b7a2015-10-31 22:55:50 -0500773///-----------------------------------------------------
774// Accepts the filename and validates per IPMI FRU spec
775//----------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600776int ipmi_validate_fru_area(const uint8_t fruid, const char *fru_file_name,
vishwac93d6d42015-12-16 11:55:16 -0600777 sd_bus *bus_type, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500778{
vishwac93d6d42015-12-16 11:55:16 -0600779 size_t data_len = 0;
780 size_t bytes_read = 0;
781 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500782
vishwac93d6d42015-12-16 11:55:16 -0600783 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
784 // are not used, keeping it here for completeness.
785 fru_area_vec_t fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800786 std::vector<std::string> defined_fru_area;
787
788 // BMC defines fru areas that should be present in Skeleton
789 rc = get_defined_fru_area(bus_type, fruid, defined_fru_area);
790 if(rc < 0)
791 {
792 fprintf(stderr, "ERROR: cannot get defined fru area\n");
793 return rc;
794 }
vishwac93d6d42015-12-16 11:55:16 -0600795 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
796 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
797 {
798 // Create an object and push onto a vector.
799 std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru>
800 (fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru);
801
802 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500803 bool present = access(fru_file_name, F_OK) == 0;
vishwac93d6d42015-12-16 11:55:16 -0600804 fru_area->set_present(present);
805
Yi Li75c2d462016-04-11 16:57:46 +0800806 // Only setup dbus path for areas defined in BMC.
807 // Otherwise Skeleton will report 'not found' error
808 std::string fru_area_name = fru_area->get_name() + std::to_string(fruid);
809 auto iter = std::find(defined_fru_area.begin(), defined_fru_area.end(),
810 fru_area_name);
811 if (iter != defined_fru_area.end())
812 {
813 fru_area->setup_sd_bus_paths();
814 }
vishwac93d6d42015-12-16 11:55:16 -0600815 fru_area_vec.emplace_back(std::move(fru_area));
816 }
817
818 FILE *fru_fp = fopen(fru_file_name,"rb");
819 if(fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500820 {
821 fprintf(stderr, "ERROR: opening:[%s]\n",fru_file_name);
822 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600823 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500824 }
825
vishwac93d6d42015-12-16 11:55:16 -0600826 // Get the size of the file to see if it meets minimum requirement
827 if(fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500828 {
829 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600830 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500831 }
832
vishwac93d6d42015-12-16 11:55:16 -0600833 // Allocate a buffer to hold entire file content
834 data_len = ftell(fru_fp);
835 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500836
vishwac93d6d42015-12-16 11:55:16 -0600837 rewind(fru_fp);
838 bytes_read = fread(fru_data, data_len, 1, fru_fp);
Vishwa4be4b7a2015-10-31 22:55:50 -0500839 if(bytes_read != 1)
840 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500841 fprintf(stderr, "Failed reading fru data. Bytes_read=[%zd]\n",bytes_read);
Vishwa4be4b7a2015-10-31 22:55:50 -0500842 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600843 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500844 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500845
vishwac93d6d42015-12-16 11:55:16 -0600846 // We are done reading.
847 fclose(fru_fp);
848 fru_fp = NULL;
849
850 rc = ipmi_validate_common_hdr(fru_data, data_len);
vishwaf3ca3522015-12-02 10:35:13 -0600851 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500852 {
vishwac93d6d42015-12-16 11:55:16 -0600853 return cleanup_error(fru_fp, fru_area_vec);
854 }
855
856 // Now that we validated the common header, populate various fru sections if we have them here.
857 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
858 if(rc < 0)
859 {
860 fprintf(stderr,"Populating FRU areas failed for:[%d]\n",fruid);
861 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500862 }
863 else
864 {
vishwac93d6d42015-12-16 11:55:16 -0600865 printf("SUCCESS: Populated FRU areas for:[%s]\n",fru_file_name);
Vishwa4be4b7a2015-10-31 22:55:50 -0500866 }
867
vishwac93d6d42015-12-16 11:55:16 -0600868#ifdef __IPMI_DEBUG__
869 for(auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500870 {
vishwac93d6d42015-12-16 11:55:16 -0600871 printf("FRU ID : [%d]\n",(iter)->get_fruid());
872 printf("AREA NAME : [%s]\n",(iter)->get_name());
873 printf("TYPE : [%d]\n",(iter)->get_type());
874 printf("LEN : [%d]\n",(iter)->get_len());
875 printf("BUS NAME : [%s]\n", (iter)->get_bus_name());
876 printf("OBJ PATH : [%s]\n", (iter)->get_obj_path());
877 printf("INTF NAME :[%s]\n", (iter)->get_intf_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500878 }
vishwac93d6d42015-12-16 11:55:16 -0600879#endif
880
881 // If the vector is populated with everything, then go ahead and update the
882 // inventory.
883 if(!(fru_area_vec.empty()))
884 {
885
886#ifdef __IPMI_DEBUG__
887 printf("\n SIZE of vector is : [%d] \n",fru_area_vec.size());
888#endif
889 rc = ipmi_update_inventory(fru_area_vec);
890 if(rc <0)
891 {
892 fprintf(stderr, "Error updating inventory\n");
893 }
894 }
895
896 // we are done with all that we wanted to do. This will do the job of
897 // calling any destructors too.
898 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500899
900 return rc;
901}