blob: 5e96696469673e5613f5ae93eef5aeb7ab35b79f [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*/
16/// \file FruUtils.hpp
17
18#pragma once
19
20#include <cstdint>
21#include <functional>
22#include <string>
23#include <vector>
24
25extern "C"
26{
27// Include for I2C_SMBUS_BLOCK_MAX
28#include <linux/i2c.h>
29}
30
31enum class fruAreas
32{
33 fruAreaInternal = 0,
34 fruAreaChassis,
35 fruAreaBoard,
36 fruAreaProduct,
37 fruAreaMultirecord
38};
39inline fruAreas operator++(fruAreas& x)
40{
41 return x = static_cast<fruAreas>(std::underlying_type<fruAreas>::type(x) +
42 1);
43}
44
45inline constexpr size_t fruBlockSize =
46 8; // FRU areas are measured in 8-byte blocks
47
48using ReadBlockFunc =
49 std::function<int64_t(int flag, int file, uint16_t address, uint16_t offset,
50 uint8_t length, uint8_t* outBuf)>;
51
52/// \brief Read and validate FRU contents.
53/// \param flag the flag required for raw i2c
54/// \param file the open file handle
55/// \param address the i2c device address
56/// \param readBlock a read method
57/// \param errorHelp and a helper string for failures
58/// \return the FRU contents from the file
59std::vector<uint8_t> readFRUContents(int flag, int file, uint16_t address,
60 ReadBlockFunc readBlock,
61 const std::string& errorHelp);
62
63/// \brief Validate an IPMI FRU common header
64/// \param blockData the bytes comprising the common header
65/// \return true if valid
66bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData);
67
68/// \brief Get offset for a common header area
69/// \param area - the area
70/// \return the field offset
71unsigned int getHeaderAreaFieldOffset(fruAreas area);
Vijay Khemka06d1b4a2021-02-09 18:39:11 +000072
73/// \brief Get name of FRU areas
74/// \param area - the area
75/// \return the name of Fru areas
76std::string getFruAreaName(fruAreas area);
77
78/// \brief verifies overlapping of other offsets against given offset area
79/// \param fruBytes Start of Fru data
80/// \param currentArea Index of current area offset to be compared
81/// \param len Length of current area space
82/// \return true on success
83bool verifyOffset(const std::vector<uint8_t>& fruBytes, fruAreas currentArea,
84 uint8_t len);