blob: c2e935a2ebf413c914d5cba90593991368603930 [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 Venture7dad86f2019-05-17 08:52:20 -070020#include "util.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -080021
Patrick Venture137ad2c2018-11-06 11:30:43 -080022#include <algorithm>
Patrick Venture192d60f2018-11-06 11:11:59 -080023#include <cstdint>
Patrick Venture18235e62018-11-08 10:21:09 -080024#include <cstring>
Patrick Ventureb3b4db72019-05-15 11:30:24 -070025#include <fstream>
Patrick Venture68cf64f2018-11-06 10:46:51 -080026#include <memory>
Patrick Ventured333a872018-12-03 16:24:26 -080027#include <phosphor-logging/log.hpp>
Patrick Venturefa6c4d92018-11-02 18:34:53 -070028#include <string>
29#include <vector>
30
Patrick Ventured333a872018-12-03 16:24:26 -080031using namespace phosphor::logging;
32
Patrick Venturec7ca2912018-11-02 11:38:33 -070033namespace blobs
34{
Patrick Ventureb3b4db72019-05-15 11:30:24 -070035
Patrick Venture68cf64f2018-11-06 10:46:51 -080036std::unique_ptr<GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080037 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070038 const std::vector<HandlerPack>& firmwares,
Patrick Venture74059d62019-05-17 10:40:26 -070039 const std::vector<DataHandlerPack>& transports,
Patrick Venture3ecb3502019-05-17 11:03:51 -070040 std::unique_ptr<VerificationInterface> verification)
Patrick Venture68cf64f2018-11-06 10:46:51 -080041{
Patrick Venture52854622018-11-06 12:30:00 -080042 /* There must be at least one. */
43 if (!firmwares.size())
44 {
Patrick Ventured333a872018-12-03 16:24:26 -080045 log<level::ERR>("Must provide at least one firmware handler.");
Patrick Venture52854622018-11-06 12:30:00 -080046 return nullptr;
47 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080048 if (!transports.size())
49 {
50 return nullptr;
51 }
Patrick Venture52854622018-11-06 12:30:00 -080052
Patrick Venturea78e39f2018-11-06 18:37:06 -080053 std::vector<std::string> blobs;
54 for (const auto& item : firmwares)
55 {
56 blobs.push_back(item.blobName);
57 }
Patrick Venture7dad86f2019-05-17 08:52:20 -070058 blobs.push_back(verifyBlobId); /* Add blob_id to always exist. */
Patrick Venture18235e62018-11-08 10:21:09 -080059
Patrick Venture7dad86f2019-05-17 08:52:20 -070060 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId))
Patrick Venture18235e62018-11-08 10:21:09 -080061 {
62 return nullptr;
63 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080064
Patrick Venture1cde5f92018-11-07 08:26:47 -080065 std::uint16_t bitmask = 0;
66 for (const auto& item : transports)
67 {
68 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
69 bitmask |= item.bitmask;
70 }
71
Patrick Venture3ecb3502019-05-17 11:03:51 -070072 return std::make_unique<FirmwareBlobHandler>(
73 firmwares, blobs, transports, bitmask, std::move(verification));
Patrick Venture68cf64f2018-11-06 10:46:51 -080074}
75
Patrick Ventured6461d62018-11-09 17:30:25 -080076/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070077bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
78{
Patrick Venture6032dc02019-05-17 11:01:44 -070079 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070080}
Patrick Venture53977962018-11-02 18:59:35 -070081
Patrick Ventured6461d62018-11-09 17:30:25 -080082/*
83 * Grab the list of supported firmware.
84 *
85 * If there's an open firmware session, it'll already be present in the
86 * list as "/flash/active/image", and if the hash has started,
87 * "/flash/active/hash" regardless of mechanism. This is done in the open
88 * comamnd, no extra work is required here.
89 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070090std::vector<std::string> FirmwareBlobHandler::getBlobIds()
91{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080092 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070093}
Patrick Venture53977962018-11-02 18:59:35 -070094
Patrick Ventured6461d62018-11-09 17:30:25 -080095/*
96 * Per the design, this mean abort, and this will trigger whatever
97 * appropriate actions are required to abort the process.
Patrick Venture9e5aab22018-11-09 20:49:28 -080098 *
99 * You cannot delete a blob that has an open handle in the system, therefore
100 * this is never called if there's an open session. Guaranteed by the blob
101 * manager.
Patrick Ventured6461d62018-11-09 17:30:25 -0800102 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700103bool FirmwareBlobHandler::deleteBlob(const std::string& path)
104{
Patrick Venture9e5aab22018-11-09 20:49:28 -0800105 const std::string* toDelete;
106
Patrick Ventureffcc5502018-11-16 12:32:38 -0800107 /* You cannot delete the verify blob -- trying to delete it, currently has
108 * no impact.
109 * TODO: Should trying to delete this cause an abort?
110 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700111 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800112 {
113 return false;
114 }
115
Patrick Venture7dad86f2019-05-17 08:52:20 -0700116 if (path == hashBlobId || path == activeHashBlobId)
Patrick Venture9e5aab22018-11-09 20:49:28 -0800117 {
118 /* They're deleting the hash. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700119 toDelete = &activeHashBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800120 }
121 else
122 {
123 /* They're deleting the image. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700124 toDelete = &activeImageBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800125 }
126
127 auto it = std::find_if(
128 blobIDs.begin(), blobIDs.end(),
129 [toDelete](const auto& iter) { return (iter == *toDelete); });
130 if (it == blobIDs.end())
131 {
132 /* Somehow they've asked to delete something we didn't say we could
133 * handle.
134 */
135 return false;
136 }
137
138 blobIDs.erase(it);
139
140 /* TODO: Handle aborting the process and fixing up the state. */
141
142 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700143}
Patrick Venture53977962018-11-02 18:59:35 -0700144
Patrick Ventured6461d62018-11-09 17:30:25 -0800145/*
146 * Stat on the files will return information such as what supported
147 * transport mechanisms are available.
148 *
149 * Stat on an active file or hash will return information such as the size
150 * of the data cached, and any additional pertinent information. The
151 * blob_state on the active files will return the state of the update.
152 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700153bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
154{
Patrick Venture46637c82018-11-06 15:20:24 -0800155 /* We know we support this path because canHandle is called ahead */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700156 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800157 {
Patrick Venture699750d2019-05-15 09:24:09 -0700158 /* TODO: We need to return information for the verify state -- did they
159 * call commit() did things start?
Patrick Ventureffcc5502018-11-16 12:32:38 -0800160 */
161 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700162 else if (path == activeImageBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800163 {
Patrick Venture699750d2019-05-15 09:24:09 -0700164 /* TODO: We need to return information for the image that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800165 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700166 else if (path == activeHashBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800167 {
Patrick Venture699750d2019-05-15 09:24:09 -0700168 /* TODO: We need to return information for the hash that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800169 }
170 else
171 {
172 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800173 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800174 meta->size = 0;
175
176 /* The generic blob_ids state is only the bits related to the transport
Patrick Ventured6461d62018-11-09 17:30:25 -0800177 * mechanisms.
178 */
Patrick Venture46637c82018-11-06 15:20:24 -0800179 return true;
180 }
181
Patrick Venturec7ca2912018-11-02 11:38:33 -0700182 return false;
183}
Patrick Venture53977962018-11-02 18:59:35 -0700184
Patrick Venturec02849b2018-11-06 17:31:15 -0800185/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800186 * Return stat information on an open session. It therefore must be an active
187 * handle to either the active image or active hash.
188 *
189 * The stat() and sessionstat() commands will return the same information in
190 * many cases, therefore the logic will be combined.
191 *
192 * TODO: combine the logic for stat and sessionstat().
193 */
194bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
195{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800196 auto item = lookup.find(session);
197 if (item == lookup.end())
198 {
199 return false;
200 }
201
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700202 /* The size here refers to the size of the file -- of something analagous.
203 */
204 meta->size = (item->second->imageHandler)
205 ? item->second->imageHandler->getSize()
206 : 0;
207
208 meta->metadata.clear();
209
Patrick Venture699750d2019-05-15 09:24:09 -0700210 /* TODO: Implement this for the verification blob, which is what we expect.
211 * Calling stat() on the verify blob without an active session should not
212 * provide insight.
213 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700214 if (item->second->activePath == verifyBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700215 {
Patrick Venture3ecb3502019-05-17 11:03:51 -0700216 auto value = verification->checkVerificationState();
Patrick Venture699750d2019-05-15 09:24:09 -0700217
Patrick Venturee955e072019-05-15 16:16:46 -0700218 meta->metadata.push_back(static_cast<std::uint8_t>(value));
219
220 /* Change the firmware handler's state and the blob's stat value
221 * depending.
222 */
223 if (value == VerifyCheckResponses::success ||
224 value == VerifyCheckResponses::failed)
225 {
226 state = UpdateState::verificationCompleted;
227 item->second->flags &= ~StateFlags::committing;
228
229 if (value == VerifyCheckResponses::success)
230 {
231 item->second->flags |= StateFlags::committed;
232 }
233 else
234 {
235 item->second->flags |= StateFlags::commit_error;
236 }
237 }
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700238 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800239
Patrick Venturee955e072019-05-15 16:16:46 -0700240 /* The blobState here relates to an active sesion, so we should return the
241 * flags used to open this session.
242 */
243 meta->blobState = item->second->flags;
244
Patrick Venturecc7d1602018-11-15 13:58:33 -0800245 /* The metadata blob returned comes from the data handler... it's used for
246 * instance, in P2A bridging to get required information about the mapping,
247 * and is the "opposite" of the lpc writemeta requirement.
248 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800249 if (item->second->dataHandler)
250 {
Patrick Venture74304642019-01-17 09:31:04 -0800251 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800252 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
253 bytes.end());
254 }
255
Patrick Venturecc7d1602018-11-15 13:58:33 -0800256 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800257}
258
259/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800260 * If you open /flash/image or /flash/tarball, or /flash/hash it will
261 * interpret the open flags and perform whatever actions are required for
262 * that update process. The session returned can be used immediately for
263 * sending data down, without requiring one to open the new active file.
264 *
265 * If you open the active flash image or active hash it will let you
266 * overwrite pieces, depending on the state.
267 *
268 * Once the verification process has started the active files cannot be
269 * opened.
270 *
271 * You can only have one open session at a time. Which means, you can only
272 * have one file open at a time. Trying to open the hash blob_id while you
273 * still have the flash image blob_id open will fail. Opening the flash
274 * blob_id when it is already open will fail.
275 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700276bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
277 const std::string& path)
278{
Patrick Venture6e307a72018-11-09 18:21:17 -0800279 /* Check that they've opened for writing - read back not currently
280 * supported.
281 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800282 if ((flags & OpenFlags::write) == 0)
283 {
284 return false;
285 }
286
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800287 /* Is the verification process underway? */
288 if (state == UpdateState::verificationStarted)
289 {
290 return false;
291 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800292
293 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800294 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800295 * TODO: Temporarily using a simple boolean flag until there's a full
296 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800297 *
298 * 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 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800307 if (fileOpen)
308 {
309 return false;
310 }
311
Patrick Ventureffcc5502018-11-16 12:32:38 -0800312 /* Handle opening the verifyBlobId --> we know the image and hash aren't
313 * open because of the fileOpen check.
314 *
315 * The file must be opened for writing, but no transport mechanism specified
316 * since it's irrelevant.
317 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700318 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800319 {
320 /* In this case, there's no image handler to use, or data handler,
321 * simply set up a session.
322 */
323 verifyImage.flags = flags;
324 verifyImage.state = Session::State::open;
325
326 lookup[session] = &verifyImage;
327
328 fileOpen = true;
329
330 return true;
331 }
332
Patrick Venturec02849b2018-11-06 17:31:15 -0800333 /* There are two abstractions at play, how you get the data and how you
334 * handle that data. such that, whether the data comes from the PCI bridge
335 * or LPC bridge is not connected to whether the data goes into a static
336 * layout flash update or a UBI tarball.
337 */
338
339 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800340 * support what they request.
341 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800342 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800343 {
344 return false;
345 }
346
347 /* 2) there isn't, so what are they opening? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700348 if (path == activeImageBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800349 {
350 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800351 * already started one (due to canHandleBlob's behavior).
352 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800353 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800354 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700355 else if (path == activeHashBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800356 {
357 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800358 * already started one (due to canHandleBlob's behavior).
359 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800360 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800361 }
Patrick Venture18235e62018-11-08 10:21:09 -0800362
363 /* How are they expecting to copy this data? */
364 auto d = std::find_if(
365 transports.begin(), transports.end(),
366 [&flags](const auto& iter) { return (iter.bitmask & flags); });
367 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800368 {
Patrick Venture18235e62018-11-08 10:21:09 -0800369 return false;
370 }
371
372 /* We found the transport handler they requested, no surprise since
373 * above we verify they selected at least one we wanted.
374 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800375
Patrick Venture6e307a72018-11-09 18:21:17 -0800376 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
377 * only non-external data pathway -- but this is just a more generic
378 * approach to that.
379 */
380 if (d->handler)
381 {
382 /* If the data handler open call fails, open fails. */
383 if (!d->handler->open())
384 {
385 return false;
386 }
387 }
388
Patrick Venture70e30bf2019-01-17 10:29:28 -0800389 /* Do we have a file handler for the type of file they're opening.
390 * Note: This should only fail if something is somehow crazy wrong.
391 * Since the canHandle() said yes, and that's tied into the list of explicit
392 * firmware handers (and file handlers, like this'll know where to write the
393 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800394 */
Patrick Venture18235e62018-11-08 10:21:09 -0800395 auto h = std::find_if(
396 handlers.begin(), handlers.end(),
397 [&path](const auto& iter) { return (iter.blobName == path); });
398 if (h == handlers.end())
399 {
400 return false;
401 }
402
403 /* Ok, so we found a handler that matched, so call open() */
404 if (!h->handler->open(path))
405 {
406 return false;
407 }
408
Patrick Venture70e30bf2019-01-17 10:29:28 -0800409 Session* curr;
410 const std::string* active;
411
Patrick Venture7dad86f2019-05-17 08:52:20 -0700412 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800413 {
414 /* 2c) are they opening the /flash/hash ? (to start the process) */
415 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700416 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800417 }
418 else
419 {
420 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700421 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800422 }
423
Patrick Venture18235e62018-11-08 10:21:09 -0800424 curr->flags = flags;
425 curr->dataHandler = d->handler;
426 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800427 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800428
429 lookup[session] = curr;
430
Patrick Venturedaf47072019-05-10 15:21:55 -0700431 /* This may be them re-opening a blob, so let's only push it onto the list
432 * when appropriate.
433 */
434 auto blobIdMatch =
435 std::find_if(blobIDs.begin(), blobIDs.end(),
436 [active](const auto& iter) { return (iter == *active); });
437 if (blobIdMatch == blobIDs.end())
438 {
439 blobIDs.push_back(*active);
440 }
Patrick Venturedb253bf2018-11-09 19:36:03 -0800441
Patrick Venture12233c52019-05-16 09:26:59 -0700442 state = UpdateState::uploadInProgress;
Patrick Venture991910a2018-11-09 19:43:48 -0800443 fileOpen = true;
444
Patrick Venture18235e62018-11-08 10:21:09 -0800445 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700446}
Patrick Venture53977962018-11-02 18:59:35 -0700447
Patrick Venture18235e62018-11-08 10:21:09 -0800448/**
449 * The write command really just grabs the data from wherever it is and sends it
450 * to the image handler. It's the image handler's responsibility to deal with
451 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800452 *
453 * This receives a session from the blob manager, therefore it is always called
454 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800455 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700456bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
457 const std::vector<uint8_t>& data)
458{
Patrick Venture18235e62018-11-08 10:21:09 -0800459 auto item = lookup.find(session);
460 if (item == lookup.end())
461 {
462 return false;
463 }
464
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800465 /* Prevent writing during verification. */
466 if (state == UpdateState::verificationStarted)
467 {
468 return false;
469 }
470
Patrick Venture8af15eb2019-05-15 09:39:22 -0700471 /* Prevent writing to the verification blob before they trigger
Patrick Venture699750d2019-05-15 09:24:09 -0700472 * verification.
473 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700474 if (item->second->activePath == verifyBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700475 {
476 return false;
477 }
Patrick Venture699750d2019-05-15 09:24:09 -0700478
Patrick Venture18235e62018-11-08 10:21:09 -0800479 std::vector<std::uint8_t> bytes;
480
Patrick Venture05abf7e2018-11-09 11:02:56 -0800481 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800482 {
483 bytes = data;
484 }
485 else
486 {
487 /* little endian required per design, and so on, but TODO: do endianness
488 * with boost.
489 */
490 struct ExtChunkHdr header;
491
492 if (data.size() != sizeof(header))
493 {
494 return false;
495 }
496
497 std::memcpy(&header, data.data(), data.size());
498 bytes = item->second->dataHandler->copyFrom(header.length);
499 }
500
501 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700502}
Patrick Venture18235e62018-11-08 10:21:09 -0800503
Patrick Venture8c535332018-11-08 15:58:00 -0800504/*
505 * If the active session (image or hash) is over LPC, this allows
506 * configuring it. This option is only available before you start
507 * writing data for the given item (image or hash). This will return
508 * false at any other part. -- the lpc handler portion will know to return
509 * false.
510 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700511bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
512 const std::vector<uint8_t>& data)
513{
Patrick Venture8c535332018-11-08 15:58:00 -0800514 auto item = lookup.find(session);
515 if (item == lookup.end())
516 {
517 return false;
518 }
519
Patrick Venture05abf7e2018-11-09 11:02:56 -0800520 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800521 {
522 return false;
523 }
524
Patrick Ventured56097a2019-05-15 09:47:55 -0700525 /* Prevent writing meta to the verification blob (it has no data handler).
526 */
527 if (item->second->dataHandler)
528 {
529 return item->second->dataHandler->writeMeta(data);
530 }
Patrick Venture699750d2019-05-15 09:24:09 -0700531
Patrick Ventured56097a2019-05-15 09:47:55 -0700532 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700533}
Patrick Venture8c535332018-11-08 15:58:00 -0800534
Patrick Ventured6461d62018-11-09 17:30:25 -0800535/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700536 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800537 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800538 * the image.
539 *
540 * For this file to have opened, the other two must be closed, which means any
541 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800542 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700543bool FirmwareBlobHandler::commit(uint16_t session,
544 const std::vector<uint8_t>& data)
545{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800546 auto item = lookup.find(session);
547 if (item == lookup.end())
548 {
549 return false;
550 }
551
552 /* You can only commit on the verifyBlodId */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700553 if (item->second->activePath != verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800554 {
555 return false;
556 }
557
Patrick Venturebe198702019-05-15 09:46:02 -0700558 /* Calling repeatedly has no effect within an update process. */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800559 if (state == UpdateState::verificationStarted)
560 {
Patrick Venturebe198702019-05-15 09:46:02 -0700561 return true;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800562 }
563
564 /* Set state to committing. */
565 item->second->flags |= StateFlags::committing;
566
567 return triggerVerification();
Patrick Venturec7ca2912018-11-02 11:38:33 -0700568}
Patrick Ventured6461d62018-11-09 17:30:25 -0800569
570/*
571 * Close must be called on the firmware image before triggering
572 * verification via commit. Once the verification is complete, you can
573 * then close the hash file.
574 *
575 * If the `verify_image.service` returned success, closing the hash file
576 * will have a specific behavior depending on the update. If it's UBI,
577 * it'll perform the install. If it's static layout, it'll do nothing. The
578 * verify_image service in the static layout case is responsible for placing
579 * the file in the correct staging position.
580 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700581bool FirmwareBlobHandler::close(uint16_t session)
582{
Patrick Venture68bb1432018-11-09 20:08:48 -0800583 auto item = lookup.find(session);
584 if (item == lookup.end())
585 {
586 return false;
587 }
588
Patrick Ventureffcc5502018-11-16 12:32:38 -0800589 /* Are you closing the verify blob? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700590 if (item->second->activePath == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800591 {
Patrick Ventureffcc5502018-11-16 12:32:38 -0800592 if (state == UpdateState::verificationStarted)
593 {
Patrick Venture12233c52019-05-16 09:26:59 -0700594 /* TODO: If they close this blob before verification finishes,
595 * that's an abort.
596 */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800597 return false;
598 }
Patrick Venture12233c52019-05-16 09:26:59 -0700599 else if (state == UpdateState::verificationCompleted)
600 {
601 /* TODO: Should this delete the verification artifact? */
602 state = UpdateState::notYetStarted;
603
604 /* A this point, delete the active blob IDs from the blob_list. */
605 blobIDs.erase(
Patrick Venture7dad86f2019-05-17 08:52:20 -0700606 std::remove(blobIDs.begin(), blobIDs.end(), activeImageBlobId));
Patrick Venture12233c52019-05-16 09:26:59 -0700607 blobIDs.erase(
Patrick Venture7dad86f2019-05-17 08:52:20 -0700608 std::remove(blobIDs.begin(), blobIDs.end(), activeHashBlobId));
Patrick Venture12233c52019-05-16 09:26:59 -0700609 }
610 /* Must be verificationPending... not yet started, they may re-open and
611 * trigger verification.
612 */
613 }
614 else
615 {
616 /* They are closing a data pathway (image, tarball, hash). */
617 state = UpdateState::verificationPending;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800618 }
619
Patrick Venture68bb1432018-11-09 20:08:48 -0800620 if (item->second->dataHandler)
621 {
622 item->second->dataHandler->close();
623 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800624 if (item->second->imageHandler)
625 {
626 item->second->imageHandler->close();
627 }
628
Patrick Venture68bb1432018-11-09 20:08:48 -0800629 item->second->state = Session::State::closed;
630 /* Do not delete the active blob_id from the list of blob_ids, because that
631 * blob_id indicates there is data stored. Delete will destroy it.
632 */
633
634 lookup.erase(item);
635
Patrick Venture991910a2018-11-09 19:43:48 -0800636 fileOpen = false;
637
Patrick Venture68bb1432018-11-09 20:08:48 -0800638 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700639}
Patrick Ventured6461d62018-11-09 17:30:25 -0800640
Patrick Venturec7ca2912018-11-02 11:38:33 -0700641bool FirmwareBlobHandler::expire(uint16_t session)
642{
643 return false;
644}
Patrick Ventured6461d62018-11-09 17:30:25 -0800645
646/*
647 * Currently, the design does not provide this with a function, however,
648 * it will likely change to support reading data back.
649 */
650std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
651 uint32_t offset,
652 uint32_t requestedSize)
653{
654 return {};
655}
656
Patrick Ventureffcc5502018-11-16 12:32:38 -0800657bool FirmwareBlobHandler::triggerVerification()
658{
Patrick Venture3ecb3502019-05-17 11:03:51 -0700659 bool result = verification->triggerVerification();
660 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800661 {
Patrick Venture453f06a2019-01-17 10:02:12 -0800662 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800663 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800664
Patrick Venture3ecb3502019-05-17 11:03:51 -0700665 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800666}
667
Patrick Venturec7ca2912018-11-02 11:38:33 -0700668} // namespace blobs