blob: 00f027a6c6b49179f7902cefe4785322ef15d0a5 [file] [log] [blame]
Patrick Venture54c3b532018-08-01 11:45:49 -07001/*
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 Venture79e131f2018-08-01 13:34:35 -070017#include <cstring>
Patrick Venturea53a7b32018-08-03 09:15:20 -070018#include <unordered_map>
Patrick Venture79e131f2018-08-01 13:34:35 -070019
Patrick Venture54c3b532018-08-01 11:45:49 -070020#include "flash-ipmi.hpp"
21#include "ipmi.hpp"
22
Patrick Venturea53a7b32018-08-03 09:15:20 -070023bool 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 Venture8d9f7322018-08-03 10:39:13 -070028 {FlashSubCmds::flashStartHash, sizeof(struct StartTx)},
Patrick Venturea53a7b32018-08-03 09:15:20 -070029 };
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 Venture54c3b532018-08-01 11:45:49 -070047ipmi_ret_t startTransfer(UpdateInterface* updater, const uint8_t* reqBuf,
48 uint8_t* replyBuf, size_t* dataLen)
49{
Patrick Venture54c3b532018-08-01 11:45:49 -070050 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 Venture79e131f2018-08-01 13:34:35 -070062
63ipmi_ret_t dataBlock(UpdateInterface* updater, const uint8_t* reqBuf,
64 uint8_t* replyBuf, size_t* dataLen)
65{
Patrick Venture79e131f2018-08-01 13:34:35 -070066 struct ChunkHdr hdr;
67 std::memcpy(&hdr, reqBuf, sizeof(hdr));
68
Patrick Venturea53a7b32018-08-03 09:15:20 -070069 size_t requestLength = (*dataLen);
70
Patrick Venture79e131f2018-08-01 13:34:35 -070071 /* 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 Venture2c1205d2018-08-03 10:23:14 -070086
87ipmi_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 Venture8d9f7322018-08-03 10:39:13 -0700102
103ipmi_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}