blob: 7d896d19adccee7f858284566146a2190f9b4fbf [file] [log] [blame]
Patrick Venture22e38752018-11-21 08:52:49 -08001/*
2 * Copyright 2018 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 Venturec7ca2912018-11-02 11:38:33 -070017#include "firmware_handler.hpp"
18
Patrick Venture84778b82019-06-26 20:11:09 -070019#include "data.hpp"
20#include "flags.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -080021#include "image_handler.hpp"
Patrick Venture1d66fe62019-06-03 14:57:27 -070022#include "status.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -070023#include "util.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -080024
Patrick Venture9efef5d2019-06-19 08:45:44 -070025#include <blobs-ipmid/blobs.hpp>
Patrick Venture9b37b092020-05-28 20:58:57 -070026
27#include <algorithm>
Patrick Venture192d60f2018-11-06 11:11:59 -080028#include <cstdint>
Patrick Venture18235e62018-11-08 10:21:09 -080029#include <cstring>
Patrick Ventureb3b4db72019-05-15 11:30:24 -070030#include <fstream>
Patrick Venture68cf64f2018-11-06 10:46:51 -080031#include <memory>
Patrick Venturefa6c4d92018-11-02 18:34:53 -070032#include <string>
33#include <vector>
34
Patrick Venture1d5a31c2019-05-20 11:38:22 -070035namespace ipmi_flash
Patrick Venturec7ca2912018-11-02 11:38:33 -070036{
Patrick Ventureb3b4db72019-05-15 11:30:24 -070037
Patrick Venture1d5a31c2019-05-20 11:38:22 -070038std::unique_ptr<blobs::GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080039 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventured4e20de2019-07-18 12:48:05 -070040 std::vector<HandlerPack>&& firmwares,
Patrick Venturefa06a5f2019-07-01 09:22:38 -070041 const std::vector<DataHandlerPack>& transports, ActionMap&& actionPacks)
Patrick Venture68cf64f2018-11-06 10:46:51 -080042{
Patrick Venture16265382019-11-06 19:31:36 -080043 /* There must be at least one in addition to the hash blob handler. */
44 if (firmwares.size() < 2)
Patrick Venture52854622018-11-06 12:30:00 -080045 {
Patrick Venture1ab824a2020-05-26 19:29:57 -070046 std::fprintf(stderr, "Must provide at least two firmware handlers.");
Patrick Venture52854622018-11-06 12:30:00 -080047 return nullptr;
48 }
Patrick Venture4e2a1432019-11-06 20:06:07 -080049 if (transports.empty())
Patrick Venture1cde5f92018-11-07 08:26:47 -080050 {
51 return nullptr;
52 }
Patrick Venture16265382019-11-06 19:31:36 -080053 if (actionPacks.empty())
54 {
55 return nullptr;
56 }
Patrick Venture52854622018-11-06 12:30:00 -080057
Patrick Venturea78e39f2018-11-06 18:37:06 -080058 std::vector<std::string> blobs;
59 for (const auto& item : firmwares)
60 {
61 blobs.push_back(item.blobName);
62 }
Patrick Venture18235e62018-11-08 10:21:09 -080063
Patrick Venture7dad86f2019-05-17 08:52:20 -070064 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId))
Patrick Venture18235e62018-11-08 10:21:09 -080065 {
66 return nullptr;
67 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080068
Benjamin Fair12901982019-11-12 13:55:46 -080069 return std::make_unique<FirmwareBlobHandler>(
70 std::move(firmwares), blobs, transports, std::move(actionPacks));
Patrick Venture68cf64f2018-11-06 10:46:51 -080071}
72
Patrick Ventured6461d62018-11-09 17:30:25 -080073/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070074bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
75{
Patrick Venture6032dc02019-05-17 11:01:44 -070076 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070077}
Patrick Venture53977962018-11-02 18:59:35 -070078
Patrick Ventured6461d62018-11-09 17:30:25 -080079/*
80 * Grab the list of supported firmware.
81 *
82 * If there's an open firmware session, it'll already be present in the
83 * list as "/flash/active/image", and if the hash has started,
84 * "/flash/active/hash" regardless of mechanism. This is done in the open
85 * comamnd, no extra work is required here.
86 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070087std::vector<std::string> FirmwareBlobHandler::getBlobIds()
88{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080089 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070090}
Patrick Venture53977962018-11-02 18:59:35 -070091
Patrick Ventured6461d62018-11-09 17:30:25 -080092/*
93 * Per the design, this mean abort, and this will trigger whatever
94 * appropriate actions are required to abort the process.
95 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070096bool FirmwareBlobHandler::deleteBlob(const std::string& path)
97{
Patrick Venture72676762019-06-17 11:22:38 -070098 switch (state)
99 {
100 case UpdateState::notYetStarted:
101 /* Trying to delete anything at this point has no effect and returns
102 * false.
103 */
104 return false;
105 case UpdateState::verificationPending:
Patrick Venture2ca66522019-06-17 11:58:38 -0700106 abortProcess();
107 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700108 case UpdateState::updatePending:
Patrick Venturec9f62392019-06-17 12:17:26 -0700109 abortProcess();
110 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700111 default:
112 break;
113 }
114
Patrick Venturebcc0c772019-06-17 10:42:06 -0700115 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700116}
Patrick Venture53977962018-11-02 18:59:35 -0700117
Patrick Ventured6461d62018-11-09 17:30:25 -0800118/*
119 * Stat on the files will return information such as what supported
120 * transport mechanisms are available.
121 *
122 * Stat on an active file or hash will return information such as the size
123 * of the data cached, and any additional pertinent information. The
124 * blob_state on the active files will return the state of the update.
125 */
Patrick Venturee312f392019-06-04 07:44:37 -0700126bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700127{
Patrick Venture46637c82018-11-06 15:20:24 -0800128 /* We know we support this path because canHandle is called ahead */
Patrick Ventured342a952019-05-29 09:09:10 -0700129 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700130 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800131 {
Patrick Ventured342a952019-05-29 09:09:10 -0700132 /* These blobs are placeholders that indicate things, or allow actions,
133 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800134 */
Patrick Ventured342a952019-05-29 09:09:10 -0700135 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800136 }
137
Patrick Ventured342a952019-05-29 09:09:10 -0700138 /* They are requesting information about the generic blob_id. */
Patrick Ventured342a952019-05-29 09:09:10 -0700139
Benjamin Fair12901982019-11-12 13:55:46 -0800140 /* Older host tools expect the blobState to contain a bitmask of available
141 * transport backends, so report that we support all of them in order to
142 * preserve backwards compatibility.
Patrick Ventured342a952019-05-29 09:09:10 -0700143 */
Benjamin Fair12901982019-11-12 13:55:46 -0800144 meta->blobState = transportMask;
145 meta->size = 0;
Patrick Ventured342a952019-05-29 09:09:10 -0700146 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700147}
Patrick Venture53977962018-11-02 18:59:35 -0700148
Patrick Ventureda66fd82019-06-03 11:11:24 -0700149ActionStatus FirmwareBlobHandler::getActionStatus()
Patrick Venturea2d82392019-06-03 10:55:17 -0700150{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700151 ActionStatus value = ActionStatus::unknown;
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700152 auto* pack = getActionPack();
Patrick Venturea2d82392019-06-03 10:55:17 -0700153
154 switch (state)
155 {
156 case UpdateState::verificationPending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700157 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700158 break;
159 case UpdateState::verificationStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700160 /* If we got here, there must be data AND a hash, not just a hash,
161 * therefore pack will be known. */
162 if (!pack)
163 {
164 break;
165 }
166 value = pack->verification->status();
Patrick Ventureda66fd82019-06-03 11:11:24 -0700167 lastVerificationStatus = value;
Patrick Venturea2d82392019-06-03 10:55:17 -0700168 break;
169 case UpdateState::verificationCompleted:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700170 value = lastVerificationStatus;
Patrick Venturea2d82392019-06-03 10:55:17 -0700171 break;
Patrick Venturea2d82392019-06-03 10:55:17 -0700172 case UpdateState::updatePending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700173 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700174 break;
175 case UpdateState::updateStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700176 if (!pack)
177 {
178 break;
179 }
180 value = pack->update->status();
Patrick Venturea2d82392019-06-03 10:55:17 -0700181 lastUpdateStatus = value;
182 break;
183 case UpdateState::updateCompleted:
184 value = lastUpdateStatus;
185 break;
186 default:
187 break;
188 }
189
190 return value;
191}
192
Patrick Venturec02849b2018-11-06 17:31:15 -0800193/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800194 * Return stat information on an open session. It therefore must be an active
195 * handle to either the active image or active hash.
Patrick Ventured6461d62018-11-09 17:30:25 -0800196 */
Patrick Venturee312f392019-06-04 07:44:37 -0700197bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800198{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800199 auto item = lookup.find(session);
200 if (item == lookup.end())
201 {
202 return false;
203 }
204
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700205 /* The size here refers to the size of the file -- of something analagous.
206 */
207 meta->size = (item->second->imageHandler)
208 ? item->second->imageHandler->getSize()
209 : 0;
210
211 meta->metadata.clear();
212
Patrick Ventureda66fd82019-06-03 11:11:24 -0700213 if (item->second->activePath == verifyBlobId ||
214 item->second->activePath == updateBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700215 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700216 ActionStatus value = getActionStatus();
Patrick Venture699750d2019-05-15 09:24:09 -0700217
Patrick Venturee955e072019-05-15 16:16:46 -0700218 meta->metadata.push_back(static_cast<std::uint8_t>(value));
219
220 /* Change the firmware handler's state and the blob's stat value
221 * depending.
222 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700223 if (value == ActionStatus::success || value == ActionStatus::failed)
Patrick Venturee955e072019-05-15 16:16:46 -0700224 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700225 if (item->second->activePath == verifyBlobId)
Patrick Venturee955e072019-05-15 16:16:46 -0700226 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700227 changeState(UpdateState::verificationCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700228 }
229 else
230 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700231 /* item->second->activePath == updateBlobId */
Patrick Venture75c0c272019-06-21 09:15:08 -0700232 changeState(UpdateState::updateCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700233 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700234
Patrick Venturef1f0f652019-06-03 09:10:19 -0700235 item->second->flags &= ~blobs::StateFlags::committing;
236
Patrick Ventureda66fd82019-06-03 11:11:24 -0700237 if (value == ActionStatus::success)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700238 {
239 item->second->flags |= blobs::StateFlags::committed;
240 }
241 else
242 {
243 item->second->flags |= blobs::StateFlags::commit_error;
244 }
245 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700246 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800247
Patrick Venturee955e072019-05-15 16:16:46 -0700248 /* The blobState here relates to an active sesion, so we should return the
249 * flags used to open this session.
250 */
251 meta->blobState = item->second->flags;
252
Patrick Venturecc7d1602018-11-15 13:58:33 -0800253 /* The metadata blob returned comes from the data handler... it's used for
254 * instance, in P2A bridging to get required information about the mapping,
255 * and is the "opposite" of the lpc writemeta requirement.
256 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800257 if (item->second->dataHandler)
258 {
Patrick Venture74304642019-01-17 09:31:04 -0800259 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800260 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
261 bytes.end());
262 }
263
Patrick Venturecc7d1602018-11-15 13:58:33 -0800264 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800265}
266
267/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800268 * If you open /flash/image or /flash/tarball, or /flash/hash it will
269 * interpret the open flags and perform whatever actions are required for
270 * that update process. The session returned can be used immediately for
271 * sending data down, without requiring one to open the new active file.
272 *
273 * If you open the active flash image or active hash it will let you
274 * overwrite pieces, depending on the state.
275 *
276 * Once the verification process has started the active files cannot be
277 * opened.
278 *
279 * You can only have one open session at a time. Which means, you can only
280 * have one file open at a time. Trying to open the hash blob_id while you
281 * still have the flash image blob_id open will fail. Opening the flash
282 * blob_id when it is already open will fail.
283 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700284bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
285 const std::string& path)
286{
Patrick Venturec02849b2018-11-06 17:31:15 -0800287 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800288 *
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800289 * Further on this, if there's an active session to the hash we don't allow
290 * re-opening the image, and if we have the image open, we don't allow
291 * opening the hash. This design decision may be re-evaluated, and changed
292 * to only allow one session per object type (of the two types). But,
293 * consider if the hash is open, do we want to allow writing to the image?
294 * And why would we? But, really, the point of no-return is once the
295 * verification process has begun -- which is done via commit() on the hash
296 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700297 */
Brandon Kim8fc60872019-10-18 15:15:50 -0700298 if (fileOpen())
Patrick Venturec02849b2018-11-06 17:31:15 -0800299 {
300 return false;
301 }
302
Patrick Venture723113f2019-06-05 09:38:35 -0700303 /* The active blobs are only meant to indicate status that something has
304 * opened the image file or the hash file.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800305 */
Patrick Venture19f5d882019-05-30 14:34:55 -0700306 if (path == activeImageBlobId || path == activeHashBlobId)
307 {
308 /* 2a) are they opening the active image? this can only happen if they
309 * already started one (due to canHandleBlob's behavior).
310 */
311 /* 2b) are they opening the active hash? this can only happen if they
312 * already started one (due to canHandleBlob's behavior).
313 */
314 return false;
315 }
316
Patrick Venture723113f2019-06-05 09:38:35 -0700317 /* Check that they've opened for writing - read back not currently
318 * supported.
319 */
320 if ((flags & blobs::OpenFlags::write) == 0)
321 {
322 return false;
323 }
324
325 /* Because canHandleBlob is called before open, we know that if they try to
326 * open the verifyBlobId, they're in a state where it's present.
327 */
328
329 switch (state)
330 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700331 case UpdateState::notYetStarted:
332 /* Only hashBlobId and firmware BlobIds present. */
333 break;
Patrick Venture723113f2019-06-05 09:38:35 -0700334 case UpdateState::uploadInProgress:
335 /* Unreachable code because if it's started a file is open. */
336 break;
337 case UpdateState::verificationPending:
338 /* Handle opening the verifyBlobId --> we know the image and hash
Brandon Kim8fc60872019-10-18 15:15:50 -0700339 * aren't open because of the fileOpen() check. They can still open
Patrick Venture723113f2019-06-05 09:38:35 -0700340 * other files from this state to transition back into
341 * uploadInProgress.
342 *
343 * The file must be opened for writing, but no transport mechanism
344 * specified since it's irrelevant.
345 */
346 if (path == verifyBlobId)
347 {
348 verifyImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700349
350 lookup[session] = &verifyImage;
351
Patrick Venture723113f2019-06-05 09:38:35 -0700352 return true;
353 }
354 break;
355 case UpdateState::verificationStarted:
356 case UpdateState::verificationCompleted:
357 /* Unreachable code because if it's started a file is open. */
358 return false;
359 case UpdateState::updatePending:
360 {
361 /* When in this state, they can only open the updateBlobId */
362 if (path == updateBlobId)
363 {
364 updateImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700365
366 lookup[session] = &updateImage;
367
Patrick Venture723113f2019-06-05 09:38:35 -0700368 return true;
369 }
370 else
371 {
372 return false;
373 }
374 }
375 case UpdateState::updateStarted:
376 case UpdateState::updateCompleted:
377 /* Unreachable code because if it's started a file is open. */
378 break;
379 default:
380 break;
381 }
382
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700383 /* To support multiple firmware options, we need to make sure they're
384 * opening the one they already opened during this update sequence, or it's
385 * the first time they're opening it.
386 */
387 if (path != hashBlobId)
388 {
389 /* If they're not opening the hashBlobId they must be opening a firmware
390 * handler.
391 */
392 if (openedFirmwareType.empty())
393 {
394 /* First time for this sequence. */
395 openedFirmwareType = path;
396 }
397 else
398 {
399 if (openedFirmwareType != path)
400 {
401 /* Previously, in this sequence they opened /flash/image, and
402 * now they're opening /flash/bios without finishing out
403 * /flash/image (for example).
404 */
405 std::fprintf(stderr, "Trying to open alternate firmware while "
406 "unfinished with other firmware.\n");
407 return false;
408 }
409 }
410 }
411
Patrick Venture723113f2019-06-05 09:38:35 -0700412 /* There are two abstractions at play, how you get the data and how you
413 * handle that data. such that, whether the data comes from the PCI bridge
414 * or LPC bridge is not connected to whether the data goes into a static
415 * layout flash update or a UBI tarball.
416 */
417
Benjamin Fair12901982019-11-12 13:55:46 -0800418 std::uint16_t transportFlag = flags & transportMask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800419
Patrick Venture18235e62018-11-08 10:21:09 -0800420 /* How are they expecting to copy this data? */
Benjamin Fair12901982019-11-12 13:55:46 -0800421 auto d = std::find_if(transports.begin(), transports.end(),
422 [&transportFlag](const auto& iter) {
423 return (iter.bitmask == transportFlag);
424 });
Patrick Venture18235e62018-11-08 10:21:09 -0800425 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800426 {
Patrick Venture18235e62018-11-08 10:21:09 -0800427 return false;
428 }
429
Benjamin Fair12901982019-11-12 13:55:46 -0800430 /* We found the transport handler they requested */
Patrick Venturec02849b2018-11-06 17:31:15 -0800431
Patrick Venture6e307a72018-11-09 18:21:17 -0800432 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
433 * only non-external data pathway -- but this is just a more generic
434 * approach to that.
435 */
436 if (d->handler)
437 {
438 /* If the data handler open call fails, open fails. */
439 if (!d->handler->open())
440 {
441 return false;
442 }
443 }
444
Patrick Venture70e30bf2019-01-17 10:29:28 -0800445 /* Do we have a file handler for the type of file they're opening.
446 * Note: This should only fail if something is somehow crazy wrong.
447 * Since the canHandle() said yes, and that's tied into the list of explicit
448 * firmware handers (and file handlers, like this'll know where to write the
449 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800450 */
Patrick Venture18235e62018-11-08 10:21:09 -0800451 auto h = std::find_if(
452 handlers.begin(), handlers.end(),
453 [&path](const auto& iter) { return (iter.blobName == path); });
454 if (h == handlers.end())
455 {
456 return false;
457 }
458
459 /* Ok, so we found a handler that matched, so call open() */
460 if (!h->handler->open(path))
461 {
462 return false;
463 }
464
Patrick Venture70e30bf2019-01-17 10:29:28 -0800465 Session* curr;
466 const std::string* active;
467
Patrick Venture7dad86f2019-05-17 08:52:20 -0700468 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800469 {
470 /* 2c) are they opening the /flash/hash ? (to start the process) */
471 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700472 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800473 }
474 else
475 {
476 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700477 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800478 }
479
Patrick Venture18235e62018-11-08 10:21:09 -0800480 curr->flags = flags;
481 curr->dataHandler = d->handler;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700482 curr->imageHandler = h->handler.get();
Patrick Venture18235e62018-11-08 10:21:09 -0800483
484 lookup[session] = curr;
485
Patrick Ventureefba42d2019-05-24 10:48:16 -0700486 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700487 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800488
Patrick Venture75c0c272019-06-21 09:15:08 -0700489 changeState(UpdateState::uploadInProgress);
Patrick Venture991910a2018-11-09 19:43:48 -0800490
Patrick Venture18235e62018-11-08 10:21:09 -0800491 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700492}
Patrick Venture53977962018-11-02 18:59:35 -0700493
Patrick Venture18235e62018-11-08 10:21:09 -0800494/**
495 * The write command really just grabs the data from wherever it is and sends it
496 * to the image handler. It's the image handler's responsibility to deal with
497 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800498 *
499 * This receives a session from the blob manager, therefore it is always called
500 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800501 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700502bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
503 const std::vector<uint8_t>& data)
504{
Patrick Venture18235e62018-11-08 10:21:09 -0800505 auto item = lookup.find(session);
506 if (item == lookup.end())
507 {
508 return false;
509 }
510
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800511 /* Prevent writing during verification. */
512 if (state == UpdateState::verificationStarted)
513 {
514 return false;
515 }
516
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700517 /* Prevent writing to the verification or update blobs. */
518 if (item->second->activePath == verifyBlobId ||
519 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700520 {
521 return false;
522 }
Patrick Venture699750d2019-05-15 09:24:09 -0700523
Patrick Venture18235e62018-11-08 10:21:09 -0800524 std::vector<std::uint8_t> bytes;
525
Patrick Venture84778b82019-06-26 20:11:09 -0700526 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800527 {
528 bytes = data;
529 }
530 else
531 {
532 /* little endian required per design, and so on, but TODO: do endianness
533 * with boost.
534 */
535 struct ExtChunkHdr header;
536
537 if (data.size() != sizeof(header))
538 {
539 return false;
540 }
541
542 std::memcpy(&header, data.data(), data.size());
543 bytes = item->second->dataHandler->copyFrom(header.length);
544 }
545
546 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700547}
Patrick Venture18235e62018-11-08 10:21:09 -0800548
Patrick Venture8c535332018-11-08 15:58:00 -0800549/*
550 * If the active session (image or hash) is over LPC, this allows
551 * configuring it. This option is only available before you start
552 * writing data for the given item (image or hash). This will return
553 * false at any other part. -- the lpc handler portion will know to return
554 * false.
555 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700556bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
557 const std::vector<uint8_t>& data)
558{
Patrick Venture8c535332018-11-08 15:58:00 -0800559 auto item = lookup.find(session);
560 if (item == lookup.end())
561 {
562 return false;
563 }
564
Patrick Venture84778b82019-06-26 20:11:09 -0700565 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800566 {
567 return false;
568 }
569
Patrick Ventured56097a2019-05-15 09:47:55 -0700570 /* Prevent writing meta to the verification blob (it has no data handler).
571 */
572 if (item->second->dataHandler)
573 {
574 return item->second->dataHandler->writeMeta(data);
575 }
Patrick Venture699750d2019-05-15 09:24:09 -0700576
Patrick Ventured56097a2019-05-15 09:47:55 -0700577 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700578}
Patrick Venture8c535332018-11-08 15:58:00 -0800579
Patrick Ventured6461d62018-11-09 17:30:25 -0800580/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700581 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800582 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800583 * the image.
584 *
585 * For this file to have opened, the other two must be closed, which means any
586 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800587 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700588bool FirmwareBlobHandler::commit(uint16_t session,
589 const std::vector<uint8_t>& data)
590{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800591 auto item = lookup.find(session);
592 if (item == lookup.end())
593 {
594 return false;
595 }
596
Patrick Venture1a406fe2019-05-31 07:29:56 -0700597 /* You can only commit on the verifyBlodId or updateBlobId */
598 if (item->second->activePath != verifyBlobId &&
599 item->second->activePath != updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800600 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700601 std::fprintf(stderr, "path: '%s' not expected for commit\n",
602 item->second->activePath.c_str());
Patrick Ventureffcc5502018-11-16 12:32:38 -0800603 return false;
604 }
605
Patrick Venture433cbc32019-05-30 09:53:10 -0700606 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800607 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700608 case UpdateState::verificationPending:
609 /* Set state to committing. */
610 item->second->flags |= blobs::StateFlags::committing;
611 return triggerVerification();
Patrick Venture433cbc32019-05-30 09:53:10 -0700612 case UpdateState::verificationStarted:
613 /* Calling repeatedly has no effect within an update process. */
614 return true;
615 case UpdateState::verificationCompleted:
616 /* Calling after the verification process has completed returns
617 * failure. */
618 return false;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700619 case UpdateState::updatePending:
Patrick Venture433cbc32019-05-30 09:53:10 -0700620 item->second->flags |= blobs::StateFlags::committing;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700621 return triggerUpdate();
Patrick Venture0079d892019-05-31 11:27:44 -0700622 case UpdateState::updateStarted:
623 /* Calling repeatedly has no effect within an update process. */
624 return true;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700625 default:
626 return false;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800627 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700628}
Patrick Ventured6461d62018-11-09 17:30:25 -0800629
630/*
631 * Close must be called on the firmware image before triggering
632 * verification via commit. Once the verification is complete, you can
633 * then close the hash file.
634 *
635 * If the `verify_image.service` returned success, closing the hash file
636 * will have a specific behavior depending on the update. If it's UBI,
637 * it'll perform the install. If it's static layout, it'll do nothing. The
638 * verify_image service in the static layout case is responsible for placing
639 * the file in the correct staging position.
640 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700641bool FirmwareBlobHandler::close(uint16_t session)
642{
Patrick Venture68bb1432018-11-09 20:08:48 -0800643 auto item = lookup.find(session);
644 if (item == lookup.end())
645 {
646 return false;
647 }
648
Patrick Venturee95dbb62019-06-05 09:59:29 -0700649 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800650 {
Patrick Venturee95dbb62019-06-05 09:59:29 -0700651 case UpdateState::uploadInProgress:
652 /* They are closing a data pathway (image, tarball, hash). */
Patrick Venture75c0c272019-06-21 09:15:08 -0700653 changeState(UpdateState::verificationPending);
Patrick Venture85ae86b2019-06-05 09:18:40 -0700654
Patrick Venture1999eef2019-07-01 11:44:09 -0700655 /* Add verify blob ID now that we can expect it, IIF they also wrote
656 * some data.
657 */
658 if (std::count(blobIDs.begin(), blobIDs.end(), activeImageBlobId))
659 {
660 addBlobId(verifyBlobId);
661 }
Patrick Venturee95dbb62019-06-05 09:59:29 -0700662 break;
663 case UpdateState::verificationPending:
664 /* They haven't triggered, therefore closing is uninteresting.
665 */
666 break;
667 case UpdateState::verificationStarted:
Patrick Ventured5741022019-06-17 09:08:35 -0700668 /* Abort without checking to see if it happened to finish. Require
669 * the caller to stat() deliberately.
Patrick Venturee95dbb62019-06-05 09:59:29 -0700670 */
Patrick Ventured5741022019-06-17 09:08:35 -0700671 abortVerification();
672 abortProcess();
673 break;
Patrick Venturee95dbb62019-06-05 09:59:29 -0700674 case UpdateState::verificationCompleted:
675 if (lastVerificationStatus == ActionStatus::success)
676 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700677 changeState(UpdateState::updatePending);
Patrick Venturee95dbb62019-06-05 09:59:29 -0700678 addBlobId(updateBlobId);
679 removeBlobId(verifyBlobId);
680 }
681 else
682 {
Patrick Venture5d9fa022019-06-17 13:13:30 -0700683 /* Verification failed, and the host-tool knows this by calling
684 * stat(), which triggered the state change to
685 * verificationCompleted.
686 *
687 * Therefore, let's abort the process at this point.
688 */
689 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700690 }
691 break;
692 case UpdateState::updatePending:
693 /* They haven't triggered the update, therefore this is
694 * uninteresting. */
695 break;
696 case UpdateState::updateStarted:
Patrick Venture49731712019-06-17 10:04:02 -0700697 /* Abort without checking to see if it happened to finish. Require
698 * the caller to stat() deliberately.
699 */
700 abortUpdate();
701 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700702 break;
703 case UpdateState::updateCompleted:
704 if (lastUpdateStatus == ActionStatus::failed)
705 {
706 /* TODO: lOG something? */
Patrick Venture4c7a4202019-06-17 13:06:55 -0700707 std::fprintf(stderr, "Update failed\n");
Patrick Venturee95dbb62019-06-05 09:59:29 -0700708 }
Patrick Venture930c7b72019-05-24 11:11:08 -0700709
Patrick Venture4c7a4202019-06-17 13:06:55 -0700710 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700711 break;
712 default:
713 break;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800714 }
715
Brandon Kima9f674c2019-10-18 15:18:49 -0700716 if (!lookup.empty())
Patrick Venture68bb1432018-11-09 20:08:48 -0800717 {
Brandon Kima9f674c2019-10-18 15:18:49 -0700718 if (item->second->dataHandler)
719 {
720 item->second->dataHandler->close();
721 }
722 if (item->second->imageHandler)
723 {
724 item->second->imageHandler->close();
725 }
726 lookup.erase(item);
Patrick Venture68bb1432018-11-09 20:08:48 -0800727 }
Patrick Venture68bb1432018-11-09 20:08:48 -0800728 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700729}
Patrick Ventured6461d62018-11-09 17:30:25 -0800730
Patrick Venture75c0c272019-06-21 09:15:08 -0700731void FirmwareBlobHandler::changeState(UpdateState next)
732{
733 state = next;
Patrick Venture6d7735d2019-06-21 10:03:19 -0700734
735 if (state == UpdateState::notYetStarted)
736 {
737 /* Going back to notyetstarted, let them trigger preparation again. */
738 preparationTriggered = false;
739 }
740 else if (state == UpdateState::uploadInProgress)
741 {
742 /* Store this transition logic here instead of ::open() */
743 if (!preparationTriggered)
744 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700745 auto* pack = getActionPack();
746 if (pack)
747 {
748 pack->preparation->trigger();
749 preparationTriggered = true;
750 }
Patrick Venture6d7735d2019-06-21 10:03:19 -0700751 }
752 }
Patrick Venture75c0c272019-06-21 09:15:08 -0700753}
754
Patrick Venturec7ca2912018-11-02 11:38:33 -0700755bool FirmwareBlobHandler::expire(uint16_t session)
756{
Patrick Venture92f26152020-05-26 19:47:36 -0700757 abortProcess();
758 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700759}
Patrick Ventured6461d62018-11-09 17:30:25 -0800760
761/*
762 * Currently, the design does not provide this with a function, however,
763 * it will likely change to support reading data back.
764 */
765std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
766 uint32_t offset,
767 uint32_t requestedSize)
768{
769 return {};
770}
771
Patrick Ventured5741022019-06-17 09:08:35 -0700772void FirmwareBlobHandler::abortProcess()
773{
774 /* Closing of open files is handled from close() -- Reaching here from
775 * delete may never be supported.
776 */
777 removeBlobId(verifyBlobId);
778 removeBlobId(updateBlobId);
779 removeBlobId(activeImageBlobId);
780 removeBlobId(activeHashBlobId);
781
Brandon Kima9f674c2019-10-18 15:18:49 -0700782 for (auto item : lookup)
783 {
784 if (item.second->dataHandler)
785 {
786 item.second->dataHandler->close();
787 }
788 if (item.second->imageHandler)
789 {
790 item.second->imageHandler->close();
791 }
792 }
793 lookup.clear();
794
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700795 openedFirmwareType = "";
Patrick Venture75c0c272019-06-21 09:15:08 -0700796 changeState(UpdateState::notYetStarted);
Patrick Ventured5741022019-06-17 09:08:35 -0700797}
798
799void FirmwareBlobHandler::abortVerification()
800{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700801 auto* pack = getActionPack();
802 if (pack)
803 {
804 pack->verification->abort();
805 }
Patrick Ventured5741022019-06-17 09:08:35 -0700806}
807
Patrick Ventureffcc5502018-11-16 12:32:38 -0800808bool FirmwareBlobHandler::triggerVerification()
809{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700810 auto* pack = getActionPack();
811 if (!pack)
812 {
813 return false;
814 }
815
816 bool result = pack->verification->trigger();
Patrick Venture3ecb3502019-05-17 11:03:51 -0700817 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800818 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700819 changeState(UpdateState::verificationStarted);
Patrick Venturecabc1172018-11-16 16:14:26 -0800820 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800821
Patrick Venture3ecb3502019-05-17 11:03:51 -0700822 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800823}
824
Patrick Venture49731712019-06-17 10:04:02 -0700825void FirmwareBlobHandler::abortUpdate()
826{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700827 auto* pack = getActionPack();
828 if (pack)
829 {
830 pack->update->abort();
831 }
Patrick Venture49731712019-06-17 10:04:02 -0700832}
833
Patrick Venture1a406fe2019-05-31 07:29:56 -0700834bool FirmwareBlobHandler::triggerUpdate()
835{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700836 auto* pack = getActionPack();
837 if (!pack)
838 {
839 return false;
840 }
841
842 bool result = pack->update->trigger();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700843 if (result)
844 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700845 changeState(UpdateState::updateStarted);
Patrick Venture1a406fe2019-05-31 07:29:56 -0700846 }
847
848 return result;
849}
850
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700851} // namespace ipmi_flash