xref: /openbmc/phosphor-ipmi-flash/tools/bt.cpp (revision 9b7a3b4fc71d100f2ad29bdf01143f8fb8deb838)
1af69625fSPatrick Venture #include "bt.hpp"
2af69625fSPatrick Venture 
3664c5bc7SPatrick Venture #include <ipmiblob/blob_errors.hpp>
49b37b095SPatrick Venture 
59b37b095SPatrick Venture #include <cstdint>
6907f3a70SPatrick Venture #include <vector>
7907f3a70SPatrick Venture 
89b534f06SPatrick Venture namespace host_tool
99b534f06SPatrick Venture {
109b534f06SPatrick Venture 
sendContents(const std::string & input,std::uint16_t session)11af69625fSPatrick Venture bool BtDataHandler::sendContents(const std::string& input,
12af69625fSPatrick Venture                                  std::uint16_t session)
13af69625fSPatrick Venture {
14907f3a70SPatrick Venture     int inputFd = sys->open(input.c_str(), 0);
15907f3a70SPatrick Venture     if (inputFd < 0)
16907f3a70SPatrick Venture     {
17af69625fSPatrick Venture         return false;
18af69625fSPatrick Venture     }
199b534f06SPatrick Venture 
20cf9b2195SPatrick Venture     std::int64_t fileSize = sys->getSize(input.c_str());
21cf9b2195SPatrick Venture     progress->start(fileSize);
22cf9b2195SPatrick Venture 
2363528046SPatrick Venture     try
2463528046SPatrick Venture     {
25907f3a70SPatrick Venture         static constexpr int btBufferLen = 50;
26907f3a70SPatrick Venture         std::uint8_t readBuffer[btBufferLen];
27907f3a70SPatrick Venture         int bytesRead;
28907f3a70SPatrick Venture         std::uint32_t offset = 0;
29907f3a70SPatrick Venture 
30907f3a70SPatrick Venture         do
31907f3a70SPatrick Venture         {
32907f3a70SPatrick Venture             bytesRead = sys->read(inputFd, readBuffer, sizeof(readBuffer));
33907f3a70SPatrick Venture             if (bytesRead > 0)
34907f3a70SPatrick Venture             {
35907f3a70SPatrick Venture                 /* minorly awkward repackaging. */
36907f3a70SPatrick Venture                 std::vector<std::uint8_t> buffer(&readBuffer[0],
37907f3a70SPatrick Venture                                                  &readBuffer[bytesRead]);
38907f3a70SPatrick Venture                 blob->writeBytes(session, offset, buffer);
39907f3a70SPatrick Venture                 offset += bytesRead;
40cf9b2195SPatrick Venture                 progress->updateProgress(bytesRead);
41907f3a70SPatrick Venture             }
42907f3a70SPatrick Venture         } while (bytesRead > 0);
43907f3a70SPatrick Venture     }
44664c5bc7SPatrick Venture     catch (const ipmiblob::BlobException& b)
45907f3a70SPatrick Venture     {
46*0d5bb784SWilliam A. Kennington III         progress->abort();
47907f3a70SPatrick Venture         sys->close(inputFd);
48907f3a70SPatrick Venture         return false;
49907f3a70SPatrick Venture     }
50907f3a70SPatrick Venture 
51*0d5bb784SWilliam A. Kennington III     progress->finish();
52907f3a70SPatrick Venture     sys->close(inputFd);
53907f3a70SPatrick Venture     return true;
54907f3a70SPatrick Venture }
55907f3a70SPatrick Venture 
569b534f06SPatrick Venture } // namespace host_tool
57