Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 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 | |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 19 | #include "data.hpp" |
| 20 | #include "flags.hpp" |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 21 | #include "pci.hpp" |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 22 | #include "tool_errors.hpp" |
| 23 | |
| 24 | #include <fmt/format.h> |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 25 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 26 | #include <ipmiblob/blob_errors.hpp> |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 27 | #include <stdplus/handle/managed.hpp> |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 28 | |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 29 | #include <cstdint> |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 30 | #include <cstring> |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 31 | #include <memory> |
Patrick Williams | 28c00d6 | 2022-04-27 08:37:13 -0500 | [diff] [blame^] | 32 | #include <span> |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 33 | #include <string> |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 34 | |
| 35 | namespace host_tool |
| 36 | { |
| 37 | |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 38 | namespace |
| 39 | { |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 40 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 41 | /** @brief RAII wrapper and its destructor for opening a file descriptor */ |
| 42 | static void closeFd(int&& fd, const internal::Sys* const& sys) |
| 43 | { |
| 44 | sys->close(fd); |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 45 | } |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 46 | using Fd = stdplus::Managed<int, const internal::Sys* const>::Handle<closeFd>; |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 47 | |
| 48 | } // namespace |
| 49 | |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 50 | bool P2aDataHandler::sendContents(const std::string& input, |
| 51 | std::uint16_t session) |
| 52 | { |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 53 | std::unique_ptr<PciBridgeIntf> bridge; |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 54 | ipmi_flash::PciConfigResponse pciResp; |
| 55 | std::int64_t fileSize; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 56 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 57 | try |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 58 | { |
Willy Tu | 8a9de24 | 2020-10-30 11:00:21 -0700 | [diff] [blame] | 59 | bridge = std::make_unique<NuvotonPciBridge>(pci, skipBridgeDisable); |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 60 | } |
Patrick Williams | 665905f | 2021-10-06 14:43:31 -0500 | [diff] [blame] | 61 | catch (const NotFoundException& e) |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 62 | {} |
Patrick Venture | 36bb467 | 2019-05-07 09:42:56 -0700 | [diff] [blame] | 63 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 64 | try |
Patrick Venture | 36bb467 | 2019-05-07 09:42:56 -0700 | [diff] [blame] | 65 | { |
Willy Tu | 8a9de24 | 2020-10-30 11:00:21 -0700 | [diff] [blame] | 66 | bridge = std::make_unique<AspeedPciBridge>(pci, skipBridgeDisable); |
Patrick Venture | 36bb467 | 2019-05-07 09:42:56 -0700 | [diff] [blame] | 67 | } |
Patrick Williams | 665905f | 2021-10-06 14:43:31 -0500 | [diff] [blame] | 68 | catch (const NotFoundException& e) |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 69 | {} |
Patrick Venture | 36bb467 | 2019-05-07 09:42:56 -0700 | [diff] [blame] | 70 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 71 | if (!bridge) |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 72 | { |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 73 | throw NotFoundException("supported PCI device"); |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 74 | } |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 75 | |
| 76 | /* Read the configuration via blobs metadata (stat). */ |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 77 | ipmiblob::StatResponse stat = blob->getStat(session); |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 78 | if (stat.metadata.size() != sizeof(ipmi_flash::PciConfigResponse)) |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 79 | { |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 80 | throw ToolException("Didn't receive expected size of metadata for " |
| 81 | "PCI Configuration response"); |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 84 | std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp)); |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 85 | bridge->configure(pciResp); |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 86 | |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 87 | /* For data blocks in 64kb, stage data, and send blob write command. */ |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 88 | Fd inputFd(sys->open(input.c_str(), 0), sys); |
| 89 | if (*inputFd < 0) |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 90 | { |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 91 | (void)inputFd.release(); |
| 92 | throw internal::errnoException( |
| 93 | fmt::format("Error opening file '{}'", input)); |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 96 | fileSize = sys->getSize(input.c_str()); |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 97 | progress->start(fileSize); |
| 98 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 99 | std::vector<std::uint8_t> readBuffer(bridge->getDataLength()); |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 100 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 101 | int bytesRead = 0; |
| 102 | std::uint32_t offset = 0; |
Patrick Venture | 6352804 | 2019-05-20 18:10:02 -0700 | [diff] [blame] | 103 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 104 | do |
| 105 | { |
| 106 | bytesRead = sys->read(*inputFd, readBuffer.data(), readBuffer.size()); |
| 107 | if (bytesRead > 0) |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 108 | { |
Patrick Williams | 28c00d6 | 2022-04-27 08:37:13 -0500 | [diff] [blame^] | 109 | bridge->write( |
| 110 | std::span<const std::uint8_t>(readBuffer.data(), bytesRead)); |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 111 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 112 | /* 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 Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 119 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 120 | /* 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 Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 126 | |
William A. Kennington III | 0d5bb78 | 2021-01-19 15:50:42 -0800 | [diff] [blame] | 127 | progress->finish(); |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 128 | return true; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | } // namespace host_tool |