Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 1 | #include <exception> |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 2 | #include <vector> |
| 3 | #include <stdlib.h> |
| 4 | #include <dlfcn.h> |
| 5 | #include <errno.h> |
| 6 | #include <stdio.h> |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 7 | #include <systemd/sd-bus.h> |
Chris Austen | b45c4cb | 2015-11-01 06:34:56 -0600 | [diff] [blame] | 8 | #include <unistd.h> |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 9 | #include <host-ipmid/ipmid-api.h> |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 10 | #include <iostream> |
| 11 | #include <memory> |
| 12 | #include <algorithm> |
| 13 | #include <fstream> |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 14 | #include <sstream> |
Brad Bishop | de6a379 | 2016-07-25 17:09:12 -0400 | [diff] [blame] | 15 | #include <mapper.h> |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame^] | 16 | #include "types.hpp" |
Ratan Gupta | 19c617b | 2017-02-10 15:39:40 +0530 | [diff] [blame] | 17 | #include "frup.hpp" |
Matthew Barth | 155c34f | 2016-10-18 14:33:17 -0500 | [diff] [blame] | 18 | #include "fru-area.hpp" |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 19 | #include <sdbusplus/server.hpp> |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 20 | |
| 21 | // OpenBMC System Manager dbus framework |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 22 | const char *sys_object_name = "/org/openbmc/managers/System"; |
| 23 | const char *sys_intf_name = "org.openbmc.managers.System"; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 24 | |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame^] | 25 | using namespace ipmi::vpd; |
| 26 | |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 27 | extern const FruMap frus; |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame^] | 28 | extern const std::map<Path, InterfaceMap> extras; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 29 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 30 | //---------------------------------------------------------------- |
| 31 | // Constructor |
| 32 | //---------------------------------------------------------------- |
| 33 | ipmi_fru::ipmi_fru(const uint8_t fruid, const ipmi_fru_area_type type, |
| 34 | sd_bus *bus_type, bool bmc_fru) |
| 35 | { |
| 36 | iv_fruid = fruid; |
| 37 | iv_type = type; |
| 38 | iv_bmc_fru = bmc_fru; |
| 39 | iv_bus_type = bus_type; |
| 40 | iv_valid = false; |
| 41 | iv_data = NULL; |
| 42 | iv_present = false; |
| 43 | |
| 44 | if(iv_type == IPMI_FRU_AREA_INTERNAL_USE) |
| 45 | { |
| 46 | iv_name = "INTERNAL_"; |
| 47 | } |
| 48 | else if(iv_type == IPMI_FRU_AREA_CHASSIS_INFO) |
| 49 | { |
| 50 | iv_name = "CHASSIS_"; |
| 51 | } |
| 52 | else if(iv_type == IPMI_FRU_AREA_BOARD_INFO) |
| 53 | { |
| 54 | iv_name = "BOARD_"; |
| 55 | } |
| 56 | else if(iv_type == IPMI_FRU_AREA_PRODUCT_INFO) |
| 57 | { |
| 58 | iv_name = "PRODUCT_"; |
| 59 | } |
| 60 | else if(iv_type == IPMI_FRU_AREA_MULTI_RECORD) |
| 61 | { |
| 62 | iv_name = "MULTI_"; |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | iv_name = IPMI_FRU_AREA_TYPE_MAX; |
| 67 | fprintf(stderr, "ERROR: Invalid Area type :[%d]\n",iv_type); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | //----------------------------------------------------- |
| 72 | // For a FRU area type, accepts the data and updates |
| 73 | // area specific data. |
| 74 | //----------------------------------------------------- |
| 75 | void ipmi_fru::set_data(const uint8_t *data, const size_t len) |
| 76 | { |
| 77 | iv_len = len; |
| 78 | iv_data = new uint8_t[len]; |
| 79 | memcpy(iv_data, data, len); |
| 80 | } |
| 81 | |
| 82 | //----------------------------------------------------- |
| 83 | // Sets the dbus parameters |
| 84 | //----------------------------------------------------- |
| 85 | void ipmi_fru::update_dbus_paths(const char *bus_name, |
| 86 | const char *obj_path, const char *intf_name) |
| 87 | { |
| 88 | iv_bus_name = bus_name; |
| 89 | iv_obj_path = obj_path; |
| 90 | iv_intf_name = intf_name; |
| 91 | } |
| 92 | |
| 93 | //------------------- |
| 94 | // Destructor |
| 95 | //------------------- |
| 96 | ipmi_fru::~ipmi_fru() |
| 97 | { |
| 98 | sd_bus_error bus_error = SD_BUS_ERROR_NULL; |
| 99 | sd_bus_message *response = NULL; |
| 100 | int rc = 0; |
| 101 | |
| 102 | if(iv_data != NULL) |
| 103 | { |
| 104 | delete [] iv_data; |
| 105 | iv_data = NULL; |
| 106 | } |
| 107 | |
| 108 | // If we have not been successful in doing some updates and we are a BMC |
| 109 | // fru, then need to set the fault bits. |
| 110 | bool valid_dbus = !(iv_bus_name.empty()) && |
| 111 | !(iv_obj_path.empty()) && |
| 112 | !(iv_intf_name.empty()); |
| 113 | |
| 114 | // Based on bmc_fru, success in updating the FRU inventory we need to set |
| 115 | // some special bits. |
| 116 | if(iv_bmc_fru && valid_dbus) |
| 117 | { |
| 118 | // Set the Fault bit if we did not successfully process the fru |
| 119 | const char *fault_bit = iv_valid ? "False" : "True"; |
| 120 | |
| 121 | rc = sd_bus_call_method(iv_bus_type, // On the System Bus |
| 122 | iv_bus_name.c_str(), // Service to contact |
| 123 | iv_obj_path.c_str(), // Object path |
| 124 | iv_intf_name.c_str(), // Interface name |
| 125 | "setFault", // Method to be called |
| 126 | &bus_error, // object to return error |
| 127 | &response, // Response message on success |
| 128 | "s", // input message (string) |
| 129 | fault_bit); // First argument to setFault |
| 130 | |
| 131 | if(rc <0) |
| 132 | { |
| 133 | fprintf(stderr,"Failed to set Fault bit, value:[%s] for fruid:[%d], path:[%s]\n", |
| 134 | fault_bit, iv_fruid, iv_obj_path.c_str()); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | printf("Fault bit set to :[%s] for fruid:[%d], Path:[%s]\n", |
| 139 | fault_bit, iv_fruid,iv_obj_path.c_str()); |
| 140 | } |
| 141 | |
| 142 | sd_bus_error_free(&bus_error); |
| 143 | sd_bus_message_unref(response); |
| 144 | |
| 145 | // Set the Present bits |
| 146 | const char *present_bit = iv_present ? "True" : "False"; |
| 147 | |
| 148 | rc = sd_bus_call_method(iv_bus_type, // On the System Bus |
| 149 | iv_bus_name.c_str(), // Service to contact |
| 150 | iv_obj_path.c_str(), // Object path |
| 151 | iv_intf_name.c_str(), // Interface name |
| 152 | "setPresent", // Method to be called |
| 153 | &bus_error, // object to return error |
| 154 | &response, // Response message on success |
| 155 | "s", // input message (string) |
| 156 | present_bit); // First argument to setPresent |
| 157 | if(rc < 0) |
| 158 | { |
| 159 | fprintf(stderr,"Failed to set Present bit for fruid:[%d], path:[%s]\n", |
| 160 | iv_fruid, iv_obj_path.c_str()); |
| 161 | } |
| 162 | else |
| 163 | { |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 164 | printf("Present bit set to :[%s] for fruid:[%d], Path[%s]:\n", |
| 165 | present_bit, iv_fruid, iv_obj_path.c_str()); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | sd_bus_error_free(&bus_error); |
| 169 | sd_bus_message_unref(response); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Sets up the sd_bus structures for the given fru type |
| 174 | int ipmi_fru::setup_sd_bus_paths(void) |
| 175 | { |
| 176 | // Need this to get respective DBUS objects |
| 177 | sd_bus_error bus_error = SD_BUS_ERROR_NULL; |
| 178 | sd_bus_message *response = NULL; |
| 179 | int rc = 0; |
| 180 | |
| 181 | // What we need is BOARD_1, PRODUCT_1, CHASSIS_1 etc.. |
Patrick Williams | 867da97 | 2016-08-17 17:40:05 -0500 | [diff] [blame] | 182 | char *inv_bus_name = NULL; |
| 183 | const char *inv_obj_path = NULL, |
Patrick Williams | 804077d | 2016-07-29 15:26:18 -0500 | [diff] [blame] | 184 | *inv_intf_name = NULL; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 185 | char fru_area_name[16] = {0}; |
Brad Bishop | de6a379 | 2016-07-25 17:09:12 -0400 | [diff] [blame] | 186 | char *sys_bus_name = NULL; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 187 | sprintf(fru_area_name,"%s%d",iv_name.c_str(), iv_fruid); |
| 188 | |
| 189 | #ifdef __IPMI_DEBUG__ |
| 190 | printf("Getting sd_bus for :[%s]\n",fru_area_name); |
| 191 | #endif |
| 192 | |
Brad Bishop | de6a379 | 2016-07-25 17:09:12 -0400 | [diff] [blame] | 193 | rc = mapper_get_service(iv_bus_type, sys_object_name, &sys_bus_name); |
| 194 | if(rc < 0) |
| 195 | { |
| 196 | fprintf(stderr, "Failed to get system manager service:[%s]\n", |
| 197 | strerror(-rc)); |
| 198 | goto exit; |
| 199 | } |
| 200 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 201 | // We want to call a method "getObjectFromId" on System Bus that is |
| 202 | // made available over OpenBmc system services. |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 203 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 204 | rc = sd_bus_call_method(iv_bus_type, // On the System Bus |
| 205 | sys_bus_name, // Service to contact |
| 206 | sys_object_name, // Object path |
| 207 | sys_intf_name, // Interface name |
| 208 | "getObjectFromId", // Method to be called |
| 209 | &bus_error, // object to return error |
| 210 | &response, // Response message on success |
| 211 | "ss", // input message (string,string) |
| 212 | "FRU_STR", // First argument to getObjectFromId |
| 213 | fru_area_name); // Second Argument |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 214 | if(rc < 0) |
| 215 | { |
| 216 | fprintf(stderr, "Failed to resolve fruid:[%d] to dbus: [%s]\n", iv_fruid, bus_error.message); |
| 217 | } |
| 218 | else |
| 219 | { |
Brad Bishop | de6a379 | 2016-07-25 17:09:12 -0400 | [diff] [blame] | 220 | // Method getObjectFromId returns 2 parameters and all are strings, namely |
| 221 | // object_path and interface name for accessing that particular |
| 222 | // FRU over Inventory SDBUS manager. 'ss' here mentions that format. |
| 223 | rc = sd_bus_message_read(response, "(ss)", &inv_obj_path, &inv_intf_name); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 224 | if(rc < 0) |
| 225 | { |
| 226 | fprintf(stderr, "Failed to parse response message:[%s]\n", strerror(-rc)); |
| 227 | } |
| 228 | else |
| 229 | { |
Brad Bishop | de6a379 | 2016-07-25 17:09:12 -0400 | [diff] [blame] | 230 | rc = mapper_get_service(iv_bus_type, inv_obj_path, &inv_bus_name); |
| 231 | if(rc < 0) |
| 232 | { |
| 233 | fprintf(stderr, "Failed to get inventory item service:[%s]\n", |
| 234 | strerror(-rc)); |
| 235 | goto exit; |
| 236 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 237 | // Update the paths in the area object |
| 238 | update_dbus_paths(inv_bus_name, inv_obj_path, inv_intf_name); |
| 239 | } |
| 240 | } |
| 241 | |
Brad Bishop | de6a379 | 2016-07-25 17:09:12 -0400 | [diff] [blame] | 242 | exit: |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 243 | #ifdef __IPMI_DEBUG__ |
| 244 | printf("fru_area=[%s], inv_bus_name=[%s], inv_obj_path=[%s], inv_intf_name=[%s]\n", |
| 245 | fru_area_name, inv_bus_name, inv_obj_path, inv_intf_name); |
| 246 | #endif |
| 247 | |
Brad Bishop | de6a379 | 2016-07-25 17:09:12 -0400 | [diff] [blame] | 248 | free(sys_bus_name); |
Patrick Williams | 867da97 | 2016-08-17 17:40:05 -0500 | [diff] [blame] | 249 | free(inv_bus_name); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 250 | sd_bus_error_free(&bus_error); |
| 251 | sd_bus_message_unref(response); |
| 252 | |
| 253 | return rc; |
| 254 | } |
| 255 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 256 | //------------------------------------------------ |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 257 | // Takes the pointer to stream of bytes and length |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 258 | // and returns the 8 bit checksum |
| 259 | // This algo is per IPMI V2.0 spec |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 260 | //------------------------------------------------- |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 261 | unsigned char calculate_crc(const unsigned char *data, size_t len) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 262 | { |
| 263 | char crc = 0; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 264 | size_t byte = 0; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 265 | |
| 266 | for(byte = 0; byte < len; byte++) |
| 267 | { |
| 268 | crc += *data++; |
| 269 | } |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 270 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 271 | return(-crc); |
| 272 | } |
| 273 | |
| 274 | //--------------------------------------------------------------------- |
| 275 | // Accepts a fru area offset in commom hdr and tells which area it is. |
| 276 | //--------------------------------------------------------------------- |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 277 | ipmi_fru_area_type get_fru_area_type(uint8_t area_offset) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 278 | { |
| 279 | ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX; |
| 280 | |
| 281 | switch(area_offset) |
| 282 | { |
| 283 | case IPMI_FRU_INTERNAL_OFFSET: |
| 284 | type = IPMI_FRU_AREA_INTERNAL_USE; |
| 285 | break; |
| 286 | |
| 287 | case IPMI_FRU_CHASSIS_OFFSET: |
| 288 | type = IPMI_FRU_AREA_CHASSIS_INFO; |
| 289 | break; |
| 290 | |
| 291 | case IPMI_FRU_BOARD_OFFSET: |
| 292 | type = IPMI_FRU_AREA_BOARD_INFO; |
| 293 | break; |
| 294 | |
| 295 | case IPMI_FRU_PRODUCT_OFFSET: |
| 296 | type = IPMI_FRU_AREA_PRODUCT_INFO; |
| 297 | break; |
| 298 | |
| 299 | case IPMI_FRU_MULTI_OFFSET: |
| 300 | type = IPMI_FRU_AREA_MULTI_RECORD; |
| 301 | break; |
| 302 | |
| 303 | default: |
| 304 | type = IPMI_FRU_AREA_TYPE_MAX; |
| 305 | } |
| 306 | |
| 307 | return type; |
| 308 | } |
| 309 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 310 | ///----------------------------------------------- |
| 311 | // Validates the data for crc and mandatory fields |
| 312 | ///----------------------------------------------- |
| 313 | int verify_fru_data(const uint8_t *data, const size_t len) |
| 314 | { |
| 315 | uint8_t checksum = 0; |
| 316 | int rc = -1; |
| 317 | |
| 318 | // Validate for first byte to always have a value of [1] |
| 319 | if(data[0] != IPMI_FRU_HDR_BYTE_ZERO) |
| 320 | { |
| 321 | fprintf(stderr, "Invalid entry:[%d] in byte-0\n",data[0]); |
| 322 | return rc; |
| 323 | } |
| 324 | #ifdef __IPMI_DEBUG__ |
| 325 | else |
| 326 | { |
| 327 | printf("SUCCESS: Validated [0x%X] in entry_1 of fru_data\n",data[0]); |
| 328 | } |
| 329 | #endif |
| 330 | |
| 331 | // See if the calculated CRC matches with the embedded one. |
| 332 | // CRC to be calculated on all except the last one that is CRC itself. |
| 333 | checksum = calculate_crc(data, len - 1); |
| 334 | if(checksum != data[len-1]) |
| 335 | { |
| 336 | #ifdef __IPMI_DEBUG__ |
| 337 | fprintf(stderr, "Checksum mismatch." |
| 338 | " Calculated:[0x%X], Embedded:[0x%X]\n", |
| 339 | checksum, data[len]); |
| 340 | #endif |
| 341 | return rc; |
| 342 | } |
| 343 | #ifdef __IPMI_DEBUG__ |
| 344 | else |
| 345 | { |
| 346 | printf("SUCCESS: Checksum matches:[0x%X]\n",checksum); |
| 347 | } |
| 348 | #endif |
| 349 | |
| 350 | return EXIT_SUCCESS; |
| 351 | } |
| 352 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 353 | //------------------------------------------------------------------------ |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 354 | // Gets the value of the key from the fru dictionary of the given section. |
| 355 | // FRU dictionary is parsed fru data for all the sections. |
| 356 | //------------------------------------------------------------------------ |
| 357 | |
| 358 | std::string getFRUValue(const std::string& section, |
| 359 | const std::string& key, |
Ratan Gupta | cdde23d | 2017-02-15 09:29:54 +0530 | [diff] [blame] | 360 | const std::string& delimiter, |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 361 | IPMIFruInfo& fruData) |
| 362 | { |
| 363 | |
| 364 | auto minIndexValue = 0; |
| 365 | auto maxIndexValue = 0; |
| 366 | std::string fruValue = ""; |
| 367 | if (section == "Board") |
| 368 | { |
| 369 | minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE; |
| 370 | maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX; |
| 371 | } |
| 372 | else if (section == "Product") |
| 373 | { |
| 374 | minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR; |
| 375 | maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX; |
| 376 | |
| 377 | } |
| 378 | else if (section == "Chassis") |
| 379 | { |
| 380 | minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE; |
| 381 | maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX; |
| 382 | } |
| 383 | |
| 384 | auto first = fruData.cbegin() + minIndexValue; |
| 385 | auto last = first + (maxIndexValue - minIndexValue) + 1; |
| 386 | |
| 387 | auto itr = std::find_if(first, last, |
| 388 | [&key](auto& e){ return key == e.first; }); |
| 389 | |
| 390 | if (itr != last) |
| 391 | { |
| 392 | fruValue = itr->second; |
| 393 | } |
Ratan Gupta | cdde23d | 2017-02-15 09:29:54 +0530 | [diff] [blame] | 394 | |
| 395 | //if the key is custom property then the value could be in two formats. |
| 396 | //1) custom field 2 = "value". |
| 397 | //2) custom field 2 = "key:value". |
| 398 | //if delimeter length = 0 i.e custom field 2 = "value" |
| 399 | |
| 400 | constexpr auto customProp = "Custom Field"; |
| 401 | if (key.find(customProp) != std::string::npos) |
| 402 | { |
| 403 | if (delimiter.length() > 0) |
| 404 | { |
| 405 | size_t delimiterpos = fruValue.find(delimiter); |
| 406 | if (delimiterpos != std::string::npos) |
| 407 | fruValue = fruValue.substr(delimiterpos + 1); |
| 408 | } |
| 409 | } |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 410 | return fruValue; |
| 411 | |
| 412 | } |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 413 | //Get the inventory service from the mapper. |
| 414 | auto getService(sdbusplus::bus::bus& bus, |
| 415 | const std::string& intf, |
| 416 | const std::string& path) |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 417 | { |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 418 | auto mapperCall = bus.new_method_call( |
| 419 | "xyz.openbmc_project.ObjectMapper", |
| 420 | "/xyz/openbmc_project/ObjectMapper", |
| 421 | "xyz.openbmc_project.ObjectMapper", |
| 422 | "GetObject"); |
| 423 | |
| 424 | mapperCall.append(path); |
| 425 | mapperCall.append(std::vector<std::string>({intf})); |
| 426 | |
| 427 | auto mapperResponseMsg = bus.call(mapperCall); |
| 428 | if (mapperResponseMsg.is_method_error()) |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 429 | { |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 430 | throw std::runtime_error("ERROR in mapper call"); |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 431 | } |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 432 | |
| 433 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 434 | mapperResponseMsg.read(mapperResponse); |
| 435 | |
| 436 | if (mapperResponse.begin() == mapperResponse.end()) |
| 437 | { |
| 438 | throw std::runtime_error("ERROR in reading the mapper response"); |
| 439 | } |
| 440 | |
| 441 | return mapperResponse.begin()->first; |
| 442 | } |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 443 | |
| 444 | //------------------------------------------------------------------------ |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 445 | // Takes FRU data, invokes Parser for each fru record area and updates |
| 446 | // Inventory |
| 447 | //------------------------------------------------------------------------ |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 448 | int ipmi_update_inventory(fru_area_vec_t& area_vec) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 449 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 450 | // Generic error reporter |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 451 | int rc = 0; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 452 | uint8_t fruid = 0; |
| 453 | IPMIFruInfo fruData; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 454 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 455 | // For each FRU area, extract the needed data , get it parsed and update |
| 456 | // the Inventory. |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 457 | for (const auto& fruArea : area_vec) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 458 | { |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 459 | fruid = fruArea->get_fruid(); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 460 | // Fill the container with information |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 461 | rc = parse_fru_area((fruArea)->get_type(), (void*)(fruArea)->get_data(), |
| 462 | (fruArea)->get_len(), fruData); |
| 463 | if (rc < 0) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 464 | { |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 465 | std::cerr << "ERROR parsing FRU records\n"; |
| 466 | return rc; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 467 | } |
| 468 | } // END walking the vector of areas and updating |
| 469 | |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 470 | // For each Fru we have the list of instances which needs to be updated. |
| 471 | // Each instance object implements certain interfaces. |
| 472 | // Each Interface is having Dbus properties. |
| 473 | // Each Dbus Property would be having metaData(eg section,VpdPropertyName). |
| 474 | |
| 475 | // Here we are just printing the object,interface and the properties. |
| 476 | // which needs to be called with the new inventory manager implementation. |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 477 | auto bus = sdbusplus::bus::new_default(); |
| 478 | using namespace std::string_literals; |
| 479 | static const auto intf = "xyz.openbmc_project.Inventory.Manager"s; |
| 480 | static const auto path = "/xyz/openbmc_project/Inventory"s; |
| 481 | std::string service; |
| 482 | try |
| 483 | { |
| 484 | service = getService(bus,intf,path); |
| 485 | } |
| 486 | catch (const std::runtime_error& e) |
| 487 | { |
| 488 | std::cerr << e.what() << "\n"; |
| 489 | return -1; |
| 490 | } |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 491 | |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 492 | auto iter = frus.find(fruid); |
| 493 | if (iter == frus.end()) |
| 494 | { |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 495 | std::cerr << "ERROR Unable to get the fru info for FRU=" |
| 496 | << static_cast<int>(fruid) << "\n"; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 497 | return -1; |
| 498 | } |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 499 | |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 500 | auto& instanceList = iter->second; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 501 | if (instanceList.size() <= 0) |
| 502 | { |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 503 | std::cout << "Object List empty for this FRU=" |
| 504 | << static_cast<int>(fruid) << "\n"; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 505 | } |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 506 | |
| 507 | ObjectMap objects; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 508 | for (auto& instance : instanceList) |
| 509 | { |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 510 | InterfaceMap interfaces; |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame^] | 511 | const auto& extrasIter = extras.find(instance.first); |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 512 | |
| 513 | for (auto& interfaceList : instance.second) |
| 514 | { |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 515 | PropertyMap props;//store all the properties |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 516 | for (auto& properties : interfaceList.second) |
| 517 | { |
Ratan Gupta | cdde23d | 2017-02-15 09:29:54 +0530 | [diff] [blame] | 518 | std::string section, property, delimiter, value; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 519 | for (auto& info : properties.second) |
| 520 | { |
| 521 | if (info.first == "IPMIFruSection") |
| 522 | { |
| 523 | section = std::move(info.second); |
| 524 | } |
| 525 | if (info.first == "IPMIFruProperty") |
| 526 | { |
| 527 | property = std::move(info.second); |
| 528 | } |
Ratan Gupta | cdde23d | 2017-02-15 09:29:54 +0530 | [diff] [blame] | 529 | if (info.first == "IPMIFruValueDelimiter") |
| 530 | { |
| 531 | //Read the delimeter as ascii value |
| 532 | //convert it into char |
| 533 | if( info.second.length() > 0 ) |
| 534 | { |
| 535 | char dlm = ' '; |
| 536 | rc = sscanf(info.second.c_str(),"%hhd",&dlm); |
| 537 | if (rc > 0) |
| 538 | { |
| 539 | delimiter = std::string(1,dlm); |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | if (!section.empty() && !property.empty()) |
| 547 | { |
Ratan Gupta | cdde23d | 2017-02-15 09:29:54 +0530 | [diff] [blame] | 548 | value = getFRUValue(section, property, delimiter, fruData); |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 549 | } |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 550 | props.emplace(std::move(properties.first), std::move(value)); |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 551 | } |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame^] | 552 | // Check and update extra properties |
| 553 | if(extras.end() != extrasIter) |
| 554 | { |
| 555 | const auto& propsIter = |
| 556 | (extrasIter->second).find(interfaceList.first); |
| 557 | if((extrasIter->second).end() != propsIter) |
| 558 | { |
| 559 | for(const auto& map : propsIter->second) |
| 560 | { |
| 561 | props.emplace(map.first, map.second); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | interfaces.emplace(std::move(interfaceList.first), |
| 566 | std::move(props)); |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 567 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 568 | |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame^] | 569 | // Call the inventory manager |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 570 | sdbusplus::message::object_path path = instance.first; |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame^] | 571 | // Check and update extra properties |
| 572 | if(extras.end() != extrasIter) |
| 573 | { |
| 574 | for(const auto& entry : extrasIter->second) |
| 575 | { |
| 576 | if(interfaces.end() == interfaces.find(entry.first)) |
| 577 | { |
| 578 | interfaces.emplace(entry.first, entry.second); |
| 579 | } |
| 580 | } |
| 581 | } |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 582 | objects.emplace(path,interfaces); |
| 583 | } |
| 584 | |
| 585 | auto pimMsg = bus.new_method_call( |
| 586 | service.c_str(), |
| 587 | path.c_str(), |
| 588 | intf.c_str(), |
| 589 | "Notify"); |
| 590 | pimMsg.append(std::move(objects)); |
| 591 | auto inventoryMgrResponseMsg = bus.call(pimMsg); |
| 592 | if (inventoryMgrResponseMsg.is_method_error()) |
| 593 | { |
| 594 | std::cerr << "Error in notify call\n"; |
| 595 | return -1; |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 596 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 597 | return rc; |
| 598 | } |
| 599 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 600 | ///---------------------------------------------------- |
| 601 | // Checks if a particular fru area is populated or not |
| 602 | ///---------------------------------------------------- |
| 603 | bool remove_invalid_area(const std::unique_ptr<ipmi_fru> &fru_area) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 604 | { |
Deepak Kodihalli | 89ededd | 2017-02-11 01:59:32 -0600 | [diff] [blame] | 605 | // Filter the ones that are empty |
| 606 | if(!(fru_area->get_len())) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 607 | { |
| 608 | return true; |
| 609 | } |
| 610 | return false; |
| 611 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 612 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 613 | ///---------------------------------------------------------------------------------- |
| 614 | // Populates various FRU areas |
| 615 | // @prereq : This must be called only after validating common header. |
| 616 | ///---------------------------------------------------------------------------------- |
| 617 | int ipmi_populate_fru_areas(uint8_t *fru_data, const size_t data_len, |
| 618 | fru_area_vec_t & fru_area_vec) |
| 619 | { |
vishwa | 13555bd | 2015-11-10 12:10:38 -0600 | [diff] [blame] | 620 | size_t area_offset = 0; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 621 | int rc = -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 622 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 623 | // Now walk the common header and see if the file size has atleast the last |
| 624 | // offset mentioned by the common_hdr. If the file size is less than the |
| 625 | // offset of any if the fru areas mentioned in the common header, then we do |
| 626 | // not have a complete file. |
| 627 | for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET; |
| 628 | fru_entry < (sizeof(struct common_header) -2); fru_entry++) |
| 629 | { |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 630 | rc = -1; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 631 | // Actual offset in the payload is the offset mentioned in common header |
| 632 | // multipled by 8. Common header is always the first 8 bytes. |
| 633 | area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES; |
| 634 | if(area_offset && (data_len < (area_offset + 2))) |
| 635 | { |
| 636 | // Our file size is less than what it needs to be. +2 because we are |
| 637 | // using area len that is at 2 byte off area_offset |
Patrick Williams | 3365ec8 | 2016-08-17 17:45:18 -0500 | [diff] [blame] | 638 | fprintf(stderr, "fru file is incomplete. Size:[%zd]\n",data_len); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 639 | return rc; |
| 640 | } |
| 641 | else if(area_offset) |
| 642 | { |
| 643 | // Read 2 bytes to know the actual size of area. |
| 644 | uint8_t area_hdr[2] = {0}; |
| 645 | memcpy(area_hdr, &((uint8_t *)fru_data)[area_offset], sizeof(area_hdr)); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 646 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 647 | // Size of this area will be the 2nd byte in the fru area header. |
| 648 | size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES; |
| 649 | uint8_t area_data[area_len] = {0}; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 650 | |
Patrick Williams | 3365ec8 | 2016-08-17 17:45:18 -0500 | [diff] [blame] | 651 | printf("fru data size:[%zd], area offset:[%zd], area_size:[%zd]\n", |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 652 | data_len, area_offset, area_len); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 653 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 654 | // See if we really have that much buffer. We have area offset amd |
| 655 | // from there, the actual len. |
| 656 | if(data_len < (area_len + area_offset)) |
| 657 | { |
Patrick Williams | 3365ec8 | 2016-08-17 17:45:18 -0500 | [diff] [blame] | 658 | fprintf(stderr, "Incomplete Fru file.. Size:[%zd]\n",data_len); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 659 | return rc; |
| 660 | } |
| 661 | |
| 662 | // Save off the data. |
| 663 | memcpy(area_data, &((uint8_t *)fru_data)[area_offset], area_len); |
| 664 | |
| 665 | // Validate the crc |
| 666 | rc = verify_fru_data(area_data, area_len); |
| 667 | if(rc < 0) |
| 668 | { |
Patrick Williams | 3365ec8 | 2016-08-17 17:45:18 -0500 | [diff] [blame] | 669 | fprintf(stderr, "Error validating fru area. offset:[%zd]\n",area_offset); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 670 | return rc; |
| 671 | } |
| 672 | else |
| 673 | { |
Patrick Williams | 3365ec8 | 2016-08-17 17:45:18 -0500 | [diff] [blame] | 674 | printf("Successfully verified area checksum. offset:[%zd]\n",area_offset); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | // We already have a vector that is passed to us containing all |
| 678 | // of the fields populated. Update the data portion now. |
| 679 | for(auto& iter : fru_area_vec) |
| 680 | { |
| 681 | if((iter)->get_type() == get_fru_area_type(fru_entry)) |
| 682 | { |
| 683 | (iter)->set_data(area_data, area_len); |
| 684 | } |
| 685 | } |
| 686 | } // If we have fru data present |
| 687 | } // Walk common_hdr |
| 688 | |
| 689 | // Not all the fields will be populated in a fru data. Mostly all cases will |
| 690 | // not have more than 2 or 3. |
| 691 | fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(), |
| 692 | remove_invalid_area), fru_area_vec.end()); |
| 693 | |
| 694 | return EXIT_SUCCESS; |
| 695 | } |
| 696 | |
| 697 | ///--------------------------------------------------------- |
| 698 | // Validates the fru data per ipmi common header constructs. |
| 699 | // Returns with updated common_hdr and also file_size |
| 700 | //---------------------------------------------------------- |
| 701 | int ipmi_validate_common_hdr(const uint8_t *fru_data, const size_t data_len) |
| 702 | { |
| 703 | int rc = -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 704 | |
| 705 | uint8_t common_hdr[sizeof(struct common_header)] = {0}; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 706 | if(data_len >= sizeof(common_hdr)) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 707 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 708 | memcpy(common_hdr, fru_data, sizeof(common_hdr)); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 709 | } |
| 710 | else |
| 711 | { |
Patrick Williams | 3365ec8 | 2016-08-17 17:45:18 -0500 | [diff] [blame] | 712 | fprintf(stderr, "Incomplete fru data file. Size:[%zd]\n", data_len); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 713 | return rc; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 714 | } |
| 715 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 716 | // Verify the crc and size |
| 717 | rc = verify_fru_data(common_hdr, sizeof(common_hdr)); |
| 718 | if(rc < 0) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 719 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 720 | fprintf(stderr, "Failed to validate common header\n"); |
| 721 | return rc; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 722 | } |
| 723 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 724 | return EXIT_SUCCESS; |
| 725 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 726 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 727 | //------------------------------------------------------------ |
| 728 | // Cleanup routine |
| 729 | //------------------------------------------------------------ |
| 730 | int cleanup_error(FILE *fru_fp, fru_area_vec_t & fru_area_vec) |
| 731 | { |
| 732 | if(fru_fp != NULL) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 733 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 734 | fclose(fru_fp); |
| 735 | fru_fp = NULL; |
| 736 | } |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 737 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 738 | if(!(fru_area_vec.empty())) |
| 739 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 740 | fru_area_vec.clear(); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 741 | } |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 742 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 743 | return -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 744 | } |
| 745 | |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 746 | |
| 747 | ///----------------------------------------------------- |
| 748 | // Get the fru area names defined in BMC for a given @fruid. |
| 749 | //---------------------------------------------------- |
| 750 | int get_defined_fru_area(sd_bus *bus_type, const uint8_t fruid, |
| 751 | std::vector<std::string> &defined_fru_area) |
| 752 | { |
| 753 | // Need this to get respective DBUS objects |
| 754 | sd_bus_error bus_error = SD_BUS_ERROR_NULL; |
| 755 | sd_bus_message *response = NULL; |
| 756 | int rc = 0; |
Patrick Williams | 804077d | 2016-07-29 15:26:18 -0500 | [diff] [blame] | 757 | const char *areas = NULL; |
Brad Bishop | de6a379 | 2016-07-25 17:09:12 -0400 | [diff] [blame] | 758 | char *sys_bus_name = NULL; |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 759 | |
| 760 | #ifdef __IPMI_DEBUG__ |
| 761 | printf("Getting fru areas defined in Skeleton for :[%d]\n", fruid); |
| 762 | #endif |
| 763 | |
Brad Bishop | de6a379 | 2016-07-25 17:09:12 -0400 | [diff] [blame] | 764 | rc = mapper_get_service(bus_type, sys_object_name, &sys_bus_name); |
| 765 | if(rc < 0) |
| 766 | { |
| 767 | fprintf(stderr, "Failed to get system manager service:[%s]\n", |
| 768 | strerror(-rc)); |
| 769 | goto exit; |
| 770 | } |
| 771 | |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 772 | // We want to call a method "getFRUArea" on System Bus that is |
| 773 | // made available over OpenBmc system services. |
| 774 | rc = sd_bus_call_method(bus_type, // On the System Bus |
| 775 | sys_bus_name, // Service to contact |
| 776 | sys_object_name, // Object path |
| 777 | sys_intf_name, // Interface name |
| 778 | "getFRUArea", // Method to be called |
| 779 | &bus_error, // object to return error |
| 780 | &response, // Response message on success |
| 781 | "y", // input message (integer) |
| 782 | fruid); // Argument |
| 783 | |
| 784 | if(rc < 0) |
| 785 | { |
| 786 | fprintf(stderr, "Failed to get fru area for fruid:[%d] to dbus: [%s]\n", |
| 787 | fruid, bus_error.message); |
| 788 | } |
| 789 | else |
| 790 | { |
| 791 | // if several fru area names are defined, the names are combined to |
| 792 | // a string seperated by ',' |
| 793 | rc = sd_bus_message_read(response, "s", &areas); |
| 794 | if(rc < 0) |
| 795 | { |
| 796 | fprintf(stderr, "Failed to parse response message from getFRUArea:[%s]\n", |
| 797 | strerror(-rc)); |
| 798 | } |
| 799 | else |
| 800 | { |
| 801 | #ifdef __IPMI_DEBUG__ |
| 802 | printf("get defined fru area: id: %d, areas: %s\n", fruid, areas); |
| 803 | #endif |
| 804 | std::string area_name; |
| 805 | std::stringstream ss(areas); |
| 806 | // fru area names string is seperated by ',', parse it into tokens |
| 807 | while (std::getline(ss, area_name, ',')) |
| 808 | { |
| 809 | if (!area_name.empty()) |
| 810 | defined_fru_area.emplace_back(area_name); |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
Brad Bishop | de6a379 | 2016-07-25 17:09:12 -0400 | [diff] [blame] | 815 | exit: |
| 816 | |
| 817 | free(sys_bus_name); |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 818 | sd_bus_error_free(&bus_error); |
| 819 | sd_bus_message_unref(response); |
| 820 | |
| 821 | return rc; |
| 822 | } |
| 823 | |
| 824 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 825 | ///----------------------------------------------------- |
| 826 | // Accepts the filename and validates per IPMI FRU spec |
| 827 | //---------------------------------------------------- |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 828 | 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] | 829 | sd_bus *bus_type, const bool bmc_fru) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 830 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 831 | size_t data_len = 0; |
| 832 | size_t bytes_read = 0; |
| 833 | int rc = -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 834 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 835 | // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL |
| 836 | // are not used, keeping it here for completeness. |
| 837 | fru_area_vec_t fru_area_vec; |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 838 | std::vector<std::string> defined_fru_area; |
| 839 | |
| 840 | // BMC defines fru areas that should be present in Skeleton |
| 841 | rc = get_defined_fru_area(bus_type, fruid, defined_fru_area); |
| 842 | if(rc < 0) |
| 843 | { |
| 844 | fprintf(stderr, "ERROR: cannot get defined fru area\n"); |
| 845 | return rc; |
| 846 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 847 | for(uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET; |
| 848 | fru_entry < (sizeof(struct common_header) -2); fru_entry++) |
| 849 | { |
| 850 | // Create an object and push onto a vector. |
| 851 | std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru> |
| 852 | (fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru); |
| 853 | |
| 854 | // Physically being present |
vishwa | 2f5a3cf | 2016-05-30 02:25:21 -0500 | [diff] [blame] | 855 | bool present = access(fru_file_name, F_OK) == 0; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 856 | fru_area->set_present(present); |
| 857 | |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 858 | // Only setup dbus path for areas defined in BMC. |
| 859 | // Otherwise Skeleton will report 'not found' error |
| 860 | std::string fru_area_name = fru_area->get_name() + std::to_string(fruid); |
| 861 | auto iter = std::find(defined_fru_area.begin(), defined_fru_area.end(), |
| 862 | fru_area_name); |
| 863 | if (iter != defined_fru_area.end()) |
| 864 | { |
| 865 | fru_area->setup_sd_bus_paths(); |
| 866 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 867 | fru_area_vec.emplace_back(std::move(fru_area)); |
| 868 | } |
| 869 | |
| 870 | FILE *fru_fp = fopen(fru_file_name,"rb"); |
| 871 | if(fru_fp == NULL) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 872 | { |
| 873 | fprintf(stderr, "ERROR: opening:[%s]\n",fru_file_name); |
| 874 | perror("Error:"); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 875 | return cleanup_error(fru_fp, fru_area_vec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 876 | } |
| 877 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 878 | // Get the size of the file to see if it meets minimum requirement |
| 879 | if(fseek(fru_fp, 0, SEEK_END)) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 880 | { |
| 881 | perror("Error:"); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 882 | return cleanup_error(fru_fp, fru_area_vec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 883 | } |
| 884 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 885 | // Allocate a buffer to hold entire file content |
| 886 | data_len = ftell(fru_fp); |
| 887 | uint8_t fru_data[data_len] = {0}; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 888 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 889 | rewind(fru_fp); |
| 890 | bytes_read = fread(fru_data, data_len, 1, fru_fp); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 891 | if(bytes_read != 1) |
| 892 | { |
Patrick Williams | 3365ec8 | 2016-08-17 17:45:18 -0500 | [diff] [blame] | 893 | fprintf(stderr, "Failed reading fru data. Bytes_read=[%zd]\n",bytes_read); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 894 | perror("Error:"); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 895 | return cleanup_error(fru_fp, fru_area_vec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 896 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 897 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 898 | // We are done reading. |
| 899 | fclose(fru_fp); |
| 900 | fru_fp = NULL; |
| 901 | |
| 902 | rc = ipmi_validate_common_hdr(fru_data, data_len); |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 903 | if(rc < 0) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 904 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 905 | return cleanup_error(fru_fp, fru_area_vec); |
| 906 | } |
| 907 | |
| 908 | // Now that we validated the common header, populate various fru sections if we have them here. |
| 909 | rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec); |
| 910 | if(rc < 0) |
| 911 | { |
| 912 | fprintf(stderr,"Populating FRU areas failed for:[%d]\n",fruid); |
| 913 | return cleanup_error(fru_fp, fru_area_vec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 914 | } |
| 915 | else |
| 916 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 917 | printf("SUCCESS: Populated FRU areas for:[%s]\n",fru_file_name); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 918 | } |
| 919 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 920 | #ifdef __IPMI_DEBUG__ |
| 921 | for(auto& iter : fru_area_vec) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 922 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 923 | printf("FRU ID : [%d]\n",(iter)->get_fruid()); |
| 924 | printf("AREA NAME : [%s]\n",(iter)->get_name()); |
| 925 | printf("TYPE : [%d]\n",(iter)->get_type()); |
| 926 | printf("LEN : [%d]\n",(iter)->get_len()); |
| 927 | printf("BUS NAME : [%s]\n", (iter)->get_bus_name()); |
| 928 | printf("OBJ PATH : [%s]\n", (iter)->get_obj_path()); |
| 929 | printf("INTF NAME :[%s]\n", (iter)->get_intf_name()); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 930 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 931 | #endif |
| 932 | |
| 933 | // If the vector is populated with everything, then go ahead and update the |
| 934 | // inventory. |
| 935 | if(!(fru_area_vec.empty())) |
| 936 | { |
| 937 | |
| 938 | #ifdef __IPMI_DEBUG__ |
| 939 | printf("\n SIZE of vector is : [%d] \n",fru_area_vec.size()); |
| 940 | #endif |
| 941 | rc = ipmi_update_inventory(fru_area_vec); |
| 942 | if(rc <0) |
| 943 | { |
| 944 | fprintf(stderr, "Error updating inventory\n"); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | // we are done with all that we wanted to do. This will do the job of |
| 949 | // calling any destructors too. |
| 950 | fru_area_vec.clear(); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 951 | |
| 952 | return rc; |
| 953 | } |