blob: b5fa8a45de72d37a1130fc2c51a03c3b4a20b1e1 [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
32bool P2aDataHandler::sendContents(const std::string& input,
33 std::uint16_t session)
34{
Patrick Venture24141612019-05-03 17:59:18 -070035 PciDevice result;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070036 PciFilter filter;
Patrick Venture36bb4672019-05-07 09:42:56 -070037 bool found = false;
Patrick Venture18bbe3c2019-05-14 11:40:57 -070038 pciaddr_t bar1;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070039
40 filter.vid = aspeedVendorId;
41 filter.did = aspeedDeviceId;
42
Patrick Venture24141612019-05-03 17:59:18 -070043 /* Find the ASPEED PCI device entry we want. */
Patrick Venture46e69492019-05-15 07:54:44 -070044 auto output = pci->getPciDevices(filter);
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070045 for (const auto& d : output)
46 {
Patrick Venture24141612019-05-03 17:59:18 -070047 std::fprintf(stderr, "[0x%x 0x%x] ", d.vid, d.did);
48
49 /* Verify it's a memory-based bar -- we want bar1. */
Patrick Venture18bbe3c2019-05-14 11:40:57 -070050 bar1 = d.bars[1];
Patrick Venture24141612019-05-03 17:59:18 -070051 if ((bar1 & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO)
52 {
53 /* We want it to not be IO-based access. */
54 continue;
55 }
56
Patrick Venture18bbe3c2019-05-14 11:40:57 -070057 /* For now capture the entire device even if we're only using BAR1 */
Patrick Venture24141612019-05-03 17:59:18 -070058 result = d;
Patrick Venture36bb4672019-05-07 09:42:56 -070059 found = true;
60 break;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070061 }
Patrick Venture36bb4672019-05-07 09:42:56 -070062
63 if (!found)
64 {
65 return false;
66 }
67
Patrick Venture24141612019-05-03 17:59:18 -070068 std::fprintf(stderr, "\n");
69
70 /* We sent the open command before this, so the window should be open and
71 * the bridge enabled.
72 */
73 std::uint32_t value;
Patrick Venture18bbe3c2019-05-14 11:40:57 -070074 if (!io->read(bar1 | aspeedP2aConfig, sizeof(value), &value))
Patrick Venture24141612019-05-03 17:59:18 -070075 {
Patrick Venturee3feacf2019-07-26 08:28:36 -070076 std::fprintf(stderr, "PCI config read failed\n");
77 return false;
78 }
79
80 if (0 == (value & p2ABridgeEnabled))
81 {
82 std::fprintf(stderr, "Bridge not enabled.\n");
83 return false;
Patrick Venture24141612019-05-03 17:59:18 -070084 }
85
86 std::fprintf(stderr, "The bridge is enabled!\n");
87
88 /* Read the configuration via blobs metadata (stat). */
Patrick Venturec73dce92019-05-07 11:32:44 -070089 ipmiblob::StatResponse stat = blob->getStat(session);
Patrick Venture1d5a31c2019-05-20 11:38:22 -070090 if (stat.metadata.size() != sizeof(ipmi_flash::PciConfigResponse))
Patrick Venturec73dce92019-05-07 11:32:44 -070091 {
92 std::fprintf(stderr, "Didn't receive expected size of metadata for "
93 "PCI Configuration response\n");
94 return false;
95 }
96
Patrick Venture1d5a31c2019-05-20 11:38:22 -070097 ipmi_flash::PciConfigResponse pciResp;
Patrick Venturec73dce92019-05-07 11:32:44 -070098 std::memcpy(&pciResp, stat.metadata.data(), sizeof(pciResp));
99 std::fprintf(stderr, "Received address: 0x%x\n", pciResp.address);
Patrick Venture24141612019-05-03 17:59:18 -0700100
Patrick Venture24141612019-05-03 17:59:18 -0700101 /* Configure the mmio to point there. */
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700102 if (!io->write(bar1 | aspeedP2aBridge, sizeof(pciResp.address),
103 &pciResp.address))
104 {
Patrick Venture24141612019-05-03 17:59:18 -0700105 // Failed to set it up, so fall back.
106 std::fprintf(stderr, "Failed to update the bridge address\n");
107 return false;
108 }
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700109
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700110 /* For data blocks in 64kb, stage data, and send blob write command. */
111 int inputFd = sys->open(input.c_str(), 0);
112 if (inputFd < 0)
113 {
114 return false;
115 }
116
Patrick Venturecf9b2192019-06-27 12:09:52 -0700117 std::int64_t fileSize = sys->getSize(input.c_str());
118 if (fileSize == 0)
119 {
120 std::fprintf(stderr, "Zero-length file, or other file access error\n");
121 return false;
122 }
123
124 progress->start(fileSize);
125
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700126 const std::uint32_t p2aLength = aspeedP2aOffset;
127
128 auto readBuffer = std::make_unique<std::uint8_t[]>(p2aLength);
129 if (nullptr == readBuffer)
130 {
131 std::fprintf(stderr, "Unable to allocate memory for read buffer.\n");
132 return false;
133 }
134
135 try
136 {
Patrick Venture63528042019-05-20 18:10:02 -0700137 int bytesRead = 0;
138 std::uint32_t offset = 0;
139
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700140 do
141 {
142 bytesRead = sys->read(inputFd, readBuffer.get(), p2aLength);
143 if (bytesRead > 0)
144 {
145 /* TODO: Will likely need to store an rv somewhere to know when
146 * we're exiting from failure.
147 */
148 if (!io->write(bar1 | aspeedP2aOffset, bytesRead,
149 readBuffer.get()))
150 {
151 std::fprintf(stderr,
152 "Failed to write to region in memory!\n");
153 break;
154 }
155
156 /* Ok, so the data is staged, now send the blob write with the
157 * details.
158 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700159 struct ipmi_flash::ExtChunkHdr chunk;
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700160 chunk.length = bytesRead;
161 std::vector<std::uint8_t> chunkBytes(sizeof(chunk));
162 std::memcpy(chunkBytes.data(), &chunk, sizeof(chunk));
163
164 /* This doesn't return anything on success. */
165 blob->writeBytes(session, offset, chunkBytes);
166 offset += bytesRead;
Patrick Venturecf9b2192019-06-27 12:09:52 -0700167 progress->updateProgress(bytesRead);
Patrick Venture18bbe3c2019-05-14 11:40:57 -0700168 }
169 } while (bytesRead > 0);
170 }
171 catch (const ipmiblob::BlobException& b)
172 {
173 sys->close(inputFd);
174 return false;
175 }
176
177 sys->close(inputFd);
178 return true;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700179}
180
181} // namespace host_tool