blob: d193a4df4be93de3b901a52ac3c496b9b3e40f94 [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 Venture137ad2c2018-11-06 11:30:43 -080025#include <algorithm>
Patrick Venture9efef5d2019-06-19 08:45:44 -070026#include <blobs-ipmid/blobs.hpp>
Patrick Venture192d60f2018-11-06 11:11:59 -080027#include <cstdint>
Patrick Venture18235e62018-11-08 10:21:09 -080028#include <cstring>
Patrick Ventureb3b4db72019-05-15 11:30:24 -070029#include <fstream>
Patrick Venture68cf64f2018-11-06 10:46:51 -080030#include <memory>
Patrick Ventured333a872018-12-03 16:24:26 -080031#include <phosphor-logging/log.hpp>
Patrick Venturefa6c4d92018-11-02 18:34:53 -070032#include <string>
33#include <vector>
34
Patrick Ventured333a872018-12-03 16:24:26 -080035using namespace phosphor::logging;
36
Patrick Venture1d5a31c2019-05-20 11:38:22 -070037namespace ipmi_flash
Patrick Venturec7ca2912018-11-02 11:38:33 -070038{
Patrick Ventureb3b4db72019-05-15 11:30:24 -070039
Patrick Venture1d5a31c2019-05-20 11:38:22 -070040std::unique_ptr<blobs::GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080041 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070042 const std::vector<HandlerPack>& firmwares,
Patrick Venturefa06a5f2019-07-01 09:22:38 -070043 const std::vector<DataHandlerPack>& transports, ActionMap&& actionPacks)
Patrick Venture68cf64f2018-11-06 10:46:51 -080044{
Patrick Venture52854622018-11-06 12:30:00 -080045 /* There must be at least one. */
46 if (!firmwares.size())
47 {
Patrick Ventured333a872018-12-03 16:24:26 -080048 log<level::ERR>("Must provide at least one firmware handler.");
Patrick Venture52854622018-11-06 12:30:00 -080049 return nullptr;
50 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080051 if (!transports.size())
52 {
53 return nullptr;
54 }
Patrick Venture52854622018-11-06 12:30:00 -080055
Patrick Venturea78e39f2018-11-06 18:37:06 -080056 std::vector<std::string> blobs;
57 for (const auto& item : firmwares)
58 {
59 blobs.push_back(item.blobName);
60 }
Patrick Venture18235e62018-11-08 10:21:09 -080061
Patrick Venture7dad86f2019-05-17 08:52:20 -070062 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId))
Patrick Venture18235e62018-11-08 10:21:09 -080063 {
64 return nullptr;
65 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080066
Patrick Venture1cde5f92018-11-07 08:26:47 -080067 std::uint16_t bitmask = 0;
68 for (const auto& item : transports)
69 {
70 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
71 bitmask |= item.bitmask;
72 }
73
Patrick Venture3ecb3502019-05-17 11:03:51 -070074 return std::make_unique<FirmwareBlobHandler>(
Patrick Venturefa06a5f2019-07-01 09:22:38 -070075 firmwares, blobs, transports, bitmask, std::move(actionPacks));
Patrick Venture68cf64f2018-11-06 10:46:51 -080076}
77
Patrick Ventured6461d62018-11-09 17:30:25 -080078/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070079bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
80{
Patrick Venture6032dc02019-05-17 11:01:44 -070081 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070082}
Patrick Venture53977962018-11-02 18:59:35 -070083
Patrick Ventured6461d62018-11-09 17:30:25 -080084/*
85 * Grab the list of supported firmware.
86 *
87 * If there's an open firmware session, it'll already be present in the
88 * list as "/flash/active/image", and if the hash has started,
89 * "/flash/active/hash" regardless of mechanism. This is done in the open
90 * comamnd, no extra work is required here.
91 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070092std::vector<std::string> FirmwareBlobHandler::getBlobIds()
93{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080094 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070095}
Patrick Venture53977962018-11-02 18:59:35 -070096
Patrick Ventured6461d62018-11-09 17:30:25 -080097/*
98 * Per the design, this mean abort, and this will trigger whatever
99 * appropriate actions are required to abort the process.
100 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700101bool FirmwareBlobHandler::deleteBlob(const std::string& path)
102{
Patrick Venturebcc0c772019-06-17 10:42:06 -0700103 /* This cannot be called if you have an open session to the path.
104 * You can have an open session to verify/update/hash/image, but not active*
105 *
106 * Therefore, if this is called, it's either on a blob that isn't presently
107 * open. However, there could be open blobs, so we need to close all open
108 * sessions. This closing on our is an invalid handler behavior. Therefore,
109 * we cannot close an active session. To enforce this, we only allow
110 * deleting if there isn't a file open.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800111 */
Patrick Venturebcc0c772019-06-17 10:42:06 -0700112 if (fileOpen)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800113 {
114 return false;
115 }
116
Patrick Venture72676762019-06-17 11:22:38 -0700117 /* only includes states where fileOpen == false */
118 switch (state)
119 {
120 case UpdateState::notYetStarted:
121 /* Trying to delete anything at this point has no effect and returns
122 * false.
123 */
124 return false;
125 case UpdateState::verificationPending:
Patrick Venture2ca66522019-06-17 11:58:38 -0700126 abortProcess();
127 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700128 case UpdateState::updatePending:
Patrick Venturec9f62392019-06-17 12:17:26 -0700129 abortProcess();
130 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700131 default:
132 break;
133 }
134
Patrick Venturebcc0c772019-06-17 10:42:06 -0700135 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700136}
Patrick Venture53977962018-11-02 18:59:35 -0700137
Patrick Ventured6461d62018-11-09 17:30:25 -0800138/*
139 * Stat on the files will return information such as what supported
140 * transport mechanisms are available.
141 *
142 * Stat on an active file or hash will return information such as the size
143 * of the data cached, and any additional pertinent information. The
144 * blob_state on the active files will return the state of the update.
145 */
Patrick Venturee312f392019-06-04 07:44:37 -0700146bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700147{
Patrick Venture46637c82018-11-06 15:20:24 -0800148 /* We know we support this path because canHandle is called ahead */
Patrick Ventured342a952019-05-29 09:09:10 -0700149 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700150 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800151 {
Patrick Ventured342a952019-05-29 09:09:10 -0700152 /* These blobs are placeholders that indicate things, or allow actions,
153 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800154 */
Patrick Ventured342a952019-05-29 09:09:10 -0700155 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800156 }
157
Patrick Ventured342a952019-05-29 09:09:10 -0700158 /* They are requesting information about the generic blob_id. */
159 meta->blobState = bitmask;
160 meta->size = 0;
161
162 /* The generic blob_ids state is only the bits related to the transport
163 * mechanisms.
164 */
165 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700166}
Patrick Venture53977962018-11-02 18:59:35 -0700167
Patrick Ventureda66fd82019-06-03 11:11:24 -0700168ActionStatus FirmwareBlobHandler::getActionStatus()
Patrick Venturea2d82392019-06-03 10:55:17 -0700169{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700170 ActionStatus value = ActionStatus::unknown;
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700171 auto* pack = getActionPack();
Patrick Venturea2d82392019-06-03 10:55:17 -0700172
173 switch (state)
174 {
175 case UpdateState::verificationPending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700176 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700177 break;
178 case UpdateState::verificationStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700179 /* If we got here, there must be data AND a hash, not just a hash,
180 * therefore pack will be known. */
181 if (!pack)
182 {
183 break;
184 }
185 value = pack->verification->status();
Patrick Ventureda66fd82019-06-03 11:11:24 -0700186 lastVerificationStatus = value;
Patrick Venturea2d82392019-06-03 10:55:17 -0700187 break;
188 case UpdateState::verificationCompleted:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700189 value = lastVerificationStatus;
Patrick Venturea2d82392019-06-03 10:55:17 -0700190 break;
Patrick Venturea2d82392019-06-03 10:55:17 -0700191 case UpdateState::updatePending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700192 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700193 break;
194 case UpdateState::updateStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700195 if (!pack)
196 {
197 break;
198 }
199 value = pack->update->status();
Patrick Venturea2d82392019-06-03 10:55:17 -0700200 lastUpdateStatus = value;
201 break;
202 case UpdateState::updateCompleted:
203 value = lastUpdateStatus;
204 break;
205 default:
206 break;
207 }
208
209 return value;
210}
211
Patrick Venturec02849b2018-11-06 17:31:15 -0800212/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800213 * Return stat information on an open session. It therefore must be an active
214 * handle to either the active image or active hash.
Patrick Ventured6461d62018-11-09 17:30:25 -0800215 */
Patrick Venturee312f392019-06-04 07:44:37 -0700216bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800217{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800218 auto item = lookup.find(session);
219 if (item == lookup.end())
220 {
221 return false;
222 }
223
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700224 /* The size here refers to the size of the file -- of something analagous.
225 */
226 meta->size = (item->second->imageHandler)
227 ? item->second->imageHandler->getSize()
228 : 0;
229
230 meta->metadata.clear();
231
Patrick Ventureda66fd82019-06-03 11:11:24 -0700232 if (item->second->activePath == verifyBlobId ||
233 item->second->activePath == updateBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700234 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700235 ActionStatus value = getActionStatus();
Patrick Venture699750d2019-05-15 09:24:09 -0700236
Patrick Venturee955e072019-05-15 16:16:46 -0700237 meta->metadata.push_back(static_cast<std::uint8_t>(value));
238
239 /* Change the firmware handler's state and the blob's stat value
240 * depending.
241 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700242 if (value == ActionStatus::success || value == ActionStatus::failed)
Patrick Venturee955e072019-05-15 16:16:46 -0700243 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700244 if (item->second->activePath == verifyBlobId)
Patrick Venturee955e072019-05-15 16:16:46 -0700245 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700246 changeState(UpdateState::verificationCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700247 }
248 else
249 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700250 /* item->second->activePath == updateBlobId */
Patrick Venture75c0c272019-06-21 09:15:08 -0700251 changeState(UpdateState::updateCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700252 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700253
Patrick Venturef1f0f652019-06-03 09:10:19 -0700254 item->second->flags &= ~blobs::StateFlags::committing;
255
Patrick Ventureda66fd82019-06-03 11:11:24 -0700256 if (value == ActionStatus::success)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700257 {
258 item->second->flags |= blobs::StateFlags::committed;
259 }
260 else
261 {
262 item->second->flags |= blobs::StateFlags::commit_error;
263 }
264 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700265 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800266
Patrick Venturee955e072019-05-15 16:16:46 -0700267 /* The blobState here relates to an active sesion, so we should return the
268 * flags used to open this session.
269 */
270 meta->blobState = item->second->flags;
271
Patrick Venturecc7d1602018-11-15 13:58:33 -0800272 /* The metadata blob returned comes from the data handler... it's used for
273 * instance, in P2A bridging to get required information about the mapping,
274 * and is the "opposite" of the lpc writemeta requirement.
275 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800276 if (item->second->dataHandler)
277 {
Patrick Venture74304642019-01-17 09:31:04 -0800278 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800279 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
280 bytes.end());
281 }
282
Patrick Venturecc7d1602018-11-15 13:58:33 -0800283 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800284}
285
286/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800287 * If you open /flash/image or /flash/tarball, or /flash/hash it will
288 * interpret the open flags and perform whatever actions are required for
289 * that update process. The session returned can be used immediately for
290 * sending data down, without requiring one to open the new active file.
291 *
292 * If you open the active flash image or active hash it will let you
293 * overwrite pieces, depending on the state.
294 *
295 * Once the verification process has started the active files cannot be
296 * opened.
297 *
298 * You can only have one open session at a time. Which means, you can only
299 * have one file open at a time. Trying to open the hash blob_id while you
300 * still have the flash image blob_id open will fail. Opening the flash
301 * blob_id when it is already open will fail.
302 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700303bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
304 const std::string& path)
305{
Patrick Venturec02849b2018-11-06 17:31:15 -0800306 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800307 *
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800308 * Further on this, if there's an active session to the hash we don't allow
309 * re-opening the image, and if we have the image open, we don't allow
310 * opening the hash. This design decision may be re-evaluated, and changed
311 * to only allow one session per object type (of the two types). But,
312 * consider if the hash is open, do we want to allow writing to the image?
313 * And why would we? But, really, the point of no-return is once the
314 * verification process has begun -- which is done via commit() on the hash
315 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700316 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800317 if (fileOpen)
318 {
319 return false;
320 }
321
Patrick Venture723113f2019-06-05 09:38:35 -0700322 /* The active blobs are only meant to indicate status that something has
323 * opened the image file or the hash file.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800324 */
Patrick Venture19f5d882019-05-30 14:34:55 -0700325 if (path == activeImageBlobId || path == activeHashBlobId)
326 {
327 /* 2a) are they opening the active image? this can only happen if they
328 * already started one (due to canHandleBlob's behavior).
329 */
330 /* 2b) are they opening the active hash? this can only happen if they
331 * already started one (due to canHandleBlob's behavior).
332 */
333 return false;
334 }
335
Patrick Venture723113f2019-06-05 09:38:35 -0700336 /* Check that they've opened for writing - read back not currently
337 * supported.
338 */
339 if ((flags & blobs::OpenFlags::write) == 0)
340 {
341 return false;
342 }
343
344 /* Because canHandleBlob is called before open, we know that if they try to
345 * open the verifyBlobId, they're in a state where it's present.
346 */
347
348 switch (state)
349 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700350 case UpdateState::notYetStarted:
351 /* Only hashBlobId and firmware BlobIds present. */
352 break;
Patrick Venture723113f2019-06-05 09:38:35 -0700353 case UpdateState::uploadInProgress:
354 /* Unreachable code because if it's started a file is open. */
355 break;
356 case UpdateState::verificationPending:
357 /* Handle opening the verifyBlobId --> we know the image and hash
358 * aren't open because of the fileOpen check. They can still open
359 * other files from this state to transition back into
360 * uploadInProgress.
361 *
362 * The file must be opened for writing, but no transport mechanism
363 * specified since it's irrelevant.
364 */
365 if (path == verifyBlobId)
366 {
367 verifyImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700368
369 lookup[session] = &verifyImage;
370
371 fileOpen = true;
372 return true;
373 }
374 break;
375 case UpdateState::verificationStarted:
376 case UpdateState::verificationCompleted:
377 /* Unreachable code because if it's started a file is open. */
378 return false;
379 case UpdateState::updatePending:
380 {
381 /* When in this state, they can only open the updateBlobId */
382 if (path == updateBlobId)
383 {
384 updateImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700385
386 lookup[session] = &updateImage;
387
388 fileOpen = true;
389 return true;
390 }
391 else
392 {
393 return false;
394 }
395 }
396 case UpdateState::updateStarted:
397 case UpdateState::updateCompleted:
398 /* Unreachable code because if it's started a file is open. */
399 break;
400 default:
401 break;
402 }
403
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700404 /* To support multiple firmware options, we need to make sure they're
405 * opening the one they already opened during this update sequence, or it's
406 * the first time they're opening it.
407 */
408 if (path != hashBlobId)
409 {
410 /* If they're not opening the hashBlobId they must be opening a firmware
411 * handler.
412 */
413 if (openedFirmwareType.empty())
414 {
415 /* First time for this sequence. */
416 openedFirmwareType = path;
417 }
418 else
419 {
420 if (openedFirmwareType != path)
421 {
422 /* Previously, in this sequence they opened /flash/image, and
423 * now they're opening /flash/bios without finishing out
424 * /flash/image (for example).
425 */
426 std::fprintf(stderr, "Trying to open alternate firmware while "
427 "unfinished with other firmware.\n");
428 return false;
429 }
430 }
431 }
432
Patrick Venture723113f2019-06-05 09:38:35 -0700433 /* There are two abstractions at play, how you get the data and how you
434 * handle that data. such that, whether the data comes from the PCI bridge
435 * or LPC bridge is not connected to whether the data goes into a static
436 * layout flash update or a UBI tarball.
437 */
438
Patrick Venturec02849b2018-11-06 17:31:15 -0800439 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800440 * support what they request.
441 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800442 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800443 {
444 return false;
445 }
446
Patrick Venture18235e62018-11-08 10:21:09 -0800447 /* How are they expecting to copy this data? */
448 auto d = std::find_if(
449 transports.begin(), transports.end(),
450 [&flags](const auto& iter) { return (iter.bitmask & flags); });
451 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800452 {
Patrick Venture18235e62018-11-08 10:21:09 -0800453 return false;
454 }
455
456 /* We found the transport handler they requested, no surprise since
457 * above we verify they selected at least one we wanted.
458 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800459
Patrick Venture6e307a72018-11-09 18:21:17 -0800460 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
461 * only non-external data pathway -- but this is just a more generic
462 * approach to that.
463 */
464 if (d->handler)
465 {
466 /* If the data handler open call fails, open fails. */
467 if (!d->handler->open())
468 {
469 return false;
470 }
471 }
472
Patrick Venture70e30bf2019-01-17 10:29:28 -0800473 /* Do we have a file handler for the type of file they're opening.
474 * Note: This should only fail if something is somehow crazy wrong.
475 * Since the canHandle() said yes, and that's tied into the list of explicit
476 * firmware handers (and file handlers, like this'll know where to write the
477 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800478 */
Patrick Venture18235e62018-11-08 10:21:09 -0800479 auto h = std::find_if(
480 handlers.begin(), handlers.end(),
481 [&path](const auto& iter) { return (iter.blobName == path); });
482 if (h == handlers.end())
483 {
484 return false;
485 }
486
487 /* Ok, so we found a handler that matched, so call open() */
488 if (!h->handler->open(path))
489 {
490 return false;
491 }
492
Patrick Venture70e30bf2019-01-17 10:29:28 -0800493 Session* curr;
494 const std::string* active;
495
Patrick Venture7dad86f2019-05-17 08:52:20 -0700496 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800497 {
498 /* 2c) are they opening the /flash/hash ? (to start the process) */
499 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700500 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800501 }
502 else
503 {
504 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700505 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800506 }
507
Patrick Venture18235e62018-11-08 10:21:09 -0800508 curr->flags = flags;
509 curr->dataHandler = d->handler;
510 curr->imageHandler = h->handler;
511
512 lookup[session] = curr;
513
Patrick Ventureefba42d2019-05-24 10:48:16 -0700514 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700515 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800516
Patrick Venture75c0c272019-06-21 09:15:08 -0700517 changeState(UpdateState::uploadInProgress);
Patrick Venture991910a2018-11-09 19:43:48 -0800518 fileOpen = true;
519
Patrick Venture18235e62018-11-08 10:21:09 -0800520 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700521}
Patrick Venture53977962018-11-02 18:59:35 -0700522
Patrick Venture18235e62018-11-08 10:21:09 -0800523/**
524 * The write command really just grabs the data from wherever it is and sends it
525 * to the image handler. It's the image handler's responsibility to deal with
526 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800527 *
528 * This receives a session from the blob manager, therefore it is always called
529 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800530 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700531bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
532 const std::vector<uint8_t>& data)
533{
Patrick Venture18235e62018-11-08 10:21:09 -0800534 auto item = lookup.find(session);
535 if (item == lookup.end())
536 {
537 return false;
538 }
539
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800540 /* Prevent writing during verification. */
541 if (state == UpdateState::verificationStarted)
542 {
543 return false;
544 }
545
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700546 /* Prevent writing to the verification or update blobs. */
547 if (item->second->activePath == verifyBlobId ||
548 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700549 {
550 return false;
551 }
Patrick Venture699750d2019-05-15 09:24:09 -0700552
Patrick Venture18235e62018-11-08 10:21:09 -0800553 std::vector<std::uint8_t> bytes;
554
Patrick Venture84778b82019-06-26 20:11:09 -0700555 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800556 {
557 bytes = data;
558 }
559 else
560 {
561 /* little endian required per design, and so on, but TODO: do endianness
562 * with boost.
563 */
564 struct ExtChunkHdr header;
565
566 if (data.size() != sizeof(header))
567 {
568 return false;
569 }
570
571 std::memcpy(&header, data.data(), data.size());
572 bytes = item->second->dataHandler->copyFrom(header.length);
573 }
574
575 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700576}
Patrick Venture18235e62018-11-08 10:21:09 -0800577
Patrick Venture8c535332018-11-08 15:58:00 -0800578/*
579 * If the active session (image or hash) is over LPC, this allows
580 * configuring it. This option is only available before you start
581 * writing data for the given item (image or hash). This will return
582 * false at any other part. -- the lpc handler portion will know to return
583 * false.
584 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700585bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
586 const std::vector<uint8_t>& data)
587{
Patrick Venture8c535332018-11-08 15:58:00 -0800588 auto item = lookup.find(session);
589 if (item == lookup.end())
590 {
591 return false;
592 }
593
Patrick Venture84778b82019-06-26 20:11:09 -0700594 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800595 {
596 return false;
597 }
598
Patrick Ventured56097a2019-05-15 09:47:55 -0700599 /* Prevent writing meta to the verification blob (it has no data handler).
600 */
601 if (item->second->dataHandler)
602 {
603 return item->second->dataHandler->writeMeta(data);
604 }
Patrick Venture699750d2019-05-15 09:24:09 -0700605
Patrick Ventured56097a2019-05-15 09:47:55 -0700606 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700607}
Patrick Venture8c535332018-11-08 15:58:00 -0800608
Patrick Ventured6461d62018-11-09 17:30:25 -0800609/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700610 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800611 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800612 * the image.
613 *
614 * For this file to have opened, the other two must be closed, which means any
615 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800616 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700617bool FirmwareBlobHandler::commit(uint16_t session,
618 const std::vector<uint8_t>& data)
619{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800620 auto item = lookup.find(session);
621 if (item == lookup.end())
622 {
623 return false;
624 }
625
Patrick Venture1a406fe2019-05-31 07:29:56 -0700626 /* You can only commit on the verifyBlodId or updateBlobId */
627 if (item->second->activePath != verifyBlobId &&
628 item->second->activePath != updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800629 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700630 std::fprintf(stderr, "path: '%s' not expected for commit\n",
631 item->second->activePath.c_str());
Patrick Ventureffcc5502018-11-16 12:32:38 -0800632 return false;
633 }
634
Patrick Venture433cbc32019-05-30 09:53:10 -0700635 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800636 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700637 case UpdateState::verificationPending:
638 /* Set state to committing. */
639 item->second->flags |= blobs::StateFlags::committing;
640 return triggerVerification();
Patrick Venture433cbc32019-05-30 09:53:10 -0700641 case UpdateState::verificationStarted:
642 /* Calling repeatedly has no effect within an update process. */
643 return true;
644 case UpdateState::verificationCompleted:
645 /* Calling after the verification process has completed returns
646 * failure. */
647 return false;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700648 case UpdateState::updatePending:
Patrick Venture433cbc32019-05-30 09:53:10 -0700649 item->second->flags |= blobs::StateFlags::committing;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700650 return triggerUpdate();
Patrick Venture0079d892019-05-31 11:27:44 -0700651 case UpdateState::updateStarted:
652 /* Calling repeatedly has no effect within an update process. */
653 return true;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700654 default:
655 return false;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800656 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700657}
Patrick Ventured6461d62018-11-09 17:30:25 -0800658
659/*
660 * Close must be called on the firmware image before triggering
661 * verification via commit. Once the verification is complete, you can
662 * then close the hash file.
663 *
664 * If the `verify_image.service` returned success, closing the hash file
665 * will have a specific behavior depending on the update. If it's UBI,
666 * it'll perform the install. If it's static layout, it'll do nothing. The
667 * verify_image service in the static layout case is responsible for placing
668 * the file in the correct staging position.
669 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700670bool FirmwareBlobHandler::close(uint16_t session)
671{
Patrick Venture68bb1432018-11-09 20:08:48 -0800672 auto item = lookup.find(session);
673 if (item == lookup.end())
674 {
675 return false;
676 }
677
Patrick Venturee95dbb62019-06-05 09:59:29 -0700678 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800679 {
Patrick Venturee95dbb62019-06-05 09:59:29 -0700680 case UpdateState::uploadInProgress:
681 /* They are closing a data pathway (image, tarball, hash). */
Patrick Venture75c0c272019-06-21 09:15:08 -0700682 changeState(UpdateState::verificationPending);
Patrick Venture85ae86b2019-06-05 09:18:40 -0700683
Patrick Venture1999eef2019-07-01 11:44:09 -0700684 /* Add verify blob ID now that we can expect it, IIF they also wrote
685 * some data.
686 */
687 if (std::count(blobIDs.begin(), blobIDs.end(), activeImageBlobId))
688 {
689 addBlobId(verifyBlobId);
690 }
Patrick Venturee95dbb62019-06-05 09:59:29 -0700691 break;
692 case UpdateState::verificationPending:
693 /* They haven't triggered, therefore closing is uninteresting.
694 */
695 break;
696 case UpdateState::verificationStarted:
Patrick Ventured5741022019-06-17 09:08:35 -0700697 /* Abort without checking to see if it happened to finish. Require
698 * the caller to stat() deliberately.
Patrick Venturee95dbb62019-06-05 09:59:29 -0700699 */
Patrick Ventured5741022019-06-17 09:08:35 -0700700 abortVerification();
701 abortProcess();
702 break;
Patrick Venturee95dbb62019-06-05 09:59:29 -0700703 case UpdateState::verificationCompleted:
704 if (lastVerificationStatus == ActionStatus::success)
705 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700706 changeState(UpdateState::updatePending);
Patrick Venturee95dbb62019-06-05 09:59:29 -0700707 addBlobId(updateBlobId);
708 removeBlobId(verifyBlobId);
709 }
710 else
711 {
Patrick Venture5d9fa022019-06-17 13:13:30 -0700712 /* Verification failed, and the host-tool knows this by calling
713 * stat(), which triggered the state change to
714 * verificationCompleted.
715 *
716 * Therefore, let's abort the process at this point.
717 */
718 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700719 }
720 break;
721 case UpdateState::updatePending:
722 /* They haven't triggered the update, therefore this is
723 * uninteresting. */
724 break;
725 case UpdateState::updateStarted:
Patrick Venture49731712019-06-17 10:04:02 -0700726 /* Abort without checking to see if it happened to finish. Require
727 * the caller to stat() deliberately.
728 */
729 abortUpdate();
730 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700731 break;
732 case UpdateState::updateCompleted:
733 if (lastUpdateStatus == ActionStatus::failed)
734 {
735 /* TODO: lOG something? */
Patrick Venture4c7a4202019-06-17 13:06:55 -0700736 std::fprintf(stderr, "Update failed\n");
Patrick Venturee95dbb62019-06-05 09:59:29 -0700737 }
Patrick Venture930c7b72019-05-24 11:11:08 -0700738
Patrick Venture4c7a4202019-06-17 13:06:55 -0700739 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700740 break;
741 default:
742 break;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800743 }
744
Patrick Venture68bb1432018-11-09 20:08:48 -0800745 if (item->second->dataHandler)
746 {
747 item->second->dataHandler->close();
748 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800749 if (item->second->imageHandler)
750 {
751 item->second->imageHandler->close();
752 }
753
Patrick Venture68bb1432018-11-09 20:08:48 -0800754 lookup.erase(item);
Patrick Venture991910a2018-11-09 19:43:48 -0800755 fileOpen = false;
Patrick Venture68bb1432018-11-09 20:08:48 -0800756 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700757}
Patrick Ventured6461d62018-11-09 17:30:25 -0800758
Patrick Venture75c0c272019-06-21 09:15:08 -0700759void FirmwareBlobHandler::changeState(UpdateState next)
760{
761 state = next;
Patrick Venture6d7735d2019-06-21 10:03:19 -0700762
763 if (state == UpdateState::notYetStarted)
764 {
765 /* Going back to notyetstarted, let them trigger preparation again. */
766 preparationTriggered = false;
767 }
768 else if (state == UpdateState::uploadInProgress)
769 {
770 /* Store this transition logic here instead of ::open() */
771 if (!preparationTriggered)
772 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700773 auto* pack = getActionPack();
774 if (pack)
775 {
776 pack->preparation->trigger();
777 preparationTriggered = true;
778 }
Patrick Venture6d7735d2019-06-21 10:03:19 -0700779 }
780 }
Patrick Venture75c0c272019-06-21 09:15:08 -0700781}
782
Patrick Venturec7ca2912018-11-02 11:38:33 -0700783bool FirmwareBlobHandler::expire(uint16_t session)
784{
785 return false;
786}
Patrick Ventured6461d62018-11-09 17:30:25 -0800787
788/*
789 * Currently, the design does not provide this with a function, however,
790 * it will likely change to support reading data back.
791 */
792std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
793 uint32_t offset,
794 uint32_t requestedSize)
795{
796 return {};
797}
798
Patrick Ventured5741022019-06-17 09:08:35 -0700799void FirmwareBlobHandler::abortProcess()
800{
801 /* Closing of open files is handled from close() -- Reaching here from
802 * delete may never be supported.
803 */
804 removeBlobId(verifyBlobId);
805 removeBlobId(updateBlobId);
806 removeBlobId(activeImageBlobId);
807 removeBlobId(activeHashBlobId);
808
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700809 openedFirmwareType = "";
Patrick Venture75c0c272019-06-21 09:15:08 -0700810 changeState(UpdateState::notYetStarted);
Patrick Ventured5741022019-06-17 09:08:35 -0700811}
812
813void FirmwareBlobHandler::abortVerification()
814{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700815 auto* pack = getActionPack();
816 if (pack)
817 {
818 pack->verification->abort();
819 }
Patrick Ventured5741022019-06-17 09:08:35 -0700820}
821
Patrick Ventureffcc5502018-11-16 12:32:38 -0800822bool FirmwareBlobHandler::triggerVerification()
823{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700824 auto* pack = getActionPack();
825 if (!pack)
826 {
827 return false;
828 }
829
830 bool result = pack->verification->trigger();
Patrick Venture3ecb3502019-05-17 11:03:51 -0700831 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800832 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700833 changeState(UpdateState::verificationStarted);
Patrick Venturecabc1172018-11-16 16:14:26 -0800834 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800835
Patrick Venture3ecb3502019-05-17 11:03:51 -0700836 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800837}
838
Patrick Venture49731712019-06-17 10:04:02 -0700839void FirmwareBlobHandler::abortUpdate()
840{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700841 auto* pack = getActionPack();
842 if (pack)
843 {
844 pack->update->abort();
845 }
Patrick Venture49731712019-06-17 10:04:02 -0700846}
847
Patrick Venture1a406fe2019-05-31 07:29:56 -0700848bool FirmwareBlobHandler::triggerUpdate()
849{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700850 auto* pack = getActionPack();
851 if (!pack)
852 {
853 return false;
854 }
855
856 bool result = pack->update->trigger();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700857 if (result)
858 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700859 changeState(UpdateState::updateStarted);
Patrick Venture1a406fe2019-05-31 07:29:56 -0700860 }
861
862 return result;
863}
864
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700865} // namespace ipmi_flash