fru-device: add FRUReader EEPROM cache
This doesn't provide a huge benefit currently, but is a preparatory step
for adding support for a wider variety of FRU format parsers. If
parsers for multiple formats are interested in reading the same areas of
an EEPROM, the caching provided by this abstraction avoids the redundant
bus traffic of all of them re-reading the same bytes over and over.
Tested: on an ASRock Rack romed8hm3, fru-device successfully recognizes
and parses the baseboard FRU EEPROM as it did prior to this patch.
Signed-off-by: Zev Weiss <zev@bewilderbeest.net>
Change-Id: I0d774508bc29dbda346309fe7fe42dbdf97bee43
diff --git a/src/fru_device.cpp b/src/fru_device.cpp
index af53e7f..b4c3668 100644
--- a/src/fru_device.cpp
+++ b/src/fru_device.cpp
@@ -116,9 +116,7 @@
}
}
-static ssize_t readFromEeprom(int flag __attribute__((unused)), int fd,
- uint16_t address __attribute__((unused)),
- off_t offset, size_t len, uint8_t* buf)
+static int64_t readFromEeprom(int fd, off_t offset, size_t len, uint8_t* buf)
{
auto result = lseek(fd, offset, SEEK_SET);
if (result < 0)
@@ -189,21 +187,21 @@
it->second->initialize();
}
-static int isDevice16Bit(int file)
+static std::optional<bool> isDevice16Bit(int file)
{
// Set the higher data word address bits to 0. It's safe on 8-bit addressing
// EEPROMs because it doesn't write any actual data.
int ret = i2c_smbus_write_byte(file, 0);
if (ret < 0)
{
- return ret;
+ return std::nullopt;
}
/* Get first byte */
int byte1 = i2c_smbus_read_byte_data(file, 0);
if (byte1 < 0)
{
- return byte1;
+ return std::nullopt;
}
/* Read 7 more bytes, it will read same first byte in case of
* 8 bit but it will read next byte in case of 16 bit
@@ -213,14 +211,14 @@
int byte2 = i2c_smbus_read_byte_data(file, 0);
if (byte2 < 0)
{
- return byte2;
+ return std::nullopt;
}
if (byte2 != byte1)
{
- return 1;
+ return true;
}
}
- return 0;
+ return false;
}
// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
@@ -256,10 +254,10 @@
return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? msgs[1].len : -1;
}
-static ssize_t readBlockData(int flag, int file, uint16_t address, off_t offset,
- size_t len, uint8_t* buf)
+static int64_t readBlockData(bool is16bit, int file, uint16_t address,
+ off_t offset, size_t len, uint8_t* buf)
{
- if (flag == 0)
+ if (!is16bit)
{
return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset),
len, buf);
@@ -285,8 +283,11 @@
std::string errorMessage = "eeprom at " + std::to_string(bus) +
" address " + std::to_string(address);
- std::vector<uint8_t> device = readFRUContents(
- 0, file, static_cast<uint16_t>(address), readFromEeprom, errorMessage);
+ auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) {
+ return readFromEeprom(file, offset, length, outbuf);
+ };
+ FRUReader reader(std::move(readFunc));
+ std::vector<uint8_t> device = readFRUContents(reader, errorMessage);
close(file);
return device;
@@ -431,8 +432,8 @@
}
/* Check for Device type if it is 8 bit or 16 bit */
- int flag = isDevice16Bit(file);
- if (flag < 0)
+ std::optional<bool> is16Bit = isDevice16Bit(file);
+ if (!is16Bit.has_value())
{
std::cerr << "failed to read bus " << bus << " address " << ii
<< "\n";
@@ -442,12 +443,17 @@
}
continue;
}
+ bool is16BitBool{*is16Bit};
+ auto readFunc = [is16BitBool, file, ii](off_t offset, size_t length,
+ uint8_t* outbuf) {
+ return readBlockData(is16BitBool, file, ii, offset, length,
+ outbuf);
+ };
+ FRUReader reader(std::move(readFunc));
std::string errorMessage =
"bus " + std::to_string(bus) + " address " + std::to_string(ii);
- std::vector<uint8_t> device =
- readFRUContents(flag, file, static_cast<uint16_t>(ii),
- readBlockData, errorMessage);
+ std::vector<uint8_t> device = readFRUContents(reader, errorMessage);
if (device.empty())
{
continue;
diff --git a/src/fru_reader.cpp b/src/fru_reader.cpp
new file mode 100644
index 0000000..e381e7e
--- /dev/null
+++ b/src/fru_reader.cpp
@@ -0,0 +1,90 @@
+/*
+// Copyright (c) 2022 Equinix, Inc.
+//
+// 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.
+*/
+
+#include "fru_reader.hpp"
+
+#include <cstring>
+
+ssize_t FRUReader::read(off_t start, size_t len, uint8_t* outbuf)
+{
+ size_t done = 0;
+ size_t remaining = len;
+ size_t cursor = start;
+ while (done < len)
+ {
+ if (eof.has_value() && cursor >= eof.value())
+ {
+ break;
+ }
+
+ const uint8_t* blkData;
+ size_t available;
+ size_t blk = cursor / cacheBlockSize;
+ size_t blkOffset = cursor % cacheBlockSize;
+ auto findBlk = cache.find(blk);
+ if (findBlk == cache.end())
+ {
+ // miss, populate cache
+ uint8_t* newData = cache[blk].data();
+ int64_t ret =
+ readFunc(blk * cacheBlockSize, cacheBlockSize, newData);
+
+ // if we've reached the end of the eeprom, record its size
+ if (ret >= 0 && static_cast<size_t>(ret) < cacheBlockSize)
+ {
+ eof = (blk * cacheBlockSize) + ret;
+ }
+
+ if (ret <= 0)
+ {
+ // don't leave empty blocks in the cache
+ cache.erase(blk);
+ return done ? done : ret;
+ }
+
+ blkData = newData;
+ available = ret;
+ }
+ else
+ {
+ // hit, use cached data
+ blkData = findBlk->second.data();
+
+ // if the hit is to the block containing the (previously
+ // discovered on the miss that populated it) end of the eeprom,
+ // don't copy spurious bytes past the end
+ if (eof.has_value() && (eof.value() / cacheBlockSize == blk))
+ {
+ available = eof.value() % cacheBlockSize;
+ }
+ else
+ {
+ available = cacheBlockSize;
+ }
+ }
+
+ size_t toCopy = (blkOffset >= available)
+ ? 0
+ : std::min(available - blkOffset, remaining);
+
+ memcpy(outbuf + done, blkData + blkOffset, toCopy);
+ cursor += toCopy;
+ done += toCopy;
+ remaining -= toCopy;
+ }
+
+ return done;
+}
diff --git a/src/fru_utils.cpp b/src/fru_utils.cpp
index 62390c6..99f4aa9 100644
--- a/src/fru_utils.cpp
+++ b/src/fru_utils.cpp
@@ -587,12 +587,11 @@
return true;
}
-bool findFRUHeader(int flag, int file, uint16_t address,
- const ReadBlockFunc& readBlock, const std::string& errorHelp,
+bool findFRUHeader(FRUReader& reader, const std::string& errorHelp,
std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData,
off_t& baseOffset)
{
- if (readBlock(flag, file, address, baseOffset, 0x8, blockData.data()) < 0)
+ if (reader.read(baseOffset, 0x8, blockData.data()) < 0)
{
std::cerr << "failed to read " << errorHelp << " base offset "
<< baseOffset << "\n";
@@ -620,8 +619,7 @@
{
// look for the FRU header at offset 0x6000
baseOffset = 0x6000;
- return findFRUHeader(flag, file, address, readBlock, errorHelp,
- blockData, baseOffset);
+ return findFRUHeader(reader, errorHelp, blockData, baseOffset);
}
if (debug)
@@ -633,15 +631,13 @@
return false;
}
-std::vector<uint8_t> readFRUContents(int flag, int file, uint16_t address,
- const ReadBlockFunc& readBlock,
+std::vector<uint8_t> readFRUContents(FRUReader& reader,
const std::string& errorHelp)
{
std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
off_t baseOffset = 0x0;
- if (!findFRUHeader(flag, file, address, readBlock, errorHelp, blockData,
- baseOffset))
+ if (!findFRUHeader(reader, errorHelp, blockData, baseOffset))
{
return {};
}
@@ -685,8 +681,7 @@
areaOffset *= fruBlockSize;
- if (readBlock(flag, file, address, baseOffset + areaOffset, 0x2,
- blockData.data()) < 0)
+ if (reader.read(baseOffset + areaOffset, 0x2, blockData.data()) < 0)
{
std::cerr << "failed to read " << errorHelp << " base offset "
<< baseOffset << "\n";
@@ -716,8 +711,7 @@
{
// In multi-area, the area offset points to the 0th record, each
// record has 3 bytes of the header we care about.
- if (readBlock(flag, file, address, baseOffset + areaOffset, 0x3,
- blockData.data()) < 0)
+ if (reader.read(baseOffset + areaOffset, 0x3, blockData.data()) < 0)
{
std::cerr << "failed to read " << errorHelp << " base offset "
<< baseOffset << "\n";
@@ -748,8 +742,8 @@
size_t requestLength =
std::min(static_cast<size_t>(I2C_SMBUS_BLOCK_MAX), fruLength);
- if (readBlock(flag, file, address, baseOffset + readOffset,
- requestLength, blockData.data()) < 0)
+ if (reader.read(baseOffset + readOffset, requestLength,
+ blockData.data()) < 0)
{
std::cerr << "failed to read " << errorHelp << " base offset "
<< baseOffset << "\n";
diff --git a/src/meson.build b/src/meson.build
index 8ddb21e..d2959bf 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -32,6 +32,7 @@
'fru_device.cpp',
'utils.cpp',
'fru_utils.cpp',
+ 'fru_reader.cpp',
cpp_args: cpp_args_fd,
dependencies: [
boost,