xref: /openbmc/phosphor-ipmi-flash/tools/bt.cpp (revision ebccf632866584bf7a9d097fdaad7c4bc8f039bc)
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         sys->close(inputFd);
25         return false;
26     }
27 
28     progress->start(fileSize);
29 
30     try
31     {
32         static constexpr int btBufferLen = 50;
33         std::uint8_t readBuffer[btBufferLen];
34         int bytesRead;
35         std::uint32_t offset = 0;
36 
37         do
38         {
39             bytesRead = sys->read(inputFd, readBuffer, sizeof(readBuffer));
40             if (bytesRead > 0)
41             {
42                 /* minorly awkward repackaging. */
43                 std::vector<std::uint8_t> buffer(&readBuffer[0],
44                                                  &readBuffer[bytesRead]);
45                 blob->writeBytes(session, offset, buffer);
46                 offset += bytesRead;
47                 progress->updateProgress(bytesRead);
48             }
49         } while (bytesRead > 0);
50     }
51     catch (const ipmiblob::BlobException& b)
52     {
53         progress->abort();
54         sys->close(inputFd);
55         return false;
56     }
57 
58     progress->finish();
59     sys->close(inputFd);
60     return true;
61 }
62 
63 } // namespace host_tool
64