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