blob: 7f86d5f0602162c4e5aa9379f0ef0077ace3d087 [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 Ventureb3b4db72019-05-15 11:30:24 -070040static constexpr auto statusPath = "/tmp/bmc.verify";
Patrick Venturec7ca2912018-11-02 11:38:33 -070041
Patrick Ventureb3b4db72019-05-15 11:30:24 -070042namespace
43{
44
45FirmwareBlobHandler::VerifyCheckResponses checkVerificationState()
46{
47 FirmwareBlobHandler::VerifyCheckResponses result =
48 FirmwareBlobHandler::VerifyCheckResponses::other;
49
50 std::ifstream ifs;
51 ifs.open(statusPath);
52 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 Venture1cde5f92018-11-07 08:26:47 -080082 const std::vector<DataHandlerPack>& transports)
Patrick Venture68cf64f2018-11-06 10:46:51 -080083{
Patrick Venture52854622018-11-06 12:30:00 -080084 /* There must be at least one. */
85 if (!firmwares.size())
86 {
Patrick Ventured333a872018-12-03 16:24:26 -080087 log<level::ERR>("Must provide at least one firmware handler.");
Patrick Venture52854622018-11-06 12:30:00 -080088 return nullptr;
89 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080090 if (!transports.size())
91 {
92 return nullptr;
93 }
Patrick Venture52854622018-11-06 12:30:00 -080094
Patrick Venturea78e39f2018-11-06 18:37:06 -080095 std::vector<std::string> blobs;
96 for (const auto& item : firmwares)
97 {
98 blobs.push_back(item.blobName);
99 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700100 blobs.push_back(verifyBlobId); /* Add blob_id to always exist. */
Patrick Venture18235e62018-11-08 10:21:09 -0800101
Patrick Venture7dad86f2019-05-17 08:52:20 -0700102 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId))
Patrick Venture18235e62018-11-08 10:21:09 -0800103 {
104 return nullptr;
105 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800106
Patrick Venture1cde5f92018-11-07 08:26:47 -0800107 std::uint16_t bitmask = 0;
108 for (const auto& item : transports)
109 {
110 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
111 bitmask |= item.bitmask;
112 }
113
Patrick Venture4eb55952018-11-16 15:36:24 -0800114 return std::make_unique<FirmwareBlobHandler>(std::move(bus), firmwares,
115 blobs, transports, bitmask);
Patrick Venture68cf64f2018-11-06 10:46:51 -0800116}
117
Patrick Ventured6461d62018-11-09 17:30:25 -0800118/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700119bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
120{
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800121 if (std::count(blobIDs.begin(), blobIDs.end(), path))
Patrick Venture137ad2c2018-11-06 11:30:43 -0800122 {
123 return true;
124 }
125
Patrick Venturec7ca2912018-11-02 11:38:33 -0700126 return false;
127}
Patrick Venture53977962018-11-02 18:59:35 -0700128
Patrick Ventured6461d62018-11-09 17:30:25 -0800129/*
130 * Grab the list of supported firmware.
131 *
132 * If there's an open firmware session, it'll already be present in the
133 * list as "/flash/active/image", and if the hash has started,
134 * "/flash/active/hash" regardless of mechanism. This is done in the open
135 * comamnd, no extra work is required here.
136 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700137std::vector<std::string> FirmwareBlobHandler::getBlobIds()
138{
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800139 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700140}
Patrick Venture53977962018-11-02 18:59:35 -0700141
Patrick Ventured6461d62018-11-09 17:30:25 -0800142/*
143 * Per the design, this mean abort, and this will trigger whatever
144 * appropriate actions are required to abort the process.
Patrick Venture9e5aab22018-11-09 20:49:28 -0800145 *
146 * You cannot delete a blob that has an open handle in the system, therefore
147 * this is never called if there's an open session. Guaranteed by the blob
148 * manager.
Patrick Ventured6461d62018-11-09 17:30:25 -0800149 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700150bool FirmwareBlobHandler::deleteBlob(const std::string& path)
151{
Patrick Venture9e5aab22018-11-09 20:49:28 -0800152 const std::string* toDelete;
153
Patrick Ventureffcc5502018-11-16 12:32:38 -0800154 /* You cannot delete the verify blob -- trying to delete it, currently has
155 * no impact.
156 * TODO: Should trying to delete this cause an abort?
157 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700158 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800159 {
160 return false;
161 }
162
Patrick Venture7dad86f2019-05-17 08:52:20 -0700163 if (path == hashBlobId || path == activeHashBlobId)
Patrick Venture9e5aab22018-11-09 20:49:28 -0800164 {
165 /* They're deleting the hash. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700166 toDelete = &activeHashBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800167 }
168 else
169 {
170 /* They're deleting the image. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700171 toDelete = &activeImageBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800172 }
173
174 auto it = std::find_if(
175 blobIDs.begin(), blobIDs.end(),
176 [toDelete](const auto& iter) { return (iter == *toDelete); });
177 if (it == blobIDs.end())
178 {
179 /* Somehow they've asked to delete something we didn't say we could
180 * handle.
181 */
182 return false;
183 }
184
185 blobIDs.erase(it);
186
187 /* TODO: Handle aborting the process and fixing up the state. */
188
189 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700190}
Patrick Venture53977962018-11-02 18:59:35 -0700191
Patrick Ventured6461d62018-11-09 17:30:25 -0800192/*
193 * Stat on the files will return information such as what supported
194 * transport mechanisms are available.
195 *
196 * Stat on an active file or hash will return information such as the size
197 * of the data cached, and any additional pertinent information. The
198 * blob_state on the active files will return the state of the update.
199 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700200bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
201{
Patrick Venture46637c82018-11-06 15:20:24 -0800202 /* We know we support this path because canHandle is called ahead */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700203 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800204 {
Patrick Venture699750d2019-05-15 09:24:09 -0700205 /* TODO: We need to return information for the verify state -- did they
206 * call commit() did things start?
Patrick Ventureffcc5502018-11-16 12:32:38 -0800207 */
208 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700209 else if (path == activeImageBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800210 {
Patrick Venture699750d2019-05-15 09:24:09 -0700211 /* TODO: We need to return information for the image that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800212 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700213 else if (path == activeHashBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800214 {
Patrick Venture699750d2019-05-15 09:24:09 -0700215 /* TODO: We need to return information for the hash that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800216 }
217 else
218 {
219 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800220 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800221 meta->size = 0;
222
223 /* The generic blob_ids state is only the bits related to the transport
Patrick Ventured6461d62018-11-09 17:30:25 -0800224 * mechanisms.
225 */
Patrick Venture46637c82018-11-06 15:20:24 -0800226 return true;
227 }
228
Patrick Venturec7ca2912018-11-02 11:38:33 -0700229 return false;
230}
Patrick Venture53977962018-11-02 18:59:35 -0700231
Patrick Venturec02849b2018-11-06 17:31:15 -0800232/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800233 * Return stat information on an open session. It therefore must be an active
234 * handle to either the active image or active hash.
235 *
236 * The stat() and sessionstat() commands will return the same information in
237 * many cases, therefore the logic will be combined.
238 *
239 * TODO: combine the logic for stat and sessionstat().
240 */
241bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
242{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800243 auto item = lookup.find(session);
244 if (item == lookup.end())
245 {
246 return false;
247 }
248
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700249 /* The size here refers to the size of the file -- of something analagous.
250 */
251 meta->size = (item->second->imageHandler)
252 ? item->second->imageHandler->getSize()
253 : 0;
254
255 meta->metadata.clear();
256
Patrick Venture699750d2019-05-15 09:24:09 -0700257 /* TODO: Implement this for the verification blob, which is what we expect.
258 * Calling stat() on the verify blob without an active session should not
259 * provide insight.
260 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700261 if (item->second->activePath == verifyBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700262 {
Patrick Venturee955e072019-05-15 16:16:46 -0700263 auto value = checkVerificationState();
Patrick Venture699750d2019-05-15 09:24:09 -0700264
Patrick Venturee955e072019-05-15 16:16:46 -0700265 meta->metadata.push_back(static_cast<std::uint8_t>(value));
266
267 /* Change the firmware handler's state and the blob's stat value
268 * depending.
269 */
270 if (value == VerifyCheckResponses::success ||
271 value == VerifyCheckResponses::failed)
272 {
273 state = UpdateState::verificationCompleted;
274 item->second->flags &= ~StateFlags::committing;
275
276 if (value == VerifyCheckResponses::success)
277 {
278 item->second->flags |= StateFlags::committed;
279 }
280 else
281 {
282 item->second->flags |= StateFlags::commit_error;
283 }
284 }
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700285 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800286
Patrick Venturee955e072019-05-15 16:16:46 -0700287 /* The blobState here relates to an active sesion, so we should return the
288 * flags used to open this session.
289 */
290 meta->blobState = item->second->flags;
291
Patrick Venturecc7d1602018-11-15 13:58:33 -0800292 /* The metadata blob returned comes from the data handler... it's used for
293 * instance, in P2A bridging to get required information about the mapping,
294 * and is the "opposite" of the lpc writemeta requirement.
295 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800296 if (item->second->dataHandler)
297 {
Patrick Venture74304642019-01-17 09:31:04 -0800298 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800299 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
300 bytes.end());
301 }
302
Patrick Venturecc7d1602018-11-15 13:58:33 -0800303 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800304}
305
306/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800307 * If you open /flash/image or /flash/tarball, or /flash/hash it will
308 * interpret the open flags and perform whatever actions are required for
309 * that update process. The session returned can be used immediately for
310 * sending data down, without requiring one to open the new active file.
311 *
312 * If you open the active flash image or active hash it will let you
313 * overwrite pieces, depending on the state.
314 *
315 * Once the verification process has started the active files cannot be
316 * opened.
317 *
318 * You can only have one open session at a time. Which means, you can only
319 * have one file open at a time. Trying to open the hash blob_id while you
320 * still have the flash image blob_id open will fail. Opening the flash
321 * blob_id when it is already open will fail.
322 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700323bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
324 const std::string& path)
325{
Patrick Venture6e307a72018-11-09 18:21:17 -0800326 /* Check that they've opened for writing - read back not currently
327 * supported.
328 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800329 if ((flags & OpenFlags::write) == 0)
330 {
331 return false;
332 }
333
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800334 /* Is the verification process underway? */
335 if (state == UpdateState::verificationStarted)
336 {
337 return false;
338 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800339
340 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800341 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800342 * TODO: Temporarily using a simple boolean flag until there's a full
343 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800344 *
345 * Further on this, if there's an active session to the hash we don't allow
346 * re-opening the image, and if we have the image open, we don't allow
347 * opening the hash. This design decision may be re-evaluated, and changed
348 * to only allow one session per object type (of the two types). But,
349 * consider if the hash is open, do we want to allow writing to the image?
350 * And why would we? But, really, the point of no-return is once the
351 * verification process has begun -- which is done via commit() on the hash
352 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700353 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800354 if (fileOpen)
355 {
356 return false;
357 }
358
Patrick Ventureffcc5502018-11-16 12:32:38 -0800359 /* Handle opening the verifyBlobId --> we know the image and hash aren't
360 * open because of the fileOpen check.
361 *
362 * The file must be opened for writing, but no transport mechanism specified
363 * since it's irrelevant.
364 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700365 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800366 {
367 /* In this case, there's no image handler to use, or data handler,
368 * simply set up a session.
369 */
370 verifyImage.flags = flags;
371 verifyImage.state = Session::State::open;
372
373 lookup[session] = &verifyImage;
374
375 fileOpen = true;
376
377 return true;
378 }
379
Patrick Venturec02849b2018-11-06 17:31:15 -0800380 /* There are two abstractions at play, how you get the data and how you
381 * handle that data. such that, whether the data comes from the PCI bridge
382 * or LPC bridge is not connected to whether the data goes into a static
383 * layout flash update or a UBI tarball.
384 */
385
386 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800387 * support what they request.
388 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800389 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800390 {
391 return false;
392 }
393
394 /* 2) there isn't, so what are they opening? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700395 if (path == activeImageBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800396 {
397 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800398 * already started one (due to canHandleBlob's behavior).
399 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800400 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800401 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700402 else if (path == activeHashBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800403 {
404 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800405 * already started one (due to canHandleBlob's behavior).
406 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800407 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800408 }
Patrick Venture18235e62018-11-08 10:21:09 -0800409
410 /* How are they expecting to copy this data? */
411 auto d = std::find_if(
412 transports.begin(), transports.end(),
413 [&flags](const auto& iter) { return (iter.bitmask & flags); });
414 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800415 {
Patrick Venture18235e62018-11-08 10:21:09 -0800416 return false;
417 }
418
419 /* We found the transport handler they requested, no surprise since
420 * above we verify they selected at least one we wanted.
421 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800422
Patrick Venture6e307a72018-11-09 18:21:17 -0800423 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
424 * only non-external data pathway -- but this is just a more generic
425 * approach to that.
426 */
427 if (d->handler)
428 {
429 /* If the data handler open call fails, open fails. */
430 if (!d->handler->open())
431 {
432 return false;
433 }
434 }
435
Patrick Venture70e30bf2019-01-17 10:29:28 -0800436 /* Do we have a file handler for the type of file they're opening.
437 * Note: This should only fail if something is somehow crazy wrong.
438 * Since the canHandle() said yes, and that's tied into the list of explicit
439 * firmware handers (and file handlers, like this'll know where to write the
440 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800441 */
Patrick Venture18235e62018-11-08 10:21:09 -0800442 auto h = std::find_if(
443 handlers.begin(), handlers.end(),
444 [&path](const auto& iter) { return (iter.blobName == path); });
445 if (h == handlers.end())
446 {
447 return false;
448 }
449
450 /* Ok, so we found a handler that matched, so call open() */
451 if (!h->handler->open(path))
452 {
453 return false;
454 }
455
Patrick Venture70e30bf2019-01-17 10:29:28 -0800456 Session* curr;
457 const std::string* active;
458
Patrick Venture7dad86f2019-05-17 08:52:20 -0700459 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800460 {
461 /* 2c) are they opening the /flash/hash ? (to start the process) */
462 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700463 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800464 }
465 else
466 {
467 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700468 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800469 }
470
Patrick Venture18235e62018-11-08 10:21:09 -0800471 curr->flags = flags;
472 curr->dataHandler = d->handler;
473 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800474 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800475
476 lookup[session] = curr;
477
Patrick Venturedaf47072019-05-10 15:21:55 -0700478 /* This may be them re-opening a blob, so let's only push it onto the list
479 * when appropriate.
480 */
481 auto blobIdMatch =
482 std::find_if(blobIDs.begin(), blobIDs.end(),
483 [active](const auto& iter) { return (iter == *active); });
484 if (blobIdMatch == blobIDs.end())
485 {
486 blobIDs.push_back(*active);
487 }
Patrick Venturedb253bf2018-11-09 19:36:03 -0800488
Patrick Venture12233c52019-05-16 09:26:59 -0700489 state = UpdateState::uploadInProgress;
Patrick Venture991910a2018-11-09 19:43:48 -0800490 fileOpen = true;
491
Patrick Venture18235e62018-11-08 10:21:09 -0800492 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700493}
Patrick Venture53977962018-11-02 18:59:35 -0700494
Patrick Venture18235e62018-11-08 10:21:09 -0800495/**
496 * The write command really just grabs the data from wherever it is and sends it
497 * to the image handler. It's the image handler's responsibility to deal with
498 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800499 *
500 * This receives a session from the blob manager, therefore it is always called
501 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800502 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700503bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
504 const std::vector<uint8_t>& data)
505{
Patrick Venture18235e62018-11-08 10:21:09 -0800506 auto item = lookup.find(session);
507 if (item == lookup.end())
508 {
509 return false;
510 }
511
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800512 /* Prevent writing during verification. */
513 if (state == UpdateState::verificationStarted)
514 {
515 return false;
516 }
517
Patrick Venture8af15eb2019-05-15 09:39:22 -0700518 /* Prevent writing to the verification blob before they trigger
Patrick Venture699750d2019-05-15 09:24:09 -0700519 * verification.
520 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700521 if (item->second->activePath == verifyBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700522 {
523 return false;
524 }
Patrick Venture699750d2019-05-15 09:24:09 -0700525
Patrick Venture18235e62018-11-08 10:21:09 -0800526 std::vector<std::uint8_t> bytes;
527
Patrick Venture05abf7e2018-11-09 11:02:56 -0800528 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800529 {
530 bytes = data;
531 }
532 else
533 {
534 /* little endian required per design, and so on, but TODO: do endianness
535 * with boost.
536 */
537 struct ExtChunkHdr header;
538
539 if (data.size() != sizeof(header))
540 {
541 return false;
542 }
543
544 std::memcpy(&header, data.data(), data.size());
545 bytes = item->second->dataHandler->copyFrom(header.length);
546 }
547
548 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700549}
Patrick Venture18235e62018-11-08 10:21:09 -0800550
Patrick Venture8c535332018-11-08 15:58:00 -0800551/*
552 * If the active session (image or hash) is over LPC, this allows
553 * configuring it. This option is only available before you start
554 * writing data for the given item (image or hash). This will return
555 * false at any other part. -- the lpc handler portion will know to return
556 * false.
557 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700558bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
559 const std::vector<uint8_t>& data)
560{
Patrick Venture8c535332018-11-08 15:58:00 -0800561 auto item = lookup.find(session);
562 if (item == lookup.end())
563 {
564 return false;
565 }
566
Patrick Venture05abf7e2018-11-09 11:02:56 -0800567 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800568 {
569 return false;
570 }
571
Patrick Ventured56097a2019-05-15 09:47:55 -0700572 /* Prevent writing meta to the verification blob (it has no data handler).
573 */
574 if (item->second->dataHandler)
575 {
576 return item->second->dataHandler->writeMeta(data);
577 }
Patrick Venture699750d2019-05-15 09:24:09 -0700578
Patrick Ventured56097a2019-05-15 09:47:55 -0700579 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700580}
Patrick Venture8c535332018-11-08 15:58:00 -0800581
Patrick Ventured6461d62018-11-09 17:30:25 -0800582/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700583 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800584 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800585 * the image.
586 *
587 * For this file to have opened, the other two must be closed, which means any
588 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800589 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700590bool FirmwareBlobHandler::commit(uint16_t session,
591 const std::vector<uint8_t>& data)
592{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800593 auto item = lookup.find(session);
594 if (item == lookup.end())
595 {
596 return false;
597 }
598
599 /* You can only commit on the verifyBlodId */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700600 if (item->second->activePath != verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800601 {
602 return false;
603 }
604
Patrick Venturebe198702019-05-15 09:46:02 -0700605 /* Calling repeatedly has no effect within an update process. */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800606 if (state == UpdateState::verificationStarted)
607 {
Patrick Venturebe198702019-05-15 09:46:02 -0700608 return true;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800609 }
610
611 /* Set state to committing. */
612 item->second->flags |= StateFlags::committing;
613
614 return triggerVerification();
Patrick Venturec7ca2912018-11-02 11:38:33 -0700615}
Patrick Ventured6461d62018-11-09 17:30:25 -0800616
617/*
618 * Close must be called on the firmware image before triggering
619 * verification via commit. Once the verification is complete, you can
620 * then close the hash file.
621 *
622 * If the `verify_image.service` returned success, closing the hash file
623 * will have a specific behavior depending on the update. If it's UBI,
624 * it'll perform the install. If it's static layout, it'll do nothing. The
625 * verify_image service in the static layout case is responsible for placing
626 * the file in the correct staging position.
627 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700628bool FirmwareBlobHandler::close(uint16_t session)
629{
Patrick Venture68bb1432018-11-09 20:08:48 -0800630 auto item = lookup.find(session);
631 if (item == lookup.end())
632 {
633 return false;
634 }
635
Patrick Ventureffcc5502018-11-16 12:32:38 -0800636 /* Are you closing the verify blob? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700637 if (item->second->activePath == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800638 {
Patrick Ventureffcc5502018-11-16 12:32:38 -0800639 if (state == UpdateState::verificationStarted)
640 {
Patrick Venture12233c52019-05-16 09:26:59 -0700641 /* TODO: If they close this blob before verification finishes,
642 * that's an abort.
643 */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800644 return false;
645 }
Patrick Venture12233c52019-05-16 09:26:59 -0700646 else if (state == UpdateState::verificationCompleted)
647 {
648 /* TODO: Should this delete the verification artifact? */
649 state = UpdateState::notYetStarted;
650
651 /* A this point, delete the active blob IDs from the blob_list. */
652 blobIDs.erase(
Patrick Venture7dad86f2019-05-17 08:52:20 -0700653 std::remove(blobIDs.begin(), blobIDs.end(), activeImageBlobId));
Patrick Venture12233c52019-05-16 09:26:59 -0700654 blobIDs.erase(
Patrick Venture7dad86f2019-05-17 08:52:20 -0700655 std::remove(blobIDs.begin(), blobIDs.end(), activeHashBlobId));
Patrick Venture12233c52019-05-16 09:26:59 -0700656 }
657 /* Must be verificationPending... not yet started, they may re-open and
658 * trigger verification.
659 */
660 }
661 else
662 {
663 /* They are closing a data pathway (image, tarball, hash). */
664 state = UpdateState::verificationPending;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800665 }
666
Patrick Venture68bb1432018-11-09 20:08:48 -0800667 if (item->second->dataHandler)
668 {
669 item->second->dataHandler->close();
670 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800671 if (item->second->imageHandler)
672 {
673 item->second->imageHandler->close();
674 }
675
Patrick Venture68bb1432018-11-09 20:08:48 -0800676 item->second->state = Session::State::closed;
677 /* Do not delete the active blob_id from the list of blob_ids, because that
678 * blob_id indicates there is data stored. Delete will destroy it.
679 */
680
681 lookup.erase(item);
682
Patrick Venture991910a2018-11-09 19:43:48 -0800683 fileOpen = false;
684
Patrick Venture68bb1432018-11-09 20:08:48 -0800685 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700686}
Patrick Ventured6461d62018-11-09 17:30:25 -0800687
Patrick Venturec7ca2912018-11-02 11:38:33 -0700688bool FirmwareBlobHandler::expire(uint16_t session)
689{
690 return false;
691}
Patrick Ventured6461d62018-11-09 17:30:25 -0800692
693/*
694 * Currently, the design does not provide this with a function, however,
695 * it will likely change to support reading data back.
696 */
697std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
698 uint32_t offset,
699 uint32_t requestedSize)
700{
701 return {};
702}
703
Patrick Ventureffcc5502018-11-16 12:32:38 -0800704bool FirmwareBlobHandler::triggerVerification()
705{
Patrick Venturecabc1172018-11-16 16:14:26 -0800706 auto method = bus.new_method_call(systemdService, systemdRoot,
707 systemdInterface, "StartUnit");
708 method.append(verifyTarget);
709 method.append("replace");
710
711 try
712 {
713 bus.call_noreply(method);
Patrick Venture453f06a2019-01-17 10:02:12 -0800714 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800715 }
716 catch (const sdbusplus::exception::SdBusError& ex)
717 {
718 /* TODO: Once logging supports unit-tests, add a log message to test
719 * this failure.
720 */
721 return false;
722 }
723
Patrick Ventureffcc5502018-11-16 12:32:38 -0800724 return true;
725}
726
Patrick Venturec7ca2912018-11-02 11:38:33 -0700727} // namespace blobs