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