Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 1 | #include "fru-area.hpp" |
| 2 | #include "frup.hpp" |
| 3 | #include "types.hpp" |
| 4 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 5 | #include <dlfcn.h> |
| 6 | #include <errno.h> |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 7 | #include <host-ipmid/ipmid-api.h> |
| 8 | #include <mapper.h> |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 9 | #include <stdio.h> |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 10 | #include <stdlib.h> |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 11 | #include <systemd/sd-bus.h> |
Chris Austen | b45c4cb | 2015-11-01 06:34:56 -0600 | [diff] [blame] | 12 | #include <unistd.h> |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 13 | |
| 14 | #include <algorithm> |
| 15 | #include <exception> |
| 16 | #include <fstream> |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 17 | #include <iostream> |
| 18 | #include <memory> |
Patrick Venture | 5739ac3 | 2018-10-16 19:17:41 -0700 | [diff] [blame] | 19 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 20 | #include <sdbusplus/server.hpp> |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 21 | #include <sstream> |
| 22 | #include <vector> |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 23 | |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame] | 24 | using namespace ipmi::vpd; |
Patrick Venture | 5739ac3 | 2018-10-16 19:17:41 -0700 | [diff] [blame] | 25 | using namespace phosphor::logging; |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame] | 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, |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 34 | sd_bus* bus_type, bool bmc_fru) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 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 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 44 | if (iv_type == IPMI_FRU_AREA_INTERNAL_USE) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 45 | { |
| 46 | iv_name = "INTERNAL_"; |
| 47 | } |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 48 | else if (iv_type == IPMI_FRU_AREA_CHASSIS_INFO) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 49 | { |
| 50 | iv_name = "CHASSIS_"; |
| 51 | } |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 52 | else if (iv_type == IPMI_FRU_AREA_BOARD_INFO) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 53 | { |
| 54 | iv_name = "BOARD_"; |
| 55 | } |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 56 | else if (iv_type == IPMI_FRU_AREA_PRODUCT_INFO) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 57 | { |
| 58 | iv_name = "PRODUCT_"; |
| 59 | } |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 60 | else if (iv_type == IPMI_FRU_AREA_MULTI_RECORD) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 61 | { |
| 62 | iv_name = "MULTI_"; |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | iv_name = IPMI_FRU_AREA_TYPE_MAX; |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 67 | fprintf(stderr, "ERROR: Invalid Area type :[%d]\n", iv_type); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | //----------------------------------------------------- |
| 72 | // For a FRU area type, accepts the data and updates |
| 73 | // area specific data. |
| 74 | //----------------------------------------------------- |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 75 | void ipmi_fru::set_data(const uint8_t* data, const size_t len) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 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 | //----------------------------------------------------- |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 85 | void ipmi_fru::update_dbus_paths(const char* bus_name, const char* obj_path, |
| 86 | const char* intf_name) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 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 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 98 | if (iv_data != NULL) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 99 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 100 | delete[] iv_data; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 101 | iv_data = NULL; |
| 102 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 103 | } |
| 104 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 105 | //------------------------------------------------ |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 106 | // Takes the pointer to stream of bytes and length |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 107 | // and returns the 8 bit checksum |
| 108 | // This algo is per IPMI V2.0 spec |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 109 | //------------------------------------------------- |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 110 | unsigned char calculate_crc(const unsigned char* data, size_t len) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 111 | { |
| 112 | char crc = 0; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 113 | size_t byte = 0; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 114 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 115 | for (byte = 0; byte < len; byte++) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 116 | { |
| 117 | crc += *data++; |
| 118 | } |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 119 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 120 | return (-crc); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | //--------------------------------------------------------------------- |
| 124 | // Accepts a fru area offset in commom hdr and tells which area it is. |
| 125 | //--------------------------------------------------------------------- |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 126 | ipmi_fru_area_type get_fru_area_type(uint8_t area_offset) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 127 | { |
| 128 | ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX; |
| 129 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 130 | switch (area_offset) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 131 | { |
| 132 | case IPMI_FRU_INTERNAL_OFFSET: |
| 133 | type = IPMI_FRU_AREA_INTERNAL_USE; |
| 134 | break; |
| 135 | |
| 136 | case IPMI_FRU_CHASSIS_OFFSET: |
| 137 | type = IPMI_FRU_AREA_CHASSIS_INFO; |
| 138 | break; |
| 139 | |
| 140 | case IPMI_FRU_BOARD_OFFSET: |
| 141 | type = IPMI_FRU_AREA_BOARD_INFO; |
| 142 | break; |
| 143 | |
| 144 | case IPMI_FRU_PRODUCT_OFFSET: |
| 145 | type = IPMI_FRU_AREA_PRODUCT_INFO; |
| 146 | break; |
| 147 | |
| 148 | case IPMI_FRU_MULTI_OFFSET: |
| 149 | type = IPMI_FRU_AREA_MULTI_RECORD; |
| 150 | break; |
| 151 | |
| 152 | default: |
| 153 | type = IPMI_FRU_AREA_TYPE_MAX; |
| 154 | } |
| 155 | |
| 156 | return type; |
| 157 | } |
| 158 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 159 | ///----------------------------------------------- |
| 160 | // Validates the data for crc and mandatory fields |
| 161 | ///----------------------------------------------- |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 162 | int verify_fru_data(const uint8_t* data, const size_t len) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 163 | { |
| 164 | uint8_t checksum = 0; |
| 165 | int rc = -1; |
| 166 | |
| 167 | // Validate for first byte to always have a value of [1] |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 168 | if (data[0] != IPMI_FRU_HDR_BYTE_ZERO) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 169 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 170 | fprintf(stderr, "Invalid entry:[%d] in byte-0\n", data[0]); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 171 | return rc; |
| 172 | } |
| 173 | #ifdef __IPMI_DEBUG__ |
| 174 | else |
| 175 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 176 | printf("SUCCESS: Validated [0x%X] in entry_1 of fru_data\n", data[0]); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 177 | } |
| 178 | #endif |
| 179 | |
| 180 | // See if the calculated CRC matches with the embedded one. |
| 181 | // CRC to be calculated on all except the last one that is CRC itself. |
| 182 | checksum = calculate_crc(data, len - 1); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 183 | if (checksum != data[len - 1]) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 184 | { |
| 185 | #ifdef __IPMI_DEBUG__ |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 186 | fprintf(stderr, |
| 187 | "Checksum mismatch." |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 188 | " Calculated:[0x%X], Embedded:[0x%X]\n", |
| 189 | checksum, data[len]); |
| 190 | #endif |
| 191 | return rc; |
| 192 | } |
| 193 | #ifdef __IPMI_DEBUG__ |
| 194 | else |
| 195 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 196 | printf("SUCCESS: Checksum matches:[0x%X]\n", checksum); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 197 | } |
| 198 | #endif |
| 199 | |
| 200 | return EXIT_SUCCESS; |
| 201 | } |
| 202 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 203 | //------------------------------------------------------------------------ |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 204 | // Gets the value of the key from the fru dictionary of the given section. |
| 205 | // FRU dictionary is parsed fru data for all the sections. |
| 206 | //------------------------------------------------------------------------ |
| 207 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 208 | std::string getFRUValue(const std::string& section, const std::string& key, |
| 209 | const std::string& delimiter, IPMIFruInfo& fruData) |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 210 | { |
| 211 | |
| 212 | auto minIndexValue = 0; |
| 213 | auto maxIndexValue = 0; |
| 214 | std::string fruValue = ""; |
| 215 | if (section == "Board") |
| 216 | { |
| 217 | minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE; |
| 218 | maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX; |
| 219 | } |
| 220 | else if (section == "Product") |
| 221 | { |
| 222 | minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR; |
| 223 | maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 224 | } |
| 225 | else if (section == "Chassis") |
| 226 | { |
| 227 | minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE; |
| 228 | maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX; |
| 229 | } |
| 230 | |
| 231 | auto first = fruData.cbegin() + minIndexValue; |
| 232 | auto last = first + (maxIndexValue - minIndexValue) + 1; |
| 233 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 234 | auto itr = |
| 235 | std::find_if(first, last, [&key](auto& e) { return key == e.first; }); |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 236 | |
| 237 | if (itr != last) |
| 238 | { |
| 239 | fruValue = itr->second; |
| 240 | } |
Ratan Gupta | cdde23d | 2017-02-15 09:29:54 +0530 | [diff] [blame] | 241 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 242 | // if the key is custom property then the value could be in two formats. |
| 243 | // 1) custom field 2 = "value". |
| 244 | // 2) custom field 2 = "key:value". |
| 245 | // if delimiter length = 0 i.e custom field 2 = "value" |
Ratan Gupta | cdde23d | 2017-02-15 09:29:54 +0530 | [diff] [blame] | 246 | |
| 247 | constexpr auto customProp = "Custom Field"; |
| 248 | if (key.find(customProp) != std::string::npos) |
| 249 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 250 | if (delimiter.length() > 0) |
| 251 | { |
| 252 | size_t delimiterpos = fruValue.find(delimiter); |
| 253 | if (delimiterpos != std::string::npos) |
| 254 | fruValue = fruValue.substr(delimiterpos + 1); |
| 255 | } |
Ratan Gupta | cdde23d | 2017-02-15 09:29:54 +0530 | [diff] [blame] | 256 | } |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 257 | return fruValue; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 258 | } |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 259 | // Get the inventory service from the mapper. |
| 260 | auto getService(sdbusplus::bus::bus& bus, const std::string& intf, |
| 261 | const std::string& path) |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 262 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 263 | auto mapperCall = |
| 264 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 265 | "/xyz/openbmc_project/object_mapper", |
| 266 | "xyz.openbmc_project.ObjectMapper", "GetObject"); |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 267 | |
| 268 | mapperCall.append(path); |
| 269 | mapperCall.append(std::vector<std::string>({intf})); |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 270 | std::map<std::string, std::vector<std::string>> mapperResponse; |
Patrick Venture | 5739ac3 | 2018-10-16 19:17:41 -0700 | [diff] [blame] | 271 | |
| 272 | try |
| 273 | { |
| 274 | auto mapperResponseMsg = bus.call(mapperCall); |
| 275 | mapperResponseMsg.read(mapperResponse); |
| 276 | } |
| 277 | catch (const sdbusplus::exception::SdBusError& ex) |
| 278 | { |
| 279 | log<level::ERR>("Exception from sdbus call", |
| 280 | entry("WHAT=%s", ex.what())); |
| 281 | throw; |
| 282 | } |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 283 | |
| 284 | if (mapperResponse.begin() == mapperResponse.end()) |
| 285 | { |
| 286 | throw std::runtime_error("ERROR in reading the mapper response"); |
| 287 | } |
| 288 | |
| 289 | return mapperResponse.begin()->first; |
| 290 | } |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 291 | |
| 292 | //------------------------------------------------------------------------ |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 293 | // Takes FRU data, invokes Parser for each fru record area and updates |
| 294 | // Inventory |
| 295 | //------------------------------------------------------------------------ |
Patrick Williams | 21eb043 | 2017-03-31 11:40:48 -0500 | [diff] [blame] | 296 | int ipmi_update_inventory(fru_area_vec_t& area_vec, sd_bus* bus_sd) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 297 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 298 | // Generic error reporter |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 299 | int rc = 0; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 300 | uint8_t fruid = 0; |
| 301 | IPMIFruInfo fruData; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 302 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 303 | // For each FRU area, extract the needed data , get it parsed and update |
| 304 | // the Inventory. |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 305 | for (const auto& fruArea : area_vec) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 306 | { |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 307 | fruid = fruArea->get_fruid(); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 308 | // Fill the container with information |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 309 | rc = parse_fru_area((fruArea)->get_type(), (void*)(fruArea)->get_data(), |
| 310 | (fruArea)->get_len(), fruData); |
| 311 | if (rc < 0) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 312 | { |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 313 | std::cerr << "ERROR parsing FRU records\n"; |
| 314 | return rc; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 315 | } |
| 316 | } // END walking the vector of areas and updating |
| 317 | |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 318 | // For each Fru we have the list of instances which needs to be updated. |
| 319 | // Each instance object implements certain interfaces. |
| 320 | // Each Interface is having Dbus properties. |
| 321 | // Each Dbus Property would be having metaData(eg section,VpdPropertyName). |
| 322 | |
| 323 | // Here we are just printing the object,interface and the properties. |
| 324 | // which needs to be called with the new inventory manager implementation. |
Patrick Williams | 49a6fcd | 2017-04-18 11:49:07 -0500 | [diff] [blame] | 325 | sdbusplus::bus::bus bus{bus_sd}; |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 326 | using namespace std::string_literals; |
| 327 | static const auto intf = "xyz.openbmc_project.Inventory.Manager"s; |
Brad Bishop | ec7648d | 2017-02-23 11:30:16 -0500 | [diff] [blame] | 328 | static const auto path = "/xyz/openbmc_project/inventory"s; |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 329 | std::string service; |
| 330 | try |
| 331 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 332 | service = getService(bus, intf, path); |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 333 | } |
Patrick Venture | 5739ac3 | 2018-10-16 19:17:41 -0700 | [diff] [blame] | 334 | catch (const std::exception& e) |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 335 | { |
| 336 | std::cerr << e.what() << "\n"; |
| 337 | return -1; |
| 338 | } |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 339 | |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 340 | auto iter = frus.find(fruid); |
| 341 | if (iter == frus.end()) |
| 342 | { |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 343 | std::cerr << "ERROR Unable to get the fru info for FRU=" |
| 344 | << static_cast<int>(fruid) << "\n"; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 345 | return -1; |
| 346 | } |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 347 | |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 348 | auto& instanceList = iter->second; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 349 | if (instanceList.size() <= 0) |
| 350 | { |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 351 | std::cout << "Object List empty for this FRU=" |
| 352 | << static_cast<int>(fruid) << "\n"; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 353 | } |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 354 | |
| 355 | ObjectMap objects; |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 356 | for (auto& instance : instanceList) |
| 357 | { |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 358 | InterfaceMap interfaces; |
Ratan Gupta | c19c054 | 2018-02-04 23:24:44 +0530 | [diff] [blame] | 359 | const auto& extrasIter = extras.find(instance.path); |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 360 | |
Ratan Gupta | c19c054 | 2018-02-04 23:24:44 +0530 | [diff] [blame] | 361 | for (auto& interfaceList : instance.interfaces) |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 362 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 363 | PropertyMap props; // store all the properties |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 364 | for (auto& properties : interfaceList.second) |
| 365 | { |
Patrick Williams | aeb726d | 2017-06-01 19:00:43 -0500 | [diff] [blame] | 366 | std::string value; |
| 367 | decltype(auto) pdata = properties.second; |
Ratan Gupta | cdde23d | 2017-02-15 09:29:54 +0530 | [diff] [blame] | 368 | |
Patrick Williams | aeb726d | 2017-06-01 19:00:43 -0500 | [diff] [blame] | 369 | if (!pdata.section.empty() && !pdata.property.empty()) |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 370 | { |
Patrick Williams | aeb726d | 2017-06-01 19:00:43 -0500 | [diff] [blame] | 371 | value = getFRUValue(pdata.section, pdata.property, |
| 372 | pdata.delimiter, fruData); |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 373 | } |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 374 | props.emplace(std::move(properties.first), std::move(value)); |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 375 | } |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame] | 376 | // Check and update extra properties |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 377 | if (extras.end() != extrasIter) |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame] | 378 | { |
| 379 | const auto& propsIter = |
| 380 | (extrasIter->second).find(interfaceList.first); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 381 | if ((extrasIter->second).end() != propsIter) |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame] | 382 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 383 | for (const auto& map : propsIter->second) |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame] | 384 | { |
| 385 | props.emplace(map.first, map.second); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | interfaces.emplace(std::move(interfaceList.first), |
| 390 | std::move(props)); |
Ratan Gupta | cb0d4e5 | 2016-12-22 19:05:57 +0530 | [diff] [blame] | 391 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 392 | |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame] | 393 | // Call the inventory manager |
Ratan Gupta | c19c054 | 2018-02-04 23:24:44 +0530 | [diff] [blame] | 394 | sdbusplus::message::object_path path = instance.path; |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame] | 395 | // Check and update extra properties |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 396 | if (extras.end() != extrasIter) |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame] | 397 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 398 | for (const auto& entry : extrasIter->second) |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame] | 399 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 400 | if (interfaces.end() == interfaces.find(entry.first)) |
Deepak Kodihalli | 788bd1f | 2017-02-20 01:21:59 -0600 | [diff] [blame] | 401 | { |
| 402 | interfaces.emplace(entry.first, entry.second); |
| 403 | } |
| 404 | } |
| 405 | } |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 406 | objects.emplace(path, interfaces); |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 407 | } |
| 408 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 409 | auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(), |
| 410 | intf.c_str(), "Notify"); |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 411 | pimMsg.append(std::move(objects)); |
Patrick Venture | 5739ac3 | 2018-10-16 19:17:41 -0700 | [diff] [blame] | 412 | |
| 413 | try |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 414 | { |
Patrick Venture | 5739ac3 | 2018-10-16 19:17:41 -0700 | [diff] [blame] | 415 | auto inventoryMgrResponseMsg = bus.call(pimMsg); |
| 416 | } |
| 417 | catch (const sdbusplus::exception::SdBusError& ex) |
| 418 | { |
| 419 | log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what())); |
Ratan Gupta | 0fc20ed | 2017-02-06 19:59:12 +0530 | [diff] [blame] | 420 | return -1; |
Ratan Gupta | 0b77cfa | 2017-01-31 17:32:31 +0530 | [diff] [blame] | 421 | } |
Patrick Venture | 5739ac3 | 2018-10-16 19:17:41 -0700 | [diff] [blame] | 422 | |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 423 | return rc; |
| 424 | } |
| 425 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 426 | ///---------------------------------------------------- |
| 427 | // Checks if a particular fru area is populated or not |
| 428 | ///---------------------------------------------------- |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 429 | bool remove_invalid_area(const std::unique_ptr<ipmi_fru>& fru_area) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 430 | { |
Deepak Kodihalli | 89ededd | 2017-02-11 01:59:32 -0600 | [diff] [blame] | 431 | // Filter the ones that are empty |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 432 | if (!(fru_area->get_len())) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 433 | { |
| 434 | return true; |
| 435 | } |
| 436 | return false; |
| 437 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 438 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 439 | ///---------------------------------------------------------------------------------- |
| 440 | // Populates various FRU areas |
| 441 | // @prereq : This must be called only after validating common header. |
| 442 | ///---------------------------------------------------------------------------------- |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 443 | int ipmi_populate_fru_areas(uint8_t* fru_data, const size_t data_len, |
| 444 | fru_area_vec_t& fru_area_vec) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 445 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 446 | int rc = -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 447 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 448 | // Now walk the common header and see if the file size has atleast the last |
| 449 | // offset mentioned by the common_hdr. If the file size is less than the |
| 450 | // offset of any if the fru areas mentioned in the common header, then we do |
| 451 | // not have a complete file. |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 452 | for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET; |
| 453 | fru_entry < (sizeof(struct common_header) - 2); fru_entry++) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 454 | { |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 455 | rc = -1; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 456 | // Actual offset in the payload is the offset mentioned in common header |
Gunnar Mills | c19b813 | 2018-06-14 08:56:17 -0500 | [diff] [blame] | 457 | // multiplied by 8. Common header is always the first 8 bytes. |
Patrick Venture | 50ddfe5 | 2018-10-16 17:16:32 -0700 | [diff] [blame] | 458 | size_t area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES; |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 459 | if (area_offset && (data_len < (area_offset + 2))) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 460 | { |
| 461 | // Our file size is less than what it needs to be. +2 because we are |
| 462 | // using area len that is at 2 byte off area_offset |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 463 | fprintf(stderr, "fru file is incomplete. Size:[%zd]\n", data_len); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 464 | return rc; |
| 465 | } |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 466 | else if (area_offset) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 467 | { |
| 468 | // Read 2 bytes to know the actual size of area. |
| 469 | uint8_t area_hdr[2] = {0}; |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 470 | memcpy(area_hdr, &((uint8_t*)fru_data)[area_offset], |
| 471 | sizeof(area_hdr)); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 472 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 473 | // Size of this area will be the 2nd byte in the fru area header. |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 474 | size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 475 | uint8_t area_data[area_len] = {0}; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 476 | |
Patrick Williams | 3365ec8 | 2016-08-17 17:45:18 -0500 | [diff] [blame] | 477 | printf("fru data size:[%zd], area offset:[%zd], area_size:[%zd]\n", |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 478 | data_len, area_offset, area_len); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 479 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 480 | // See if we really have that much buffer. We have area offset amd |
| 481 | // from there, the actual len. |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 482 | if (data_len < (area_len + area_offset)) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 483 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 484 | fprintf(stderr, "Incomplete Fru file.. Size:[%zd]\n", data_len); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 485 | return rc; |
| 486 | } |
| 487 | |
| 488 | // Save off the data. |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 489 | memcpy(area_data, &((uint8_t*)fru_data)[area_offset], area_len); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 490 | |
| 491 | // Validate the crc |
| 492 | rc = verify_fru_data(area_data, area_len); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 493 | if (rc < 0) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 494 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 495 | fprintf(stderr, "Error validating fru area. offset:[%zd]\n", |
| 496 | area_offset); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 497 | return rc; |
| 498 | } |
| 499 | else |
| 500 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 501 | printf("Successfully verified area checksum. offset:[%zd]\n", |
| 502 | area_offset); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | // We already have a vector that is passed to us containing all |
| 506 | // of the fields populated. Update the data portion now. |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 507 | for (auto& iter : fru_area_vec) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 508 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 509 | if ((iter)->get_type() == get_fru_area_type(fru_entry)) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 510 | { |
| 511 | (iter)->set_data(area_data, area_len); |
| 512 | } |
| 513 | } |
| 514 | } // If we have fru data present |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 515 | } // Walk common_hdr |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 516 | |
| 517 | // Not all the fields will be populated in a fru data. Mostly all cases will |
| 518 | // not have more than 2 or 3. |
| 519 | fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(), |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 520 | remove_invalid_area), |
| 521 | fru_area_vec.end()); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 522 | |
| 523 | return EXIT_SUCCESS; |
| 524 | } |
| 525 | |
| 526 | ///--------------------------------------------------------- |
| 527 | // Validates the fru data per ipmi common header constructs. |
| 528 | // Returns with updated common_hdr and also file_size |
| 529 | //---------------------------------------------------------- |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 530 | int ipmi_validate_common_hdr(const uint8_t* fru_data, const size_t data_len) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 531 | { |
| 532 | int rc = -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 533 | |
| 534 | uint8_t common_hdr[sizeof(struct common_header)] = {0}; |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 535 | if (data_len >= sizeof(common_hdr)) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 536 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 537 | memcpy(common_hdr, fru_data, sizeof(common_hdr)); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 538 | } |
| 539 | else |
| 540 | { |
Patrick Williams | 3365ec8 | 2016-08-17 17:45:18 -0500 | [diff] [blame] | 541 | fprintf(stderr, "Incomplete fru data file. Size:[%zd]\n", data_len); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 542 | return rc; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 543 | } |
| 544 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 545 | // Verify the crc and size |
| 546 | rc = verify_fru_data(common_hdr, sizeof(common_hdr)); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 547 | if (rc < 0) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 548 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 549 | fprintf(stderr, "Failed to validate common header\n"); |
| 550 | return rc; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 551 | } |
| 552 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 553 | return EXIT_SUCCESS; |
| 554 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 555 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 556 | //------------------------------------------------------------ |
| 557 | // Cleanup routine |
| 558 | //------------------------------------------------------------ |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 559 | int cleanup_error(FILE* fru_fp, fru_area_vec_t& fru_area_vec) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 560 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 561 | if (fru_fp != NULL) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 562 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 563 | fclose(fru_fp); |
| 564 | fru_fp = NULL; |
| 565 | } |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 566 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 567 | if (!(fru_area_vec.empty())) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 568 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 569 | fru_area_vec.clear(); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 570 | } |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 571 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 572 | return -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | ///----------------------------------------------------- |
| 576 | // Accepts the filename and validates per IPMI FRU spec |
| 577 | //---------------------------------------------------- |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 578 | int ipmi_validate_fru_area(const uint8_t fruid, const char* fru_file_name, |
| 579 | sd_bus* bus_type, const bool bmc_fru) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 580 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 581 | size_t data_len = 0; |
| 582 | size_t bytes_read = 0; |
| 583 | int rc = -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 584 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 585 | // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL |
| 586 | // are not used, keeping it here for completeness. |
| 587 | fru_area_vec_t fru_area_vec; |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 588 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 589 | for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET; |
| 590 | fru_entry < (sizeof(struct common_header) - 2); fru_entry++) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 591 | { |
| 592 | // Create an object and push onto a vector. |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 593 | std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru>( |
| 594 | fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 595 | |
| 596 | // Physically being present |
vishwa | 2f5a3cf | 2016-05-30 02:25:21 -0500 | [diff] [blame] | 597 | bool present = access(fru_file_name, F_OK) == 0; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 598 | fru_area->set_present(present); |
| 599 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 600 | fru_area_vec.emplace_back(std::move(fru_area)); |
| 601 | } |
| 602 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 603 | FILE* fru_fp = fopen(fru_file_name, "rb"); |
| 604 | if (fru_fp == NULL) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 605 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 606 | fprintf(stderr, "ERROR: opening:[%s]\n", fru_file_name); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 607 | perror("Error:"); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 608 | return cleanup_error(fru_fp, fru_area_vec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 609 | } |
| 610 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 611 | // Get the size of the file to see if it meets minimum requirement |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 612 | if (fseek(fru_fp, 0, SEEK_END)) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 613 | { |
| 614 | perror("Error:"); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 615 | return cleanup_error(fru_fp, fru_area_vec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 616 | } |
| 617 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 618 | // Allocate a buffer to hold entire file content |
| 619 | data_len = ftell(fru_fp); |
| 620 | uint8_t fru_data[data_len] = {0}; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 621 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 622 | rewind(fru_fp); |
| 623 | bytes_read = fread(fru_data, data_len, 1, fru_fp); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 624 | if (bytes_read != 1) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 625 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 626 | fprintf(stderr, "Failed reading fru data. Bytes_read=[%zd]\n", |
| 627 | bytes_read); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 628 | perror("Error:"); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 629 | return cleanup_error(fru_fp, fru_area_vec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 630 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 631 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 632 | // We are done reading. |
| 633 | fclose(fru_fp); |
| 634 | fru_fp = NULL; |
| 635 | |
| 636 | rc = ipmi_validate_common_hdr(fru_data, data_len); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 637 | if (rc < 0) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 638 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 639 | return cleanup_error(fru_fp, fru_area_vec); |
| 640 | } |
| 641 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 642 | // Now that we validated the common header, populate various fru sections if |
| 643 | // we have them here. |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 644 | rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 645 | if (rc < 0) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 646 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 647 | fprintf(stderr, "Populating FRU areas failed for:[%d]\n", fruid); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 648 | return cleanup_error(fru_fp, fru_area_vec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 649 | } |
| 650 | else |
| 651 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 652 | printf("SUCCESS: Populated FRU areas for:[%s]\n", fru_file_name); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 653 | } |
| 654 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 655 | #ifdef __IPMI_DEBUG__ |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 656 | for (auto& iter : fru_area_vec) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 657 | { |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 658 | printf("FRU ID : [%d]\n", (iter)->get_fruid()); |
| 659 | printf("AREA NAME : [%s]\n", (iter)->get_name()); |
| 660 | printf("TYPE : [%d]\n", (iter)->get_type()); |
| 661 | printf("LEN : [%d]\n", (iter)->get_len()); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 662 | printf("BUS NAME : [%s]\n", (iter)->get_bus_name()); |
| 663 | printf("OBJ PATH : [%s]\n", (iter)->get_obj_path()); |
| 664 | printf("INTF NAME :[%s]\n", (iter)->get_intf_name()); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 665 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 666 | #endif |
| 667 | |
| 668 | // If the vector is populated with everything, then go ahead and update the |
| 669 | // inventory. |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 670 | if (!(fru_area_vec.empty())) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 671 | { |
| 672 | |
| 673 | #ifdef __IPMI_DEBUG__ |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 674 | printf("\n SIZE of vector is : [%d] \n", fru_area_vec.size()); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 675 | #endif |
Patrick Williams | 21eb043 | 2017-03-31 11:40:48 -0500 | [diff] [blame] | 676 | rc = ipmi_update_inventory(fru_area_vec, bus_type); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 677 | if (rc < 0) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 678 | { |
| 679 | fprintf(stderr, "Error updating inventory\n"); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | // we are done with all that we wanted to do. This will do the job of |
| 684 | // calling any destructors too. |
| 685 | fru_area_vec.clear(); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 686 | |
| 687 | return rc; |
| 688 | } |