xref: /openbmc/phosphor-ipmi-flash/tools/bt.cpp (revision c04c2c5c)
1 #include "bt.hpp"
2 
3 #include <ipmiblob/blob_errors.hpp>
4 
5 #include <cstdint>
6 #include <vector>
7 
8 namespace host_tool
9 {
10 
11 bool BtDataHandler::sendContents(const std::string& input,
12                                  std::uint16_t session)
13 {
14     int inputFd = sys->open(input.c_str(), 0);
15     if (inputFd < 0)
16     {
17         return false;
18     }
19 
20     std::int64_t fileSize = sys->getSize(input.c_str());
21     if (fileSize == 0)
22     {
23         std::fprintf(stderr, "Zero-length file, or other file access error\n");
24         return false;
25     }
26 
27     progress->start(fileSize);
28 
29     try
30     {
31         static constexpr int btBufferLen = 50;
32         std::uint8_t readBuffer[btBufferLen];
33         int bytesRead;
34         std::uint32_t offset = 0;
35 
36         do
37         {
38             bytesRead = sys->read(inputFd, readBuffer, sizeof(readBuffer));
39             if (bytesRead > 0)
40             {
41                 /* minorly awkward repackaging. */
42                 std::vector<std::uint8_t> buffer(&readBuffer[0],
43                                                  &readBuffer[bytesRead]);
44                 blob->writeBytes(session, offset, buffer);
45                 offset += bytesRead;
46                 progress->updateProgress(bytesRead);
47             }
48         } while (bytesRead > 0);
49     }
50     catch (const ipmiblob::BlobException& b)
51     {
52         sys->close(inputFd);
53         return false;
54     }
55 
56     sys->close(inputFd);
57     return true;
58 }
59 
60 } // namespace host_tool
61