blob: d8ddf8d8712196cd532b53acbf0e0c7358bce2cd [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
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700254 /* The size here refers to the size of the file -- of something analagous.
255 */
256 meta->size = (item->second->imageHandler)
257 ? item->second->imageHandler->getSize()
258 : 0;
259
260 meta->metadata.clear();
261
Patrick Venture699750d2019-05-15 09:24:09 -0700262 /* TODO: Implement this for the verification blob, which is what we expect.
263 * Calling stat() on the verify blob without an active session should not
264 * provide insight.
265 */
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700266 if (item->second->activePath == verifyBlobID)
267 {
Patrick Venturee955e072019-05-15 16:16:46 -0700268 auto value = checkVerificationState();
Patrick Venture699750d2019-05-15 09:24:09 -0700269
Patrick Venturee955e072019-05-15 16:16:46 -0700270 meta->metadata.push_back(static_cast<std::uint8_t>(value));
271
272 /* Change the firmware handler's state and the blob's stat value
273 * depending.
274 */
275 if (value == VerifyCheckResponses::success ||
276 value == VerifyCheckResponses::failed)
277 {
278 state = UpdateState::verificationCompleted;
279 item->second->flags &= ~StateFlags::committing;
280
281 if (value == VerifyCheckResponses::success)
282 {
283 item->second->flags |= StateFlags::committed;
284 }
285 else
286 {
287 item->second->flags |= StateFlags::commit_error;
288 }
289 }
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700290 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800291
Patrick Venturee955e072019-05-15 16:16:46 -0700292 /* The blobState here relates to an active sesion, so we should return the
293 * flags used to open this session.
294 */
295 meta->blobState = item->second->flags;
296
Patrick Venturecc7d1602018-11-15 13:58:33 -0800297 /* The metadata blob returned comes from the data handler... it's used for
298 * instance, in P2A bridging to get required information about the mapping,
299 * and is the "opposite" of the lpc writemeta requirement.
300 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800301 if (item->second->dataHandler)
302 {
Patrick Venture74304642019-01-17 09:31:04 -0800303 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800304 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
305 bytes.end());
306 }
307
Patrick Venturecc7d1602018-11-15 13:58:33 -0800308 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800309}
310
311/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800312 * If you open /flash/image or /flash/tarball, or /flash/hash it will
313 * interpret the open flags and perform whatever actions are required for
314 * that update process. The session returned can be used immediately for
315 * sending data down, without requiring one to open the new active file.
316 *
317 * If you open the active flash image or active hash it will let you
318 * overwrite pieces, depending on the state.
319 *
320 * Once the verification process has started the active files cannot be
321 * opened.
322 *
323 * You can only have one open session at a time. Which means, you can only
324 * have one file open at a time. Trying to open the hash blob_id while you
325 * still have the flash image blob_id open will fail. Opening the flash
326 * blob_id when it is already open will fail.
327 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700328bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
329 const std::string& path)
330{
Patrick Venture6e307a72018-11-09 18:21:17 -0800331 /* Check that they've opened for writing - read back not currently
332 * supported.
333 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800334 if ((flags & OpenFlags::write) == 0)
335 {
336 return false;
337 }
338
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800339 /* Is the verification process underway? */
340 if (state == UpdateState::verificationStarted)
341 {
342 return false;
343 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800344
345 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800346 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800347 * TODO: Temporarily using a simple boolean flag until there's a full
348 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800349 *
350 * Further on this, if there's an active session to the hash we don't allow
351 * re-opening the image, and if we have the image open, we don't allow
352 * opening the hash. This design decision may be re-evaluated, and changed
353 * to only allow one session per object type (of the two types). But,
354 * consider if the hash is open, do we want to allow writing to the image?
355 * And why would we? But, really, the point of no-return is once the
356 * verification process has begun -- which is done via commit() on the hash
357 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700358 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800359 if (fileOpen)
360 {
361 return false;
362 }
363
Patrick Ventureffcc5502018-11-16 12:32:38 -0800364 /* Handle opening the verifyBlobId --> we know the image and hash aren't
365 * open because of the fileOpen check.
366 *
367 * The file must be opened for writing, but no transport mechanism specified
368 * since it's irrelevant.
369 */
370 if (path == verifyBlobID)
371 {
372 /* In this case, there's no image handler to use, or data handler,
373 * simply set up a session.
374 */
375 verifyImage.flags = flags;
376 verifyImage.state = Session::State::open;
377
378 lookup[session] = &verifyImage;
379
380 fileOpen = true;
381
382 return true;
383 }
384
Patrick Venturec02849b2018-11-06 17:31:15 -0800385 /* There are two abstractions at play, how you get the data and how you
386 * handle that data. such that, whether the data comes from the PCI bridge
387 * or LPC bridge is not connected to whether the data goes into a static
388 * layout flash update or a UBI tarball.
389 */
390
391 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800392 * support what they request.
393 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800394 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800395 {
396 return false;
397 }
398
399 /* 2) there isn't, so what are they opening? */
400 if (path == activeImageBlobID)
401 {
402 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800403 * already started one (due to canHandleBlob's behavior).
404 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800405 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800406 }
407 else if (path == activeHashBlobID)
408 {
409 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800410 * already started one (due to canHandleBlob's behavior).
411 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800412 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800413 }
Patrick Venture18235e62018-11-08 10:21:09 -0800414
415 /* How are they expecting to copy this data? */
416 auto d = std::find_if(
417 transports.begin(), transports.end(),
418 [&flags](const auto& iter) { return (iter.bitmask & flags); });
419 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800420 {
Patrick Venture18235e62018-11-08 10:21:09 -0800421 return false;
422 }
423
424 /* We found the transport handler they requested, no surprise since
425 * above we verify they selected at least one we wanted.
426 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800427
Patrick Venture6e307a72018-11-09 18:21:17 -0800428 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
429 * only non-external data pathway -- but this is just a more generic
430 * approach to that.
431 */
432 if (d->handler)
433 {
434 /* If the data handler open call fails, open fails. */
435 if (!d->handler->open())
436 {
437 return false;
438 }
439 }
440
Patrick Venture70e30bf2019-01-17 10:29:28 -0800441 /* Do we have a file handler for the type of file they're opening.
442 * Note: This should only fail if something is somehow crazy wrong.
443 * Since the canHandle() said yes, and that's tied into the list of explicit
444 * firmware handers (and file handlers, like this'll know where to write the
445 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800446 */
Patrick Venture18235e62018-11-08 10:21:09 -0800447 auto h = std::find_if(
448 handlers.begin(), handlers.end(),
449 [&path](const auto& iter) { return (iter.blobName == path); });
450 if (h == handlers.end())
451 {
452 return false;
453 }
454
455 /* Ok, so we found a handler that matched, so call open() */
456 if (!h->handler->open(path))
457 {
458 return false;
459 }
460
Patrick Venture70e30bf2019-01-17 10:29:28 -0800461 Session* curr;
462 const std::string* active;
463
464 if (path == hashBlobID)
465 {
466 /* 2c) are they opening the /flash/hash ? (to start the process) */
467 curr = &activeHash;
468 active = &activeHashBlobID;
469 }
470 else
471 {
472 curr = &activeImage;
473 active = &activeImageBlobID;
474 }
475
Patrick Venture18235e62018-11-08 10:21:09 -0800476 curr->flags = flags;
477 curr->dataHandler = d->handler;
478 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800479 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800480
481 lookup[session] = curr;
482
Patrick Venturedaf47072019-05-10 15:21:55 -0700483 /* This may be them re-opening a blob, so let's only push it onto the list
484 * when appropriate.
485 */
486 auto blobIdMatch =
487 std::find_if(blobIDs.begin(), blobIDs.end(),
488 [active](const auto& iter) { return (iter == *active); });
489 if (blobIdMatch == blobIDs.end())
490 {
491 blobIDs.push_back(*active);
492 }
Patrick Venturedb253bf2018-11-09 19:36:03 -0800493
Patrick Venture991910a2018-11-09 19:43:48 -0800494 fileOpen = true;
495
Patrick Venture18235e62018-11-08 10:21:09 -0800496 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700497}
Patrick Venture53977962018-11-02 18:59:35 -0700498
Patrick Venture18235e62018-11-08 10:21:09 -0800499/**
500 * The write command really just grabs the data from wherever it is and sends it
501 * to the image handler. It's the image handler's responsibility to deal with
502 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800503 *
504 * This receives a session from the blob manager, therefore it is always called
505 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800506 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700507bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
508 const std::vector<uint8_t>& data)
509{
Patrick Venture18235e62018-11-08 10:21:09 -0800510 auto item = lookup.find(session);
511 if (item == lookup.end())
512 {
513 return false;
514 }
515
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800516 /* Prevent writing during verification. */
517 if (state == UpdateState::verificationStarted)
518 {
519 return false;
520 }
521
Patrick Venture8af15eb2019-05-15 09:39:22 -0700522 /* Prevent writing to the verification blob before they trigger
Patrick Venture699750d2019-05-15 09:24:09 -0700523 * verification.
524 */
Patrick Venture8af15eb2019-05-15 09:39:22 -0700525 if (item->second->activePath == verifyBlobID)
526 {
527 return false;
528 }
Patrick Venture699750d2019-05-15 09:24:09 -0700529
Patrick Venture18235e62018-11-08 10:21:09 -0800530 std::vector<std::uint8_t> bytes;
531
Patrick Venture05abf7e2018-11-09 11:02:56 -0800532 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800533 {
534 bytes = data;
535 }
536 else
537 {
538 /* little endian required per design, and so on, but TODO: do endianness
539 * with boost.
540 */
541 struct ExtChunkHdr header;
542
543 if (data.size() != sizeof(header))
544 {
545 return false;
546 }
547
548 std::memcpy(&header, data.data(), data.size());
549 bytes = item->second->dataHandler->copyFrom(header.length);
550 }
551
552 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700553}
Patrick Venture18235e62018-11-08 10:21:09 -0800554
Patrick Venture8c535332018-11-08 15:58:00 -0800555/*
556 * If the active session (image or hash) is over LPC, this allows
557 * configuring it. This option is only available before you start
558 * writing data for the given item (image or hash). This will return
559 * false at any other part. -- the lpc handler portion will know to return
560 * false.
561 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700562bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
563 const std::vector<uint8_t>& data)
564{
Patrick Venture8c535332018-11-08 15:58:00 -0800565 auto item = lookup.find(session);
566 if (item == lookup.end())
567 {
568 return false;
569 }
570
Patrick Venture05abf7e2018-11-09 11:02:56 -0800571 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800572 {
573 return false;
574 }
575
Patrick Ventured56097a2019-05-15 09:47:55 -0700576 /* Prevent writing meta to the verification blob (it has no data handler).
577 */
578 if (item->second->dataHandler)
579 {
580 return item->second->dataHandler->writeMeta(data);
581 }
Patrick Venture699750d2019-05-15 09:24:09 -0700582
Patrick Ventured56097a2019-05-15 09:47:55 -0700583 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700584}
Patrick Venture8c535332018-11-08 15:58:00 -0800585
Patrick Ventured6461d62018-11-09 17:30:25 -0800586/*
Patrick Ventureffcc5502018-11-16 12:32:38 -0800587 * If this command is called on the session for the verifyBlobID, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800588 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800589 * the image.
590 *
591 * For this file to have opened, the other two must be closed, which means any
592 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800593 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700594bool FirmwareBlobHandler::commit(uint16_t session,
595 const std::vector<uint8_t>& data)
596{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800597 auto item = lookup.find(session);
598 if (item == lookup.end())
599 {
600 return false;
601 }
602
603 /* You can only commit on the verifyBlodId */
604 if (item->second->activePath != verifyBlobID)
605 {
606 return false;
607 }
608
Patrick Venturebe198702019-05-15 09:46:02 -0700609 /* Calling repeatedly has no effect within an update process. */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800610 if (state == UpdateState::verificationStarted)
611 {
Patrick Venturebe198702019-05-15 09:46:02 -0700612 return true;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800613 }
614
615 /* Set state to committing. */
616 item->second->flags |= StateFlags::committing;
617
618 return triggerVerification();
Patrick Venturec7ca2912018-11-02 11:38:33 -0700619}
Patrick Ventured6461d62018-11-09 17:30:25 -0800620
621/*
622 * Close must be called on the firmware image before triggering
623 * verification via commit. Once the verification is complete, you can
624 * then close the hash file.
625 *
626 * If the `verify_image.service` returned success, closing the hash file
627 * will have a specific behavior depending on the update. If it's UBI,
628 * it'll perform the install. If it's static layout, it'll do nothing. The
629 * verify_image service in the static layout case is responsible for placing
630 * the file in the correct staging position.
631 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700632bool FirmwareBlobHandler::close(uint16_t session)
633{
Patrick Venture68bb1432018-11-09 20:08:48 -0800634 auto item = lookup.find(session);
635 if (item == lookup.end())
636 {
637 return false;
638 }
639
Patrick Ventureffcc5502018-11-16 12:32:38 -0800640 /* Are you closing the verify blob? */
641 if (item->second->activePath == verifyBlobID)
642 {
643 /* If they close this blob before verification finishes, that's an
644 * abort.
645 * TODO: implement this, for now just let them close the file.
646 */
647 if (state == UpdateState::verificationStarted)
648 {
649 return false;
650 }
651 }
652
Patrick Venture68bb1432018-11-09 20:08:48 -0800653 if (item->second->dataHandler)
654 {
655 item->second->dataHandler->close();
656 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800657 if (item->second->imageHandler)
658 {
659 item->second->imageHandler->close();
660 }
661
Patrick Venture68bb1432018-11-09 20:08:48 -0800662 item->second->state = Session::State::closed;
663 /* Do not delete the active blob_id from the list of blob_ids, because that
664 * blob_id indicates there is data stored. Delete will destroy it.
665 */
666
667 lookup.erase(item);
668
Patrick Venture991910a2018-11-09 19:43:48 -0800669 fileOpen = false;
670
Patrick Venture68bb1432018-11-09 20:08:48 -0800671 /* TODO: implement other aspects of closing out a session, such as changing
672 * global firmware state.
673 */
674 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700675}
Patrick Ventured6461d62018-11-09 17:30:25 -0800676
Patrick Venturec7ca2912018-11-02 11:38:33 -0700677bool FirmwareBlobHandler::expire(uint16_t session)
678{
679 return false;
680}
Patrick Ventured6461d62018-11-09 17:30:25 -0800681
682/*
683 * Currently, the design does not provide this with a function, however,
684 * it will likely change to support reading data back.
685 */
686std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
687 uint32_t offset,
688 uint32_t requestedSize)
689{
690 return {};
691}
692
Patrick Ventureffcc5502018-11-16 12:32:38 -0800693bool FirmwareBlobHandler::triggerVerification()
694{
Patrick Venturecabc1172018-11-16 16:14:26 -0800695 auto method = bus.new_method_call(systemdService, systemdRoot,
696 systemdInterface, "StartUnit");
697 method.append(verifyTarget);
698 method.append("replace");
699
700 try
701 {
702 bus.call_noreply(method);
Patrick Venture453f06a2019-01-17 10:02:12 -0800703 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800704 }
705 catch (const sdbusplus::exception::SdBusError& ex)
706 {
707 /* TODO: Once logging supports unit-tests, add a log message to test
708 * this failure.
709 */
710 return false;
711 }
712
Patrick Ventureffcc5502018-11-16 12:32:38 -0800713 return true;
714}
715
Patrick Venturec7ca2912018-11-02 11:38:33 -0700716} // namespace blobs