| /* |
| // Copyright (c) 2018 Intel Corporation |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| */ |
| |
| // Copied required functions from |
| // https://github.com/openbmc/entity-manager/blob/master/src/fru_utils.hpp and |
| // modified as needed |
| |
| #pragma once |
| #include "fru_reader.hpp" |
| |
| #include <cstdint> |
| #include <functional> |
| #include <map> |
| #include <regex> |
| #include <string> |
| #include <utility> |
| #include <vector> |
| |
| constexpr size_t fruBlockSize = 8; |
| |
| enum class DecodeState : uint8_t |
| { |
| ok, |
| end, |
| err, |
| }; |
| |
| enum class resCodes : uint8_t |
| { |
| resOK, |
| resWarn, |
| resErr |
| }; |
| |
| enum class fruAreas : uint8_t |
| { |
| fruAreaInternal = 0, |
| fruAreaChassis, |
| fruAreaBoard, |
| fruAreaProduct, |
| fruAreaMultirecord |
| }; |
| |
| struct FruArea |
| { |
| size_t start; // Fru Area Start offset |
| size_t size; // Fru Area Size |
| size_t end; // Fru Area end offset |
| size_t updateFieldLoc; // Fru Area update Field Location |
| size_t restFieldsLoc; // Starting location of restFRUArea data |
| size_t restFieldsEnd; // Ending location of restFRUArea data |
| }; |
| |
| const std::vector<std::string> fruAreaNames = {"INTERNAL", "CHASSIS", "BOARD", |
| "PRODUCT", "MULTIRECORD"}; |
| const std::regex nonAsciiRegex("[^\x01-\x7f]"); |
| |
| const std::vector<std::string> chassisFruAreas = {"PART_NUMBER", |
| "SERIAL_NUMBER"}; |
| |
| const std::vector<std::string> boardFruAreas = {"MANUFACTURER", "PRODUCT_NAME", |
| "SERIAL_NUMBER", "PART_NUMBER", |
| "FRU_VERSION_ID"}; |
| |
| const std::vector<std::string> productFruAreas = { |
| "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER", "VERSION", |
| "SERIAL_NUMBER", "ASSET_TAG", "FRU_VERSION_ID"}; |
| |
| const std::string fruCustomFieldName = "INFO_AM"; |
| |
| inline fruAreas operator++(fruAreas& x) |
| { |
| return x = static_cast<fruAreas>(std::underlying_type<fruAreas>::type(x) + |
| 1); |
| } |
| |
| inline const std::string& getFruAreaName(fruAreas area) |
| { |
| return fruAreaNames[static_cast<unsigned int>(area)]; |
| } |
| |
| std::tm intelEpoch(); |
| |
| char sixBitToChar(uint8_t val); |
| |
| /* 0xd - 0xf are reserved values, but not fatal; use a placeholder char. */ |
| constexpr std::array<char, 6> bcdHighChars = { |
| ' ', '-', '.', 'X', 'X', 'X', |
| }; |
| |
| char bcdPlusToChar(uint8_t val); |
| |
| bool verifyOffset(const std::vector<uint8_t>& fruBytes, fruAreas currentArea, |
| uint8_t len); |
| |
| std::pair<DecodeState, std::string> |
| decodeFRUData(std::vector<uint8_t>::const_iterator& iter, |
| const std::vector<uint8_t>::const_iterator& end, |
| bool isLangEng); |
| |
| bool checkLangEng(uint8_t lang); |
| |
| resCodes formatIPMIFRU(const std::vector<uint8_t>& fruBytes, |
| std::map<std::string, std::string>& result); |
| |
| uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter, |
| std::vector<uint8_t>::const_iterator end); |
| |
| uint8_t calculateChecksum(std::vector<uint8_t>& fruAreaData); |
| |
| /// \brief Find a FRU header. |
| /// \param reader the FRUReader to read via |
| /// \param errorHelp and a helper string for failures |
| /// \param blockData buffer to return the last read block |
| /// \param baseOffset the offset to start the search at; |
| /// set to 0 to perform search; |
| /// returns the offset at which a header was found |
| /// \return whether a header was found |
| bool findFRUHeader(FRUReader& reader, const std::string& errorHelp, |
| std::array<uint8_t, SMBUS_BLOCK_MAX>& blockData, |
| off_t& baseOffset); |
| |
| /// \brief Read and validate FRU contents. |
| /// \param reader the FRUReader to read via |
| /// \param errorHelp and a helper string for failures |
| /// \return the FRU contents from the file |
| std::vector<uint8_t> readFRUContents(FRUReader& reader, |
| const std::string& errorHelp); |
| |
| /// \brief Validate an IPMI FRU common header |
| /// \param blockData the bytes comprising the common header |
| /// \return true if valid |
| bool validateHeader(const std::array<uint8_t, SMBUS_BLOCK_MAX>& blockData); |
| |
| /// \brief Get offset for a common header area |
| /// \param area - the area |
| /// \return the field offset |
| unsigned int getHeaderAreaFieldOffset(fruAreas area); |