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