blob: 6e020240b3d55e1642df8f305920124a565a4736 [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"
Patrick Venture7dad86f2019-05-17 08:52:20 -070020#include "util.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -080021
Patrick Venture137ad2c2018-11-06 11:30:43 -080022#include <algorithm>
Patrick Venture192d60f2018-11-06 11:11:59 -080023#include <cstdint>
Patrick Venture18235e62018-11-08 10:21:09 -080024#include <cstring>
Patrick Ventureb3b4db72019-05-15 11:30:24 -070025#include <fstream>
Patrick Venture68cf64f2018-11-06 10:46:51 -080026#include <memory>
Patrick Ventured333a872018-12-03 16:24:26 -080027#include <phosphor-logging/log.hpp>
Patrick Venturefa6c4d92018-11-02 18:34:53 -070028#include <string>
29#include <vector>
30
Patrick Ventured333a872018-12-03 16:24:26 -080031using namespace phosphor::logging;
32
Patrick Venturec7ca2912018-11-02 11:38:33 -070033namespace blobs
34{
Patrick Venturecabc1172018-11-16 16:14:26 -080035// systemd service to kick start a target.
36static constexpr auto systemdService = "org.freedesktop.systemd1";
37static constexpr auto systemdRoot = "/org/freedesktop/systemd1";
38static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
39static constexpr auto verifyTarget = "verify_image.service";
Patrick Venturec7ca2912018-11-02 11:38:33 -070040
Patrick Ventureb3b4db72019-05-15 11:30:24 -070041namespace
42{
43
Patrick Venture74059d62019-05-17 10:40:26 -070044FirmwareBlobHandler::VerifyCheckResponses
45 checkVerificationState(const std::string& path)
Patrick Ventureb3b4db72019-05-15 11:30:24 -070046{
47 FirmwareBlobHandler::VerifyCheckResponses result =
48 FirmwareBlobHandler::VerifyCheckResponses::other;
49
50 std::ifstream ifs;
Patrick Venture74059d62019-05-17 10:40:26 -070051 ifs.open(path);
Patrick Ventureb3b4db72019-05-15 11:30:24 -070052 if (ifs.good())
53 {
54 /*
55 * Check for the contents of the file, accepting:
56 * running, success, or failed.
57 */
58 std::string status;
59 ifs >> status;
60 if (status == "running")
61 {
62 result = FirmwareBlobHandler::VerifyCheckResponses::running;
63 }
64 else if (status == "success")
65 {
66 result = FirmwareBlobHandler::VerifyCheckResponses::success;
67 }
68 else if (status == "failed")
69 {
70 result = FirmwareBlobHandler::VerifyCheckResponses::failed;
71 }
72 }
73
74 return result;
75}
76
77} // namespace
78
Patrick Venture68cf64f2018-11-06 10:46:51 -080079std::unique_ptr<GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080080 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture4eb55952018-11-16 15:36:24 -080081 sdbusplus::bus::bus&& bus, const std::vector<HandlerPack>& firmwares,
Patrick Venture74059d62019-05-17 10:40:26 -070082 const std::vector<DataHandlerPack>& transports,
83 const std::string& verificationPath)
Patrick Venture68cf64f2018-11-06 10:46:51 -080084{
Patrick Venture52854622018-11-06 12:30:00 -080085 /* There must be at least one. */
86 if (!firmwares.size())
87 {
Patrick Ventured333a872018-12-03 16:24:26 -080088 log<level::ERR>("Must provide at least one firmware handler.");
Patrick Venture52854622018-11-06 12:30:00 -080089 return nullptr;
90 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080091 if (!transports.size())
92 {
93 return nullptr;
94 }
Patrick Venture52854622018-11-06 12:30:00 -080095
Patrick Venturea78e39f2018-11-06 18:37:06 -080096 std::vector<std::string> blobs;
97 for (const auto& item : firmwares)
98 {
99 blobs.push_back(item.blobName);
100 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700101 blobs.push_back(verifyBlobId); /* Add blob_id to always exist. */
Patrick Venture18235e62018-11-08 10:21:09 -0800102
Patrick Venture7dad86f2019-05-17 08:52:20 -0700103 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId))
Patrick Venture18235e62018-11-08 10:21:09 -0800104 {
105 return nullptr;
106 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800107
Patrick Venture1cde5f92018-11-07 08:26:47 -0800108 std::uint16_t bitmask = 0;
109 for (const auto& item : transports)
110 {
111 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
112 bitmask |= item.bitmask;
113 }
114
Patrick Venture4eb55952018-11-16 15:36:24 -0800115 return std::make_unique<FirmwareBlobHandler>(std::move(bus), firmwares,
Patrick Venture74059d62019-05-17 10:40:26 -0700116 blobs, transports, bitmask,
117 verificationPath);
Patrick Venture68cf64f2018-11-06 10:46:51 -0800118}
119
Patrick Ventured6461d62018-11-09 17:30:25 -0800120/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700121bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
122{
Patrick Venture6032dc02019-05-17 11:01:44 -0700123 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700124}
Patrick Venture53977962018-11-02 18:59:35 -0700125
Patrick Ventured6461d62018-11-09 17:30:25 -0800126/*
127 * Grab the list of supported firmware.
128 *
129 * If there's an open firmware session, it'll already be present in the
130 * list as "/flash/active/image", and if the hash has started,
131 * "/flash/active/hash" regardless of mechanism. This is done in the open
132 * comamnd, no extra work is required here.
133 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700134std::vector<std::string> FirmwareBlobHandler::getBlobIds()
135{
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800136 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700137}
Patrick Venture53977962018-11-02 18:59:35 -0700138
Patrick Ventured6461d62018-11-09 17:30:25 -0800139/*
140 * Per the design, this mean abort, and this will trigger whatever
141 * appropriate actions are required to abort the process.
Patrick Venture9e5aab22018-11-09 20:49:28 -0800142 *
143 * You cannot delete a blob that has an open handle in the system, therefore
144 * this is never called if there's an open session. Guaranteed by the blob
145 * manager.
Patrick Ventured6461d62018-11-09 17:30:25 -0800146 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700147bool FirmwareBlobHandler::deleteBlob(const std::string& path)
148{
Patrick Venture9e5aab22018-11-09 20:49:28 -0800149 const std::string* toDelete;
150
Patrick Ventureffcc5502018-11-16 12:32:38 -0800151 /* You cannot delete the verify blob -- trying to delete it, currently has
152 * no impact.
153 * TODO: Should trying to delete this cause an abort?
154 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700155 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800156 {
157 return false;
158 }
159
Patrick Venture7dad86f2019-05-17 08:52:20 -0700160 if (path == hashBlobId || path == activeHashBlobId)
Patrick Venture9e5aab22018-11-09 20:49:28 -0800161 {
162 /* They're deleting the hash. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700163 toDelete = &activeHashBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800164 }
165 else
166 {
167 /* They're deleting the image. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700168 toDelete = &activeImageBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800169 }
170
171 auto it = std::find_if(
172 blobIDs.begin(), blobIDs.end(),
173 [toDelete](const auto& iter) { return (iter == *toDelete); });
174 if (it == blobIDs.end())
175 {
176 /* Somehow they've asked to delete something we didn't say we could
177 * handle.
178 */
179 return false;
180 }
181
182 blobIDs.erase(it);
183
184 /* TODO: Handle aborting the process and fixing up the state. */
185
186 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700187}
Patrick Venture53977962018-11-02 18:59:35 -0700188
Patrick Ventured6461d62018-11-09 17:30:25 -0800189/*
190 * Stat on the files will return information such as what supported
191 * transport mechanisms are available.
192 *
193 * Stat on an active file or hash will return information such as the size
194 * of the data cached, and any additional pertinent information. The
195 * blob_state on the active files will return the state of the update.
196 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700197bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
198{
Patrick Venture46637c82018-11-06 15:20:24 -0800199 /* We know we support this path because canHandle is called ahead */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700200 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800201 {
Patrick Venture699750d2019-05-15 09:24:09 -0700202 /* TODO: We need to return information for the verify state -- did they
203 * call commit() did things start?
Patrick Ventureffcc5502018-11-16 12:32:38 -0800204 */
205 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700206 else if (path == activeImageBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800207 {
Patrick Venture699750d2019-05-15 09:24:09 -0700208 /* TODO: We need to return information for the image that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800209 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700210 else if (path == activeHashBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800211 {
Patrick Venture699750d2019-05-15 09:24:09 -0700212 /* TODO: We need to return information for the hash that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800213 }
214 else
215 {
216 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800217 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800218 meta->size = 0;
219
220 /* The generic blob_ids state is only the bits related to the transport
Patrick Ventured6461d62018-11-09 17:30:25 -0800221 * mechanisms.
222 */
Patrick Venture46637c82018-11-06 15:20:24 -0800223 return true;
224 }
225
Patrick Venturec7ca2912018-11-02 11:38:33 -0700226 return false;
227}
Patrick Venture53977962018-11-02 18:59:35 -0700228
Patrick Venturec02849b2018-11-06 17:31:15 -0800229/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800230 * Return stat information on an open session. It therefore must be an active
231 * handle to either the active image or active hash.
232 *
233 * The stat() and sessionstat() commands will return the same information in
234 * many cases, therefore the logic will be combined.
235 *
236 * TODO: combine the logic for stat and sessionstat().
237 */
238bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
239{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800240 auto item = lookup.find(session);
241 if (item == lookup.end())
242 {
243 return false;
244 }
245
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700246 /* The size here refers to the size of the file -- of something analagous.
247 */
248 meta->size = (item->second->imageHandler)
249 ? item->second->imageHandler->getSize()
250 : 0;
251
252 meta->metadata.clear();
253
Patrick Venture699750d2019-05-15 09:24:09 -0700254 /* TODO: Implement this for the verification blob, which is what we expect.
255 * Calling stat() on the verify blob without an active session should not
256 * provide insight.
257 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700258 if (item->second->activePath == verifyBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700259 {
Patrick Venture74059d62019-05-17 10:40:26 -0700260 auto value = checkVerificationState(verificationPath);
Patrick Venture699750d2019-05-15 09:24:09 -0700261
Patrick Venturee955e072019-05-15 16:16:46 -0700262 meta->metadata.push_back(static_cast<std::uint8_t>(value));
263
264 /* Change the firmware handler's state and the blob's stat value
265 * depending.
266 */
267 if (value == VerifyCheckResponses::success ||
268 value == VerifyCheckResponses::failed)
269 {
270 state = UpdateState::verificationCompleted;
271 item->second->flags &= ~StateFlags::committing;
272
273 if (value == VerifyCheckResponses::success)
274 {
275 item->second->flags |= StateFlags::committed;
276 }
277 else
278 {
279 item->second->flags |= StateFlags::commit_error;
280 }
281 }
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700282 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800283
Patrick Venturee955e072019-05-15 16:16:46 -0700284 /* The blobState here relates to an active sesion, so we should return the
285 * flags used to open this session.
286 */
287 meta->blobState = item->second->flags;
288
Patrick Venturecc7d1602018-11-15 13:58:33 -0800289 /* The metadata blob returned comes from the data handler... it's used for
290 * instance, in P2A bridging to get required information about the mapping,
291 * and is the "opposite" of the lpc writemeta requirement.
292 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800293 if (item->second->dataHandler)
294 {
Patrick Venture74304642019-01-17 09:31:04 -0800295 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800296 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
297 bytes.end());
298 }
299
Patrick Venturecc7d1602018-11-15 13:58:33 -0800300 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800301}
302
303/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800304 * If you open /flash/image or /flash/tarball, or /flash/hash it will
305 * interpret the open flags and perform whatever actions are required for
306 * that update process. The session returned can be used immediately for
307 * sending data down, without requiring one to open the new active file.
308 *
309 * If you open the active flash image or active hash it will let you
310 * overwrite pieces, depending on the state.
311 *
312 * Once the verification process has started the active files cannot be
313 * opened.
314 *
315 * You can only have one open session at a time. Which means, you can only
316 * have one file open at a time. Trying to open the hash blob_id while you
317 * still have the flash image blob_id open will fail. Opening the flash
318 * blob_id when it is already open will fail.
319 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700320bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
321 const std::string& path)
322{
Patrick Venture6e307a72018-11-09 18:21:17 -0800323 /* Check that they've opened for writing - read back not currently
324 * supported.
325 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800326 if ((flags & OpenFlags::write) == 0)
327 {
328 return false;
329 }
330
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800331 /* Is the verification process underway? */
332 if (state == UpdateState::verificationStarted)
333 {
334 return false;
335 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800336
337 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800338 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800339 * TODO: Temporarily using a simple boolean flag until there's a full
340 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800341 *
342 * Further on this, if there's an active session to the hash we don't allow
343 * re-opening the image, and if we have the image open, we don't allow
344 * opening the hash. This design decision may be re-evaluated, and changed
345 * to only allow one session per object type (of the two types). But,
346 * consider if the hash is open, do we want to allow writing to the image?
347 * And why would we? But, really, the point of no-return is once the
348 * verification process has begun -- which is done via commit() on the hash
349 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700350 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800351 if (fileOpen)
352 {
353 return false;
354 }
355
Patrick Ventureffcc5502018-11-16 12:32:38 -0800356 /* Handle opening the verifyBlobId --> we know the image and hash aren't
357 * open because of the fileOpen check.
358 *
359 * The file must be opened for writing, but no transport mechanism specified
360 * since it's irrelevant.
361 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700362 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800363 {
364 /* In this case, there's no image handler to use, or data handler,
365 * simply set up a session.
366 */
367 verifyImage.flags = flags;
368 verifyImage.state = Session::State::open;
369
370 lookup[session] = &verifyImage;
371
372 fileOpen = true;
373
374 return true;
375 }
376
Patrick Venturec02849b2018-11-06 17:31:15 -0800377 /* There are two abstractions at play, how you get the data and how you
378 * handle that data. such that, whether the data comes from the PCI bridge
379 * or LPC bridge is not connected to whether the data goes into a static
380 * layout flash update or a UBI tarball.
381 */
382
383 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800384 * support what they request.
385 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800386 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800387 {
388 return false;
389 }
390
391 /* 2) there isn't, so what are they opening? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700392 if (path == activeImageBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800393 {
394 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800395 * already started one (due to canHandleBlob's behavior).
396 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800397 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800398 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700399 else if (path == activeHashBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800400 {
401 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800402 * already started one (due to canHandleBlob's behavior).
403 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800404 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800405 }
Patrick Venture18235e62018-11-08 10:21:09 -0800406
407 /* How are they expecting to copy this data? */
408 auto d = std::find_if(
409 transports.begin(), transports.end(),
410 [&flags](const auto& iter) { return (iter.bitmask & flags); });
411 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800412 {
Patrick Venture18235e62018-11-08 10:21:09 -0800413 return false;
414 }
415
416 /* We found the transport handler they requested, no surprise since
417 * above we verify they selected at least one we wanted.
418 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800419
Patrick Venture6e307a72018-11-09 18:21:17 -0800420 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
421 * only non-external data pathway -- but this is just a more generic
422 * approach to that.
423 */
424 if (d->handler)
425 {
426 /* If the data handler open call fails, open fails. */
427 if (!d->handler->open())
428 {
429 return false;
430 }
431 }
432
Patrick Venture70e30bf2019-01-17 10:29:28 -0800433 /* Do we have a file handler for the type of file they're opening.
434 * Note: This should only fail if something is somehow crazy wrong.
435 * Since the canHandle() said yes, and that's tied into the list of explicit
436 * firmware handers (and file handlers, like this'll know where to write the
437 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800438 */
Patrick Venture18235e62018-11-08 10:21:09 -0800439 auto h = std::find_if(
440 handlers.begin(), handlers.end(),
441 [&path](const auto& iter) { return (iter.blobName == path); });
442 if (h == handlers.end())
443 {
444 return false;
445 }
446
447 /* Ok, so we found a handler that matched, so call open() */
448 if (!h->handler->open(path))
449 {
450 return false;
451 }
452
Patrick Venture70e30bf2019-01-17 10:29:28 -0800453 Session* curr;
454 const std::string* active;
455
Patrick Venture7dad86f2019-05-17 08:52:20 -0700456 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800457 {
458 /* 2c) are they opening the /flash/hash ? (to start the process) */
459 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700460 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800461 }
462 else
463 {
464 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700465 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800466 }
467
Patrick Venture18235e62018-11-08 10:21:09 -0800468 curr->flags = flags;
469 curr->dataHandler = d->handler;
470 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800471 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800472
473 lookup[session] = curr;
474
Patrick Venturedaf47072019-05-10 15:21:55 -0700475 /* This may be them re-opening a blob, so let's only push it onto the list
476 * when appropriate.
477 */
478 auto blobIdMatch =
479 std::find_if(blobIDs.begin(), blobIDs.end(),
480 [active](const auto& iter) { return (iter == *active); });
481 if (blobIdMatch == blobIDs.end())
482 {
483 blobIDs.push_back(*active);
484 }
Patrick Venturedb253bf2018-11-09 19:36:03 -0800485
Patrick Venture12233c52019-05-16 09:26:59 -0700486 state = UpdateState::uploadInProgress;
Patrick Venture991910a2018-11-09 19:43:48 -0800487 fileOpen = true;
488
Patrick Venture18235e62018-11-08 10:21:09 -0800489 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700490}
Patrick Venture53977962018-11-02 18:59:35 -0700491
Patrick Venture18235e62018-11-08 10:21:09 -0800492/**
493 * The write command really just grabs the data from wherever it is and sends it
494 * to the image handler. It's the image handler's responsibility to deal with
495 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800496 *
497 * This receives a session from the blob manager, therefore it is always called
498 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800499 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700500bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
501 const std::vector<uint8_t>& data)
502{
Patrick Venture18235e62018-11-08 10:21:09 -0800503 auto item = lookup.find(session);
504 if (item == lookup.end())
505 {
506 return false;
507 }
508
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800509 /* Prevent writing during verification. */
510 if (state == UpdateState::verificationStarted)
511 {
512 return false;
513 }
514
Patrick Venture8af15eb2019-05-15 09:39:22 -0700515 /* Prevent writing to the verification blob before they trigger
Patrick Venture699750d2019-05-15 09:24:09 -0700516 * verification.
517 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700518 if (item->second->activePath == verifyBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700519 {
520 return false;
521 }
Patrick Venture699750d2019-05-15 09:24:09 -0700522
Patrick Venture18235e62018-11-08 10:21:09 -0800523 std::vector<std::uint8_t> bytes;
524
Patrick Venture05abf7e2018-11-09 11:02:56 -0800525 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800526 {
527 bytes = data;
528 }
529 else
530 {
531 /* little endian required per design, and so on, but TODO: do endianness
532 * with boost.
533 */
534 struct ExtChunkHdr header;
535
536 if (data.size() != sizeof(header))
537 {
538 return false;
539 }
540
541 std::memcpy(&header, data.data(), data.size());
542 bytes = item->second->dataHandler->copyFrom(header.length);
543 }
544
545 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700546}
Patrick Venture18235e62018-11-08 10:21:09 -0800547
Patrick Venture8c535332018-11-08 15:58:00 -0800548/*
549 * If the active session (image or hash) is over LPC, this allows
550 * configuring it. This option is only available before you start
551 * writing data for the given item (image or hash). This will return
552 * false at any other part. -- the lpc handler portion will know to return
553 * false.
554 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700555bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
556 const std::vector<uint8_t>& data)
557{
Patrick Venture8c535332018-11-08 15:58:00 -0800558 auto item = lookup.find(session);
559 if (item == lookup.end())
560 {
561 return false;
562 }
563
Patrick Venture05abf7e2018-11-09 11:02:56 -0800564 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800565 {
566 return false;
567 }
568
Patrick Ventured56097a2019-05-15 09:47:55 -0700569 /* Prevent writing meta to the verification blob (it has no data handler).
570 */
571 if (item->second->dataHandler)
572 {
573 return item->second->dataHandler->writeMeta(data);
574 }
Patrick Venture699750d2019-05-15 09:24:09 -0700575
Patrick Ventured56097a2019-05-15 09:47:55 -0700576 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700577}
Patrick Venture8c535332018-11-08 15:58:00 -0800578
Patrick Ventured6461d62018-11-09 17:30:25 -0800579/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700580 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800581 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800582 * the image.
583 *
584 * For this file to have opened, the other two must be closed, which means any
585 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800586 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700587bool FirmwareBlobHandler::commit(uint16_t session,
588 const std::vector<uint8_t>& data)
589{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800590 auto item = lookup.find(session);
591 if (item == lookup.end())
592 {
593 return false;
594 }
595
596 /* You can only commit on the verifyBlodId */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700597 if (item->second->activePath != verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800598 {
599 return false;
600 }
601
Patrick Venturebe198702019-05-15 09:46:02 -0700602 /* Calling repeatedly has no effect within an update process. */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800603 if (state == UpdateState::verificationStarted)
604 {
Patrick Venturebe198702019-05-15 09:46:02 -0700605 return true;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800606 }
607
608 /* Set state to committing. */
609 item->second->flags |= StateFlags::committing;
610
611 return triggerVerification();
Patrick Venturec7ca2912018-11-02 11:38:33 -0700612}
Patrick Ventured6461d62018-11-09 17:30:25 -0800613
614/*
615 * Close must be called on the firmware image before triggering
616 * verification via commit. Once the verification is complete, you can
617 * then close the hash file.
618 *
619 * If the `verify_image.service` returned success, closing the hash file
620 * will have a specific behavior depending on the update. If it's UBI,
621 * it'll perform the install. If it's static layout, it'll do nothing. The
622 * verify_image service in the static layout case is responsible for placing
623 * the file in the correct staging position.
624 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700625bool FirmwareBlobHandler::close(uint16_t session)
626{
Patrick Venture68bb1432018-11-09 20:08:48 -0800627 auto item = lookup.find(session);
628 if (item == lookup.end())
629 {
630 return false;
631 }
632
Patrick Ventureffcc5502018-11-16 12:32:38 -0800633 /* Are you closing the verify blob? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700634 if (item->second->activePath == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800635 {
Patrick Ventureffcc5502018-11-16 12:32:38 -0800636 if (state == UpdateState::verificationStarted)
637 {
Patrick Venture12233c52019-05-16 09:26:59 -0700638 /* TODO: If they close this blob before verification finishes,
639 * that's an abort.
640 */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800641 return false;
642 }
Patrick Venture12233c52019-05-16 09:26:59 -0700643 else if (state == UpdateState::verificationCompleted)
644 {
645 /* TODO: Should this delete the verification artifact? */
646 state = UpdateState::notYetStarted;
647
648 /* A this point, delete the active blob IDs from the blob_list. */
649 blobIDs.erase(
Patrick Venture7dad86f2019-05-17 08:52:20 -0700650 std::remove(blobIDs.begin(), blobIDs.end(), activeImageBlobId));
Patrick Venture12233c52019-05-16 09:26:59 -0700651 blobIDs.erase(
Patrick Venture7dad86f2019-05-17 08:52:20 -0700652 std::remove(blobIDs.begin(), blobIDs.end(), activeHashBlobId));
Patrick Venture12233c52019-05-16 09:26:59 -0700653 }
654 /* Must be verificationPending... not yet started, they may re-open and
655 * trigger verification.
656 */
657 }
658 else
659 {
660 /* They are closing a data pathway (image, tarball, hash). */
661 state = UpdateState::verificationPending;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800662 }
663
Patrick Venture68bb1432018-11-09 20:08:48 -0800664 if (item->second->dataHandler)
665 {
666 item->second->dataHandler->close();
667 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800668 if (item->second->imageHandler)
669 {
670 item->second->imageHandler->close();
671 }
672
Patrick Venture68bb1432018-11-09 20:08:48 -0800673 item->second->state = Session::State::closed;
674 /* Do not delete the active blob_id from the list of blob_ids, because that
675 * blob_id indicates there is data stored. Delete will destroy it.
676 */
677
678 lookup.erase(item);
679
Patrick Venture991910a2018-11-09 19:43:48 -0800680 fileOpen = false;
681
Patrick Venture68bb1432018-11-09 20:08:48 -0800682 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700683}
Patrick Ventured6461d62018-11-09 17:30:25 -0800684
Patrick Venturec7ca2912018-11-02 11:38:33 -0700685bool FirmwareBlobHandler::expire(uint16_t session)
686{
687 return false;
688}
Patrick Ventured6461d62018-11-09 17:30:25 -0800689
690/*
691 * Currently, the design does not provide this with a function, however,
692 * it will likely change to support reading data back.
693 */
694std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
695 uint32_t offset,
696 uint32_t requestedSize)
697{
698 return {};
699}
700
Patrick Ventureffcc5502018-11-16 12:32:38 -0800701bool FirmwareBlobHandler::triggerVerification()
702{
Patrick Venturecabc1172018-11-16 16:14:26 -0800703 auto method = bus.new_method_call(systemdService, systemdRoot,
704 systemdInterface, "StartUnit");
705 method.append(verifyTarget);
706 method.append("replace");
707
708 try
709 {
710 bus.call_noreply(method);
Patrick Venture453f06a2019-01-17 10:02:12 -0800711 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800712 }
713 catch (const sdbusplus::exception::SdBusError& ex)
714 {
715 /* TODO: Once logging supports unit-tests, add a log message to test
716 * this failure.
717 */
718 return false;
719 }
720
Patrick Ventureffcc5502018-11-16 12:32:38 -0800721 return true;
722}
723
Patrick Venturec7ca2912018-11-02 11:38:33 -0700724} // namespace blobs