blob: b92887f48562463a8a10acaf24917806e57a4303 [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>
32#include <string>
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070033
34namespace host_tool
35{
36
Patrick Venturecf0e5de2019-07-26 08:25:33 -070037namespace
38{
Patrick Venturecf0e5de2019-07-26 08:25:33 -070039
Benjamin Faire5aafa52020-06-05 21:04:24 -070040/** @brief RAII wrapper and its destructor for opening a file descriptor */
41static void closeFd(int&& fd, const internal::Sys* const& sys)
42{
43 sys->close(fd);
Patrick Venturecf0e5de2019-07-26 08:25:33 -070044}
Benjamin Faire5aafa52020-06-05 21:04:24 -070045using Fd = stdplus::Managed<int, const internal::Sys* const>::Handle<closeFd>;
Patrick Venturecf0e5de2019-07-26 08:25:33 -070046
47} // namespace
48
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070049bool P2aDataHandler::sendContents(const std::string& input,
50 std::uint16_t session)
51{
Benjamin Faire5aafa52020-06-05 21:04:24 -070052 std::unique_ptr<PciBridgeIntf> bridge;
Patrick Venturecf0e5de2019-07-26 08:25:33 -070053 ipmi_flash::PciConfigResponse pciResp;
54 std::int64_t fileSize;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070055
Benjamin Faire5aafa52020-06-05 21:04:24 -070056 try
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070057 {
Willy Tu8a9de242020-10-30 11:00:21 -070058 bridge = std::make_unique<NuvotonPciBridge>(pci, skipBridgeDisable);
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070059 }
Benjamin Faire5aafa52020-06-05 21:04:24 -070060 catch (NotFoundException& e)
61 {}
Patrick Venture36bb4672019-05-07 09:42:56 -070062
Benjamin Faire5aafa52020-06-05 21:04:24 -070063 try
Patrick Venture36bb4672019-05-07 09:42:56 -070064 {
Willy Tu8a9de242020-10-30 11:00:21 -070065 bridge = std::make_unique<AspeedPciBridge>(pci, skipBridgeDisable);
Patrick Venture36bb4672019-05-07 09:42:56 -070066 }
Benjamin Faire5aafa52020-06-05 21:04:24 -070067 catch (NotFoundException& e)
68 {}
Patrick Venture36bb4672019-05-07 09:42:56 -070069
Benjamin Faire5aafa52020-06-05 21:04:24 -070070 if (!bridge)
Patrick Venture24141612019-05-03 17:59:18 -070071 {
Benjamin Faire5aafa52020-06-05 21:04:24 -070072 throw NotFoundException("supported PCI device");
Medad CChienc8445aa2020-04-23 09:47:46 +080073 }
Patrick Venture24141612019-05-03 17:59:18 -070074
75 /* Read the configuration via blobs metadata (stat). */
Patrick Venturec73dce92019-05-07 11:32:44 -070076 ipmiblob::StatResponse stat = blob->getStat(session);
Patrick Venture1d5a31c2019-05-20 11:38:22 -070077 if (stat.metadata.size() != sizeof(ipmi_flash::PciConfigResponse))
Patrick Venturec73dce92019-05-07 11:32:44 -070078 {
Benjamin Faire5aafa52020-06-05 21:04:24 -070079 throw ToolException("Didn't receive expected size of metadata for "
80 "PCI Configuration response");
Patrick Venturec73dce92019-05-07 11:32:44 -070081 }
82
Patrick Venturec73dce92019-05-07 11:32:44 -070083 std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp));
Benjamin Faire5aafa52020-06-05 21:04:24 -070084 bridge->configure(pciResp);
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070085
Patrick Venture18bbe3c2019-05-14 11:40:57 -070086 /* For data blocks in 64kb, stage data, and send blob write command. */
Benjamin Faire5aafa52020-06-05 21:04:24 -070087 Fd inputFd(sys->open(input.c_str(), 0), sys);
88 if (*inputFd < 0)
Patrick Venture18bbe3c2019-05-14 11:40:57 -070089 {
Benjamin Faire5aafa52020-06-05 21:04:24 -070090 (void)inputFd.release();
91 throw internal::errnoException(
92 fmt::format("Error opening file '{}'", input));
Patrick Venture18bbe3c2019-05-14 11:40:57 -070093 }
94
Patrick Venturecf0e5de2019-07-26 08:25:33 -070095 fileSize = sys->getSize(input.c_str());
Patrick Venturecf9b2192019-06-27 12:09:52 -070096 if (fileSize == 0)
97 {
Benjamin Faire5aafa52020-06-05 21:04:24 -070098 throw ToolException("Zero-length file, or other file access error");
Patrick Venturecf9b2192019-06-27 12:09:52 -070099 }
100
101 progress->start(fileSize);
102
Benjamin Faire5aafa52020-06-05 21:04:24 -0700103 std::vector<std::uint8_t> readBuffer(bridge->getDataLength());
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700104
Benjamin Faire5aafa52020-06-05 21:04:24 -0700105 int bytesRead = 0;
106 std::uint32_t offset = 0;
Patrick Venture63528042019-05-20 18:10:02 -0700107
Benjamin Faire5aafa52020-06-05 21:04:24 -0700108 do
109 {
110 bytesRead = sys->read(*inputFd, readBuffer.data(), readBuffer.size());
111 if (bytesRead > 0)
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700112 {
Benjamin Faire5aafa52020-06-05 21:04:24 -0700113 bridge->write(stdplus::span<const std::uint8_t>(readBuffer.data(),
114 bytesRead));
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700115
Benjamin Faire5aafa52020-06-05 21:04:24 -0700116 /* 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));
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700123
Benjamin Faire5aafa52020-06-05 21:04:24 -0700124 /* 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);
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700130
William A. Kennington III0d5bb782021-01-19 15:50:42 -0800131 progress->finish();
Benjamin Faire5aafa52020-06-05 21:04:24 -0700132 return true;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700133}
134
135} // namespace host_tool