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