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> |
| 18 | |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 19 | #include "flash-ipmi.hpp" |
| 20 | #include "ipmi.hpp" |
| 21 | |
| 22 | ipmi_ret_t startTransfer(UpdateInterface* updater, const uint8_t* reqBuf, |
| 23 | uint8_t* replyBuf, size_t* dataLen) |
| 24 | { |
| 25 | /* Validate the request buffer. */ |
| 26 | if (sizeof(struct StartTx) > (*dataLen)) |
| 27 | { |
| 28 | return IPMI_CC_INVALID; |
| 29 | } |
| 30 | |
| 31 | auto request = reinterpret_cast<const struct StartTx*>(reqBuf); |
| 32 | |
| 33 | if (!updater->start(request->length)) |
| 34 | { |
| 35 | return IPMI_CC_INVALID; |
| 36 | } |
| 37 | |
| 38 | /* We were successful and set the response byte to 0. */ |
| 39 | replyBuf[0] = 0x00; |
| 40 | (*dataLen) = 1; |
| 41 | return IPMI_CC_OK; |
| 42 | } |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame^] | 43 | |
| 44 | ipmi_ret_t dataBlock(UpdateInterface* updater, const uint8_t* reqBuf, |
| 45 | uint8_t* replyBuf, size_t* dataLen) |
| 46 | { |
| 47 | size_t requestLength = (*dataLen); |
| 48 | /* Require at least one byte. */ |
| 49 | if (requestLength < sizeof(struct ChunkHdr) + 1) |
| 50 | { |
| 51 | return IPMI_CC_INVALID; |
| 52 | } |
| 53 | |
| 54 | struct ChunkHdr hdr; |
| 55 | std::memcpy(&hdr, reqBuf, sizeof(hdr)); |
| 56 | |
| 57 | /* Grab the bytes from the packet. */ |
| 58 | size_t bytesLength = requestLength - sizeof(struct ChunkHdr); |
| 59 | std::vector<uint8_t> bytes(bytesLength); |
| 60 | std::memcpy(bytes.data(), &reqBuf[sizeof(struct ChunkHdr)], bytesLength); |
| 61 | |
| 62 | if (!updater->flashData(hdr.offset, bytes)) |
| 63 | { |
| 64 | return IPMI_CC_INVALID; |
| 65 | } |
| 66 | |
| 67 | /* We were successful and set the response byte to 0. */ |
| 68 | replyBuf[0] = 0x00; |
| 69 | (*dataLen) = 1; |
| 70 | return IPMI_CC_OK; |
| 71 | } |