blob: 67911487ecc5e84e1080791afdd378e609325305 [file] [log] [blame]
Vishwa4be4b7a2015-10-31 22:55:50 -05001#include <vector>
2#include <stdlib.h>
3#include <dlfcn.h>
4#include <errno.h>
5#include <stdio.h>
Vishwa4be4b7a2015-10-31 22:55:50 -05006#include <systemd/sd-bus.h>
Chris Austenb45c4cb2015-11-01 06:34:56 -06007#include <unistd.h>
vishwa13555bd2015-11-10 12:10:38 -06008#include <host-ipmid/ipmid-api.h>
vishwac93d6d42015-12-16 11:55:16 -06009#include <iostream>
10#include <memory>
11#include <algorithm>
12#include <fstream>
Yi Li75c2d462016-04-11 16:57:46 +080013#include <sstream>
vishwac93d6d42015-12-16 11:55:16 -060014#include "frup.h"
15#include "fru-area.H"
Vishwa4be4b7a2015-10-31 22:55:50 -050016
17// OpenBMC System Manager dbus framework
vishwa13555bd2015-11-10 12:10:38 -060018const char *sys_bus_name = "org.openbmc.managers.System";
19const char *sys_object_name = "/org/openbmc/managers/System";
20const char *sys_intf_name = "org.openbmc.managers.System";
Vishwa4be4b7a2015-10-31 22:55:50 -050021
vishwac93d6d42015-12-16 11:55:16 -060022//----------------------------------------------------------------
23// Constructor
24//----------------------------------------------------------------
25ipmi_fru::ipmi_fru(const uint8_t fruid, const ipmi_fru_area_type type,
26 sd_bus *bus_type, bool bmc_fru)
27{
28 iv_fruid = fruid;
29 iv_type = type;
30 iv_bmc_fru = bmc_fru;
31 iv_bus_type = bus_type;
32 iv_valid = false;
33 iv_data = NULL;
34 iv_present = false;
35
36 if(iv_type == IPMI_FRU_AREA_INTERNAL_USE)
37 {
38 iv_name = "INTERNAL_";
39 }
40 else if(iv_type == IPMI_FRU_AREA_CHASSIS_INFO)
41 {
42 iv_name = "CHASSIS_";
43 }
44 else if(iv_type == IPMI_FRU_AREA_BOARD_INFO)
45 {
46 iv_name = "BOARD_";
47 }
48 else if(iv_type == IPMI_FRU_AREA_PRODUCT_INFO)
49 {
50 iv_name = "PRODUCT_";
51 }
52 else if(iv_type == IPMI_FRU_AREA_MULTI_RECORD)
53 {
54 iv_name = "MULTI_";
55 }
56 else
57 {
58 iv_name = IPMI_FRU_AREA_TYPE_MAX;
59 fprintf(stderr, "ERROR: Invalid Area type :[%d]\n",iv_type);
60 }
61}
62
63//-----------------------------------------------------
64// For a FRU area type, accepts the data and updates
65// area specific data.
66//-----------------------------------------------------
67void ipmi_fru::set_data(const uint8_t *data, const size_t len)
68{
69 iv_len = len;
70 iv_data = new uint8_t[len];
71 memcpy(iv_data, data, len);
72}
73
74//-----------------------------------------------------
75// Sets the dbus parameters
76//-----------------------------------------------------
77void ipmi_fru::update_dbus_paths(const char *bus_name,
78 const char *obj_path, const char *intf_name)
79{
80 iv_bus_name = bus_name;
81 iv_obj_path = obj_path;
82 iv_intf_name = intf_name;
83}
84
85//-------------------
86// Destructor
87//-------------------
88ipmi_fru::~ipmi_fru()
89{
90 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
91 sd_bus_message *response = NULL;
92 int rc = 0;
93
94 if(iv_data != NULL)
95 {
96 delete [] iv_data;
97 iv_data = NULL;
98 }
99
100 // If we have not been successful in doing some updates and we are a BMC
101 // fru, then need to set the fault bits.
102 bool valid_dbus = !(iv_bus_name.empty()) &&
103 !(iv_obj_path.empty()) &&
104 !(iv_intf_name.empty());
105
106 // Based on bmc_fru, success in updating the FRU inventory we need to set
107 // some special bits.
108 if(iv_bmc_fru && valid_dbus)
109 {
110 // Set the Fault bit if we did not successfully process the fru
111 const char *fault_bit = iv_valid ? "False" : "True";
112
113 rc = sd_bus_call_method(iv_bus_type, // On the System Bus
114 iv_bus_name.c_str(), // Service to contact
115 iv_obj_path.c_str(), // Object path
116 iv_intf_name.c_str(), // Interface name
117 "setFault", // Method to be called
118 &bus_error, // object to return error
119 &response, // Response message on success
120 "s", // input message (string)
121 fault_bit); // First argument to setFault
122
123 if(rc <0)
124 {
125 fprintf(stderr,"Failed to set Fault bit, value:[%s] for fruid:[%d], path:[%s]\n",
126 fault_bit, iv_fruid, iv_obj_path.c_str());
127 }
128 else
129 {
130 printf("Fault bit set to :[%s] for fruid:[%d], Path:[%s]\n",
131 fault_bit, iv_fruid,iv_obj_path.c_str());
132 }
133
134 sd_bus_error_free(&bus_error);
135 sd_bus_message_unref(response);
136
137 // Set the Present bits
138 const char *present_bit = iv_present ? "True" : "False";
139
140 rc = sd_bus_call_method(iv_bus_type, // On the System Bus
141 iv_bus_name.c_str(), // Service to contact
142 iv_obj_path.c_str(), // Object path
143 iv_intf_name.c_str(), // Interface name
144 "setPresent", // Method to be called
145 &bus_error, // object to return error
146 &response, // Response message on success
147 "s", // input message (string)
148 present_bit); // First argument to setPresent
149 if(rc < 0)
150 {
151 fprintf(stderr,"Failed to set Present bit for fruid:[%d], path:[%s]\n",
152 iv_fruid, iv_obj_path.c_str());
153 }
154 else
155 {
Yi Li75c2d462016-04-11 16:57:46 +0800156 printf("Present bit set to :[%s] for fruid:[%d], Path[%s]:\n",
157 present_bit, iv_fruid, iv_obj_path.c_str());
vishwac93d6d42015-12-16 11:55:16 -0600158 }
159
160 sd_bus_error_free(&bus_error);
161 sd_bus_message_unref(response);
162 }
163}
164
165// Sets up the sd_bus structures for the given fru type
166int ipmi_fru::setup_sd_bus_paths(void)
167{
168 // Need this to get respective DBUS objects
169 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
170 sd_bus_message *response = NULL;
171 int rc = 0;
172
173 // What we need is BOARD_1, PRODUCT_1, CHASSIS_1 etc..
174 char *inv_bus_name, *inv_obj_path, *inv_intf_name;
175 char fru_area_name[16] = {0};
176 sprintf(fru_area_name,"%s%d",iv_name.c_str(), iv_fruid);
177
178#ifdef __IPMI_DEBUG__
179 printf("Getting sd_bus for :[%s]\n",fru_area_name);
180#endif
181
182 // We want to call a method "getObjectFromId" on System Bus that is
183 // made available over OpenBmc system services.
Yi Li75c2d462016-04-11 16:57:46 +0800184
vishwac93d6d42015-12-16 11:55:16 -0600185 rc = sd_bus_call_method(iv_bus_type, // On the System Bus
186 sys_bus_name, // Service to contact
187 sys_object_name, // Object path
188 sys_intf_name, // Interface name
189 "getObjectFromId", // Method to be called
190 &bus_error, // object to return error
191 &response, // Response message on success
192 "ss", // input message (string,string)
193 "FRU_STR", // First argument to getObjectFromId
194 fru_area_name); // Second Argument
vishwac93d6d42015-12-16 11:55:16 -0600195 if(rc < 0)
196 {
197 fprintf(stderr, "Failed to resolve fruid:[%d] to dbus: [%s]\n", iv_fruid, bus_error.message);
198 }
199 else
200 {
201 // Method getObjectFromId returns 3 parameters and all are strings, namely
202 // bus_name , object_path and interface name for accessing that particular
203 // FRU over Inventory SDBUS manager. 'sss' here mentions that format.
204 rc = sd_bus_message_read(response, "(sss)", &inv_bus_name, &inv_obj_path, &inv_intf_name);
205 if(rc < 0)
206 {
207 fprintf(stderr, "Failed to parse response message:[%s]\n", strerror(-rc));
208 }
209 else
210 {
211 // Update the paths in the area object
212 update_dbus_paths(inv_bus_name, inv_obj_path, inv_intf_name);
213 }
214 }
215
216#ifdef __IPMI_DEBUG__
217 printf("fru_area=[%s], inv_bus_name=[%s], inv_obj_path=[%s], inv_intf_name=[%s]\n",
218 fru_area_name, inv_bus_name, inv_obj_path, inv_intf_name);
219#endif
220
221 sd_bus_error_free(&bus_error);
222 sd_bus_message_unref(response);
223
224 return rc;
225}
226
Vishwa4be4b7a2015-10-31 22:55:50 -0500227//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600228// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600229// and returns the 8 bit checksum
230// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500231//-------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600232unsigned char calculate_crc(const unsigned char *data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500233{
234 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600235 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500236
237 for(byte = 0; byte < len; byte++)
238 {
239 crc += *data++;
240 }
vishwaf3ca3522015-12-02 10:35:13 -0600241
Vishwa4be4b7a2015-10-31 22:55:50 -0500242 return(-crc);
243}
244
245//---------------------------------------------------------------------
246// Accepts a fru area offset in commom hdr and tells which area it is.
247//---------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600248ipmi_fru_area_type get_fru_area_type(uint8_t area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500249{
250 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
251
252 switch(area_offset)
253 {
254 case IPMI_FRU_INTERNAL_OFFSET:
255 type = IPMI_FRU_AREA_INTERNAL_USE;
256 break;
257
258 case IPMI_FRU_CHASSIS_OFFSET:
259 type = IPMI_FRU_AREA_CHASSIS_INFO;
260 break;
261
262 case IPMI_FRU_BOARD_OFFSET:
263 type = IPMI_FRU_AREA_BOARD_INFO;
264 break;
265
266 case IPMI_FRU_PRODUCT_OFFSET:
267 type = IPMI_FRU_AREA_PRODUCT_INFO;
268 break;
269
270 case IPMI_FRU_MULTI_OFFSET:
271 type = IPMI_FRU_AREA_MULTI_RECORD;
272 break;
273
274 default:
275 type = IPMI_FRU_AREA_TYPE_MAX;
276 }
277
278 return type;
279}
280
vishwac93d6d42015-12-16 11:55:16 -0600281///-----------------------------------------------
282// Validates the data for crc and mandatory fields
283///-----------------------------------------------
284int verify_fru_data(const uint8_t *data, const size_t len)
285{
286 uint8_t checksum = 0;
287 int rc = -1;
288
289 // Validate for first byte to always have a value of [1]
290 if(data[0] != IPMI_FRU_HDR_BYTE_ZERO)
291 {
292 fprintf(stderr, "Invalid entry:[%d] in byte-0\n",data[0]);
293 return rc;
294 }
295#ifdef __IPMI_DEBUG__
296 else
297 {
298 printf("SUCCESS: Validated [0x%X] in entry_1 of fru_data\n",data[0]);
299 }
300#endif
301
302 // See if the calculated CRC matches with the embedded one.
303 // CRC to be calculated on all except the last one that is CRC itself.
304 checksum = calculate_crc(data, len - 1);
305 if(checksum != data[len-1])
306 {
307#ifdef __IPMI_DEBUG__
308 fprintf(stderr, "Checksum mismatch."
309 " Calculated:[0x%X], Embedded:[0x%X]\n",
310 checksum, data[len]);
311#endif
312 return rc;
313 }
314#ifdef __IPMI_DEBUG__
315 else
316 {
317 printf("SUCCESS: Checksum matches:[0x%X]\n",checksum);
318 }
319#endif
320
321 return EXIT_SUCCESS;
322}
323
Vishwa4be4b7a2015-10-31 22:55:50 -0500324//------------------------------------------------------------------------
325// Takes FRU data, invokes Parser for each fru record area and updates
326// Inventory
327//------------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600328int ipmi_update_inventory(fru_area_vec_t & area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500329{
vishwac93d6d42015-12-16 11:55:16 -0600330 // Generic error reporter
Vishwa4be4b7a2015-10-31 22:55:50 -0500331 int rc = 0;
vishwaf3ca3522015-12-02 10:35:13 -0600332
Vishwa4be4b7a2015-10-31 22:55:50 -0500333 // Dictionary object to hold Name:Value pair
334 sd_bus_message *fru_dict = NULL;
335
336 // SD Bus error report mechanism.
337 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
338
vishwac93d6d42015-12-16 11:55:16 -0600339 // Response from sd bus calls
Vishwa4be4b7a2015-10-31 22:55:50 -0500340 sd_bus_message *response = NULL;
341
Vishwa4be4b7a2015-10-31 22:55:50 -0500342 // For each FRU area, extract the needed data , get it parsed and update
343 // the Inventory.
344 for(auto& iter : area_vec)
345 {
vishwac93d6d42015-12-16 11:55:16 -0600346 // Start fresh on each.
Vishwa4be4b7a2015-10-31 22:55:50 -0500347 sd_bus_error_free(&bus_error);
348 sd_bus_message_unref(response);
349 sd_bus_message_unref(fru_dict);
vishwaf3ca3522015-12-02 10:35:13 -0600350
Vishwa4be4b7a2015-10-31 22:55:50 -0500351 // Constructor to allow further initializations and customization.
vishwac93d6d42015-12-16 11:55:16 -0600352 rc = sd_bus_message_new_method_call((iter)->get_bus_type(),
Vishwa4be4b7a2015-10-31 22:55:50 -0500353 &fru_dict,
vishwac93d6d42015-12-16 11:55:16 -0600354 (iter)->get_bus_name(),
355 (iter)->get_obj_path(),
356 (iter)->get_intf_name(),
Vishwa4be4b7a2015-10-31 22:55:50 -0500357 "update");
358 if(rc < 0)
359 {
vishwac93d6d42015-12-16 11:55:16 -0600360 fprintf(stderr,"ERROR: creating a update method call for bus_name:[%s]\n",
361 (iter)->get_bus_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500362 break;
363 }
364
365 // A Dictionary ({}) having (string, variant)
366 rc = sd_bus_message_open_container(fru_dict, 'a', "{sv}");
367 if(rc < 0)
368 {
369 fprintf(stderr,"ERROR:[%d] creating a dict container:\n",errno);
370 break;
371 }
372
373 // Fill the container with information
vishwac93d6d42015-12-16 11:55:16 -0600374 rc = parse_fru_area((iter)->get_type(), (void *)(iter)->get_data(), (iter)->get_len(), fru_dict);
Vishwa4be4b7a2015-10-31 22:55:50 -0500375 if(rc < 0)
376 {
377 fprintf(stderr,"ERROR parsing FRU records\n");
378 break;
379 }
380
381 sd_bus_message_close_container(fru_dict);
382
383 // Now, Make the actual call to update the FRU inventory database with the
384 // dictionary given by FRU Parser. There is no response message expected for
385 // this.
vishwac93d6d42015-12-16 11:55:16 -0600386 rc = sd_bus_call((iter)->get_bus_type(), // On the System Bus
387 fru_dict, // With the Name:value dictionary array
388 0, //
389 &bus_error, // Object to return error.
390 &response); // Response message if any.
Vishwa4be4b7a2015-10-31 22:55:50 -0500391
392 if(rc < 0)
393 {
394 fprintf(stderr, "ERROR:[%s] updating FRU inventory for ID:[0x%X]\n",
vishwac93d6d42015-12-16 11:55:16 -0600395 bus_error.message, (iter)->get_fruid());
396 break;
Vishwa4be4b7a2015-10-31 22:55:50 -0500397 }
vishwac93d6d42015-12-16 11:55:16 -0600398 else if((iter)->is_bmc_fru())
vishwaf3ca3522015-12-02 10:35:13 -0600399 {
vishwac93d6d42015-12-16 11:55:16 -0600400 // For FRUs that are accessible by HostBoot, host boot does all of
401 // these.
402 printf("SUCCESS: Updated:[%s_%d] successfully. Setting Valid bit\n",
403 (iter)->get_name(), (iter)->get_fruid());
vishwaf3ca3522015-12-02 10:35:13 -0600404
vishwac93d6d42015-12-16 11:55:16 -0600405 (iter)->set_valid(true);
vishwaf3ca3522015-12-02 10:35:13 -0600406 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500407 else
408 {
vishwac93d6d42015-12-16 11:55:16 -0600409 printf("SUCCESS: Updated:[%s_%d] successfully\n",
410 (iter)->get_name(), (iter)->get_fruid());
Vishwa4be4b7a2015-10-31 22:55:50 -0500411 }
412 } // END walking the vector of areas and updating
413
414 sd_bus_error_free(&bus_error);
415 sd_bus_message_unref(response);
416 sd_bus_message_unref(fru_dict);
Vishwa4be4b7a2015-10-31 22:55:50 -0500417
418 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{
vishwac93d6d42015-12-16 11:55:16 -0600426 // Filter the ones that do not have dbus reference.
427 if((strlen((fru_area)->get_bus_name()) == 0) ||
428 (strlen((fru_area)->get_obj_path()) == 0) ||
429 (strlen((fru_area)->get_intf_name()) == 0))
430 {
431 return true;
432 }
433 return false;
434}
Vishwa4be4b7a2015-10-31 22:55:50 -0500435
vishwac93d6d42015-12-16 11:55:16 -0600436///----------------------------------------------------------------------------------
437// Populates various FRU areas
438// @prereq : This must be called only after validating common header.
439///----------------------------------------------------------------------------------
440int ipmi_populate_fru_areas(uint8_t *fru_data, const size_t data_len,
441 fru_area_vec_t & fru_area_vec)
442{
vishwa13555bd2015-11-10 12:10:38 -0600443 size_t area_offset = 0;
vishwac93d6d42015-12-16 11:55:16 -0600444 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500445
vishwac93d6d42015-12-16 11:55:16 -0600446 // Now walk the common header and see if the file size has atleast the last
447 // offset mentioned by the common_hdr. If the file size is less than the
448 // offset of any if the fru areas mentioned in the common header, then we do
449 // not have a complete file.
450 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
451 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
452 {
Yi Li75c2d462016-04-11 16:57:46 +0800453 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600454 // Actual offset in the payload is the offset mentioned in common header
455 // multipled by 8. Common header is always the first 8 bytes.
456 area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
457 if(area_offset && (data_len < (area_offset + 2)))
458 {
459 // Our file size is less than what it needs to be. +2 because we are
460 // using area len that is at 2 byte off area_offset
461 fprintf(stderr, "fru file is incomplete. Size:[%d]\n",data_len);
462 return rc;
463 }
464 else if(area_offset)
465 {
466 // Read 2 bytes to know the actual size of area.
467 uint8_t area_hdr[2] = {0};
468 memcpy(area_hdr, &((uint8_t *)fru_data)[area_offset], sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500469
vishwac93d6d42015-12-16 11:55:16 -0600470 // Size of this area will be the 2nd byte in the fru area header.
471 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
472 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500473
vishwac93d6d42015-12-16 11:55:16 -0600474 printf("fru data size:[%d], area offset:[%d], area_size:[%d]\n",
475 data_len, area_offset, area_len);
Vishwa4be4b7a2015-10-31 22:55:50 -0500476
vishwac93d6d42015-12-16 11:55:16 -0600477 // See if we really have that much buffer. We have area offset amd
478 // from there, the actual len.
479 if(data_len < (area_len + area_offset))
480 {
481 fprintf(stderr, "Incomplete Fru file.. Size:[%d]\n",data_len);
482 return rc;
483 }
484
485 // Save off the data.
486 memcpy(area_data, &((uint8_t *)fru_data)[area_offset], area_len);
487
488 // Validate the crc
489 rc = verify_fru_data(area_data, area_len);
490 if(rc < 0)
491 {
492 fprintf(stderr, "Error validating fru area. offset:[%d]\n",area_offset);
493 return rc;
494 }
495 else
496 {
497 printf("Successfully verified area checksum. offset:[%d]\n",area_offset);
498 }
499
500 // We already have a vector that is passed to us containing all
501 // of the fields populated. Update the data portion now.
502 for(auto& iter : fru_area_vec)
503 {
504 if((iter)->get_type() == get_fru_area_type(fru_entry))
505 {
506 (iter)->set_data(area_data, area_len);
507 }
508 }
509 } // If we have fru data present
510 } // Walk common_hdr
511
512 // Not all the fields will be populated in a fru data. Mostly all cases will
513 // not have more than 2 or 3.
514 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
515 remove_invalid_area), fru_area_vec.end());
516
517 return EXIT_SUCCESS;
518}
519
520///---------------------------------------------------------
521// Validates the fru data per ipmi common header constructs.
522// Returns with updated common_hdr and also file_size
523//----------------------------------------------------------
524int ipmi_validate_common_hdr(const uint8_t *fru_data, const size_t data_len)
525{
526 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500527
528 uint8_t common_hdr[sizeof(struct common_header)] = {0};
vishwac93d6d42015-12-16 11:55:16 -0600529 if(data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500530 {
vishwac93d6d42015-12-16 11:55:16 -0600531 memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500532 }
533 else
534 {
vishwac93d6d42015-12-16 11:55:16 -0600535 fprintf(stderr, "Incomplete fru data file. Size:[%d]\n", data_len);
536 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500537 }
538
vishwac93d6d42015-12-16 11:55:16 -0600539 // Verify the crc and size
540 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
541 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500542 {
vishwac93d6d42015-12-16 11:55:16 -0600543 fprintf(stderr, "Failed to validate common header\n");
544 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500545 }
546
vishwac93d6d42015-12-16 11:55:16 -0600547 return EXIT_SUCCESS;
548}
Vishwa4be4b7a2015-10-31 22:55:50 -0500549
vishwac93d6d42015-12-16 11:55:16 -0600550//------------------------------------------------------------
551// Cleanup routine
552//------------------------------------------------------------
553int cleanup_error(FILE *fru_fp, fru_area_vec_t & fru_area_vec)
554{
555 if(fru_fp != NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500556 {
vishwac93d6d42015-12-16 11:55:16 -0600557 fclose(fru_fp);
558 fru_fp = NULL;
559 }
vishwaf3ca3522015-12-02 10:35:13 -0600560
Vishwa4be4b7a2015-10-31 22:55:50 -0500561 if(!(fru_area_vec.empty()))
562 {
vishwac93d6d42015-12-16 11:55:16 -0600563 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500564 }
vishwaf3ca3522015-12-02 10:35:13 -0600565
vishwac93d6d42015-12-16 11:55:16 -0600566 return -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500567}
568
Yi Li75c2d462016-04-11 16:57:46 +0800569
570///-----------------------------------------------------
571// Get the fru area names defined in BMC for a given @fruid.
572//----------------------------------------------------
573int get_defined_fru_area(sd_bus *bus_type, const uint8_t fruid,
574 std::vector<std::string> &defined_fru_area)
575{
576 // Need this to get respective DBUS objects
577 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
578 sd_bus_message *response = NULL;
579 int rc = 0;
580 char *areas = NULL;
581
582#ifdef __IPMI_DEBUG__
583 printf("Getting fru areas defined in Skeleton for :[%d]\n", fruid);
584#endif
585
586 // We want to call a method "getFRUArea" on System Bus that is
587 // made available over OpenBmc system services.
588 rc = sd_bus_call_method(bus_type, // On the System Bus
589 sys_bus_name, // Service to contact
590 sys_object_name, // Object path
591 sys_intf_name, // Interface name
592 "getFRUArea", // Method to be called
593 &bus_error, // object to return error
594 &response, // Response message on success
595 "y", // input message (integer)
596 fruid); // Argument
597
598 if(rc < 0)
599 {
600 fprintf(stderr, "Failed to get fru area for fruid:[%d] to dbus: [%s]\n",
601 fruid, bus_error.message);
602 }
603 else
604 {
605 // if several fru area names are defined, the names are combined to
606 // a string seperated by ','
607 rc = sd_bus_message_read(response, "s", &areas);
608 if(rc < 0)
609 {
610 fprintf(stderr, "Failed to parse response message from getFRUArea:[%s]\n",
611 strerror(-rc));
612 }
613 else
614 {
615#ifdef __IPMI_DEBUG__
616 printf("get defined fru area: id: %d, areas: %s\n", fruid, areas);
617#endif
618 std::string area_name;
619 std::stringstream ss(areas);
620 // fru area names string is seperated by ',', parse it into tokens
621 while (std::getline(ss, area_name, ','))
622 {
623 if (!area_name.empty())
624 defined_fru_area.emplace_back(area_name);
625 }
626 }
627 }
628
629 sd_bus_error_free(&bus_error);
630 sd_bus_message_unref(response);
631
632 return rc;
633}
634
635
Vishwa4be4b7a2015-10-31 22:55:50 -0500636///-----------------------------------------------------
637// Accepts the filename and validates per IPMI FRU spec
638//----------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600639int ipmi_validate_fru_area(const uint8_t fruid, const char *fru_file_name,
vishwac93d6d42015-12-16 11:55:16 -0600640 sd_bus *bus_type, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500641{
vishwac93d6d42015-12-16 11:55:16 -0600642 size_t data_len = 0;
643 size_t bytes_read = 0;
644 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500645
vishwac93d6d42015-12-16 11:55:16 -0600646 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
647 // are not used, keeping it here for completeness.
648 fru_area_vec_t fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800649 std::vector<std::string> defined_fru_area;
650
651 // BMC defines fru areas that should be present in Skeleton
652 rc = get_defined_fru_area(bus_type, fruid, defined_fru_area);
653 if(rc < 0)
654 {
655 fprintf(stderr, "ERROR: cannot get defined fru area\n");
656 return rc;
657 }
vishwac93d6d42015-12-16 11:55:16 -0600658 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
659 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
660 {
661 // Create an object and push onto a vector.
662 std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru>
663 (fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru);
664
665 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500666 bool present = access(fru_file_name, F_OK) == 0;
vishwac93d6d42015-12-16 11:55:16 -0600667 fru_area->set_present(present);
668
Yi Li75c2d462016-04-11 16:57:46 +0800669 // Only setup dbus path for areas defined in BMC.
670 // Otherwise Skeleton will report 'not found' error
671 std::string fru_area_name = fru_area->get_name() + std::to_string(fruid);
672 auto iter = std::find(defined_fru_area.begin(), defined_fru_area.end(),
673 fru_area_name);
674 if (iter != defined_fru_area.end())
675 {
676 fru_area->setup_sd_bus_paths();
677 }
vishwac93d6d42015-12-16 11:55:16 -0600678 fru_area_vec.emplace_back(std::move(fru_area));
679 }
680
681 FILE *fru_fp = fopen(fru_file_name,"rb");
682 if(fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500683 {
684 fprintf(stderr, "ERROR: opening:[%s]\n",fru_file_name);
685 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600686 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500687 }
688
vishwac93d6d42015-12-16 11:55:16 -0600689 // Get the size of the file to see if it meets minimum requirement
690 if(fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500691 {
692 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600693 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500694 }
695
vishwac93d6d42015-12-16 11:55:16 -0600696 // Allocate a buffer to hold entire file content
697 data_len = ftell(fru_fp);
698 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500699
vishwac93d6d42015-12-16 11:55:16 -0600700 rewind(fru_fp);
701 bytes_read = fread(fru_data, data_len, 1, fru_fp);
Vishwa4be4b7a2015-10-31 22:55:50 -0500702 if(bytes_read != 1)
703 {
vishwac93d6d42015-12-16 11:55:16 -0600704 fprintf(stderr, "Failed reading fru data. Bytes_read=[%d]\n",bytes_read);
Vishwa4be4b7a2015-10-31 22:55:50 -0500705 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600706 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500707 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500708
vishwac93d6d42015-12-16 11:55:16 -0600709 // We are done reading.
710 fclose(fru_fp);
711 fru_fp = NULL;
712
713 rc = ipmi_validate_common_hdr(fru_data, data_len);
vishwaf3ca3522015-12-02 10:35:13 -0600714 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500715 {
vishwac93d6d42015-12-16 11:55:16 -0600716 return cleanup_error(fru_fp, fru_area_vec);
717 }
718
719 // Now that we validated the common header, populate various fru sections if we have them here.
720 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
721 if(rc < 0)
722 {
723 fprintf(stderr,"Populating FRU areas failed for:[%d]\n",fruid);
724 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500725 }
726 else
727 {
vishwac93d6d42015-12-16 11:55:16 -0600728 printf("SUCCESS: Populated FRU areas for:[%s]\n",fru_file_name);
Vishwa4be4b7a2015-10-31 22:55:50 -0500729 }
730
vishwac93d6d42015-12-16 11:55:16 -0600731#ifdef __IPMI_DEBUG__
732 for(auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500733 {
vishwac93d6d42015-12-16 11:55:16 -0600734 printf("FRU ID : [%d]\n",(iter)->get_fruid());
735 printf("AREA NAME : [%s]\n",(iter)->get_name());
736 printf("TYPE : [%d]\n",(iter)->get_type());
737 printf("LEN : [%d]\n",(iter)->get_len());
738 printf("BUS NAME : [%s]\n", (iter)->get_bus_name());
739 printf("OBJ PATH : [%s]\n", (iter)->get_obj_path());
740 printf("INTF NAME :[%s]\n", (iter)->get_intf_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500741 }
vishwac93d6d42015-12-16 11:55:16 -0600742#endif
743
744 // If the vector is populated with everything, then go ahead and update the
745 // inventory.
746 if(!(fru_area_vec.empty()))
747 {
748
749#ifdef __IPMI_DEBUG__
750 printf("\n SIZE of vector is : [%d] \n",fru_area_vec.size());
751#endif
752 rc = ipmi_update_inventory(fru_area_vec);
753 if(rc <0)
754 {
755 fprintf(stderr, "Error updating inventory\n");
756 }
757 }
758
759 // we are done with all that we wanted to do. This will do the job of
760 // calling any destructors too.
761 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500762
763 return rc;
764}