1b5bf0fc2SPatrick Venture /* 2b5bf0fc2SPatrick Venture * Copyright 2019 Google Inc. 3b5bf0fc2SPatrick Venture * 4b5bf0fc2SPatrick Venture * Licensed under the Apache License, Version 2.0 (the "License"); 5b5bf0fc2SPatrick Venture * you may not use this file except in compliance with the License. 6b5bf0fc2SPatrick Venture * You may obtain a copy of the License at 7b5bf0fc2SPatrick Venture * 8b5bf0fc2SPatrick Venture * http://www.apache.org/licenses/LICENSE-2.0 9b5bf0fc2SPatrick Venture * 10b5bf0fc2SPatrick Venture * Unless required by applicable law or agreed to in writing, software 11b5bf0fc2SPatrick Venture * distributed under the License is distributed on an "AS IS" BASIS, 12b5bf0fc2SPatrick Venture * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b5bf0fc2SPatrick Venture * See the License for the specific language governing permissions and 14b5bf0fc2SPatrick Venture * limitations under the License. 15b5bf0fc2SPatrick Venture */ 16b5bf0fc2SPatrick Venture 17b5bf0fc2SPatrick Venture #include "p2a.hpp" 18b5bf0fc2SPatrick Venture 1984778b8dSPatrick Venture #include "data.hpp" 2084778b8dSPatrick Venture #include "flags.hpp" 21b5bf0fc2SPatrick Venture #include "pci.hpp" 22e5aafa5bSBenjamin Fair #include "tool_errors.hpp" 23e5aafa5bSBenjamin Fair 24e5aafa5bSBenjamin Fair #include <fmt/format.h> 25c73dce91SPatrick Venture 269b37b095SPatrick Venture #include <ipmiblob/blob_errors.hpp> 27e5aafa5bSBenjamin Fair #include <stdplus/handle/managed.hpp> 289b37b095SPatrick Venture 2984778b8dSPatrick Venture #include <cstdint> 30c73dce91SPatrick Venture #include <cstring> 3184778b8dSPatrick Venture #include <memory> 3284778b8dSPatrick Venture #include <string> 33b5bf0fc2SPatrick Venture 34b5bf0fc2SPatrick Venture namespace host_tool 35b5bf0fc2SPatrick Venture { 36b5bf0fc2SPatrick Venture 37cf0e5de3SPatrick Venture namespace 38cf0e5de3SPatrick Venture { 39cf0e5de3SPatrick Venture 40e5aafa5bSBenjamin Fair /** @brief RAII wrapper and its destructor for opening a file descriptor */ 41e5aafa5bSBenjamin Fair static void closeFd(int&& fd, const internal::Sys* const& sys) 42e5aafa5bSBenjamin Fair { 43e5aafa5bSBenjamin Fair sys->close(fd); 44cf0e5de3SPatrick Venture } 45e5aafa5bSBenjamin Fair using Fd = stdplus::Managed<int, const internal::Sys* const>::Handle<closeFd>; 46cf0e5de3SPatrick Venture 47cf0e5de3SPatrick Venture } // namespace 48cf0e5de3SPatrick Venture 49b5bf0fc2SPatrick Venture bool P2aDataHandler::sendContents(const std::string& input, 50b5bf0fc2SPatrick Venture std::uint16_t session) 51b5bf0fc2SPatrick Venture { 52e5aafa5bSBenjamin Fair std::unique_ptr<PciBridgeIntf> bridge; 53cf0e5de3SPatrick Venture ipmi_flash::PciConfigResponse pciResp; 54cf0e5de3SPatrick Venture std::int64_t fileSize; 55b5bf0fc2SPatrick Venture 56e5aafa5bSBenjamin Fair try 57c8445aaaSMedad CChien { 58*8a9de245SWilly Tu bridge = std::make_unique<NuvotonPciBridge>(pci, skipBridgeDisable); 59b5bf0fc2SPatrick Venture } 60e5aafa5bSBenjamin Fair catch (NotFoundException& e) 61e5aafa5bSBenjamin Fair {} 62b5bf0fc2SPatrick Venture 63e5aafa5bSBenjamin Fair try 64c8445aaaSMedad CChien { 65*8a9de245SWilly Tu bridge = std::make_unique<AspeedPciBridge>(pci, skipBridgeDisable); 66c8445aaaSMedad CChien } 67e5aafa5bSBenjamin Fair catch (NotFoundException& e) 68e5aafa5bSBenjamin Fair {} 69c8445aaaSMedad CChien 70e5aafa5bSBenjamin Fair if (!bridge) 7136bb4670SPatrick Venture { 72e5aafa5bSBenjamin Fair throw NotFoundException("supported PCI device"); 73c8445aaaSMedad CChien } 7424141611SPatrick Venture 7524141611SPatrick Venture /* Read the configuration via blobs metadata (stat). */ 76c73dce91SPatrick Venture ipmiblob::StatResponse stat = blob->getStat(session); 771d5a31c9SPatrick Venture if (stat.metadata.size() != sizeof(ipmi_flash::PciConfigResponse)) 78c73dce91SPatrick Venture { 79e5aafa5bSBenjamin Fair throw ToolException("Didn't receive expected size of metadata for " 80e5aafa5bSBenjamin Fair "PCI Configuration response"); 81c73dce91SPatrick Venture } 82c73dce91SPatrick Venture 83c73dce91SPatrick Venture std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp)); 84e5aafa5bSBenjamin Fair bridge->configure(pciResp); 8524141611SPatrick Venture 8618bbe3c6SPatrick Venture /* For data blocks in 64kb, stage data, and send blob write command. */ 87e5aafa5bSBenjamin Fair Fd inputFd(sys->open(input.c_str(), 0), sys); 88e5aafa5bSBenjamin Fair if (*inputFd < 0) 8918bbe3c6SPatrick Venture { 90e5aafa5bSBenjamin Fair (void)inputFd.release(); 91e5aafa5bSBenjamin Fair throw internal::errnoException( 92e5aafa5bSBenjamin Fair fmt::format("Error opening file '{}'", input)); 93b5bf0fc2SPatrick Venture } 94b5bf0fc2SPatrick Venture 95cf0e5de3SPatrick Venture fileSize = sys->getSize(input.c_str()); 96cf9b2195SPatrick Venture if (fileSize == 0) 97cf9b2195SPatrick Venture { 98e5aafa5bSBenjamin Fair throw ToolException("Zero-length file, or other file access error"); 99cf9b2195SPatrick Venture } 100cf9b2195SPatrick Venture 101cf9b2195SPatrick Venture progress->start(fileSize); 102cf9b2195SPatrick Venture 103e5aafa5bSBenjamin Fair std::vector<std::uint8_t> readBuffer(bridge->getDataLength()); 10418bbe3c6SPatrick Venture 10563528046SPatrick Venture int bytesRead = 0; 10663528046SPatrick Venture std::uint32_t offset = 0; 10763528046SPatrick Venture 10818bbe3c6SPatrick Venture do 10918bbe3c6SPatrick Venture { 110e5aafa5bSBenjamin Fair bytesRead = sys->read(*inputFd, readBuffer.data(), readBuffer.size()); 11118bbe3c6SPatrick Venture if (bytesRead > 0) 11218bbe3c6SPatrick Venture { 113e5aafa5bSBenjamin Fair bridge->write(stdplus::span<const std::uint8_t>(readBuffer.data(), 114e5aafa5bSBenjamin Fair bytesRead)); 11518bbe3c6SPatrick Venture 116c8445aaaSMedad CChien /* Ok, so the data is staged, now send the blob write with the 117c8445aaaSMedad CChien * details. 11818bbe3c6SPatrick Venture */ 1191d5a31c9SPatrick Venture struct ipmi_flash::ExtChunkHdr chunk; 12018bbe3c6SPatrick Venture chunk.length = bytesRead; 12118bbe3c6SPatrick Venture std::vector<std::uint8_t> chunkBytes(sizeof(chunk)); 12218bbe3c6SPatrick Venture std::memcpy(chunkBytes.data(), &chunk, sizeof(chunk)); 12318bbe3c6SPatrick Venture 12418bbe3c6SPatrick Venture /* This doesn't return anything on success. */ 12518bbe3c6SPatrick Venture blob->writeBytes(session, offset, chunkBytes); 12618bbe3c6SPatrick Venture offset += bytesRead; 127cf9b2195SPatrick Venture progress->updateProgress(bytesRead); 12818bbe3c6SPatrick Venture } 12918bbe3c6SPatrick Venture } while (bytesRead > 0); 13018bbe3c6SPatrick Venture 131e5aafa5bSBenjamin Fair return true; 13218bbe3c6SPatrick Venture } 13318bbe3c6SPatrick Venture 134b5bf0fc2SPatrick Venture } // namespace host_tool 135