Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 16 | /// \file fru_utils.hpp |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 17 | |
| 18 | #pragma once |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 19 | #include "fru_reader.hpp" |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 20 | |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 21 | #include <boost/container/flat_map.hpp> |
Kumar Thangavel | d79d025 | 2022-08-24 14:26:01 +0530 | [diff] [blame] | 22 | #include <sdbusplus/asio/object_server.hpp> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 23 | |
| 24 | #include <cstdint> |
Patrick Williams | b9a1f96 | 2024-07-13 16:34:49 -0500 | [diff] [blame] | 25 | #include <cstdio> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 26 | #include <functional> |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 27 | #include <regex> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 28 | #include <string> |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 29 | #include <utility> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 30 | #include <vector> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 31 | extern "C" |
| 32 | { |
| 33 | // Include for I2C_SMBUS_BLOCK_MAX |
| 34 | #include <linux/i2c.h> |
| 35 | } |
| 36 | |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 37 | constexpr size_t fruBlockSize = 8; |
| 38 | |
Kumar Thangavel | c74e751 | 2022-02-03 22:53:05 +0530 | [diff] [blame] | 39 | using DeviceMap = boost::container::flat_map<int, std::vector<uint8_t>>; |
| 40 | using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>; |
| 41 | |
Ed Tanous | fc17142 | 2024-04-04 17:18:16 -0700 | [diff] [blame] | 42 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Kumar Thangavel | c74e751 | 2022-02-03 22:53:05 +0530 | [diff] [blame] | 43 | inline BusMap busMap; |
| 44 | |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 45 | enum class DecodeState |
| 46 | { |
| 47 | ok, |
| 48 | end, |
| 49 | err, |
| 50 | }; |
| 51 | |
| 52 | enum class resCodes |
| 53 | { |
| 54 | resOK, |
| 55 | resWarn, |
| 56 | resErr |
| 57 | }; |
| 58 | |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 59 | enum class fruAreas |
| 60 | { |
| 61 | fruAreaInternal = 0, |
| 62 | fruAreaChassis, |
| 63 | fruAreaBoard, |
| 64 | fruAreaProduct, |
| 65 | fruAreaMultirecord |
| 66 | }; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 67 | |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 68 | struct FruArea |
| 69 | { |
| 70 | size_t start; // Fru Area Start offset |
| 71 | size_t size; // Fru Area Size |
| 72 | size_t end; // Fru Area end offset |
| 73 | size_t updateFieldLoc; // Fru Area update Field Location |
Kumar Thangavel | 51b557b | 2022-09-13 13:40:47 +0530 | [diff] [blame] | 74 | size_t restFieldsLoc; // Starting location of restFRUArea data |
| 75 | size_t restFieldsEnd; // Ending location of restFRUArea data |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 76 | }; |
| 77 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 78 | const std::vector<std::string> fruAreaNames = {"INTERNAL", "CHASSIS", "BOARD", |
| 79 | "PRODUCT", "MULTIRECORD"}; |
| 80 | const std::regex nonAsciiRegex("[^\x01-\x7f]"); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 81 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 82 | const std::vector<std::string> chassisFruAreas = {"PART_NUMBER", |
| 83 | "SERIAL_NUMBER"}; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 84 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 85 | const std::vector<std::string> boardFruAreas = { |
| 86 | "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER", |
| 87 | "FRU_VERSION_ID"}; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 88 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 89 | const std::vector<std::string> productFruAreas = { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 90 | "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER", "VERSION", |
| 91 | "SERIAL_NUMBER", "ASSET_TAG", "FRU_VERSION_ID"}; |
| 92 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 93 | const std::string fruCustomFieldName = "INFO_AM"; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 94 | |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 95 | inline fruAreas operator++(fruAreas& x) |
| 96 | { |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 97 | return x = static_cast<fruAreas>( |
| 98 | std::underlying_type<fruAreas>::type(x) + 1); |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 101 | inline const std::string& getFruAreaName(fruAreas area) |
| 102 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 103 | return fruAreaNames[static_cast<unsigned int>(area)]; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 104 | } |
| 105 | |
Delphine CC Chiu | a3ca14a | 2024-03-27 17:02:24 +0800 | [diff] [blame] | 106 | std::tm intelEpoch(); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 107 | |
| 108 | char sixBitToChar(uint8_t val); |
| 109 | |
| 110 | /* 0xd - 0xf are reserved values, but not fatal; use a placeholder char. */ |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 111 | constexpr std::array<char, 6> bcdHighChars = { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 112 | ' ', '-', '.', 'X', 'X', 'X', |
| 113 | }; |
| 114 | |
| 115 | char bcdPlusToChar(uint8_t val); |
| 116 | |
| 117 | bool verifyOffset(const std::vector<uint8_t>& fruBytes, fruAreas currentArea, |
| 118 | uint8_t len); |
| 119 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 120 | std::pair<DecodeState, std::string> decodeFRUData( |
| 121 | std::vector<uint8_t>::const_iterator& iter, |
| 122 | const std::vector<uint8_t>::const_iterator& end, bool isLangEng); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 123 | |
| 124 | bool checkLangEng(uint8_t lang); |
| 125 | |
| 126 | resCodes |
Michael Shen | 0961b11 | 2022-02-22 11:06:33 +0800 | [diff] [blame] | 127 | formatIPMIFRU(const std::vector<uint8_t>& fruBytes, |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 128 | boost::container::flat_map<std::string, std::string>& result); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 129 | |
krishnar4 | 213ee21 | 2022-11-11 15:39:30 +0530 | [diff] [blame] | 130 | std::vector<uint8_t>& getFRUInfo(const uint16_t& bus, const uint8_t& address); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 131 | |
| 132 | uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter, |
| 133 | std::vector<uint8_t>::const_iterator end); |
| 134 | |
| 135 | uint8_t calculateChecksum(std::vector<uint8_t>& fruAreaData); |
| 136 | |
Patrick Williams | b707743 | 2024-08-16 15:22:21 -0400 | [diff] [blame] | 137 | unsigned int updateFRUAreaLenAndChecksum( |
| 138 | std::vector<uint8_t>& fruData, size_t fruAreaStart, |
| 139 | size_t fruAreaEndOfFieldsOffset, size_t fruAreaEndOffset); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 140 | |
| 141 | ssize_t getFieldLength(uint8_t fruFieldTypeLenValue); |
| 142 | |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 143 | /// \brief Find a FRU header. |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 144 | /// \param reader the FRUReader to read via |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 145 | /// \param errorHelp and a helper string for failures |
| 146 | /// \param blockData buffer to return the last read block |
| 147 | /// \param baseOffset the offset to start the search at; |
| 148 | /// set to 0 to perform search; |
| 149 | /// returns the offset at which a header was found |
| 150 | /// \return whether a header was found |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 151 | bool findFRUHeader(FRUReader& reader, const std::string& errorHelp, |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 152 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData, |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 153 | off_t& baseOffset); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 154 | |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 155 | /// \brief Read and validate FRU contents. |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 156 | /// \param reader the FRUReader to read via |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 157 | /// \param errorHelp and a helper string for failures |
Marvin Drees | 2b3ed30 | 2023-04-14 16:35:14 +0200 | [diff] [blame] | 158 | /// \return the FRU contents from the file and bool indicating if the FRU Header |
| 159 | /// was found |
| 160 | std::pair<std::vector<uint8_t>, bool> |
| 161 | readFRUContents(FRUReader& reader, const std::string& errorHelp); |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 162 | |
| 163 | /// \brief Validate an IPMI FRU common header |
| 164 | /// \param blockData the bytes comprising the common header |
| 165 | /// \return true if valid |
| 166 | bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData); |
| 167 | |
| 168 | /// \brief Get offset for a common header area |
| 169 | /// \param area - the area |
| 170 | /// \return the field offset |
| 171 | unsigned int getHeaderAreaFieldOffset(fruAreas area); |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 172 | |
| 173 | /// \brief Iterate fruArea Names and find offset/location and fields and size of |
| 174 | /// properties |
| 175 | /// \param fruData - vector to store fru data |
| 176 | /// \param propertyName - fru property Name |
Kumar Thangavel | 51b557b | 2022-09-13 13:40:47 +0530 | [diff] [blame] | 177 | /// \param fruAreaParams - struct to have fru Area parameters like length, |
| 178 | /// size. |
| 179 | /// \return true if fru field is found, fruAreaParams like updateFieldLoc, |
| 180 | /// Start, Size, End are updated with fruArea and field info. |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame] | 181 | bool findFruAreaLocationAndField(std::vector<uint8_t>& fruData, |
| 182 | const std::string& propertyName, |
Kumar Thangavel | 51b557b | 2022-09-13 13:40:47 +0530 | [diff] [blame] | 183 | struct FruArea& fruAreaParams); |
| 184 | |
| 185 | /// \brief Copy the fru Area fields and properties into restFRUAreaFieldsData. |
| 186 | /// restFRUAreaField is the rest of the fields in FRU area after the field that |
| 187 | /// is being updated. |
| 188 | /// \param fruData - vector to store fru data |
| 189 | /// \param propertyName - fru property Name |
| 190 | /// \param fruAreaParams - struct to have fru Area parameters like length |
| 191 | /// \param restFRUAreaFieldsData - vector to store fru Area Fields and |
| 192 | /// properties. |
| 193 | /// \return true on success false on failure. restFieldLoc and restFieldEnd |
| 194 | /// are updated. |
Kumar Thangavel | 9f2162a | 2022-08-10 18:05:20 +0530 | [diff] [blame] | 195 | |
Kumar Thangavel | 51b557b | 2022-09-13 13:40:47 +0530 | [diff] [blame] | 196 | bool copyRestFRUArea(std::vector<uint8_t>& fruData, |
| 197 | const std::string& propertyName, |
| 198 | struct FruArea& fruAreaParams, |
| 199 | std::vector<uint8_t>& restFRUAreaFieldsData); |
Kumar Thangavel | d79d025 | 2022-08-24 14:26:01 +0530 | [diff] [blame] | 200 | |
| 201 | /// \brief Get all device dbus path and match path with product name using |
| 202 | /// regular expression and find the device index for all devices. |
| 203 | /// \param dbusInterfaceMap - Map to store fru device dbus path and interface |
| 204 | /// \param productName - fru device product name. |
| 205 | /// \return optional<int> highest index for fru device on success, return |
| 206 | /// nullopt on failure. |
| 207 | std::optional<int> findIndexForFRU( |
| 208 | boost::container::flat_map< |
| 209 | std::pair<size_t, size_t>, |
| 210 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
| 211 | std::string& productName); |
Kumar Thangavel | 9f2162a | 2022-08-10 18:05:20 +0530 | [diff] [blame] | 212 | |
| 213 | /// \brief It does format fru data and find productName in the formatted |
| 214 | /// fru data and return productName. |
| 215 | /// \param device - vector that contains device list |
| 216 | /// \param formattedFRU - map that contains formatted FRU data |
| 217 | /// \param bus - bus number of the device |
| 218 | /// \param address - address of the device |
| 219 | /// \param unknownBusObjectCount - Unknown Bus object counter variable |
| 220 | /// \return optional string. it returns productName or NULL |
| 221 | |
| 222 | std::optional<std::string> getProductName( |
| 223 | std::vector<uint8_t>& device, |
| 224 | boost::container::flat_map<std::string, std::string>& formattedFRU, |
| 225 | uint32_t bus, uint32_t address, size_t& unknownBusObjectCount); |
Kumar Thangavel | 9d6f590 | 2022-08-26 16:52:14 +0530 | [diff] [blame] | 226 | |
| 227 | bool getFruData(std::vector<uint8_t>& fruData, uint32_t bus, uint32_t address); |