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;