blob: dd05ccdf1d7c5c3388e2cea87cde8a828f95d433 [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 Venture18bbe3c2019-05-14 11:40:57 -070019#include "firmware_handler.hpp"
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070020#include "pci.hpp"
Patrick Venturec73dce92019-05-07 11:32:44 -070021#include "pci_handler.hpp"
22
23#include <cstring>
Patrick Venture18bbe3c2019-05-14 11:40:57 -070024#include <ipmiblob/blob_errors.hpp>
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070025
26namespace host_tool
27{
28
29bool P2aDataHandler::sendContents(const std::string& input,
30 std::uint16_t session)
31{
Patrick Venture24141612019-05-03 17:59:18 -070032 PciDevice result;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070033 PciUtilImpl pci;
34 PciFilter filter;
Patrick Venture36bb4672019-05-07 09:42:56 -070035 bool found = false;
Patrick Venture18bbe3c2019-05-14 11:40:57 -070036 pciaddr_t bar1;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070037
38 filter.vid = aspeedVendorId;
39 filter.did = aspeedDeviceId;
40
Patrick Venture24141612019-05-03 17:59:18 -070041 /* Find the ASPEED PCI device entry we want. */
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070042 auto output = pci.getPciDevices(filter);
43 for (const auto& d : output)
44 {
Patrick Venture24141612019-05-03 17:59:18 -070045 std::fprintf(stderr, "[0x%x 0x%x] ", d.vid, d.did);
46
47 /* Verify it's a memory-based bar -- we want bar1. */
Patrick Venture18bbe3c2019-05-14 11:40:57 -070048 bar1 = d.bars[1];
Patrick Venture24141612019-05-03 17:59:18 -070049 if ((bar1 & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
50 {
51 /* We want it to not be IO-based access. */
52 continue;
53 }
54
Patrick Venture18bbe3c2019-05-14 11:40:57 -070055 /* For now capture the entire device even if we're only using BAR1 */
Patrick Venture24141612019-05-03 17:59:18 -070056 result = d;
Patrick Venture36bb4672019-05-07 09:42:56 -070057 found = true;
58 break;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070059 }
Patrick Venture36bb4672019-05-07 09:42:56 -070060
61 if (!found)
62 {
63 return false;
64 }
65
Patrick Venture24141612019-05-03 17:59:18 -070066 std::fprintf(stderr, "\n");
67
68 /* We sent the open command before this, so the window should be open and
69 * the bridge enabled.
70 */
71 std::uint32_t value;
Patrick Venture18bbe3c2019-05-14 11:40:57 -070072 if (!io->read(bar1 | aspeedP2aConfig, sizeof(value), &value))
Patrick Venture24141612019-05-03 17:59:18 -070073 {
74 if (0 == (value & p2ABridgeEnabled))
75 {
76 std::fprintf(stderr, "Bridge not enabled.\n");
77 return false;
78 }
79 }
80
81 std::fprintf(stderr, "The bridge is enabled!\n");
82
83 /* Read the configuration via blobs metadata (stat). */
Patrick Venturec73dce92019-05-07 11:32:44 -070084 ipmiblob::StatResponse stat = blob->getStat(session);
85 if (stat.metadata.size() != sizeof(blobs::PciConfigResponse))
86 {
87 std::fprintf(stderr, "Didn't receive expected size of metadata for "
88 "PCI Configuration response\n");
89 return false;
90 }
91
92 blobs::PciConfigResponse pciResp;
93 std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp));
94 std::fprintf(stderr, "Received address: 0x%x\n", pciResp.address);
Patrick Venture24141612019-05-03 17:59:18 -070095
Patrick Venture24141612019-05-03 17:59:18 -070096 /* Configure the mmio to point there. */
Patrick Venture18bbe3c2019-05-14 11:40:57 -070097 if (!io->write(bar1 | aspeedP2aBridge, sizeof(pciResp.address),
98 &pciResp.address))
99 {
Patrick Venture24141612019-05-03 17:59:18 -0700100 // Failed to set it up, so fall back.
101 std::fprintf(stderr, "Failed to update the bridge address\n");
102 return false;
103 }
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700104
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700105 /* For data blocks in 64kb, stage data, and send blob write command. */
106 int inputFd = sys->open(input.c_str(), 0);
107 if (inputFd < 0)
108 {
109 return false;
110 }
111
112 int bytesRead = 0;
113 std::uint32_t offset = 0;
114 const std::uint32_t p2aLength = aspeedP2aOffset;
115
116 auto readBuffer = std::make_unique<std::uint8_t[]>(p2aLength);
117 if (nullptr == readBuffer)
118 {
119 std::fprintf(stderr, "Unable to allocate memory for read buffer.\n");
120 return false;
121 }
122
123 try
124 {
125 do
126 {
127 bytesRead = sys->read(inputFd, readBuffer.get(), p2aLength);
128 if (bytesRead > 0)
129 {
130 /* TODO: Will likely need to store an rv somewhere to know when
131 * we're exiting from failure.
132 */
133 if (!io->write(bar1 | aspeedP2aOffset, bytesRead,
134 readBuffer.get()))
135 {
136 std::fprintf(stderr,
137 "Failed to write to region in memory!\n");
138 break;
139 }
140
141 /* Ok, so the data is staged, now send the blob write with the
142 * details.
143 */
144 struct blobs::ExtChunkHdr chunk;
145 chunk.length = bytesRead;
146 std::vector<std::uint8_t> chunkBytes(sizeof(chunk));
147 std::memcpy(chunkBytes.data(), &chunk, sizeof(chunk));
148
149 /* This doesn't return anything on success. */
150 blob->writeBytes(session, offset, chunkBytes);
151 offset += bytesRead;
152 }
153 } while (bytesRead > 0);
154 }
155 catch (const ipmiblob::BlobException& b)
156 {
157 sys->close(inputFd);
158 return false;
159 }
160
161 sys->close(inputFd);
162 return true;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700163}
164
165} // namespace host_tool