Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 1 | /* |
Patrick Venture | 514f648 | 2018-08-07 14:27:58 -0700 | [diff] [blame] | 2 | * Copyright 2018 Google Inc. |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 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 "flash-ipmi.hpp" |
| 18 | |
Patrick Venture | 8ec019f | 2018-08-07 11:22:33 -0700 | [diff] [blame] | 19 | #include <cstdio> |
| 20 | #include <phosphor-logging/log.hpp> |
| 21 | |
| 22 | using namespace phosphor::logging; |
| 23 | |
| 24 | namespace |
| 25 | { |
| 26 | |
| 27 | /** |
| 28 | * Close a file pointer and set to null. |
| 29 | * |
| 30 | * @param[in] fd a pointer to your file handle. |
| 31 | */ |
| 32 | void closeFile(std::FILE** fd) |
| 33 | { |
| 34 | if (!fd) |
| 35 | { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | if (*fd) |
| 40 | { |
| 41 | std::fclose(*fd); |
| 42 | (*fd) = nullptr; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void FlashUpdate::closeEverything() |
| 48 | { |
| 49 | closeFile(&flashFd); |
Patrick Venture | 6f17bd2 | 2018-08-07 13:24:17 -0700 | [diff] [blame] | 50 | closeFile(&hashFd); |
Patrick Venture | 8ec019f | 2018-08-07 11:22:33 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | FlashUpdate::~FlashUpdate() |
| 54 | { |
| 55 | /* Close without deleting. This object can only be destroyed if the ipmi |
| 56 | * daemon unloads it, by closing down. In this event, we want the verified |
| 57 | * file to live on. |
| 58 | */ |
| 59 | closeEverything(); |
| 60 | } |
| 61 | |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 62 | void FlashUpdate::abortEverything() |
| 63 | { |
Patrick Venture | 8ec019f | 2018-08-07 11:22:33 -0700 | [diff] [blame] | 64 | closeEverything(); |
| 65 | |
| 66 | /* TODO: And now delete everything */ |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | |
| 70 | bool FlashUpdate::openEverything() |
| 71 | { |
Patrick Venture | 3c086f2 | 2018-08-07 11:59:20 -0700 | [diff] [blame] | 72 | flashFd = std::fopen(tmpPath.c_str(), "wb"); |
Patrick Venture | 8ec019f | 2018-08-07 11:22:33 -0700 | [diff] [blame] | 73 | if (flashFd == nullptr) |
| 74 | { |
| 75 | log<level::INFO>("Unable to open staging path", |
| 76 | entry("PATH=%s", tmpPath.c_str())); |
| 77 | return false; |
| 78 | } |
| 79 | |
Patrick Venture | 6f17bd2 | 2018-08-07 13:24:17 -0700 | [diff] [blame] | 80 | /* hash path is basically optional. */ |
| 81 | if (!hashPath.empty()) |
| 82 | { |
| 83 | hashFd = std::fopen(hashPath.c_str(), "wb"); |
| 84 | if (hashFd == nullptr) |
| 85 | { |
| 86 | log<level::INFO>("Unable to open hash storage path", |
| 87 | entry("PATH=%s", hashPath.c_str())); |
| 88 | closeFile(&flashFd); |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
| 96 | /* Prepare to receive a BMC image and then a signature. */ |
Patrick Venture | 8ec019f | 2018-08-07 11:22:33 -0700 | [diff] [blame] | 97 | bool FlashUpdate::start(uint32_t length) |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 98 | { |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 99 | /* Close out and delete everything. */ |
| 100 | abortEverything(); |
| 101 | |
Patrick Venture | 8ec019f | 2018-08-07 11:22:33 -0700 | [diff] [blame] | 102 | /* TODO: Validate request->length */ |
| 103 | flashLength = length; |
| 104 | |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 105 | /* Start over! */ |
| 106 | return openEverything(); |
| 107 | } |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 108 | |
Patrick Venture | 3c086f2 | 2018-08-07 11:59:20 -0700 | [diff] [blame] | 109 | bool FlashUpdate::writeBlock(std::FILE* fd, uint32_t offset, |
| 110 | const std::vector<uint8_t>& bytes) |
| 111 | { |
| 112 | /* Seek into position, let's assume fseek won't call if offset matches |
| 113 | * position. |
| 114 | */ |
| 115 | if (std::fseek(fd, offset, SEEK_SET)) |
| 116 | { |
| 117 | log<level::ERR>("Unable to seek into file to write bytes."); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | /* Write the bytes. */ |
| 122 | auto written = std::fwrite(bytes.data(), 1, bytes.size(), fd); |
| 123 | |
| 124 | if (written != bytes.size()) |
| 125 | { |
| 126 | log<level::ERR>("Unable to write all the bytes requested."); |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | (void)std::fflush(fd); |
| 131 | return true; |
| 132 | } |
| 133 | |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 134 | bool FlashUpdate::flashData(uint32_t offset, const std::vector<uint8_t>& bytes) |
| 135 | { |
Patrick Venture | 3c086f2 | 2018-08-07 11:59:20 -0700 | [diff] [blame] | 136 | if (flashFd) |
| 137 | { |
| 138 | return writeBlock(flashFd, offset, bytes); |
| 139 | } |
| 140 | |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 141 | return false; |
| 142 | } |
Patrick Venture | 2c1205d | 2018-08-03 10:23:14 -0700 | [diff] [blame] | 143 | |
| 144 | bool FlashUpdate::flashFinish() |
| 145 | { |
Patrick Venture | 5770366 | 2018-08-07 12:52:24 -0700 | [diff] [blame] | 146 | /* If it's open, close it. */ |
| 147 | if (flashFd) |
| 148 | { |
| 149 | closeFile(&flashFd); |
| 150 | return true; |
| 151 | } |
| 152 | |
Patrick Venture | 2c1205d | 2018-08-03 10:23:14 -0700 | [diff] [blame] | 153 | return false; |
| 154 | } |
Patrick Venture | 8d9f732 | 2018-08-03 10:39:13 -0700 | [diff] [blame] | 155 | |
| 156 | bool FlashUpdate::startHash(uint32_t length) |
| 157 | { |
Patrick Venture | 6f17bd2 | 2018-08-07 13:24:17 -0700 | [diff] [blame] | 158 | if (!hashFd) |
| 159 | { |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | hashLength = length; |
| 164 | return true; |
Patrick Venture | 8d9f732 | 2018-08-03 10:39:13 -0700 | [diff] [blame] | 165 | } |
Patrick Venture | cfe6687 | 2018-08-03 13:32:33 -0700 | [diff] [blame] | 166 | |
| 167 | bool FlashUpdate::hashData(uint32_t offset, const std::vector<uint8_t>& bytes) |
| 168 | { |
Patrick Venture | cbe5149 | 2018-08-07 14:09:17 -0700 | [diff] [blame^] | 169 | if (hashFd) |
| 170 | { |
| 171 | return writeBlock(hashFd, offset, bytes); |
| 172 | } |
| 173 | |
Patrick Venture | cfe6687 | 2018-08-03 13:32:33 -0700 | [diff] [blame] | 174 | return false; |
| 175 | } |
Patrick Venture | fbc7d19 | 2018-08-03 13:54:21 -0700 | [diff] [blame] | 176 | |
| 177 | bool FlashUpdate::hashFinish() |
| 178 | { |
| 179 | /* TODO: implement. */ |
| 180 | return false; |
| 181 | } |
Patrick Venture | 1cb87d2 | 2018-08-03 18:22:09 -0700 | [diff] [blame] | 182 | |
| 183 | bool FlashUpdate::startDataVerification() |
| 184 | { |
| 185 | /* TODO: implement. */ |
| 186 | return false; |
| 187 | } |
Patrick Venture | 5c251ca | 2018-08-03 18:31:01 -0700 | [diff] [blame] | 188 | |
| 189 | bool FlashUpdate::abortUpdate() |
| 190 | { |
| 191 | /* TODO: implement. */ |
| 192 | return false; |
| 193 | } |