blob: bccaf0ab33418c2c233b5f2bcbb13abe97e885f7 [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"
16#include "fru-area.H"
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 Williams804077d2016-07-29 15:26:18 -0500174 const char *inv_bus_name = NULL, *inv_obj_path = NULL,
175 *inv_intf_name = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600176 char fru_area_name[16] = {0};
Brad Bishopde6a3792016-07-25 17:09:12 -0400177 char *sys_bus_name = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600178 sprintf(fru_area_name,"%s%d",iv_name.c_str(), iv_fruid);
179
180#ifdef __IPMI_DEBUG__
181 printf("Getting sd_bus for :[%s]\n",fru_area_name);
182#endif
183
Brad Bishopde6a3792016-07-25 17:09:12 -0400184 rc = mapper_get_service(iv_bus_type, sys_object_name, &sys_bus_name);
185 if(rc < 0)
186 {
187 fprintf(stderr, "Failed to get system manager service:[%s]\n",
188 strerror(-rc));
189 goto exit;
190 }
191
vishwac93d6d42015-12-16 11:55:16 -0600192 // We want to call a method "getObjectFromId" on System Bus that is
193 // made available over OpenBmc system services.
Yi Li75c2d462016-04-11 16:57:46 +0800194
vishwac93d6d42015-12-16 11:55:16 -0600195 rc = sd_bus_call_method(iv_bus_type, // On the System Bus
196 sys_bus_name, // Service to contact
197 sys_object_name, // Object path
198 sys_intf_name, // Interface name
199 "getObjectFromId", // Method to be called
200 &bus_error, // object to return error
201 &response, // Response message on success
202 "ss", // input message (string,string)
203 "FRU_STR", // First argument to getObjectFromId
204 fru_area_name); // Second Argument
vishwac93d6d42015-12-16 11:55:16 -0600205 if(rc < 0)
206 {
207 fprintf(stderr, "Failed to resolve fruid:[%d] to dbus: [%s]\n", iv_fruid, bus_error.message);
208 }
209 else
210 {
Brad Bishopde6a3792016-07-25 17:09:12 -0400211 // Method getObjectFromId returns 2 parameters and all are strings, namely
212 // object_path and interface name for accessing that particular
213 // FRU over Inventory SDBUS manager. 'ss' here mentions that format.
214 rc = sd_bus_message_read(response, "(ss)", &inv_obj_path, &inv_intf_name);
vishwac93d6d42015-12-16 11:55:16 -0600215 if(rc < 0)
216 {
217 fprintf(stderr, "Failed to parse response message:[%s]\n", strerror(-rc));
218 }
219 else
220 {
Brad Bishopde6a3792016-07-25 17:09:12 -0400221 rc = mapper_get_service(iv_bus_type, inv_obj_path, &inv_bus_name);
222 if(rc < 0)
223 {
224 fprintf(stderr, "Failed to get inventory item service:[%s]\n",
225 strerror(-rc));
226 goto exit;
227 }
vishwac93d6d42015-12-16 11:55:16 -0600228 // Update the paths in the area object
229 update_dbus_paths(inv_bus_name, inv_obj_path, inv_intf_name);
230 }
231 }
232
Brad Bishopde6a3792016-07-25 17:09:12 -0400233exit:
vishwac93d6d42015-12-16 11:55:16 -0600234#ifdef __IPMI_DEBUG__
235 printf("fru_area=[%s], inv_bus_name=[%s], inv_obj_path=[%s], inv_intf_name=[%s]\n",
236 fru_area_name, inv_bus_name, inv_obj_path, inv_intf_name);
237#endif
238
Brad Bishopde6a3792016-07-25 17:09:12 -0400239 free(sys_bus_name);
vishwac93d6d42015-12-16 11:55:16 -0600240 sd_bus_error_free(&bus_error);
241 sd_bus_message_unref(response);
242
243 return rc;
244}
245
Vishwa4be4b7a2015-10-31 22:55:50 -0500246//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600247// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600248// and returns the 8 bit checksum
249// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500250//-------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600251unsigned char calculate_crc(const unsigned char *data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500252{
253 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600254 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500255
256 for(byte = 0; byte < len; byte++)
257 {
258 crc += *data++;
259 }
vishwaf3ca3522015-12-02 10:35:13 -0600260
Vishwa4be4b7a2015-10-31 22:55:50 -0500261 return(-crc);
262}
263
264//---------------------------------------------------------------------
265// Accepts a fru area offset in commom hdr and tells which area it is.
266//---------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600267ipmi_fru_area_type get_fru_area_type(uint8_t area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500268{
269 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
270
271 switch(area_offset)
272 {
273 case IPMI_FRU_INTERNAL_OFFSET:
274 type = IPMI_FRU_AREA_INTERNAL_USE;
275 break;
276
277 case IPMI_FRU_CHASSIS_OFFSET:
278 type = IPMI_FRU_AREA_CHASSIS_INFO;
279 break;
280
281 case IPMI_FRU_BOARD_OFFSET:
282 type = IPMI_FRU_AREA_BOARD_INFO;
283 break;
284
285 case IPMI_FRU_PRODUCT_OFFSET:
286 type = IPMI_FRU_AREA_PRODUCT_INFO;
287 break;
288
289 case IPMI_FRU_MULTI_OFFSET:
290 type = IPMI_FRU_AREA_MULTI_RECORD;
291 break;
292
293 default:
294 type = IPMI_FRU_AREA_TYPE_MAX;
295 }
296
297 return type;
298}
299
vishwac93d6d42015-12-16 11:55:16 -0600300///-----------------------------------------------
301// Validates the data for crc and mandatory fields
302///-----------------------------------------------
303int verify_fru_data(const uint8_t *data, const size_t len)
304{
305 uint8_t checksum = 0;
306 int rc = -1;
307
308 // Validate for first byte to always have a value of [1]
309 if(data[0] != IPMI_FRU_HDR_BYTE_ZERO)
310 {
311 fprintf(stderr, "Invalid entry:[%d] in byte-0\n",data[0]);
312 return rc;
313 }
314#ifdef __IPMI_DEBUG__
315 else
316 {
317 printf("SUCCESS: Validated [0x%X] in entry_1 of fru_data\n",data[0]);
318 }
319#endif
320
321 // See if the calculated CRC matches with the embedded one.
322 // CRC to be calculated on all except the last one that is CRC itself.
323 checksum = calculate_crc(data, len - 1);
324 if(checksum != data[len-1])
325 {
326#ifdef __IPMI_DEBUG__
327 fprintf(stderr, "Checksum mismatch."
328 " Calculated:[0x%X], Embedded:[0x%X]\n",
329 checksum, data[len]);
330#endif
331 return rc;
332 }
333#ifdef __IPMI_DEBUG__
334 else
335 {
336 printf("SUCCESS: Checksum matches:[0x%X]\n",checksum);
337 }
338#endif
339
340 return EXIT_SUCCESS;
341}
342
Vishwa4be4b7a2015-10-31 22:55:50 -0500343//------------------------------------------------------------------------
344// Takes FRU data, invokes Parser for each fru record area and updates
345// Inventory
346//------------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600347int ipmi_update_inventory(fru_area_vec_t & area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500348{
vishwac93d6d42015-12-16 11:55:16 -0600349 // Generic error reporter
Vishwa4be4b7a2015-10-31 22:55:50 -0500350 int rc = 0;
vishwaf3ca3522015-12-02 10:35:13 -0600351
Vishwa4be4b7a2015-10-31 22:55:50 -0500352 // Dictionary object to hold Name:Value pair
353 sd_bus_message *fru_dict = NULL;
354
355 // SD Bus error report mechanism.
356 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
357
vishwac93d6d42015-12-16 11:55:16 -0600358 // Response from sd bus calls
Vishwa4be4b7a2015-10-31 22:55:50 -0500359 sd_bus_message *response = NULL;
360
Vishwa4be4b7a2015-10-31 22:55:50 -0500361 // For each FRU area, extract the needed data , get it parsed and update
362 // the Inventory.
363 for(auto& iter : area_vec)
364 {
vishwac93d6d42015-12-16 11:55:16 -0600365 // Start fresh on each.
Vishwa4be4b7a2015-10-31 22:55:50 -0500366 sd_bus_error_free(&bus_error);
367 sd_bus_message_unref(response);
368 sd_bus_message_unref(fru_dict);
vishwaf3ca3522015-12-02 10:35:13 -0600369
Vishwa4be4b7a2015-10-31 22:55:50 -0500370 // Constructor to allow further initializations and customization.
vishwac93d6d42015-12-16 11:55:16 -0600371 rc = sd_bus_message_new_method_call((iter)->get_bus_type(),
Vishwa4be4b7a2015-10-31 22:55:50 -0500372 &fru_dict,
vishwac93d6d42015-12-16 11:55:16 -0600373 (iter)->get_bus_name(),
374 (iter)->get_obj_path(),
375 (iter)->get_intf_name(),
Vishwa4be4b7a2015-10-31 22:55:50 -0500376 "update");
377 if(rc < 0)
378 {
vishwac93d6d42015-12-16 11:55:16 -0600379 fprintf(stderr,"ERROR: creating a update method call for bus_name:[%s]\n",
380 (iter)->get_bus_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500381 break;
382 }
383
384 // A Dictionary ({}) having (string, variant)
385 rc = sd_bus_message_open_container(fru_dict, 'a', "{sv}");
386 if(rc < 0)
387 {
388 fprintf(stderr,"ERROR:[%d] creating a dict container:\n",errno);
389 break;
390 }
391
392 // Fill the container with information
vishwac93d6d42015-12-16 11:55:16 -0600393 rc = parse_fru_area((iter)->get_type(), (void *)(iter)->get_data(), (iter)->get_len(), fru_dict);
Vishwa4be4b7a2015-10-31 22:55:50 -0500394 if(rc < 0)
395 {
396 fprintf(stderr,"ERROR parsing FRU records\n");
397 break;
398 }
399
400 sd_bus_message_close_container(fru_dict);
401
402 // Now, Make the actual call to update the FRU inventory database with the
403 // dictionary given by FRU Parser. There is no response message expected for
404 // this.
vishwac93d6d42015-12-16 11:55:16 -0600405 rc = sd_bus_call((iter)->get_bus_type(), // On the System Bus
406 fru_dict, // With the Name:value dictionary array
407 0, //
408 &bus_error, // Object to return error.
409 &response); // Response message if any.
Vishwa4be4b7a2015-10-31 22:55:50 -0500410
411 if(rc < 0)
412 {
413 fprintf(stderr, "ERROR:[%s] updating FRU inventory for ID:[0x%X]\n",
vishwac93d6d42015-12-16 11:55:16 -0600414 bus_error.message, (iter)->get_fruid());
415 break;
Vishwa4be4b7a2015-10-31 22:55:50 -0500416 }
vishwac93d6d42015-12-16 11:55:16 -0600417 else if((iter)->is_bmc_fru())
vishwaf3ca3522015-12-02 10:35:13 -0600418 {
vishwac93d6d42015-12-16 11:55:16 -0600419 // For FRUs that are accessible by HostBoot, host boot does all of
420 // these.
421 printf("SUCCESS: Updated:[%s_%d] successfully. Setting Valid bit\n",
422 (iter)->get_name(), (iter)->get_fruid());
vishwaf3ca3522015-12-02 10:35:13 -0600423
vishwac93d6d42015-12-16 11:55:16 -0600424 (iter)->set_valid(true);
vishwaf3ca3522015-12-02 10:35:13 -0600425 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500426 else
427 {
vishwac93d6d42015-12-16 11:55:16 -0600428 printf("SUCCESS: Updated:[%s_%d] successfully\n",
429 (iter)->get_name(), (iter)->get_fruid());
Vishwa4be4b7a2015-10-31 22:55:50 -0500430 }
431 } // END walking the vector of areas and updating
432
433 sd_bus_error_free(&bus_error);
434 sd_bus_message_unref(response);
435 sd_bus_message_unref(fru_dict);
Vishwa4be4b7a2015-10-31 22:55:50 -0500436
437 return rc;
438}
439
vishwac93d6d42015-12-16 11:55:16 -0600440///----------------------------------------------------
441// Checks if a particular fru area is populated or not
442///----------------------------------------------------
443bool remove_invalid_area(const std::unique_ptr<ipmi_fru> &fru_area)
Vishwa4be4b7a2015-10-31 22:55:50 -0500444{
vishwac93d6d42015-12-16 11:55:16 -0600445 // Filter the ones that do not have dbus reference.
446 if((strlen((fru_area)->get_bus_name()) == 0) ||
447 (strlen((fru_area)->get_obj_path()) == 0) ||
448 (strlen((fru_area)->get_intf_name()) == 0))
449 {
450 return true;
451 }
452 return false;
453}
Vishwa4be4b7a2015-10-31 22:55:50 -0500454
vishwac93d6d42015-12-16 11:55:16 -0600455///----------------------------------------------------------------------------------
456// Populates various FRU areas
457// @prereq : This must be called only after validating common header.
458///----------------------------------------------------------------------------------
459int ipmi_populate_fru_areas(uint8_t *fru_data, const size_t data_len,
460 fru_area_vec_t & fru_area_vec)
461{
vishwa13555bd2015-11-10 12:10:38 -0600462 size_t area_offset = 0;
vishwac93d6d42015-12-16 11:55:16 -0600463 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500464
vishwac93d6d42015-12-16 11:55:16 -0600465 // Now walk the common header and see if the file size has atleast the last
466 // offset mentioned by the common_hdr. If the file size is less than the
467 // offset of any if the fru areas mentioned in the common header, then we do
468 // not have a complete file.
469 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
470 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
471 {
Yi Li75c2d462016-04-11 16:57:46 +0800472 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600473 // Actual offset in the payload is the offset mentioned in common header
474 // multipled by 8. Common header is always the first 8 bytes.
475 area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
476 if(area_offset && (data_len < (area_offset + 2)))
477 {
478 // Our file size is less than what it needs to be. +2 because we are
479 // using area len that is at 2 byte off area_offset
480 fprintf(stderr, "fru file is incomplete. Size:[%d]\n",data_len);
481 return rc;
482 }
483 else if(area_offset)
484 {
485 // Read 2 bytes to know the actual size of area.
486 uint8_t area_hdr[2] = {0};
487 memcpy(area_hdr, &((uint8_t *)fru_data)[area_offset], sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500488
vishwac93d6d42015-12-16 11:55:16 -0600489 // Size of this area will be the 2nd byte in the fru area header.
490 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
491 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500492
vishwac93d6d42015-12-16 11:55:16 -0600493 printf("fru data size:[%d], area offset:[%d], area_size:[%d]\n",
494 data_len, area_offset, area_len);
Vishwa4be4b7a2015-10-31 22:55:50 -0500495
vishwac93d6d42015-12-16 11:55:16 -0600496 // See if we really have that much buffer. We have area offset amd
497 // from there, the actual len.
498 if(data_len < (area_len + area_offset))
499 {
500 fprintf(stderr, "Incomplete Fru file.. Size:[%d]\n",data_len);
501 return rc;
502 }
503
504 // Save off the data.
505 memcpy(area_data, &((uint8_t *)fru_data)[area_offset], area_len);
506
507 // Validate the crc
508 rc = verify_fru_data(area_data, area_len);
509 if(rc < 0)
510 {
511 fprintf(stderr, "Error validating fru area. offset:[%d]\n",area_offset);
512 return rc;
513 }
514 else
515 {
516 printf("Successfully verified area checksum. offset:[%d]\n",area_offset);
517 }
518
519 // We already have a vector that is passed to us containing all
520 // of the fields populated. Update the data portion now.
521 for(auto& iter : fru_area_vec)
522 {
523 if((iter)->get_type() == get_fru_area_type(fru_entry))
524 {
525 (iter)->set_data(area_data, area_len);
526 }
527 }
528 } // If we have fru data present
529 } // Walk common_hdr
530
531 // Not all the fields will be populated in a fru data. Mostly all cases will
532 // not have more than 2 or 3.
533 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
534 remove_invalid_area), fru_area_vec.end());
535
536 return EXIT_SUCCESS;
537}
538
539///---------------------------------------------------------
540// Validates the fru data per ipmi common header constructs.
541// Returns with updated common_hdr and also file_size
542//----------------------------------------------------------
543int ipmi_validate_common_hdr(const uint8_t *fru_data, const size_t data_len)
544{
545 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500546
547 uint8_t common_hdr[sizeof(struct common_header)] = {0};
vishwac93d6d42015-12-16 11:55:16 -0600548 if(data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500549 {
vishwac93d6d42015-12-16 11:55:16 -0600550 memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500551 }
552 else
553 {
vishwac93d6d42015-12-16 11:55:16 -0600554 fprintf(stderr, "Incomplete fru data file. Size:[%d]\n", data_len);
555 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500556 }
557
vishwac93d6d42015-12-16 11:55:16 -0600558 // Verify the crc and size
559 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
560 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500561 {
vishwac93d6d42015-12-16 11:55:16 -0600562 fprintf(stderr, "Failed to validate common header\n");
563 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500564 }
565
vishwac93d6d42015-12-16 11:55:16 -0600566 return EXIT_SUCCESS;
567}
Vishwa4be4b7a2015-10-31 22:55:50 -0500568
vishwac93d6d42015-12-16 11:55:16 -0600569//------------------------------------------------------------
570// Cleanup routine
571//------------------------------------------------------------
572int cleanup_error(FILE *fru_fp, fru_area_vec_t & fru_area_vec)
573{
574 if(fru_fp != NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500575 {
vishwac93d6d42015-12-16 11:55:16 -0600576 fclose(fru_fp);
577 fru_fp = NULL;
578 }
vishwaf3ca3522015-12-02 10:35:13 -0600579
Vishwa4be4b7a2015-10-31 22:55:50 -0500580 if(!(fru_area_vec.empty()))
581 {
vishwac93d6d42015-12-16 11:55:16 -0600582 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500583 }
vishwaf3ca3522015-12-02 10:35:13 -0600584
vishwac93d6d42015-12-16 11:55:16 -0600585 return -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500586}
587
Yi Li75c2d462016-04-11 16:57:46 +0800588
589///-----------------------------------------------------
590// Get the fru area names defined in BMC for a given @fruid.
591//----------------------------------------------------
592int get_defined_fru_area(sd_bus *bus_type, const uint8_t fruid,
593 std::vector<std::string> &defined_fru_area)
594{
595 // Need this to get respective DBUS objects
596 sd_bus_error bus_error = SD_BUS_ERROR_NULL;
597 sd_bus_message *response = NULL;
598 int rc = 0;
Patrick Williams804077d2016-07-29 15:26:18 -0500599 const char *areas = NULL;
Brad Bishopde6a3792016-07-25 17:09:12 -0400600 char *sys_bus_name = NULL;
Yi Li75c2d462016-04-11 16:57:46 +0800601
602#ifdef __IPMI_DEBUG__
603 printf("Getting fru areas defined in Skeleton for :[%d]\n", fruid);
604#endif
605
Brad Bishopde6a3792016-07-25 17:09:12 -0400606 rc = mapper_get_service(bus_type, sys_object_name, &sys_bus_name);
607 if(rc < 0)
608 {
609 fprintf(stderr, "Failed to get system manager service:[%s]\n",
610 strerror(-rc));
611 goto exit;
612 }
613
Yi Li75c2d462016-04-11 16:57:46 +0800614 // We want to call a method "getFRUArea" on System Bus that is
615 // made available over OpenBmc system services.
616 rc = sd_bus_call_method(bus_type, // On the System Bus
617 sys_bus_name, // Service to contact
618 sys_object_name, // Object path
619 sys_intf_name, // Interface name
620 "getFRUArea", // Method to be called
621 &bus_error, // object to return error
622 &response, // Response message on success
623 "y", // input message (integer)
624 fruid); // Argument
625
626 if(rc < 0)
627 {
628 fprintf(stderr, "Failed to get fru area for fruid:[%d] to dbus: [%s]\n",
629 fruid, bus_error.message);
630 }
631 else
632 {
633 // if several fru area names are defined, the names are combined to
634 // a string seperated by ','
635 rc = sd_bus_message_read(response, "s", &areas);
636 if(rc < 0)
637 {
638 fprintf(stderr, "Failed to parse response message from getFRUArea:[%s]\n",
639 strerror(-rc));
640 }
641 else
642 {
643#ifdef __IPMI_DEBUG__
644 printf("get defined fru area: id: %d, areas: %s\n", fruid, areas);
645#endif
646 std::string area_name;
647 std::stringstream ss(areas);
648 // fru area names string is seperated by ',', parse it into tokens
649 while (std::getline(ss, area_name, ','))
650 {
651 if (!area_name.empty())
652 defined_fru_area.emplace_back(area_name);
653 }
654 }
655 }
656
Brad Bishopde6a3792016-07-25 17:09:12 -0400657exit:
658
659 free(sys_bus_name);
Yi Li75c2d462016-04-11 16:57:46 +0800660 sd_bus_error_free(&bus_error);
661 sd_bus_message_unref(response);
662
663 return rc;
664}
665
666
Vishwa4be4b7a2015-10-31 22:55:50 -0500667///-----------------------------------------------------
668// Accepts the filename and validates per IPMI FRU spec
669//----------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600670int ipmi_validate_fru_area(const uint8_t fruid, const char *fru_file_name,
vishwac93d6d42015-12-16 11:55:16 -0600671 sd_bus *bus_type, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500672{
vishwac93d6d42015-12-16 11:55:16 -0600673 size_t data_len = 0;
674 size_t bytes_read = 0;
675 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500676
vishwac93d6d42015-12-16 11:55:16 -0600677 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
678 // are not used, keeping it here for completeness.
679 fru_area_vec_t fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800680 std::vector<std::string> defined_fru_area;
681
682 // BMC defines fru areas that should be present in Skeleton
683 rc = get_defined_fru_area(bus_type, fruid, defined_fru_area);
684 if(rc < 0)
685 {
686 fprintf(stderr, "ERROR: cannot get defined fru area\n");
687 return rc;
688 }
vishwac93d6d42015-12-16 11:55:16 -0600689 for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
690 fru_entry < (sizeof(struct common_header) -2); fru_entry++)
691 {
692 // Create an object and push onto a vector.
693 std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru>
694 (fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru);
695
696 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500697 bool present = access(fru_file_name, F_OK) == 0;
vishwac93d6d42015-12-16 11:55:16 -0600698 fru_area->set_present(present);
699
Yi Li75c2d462016-04-11 16:57:46 +0800700 // Only setup dbus path for areas defined in BMC.
701 // Otherwise Skeleton will report 'not found' error
702 std::string fru_area_name = fru_area->get_name() + std::to_string(fruid);
703 auto iter = std::find(defined_fru_area.begin(), defined_fru_area.end(),
704 fru_area_name);
705 if (iter != defined_fru_area.end())
706 {
707 fru_area->setup_sd_bus_paths();
708 }
vishwac93d6d42015-12-16 11:55:16 -0600709 fru_area_vec.emplace_back(std::move(fru_area));
710 }
711
712 FILE *fru_fp = fopen(fru_file_name,"rb");
713 if(fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500714 {
715 fprintf(stderr, "ERROR: opening:[%s]\n",fru_file_name);
716 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600717 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500718 }
719
vishwac93d6d42015-12-16 11:55:16 -0600720 // Get the size of the file to see if it meets minimum requirement
721 if(fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500722 {
723 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600724 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500725 }
726
vishwac93d6d42015-12-16 11:55:16 -0600727 // Allocate a buffer to hold entire file content
728 data_len = ftell(fru_fp);
729 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500730
vishwac93d6d42015-12-16 11:55:16 -0600731 rewind(fru_fp);
732 bytes_read = fread(fru_data, data_len, 1, fru_fp);
Vishwa4be4b7a2015-10-31 22:55:50 -0500733 if(bytes_read != 1)
734 {
vishwac93d6d42015-12-16 11:55:16 -0600735 fprintf(stderr, "Failed reading fru data. Bytes_read=[%d]\n",bytes_read);
Vishwa4be4b7a2015-10-31 22:55:50 -0500736 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600737 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500738 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500739
vishwac93d6d42015-12-16 11:55:16 -0600740 // We are done reading.
741 fclose(fru_fp);
742 fru_fp = NULL;
743
744 rc = ipmi_validate_common_hdr(fru_data, data_len);
vishwaf3ca3522015-12-02 10:35:13 -0600745 if(rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500746 {
vishwac93d6d42015-12-16 11:55:16 -0600747 return cleanup_error(fru_fp, fru_area_vec);
748 }
749
750 // Now that we validated the common header, populate various fru sections if we have them here.
751 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
752 if(rc < 0)
753 {
754 fprintf(stderr,"Populating FRU areas failed for:[%d]\n",fruid);
755 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500756 }
757 else
758 {
vishwac93d6d42015-12-16 11:55:16 -0600759 printf("SUCCESS: Populated FRU areas for:[%s]\n",fru_file_name);
Vishwa4be4b7a2015-10-31 22:55:50 -0500760 }
761
vishwac93d6d42015-12-16 11:55:16 -0600762#ifdef __IPMI_DEBUG__
763 for(auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500764 {
vishwac93d6d42015-12-16 11:55:16 -0600765 printf("FRU ID : [%d]\n",(iter)->get_fruid());
766 printf("AREA NAME : [%s]\n",(iter)->get_name());
767 printf("TYPE : [%d]\n",(iter)->get_type());
768 printf("LEN : [%d]\n",(iter)->get_len());
769 printf("BUS NAME : [%s]\n", (iter)->get_bus_name());
770 printf("OBJ PATH : [%s]\n", (iter)->get_obj_path());
771 printf("INTF NAME :[%s]\n", (iter)->get_intf_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500772 }
vishwac93d6d42015-12-16 11:55:16 -0600773#endif
774
775 // If the vector is populated with everything, then go ahead and update the
776 // inventory.
777 if(!(fru_area_vec.empty()))
778 {
779
780#ifdef __IPMI_DEBUG__
781 printf("\n SIZE of vector is : [%d] \n",fru_area_vec.size());
782#endif
783 rc = ipmi_update_inventory(fru_area_vec);
784 if(rc <0)
785 {
786 fprintf(stderr, "Error updating inventory\n");
787 }
788 }
789
790 // we are done with all that we wanted to do. This will do the job of
791 // calling any destructors too.
792 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500793
794 return rc;
795}