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 19*84778b8dSPatrick Venture #include "data.hpp" 20*84778b8dSPatrick Venture #include "flags.hpp" 21b5bf0fc2SPatrick Venture #include "pci.hpp" 22c73dce91SPatrick Venture 23*84778b8dSPatrick Venture #include <cstdint> 24c73dce91SPatrick Venture #include <cstring> 2518bbe3c6SPatrick Venture #include <ipmiblob/blob_errors.hpp> 26*84778b8dSPatrick Venture #include <memory> 27*84778b8dSPatrick Venture #include <string> 28b5bf0fc2SPatrick Venture 29b5bf0fc2SPatrick Venture namespace host_tool 30b5bf0fc2SPatrick Venture { 31b5bf0fc2SPatrick Venture 32b5bf0fc2SPatrick Venture bool P2aDataHandler::sendContents(const std::string& input, 33b5bf0fc2SPatrick Venture std::uint16_t session) 34b5bf0fc2SPatrick Venture { 3524141611SPatrick Venture PciDevice result; 36b5bf0fc2SPatrick Venture PciFilter filter; 3736bb4670SPatrick Venture bool found = false; 3818bbe3c6SPatrick Venture pciaddr_t bar1; 39b5bf0fc2SPatrick Venture 40b5bf0fc2SPatrick Venture filter.vid = aspeedVendorId; 41b5bf0fc2SPatrick Venture filter.did = aspeedDeviceId; 42b5bf0fc2SPatrick Venture 4324141611SPatrick Venture /* Find the ASPEED PCI device entry we want. */ 4446e69491SPatrick Venture auto output = pci->getPciDevices(filter); 45b5bf0fc2SPatrick Venture for (const auto& d : output) 46b5bf0fc2SPatrick Venture { 4724141611SPatrick Venture std::fprintf(stderr, "[0x%x 0x%x] ", d.vid, d.did); 4824141611SPatrick Venture 4924141611SPatrick Venture /* Verify it's a memory-based bar -- we want bar1. */ 5018bbe3c6SPatrick Venture bar1 = d.bars[1]; 5124141611SPatrick Venture if ((bar1 & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) 5224141611SPatrick Venture { 5324141611SPatrick Venture /* We want it to not be IO-based access. */ 5424141611SPatrick Venture continue; 55b5bf0fc2SPatrick Venture } 56b5bf0fc2SPatrick Venture 5718bbe3c6SPatrick Venture /* For now capture the entire device even if we're only using BAR1 */ 5824141611SPatrick Venture result = d; 5936bb4670SPatrick Venture found = true; 6036bb4670SPatrick Venture break; 6124141611SPatrick Venture } 6236bb4670SPatrick Venture 6336bb4670SPatrick Venture if (!found) 6436bb4670SPatrick Venture { 6536bb4670SPatrick Venture return false; 6636bb4670SPatrick Venture } 6736bb4670SPatrick Venture 6824141611SPatrick Venture std::fprintf(stderr, "\n"); 6924141611SPatrick Venture 7024141611SPatrick Venture /* We sent the open command before this, so the window should be open and 7124141611SPatrick Venture * the bridge enabled. 7224141611SPatrick Venture */ 7324141611SPatrick Venture std::uint32_t value; 7418bbe3c6SPatrick Venture if (!io->read(bar1 | aspeedP2aConfig, sizeof(value), &value)) 7524141611SPatrick Venture { 7624141611SPatrick Venture if (0 == (value & p2ABridgeEnabled)) 7724141611SPatrick Venture { 7824141611SPatrick Venture std::fprintf(stderr, "Bridge not enabled.\n"); 7924141611SPatrick Venture return false; 8024141611SPatrick Venture } 8124141611SPatrick Venture } 8224141611SPatrick Venture 8324141611SPatrick Venture std::fprintf(stderr, "The bridge is enabled!\n"); 8424141611SPatrick Venture 8524141611SPatrick Venture /* Read the configuration via blobs metadata (stat). */ 86c73dce91SPatrick Venture ipmiblob::StatResponse stat = blob->getStat(session); 871d5a31c9SPatrick Venture if (stat.metadata.size() != sizeof(ipmi_flash::PciConfigResponse)) 88c73dce91SPatrick Venture { 89c73dce91SPatrick Venture std::fprintf(stderr, "Didn't receive expected size of metadata for " 90c73dce91SPatrick Venture "PCI Configuration response\n"); 91c73dce91SPatrick Venture return false; 92c73dce91SPatrick Venture } 93c73dce91SPatrick Venture 941d5a31c9SPatrick Venture ipmi_flash::PciConfigResponse pciResp; 95c73dce91SPatrick Venture std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp)); 96c73dce91SPatrick Venture std::fprintf(stderr, "Received address: 0x%x\n", pciResp.address); 9724141611SPatrick Venture 9824141611SPatrick Venture /* Configure the mmio to point there. */ 9918bbe3c6SPatrick Venture if (!io->write(bar1 | aspeedP2aBridge, sizeof(pciResp.address), 10018bbe3c6SPatrick Venture &pciResp.address)) 10118bbe3c6SPatrick Venture { 10224141611SPatrick Venture // Failed to set it up, so fall back. 10324141611SPatrick Venture std::fprintf(stderr, "Failed to update the bridge address\n"); 10424141611SPatrick Venture return false; 10524141611SPatrick Venture } 10624141611SPatrick Venture 10718bbe3c6SPatrick Venture /* For data blocks in 64kb, stage data, and send blob write command. */ 10818bbe3c6SPatrick Venture int inputFd = sys->open(input.c_str(), 0); 10918bbe3c6SPatrick Venture if (inputFd < 0) 11018bbe3c6SPatrick Venture { 111b5bf0fc2SPatrick Venture return false; 112b5bf0fc2SPatrick Venture } 113b5bf0fc2SPatrick Venture 11418bbe3c6SPatrick Venture const std::uint32_t p2aLength = aspeedP2aOffset; 11518bbe3c6SPatrick Venture 11618bbe3c6SPatrick Venture auto readBuffer = std::make_unique<std::uint8_t[]>(p2aLength); 11718bbe3c6SPatrick Venture if (nullptr == readBuffer) 11818bbe3c6SPatrick Venture { 11918bbe3c6SPatrick Venture std::fprintf(stderr, "Unable to allocate memory for read buffer.\n"); 12018bbe3c6SPatrick Venture return false; 12118bbe3c6SPatrick Venture } 12218bbe3c6SPatrick Venture 12318bbe3c6SPatrick Venture try 12418bbe3c6SPatrick Venture { 12563528046SPatrick Venture int bytesRead = 0; 12663528046SPatrick Venture std::uint32_t offset = 0; 12763528046SPatrick Venture 12818bbe3c6SPatrick Venture do 12918bbe3c6SPatrick Venture { 13018bbe3c6SPatrick Venture bytesRead = sys->read(inputFd, readBuffer.get(), p2aLength); 13118bbe3c6SPatrick Venture if (bytesRead > 0) 13218bbe3c6SPatrick Venture { 13318bbe3c6SPatrick Venture /* TODO: Will likely need to store an rv somewhere to know when 13418bbe3c6SPatrick Venture * we're exiting from failure. 13518bbe3c6SPatrick Venture */ 13618bbe3c6SPatrick Venture if (!io->write(bar1 | aspeedP2aOffset, bytesRead, 13718bbe3c6SPatrick Venture readBuffer.get())) 13818bbe3c6SPatrick Venture { 13918bbe3c6SPatrick Venture std::fprintf(stderr, 14018bbe3c6SPatrick Venture "Failed to write to region in memory!\n"); 14118bbe3c6SPatrick Venture break; 14218bbe3c6SPatrick Venture } 14318bbe3c6SPatrick Venture 14418bbe3c6SPatrick Venture /* Ok, so the data is staged, now send the blob write with the 14518bbe3c6SPatrick Venture * details. 14618bbe3c6SPatrick Venture */ 1471d5a31c9SPatrick Venture struct ipmi_flash::ExtChunkHdr chunk; 14818bbe3c6SPatrick Venture chunk.length = bytesRead; 14918bbe3c6SPatrick Venture std::vector<std::uint8_t> chunkBytes(sizeof(chunk)); 15018bbe3c6SPatrick Venture std::memcpy(chunkBytes.data(), &chunk, sizeof(chunk)); 15118bbe3c6SPatrick Venture 15218bbe3c6SPatrick Venture /* This doesn't return anything on success. */ 15318bbe3c6SPatrick Venture blob->writeBytes(session, offset, chunkBytes); 15418bbe3c6SPatrick Venture offset += bytesRead; 15518bbe3c6SPatrick Venture } 15618bbe3c6SPatrick Venture } while (bytesRead > 0); 15718bbe3c6SPatrick Venture } 15818bbe3c6SPatrick Venture catch (const ipmiblob::BlobException& b) 15918bbe3c6SPatrick Venture { 16018bbe3c6SPatrick Venture sys->close(inputFd); 16118bbe3c6SPatrick Venture return false; 16218bbe3c6SPatrick Venture } 16318bbe3c6SPatrick Venture 16418bbe3c6SPatrick Venture sys->close(inputFd); 16518bbe3c6SPatrick Venture return true; 16618bbe3c6SPatrick Venture } 16718bbe3c6SPatrick Venture 168b5bf0fc2SPatrick Venture } // namespace host_tool 169