xref: /openbmc/phosphor-ipmi-flash/tools/p2a.cpp (revision 14c78ed1)
1 /*
2  * Copyright 2019 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "p2a.hpp"
18 
19 #include "data.hpp"
20 #include "flags.hpp"
21 #include "pci.hpp"
22 #include "tool_errors.hpp"
23 
24 #include <ipmiblob/blob_errors.hpp>
25 #include <stdplus/handle/managed.hpp>
26 
27 #include <cstdint>
28 #include <cstring>
29 #include <format>
30 #include <memory>
31 #include <span>
32 #include <string>
33 
34 namespace host_tool
35 {
36 
37 namespace
38 {
39 
40 /** @brief RAII wrapper and its destructor for opening a file descriptor */
closeFd(int && fd,const internal::Sys * const & sys)41 static void closeFd(int&& fd, const internal::Sys* const& sys)
42 {
43     sys->close(fd);
44 }
45 using Fd = stdplus::Managed<int, const internal::Sys* const>::Handle<closeFd>;
46 
47 } // namespace
48 
sendContents(const std::string & input,std::uint16_t session)49 bool P2aDataHandler::sendContents(const std::string& input,
50                                   std::uint16_t session)
51 {
52     std::unique_ptr<PciBridgeIntf> bridge;
53     ipmi_flash::PciConfigResponse pciResp;
54     std::int64_t fileSize;
55 
56     try
57     {
58         bridge = std::make_unique<NuvotonPciBridge>(pci, skipBridgeDisable);
59     }
60     catch (const NotFoundException& e)
61     {}
62 
63     try
64     {
65         bridge = std::make_unique<AspeedPciBridge>(pci, skipBridgeDisable);
66     }
67     catch (const NotFoundException& e)
68     {}
69 
70     if (!bridge)
71     {
72         throw NotFoundException("supported PCI device");
73     }
74 
75     /* Read the configuration via blobs metadata (stat). */
76     ipmiblob::StatResponse stat = blob->getStat(session);
77     if (stat.metadata.size() != sizeof(ipmi_flash::PciConfigResponse))
78     {
79         throw ToolException("Didn't receive expected size of metadata for "
80                             "PCI Configuration response");
81     }
82 
83     std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp));
84     bridge->configure(pciResp);
85 
86     /* For data blocks in 64kb, stage data, and send blob write command. */
87     Fd inputFd(sys->open(input.c_str(), 0), sys);
88     if (*inputFd < 0)
89     {
90         (void)inputFd.release();
91         throw internal::errnoException(
92             std::format("Error opening file '{}'", input));
93     }
94 
95     fileSize = sys->getSize(input.c_str());
96     progress->start(fileSize);
97 
98     std::vector<std::uint8_t> readBuffer(bridge->getDataLength());
99 
100     int bytesRead = 0;
101     std::uint32_t offset = 0;
102 
103     do
104     {
105         bytesRead = sys->read(*inputFd, readBuffer.data(), readBuffer.size());
106         if (bytesRead > 0)
107         {
108             bridge->write(
109                 std::span<const std::uint8_t>(readBuffer.data(), bytesRead));
110 
111             /* Ok, so the data is staged, now send the blob write with the
112              * details.
113              */
114             struct ipmi_flash::ExtChunkHdr chunk;
115             chunk.length = bytesRead;
116             std::vector<std::uint8_t> chunkBytes(sizeof(chunk));
117             std::memcpy(chunkBytes.data(), &chunk, sizeof(chunk));
118 
119             /* This doesn't return anything on success. */
120             blob->writeBytes(session, offset, chunkBytes);
121             offset += bytesRead;
122             progress->updateProgress(bytesRead);
123         }
124     } while (bytesRead > 0);
125 
126     progress->finish();
127     return true;
128 }
129 
130 } // namespace host_tool
131