blob: f074bcea2b12b831835042d528d58aacc74e77b4 [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 Venturedb253bf2018-11-09 19:36:03 -0800414 blobIDs.push_back(*active);
415
Patrick Venture991910a2018-11-09 19:43:48 -0800416 fileOpen = true;
417
Patrick Venture18235e62018-11-08 10:21:09 -0800418 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700419}
Patrick Venture53977962018-11-02 18:59:35 -0700420
Patrick Venture18235e62018-11-08 10:21:09 -0800421/**
422 * The write command really just grabs the data from wherever it is and sends it
423 * to the image handler. It's the image handler's responsibility to deal with
424 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800425 *
426 * This receives a session from the blob manager, therefore it is always called
427 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800428 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700429bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
430 const std::vector<uint8_t>& data)
431{
Patrick Venture18235e62018-11-08 10:21:09 -0800432 auto item = lookup.find(session);
433 if (item == lookup.end())
434 {
435 return false;
436 }
437
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800438 /* Prevent writing during verification. */
439 if (state == UpdateState::verificationStarted)
440 {
441 return false;
442 }
443
Patrick Venture18235e62018-11-08 10:21:09 -0800444 std::vector<std::uint8_t> bytes;
445
Patrick Venture05abf7e2018-11-09 11:02:56 -0800446 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800447 {
448 bytes = data;
449 }
450 else
451 {
452 /* little endian required per design, and so on, but TODO: do endianness
453 * with boost.
454 */
455 struct ExtChunkHdr header;
456
457 if (data.size() != sizeof(header))
458 {
459 return false;
460 }
461
462 std::memcpy(&header, data.data(), data.size());
463 bytes = item->second->dataHandler->copyFrom(header.length);
464 }
465
466 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700467}
Patrick Venture18235e62018-11-08 10:21:09 -0800468
Patrick Venture8c535332018-11-08 15:58:00 -0800469/*
470 * If the active session (image or hash) is over LPC, this allows
471 * configuring it. This option is only available before you start
472 * writing data for the given item (image or hash). This will return
473 * false at any other part. -- the lpc handler portion will know to return
474 * false.
475 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700476bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
477 const std::vector<uint8_t>& data)
478{
Patrick Venture8c535332018-11-08 15:58:00 -0800479 auto item = lookup.find(session);
480 if (item == lookup.end())
481 {
482 return false;
483 }
484
Patrick Venture05abf7e2018-11-09 11:02:56 -0800485 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800486 {
487 return false;
488 }
489
Patrick Venture74304642019-01-17 09:31:04 -0800490 return item->second->dataHandler->writeMeta(data);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700491}
Patrick Venture8c535332018-11-08 15:58:00 -0800492
Patrick Ventured6461d62018-11-09 17:30:25 -0800493/*
Patrick Ventureffcc5502018-11-16 12:32:38 -0800494 * If this command is called on the session for the verifyBlobID, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800495 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800496 * the image.
497 *
498 * For this file to have opened, the other two must be closed, which means any
499 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800500 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700501bool FirmwareBlobHandler::commit(uint16_t session,
502 const std::vector<uint8_t>& data)
503{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800504 auto item = lookup.find(session);
505 if (item == lookup.end())
506 {
507 return false;
508 }
509
510 /* You can only commit on the verifyBlodId */
511 if (item->second->activePath != verifyBlobID)
512 {
513 return false;
514 }
515
516 /* Can only be called once per verification. */
517 if (state == UpdateState::verificationStarted)
518 {
519 return false;
520 }
521
522 /* Set state to committing. */
523 item->second->flags |= StateFlags::committing;
524
525 return triggerVerification();
Patrick Venturec7ca2912018-11-02 11:38:33 -0700526}
Patrick Ventured6461d62018-11-09 17:30:25 -0800527
528/*
529 * Close must be called on the firmware image before triggering
530 * verification via commit. Once the verification is complete, you can
531 * then close the hash file.
532 *
533 * If the `verify_image.service` returned success, closing the hash file
534 * will have a specific behavior depending on the update. If it's UBI,
535 * it'll perform the install. If it's static layout, it'll do nothing. The
536 * verify_image service in the static layout case is responsible for placing
537 * the file in the correct staging position.
538 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700539bool FirmwareBlobHandler::close(uint16_t session)
540{
Patrick Venture68bb1432018-11-09 20:08:48 -0800541 auto item = lookup.find(session);
542 if (item == lookup.end())
543 {
544 return false;
545 }
546
Patrick Ventureffcc5502018-11-16 12:32:38 -0800547 /* Are you closing the verify blob? */
548 if (item->second->activePath == verifyBlobID)
549 {
550 /* If they close this blob before verification finishes, that's an
551 * abort.
552 * TODO: implement this, for now just let them close the file.
553 */
554 if (state == UpdateState::verificationStarted)
555 {
556 return false;
557 }
558 }
559
Patrick Venture68bb1432018-11-09 20:08:48 -0800560 if (item->second->dataHandler)
561 {
562 item->second->dataHandler->close();
563 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800564 if (item->second->imageHandler)
565 {
566 item->second->imageHandler->close();
567 }
568
Patrick Venture68bb1432018-11-09 20:08:48 -0800569 item->second->state = Session::State::closed;
570 /* Do not delete the active blob_id from the list of blob_ids, because that
571 * blob_id indicates there is data stored. Delete will destroy it.
572 */
573
574 lookup.erase(item);
575
Patrick Venture991910a2018-11-09 19:43:48 -0800576 fileOpen = false;
577
Patrick Venture68bb1432018-11-09 20:08:48 -0800578 /* TODO: implement other aspects of closing out a session, such as changing
579 * global firmware state.
580 */
581 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700582}
Patrick Ventured6461d62018-11-09 17:30:25 -0800583
Patrick Venturec7ca2912018-11-02 11:38:33 -0700584bool FirmwareBlobHandler::expire(uint16_t session)
585{
586 return false;
587}
Patrick Ventured6461d62018-11-09 17:30:25 -0800588
589/*
590 * Currently, the design does not provide this with a function, however,
591 * it will likely change to support reading data back.
592 */
593std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
594 uint32_t offset,
595 uint32_t requestedSize)
596{
597 return {};
598}
599
Patrick Ventureffcc5502018-11-16 12:32:38 -0800600bool FirmwareBlobHandler::triggerVerification()
601{
Patrick Venturecabc1172018-11-16 16:14:26 -0800602 auto method = bus.new_method_call(systemdService, systemdRoot,
603 systemdInterface, "StartUnit");
604 method.append(verifyTarget);
605 method.append("replace");
606
607 try
608 {
609 bus.call_noreply(method);
Patrick Venture453f06a2019-01-17 10:02:12 -0800610 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800611 }
612 catch (const sdbusplus::exception::SdBusError& ex)
613 {
614 /* TODO: Once logging supports unit-tests, add a log message to test
615 * this failure.
616 */
617 return false;
618 }
619
Patrick Ventureffcc5502018-11-16 12:32:38 -0800620 return true;
621}
622
Patrick Venturec7ca2912018-11-02 11:38:33 -0700623} // namespace blobs