blob: 5873c69c5222d6e1706a5788d9b5da55ff3b224f [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 Venture4cceb8e2018-11-06 11:56:48 -0800123 if (std::count(blobIDs.begin(), blobIDs.end(), path))
Patrick Venture137ad2c2018-11-06 11:30:43 -0800124 {
125 return true;
126 }
127
Patrick Venturec7ca2912018-11-02 11:38:33 -0700128 return false;
129}
Patrick Venture53977962018-11-02 18:59:35 -0700130
Patrick Ventured6461d62018-11-09 17:30:25 -0800131/*
132 * Grab the list of supported firmware.
133 *
134 * If there's an open firmware session, it'll already be present in the
135 * list as "/flash/active/image", and if the hash has started,
136 * "/flash/active/hash" regardless of mechanism. This is done in the open
137 * comamnd, no extra work is required here.
138 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700139std::vector<std::string> FirmwareBlobHandler::getBlobIds()
140{
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800141 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700142}
Patrick Venture53977962018-11-02 18:59:35 -0700143
Patrick Ventured6461d62018-11-09 17:30:25 -0800144/*
145 * Per the design, this mean abort, and this will trigger whatever
146 * appropriate actions are required to abort the process.
Patrick Venture9e5aab22018-11-09 20:49:28 -0800147 *
148 * You cannot delete a blob that has an open handle in the system, therefore
149 * this is never called if there's an open session. Guaranteed by the blob
150 * manager.
Patrick Ventured6461d62018-11-09 17:30:25 -0800151 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700152bool FirmwareBlobHandler::deleteBlob(const std::string& path)
153{
Patrick Venture9e5aab22018-11-09 20:49:28 -0800154 const std::string* toDelete;
155
Patrick Ventureffcc5502018-11-16 12:32:38 -0800156 /* You cannot delete the verify blob -- trying to delete it, currently has
157 * no impact.
158 * TODO: Should trying to delete this cause an abort?
159 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700160 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800161 {
162 return false;
163 }
164
Patrick Venture7dad86f2019-05-17 08:52:20 -0700165 if (path == hashBlobId || path == activeHashBlobId)
Patrick Venture9e5aab22018-11-09 20:49:28 -0800166 {
167 /* They're deleting the hash. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700168 toDelete = &activeHashBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800169 }
170 else
171 {
172 /* They're deleting the image. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700173 toDelete = &activeImageBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800174 }
175
176 auto it = std::find_if(
177 blobIDs.begin(), blobIDs.end(),
178 [toDelete](const auto& iter) { return (iter == *toDelete); });
179 if (it == blobIDs.end())
180 {
181 /* Somehow they've asked to delete something we didn't say we could
182 * handle.
183 */
184 return false;
185 }
186
187 blobIDs.erase(it);
188
189 /* TODO: Handle aborting the process and fixing up the state. */
190
191 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700192}
Patrick Venture53977962018-11-02 18:59:35 -0700193
Patrick Ventured6461d62018-11-09 17:30:25 -0800194/*
195 * Stat on the files will return information such as what supported
196 * transport mechanisms are available.
197 *
198 * Stat on an active file or hash will return information such as the size
199 * of the data cached, and any additional pertinent information. The
200 * blob_state on the active files will return the state of the update.
201 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700202bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
203{
Patrick Venture46637c82018-11-06 15:20:24 -0800204 /* We know we support this path because canHandle is called ahead */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700205 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800206 {
Patrick Venture699750d2019-05-15 09:24:09 -0700207 /* TODO: We need to return information for the verify state -- did they
208 * call commit() did things start?
Patrick Ventureffcc5502018-11-16 12:32:38 -0800209 */
210 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700211 else if (path == activeImageBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800212 {
Patrick Venture699750d2019-05-15 09:24:09 -0700213 /* TODO: We need to return information for the image that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800214 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700215 else if (path == activeHashBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800216 {
Patrick Venture699750d2019-05-15 09:24:09 -0700217 /* TODO: We need to return information for the hash that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800218 }
219 else
220 {
221 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800222 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800223 meta->size = 0;
224
225 /* The generic blob_ids state is only the bits related to the transport
Patrick Ventured6461d62018-11-09 17:30:25 -0800226 * mechanisms.
227 */
Patrick Venture46637c82018-11-06 15:20:24 -0800228 return true;
229 }
230
Patrick Venturec7ca2912018-11-02 11:38:33 -0700231 return false;
232}
Patrick Venture53977962018-11-02 18:59:35 -0700233
Patrick Venturec02849b2018-11-06 17:31:15 -0800234/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800235 * Return stat information on an open session. It therefore must be an active
236 * handle to either the active image or active hash.
237 *
238 * The stat() and sessionstat() commands will return the same information in
239 * many cases, therefore the logic will be combined.
240 *
241 * TODO: combine the logic for stat and sessionstat().
242 */
243bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
244{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800245 auto item = lookup.find(session);
246 if (item == lookup.end())
247 {
248 return false;
249 }
250
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700251 /* The size here refers to the size of the file -- of something analagous.
252 */
253 meta->size = (item->second->imageHandler)
254 ? item->second->imageHandler->getSize()
255 : 0;
256
257 meta->metadata.clear();
258
Patrick Venture699750d2019-05-15 09:24:09 -0700259 /* TODO: Implement this for the verification blob, which is what we expect.
260 * Calling stat() on the verify blob without an active session should not
261 * provide insight.
262 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700263 if (item->second->activePath == verifyBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700264 {
Patrick Venture74059d62019-05-17 10:40:26 -0700265 auto value = checkVerificationState(verificationPath);
Patrick Venture699750d2019-05-15 09:24:09 -0700266
Patrick Venturee955e072019-05-15 16:16:46 -0700267 meta->metadata.push_back(static_cast<std::uint8_t>(value));
268
269 /* Change the firmware handler's state and the blob's stat value
270 * depending.
271 */
272 if (value == VerifyCheckResponses::success ||
273 value == VerifyCheckResponses::failed)
274 {
275 state = UpdateState::verificationCompleted;
276 item->second->flags &= ~StateFlags::committing;
277
278 if (value == VerifyCheckResponses::success)
279 {
280 item->second->flags |= StateFlags::committed;
281 }
282 else
283 {
284 item->second->flags |= StateFlags::commit_error;
285 }
286 }
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700287 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800288
Patrick Venturee955e072019-05-15 16:16:46 -0700289 /* The blobState here relates to an active sesion, so we should return the
290 * flags used to open this session.
291 */
292 meta->blobState = item->second->flags;
293
Patrick Venturecc7d1602018-11-15 13:58:33 -0800294 /* The metadata blob returned comes from the data handler... it's used for
295 * instance, in P2A bridging to get required information about the mapping,
296 * and is the "opposite" of the lpc writemeta requirement.
297 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800298 if (item->second->dataHandler)
299 {
Patrick Venture74304642019-01-17 09:31:04 -0800300 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800301 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
302 bytes.end());
303 }
304
Patrick Venturecc7d1602018-11-15 13:58:33 -0800305 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800306}
307
308/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800309 * If you open /flash/image or /flash/tarball, or /flash/hash it will
310 * interpret the open flags and perform whatever actions are required for
311 * that update process. The session returned can be used immediately for
312 * sending data down, without requiring one to open the new active file.
313 *
314 * If you open the active flash image or active hash it will let you
315 * overwrite pieces, depending on the state.
316 *
317 * Once the verification process has started the active files cannot be
318 * opened.
319 *
320 * You can only have one open session at a time. Which means, you can only
321 * have one file open at a time. Trying to open the hash blob_id while you
322 * still have the flash image blob_id open will fail. Opening the flash
323 * blob_id when it is already open will fail.
324 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700325bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
326 const std::string& path)
327{
Patrick Venture6e307a72018-11-09 18:21:17 -0800328 /* Check that they've opened for writing - read back not currently
329 * supported.
330 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800331 if ((flags & OpenFlags::write) == 0)
332 {
333 return false;
334 }
335
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800336 /* Is the verification process underway? */
337 if (state == UpdateState::verificationStarted)
338 {
339 return false;
340 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800341
342 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800343 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800344 * TODO: Temporarily using a simple boolean flag until there's a full
345 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800346 *
347 * Further on this, if there's an active session to the hash we don't allow
348 * re-opening the image, and if we have the image open, we don't allow
349 * opening the hash. This design decision may be re-evaluated, and changed
350 * to only allow one session per object type (of the two types). But,
351 * consider if the hash is open, do we want to allow writing to the image?
352 * And why would we? But, really, the point of no-return is once the
353 * verification process has begun -- which is done via commit() on the hash
354 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700355 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800356 if (fileOpen)
357 {
358 return false;
359 }
360
Patrick Ventureffcc5502018-11-16 12:32:38 -0800361 /* Handle opening the verifyBlobId --> we know the image and hash aren't
362 * open because of the fileOpen check.
363 *
364 * The file must be opened for writing, but no transport mechanism specified
365 * since it's irrelevant.
366 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700367 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800368 {
369 /* In this case, there's no image handler to use, or data handler,
370 * simply set up a session.
371 */
372 verifyImage.flags = flags;
373 verifyImage.state = Session::State::open;
374
375 lookup[session] = &verifyImage;
376
377 fileOpen = true;
378
379 return true;
380 }
381
Patrick Venturec02849b2018-11-06 17:31:15 -0800382 /* There are two abstractions at play, how you get the data and how you
383 * handle that data. such that, whether the data comes from the PCI bridge
384 * or LPC bridge is not connected to whether the data goes into a static
385 * layout flash update or a UBI tarball.
386 */
387
388 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800389 * support what they request.
390 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800391 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800392 {
393 return false;
394 }
395
396 /* 2) there isn't, so what are they opening? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700397 if (path == activeImageBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800398 {
399 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800400 * already started one (due to canHandleBlob's behavior).
401 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800402 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800403 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700404 else if (path == activeHashBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800405 {
406 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800407 * already started one (due to canHandleBlob's behavior).
408 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800409 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800410 }
Patrick Venture18235e62018-11-08 10:21:09 -0800411
412 /* How are they expecting to copy this data? */
413 auto d = std::find_if(
414 transports.begin(), transports.end(),
415 [&flags](const auto& iter) { return (iter.bitmask & flags); });
416 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800417 {
Patrick Venture18235e62018-11-08 10:21:09 -0800418 return false;
419 }
420
421 /* We found the transport handler they requested, no surprise since
422 * above we verify they selected at least one we wanted.
423 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800424
Patrick Venture6e307a72018-11-09 18:21:17 -0800425 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
426 * only non-external data pathway -- but this is just a more generic
427 * approach to that.
428 */
429 if (d->handler)
430 {
431 /* If the data handler open call fails, open fails. */
432 if (!d->handler->open())
433 {
434 return false;
435 }
436 }
437
Patrick Venture70e30bf2019-01-17 10:29:28 -0800438 /* Do we have a file handler for the type of file they're opening.
439 * Note: This should only fail if something is somehow crazy wrong.
440 * Since the canHandle() said yes, and that's tied into the list of explicit
441 * firmware handers (and file handlers, like this'll know where to write the
442 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800443 */
Patrick Venture18235e62018-11-08 10:21:09 -0800444 auto h = std::find_if(
445 handlers.begin(), handlers.end(),
446 [&path](const auto& iter) { return (iter.blobName == path); });
447 if (h == handlers.end())
448 {
449 return false;
450 }
451
452 /* Ok, so we found a handler that matched, so call open() */
453 if (!h->handler->open(path))
454 {
455 return false;
456 }
457
Patrick Venture70e30bf2019-01-17 10:29:28 -0800458 Session* curr;
459 const std::string* active;
460
Patrick Venture7dad86f2019-05-17 08:52:20 -0700461 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800462 {
463 /* 2c) are they opening the /flash/hash ? (to start the process) */
464 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700465 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800466 }
467 else
468 {
469 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700470 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800471 }
472
Patrick Venture18235e62018-11-08 10:21:09 -0800473 curr->flags = flags;
474 curr->dataHandler = d->handler;
475 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800476 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800477
478 lookup[session] = curr;
479
Patrick Venturedaf47072019-05-10 15:21:55 -0700480 /* This may be them re-opening a blob, so let's only push it onto the list
481 * when appropriate.
482 */
483 auto blobIdMatch =
484 std::find_if(blobIDs.begin(), blobIDs.end(),
485 [active](const auto& iter) { return (iter == *active); });
486 if (blobIdMatch == blobIDs.end())
487 {
488 blobIDs.push_back(*active);
489 }
Patrick Venturedb253bf2018-11-09 19:36:03 -0800490
Patrick Venture12233c52019-05-16 09:26:59 -0700491 state = UpdateState::uploadInProgress;
Patrick Venture991910a2018-11-09 19:43:48 -0800492 fileOpen = true;
493
Patrick Venture18235e62018-11-08 10:21:09 -0800494 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700495}
Patrick Venture53977962018-11-02 18:59:35 -0700496
Patrick Venture18235e62018-11-08 10:21:09 -0800497/**
498 * The write command really just grabs the data from wherever it is and sends it
499 * to the image handler. It's the image handler's responsibility to deal with
500 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800501 *
502 * This receives a session from the blob manager, therefore it is always called
503 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800504 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700505bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
506 const std::vector<uint8_t>& data)
507{
Patrick Venture18235e62018-11-08 10:21:09 -0800508 auto item = lookup.find(session);
509 if (item == lookup.end())
510 {
511 return false;
512 }
513
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800514 /* Prevent writing during verification. */
515 if (state == UpdateState::verificationStarted)
516 {
517 return false;
518 }
519
Patrick Venture8af15eb2019-05-15 09:39:22 -0700520 /* Prevent writing to the verification blob before they trigger
Patrick Venture699750d2019-05-15 09:24:09 -0700521 * verification.
522 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700523 if (item->second->activePath == verifyBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700524 {
525 return false;
526 }
Patrick Venture699750d2019-05-15 09:24:09 -0700527
Patrick Venture18235e62018-11-08 10:21:09 -0800528 std::vector<std::uint8_t> bytes;
529
Patrick Venture05abf7e2018-11-09 11:02:56 -0800530 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800531 {
532 bytes = data;
533 }
534 else
535 {
536 /* little endian required per design, and so on, but TODO: do endianness
537 * with boost.
538 */
539 struct ExtChunkHdr header;
540
541 if (data.size() != sizeof(header))
542 {
543 return false;
544 }
545
546 std::memcpy(&header, data.data(), data.size());
547 bytes = item->second->dataHandler->copyFrom(header.length);
548 }
549
550 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700551}
Patrick Venture18235e62018-11-08 10:21:09 -0800552
Patrick Venture8c535332018-11-08 15:58:00 -0800553/*
554 * If the active session (image or hash) is over LPC, this allows
555 * configuring it. This option is only available before you start
556 * writing data for the given item (image or hash). This will return
557 * false at any other part. -- the lpc handler portion will know to return
558 * false.
559 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700560bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
561 const std::vector<uint8_t>& data)
562{
Patrick Venture8c535332018-11-08 15:58:00 -0800563 auto item = lookup.find(session);
564 if (item == lookup.end())
565 {
566 return false;
567 }
568
Patrick Venture05abf7e2018-11-09 11:02:56 -0800569 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800570 {
571 return false;
572 }
573
Patrick Ventured56097a2019-05-15 09:47:55 -0700574 /* Prevent writing meta to the verification blob (it has no data handler).
575 */
576 if (item->second->dataHandler)
577 {
578 return item->second->dataHandler->writeMeta(data);
579 }
Patrick Venture699750d2019-05-15 09:24:09 -0700580
Patrick Ventured56097a2019-05-15 09:47:55 -0700581 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700582}
Patrick Venture8c535332018-11-08 15:58:00 -0800583
Patrick Ventured6461d62018-11-09 17:30:25 -0800584/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700585 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800586 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800587 * the image.
588 *
589 * For this file to have opened, the other two must be closed, which means any
590 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800591 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700592bool FirmwareBlobHandler::commit(uint16_t session,
593 const std::vector<uint8_t>& data)
594{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800595 auto item = lookup.find(session);
596 if (item == lookup.end())
597 {
598 return false;
599 }
600
601 /* You can only commit on the verifyBlodId */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700602 if (item->second->activePath != verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800603 {
604 return false;
605 }
606
Patrick Venturebe198702019-05-15 09:46:02 -0700607 /* Calling repeatedly has no effect within an update process. */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800608 if (state == UpdateState::verificationStarted)
609 {
Patrick Venturebe198702019-05-15 09:46:02 -0700610 return true;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800611 }
612
613 /* Set state to committing. */
614 item->second->flags |= StateFlags::committing;
615
616 return triggerVerification();
Patrick Venturec7ca2912018-11-02 11:38:33 -0700617}
Patrick Ventured6461d62018-11-09 17:30:25 -0800618
619/*
620 * Close must be called on the firmware image before triggering
621 * verification via commit. Once the verification is complete, you can
622 * then close the hash file.
623 *
624 * If the `verify_image.service` returned success, closing the hash file
625 * will have a specific behavior depending on the update. If it's UBI,
626 * it'll perform the install. If it's static layout, it'll do nothing. The
627 * verify_image service in the static layout case is responsible for placing
628 * the file in the correct staging position.
629 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700630bool FirmwareBlobHandler::close(uint16_t session)
631{
Patrick Venture68bb1432018-11-09 20:08:48 -0800632 auto item = lookup.find(session);
633 if (item == lookup.end())
634 {
635 return false;
636 }
637
Patrick Ventureffcc5502018-11-16 12:32:38 -0800638 /* Are you closing the verify blob? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700639 if (item->second->activePath == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800640 {
Patrick Ventureffcc5502018-11-16 12:32:38 -0800641 if (state == UpdateState::verificationStarted)
642 {
Patrick Venture12233c52019-05-16 09:26:59 -0700643 /* TODO: If they close this blob before verification finishes,
644 * that's an abort.
645 */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800646 return false;
647 }
Patrick Venture12233c52019-05-16 09:26:59 -0700648 else if (state == UpdateState::verificationCompleted)
649 {
650 /* TODO: Should this delete the verification artifact? */
651 state = UpdateState::notYetStarted;
652
653 /* A this point, delete the active blob IDs from the blob_list. */
654 blobIDs.erase(
Patrick Venture7dad86f2019-05-17 08:52:20 -0700655 std::remove(blobIDs.begin(), blobIDs.end(), activeImageBlobId));
Patrick Venture12233c52019-05-16 09:26:59 -0700656 blobIDs.erase(
Patrick Venture7dad86f2019-05-17 08:52:20 -0700657 std::remove(blobIDs.begin(), blobIDs.end(), activeHashBlobId));
Patrick Venture12233c52019-05-16 09:26:59 -0700658 }
659 /* Must be verificationPending... not yet started, they may re-open and
660 * trigger verification.
661 */
662 }
663 else
664 {
665 /* They are closing a data pathway (image, tarball, hash). */
666 state = UpdateState::verificationPending;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800667 }
668
Patrick Venture68bb1432018-11-09 20:08:48 -0800669 if (item->second->dataHandler)
670 {
671 item->second->dataHandler->close();
672 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800673 if (item->second->imageHandler)
674 {
675 item->second->imageHandler->close();
676 }
677
Patrick Venture68bb1432018-11-09 20:08:48 -0800678 item->second->state = Session::State::closed;
679 /* Do not delete the active blob_id from the list of blob_ids, because that
680 * blob_id indicates there is data stored. Delete will destroy it.
681 */
682
683 lookup.erase(item);
684
Patrick Venture991910a2018-11-09 19:43:48 -0800685 fileOpen = false;
686
Patrick Venture68bb1432018-11-09 20:08:48 -0800687 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700688}
Patrick Ventured6461d62018-11-09 17:30:25 -0800689
Patrick Venturec7ca2912018-11-02 11:38:33 -0700690bool FirmwareBlobHandler::expire(uint16_t session)
691{
692 return false;
693}
Patrick Ventured6461d62018-11-09 17:30:25 -0800694
695/*
696 * Currently, the design does not provide this with a function, however,
697 * it will likely change to support reading data back.
698 */
699std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
700 uint32_t offset,
701 uint32_t requestedSize)
702{
703 return {};
704}
705
Patrick Ventureffcc5502018-11-16 12:32:38 -0800706bool FirmwareBlobHandler::triggerVerification()
707{
Patrick Venturecabc1172018-11-16 16:14:26 -0800708 auto method = bus.new_method_call(systemdService, systemdRoot,
709 systemdInterface, "StartUnit");
710 method.append(verifyTarget);
711 method.append("replace");
712
713 try
714 {
715 bus.call_noreply(method);
Patrick Venture453f06a2019-01-17 10:02:12 -0800716 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800717 }
718 catch (const sdbusplus::exception::SdBusError& ex)
719 {
720 /* TODO: Once logging supports unit-tests, add a log message to test
721 * this failure.
722 */
723 return false;
724 }
725
Patrick Ventureffcc5502018-11-16 12:32:38 -0800726 return true;
727}
728
Patrick Venturec7ca2912018-11-02 11:38:33 -0700729} // namespace blobs