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 | |
Patrick Venture | 1aedab2 | 2018-09-10 14:41:45 -0700 | [diff] [blame] | 17 | #include "ipmi.hpp" |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 18 | |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 19 | #include "flash-ipmi.hpp" |
Patrick Venture | 1aedab2 | 2018-09-10 14:41:45 -0700 | [diff] [blame] | 20 | |
| 21 | #include <cstring> |
| 22 | #include <unordered_map> |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 23 | |
Patrick Venture | 9a5a79a | 2018-08-03 17:23:57 -0700 | [diff] [blame] | 24 | IpmiFlashHandler getCommandHandler(FlashSubCmds command) |
| 25 | { |
| 26 | static const std::unordered_map<FlashSubCmds, IpmiFlashHandler> |
| 27 | subHandlers = { |
| 28 | {FlashSubCmds::flashStartTransfer, startTransfer}, |
| 29 | {FlashSubCmds::flashDataBlock, dataBlock}, |
| 30 | {FlashSubCmds::flashDataFinish, dataFinish}, |
| 31 | {FlashSubCmds::flashStartHash, startHash}, |
| 32 | {FlashSubCmds::flashHashData, hashBlock}, |
| 33 | {FlashSubCmds::flashHashFinish, hashFinish}, |
Patrick Venture | 1cb87d2 | 2018-08-03 18:22:09 -0700 | [diff] [blame] | 34 | {FlashSubCmds::flashDataVerify, dataVerify}, |
Patrick Venture | 5c251ca | 2018-08-03 18:31:01 -0700 | [diff] [blame] | 35 | {FlashSubCmds::flashAbort, abortUpdate}, |
Patrick Venture | fdc65b2 | 2018-08-07 14:37:58 -0700 | [diff] [blame] | 36 | {FlashSubCmds::flashVerifyCheck, checkVerify}, |
Patrick Venture | 9a5a79a | 2018-08-03 17:23:57 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | auto results = subHandlers.find(command); |
| 40 | if (results == subHandlers.end()) |
| 41 | { |
| 42 | return nullptr; |
| 43 | } |
| 44 | |
| 45 | return results->second; |
| 46 | } |
| 47 | |
Patrick Venture | a53a7b3 | 2018-08-03 09:15:20 -0700 | [diff] [blame] | 48 | bool validateRequestLength(FlashSubCmds command, size_t requestLen) |
| 49 | { |
| 50 | static const std::unordered_map<FlashSubCmds, size_t> minimumLengths = { |
| 51 | {FlashSubCmds::flashStartTransfer, sizeof(struct StartTx)}, |
| 52 | {FlashSubCmds::flashDataBlock, sizeof(struct ChunkHdr) + 1}, |
Patrick Venture | 8d9f732 | 2018-08-03 10:39:13 -0700 | [diff] [blame] | 53 | {FlashSubCmds::flashStartHash, sizeof(struct StartTx)}, |
Patrick Venture | cfe6687 | 2018-08-03 13:32:33 -0700 | [diff] [blame] | 54 | {FlashSubCmds::flashHashData, sizeof(struct ChunkHdr) + 1}, |
Patrick Venture | a53a7b3 | 2018-08-03 09:15:20 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | auto results = minimumLengths.find(command); |
| 58 | if (results == minimumLengths.end()) |
| 59 | { |
| 60 | /* Valid length by default if we don't care. */ |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | /* If the request is shorter than the minimum, it's invalid. */ |
| 65 | if (requestLen < results->second) |
| 66 | { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 73 | ipmi_ret_t startTransfer(UpdateInterface* updater, const uint8_t* reqBuf, |
| 74 | uint8_t* replyBuf, size_t* dataLen) |
| 75 | { |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 76 | auto request = reinterpret_cast<const struct StartTx*>(reqBuf); |
| 77 | |
| 78 | if (!updater->start(request->length)) |
| 79 | { |
| 80 | return IPMI_CC_INVALID; |
| 81 | } |
| 82 | |
| 83 | /* We were successful and set the response byte to 0. */ |
| 84 | replyBuf[0] = 0x00; |
| 85 | (*dataLen) = 1; |
| 86 | return IPMI_CC_OK; |
| 87 | } |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 88 | |
| 89 | ipmi_ret_t dataBlock(UpdateInterface* updater, const uint8_t* reqBuf, |
| 90 | uint8_t* replyBuf, size_t* dataLen) |
| 91 | { |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 92 | struct ChunkHdr hdr; |
| 93 | std::memcpy(&hdr, reqBuf, sizeof(hdr)); |
| 94 | |
Patrick Venture | cfe6687 | 2018-08-03 13:32:33 -0700 | [diff] [blame] | 95 | auto requestLength = *dataLen; |
Patrick Venture | a53a7b3 | 2018-08-03 09:15:20 -0700 | [diff] [blame] | 96 | |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 97 | /* Grab the bytes from the packet. */ |
Patrick Venture | cfe6687 | 2018-08-03 13:32:33 -0700 | [diff] [blame] | 98 | auto bytesLength = requestLength - sizeof(struct ChunkHdr); |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 99 | std::vector<uint8_t> bytes(bytesLength); |
| 100 | std::memcpy(bytes.data(), &reqBuf[sizeof(struct ChunkHdr)], bytesLength); |
| 101 | |
| 102 | if (!updater->flashData(hdr.offset, bytes)) |
| 103 | { |
| 104 | return IPMI_CC_INVALID; |
| 105 | } |
| 106 | |
| 107 | /* We were successful and set the response byte to 0. */ |
| 108 | replyBuf[0] = 0x00; |
| 109 | (*dataLen) = 1; |
| 110 | return IPMI_CC_OK; |
| 111 | } |
Patrick Venture | 2c1205d | 2018-08-03 10:23:14 -0700 | [diff] [blame] | 112 | |
| 113 | ipmi_ret_t dataFinish(UpdateInterface* updater, const uint8_t* reqBuf, |
| 114 | uint8_t* replyBuf, size_t* dataLen) |
| 115 | { |
| 116 | if (!updater->flashFinish()) |
| 117 | { |
| 118 | return IPMI_CC_INVALID; |
| 119 | } |
| 120 | |
| 121 | /* TODO: If all commands return this on success, handle it in one place. */ |
| 122 | |
| 123 | /* We were successful and set the response byte to 0. */ |
| 124 | replyBuf[0] = 0x00; |
| 125 | (*dataLen) = 1; |
| 126 | return IPMI_CC_OK; |
| 127 | } |
Patrick Venture | 8d9f732 | 2018-08-03 10:39:13 -0700 | [diff] [blame] | 128 | |
| 129 | ipmi_ret_t startHash(UpdateInterface* updater, const uint8_t* reqBuf, |
| 130 | uint8_t* replyBuf, size_t* dataLen) |
| 131 | { |
| 132 | auto request = reinterpret_cast<const struct StartTx*>(reqBuf); |
| 133 | |
| 134 | if (!updater->startHash(request->length)) |
| 135 | { |
| 136 | return IPMI_CC_INVALID; |
| 137 | } |
| 138 | |
| 139 | /* We were successful and set the response byte to 0. */ |
| 140 | replyBuf[0] = 0x00; |
| 141 | (*dataLen) = 1; |
| 142 | return IPMI_CC_OK; |
| 143 | } |
Patrick Venture | cfe6687 | 2018-08-03 13:32:33 -0700 | [diff] [blame] | 144 | |
| 145 | ipmi_ret_t hashBlock(UpdateInterface* updater, const uint8_t* reqBuf, |
| 146 | uint8_t* replyBuf, size_t* dataLen) |
| 147 | { |
| 148 | struct ChunkHdr hdr; |
| 149 | std::memcpy(&hdr, reqBuf, sizeof(hdr)); |
| 150 | |
| 151 | auto requestLength = *dataLen; |
| 152 | |
| 153 | /* Grab the bytes from the packet. */ |
| 154 | auto bytesLength = requestLength - sizeof(struct ChunkHdr); |
| 155 | std::vector<uint8_t> bytes(bytesLength); |
| 156 | std::memcpy(bytes.data(), &reqBuf[sizeof(struct ChunkHdr)], bytesLength); |
| 157 | |
| 158 | /* TODO: Refactor this and dataBlock for re-use. */ |
| 159 | |
| 160 | if (!updater->hashData(hdr.offset, bytes)) |
| 161 | { |
| 162 | return IPMI_CC_INVALID; |
| 163 | } |
| 164 | |
| 165 | /* We were successful and set the response byte to 0. */ |
| 166 | replyBuf[0] = 0x00; |
| 167 | (*dataLen) = 1; |
| 168 | return IPMI_CC_OK; |
| 169 | } |
Patrick Venture | fbc7d19 | 2018-08-03 13:54:21 -0700 | [diff] [blame] | 170 | |
| 171 | ipmi_ret_t hashFinish(UpdateInterface* updater, const uint8_t* reqBuf, |
| 172 | uint8_t* replyBuf, size_t* dataLen) |
| 173 | { |
| 174 | if (!updater->hashFinish()) |
| 175 | { |
| 176 | return IPMI_CC_INVALID; |
| 177 | } |
| 178 | |
| 179 | /* We were successful and set the response byte to 0. */ |
| 180 | replyBuf[0] = 0x00; |
| 181 | (*dataLen) = 1; |
| 182 | return IPMI_CC_OK; |
| 183 | } |
Patrick Venture | 1cb87d2 | 2018-08-03 18:22:09 -0700 | [diff] [blame] | 184 | |
| 185 | ipmi_ret_t dataVerify(UpdateInterface* updater, const uint8_t* reqBuf, |
| 186 | uint8_t* replyBuf, size_t* dataLen) |
| 187 | { |
| 188 | if (!updater->startDataVerification()) |
| 189 | { |
| 190 | return IPMI_CC_INVALID; |
| 191 | } |
| 192 | |
| 193 | /* We were successful and set the response byte to 0. */ |
| 194 | replyBuf[0] = 0x00; |
| 195 | (*dataLen) = 1; |
| 196 | return IPMI_CC_OK; |
| 197 | } |
Patrick Venture | 5c251ca | 2018-08-03 18:31:01 -0700 | [diff] [blame] | 198 | |
| 199 | ipmi_ret_t abortUpdate(UpdateInterface* updater, const uint8_t* reqBuf, |
| 200 | uint8_t* replyBuf, size_t* dataLen) |
| 201 | { |
| 202 | /* TODO: May make sense to work all the pass-through commands into one |
| 203 | * piece of code |
| 204 | */ |
| 205 | if (!updater->abortUpdate()) |
| 206 | { |
| 207 | return IPMI_CC_INVALID; |
| 208 | } |
| 209 | |
| 210 | /* We were successful and set the response byte to 0. */ |
| 211 | replyBuf[0] = 0x00; |
| 212 | (*dataLen) = 1; |
| 213 | return IPMI_CC_OK; |
| 214 | } |
Patrick Venture | fdc65b2 | 2018-08-07 14:37:58 -0700 | [diff] [blame] | 215 | |
| 216 | ipmi_ret_t checkVerify(UpdateInterface* updater, const uint8_t* reqBuf, |
| 217 | uint8_t* replyBuf, size_t* dataLen) |
| 218 | { |
| 219 | auto value = updater->checkVerify(); |
| 220 | replyBuf[0] = static_cast<uint8_t>(value); |
| 221 | (*dataLen) = 1; |
| 222 | return IPMI_CC_OK; |
| 223 | } |