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