blob: 5146c853f51d0f7097817132a5e8f6e7a7c5fdb7 [file] [log] [blame]
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -07001/*
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 Venture84778b82019-06-26 20:11:09 -070019#include "data.hpp"
20#include "flags.hpp"
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070021#include "pci.hpp"
Patrick Venturec73dce92019-05-07 11:32:44 -070022
Patrick Venture84778b82019-06-26 20:11:09 -070023#include <cstdint>
Patrick Venturec73dce92019-05-07 11:32:44 -070024#include <cstring>
Patrick Venture18bbe3c2019-05-14 11:40:57 -070025#include <ipmiblob/blob_errors.hpp>
Patrick Venture84778b82019-06-26 20:11:09 -070026#include <memory>
27#include <string>
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070028
29namespace host_tool
30{
31
Patrick Venturecf0e5de2019-07-26 08:25:33 -070032namespace
33{
34void 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 Ventureb5bf0fc2019-05-03 14:33:49 -070049bool P2aDataHandler::sendContents(const std::string& input,
50 std::uint16_t session)
51{
Patrick Venture24141612019-05-03 17:59:18 -070052 PciDevice result;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070053 PciFilter filter;
Patrick Venture36bb4672019-05-07 09:42:56 -070054 bool found = false;
Medad CChienc8445aa2020-04-23 09:47:46 +080055 pciaddr_t bar;
Patrick Venturecf0e5de2019-07-26 08:25:33 -070056 bool returnValue = false;
57 int inputFd = -1;
58 ipmi_flash::PciConfigResponse pciResp;
59 std::int64_t fileSize;
Patrick Venturecf0e5de2019-07-26 08:25:33 -070060 std::unique_ptr<std::uint8_t[]> readBuffer;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070061
Medad CChienc8445aa2020-04-23 09:47:46 +080062 std::uint16_t pciDeviceVID;
63 std::uint16_t pciDeviceDID;
64 std::uint32_t p2aOffset;
65 std::uint32_t p2aLength;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070066
Medad CChienc8445aa2020-04-23 09:47:46 +080067 for (auto device : PCIDeviceList)
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070068 {
Medad CChienc8445aa2020-04-23 09:47:46 +080069 filter.vid = device.VID;
70 filter.did = device.DID;
Patrick Venture24141612019-05-03 17:59:18 -070071
Medad CChienc8445aa2020-04-23 09:47:46 +080072 /* Find the PCI device entry we want. */
73 auto output = pci->getPciDevices(filter);
74 for (const auto& d : output)
Patrick Venture24141612019-05-03 17:59:18 -070075 {
Medad CChienc8445aa2020-04-23 09:47:46 +080076 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 Venture24141612019-05-03 17:59:18 -070092 }
93
Medad CChienc8445aa2020-04-23 09:47:46 +080094 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 Ventureb5bf0fc2019-05-03 14:33:49 -0700105 }
Patrick Venture36bb4672019-05-07 09:42:56 -0700106
107 if (!found)
108 {
109 return false;
110 }
111
Patrick Venture24141612019-05-03 17:59:18 -0700112 std::fprintf(stderr, "\n");
113
114 /* We sent the open command before this, so the window should be open and
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700115 * the bridge enabled on the BMC.
Patrick Venture24141612019-05-03 17:59:18 -0700116 */
Medad CChienc8445aa2020-04-23 09:47:46 +0800117 if (pciDeviceVID == aspeedPciDeviceInfo.VID &&
118 pciDeviceDID == aspeedPciDeviceInfo.DID)
Patrick Venture24141612019-05-03 17:59:18 -0700119 {
Medad CChienc8445aa2020-04-23 09:47:46 +0800120 std::uint32_t value;
121 if (!io->read(bar + aspeedP2aConfig, sizeof(value), &value))
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700122 {
Medad CChienc8445aa2020-04-23 09:47:46 +0800123 std::fprintf(stderr, "PCI config read failed\n");
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700124 return false;
125 }
Patrick Venture24141612019-05-03 17:59:18 -0700126
Medad CChienc8445aa2020-04-23 09:47:46 +0800127 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 Venture24141612019-05-03 17:59:18 -0700142
143 /* Read the configuration via blobs metadata (stat). */
Patrick Venturec73dce92019-05-07 11:32:44 -0700144 ipmiblob::StatResponse stat = blob->getStat(session);
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700145 if (stat.metadata.size() != sizeof(ipmi_flash::PciConfigResponse))
Patrick Venturec73dce92019-05-07 11:32:44 -0700146 {
147 std::fprintf(stderr, "Didn't receive expected size of metadata for "
148 "PCI Configuration response\n");
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700149 goto exit;
Patrick Venturec73dce92019-05-07 11:32:44 -0700150 }
151
Patrick Venturec73dce92019-05-07 11:32:44 -0700152 std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp));
153 std::fprintf(stderr, "Received address: 0x%x\n", pciResp.address);
Patrick Venture24141612019-05-03 17:59:18 -0700154
Medad CChienc8445aa2020-04-23 09:47:46 +0800155 if (pciDeviceVID == aspeedPciDeviceInfo.VID &&
156 pciDeviceDID == aspeedPciDeviceInfo.DID)
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700157 {
Medad CChienc8445aa2020-04-23 09:47:46 +0800158 /* 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 Venture24141612019-05-03 17:59:18 -0700166 }
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700167
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700168 /* For data blocks in 64kb, stage data, and send blob write command. */
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700169 inputFd = sys->open(input.c_str(), 0);
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700170 if (inputFd < 0)
171 {
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700172 std::fprintf(stderr, "Unable to open file: '%s'\n", input.c_str());
173 goto exit;
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700174 }
175
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700176 fileSize = sys->getSize(input.c_str());
Patrick Venturecf9b2192019-06-27 12:09:52 -0700177 if (fileSize == 0)
178 {
179 std::fprintf(stderr, "Zero-length file, or other file access error\n");
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700180 goto exit;
Patrick Venturecf9b2192019-06-27 12:09:52 -0700181 }
182
183 progress->start(fileSize);
184
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700185 readBuffer = std::make_unique<std::uint8_t[]>(p2aLength);
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700186 if (nullptr == readBuffer)
187 {
188 std::fprintf(stderr, "Unable to allocate memory for read buffer.\n");
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700189 goto exit;
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700190 }
191
192 try
193 {
Patrick Venture63528042019-05-20 18:10:02 -0700194 int bytesRead = 0;
195 std::uint32_t offset = 0;
196
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700197 do
198 {
199 bytesRead = sys->read(inputFd, readBuffer.get(), p2aLength);
200 if (bytesRead > 0)
201 {
Medad CChienc8445aa2020-04-23 09:47:46 +0800202 /* TODO: Will likely need to store an rv somewhere to know when
203 * we're exiting from failure.
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700204 */
Medad CChienc8445aa2020-04-23 09:47:46 +0800205 if (!io->write(bar + p2aOffset, bytesRead, readBuffer.get()))
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700206 {
207 std::fprintf(stderr,
208 "Failed to write to region in memory!\n");
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700209 goto exit;
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700210 }
211
Medad CChienc8445aa2020-04-23 09:47:46 +0800212 /* Ok, so the data is staged, now send the blob write with the
213 * details.
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700214 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700215 struct ipmi_flash::ExtChunkHdr chunk;
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700216 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 Venturecf9b2192019-06-27 12:09:52 -0700223 progress->updateProgress(bytesRead);
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700224 }
225 } while (bytesRead > 0);
226 }
227 catch (const ipmiblob::BlobException& b)
228 {
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700229 goto exit;
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700230 }
231
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700232 /* defaults to failure. */
233 returnValue = true;
234
235exit:
Medad CChienc8445aa2020-04-23 09:47:46 +0800236 /* disable ASPEED PCI bridge. */
237 if (pciDeviceVID == aspeedPciDeviceInfo.VID &&
238 pciDeviceDID == aspeedPciDeviceInfo.DID)
239 {
240 disablePciBridge(io, bar);
241 }
Patrick Venturecf0e5de2019-07-26 08:25:33 -0700242
243 /* close input file. */
244 if (inputFd != -1)
245 {
246 sys->close(inputFd);
247 }
248 return returnValue;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700249}
250
251} // namespace host_tool