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 | |
William A. Kennington III | 8ab5784 | 2019-02-12 12:49:51 -0800 | [diff] [blame] | 7 | #include <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 Williams | 5e8829e | 2022-07-22 19:26:53 -0500 | [diff] [blame^] | 130 | auto getService(sdbusplus::bus_t& bus, const std::string& intf, |
Patrick Venture | 17baa27 | 2018-10-20 09:48:08 -0700 | [diff] [blame] | 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 | } |
Patrick Williams | 5e8829e | 2022-07-22 19:26:53 -0500 | [diff] [blame^] | 147 | catch (const sdbusplus::exception_t& ex) |
Patrick Venture | 17baa27 | 2018-10-20 09:48:08 -0700 | [diff] [blame] | 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 Williams | 5e8829e | 2022-07-22 19:26:53 -0500 | [diff] [blame^] | 170 | int updateInventory(FruAreaVector& areaVector, sdbusplus::bus_t& 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 | de0a59e | 2019-06-18 09:49:27 -0700 | [diff] [blame] | 217 | log<level::ERR>("Unable to find FRUID in generated list", |
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 | } |
Patrick Williams | 5e8829e | 2022-07-22 19:26:53 -0500 | [diff] [blame^] | 291 | catch (const sdbusplus::exception_t& ex) |
Patrick Venture | 44a957d | 2018-10-20 09:41:29 -0700 | [diff] [blame] | 292 | { |
Patrick Venture | 5b31e6b | 2019-08-05 07:37:19 -0700 | [diff] [blame] | 293 | log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what()), |
| 294 | entry("SERVICE=%s", service.c_str()), |
| 295 | entry("PATH=%s", path.c_str())); |
Patrick Venture | 44a957d | 2018-10-20 09:41:29 -0700 | [diff] [blame] | 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | return rc; |
| 300 | } |
| 301 | |
Patrick Venture | 234b735 | 2018-10-20 09:37:09 -0700 | [diff] [blame] | 302 | } // namespace |
| 303 | |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 304 | /** |
| 305 | * Takes the pointer to stream of bytes and length and returns the 8 bit |
| 306 | * checksum. This algo is per IPMI V2.0 spec |
| 307 | * |
| 308 | * @param[in] data - data for running crc |
| 309 | * @param[in] len - the length over which to run the crc |
| 310 | * @return the CRC value |
| 311 | */ |
Patrick Venture | 062e145 | 2018-10-21 09:16:47 -0700 | [diff] [blame] | 312 | unsigned char calculateCRC(const unsigned char* data, size_t len) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 313 | { |
| 314 | char crc = 0; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 315 | size_t byte = 0; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 316 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 317 | for (byte = 0; byte < len; byte++) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 318 | { |
| 319 | crc += *data++; |
| 320 | } |
vishwa | f3ca352 | 2015-12-02 10:35:13 -0600 | [diff] [blame] | 321 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 322 | return (-crc); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 323 | } |
| 324 | |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 325 | /** |
| 326 | * Accepts a FRU area offset into a commom header and tells which area it is. |
| 327 | * |
| 328 | * @param[in] areaOffset - offset to lookup the area type |
| 329 | * @return the ipmi_fru_area_type |
| 330 | */ |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 331 | ipmi_fru_area_type getFruAreaType(uint8_t areaOffset) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 332 | { |
| 333 | ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX; |
| 334 | |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 335 | switch (areaOffset) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 336 | { |
| 337 | case IPMI_FRU_INTERNAL_OFFSET: |
| 338 | type = IPMI_FRU_AREA_INTERNAL_USE; |
| 339 | break; |
| 340 | |
| 341 | case IPMI_FRU_CHASSIS_OFFSET: |
| 342 | type = IPMI_FRU_AREA_CHASSIS_INFO; |
| 343 | break; |
| 344 | |
| 345 | case IPMI_FRU_BOARD_OFFSET: |
| 346 | type = IPMI_FRU_AREA_BOARD_INFO; |
| 347 | break; |
| 348 | |
| 349 | case IPMI_FRU_PRODUCT_OFFSET: |
| 350 | type = IPMI_FRU_AREA_PRODUCT_INFO; |
| 351 | break; |
| 352 | |
| 353 | case IPMI_FRU_MULTI_OFFSET: |
| 354 | type = IPMI_FRU_AREA_MULTI_RECORD; |
| 355 | break; |
| 356 | |
| 357 | default: |
| 358 | type = IPMI_FRU_AREA_TYPE_MAX; |
| 359 | } |
| 360 | |
| 361 | return type; |
| 362 | } |
| 363 | |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 364 | /** |
Thang Q. Nguyen | 472d3e2 | 2021-01-06 04:17:40 +0000 | [diff] [blame] | 365 | * Validates the data for multirecord fields and CRC if selected |
| 366 | * |
| 367 | * @param[in] data - the data to verify |
| 368 | * @param[in] len - the length of the region to verify |
| 369 | * @param[in] validateCrc - whether to validate the CRC |
| 370 | * @return non-zero on failure |
| 371 | */ |
| 372 | int verifyFruMultiRecData(const uint8_t* data, const size_t len, |
| 373 | bool validateCrc) |
| 374 | { |
| 375 | uint8_t checksum = 0; |
| 376 | int rc = -1; |
| 377 | |
| 378 | if (!validateCrc) |
| 379 | { |
| 380 | // There's nothing else to do for this area. |
| 381 | return EXIT_SUCCESS; |
| 382 | } |
| 383 | |
| 384 | // As per the IPMI platform spec, byte[3] is the record checksum. |
| 385 | checksum = calculateCRC(data, len); |
| 386 | if (checksum != data[3]) |
| 387 | { |
| 388 | #ifdef __IPMI_DEBUG__ |
| 389 | log<level::ERR>( |
| 390 | "Checksum mismatch", |
| 391 | entry("Calculated=0x%X", static_cast<uint32_t>(checksum)), |
| 392 | entry("Embedded=0x%X", static_cast<uint32_t>(data[3]))); |
| 393 | #endif |
| 394 | return rc; |
| 395 | } |
| 396 | #ifdef __IPMI_DEBUG__ |
| 397 | else |
| 398 | { |
| 399 | log<level::DEBUG>("Checksum matches"); |
| 400 | } |
| 401 | #endif |
| 402 | |
| 403 | return EXIT_SUCCESS; |
| 404 | } |
| 405 | |
| 406 | /** |
Oskar Senft | 6954086 | 2018-12-04 15:52:31 -0500 | [diff] [blame] | 407 | * Validates the data for mandatory fields and CRC if selected. |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 408 | * |
| 409 | * @param[in] data - the data to verify |
| 410 | * @param[in] len - the length of the region to verify |
Oskar Senft | 6954086 | 2018-12-04 15:52:31 -0500 | [diff] [blame] | 411 | * @param[in] validateCrc - whether to validate the CRC |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 412 | * @return non-zero on failure |
| 413 | */ |
Oskar Senft | 6954086 | 2018-12-04 15:52:31 -0500 | [diff] [blame] | 414 | int verifyFruData(const uint8_t* data, const size_t len, bool validateCrc) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 415 | { |
| 416 | uint8_t checksum = 0; |
| 417 | int rc = -1; |
| 418 | |
| 419 | // Validate for first byte to always have a value of [1] |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 420 | if (data[0] != IPMI_FRU_HDR_BYTE_ZERO) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 421 | { |
Patrick Venture | 5c78721 | 2018-10-17 13:48:23 -0700 | [diff] [blame] | 422 | log<level::ERR>("Invalid entry in byte-0", |
| 423 | entry("ENTRY=0x%X", static_cast<uint32_t>(data[0]))); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 424 | return rc; |
| 425 | } |
| 426 | #ifdef __IPMI_DEBUG__ |
| 427 | else |
| 428 | { |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 429 | log<level::DEBUG>("Validated in entry_1 of fruData", |
Patrick Venture | 5c78721 | 2018-10-17 13:48:23 -0700 | [diff] [blame] | 430 | entry("ENTRY=0x%X", static_cast<uint32_t>(data[0]))); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 431 | } |
| 432 | #endif |
| 433 | |
Oskar Senft | 6954086 | 2018-12-04 15:52:31 -0500 | [diff] [blame] | 434 | if (!validateCrc) |
| 435 | { |
| 436 | // There's nothing else to do for this area. |
| 437 | return EXIT_SUCCESS; |
| 438 | } |
| 439 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 440 | // See if the calculated CRC matches with the embedded one. |
| 441 | // 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] | 442 | checksum = calculateCRC(data, len - 1); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 443 | if (checksum != data[len - 1]) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 444 | { |
| 445 | #ifdef __IPMI_DEBUG__ |
Patrick Venture | 5c78721 | 2018-10-17 13:48:23 -0700 | [diff] [blame] | 446 | log<level::ERR>( |
| 447 | "Checksum mismatch", |
| 448 | entry("Calculated=0x%X", static_cast<uint32_t>(checksum)), |
| 449 | entry("Embedded=0x%X", static_cast<uint32_t>(data[len]))); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 450 | #endif |
| 451 | return rc; |
| 452 | } |
| 453 | #ifdef __IPMI_DEBUG__ |
| 454 | else |
| 455 | { |
Patrick Venture | 5c78721 | 2018-10-17 13:48:23 -0700 | [diff] [blame] | 456 | log<level::DEBUG>("Checksum matches"); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 457 | } |
| 458 | #endif |
| 459 | |
| 460 | return EXIT_SUCCESS; |
| 461 | } |
| 462 | |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 463 | /** |
| 464 | * Checks if a particular FRU area is populated or not. |
| 465 | * |
| 466 | * @param[in] reference to FRU area pointer |
| 467 | * @return true if the area is empty |
| 468 | */ |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 469 | bool removeInvalidArea(const std::unique_ptr<IPMIFruArea>& fruArea) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 470 | { |
Deepak Kodihalli | 89ededd | 2017-02-11 01:59:32 -0600 | [diff] [blame] | 471 | // Filter the ones that are empty |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 472 | if (!(fruArea->getLength())) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 473 | { |
| 474 | return true; |
| 475 | } |
| 476 | return false; |
| 477 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 478 | |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 479 | /** |
| 480 | * Populates various FRU areas. |
| 481 | * |
| 482 | * @prereq : This must be called only after validating common header |
| 483 | * @param[in] fruData - pointer to the FRU bytes |
| 484 | * @param[in] dataLen - the length of the FRU data |
| 485 | * @param[in] fruAreaVec - the FRU area vector to update |
| 486 | */ |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 487 | int ipmiPopulateFruAreas(uint8_t* fruData, const size_t dataLen, |
| 488 | FruAreaVector& fruAreaVec) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 489 | { |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 490 | // 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] | 491 | // 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] | 492 | // 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] | 493 | // then we do not have a complete file. |
| 494 | for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET; |
| 495 | fruEntry < (sizeof(struct common_header) - 2); fruEntry++) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 496 | { |
Patrick Venture | 2cdb0e3 | 2019-08-05 07:47:01 -0700 | [diff] [blame] | 497 | int rc = -1; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 498 | // Actual offset in the payload is the offset mentioned in common header |
Gunnar Mills | c19b813 | 2018-06-14 08:56:17 -0500 | [diff] [blame] | 499 | // multiplied by 8. Common header is always the first 8 bytes. |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 500 | size_t areaOffset = fruData[fruEntry] * IPMI_EIGHT_BYTES; |
| 501 | if (areaOffset && (dataLen < (areaOffset + 2))) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 502 | { |
| 503 | // 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] | 504 | // using area len that is at 2 byte off areaOffset |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 505 | log<level::ERR>("FRU file is incomplete", |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 506 | entry("SIZE=%d", dataLen)); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 507 | return rc; |
| 508 | } |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 509 | else if (areaOffset) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 510 | { |
Thang Q. Nguyen | 472d3e2 | 2021-01-06 04:17:40 +0000 | [diff] [blame] | 511 | // Read 3 bytes to know the actual size of area. |
| 512 | uint8_t areaHeader[3] = {0}; |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 513 | std::memcpy(areaHeader, &((uint8_t*)fruData)[areaOffset], |
| 514 | sizeof(areaHeader)); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 515 | |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 516 | // Size of this area will be the 2nd byte in the FRU area header. |
Thang Q. Nguyen | 472d3e2 | 2021-01-06 04:17:40 +0000 | [diff] [blame] | 517 | size_t areaLen; |
| 518 | if (fruEntry == IPMI_FRU_MULTI_OFFSET) |
| 519 | { |
| 520 | areaLen = areaHeader[2] + IPMI_FRU_MULTIREC_HDR_BYTES; |
| 521 | } |
| 522 | else |
| 523 | { |
| 524 | areaLen = areaHeader[1] * IPMI_EIGHT_BYTES; |
| 525 | } |
| 526 | |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 527 | uint8_t areaData[areaLen] = {0}; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 528 | |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 529 | log<level::DEBUG>("FRU Data", entry("SIZE=%d", dataLen), |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 530 | entry("AREA OFFSET=%d", areaOffset), |
| 531 | entry("AREA_SIZE=%d", areaLen)); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 532 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 533 | // See if we really have that much buffer. We have area offset amd |
| 534 | // from there, the actual len. |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 535 | if (dataLen < (areaLen + areaOffset)) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 536 | { |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 537 | log<level::ERR>("Incomplete FRU file", |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 538 | entry("SIZE=%d", dataLen)); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 539 | return rc; |
| 540 | } |
| 541 | |
| 542 | // Save off the data. |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 543 | std::memcpy(areaData, &((uint8_t*)fruData)[areaOffset], areaLen); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 544 | |
Oskar Senft | 6954086 | 2018-12-04 15:52:31 -0500 | [diff] [blame] | 545 | // Validate the CRC, but not for the internal use area, since its |
| 546 | // contents beyond the first byte are not defined in the spec and |
| 547 | // it may not end with a CRC byte. |
| 548 | bool validateCrc = fruEntry != IPMI_FRU_INTERNAL_OFFSET; |
Thang Q. Nguyen | 472d3e2 | 2021-01-06 04:17:40 +0000 | [diff] [blame] | 549 | |
| 550 | if (fruEntry == IPMI_FRU_MULTI_OFFSET) |
| 551 | { |
| 552 | rc = verifyFruMultiRecData(areaData, areaLen, validateCrc); |
| 553 | } |
| 554 | else |
| 555 | { |
| 556 | rc = verifyFruData(areaData, areaLen, validateCrc); |
| 557 | } |
| 558 | |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 559 | if (rc < 0) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 560 | { |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 561 | log<level::ERR>("Err validating FRU area", |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 562 | entry("OFFSET=%d", areaOffset)); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 563 | return rc; |
| 564 | } |
| 565 | else |
| 566 | { |
Oskar Senft | 6954086 | 2018-12-04 15:52:31 -0500 | [diff] [blame] | 567 | log<level::DEBUG>("Successfully verified area.", |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 568 | entry("OFFSET=%d", areaOffset)); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | // We already have a vector that is passed to us containing all |
| 572 | // of the fields populated. Update the data portion now. |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 573 | for (auto& iter : fruAreaVec) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 574 | { |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 575 | if (iter->getType() == getFruAreaType(fruEntry)) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 576 | { |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 577 | iter->setData(areaData, areaLen); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 578 | } |
| 579 | } |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 580 | } // If we have FRU data present |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 581 | } // Walk struct common_header |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 582 | |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 583 | // 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] | 584 | // not have more than 2 or 3. |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 585 | fruAreaVec.erase( |
| 586 | std::remove_if(fruAreaVec.begin(), fruAreaVec.end(), removeInvalidArea), |
| 587 | fruAreaVec.end()); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 588 | |
| 589 | return EXIT_SUCCESS; |
| 590 | } |
| 591 | |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 592 | /** |
| 593 | * Validates the FRU data per ipmi common header constructs. |
| 594 | * Returns with updated struct common_header and also file_size |
| 595 | * |
| 596 | * @param[in] fruData - the FRU data |
| 597 | * @param[in] dataLen - the length of the data |
| 598 | * @return non-zero on failure |
| 599 | */ |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 600 | int ipmiValidateCommonHeader(const uint8_t* fruData, const size_t dataLen) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 601 | { |
| 602 | int rc = -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 603 | |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 604 | uint8_t commonHdr[sizeof(struct common_header)] = {0}; |
| 605 | if (dataLen >= sizeof(commonHdr)) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 606 | { |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 607 | std::memcpy(commonHdr, fruData, sizeof(commonHdr)); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 608 | } |
| 609 | else |
| 610 | { |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 611 | log<level::ERR>("Incomplete FRU data file", entry("SIZE=%d", dataLen)); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 612 | return rc; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 613 | } |
| 614 | |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 615 | // Verify the CRC and size |
Oskar Senft | 6954086 | 2018-12-04 15:52:31 -0500 | [diff] [blame] | 616 | rc = verifyFruData(commonHdr, sizeof(commonHdr), true); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 617 | if (rc < 0) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 618 | { |
Patrick Venture | 5c78721 | 2018-10-17 13:48:23 -0700 | [diff] [blame] | 619 | log<level::ERR>("Failed to validate common header"); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 620 | return rc; |
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 | return EXIT_SUCCESS; |
| 624 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 625 | |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 626 | int validateFRUArea(const uint8_t fruid, const char* fruFilename, |
Patrick Williams | 5e8829e | 2022-07-22 19:26:53 -0500 | [diff] [blame^] | 627 | sdbusplus::bus_t& bus, const bool bmcOnlyFru) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 628 | { |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 629 | size_t dataLen = 0; |
| 630 | size_t bytesRead = 0; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 631 | int rc = -1; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 632 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 633 | // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL |
| 634 | // are not used, keeping it here for completeness. |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 635 | FruAreaVector fruAreaVec; |
Yi Li | 75c2d46 | 2016-04-11 16:57:46 +0800 | [diff] [blame] | 636 | |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 637 | for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET; |
| 638 | fruEntry < (sizeof(struct common_header) - 2); fruEntry++) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 639 | { |
| 640 | // Create an object and push onto a vector. |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 641 | std::unique_ptr<IPMIFruArea> fruArea = std::make_unique<IPMIFruArea>( |
| 642 | fruid, getFruAreaType(fruEntry), bmcOnlyFru); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 643 | |
| 644 | // Physically being present |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 645 | bool present = access(fruFilename, F_OK) == 0; |
| 646 | fruArea->setPresent(present); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 647 | |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 648 | fruAreaVec.emplace_back(std::move(fruArea)); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 649 | } |
| 650 | |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 651 | FILE* fruFilePointer = std::fopen(fruFilename, "rb"); |
| 652 | if (fruFilePointer == NULL) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 653 | { |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 654 | log<level::ERR>("Unable to open FRU file", |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 655 | entry("FILE=%s", fruFilename), |
Patrick Venture | 5c78721 | 2018-10-17 13:48:23 -0700 | [diff] [blame] | 656 | entry("ERRNO=%s", std::strerror(errno))); |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 657 | return cleanupError(fruFilePointer, fruAreaVec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 658 | } |
| 659 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 660 | // 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] | 661 | if (std::fseek(fruFilePointer, 0, SEEK_END)) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 662 | { |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 663 | log<level::ERR>("Unable to seek FRU file", |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 664 | entry("FILE=%s", fruFilename), |
Patrick Venture | 5c78721 | 2018-10-17 13:48:23 -0700 | [diff] [blame] | 665 | entry("ERRNO=%s", std::strerror(errno))); |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 666 | return cleanupError(fruFilePointer, fruAreaVec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 667 | } |
| 668 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 669 | // Allocate a buffer to hold entire file content |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 670 | dataLen = std::ftell(fruFilePointer); |
| 671 | uint8_t fruData[dataLen] = {0}; |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 672 | |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 673 | std::rewind(fruFilePointer); |
| 674 | bytesRead = std::fread(fruData, dataLen, 1, fruFilePointer); |
| 675 | if (bytesRead != 1) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 676 | { |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 677 | log<level::ERR>("Failed reading FRU data.", |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 678 | entry("BYTESREAD=%d", bytesRead), |
Patrick Venture | 5c78721 | 2018-10-17 13:48:23 -0700 | [diff] [blame] | 679 | entry("ERRNO=%s", std::strerror(errno))); |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 680 | return cleanupError(fruFilePointer, fruAreaVec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 681 | } |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 682 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 683 | // We are done reading. |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 684 | std::fclose(fruFilePointer); |
| 685 | fruFilePointer = NULL; |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 686 | |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 687 | rc = ipmiValidateCommonHeader(fruData, dataLen); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 688 | if (rc < 0) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 689 | { |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 690 | return cleanupError(fruFilePointer, fruAreaVec); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 691 | } |
| 692 | |
Patrick Venture | 6cc8904 | 2018-10-21 14:26:39 -0700 | [diff] [blame] | 693 | // Now that we validated the common header, populate various FRU sections if |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 694 | // we have them here. |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 695 | rc = ipmiPopulateFruAreas(fruData, dataLen, fruAreaVec); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 696 | if (rc < 0) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 697 | { |
Patrick Venture | 5c78721 | 2018-10-17 13:48:23 -0700 | [diff] [blame] | 698 | log<level::ERR>("Populating FRU areas failed", entry("FRU=%d", fruid)); |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 699 | return cleanupError(fruFilePointer, fruAreaVec); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 700 | } |
| 701 | else |
| 702 | { |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 703 | log<level::DEBUG>("Populated FRU areas", entry("FILE=%s", fruFilename)); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 704 | } |
| 705 | |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 706 | #ifdef __IPMI_DEBUG__ |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 707 | for (const auto& iter : fruAreaVec) |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 708 | { |
Patrick Venture | 0e3a1c4 | 2018-10-20 21:15:41 -0700 | [diff] [blame] | 709 | std::printf("FRU ID : [%d]\n", iter->getFruID()); |
| 710 | std::printf("AREA NAME : [%s]\n", iter->getName()); |
| 711 | std::printf("TYPE : [%d]\n", iter->getType()); |
| 712 | std::printf("LEN : [%d]\n", iter->getLength()); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 713 | } |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 714 | #endif |
| 715 | |
| 716 | // If the vector is populated with everything, then go ahead and update the |
| 717 | // inventory. |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 718 | if (!(fruAreaVec.empty())) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 719 | { |
| 720 | |
| 721 | #ifdef __IPMI_DEBUG__ |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 722 | std::printf("\n SIZE of vector is : [%d] \n", fruAreaVec.size()); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 723 | #endif |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 724 | rc = updateInventory(fruAreaVec, bus); |
Patrick Venture | c9508db | 2018-10-16 17:18:43 -0700 | [diff] [blame] | 725 | if (rc < 0) |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 726 | { |
Patrick Venture | 5c78721 | 2018-10-17 13:48:23 -0700 | [diff] [blame] | 727 | log<level::ERR>("Error updating inventory."); |
vishwa | c93d6d4 | 2015-12-16 11:55:16 -0600 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
| 731 | // we are done with all that we wanted to do. This will do the job of |
| 732 | // calling any destructors too. |
Patrick Venture | ef83b99 | 2018-10-21 09:32:44 -0700 | [diff] [blame] | 733 | fruAreaVec.clear(); |
Vishwa | 4be4b7a | 2015-10-31 22:55:50 -0500 | [diff] [blame] | 734 | |
| 735 | return rc; |
| 736 | } |