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