blob: 6e28625905577d520de10474c6a7d40b642fc2c8 [file] [log] [blame]
Patrick Venture54c3b532018-08-01 11:45:49 -07001/*
Patrick Venture514f6482018-08-07 14:27:58 -07002 * Copyright 2018 Google Inc.
Patrick Venture54c3b532018-08-01 11:45:49 -07003 *
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 Venture1aedab22018-09-10 14:41:45 -070017#include "ipmi.hpp"
Patrick Venture79e131f2018-08-01 13:34:35 -070018
Patrick Venture54c3b532018-08-01 11:45:49 -070019#include "flash-ipmi.hpp"
Patrick Venture1aedab22018-09-10 14:41:45 -070020
21#include <cstring>
22#include <unordered_map>
Patrick Venture54c3b532018-08-01 11:45:49 -070023
Patrick Venture9a5a79a2018-08-03 17:23:57 -070024IpmiFlashHandler getCommandHandler(FlashSubCmds command)
25{
26 static const std::unordered_map<FlashSubCmds, IpmiFlashHandler>
27 subHandlers = {
28 {FlashSubCmds::flashStartTransfer, startTransfer},
29 {FlashSubCmds::flashDataBlock, dataBlock},
30 {FlashSubCmds::flashDataFinish, dataFinish},
31 {FlashSubCmds::flashStartHash, startHash},
32 {FlashSubCmds::flashHashData, hashBlock},
33 {FlashSubCmds::flashHashFinish, hashFinish},
Patrick Venture1cb87d22018-08-03 18:22:09 -070034 {FlashSubCmds::flashDataVerify, dataVerify},
Patrick Venture5c251ca2018-08-03 18:31:01 -070035 {FlashSubCmds::flashAbort, abortUpdate},
Patrick Venturefdc65b22018-08-07 14:37:58 -070036 {FlashSubCmds::flashVerifyCheck, checkVerify},
Patrick Venture9a5a79a2018-08-03 17:23:57 -070037 };
38
39 auto results = subHandlers.find(command);
40 if (results == subHandlers.end())
41 {
42 return nullptr;
43 }
44
45 return results->second;
46}
47
Patrick Venturea53a7b32018-08-03 09:15:20 -070048bool validateRequestLength(FlashSubCmds command, size_t requestLen)
49{
50 static const std::unordered_map<FlashSubCmds, size_t> minimumLengths = {
51 {FlashSubCmds::flashStartTransfer, sizeof(struct StartTx)},
52 {FlashSubCmds::flashDataBlock, sizeof(struct ChunkHdr) + 1},
Patrick Venture8d9f7322018-08-03 10:39:13 -070053 {FlashSubCmds::flashStartHash, sizeof(struct StartTx)},
Patrick Venturecfe66872018-08-03 13:32:33 -070054 {FlashSubCmds::flashHashData, sizeof(struct ChunkHdr) + 1},
Patrick Venturea53a7b32018-08-03 09:15:20 -070055 };
56
57 auto results = minimumLengths.find(command);
58 if (results == minimumLengths.end())
59 {
60 /* Valid length by default if we don't care. */
61 return true;
62 }
63
64 /* If the request is shorter than the minimum, it's invalid. */
65 if (requestLen < results->second)
66 {
67 return false;
68 }
69
70 return true;
71}
72
Patrick Venture54c3b532018-08-01 11:45:49 -070073ipmi_ret_t startTransfer(UpdateInterface* updater, const uint8_t* reqBuf,
74 uint8_t* replyBuf, size_t* dataLen)
75{
Patrick Venture54c3b532018-08-01 11:45:49 -070076 auto request = reinterpret_cast<const struct StartTx*>(reqBuf);
77
78 if (!updater->start(request->length))
79 {
80 return IPMI_CC_INVALID;
81 }
82
83 /* We were successful and set the response byte to 0. */
84 replyBuf[0] = 0x00;
85 (*dataLen) = 1;
86 return IPMI_CC_OK;
87}
Patrick Venture79e131f2018-08-01 13:34:35 -070088
89ipmi_ret_t dataBlock(UpdateInterface* updater, const uint8_t* reqBuf,
90 uint8_t* replyBuf, size_t* dataLen)
91{
Patrick Venture79e131f2018-08-01 13:34:35 -070092 struct ChunkHdr hdr;
93 std::memcpy(&hdr, reqBuf, sizeof(hdr));
94
Patrick Venturecfe66872018-08-03 13:32:33 -070095 auto requestLength = *dataLen;
Patrick Venturea53a7b32018-08-03 09:15:20 -070096
Patrick Venture79e131f2018-08-01 13:34:35 -070097 /* Grab the bytes from the packet. */
Patrick Venturecfe66872018-08-03 13:32:33 -070098 auto bytesLength = requestLength - sizeof(struct ChunkHdr);
Patrick Venture79e131f2018-08-01 13:34:35 -070099 std::vector<uint8_t> bytes(bytesLength);
100 std::memcpy(bytes.data(), &reqBuf[sizeof(struct ChunkHdr)], bytesLength);
101
102 if (!updater->flashData(hdr.offset, bytes))
103 {
104 return IPMI_CC_INVALID;
105 }
106
107 /* We were successful and set the response byte to 0. */
108 replyBuf[0] = 0x00;
109 (*dataLen) = 1;
110 return IPMI_CC_OK;
111}
Patrick Venture2c1205d2018-08-03 10:23:14 -0700112
113ipmi_ret_t dataFinish(UpdateInterface* updater, const uint8_t* reqBuf,
114 uint8_t* replyBuf, size_t* dataLen)
115{
116 if (!updater->flashFinish())
117 {
118 return IPMI_CC_INVALID;
119 }
120
121 /* TODO: If all commands return this on success, handle it in one place. */
122
123 /* We were successful and set the response byte to 0. */
124 replyBuf[0] = 0x00;
125 (*dataLen) = 1;
126 return IPMI_CC_OK;
127}
Patrick Venture8d9f7322018-08-03 10:39:13 -0700128
129ipmi_ret_t startHash(UpdateInterface* updater, const uint8_t* reqBuf,
130 uint8_t* replyBuf, size_t* dataLen)
131{
132 auto request = reinterpret_cast<const struct StartTx*>(reqBuf);
133
134 if (!updater->startHash(request->length))
135 {
136 return IPMI_CC_INVALID;
137 }
138
139 /* We were successful and set the response byte to 0. */
140 replyBuf[0] = 0x00;
141 (*dataLen) = 1;
142 return IPMI_CC_OK;
143}
Patrick Venturecfe66872018-08-03 13:32:33 -0700144
145ipmi_ret_t hashBlock(UpdateInterface* updater, const uint8_t* reqBuf,
146 uint8_t* replyBuf, size_t* dataLen)
147{
148 struct ChunkHdr hdr;
149 std::memcpy(&hdr, reqBuf, sizeof(hdr));
150
151 auto requestLength = *dataLen;
152
153 /* Grab the bytes from the packet. */
154 auto bytesLength = requestLength - sizeof(struct ChunkHdr);
155 std::vector<uint8_t> bytes(bytesLength);
156 std::memcpy(bytes.data(), &reqBuf[sizeof(struct ChunkHdr)], bytesLength);
157
158 /* TODO: Refactor this and dataBlock for re-use. */
159
160 if (!updater->hashData(hdr.offset, bytes))
161 {
162 return IPMI_CC_INVALID;
163 }
164
165 /* We were successful and set the response byte to 0. */
166 replyBuf[0] = 0x00;
167 (*dataLen) = 1;
168 return IPMI_CC_OK;
169}
Patrick Venturefbc7d192018-08-03 13:54:21 -0700170
171ipmi_ret_t hashFinish(UpdateInterface* updater, const uint8_t* reqBuf,
172 uint8_t* replyBuf, size_t* dataLen)
173{
174 if (!updater->hashFinish())
175 {
176 return IPMI_CC_INVALID;
177 }
178
179 /* We were successful and set the response byte to 0. */
180 replyBuf[0] = 0x00;
181 (*dataLen) = 1;
182 return IPMI_CC_OK;
183}
Patrick Venture1cb87d22018-08-03 18:22:09 -0700184
185ipmi_ret_t dataVerify(UpdateInterface* updater, const uint8_t* reqBuf,
186 uint8_t* replyBuf, size_t* dataLen)
187{
188 if (!updater->startDataVerification())
189 {
190 return IPMI_CC_INVALID;
191 }
192
193 /* We were successful and set the response byte to 0. */
194 replyBuf[0] = 0x00;
195 (*dataLen) = 1;
196 return IPMI_CC_OK;
197}
Patrick Venture5c251ca2018-08-03 18:31:01 -0700198
199ipmi_ret_t abortUpdate(UpdateInterface* updater, const uint8_t* reqBuf,
200 uint8_t* replyBuf, size_t* dataLen)
201{
202 /* TODO: May make sense to work all the pass-through commands into one
203 * piece of code
204 */
205 if (!updater->abortUpdate())
206 {
207 return IPMI_CC_INVALID;
208 }
209
210 /* We were successful and set the response byte to 0. */
211 replyBuf[0] = 0x00;
212 (*dataLen) = 1;
213 return IPMI_CC_OK;
214}
Patrick Venturefdc65b22018-08-07 14:37:58 -0700215
216ipmi_ret_t checkVerify(UpdateInterface* updater, const uint8_t* reqBuf,
217 uint8_t* replyBuf, size_t* dataLen)
218{
219 auto value = updater->checkVerify();
220 replyBuf[0] = static_cast<uint8_t>(value);
221 (*dataLen) = 1;
222 return IPMI_CC_OK;
223}