blob: 9e246ae18389199ee4403012fa78a2b75bd2c4fc [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 PciFilter filter;
Patrick Venture36bb4672019-05-07 09:42:56 -070034 bool found = false;
Patrick Venture18bbe3c2019-05-14 11:40:57 -070035 pciaddr_t bar1;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070036
37 filter.vid = aspeedVendorId;
38 filter.did = aspeedDeviceId;
39
Patrick Venture24141612019-05-03 17:59:18 -070040 /* Find the ASPEED PCI device entry we want. */
Patrick Venture46e69492019-05-15 07:54:44 -070041 auto output = pci->getPciDevices(filter);
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070042 for (const auto& d : output)
43 {
Patrick Venture24141612019-05-03 17:59:18 -070044 std::fprintf(stderr, "[0x%x 0x%x] ", d.vid, d.did);
45
46 /* Verify it's a memory-based bar -- we want bar1. */
Patrick Venture18bbe3c2019-05-14 11:40:57 -070047 bar1 = d.bars[1];
Patrick Venture24141612019-05-03 17:59:18 -070048 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 Venture18bbe3c2019-05-14 11:40:57 -070054 /* For now capture the entire device even if we're only using BAR1 */
Patrick Venture24141612019-05-03 17:59:18 -070055 result = d;
Patrick Venture36bb4672019-05-07 09:42:56 -070056 found = true;
57 break;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070058 }
Patrick Venture36bb4672019-05-07 09:42:56 -070059
60 if (!found)
61 {
62 return false;
63 }
64
Patrick Venture24141612019-05-03 17:59:18 -070065 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 Venture18bbe3c2019-05-14 11:40:57 -070071 if (!io->read(bar1 | aspeedP2aConfig, sizeof(value), &value))
Patrick Venture24141612019-05-03 17:59:18 -070072 {
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 Venturec73dce92019-05-07 11:32:44 -070083 ipmiblob::StatResponse stat = blob->getStat(session);
Patrick Venture1d5a31c2019-05-20 11:38:22 -070084 if (stat.metadata.size() != sizeof(ipmi_flash::PciConfigResponse))
Patrick Venturec73dce92019-05-07 11:32:44 -070085 {
86 std::fprintf(stderr, "Didn't receive expected size of metadata for "
87 "PCI Configuration response\n");
88 return false;
89 }
90
Patrick Venture1d5a31c2019-05-20 11:38:22 -070091 ipmi_flash::PciConfigResponse pciResp;
Patrick Venturec73dce92019-05-07 11:32:44 -070092 std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp));
93 std::fprintf(stderr, "Received address: 0x%x\n", pciResp.address);
Patrick Venture24141612019-05-03 17:59:18 -070094
Patrick Venture24141612019-05-03 17:59:18 -070095 /* Configure the mmio to point there. */
Patrick Venture18bbe3c2019-05-14 11:40:57 -070096 if (!io->write(bar1 | aspeedP2aBridge, sizeof(pciResp.address),
97 &pciResp.address))
98 {
Patrick Venture24141612019-05-03 17:59:18 -070099 // Failed to set it up, so fall back.
100 std::fprintf(stderr, "Failed to update the bridge address\n");
101 return false;
102 }
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700103
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700104 /* 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
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700111 const std::uint32_t p2aLength = aspeedP2aOffset;
112
113 auto readBuffer = std::make_unique<std::uint8_t[]>(p2aLength);
114 if (nullptr == readBuffer)
115 {
116 std::fprintf(stderr, "Unable to allocate memory for read buffer.\n");
117 return false;
118 }
119
120 try
121 {
Patrick Venture63528042019-05-20 18:10:02 -0700122 int bytesRead = 0;
123 std::uint32_t offset = 0;
124
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700125 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 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700144 struct ipmi_flash::ExtChunkHdr chunk;
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700145 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