blob: 3619966a47840f6115d0e631ef99f1c70d48a1b7 [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>
Brad Bishopde6a3792016-07-25 17:09:12 -040014#include <mapper.h>
vishwac93d6d42015-12-16 11:55:16 -060015#include "frup.h"
Matthew Barth155c34f2016-10-18 14:33:17 -050016#include "fru-area.hpp"
Vishwa4be4b7a2015-10-31 22:55:50 -050017
18// OpenBMC System Manager dbus framework
vishwa13555bd2015-11-10 12:10:38 -060019const 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..
Patrick Williams867da972016-08-17 17:40:05 -0500174 char *inv_bus_name = NULL;
175 const char *inv_obj_path = NULL,
Patrick Williams804077d2016-07-29 15:26:18 -0500176 *inv_intf_name = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600177 char fru_area_name[16] = {0};
Brad Bishopde6a3792016-07-25 17:09:12 -0400178 char *sys_bus_name = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600179 sprintf(fru_area_name,"%s%d",iv_name.c_str(), iv_fruid);
180
181#ifdef __IPMI_DEBUG__
182 printf("Getting sd_bus for :[%s]\n",fru_area_name);
183#endif
184
Brad Bishopde6a3792016-07-25 17:09:12 -0400185 rc = mapper_get_service(iv_bus_type, sys_object_name, &sys_bus_name);
186 if(rc < 0)
187 {
188 fprintf(stderr, "Failed to get system manager service:[%s]\n",
189 strerror(-rc));
190 goto exit;
191 }
192
vishwac93d6d42015-12-16 11:55:16 -0600193 // We want to call a method "getObjectFromId" on System Bus that is
194 // made available over OpenBmc system services.
Yi Li75c2d462016-04-11 16:57:46 +0800195
vishwac93d6d42015-12-16 11:55:16 -0600196 rc = sd_bus_call_method(iv_bus_type, // On the System Bus
197 sys_bus_name, // Service to contact
198 sys_object_name, // Object path
199 sys_intf_name, // Interface name
200 "getObjectFromId", // Method to be called
201 &bus_error, // object to return error
202 &response, // Response message on success
203 "ss", // input message (string,string)
204 "FRU_STR", // First argument to getObjectFromId
205 fru_area_name); // Second Argument
vishwac93d6d42015-12-16 11:55:16 -0600206 if(rc < 0)
207 {
208 fprintf(stderr, "Failed to resolve fruid:[%d] to dbus: [%s]\n", iv_fruid, bus_error.message);
209 }
210 else
211 {
Brad Bishopde6a3792016-07-25 17:09:12 -0400212 // Method getObjectFromId returns 2 parameters and all are strings, namely
213 // object_path and interface name for accessing that particular
214 // FRU over Inventory SDBUS manager. 'ss' here mentions that format.
215 rc = sd_bus_message_read(response, "(ss)", &inv_obj_path, &inv_intf_name);
vishwac93d6d42015-12-16 11:55:16 -0600216 if(rc < 0)
217 {
218 fprintf(stderr, "Failed to parse response message:[%s]\n", strerror(-rc));
219 }
220 else
221 {
Brad Bishopde6a3792016-07-25 17:09:12 -0400222 rc = mapper_get_service(iv_bus_type, inv_obj_path, &inv_bus_name);
223 if(rc < 0)
224 {
225 fprintf(stderr, "Failed to get inventory item service:[%s]\n",
226 strerror(-rc));
227 goto exit;
228 }
vishwac93d6d42015-12-16 11:55:16 -0600229 // Update the paths in the area object
230 update_dbus_paths(inv_bus_name, inv_obj_path, inv_intf_name);
231 }
232 }
233
Brad Bishopde6a3792016-07-25 17:09:12 -0400234exit:
vishwac93d6d42015-12-16 11:55:16 -0600235#ifdef __IPMI_DEBUG__
236 printf("fru_area=[%s], inv_bus_name=[%s], inv_obj_path=[%s], inv_intf_name=[%s]\n",
237 fru_area_name, inv_bus_name, inv_obj_path, inv_intf_name);
238#endif
239
Brad Bishopde6a3792016-07-25 17:09:12 -0400240 free(sys_bus_name);
Patrick Williams867da972016-08-17 17:40:05 -0500241 free(inv_bus_name);
vishwac93d6d42015-12-16 11:55:16 -0600242 sd_bus_error_free(&bus_error);
243 sd_bus_message_unref(response);
244
245 return rc;
246}
247
Vishwa4be4b7a2015-10-31 22:55:50 -0500248//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600249// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600250// and returns the 8 bit checksum
251// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500252//-------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600253unsigned char calculate_crc(const unsigned char *data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500254{
255 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600256 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500257
258 for(byte = 0; byte < len; byte++)
259 {
260 crc += *data++;
261 }
vishwaf3ca3522015-12-02 10:35:13 -0600262
Vishwa4be4b7a2015-10-31 22:55:50 -0500263 return(-crc);
264}
265
266//---------------------------------------------------------------------
267// Accepts a fru area offset in commom hdr and tells which area it is.
268//---------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600269ipmi_fru_area_type get_fru_area_type(uint8_t area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500270{
271 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
272
273 switch(area_offset)
274 {
275 case IPMI_FRU_INTERNAL_OFFSET:
276 type = IPMI_FRU_AREA_INTERNAL_USE;
277 break;
278
279 case IPMI_FRU_CHASSIS_OFFSET:
280 type = IPMI_FRU_AREA_CHASSIS_INFO;
281 break;
282
283 case IPMI_FRU_BOARD_OFFSET:
284 type = IPMI_FRU_AREA_BOARD_INFO;
285 break;
286
287 case IPMI_FRU_PRODUCT_OFFSET:
288 type = IPMI_FRU_AREA_PRODUCT_INFO;
289 break;
290
291 case IPMI_FRU_MULTI_OFFSET:
292 type = IPMI_FRU_AREA_MULTI_RECORD;
293 break;
294
295 default:
296 type = IPMI_FRU_AREA_TYPE_MAX;
297 }
298
299 return type;
300}
301
vishwac93d6d42015-12-16 11:55:16 -0600302///-----------------------------------------------
303// Validates the data for crc and mandatory fields
304///-----------------------------------------------
305int verify_fru_data(const uint8_t *data, const size_t len)
306{
307 uint8_t checksum = 0;
308 int rc = -1;
309
310 // Validate for first byte to always have a value of [1]
311 if(data[0] != IPMI_FRU_HDR_BYTE_ZERO)
312 {
313 fprintf(stderr, "Invalid entry:[%d] in byte-0\n",data[0]);
314 return rc;
315 }
316#ifdef __IPMI_DEBUG__
317 else
318 {
319 printf("SUCCESS: Validated [0x%X] in entry_1 of fru_data\n",data[0]);
320 }
321#endif
322
323 // See if the calculated CRC matches with the embedded one.
324 // CRC to be calculated on all except the last one that is CRC itself.
325 checksum = calculate_crc(data, len - 1);
326 if(checksum != data[len-1])
327 {
328#ifdef __IPMI_DEBUG__
329 fprintf(stderr, "Checksum mismatch."
330 " Calculated:[0x%X], Embedded:[0x%X]\n",
331 checksum, data[len]);
332#endif
333 return rc;
334 }
335#ifdef __IPMI_DEBUG__
336 else
337 {
338 printf("SUCCESS: Checksum matches:[0x%X]\n",checksum);
339 }
340#endif
341
342 return EXIT_SUCCESS;
343}
344
Vishwa4be4b7a2015-10-31 22:55:50 -0500345//------------------------------------------------------------------------
346// Takes FRU data, invokes Parser for each fru record area and updates
347// Inventory
348//------------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600349int ipmi_update_inventory(fru_area_vec_t & area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500350{
vishwac93d6d42015-12-16 11:55:16 -0600351 // Generic error reporter
Vishwa4be4b7a2015-10-31 22:55:50 -0500352 int rc = 0;
vishwaf3ca3522015-12-02 10:35:13 -0600353
Vishwa4be4b7a2015-10-31 22:55:50 -0500354 // Dictionary object to hold Name:Value pair
355 sd_bus_message *fru_dict = NULL;
356
357 // SD Bus error report mechanism.
358 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
359
vishwac93d6d42015-12-16 11:55:16 -0600360 // Response from sd bus calls
Vishwa4be4b7a2015-10-31 22:55:50 -0500361 sd_bus_message *response = NULL;
362
Vishwa4be4b7a2015-10-31 22:55:50 -0500363 // For each FRU area, extract the needed data , get it parsed and update
364 // the Inventory.
365 for(auto& iter : area_vec)
366 {
vishwac93d6d42015-12-16 11:55:16 -0600367 // Start fresh on each.
Vishwa4be4b7a2015-10-31 22:55:50 -0500368 sd_bus_error_free(&bus_error);
369 sd_bus_message_unref(response);
370 sd_bus_message_unref(fru_dict);
vishwaf3ca3522015-12-02 10:35:13 -0600371
Vishwa4be4b7a2015-10-31 22:55:50 -0500372 // Constructor to allow further initializations and customization.
vishwac93d6d42015-12-16 11:55:16 -0600373 rc = sd_bus_message_new_method_call((iter)->get_bus_type(),
Vishwa4be4b7a2015-10-31 22:55:50 -0500374 &fru_dict,
vishwac93d6d42015-12-16 11:55:16 -0600375 (iter)->get_bus_name(),
376 (iter)->get_obj_path(),
377 (iter)->get_intf_name(),
Vishwa4be4b7a2015-10-31 22:55:50 -0500378 "update");
379 if(rc < 0)
380 {
vishwac93d6d42015-12-16 11:55:16 -0600381 fprintf(stderr,"ERROR: creating a update method call for bus_name:[%s]\n",
382 (iter)->get_bus_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500383 break;
384 }
385
386 // A Dictionary ({}) having (string, variant)
387 rc = sd_bus_message_open_container(fru_dict, 'a', "{sv}");
388 if(rc < 0)
389 {
390 fprintf(stderr,"ERROR:[%d] creating a dict container:\n",errno);
391 break;
392 }
393
394 // Fill the container with information
vishwac93d6d42015-12-16 11:55:16 -0600395 rc = parse_fru_area((iter)->get_type(), (void *)(iter)->get_data(), (iter)->get_len(), fru_dict);
Vishwa4be4b7a2015-10-31 22:55:50 -0500396 if(rc < 0)
397 {
398 fprintf(stderr,"ERROR parsing FRU records\n");
399 break;
400 }
401
402 sd_bus_message_close_container(fru_dict);
403
404 // Now, Make the actual call to update the FRU inventory database with the
405 // dictionary given by FRU Parser. There is no response message expected for
406 // this.
vishwac93d6d42015-12-16 11:55:16 -0600407 rc = sd_bus_call((iter)->get_bus_type(), // On the System Bus
408 fru_dict, // With the Name:value dictionary array
409 0, //
410 &bus_error, // Object to return error.
411 &response); // Response message if any.
Vishwa4be4b7a2015-10-31 22:55:50 -0500412
413 if(rc < 0)
414 {
415 fprintf(stderr, "ERROR:[%s] updating FRU inventory for ID:[0x%X]\n",
vishwac93d6d42015-12-16 11:55:16 -0600416 bus_error.message, (iter)->get_fruid());
417 break;
Vishwa4be4b7a2015-10-31 22:55:50 -0500418 }
vishwac93d6d42015-12-16 11:55:16 -0600419 else if((iter)->is_bmc_fru())
vishwaf3ca3522015-12-02 10:35:13 -0600420 {
vishwac93d6d42015-12-16 11:55:16 -0600421 // For FRUs that are accessible by HostBoot, host boot does all of
422 // these.
423 printf("SUCCESS: Updated:[%s_%d] successfully. Setting Valid bit\n",
424 (iter)->get_name(), (iter)->get_fruid());
vishwaf3ca3522015-12-02 10:35:13 -0600425
vishwac93d6d42015-12-16 11:55:16 -0600426 (iter)->set_valid(true);
vishwaf3ca3522015-12-02 10:35:13 -0600427 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500428 else
429 {
vishwac93d6d42015-12-16 11:55:16 -0600430 printf("SUCCESS: Updated:[%s_%d] successfully\n",
431 (iter)->get_name(), (iter)->get_fruid());
Vishwa4be4b7a2015-10-31 22:55:50 -0500432 }
433 } // END walking the vector of areas and updating
434
435 sd_bus_error_free(&bus_error);
436 sd_bus_message_unref(response);
437 sd_bus_message_unref(fru_dict);
Vishwa4be4b7a2015-10-31 22:55:50 -0500438
439 return rc;
440}
441
vishwac93d6d42015-12-16 11:55:16 -0600442///----------------------------------------------------
443// Checks if a particular fru area is populated or not
444///----------------------------------------------------
445bool remove_invalid_area(const std::unique_ptr<ipmi_fru> &fru_area)
Vishwa4be4b7a2015-10-31 22:55:50 -0500446{
vishwac93d6d42015-12-16 11:55:16 -0600447 // Filter the ones that do not have dbus reference.
448 if((strlen((fru_area)->get_bus_name()) == 0) ||
449 (strlen((fru_area)->get_obj_path()) == 0) ||
450 (strlen((fru_area)->get_intf_name()) == 0))
451 {
452 return true;
453 }
454 return false;
455}
Vishwa4be4b7a2015-10-31 22:55:50 -0500456
vishwac93d6d42015-12-16 11:55:16 -0600457///----------------------------------------------------------------------------------
458// Populates various FRU areas
459// @prereq : This must be called only after validating common header.
460///----------------------------------------------------------------------------------
461int ipmi_populate_fru_areas(uint8_t *fru_data, const size_t data_len,
462 fru_area_vec_t & fru_area_vec)
463{
vishwa13555bd2015-11-10 12:10:38 -0600464 size_t area_offset = 0;
vishwac93d6d42015-12-16 11:55:16 -0600465 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500466
vishwac93d6d42015-12-16 11:55:16 -0600467 // Now walk the common header and see if the file size has atleast the last
468 // offset mentioned by the common_hdr. If the file size is less than the
469 // offset of any if the fru areas mentioned in the common header, then we do
470 // not have a complete file.
471 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
472 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
473 {
Yi Li75c2d462016-04-11 16:57:46 +0800474 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600475 // Actual offset in the payload is the offset mentioned in common header
476 // multipled by 8. Common header is always the first 8 bytes.
477 area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
478 if(area_offset && (data_len < (area_offset + 2)))
479 {
480 // Our file size is less than what it needs to be. +2 because we are
481 // using area len that is at 2 byte off area_offset
Patrick Williams3365ec82016-08-17 17:45:18 -0500482 fprintf(stderr, "fru file is incomplete. Size:[%zd]\n",data_len);
vishwac93d6d42015-12-16 11:55:16 -0600483 return rc;
484 }
485 else if(area_offset)
486 {
487 // Read 2 bytes to know the actual size of area.
488 uint8_t area_hdr[2] = {0};
489 memcpy(area_hdr, &((uint8_t *)fru_data)[area_offset], sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500490
vishwac93d6d42015-12-16 11:55:16 -0600491 // Size of this area will be the 2nd byte in the fru area header.
492 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
493 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500494
Patrick Williams3365ec82016-08-17 17:45:18 -0500495 printf("fru data size:[%zd], area offset:[%zd], area_size:[%zd]\n",
vishwac93d6d42015-12-16 11:55:16 -0600496 data_len, area_offset, area_len);
Vishwa4be4b7a2015-10-31 22:55:50 -0500497
vishwac93d6d42015-12-16 11:55:16 -0600498 // See if we really have that much buffer. We have area offset amd
499 // from there, the actual len.
500 if(data_len < (area_len + area_offset))
501 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500502 fprintf(stderr, "Incomplete Fru file.. Size:[%zd]\n",data_len);
vishwac93d6d42015-12-16 11:55:16 -0600503 return rc;
504 }
505
506 // Save off the data.
507 memcpy(area_data, &((uint8_t *)fru_data)[area_offset], area_len);
508
509 // Validate the crc
510 rc = verify_fru_data(area_data, area_len);
511 if(rc < 0)
512 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500513 fprintf(stderr, "Error validating fru area. offset:[%zd]\n",area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600514 return rc;
515 }
516 else
517 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500518 printf("Successfully verified area checksum. offset:[%zd]\n",area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600519 }
520
521 // We already have a vector that is passed to us containing all
522 // of the fields populated. Update the data portion now.
523 for(auto& iter : fru_area_vec)
524 {
525 if((iter)->get_type() == get_fru_area_type(fru_entry))
526 {
527 (iter)->set_data(area_data, area_len);
528 }
529 }
530 } // If we have fru data present
531 } // Walk common_hdr
532
533 // Not all the fields will be populated in a fru data. Mostly all cases will
534 // not have more than 2 or 3.
535 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
536 remove_invalid_area), fru_area_vec.end());
537
538 return EXIT_SUCCESS;
539}
540
541///---------------------------------------------------------
542// Validates the fru data per ipmi common header constructs.
543// Returns with updated common_hdr and also file_size
544//----------------------------------------------------------
545int ipmi_validate_common_hdr(const uint8_t *fru_data, const size_t data_len)
546{
547 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500548
549 uint8_t common_hdr[sizeof(struct common_header)] = {0};
vishwac93d6d42015-12-16 11:55:16 -0600550 if(data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500551 {
vishwac93d6d42015-12-16 11:55:16 -0600552 memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500553 }
554 else
555 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500556 fprintf(stderr, "Incomplete fru data file. Size:[%zd]\n", data_len);
vishwac93d6d42015-12-16 11:55:16 -0600557 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500558 }
559
vishwac93d6d42015-12-16 11:55:16 -0600560 // Verify the crc and size
561 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
562 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500563 {
vishwac93d6d42015-12-16 11:55:16 -0600564 fprintf(stderr, "Failed to validate common header\n");
565 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500566 }
567
vishwac93d6d42015-12-16 11:55:16 -0600568 return EXIT_SUCCESS;
569}
Vishwa4be4b7a2015-10-31 22:55:50 -0500570
vishwac93d6d42015-12-16 11:55:16 -0600571//------------------------------------------------------------
572// Cleanup routine
573//------------------------------------------------------------
574int cleanup_error(FILE *fru_fp, fru_area_vec_t & fru_area_vec)
575{
576 if(fru_fp != NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500577 {
vishwac93d6d42015-12-16 11:55:16 -0600578 fclose(fru_fp);
579 fru_fp = NULL;
580 }
vishwaf3ca3522015-12-02 10:35:13 -0600581
Vishwa4be4b7a2015-10-31 22:55:50 -0500582 if(!(fru_area_vec.empty()))
583 {
vishwac93d6d42015-12-16 11:55:16 -0600584 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500585 }
vishwaf3ca3522015-12-02 10:35:13 -0600586
vishwac93d6d42015-12-16 11:55:16 -0600587 return -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500588}
589
Yi Li75c2d462016-04-11 16:57:46 +0800590
591///-----------------------------------------------------
592// Get the fru area names defined in BMC for a given @fruid.
593//----------------------------------------------------
594int get_defined_fru_area(sd_bus *bus_type, const uint8_t fruid,
595 std::vector<std::string> &defined_fru_area)
596{
597 // Need this to get respective DBUS objects
598 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
599 sd_bus_message *response = NULL;
600 int rc = 0;
Patrick Williams804077d2016-07-29 15:26:18 -0500601 const char *areas = NULL;
Brad Bishopde6a3792016-07-25 17:09:12 -0400602 char *sys_bus_name = NULL;
Yi Li75c2d462016-04-11 16:57:46 +0800603
604#ifdef __IPMI_DEBUG__
605 printf("Getting fru areas defined in Skeleton for :[%d]\n", fruid);
606#endif
607
Brad Bishopde6a3792016-07-25 17:09:12 -0400608 rc = mapper_get_service(bus_type, sys_object_name, &sys_bus_name);
609 if(rc < 0)
610 {
611 fprintf(stderr, "Failed to get system manager service:[%s]\n",
612 strerror(-rc));
613 goto exit;
614 }
615
Yi Li75c2d462016-04-11 16:57:46 +0800616 // We want to call a method "getFRUArea" on System Bus that is
617 // made available over OpenBmc system services.
618 rc = sd_bus_call_method(bus_type, // On the System Bus
619 sys_bus_name, // Service to contact
620 sys_object_name, // Object path
621 sys_intf_name, // Interface name
622 "getFRUArea", // Method to be called
623 &bus_error, // object to return error
624 &response, // Response message on success
625 "y", // input message (integer)
626 fruid); // Argument
627
628 if(rc < 0)
629 {
630 fprintf(stderr, "Failed to get fru area for fruid:[%d] to dbus: [%s]\n",
631 fruid, bus_error.message);
632 }
633 else
634 {
635 // if several fru area names are defined, the names are combined to
636 // a string seperated by ','
637 rc = sd_bus_message_read(response, "s", &areas);
638 if(rc < 0)
639 {
640 fprintf(stderr, "Failed to parse response message from getFRUArea:[%s]\n",
641 strerror(-rc));
642 }
643 else
644 {
645#ifdef __IPMI_DEBUG__
646 printf("get defined fru area: id: %d, areas: %s\n", fruid, areas);
647#endif
648 std::string area_name;
649 std::stringstream ss(areas);
650 // fru area names string is seperated by ',', parse it into tokens
651 while (std::getline(ss, area_name, ','))
652 {
653 if (!area_name.empty())
654 defined_fru_area.emplace_back(area_name);
655 }
656 }
657 }
658
Brad Bishopde6a3792016-07-25 17:09:12 -0400659exit:
660
661 free(sys_bus_name);
Yi Li75c2d462016-04-11 16:57:46 +0800662 sd_bus_error_free(&bus_error);
663 sd_bus_message_unref(response);
664
665 return rc;
666}
667
668
Vishwa4be4b7a2015-10-31 22:55:50 -0500669///-----------------------------------------------------
670// Accepts the filename and validates per IPMI FRU spec
671//----------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600672int ipmi_validate_fru_area(const uint8_t fruid, const char *fru_file_name,
vishwac93d6d42015-12-16 11:55:16 -0600673 sd_bus *bus_type, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500674{
vishwac93d6d42015-12-16 11:55:16 -0600675 size_t data_len = 0;
676 size_t bytes_read = 0;
677 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500678
vishwac93d6d42015-12-16 11:55:16 -0600679 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
680 // are not used, keeping it here for completeness.
681 fru_area_vec_t fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800682 std::vector<std::string> defined_fru_area;
683
684 // BMC defines fru areas that should be present in Skeleton
685 rc = get_defined_fru_area(bus_type, fruid, defined_fru_area);
686 if(rc < 0)
687 {
688 fprintf(stderr, "ERROR: cannot get defined fru area\n");
689 return rc;
690 }
vishwac93d6d42015-12-16 11:55:16 -0600691 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
692 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
693 {
694 // Create an object and push onto a vector.
695 std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru>
696 (fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru);
697
698 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500699 bool present = access(fru_file_name, F_OK) == 0;
vishwac93d6d42015-12-16 11:55:16 -0600700 fru_area->set_present(present);
701
Yi Li75c2d462016-04-11 16:57:46 +0800702 // Only setup dbus path for areas defined in BMC.
703 // Otherwise Skeleton will report 'not found' error
704 std::string fru_area_name = fru_area->get_name() + std::to_string(fruid);
705 auto iter = std::find(defined_fru_area.begin(), defined_fru_area.end(),
706 fru_area_name);
707 if (iter != defined_fru_area.end())
708 {
709 fru_area->setup_sd_bus_paths();
710 }
vishwac93d6d42015-12-16 11:55:16 -0600711 fru_area_vec.emplace_back(std::move(fru_area));
712 }
713
714 FILE *fru_fp = fopen(fru_file_name,"rb");
715 if(fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500716 {
717 fprintf(stderr, "ERROR: opening:[%s]\n",fru_file_name);
718 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600719 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500720 }
721
vishwac93d6d42015-12-16 11:55:16 -0600722 // Get the size of the file to see if it meets minimum requirement
723 if(fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500724 {
725 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600726 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500727 }
728
vishwac93d6d42015-12-16 11:55:16 -0600729 // Allocate a buffer to hold entire file content
730 data_len = ftell(fru_fp);
731 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500732
vishwac93d6d42015-12-16 11:55:16 -0600733 rewind(fru_fp);
734 bytes_read = fread(fru_data, data_len, 1, fru_fp);
Vishwa4be4b7a2015-10-31 22:55:50 -0500735 if(bytes_read != 1)
736 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500737 fprintf(stderr, "Failed reading fru data. Bytes_read=[%zd]\n",bytes_read);
Vishwa4be4b7a2015-10-31 22:55:50 -0500738 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600739 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500740 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500741
vishwac93d6d42015-12-16 11:55:16 -0600742 // We are done reading.
743 fclose(fru_fp);
744 fru_fp = NULL;
745
746 rc = ipmi_validate_common_hdr(fru_data, data_len);
vishwaf3ca3522015-12-02 10:35:13 -0600747 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500748 {
vishwac93d6d42015-12-16 11:55:16 -0600749 return cleanup_error(fru_fp, fru_area_vec);
750 }
751
752 // Now that we validated the common header, populate various fru sections if we have them here.
753 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
754 if(rc < 0)
755 {
756 fprintf(stderr,"Populating FRU areas failed for:[%d]\n",fruid);
757 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500758 }
759 else
760 {
vishwac93d6d42015-12-16 11:55:16 -0600761 printf("SUCCESS: Populated FRU areas for:[%s]\n",fru_file_name);
Vishwa4be4b7a2015-10-31 22:55:50 -0500762 }
763
vishwac93d6d42015-12-16 11:55:16 -0600764#ifdef __IPMI_DEBUG__
765 for(auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500766 {
vishwac93d6d42015-12-16 11:55:16 -0600767 printf("FRU ID : [%d]\n",(iter)->get_fruid());
768 printf("AREA NAME : [%s]\n",(iter)->get_name());
769 printf("TYPE : [%d]\n",(iter)->get_type());
770 printf("LEN : [%d]\n",(iter)->get_len());
771 printf("BUS NAME : [%s]\n", (iter)->get_bus_name());
772 printf("OBJ PATH : [%s]\n", (iter)->get_obj_path());
773 printf("INTF NAME :[%s]\n", (iter)->get_intf_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500774 }
vishwac93d6d42015-12-16 11:55:16 -0600775#endif
776
777 // If the vector is populated with everything, then go ahead and update the
778 // inventory.
779 if(!(fru_area_vec.empty()))
780 {
781
782#ifdef __IPMI_DEBUG__
783 printf("\n SIZE of vector is : [%d] \n",fru_area_vec.size());
784#endif
785 rc = ipmi_update_inventory(fru_area_vec);
786 if(rc <0)
787 {
788 fprintf(stderr, "Error updating inventory\n");
789 }
790 }
791
792 // we are done with all that we wanted to do. This will do the job of
793 // calling any destructors too.
794 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500795
796 return rc;
797}