blob: 03cf1c22a7d877e4993df1f51817b4e34445cdd6 [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 Venturefa6c4d92018-11-02 18:34:53 -070025#include <string>
26#include <vector>
27
Patrick Venturec7ca2912018-11-02 11:38:33 -070028namespace blobs
29{
Patrick Venturecabc1172018-11-16 16:14:26 -080030// systemd service to kick start a target.
31static constexpr auto systemdService = "org.freedesktop.systemd1";
32static constexpr auto systemdRoot = "/org/freedesktop/systemd1";
33static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
34static constexpr auto verifyTarget = "verify_image.service";
Patrick Venturec7ca2912018-11-02 11:38:33 -070035
Patrick Ventureffcc5502018-11-16 12:32:38 -080036const std::string FirmwareBlobHandler::verifyBlobID = "/flash/verify";
Patrick Venture21be45a2018-11-06 12:08:52 -080037const std::string FirmwareBlobHandler::hashBlobID = "/flash/hash";
Patrick Venture7b9256f2018-11-06 15:06:04 -080038const std::string FirmwareBlobHandler::activeImageBlobID =
39 "/flash/active/image";
40const std::string FirmwareBlobHandler::activeHashBlobID = "/flash/active/hash";
Patrick Venture4cceb8e2018-11-06 11:56:48 -080041
Patrick Venture68cf64f2018-11-06 10:46:51 -080042std::unique_ptr<GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080043 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture4eb55952018-11-16 15:36:24 -080044 sdbusplus::bus::bus&& bus, const std::vector<HandlerPack>& firmwares,
Patrick Venture1cde5f92018-11-07 08:26:47 -080045 const std::vector<DataHandlerPack>& transports)
Patrick Venture68cf64f2018-11-06 10:46:51 -080046{
Patrick Venture52854622018-11-06 12:30:00 -080047 /* There must be at least one. */
48 if (!firmwares.size())
49 {
50 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 Ventureffcc5502018-11-16 12:32:38 -080062 blobs.push_back(verifyBlobID); /* Add blob_id to always exist. */
Patrick Venture18235e62018-11-08 10:21:09 -080063
64 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobID))
65 {
66 return nullptr;
67 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080068
Patrick Venture1cde5f92018-11-07 08:26:47 -080069 std::uint16_t bitmask = 0;
70 for (const auto& item : transports)
71 {
72 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
73 bitmask |= item.bitmask;
74 }
75
Patrick Venture4eb55952018-11-16 15:36:24 -080076 return std::make_unique<FirmwareBlobHandler>(std::move(bus), firmwares,
77 blobs, transports, bitmask);
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 Venture4cceb8e2018-11-06 11:56:48 -080083 if (std::count(blobIDs.begin(), blobIDs.end(), path))
Patrick Venture137ad2c2018-11-06 11:30:43 -080084 {
85 return true;
86 }
87
Patrick Venturec7ca2912018-11-02 11:38:33 -070088 return false;
89}
Patrick Venture53977962018-11-02 18:59:35 -070090
Patrick Ventured6461d62018-11-09 17:30:25 -080091/*
92 * Grab the list of supported firmware.
93 *
94 * If there's an open firmware session, it'll already be present in the
95 * list as "/flash/active/image", and if the hash has started,
96 * "/flash/active/hash" regardless of mechanism. This is done in the open
97 * comamnd, no extra work is required here.
98 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070099std::vector<std::string> FirmwareBlobHandler::getBlobIds()
100{
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800101 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700102}
Patrick Venture53977962018-11-02 18:59:35 -0700103
Patrick Ventured6461d62018-11-09 17:30:25 -0800104/*
105 * Per the design, this mean abort, and this will trigger whatever
106 * appropriate actions are required to abort the process.
Patrick Venture9e5aab22018-11-09 20:49:28 -0800107 *
108 * You cannot delete a blob that has an open handle in the system, therefore
109 * this is never called if there's an open session. Guaranteed by the blob
110 * manager.
Patrick Ventured6461d62018-11-09 17:30:25 -0800111 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700112bool FirmwareBlobHandler::deleteBlob(const std::string& path)
113{
Patrick Venture9e5aab22018-11-09 20:49:28 -0800114 const std::string* toDelete;
115
Patrick Ventureffcc5502018-11-16 12:32:38 -0800116 /* You cannot delete the verify blob -- trying to delete it, currently has
117 * no impact.
118 * TODO: Should trying to delete this cause an abort?
119 */
120 if (path == verifyBlobID)
121 {
122 return false;
123 }
124
Patrick Venture9e5aab22018-11-09 20:49:28 -0800125 if (path == hashBlobID || path == activeHashBlobID)
126 {
127 /* They're deleting the hash. */
128 toDelete = &activeHashBlobID;
129 }
130 else
131 {
132 /* They're deleting the image. */
133 toDelete = &activeImageBlobID;
134 }
135
136 auto it = std::find_if(
137 blobIDs.begin(), blobIDs.end(),
138 [toDelete](const auto& iter) { return (iter == *toDelete); });
139 if (it == blobIDs.end())
140 {
141 /* Somehow they've asked to delete something we didn't say we could
142 * handle.
143 */
144 return false;
145 }
146
147 blobIDs.erase(it);
148
149 /* TODO: Handle aborting the process and fixing up the state. */
150
151 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700152}
Patrick Venture53977962018-11-02 18:59:35 -0700153
Patrick Ventured6461d62018-11-09 17:30:25 -0800154/*
155 * Stat on the files will return information such as what supported
156 * transport mechanisms are available.
157 *
158 * Stat on an active file or hash will return information such as the size
159 * of the data cached, and any additional pertinent information. The
160 * blob_state on the active files will return the state of the update.
161 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700162bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
163{
Patrick Venture46637c82018-11-06 15:20:24 -0800164 /* We know we support this path because canHandle is called ahead */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800165 if (path == verifyBlobID)
166 {
167 /* We need to return information for the verify state -- did they call
168 * commit() did things start?
169 */
170 }
171 else if (path == activeImageBlobID)
Patrick Venture46637c82018-11-06 15:20:24 -0800172 {
173 /* We need to return information for the image that's staged. */
174 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800175 else if (path == activeHashBlobID)
Patrick Venture46637c82018-11-06 15:20:24 -0800176 {
177 /* We need to return information for the hash that's staged. */
178 }
179 else
180 {
181 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800182 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800183 meta->size = 0;
184
185 /* The generic blob_ids state is only the bits related to the transport
Patrick Ventured6461d62018-11-09 17:30:25 -0800186 * mechanisms.
187 */
Patrick Venture46637c82018-11-06 15:20:24 -0800188 return true;
189 }
190
Patrick Venturec7ca2912018-11-02 11:38:33 -0700191 return false;
192}
Patrick Venture53977962018-11-02 18:59:35 -0700193
Patrick Venturec02849b2018-11-06 17:31:15 -0800194/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800195 * Return stat information on an open session. It therefore must be an active
196 * handle to either the active image or active hash.
197 *
198 * The stat() and sessionstat() commands will return the same information in
199 * many cases, therefore the logic will be combined.
200 *
201 * TODO: combine the logic for stat and sessionstat().
202 */
203bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
204{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800205 auto item = lookup.find(session);
206 if (item == lookup.end())
207 {
208 return false;
209 }
210
211 /* The blobState here relates to an active sesion, so we should return the
212 * flags used to open this session.
Patrick Ventured6461d62018-11-09 17:30:25 -0800213 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800214 meta->blobState = item->second->flags;
215 /* The size here refers to the size of the file -- of something analagous.
216 */
217 meta->size = item->second->imageHandler->getSize();
218
219 /* The metadata blob returned comes from the data handler... it's used for
220 * instance, in P2A bridging to get required information about the mapping,
221 * and is the "opposite" of the lpc writemeta requirement.
222 */
223 meta->metadata.clear();
224 if (item->second->dataHandler)
225 {
226 auto bytes = item->second->dataHandler->read();
227 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
228 bytes.end());
229 }
230
231 /* TODO: During things like verification, etc, we can report the state as
232 * committed, etc, so we'll need to do that.
233 */
234
235 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800236}
237
238/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800239 * If you open /flash/image or /flash/tarball, or /flash/hash it will
240 * interpret the open flags and perform whatever actions are required for
241 * that update process. The session returned can be used immediately for
242 * sending data down, without requiring one to open the new active file.
243 *
244 * If you open the active flash image or active hash it will let you
245 * overwrite pieces, depending on the state.
246 *
247 * Once the verification process has started the active files cannot be
248 * opened.
249 *
250 * You can only have one open session at a time. Which means, you can only
251 * have one file open at a time. Trying to open the hash blob_id while you
252 * still have the flash image blob_id open will fail. Opening the flash
253 * blob_id when it is already open will fail.
254 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700255bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
256 const std::string& path)
257{
Patrick Venture6e307a72018-11-09 18:21:17 -0800258 /* Check that they've opened for writing - read back not currently
259 * supported.
260 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800261 if ((flags & OpenFlags::write) == 0)
262 {
263 return false;
264 }
265
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800266 /* Is the verification process underway? */
267 if (state == UpdateState::verificationStarted)
268 {
269 return false;
270 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800271
272 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800273 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800274 * TODO: Temporarily using a simple boolean flag until there's a full
275 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800276 *
277 * Further on this, if there's an active session to the hash we don't allow
278 * re-opening the image, and if we have the image open, we don't allow
279 * opening the hash. This design decision may be re-evaluated, and changed
280 * to only allow one session per object type (of the two types). But,
281 * consider if the hash is open, do we want to allow writing to the image?
282 * And why would we? But, really, the point of no-return is once the
283 * verification process has begun -- which is done via commit() on the hash
284 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700285 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800286 if (fileOpen)
287 {
288 return false;
289 }
290
Patrick Ventureffcc5502018-11-16 12:32:38 -0800291 /* Handle opening the verifyBlobId --> we know the image and hash aren't
292 * open because of the fileOpen check.
293 *
294 * The file must be opened for writing, but no transport mechanism specified
295 * since it's irrelevant.
296 */
297 if (path == verifyBlobID)
298 {
299 /* In this case, there's no image handler to use, or data handler,
300 * simply set up a session.
301 */
302 verifyImage.flags = flags;
303 verifyImage.state = Session::State::open;
304
305 lookup[session] = &verifyImage;
306
307 fileOpen = true;
308
309 return true;
310 }
311
Patrick Venturec02849b2018-11-06 17:31:15 -0800312 /* There are two abstractions at play, how you get the data and how you
313 * handle that data. such that, whether the data comes from the PCI bridge
314 * or LPC bridge is not connected to whether the data goes into a static
315 * layout flash update or a UBI tarball.
316 */
317
318 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800319 * support what they request.
320 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800321 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800322 {
323 return false;
324 }
325
326 /* 2) there isn't, so what are they opening? */
327 if (path == activeImageBlobID)
328 {
329 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800330 * already started one (due to canHandleBlob's behavior).
331 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800332 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800333 }
334 else if (path == activeHashBlobID)
335 {
336 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800337 * already started one (due to canHandleBlob's behavior).
338 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800339 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800340 }
Patrick Venture18235e62018-11-08 10:21:09 -0800341
342 /* How are they expecting to copy this data? */
343 auto d = std::find_if(
344 transports.begin(), transports.end(),
345 [&flags](const auto& iter) { return (iter.bitmask & flags); });
346 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800347 {
Patrick Venture18235e62018-11-08 10:21:09 -0800348 return false;
349 }
350
351 /* We found the transport handler they requested, no surprise since
352 * above we verify they selected at least one we wanted.
353 */
354 Session* curr;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800355 const std::string* active;
Patrick Venture18235e62018-11-08 10:21:09 -0800356
357 if (path == hashBlobID)
358 {
Patrick Venturec02849b2018-11-06 17:31:15 -0800359 /* 2c) are they opening the /flash/hash ? (to start the process) */
Patrick Venture21c62c12018-11-09 17:46:58 -0800360 curr = &activeHash;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800361 active = &activeHashBlobID;
Patrick Venturec02849b2018-11-06 17:31:15 -0800362 }
363 else
364 {
Patrick Venture18235e62018-11-08 10:21:09 -0800365 curr = &activeImage;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800366 active = &activeImageBlobID;
Patrick Venturec02849b2018-11-06 17:31:15 -0800367 }
368
Patrick Venture6e307a72018-11-09 18:21:17 -0800369 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
370 * only non-external data pathway -- but this is just a more generic
371 * approach to that.
372 */
373 if (d->handler)
374 {
375 /* If the data handler open call fails, open fails. */
376 if (!d->handler->open())
377 {
378 return false;
379 }
380 }
381
Patrick Venture18235e62018-11-08 10:21:09 -0800382 /* 2d) are they opening the /flash/tarball ? (to start the UBI process)
Patrick Ventured6461d62018-11-09 17:30:25 -0800383 * 2e) are they opening the /flash/image ? (to start the process)
384 * 2...) are they opening the /flash/... ? (to start the process)
Patrick Venture18235e62018-11-08 10:21:09 -0800385 */
Patrick Venture18235e62018-11-08 10:21:09 -0800386 auto h = std::find_if(
387 handlers.begin(), handlers.end(),
388 [&path](const auto& iter) { return (iter.blobName == path); });
389 if (h == handlers.end())
390 {
391 return false;
392 }
393
394 /* Ok, so we found a handler that matched, so call open() */
395 if (!h->handler->open(path))
396 {
397 return false;
398 }
399
400 curr->flags = flags;
401 curr->dataHandler = d->handler;
402 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800403 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800404
405 lookup[session] = curr;
406
Patrick Venturedb253bf2018-11-09 19:36:03 -0800407 blobIDs.push_back(*active);
408
Patrick Venture991910a2018-11-09 19:43:48 -0800409 fileOpen = true;
410
Patrick Venture18235e62018-11-08 10:21:09 -0800411 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700412}
Patrick Venture53977962018-11-02 18:59:35 -0700413
Patrick Venture18235e62018-11-08 10:21:09 -0800414/**
415 * The write command really just grabs the data from wherever it is and sends it
416 * to the image handler. It's the image handler's responsibility to deal with
417 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800418 *
419 * This receives a session from the blob manager, therefore it is always called
420 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800421 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700422bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
423 const std::vector<uint8_t>& data)
424{
Patrick Venture18235e62018-11-08 10:21:09 -0800425 auto item = lookup.find(session);
426 if (item == lookup.end())
427 {
428 return false;
429 }
430
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800431 /* Prevent writing during verification. */
432 if (state == UpdateState::verificationStarted)
433 {
434 return false;
435 }
436
Patrick Venture18235e62018-11-08 10:21:09 -0800437 std::vector<std::uint8_t> bytes;
438
Patrick Venture05abf7e2018-11-09 11:02:56 -0800439 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800440 {
441 bytes = data;
442 }
443 else
444 {
445 /* little endian required per design, and so on, but TODO: do endianness
446 * with boost.
447 */
448 struct ExtChunkHdr header;
449
450 if (data.size() != sizeof(header))
451 {
452 return false;
453 }
454
455 std::memcpy(&header, data.data(), data.size());
456 bytes = item->second->dataHandler->copyFrom(header.length);
457 }
458
459 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700460}
Patrick Venture18235e62018-11-08 10:21:09 -0800461
Patrick Venture8c535332018-11-08 15:58:00 -0800462/*
463 * If the active session (image or hash) is over LPC, this allows
464 * configuring it. This option is only available before you start
465 * writing data for the given item (image or hash). This will return
466 * false at any other part. -- the lpc handler portion will know to return
467 * false.
468 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700469bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
470 const std::vector<uint8_t>& data)
471{
Patrick Venture8c535332018-11-08 15:58:00 -0800472 auto item = lookup.find(session);
473 if (item == lookup.end())
474 {
475 return false;
476 }
477
Patrick Venture05abf7e2018-11-09 11:02:56 -0800478 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800479 {
480 return false;
481 }
482
483 return item->second->dataHandler->write(data);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700484}
Patrick Venture8c535332018-11-08 15:58:00 -0800485
Patrick Ventured6461d62018-11-09 17:30:25 -0800486/*
Patrick Ventureffcc5502018-11-16 12:32:38 -0800487 * If this command is called on the session for the verifyBlobID, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800488 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800489 * the image.
490 *
491 * For this file to have opened, the other two must be closed, which means any
492 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800493 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700494bool FirmwareBlobHandler::commit(uint16_t session,
495 const std::vector<uint8_t>& data)
496{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800497 auto item = lookup.find(session);
498 if (item == lookup.end())
499 {
500 return false;
501 }
502
503 /* You can only commit on the verifyBlodId */
504 if (item->second->activePath != verifyBlobID)
505 {
506 return false;
507 }
508
509 /* Can only be called once per verification. */
510 if (state == UpdateState::verificationStarted)
511 {
512 return false;
513 }
514
515 /* Set state to committing. */
516 item->second->flags |= StateFlags::committing;
517
518 return triggerVerification();
Patrick Venturec7ca2912018-11-02 11:38:33 -0700519}
Patrick Ventured6461d62018-11-09 17:30:25 -0800520
521/*
522 * Close must be called on the firmware image before triggering
523 * verification via commit. Once the verification is complete, you can
524 * then close the hash file.
525 *
526 * If the `verify_image.service` returned success, closing the hash file
527 * will have a specific behavior depending on the update. If it's UBI,
528 * it'll perform the install. If it's static layout, it'll do nothing. The
529 * verify_image service in the static layout case is responsible for placing
530 * the file in the correct staging position.
531 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700532bool FirmwareBlobHandler::close(uint16_t session)
533{
Patrick Venture68bb1432018-11-09 20:08:48 -0800534 auto item = lookup.find(session);
535 if (item == lookup.end())
536 {
537 return false;
538 }
539
Patrick Ventureffcc5502018-11-16 12:32:38 -0800540 /* Are you closing the verify blob? */
541 if (item->second->activePath == verifyBlobID)
542 {
543 /* If they close this blob before verification finishes, that's an
544 * abort.
545 * TODO: implement this, for now just let them close the file.
546 */
547 if (state == UpdateState::verificationStarted)
548 {
549 return false;
550 }
551 }
552
Patrick Venture68bb1432018-11-09 20:08:48 -0800553 if (item->second->dataHandler)
554 {
555 item->second->dataHandler->close();
556 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800557 if (item->second->imageHandler)
558 {
559 item->second->imageHandler->close();
560 }
561
Patrick Venture68bb1432018-11-09 20:08:48 -0800562 item->second->state = Session::State::closed;
563 /* Do not delete the active blob_id from the list of blob_ids, because that
564 * blob_id indicates there is data stored. Delete will destroy it.
565 */
566
567 lookup.erase(item);
568
Patrick Venture991910a2018-11-09 19:43:48 -0800569 fileOpen = false;
570
Patrick Venture68bb1432018-11-09 20:08:48 -0800571 /* TODO: implement other aspects of closing out a session, such as changing
572 * global firmware state.
573 */
574 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700575}
Patrick Ventured6461d62018-11-09 17:30:25 -0800576
Patrick Venturec7ca2912018-11-02 11:38:33 -0700577bool FirmwareBlobHandler::expire(uint16_t session)
578{
579 return false;
580}
Patrick Ventured6461d62018-11-09 17:30:25 -0800581
582/*
583 * Currently, the design does not provide this with a function, however,
584 * it will likely change to support reading data back.
585 */
586std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
587 uint32_t offset,
588 uint32_t requestedSize)
589{
590 return {};
591}
592
Patrick Ventureffcc5502018-11-16 12:32:38 -0800593bool FirmwareBlobHandler::triggerVerification()
594{
Patrick Venturecabc1172018-11-16 16:14:26 -0800595 auto method = bus.new_method_call(systemdService, systemdRoot,
596 systemdInterface, "StartUnit");
597 method.append(verifyTarget);
598 method.append("replace");
599
600 try
601 {
602 bus.call_noreply(method);
603 }
604 catch (const sdbusplus::exception::SdBusError& ex)
605 {
606 /* TODO: Once logging supports unit-tests, add a log message to test
607 * this failure.
608 */
609 return false;
610 }
611
Patrick Ventureffcc5502018-11-16 12:32:38 -0800612 state = UpdateState::verificationStarted;
613
614 /* TODO: implement this. */
615 return true;
616}
617
Patrick Venturec7ca2912018-11-02 11:38:33 -0700618} // namespace blobs