blob: a07ddbb76de0d0f1e449363aa1af5ccbc5c12347 [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 Ventureb3b4db72019-05-15 11:30:24 -070024#include <fstream>
Patrick Venture68cf64f2018-11-06 10:46:51 -080025#include <memory>
Patrick Ventured333a872018-12-03 16:24:26 -080026#include <phosphor-logging/log.hpp>
Patrick Venturefa6c4d92018-11-02 18:34:53 -070027#include <string>
28#include <vector>
29
Patrick Ventured333a872018-12-03 16:24:26 -080030using namespace phosphor::logging;
31
Patrick Venturec7ca2912018-11-02 11:38:33 -070032namespace blobs
33{
Patrick Venturecabc1172018-11-16 16:14:26 -080034// systemd service to kick start a target.
35static constexpr auto systemdService = "org.freedesktop.systemd1";
36static constexpr auto systemdRoot = "/org/freedesktop/systemd1";
37static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
38static constexpr auto verifyTarget = "verify_image.service";
Patrick Ventureb3b4db72019-05-15 11:30:24 -070039static constexpr auto statusPath = "/tmp/bmc.verify";
Patrick Venturec7ca2912018-11-02 11:38:33 -070040
Patrick Ventureffcc5502018-11-16 12:32:38 -080041const std::string FirmwareBlobHandler::verifyBlobID = "/flash/verify";
Patrick Venture21be45a2018-11-06 12:08:52 -080042const std::string FirmwareBlobHandler::hashBlobID = "/flash/hash";
Patrick Venture7b9256f2018-11-06 15:06:04 -080043const std::string FirmwareBlobHandler::activeImageBlobID =
44 "/flash/active/image";
45const std::string FirmwareBlobHandler::activeHashBlobID = "/flash/active/hash";
Patrick Venture4cceb8e2018-11-06 11:56:48 -080046
Patrick Ventureb3b4db72019-05-15 11:30:24 -070047namespace
48{
49
50FirmwareBlobHandler::VerifyCheckResponses checkVerificationState()
51{
52 FirmwareBlobHandler::VerifyCheckResponses result =
53 FirmwareBlobHandler::VerifyCheckResponses::other;
54
55 std::ifstream ifs;
56 ifs.open(statusPath);
57 if (ifs.good())
58 {
59 /*
60 * Check for the contents of the file, accepting:
61 * running, success, or failed.
62 */
63 std::string status;
64 ifs >> status;
65 if (status == "running")
66 {
67 result = FirmwareBlobHandler::VerifyCheckResponses::running;
68 }
69 else if (status == "success")
70 {
71 result = FirmwareBlobHandler::VerifyCheckResponses::success;
72 }
73 else if (status == "failed")
74 {
75 result = FirmwareBlobHandler::VerifyCheckResponses::failed;
76 }
77 }
78
79 return result;
80}
81
82} // namespace
83
Patrick Venture68cf64f2018-11-06 10:46:51 -080084std::unique_ptr<GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080085 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture4eb55952018-11-16 15:36:24 -080086 sdbusplus::bus::bus&& bus, const std::vector<HandlerPack>& firmwares,
Patrick Venture1cde5f92018-11-07 08:26:47 -080087 const std::vector<DataHandlerPack>& transports)
Patrick Venture68cf64f2018-11-06 10:46:51 -080088{
Patrick Venture52854622018-11-06 12:30:00 -080089 /* There must be at least one. */
90 if (!firmwares.size())
91 {
Patrick Ventured333a872018-12-03 16:24:26 -080092 log<level::ERR>("Must provide at least one firmware handler.");
Patrick Venture52854622018-11-06 12:30:00 -080093 return nullptr;
94 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080095 if (!transports.size())
96 {
97 return nullptr;
98 }
Patrick Venture52854622018-11-06 12:30:00 -080099
Patrick Venturea78e39f2018-11-06 18:37:06 -0800100 std::vector<std::string> blobs;
101 for (const auto& item : firmwares)
102 {
103 blobs.push_back(item.blobName);
104 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800105 blobs.push_back(verifyBlobID); /* Add blob_id to always exist. */
Patrick Venture18235e62018-11-08 10:21:09 -0800106
107 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobID))
108 {
109 return nullptr;
110 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800111
Patrick Venture1cde5f92018-11-07 08:26:47 -0800112 std::uint16_t bitmask = 0;
113 for (const auto& item : transports)
114 {
115 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
116 bitmask |= item.bitmask;
117 }
118
Patrick Venture4eb55952018-11-16 15:36:24 -0800119 return std::make_unique<FirmwareBlobHandler>(std::move(bus), firmwares,
120 blobs, transports, bitmask);
Patrick Venture68cf64f2018-11-06 10:46:51 -0800121}
122
Patrick Ventured6461d62018-11-09 17:30:25 -0800123/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700124bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
125{
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800126 if (std::count(blobIDs.begin(), blobIDs.end(), path))
Patrick Venture137ad2c2018-11-06 11:30:43 -0800127 {
128 return true;
129 }
130
Patrick Venturec7ca2912018-11-02 11:38:33 -0700131 return false;
132}
Patrick Venture53977962018-11-02 18:59:35 -0700133
Patrick Ventured6461d62018-11-09 17:30:25 -0800134/*
135 * Grab the list of supported firmware.
136 *
137 * If there's an open firmware session, it'll already be present in the
138 * list as "/flash/active/image", and if the hash has started,
139 * "/flash/active/hash" regardless of mechanism. This is done in the open
140 * comamnd, no extra work is required here.
141 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700142std::vector<std::string> FirmwareBlobHandler::getBlobIds()
143{
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800144 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700145}
Patrick Venture53977962018-11-02 18:59:35 -0700146
Patrick Ventured6461d62018-11-09 17:30:25 -0800147/*
148 * Per the design, this mean abort, and this will trigger whatever
149 * appropriate actions are required to abort the process.
Patrick Venture9e5aab22018-11-09 20:49:28 -0800150 *
151 * You cannot delete a blob that has an open handle in the system, therefore
152 * this is never called if there's an open session. Guaranteed by the blob
153 * manager.
Patrick Ventured6461d62018-11-09 17:30:25 -0800154 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700155bool FirmwareBlobHandler::deleteBlob(const std::string& path)
156{
Patrick Venture9e5aab22018-11-09 20:49:28 -0800157 const std::string* toDelete;
158
Patrick Ventureffcc5502018-11-16 12:32:38 -0800159 /* You cannot delete the verify blob -- trying to delete it, currently has
160 * no impact.
161 * TODO: Should trying to delete this cause an abort?
162 */
163 if (path == verifyBlobID)
164 {
165 return false;
166 }
167
Patrick Venture9e5aab22018-11-09 20:49:28 -0800168 if (path == hashBlobID || path == activeHashBlobID)
169 {
170 /* They're deleting the hash. */
171 toDelete = &activeHashBlobID;
172 }
173 else
174 {
175 /* They're deleting the image. */
176 toDelete = &activeImageBlobID;
177 }
178
179 auto it = std::find_if(
180 blobIDs.begin(), blobIDs.end(),
181 [toDelete](const auto& iter) { return (iter == *toDelete); });
182 if (it == blobIDs.end())
183 {
184 /* Somehow they've asked to delete something we didn't say we could
185 * handle.
186 */
187 return false;
188 }
189
190 blobIDs.erase(it);
191
192 /* TODO: Handle aborting the process and fixing up the state. */
193
194 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700195}
Patrick Venture53977962018-11-02 18:59:35 -0700196
Patrick Ventured6461d62018-11-09 17:30:25 -0800197/*
198 * Stat on the files will return information such as what supported
199 * transport mechanisms are available.
200 *
201 * Stat on an active file or hash will return information such as the size
202 * of the data cached, and any additional pertinent information. The
203 * blob_state on the active files will return the state of the update.
204 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700205bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
206{
Patrick Venture46637c82018-11-06 15:20:24 -0800207 /* We know we support this path because canHandle is called ahead */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800208 if (path == verifyBlobID)
209 {
Patrick Venture699750d2019-05-15 09:24:09 -0700210 /* TODO: We need to return information for the verify state -- did they
211 * call commit() did things start?
Patrick Ventureffcc5502018-11-16 12:32:38 -0800212 */
213 }
214 else if (path == activeImageBlobID)
Patrick Venture46637c82018-11-06 15:20:24 -0800215 {
Patrick Venture699750d2019-05-15 09:24:09 -0700216 /* TODO: We need to return information for the image that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800217 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800218 else if (path == activeHashBlobID)
Patrick Venture46637c82018-11-06 15:20:24 -0800219 {
Patrick Venture699750d2019-05-15 09:24:09 -0700220 /* TODO: We need to return information for the hash that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800221 }
222 else
223 {
224 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800225 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800226 meta->size = 0;
227
228 /* The generic blob_ids state is only the bits related to the transport
Patrick Ventured6461d62018-11-09 17:30:25 -0800229 * mechanisms.
230 */
Patrick Venture46637c82018-11-06 15:20:24 -0800231 return true;
232 }
233
Patrick Venturec7ca2912018-11-02 11:38:33 -0700234 return false;
235}
Patrick Venture53977962018-11-02 18:59:35 -0700236
Patrick Venturec02849b2018-11-06 17:31:15 -0800237/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800238 * Return stat information on an open session. It therefore must be an active
239 * handle to either the active image or active hash.
240 *
241 * The stat() and sessionstat() commands will return the same information in
242 * many cases, therefore the logic will be combined.
243 *
244 * TODO: combine the logic for stat and sessionstat().
245 */
246bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
247{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800248 auto item = lookup.find(session);
249 if (item == lookup.end())
250 {
251 return false;
252 }
253
254 /* The blobState here relates to an active sesion, so we should return the
255 * flags used to open this session.
Patrick Ventured6461d62018-11-09 17:30:25 -0800256 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800257 meta->blobState = item->second->flags;
Patrick Venture699750d2019-05-15 09:24:09 -0700258
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700259 /* The size here refers to the size of the file -- of something analagous.
260 */
261 meta->size = (item->second->imageHandler)
262 ? item->second->imageHandler->getSize()
263 : 0;
264
265 meta->metadata.clear();
266
Patrick Venture699750d2019-05-15 09:24:09 -0700267 /* TODO: Implement this for the verification blob, which is what we expect.
268 * Calling stat() on the verify blob without an active session should not
269 * provide insight.
270 */
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700271 if (item->second->activePath == verifyBlobID)
272 {
273 meta->metadata.push_back(
274 static_cast<std::uint8_t>(checkVerificationState()));
Patrick Venture699750d2019-05-15 09:24:09 -0700275
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700276 return true;
277 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800278
279 /* The metadata blob returned comes from the data handler... it's used for
280 * instance, in P2A bridging to get required information about the mapping,
281 * and is the "opposite" of the lpc writemeta requirement.
282 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800283 if (item->second->dataHandler)
284 {
Patrick Venture74304642019-01-17 09:31:04 -0800285 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800286 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
287 bytes.end());
288 }
289
Patrick Venturecc7d1602018-11-15 13:58:33 -0800290 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800291}
292
293/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800294 * If you open /flash/image or /flash/tarball, or /flash/hash it will
295 * interpret the open flags and perform whatever actions are required for
296 * that update process. The session returned can be used immediately for
297 * sending data down, without requiring one to open the new active file.
298 *
299 * If you open the active flash image or active hash it will let you
300 * overwrite pieces, depending on the state.
301 *
302 * Once the verification process has started the active files cannot be
303 * opened.
304 *
305 * You can only have one open session at a time. Which means, you can only
306 * have one file open at a time. Trying to open the hash blob_id while you
307 * still have the flash image blob_id open will fail. Opening the flash
308 * blob_id when it is already open will fail.
309 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700310bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
311 const std::string& path)
312{
Patrick Venture6e307a72018-11-09 18:21:17 -0800313 /* Check that they've opened for writing - read back not currently
314 * supported.
315 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800316 if ((flags & OpenFlags::write) == 0)
317 {
318 return false;
319 }
320
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800321 /* Is the verification process underway? */
322 if (state == UpdateState::verificationStarted)
323 {
324 return false;
325 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800326
327 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800328 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800329 * TODO: Temporarily using a simple boolean flag until there's a full
330 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800331 *
332 * Further on this, if there's an active session to the hash we don't allow
333 * re-opening the image, and if we have the image open, we don't allow
334 * opening the hash. This design decision may be re-evaluated, and changed
335 * to only allow one session per object type (of the two types). But,
336 * consider if the hash is open, do we want to allow writing to the image?
337 * And why would we? But, really, the point of no-return is once the
338 * verification process has begun -- which is done via commit() on the hash
339 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700340 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800341 if (fileOpen)
342 {
343 return false;
344 }
345
Patrick Ventureffcc5502018-11-16 12:32:38 -0800346 /* Handle opening the verifyBlobId --> we know the image and hash aren't
347 * open because of the fileOpen check.
348 *
349 * The file must be opened for writing, but no transport mechanism specified
350 * since it's irrelevant.
351 */
352 if (path == verifyBlobID)
353 {
354 /* In this case, there's no image handler to use, or data handler,
355 * simply set up a session.
356 */
357 verifyImage.flags = flags;
358 verifyImage.state = Session::State::open;
359
360 lookup[session] = &verifyImage;
361
362 fileOpen = true;
363
364 return true;
365 }
366
Patrick Venturec02849b2018-11-06 17:31:15 -0800367 /* There are two abstractions at play, how you get the data and how you
368 * handle that data. such that, whether the data comes from the PCI bridge
369 * or LPC bridge is not connected to whether the data goes into a static
370 * layout flash update or a UBI tarball.
371 */
372
373 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800374 * support what they request.
375 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800376 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800377 {
378 return false;
379 }
380
381 /* 2) there isn't, so what are they opening? */
382 if (path == activeImageBlobID)
383 {
384 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800385 * already started one (due to canHandleBlob's behavior).
386 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800387 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800388 }
389 else if (path == activeHashBlobID)
390 {
391 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800392 * already started one (due to canHandleBlob's behavior).
393 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800394 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800395 }
Patrick Venture18235e62018-11-08 10:21:09 -0800396
397 /* How are they expecting to copy this data? */
398 auto d = std::find_if(
399 transports.begin(), transports.end(),
400 [&flags](const auto& iter) { return (iter.bitmask & flags); });
401 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800402 {
Patrick Venture18235e62018-11-08 10:21:09 -0800403 return false;
404 }
405
406 /* We found the transport handler they requested, no surprise since
407 * above we verify they selected at least one we wanted.
408 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800409
Patrick Venture6e307a72018-11-09 18:21:17 -0800410 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
411 * only non-external data pathway -- but this is just a more generic
412 * approach to that.
413 */
414 if (d->handler)
415 {
416 /* If the data handler open call fails, open fails. */
417 if (!d->handler->open())
418 {
419 return false;
420 }
421 }
422
Patrick Venture70e30bf2019-01-17 10:29:28 -0800423 /* Do we have a file handler for the type of file they're opening.
424 * Note: This should only fail if something is somehow crazy wrong.
425 * Since the canHandle() said yes, and that's tied into the list of explicit
426 * firmware handers (and file handlers, like this'll know where to write the
427 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800428 */
Patrick Venture18235e62018-11-08 10:21:09 -0800429 auto h = std::find_if(
430 handlers.begin(), handlers.end(),
431 [&path](const auto& iter) { return (iter.blobName == path); });
432 if (h == handlers.end())
433 {
434 return false;
435 }
436
437 /* Ok, so we found a handler that matched, so call open() */
438 if (!h->handler->open(path))
439 {
440 return false;
441 }
442
Patrick Venture70e30bf2019-01-17 10:29:28 -0800443 Session* curr;
444 const std::string* active;
445
446 if (path == hashBlobID)
447 {
448 /* 2c) are they opening the /flash/hash ? (to start the process) */
449 curr = &activeHash;
450 active = &activeHashBlobID;
451 }
452 else
453 {
454 curr = &activeImage;
455 active = &activeImageBlobID;
456 }
457
Patrick Venture18235e62018-11-08 10:21:09 -0800458 curr->flags = flags;
459 curr->dataHandler = d->handler;
460 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800461 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800462
463 lookup[session] = curr;
464
Patrick Venturedaf47072019-05-10 15:21:55 -0700465 /* This may be them re-opening a blob, so let's only push it onto the list
466 * when appropriate.
467 */
468 auto blobIdMatch =
469 std::find_if(blobIDs.begin(), blobIDs.end(),
470 [active](const auto& iter) { return (iter == *active); });
471 if (blobIdMatch == blobIDs.end())
472 {
473 blobIDs.push_back(*active);
474 }
Patrick Venturedb253bf2018-11-09 19:36:03 -0800475
Patrick Venture991910a2018-11-09 19:43:48 -0800476 fileOpen = true;
477
Patrick Venture18235e62018-11-08 10:21:09 -0800478 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700479}
Patrick Venture53977962018-11-02 18:59:35 -0700480
Patrick Venture18235e62018-11-08 10:21:09 -0800481/**
482 * The write command really just grabs the data from wherever it is and sends it
483 * to the image handler. It's the image handler's responsibility to deal with
484 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800485 *
486 * This receives a session from the blob manager, therefore it is always called
487 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800488 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700489bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
490 const std::vector<uint8_t>& data)
491{
Patrick Venture18235e62018-11-08 10:21:09 -0800492 auto item = lookup.find(session);
493 if (item == lookup.end())
494 {
495 return false;
496 }
497
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800498 /* Prevent writing during verification. */
499 if (state == UpdateState::verificationStarted)
500 {
501 return false;
502 }
503
Patrick Venture8af15eb2019-05-15 09:39:22 -0700504 /* Prevent writing to the verification blob before they trigger
Patrick Venture699750d2019-05-15 09:24:09 -0700505 * verification.
506 */
Patrick Venture8af15eb2019-05-15 09:39:22 -0700507 if (item->second->activePath == verifyBlobID)
508 {
509 return false;
510 }
Patrick Venture699750d2019-05-15 09:24:09 -0700511
Patrick Venture18235e62018-11-08 10:21:09 -0800512 std::vector<std::uint8_t> bytes;
513
Patrick Venture05abf7e2018-11-09 11:02:56 -0800514 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800515 {
516 bytes = data;
517 }
518 else
519 {
520 /* little endian required per design, and so on, but TODO: do endianness
521 * with boost.
522 */
523 struct ExtChunkHdr header;
524
525 if (data.size() != sizeof(header))
526 {
527 return false;
528 }
529
530 std::memcpy(&header, data.data(), data.size());
531 bytes = item->second->dataHandler->copyFrom(header.length);
532 }
533
534 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700535}
Patrick Venture18235e62018-11-08 10:21:09 -0800536
Patrick Venture8c535332018-11-08 15:58:00 -0800537/*
538 * If the active session (image or hash) is over LPC, this allows
539 * configuring it. This option is only available before you start
540 * writing data for the given item (image or hash). This will return
541 * false at any other part. -- the lpc handler portion will know to return
542 * false.
543 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700544bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
545 const std::vector<uint8_t>& data)
546{
Patrick Venture8c535332018-11-08 15:58:00 -0800547 auto item = lookup.find(session);
548 if (item == lookup.end())
549 {
550 return false;
551 }
552
Patrick Venture05abf7e2018-11-09 11:02:56 -0800553 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800554 {
555 return false;
556 }
557
Patrick Ventured56097a2019-05-15 09:47:55 -0700558 /* Prevent writing meta to the verification blob (it has no data handler).
559 */
560 if (item->second->dataHandler)
561 {
562 return item->second->dataHandler->writeMeta(data);
563 }
Patrick Venture699750d2019-05-15 09:24:09 -0700564
Patrick Ventured56097a2019-05-15 09:47:55 -0700565 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700566}
Patrick Venture8c535332018-11-08 15:58:00 -0800567
Patrick Ventured6461d62018-11-09 17:30:25 -0800568/*
Patrick Ventureffcc5502018-11-16 12:32:38 -0800569 * If this command is called on the session for the verifyBlobID, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800570 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800571 * the image.
572 *
573 * For this file to have opened, the other two must be closed, which means any
574 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800575 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700576bool FirmwareBlobHandler::commit(uint16_t session,
577 const std::vector<uint8_t>& data)
578{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800579 auto item = lookup.find(session);
580 if (item == lookup.end())
581 {
582 return false;
583 }
584
585 /* You can only commit on the verifyBlodId */
586 if (item->second->activePath != verifyBlobID)
587 {
588 return false;
589 }
590
Patrick Venturebe198702019-05-15 09:46:02 -0700591 /* Calling repeatedly has no effect within an update process. */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800592 if (state == UpdateState::verificationStarted)
593 {
Patrick Venturebe198702019-05-15 09:46:02 -0700594 return true;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800595 }
596
597 /* Set state to committing. */
598 item->second->flags |= StateFlags::committing;
599
600 return triggerVerification();
Patrick Venturec7ca2912018-11-02 11:38:33 -0700601}
Patrick Ventured6461d62018-11-09 17:30:25 -0800602
603/*
604 * Close must be called on the firmware image before triggering
605 * verification via commit. Once the verification is complete, you can
606 * then close the hash file.
607 *
608 * If the `verify_image.service` returned success, closing the hash file
609 * will have a specific behavior depending on the update. If it's UBI,
610 * it'll perform the install. If it's static layout, it'll do nothing. The
611 * verify_image service in the static layout case is responsible for placing
612 * the file in the correct staging position.
613 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700614bool FirmwareBlobHandler::close(uint16_t session)
615{
Patrick Venture68bb1432018-11-09 20:08:48 -0800616 auto item = lookup.find(session);
617 if (item == lookup.end())
618 {
619 return false;
620 }
621
Patrick Ventureffcc5502018-11-16 12:32:38 -0800622 /* Are you closing the verify blob? */
623 if (item->second->activePath == verifyBlobID)
624 {
625 /* If they close this blob before verification finishes, that's an
626 * abort.
627 * TODO: implement this, for now just let them close the file.
628 */
629 if (state == UpdateState::verificationStarted)
630 {
631 return false;
632 }
633 }
634
Patrick Venture68bb1432018-11-09 20:08:48 -0800635 if (item->second->dataHandler)
636 {
637 item->second->dataHandler->close();
638 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800639 if (item->second->imageHandler)
640 {
641 item->second->imageHandler->close();
642 }
643
Patrick Venture68bb1432018-11-09 20:08:48 -0800644 item->second->state = Session::State::closed;
645 /* Do not delete the active blob_id from the list of blob_ids, because that
646 * blob_id indicates there is data stored. Delete will destroy it.
647 */
648
649 lookup.erase(item);
650
Patrick Venture991910a2018-11-09 19:43:48 -0800651 fileOpen = false;
652
Patrick Venture68bb1432018-11-09 20:08:48 -0800653 /* TODO: implement other aspects of closing out a session, such as changing
654 * global firmware state.
655 */
656 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700657}
Patrick Ventured6461d62018-11-09 17:30:25 -0800658
Patrick Venturec7ca2912018-11-02 11:38:33 -0700659bool FirmwareBlobHandler::expire(uint16_t session)
660{
661 return false;
662}
Patrick Ventured6461d62018-11-09 17:30:25 -0800663
664/*
665 * Currently, the design does not provide this with a function, however,
666 * it will likely change to support reading data back.
667 */
668std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
669 uint32_t offset,
670 uint32_t requestedSize)
671{
672 return {};
673}
674
Patrick Ventureffcc5502018-11-16 12:32:38 -0800675bool FirmwareBlobHandler::triggerVerification()
676{
Patrick Venturecabc1172018-11-16 16:14:26 -0800677 auto method = bus.new_method_call(systemdService, systemdRoot,
678 systemdInterface, "StartUnit");
679 method.append(verifyTarget);
680 method.append("replace");
681
682 try
683 {
684 bus.call_noreply(method);
Patrick Venture453f06a2019-01-17 10:02:12 -0800685 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800686 }
687 catch (const sdbusplus::exception::SdBusError& ex)
688 {
689 /* TODO: Once logging supports unit-tests, add a log message to test
690 * this failure.
691 */
692 return false;
693 }
694
Patrick Ventureffcc5502018-11-16 12:32:38 -0800695 return true;
696}
697
Patrick Venturec7ca2912018-11-02 11:38:33 -0700698} // namespace blobs