blob: 4d06b5837b99bb4a5381ae871920269938aaba7e [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 Venture16265382019-11-06 19:31:36 -080045 /* There must be at least one in addition to the hash blob handler. */
46 if (firmwares.size() < 2)
Patrick Venture52854622018-11-06 12:30:00 -080047 {
Patrick Venture16265382019-11-06 19:31:36 -080048 log<level::ERR>("Must provide at least two firmware handlers.");
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 Venture16265382019-11-06 19:31:36 -080055 if (actionPacks.empty())
56 {
57 return nullptr;
58 }
Patrick Venture52854622018-11-06 12:30:00 -080059
Patrick Venturea78e39f2018-11-06 18:37:06 -080060 std::vector<std::string> blobs;
61 for (const auto& item : firmwares)
62 {
63 blobs.push_back(item.blobName);
64 }
Patrick Venture18235e62018-11-08 10:21:09 -080065
Patrick Venture7dad86f2019-05-17 08:52:20 -070066 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId))
Patrick Venture18235e62018-11-08 10:21:09 -080067 {
68 return nullptr;
69 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080070
Patrick Venture1cde5f92018-11-07 08:26:47 -080071 std::uint16_t bitmask = 0;
72 for (const auto& item : transports)
73 {
74 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
75 bitmask |= item.bitmask;
76 }
77
Patrick Ventured4e20de2019-07-18 12:48:05 -070078 return std::make_unique<FirmwareBlobHandler>(std::move(firmwares), blobs,
79 transports, bitmask,
80 std::move(actionPacks));
Patrick Venture68cf64f2018-11-06 10:46:51 -080081}
82
Patrick Ventured6461d62018-11-09 17:30:25 -080083/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070084bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
85{
Patrick Venture6032dc02019-05-17 11:01:44 -070086 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070087}
Patrick Venture53977962018-11-02 18:59:35 -070088
Patrick Ventured6461d62018-11-09 17:30:25 -080089/*
90 * Grab the list of supported firmware.
91 *
92 * If there's an open firmware session, it'll already be present in the
93 * list as "/flash/active/image", and if the hash has started,
94 * "/flash/active/hash" regardless of mechanism. This is done in the open
95 * comamnd, no extra work is required here.
96 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070097std::vector<std::string> FirmwareBlobHandler::getBlobIds()
98{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080099 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700100}
Patrick Venture53977962018-11-02 18:59:35 -0700101
Patrick Ventured6461d62018-11-09 17:30:25 -0800102/*
103 * Per the design, this mean abort, and this will trigger whatever
104 * appropriate actions are required to abort the process.
105 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700106bool FirmwareBlobHandler::deleteBlob(const std::string& path)
107{
Patrick Venture72676762019-06-17 11:22:38 -0700108 switch (state)
109 {
110 case UpdateState::notYetStarted:
111 /* Trying to delete anything at this point has no effect and returns
112 * false.
113 */
114 return false;
115 case UpdateState::verificationPending:
Patrick Venture2ca66522019-06-17 11:58:38 -0700116 abortProcess();
117 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700118 case UpdateState::updatePending:
Patrick Venturec9f62392019-06-17 12:17:26 -0700119 abortProcess();
120 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700121 default:
122 break;
123 }
124
Patrick Venturebcc0c772019-06-17 10:42:06 -0700125 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700126}
Patrick Venture53977962018-11-02 18:59:35 -0700127
Patrick Ventured6461d62018-11-09 17:30:25 -0800128/*
129 * Stat on the files will return information such as what supported
130 * transport mechanisms are available.
131 *
132 * Stat on an active file or hash will return information such as the size
133 * of the data cached, and any additional pertinent information. The
134 * blob_state on the active files will return the state of the update.
135 */
Patrick Venturee312f392019-06-04 07:44:37 -0700136bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700137{
Patrick Venture46637c82018-11-06 15:20:24 -0800138 /* We know we support this path because canHandle is called ahead */
Patrick Ventured342a952019-05-29 09:09:10 -0700139 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700140 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800141 {
Patrick Ventured342a952019-05-29 09:09:10 -0700142 /* These blobs are placeholders that indicate things, or allow actions,
143 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800144 */
Patrick Ventured342a952019-05-29 09:09:10 -0700145 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800146 }
147
Patrick Ventured342a952019-05-29 09:09:10 -0700148 /* They are requesting information about the generic blob_id. */
149 meta->blobState = bitmask;
150 meta->size = 0;
151
152 /* The generic blob_ids state is only the bits related to the transport
153 * mechanisms.
154 */
155 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700156}
Patrick Venture53977962018-11-02 18:59:35 -0700157
Patrick Ventureda66fd82019-06-03 11:11:24 -0700158ActionStatus FirmwareBlobHandler::getActionStatus()
Patrick Venturea2d82392019-06-03 10:55:17 -0700159{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700160 ActionStatus value = ActionStatus::unknown;
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700161 auto* pack = getActionPack();
Patrick Venturea2d82392019-06-03 10:55:17 -0700162
163 switch (state)
164 {
165 case UpdateState::verificationPending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700166 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700167 break;
168 case UpdateState::verificationStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700169 /* If we got here, there must be data AND a hash, not just a hash,
170 * therefore pack will be known. */
171 if (!pack)
172 {
173 break;
174 }
175 value = pack->verification->status();
Patrick Ventureda66fd82019-06-03 11:11:24 -0700176 lastVerificationStatus = value;
Patrick Venturea2d82392019-06-03 10:55:17 -0700177 break;
178 case UpdateState::verificationCompleted:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700179 value = lastVerificationStatus;
Patrick Venturea2d82392019-06-03 10:55:17 -0700180 break;
Patrick Venturea2d82392019-06-03 10:55:17 -0700181 case UpdateState::updatePending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700182 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700183 break;
184 case UpdateState::updateStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700185 if (!pack)
186 {
187 break;
188 }
189 value = pack->update->status();
Patrick Venturea2d82392019-06-03 10:55:17 -0700190 lastUpdateStatus = value;
191 break;
192 case UpdateState::updateCompleted:
193 value = lastUpdateStatus;
194 break;
195 default:
196 break;
197 }
198
199 return value;
200}
201
Patrick Venturec02849b2018-11-06 17:31:15 -0800202/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800203 * Return stat information on an open session. It therefore must be an active
204 * handle to either the active image or active hash.
Patrick Ventured6461d62018-11-09 17:30:25 -0800205 */
Patrick Venturee312f392019-06-04 07:44:37 -0700206bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800207{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800208 auto item = lookup.find(session);
209 if (item == lookup.end())
210 {
211 return false;
212 }
213
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700214 /* The size here refers to the size of the file -- of something analagous.
215 */
216 meta->size = (item->second->imageHandler)
217 ? item->second->imageHandler->getSize()
218 : 0;
219
220 meta->metadata.clear();
221
Patrick Ventureda66fd82019-06-03 11:11:24 -0700222 if (item->second->activePath == verifyBlobId ||
223 item->second->activePath == updateBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700224 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700225 ActionStatus value = getActionStatus();
Patrick Venture699750d2019-05-15 09:24:09 -0700226
Patrick Venturee955e072019-05-15 16:16:46 -0700227 meta->metadata.push_back(static_cast<std::uint8_t>(value));
228
229 /* Change the firmware handler's state and the blob's stat value
230 * depending.
231 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700232 if (value == ActionStatus::success || value == ActionStatus::failed)
Patrick Venturee955e072019-05-15 16:16:46 -0700233 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700234 if (item->second->activePath == verifyBlobId)
Patrick Venturee955e072019-05-15 16:16:46 -0700235 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700236 changeState(UpdateState::verificationCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700237 }
238 else
239 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700240 /* item->second->activePath == updateBlobId */
Patrick Venture75c0c272019-06-21 09:15:08 -0700241 changeState(UpdateState::updateCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700242 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700243
Patrick Venturef1f0f652019-06-03 09:10:19 -0700244 item->second->flags &= ~blobs::StateFlags::committing;
245
Patrick Ventureda66fd82019-06-03 11:11:24 -0700246 if (value == ActionStatus::success)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700247 {
248 item->second->flags |= blobs::StateFlags::committed;
249 }
250 else
251 {
252 item->second->flags |= blobs::StateFlags::commit_error;
253 }
254 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700255 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800256
Patrick Venturee955e072019-05-15 16:16:46 -0700257 /* The blobState here relates to an active sesion, so we should return the
258 * flags used to open this session.
259 */
260 meta->blobState = item->second->flags;
261
Patrick Venturecc7d1602018-11-15 13:58:33 -0800262 /* The metadata blob returned comes from the data handler... it's used for
263 * instance, in P2A bridging to get required information about the mapping,
264 * and is the "opposite" of the lpc writemeta requirement.
265 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800266 if (item->second->dataHandler)
267 {
Patrick Venture74304642019-01-17 09:31:04 -0800268 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800269 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
270 bytes.end());
271 }
272
Patrick Venturecc7d1602018-11-15 13:58:33 -0800273 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800274}
275
276/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800277 * If you open /flash/image or /flash/tarball, or /flash/hash it will
278 * interpret the open flags and perform whatever actions are required for
279 * that update process. The session returned can be used immediately for
280 * sending data down, without requiring one to open the new active file.
281 *
282 * If you open the active flash image or active hash it will let you
283 * overwrite pieces, depending on the state.
284 *
285 * Once the verification process has started the active files cannot be
286 * opened.
287 *
288 * You can only have one open session at a time. Which means, you can only
289 * have one file open at a time. Trying to open the hash blob_id while you
290 * still have the flash image blob_id open will fail. Opening the flash
291 * blob_id when it is already open will fail.
292 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700293bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
294 const std::string& path)
295{
Patrick Venturec02849b2018-11-06 17:31:15 -0800296 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800297 *
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800298 * Further on this, if there's an active session to the hash we don't allow
299 * re-opening the image, and if we have the image open, we don't allow
300 * opening the hash. This design decision may be re-evaluated, and changed
301 * to only allow one session per object type (of the two types). But,
302 * consider if the hash is open, do we want to allow writing to the image?
303 * And why would we? But, really, the point of no-return is once the
304 * verification process has begun -- which is done via commit() on the hash
305 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700306 */
Brandon Kim8fc60872019-10-18 15:15:50 -0700307 if (fileOpen())
Patrick Venturec02849b2018-11-06 17:31:15 -0800308 {
309 return false;
310 }
311
Patrick Venture723113f2019-06-05 09:38:35 -0700312 /* The active blobs are only meant to indicate status that something has
313 * opened the image file or the hash file.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800314 */
Patrick Venture19f5d882019-05-30 14:34:55 -0700315 if (path == activeImageBlobId || path == activeHashBlobId)
316 {
317 /* 2a) are they opening the active image? this can only happen if they
318 * already started one (due to canHandleBlob's behavior).
319 */
320 /* 2b) are they opening the active hash? this can only happen if they
321 * already started one (due to canHandleBlob's behavior).
322 */
323 return false;
324 }
325
Patrick Venture723113f2019-06-05 09:38:35 -0700326 /* Check that they've opened for writing - read back not currently
327 * supported.
328 */
329 if ((flags & blobs::OpenFlags::write) == 0)
330 {
331 return false;
332 }
333
334 /* Because canHandleBlob is called before open, we know that if they try to
335 * open the verifyBlobId, they're in a state where it's present.
336 */
337
338 switch (state)
339 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700340 case UpdateState::notYetStarted:
341 /* Only hashBlobId and firmware BlobIds present. */
342 break;
Patrick Venture723113f2019-06-05 09:38:35 -0700343 case UpdateState::uploadInProgress:
344 /* Unreachable code because if it's started a file is open. */
345 break;
346 case UpdateState::verificationPending:
347 /* Handle opening the verifyBlobId --> we know the image and hash
Brandon Kim8fc60872019-10-18 15:15:50 -0700348 * aren't open because of the fileOpen() check. They can still open
Patrick Venture723113f2019-06-05 09:38:35 -0700349 * other files from this state to transition back into
350 * uploadInProgress.
351 *
352 * The file must be opened for writing, but no transport mechanism
353 * specified since it's irrelevant.
354 */
355 if (path == verifyBlobId)
356 {
357 verifyImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700358
359 lookup[session] = &verifyImage;
360
Patrick Venture723113f2019-06-05 09:38:35 -0700361 return true;
362 }
363 break;
364 case UpdateState::verificationStarted:
365 case UpdateState::verificationCompleted:
366 /* Unreachable code because if it's started a file is open. */
367 return false;
368 case UpdateState::updatePending:
369 {
370 /* When in this state, they can only open the updateBlobId */
371 if (path == updateBlobId)
372 {
373 updateImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700374
375 lookup[session] = &updateImage;
376
Patrick Venture723113f2019-06-05 09:38:35 -0700377 return true;
378 }
379 else
380 {
381 return false;
382 }
383 }
384 case UpdateState::updateStarted:
385 case UpdateState::updateCompleted:
386 /* Unreachable code because if it's started a file is open. */
387 break;
388 default:
389 break;
390 }
391
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700392 /* To support multiple firmware options, we need to make sure they're
393 * opening the one they already opened during this update sequence, or it's
394 * the first time they're opening it.
395 */
396 if (path != hashBlobId)
397 {
398 /* If they're not opening the hashBlobId they must be opening a firmware
399 * handler.
400 */
401 if (openedFirmwareType.empty())
402 {
403 /* First time for this sequence. */
404 openedFirmwareType = path;
405 }
406 else
407 {
408 if (openedFirmwareType != path)
409 {
410 /* Previously, in this sequence they opened /flash/image, and
411 * now they're opening /flash/bios without finishing out
412 * /flash/image (for example).
413 */
414 std::fprintf(stderr, "Trying to open alternate firmware while "
415 "unfinished with other firmware.\n");
416 return false;
417 }
418 }
419 }
420
Patrick Venture723113f2019-06-05 09:38:35 -0700421 /* There are two abstractions at play, how you get the data and how you
422 * handle that data. such that, whether the data comes from the PCI bridge
423 * or LPC bridge is not connected to whether the data goes into a static
424 * layout flash update or a UBI tarball.
425 */
426
Patrick Venturec02849b2018-11-06 17:31:15 -0800427 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800428 * support what they request.
429 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800430 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800431 {
432 return false;
433 }
434
Patrick Venture18235e62018-11-08 10:21:09 -0800435 /* How are they expecting to copy this data? */
436 auto d = std::find_if(
437 transports.begin(), transports.end(),
438 [&flags](const auto& iter) { return (iter.bitmask & flags); });
439 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800440 {
Patrick Venture18235e62018-11-08 10:21:09 -0800441 return false;
442 }
443
444 /* We found the transport handler they requested, no surprise since
445 * above we verify they selected at least one we wanted.
446 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800447
Patrick Venture6e307a72018-11-09 18:21:17 -0800448 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
449 * only non-external data pathway -- but this is just a more generic
450 * approach to that.
451 */
452 if (d->handler)
453 {
454 /* If the data handler open call fails, open fails. */
455 if (!d->handler->open())
456 {
457 return false;
458 }
459 }
460
Patrick Venture70e30bf2019-01-17 10:29:28 -0800461 /* Do we have a file handler for the type of file they're opening.
462 * Note: This should only fail if something is somehow crazy wrong.
463 * Since the canHandle() said yes, and that's tied into the list of explicit
464 * firmware handers (and file handlers, like this'll know where to write the
465 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800466 */
Patrick Venture18235e62018-11-08 10:21:09 -0800467 auto h = std::find_if(
468 handlers.begin(), handlers.end(),
469 [&path](const auto& iter) { return (iter.blobName == path); });
470 if (h == handlers.end())
471 {
472 return false;
473 }
474
475 /* Ok, so we found a handler that matched, so call open() */
476 if (!h->handler->open(path))
477 {
478 return false;
479 }
480
Patrick Venture70e30bf2019-01-17 10:29:28 -0800481 Session* curr;
482 const std::string* active;
483
Patrick Venture7dad86f2019-05-17 08:52:20 -0700484 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800485 {
486 /* 2c) are they opening the /flash/hash ? (to start the process) */
487 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700488 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800489 }
490 else
491 {
492 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700493 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800494 }
495
Patrick Venture18235e62018-11-08 10:21:09 -0800496 curr->flags = flags;
497 curr->dataHandler = d->handler;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700498 curr->imageHandler = h->handler.get();
Patrick Venture18235e62018-11-08 10:21:09 -0800499
500 lookup[session] = curr;
501
Patrick Ventureefba42d2019-05-24 10:48:16 -0700502 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700503 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800504
Patrick Venture75c0c272019-06-21 09:15:08 -0700505 changeState(UpdateState::uploadInProgress);
Patrick Venture991910a2018-11-09 19:43:48 -0800506
Patrick Venture18235e62018-11-08 10:21:09 -0800507 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700508}
Patrick Venture53977962018-11-02 18:59:35 -0700509
Patrick Venture18235e62018-11-08 10:21:09 -0800510/**
511 * The write command really just grabs the data from wherever it is and sends it
512 * to the image handler. It's the image handler's responsibility to deal with
513 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800514 *
515 * This receives a session from the blob manager, therefore it is always called
516 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800517 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700518bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
519 const std::vector<uint8_t>& data)
520{
Patrick Venture18235e62018-11-08 10:21:09 -0800521 auto item = lookup.find(session);
522 if (item == lookup.end())
523 {
524 return false;
525 }
526
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800527 /* Prevent writing during verification. */
528 if (state == UpdateState::verificationStarted)
529 {
530 return false;
531 }
532
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700533 /* Prevent writing to the verification or update blobs. */
534 if (item->second->activePath == verifyBlobId ||
535 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700536 {
537 return false;
538 }
Patrick Venture699750d2019-05-15 09:24:09 -0700539
Patrick Venture18235e62018-11-08 10:21:09 -0800540 std::vector<std::uint8_t> bytes;
541
Patrick Venture84778b82019-06-26 20:11:09 -0700542 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800543 {
544 bytes = data;
545 }
546 else
547 {
548 /* little endian required per design, and so on, but TODO: do endianness
549 * with boost.
550 */
551 struct ExtChunkHdr header;
552
553 if (data.size() != sizeof(header))
554 {
555 return false;
556 }
557
558 std::memcpy(&header, data.data(), data.size());
559 bytes = item->second->dataHandler->copyFrom(header.length);
560 }
561
562 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700563}
Patrick Venture18235e62018-11-08 10:21:09 -0800564
Patrick Venture8c535332018-11-08 15:58:00 -0800565/*
566 * If the active session (image or hash) is over LPC, this allows
567 * configuring it. This option is only available before you start
568 * writing data for the given item (image or hash). This will return
569 * false at any other part. -- the lpc handler portion will know to return
570 * false.
571 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700572bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
573 const std::vector<uint8_t>& data)
574{
Patrick Venture8c535332018-11-08 15:58:00 -0800575 auto item = lookup.find(session);
576 if (item == lookup.end())
577 {
578 return false;
579 }
580
Patrick Venture84778b82019-06-26 20:11:09 -0700581 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800582 {
583 return false;
584 }
585
Patrick Ventured56097a2019-05-15 09:47:55 -0700586 /* Prevent writing meta to the verification blob (it has no data handler).
587 */
588 if (item->second->dataHandler)
589 {
590 return item->second->dataHandler->writeMeta(data);
591 }
Patrick Venture699750d2019-05-15 09:24:09 -0700592
Patrick Ventured56097a2019-05-15 09:47:55 -0700593 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700594}
Patrick Venture8c535332018-11-08 15:58:00 -0800595
Patrick Ventured6461d62018-11-09 17:30:25 -0800596/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700597 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800598 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800599 * the image.
600 *
601 * For this file to have opened, the other two must be closed, which means any
602 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800603 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700604bool FirmwareBlobHandler::commit(uint16_t session,
605 const std::vector<uint8_t>& data)
606{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800607 auto item = lookup.find(session);
608 if (item == lookup.end())
609 {
610 return false;
611 }
612
Patrick Venture1a406fe2019-05-31 07:29:56 -0700613 /* You can only commit on the verifyBlodId or updateBlobId */
614 if (item->second->activePath != verifyBlobId &&
615 item->second->activePath != updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800616 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700617 std::fprintf(stderr, "path: '%s' not expected for commit\n",
618 item->second->activePath.c_str());
Patrick Ventureffcc5502018-11-16 12:32:38 -0800619 return false;
620 }
621
Patrick Venture433cbc32019-05-30 09:53:10 -0700622 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800623 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700624 case UpdateState::verificationPending:
625 /* Set state to committing. */
626 item->second->flags |= blobs::StateFlags::committing;
627 return triggerVerification();
Patrick Venture433cbc32019-05-30 09:53:10 -0700628 case UpdateState::verificationStarted:
629 /* Calling repeatedly has no effect within an update process. */
630 return true;
631 case UpdateState::verificationCompleted:
632 /* Calling after the verification process has completed returns
633 * failure. */
634 return false;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700635 case UpdateState::updatePending:
Patrick Venture433cbc32019-05-30 09:53:10 -0700636 item->second->flags |= blobs::StateFlags::committing;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700637 return triggerUpdate();
Patrick Venture0079d892019-05-31 11:27:44 -0700638 case UpdateState::updateStarted:
639 /* Calling repeatedly has no effect within an update process. */
640 return true;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700641 default:
642 return false;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800643 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700644}
Patrick Ventured6461d62018-11-09 17:30:25 -0800645
646/*
647 * Close must be called on the firmware image before triggering
648 * verification via commit. Once the verification is complete, you can
649 * then close the hash file.
650 *
651 * If the `verify_image.service` returned success, closing the hash file
652 * will have a specific behavior depending on the update. If it's UBI,
653 * it'll perform the install. If it's static layout, it'll do nothing. The
654 * verify_image service in the static layout case is responsible for placing
655 * the file in the correct staging position.
656 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700657bool FirmwareBlobHandler::close(uint16_t session)
658{
Patrick Venture68bb1432018-11-09 20:08:48 -0800659 auto item = lookup.find(session);
660 if (item == lookup.end())
661 {
662 return false;
663 }
664
Patrick Venturee95dbb62019-06-05 09:59:29 -0700665 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800666 {
Patrick Venturee95dbb62019-06-05 09:59:29 -0700667 case UpdateState::uploadInProgress:
668 /* They are closing a data pathway (image, tarball, hash). */
Patrick Venture75c0c272019-06-21 09:15:08 -0700669 changeState(UpdateState::verificationPending);
Patrick Venture85ae86b2019-06-05 09:18:40 -0700670
Patrick Venture1999eef2019-07-01 11:44:09 -0700671 /* Add verify blob ID now that we can expect it, IIF they also wrote
672 * some data.
673 */
674 if (std::count(blobIDs.begin(), blobIDs.end(), activeImageBlobId))
675 {
676 addBlobId(verifyBlobId);
677 }
Patrick Venturee95dbb62019-06-05 09:59:29 -0700678 break;
679 case UpdateState::verificationPending:
680 /* They haven't triggered, therefore closing is uninteresting.
681 */
682 break;
683 case UpdateState::verificationStarted:
Patrick Ventured5741022019-06-17 09:08:35 -0700684 /* Abort without checking to see if it happened to finish. Require
685 * the caller to stat() deliberately.
Patrick Venturee95dbb62019-06-05 09:59:29 -0700686 */
Patrick Ventured5741022019-06-17 09:08:35 -0700687 abortVerification();
688 abortProcess();
689 break;
Patrick Venturee95dbb62019-06-05 09:59:29 -0700690 case UpdateState::verificationCompleted:
691 if (lastVerificationStatus == ActionStatus::success)
692 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700693 changeState(UpdateState::updatePending);
Patrick Venturee95dbb62019-06-05 09:59:29 -0700694 addBlobId(updateBlobId);
695 removeBlobId(verifyBlobId);
696 }
697 else
698 {
Patrick Venture5d9fa022019-06-17 13:13:30 -0700699 /* Verification failed, and the host-tool knows this by calling
700 * stat(), which triggered the state change to
701 * verificationCompleted.
702 *
703 * Therefore, let's abort the process at this point.
704 */
705 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700706 }
707 break;
708 case UpdateState::updatePending:
709 /* They haven't triggered the update, therefore this is
710 * uninteresting. */
711 break;
712 case UpdateState::updateStarted:
Patrick Venture49731712019-06-17 10:04:02 -0700713 /* Abort without checking to see if it happened to finish. Require
714 * the caller to stat() deliberately.
715 */
716 abortUpdate();
717 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700718 break;
719 case UpdateState::updateCompleted:
720 if (lastUpdateStatus == ActionStatus::failed)
721 {
722 /* TODO: lOG something? */
Patrick Venture4c7a4202019-06-17 13:06:55 -0700723 std::fprintf(stderr, "Update failed\n");
Patrick Venturee95dbb62019-06-05 09:59:29 -0700724 }
Patrick Venture930c7b72019-05-24 11:11:08 -0700725
Patrick Venture4c7a4202019-06-17 13:06:55 -0700726 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700727 break;
728 default:
729 break;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800730 }
731
Brandon Kima9f674c2019-10-18 15:18:49 -0700732 if (!lookup.empty())
Patrick Venture68bb1432018-11-09 20:08:48 -0800733 {
Brandon Kima9f674c2019-10-18 15:18:49 -0700734 if (item->second->dataHandler)
735 {
736 item->second->dataHandler->close();
737 }
738 if (item->second->imageHandler)
739 {
740 item->second->imageHandler->close();
741 }
742 lookup.erase(item);
Patrick Venture68bb1432018-11-09 20:08:48 -0800743 }
Patrick Venture68bb1432018-11-09 20:08:48 -0800744 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700745}
Patrick Ventured6461d62018-11-09 17:30:25 -0800746
Patrick Venture75c0c272019-06-21 09:15:08 -0700747void FirmwareBlobHandler::changeState(UpdateState next)
748{
749 state = next;
Patrick Venture6d7735d2019-06-21 10:03:19 -0700750
751 if (state == UpdateState::notYetStarted)
752 {
753 /* Going back to notyetstarted, let them trigger preparation again. */
754 preparationTriggered = false;
755 }
756 else if (state == UpdateState::uploadInProgress)
757 {
758 /* Store this transition logic here instead of ::open() */
759 if (!preparationTriggered)
760 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700761 auto* pack = getActionPack();
762 if (pack)
763 {
764 pack->preparation->trigger();
765 preparationTriggered = true;
766 }
Patrick Venture6d7735d2019-06-21 10:03:19 -0700767 }
768 }
Patrick Venture75c0c272019-06-21 09:15:08 -0700769}
770
Patrick Venturec7ca2912018-11-02 11:38:33 -0700771bool FirmwareBlobHandler::expire(uint16_t session)
772{
773 return false;
774}
Patrick Ventured6461d62018-11-09 17:30:25 -0800775
776/*
777 * Currently, the design does not provide this with a function, however,
778 * it will likely change to support reading data back.
779 */
780std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
781 uint32_t offset,
782 uint32_t requestedSize)
783{
784 return {};
785}
786
Patrick Ventured5741022019-06-17 09:08:35 -0700787void FirmwareBlobHandler::abortProcess()
788{
789 /* Closing of open files is handled from close() -- Reaching here from
790 * delete may never be supported.
791 */
792 removeBlobId(verifyBlobId);
793 removeBlobId(updateBlobId);
794 removeBlobId(activeImageBlobId);
795 removeBlobId(activeHashBlobId);
796
Brandon Kima9f674c2019-10-18 15:18:49 -0700797 for (auto item : lookup)
798 {
799 if (item.second->dataHandler)
800 {
801 item.second->dataHandler->close();
802 }
803 if (item.second->imageHandler)
804 {
805 item.second->imageHandler->close();
806 }
807 }
808 lookup.clear();
809
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700810 openedFirmwareType = "";
Patrick Venture75c0c272019-06-21 09:15:08 -0700811 changeState(UpdateState::notYetStarted);
Patrick Ventured5741022019-06-17 09:08:35 -0700812}
813
814void FirmwareBlobHandler::abortVerification()
815{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700816 auto* pack = getActionPack();
817 if (pack)
818 {
819 pack->verification->abort();
820 }
Patrick Ventured5741022019-06-17 09:08:35 -0700821}
822
Patrick Ventureffcc5502018-11-16 12:32:38 -0800823bool FirmwareBlobHandler::triggerVerification()
824{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700825 auto* pack = getActionPack();
826 if (!pack)
827 {
828 return false;
829 }
830
831 bool result = pack->verification->trigger();
Patrick Venture3ecb3502019-05-17 11:03:51 -0700832 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800833 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700834 changeState(UpdateState::verificationStarted);
Patrick Venturecabc1172018-11-16 16:14:26 -0800835 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800836
Patrick Venture3ecb3502019-05-17 11:03:51 -0700837 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800838}
839
Patrick Venture49731712019-06-17 10:04:02 -0700840void FirmwareBlobHandler::abortUpdate()
841{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700842 auto* pack = getActionPack();
843 if (pack)
844 {
845 pack->update->abort();
846 }
Patrick Venture49731712019-06-17 10:04:02 -0700847}
848
Patrick Venture1a406fe2019-05-31 07:29:56 -0700849bool FirmwareBlobHandler::triggerUpdate()
850{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700851 auto* pack = getActionPack();
852 if (!pack)
853 {
854 return false;
855 }
856
857 bool result = pack->update->trigger();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700858 if (result)
859 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700860 changeState(UpdateState::updateStarted);
Patrick Venture1a406fe2019-05-31 07:29:56 -0700861 }
862
863 return result;
864}
865
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700866} // namespace ipmi_flash