xref: /openbmc/entity-manager/src/fru_device/fru_utils.hpp (revision 89092a9ccd91f315aea7814040521f2107d9594e)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
3 
4 #pragma once
5 #include "fru_reader.hpp"
6 
7 #include <boost/container/flat_map.hpp>
8 #include <sdbusplus/asio/object_server.hpp>
9 
10 #include <cstdint>
11 #include <cstdio>
12 #include <functional>
13 #include <regex>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 extern "C"
18 {
19 // Include for I2C_SMBUS_BLOCK_MAX
20 #include <linux/i2c.h>
21 }
22 
23 constexpr size_t fruBlockSize = 8;
24 
25 using DeviceMap = boost::container::flat_map<int, std::vector<uint8_t>>;
26 using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>;
27 
28 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
29 inline BusMap busMap;
30 
31 enum class DecodeState
32 {
33     ok,
34     end,
35     err,
36 };
37 
38 enum class resCodes
39 {
40     resOK,
41     resWarn,
42     resErr
43 };
44 
45 enum class fruAreas
46 {
47     fruAreaInternal = 0,
48     fruAreaChassis,
49     fruAreaBoard,
50     fruAreaProduct,
51     fruAreaMultirecord
52 };
53 
54 struct FruArea
55 {
56     size_t start;          // Fru Area Start offset
57     size_t size;           // Fru Area Size
58     size_t end;            // Fru Area end offset
59     size_t updateFieldLoc; // Fru Area update Field Location
60     size_t restFieldsLoc;  // Starting location of restFRUArea data
61     size_t restFieldsEnd;  // Ending location of restFRUArea data
62 };
63 
64 const std::vector<std::string> fruAreaNames = {"INTERNAL", "CHASSIS", "BOARD",
65                                                "PRODUCT", "MULTIRECORD"};
66 const std::regex nonAsciiRegex("[^\x01-\x7f]");
67 
68 const std::vector<std::string> chassisFruAreas = {"PART_NUMBER",
69                                                   "SERIAL_NUMBER"};
70 
71 const std::vector<std::string> boardFruAreas = {
72     "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER",
73     "FRU_VERSION_ID"};
74 
75 const std::vector<std::string> productFruAreas = {
76     "MANUFACTURER",  "PRODUCT_NAME", "PART_NUMBER",   "VERSION",
77     "SERIAL_NUMBER", "ASSET_TAG",    "FRU_VERSION_ID"};
78 
79 const std::string fruCustomFieldName = "INFO_AM";
80 
operator ++(fruAreas & x)81 inline fruAreas operator++(fruAreas& x)
82 {
83     return x = static_cast<fruAreas>(
84                std::underlying_type<fruAreas>::type(x) + 1);
85 }
86 
getFruAreaName(fruAreas area)87 inline const std::string& getFruAreaName(fruAreas area)
88 {
89     return fruAreaNames[static_cast<unsigned int>(area)];
90 }
91 
92 std::tm intelEpoch();
93 
94 char sixBitToChar(uint8_t val);
95 
96 /* 0xd - 0xf are reserved values, but not fatal; use a placeholder char. */
97 constexpr std::array<char, 6> bcdHighChars = {
98     ' ', '-', '.', 'X', 'X', 'X',
99 };
100 
101 char bcdPlusToChar(uint8_t val);
102 
103 bool verifyOffset(std::span<const uint8_t> fruBytes, fruAreas currentArea,
104                   uint8_t len);
105 
106 std::pair<DecodeState, std::string> decodeFRUData(
107     std::span<const uint8_t>::const_iterator& iter,
108     std::span<const uint8_t>::const_iterator& end, bool isLangEng);
109 
110 bool checkLangEng(uint8_t lang);
111 
112 resCodes formatIPMIFRU(
113     std::span<const uint8_t> fruBytes,
114     boost::container::flat_map<std::string, std::string>& result);
115 
116 std::vector<uint8_t>& getFRUInfo(const uint16_t& bus, const uint8_t& address);
117 
118 uint8_t calculateChecksum(std::span<const uint8_t>::const_iterator iter,
119                           std::span<const uint8_t>::const_iterator end);
120 
121 uint8_t calculateChecksum(std::span<const uint8_t> fruAreaData);
122 
123 unsigned int updateFRUAreaLenAndChecksum(
124     std::vector<uint8_t>& fruData, size_t fruAreaStart,
125     size_t fruAreaEndOfFieldsOffset, size_t fruAreaEndOffset);
126 
127 ssize_t getFieldLength(uint8_t fruFieldTypeLenValue);
128 
129 /// \brief Find a FRU header.
130 /// \param reader the FRUReader to read via
131 /// \param errorHelp and a helper string for failures
132 /// \param blockData buffer to return the last read block
133 /// \param baseOffset the offset to start the search at;
134 ///        set to 0 to perform search;
135 ///        returns the offset at which a header was found
136 /// \return whether a header was found
137 bool findFRUHeader(FRUReader& reader, const std::string& errorHelp,
138                    std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData,
139                    off_t& baseOffset);
140 
141 /// \brief Read and validate FRU contents.
142 /// \param reader the FRUReader to read via
143 /// \param errorHelp and a helper string for failures
144 /// \return the FRU contents from the file and bool indicating if the FRU Header
145 /// was found
146 std::pair<std::vector<uint8_t>, bool> readFRUContents(
147     FRUReader& reader, const std::string& errorHelp);
148 
149 /// \brief Validate an IPMI FRU common header
150 /// \param blockData the bytes comprising the common header
151 /// \return true if valid
152 bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData);
153 
154 /// \brief Get offset for a common header area
155 /// \param area - the area
156 /// \return the field offset
157 unsigned int getHeaderAreaFieldOffset(fruAreas area);
158 
159 /// \brief Iterate fruArea Names and find offset/location and fields and size of
160 /// properties
161 /// \param fruData - vector to store fru data
162 /// \param propertyName - fru property Name
163 /// \param fruAreaParams - struct to have fru Area parameters like length,
164 /// size.
165 /// \return true if fru field is found, fruAreaParams like updateFieldLoc,
166 /// Start, Size, End are updated with fruArea and field info.
167 bool findFruAreaLocationAndField(std::vector<uint8_t>& fruData,
168                                  const std::string& propertyName,
169                                  struct FruArea& fruAreaParams);
170 
171 /// \brief Copy the fru Area fields and properties into restFRUAreaFieldsData.
172 /// restFRUAreaField is the rest of the fields in FRU area after the field that
173 /// is being updated.
174 /// \param fruData - vector to store fru data
175 /// \param propertyName - fru property Name
176 /// \param fruAreaParams - struct to have fru Area parameters like length
177 /// \param restFRUAreaFieldsData - vector to store fru Area Fields and
178 /// properties.
179 /// \return true on success false on failure. restFieldLoc and restFieldEnd
180 /// are updated.
181 
182 bool copyRestFRUArea(std::vector<uint8_t>& fruData,
183                      const std::string& propertyName,
184                      struct FruArea& fruAreaParams,
185                      std::vector<uint8_t>& restFRUAreaFieldsData);
186 
187 /// \brief Get all device dbus path and match path with product name using
188 /// regular expression and find the device index for all devices.
189 /// \param dbusInterfaceMap - Map to store fru device dbus path and interface
190 /// \param productName - fru device product name.
191 /// \return optional<int> highest index for fru device on success, return
192 /// nullopt on failure.
193 std::optional<int> findIndexForFRU(
194     boost::container::flat_map<
195         std::pair<size_t, size_t>,
196         std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
197     std::string& productName);
198 
199 /// \brief It does format fru data and find productName in the formatted
200 /// fru data and return productName.
201 /// \param device - vector that contains device list
202 /// \param formattedFRU - map that contains formatted FRU data
203 /// \param bus - bus number of the device
204 /// \param address - address of the device
205 /// \param unknownBusObjectCount - Unknown Bus object counter variable
206 /// \return optional string. it returns productName or NULL
207 
208 std::optional<std::string> getProductName(
209     std::vector<uint8_t>& device,
210     boost::container::flat_map<std::string, std::string>& formattedFRU,
211     uint32_t bus, uint32_t address, size_t& unknownBusObjectCount);
212 
213 bool getFruData(std::vector<uint8_t>& fruData, uint32_t bus, uint32_t address);
214 
215 bool isFieldEditable(std::string_view fieldName);
216