frudevice: Determine FRU write offset
This commit introduces the use of findFruHeader during the write
operation to accurately locate and set the correct offset for writing
FRU data. This ensures that the written FRU information is placed at
the correct location within the EEPROM, avoiding potential issues such
as overwriting unrelated data.
Change-Id: Ifd3a572420f59a9189fff39aaa2b8678bf8217a7
Signed-off-by: Naresh Solanki <naresh.solanki@9elements.com>
diff --git a/src/fru_device.cpp b/src/fru_device.cpp
index a455725..9e270f9 100644
--- a/src/fru_device.cpp
+++ b/src/fru_device.cpp
@@ -939,6 +939,8 @@
if (hasEepromFile(bus, address))
{
auto path = getEepromPath(bus, address);
+ off_t offset = 0;
+
int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
if (eeprom < 0)
{
@@ -947,6 +949,27 @@
return false;
}
+ std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{};
+ std::string errorMessage = "eeprom at " + std::to_string(bus) +
+ " address " + std::to_string(address);
+ auto readFunc = [eeprom](off_t offset, size_t length, uint8_t* outbuf) {
+ return readFromEeprom(eeprom, offset, length, outbuf);
+ };
+ FRUReader reader(std::move(readFunc));
+
+ if (!findFRUHeader(reader, errorMessage, blockData, offset))
+ {
+ offset = 0;
+ }
+
+ if (lseek(eeprom, offset, SEEK_SET) < 0)
+ {
+ std::cerr << "Unable to seek to offset " << offset
+ << " in device: " << path << "\n";
+ close(eeprom);
+ throw DBusInternalError();
+ }
+
ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
if (writtenBytes < 0)
{