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