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" |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 22 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame^] | 23 | #include <ipmiblob/blob_errors.hpp> |
| 24 | |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 25 | #include <cstdint> |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 26 | #include <cstring> |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 27 | #include <memory> |
| 28 | #include <string> |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 29 | |
| 30 | namespace host_tool |
| 31 | { |
| 32 | |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 33 | namespace |
| 34 | { |
| 35 | void disablePciBridge(HostIoInterface* io, std::size_t address) |
| 36 | { |
| 37 | /* Read current value, and just blindly unset the bit. */ |
| 38 | std::uint32_t value; |
| 39 | if (!io->read(address + aspeedP2aConfig, sizeof(value), &value)) |
| 40 | { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | value &= ~p2ABridgeEnabled; |
| 45 | io->write(address + aspeedP2aConfig, sizeof(value), &value); |
| 46 | } |
| 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 | { |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 53 | PciDevice result; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 54 | PciFilter filter; |
Patrick Venture | 36bb467 | 2019-05-07 09:42:56 -0700 | [diff] [blame] | 55 | bool found = false; |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 56 | pciaddr_t bar; |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 57 | bool returnValue = false; |
| 58 | int inputFd = -1; |
| 59 | ipmi_flash::PciConfigResponse pciResp; |
| 60 | std::int64_t fileSize; |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 61 | std::unique_ptr<std::uint8_t[]> readBuffer; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 62 | |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 63 | std::uint16_t pciDeviceVID; |
| 64 | std::uint16_t pciDeviceDID; |
| 65 | std::uint32_t p2aOffset; |
| 66 | std::uint32_t p2aLength; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 67 | |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 68 | for (auto device : PCIDeviceList) |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 69 | { |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 70 | filter.vid = device.VID; |
| 71 | filter.did = device.DID; |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 72 | |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 73 | /* Find the PCI device entry we want. */ |
| 74 | auto output = pci->getPciDevices(filter); |
| 75 | for (const auto& d : output) |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 76 | { |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 77 | std::fprintf(stderr, "[0x%x 0x%x] \n", d.vid, d.did); |
| 78 | |
| 79 | /* Verify it's a memory-based bar. */ |
| 80 | bar = d.bars[device.bar]; |
| 81 | |
| 82 | if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) |
| 83 | { |
| 84 | /* We want it to not be IO-based access. */ |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | /* For now capture the entire device even if we're only using BAR0 |
| 89 | */ |
| 90 | result = d; |
| 91 | found = true; |
| 92 | break; |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 95 | if (found) |
| 96 | { |
| 97 | std::fprintf(stderr, "Find [0x%x 0x%x] \n", device.VID, device.DID); |
| 98 | std::fprintf(stderr, "bar%u[0x%x] \n", device.bar, |
| 99 | (unsigned int)bar); |
| 100 | pciDeviceVID = device.VID; |
| 101 | pciDeviceDID = device.DID; |
| 102 | p2aOffset = device.Offset; |
| 103 | p2aLength = device.Length; |
| 104 | break; |
| 105 | } |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 106 | } |
Patrick Venture | 36bb467 | 2019-05-07 09:42:56 -0700 | [diff] [blame] | 107 | |
| 108 | if (!found) |
| 109 | { |
| 110 | return false; |
| 111 | } |
| 112 | |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 113 | std::fprintf(stderr, "\n"); |
| 114 | |
| 115 | /* We sent the open command before this, so the window should be open and |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 116 | * the bridge enabled on the BMC. |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 117 | */ |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 118 | if (pciDeviceVID == aspeedPciDeviceInfo.VID && |
| 119 | pciDeviceDID == aspeedPciDeviceInfo.DID) |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 120 | { |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 121 | std::uint32_t value; |
| 122 | if (!io->read(bar + aspeedP2aConfig, sizeof(value), &value)) |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 123 | { |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 124 | std::fprintf(stderr, "PCI config read failed\n"); |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 125 | return false; |
| 126 | } |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 127 | |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 128 | if (0 == (value & p2ABridgeEnabled)) |
| 129 | { |
| 130 | std::fprintf(stderr, "Bridge not enabled - Enabling from host\n"); |
| 131 | |
| 132 | value |= p2ABridgeEnabled; |
| 133 | if (!io->write(bar + aspeedP2aConfig, sizeof(value), &value)) |
| 134 | { |
| 135 | std::fprintf(stderr, "PCI config write failed\n"); |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* From this point down we need to disable the bridge. */ |
| 141 | std::fprintf(stderr, "The bridge is enabled!\n"); |
| 142 | } |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 143 | |
| 144 | /* Read the configuration via blobs metadata (stat). */ |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 145 | ipmiblob::StatResponse stat = blob->getStat(session); |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 146 | if (stat.metadata.size() != sizeof(ipmi_flash::PciConfigResponse)) |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 147 | { |
| 148 | std::fprintf(stderr, "Didn't receive expected size of metadata for " |
| 149 | "PCI Configuration response\n"); |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 150 | goto exit; |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Patrick Venture | c73dce9 | 2019-05-07 11:32:44 -0700 | [diff] [blame] | 153 | std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp)); |
| 154 | std::fprintf(stderr, "Received address: 0x%x\n", pciResp.address); |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 155 | |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 156 | if (pciDeviceVID == aspeedPciDeviceInfo.VID && |
| 157 | pciDeviceDID == aspeedPciDeviceInfo.DID) |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 158 | { |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 159 | /* Configure the mmio to point there. */ |
| 160 | if (!io->write(bar + aspeedP2aBridge, sizeof(pciResp.address), |
| 161 | &pciResp.address)) |
| 162 | { |
| 163 | // Failed to set it up, so fall back. |
| 164 | std::fprintf(stderr, "Failed to update the bridge address\n"); |
| 165 | goto exit; |
| 166 | } |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 167 | } |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 168 | |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 169 | /* For data blocks in 64kb, stage data, and send blob write command. */ |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 170 | inputFd = sys->open(input.c_str(), 0); |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 171 | if (inputFd < 0) |
| 172 | { |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 173 | std::fprintf(stderr, "Unable to open file: '%s'\n", input.c_str()); |
| 174 | goto exit; |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 177 | fileSize = sys->getSize(input.c_str()); |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 178 | if (fileSize == 0) |
| 179 | { |
| 180 | std::fprintf(stderr, "Zero-length file, or other file access error\n"); |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 181 | goto exit; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | progress->start(fileSize); |
| 185 | |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 186 | readBuffer = std::make_unique<std::uint8_t[]>(p2aLength); |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 187 | if (nullptr == readBuffer) |
| 188 | { |
| 189 | std::fprintf(stderr, "Unable to allocate memory for read buffer.\n"); |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 190 | goto exit; |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | try |
| 194 | { |
Patrick Venture | 6352804 | 2019-05-20 18:10:02 -0700 | [diff] [blame] | 195 | int bytesRead = 0; |
| 196 | std::uint32_t offset = 0; |
| 197 | |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 198 | do |
| 199 | { |
| 200 | bytesRead = sys->read(inputFd, readBuffer.get(), p2aLength); |
| 201 | if (bytesRead > 0) |
| 202 | { |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 203 | /* TODO: Will likely need to store an rv somewhere to know when |
| 204 | * we're exiting from failure. |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 205 | */ |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 206 | if (!io->write(bar + p2aOffset, bytesRead, readBuffer.get())) |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 207 | { |
| 208 | std::fprintf(stderr, |
| 209 | "Failed to write to region in memory!\n"); |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 210 | goto exit; |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 213 | /* Ok, so the data is staged, now send the blob write with the |
| 214 | * details. |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 215 | */ |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 216 | struct ipmi_flash::ExtChunkHdr chunk; |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 217 | chunk.length = bytesRead; |
| 218 | std::vector<std::uint8_t> chunkBytes(sizeof(chunk)); |
| 219 | std::memcpy(chunkBytes.data(), &chunk, sizeof(chunk)); |
| 220 | |
| 221 | /* This doesn't return anything on success. */ |
| 222 | blob->writeBytes(session, offset, chunkBytes); |
| 223 | offset += bytesRead; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 224 | progress->updateProgress(bytesRead); |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 225 | } |
| 226 | } while (bytesRead > 0); |
| 227 | } |
| 228 | catch (const ipmiblob::BlobException& b) |
| 229 | { |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 230 | goto exit; |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 233 | /* defaults to failure. */ |
| 234 | returnValue = true; |
| 235 | |
| 236 | exit: |
Medad CChien | c8445aa | 2020-04-23 09:47:46 +0800 | [diff] [blame] | 237 | /* disable ASPEED PCI bridge. */ |
| 238 | if (pciDeviceVID == aspeedPciDeviceInfo.VID && |
| 239 | pciDeviceDID == aspeedPciDeviceInfo.DID) |
| 240 | { |
| 241 | disablePciBridge(io, bar); |
| 242 | } |
Patrick Venture | cf0e5de | 2019-07-26 08:25:33 -0700 | [diff] [blame] | 243 | |
| 244 | /* close input file. */ |
| 245 | if (inputFd != -1) |
| 246 | { |
| 247 | sys->close(inputFd); |
| 248 | } |
| 249 | return returnValue; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | } // namespace host_tool |