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