blob: 5f9a089e893d1f54972f3365a6af2d510c3df874 [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 Venture26a17262019-05-20 11:03:35 -070021#include "verify.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -080022
Patrick Venture137ad2c2018-11-06 11:30:43 -080023#include <algorithm>
Patrick Venture192d60f2018-11-06 11:11:59 -080024#include <cstdint>
Patrick Venture18235e62018-11-08 10:21:09 -080025#include <cstring>
Patrick Ventureb3b4db72019-05-15 11:30:24 -070026#include <fstream>
Patrick Venture68cf64f2018-11-06 10:46:51 -080027#include <memory>
Patrick Ventured333a872018-12-03 16:24:26 -080028#include <phosphor-logging/log.hpp>
Patrick Venturefa6c4d92018-11-02 18:34:53 -070029#include <string>
30#include <vector>
31
Patrick Ventured333a872018-12-03 16:24:26 -080032using namespace phosphor::logging;
33
Patrick Venturec7ca2912018-11-02 11:38:33 -070034namespace blobs
35{
Patrick Ventureb3b4db72019-05-15 11:30:24 -070036
Patrick Venture68cf64f2018-11-06 10:46:51 -080037std::unique_ptr<GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080038 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070039 const std::vector<HandlerPack>& firmwares,
Patrick Venture74059d62019-05-17 10:40:26 -070040 const std::vector<DataHandlerPack>& transports,
Patrick Venture3ecb3502019-05-17 11:03:51 -070041 std::unique_ptr<VerificationInterface> verification)
Patrick Venture68cf64f2018-11-06 10:46:51 -080042{
Patrick Venture52854622018-11-06 12:30:00 -080043 /* There must be at least one. */
44 if (!firmwares.size())
45 {
Patrick Ventured333a872018-12-03 16:24:26 -080046 log<level::ERR>("Must provide at least one firmware handler.");
Patrick Venture52854622018-11-06 12:30:00 -080047 return nullptr;
48 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080049 if (!transports.size())
50 {
51 return nullptr;
52 }
Patrick Venture52854622018-11-06 12:30:00 -080053
Patrick Venturea78e39f2018-11-06 18:37:06 -080054 std::vector<std::string> blobs;
55 for (const auto& item : firmwares)
56 {
57 blobs.push_back(item.blobName);
58 }
Patrick Venture7dad86f2019-05-17 08:52:20 -070059 blobs.push_back(verifyBlobId); /* Add blob_id to always exist. */
Patrick Venture18235e62018-11-08 10:21:09 -080060
Patrick Venture7dad86f2019-05-17 08:52:20 -070061 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId))
Patrick Venture18235e62018-11-08 10:21:09 -080062 {
63 return nullptr;
64 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080065
Patrick Venture1cde5f92018-11-07 08:26:47 -080066 std::uint16_t bitmask = 0;
67 for (const auto& item : transports)
68 {
69 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
70 bitmask |= item.bitmask;
71 }
72
Patrick Venture3ecb3502019-05-17 11:03:51 -070073 return std::make_unique<FirmwareBlobHandler>(
74 firmwares, blobs, transports, bitmask, std::move(verification));
Patrick Venture68cf64f2018-11-06 10:46:51 -080075}
76
Patrick Ventured6461d62018-11-09 17:30:25 -080077/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070078bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
79{
Patrick Venture6032dc02019-05-17 11:01:44 -070080 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070081}
Patrick Venture53977962018-11-02 18:59:35 -070082
Patrick Ventured6461d62018-11-09 17:30:25 -080083/*
84 * Grab the list of supported firmware.
85 *
86 * If there's an open firmware session, it'll already be present in the
87 * list as "/flash/active/image", and if the hash has started,
88 * "/flash/active/hash" regardless of mechanism. This is done in the open
89 * comamnd, no extra work is required here.
90 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070091std::vector<std::string> FirmwareBlobHandler::getBlobIds()
92{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080093 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070094}
Patrick Venture53977962018-11-02 18:59:35 -070095
Patrick Ventured6461d62018-11-09 17:30:25 -080096/*
97 * Per the design, this mean abort, and this will trigger whatever
98 * appropriate actions are required to abort the process.
Patrick Venture9e5aab22018-11-09 20:49:28 -080099 *
100 * You cannot delete a blob that has an open handle in the system, therefore
101 * this is never called if there's an open session. Guaranteed by the blob
102 * manager.
Patrick Ventured6461d62018-11-09 17:30:25 -0800103 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700104bool FirmwareBlobHandler::deleteBlob(const std::string& path)
105{
Patrick Venture9e5aab22018-11-09 20:49:28 -0800106 const std::string* toDelete;
107
Patrick Ventureffcc5502018-11-16 12:32:38 -0800108 /* You cannot delete the verify blob -- trying to delete it, currently has
109 * no impact.
110 * TODO: Should trying to delete this cause an abort?
111 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700112 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800113 {
114 return false;
115 }
116
Patrick Venture7dad86f2019-05-17 08:52:20 -0700117 if (path == hashBlobId || path == activeHashBlobId)
Patrick Venture9e5aab22018-11-09 20:49:28 -0800118 {
119 /* They're deleting the hash. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700120 toDelete = &activeHashBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800121 }
122 else
123 {
124 /* They're deleting the image. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700125 toDelete = &activeImageBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800126 }
127
128 auto it = std::find_if(
129 blobIDs.begin(), blobIDs.end(),
130 [toDelete](const auto& iter) { return (iter == *toDelete); });
131 if (it == blobIDs.end())
132 {
133 /* Somehow they've asked to delete something we didn't say we could
134 * handle.
135 */
136 return false;
137 }
138
139 blobIDs.erase(it);
140
141 /* TODO: Handle aborting the process and fixing up the state. */
142
143 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700144}
Patrick Venture53977962018-11-02 18:59:35 -0700145
Patrick Ventured6461d62018-11-09 17:30:25 -0800146/*
147 * Stat on the files will return information such as what supported
148 * transport mechanisms are available.
149 *
150 * Stat on an active file or hash will return information such as the size
151 * of the data cached, and any additional pertinent information. The
152 * blob_state on the active files will return the state of the update.
153 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700154bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
155{
Patrick Venture46637c82018-11-06 15:20:24 -0800156 /* We know we support this path because canHandle is called ahead */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700157 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800158 {
Patrick Venture699750d2019-05-15 09:24:09 -0700159 /* TODO: We need to return information for the verify state -- did they
160 * call commit() did things start?
Patrick Ventureffcc5502018-11-16 12:32:38 -0800161 */
162 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700163 else if (path == activeImageBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800164 {
Patrick Venture699750d2019-05-15 09:24:09 -0700165 /* TODO: We need to return information for the image that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800166 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700167 else if (path == activeHashBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800168 {
Patrick Venture699750d2019-05-15 09:24:09 -0700169 /* TODO: We need to return information for the hash that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800170 }
171 else
172 {
173 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800174 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800175 meta->size = 0;
176
177 /* The generic blob_ids state is only the bits related to the transport
Patrick Ventured6461d62018-11-09 17:30:25 -0800178 * mechanisms.
179 */
Patrick Venture46637c82018-11-06 15:20:24 -0800180 return true;
181 }
182
Patrick Venturec7ca2912018-11-02 11:38:33 -0700183 return false;
184}
Patrick Venture53977962018-11-02 18:59:35 -0700185
Patrick Venturec02849b2018-11-06 17:31:15 -0800186/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800187 * Return stat information on an open session. It therefore must be an active
188 * handle to either the active image or active hash.
189 *
190 * The stat() and sessionstat() commands will return the same information in
191 * many cases, therefore the logic will be combined.
192 *
193 * TODO: combine the logic for stat and sessionstat().
194 */
195bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
196{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800197 auto item = lookup.find(session);
198 if (item == lookup.end())
199 {
200 return false;
201 }
202
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700203 /* The size here refers to the size of the file -- of something analagous.
204 */
205 meta->size = (item->second->imageHandler)
206 ? item->second->imageHandler->getSize()
207 : 0;
208
209 meta->metadata.clear();
210
Patrick Venture699750d2019-05-15 09:24:09 -0700211 /* TODO: Implement this for the verification blob, which is what we expect.
212 * Calling stat() on the verify blob without an active session should not
213 * provide insight.
214 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700215 if (item->second->activePath == verifyBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700216 {
Patrick Venture3ecb3502019-05-17 11:03:51 -0700217 auto value = verification->checkVerificationState();
Patrick Venture699750d2019-05-15 09:24:09 -0700218
Patrick Venturee955e072019-05-15 16:16:46 -0700219 meta->metadata.push_back(static_cast<std::uint8_t>(value));
220
221 /* Change the firmware handler's state and the blob's stat value
222 * depending.
223 */
224 if (value == VerifyCheckResponses::success ||
225 value == VerifyCheckResponses::failed)
226 {
227 state = UpdateState::verificationCompleted;
228 item->second->flags &= ~StateFlags::committing;
229
230 if (value == VerifyCheckResponses::success)
231 {
232 item->second->flags |= StateFlags::committed;
233 }
234 else
235 {
236 item->second->flags |= StateFlags::commit_error;
237 }
238 }
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700239 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800240
Patrick Venturee955e072019-05-15 16:16:46 -0700241 /* The blobState here relates to an active sesion, so we should return the
242 * flags used to open this session.
243 */
244 meta->blobState = item->second->flags;
245
Patrick Venturecc7d1602018-11-15 13:58:33 -0800246 /* The metadata blob returned comes from the data handler... it's used for
247 * instance, in P2A bridging to get required information about the mapping,
248 * and is the "opposite" of the lpc writemeta requirement.
249 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800250 if (item->second->dataHandler)
251 {
Patrick Venture74304642019-01-17 09:31:04 -0800252 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800253 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
254 bytes.end());
255 }
256
Patrick Venturecc7d1602018-11-15 13:58:33 -0800257 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800258}
259
260/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800261 * If you open /flash/image or /flash/tarball, or /flash/hash it will
262 * interpret the open flags and perform whatever actions are required for
263 * that update process. The session returned can be used immediately for
264 * sending data down, without requiring one to open the new active file.
265 *
266 * If you open the active flash image or active hash it will let you
267 * overwrite pieces, depending on the state.
268 *
269 * Once the verification process has started the active files cannot be
270 * opened.
271 *
272 * You can only have one open session at a time. Which means, you can only
273 * have one file open at a time. Trying to open the hash blob_id while you
274 * still have the flash image blob_id open will fail. Opening the flash
275 * blob_id when it is already open will fail.
276 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700277bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
278 const std::string& path)
279{
Patrick Venture6e307a72018-11-09 18:21:17 -0800280 /* Check that they've opened for writing - read back not currently
281 * supported.
282 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800283 if ((flags & OpenFlags::write) == 0)
284 {
285 return false;
286 }
287
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800288 /* Is the verification process underway? */
289 if (state == UpdateState::verificationStarted)
290 {
291 return false;
292 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800293
294 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800295 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800296 * TODO: Temporarily using a simple boolean flag until there's a full
297 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800298 *
299 * Further on this, if there's an active session to the hash we don't allow
300 * re-opening the image, and if we have the image open, we don't allow
301 * opening the hash. This design decision may be re-evaluated, and changed
302 * to only allow one session per object type (of the two types). But,
303 * consider if the hash is open, do we want to allow writing to the image?
304 * And why would we? But, really, the point of no-return is once the
305 * verification process has begun -- which is done via commit() on the hash
306 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700307 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800308 if (fileOpen)
309 {
310 return false;
311 }
312
Patrick Ventureffcc5502018-11-16 12:32:38 -0800313 /* Handle opening the verifyBlobId --> we know the image and hash aren't
314 * open because of the fileOpen check.
315 *
316 * The file must be opened for writing, but no transport mechanism specified
317 * since it's irrelevant.
318 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700319 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800320 {
321 /* In this case, there's no image handler to use, or data handler,
322 * simply set up a session.
323 */
324 verifyImage.flags = flags;
325 verifyImage.state = Session::State::open;
326
327 lookup[session] = &verifyImage;
328
329 fileOpen = true;
330
331 return true;
332 }
333
Patrick Venturec02849b2018-11-06 17:31:15 -0800334 /* There are two abstractions at play, how you get the data and how you
335 * handle that data. such that, whether the data comes from the PCI bridge
336 * or LPC bridge is not connected to whether the data goes into a static
337 * layout flash update or a UBI tarball.
338 */
339
340 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800341 * support what they request.
342 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800343 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800344 {
345 return false;
346 }
347
348 /* 2) there isn't, so what are they opening? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700349 if (path == activeImageBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800350 {
351 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800352 * already started one (due to canHandleBlob's behavior).
353 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800354 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800355 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700356 else if (path == activeHashBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800357 {
358 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800359 * already started one (due to canHandleBlob's behavior).
360 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800361 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800362 }
Patrick Venture18235e62018-11-08 10:21:09 -0800363
364 /* How are they expecting to copy this data? */
365 auto d = std::find_if(
366 transports.begin(), transports.end(),
367 [&flags](const auto& iter) { return (iter.bitmask & flags); });
368 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800369 {
Patrick Venture18235e62018-11-08 10:21:09 -0800370 return false;
371 }
372
373 /* We found the transport handler they requested, no surprise since
374 * above we verify they selected at least one we wanted.
375 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800376
Patrick Venture6e307a72018-11-09 18:21:17 -0800377 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
378 * only non-external data pathway -- but this is just a more generic
379 * approach to that.
380 */
381 if (d->handler)
382 {
383 /* If the data handler open call fails, open fails. */
384 if (!d->handler->open())
385 {
386 return false;
387 }
388 }
389
Patrick Venture70e30bf2019-01-17 10:29:28 -0800390 /* Do we have a file handler for the type of file they're opening.
391 * Note: This should only fail if something is somehow crazy wrong.
392 * Since the canHandle() said yes, and that's tied into the list of explicit
393 * firmware handers (and file handlers, like this'll know where to write the
394 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800395 */
Patrick Venture18235e62018-11-08 10:21:09 -0800396 auto h = std::find_if(
397 handlers.begin(), handlers.end(),
398 [&path](const auto& iter) { return (iter.blobName == path); });
399 if (h == handlers.end())
400 {
401 return false;
402 }
403
404 /* Ok, so we found a handler that matched, so call open() */
405 if (!h->handler->open(path))
406 {
407 return false;
408 }
409
Patrick Venture70e30bf2019-01-17 10:29:28 -0800410 Session* curr;
411 const std::string* active;
412
Patrick Venture7dad86f2019-05-17 08:52:20 -0700413 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800414 {
415 /* 2c) are they opening the /flash/hash ? (to start the process) */
416 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700417 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800418 }
419 else
420 {
421 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700422 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800423 }
424
Patrick Venture18235e62018-11-08 10:21:09 -0800425 curr->flags = flags;
426 curr->dataHandler = d->handler;
427 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800428 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800429
430 lookup[session] = curr;
431
Patrick Venturedaf47072019-05-10 15:21:55 -0700432 /* This may be them re-opening a blob, so let's only push it onto the list
433 * when appropriate.
434 */
435 auto blobIdMatch =
436 std::find_if(blobIDs.begin(), blobIDs.end(),
437 [active](const auto& iter) { return (iter == *active); });
438 if (blobIdMatch == blobIDs.end())
439 {
440 blobIDs.push_back(*active);
441 }
Patrick Venturedb253bf2018-11-09 19:36:03 -0800442
Patrick Venture12233c52019-05-16 09:26:59 -0700443 state = UpdateState::uploadInProgress;
Patrick Venture991910a2018-11-09 19:43:48 -0800444 fileOpen = true;
445
Patrick Venture18235e62018-11-08 10:21:09 -0800446 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700447}
Patrick Venture53977962018-11-02 18:59:35 -0700448
Patrick Venture18235e62018-11-08 10:21:09 -0800449/**
450 * The write command really just grabs the data from wherever it is and sends it
451 * to the image handler. It's the image handler's responsibility to deal with
452 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800453 *
454 * This receives a session from the blob manager, therefore it is always called
455 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800456 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700457bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
458 const std::vector<uint8_t>& data)
459{
Patrick Venture18235e62018-11-08 10:21:09 -0800460 auto item = lookup.find(session);
461 if (item == lookup.end())
462 {
463 return false;
464 }
465
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800466 /* Prevent writing during verification. */
467 if (state == UpdateState::verificationStarted)
468 {
469 return false;
470 }
471
Patrick Venture8af15eb2019-05-15 09:39:22 -0700472 /* Prevent writing to the verification blob before they trigger
Patrick Venture699750d2019-05-15 09:24:09 -0700473 * verification.
474 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700475 if (item->second->activePath == verifyBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700476 {
477 return false;
478 }
Patrick Venture699750d2019-05-15 09:24:09 -0700479
Patrick Venture18235e62018-11-08 10:21:09 -0800480 std::vector<std::uint8_t> bytes;
481
Patrick Venture05abf7e2018-11-09 11:02:56 -0800482 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800483 {
484 bytes = data;
485 }
486 else
487 {
488 /* little endian required per design, and so on, but TODO: do endianness
489 * with boost.
490 */
491 struct ExtChunkHdr header;
492
493 if (data.size() != sizeof(header))
494 {
495 return false;
496 }
497
498 std::memcpy(&header, data.data(), data.size());
499 bytes = item->second->dataHandler->copyFrom(header.length);
500 }
501
502 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700503}
Patrick Venture18235e62018-11-08 10:21:09 -0800504
Patrick Venture8c535332018-11-08 15:58:00 -0800505/*
506 * If the active session (image or hash) is over LPC, this allows
507 * configuring it. This option is only available before you start
508 * writing data for the given item (image or hash). This will return
509 * false at any other part. -- the lpc handler portion will know to return
510 * false.
511 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700512bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
513 const std::vector<uint8_t>& data)
514{
Patrick Venture8c535332018-11-08 15:58:00 -0800515 auto item = lookup.find(session);
516 if (item == lookup.end())
517 {
518 return false;
519 }
520
Patrick Venture05abf7e2018-11-09 11:02:56 -0800521 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800522 {
523 return false;
524 }
525
Patrick Ventured56097a2019-05-15 09:47:55 -0700526 /* Prevent writing meta to the verification blob (it has no data handler).
527 */
528 if (item->second->dataHandler)
529 {
530 return item->second->dataHandler->writeMeta(data);
531 }
Patrick Venture699750d2019-05-15 09:24:09 -0700532
Patrick Ventured56097a2019-05-15 09:47:55 -0700533 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700534}
Patrick Venture8c535332018-11-08 15:58:00 -0800535
Patrick Ventured6461d62018-11-09 17:30:25 -0800536/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700537 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800538 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800539 * the image.
540 *
541 * For this file to have opened, the other two must be closed, which means any
542 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800543 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700544bool FirmwareBlobHandler::commit(uint16_t session,
545 const std::vector<uint8_t>& data)
546{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800547 auto item = lookup.find(session);
548 if (item == lookup.end())
549 {
550 return false;
551 }
552
553 /* You can only commit on the verifyBlodId */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700554 if (item->second->activePath != verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800555 {
556 return false;
557 }
558
Patrick Venturebe198702019-05-15 09:46:02 -0700559 /* Calling repeatedly has no effect within an update process. */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800560 if (state == UpdateState::verificationStarted)
561 {
Patrick Venturebe198702019-05-15 09:46:02 -0700562 return true;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800563 }
564
565 /* Set state to committing. */
566 item->second->flags |= StateFlags::committing;
567
568 return triggerVerification();
Patrick Venturec7ca2912018-11-02 11:38:33 -0700569}
Patrick Ventured6461d62018-11-09 17:30:25 -0800570
571/*
572 * Close must be called on the firmware image before triggering
573 * verification via commit. Once the verification is complete, you can
574 * then close the hash file.
575 *
576 * If the `verify_image.service` returned success, closing the hash file
577 * will have a specific behavior depending on the update. If it's UBI,
578 * it'll perform the install. If it's static layout, it'll do nothing. The
579 * verify_image service in the static layout case is responsible for placing
580 * the file in the correct staging position.
581 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700582bool FirmwareBlobHandler::close(uint16_t session)
583{
Patrick Venture68bb1432018-11-09 20:08:48 -0800584 auto item = lookup.find(session);
585 if (item == lookup.end())
586 {
587 return false;
588 }
589
Patrick Ventureffcc5502018-11-16 12:32:38 -0800590 /* Are you closing the verify blob? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700591 if (item->second->activePath == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800592 {
Patrick Ventureffcc5502018-11-16 12:32:38 -0800593 if (state == UpdateState::verificationStarted)
594 {
Patrick Venture12233c52019-05-16 09:26:59 -0700595 /* TODO: If they close this blob before verification finishes,
596 * that's an abort.
597 */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800598 return false;
599 }
Patrick Venture12233c52019-05-16 09:26:59 -0700600 else if (state == UpdateState::verificationCompleted)
601 {
602 /* TODO: Should this delete the verification artifact? */
603 state = UpdateState::notYetStarted;
604
605 /* A this point, delete the active blob IDs from the blob_list. */
606 blobIDs.erase(
Patrick Venture7dad86f2019-05-17 08:52:20 -0700607 std::remove(blobIDs.begin(), blobIDs.end(), activeImageBlobId));
Patrick Venture12233c52019-05-16 09:26:59 -0700608 blobIDs.erase(
Patrick Venture7dad86f2019-05-17 08:52:20 -0700609 std::remove(blobIDs.begin(), blobIDs.end(), activeHashBlobId));
Patrick Venture12233c52019-05-16 09:26:59 -0700610 }
611 /* Must be verificationPending... not yet started, they may re-open and
612 * trigger verification.
613 */
614 }
615 else
616 {
617 /* They are closing a data pathway (image, tarball, hash). */
618 state = UpdateState::verificationPending;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800619 }
620
Patrick Venture68bb1432018-11-09 20:08:48 -0800621 if (item->second->dataHandler)
622 {
623 item->second->dataHandler->close();
624 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800625 if (item->second->imageHandler)
626 {
627 item->second->imageHandler->close();
628 }
629
Patrick Venture68bb1432018-11-09 20:08:48 -0800630 item->second->state = Session::State::closed;
631 /* Do not delete the active blob_id from the list of blob_ids, because that
632 * blob_id indicates there is data stored. Delete will destroy it.
633 */
634
635 lookup.erase(item);
636
Patrick Venture991910a2018-11-09 19:43:48 -0800637 fileOpen = false;
638
Patrick Venture68bb1432018-11-09 20:08:48 -0800639 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700640}
Patrick Ventured6461d62018-11-09 17:30:25 -0800641
Patrick Venturec7ca2912018-11-02 11:38:33 -0700642bool FirmwareBlobHandler::expire(uint16_t session)
643{
644 return false;
645}
Patrick Ventured6461d62018-11-09 17:30:25 -0800646
647/*
648 * Currently, the design does not provide this with a function, however,
649 * it will likely change to support reading data back.
650 */
651std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
652 uint32_t offset,
653 uint32_t requestedSize)
654{
655 return {};
656}
657
Patrick Ventureffcc5502018-11-16 12:32:38 -0800658bool FirmwareBlobHandler::triggerVerification()
659{
Patrick Venture3ecb3502019-05-17 11:03:51 -0700660 bool result = verification->triggerVerification();
661 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800662 {
Patrick Venture453f06a2019-01-17 10:02:12 -0800663 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800664 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800665
Patrick Venture3ecb3502019-05-17 11:03:51 -0700666 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800667}
668
Patrick Venturec7ca2912018-11-02 11:38:33 -0700669} // namespace blobs