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 | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame^] | 19 | #include "firmware_handler.hpp" |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 20 | #include "pci.hpp" |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 21 | #include "pci_handler.hpp" |
| 22 | |
| 23 | #include <cstring> |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame^] | 24 | #include <ipmiblob/blob_errors.hpp> |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 25 | |
| 26 | namespace host_tool |
| 27 | { |
| 28 | |
| 29 | bool P2aDataHandler::sendContents(const std::string& input, |
| 30 | std::uint16_t session) |
| 31 | { |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 32 | PciDevice result; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 33 | PciUtilImpl pci; |
| 34 | PciFilter filter; |
Patrick Venture | 36bb467 | 2019-05-07 09:42:56 -0700 | [diff] [blame] | 35 | bool found = false; |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame^] | 36 | pciaddr_t bar1; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 37 | |
| 38 | filter.vid = aspeedVendorId; |
| 39 | filter.did = aspeedDeviceId; |
| 40 | |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 41 | /* Find the ASPEED PCI device entry we want. */ |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 42 | auto output = pci.getPciDevices(filter); |
| 43 | for (const auto& d : output) |
| 44 | { |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 45 | std::fprintf(stderr, "[0x%x 0x%x] ", d.vid, d.did); |
| 46 | |
| 47 | /* Verify it's a memory-based bar -- we want bar1. */ |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame^] | 48 | bar1 = d.bars[1]; |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 49 | if ((bar1 & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) |
| 50 | { |
| 51 | /* We want it to not be IO-based access. */ |
| 52 | continue; |
| 53 | } |
| 54 | |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame^] | 55 | /* For now capture the entire device even if we're only using BAR1 */ |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 56 | result = d; |
Patrick Venture | 36bb467 | 2019-05-07 09:42:56 -0700 | [diff] [blame] | 57 | found = true; |
| 58 | break; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 59 | } |
Patrick Venture | 36bb467 | 2019-05-07 09:42:56 -0700 | [diff] [blame] | 60 | |
| 61 | if (!found) |
| 62 | { |
| 63 | return false; |
| 64 | } |
| 65 | |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 66 | std::fprintf(stderr, "\n"); |
| 67 | |
| 68 | /* We sent the open command before this, so the window should be open and |
| 69 | * the bridge enabled. |
| 70 | */ |
| 71 | std::uint32_t value; |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame^] | 72 | if (!io->read(bar1 | aspeedP2aConfig, sizeof(value), &value)) |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 73 | { |
| 74 | if (0 == (value & p2ABridgeEnabled)) |
| 75 | { |
| 76 | std::fprintf(stderr, "Bridge not enabled.\n"); |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | std::fprintf(stderr, "The bridge is enabled!\n"); |
| 82 | |
| 83 | /* Read the configuration via blobs metadata (stat). */ |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 84 | ipmiblob::StatResponse stat = blob->getStat(session); |
| 85 | if (stat.metadata.size() != sizeof(blobs::PciConfigResponse)) |
| 86 | { |
| 87 | std::fprintf(stderr, "Didn't receive expected size of metadata for " |
| 88 | "PCI Configuration response\n"); |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | blobs::PciConfigResponse pciResp; |
| 93 | std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp)); |
| 94 | std::fprintf(stderr, "Received address: 0x%x\n", pciResp.address); |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 95 | |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 96 | /* Configure the mmio to point there. */ |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame^] | 97 | if (!io->write(bar1 | aspeedP2aBridge, sizeof(pciResp.address), |
| 98 | &pciResp.address)) |
| 99 | { |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 100 | // Failed to set it up, so fall back. |
| 101 | std::fprintf(stderr, "Failed to update the bridge address\n"); |
| 102 | return false; |
| 103 | } |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 104 | |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame^] | 105 | /* For data blocks in 64kb, stage data, and send blob write command. */ |
| 106 | int inputFd = sys->open(input.c_str(), 0); |
| 107 | if (inputFd < 0) |
| 108 | { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | int bytesRead = 0; |
| 113 | std::uint32_t offset = 0; |
| 114 | const std::uint32_t p2aLength = aspeedP2aOffset; |
| 115 | |
| 116 | auto readBuffer = std::make_unique<std::uint8_t[]>(p2aLength); |
| 117 | if (nullptr == readBuffer) |
| 118 | { |
| 119 | std::fprintf(stderr, "Unable to allocate memory for read buffer.\n"); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | try |
| 124 | { |
| 125 | do |
| 126 | { |
| 127 | bytesRead = sys->read(inputFd, readBuffer.get(), p2aLength); |
| 128 | if (bytesRead > 0) |
| 129 | { |
| 130 | /* TODO: Will likely need to store an rv somewhere to know when |
| 131 | * we're exiting from failure. |
| 132 | */ |
| 133 | if (!io->write(bar1 | aspeedP2aOffset, bytesRead, |
| 134 | readBuffer.get())) |
| 135 | { |
| 136 | std::fprintf(stderr, |
| 137 | "Failed to write to region in memory!\n"); |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | /* Ok, so the data is staged, now send the blob write with the |
| 142 | * details. |
| 143 | */ |
| 144 | struct blobs::ExtChunkHdr chunk; |
| 145 | chunk.length = bytesRead; |
| 146 | std::vector<std::uint8_t> chunkBytes(sizeof(chunk)); |
| 147 | std::memcpy(chunkBytes.data(), &chunk, sizeof(chunk)); |
| 148 | |
| 149 | /* This doesn't return anything on success. */ |
| 150 | blob->writeBytes(session, offset, chunkBytes); |
| 151 | offset += bytesRead; |
| 152 | } |
| 153 | } while (bytesRead > 0); |
| 154 | } |
| 155 | catch (const ipmiblob::BlobException& b) |
| 156 | { |
| 157 | sys->close(inputFd); |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | sys->close(inputFd); |
| 162 | return true; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | } // namespace host_tool |