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> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 22 | |
| 23 | #include <cstdint> |
| 24 | #include <functional> |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 25 | #include <regex> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 26 | #include <string> |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 27 | #include <utility> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 28 | #include <vector> |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 29 | extern "C" |
| 30 | { |
| 31 | // Include for I2C_SMBUS_BLOCK_MAX |
| 32 | #include <linux/i2c.h> |
| 33 | } |
| 34 | |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 35 | constexpr size_t fruBlockSize = 8; |
| 36 | |
Kumar Thangavel | c74e751 | 2022-02-03 22:53:05 +0530 | [diff] [blame] | 37 | using DeviceMap = boost::container::flat_map<int, std::vector<uint8_t>>; |
| 38 | using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>; |
| 39 | |
| 40 | inline BusMap busMap; |
| 41 | |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 42 | enum class DecodeState |
| 43 | { |
| 44 | ok, |
| 45 | end, |
| 46 | err, |
| 47 | }; |
| 48 | |
| 49 | enum class resCodes |
| 50 | { |
| 51 | resOK, |
| 52 | resWarn, |
| 53 | resErr |
| 54 | }; |
| 55 | |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 56 | enum class fruAreas |
| 57 | { |
| 58 | fruAreaInternal = 0, |
| 59 | fruAreaChassis, |
| 60 | fruAreaBoard, |
| 61 | fruAreaProduct, |
| 62 | fruAreaMultirecord |
| 63 | }; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 64 | |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame^] | 65 | struct FruArea |
| 66 | { |
| 67 | size_t start; // Fru Area Start offset |
| 68 | size_t size; // Fru Area Size |
| 69 | size_t end; // Fru Area end offset |
| 70 | size_t updateFieldLoc; // Fru Area update Field Location |
| 71 | }; |
| 72 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 73 | const std::vector<std::string> fruAreaNames = {"INTERNAL", "CHASSIS", "BOARD", |
| 74 | "PRODUCT", "MULTIRECORD"}; |
| 75 | const std::regex nonAsciiRegex("[^\x01-\x7f]"); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 76 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 77 | const std::vector<std::string> chassisFruAreas = {"PART_NUMBER", |
| 78 | "SERIAL_NUMBER"}; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 79 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 80 | const std::vector<std::string> boardFruAreas = {"MANUFACTURER", "PRODUCT_NAME", |
| 81 | "SERIAL_NUMBER", "PART_NUMBER", |
| 82 | "FRU_VERSION_ID"}; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 83 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 84 | const std::vector<std::string> productFruAreas = { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 85 | "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER", "VERSION", |
| 86 | "SERIAL_NUMBER", "ASSET_TAG", "FRU_VERSION_ID"}; |
| 87 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 88 | const std::string fruCustomFieldName = "INFO_AM"; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 89 | |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 90 | inline fruAreas operator++(fruAreas& x) |
| 91 | { |
| 92 | return x = static_cast<fruAreas>(std::underlying_type<fruAreas>::type(x) + |
| 93 | 1); |
| 94 | } |
| 95 | |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 96 | inline const std::string& getFruAreaName(fruAreas area) |
| 97 | { |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 98 | return fruAreaNames[static_cast<unsigned int>(area)]; |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 99 | } |
| 100 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 101 | std::tm intelEpoch(void); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 102 | |
| 103 | char sixBitToChar(uint8_t val); |
| 104 | |
| 105 | /* 0xd - 0xf are reserved values, but not fatal; use a placeholder char. */ |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 106 | constexpr std::array<char, 6> bcdHighChars = { |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 107 | ' ', '-', '.', 'X', 'X', 'X', |
| 108 | }; |
| 109 | |
| 110 | char bcdPlusToChar(uint8_t val); |
| 111 | |
| 112 | bool verifyOffset(const std::vector<uint8_t>& fruBytes, fruAreas currentArea, |
| 113 | uint8_t len); |
| 114 | |
| 115 | std::pair<DecodeState, std::string> |
| 116 | decodeFRUData(std::vector<uint8_t>::const_iterator& iter, |
| 117 | const std::vector<uint8_t>::const_iterator& end, |
| 118 | bool isLangEng); |
| 119 | |
| 120 | bool checkLangEng(uint8_t lang); |
| 121 | |
| 122 | resCodes |
Michael Shen | 0961b11 | 2022-02-22 11:06:33 +0800 | [diff] [blame] | 123 | formatIPMIFRU(const std::vector<uint8_t>& fruBytes, |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 124 | boost::container::flat_map<std::string, std::string>& result); |
Kumar Thangavel | c8dc4af | 2021-01-12 10:36:38 +0530 | [diff] [blame] | 125 | |
| 126 | std::vector<uint8_t>& getFRUInfo(const uint8_t& bus, const uint8_t& address); |
| 127 | |
| 128 | uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter, |
| 129 | std::vector<uint8_t>::const_iterator end); |
| 130 | |
| 131 | uint8_t calculateChecksum(std::vector<uint8_t>& fruAreaData); |
| 132 | |
| 133 | unsigned int updateFRUAreaLenAndChecksum(std::vector<uint8_t>& fruData, |
| 134 | size_t fruAreaStart, |
| 135 | size_t fruAreaEndOfFieldsOffset, |
| 136 | size_t fruAreaEndOffset); |
| 137 | |
| 138 | ssize_t getFieldLength(uint8_t fruFieldTypeLenValue); |
| 139 | |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 140 | /// \brief Find a FRU header. |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 141 | /// \param reader the FRUReader to read via |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 142 | /// \param errorHelp and a helper string for failures |
| 143 | /// \param blockData buffer to return the last read block |
| 144 | /// \param baseOffset the offset to start the search at; |
| 145 | /// set to 0 to perform search; |
| 146 | /// returns the offset at which a header was found |
| 147 | /// \return whether a header was found |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 148 | bool findFRUHeader(FRUReader& reader, const std::string& errorHelp, |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 149 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData, |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 150 | off_t& baseOffset); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 151 | |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 152 | /// \brief Read and validate FRU contents. |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 153 | /// \param reader the FRUReader to read via |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 154 | /// \param errorHelp and a helper string for failures |
| 155 | /// \return the FRU contents from the file |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 156 | std::vector<uint8_t> readFRUContents(FRUReader& reader, |
Patrick Venture | ab29641 | 2020-12-30 13:39:37 -0800 | [diff] [blame] | 157 | const std::string& errorHelp); |
| 158 | |
| 159 | /// \brief Validate an IPMI FRU common header |
| 160 | /// \param blockData the bytes comprising the common header |
| 161 | /// \return true if valid |
| 162 | bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData); |
| 163 | |
| 164 | /// \brief Get offset for a common header area |
| 165 | /// \param area - the area |
| 166 | /// \return the field offset |
| 167 | unsigned int getHeaderAreaFieldOffset(fruAreas area); |
Kumar Thangavel | bdfc5ec | 2022-08-29 22:23:00 +0530 | [diff] [blame^] | 168 | |
| 169 | /// \brief Iterate fruArea Names and find offset/location and fields and size of |
| 170 | /// properties |
| 171 | /// \param fruData - vector to store fru data |
| 172 | /// \param propertyName - fru property Name |
| 173 | /// \param fruAreaParams - struct to have fru Area paramteters like length, |
| 174 | /// size. \return true if fru field is found, fruAreaParams are updated with |
| 175 | /// fruArea and field info. |
| 176 | bool findFruAreaLocationAndField(std::vector<uint8_t>& fruData, |
| 177 | const std::string& propertyName, |
| 178 | struct FruArea& fruAreaParams, |
| 179 | size_t& fruDataIter); |