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