Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 "lpc.hpp" |
| 18 | |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 19 | #include "data.hpp" |
| 20 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 21 | #include <ipmiblob/blob_errors.hpp> |
| 22 | |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 23 | #include <cerrno> |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 24 | #include <cstdint> |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 25 | #include <cstring> |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 26 | #include <memory> |
| 27 | #include <string> |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 28 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 29 | namespace host_tool |
| 30 | { |
| 31 | |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 32 | bool LpcDataHandler::sendContents(const std::string& input, |
| 33 | std::uint16_t session) |
| 34 | { |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 35 | LpcRegion host_lpc_buf; |
Patrick Venture | 03db87e | 2019-06-20 07:56:06 -0700 | [diff] [blame] | 36 | host_lpc_buf.address = address; |
| 37 | host_lpc_buf.length = length; |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 38 | |
| 39 | std::vector<std::uint8_t> payload(sizeof(host_lpc_buf)); |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 40 | |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 41 | while (true) |
| 42 | { |
| 43 | /* If the writeMeta() is rejected we need to call sessionStat on it. */ |
| 44 | try |
| 45 | { |
| 46 | std::fprintf(stderr, "sending writeMeta\n"); |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 47 | |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 48 | std::memcpy(payload.data(), &host_lpc_buf, sizeof(host_lpc_buf)); |
| 49 | blob->writeMeta(session, 0x00, payload); |
| 50 | |
| 51 | std::fprintf(stderr, "writemeta sent\n"); |
| 52 | |
| 53 | break; |
| 54 | } |
| 55 | catch (...) |
| 56 | { |
| 57 | std::fprintf(stderr, "caught exception\n"); |
| 58 | |
| 59 | ipmiblob::StatResponse resp = blob->getStat(session); |
| 60 | if (resp.metadata.empty()) |
| 61 | { |
| 62 | std::fprintf(stderr, "Received no metadata bytes back!"); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | struct MemoryMapResultDetails |
| 67 | { |
| 68 | std::uint8_t code; |
| 69 | std::uint32_t offset; |
| 70 | std::uint32_t length; |
| 71 | } __attribute__((packed)); |
| 72 | |
| 73 | struct MemoryMapResultDetails bytes; |
| 74 | |
| 75 | if (resp.metadata.size() != sizeof(bytes)) |
| 76 | { |
| 77 | std::fprintf( |
| 78 | stderr, |
| 79 | "Received insufficient bytes back on expected return!\n"); |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | std::memcpy(&bytes, resp.metadata.data(), sizeof(bytes)); |
| 84 | |
| 85 | if (bytes.code == EFBIG) |
| 86 | { |
| 87 | std::fprintf(stderr, "EFBIG returned!\n"); |
| 88 | |
| 89 | host_lpc_buf.length = bytes.length; |
| 90 | host_lpc_buf.address += bytes.offset; |
| 91 | } |
| 92 | else if (bytes.code == 0) |
| 93 | { |
| 94 | /* We're good, continue! */ |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* For data blockss, stage data, and send blob write command. */ |
| 101 | int inputFd = sys->open(input.c_str(), 0); |
| 102 | if (inputFd < 0) |
| 103 | { |
| 104 | return false; |
| 105 | } |
| 106 | |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 107 | std::int64_t fileSize = sys->getSize(input.c_str()); |
| 108 | if (fileSize == 0) |
| 109 | { |
| 110 | std::fprintf(stderr, "Zero-length file, or other file access error\n"); |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | progress->start(fileSize); |
| 115 | |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 116 | /* For Nuvoton the maximum is 4K */ |
| 117 | auto readBuffer = std::make_unique<std::uint8_t[]>(host_lpc_buf.length); |
| 118 | if (nullptr == readBuffer) |
| 119 | { |
William A. Kennington III | 0d5bb78 | 2021-01-19 15:50:42 -0800 | [diff] [blame] | 120 | progress->abort(); |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 121 | sys->close(inputFd); |
| 122 | std::fprintf(stderr, "Unable to allocate memory for read buffer.\n"); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | /* TODO: This is similar to PCI insomuch as how it sends data, so combine. |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 127 | */ |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 128 | try |
| 129 | { |
| 130 | int bytesRead = 0; |
| 131 | std::uint32_t offset = 0; |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 132 | |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 133 | do |
| 134 | { |
| 135 | bytesRead = |
| 136 | sys->read(inputFd, readBuffer.get(), host_lpc_buf.length); |
| 137 | if (bytesRead > 0) |
| 138 | { |
| 139 | if (!io->write(host_lpc_buf.address, bytesRead, |
| 140 | readBuffer.get())) |
| 141 | { |
| 142 | std::fprintf(stderr, |
| 143 | "Failed to write to region in memory!\n"); |
| 144 | } |
| 145 | |
| 146 | struct ipmi_flash::ExtChunkHdr chunk; |
| 147 | chunk.length = bytesRead; |
| 148 | std::vector<std::uint8_t> chunkBytes(sizeof(chunk)); |
| 149 | std::memcpy(chunkBytes.data(), &chunk, sizeof(chunk)); |
| 150 | |
| 151 | /* This doesn't return anything on success. */ |
| 152 | blob->writeBytes(session, offset, chunkBytes); |
| 153 | offset += bytesRead; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 154 | progress->updateProgress(bytesRead); |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 155 | } |
| 156 | } while (bytesRead > 0); |
| 157 | } |
| 158 | catch (const ipmiblob::BlobException& b) |
| 159 | { |
William A. Kennington III | 0d5bb78 | 2021-01-19 15:50:42 -0800 | [diff] [blame] | 160 | progress->abort(); |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 161 | sys->close(inputFd); |
| 162 | return false; |
| 163 | } |
| 164 | |
William A. Kennington III | 0d5bb78 | 2021-01-19 15:50:42 -0800 | [diff] [blame] | 165 | progress->finish(); |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 166 | sys->close(inputFd); |
| 167 | return true; |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 168 | } |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 169 | |
| 170 | } // namespace host_tool |