Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
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 | a53a7b3 | 2018-08-03 09:15:20 -0700 | [diff] [blame] | 23 | bool validateRequestLength(FlashSubCmds command, size_t requestLen) |
| 24 | { |
| 25 | static const std::unordered_map<FlashSubCmds, size_t> minimumLengths = { |
| 26 | {FlashSubCmds::flashStartTransfer, sizeof(struct StartTx)}, |
| 27 | {FlashSubCmds::flashDataBlock, sizeof(struct ChunkHdr) + 1}, |
Patrick Venture | 8d9f732 | 2018-08-03 10:39:13 -0700 | [diff] [blame^] | 28 | {FlashSubCmds::flashStartHash, sizeof(struct StartTx)}, |
Patrick Venture | a53a7b3 | 2018-08-03 09:15:20 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | auto results = minimumLengths.find(command); |
| 32 | if (results == minimumLengths.end()) |
| 33 | { |
| 34 | /* Valid length by default if we don't care. */ |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | /* If the request is shorter than the minimum, it's invalid. */ |
| 39 | if (requestLen < results->second) |
| 40 | { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | return true; |
| 45 | } |
| 46 | |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 47 | ipmi_ret_t startTransfer(UpdateInterface* updater, const uint8_t* reqBuf, |
| 48 | uint8_t* replyBuf, size_t* dataLen) |
| 49 | { |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 50 | auto request = reinterpret_cast<const struct StartTx*>(reqBuf); |
| 51 | |
| 52 | if (!updater->start(request->length)) |
| 53 | { |
| 54 | return IPMI_CC_INVALID; |
| 55 | } |
| 56 | |
| 57 | /* We were successful and set the response byte to 0. */ |
| 58 | replyBuf[0] = 0x00; |
| 59 | (*dataLen) = 1; |
| 60 | return IPMI_CC_OK; |
| 61 | } |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 62 | |
| 63 | ipmi_ret_t dataBlock(UpdateInterface* updater, const uint8_t* reqBuf, |
| 64 | uint8_t* replyBuf, size_t* dataLen) |
| 65 | { |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 66 | struct ChunkHdr hdr; |
| 67 | std::memcpy(&hdr, reqBuf, sizeof(hdr)); |
| 68 | |
Patrick Venture | a53a7b3 | 2018-08-03 09:15:20 -0700 | [diff] [blame] | 69 | size_t requestLength = (*dataLen); |
| 70 | |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 71 | /* Grab the bytes from the packet. */ |
| 72 | size_t bytesLength = requestLength - sizeof(struct ChunkHdr); |
| 73 | std::vector<uint8_t> bytes(bytesLength); |
| 74 | std::memcpy(bytes.data(), &reqBuf[sizeof(struct ChunkHdr)], bytesLength); |
| 75 | |
| 76 | if (!updater->flashData(hdr.offset, bytes)) |
| 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 | 2c1205d | 2018-08-03 10:23:14 -0700 | [diff] [blame] | 86 | |
| 87 | ipmi_ret_t dataFinish(UpdateInterface* updater, const uint8_t* reqBuf, |
| 88 | uint8_t* replyBuf, size_t* dataLen) |
| 89 | { |
| 90 | if (!updater->flashFinish()) |
| 91 | { |
| 92 | return IPMI_CC_INVALID; |
| 93 | } |
| 94 | |
| 95 | /* TODO: If all commands return this on success, handle it in one place. */ |
| 96 | |
| 97 | /* We were successful and set the response byte to 0. */ |
| 98 | replyBuf[0] = 0x00; |
| 99 | (*dataLen) = 1; |
| 100 | return IPMI_CC_OK; |
| 101 | } |
Patrick Venture | 8d9f732 | 2018-08-03 10:39:13 -0700 | [diff] [blame^] | 102 | |
| 103 | ipmi_ret_t startHash(UpdateInterface* updater, const uint8_t* reqBuf, |
| 104 | uint8_t* replyBuf, size_t* dataLen) |
| 105 | { |
| 106 | auto request = reinterpret_cast<const struct StartTx*>(reqBuf); |
| 107 | |
| 108 | if (!updater->startHash(request->length)) |
| 109 | { |
| 110 | return IPMI_CC_INVALID; |
| 111 | } |
| 112 | |
| 113 | /* We were successful and set the response byte to 0. */ |
| 114 | replyBuf[0] = 0x00; |
| 115 | (*dataLen) = 1; |
| 116 | return IPMI_CC_OK; |
| 117 | } |