fru-device: use off_t/size_t for FRU IO offsets & lengths

While the 8-bit and 16-bit types that had been used for these were
adequate for the small devices typically used for most FRU EEPROMs, some
do get within striking distance of the limits of 16-bit types, and we
might as well use the more "natural" types that are intended for these
sorts of parameters, which avoid the risk of range problems and signal
intent more clearly & explicitly.

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: I38251fec2857923356d324e01af77795186560eb
diff --git a/include/FruUtils.hpp b/include/FruUtils.hpp
index beab032..8d94876 100644
--- a/include/FruUtils.hpp
+++ b/include/FruUtils.hpp
@@ -84,8 +84,8 @@
 }
 
 using ReadBlockFunc =
-    std::function<int64_t(int flag, int file, uint16_t address, uint16_t offset,
-                          uint8_t length, uint8_t* outBuf)>;
+    std::function<ssize_t(int flag, int file, uint16_t address, off_t offset,
+                          size_t length, uint8_t* outBuf)>;
 
 inline const std::string& getFruAreaName(fruAreas area)
 {
@@ -145,7 +145,7 @@
 bool findFRUHeader(int flag, int file, uint16_t address,
                    const ReadBlockFunc& readBlock, const std::string& errorHelp,
                    std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData,
-                   uint16_t& baseOffset);
+                   off_t& baseOffset);
 
 /// \brief Read and validate FRU contents.
 /// \param flag the flag required for raw i2c
diff --git a/src/FruDevice.cpp b/src/FruDevice.cpp
index 620da6e..ec42eca 100644
--- a/src/FruDevice.cpp
+++ b/src/FruDevice.cpp
@@ -116,9 +116,9 @@
     }
 }
 
-static int64_t readFromEeprom(int flag __attribute__((unused)), int fd,
+static ssize_t readFromEeprom(int flag __attribute__((unused)), int fd,
                               uint16_t address __attribute__((unused)),
-                              uint16_t offset, uint8_t len, uint8_t* buf)
+                              off_t offset, size_t len, uint8_t* buf)
 {
     auto result = lseek(fd, offset, SEEK_SET);
     if (result < 0)
@@ -256,8 +256,8 @@
     return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? msgs[1].len : -1;
 }
 
-static int64_t readBlockData(int flag, int file, uint16_t address,
-                             uint16_t offset, uint8_t len, uint8_t* buf)
+static ssize_t readBlockData(int flag, int file, uint16_t address, off_t offset,
+                             size_t len, uint8_t* buf)
 {
     if (flag == 0)
     {
diff --git a/src/FruUtils.cpp b/src/FruUtils.cpp
index 9a7c519..70f461c 100644
--- a/src/FruUtils.cpp
+++ b/src/FruUtils.cpp
@@ -590,7 +590,7 @@
 bool findFRUHeader(int flag, int file, uint16_t address,
                    const ReadBlockFunc& readBlock, const std::string& errorHelp,
                    std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData,
-                   uint16_t& baseOffset)
+                   off_t& baseOffset)
 {
     if (readBlock(flag, file, address, baseOffset, 0x8, blockData.data()) < 0)
     {
@@ -638,7 +638,7 @@
                                      const std::string& errorHelp)
 {
     std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
-    uint16_t baseOffset = 0x0;
+    off_t baseOffset = 0x0;
 
     if (!findFRUHeader(flag, file, address, readBlock, errorHelp, blockData,
                        baseOffset))
@@ -685,8 +685,7 @@
 
         areaOffset *= fruBlockSize;
 
-        if (readBlock(flag, file, address,
-                      baseOffset + static_cast<uint16_t>(areaOffset), 0x2,
+        if (readBlock(flag, file, address, baseOffset + areaOffset, 0x2,
                       blockData.data()) < 0)
         {
             std::cerr << "failed to read " << errorHelp << " base offset "
@@ -717,8 +716,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 + static_cast<uint16_t>(areaOffset), 0x3,
+            if (readBlock(flag, file, address, baseOffset + areaOffset, 0x3,
                           blockData.data()) < 0)
             {
                 std::cerr << "failed to read " << errorHelp << " base offset "
@@ -750,10 +748,8 @@
         size_t requestLength =
             std::min(static_cast<size_t>(I2C_SMBUS_BLOCK_MAX), fruLength);
 
-        if (readBlock(flag, file, address,
-                      baseOffset + static_cast<uint16_t>(readOffset),
-                      static_cast<uint8_t>(requestLength),
-                      blockData.data()) < 0)
+        if (readBlock(flag, file, address, baseOffset + readOffset,
+                      requestLength, blockData.data()) < 0)
         {
             std::cerr << "failed to read " << errorHelp << " base offset "
                       << baseOffset << "\n";
diff --git a/test/test_fru-utils.cpp b/test/test_fru-utils.cpp
index f37da0e..60b71e0 100644
--- a/test/test_fru-utils.cpp
+++ b/test/test_fru-utils.cpp
@@ -147,10 +147,10 @@
 
 int64_t getDataTempl(const std::vector<uint8_t>& data,
                      [[maybe_unused]] int flag, [[maybe_unused]] int file,
-                     [[maybe_unused]] uint16_t address, uint16_t offset,
-                     uint8_t length, uint8_t* outBuf)
+                     [[maybe_unused]] uint16_t address, off_t offset,
+                     size_t length, uint8_t* outBuf)
 {
-    if (offset >= data.size())
+    if (offset >= static_cast<off_t>(data.size()))
     {
         return 0;
     }
@@ -168,7 +168,7 @@
 TEST(FindFRUHeaderTest, InvalidHeader)
 {
     const std::vector<uint8_t> data = {255, 16};
-    uint16_t offset = 0;
+    off_t offset = 0;
     std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
     auto getData = [&data](auto fl, auto fi, auto a, auto o, auto l, auto* b) {
         return getDataTempl(data, fl, fi, a, o, l, b);
@@ -180,7 +180,7 @@
 TEST(FindFRUHeaderTest, NoData)
 {
     const std::vector<uint8_t> data = {};
-    uint16_t offset = 0;
+    off_t offset = 0;
     std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
     auto getData = [&data](auto fl, auto fi, auto a, auto o, auto l, auto* b) {
         return getDataTempl(data, fl, fi, a, o, l, b);
@@ -193,7 +193,7 @@
 {
     const std::vector<uint8_t> data = {0x01, 0x00, 0x01, 0x02,
                                        0x03, 0x04, 0x00, 0xf5};
-    uint16_t offset = 0;
+    off_t offset = 0;
     std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
     auto getData = [&data](auto fl, auto fi, auto a, auto o, auto l, auto* b) {
         return getDataTempl(data, fl, fi, a, o, l, b);
@@ -207,7 +207,7 @@
 {
     std::vector<uint8_t> data = {'$', 'T', 'Y', 'A', 'N', '$', 0, 0};
     data.resize(0x6000 + I2C_SMBUS_BLOCK_MAX);
-    uint16_t offset = 0;
+    off_t offset = 0;
     std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
     auto getData = [&data](auto fl, auto fi, auto a, auto o, auto l, auto* b) {
         return getDataTempl(data, fl, fi, a, o, l, b);
@@ -219,7 +219,7 @@
 TEST(FindFRUHeaderTest, TyanNoData)
 {
     const std::vector<uint8_t> data = {'$', 'T', 'Y', 'A', 'N', '$', 0, 0};
-    uint16_t offset = 0;
+    off_t offset = 0;
     std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
     auto getData = [&data](auto fl, auto fi, auto a, auto o, auto l, auto* b) {
         return getDataTempl(data, fl, fi, a, o, l, b);
@@ -236,7 +236,7 @@
         0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0xf5};
     copy(fruHeader.begin(), fruHeader.end(), back_inserter(data));
 
-    uint16_t offset = 0;
+    off_t offset = 0;
     std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
     auto getData = [&data](auto fl, auto fi, auto a, auto o, auto l, auto* b) {
         return getDataTempl(data, fl, fi, a, o, l, b);