xref: /openbmc/entity-manager/src/fru_device/fru_reader.cpp (revision 3cbff97f6980709aec45730515e48e85c7c02cc1)
1*3cbff97fSChristopher Meis /*
2*3cbff97fSChristopher Meis // Copyright (c) 2022 Equinix, Inc.
3*3cbff97fSChristopher Meis //
4*3cbff97fSChristopher Meis // Licensed under the Apache License, Version 2.0 (the "License");
5*3cbff97fSChristopher Meis // you may not use this file except in compliance with the License.
6*3cbff97fSChristopher Meis // You may obtain a copy of the License at
7*3cbff97fSChristopher Meis //
8*3cbff97fSChristopher Meis //      http://www.apache.org/licenses/LICENSE-2.0
9*3cbff97fSChristopher Meis //
10*3cbff97fSChristopher Meis // Unless required by applicable law or agreed to in writing, software
11*3cbff97fSChristopher Meis // distributed under the License is distributed on an "AS IS" BASIS,
12*3cbff97fSChristopher Meis // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*3cbff97fSChristopher Meis // See the License for the specific language governing permissions and
14*3cbff97fSChristopher Meis // limitations under the License.
15*3cbff97fSChristopher Meis */
16*3cbff97fSChristopher Meis 
17*3cbff97fSChristopher Meis #include "fru_reader.hpp"
18*3cbff97fSChristopher Meis 
19*3cbff97fSChristopher Meis #include <cstring>
20*3cbff97fSChristopher Meis 
read(off_t start,size_t len,uint8_t * outbuf)21*3cbff97fSChristopher Meis ssize_t FRUReader::read(off_t start, size_t len, uint8_t* outbuf)
22*3cbff97fSChristopher Meis {
23*3cbff97fSChristopher Meis     size_t done = 0;
24*3cbff97fSChristopher Meis     size_t remaining = len;
25*3cbff97fSChristopher Meis     size_t cursor = start;
26*3cbff97fSChristopher Meis     while (done < len)
27*3cbff97fSChristopher Meis     {
28*3cbff97fSChristopher Meis         if (eof.has_value() && cursor >= eof.value())
29*3cbff97fSChristopher Meis         {
30*3cbff97fSChristopher Meis             break;
31*3cbff97fSChristopher Meis         }
32*3cbff97fSChristopher Meis 
33*3cbff97fSChristopher Meis         const uint8_t* blkData = nullptr;
34*3cbff97fSChristopher Meis         size_t available = 0;
35*3cbff97fSChristopher Meis         size_t blk = cursor / cacheBlockSize;
36*3cbff97fSChristopher Meis         size_t blkOffset = cursor % cacheBlockSize;
37*3cbff97fSChristopher Meis         auto findBlk = cache.find(blk);
38*3cbff97fSChristopher Meis         if (findBlk == cache.end())
39*3cbff97fSChristopher Meis         {
40*3cbff97fSChristopher Meis             // miss, populate cache
41*3cbff97fSChristopher Meis             uint8_t* newData = cache[blk].data();
42*3cbff97fSChristopher Meis             int64_t ret =
43*3cbff97fSChristopher Meis                 readFunc(blk * cacheBlockSize, cacheBlockSize, newData);
44*3cbff97fSChristopher Meis 
45*3cbff97fSChristopher Meis             // if we've reached the end of the eeprom, record its size
46*3cbff97fSChristopher Meis             if (ret >= 0 && static_cast<size_t>(ret) < cacheBlockSize)
47*3cbff97fSChristopher Meis             {
48*3cbff97fSChristopher Meis                 eof = (blk * cacheBlockSize) + ret;
49*3cbff97fSChristopher Meis             }
50*3cbff97fSChristopher Meis 
51*3cbff97fSChristopher Meis             if (ret <= 0)
52*3cbff97fSChristopher Meis             {
53*3cbff97fSChristopher Meis                 // don't leave empty blocks in the cache
54*3cbff97fSChristopher Meis                 cache.erase(blk);
55*3cbff97fSChristopher Meis                 return done != 0U ? done : ret;
56*3cbff97fSChristopher Meis             }
57*3cbff97fSChristopher Meis 
58*3cbff97fSChristopher Meis             blkData = newData;
59*3cbff97fSChristopher Meis             available = ret;
60*3cbff97fSChristopher Meis         }
61*3cbff97fSChristopher Meis         else
62*3cbff97fSChristopher Meis         {
63*3cbff97fSChristopher Meis             // hit, use cached data
64*3cbff97fSChristopher Meis             blkData = findBlk->second.data();
65*3cbff97fSChristopher Meis 
66*3cbff97fSChristopher Meis             // if the hit is to the block containing the (previously
67*3cbff97fSChristopher Meis             // discovered on the miss that populated it) end of the eeprom,
68*3cbff97fSChristopher Meis             // don't copy spurious bytes past the end
69*3cbff97fSChristopher Meis             if (eof.has_value() && (eof.value() / cacheBlockSize == blk))
70*3cbff97fSChristopher Meis             {
71*3cbff97fSChristopher Meis                 available = eof.value() % cacheBlockSize;
72*3cbff97fSChristopher Meis             }
73*3cbff97fSChristopher Meis             else
74*3cbff97fSChristopher Meis             {
75*3cbff97fSChristopher Meis                 available = cacheBlockSize;
76*3cbff97fSChristopher Meis             }
77*3cbff97fSChristopher Meis         }
78*3cbff97fSChristopher Meis 
79*3cbff97fSChristopher Meis         size_t toCopy = (blkOffset >= available)
80*3cbff97fSChristopher Meis                             ? 0
81*3cbff97fSChristopher Meis                             : std::min(available - blkOffset, remaining);
82*3cbff97fSChristopher Meis 
83*3cbff97fSChristopher Meis         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
84*3cbff97fSChristopher Meis         memcpy(outbuf + done, blkData + blkOffset, toCopy);
85*3cbff97fSChristopher Meis         cursor += toCopy;
86*3cbff97fSChristopher Meis         done += toCopy;
87*3cbff97fSChristopher Meis         remaining -= toCopy;
88*3cbff97fSChristopher Meis     }
89*3cbff97fSChristopher Meis 
90*3cbff97fSChristopher Meis     return done;
91*3cbff97fSChristopher Meis }
92