blob: 3cd641a43f39638c267729998c3e2a2a18ef675c [file] [log] [blame]
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -07001/*
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
Patrick Venture84778b82019-06-26 20:11:09 -070019#include "data.hpp"
20#include "flags.hpp"
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070021#include "pci.hpp"
Benjamin Faire5aafa52020-06-05 21:04:24 -070022#include "tool_errors.hpp"
23
24#include <fmt/format.h>
Patrick Venturec73dce92019-05-07 11:32:44 -070025
Patrick Venture9b37b092020-05-28 20:58:57 -070026#include <ipmiblob/blob_errors.hpp>
Benjamin Faire5aafa52020-06-05 21:04:24 -070027#include <stdplus/handle/managed.hpp>
Patrick Venture9b37b092020-05-28 20:58:57 -070028
Patrick Venture84778b82019-06-26 20:11:09 -070029#include <cstdint>
Patrick Venturec73dce92019-05-07 11:32:44 -070030#include <cstring>
Patrick Venture84778b82019-06-26 20:11:09 -070031#include <memory>
Patrick Williams28c00d62022-04-27 08:37:13 -050032#include <span>
Patrick Venture84778b82019-06-26 20:11:09 -070033#include <string>
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070034
35namespace host_tool
36{
37
Patrick Venturecf0e5de2019-07-26 08:25:33 -070038namespace
39{
Patrick Venturecf0e5de2019-07-26 08:25:33 -070040
Benjamin Faire5aafa52020-06-05 21:04:24 -070041/** @brief RAII wrapper and its destructor for opening a file descriptor */
42static void closeFd(int&& fd, const internal::Sys* const& sys)
43{
44 sys->close(fd);
Patrick Venturecf0e5de2019-07-26 08:25:33 -070045}
Benjamin Faire5aafa52020-06-05 21:04:24 -070046using Fd = stdplus::Managed<int, const internal::Sys* const>::Handle<closeFd>;
Patrick Venturecf0e5de2019-07-26 08:25:33 -070047
48} // namespace
49
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070050bool P2aDataHandler::sendContents(const std::string& input,
51 std::uint16_t session)
52{
Benjamin Faire5aafa52020-06-05 21:04:24 -070053 std::unique_ptr<PciBridgeIntf> bridge;
Patrick Venturecf0e5de2019-07-26 08:25:33 -070054 ipmi_flash::PciConfigResponse pciResp;
55 std::int64_t fileSize;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070056
Benjamin Faire5aafa52020-06-05 21:04:24 -070057 try
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070058 {
Willy Tu8a9de242020-10-30 11:00:21 -070059 bridge = std::make_unique<NuvotonPciBridge>(pci, skipBridgeDisable);
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070060 }
Patrick Williams665905f2021-10-06 14:43:31 -050061 catch (const NotFoundException& e)
Benjamin Faire5aafa52020-06-05 21:04:24 -070062 {}
Patrick Venture36bb4672019-05-07 09:42:56 -070063
Benjamin Faire5aafa52020-06-05 21:04:24 -070064 try
Patrick Venture36bb4672019-05-07 09:42:56 -070065 {
Willy Tu8a9de242020-10-30 11:00:21 -070066 bridge = std::make_unique<AspeedPciBridge>(pci, skipBridgeDisable);
Patrick Venture36bb4672019-05-07 09:42:56 -070067 }
Patrick Williams665905f2021-10-06 14:43:31 -050068 catch (const NotFoundException& e)
Benjamin Faire5aafa52020-06-05 21:04:24 -070069 {}
Patrick Venture36bb4672019-05-07 09:42:56 -070070
Benjamin Faire5aafa52020-06-05 21:04:24 -070071 if (!bridge)
Patrick Venture24141612019-05-03 17:59:18 -070072 {
Benjamin Faire5aafa52020-06-05 21:04:24 -070073 throw NotFoundException("supported PCI device");
Medad CChienc8445aa2020-04-23 09:47:46 +080074 }
Patrick Venture24141612019-05-03 17:59:18 -070075
76 /* Read the configuration via blobs metadata (stat). */
Patrick Venturec73dce92019-05-07 11:32:44 -070077 ipmiblob::StatResponse stat = blob->getStat(session);
Patrick Venture1d5a31c2019-05-20 11:38:22 -070078 if (stat.metadata.size() != sizeof(ipmi_flash::PciConfigResponse))
Patrick Venturec73dce92019-05-07 11:32:44 -070079 {
Benjamin Faire5aafa52020-06-05 21:04:24 -070080 throw ToolException("Didn't receive expected size of metadata for "
81 "PCI Configuration response");
Patrick Venturec73dce92019-05-07 11:32:44 -070082 }
83
Patrick Venturec73dce92019-05-07 11:32:44 -070084 std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp));
Benjamin Faire5aafa52020-06-05 21:04:24 -070085 bridge->configure(pciResp);
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070086
Patrick Venture18bbe3c2019-05-14 11:40:57 -070087 /* For data blocks in 64kb, stage data, and send blob write command. */
Benjamin Faire5aafa52020-06-05 21:04:24 -070088 Fd inputFd(sys->open(input.c_str(), 0), sys);
89 if (*inputFd < 0)
Patrick Venture18bbe3c2019-05-14 11:40:57 -070090 {
Benjamin Faire5aafa52020-06-05 21:04:24 -070091 (void)inputFd.release();
92 throw internal::errnoException(
93 fmt::format("Error opening file '{}'", input));
Patrick Venture18bbe3c2019-05-14 11:40:57 -070094 }
95
Patrick Venturecf0e5de2019-07-26 08:25:33 -070096 fileSize = sys->getSize(input.c_str());
Patrick Venturecf9b2192019-06-27 12:09:52 -070097 progress->start(fileSize);
98
Benjamin Faire5aafa52020-06-05 21:04:24 -070099 std::vector<std::uint8_t> readBuffer(bridge->getDataLength());
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700100
Benjamin Faire5aafa52020-06-05 21:04:24 -0700101 int bytesRead = 0;
102 std::uint32_t offset = 0;
Patrick Venture63528042019-05-20 18:10:02 -0700103
Benjamin Faire5aafa52020-06-05 21:04:24 -0700104 do
105 {
106 bytesRead = sys->read(*inputFd, readBuffer.data(), readBuffer.size());
107 if (bytesRead > 0)
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700108 {
Patrick Williams28c00d62022-04-27 08:37:13 -0500109 bridge->write(
110 std::span<const std::uint8_t>(readBuffer.data(), bytesRead));
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700111
Benjamin Faire5aafa52020-06-05 21:04:24 -0700112 /* Ok, so the data is staged, now send the blob write with the
113 * details.
114 */
115 struct ipmi_flash::ExtChunkHdr chunk;
116 chunk.length = bytesRead;
117 std::vector<std::uint8_t> chunkBytes(sizeof(chunk));
118 std::memcpy(chunkBytes.data(), &chunk, sizeof(chunk));
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700119
Benjamin Faire5aafa52020-06-05 21:04:24 -0700120 /* This doesn't return anything on success. */
121 blob->writeBytes(session, offset, chunkBytes);
122 offset += bytesRead;
123 progress->updateProgress(bytesRead);
124 }
125 } while (bytesRead > 0);
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700126
William A. Kennington III0d5bb782021-01-19 15:50:42 -0800127 progress->finish();
Benjamin Faire5aafa52020-06-05 21:04:24 -0700128 return true;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700129}
130
131} // namespace host_tool