blob: 9268809edcde62d09524f9efaf7c87b98de01489 [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 Venture7b783432020-09-22 15:55:08 -070041 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;
Willy Tu99d292a2022-02-08 17:40:58 -080059 blobs.reserve(firmwares.size());
60 std::for_each(
61 firmwares.begin(), firmwares.end(),
62 [&blobs](const auto& blob) { blobs.emplace_back(blob.blobName); });
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
Patrick Venture7b783432020-09-22 15:55:08 -070069 return std::make_unique<FirmwareBlobHandler>(std::move(firmwares), blobs,
70 std::move(transports),
71 std::move(actionPacks));
Patrick Venture68cf64f2018-11-06 10:46:51 -080072}
73
Patrick Ventured6461d62018-11-09 17:30:25 -080074/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070075bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
76{
Patrick Venture6032dc02019-05-17 11:01:44 -070077 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070078}
Patrick Venture53977962018-11-02 18:59:35 -070079
Patrick Ventured6461d62018-11-09 17:30:25 -080080/*
81 * Grab the list of supported firmware.
82 *
83 * If there's an open firmware session, it'll already be present in the
84 * list as "/flash/active/image", and if the hash has started,
85 * "/flash/active/hash" regardless of mechanism. This is done in the open
86 * comamnd, no extra work is required here.
87 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070088std::vector<std::string> FirmwareBlobHandler::getBlobIds()
89{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080090 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070091}
Patrick Venture53977962018-11-02 18:59:35 -070092
Patrick Ventured6461d62018-11-09 17:30:25 -080093/*
94 * Per the design, this mean abort, and this will trigger whatever
95 * appropriate actions are required to abort the process.
96 */
Willy Tub487eb42021-09-16 21:44:43 -070097bool FirmwareBlobHandler::deleteBlob(const std::string&)
Patrick Venturec7ca2912018-11-02 11:38:33 -070098{
Patrick Venture72676762019-06-17 11:22:38 -070099 switch (state)
100 {
101 case UpdateState::notYetStarted:
102 /* Trying to delete anything at this point has no effect and returns
103 * false.
104 */
105 return false;
106 case UpdateState::verificationPending:
Patrick Venture2ca66522019-06-17 11:58:38 -0700107 abortProcess();
108 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700109 case UpdateState::updatePending:
Patrick Venturec9f62392019-06-17 12:17:26 -0700110 abortProcess();
111 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700112 default:
113 break;
114 }
115
Patrick Venturebcc0c772019-06-17 10:42:06 -0700116 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700117}
Patrick Venture53977962018-11-02 18:59:35 -0700118
Patrick Ventured6461d62018-11-09 17:30:25 -0800119/*
120 * Stat on the files will return information such as what supported
121 * transport mechanisms are available.
122 *
123 * Stat on an active file or hash will return information such as the size
124 * of the data cached, and any additional pertinent information. The
125 * blob_state on the active files will return the state of the update.
126 */
Patrick Venturee312f392019-06-04 07:44:37 -0700127bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700128{
Patrick Venture46637c82018-11-06 15:20:24 -0800129 /* We know we support this path because canHandle is called ahead */
Patrick Ventured342a952019-05-29 09:09:10 -0700130 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700131 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800132 {
Patrick Ventured342a952019-05-29 09:09:10 -0700133 /* These blobs are placeholders that indicate things, or allow actions,
134 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800135 */
Patrick Ventured342a952019-05-29 09:09:10 -0700136 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800137 }
138
Patrick Ventured342a952019-05-29 09:09:10 -0700139 /* They are requesting information about the generic blob_id. */
Patrick Ventured342a952019-05-29 09:09:10 -0700140
Benjamin Fair12901982019-11-12 13:55:46 -0800141 /* Older host tools expect the blobState to contain a bitmask of available
142 * transport backends, so report that we support all of them in order to
143 * preserve backwards compatibility.
Patrick Ventured342a952019-05-29 09:09:10 -0700144 */
Benjamin Fair12901982019-11-12 13:55:46 -0800145 meta->blobState = transportMask;
146 meta->size = 0;
Patrick Ventured342a952019-05-29 09:09:10 -0700147 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700148}
Patrick Venture53977962018-11-02 18:59:35 -0700149
Patrick Ventureda66fd82019-06-03 11:11:24 -0700150ActionStatus FirmwareBlobHandler::getActionStatus()
Patrick Venturea2d82392019-06-03 10:55:17 -0700151{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700152 ActionStatus value = ActionStatus::unknown;
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700153 auto* pack = getActionPack();
Patrick Venturea2d82392019-06-03 10:55:17 -0700154
155 switch (state)
156 {
157 case UpdateState::verificationPending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700158 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700159 break;
160 case UpdateState::verificationStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700161 /* If we got here, there must be data AND a hash, not just a hash,
162 * therefore pack will be known. */
163 if (!pack)
164 {
165 break;
166 }
167 value = pack->verification->status();
Patrick Ventureda66fd82019-06-03 11:11:24 -0700168 lastVerificationStatus = value;
Patrick Venturea2d82392019-06-03 10:55:17 -0700169 break;
170 case UpdateState::verificationCompleted:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700171 value = lastVerificationStatus;
Patrick Venturea2d82392019-06-03 10:55:17 -0700172 break;
Patrick Venturea2d82392019-06-03 10:55:17 -0700173 case UpdateState::updatePending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700174 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700175 break;
176 case UpdateState::updateStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700177 if (!pack)
178 {
179 break;
180 }
181 value = pack->update->status();
Patrick Venturea2d82392019-06-03 10:55:17 -0700182 lastUpdateStatus = value;
183 break;
184 case UpdateState::updateCompleted:
185 value = lastUpdateStatus;
186 break;
187 default:
188 break;
189 }
190
191 return value;
192}
193
Patrick Venturec02849b2018-11-06 17:31:15 -0800194/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800195 * Return stat information on an open session. It therefore must be an active
196 * handle to either the active image or active hash.
Patrick Ventured6461d62018-11-09 17:30:25 -0800197 */
Patrick Venturee312f392019-06-04 07:44:37 -0700198bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800199{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800200 auto item = lookup.find(session);
201 if (item == lookup.end())
202 {
203 return false;
204 }
205
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700206 /* The size here refers to the size of the file -- of something analagous.
207 */
208 meta->size = (item->second->imageHandler)
209 ? item->second->imageHandler->getSize()
210 : 0;
211
212 meta->metadata.clear();
213
Patrick Ventureda66fd82019-06-03 11:11:24 -0700214 if (item->second->activePath == verifyBlobId ||
215 item->second->activePath == updateBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700216 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700217 ActionStatus value = getActionStatus();
Patrick Venture699750d2019-05-15 09:24:09 -0700218
Patrick Venturee955e072019-05-15 16:16:46 -0700219 meta->metadata.push_back(static_cast<std::uint8_t>(value));
220
221 /* Change the firmware handler's state and the blob's stat value
222 * depending.
223 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700224 if (value == ActionStatus::success || value == ActionStatus::failed)
Patrick Venturee955e072019-05-15 16:16:46 -0700225 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700226 if (item->second->activePath == verifyBlobId)
Patrick Venturee955e072019-05-15 16:16:46 -0700227 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700228 changeState(UpdateState::verificationCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700229 }
230 else
231 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700232 /* item->second->activePath == updateBlobId */
Patrick Venture75c0c272019-06-21 09:15:08 -0700233 changeState(UpdateState::updateCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700234 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700235
Patrick Venturef1f0f652019-06-03 09:10:19 -0700236 item->second->flags &= ~blobs::StateFlags::committing;
237
Patrick Ventureda66fd82019-06-03 11:11:24 -0700238 if (value == ActionStatus::success)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700239 {
240 item->second->flags |= blobs::StateFlags::committed;
241 }
242 else
243 {
244 item->second->flags |= blobs::StateFlags::commit_error;
245 }
246 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700247 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800248
Patrick Venturee955e072019-05-15 16:16:46 -0700249 /* The blobState here relates to an active sesion, so we should return the
250 * flags used to open this session.
251 */
252 meta->blobState = item->second->flags;
253
Patrick Venturecc7d1602018-11-15 13:58:33 -0800254 /* The metadata blob returned comes from the data handler... it's used for
255 * instance, in P2A bridging to get required information about the mapping,
256 * and is the "opposite" of the lpc writemeta requirement.
257 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800258 if (item->second->dataHandler)
259 {
Patrick Venture74304642019-01-17 09:31:04 -0800260 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800261 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
262 bytes.end());
263 }
264
Patrick Venturecc7d1602018-11-15 13:58:33 -0800265 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800266}
267
268/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800269 * If you open /flash/image or /flash/tarball, or /flash/hash it will
270 * interpret the open flags and perform whatever actions are required for
271 * that update process. The session returned can be used immediately for
272 * sending data down, without requiring one to open the new active file.
273 *
274 * If you open the active flash image or active hash it will let you
275 * overwrite pieces, depending on the state.
276 *
277 * Once the verification process has started the active files cannot be
278 * opened.
279 *
280 * You can only have one open session at a time. Which means, you can only
281 * have one file open at a time. Trying to open the hash blob_id while you
282 * still have the flash image blob_id open will fail. Opening the flash
283 * blob_id when it is already open will fail.
284 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700285bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
286 const std::string& path)
287{
Patrick Venturec02849b2018-11-06 17:31:15 -0800288 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800289 *
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800290 * Further on this, if there's an active session to the hash we don't allow
291 * re-opening the image, and if we have the image open, we don't allow
292 * opening the hash. This design decision may be re-evaluated, and changed
293 * to only allow one session per object type (of the two types). But,
294 * consider if the hash is open, do we want to allow writing to the image?
295 * And why would we? But, really, the point of no-return is once the
296 * verification process has begun -- which is done via commit() on the hash
297 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700298 */
Brandon Kim8fc60872019-10-18 15:15:50 -0700299 if (fileOpen())
Patrick Venturec02849b2018-11-06 17:31:15 -0800300 {
301 return false;
302 }
303
Patrick Venture723113f2019-06-05 09:38:35 -0700304 /* The active blobs are only meant to indicate status that something has
305 * opened the image file or the hash file.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800306 */
Patrick Venture19f5d882019-05-30 14:34:55 -0700307 if (path == activeImageBlobId || path == activeHashBlobId)
308 {
309 /* 2a) are they opening the active image? this can only happen if they
310 * already started one (due to canHandleBlob's behavior).
311 */
312 /* 2b) are they opening the active hash? this can only happen if they
313 * already started one (due to canHandleBlob's behavior).
314 */
315 return false;
316 }
317
Patrick Venture723113f2019-06-05 09:38:35 -0700318 /* Check that they've opened for writing - read back not currently
319 * supported.
320 */
321 if ((flags & blobs::OpenFlags::write) == 0)
322 {
323 return false;
324 }
325
326 /* Because canHandleBlob is called before open, we know that if they try to
327 * open the verifyBlobId, they're in a state where it's present.
328 */
329
330 switch (state)
331 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700332 case UpdateState::notYetStarted:
333 /* Only hashBlobId and firmware BlobIds present. */
334 break;
Patrick Venture723113f2019-06-05 09:38:35 -0700335 case UpdateState::uploadInProgress:
336 /* Unreachable code because if it's started a file is open. */
337 break;
338 case UpdateState::verificationPending:
339 /* Handle opening the verifyBlobId --> we know the image and hash
Brandon Kim8fc60872019-10-18 15:15:50 -0700340 * aren't open because of the fileOpen() check. They can still open
Patrick Venture723113f2019-06-05 09:38:35 -0700341 * other files from this state to transition back into
342 * uploadInProgress.
343 *
344 * The file must be opened for writing, but no transport mechanism
345 * specified since it's irrelevant.
346 */
347 if (path == verifyBlobId)
348 {
349 verifyImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700350
351 lookup[session] = &verifyImage;
352
Patrick Venture723113f2019-06-05 09:38:35 -0700353 return true;
354 }
355 break;
356 case UpdateState::verificationStarted:
357 case UpdateState::verificationCompleted:
358 /* Unreachable code because if it's started a file is open. */
359 return false;
360 case UpdateState::updatePending:
361 {
362 /* When in this state, they can only open the updateBlobId */
363 if (path == updateBlobId)
364 {
365 updateImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700366
367 lookup[session] = &updateImage;
368
Patrick Venture723113f2019-06-05 09:38:35 -0700369 return true;
370 }
371 else
372 {
373 return false;
374 }
375 }
376 case UpdateState::updateStarted:
377 case UpdateState::updateCompleted:
378 /* Unreachable code because if it's started a file is open. */
379 break;
380 default:
381 break;
382 }
383
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700384 /* To support multiple firmware options, we need to make sure they're
385 * opening the one they already opened during this update sequence, or it's
386 * the first time they're opening it.
387 */
388 if (path != hashBlobId)
389 {
390 /* If they're not opening the hashBlobId they must be opening a firmware
391 * handler.
392 */
393 if (openedFirmwareType.empty())
394 {
395 /* First time for this sequence. */
396 openedFirmwareType = path;
397 }
398 else
399 {
400 if (openedFirmwareType != path)
401 {
402 /* Previously, in this sequence they opened /flash/image, and
403 * now they're opening /flash/bios without finishing out
404 * /flash/image (for example).
405 */
406 std::fprintf(stderr, "Trying to open alternate firmware while "
407 "unfinished with other firmware.\n");
408 return false;
409 }
410 }
411 }
412
Patrick Venture723113f2019-06-05 09:38:35 -0700413 /* There are two abstractions at play, how you get the data and how you
414 * handle that data. such that, whether the data comes from the PCI bridge
415 * or LPC bridge is not connected to whether the data goes into a static
416 * layout flash update or a UBI tarball.
417 */
418
Benjamin Fair12901982019-11-12 13:55:46 -0800419 std::uint16_t transportFlag = flags & transportMask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800420
Patrick Venture18235e62018-11-08 10:21:09 -0800421 /* How are they expecting to copy this data? */
Benjamin Fair12901982019-11-12 13:55:46 -0800422 auto d = std::find_if(transports.begin(), transports.end(),
423 [&transportFlag](const auto& iter) {
424 return (iter.bitmask == transportFlag);
425 });
Patrick Venture18235e62018-11-08 10:21:09 -0800426 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800427 {
Patrick Venture18235e62018-11-08 10:21:09 -0800428 return false;
429 }
430
Benjamin Fair12901982019-11-12 13:55:46 -0800431 /* We found the transport handler they requested */
Patrick Venturec02849b2018-11-06 17:31:15 -0800432
Patrick Venture6e307a72018-11-09 18:21:17 -0800433 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
434 * only non-external data pathway -- but this is just a more generic
435 * approach to that.
436 */
437 if (d->handler)
438 {
439 /* If the data handler open call fails, open fails. */
440 if (!d->handler->open())
441 {
442 return false;
443 }
444 }
445
Patrick Venture70e30bf2019-01-17 10:29:28 -0800446 /* Do we have a file handler for the type of file they're opening.
447 * Note: This should only fail if something is somehow crazy wrong.
448 * Since the canHandle() said yes, and that's tied into the list of explicit
449 * firmware handers (and file handlers, like this'll know where to write the
450 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800451 */
Patrick Venture18235e62018-11-08 10:21:09 -0800452 auto h = std::find_if(
453 handlers.begin(), handlers.end(),
454 [&path](const auto& iter) { return (iter.blobName == path); });
455 if (h == handlers.end())
456 {
457 return false;
458 }
459
460 /* Ok, so we found a handler that matched, so call open() */
Jason Ling56a22732020-10-23 19:53:17 -0700461 if (!h->handler->open(path, std::ios::out))
Patrick Venture18235e62018-11-08 10:21:09 -0800462 {
463 return false;
464 }
465
Patrick Venture70e30bf2019-01-17 10:29:28 -0800466 Session* curr;
Patrick Ventureede9c9f2020-09-30 13:49:19 -0700467 const char* active;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800468
Patrick Venture7dad86f2019-05-17 08:52:20 -0700469 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800470 {
471 /* 2c) are they opening the /flash/hash ? (to start the process) */
472 curr = &activeHash;
Patrick Ventureede9c9f2020-09-30 13:49:19 -0700473 active = activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800474 }
475 else
476 {
477 curr = &activeImage;
Patrick Ventureede9c9f2020-09-30 13:49:19 -0700478 active = activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800479 }
480
Patrick Venture18235e62018-11-08 10:21:09 -0800481 curr->flags = flags;
Patrick Venture4934daa2020-09-22 16:37:44 -0700482 curr->dataHandler = d->handler.get();
Patrick Ventured4e20de2019-07-18 12:48:05 -0700483 curr->imageHandler = h->handler.get();
Patrick Venture18235e62018-11-08 10:21:09 -0800484
485 lookup[session] = curr;
486
Patrick Ventureede9c9f2020-09-30 13:49:19 -0700487 addBlobId(active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700488 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800489
Patrick Venture75c0c272019-06-21 09:15:08 -0700490 changeState(UpdateState::uploadInProgress);
Patrick Venture991910a2018-11-09 19:43:48 -0800491
Patrick Venture18235e62018-11-08 10:21:09 -0800492 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700493}
Patrick Venture53977962018-11-02 18:59:35 -0700494
Patrick Venture18235e62018-11-08 10:21:09 -0800495/**
496 * The write command really just grabs the data from wherever it is and sends it
497 * to the image handler. It's the image handler's responsibility to deal with
498 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800499 *
500 * This receives a session from the blob manager, therefore it is always called
501 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800502 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700503bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
504 const std::vector<uint8_t>& data)
505{
Patrick Venture18235e62018-11-08 10:21:09 -0800506 auto item = lookup.find(session);
507 if (item == lookup.end())
508 {
509 return false;
510 }
511
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800512 /* Prevent writing during verification. */
513 if (state == UpdateState::verificationStarted)
514 {
515 return false;
516 }
517
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700518 /* Prevent writing to the verification or update blobs. */
519 if (item->second->activePath == verifyBlobId ||
520 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700521 {
522 return false;
523 }
Patrick Venture699750d2019-05-15 09:24:09 -0700524
Patrick Venture18235e62018-11-08 10:21:09 -0800525 std::vector<std::uint8_t> bytes;
526
Patrick Venture84778b82019-06-26 20:11:09 -0700527 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800528 {
529 bytes = data;
530 }
531 else
532 {
533 /* little endian required per design, and so on, but TODO: do endianness
534 * with boost.
535 */
536 struct ExtChunkHdr header;
537
538 if (data.size() != sizeof(header))
539 {
540 return false;
541 }
542
543 std::memcpy(&header, data.data(), data.size());
544 bytes = item->second->dataHandler->copyFrom(header.length);
545 }
546
547 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700548}
Patrick Venture18235e62018-11-08 10:21:09 -0800549
Patrick Venture8c535332018-11-08 15:58:00 -0800550/*
551 * If the active session (image or hash) is over LPC, this allows
552 * configuring it. This option is only available before you start
553 * writing data for the given item (image or hash). This will return
554 * false at any other part. -- the lpc handler portion will know to return
555 * false.
556 */
Willy Tub487eb42021-09-16 21:44:43 -0700557bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t,
Patrick Venturec7ca2912018-11-02 11:38:33 -0700558 const std::vector<uint8_t>& data)
559{
Patrick Venture8c535332018-11-08 15:58:00 -0800560 auto item = lookup.find(session);
561 if (item == lookup.end())
562 {
563 return false;
564 }
565
Patrick Venture84778b82019-06-26 20:11:09 -0700566 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800567 {
568 return false;
569 }
570
Patrick Ventured56097a2019-05-15 09:47:55 -0700571 /* Prevent writing meta to the verification blob (it has no data handler).
572 */
573 if (item->second->dataHandler)
574 {
575 return item->second->dataHandler->writeMeta(data);
576 }
Patrick Venture699750d2019-05-15 09:24:09 -0700577
Patrick Ventured56097a2019-05-15 09:47:55 -0700578 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700579}
Patrick Venture8c535332018-11-08 15:58:00 -0800580
Patrick Ventured6461d62018-11-09 17:30:25 -0800581/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700582 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800583 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800584 * the image.
585 *
586 * For this file to have opened, the other two must be closed, which means any
587 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800588 */
Willy Tub487eb42021-09-16 21:44:43 -0700589bool FirmwareBlobHandler::commit(uint16_t session, const std::vector<uint8_t>&)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700590{
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
Willy Tub487eb42021-09-16 21:44:43 -0700755bool FirmwareBlobHandler::expire(uint16_t)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700756{
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 */
Willy Tub487eb42021-09-16 21:44:43 -0700765std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t, uint32_t, uint32_t)
Patrick Ventured6461d62018-11-09 17:30:25 -0800766{
767 return {};
768}
769
Patrick Ventured5741022019-06-17 09:08:35 -0700770void FirmwareBlobHandler::abortProcess()
771{
772 /* Closing of open files is handled from close() -- Reaching here from
773 * delete may never be supported.
774 */
775 removeBlobId(verifyBlobId);
776 removeBlobId(updateBlobId);
777 removeBlobId(activeImageBlobId);
778 removeBlobId(activeHashBlobId);
779
Brandon Kima9f674c2019-10-18 15:18:49 -0700780 for (auto item : lookup)
781 {
782 if (item.second->dataHandler)
783 {
784 item.second->dataHandler->close();
785 }
786 if (item.second->imageHandler)
787 {
788 item.second->imageHandler->close();
789 }
790 }
791 lookup.clear();
792
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700793 openedFirmwareType = "";
Patrick Venture75c0c272019-06-21 09:15:08 -0700794 changeState(UpdateState::notYetStarted);
Patrick Ventured5741022019-06-17 09:08:35 -0700795}
796
797void FirmwareBlobHandler::abortVerification()
798{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700799 auto* pack = getActionPack();
800 if (pack)
801 {
802 pack->verification->abort();
803 }
Patrick Ventured5741022019-06-17 09:08:35 -0700804}
805
Patrick Ventureffcc5502018-11-16 12:32:38 -0800806bool FirmwareBlobHandler::triggerVerification()
807{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700808 auto* pack = getActionPack();
809 if (!pack)
810 {
811 return false;
812 }
813
814 bool result = pack->verification->trigger();
Patrick Venture3ecb3502019-05-17 11:03:51 -0700815 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800816 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700817 changeState(UpdateState::verificationStarted);
Patrick Venturecabc1172018-11-16 16:14:26 -0800818 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800819
Patrick Venture3ecb3502019-05-17 11:03:51 -0700820 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800821}
822
Patrick Venture49731712019-06-17 10:04:02 -0700823void FirmwareBlobHandler::abortUpdate()
824{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700825 auto* pack = getActionPack();
826 if (pack)
827 {
828 pack->update->abort();
829 }
Patrick Venture49731712019-06-17 10:04:02 -0700830}
831
Patrick Venture1a406fe2019-05-31 07:29:56 -0700832bool FirmwareBlobHandler::triggerUpdate()
833{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700834 auto* pack = getActionPack();
835 if (!pack)
836 {
837 return false;
838 }
839
840 bool result = pack->update->trigger();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700841 if (result)
842 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700843 changeState(UpdateState::updateStarted);
Patrick Venture1a406fe2019-05-31 07:29:56 -0700844 }
845
846 return result;
847}
848
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700849} // namespace ipmi_flash