Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 1 | #include <vector> |
| 2 | #include <stdlib.h> |
| 3 | #include <dlfcn.h> |
| 4 | #include <errno.h> |
| 5 | #include <stdio.h> |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 6 | #include <systemd/sd-bus.h> |
Chris Austen | b45c4cb | 2015-11-01 06:34:56 -0600 | [diff] [blame] | 7 | #include <unistd.h> |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 8 | #include <host-ipmid/ipmid-api.h> |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 9 | #include <iostream> |
| 10 | #include <memory> |
| 11 | #include <algorithm> |
| 12 | #include <fstream> |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 13 | #include <sstream> |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 14 | #include "frup.h" |
| 15 | #include "fru-area.H" |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 16 | |
| 17 | // OpenBMC System Manager dbus framework |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 18 | const char *sys_bus_name = "org.openbmc.managers.System"; |
| 19 | const char *sys_object_name = "/org/openbmc/managers/System"; |
| 20 | const char *sys_intf_name = "org.openbmc.managers.System"; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 21 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 22 | //---------------------------------------------------------------- |
| 23 | // Constructor |
| 24 | //---------------------------------------------------------------- |
| 25 | ipmi_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 | //----------------------------------------------------- |
| 67 | void 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 | //----------------------------------------------------- |
| 77 | void 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 | //------------------- |
| 88 | ipmi_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 Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 156 | printf("Present bit set to :[%s] for fruid:[%d], Path[%s]:\n", |
| 157 | present_bit, iv_fruid, iv_obj_path.c_str()); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 158 | } |
| 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 |
| 166 | int 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 Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 184 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 185 | 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 |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 195 | 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 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 227 | //------------------------------------------------ |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 228 | // Takes the pointer to stream of bytes and length |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 229 | // and returns the 8 bit checksum |
| 230 | // This algo is per IPMI V2.0 spec |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 231 | //------------------------------------------------- |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 232 | unsigned char calculate_crc(const unsigned char *data, size_t len) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 233 | { |
| 234 | char crc = 0; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 235 | size_t byte = 0; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 236 | |
| 237 | for(byte = 0; byte < len; byte++) |
| 238 | { |
| 239 | crc += *data++; |
| 240 | } |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 241 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 242 | return(-crc); |
| 243 | } |
| 244 | |
| 245 | //--------------------------------------------------------------------- |
| 246 | // Accepts a fru area offset in commom hdr and tells which area it is. |
| 247 | //--------------------------------------------------------------------- |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 248 | ipmi_fru_area_type get_fru_area_type(uint8_t area_offset) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 249 | { |
| 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 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 281 | ///----------------------------------------------- |
| 282 | // Validates the data for crc and mandatory fields |
| 283 | ///----------------------------------------------- |
| 284 | int 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 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 324 | //------------------------------------------------------------------------ |
| 325 | // Takes FRU data, invokes Parser for each fru record area and updates |
| 326 | // Inventory |
| 327 | //------------------------------------------------------------------------ |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 328 | int ipmi_update_inventory(fru_area_vec_t & area_vec) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 329 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 330 | // Generic error reporter |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 331 | int rc = 0; |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 332 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 333 | // 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 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 339 | // Response from sd bus calls |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 340 | sd_bus_message *response = NULL; |
| 341 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 342 | // For each FRU area, extract the needed data , get it parsed and update |
| 343 | // the Inventory. |
| 344 | for(auto& iter : area_vec) |
| 345 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 346 | // Start fresh on each. |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 347 | sd_bus_error_free(&bus_error); |
| 348 | sd_bus_message_unref(response); |
| 349 | sd_bus_message_unref(fru_dict); |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 350 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 351 | // Constructor to allow further initializations and customization. |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 352 | rc = sd_bus_message_new_method_call((iter)->get_bus_type(), |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 353 | &fru_dict, |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 354 | (iter)->get_bus_name(), |
| 355 | (iter)->get_obj_path(), |
| 356 | (iter)->get_intf_name(), |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 357 | "update"); |
| 358 | if(rc < 0) |
| 359 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 360 | fprintf(stderr,"ERROR: creating a update method call for bus_name:[%s]\n", |
| 361 | (iter)->get_bus_name()); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 362 | 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 |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 374 | rc = parse_fru_area((iter)->get_type(), (void *)(iter)->get_data(), (iter)->get_len(), fru_dict); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 375 | 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. |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 386 | 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. |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 391 | |
| 392 | if(rc < 0) |
| 393 | { |
| 394 | fprintf(stderr, "ERROR:[%s] updating FRU inventory for ID:[0x%X]\n", |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 395 | bus_error.message, (iter)->get_fruid()); |
| 396 | break; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 397 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 398 | else if((iter)->is_bmc_fru()) |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 399 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 400 | // 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()); |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 404 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 405 | (iter)->set_valid(true); |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 406 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 407 | else |
| 408 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 409 | printf("SUCCESS: Updated:[%s_%d] successfully\n", |
| 410 | (iter)->get_name(), (iter)->get_fruid()); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 411 | } |
| 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); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 417 | |
| 418 | return rc; |
| 419 | } |
| 420 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 421 | ///---------------------------------------------------- |
| 422 | // Checks if a particular fru area is populated or not |
| 423 | ///---------------------------------------------------- |
| 424 | bool remove_invalid_area(const std::unique_ptr<ipmi_fru> &fru_area) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 425 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 426 | // 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 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 435 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 436 | ///---------------------------------------------------------------------------------- |
| 437 | // Populates various FRU areas |
| 438 | // @prereq : This must be called only after validating common header. |
| 439 | ///---------------------------------------------------------------------------------- |
| 440 | int ipmi_populate_fru_areas(uint8_t *fru_data, const size_t data_len, |
| 441 | fru_area_vec_t & fru_area_vec) |
| 442 | { |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 443 | size_t area_offset = 0; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 444 | int rc = -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 445 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 446 | // 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 Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 453 | rc = -1; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 454 | // 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)); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 469 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 470 | // 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}; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 473 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 474 | printf("fru data size:[%d], area offset:[%d], area_size:[%d]\n", |
| 475 | data_len, area_offset, area_len); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 476 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 477 | // 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 | //---------------------------------------------------------- |
| 524 | int ipmi_validate_common_hdr(const uint8_t *fru_data, const size_t data_len) |
| 525 | { |
| 526 | int rc = -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 527 | |
| 528 | uint8_t common_hdr[sizeof(struct common_header)] = {0}; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 529 | if(data_len >= sizeof(common_hdr)) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 530 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 531 | memcpy(common_hdr, fru_data, sizeof(common_hdr)); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 532 | } |
| 533 | else |
| 534 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 535 | fprintf(stderr, "Incomplete fru data file. Size:[%d]\n", data_len); |
| 536 | return rc; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 537 | } |
| 538 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 539 | // Verify the crc and size |
| 540 | rc = verify_fru_data(common_hdr, sizeof(common_hdr)); |
| 541 | if(rc < 0) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 542 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 543 | fprintf(stderr, "Failed to validate common header\n"); |
| 544 | return rc; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 545 | } |
| 546 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 547 | return EXIT_SUCCESS; |
| 548 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 549 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 550 | //------------------------------------------------------------ |
| 551 | // Cleanup routine |
| 552 | //------------------------------------------------------------ |
| 553 | int cleanup_error(FILE *fru_fp, fru_area_vec_t & fru_area_vec) |
| 554 | { |
| 555 | if(fru_fp != NULL) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 556 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 557 | fclose(fru_fp); |
| 558 | fru_fp = NULL; |
| 559 | } |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 560 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 561 | if(!(fru_area_vec.empty())) |
| 562 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 563 | fru_area_vec.clear(); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 564 | } |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 565 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 566 | return -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 567 | } |
| 568 | |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 569 | |
| 570 | ///----------------------------------------------------- |
| 571 | // Get the fru area names defined in BMC for a given @fruid. |
| 572 | //---------------------------------------------------- |
| 573 | int 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 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 636 | ///----------------------------------------------------- |
| 637 | // Accepts the filename and validates per IPMI FRU spec |
| 638 | //---------------------------------------------------- |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 639 | int ipmi_validate_fru_area(const uint8_t fruid, const char *fru_file_name, |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 640 | sd_bus *bus_type, const bool bmc_fru) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 641 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 642 | size_t data_len = 0; |
| 643 | size_t bytes_read = 0; |
| 644 | int rc = -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 645 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 646 | // 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 Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 649 | 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 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 658 | 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 |
vishwa | 2f5a3cf | 2016-05-30 02:25:21 -0500 | [diff] [blame] | 666 | bool present = access(fru_file_name, F_OK) == 0; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 667 | fru_area->set_present(present); |
| 668 | |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 669 | // 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 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 678 | 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) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 683 | { |
| 684 | fprintf(stderr, "ERROR: opening:[%s]\n",fru_file_name); |
| 685 | perror("Error:"); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 686 | return cleanup_error(fru_fp, fru_area_vec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 687 | } |
| 688 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 689 | // Get the size of the file to see if it meets minimum requirement |
| 690 | if(fseek(fru_fp, 0, SEEK_END)) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 691 | { |
| 692 | perror("Error:"); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 693 | return cleanup_error(fru_fp, fru_area_vec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 694 | } |
| 695 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 696 | // Allocate a buffer to hold entire file content |
| 697 | data_len = ftell(fru_fp); |
| 698 | uint8_t fru_data[data_len] = {0}; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 699 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 700 | rewind(fru_fp); |
| 701 | bytes_read = fread(fru_data, data_len, 1, fru_fp); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 702 | if(bytes_read != 1) |
| 703 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 704 | fprintf(stderr, "Failed reading fru data. Bytes_read=[%d]\n",bytes_read); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 705 | perror("Error:"); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 706 | return cleanup_error(fru_fp, fru_area_vec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 707 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 708 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 709 | // We are done reading. |
| 710 | fclose(fru_fp); |
| 711 | fru_fp = NULL; |
| 712 | |
| 713 | rc = ipmi_validate_common_hdr(fru_data, data_len); |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 714 | if(rc < 0) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 715 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 716 | 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); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 725 | } |
| 726 | else |
| 727 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 728 | printf("SUCCESS: Populated FRU areas for:[%s]\n",fru_file_name); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 729 | } |
| 730 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 731 | #ifdef __IPMI_DEBUG__ |
| 732 | for(auto& iter : fru_area_vec) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 733 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 734 | 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()); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 741 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 742 | #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(); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 762 | |
| 763 | return rc; |
| 764 | } |