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