blob: 2a48285039a5686efa25519b71a184be9042a14a [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);
84 if (stat.metadata.size() != sizeof(blobs::PciConfigResponse))
85 {
86 std::fprintf(stderr, "Didn't receive expected size of metadata for "
87 "PCI Configuration response\n");
88 return false;
89 }
90
91 blobs::PciConfigResponse pciResp;
92 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
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 */
143 struct blobs::ExtChunkHdr chunk;
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 Ventureb5bf0fc2019-05-03 14:33:49 -0700162}
163
164} // namespace host_tool