blob: fc6092cc7ac7e5ad50cfbf4b0333c331d31d30b1 [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 Venture9a5a79a2018-08-03 17:23:57 -070023IpmiFlashHandler 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 Venture1cb87d22018-08-03 18:22:09 -070033 {FlashSubCmds::flashDataVerify, dataVerify},
Patrick Venture9a5a79a2018-08-03 17:23:57 -070034 };
35
36 auto results = subHandlers.find(command);
37 if (results == subHandlers.end())
38 {
39 return nullptr;
40 }
41
42 return results->second;
43}
44
Patrick Venturea53a7b32018-08-03 09:15:20 -070045bool validateRequestLength(FlashSubCmds command, size_t requestLen)
46{
47 static const std::unordered_map<FlashSubCmds, size_t> minimumLengths = {
48 {FlashSubCmds::flashStartTransfer, sizeof(struct StartTx)},
49 {FlashSubCmds::flashDataBlock, sizeof(struct ChunkHdr) + 1},
Patrick Venture8d9f7322018-08-03 10:39:13 -070050 {FlashSubCmds::flashStartHash, sizeof(struct StartTx)},
Patrick Venturecfe66872018-08-03 13:32:33 -070051 {FlashSubCmds::flashHashData, sizeof(struct ChunkHdr) + 1},
Patrick Venturea53a7b32018-08-03 09:15:20 -070052 };
53
54 auto results = minimumLengths.find(command);
55 if (results == minimumLengths.end())
56 {
57 /* Valid length by default if we don't care. */
58 return true;
59 }
60
61 /* If the request is shorter than the minimum, it's invalid. */
62 if (requestLen < results->second)
63 {
64 return false;
65 }
66
67 return true;
68}
69
Patrick Venture54c3b532018-08-01 11:45:49 -070070ipmi_ret_t startTransfer(UpdateInterface* updater, const uint8_t* reqBuf,
71 uint8_t* replyBuf, size_t* dataLen)
72{
Patrick Venture54c3b532018-08-01 11:45:49 -070073 auto request = reinterpret_cast<const struct StartTx*>(reqBuf);
74
75 if (!updater->start(request->length))
76 {
77 return IPMI_CC_INVALID;
78 }
79
80 /* We were successful and set the response byte to 0. */
81 replyBuf[0] = 0x00;
82 (*dataLen) = 1;
83 return IPMI_CC_OK;
84}
Patrick Venture79e131f2018-08-01 13:34:35 -070085
86ipmi_ret_t dataBlock(UpdateInterface* updater, const uint8_t* reqBuf,
87 uint8_t* replyBuf, size_t* dataLen)
88{
Patrick Venture79e131f2018-08-01 13:34:35 -070089 struct ChunkHdr hdr;
90 std::memcpy(&hdr, reqBuf, sizeof(hdr));
91
Patrick Venturecfe66872018-08-03 13:32:33 -070092 auto requestLength = *dataLen;
Patrick Venturea53a7b32018-08-03 09:15:20 -070093
Patrick Venture79e131f2018-08-01 13:34:35 -070094 /* Grab the bytes from the packet. */
Patrick Venturecfe66872018-08-03 13:32:33 -070095 auto bytesLength = requestLength - sizeof(struct ChunkHdr);
Patrick Venture79e131f2018-08-01 13:34:35 -070096 std::vector<uint8_t> bytes(bytesLength);
97 std::memcpy(bytes.data(), &reqBuf[sizeof(struct ChunkHdr)], bytesLength);
98
99 if (!updater->flashData(hdr.offset, bytes))
100 {
101 return IPMI_CC_INVALID;
102 }
103
104 /* We were successful and set the response byte to 0. */
105 replyBuf[0] = 0x00;
106 (*dataLen) = 1;
107 return IPMI_CC_OK;
108}
Patrick Venture2c1205d2018-08-03 10:23:14 -0700109
110ipmi_ret_t dataFinish(UpdateInterface* updater, const uint8_t* reqBuf,
111 uint8_t* replyBuf, size_t* dataLen)
112{
113 if (!updater->flashFinish())
114 {
115 return IPMI_CC_INVALID;
116 }
117
118 /* TODO: If all commands return this on success, handle it in one place. */
119
120 /* We were successful and set the response byte to 0. */
121 replyBuf[0] = 0x00;
122 (*dataLen) = 1;
123 return IPMI_CC_OK;
124}
Patrick Venture8d9f7322018-08-03 10:39:13 -0700125
126ipmi_ret_t startHash(UpdateInterface* updater, const uint8_t* reqBuf,
127 uint8_t* replyBuf, size_t* dataLen)
128{
129 auto request = reinterpret_cast<const struct StartTx*>(reqBuf);
130
131 if (!updater->startHash(request->length))
132 {
133 return IPMI_CC_INVALID;
134 }
135
136 /* We were successful and set the response byte to 0. */
137 replyBuf[0] = 0x00;
138 (*dataLen) = 1;
139 return IPMI_CC_OK;
140}
Patrick Venturecfe66872018-08-03 13:32:33 -0700141
142ipmi_ret_t hashBlock(UpdateInterface* updater, const uint8_t* reqBuf,
143 uint8_t* replyBuf, size_t* dataLen)
144{
145 struct ChunkHdr hdr;
146 std::memcpy(&hdr, reqBuf, sizeof(hdr));
147
148 auto requestLength = *dataLen;
149
150 /* Grab the bytes from the packet. */
151 auto bytesLength = requestLength - sizeof(struct ChunkHdr);
152 std::vector<uint8_t> bytes(bytesLength);
153 std::memcpy(bytes.data(), &reqBuf[sizeof(struct ChunkHdr)], bytesLength);
154
155 /* TODO: Refactor this and dataBlock for re-use. */
156
157 if (!updater->hashData(hdr.offset, bytes))
158 {
159 return IPMI_CC_INVALID;
160 }
161
162 /* We were successful and set the response byte to 0. */
163 replyBuf[0] = 0x00;
164 (*dataLen) = 1;
165 return IPMI_CC_OK;
166}
Patrick Venturefbc7d192018-08-03 13:54:21 -0700167
168ipmi_ret_t hashFinish(UpdateInterface* updater, const uint8_t* reqBuf,
169 uint8_t* replyBuf, size_t* dataLen)
170{
171 if (!updater->hashFinish())
172 {
173 return IPMI_CC_INVALID;
174 }
175
176 /* We were successful and set the response byte to 0. */
177 replyBuf[0] = 0x00;
178 (*dataLen) = 1;
179 return IPMI_CC_OK;
180}
Patrick Venture1cb87d22018-08-03 18:22:09 -0700181
182ipmi_ret_t dataVerify(UpdateInterface* updater, const uint8_t* reqBuf,
183 uint8_t* replyBuf, size_t* dataLen)
184{
185 if (!updater->startDataVerification())
186 {
187 return IPMI_CC_INVALID;
188 }
189
190 /* We were successful and set the response byte to 0. */
191 replyBuf[0] = 0x00;
192 (*dataLen) = 1;
193 return IPMI_CC_OK;
194}