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> |
| 32 | #include <string> |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 33 | |
| 34 | namespace host_tool |
| 35 | { |
| 36 | |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 37 | namespace |
| 38 | { |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 39 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 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); |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 44 | } |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 45 | using Fd = stdplus::Managed<int, const internal::Sys* const>::Handle<closeFd>; |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 46 | |
| 47 | } // namespace |
| 48 | |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 49 | bool P2aDataHandler::sendContents(const std::string& input, |
| 50 | std::uint16_t session) |
| 51 | { |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 52 | std::unique_ptr<PciBridgeIntf> bridge; |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 53 | ipmi_flash::PciConfigResponse pciResp; |
| 54 | std::int64_t fileSize; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 55 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 56 | try |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 57 | { |
Willy Tu | 8a9de24 | 2020-10-30 11:00:21 -0700 | [diff] [blame] | 58 | bridge = std::make_unique<NuvotonPciBridge>(pci, skipBridgeDisable); |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 59 | } |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 60 | catch (NotFoundException& e) |
| 61 | {} |
Patrick Venture | 36bb467 | 2019-05-07 09:42:56 -0700 | [diff] [blame] | 62 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 63 | try |
Patrick Venture | 36bb467 | 2019-05-07 09:42:56 -0700 | [diff] [blame] | 64 | { |
Willy Tu | 8a9de24 | 2020-10-30 11:00:21 -0700 | [diff] [blame] | 65 | bridge = std::make_unique<AspeedPciBridge>(pci, skipBridgeDisable); |
Patrick Venture | 36bb467 | 2019-05-07 09:42:56 -0700 | [diff] [blame] | 66 | } |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 67 | catch (NotFoundException& e) |
| 68 | {} |
Patrick Venture | 36bb467 | 2019-05-07 09:42:56 -0700 | [diff] [blame] | 69 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 70 | if (!bridge) |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 71 | { |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 72 | throw NotFoundException("supported PCI device"); |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 73 | } |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 74 | |
| 75 | /* Read the configuration via blobs metadata (stat). */ |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 76 | ipmiblob::StatResponse stat = blob->getStat(session); |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 77 | if (stat.metadata.size() != sizeof(ipmi_flash::PciConfigResponse)) |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 78 | { |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 79 | throw ToolException("Didn't receive expected size of metadata for " |
| 80 | "PCI Configuration response"); |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 83 | std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp)); |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 84 | bridge->configure(pciResp); |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 85 | |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 86 | /* For data blocks in 64kb, stage data, and send blob write command. */ |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 87 | Fd inputFd(sys->open(input.c_str(), 0), sys); |
| 88 | if (*inputFd < 0) |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 89 | { |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 90 | (void)inputFd.release(); |
| 91 | throw internal::errnoException( |
| 92 | fmt::format("Error opening file '{}'", input)); |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 95 | fileSize = sys->getSize(input.c_str()); |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 96 | if (fileSize == 0) |
| 97 | { |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 98 | throw ToolException("Zero-length file, or other file access error"); |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | progress->start(fileSize); |
| 102 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 103 | std::vector<std::uint8_t> readBuffer(bridge->getDataLength()); |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 104 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 105 | int bytesRead = 0; |
| 106 | std::uint32_t offset = 0; |
Patrick Venture | 6352804 | 2019-05-20 18:10:02 -0700 | [diff] [blame] | 107 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 108 | do |
| 109 | { |
| 110 | bytesRead = sys->read(*inputFd, readBuffer.data(), readBuffer.size()); |
| 111 | if (bytesRead > 0) |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 112 | { |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 113 | bridge->write(stdplus::span<const std::uint8_t>(readBuffer.data(), |
| 114 | bytesRead)); |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 115 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 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)); |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 123 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 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); |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 130 | |
Benjamin Fair | e5aafa5 | 2020-06-05 21:04:24 -0700 | [diff] [blame] | 131 | return true; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | } // namespace host_tool |