xref: /openbmc/phosphor-ipmi-flash/tools/p2a.cpp (revision 14c78ed1)
1b5bf0fc2SPatrick Venture /*
2b5bf0fc2SPatrick Venture  * Copyright 2019 Google Inc.
3b5bf0fc2SPatrick Venture  *
4b5bf0fc2SPatrick Venture  * Licensed under the Apache License, Version 2.0 (the "License");
5b5bf0fc2SPatrick Venture  * you may not use this file except in compliance with the License.
6b5bf0fc2SPatrick Venture  * You may obtain a copy of the License at
7b5bf0fc2SPatrick Venture  *
8b5bf0fc2SPatrick Venture  *     http://www.apache.org/licenses/LICENSE-2.0
9b5bf0fc2SPatrick Venture  *
10b5bf0fc2SPatrick Venture  * Unless required by applicable law or agreed to in writing, software
11b5bf0fc2SPatrick Venture  * distributed under the License is distributed on an "AS IS" BASIS,
12b5bf0fc2SPatrick Venture  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b5bf0fc2SPatrick Venture  * See the License for the specific language governing permissions and
14b5bf0fc2SPatrick Venture  * limitations under the License.
15b5bf0fc2SPatrick Venture  */
16b5bf0fc2SPatrick Venture 
17b5bf0fc2SPatrick Venture #include "p2a.hpp"
18b5bf0fc2SPatrick Venture 
1984778b8dSPatrick Venture #include "data.hpp"
2084778b8dSPatrick Venture #include "flags.hpp"
21b5bf0fc2SPatrick Venture #include "pci.hpp"
22e5aafa5bSBenjamin Fair #include "tool_errors.hpp"
23e5aafa5bSBenjamin Fair 
249b37b095SPatrick Venture #include <ipmiblob/blob_errors.hpp>
25e5aafa5bSBenjamin Fair #include <stdplus/handle/managed.hpp>
269b37b095SPatrick Venture 
2784778b8dSPatrick Venture #include <cstdint>
28c73dce91SPatrick Venture #include <cstring>
29*14c78ed1SPatrick Williams #include <format>
3084778b8dSPatrick Venture #include <memory>
3128c00d6eSPatrick Williams #include <span>
3284778b8dSPatrick Venture #include <string>
33b5bf0fc2SPatrick Venture 
34b5bf0fc2SPatrick Venture namespace host_tool
35b5bf0fc2SPatrick Venture {
36b5bf0fc2SPatrick Venture 
37cf0e5de3SPatrick Venture namespace
38cf0e5de3SPatrick Venture {
39cf0e5de3SPatrick Venture 
40e5aafa5bSBenjamin Fair /** @brief RAII wrapper and its destructor for opening a file descriptor */
closeFd(int && fd,const internal::Sys * const & sys)41e5aafa5bSBenjamin Fair static void closeFd(int&& fd, const internal::Sys* const& sys)
42e5aafa5bSBenjamin Fair {
43e5aafa5bSBenjamin Fair     sys->close(fd);
44cf0e5de3SPatrick Venture }
45e5aafa5bSBenjamin Fair using Fd = stdplus::Managed<int, const internal::Sys* const>::Handle<closeFd>;
46cf0e5de3SPatrick Venture 
47cf0e5de3SPatrick Venture } // namespace
48cf0e5de3SPatrick Venture 
sendContents(const std::string & input,std::uint16_t session)49b5bf0fc2SPatrick Venture bool P2aDataHandler::sendContents(const std::string& input,
50b5bf0fc2SPatrick Venture                                   std::uint16_t session)
51b5bf0fc2SPatrick Venture {
52e5aafa5bSBenjamin Fair     std::unique_ptr<PciBridgeIntf> bridge;
53cf0e5de3SPatrick Venture     ipmi_flash::PciConfigResponse pciResp;
54cf0e5de3SPatrick Venture     std::int64_t fileSize;
55b5bf0fc2SPatrick Venture 
56e5aafa5bSBenjamin Fair     try
57c8445aaaSMedad CChien     {
588a9de245SWilly Tu         bridge = std::make_unique<NuvotonPciBridge>(pci, skipBridgeDisable);
59b5bf0fc2SPatrick Venture     }
60665905ffSPatrick Williams     catch (const NotFoundException& e)
61e5aafa5bSBenjamin Fair     {}
62b5bf0fc2SPatrick Venture 
63e5aafa5bSBenjamin Fair     try
64c8445aaaSMedad CChien     {
658a9de245SWilly Tu         bridge = std::make_unique<AspeedPciBridge>(pci, skipBridgeDisable);
66c8445aaaSMedad CChien     }
67665905ffSPatrick Williams     catch (const NotFoundException& e)
68e5aafa5bSBenjamin Fair     {}
69c8445aaaSMedad CChien 
70e5aafa5bSBenjamin Fair     if (!bridge)
7136bb4670SPatrick Venture     {
72e5aafa5bSBenjamin Fair         throw NotFoundException("supported PCI device");
73c8445aaaSMedad CChien     }
7424141611SPatrick Venture 
7524141611SPatrick Venture     /* Read the configuration via blobs metadata (stat). */
76c73dce91SPatrick Venture     ipmiblob::StatResponse stat = blob->getStat(session);
771d5a31c9SPatrick Venture     if (stat.metadata.size() != sizeof(ipmi_flash::PciConfigResponse))
78c73dce91SPatrick Venture     {
79e5aafa5bSBenjamin Fair         throw ToolException("Didn't receive expected size of metadata for "
80e5aafa5bSBenjamin Fair                             "PCI Configuration response");
81c73dce91SPatrick Venture     }
82c73dce91SPatrick Venture 
83c73dce91SPatrick Venture     std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp));
84e5aafa5bSBenjamin Fair     bridge->configure(pciResp);
8524141611SPatrick Venture 
8618bbe3c6SPatrick Venture     /* For data blocks in 64kb, stage data, and send blob write command. */
87e5aafa5bSBenjamin Fair     Fd inputFd(sys->open(input.c_str(), 0), sys);
88e5aafa5bSBenjamin Fair     if (*inputFd < 0)
8918bbe3c6SPatrick Venture     {
90e5aafa5bSBenjamin Fair         (void)inputFd.release();
91e5aafa5bSBenjamin Fair         throw internal::errnoException(
92*14c78ed1SPatrick Williams             std::format("Error opening file '{}'", input));
93b5bf0fc2SPatrick Venture     }
94b5bf0fc2SPatrick Venture 
95cf0e5de3SPatrick Venture     fileSize = sys->getSize(input.c_str());
96cf9b2195SPatrick Venture     progress->start(fileSize);
97cf9b2195SPatrick Venture 
98e5aafa5bSBenjamin Fair     std::vector<std::uint8_t> readBuffer(bridge->getDataLength());
9918bbe3c6SPatrick Venture 
10063528046SPatrick Venture     int bytesRead = 0;
10163528046SPatrick Venture     std::uint32_t offset = 0;
10263528046SPatrick Venture 
10318bbe3c6SPatrick Venture     do
10418bbe3c6SPatrick Venture     {
105e5aafa5bSBenjamin Fair         bytesRead = sys->read(*inputFd, readBuffer.data(), readBuffer.size());
10618bbe3c6SPatrick Venture         if (bytesRead > 0)
10718bbe3c6SPatrick Venture         {
10828c00d6eSPatrick Williams             bridge->write(
10928c00d6eSPatrick Williams                 std::span<const std::uint8_t>(readBuffer.data(), bytesRead));
11018bbe3c6SPatrick Venture 
111c8445aaaSMedad CChien             /* Ok, so the data is staged, now send the blob write with the
112c8445aaaSMedad CChien              * details.
11318bbe3c6SPatrick Venture              */
1141d5a31c9SPatrick Venture             struct ipmi_flash::ExtChunkHdr chunk;
11518bbe3c6SPatrick Venture             chunk.length = bytesRead;
11618bbe3c6SPatrick Venture             std::vector<std::uint8_t> chunkBytes(sizeof(chunk));
11718bbe3c6SPatrick Venture             std::memcpy(chunkBytes.data(), &chunk, sizeof(chunk));
11818bbe3c6SPatrick Venture 
11918bbe3c6SPatrick Venture             /* This doesn't return anything on success. */
12018bbe3c6SPatrick Venture             blob->writeBytes(session, offset, chunkBytes);
12118bbe3c6SPatrick Venture             offset += bytesRead;
122cf9b2195SPatrick Venture             progress->updateProgress(bytesRead);
12318bbe3c6SPatrick Venture         }
12418bbe3c6SPatrick Venture     } while (bytesRead > 0);
12518bbe3c6SPatrick Venture 
1260d5bb784SWilliam A. Kennington III     progress->finish();
127e5aafa5bSBenjamin Fair     return true;
12818bbe3c6SPatrick Venture }
12918bbe3c6SPatrick Venture 
130b5bf0fc2SPatrick Venture } // namespace host_tool
131