blob: 2d0c7194481be010ea5133e601b4a4ec559ed559 [file] [log] [blame]
Patrick Ventureab296412020-12-30 13:39:37 -08001/*
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 Bishope45d8c72022-05-25 15:12:53 -040016/// \file fru_utils.hpp
Patrick Ventureab296412020-12-30 13:39:37 -080017
18#pragma once
Zev Weiss309c0b12022-02-25 01:44:12 +000019#include "fru_reader.hpp"
Ed Tanous3013fb42022-07-09 08:27:06 -070020
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053021#include <boost/container/flat_map.hpp>
Patrick Ventureab296412020-12-30 13:39:37 -080022
23#include <cstdint>
24#include <functional>
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053025#include <regex>
Patrick Ventureab296412020-12-30 13:39:37 -080026#include <string>
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053027#include <utility>
Patrick Ventureab296412020-12-30 13:39:37 -080028#include <vector>
Patrick Ventureab296412020-12-30 13:39:37 -080029extern "C"
30{
31// Include for I2C_SMBUS_BLOCK_MAX
32#include <linux/i2c.h>
33}
34
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053035constexpr size_t fruBlockSize = 8;
36
Kumar Thangavelc74e7512022-02-03 22:53:05 +053037using DeviceMap = boost::container::flat_map<int, std::vector<uint8_t>>;
38using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>;
39
40inline BusMap busMap;
41
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053042enum class DecodeState
43{
44 ok,
45 end,
46 err,
47};
48
49enum class resCodes
50{
51 resOK,
52 resWarn,
53 resErr
54};
55
Patrick Ventureab296412020-12-30 13:39:37 -080056enum class fruAreas
57{
58 fruAreaInternal = 0,
59 fruAreaChassis,
60 fruAreaBoard,
61 fruAreaProduct,
62 fruAreaMultirecord
63};
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053064
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +053065struct 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 Tanous07d467b2021-02-23 14:48:37 -080073const std::vector<std::string> fruAreaNames = {"INTERNAL", "CHASSIS", "BOARD",
74 "PRODUCT", "MULTIRECORD"};
75const std::regex nonAsciiRegex("[^\x01-\x7f]");
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053076
Ed Tanous07d467b2021-02-23 14:48:37 -080077const std::vector<std::string> chassisFruAreas = {"PART_NUMBER",
78 "SERIAL_NUMBER"};
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053079
Ed Tanous07d467b2021-02-23 14:48:37 -080080const std::vector<std::string> boardFruAreas = {"MANUFACTURER", "PRODUCT_NAME",
81 "SERIAL_NUMBER", "PART_NUMBER",
82 "FRU_VERSION_ID"};
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053083
Ed Tanous07d467b2021-02-23 14:48:37 -080084const std::vector<std::string> productFruAreas = {
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053085 "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER", "VERSION",
86 "SERIAL_NUMBER", "ASSET_TAG", "FRU_VERSION_ID"};
87
Ed Tanous07d467b2021-02-23 14:48:37 -080088const std::string fruCustomFieldName = "INFO_AM";
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053089
Patrick Ventureab296412020-12-30 13:39:37 -080090inline fruAreas operator++(fruAreas& x)
91{
92 return x = static_cast<fruAreas>(std::underlying_type<fruAreas>::type(x) +
93 1);
94}
95
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053096inline const std::string& getFruAreaName(fruAreas area)
97{
Ed Tanous07d467b2021-02-23 14:48:37 -080098 return fruAreaNames[static_cast<unsigned int>(area)];
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +053099}
100
Ed Tanous07d467b2021-02-23 14:48:37 -0800101std::tm intelEpoch(void);
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530102
103char sixBitToChar(uint8_t val);
104
105/* 0xd - 0xf are reserved values, but not fatal; use a placeholder char. */
Ed Tanous3013fb42022-07-09 08:27:06 -0700106constexpr std::array<char, 6> bcdHighChars = {
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530107 ' ', '-', '.', 'X', 'X', 'X',
108};
109
110char bcdPlusToChar(uint8_t val);
111
112bool verifyOffset(const std::vector<uint8_t>& fruBytes, fruAreas currentArea,
113 uint8_t len);
114
115std::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
120bool checkLangEng(uint8_t lang);
121
122resCodes
Michael Shen0961b112022-02-22 11:06:33 +0800123 formatIPMIFRU(const std::vector<uint8_t>& fruBytes,
Andrew Jefferyf8ae2ba2022-03-25 15:13:55 +1030124 boost::container::flat_map<std::string, std::string>& result);
Kumar Thangavelc8dc4af2021-01-12 10:36:38 +0530125
126std::vector<uint8_t>& getFRUInfo(const uint8_t& bus, const uint8_t& address);
127
128uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter,
129 std::vector<uint8_t>::const_iterator end);
130
131uint8_t calculateChecksum(std::vector<uint8_t>& fruAreaData);
132
133unsigned int updateFRUAreaLenAndChecksum(std::vector<uint8_t>& fruData,
134 size_t fruAreaStart,
135 size_t fruAreaEndOfFieldsOffset,
136 size_t fruAreaEndOffset);
137
138ssize_t getFieldLength(uint8_t fruFieldTypeLenValue);
139
Patrick Ventureab296412020-12-30 13:39:37 -0800140/// \brief Read and validate FRU contents.
Zev Weiss3f86ca32022-03-21 22:39:12 +0000141/// \param reader the BaseFRUReader to read via
Patrick Ventureab296412020-12-30 13:39:37 -0800142/// \param errorHelp and a helper string for failures
143/// \return the FRU contents from the file
Zev Weiss3f86ca32022-03-21 22:39:12 +0000144std::vector<uint8_t> readFRUContents(BaseFRUReader& reader,
Patrick Ventureab296412020-12-30 13:39:37 -0800145 const std::string& errorHelp);
146
147/// \brief Validate an IPMI FRU common header
Zev Weisse6c86b72022-02-26 00:23:13 +0000148/// \param hdr the bytes comprising the common header
Patrick Ventureab296412020-12-30 13:39:37 -0800149/// \return true if valid
Zev Weisse6c86b72022-02-26 00:23:13 +0000150bool validateIPMIHeader(const std::vector<uint8_t>& hdr);
Patrick Ventureab296412020-12-30 13:39:37 -0800151
152/// \brief Get offset for a common header area
153/// \param area - the area
154/// \return the field offset
155unsigned int getHeaderAreaFieldOffset(fruAreas area);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +0530156
157/// \brief Iterate fruArea Names and find offset/location and fields and size of
158/// properties
159/// \param fruData - vector to store fru data
160/// \param propertyName - fru property Name
161/// \param fruAreaParams - struct to have fru Area paramteters like length,
162/// size. \return true if fru field is found, fruAreaParams are updated with
163/// fruArea and field info.
164bool findFruAreaLocationAndField(std::vector<uint8_t>& fruData,
165 const std::string& propertyName,
166 struct FruArea& fruAreaParams,
167 size_t& fruDataIter);