blob: aaabe83e9c7a2a2f324e226314ac016b9bd94dd0 [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 Venturea78e39f2018-11-06 18:37:06 -080019#include "image_handler.hpp"
Patrick Venture1d66fe62019-06-03 14:57:27 -070020#include "status.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -070021#include "util.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -080022
Patrick Venture137ad2c2018-11-06 11:30:43 -080023#include <algorithm>
Patrick Venture9efef5d2019-06-19 08:45:44 -070024#include <blobs-ipmid/blobs.hpp>
Patrick Venture192d60f2018-11-06 11:11:59 -080025#include <cstdint>
Patrick Venture18235e62018-11-08 10:21:09 -080026#include <cstring>
Patrick Ventureb3b4db72019-05-15 11:30:24 -070027#include <fstream>
Patrick Venture68cf64f2018-11-06 10:46:51 -080028#include <memory>
Patrick Ventured333a872018-12-03 16:24:26 -080029#include <phosphor-logging/log.hpp>
Patrick Venturefa6c4d92018-11-02 18:34:53 -070030#include <string>
31#include <vector>
32
Patrick Ventured333a872018-12-03 16:24:26 -080033using namespace phosphor::logging;
34
Patrick Venture1d5a31c2019-05-20 11:38:22 -070035namespace ipmi_flash
Patrick Venturec7ca2912018-11-02 11:38:33 -070036{
Patrick Ventureb3b4db72019-05-15 11:30:24 -070037
Patrick Venture1d5a31c2019-05-20 11:38:22 -070038std::unique_ptr<blobs::GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080039 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070040 const std::vector<HandlerPack>& firmwares,
Patrick Venture74059d62019-05-17 10:40:26 -070041 const std::vector<DataHandlerPack>& transports,
Patrick Venture6d7735d2019-06-21 10:03:19 -070042 std::unique_ptr<TriggerableActionInterface> preparation,
Patrick Venture1d66fe62019-06-03 14:57:27 -070043 std::unique_ptr<TriggerableActionInterface> verification,
44 std::unique_ptr<TriggerableActionInterface> update)
Patrick Venture68cf64f2018-11-06 10:46:51 -080045{
Patrick Venture52854622018-11-06 12:30:00 -080046 /* There must be at least one. */
47 if (!firmwares.size())
48 {
Patrick Ventured333a872018-12-03 16:24:26 -080049 log<level::ERR>("Must provide at least one firmware handler.");
Patrick Venture52854622018-11-06 12:30:00 -080050 return nullptr;
51 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080052 if (!transports.size())
53 {
54 return nullptr;
55 }
Patrick Venture52854622018-11-06 12:30:00 -080056
Patrick Venturea78e39f2018-11-06 18:37:06 -080057 std::vector<std::string> blobs;
58 for (const auto& item : firmwares)
59 {
60 blobs.push_back(item.blobName);
61 }
Patrick Venture18235e62018-11-08 10:21:09 -080062
Patrick Venture7dad86f2019-05-17 08:52:20 -070063 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId))
Patrick Venture18235e62018-11-08 10:21:09 -080064 {
65 return nullptr;
66 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080067
Patrick Venture1cde5f92018-11-07 08:26:47 -080068 std::uint16_t bitmask = 0;
69 for (const auto& item : transports)
70 {
71 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
72 bitmask |= item.bitmask;
73 }
74
Patrick Venture3ecb3502019-05-17 11:03:51 -070075 return std::make_unique<FirmwareBlobHandler>(
Patrick Venture6d7735d2019-06-21 10:03:19 -070076 firmwares, blobs, transports, bitmask, std::move(preparation),
77 std::move(verification), std::move(update));
Patrick Venture68cf64f2018-11-06 10:46:51 -080078}
79
Patrick Ventured6461d62018-11-09 17:30:25 -080080/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070081bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
82{
Patrick Venture6032dc02019-05-17 11:01:44 -070083 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070084}
Patrick Venture53977962018-11-02 18:59:35 -070085
Patrick Ventured6461d62018-11-09 17:30:25 -080086/*
87 * Grab the list of supported firmware.
88 *
89 * If there's an open firmware session, it'll already be present in the
90 * list as "/flash/active/image", and if the hash has started,
91 * "/flash/active/hash" regardless of mechanism. This is done in the open
92 * comamnd, no extra work is required here.
93 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070094std::vector<std::string> FirmwareBlobHandler::getBlobIds()
95{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080096 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070097}
Patrick Venture53977962018-11-02 18:59:35 -070098
Patrick Ventured6461d62018-11-09 17:30:25 -080099/*
100 * Per the design, this mean abort, and this will trigger whatever
101 * appropriate actions are required to abort the process.
102 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700103bool FirmwareBlobHandler::deleteBlob(const std::string& path)
104{
Patrick Venturebcc0c772019-06-17 10:42:06 -0700105 /* This cannot be called if you have an open session to the path.
106 * You can have an open session to verify/update/hash/image, but not active*
107 *
108 * Therefore, if this is called, it's either on a blob that isn't presently
109 * open. However, there could be open blobs, so we need to close all open
110 * sessions. This closing on our is an invalid handler behavior. Therefore,
111 * we cannot close an active session. To enforce this, we only allow
112 * deleting if there isn't a file open.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800113 */
Patrick Venturebcc0c772019-06-17 10:42:06 -0700114 if (fileOpen)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800115 {
116 return false;
117 }
118
Patrick Venture72676762019-06-17 11:22:38 -0700119 /* only includes states where fileOpen == false */
120 switch (state)
121 {
122 case UpdateState::notYetStarted:
123 /* Trying to delete anything at this point has no effect and returns
124 * false.
125 */
126 return false;
127 case UpdateState::verificationPending:
Patrick Venture2ca66522019-06-17 11:58:38 -0700128 abortProcess();
129 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700130 case UpdateState::updatePending:
Patrick Venturec9f62392019-06-17 12:17:26 -0700131 abortProcess();
132 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700133 default:
134 break;
135 }
136
Patrick Venturebcc0c772019-06-17 10:42:06 -0700137 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700138}
Patrick Venture53977962018-11-02 18:59:35 -0700139
Patrick Ventured6461d62018-11-09 17:30:25 -0800140/*
141 * Stat on the files will return information such as what supported
142 * transport mechanisms are available.
143 *
144 * Stat on an active file or hash will return information such as the size
145 * of the data cached, and any additional pertinent information. The
146 * blob_state on the active files will return the state of the update.
147 */
Patrick Venturee312f392019-06-04 07:44:37 -0700148bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700149{
Patrick Venture46637c82018-11-06 15:20:24 -0800150 /* We know we support this path because canHandle is called ahead */
Patrick Ventured342a952019-05-29 09:09:10 -0700151 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700152 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800153 {
Patrick Ventured342a952019-05-29 09:09:10 -0700154 /* These blobs are placeholders that indicate things, or allow actions,
155 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800156 */
Patrick Ventured342a952019-05-29 09:09:10 -0700157 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800158 }
159
Patrick Ventured342a952019-05-29 09:09:10 -0700160 /* They are requesting information about the generic blob_id. */
161 meta->blobState = bitmask;
162 meta->size = 0;
163
164 /* The generic blob_ids state is only the bits related to the transport
165 * mechanisms.
166 */
167 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700168}
Patrick Venture53977962018-11-02 18:59:35 -0700169
Patrick Ventureda66fd82019-06-03 11:11:24 -0700170ActionStatus FirmwareBlobHandler::getActionStatus()
Patrick Venturea2d82392019-06-03 10:55:17 -0700171{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700172 ActionStatus value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700173
174 switch (state)
175 {
176 case UpdateState::verificationPending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700177 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700178 break;
179 case UpdateState::verificationStarted:
180 value = verification->status();
Patrick Ventureda66fd82019-06-03 11:11:24 -0700181 lastVerificationStatus = value;
Patrick Venturea2d82392019-06-03 10:55:17 -0700182 break;
183 case UpdateState::verificationCompleted:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700184 value = lastVerificationStatus;
Patrick Venturea2d82392019-06-03 10:55:17 -0700185 break;
Patrick Venturea2d82392019-06-03 10:55:17 -0700186 case UpdateState::updatePending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700187 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700188 break;
189 case UpdateState::updateStarted:
190 value = update->status();
191 lastUpdateStatus = value;
192 break;
193 case UpdateState::updateCompleted:
194 value = lastUpdateStatus;
195 break;
196 default:
197 break;
198 }
199
200 return value;
201}
202
Patrick Venturec02849b2018-11-06 17:31:15 -0800203/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800204 * Return stat information on an open session. It therefore must be an active
205 * handle to either the active image or active hash.
Patrick Ventured6461d62018-11-09 17:30:25 -0800206 */
Patrick Venturee312f392019-06-04 07:44:37 -0700207bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800208{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800209 auto item = lookup.find(session);
210 if (item == lookup.end())
211 {
212 return false;
213 }
214
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700215 /* The size here refers to the size of the file -- of something analagous.
216 */
217 meta->size = (item->second->imageHandler)
218 ? item->second->imageHandler->getSize()
219 : 0;
220
221 meta->metadata.clear();
222
Patrick Ventureda66fd82019-06-03 11:11:24 -0700223 if (item->second->activePath == verifyBlobId ||
224 item->second->activePath == updateBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700225 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700226 ActionStatus value = getActionStatus();
Patrick Venture699750d2019-05-15 09:24:09 -0700227
Patrick Venturee955e072019-05-15 16:16:46 -0700228 meta->metadata.push_back(static_cast<std::uint8_t>(value));
229
230 /* Change the firmware handler's state and the blob's stat value
231 * depending.
232 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700233 if (value == ActionStatus::success || value == ActionStatus::failed)
Patrick Venturee955e072019-05-15 16:16:46 -0700234 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700235 if (item->second->activePath == verifyBlobId)
Patrick Venturee955e072019-05-15 16:16:46 -0700236 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700237 changeState(UpdateState::verificationCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700238 }
239 else
240 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700241 /* item->second->activePath == updateBlobId */
Patrick Venture75c0c272019-06-21 09:15:08 -0700242 changeState(UpdateState::updateCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700243 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700244
Patrick Venturef1f0f652019-06-03 09:10:19 -0700245 item->second->flags &= ~blobs::StateFlags::committing;
246
Patrick Ventureda66fd82019-06-03 11:11:24 -0700247 if (value == ActionStatus::success)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700248 {
249 item->second->flags |= blobs::StateFlags::committed;
250 }
251 else
252 {
253 item->second->flags |= blobs::StateFlags::commit_error;
254 }
255 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700256 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800257
Patrick Venturee955e072019-05-15 16:16:46 -0700258 /* The blobState here relates to an active sesion, so we should return the
259 * flags used to open this session.
260 */
261 meta->blobState = item->second->flags;
262
Patrick Venturecc7d1602018-11-15 13:58:33 -0800263 /* The metadata blob returned comes from the data handler... it's used for
264 * instance, in P2A bridging to get required information about the mapping,
265 * and is the "opposite" of the lpc writemeta requirement.
266 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800267 if (item->second->dataHandler)
268 {
Patrick Venture74304642019-01-17 09:31:04 -0800269 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800270 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
271 bytes.end());
272 }
273
Patrick Venturecc7d1602018-11-15 13:58:33 -0800274 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800275}
276
277/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800278 * If you open /flash/image or /flash/tarball, or /flash/hash it will
279 * interpret the open flags and perform whatever actions are required for
280 * that update process. The session returned can be used immediately for
281 * sending data down, without requiring one to open the new active file.
282 *
283 * If you open the active flash image or active hash it will let you
284 * overwrite pieces, depending on the state.
285 *
286 * Once the verification process has started the active files cannot be
287 * opened.
288 *
289 * You can only have one open session at a time. Which means, you can only
290 * have one file open at a time. Trying to open the hash blob_id while you
291 * still have the flash image blob_id open will fail. Opening the flash
292 * blob_id when it is already open will fail.
293 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700294bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
295 const std::string& path)
296{
Patrick Venturec02849b2018-11-06 17:31:15 -0800297 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800298 *
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800299 * Further on this, if there's an active session to the hash we don't allow
300 * re-opening the image, and if we have the image open, we don't allow
301 * opening the hash. This design decision may be re-evaluated, and changed
302 * to only allow one session per object type (of the two types). But,
303 * consider if the hash is open, do we want to allow writing to the image?
304 * And why would we? But, really, the point of no-return is once the
305 * verification process has begun -- which is done via commit() on the hash
306 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700307 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800308 if (fileOpen)
309 {
310 return false;
311 }
312
Patrick Venture723113f2019-06-05 09:38:35 -0700313 /* The active blobs are only meant to indicate status that something has
314 * opened the image file or the hash file.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800315 */
Patrick Venture19f5d882019-05-30 14:34:55 -0700316 if (path == activeImageBlobId || path == activeHashBlobId)
317 {
318 /* 2a) are they opening the active image? this can only happen if they
319 * already started one (due to canHandleBlob's behavior).
320 */
321 /* 2b) are they opening the active hash? this can only happen if they
322 * already started one (due to canHandleBlob's behavior).
323 */
324 return false;
325 }
326
Patrick Venture723113f2019-06-05 09:38:35 -0700327 /* Check that they've opened for writing - read back not currently
328 * supported.
329 */
330 if ((flags & blobs::OpenFlags::write) == 0)
331 {
332 return false;
333 }
334
335 /* Because canHandleBlob is called before open, we know that if they try to
336 * open the verifyBlobId, they're in a state where it's present.
337 */
338
339 switch (state)
340 {
341 case UpdateState::uploadInProgress:
342 /* Unreachable code because if it's started a file is open. */
343 break;
344 case UpdateState::verificationPending:
345 /* Handle opening the verifyBlobId --> we know the image and hash
346 * aren't open because of the fileOpen check. They can still open
347 * other files from this state to transition back into
348 * uploadInProgress.
349 *
350 * The file must be opened for writing, but no transport mechanism
351 * specified since it's irrelevant.
352 */
353 if (path == verifyBlobId)
354 {
355 verifyImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700356
357 lookup[session] = &verifyImage;
358
359 fileOpen = true;
360 return true;
361 }
362 break;
363 case UpdateState::verificationStarted:
364 case UpdateState::verificationCompleted:
365 /* Unreachable code because if it's started a file is open. */
366 return false;
367 case UpdateState::updatePending:
368 {
369 /* When in this state, they can only open the updateBlobId */
370 if (path == updateBlobId)
371 {
372 updateImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700373
374 lookup[session] = &updateImage;
375
376 fileOpen = true;
377 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
392 /* There are two abstractions at play, how you get the data and how you
393 * handle that data. such that, whether the data comes from the PCI bridge
394 * or LPC bridge is not connected to whether the data goes into a static
395 * layout flash update or a UBI tarball.
396 */
397
Patrick Venturec02849b2018-11-06 17:31:15 -0800398 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800399 * support what they request.
400 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800401 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800402 {
403 return false;
404 }
405
Patrick Venture18235e62018-11-08 10:21:09 -0800406 /* How are they expecting to copy this data? */
407 auto d = std::find_if(
408 transports.begin(), transports.end(),
409 [&flags](const auto& iter) { return (iter.bitmask & flags); });
410 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800411 {
Patrick Venture18235e62018-11-08 10:21:09 -0800412 return false;
413 }
414
415 /* We found the transport handler they requested, no surprise since
416 * above we verify they selected at least one we wanted.
417 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800418
Patrick Venture6e307a72018-11-09 18:21:17 -0800419 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
420 * only non-external data pathway -- but this is just a more generic
421 * approach to that.
422 */
423 if (d->handler)
424 {
425 /* If the data handler open call fails, open fails. */
426 if (!d->handler->open())
427 {
428 return false;
429 }
430 }
431
Patrick Venture70e30bf2019-01-17 10:29:28 -0800432 /* Do we have a file handler for the type of file they're opening.
433 * Note: This should only fail if something is somehow crazy wrong.
434 * Since the canHandle() said yes, and that's tied into the list of explicit
435 * firmware handers (and file handlers, like this'll know where to write the
436 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800437 */
Patrick Venture18235e62018-11-08 10:21:09 -0800438 auto h = std::find_if(
439 handlers.begin(), handlers.end(),
440 [&path](const auto& iter) { return (iter.blobName == path); });
441 if (h == handlers.end())
442 {
443 return false;
444 }
445
446 /* Ok, so we found a handler that matched, so call open() */
447 if (!h->handler->open(path))
448 {
449 return false;
450 }
451
Patrick Venture70e30bf2019-01-17 10:29:28 -0800452 Session* curr;
453 const std::string* active;
454
Patrick Venture7dad86f2019-05-17 08:52:20 -0700455 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800456 {
457 /* 2c) are they opening the /flash/hash ? (to start the process) */
458 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700459 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800460 }
461 else
462 {
463 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700464 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800465 }
466
Patrick Venture18235e62018-11-08 10:21:09 -0800467 curr->flags = flags;
468 curr->dataHandler = d->handler;
469 curr->imageHandler = h->handler;
470
471 lookup[session] = curr;
472
Patrick Ventureefba42d2019-05-24 10:48:16 -0700473 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700474 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800475
Patrick Venture75c0c272019-06-21 09:15:08 -0700476 changeState(UpdateState::uploadInProgress);
Patrick Venture991910a2018-11-09 19:43:48 -0800477 fileOpen = true;
478
Patrick Venture18235e62018-11-08 10:21:09 -0800479 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700480}
Patrick Venture53977962018-11-02 18:59:35 -0700481
Patrick Venture18235e62018-11-08 10:21:09 -0800482/**
483 * The write command really just grabs the data from wherever it is and sends it
484 * to the image handler. It's the image handler's responsibility to deal with
485 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800486 *
487 * This receives a session from the blob manager, therefore it is always called
488 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800489 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700490bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
491 const std::vector<uint8_t>& data)
492{
Patrick Venture18235e62018-11-08 10:21:09 -0800493 auto item = lookup.find(session);
494 if (item == lookup.end())
495 {
496 return false;
497 }
498
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800499 /* Prevent writing during verification. */
500 if (state == UpdateState::verificationStarted)
501 {
502 return false;
503 }
504
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700505 /* Prevent writing to the verification or update blobs. */
506 if (item->second->activePath == verifyBlobId ||
507 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700508 {
509 return false;
510 }
Patrick Venture699750d2019-05-15 09:24:09 -0700511
Patrick Venture18235e62018-11-08 10:21:09 -0800512 std::vector<std::uint8_t> bytes;
513
Patrick Venture05abf7e2018-11-09 11:02:56 -0800514 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800515 {
516 bytes = data;
517 }
518 else
519 {
520 /* little endian required per design, and so on, but TODO: do endianness
521 * with boost.
522 */
523 struct ExtChunkHdr header;
524
525 if (data.size() != sizeof(header))
526 {
527 return false;
528 }
529
530 std::memcpy(&header, data.data(), data.size());
531 bytes = item->second->dataHandler->copyFrom(header.length);
532 }
533
534 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700535}
Patrick Venture18235e62018-11-08 10:21:09 -0800536
Patrick Venture8c535332018-11-08 15:58:00 -0800537/*
538 * If the active session (image or hash) is over LPC, this allows
539 * configuring it. This option is only available before you start
540 * writing data for the given item (image or hash). This will return
541 * false at any other part. -- the lpc handler portion will know to return
542 * false.
543 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700544bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
545 const std::vector<uint8_t>& data)
546{
Patrick Venture8c535332018-11-08 15:58:00 -0800547 auto item = lookup.find(session);
548 if (item == lookup.end())
549 {
550 return false;
551 }
552
Patrick Venture05abf7e2018-11-09 11:02:56 -0800553 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800554 {
555 return false;
556 }
557
Patrick Ventured56097a2019-05-15 09:47:55 -0700558 /* Prevent writing meta to the verification blob (it has no data handler).
559 */
560 if (item->second->dataHandler)
561 {
562 return item->second->dataHandler->writeMeta(data);
563 }
Patrick Venture699750d2019-05-15 09:24:09 -0700564
Patrick Ventured56097a2019-05-15 09:47:55 -0700565 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700566}
Patrick Venture8c535332018-11-08 15:58:00 -0800567
Patrick Ventured6461d62018-11-09 17:30:25 -0800568/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700569 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800570 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800571 * the image.
572 *
573 * For this file to have opened, the other two must be closed, which means any
574 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800575 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700576bool FirmwareBlobHandler::commit(uint16_t session,
577 const std::vector<uint8_t>& data)
578{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800579 auto item = lookup.find(session);
580 if (item == lookup.end())
581 {
582 return false;
583 }
584
Patrick Venture1a406fe2019-05-31 07:29:56 -0700585 /* You can only commit on the verifyBlodId or updateBlobId */
586 if (item->second->activePath != verifyBlobId &&
587 item->second->activePath != updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800588 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700589 std::fprintf(stderr, "path: '%s' not expected for commit\n",
590 item->second->activePath.c_str());
Patrick Ventureffcc5502018-11-16 12:32:38 -0800591 return false;
592 }
593
Patrick Venture433cbc32019-05-30 09:53:10 -0700594 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800595 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700596 case UpdateState::verificationPending:
597 /* Set state to committing. */
598 item->second->flags |= blobs::StateFlags::committing;
599 return triggerVerification();
Patrick Venture433cbc32019-05-30 09:53:10 -0700600 case UpdateState::verificationStarted:
601 /* Calling repeatedly has no effect within an update process. */
602 return true;
603 case UpdateState::verificationCompleted:
604 /* Calling after the verification process has completed returns
605 * failure. */
606 return false;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700607 case UpdateState::updatePending:
Patrick Venture433cbc32019-05-30 09:53:10 -0700608 item->second->flags |= blobs::StateFlags::committing;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700609 return triggerUpdate();
Patrick Venture0079d892019-05-31 11:27:44 -0700610 case UpdateState::updateStarted:
611 /* Calling repeatedly has no effect within an update process. */
612 return true;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700613 default:
614 return false;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800615 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700616}
Patrick Ventured6461d62018-11-09 17:30:25 -0800617
618/*
619 * Close must be called on the firmware image before triggering
620 * verification via commit. Once the verification is complete, you can
621 * then close the hash file.
622 *
623 * If the `verify_image.service` returned success, closing the hash file
624 * will have a specific behavior depending on the update. If it's UBI,
625 * it'll perform the install. If it's static layout, it'll do nothing. The
626 * verify_image service in the static layout case is responsible for placing
627 * the file in the correct staging position.
628 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700629bool FirmwareBlobHandler::close(uint16_t session)
630{
Patrick Venture68bb1432018-11-09 20:08:48 -0800631 auto item = lookup.find(session);
632 if (item == lookup.end())
633 {
634 return false;
635 }
636
Patrick Venturee95dbb62019-06-05 09:59:29 -0700637 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800638 {
Patrick Venturee95dbb62019-06-05 09:59:29 -0700639 case UpdateState::uploadInProgress:
640 /* They are closing a data pathway (image, tarball, hash). */
Patrick Venture75c0c272019-06-21 09:15:08 -0700641 changeState(UpdateState::verificationPending);
Patrick Venture85ae86b2019-06-05 09:18:40 -0700642
Patrick Venturee95dbb62019-06-05 09:59:29 -0700643 /* Add verify blob ID now that we can expect it. */
644 addBlobId(verifyBlobId);
645 break;
646 case UpdateState::verificationPending:
647 /* They haven't triggered, therefore closing is uninteresting.
648 */
649 break;
650 case UpdateState::verificationStarted:
Patrick Ventured5741022019-06-17 09:08:35 -0700651 /* Abort without checking to see if it happened to finish. Require
652 * the caller to stat() deliberately.
Patrick Venturee95dbb62019-06-05 09:59:29 -0700653 */
Patrick Ventured5741022019-06-17 09:08:35 -0700654 abortVerification();
655 abortProcess();
656 break;
Patrick Venturee95dbb62019-06-05 09:59:29 -0700657 case UpdateState::verificationCompleted:
658 if (lastVerificationStatus == ActionStatus::success)
659 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700660 changeState(UpdateState::updatePending);
Patrick Venturee95dbb62019-06-05 09:59:29 -0700661 addBlobId(updateBlobId);
662 removeBlobId(verifyBlobId);
663 }
664 else
665 {
Patrick Venture5d9fa022019-06-17 13:13:30 -0700666 /* Verification failed, and the host-tool knows this by calling
667 * stat(), which triggered the state change to
668 * verificationCompleted.
669 *
670 * Therefore, let's abort the process at this point.
671 */
672 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700673 }
674 break;
675 case UpdateState::updatePending:
676 /* They haven't triggered the update, therefore this is
677 * uninteresting. */
678 break;
679 case UpdateState::updateStarted:
Patrick Venture49731712019-06-17 10:04:02 -0700680 /* Abort without checking to see if it happened to finish. Require
681 * the caller to stat() deliberately.
682 */
683 abortUpdate();
684 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700685 break;
686 case UpdateState::updateCompleted:
687 if (lastUpdateStatus == ActionStatus::failed)
688 {
689 /* TODO: lOG something? */
Patrick Venture4c7a4202019-06-17 13:06:55 -0700690 std::fprintf(stderr, "Update failed\n");
Patrick Venturee95dbb62019-06-05 09:59:29 -0700691 }
Patrick Venture930c7b72019-05-24 11:11:08 -0700692
Patrick Venture4c7a4202019-06-17 13:06:55 -0700693 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700694 break;
695 default:
696 break;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800697 }
698
Patrick Venture68bb1432018-11-09 20:08:48 -0800699 if (item->second->dataHandler)
700 {
701 item->second->dataHandler->close();
702 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800703 if (item->second->imageHandler)
704 {
705 item->second->imageHandler->close();
706 }
707
Patrick Venture68bb1432018-11-09 20:08:48 -0800708 lookup.erase(item);
Patrick Venture991910a2018-11-09 19:43:48 -0800709 fileOpen = false;
Patrick Venture68bb1432018-11-09 20:08:48 -0800710 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700711}
Patrick Ventured6461d62018-11-09 17:30:25 -0800712
Patrick Venture75c0c272019-06-21 09:15:08 -0700713void FirmwareBlobHandler::changeState(UpdateState next)
714{
715 state = next;
Patrick Venture6d7735d2019-06-21 10:03:19 -0700716
717 if (state == UpdateState::notYetStarted)
718 {
719 /* Going back to notyetstarted, let them trigger preparation again. */
720 preparationTriggered = false;
721 }
722 else if (state == UpdateState::uploadInProgress)
723 {
724 /* Store this transition logic here instead of ::open() */
725 if (!preparationTriggered)
726 {
727 preparation->trigger();
728 preparationTriggered = true;
729 }
730 }
Patrick Venture75c0c272019-06-21 09:15:08 -0700731}
732
Patrick Venturec7ca2912018-11-02 11:38:33 -0700733bool FirmwareBlobHandler::expire(uint16_t session)
734{
735 return false;
736}
Patrick Ventured6461d62018-11-09 17:30:25 -0800737
738/*
739 * Currently, the design does not provide this with a function, however,
740 * it will likely change to support reading data back.
741 */
742std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
743 uint32_t offset,
744 uint32_t requestedSize)
745{
746 return {};
747}
748
Patrick Ventured5741022019-06-17 09:08:35 -0700749void FirmwareBlobHandler::abortProcess()
750{
751 /* Closing of open files is handled from close() -- Reaching here from
752 * delete may never be supported.
753 */
754 removeBlobId(verifyBlobId);
755 removeBlobId(updateBlobId);
756 removeBlobId(activeImageBlobId);
757 removeBlobId(activeHashBlobId);
758
Patrick Venture75c0c272019-06-21 09:15:08 -0700759 changeState(UpdateState::notYetStarted);
Patrick Ventured5741022019-06-17 09:08:35 -0700760}
761
762void FirmwareBlobHandler::abortVerification()
763{
764 verification->abort();
765}
766
Patrick Ventureffcc5502018-11-16 12:32:38 -0800767bool FirmwareBlobHandler::triggerVerification()
768{
Patrick Venture1d66fe62019-06-03 14:57:27 -0700769 bool result = verification->trigger();
Patrick Venture3ecb3502019-05-17 11:03:51 -0700770 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800771 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700772 changeState(UpdateState::verificationStarted);
Patrick Venturecabc1172018-11-16 16:14:26 -0800773 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800774
Patrick Venture3ecb3502019-05-17 11:03:51 -0700775 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800776}
777
Patrick Venture49731712019-06-17 10:04:02 -0700778void FirmwareBlobHandler::abortUpdate()
779{
780 update->abort();
781}
782
Patrick Venture1a406fe2019-05-31 07:29:56 -0700783bool FirmwareBlobHandler::triggerUpdate()
784{
Patrick Venture1d66fe62019-06-03 14:57:27 -0700785 bool result = update->trigger();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700786 if (result)
787 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700788 changeState(UpdateState::updateStarted);
Patrick Venture1a406fe2019-05-31 07:29:56 -0700789 }
790
791 return result;
792}
793
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700794} // namespace ipmi_flash