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