xref: /openbmc/phosphor-ipmi-flash/tools/p2a.cpp (revision f450486f)
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 <fmt/format.h>
25 
26 #include <ipmiblob/blob_errors.hpp>
27 #include <stdplus/handle/managed.hpp>
28 
29 #include <cstdint>
30 #include <cstring>
31 #include <memory>
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 */
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 
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 (NotFoundException& e)
61     {}
62 
63     try
64     {
65         bridge = std::make_unique<AspeedPciBridge>(pci, skipBridgeDisable);
66     }
67     catch (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             fmt::format("Error opening file '{}'", input));
93     }
94 
95     fileSize = sys->getSize(input.c_str());
96     if (fileSize == 0)
97     {
98         throw ToolException("Zero-length file, or other file access error");
99     }
100 
101     progress->start(fileSize);
102 
103     std::vector<std::uint8_t> readBuffer(bridge->getDataLength());
104 
105     int bytesRead = 0;
106     std::uint32_t offset = 0;
107 
108     do
109     {
110         bytesRead = sys->read(*inputFd, readBuffer.data(), readBuffer.size());
111         if (bytesRead > 0)
112         {
113             bridge->write(stdplus::span<const std::uint8_t>(readBuffer.data(),
114                                                             bytesRead));
115 
116             /* Ok, so the data is staged, now send the blob write with the
117              * details.
118              */
119             struct ipmi_flash::ExtChunkHdr chunk;
120             chunk.length = bytesRead;
121             std::vector<std::uint8_t> chunkBytes(sizeof(chunk));
122             std::memcpy(chunkBytes.data(), &chunk, sizeof(chunk));
123 
124             /* This doesn't return anything on success. */
125             blob->writeBytes(session, offset, chunkBytes);
126             offset += bytesRead;
127             progress->updateProgress(bytesRead);
128         }
129     } while (bytesRead > 0);
130 
131     progress->finish();
132     return true;
133 }
134 
135 } // namespace host_tool
136