blob: af617301f9d673d87d69364642ec824a9ef28021 [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"
20
Patrick Venture137ad2c2018-11-06 11:30:43 -080021#include <algorithm>
Patrick Venture192d60f2018-11-06 11:11:59 -080022#include <cstdint>
Patrick Venture18235e62018-11-08 10:21:09 -080023#include <cstring>
Patrick Venture68cf64f2018-11-06 10:46:51 -080024#include <memory>
Patrick Ventured333a872018-12-03 16:24:26 -080025#include <phosphor-logging/log.hpp>
Patrick Venturefa6c4d92018-11-02 18:34:53 -070026#include <string>
27#include <vector>
28
Patrick Ventured333a872018-12-03 16:24:26 -080029using namespace phosphor::logging;
30
Patrick Venturec7ca2912018-11-02 11:38:33 -070031namespace blobs
32{
Patrick Venturecabc1172018-11-16 16:14:26 -080033// systemd service to kick start a target.
34static constexpr auto systemdService = "org.freedesktop.systemd1";
35static constexpr auto systemdRoot = "/org/freedesktop/systemd1";
36static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
37static constexpr auto verifyTarget = "verify_image.service";
Patrick Venturec7ca2912018-11-02 11:38:33 -070038
Patrick Ventureffcc5502018-11-16 12:32:38 -080039const std::string FirmwareBlobHandler::verifyBlobID = "/flash/verify";
Patrick Venture21be45a2018-11-06 12:08:52 -080040const std::string FirmwareBlobHandler::hashBlobID = "/flash/hash";
Patrick Venture7b9256f2018-11-06 15:06:04 -080041const std::string FirmwareBlobHandler::activeImageBlobID =
42 "/flash/active/image";
43const std::string FirmwareBlobHandler::activeHashBlobID = "/flash/active/hash";
Patrick Venture4cceb8e2018-11-06 11:56:48 -080044
Patrick Venture68cf64f2018-11-06 10:46:51 -080045std::unique_ptr<GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080046 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture4eb55952018-11-16 15:36:24 -080047 sdbusplus::bus::bus&& bus, const std::vector<HandlerPack>& firmwares,
Patrick Venture1cde5f92018-11-07 08:26:47 -080048 const std::vector<DataHandlerPack>& transports)
Patrick Venture68cf64f2018-11-06 10:46:51 -080049{
Patrick Venture52854622018-11-06 12:30:00 -080050 /* There must be at least one. */
51 if (!firmwares.size())
52 {
Patrick Ventured333a872018-12-03 16:24:26 -080053 log<level::ERR>("Must provide at least one firmware handler.");
Patrick Venture52854622018-11-06 12:30:00 -080054 return nullptr;
55 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080056 if (!transports.size())
57 {
58 return nullptr;
59 }
Patrick Venture52854622018-11-06 12:30:00 -080060
Patrick Venturea78e39f2018-11-06 18:37:06 -080061 std::vector<std::string> blobs;
62 for (const auto& item : firmwares)
63 {
64 blobs.push_back(item.blobName);
65 }
Patrick Ventureffcc5502018-11-16 12:32:38 -080066 blobs.push_back(verifyBlobID); /* Add blob_id to always exist. */
Patrick Venture18235e62018-11-08 10:21:09 -080067
68 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobID))
69 {
70 return nullptr;
71 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080072
Patrick Venture1cde5f92018-11-07 08:26:47 -080073 std::uint16_t bitmask = 0;
74 for (const auto& item : transports)
75 {
76 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
77 bitmask |= item.bitmask;
78 }
79
Patrick Venture4eb55952018-11-16 15:36:24 -080080 return std::make_unique<FirmwareBlobHandler>(std::move(bus), firmwares,
81 blobs, transports, bitmask);
Patrick Venture68cf64f2018-11-06 10:46:51 -080082}
83
Patrick Ventured6461d62018-11-09 17:30:25 -080084/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070085bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
86{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080087 if (std::count(blobIDs.begin(), blobIDs.end(), path))
Patrick Venture137ad2c2018-11-06 11:30:43 -080088 {
89 return true;
90 }
91
Patrick Venturec7ca2912018-11-02 11:38:33 -070092 return false;
93}
Patrick Venture53977962018-11-02 18:59:35 -070094
Patrick Ventured6461d62018-11-09 17:30:25 -080095/*
96 * Grab the list of supported firmware.
97 *
98 * If there's an open firmware session, it'll already be present in the
99 * list as "/flash/active/image", and if the hash has started,
100 * "/flash/active/hash" regardless of mechanism. This is done in the open
101 * comamnd, no extra work is required here.
102 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700103std::vector<std::string> FirmwareBlobHandler::getBlobIds()
104{
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800105 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700106}
Patrick Venture53977962018-11-02 18:59:35 -0700107
Patrick Ventured6461d62018-11-09 17:30:25 -0800108/*
109 * Per the design, this mean abort, and this will trigger whatever
110 * appropriate actions are required to abort the process.
Patrick Venture9e5aab22018-11-09 20:49:28 -0800111 *
112 * You cannot delete a blob that has an open handle in the system, therefore
113 * this is never called if there's an open session. Guaranteed by the blob
114 * manager.
Patrick Ventured6461d62018-11-09 17:30:25 -0800115 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700116bool FirmwareBlobHandler::deleteBlob(const std::string& path)
117{
Patrick Venture9e5aab22018-11-09 20:49:28 -0800118 const std::string* toDelete;
119
Patrick Ventureffcc5502018-11-16 12:32:38 -0800120 /* You cannot delete the verify blob -- trying to delete it, currently has
121 * no impact.
122 * TODO: Should trying to delete this cause an abort?
123 */
124 if (path == verifyBlobID)
125 {
126 return false;
127 }
128
Patrick Venture9e5aab22018-11-09 20:49:28 -0800129 if (path == hashBlobID || path == activeHashBlobID)
130 {
131 /* They're deleting the hash. */
132 toDelete = &activeHashBlobID;
133 }
134 else
135 {
136 /* They're deleting the image. */
137 toDelete = &activeImageBlobID;
138 }
139
140 auto it = std::find_if(
141 blobIDs.begin(), blobIDs.end(),
142 [toDelete](const auto& iter) { return (iter == *toDelete); });
143 if (it == blobIDs.end())
144 {
145 /* Somehow they've asked to delete something we didn't say we could
146 * handle.
147 */
148 return false;
149 }
150
151 blobIDs.erase(it);
152
153 /* TODO: Handle aborting the process and fixing up the state. */
154
155 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700156}
Patrick Venture53977962018-11-02 18:59:35 -0700157
Patrick Ventured6461d62018-11-09 17:30:25 -0800158/*
159 * Stat on the files will return information such as what supported
160 * transport mechanisms are available.
161 *
162 * Stat on an active file or hash will return information such as the size
163 * of the data cached, and any additional pertinent information. The
164 * blob_state on the active files will return the state of the update.
165 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700166bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
167{
Patrick Venture46637c82018-11-06 15:20:24 -0800168 /* We know we support this path because canHandle is called ahead */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800169 if (path == verifyBlobID)
170 {
171 /* We need to return information for the verify state -- did they call
172 * commit() did things start?
173 */
174 }
175 else if (path == activeImageBlobID)
Patrick Venture46637c82018-11-06 15:20:24 -0800176 {
177 /* We need to return information for the image that's staged. */
178 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800179 else if (path == activeHashBlobID)
Patrick Venture46637c82018-11-06 15:20:24 -0800180 {
181 /* We need to return information for the hash that's staged. */
182 }
183 else
184 {
185 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800186 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800187 meta->size = 0;
188
189 /* The generic blob_ids state is only the bits related to the transport
Patrick Ventured6461d62018-11-09 17:30:25 -0800190 * mechanisms.
191 */
Patrick Venture46637c82018-11-06 15:20:24 -0800192 return true;
193 }
194
Patrick Venturec7ca2912018-11-02 11:38:33 -0700195 return false;
196}
Patrick Venture53977962018-11-02 18:59:35 -0700197
Patrick Venturec02849b2018-11-06 17:31:15 -0800198/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800199 * Return stat information on an open session. It therefore must be an active
200 * handle to either the active image or active hash.
201 *
202 * The stat() and sessionstat() commands will return the same information in
203 * many cases, therefore the logic will be combined.
204 *
205 * TODO: combine the logic for stat and sessionstat().
206 */
207bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
208{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800209 auto item = lookup.find(session);
210 if (item == lookup.end())
211 {
212 return false;
213 }
214
215 /* The blobState here relates to an active sesion, so we should return the
216 * flags used to open this session.
Patrick Ventured6461d62018-11-09 17:30:25 -0800217 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800218 meta->blobState = item->second->flags;
219 /* The size here refers to the size of the file -- of something analagous.
220 */
221 meta->size = item->second->imageHandler->getSize();
222
223 /* The metadata blob returned comes from the data handler... it's used for
224 * instance, in P2A bridging to get required information about the mapping,
225 * and is the "opposite" of the lpc writemeta requirement.
226 */
227 meta->metadata.clear();
228 if (item->second->dataHandler)
229 {
Patrick Venture74304642019-01-17 09:31:04 -0800230 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800231 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
232 bytes.end());
233 }
234
235 /* TODO: During things like verification, etc, we can report the state as
236 * committed, etc, so we'll need to do that.
237 */
238
239 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800240}
241
242/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800243 * If you open /flash/image or /flash/tarball, or /flash/hash it will
244 * interpret the open flags and perform whatever actions are required for
245 * that update process. The session returned can be used immediately for
246 * sending data down, without requiring one to open the new active file.
247 *
248 * If you open the active flash image or active hash it will let you
249 * overwrite pieces, depending on the state.
250 *
251 * Once the verification process has started the active files cannot be
252 * opened.
253 *
254 * You can only have one open session at a time. Which means, you can only
255 * have one file open at a time. Trying to open the hash blob_id while you
256 * still have the flash image blob_id open will fail. Opening the flash
257 * blob_id when it is already open will fail.
258 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700259bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
260 const std::string& path)
261{
Patrick Venture6e307a72018-11-09 18:21:17 -0800262 /* Check that they've opened for writing - read back not currently
263 * supported.
264 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800265 if ((flags & OpenFlags::write) == 0)
266 {
267 return false;
268 }
269
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800270 /* Is the verification process underway? */
271 if (state == UpdateState::verificationStarted)
272 {
273 return false;
274 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800275
276 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800277 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800278 * TODO: Temporarily using a simple boolean flag until there's a full
279 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800280 *
281 * Further on this, if there's an active session to the hash we don't allow
282 * re-opening the image, and if we have the image open, we don't allow
283 * opening the hash. This design decision may be re-evaluated, and changed
284 * to only allow one session per object type (of the two types). But,
285 * consider if the hash is open, do we want to allow writing to the image?
286 * And why would we? But, really, the point of no-return is once the
287 * verification process has begun -- which is done via commit() on the hash
288 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700289 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800290 if (fileOpen)
291 {
292 return false;
293 }
294
Patrick Ventureffcc5502018-11-16 12:32:38 -0800295 /* Handle opening the verifyBlobId --> we know the image and hash aren't
296 * open because of the fileOpen check.
297 *
298 * The file must be opened for writing, but no transport mechanism specified
299 * since it's irrelevant.
300 */
301 if (path == verifyBlobID)
302 {
303 /* In this case, there's no image handler to use, or data handler,
304 * simply set up a session.
305 */
306 verifyImage.flags = flags;
307 verifyImage.state = Session::State::open;
308
309 lookup[session] = &verifyImage;
310
311 fileOpen = true;
312
313 return true;
314 }
315
Patrick Venturec02849b2018-11-06 17:31:15 -0800316 /* There are two abstractions at play, how you get the data and how you
317 * handle that data. such that, whether the data comes from the PCI bridge
318 * or LPC bridge is not connected to whether the data goes into a static
319 * layout flash update or a UBI tarball.
320 */
321
322 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800323 * support what they request.
324 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800325 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800326 {
327 return false;
328 }
329
330 /* 2) there isn't, so what are they opening? */
331 if (path == activeImageBlobID)
332 {
333 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800334 * already started one (due to canHandleBlob's behavior).
335 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800336 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800337 }
338 else if (path == activeHashBlobID)
339 {
340 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800341 * already started one (due to canHandleBlob's behavior).
342 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800343 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800344 }
Patrick Venture18235e62018-11-08 10:21:09 -0800345
346 /* How are they expecting to copy this data? */
347 auto d = std::find_if(
348 transports.begin(), transports.end(),
349 [&flags](const auto& iter) { return (iter.bitmask & flags); });
350 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800351 {
Patrick Venture18235e62018-11-08 10:21:09 -0800352 return false;
353 }
354
355 /* We found the transport handler they requested, no surprise since
356 * above we verify they selected at least one we wanted.
357 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800358
Patrick Venture6e307a72018-11-09 18:21:17 -0800359 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
360 * only non-external data pathway -- but this is just a more generic
361 * approach to that.
362 */
363 if (d->handler)
364 {
365 /* If the data handler open call fails, open fails. */
366 if (!d->handler->open())
367 {
368 return false;
369 }
370 }
371
Patrick Venture70e30bf2019-01-17 10:29:28 -0800372 /* Do we have a file handler for the type of file they're opening.
373 * Note: This should only fail if something is somehow crazy wrong.
374 * Since the canHandle() said yes, and that's tied into the list of explicit
375 * firmware handers (and file handlers, like this'll know where to write the
376 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800377 */
Patrick Venture18235e62018-11-08 10:21:09 -0800378 auto h = std::find_if(
379 handlers.begin(), handlers.end(),
380 [&path](const auto& iter) { return (iter.blobName == path); });
381 if (h == handlers.end())
382 {
383 return false;
384 }
385
386 /* Ok, so we found a handler that matched, so call open() */
387 if (!h->handler->open(path))
388 {
389 return false;
390 }
391
Patrick Venture70e30bf2019-01-17 10:29:28 -0800392 Session* curr;
393 const std::string* active;
394
395 if (path == hashBlobID)
396 {
397 /* 2c) are they opening the /flash/hash ? (to start the process) */
398 curr = &activeHash;
399 active = &activeHashBlobID;
400 }
401 else
402 {
403 curr = &activeImage;
404 active = &activeImageBlobID;
405 }
406
Patrick Venture18235e62018-11-08 10:21:09 -0800407 curr->flags = flags;
408 curr->dataHandler = d->handler;
409 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800410 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800411
412 lookup[session] = curr;
413
Patrick Venturedaf47072019-05-10 15:21:55 -0700414 /* This may be them re-opening a blob, so let's only push it onto the list
415 * when appropriate.
416 */
417 auto blobIdMatch =
418 std::find_if(blobIDs.begin(), blobIDs.end(),
419 [active](const auto& iter) { return (iter == *active); });
420 if (blobIdMatch == blobIDs.end())
421 {
422 blobIDs.push_back(*active);
423 }
Patrick Venturedb253bf2018-11-09 19:36:03 -0800424
Patrick Venture991910a2018-11-09 19:43:48 -0800425 fileOpen = true;
426
Patrick Venture18235e62018-11-08 10:21:09 -0800427 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700428}
Patrick Venture53977962018-11-02 18:59:35 -0700429
Patrick Venture18235e62018-11-08 10:21:09 -0800430/**
431 * The write command really just grabs the data from wherever it is and sends it
432 * to the image handler. It's the image handler's responsibility to deal with
433 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800434 *
435 * This receives a session from the blob manager, therefore it is always called
436 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800437 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700438bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
439 const std::vector<uint8_t>& data)
440{
Patrick Venture18235e62018-11-08 10:21:09 -0800441 auto item = lookup.find(session);
442 if (item == lookup.end())
443 {
444 return false;
445 }
446
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800447 /* Prevent writing during verification. */
448 if (state == UpdateState::verificationStarted)
449 {
450 return false;
451 }
452
Patrick Venture18235e62018-11-08 10:21:09 -0800453 std::vector<std::uint8_t> bytes;
454
Patrick Venture05abf7e2018-11-09 11:02:56 -0800455 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800456 {
457 bytes = data;
458 }
459 else
460 {
461 /* little endian required per design, and so on, but TODO: do endianness
462 * with boost.
463 */
464 struct ExtChunkHdr header;
465
466 if (data.size() != sizeof(header))
467 {
468 return false;
469 }
470
471 std::memcpy(&header, data.data(), data.size());
472 bytes = item->second->dataHandler->copyFrom(header.length);
473 }
474
475 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700476}
Patrick Venture18235e62018-11-08 10:21:09 -0800477
Patrick Venture8c535332018-11-08 15:58:00 -0800478/*
479 * If the active session (image or hash) is over LPC, this allows
480 * configuring it. This option is only available before you start
481 * writing data for the given item (image or hash). This will return
482 * false at any other part. -- the lpc handler portion will know to return
483 * false.
484 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700485bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
486 const std::vector<uint8_t>& data)
487{
Patrick Venture8c535332018-11-08 15:58:00 -0800488 auto item = lookup.find(session);
489 if (item == lookup.end())
490 {
491 return false;
492 }
493
Patrick Venture05abf7e2018-11-09 11:02:56 -0800494 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800495 {
496 return false;
497 }
498
Patrick Venture74304642019-01-17 09:31:04 -0800499 return item->second->dataHandler->writeMeta(data);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700500}
Patrick Venture8c535332018-11-08 15:58:00 -0800501
Patrick Ventured6461d62018-11-09 17:30:25 -0800502/*
Patrick Ventureffcc5502018-11-16 12:32:38 -0800503 * If this command is called on the session for the verifyBlobID, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800504 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800505 * the image.
506 *
507 * For this file to have opened, the other two must be closed, which means any
508 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800509 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700510bool FirmwareBlobHandler::commit(uint16_t session,
511 const std::vector<uint8_t>& data)
512{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800513 auto item = lookup.find(session);
514 if (item == lookup.end())
515 {
516 return false;
517 }
518
519 /* You can only commit on the verifyBlodId */
520 if (item->second->activePath != verifyBlobID)
521 {
522 return false;
523 }
524
525 /* Can only be called once per verification. */
526 if (state == UpdateState::verificationStarted)
527 {
528 return false;
529 }
530
531 /* Set state to committing. */
532 item->second->flags |= StateFlags::committing;
533
534 return triggerVerification();
Patrick Venturec7ca2912018-11-02 11:38:33 -0700535}
Patrick Ventured6461d62018-11-09 17:30:25 -0800536
537/*
538 * Close must be called on the firmware image before triggering
539 * verification via commit. Once the verification is complete, you can
540 * then close the hash file.
541 *
542 * If the `verify_image.service` returned success, closing the hash file
543 * will have a specific behavior depending on the update. If it's UBI,
544 * it'll perform the install. If it's static layout, it'll do nothing. The
545 * verify_image service in the static layout case is responsible for placing
546 * the file in the correct staging position.
547 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700548bool FirmwareBlobHandler::close(uint16_t session)
549{
Patrick Venture68bb1432018-11-09 20:08:48 -0800550 auto item = lookup.find(session);
551 if (item == lookup.end())
552 {
553 return false;
554 }
555
Patrick Ventureffcc5502018-11-16 12:32:38 -0800556 /* Are you closing the verify blob? */
557 if (item->second->activePath == verifyBlobID)
558 {
559 /* If they close this blob before verification finishes, that's an
560 * abort.
561 * TODO: implement this, for now just let them close the file.
562 */
563 if (state == UpdateState::verificationStarted)
564 {
565 return false;
566 }
567 }
568
Patrick Venture68bb1432018-11-09 20:08:48 -0800569 if (item->second->dataHandler)
570 {
571 item->second->dataHandler->close();
572 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800573 if (item->second->imageHandler)
574 {
575 item->second->imageHandler->close();
576 }
577
Patrick Venture68bb1432018-11-09 20:08:48 -0800578 item->second->state = Session::State::closed;
579 /* Do not delete the active blob_id from the list of blob_ids, because that
580 * blob_id indicates there is data stored. Delete will destroy it.
581 */
582
583 lookup.erase(item);
584
Patrick Venture991910a2018-11-09 19:43:48 -0800585 fileOpen = false;
586
Patrick Venture68bb1432018-11-09 20:08:48 -0800587 /* TODO: implement other aspects of closing out a session, such as changing
588 * global firmware state.
589 */
590 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700591}
Patrick Ventured6461d62018-11-09 17:30:25 -0800592
Patrick Venturec7ca2912018-11-02 11:38:33 -0700593bool FirmwareBlobHandler::expire(uint16_t session)
594{
595 return false;
596}
Patrick Ventured6461d62018-11-09 17:30:25 -0800597
598/*
599 * Currently, the design does not provide this with a function, however,
600 * it will likely change to support reading data back.
601 */
602std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
603 uint32_t offset,
604 uint32_t requestedSize)
605{
606 return {};
607}
608
Patrick Ventureffcc5502018-11-16 12:32:38 -0800609bool FirmwareBlobHandler::triggerVerification()
610{
Patrick Venturecabc1172018-11-16 16:14:26 -0800611 auto method = bus.new_method_call(systemdService, systemdRoot,
612 systemdInterface, "StartUnit");
613 method.append(verifyTarget);
614 method.append("replace");
615
616 try
617 {
618 bus.call_noreply(method);
Patrick Venture453f06a2019-01-17 10:02:12 -0800619 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800620 }
621 catch (const sdbusplus::exception::SdBusError& ex)
622 {
623 /* TODO: Once logging supports unit-tests, add a log message to test
624 * this failure.
625 */
626 return false;
627 }
628
Patrick Ventureffcc5502018-11-16 12:32:38 -0800629 return true;
630}
631
Patrick Venturec7ca2912018-11-02 11:38:33 -0700632} // namespace blobs