blob: 8daead64524f79595223c60ee439816b7d68fc54 [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 {
Patrick Venture699750d2019-05-15 09:24:09 -0700171 /* TODO: We need to return information for the verify state -- did they
172 * call commit() did things start?
Patrick Ventureffcc5502018-11-16 12:32:38 -0800173 */
174 }
175 else if (path == activeImageBlobID)
Patrick Venture46637c82018-11-06 15:20:24 -0800176 {
Patrick Venture699750d2019-05-15 09:24:09 -0700177 /* TODO: We need to return information for the image that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800178 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800179 else if (path == activeHashBlobID)
Patrick Venture46637c82018-11-06 15:20:24 -0800180 {
Patrick Venture699750d2019-05-15 09:24:09 -0700181 /* TODO: We need to return information for the hash that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800182 }
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;
Patrick Venture699750d2019-05-15 09:24:09 -0700219
220 /* TODO: Implement this for the verification blob, which is what we expect.
221 * Calling stat() on the verify blob without an active session should not
222 * provide insight.
223 */
224
Patrick Venturecc7d1602018-11-15 13:58:33 -0800225 /* The size here refers to the size of the file -- of something analagous.
226 */
227 meta->size = item->second->imageHandler->getSize();
228
229 /* The metadata blob returned comes from the data handler... it's used for
230 * instance, in P2A bridging to get required information about the mapping,
231 * and is the "opposite" of the lpc writemeta requirement.
232 */
233 meta->metadata.clear();
234 if (item->second->dataHandler)
235 {
Patrick Venture74304642019-01-17 09:31:04 -0800236 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800237 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
238 bytes.end());
239 }
240
241 /* TODO: During things like verification, etc, we can report the state as
242 * committed, etc, so we'll need to do that.
243 */
244
245 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800246}
247
248/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800249 * If you open /flash/image or /flash/tarball, or /flash/hash it will
250 * interpret the open flags and perform whatever actions are required for
251 * that update process. The session returned can be used immediately for
252 * sending data down, without requiring one to open the new active file.
253 *
254 * If you open the active flash image or active hash it will let you
255 * overwrite pieces, depending on the state.
256 *
257 * Once the verification process has started the active files cannot be
258 * opened.
259 *
260 * You can only have one open session at a time. Which means, you can only
261 * have one file open at a time. Trying to open the hash blob_id while you
262 * still have the flash image blob_id open will fail. Opening the flash
263 * blob_id when it is already open will fail.
264 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700265bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
266 const std::string& path)
267{
Patrick Venture6e307a72018-11-09 18:21:17 -0800268 /* Check that they've opened for writing - read back not currently
269 * supported.
270 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800271 if ((flags & OpenFlags::write) == 0)
272 {
273 return false;
274 }
275
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800276 /* Is the verification process underway? */
277 if (state == UpdateState::verificationStarted)
278 {
279 return false;
280 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800281
282 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800283 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800284 * TODO: Temporarily using a simple boolean flag until there's a full
285 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800286 *
287 * Further on this, if there's an active session to the hash we don't allow
288 * re-opening the image, and if we have the image open, we don't allow
289 * opening the hash. This design decision may be re-evaluated, and changed
290 * to only allow one session per object type (of the two types). But,
291 * consider if the hash is open, do we want to allow writing to the image?
292 * And why would we? But, really, the point of no-return is once the
293 * verification process has begun -- which is done via commit() on the hash
294 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700295 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800296 if (fileOpen)
297 {
298 return false;
299 }
300
Patrick Ventureffcc5502018-11-16 12:32:38 -0800301 /* Handle opening the verifyBlobId --> we know the image and hash aren't
302 * open because of the fileOpen check.
303 *
304 * The file must be opened for writing, but no transport mechanism specified
305 * since it's irrelevant.
306 */
307 if (path == verifyBlobID)
308 {
309 /* In this case, there's no image handler to use, or data handler,
310 * simply set up a session.
311 */
312 verifyImage.flags = flags;
313 verifyImage.state = Session::State::open;
314
315 lookup[session] = &verifyImage;
316
317 fileOpen = true;
318
319 return true;
320 }
321
Patrick Venturec02849b2018-11-06 17:31:15 -0800322 /* There are two abstractions at play, how you get the data and how you
323 * handle that data. such that, whether the data comes from the PCI bridge
324 * or LPC bridge is not connected to whether the data goes into a static
325 * layout flash update or a UBI tarball.
326 */
327
328 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800329 * support what they request.
330 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800331 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800332 {
333 return false;
334 }
335
336 /* 2) there isn't, so what are they opening? */
337 if (path == activeImageBlobID)
338 {
339 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800340 * already started one (due to canHandleBlob's behavior).
341 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800342 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800343 }
344 else if (path == activeHashBlobID)
345 {
346 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800347 * already started one (due to canHandleBlob's behavior).
348 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800349 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800350 }
Patrick Venture18235e62018-11-08 10:21:09 -0800351
352 /* How are they expecting to copy this data? */
353 auto d = std::find_if(
354 transports.begin(), transports.end(),
355 [&flags](const auto& iter) { return (iter.bitmask & flags); });
356 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800357 {
Patrick Venture18235e62018-11-08 10:21:09 -0800358 return false;
359 }
360
361 /* We found the transport handler they requested, no surprise since
362 * above we verify they selected at least one we wanted.
363 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800364
Patrick Venture6e307a72018-11-09 18:21:17 -0800365 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
366 * only non-external data pathway -- but this is just a more generic
367 * approach to that.
368 */
369 if (d->handler)
370 {
371 /* If the data handler open call fails, open fails. */
372 if (!d->handler->open())
373 {
374 return false;
375 }
376 }
377
Patrick Venture70e30bf2019-01-17 10:29:28 -0800378 /* Do we have a file handler for the type of file they're opening.
379 * Note: This should only fail if something is somehow crazy wrong.
380 * Since the canHandle() said yes, and that's tied into the list of explicit
381 * firmware handers (and file handlers, like this'll know where to write the
382 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800383 */
Patrick Venture18235e62018-11-08 10:21:09 -0800384 auto h = std::find_if(
385 handlers.begin(), handlers.end(),
386 [&path](const auto& iter) { return (iter.blobName == path); });
387 if (h == handlers.end())
388 {
389 return false;
390 }
391
392 /* Ok, so we found a handler that matched, so call open() */
393 if (!h->handler->open(path))
394 {
395 return false;
396 }
397
Patrick Venture70e30bf2019-01-17 10:29:28 -0800398 Session* curr;
399 const std::string* active;
400
401 if (path == hashBlobID)
402 {
403 /* 2c) are they opening the /flash/hash ? (to start the process) */
404 curr = &activeHash;
405 active = &activeHashBlobID;
406 }
407 else
408 {
409 curr = &activeImage;
410 active = &activeImageBlobID;
411 }
412
Patrick Venture18235e62018-11-08 10:21:09 -0800413 curr->flags = flags;
414 curr->dataHandler = d->handler;
415 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800416 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800417
418 lookup[session] = curr;
419
Patrick Venturedaf47072019-05-10 15:21:55 -0700420 /* This may be them re-opening a blob, so let's only push it onto the list
421 * when appropriate.
422 */
423 auto blobIdMatch =
424 std::find_if(blobIDs.begin(), blobIDs.end(),
425 [active](const auto& iter) { return (iter == *active); });
426 if (blobIdMatch == blobIDs.end())
427 {
428 blobIDs.push_back(*active);
429 }
Patrick Venturedb253bf2018-11-09 19:36:03 -0800430
Patrick Venture991910a2018-11-09 19:43:48 -0800431 fileOpen = true;
432
Patrick Venture18235e62018-11-08 10:21:09 -0800433 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700434}
Patrick Venture53977962018-11-02 18:59:35 -0700435
Patrick Venture18235e62018-11-08 10:21:09 -0800436/**
437 * The write command really just grabs the data from wherever it is and sends it
438 * to the image handler. It's the image handler's responsibility to deal with
439 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800440 *
441 * This receives a session from the blob manager, therefore it is always called
442 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800443 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700444bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
445 const std::vector<uint8_t>& data)
446{
Patrick Venture18235e62018-11-08 10:21:09 -0800447 auto item = lookup.find(session);
448 if (item == lookup.end())
449 {
450 return false;
451 }
452
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800453 /* Prevent writing during verification. */
454 if (state == UpdateState::verificationStarted)
455 {
456 return false;
457 }
458
Patrick Venture8af15eb2019-05-15 09:39:22 -0700459 /* Prevent writing to the verification blob before they trigger
Patrick Venture699750d2019-05-15 09:24:09 -0700460 * verification.
461 */
Patrick Venture8af15eb2019-05-15 09:39:22 -0700462 if (item->second->activePath == verifyBlobID)
463 {
464 return false;
465 }
Patrick Venture699750d2019-05-15 09:24:09 -0700466
Patrick Venture18235e62018-11-08 10:21:09 -0800467 std::vector<std::uint8_t> bytes;
468
Patrick Venture05abf7e2018-11-09 11:02:56 -0800469 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800470 {
471 bytes = data;
472 }
473 else
474 {
475 /* little endian required per design, and so on, but TODO: do endianness
476 * with boost.
477 */
478 struct ExtChunkHdr header;
479
480 if (data.size() != sizeof(header))
481 {
482 return false;
483 }
484
485 std::memcpy(&header, data.data(), data.size());
486 bytes = item->second->dataHandler->copyFrom(header.length);
487 }
488
489 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700490}
Patrick Venture18235e62018-11-08 10:21:09 -0800491
Patrick Venture8c535332018-11-08 15:58:00 -0800492/*
493 * If the active session (image or hash) is over LPC, this allows
494 * configuring it. This option is only available before you start
495 * writing data for the given item (image or hash). This will return
496 * false at any other part. -- the lpc handler portion will know to return
497 * false.
498 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700499bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
500 const std::vector<uint8_t>& data)
501{
Patrick Venture8c535332018-11-08 15:58:00 -0800502 auto item = lookup.find(session);
503 if (item == lookup.end())
504 {
505 return false;
506 }
507
Patrick Venture05abf7e2018-11-09 11:02:56 -0800508 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800509 {
510 return false;
511 }
512
Patrick Ventured56097a2019-05-15 09:47:55 -0700513 /* Prevent writing meta to the verification blob (it has no data handler).
514 */
515 if (item->second->dataHandler)
516 {
517 return item->second->dataHandler->writeMeta(data);
518 }
Patrick Venture699750d2019-05-15 09:24:09 -0700519
Patrick Ventured56097a2019-05-15 09:47:55 -0700520 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700521}
Patrick Venture8c535332018-11-08 15:58:00 -0800522
Patrick Ventured6461d62018-11-09 17:30:25 -0800523/*
Patrick Ventureffcc5502018-11-16 12:32:38 -0800524 * If this command is called on the session for the verifyBlobID, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800525 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800526 * the image.
527 *
528 * For this file to have opened, the other two must be closed, which means any
529 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800530 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700531bool FirmwareBlobHandler::commit(uint16_t session,
532 const std::vector<uint8_t>& data)
533{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800534 auto item = lookup.find(session);
535 if (item == lookup.end())
536 {
537 return false;
538 }
539
540 /* You can only commit on the verifyBlodId */
541 if (item->second->activePath != verifyBlobID)
542 {
543 return false;
544 }
545
Patrick Venturebe198702019-05-15 09:46:02 -0700546 /* Calling repeatedly has no effect within an update process. */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800547 if (state == UpdateState::verificationStarted)
548 {
Patrick Venturebe198702019-05-15 09:46:02 -0700549 return true;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800550 }
551
552 /* Set state to committing. */
553 item->second->flags |= StateFlags::committing;
554
555 return triggerVerification();
Patrick Venturec7ca2912018-11-02 11:38:33 -0700556}
Patrick Ventured6461d62018-11-09 17:30:25 -0800557
558/*
559 * Close must be called on the firmware image before triggering
560 * verification via commit. Once the verification is complete, you can
561 * then close the hash file.
562 *
563 * If the `verify_image.service` returned success, closing the hash file
564 * will have a specific behavior depending on the update. If it's UBI,
565 * it'll perform the install. If it's static layout, it'll do nothing. The
566 * verify_image service in the static layout case is responsible for placing
567 * the file in the correct staging position.
568 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700569bool FirmwareBlobHandler::close(uint16_t session)
570{
Patrick Venture68bb1432018-11-09 20:08:48 -0800571 auto item = lookup.find(session);
572 if (item == lookup.end())
573 {
574 return false;
575 }
576
Patrick Ventureffcc5502018-11-16 12:32:38 -0800577 /* Are you closing the verify blob? */
578 if (item->second->activePath == verifyBlobID)
579 {
580 /* If they close this blob before verification finishes, that's an
581 * abort.
582 * TODO: implement this, for now just let them close the file.
583 */
584 if (state == UpdateState::verificationStarted)
585 {
586 return false;
587 }
588 }
589
Patrick Venture68bb1432018-11-09 20:08:48 -0800590 if (item->second->dataHandler)
591 {
592 item->second->dataHandler->close();
593 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800594 if (item->second->imageHandler)
595 {
596 item->second->imageHandler->close();
597 }
598
Patrick Venture68bb1432018-11-09 20:08:48 -0800599 item->second->state = Session::State::closed;
600 /* Do not delete the active blob_id from the list of blob_ids, because that
601 * blob_id indicates there is data stored. Delete will destroy it.
602 */
603
604 lookup.erase(item);
605
Patrick Venture991910a2018-11-09 19:43:48 -0800606 fileOpen = false;
607
Patrick Venture68bb1432018-11-09 20:08:48 -0800608 /* TODO: implement other aspects of closing out a session, such as changing
609 * global firmware state.
610 */
611 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700612}
Patrick Ventured6461d62018-11-09 17:30:25 -0800613
Patrick Venturec7ca2912018-11-02 11:38:33 -0700614bool FirmwareBlobHandler::expire(uint16_t session)
615{
616 return false;
617}
Patrick Ventured6461d62018-11-09 17:30:25 -0800618
619/*
620 * Currently, the design does not provide this with a function, however,
621 * it will likely change to support reading data back.
622 */
623std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
624 uint32_t offset,
625 uint32_t requestedSize)
626{
627 return {};
628}
629
Patrick Ventureffcc5502018-11-16 12:32:38 -0800630bool FirmwareBlobHandler::triggerVerification()
631{
Patrick Venturecabc1172018-11-16 16:14:26 -0800632 auto method = bus.new_method_call(systemdService, systemdRoot,
633 systemdInterface, "StartUnit");
634 method.append(verifyTarget);
635 method.append("replace");
636
637 try
638 {
639 bus.call_noreply(method);
Patrick Venture453f06a2019-01-17 10:02:12 -0800640 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800641 }
642 catch (const sdbusplus::exception::SdBusError& ex)
643 {
644 /* TODO: Once logging supports unit-tests, add a log message to test
645 * this failure.
646 */
647 return false;
648 }
649
Patrick Ventureffcc5502018-11-16 12:32:38 -0800650 return true;
651}
652
Patrick Venturec7ca2912018-11-02 11:38:33 -0700653} // namespace blobs