blob: d58825ec5b2bbbccf5665171ffcea0ad370c2568 [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 Ventured4e20de2019-07-18 12:48:05 -070042 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 Ventured4e20de2019-07-18 12:48:05 -070074 return std::make_unique<FirmwareBlobHandler>(std::move(firmwares), blobs,
75 transports, bitmask,
76 std::move(actionPacks));
Patrick Venture68cf64f2018-11-06 10:46:51 -080077}
78
Patrick Ventured6461d62018-11-09 17:30:25 -080079/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070080bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
81{
Patrick Venture6032dc02019-05-17 11:01:44 -070082 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070083}
Patrick Venture53977962018-11-02 18:59:35 -070084
Patrick Ventured6461d62018-11-09 17:30:25 -080085/*
86 * Grab the list of supported firmware.
87 *
88 * If there's an open firmware session, it'll already be present in the
89 * list as "/flash/active/image", and if the hash has started,
90 * "/flash/active/hash" regardless of mechanism. This is done in the open
91 * comamnd, no extra work is required here.
92 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070093std::vector<std::string> FirmwareBlobHandler::getBlobIds()
94{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080095 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070096}
Patrick Venture53977962018-11-02 18:59:35 -070097
Patrick Ventured6461d62018-11-09 17:30:25 -080098/*
99 * Per the design, this mean abort, and this will trigger whatever
100 * appropriate actions are required to abort the process.
101 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700102bool FirmwareBlobHandler::deleteBlob(const std::string& path)
103{
Patrick Venture72676762019-06-17 11:22:38 -0700104 switch (state)
105 {
106 case UpdateState::notYetStarted:
107 /* Trying to delete anything at this point has no effect and returns
108 * false.
109 */
110 return false;
111 case UpdateState::verificationPending:
Patrick Venture2ca66522019-06-17 11:58:38 -0700112 abortProcess();
113 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700114 case UpdateState::updatePending:
Patrick Venturec9f62392019-06-17 12:17:26 -0700115 abortProcess();
116 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700117 default:
118 break;
119 }
120
Patrick Venturebcc0c772019-06-17 10:42:06 -0700121 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700122}
Patrick Venture53977962018-11-02 18:59:35 -0700123
Patrick Ventured6461d62018-11-09 17:30:25 -0800124/*
125 * Stat on the files will return information such as what supported
126 * transport mechanisms are available.
127 *
128 * Stat on an active file or hash will return information such as the size
129 * of the data cached, and any additional pertinent information. The
130 * blob_state on the active files will return the state of the update.
131 */
Patrick Venturee312f392019-06-04 07:44:37 -0700132bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700133{
Patrick Venture46637c82018-11-06 15:20:24 -0800134 /* We know we support this path because canHandle is called ahead */
Patrick Ventured342a952019-05-29 09:09:10 -0700135 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700136 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800137 {
Patrick Ventured342a952019-05-29 09:09:10 -0700138 /* These blobs are placeholders that indicate things, or allow actions,
139 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800140 */
Patrick Ventured342a952019-05-29 09:09:10 -0700141 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800142 }
143
Patrick Ventured342a952019-05-29 09:09:10 -0700144 /* They are requesting information about the generic blob_id. */
145 meta->blobState = bitmask;
146 meta->size = 0;
147
148 /* The generic blob_ids state is only the bits related to the transport
149 * mechanisms.
150 */
151 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700152}
Patrick Venture53977962018-11-02 18:59:35 -0700153
Patrick Ventureda66fd82019-06-03 11:11:24 -0700154ActionStatus FirmwareBlobHandler::getActionStatus()
Patrick Venturea2d82392019-06-03 10:55:17 -0700155{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700156 ActionStatus value = ActionStatus::unknown;
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700157 auto* pack = getActionPack();
Patrick Venturea2d82392019-06-03 10:55:17 -0700158
159 switch (state)
160 {
161 case UpdateState::verificationPending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700162 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700163 break;
164 case UpdateState::verificationStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700165 /* If we got here, there must be data AND a hash, not just a hash,
166 * therefore pack will be known. */
167 if (!pack)
168 {
169 break;
170 }
171 value = pack->verification->status();
Patrick Ventureda66fd82019-06-03 11:11:24 -0700172 lastVerificationStatus = value;
Patrick Venturea2d82392019-06-03 10:55:17 -0700173 break;
174 case UpdateState::verificationCompleted:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700175 value = lastVerificationStatus;
Patrick Venturea2d82392019-06-03 10:55:17 -0700176 break;
Patrick Venturea2d82392019-06-03 10:55:17 -0700177 case UpdateState::updatePending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700178 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700179 break;
180 case UpdateState::updateStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700181 if (!pack)
182 {
183 break;
184 }
185 value = pack->update->status();
Patrick Venturea2d82392019-06-03 10:55:17 -0700186 lastUpdateStatus = value;
187 break;
188 case UpdateState::updateCompleted:
189 value = lastUpdateStatus;
190 break;
191 default:
192 break;
193 }
194
195 return value;
196}
197
Patrick Venturec02849b2018-11-06 17:31:15 -0800198/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800199 * Return stat information on an open session. It therefore must be an active
200 * handle to either the active image or active hash.
Patrick Ventured6461d62018-11-09 17:30:25 -0800201 */
Patrick Venturee312f392019-06-04 07:44:37 -0700202bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800203{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800204 auto item = lookup.find(session);
205 if (item == lookup.end())
206 {
207 return false;
208 }
209
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700210 /* The size here refers to the size of the file -- of something analagous.
211 */
212 meta->size = (item->second->imageHandler)
213 ? item->second->imageHandler->getSize()
214 : 0;
215
216 meta->metadata.clear();
217
Patrick Ventureda66fd82019-06-03 11:11:24 -0700218 if (item->second->activePath == verifyBlobId ||
219 item->second->activePath == updateBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700220 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700221 ActionStatus value = getActionStatus();
Patrick Venture699750d2019-05-15 09:24:09 -0700222
Patrick Venturee955e072019-05-15 16:16:46 -0700223 meta->metadata.push_back(static_cast<std::uint8_t>(value));
224
225 /* Change the firmware handler's state and the blob's stat value
226 * depending.
227 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700228 if (value == ActionStatus::success || value == ActionStatus::failed)
Patrick Venturee955e072019-05-15 16:16:46 -0700229 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700230 if (item->second->activePath == verifyBlobId)
Patrick Venturee955e072019-05-15 16:16:46 -0700231 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700232 changeState(UpdateState::verificationCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700233 }
234 else
235 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700236 /* item->second->activePath == updateBlobId */
Patrick Venture75c0c272019-06-21 09:15:08 -0700237 changeState(UpdateState::updateCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700238 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700239
Patrick Venturef1f0f652019-06-03 09:10:19 -0700240 item->second->flags &= ~blobs::StateFlags::committing;
241
Patrick Ventureda66fd82019-06-03 11:11:24 -0700242 if (value == ActionStatus::success)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700243 {
244 item->second->flags |= blobs::StateFlags::committed;
245 }
246 else
247 {
248 item->second->flags |= blobs::StateFlags::commit_error;
249 }
250 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700251 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800252
Patrick Venturee955e072019-05-15 16:16:46 -0700253 /* The blobState here relates to an active sesion, so we should return the
254 * flags used to open this session.
255 */
256 meta->blobState = item->second->flags;
257
Patrick Venturecc7d1602018-11-15 13:58:33 -0800258 /* The metadata blob returned comes from the data handler... it's used for
259 * instance, in P2A bridging to get required information about the mapping,
260 * and is the "opposite" of the lpc writemeta requirement.
261 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800262 if (item->second->dataHandler)
263 {
Patrick Venture74304642019-01-17 09:31:04 -0800264 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800265 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
266 bytes.end());
267 }
268
Patrick Venturecc7d1602018-11-15 13:58:33 -0800269 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800270}
271
272/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800273 * If you open /flash/image or /flash/tarball, or /flash/hash it will
274 * interpret the open flags and perform whatever actions are required for
275 * that update process. The session returned can be used immediately for
276 * sending data down, without requiring one to open the new active file.
277 *
278 * If you open the active flash image or active hash it will let you
279 * overwrite pieces, depending on the state.
280 *
281 * Once the verification process has started the active files cannot be
282 * opened.
283 *
284 * You can only have one open session at a time. Which means, you can only
285 * have one file open at a time. Trying to open the hash blob_id while you
286 * still have the flash image blob_id open will fail. Opening the flash
287 * blob_id when it is already open will fail.
288 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700289bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
290 const std::string& path)
291{
Patrick Venturec02849b2018-11-06 17:31:15 -0800292 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800293 *
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800294 * Further on this, if there's an active session to the hash we don't allow
295 * re-opening the image, and if we have the image open, we don't allow
296 * opening the hash. This design decision may be re-evaluated, and changed
297 * to only allow one session per object type (of the two types). But,
298 * consider if the hash is open, do we want to allow writing to the image?
299 * And why would we? But, really, the point of no-return is once the
300 * verification process has begun -- which is done via commit() on the hash
301 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700302 */
Brandon Kim8fc60872019-10-18 15:15:50 -0700303 if (fileOpen())
Patrick Venturec02849b2018-11-06 17:31:15 -0800304 {
305 return false;
306 }
307
Patrick Venture723113f2019-06-05 09:38:35 -0700308 /* The active blobs are only meant to indicate status that something has
309 * opened the image file or the hash file.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800310 */
Patrick Venture19f5d882019-05-30 14:34:55 -0700311 if (path == activeImageBlobId || path == activeHashBlobId)
312 {
313 /* 2a) are they opening the active image? this can only happen if they
314 * already started one (due to canHandleBlob's behavior).
315 */
316 /* 2b) are they opening the active hash? this can only happen if they
317 * already started one (due to canHandleBlob's behavior).
318 */
319 return false;
320 }
321
Patrick Venture723113f2019-06-05 09:38:35 -0700322 /* Check that they've opened for writing - read back not currently
323 * supported.
324 */
325 if ((flags & blobs::OpenFlags::write) == 0)
326 {
327 return false;
328 }
329
330 /* Because canHandleBlob is called before open, we know that if they try to
331 * open the verifyBlobId, they're in a state where it's present.
332 */
333
334 switch (state)
335 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700336 case UpdateState::notYetStarted:
337 /* Only hashBlobId and firmware BlobIds present. */
338 break;
Patrick Venture723113f2019-06-05 09:38:35 -0700339 case UpdateState::uploadInProgress:
340 /* Unreachable code because if it's started a file is open. */
341 break;
342 case UpdateState::verificationPending:
343 /* Handle opening the verifyBlobId --> we know the image and hash
Brandon Kim8fc60872019-10-18 15:15:50 -0700344 * aren't open because of the fileOpen() check. They can still open
Patrick Venture723113f2019-06-05 09:38:35 -0700345 * other files from this state to transition back into
346 * uploadInProgress.
347 *
348 * The file must be opened for writing, but no transport mechanism
349 * specified since it's irrelevant.
350 */
351 if (path == verifyBlobId)
352 {
353 verifyImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700354
355 lookup[session] = &verifyImage;
356
Patrick Venture723113f2019-06-05 09:38:35 -0700357 return true;
358 }
359 break;
360 case UpdateState::verificationStarted:
361 case UpdateState::verificationCompleted:
362 /* Unreachable code because if it's started a file is open. */
363 return false;
364 case UpdateState::updatePending:
365 {
366 /* When in this state, they can only open the updateBlobId */
367 if (path == updateBlobId)
368 {
369 updateImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700370
371 lookup[session] = &updateImage;
372
Patrick Venture723113f2019-06-05 09:38:35 -0700373 return true;
374 }
375 else
376 {
377 return false;
378 }
379 }
380 case UpdateState::updateStarted:
381 case UpdateState::updateCompleted:
382 /* Unreachable code because if it's started a file is open. */
383 break;
384 default:
385 break;
386 }
387
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700388 /* To support multiple firmware options, we need to make sure they're
389 * opening the one they already opened during this update sequence, or it's
390 * the first time they're opening it.
391 */
392 if (path != hashBlobId)
393 {
394 /* If they're not opening the hashBlobId they must be opening a firmware
395 * handler.
396 */
397 if (openedFirmwareType.empty())
398 {
399 /* First time for this sequence. */
400 openedFirmwareType = path;
401 }
402 else
403 {
404 if (openedFirmwareType != path)
405 {
406 /* Previously, in this sequence they opened /flash/image, and
407 * now they're opening /flash/bios without finishing out
408 * /flash/image (for example).
409 */
410 std::fprintf(stderr, "Trying to open alternate firmware while "
411 "unfinished with other firmware.\n");
412 return false;
413 }
414 }
415 }
416
Patrick Venture723113f2019-06-05 09:38:35 -0700417 /* There are two abstractions at play, how you get the data and how you
418 * handle that data. such that, whether the data comes from the PCI bridge
419 * or LPC bridge is not connected to whether the data goes into a static
420 * layout flash update or a UBI tarball.
421 */
422
Patrick Venturec02849b2018-11-06 17:31:15 -0800423 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800424 * support what they request.
425 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800426 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800427 {
428 return false;
429 }
430
Patrick Venture18235e62018-11-08 10:21:09 -0800431 /* How are they expecting to copy this data? */
432 auto d = std::find_if(
433 transports.begin(), transports.end(),
434 [&flags](const auto& iter) { return (iter.bitmask & flags); });
435 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800436 {
Patrick Venture18235e62018-11-08 10:21:09 -0800437 return false;
438 }
439
440 /* We found the transport handler they requested, no surprise since
441 * above we verify they selected at least one we wanted.
442 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800443
Patrick Venture6e307a72018-11-09 18:21:17 -0800444 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
445 * only non-external data pathway -- but this is just a more generic
446 * approach to that.
447 */
448 if (d->handler)
449 {
450 /* If the data handler open call fails, open fails. */
451 if (!d->handler->open())
452 {
453 return false;
454 }
455 }
456
Patrick Venture70e30bf2019-01-17 10:29:28 -0800457 /* Do we have a file handler for the type of file they're opening.
458 * Note: This should only fail if something is somehow crazy wrong.
459 * Since the canHandle() said yes, and that's tied into the list of explicit
460 * firmware handers (and file handlers, like this'll know where to write the
461 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800462 */
Patrick Venture18235e62018-11-08 10:21:09 -0800463 auto h = std::find_if(
464 handlers.begin(), handlers.end(),
465 [&path](const auto& iter) { return (iter.blobName == path); });
466 if (h == handlers.end())
467 {
468 return false;
469 }
470
471 /* Ok, so we found a handler that matched, so call open() */
472 if (!h->handler->open(path))
473 {
474 return false;
475 }
476
Patrick Venture70e30bf2019-01-17 10:29:28 -0800477 Session* curr;
478 const std::string* active;
479
Patrick Venture7dad86f2019-05-17 08:52:20 -0700480 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800481 {
482 /* 2c) are they opening the /flash/hash ? (to start the process) */
483 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700484 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800485 }
486 else
487 {
488 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700489 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800490 }
491
Patrick Venture18235e62018-11-08 10:21:09 -0800492 curr->flags = flags;
493 curr->dataHandler = d->handler;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700494 curr->imageHandler = h->handler.get();
Patrick Venture18235e62018-11-08 10:21:09 -0800495
496 lookup[session] = curr;
497
Patrick Ventureefba42d2019-05-24 10:48:16 -0700498 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700499 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800500
Patrick Venture75c0c272019-06-21 09:15:08 -0700501 changeState(UpdateState::uploadInProgress);
Patrick Venture991910a2018-11-09 19:43:48 -0800502
Patrick Venture18235e62018-11-08 10:21:09 -0800503 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700504}
Patrick Venture53977962018-11-02 18:59:35 -0700505
Patrick Venture18235e62018-11-08 10:21:09 -0800506/**
507 * The write command really just grabs the data from wherever it is and sends it
508 * to the image handler. It's the image handler's responsibility to deal with
509 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800510 *
511 * This receives a session from the blob manager, therefore it is always called
512 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800513 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700514bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
515 const std::vector<uint8_t>& data)
516{
Patrick Venture18235e62018-11-08 10:21:09 -0800517 auto item = lookup.find(session);
518 if (item == lookup.end())
519 {
520 return false;
521 }
522
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800523 /* Prevent writing during verification. */
524 if (state == UpdateState::verificationStarted)
525 {
526 return false;
527 }
528
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700529 /* Prevent writing to the verification or update blobs. */
530 if (item->second->activePath == verifyBlobId ||
531 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700532 {
533 return false;
534 }
Patrick Venture699750d2019-05-15 09:24:09 -0700535
Patrick Venture18235e62018-11-08 10:21:09 -0800536 std::vector<std::uint8_t> bytes;
537
Patrick Venture84778b82019-06-26 20:11:09 -0700538 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800539 {
540 bytes = data;
541 }
542 else
543 {
544 /* little endian required per design, and so on, but TODO: do endianness
545 * with boost.
546 */
547 struct ExtChunkHdr header;
548
549 if (data.size() != sizeof(header))
550 {
551 return false;
552 }
553
554 std::memcpy(&header, data.data(), data.size());
555 bytes = item->second->dataHandler->copyFrom(header.length);
556 }
557
558 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700559}
Patrick Venture18235e62018-11-08 10:21:09 -0800560
Patrick Venture8c535332018-11-08 15:58:00 -0800561/*
562 * If the active session (image or hash) is over LPC, this allows
563 * configuring it. This option is only available before you start
564 * writing data for the given item (image or hash). This will return
565 * false at any other part. -- the lpc handler portion will know to return
566 * false.
567 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700568bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
569 const std::vector<uint8_t>& data)
570{
Patrick Venture8c535332018-11-08 15:58:00 -0800571 auto item = lookup.find(session);
572 if (item == lookup.end())
573 {
574 return false;
575 }
576
Patrick Venture84778b82019-06-26 20:11:09 -0700577 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800578 {
579 return false;
580 }
581
Patrick Ventured56097a2019-05-15 09:47:55 -0700582 /* Prevent writing meta to the verification blob (it has no data handler).
583 */
584 if (item->second->dataHandler)
585 {
586 return item->second->dataHandler->writeMeta(data);
587 }
Patrick Venture699750d2019-05-15 09:24:09 -0700588
Patrick Ventured56097a2019-05-15 09:47:55 -0700589 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700590}
Patrick Venture8c535332018-11-08 15:58:00 -0800591
Patrick Ventured6461d62018-11-09 17:30:25 -0800592/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700593 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800594 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800595 * the image.
596 *
597 * For this file to have opened, the other two must be closed, which means any
598 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800599 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700600bool FirmwareBlobHandler::commit(uint16_t session,
601 const std::vector<uint8_t>& data)
602{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800603 auto item = lookup.find(session);
604 if (item == lookup.end())
605 {
606 return false;
607 }
608
Patrick Venture1a406fe2019-05-31 07:29:56 -0700609 /* You can only commit on the verifyBlodId or updateBlobId */
610 if (item->second->activePath != verifyBlobId &&
611 item->second->activePath != updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800612 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700613 std::fprintf(stderr, "path: '%s' not expected for commit\n",
614 item->second->activePath.c_str());
Patrick Ventureffcc5502018-11-16 12:32:38 -0800615 return false;
616 }
617
Patrick Venture433cbc32019-05-30 09:53:10 -0700618 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800619 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700620 case UpdateState::verificationPending:
621 /* Set state to committing. */
622 item->second->flags |= blobs::StateFlags::committing;
623 return triggerVerification();
Patrick Venture433cbc32019-05-30 09:53:10 -0700624 case UpdateState::verificationStarted:
625 /* Calling repeatedly has no effect within an update process. */
626 return true;
627 case UpdateState::verificationCompleted:
628 /* Calling after the verification process has completed returns
629 * failure. */
630 return false;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700631 case UpdateState::updatePending:
Patrick Venture433cbc32019-05-30 09:53:10 -0700632 item->second->flags |= blobs::StateFlags::committing;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700633 return triggerUpdate();
Patrick Venture0079d892019-05-31 11:27:44 -0700634 case UpdateState::updateStarted:
635 /* Calling repeatedly has no effect within an update process. */
636 return true;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700637 default:
638 return false;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800639 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700640}
Patrick Ventured6461d62018-11-09 17:30:25 -0800641
642/*
643 * Close must be called on the firmware image before triggering
644 * verification via commit. Once the verification is complete, you can
645 * then close the hash file.
646 *
647 * If the `verify_image.service` returned success, closing the hash file
648 * will have a specific behavior depending on the update. If it's UBI,
649 * it'll perform the install. If it's static layout, it'll do nothing. The
650 * verify_image service in the static layout case is responsible for placing
651 * the file in the correct staging position.
652 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700653bool FirmwareBlobHandler::close(uint16_t session)
654{
Patrick Venture68bb1432018-11-09 20:08:48 -0800655 auto item = lookup.find(session);
656 if (item == lookup.end())
657 {
658 return false;
659 }
660
Patrick Venturee95dbb62019-06-05 09:59:29 -0700661 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800662 {
Patrick Venturee95dbb62019-06-05 09:59:29 -0700663 case UpdateState::uploadInProgress:
664 /* They are closing a data pathway (image, tarball, hash). */
Patrick Venture75c0c272019-06-21 09:15:08 -0700665 changeState(UpdateState::verificationPending);
Patrick Venture85ae86b2019-06-05 09:18:40 -0700666
Patrick Venture1999eef2019-07-01 11:44:09 -0700667 /* Add verify blob ID now that we can expect it, IIF they also wrote
668 * some data.
669 */
670 if (std::count(blobIDs.begin(), blobIDs.end(), activeImageBlobId))
671 {
672 addBlobId(verifyBlobId);
673 }
Patrick Venturee95dbb62019-06-05 09:59:29 -0700674 break;
675 case UpdateState::verificationPending:
676 /* They haven't triggered, therefore closing is uninteresting.
677 */
678 break;
679 case UpdateState::verificationStarted:
Patrick Ventured5741022019-06-17 09:08:35 -0700680 /* Abort without checking to see if it happened to finish. Require
681 * the caller to stat() deliberately.
Patrick Venturee95dbb62019-06-05 09:59:29 -0700682 */
Patrick Ventured5741022019-06-17 09:08:35 -0700683 abortVerification();
684 abortProcess();
685 break;
Patrick Venturee95dbb62019-06-05 09:59:29 -0700686 case UpdateState::verificationCompleted:
687 if (lastVerificationStatus == ActionStatus::success)
688 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700689 changeState(UpdateState::updatePending);
Patrick Venturee95dbb62019-06-05 09:59:29 -0700690 addBlobId(updateBlobId);
691 removeBlobId(verifyBlobId);
692 }
693 else
694 {
Patrick Venture5d9fa022019-06-17 13:13:30 -0700695 /* Verification failed, and the host-tool knows this by calling
696 * stat(), which triggered the state change to
697 * verificationCompleted.
698 *
699 * Therefore, let's abort the process at this point.
700 */
701 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700702 }
703 break;
704 case UpdateState::updatePending:
705 /* They haven't triggered the update, therefore this is
706 * uninteresting. */
707 break;
708 case UpdateState::updateStarted:
Patrick Venture49731712019-06-17 10:04:02 -0700709 /* Abort without checking to see if it happened to finish. Require
710 * the caller to stat() deliberately.
711 */
712 abortUpdate();
713 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700714 break;
715 case UpdateState::updateCompleted:
716 if (lastUpdateStatus == ActionStatus::failed)
717 {
718 /* TODO: lOG something? */
Patrick Venture4c7a4202019-06-17 13:06:55 -0700719 std::fprintf(stderr, "Update failed\n");
Patrick Venturee95dbb62019-06-05 09:59:29 -0700720 }
Patrick Venture930c7b72019-05-24 11:11:08 -0700721
Patrick Venture4c7a4202019-06-17 13:06:55 -0700722 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700723 break;
724 default:
725 break;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800726 }
727
Brandon Kima9f674c2019-10-18 15:18:49 -0700728 if (!lookup.empty())
Patrick Venture68bb1432018-11-09 20:08:48 -0800729 {
Brandon Kima9f674c2019-10-18 15:18:49 -0700730 if (item->second->dataHandler)
731 {
732 item->second->dataHandler->close();
733 }
734 if (item->second->imageHandler)
735 {
736 item->second->imageHandler->close();
737 }
738 lookup.erase(item);
Patrick Venture68bb1432018-11-09 20:08:48 -0800739 }
Patrick Venture68bb1432018-11-09 20:08:48 -0800740 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700741}
Patrick Ventured6461d62018-11-09 17:30:25 -0800742
Patrick Venture75c0c272019-06-21 09:15:08 -0700743void FirmwareBlobHandler::changeState(UpdateState next)
744{
745 state = next;
Patrick Venture6d7735d2019-06-21 10:03:19 -0700746
747 if (state == UpdateState::notYetStarted)
748 {
749 /* Going back to notyetstarted, let them trigger preparation again. */
750 preparationTriggered = false;
751 }
752 else if (state == UpdateState::uploadInProgress)
753 {
754 /* Store this transition logic here instead of ::open() */
755 if (!preparationTriggered)
756 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700757 auto* pack = getActionPack();
758 if (pack)
759 {
760 pack->preparation->trigger();
761 preparationTriggered = true;
762 }
Patrick Venture6d7735d2019-06-21 10:03:19 -0700763 }
764 }
Patrick Venture75c0c272019-06-21 09:15:08 -0700765}
766
Patrick Venturec7ca2912018-11-02 11:38:33 -0700767bool FirmwareBlobHandler::expire(uint16_t session)
768{
769 return false;
770}
Patrick Ventured6461d62018-11-09 17:30:25 -0800771
772/*
773 * Currently, the design does not provide this with a function, however,
774 * it will likely change to support reading data back.
775 */
776std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
777 uint32_t offset,
778 uint32_t requestedSize)
779{
780 return {};
781}
782
Patrick Ventured5741022019-06-17 09:08:35 -0700783void FirmwareBlobHandler::abortProcess()
784{
785 /* Closing of open files is handled from close() -- Reaching here from
786 * delete may never be supported.
787 */
788 removeBlobId(verifyBlobId);
789 removeBlobId(updateBlobId);
790 removeBlobId(activeImageBlobId);
791 removeBlobId(activeHashBlobId);
792
Brandon Kima9f674c2019-10-18 15:18:49 -0700793 for (auto item : lookup)
794 {
795 if (item.second->dataHandler)
796 {
797 item.second->dataHandler->close();
798 }
799 if (item.second->imageHandler)
800 {
801 item.second->imageHandler->close();
802 }
803 }
804 lookup.clear();
805
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700806 openedFirmwareType = "";
Patrick Venture75c0c272019-06-21 09:15:08 -0700807 changeState(UpdateState::notYetStarted);
Patrick Ventured5741022019-06-17 09:08:35 -0700808}
809
810void FirmwareBlobHandler::abortVerification()
811{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700812 auto* pack = getActionPack();
813 if (pack)
814 {
815 pack->verification->abort();
816 }
Patrick Ventured5741022019-06-17 09:08:35 -0700817}
818
Patrick Ventureffcc5502018-11-16 12:32:38 -0800819bool FirmwareBlobHandler::triggerVerification()
820{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700821 auto* pack = getActionPack();
822 if (!pack)
823 {
824 return false;
825 }
826
827 bool result = pack->verification->trigger();
Patrick Venture3ecb3502019-05-17 11:03:51 -0700828 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800829 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700830 changeState(UpdateState::verificationStarted);
Patrick Venturecabc1172018-11-16 16:14:26 -0800831 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800832
Patrick Venture3ecb3502019-05-17 11:03:51 -0700833 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800834}
835
Patrick Venture49731712019-06-17 10:04:02 -0700836void FirmwareBlobHandler::abortUpdate()
837{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700838 auto* pack = getActionPack();
839 if (pack)
840 {
841 pack->update->abort();
842 }
Patrick Venture49731712019-06-17 10:04:02 -0700843}
844
Patrick Venture1a406fe2019-05-31 07:29:56 -0700845bool FirmwareBlobHandler::triggerUpdate()
846{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700847 auto* pack = getActionPack();
848 if (!pack)
849 {
850 return false;
851 }
852
853 bool result = pack->update->trigger();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700854 if (result)
855 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700856 changeState(UpdateState::updateStarted);
Patrick Venture1a406fe2019-05-31 07:29:56 -0700857 }
858
859 return result;
860}
861
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700862} // namespace ipmi_flash