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