blob: 675929c0053ac8b77d09e3ac9fd529f615163cf5 [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 Venture1d5a31c2019-05-20 11:38:22 -070034namespace ipmi_flash
Patrick Venturec7ca2912018-11-02 11:38:33 -070035{
Patrick Ventureb3b4db72019-05-15 11:30:24 -070036
Patrick Venture1d5a31c2019-05-20 11:38:22 -070037std::unique_ptr<blobs::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 Venture27ac5822019-05-20 17:39:31 -070041 std::unique_ptr<VerificationInterface> verification,
42 std::unique_ptr<UpdateInterface> update)
Patrick Venture68cf64f2018-11-06 10:46:51 -080043{
Patrick Venture52854622018-11-06 12:30:00 -080044 /* There must be at least one. */
45 if (!firmwares.size())
46 {
Patrick Ventured333a872018-12-03 16:24:26 -080047 log<level::ERR>("Must provide at least one firmware handler.");
Patrick Venture52854622018-11-06 12:30:00 -080048 return nullptr;
49 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080050 if (!transports.size())
51 {
52 return nullptr;
53 }
Patrick Venture52854622018-11-06 12:30:00 -080054
Patrick Venturea78e39f2018-11-06 18:37:06 -080055 std::vector<std::string> blobs;
56 for (const auto& item : firmwares)
57 {
58 blobs.push_back(item.blobName);
59 }
Patrick Venture7dad86f2019-05-17 08:52:20 -070060 blobs.push_back(verifyBlobId); /* Add blob_id to always exist. */
Patrick Venture18235e62018-11-08 10:21:09 -080061
Patrick Venture7dad86f2019-05-17 08:52:20 -070062 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId))
Patrick Venture18235e62018-11-08 10:21:09 -080063 {
64 return nullptr;
65 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080066
Patrick Venture1cde5f92018-11-07 08:26:47 -080067 std::uint16_t bitmask = 0;
68 for (const auto& item : transports)
69 {
70 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
71 bitmask |= item.bitmask;
72 }
73
Patrick Venture3ecb3502019-05-17 11:03:51 -070074 return std::make_unique<FirmwareBlobHandler>(
Patrick Venture27ac5822019-05-20 17:39:31 -070075 firmwares, blobs, transports, bitmask, std::move(verification),
76 std::move(update));
Patrick Venture68cf64f2018-11-06 10:46:51 -080077}
78
Patrick Ventured6461d62018-11-09 17:30:25 -080079/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070080bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
81{
Patrick Venture6032dc02019-05-17 11:01:44 -070082 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070083}
Patrick Venture53977962018-11-02 18:59:35 -070084
Patrick Ventured6461d62018-11-09 17:30:25 -080085/*
86 * Grab the list of supported firmware.
87 *
88 * If there's an open firmware session, it'll already be present in the
89 * list as "/flash/active/image", and if the hash has started,
90 * "/flash/active/hash" regardless of mechanism. This is done in the open
91 * comamnd, no extra work is required here.
92 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070093std::vector<std::string> FirmwareBlobHandler::getBlobIds()
94{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080095 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070096}
Patrick Venture53977962018-11-02 18:59:35 -070097
Patrick Ventured6461d62018-11-09 17:30:25 -080098/*
99 * Per the design, this mean abort, and this will trigger whatever
100 * appropriate actions are required to abort the process.
Patrick Venture9e5aab22018-11-09 20:49:28 -0800101 *
102 * You cannot delete a blob that has an open handle in the system, therefore
103 * this is never called if there's an open session. Guaranteed by the blob
104 * manager.
Patrick Ventured6461d62018-11-09 17:30:25 -0800105 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700106bool FirmwareBlobHandler::deleteBlob(const std::string& path)
107{
Patrick Venture9e5aab22018-11-09 20:49:28 -0800108 const std::string* toDelete;
109
Patrick Ventureffcc5502018-11-16 12:32:38 -0800110 /* You cannot delete the verify blob -- trying to delete it, currently has
111 * no impact.
112 * TODO: Should trying to delete this cause an abort?
113 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700114 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800115 {
116 return false;
117 }
118
Patrick Venture7dad86f2019-05-17 08:52:20 -0700119 if (path == hashBlobId || path == activeHashBlobId)
Patrick Venture9e5aab22018-11-09 20:49:28 -0800120 {
121 /* They're deleting the hash. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700122 toDelete = &activeHashBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800123 }
124 else
125 {
126 /* They're deleting the image. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700127 toDelete = &activeImageBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800128 }
129
130 auto it = std::find_if(
131 blobIDs.begin(), blobIDs.end(),
132 [toDelete](const auto& iter) { return (iter == *toDelete); });
133 if (it == blobIDs.end())
134 {
135 /* Somehow they've asked to delete something we didn't say we could
136 * handle.
137 */
138 return false;
139 }
140
141 blobIDs.erase(it);
142
143 /* TODO: Handle aborting the process and fixing up the state. */
144
145 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700146}
Patrick Venture53977962018-11-02 18:59:35 -0700147
Patrick Ventured6461d62018-11-09 17:30:25 -0800148/*
149 * Stat on the files will return information such as what supported
150 * transport mechanisms are available.
151 *
152 * Stat on an active file or hash will return information such as the size
153 * of the data cached, and any additional pertinent information. The
154 * blob_state on the active files will return the state of the update.
155 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700156bool FirmwareBlobHandler::stat(const std::string& path,
157 struct blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700158{
Patrick Venture46637c82018-11-06 15:20:24 -0800159 /* We know we support this path because canHandle is called ahead */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700160 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800161 {
Patrick Venture699750d2019-05-15 09:24:09 -0700162 /* TODO: We need to return information for the verify state -- did they
163 * call commit() did things start?
Patrick Ventureffcc5502018-11-16 12:32:38 -0800164 */
165 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700166 else if (path == activeImageBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800167 {
Patrick Venture699750d2019-05-15 09:24:09 -0700168 /* TODO: We need to return information for the image that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800169 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700170 else if (path == activeHashBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800171 {
Patrick Venture699750d2019-05-15 09:24:09 -0700172 /* TODO: We need to return information for the hash that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800173 }
174 else
175 {
176 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800177 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800178 meta->size = 0;
179
180 /* The generic blob_ids state is only the bits related to the transport
Patrick Ventured6461d62018-11-09 17:30:25 -0800181 * mechanisms.
182 */
Patrick Venture46637c82018-11-06 15:20:24 -0800183 return true;
184 }
185
Patrick Venturec7ca2912018-11-02 11:38:33 -0700186 return false;
187}
Patrick Venture53977962018-11-02 18:59:35 -0700188
Patrick Venturec02849b2018-11-06 17:31:15 -0800189/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800190 * Return stat information on an open session. It therefore must be an active
191 * handle to either the active image or active hash.
192 *
193 * The stat() and sessionstat() commands will return the same information in
194 * many cases, therefore the logic will be combined.
195 *
196 * TODO: combine the logic for stat and sessionstat().
197 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700198bool FirmwareBlobHandler::stat(uint16_t session, struct blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800199{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800200 auto item = lookup.find(session);
201 if (item == lookup.end())
202 {
203 return false;
204 }
205
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700206 /* The size here refers to the size of the file -- of something analagous.
207 */
208 meta->size = (item->second->imageHandler)
209 ? item->second->imageHandler->getSize()
210 : 0;
211
212 meta->metadata.clear();
213
Patrick Venture699750d2019-05-15 09:24:09 -0700214 /* TODO: Implement this for the verification blob, which is what we expect.
215 * Calling stat() on the verify blob without an active session should not
216 * provide insight.
217 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700218 if (item->second->activePath == verifyBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700219 {
Patrick Venture01a33272019-05-23 19:48:22 -0700220 VerifyCheckResponses value;
221
222 if (state == UpdateState::verificationPending)
223 {
224 value = VerifyCheckResponses::other;
225 }
226 else
227 {
228 value = verification->checkVerificationState();
229 }
Patrick Venture699750d2019-05-15 09:24:09 -0700230
Patrick Venturee955e072019-05-15 16:16:46 -0700231 meta->metadata.push_back(static_cast<std::uint8_t>(value));
232
233 /* Change the firmware handler's state and the blob's stat value
234 * depending.
235 */
236 if (value == VerifyCheckResponses::success ||
237 value == VerifyCheckResponses::failed)
238 {
239 state = UpdateState::verificationCompleted;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700240 item->second->flags &= ~blobs::StateFlags::committing;
Patrick Venturee955e072019-05-15 16:16:46 -0700241
242 if (value == VerifyCheckResponses::success)
243 {
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700244 item->second->flags |= blobs::StateFlags::committed;
Patrick Venturee955e072019-05-15 16:16:46 -0700245 }
246 else
247 {
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700248 item->second->flags |= blobs::StateFlags::commit_error;
Patrick Venturee955e072019-05-15 16:16:46 -0700249 }
250 }
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700251 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800252
Patrick Venturee955e072019-05-15 16:16:46 -0700253 /* The blobState here relates to an active sesion, so we should return the
254 * flags used to open this session.
255 */
256 meta->blobState = item->second->flags;
257
Patrick Venturecc7d1602018-11-15 13:58:33 -0800258 /* The metadata blob returned comes from the data handler... it's used for
259 * instance, in P2A bridging to get required information about the mapping,
260 * and is the "opposite" of the lpc writemeta requirement.
261 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800262 if (item->second->dataHandler)
263 {
Patrick Venture74304642019-01-17 09:31:04 -0800264 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800265 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
266 bytes.end());
267 }
268
Patrick Venturecc7d1602018-11-15 13:58:33 -0800269 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800270}
271
272/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800273 * If you open /flash/image or /flash/tarball, or /flash/hash it will
274 * interpret the open flags and perform whatever actions are required for
275 * that update process. The session returned can be used immediately for
276 * sending data down, without requiring one to open the new active file.
277 *
278 * If you open the active flash image or active hash it will let you
279 * overwrite pieces, depending on the state.
280 *
281 * Once the verification process has started the active files cannot be
282 * opened.
283 *
284 * You can only have one open session at a time. Which means, you can only
285 * have one file open at a time. Trying to open the hash blob_id while you
286 * still have the flash image blob_id open will fail. Opening the flash
287 * blob_id when it is already open will fail.
288 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700289bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
290 const std::string& path)
291{
Patrick Venture6e307a72018-11-09 18:21:17 -0800292 /* Check that they've opened for writing - read back not currently
293 * supported.
294 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700295 if ((flags & blobs::OpenFlags::write) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800296 {
297 return false;
298 }
299
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800300 /* Is the verification process underway? */
301 if (state == UpdateState::verificationStarted)
302 {
303 return false;
304 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800305
306 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800307 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800308 * TODO: Temporarily using a simple boolean flag until there's a full
309 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800310 *
311 * Further on this, if there's an active session to the hash we don't allow
312 * re-opening the image, and if we have the image open, we don't allow
313 * opening the hash. This design decision may be re-evaluated, and changed
314 * to only allow one session per object type (of the two types). But,
315 * consider if the hash is open, do we want to allow writing to the image?
316 * And why would we? But, really, the point of no-return is once the
317 * verification process has begun -- which is done via commit() on the hash
318 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700319 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800320 if (fileOpen)
321 {
322 return false;
323 }
324
Patrick Ventureffcc5502018-11-16 12:32:38 -0800325 /* Handle opening the verifyBlobId --> we know the image and hash aren't
326 * open because of the fileOpen check.
327 *
328 * The file must be opened for writing, but no transport mechanism specified
329 * since it's irrelevant.
330 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700331 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800332 {
333 /* In this case, there's no image handler to use, or data handler,
334 * simply set up a session.
335 */
336 verifyImage.flags = flags;
337 verifyImage.state = Session::State::open;
338
339 lookup[session] = &verifyImage;
340
341 fileOpen = true;
342
343 return true;
344 }
345
Patrick Venturec02849b2018-11-06 17:31:15 -0800346 /* There are two abstractions at play, how you get the data and how you
347 * handle that data. such that, whether the data comes from the PCI bridge
348 * or LPC bridge is not connected to whether the data goes into a static
349 * layout flash update or a UBI tarball.
350 */
351
352 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800353 * support what they request.
354 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800355 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800356 {
357 return false;
358 }
359
360 /* 2) there isn't, so what are they opening? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700361 if (path == activeImageBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800362 {
363 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800364 * already started one (due to canHandleBlob's behavior).
365 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800366 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800367 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700368 else if (path == activeHashBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800369 {
370 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800371 * already started one (due to canHandleBlob's behavior).
372 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800373 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800374 }
Patrick Venture18235e62018-11-08 10:21:09 -0800375
376 /* How are they expecting to copy this data? */
377 auto d = std::find_if(
378 transports.begin(), transports.end(),
379 [&flags](const auto& iter) { return (iter.bitmask & flags); });
380 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800381 {
Patrick Venture18235e62018-11-08 10:21:09 -0800382 return false;
383 }
384
385 /* We found the transport handler they requested, no surprise since
386 * above we verify they selected at least one we wanted.
387 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800388
Patrick Venture6e307a72018-11-09 18:21:17 -0800389 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
390 * only non-external data pathway -- but this is just a more generic
391 * approach to that.
392 */
393 if (d->handler)
394 {
395 /* If the data handler open call fails, open fails. */
396 if (!d->handler->open())
397 {
398 return false;
399 }
400 }
401
Patrick Venture70e30bf2019-01-17 10:29:28 -0800402 /* Do we have a file handler for the type of file they're opening.
403 * Note: This should only fail if something is somehow crazy wrong.
404 * Since the canHandle() said yes, and that's tied into the list of explicit
405 * firmware handers (and file handlers, like this'll know where to write the
406 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800407 */
Patrick Venture18235e62018-11-08 10:21:09 -0800408 auto h = std::find_if(
409 handlers.begin(), handlers.end(),
410 [&path](const auto& iter) { return (iter.blobName == path); });
411 if (h == handlers.end())
412 {
413 return false;
414 }
415
416 /* Ok, so we found a handler that matched, so call open() */
417 if (!h->handler->open(path))
418 {
419 return false;
420 }
421
Patrick Venture70e30bf2019-01-17 10:29:28 -0800422 Session* curr;
423 const std::string* active;
424
Patrick Venture7dad86f2019-05-17 08:52:20 -0700425 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800426 {
427 /* 2c) are they opening the /flash/hash ? (to start the process) */
428 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700429 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800430 }
431 else
432 {
433 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700434 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800435 }
436
Patrick Venture18235e62018-11-08 10:21:09 -0800437 curr->flags = flags;
438 curr->dataHandler = d->handler;
439 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800440 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800441
442 lookup[session] = curr;
443
Patrick Venturedaf47072019-05-10 15:21:55 -0700444 /* This may be them re-opening a blob, so let's only push it onto the list
445 * when appropriate.
446 */
447 auto blobIdMatch =
448 std::find_if(blobIDs.begin(), blobIDs.end(),
449 [active](const auto& iter) { return (iter == *active); });
450 if (blobIdMatch == blobIDs.end())
451 {
452 blobIDs.push_back(*active);
453 }
Patrick Venturedb253bf2018-11-09 19:36:03 -0800454
Patrick Venture12233c52019-05-16 09:26:59 -0700455 state = UpdateState::uploadInProgress;
Patrick Venture991910a2018-11-09 19:43:48 -0800456 fileOpen = true;
457
Patrick Venture18235e62018-11-08 10:21:09 -0800458 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700459}
Patrick Venture53977962018-11-02 18:59:35 -0700460
Patrick Venture18235e62018-11-08 10:21:09 -0800461/**
462 * The write command really just grabs the data from wherever it is and sends it
463 * to the image handler. It's the image handler's responsibility to deal with
464 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800465 *
466 * This receives a session from the blob manager, therefore it is always called
467 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800468 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700469bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
470 const std::vector<uint8_t>& data)
471{
Patrick Venture18235e62018-11-08 10:21:09 -0800472 auto item = lookup.find(session);
473 if (item == lookup.end())
474 {
475 return false;
476 }
477
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800478 /* Prevent writing during verification. */
479 if (state == UpdateState::verificationStarted)
480 {
481 return false;
482 }
483
Patrick Venture8af15eb2019-05-15 09:39:22 -0700484 /* Prevent writing to the verification blob before they trigger
Patrick Venture699750d2019-05-15 09:24:09 -0700485 * verification.
486 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700487 if (item->second->activePath == verifyBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700488 {
489 return false;
490 }
Patrick Venture699750d2019-05-15 09:24:09 -0700491
Patrick Venture18235e62018-11-08 10:21:09 -0800492 std::vector<std::uint8_t> bytes;
493
Patrick Venture05abf7e2018-11-09 11:02:56 -0800494 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800495 {
496 bytes = data;
497 }
498 else
499 {
500 /* little endian required per design, and so on, but TODO: do endianness
501 * with boost.
502 */
503 struct ExtChunkHdr header;
504
505 if (data.size() != sizeof(header))
506 {
507 return false;
508 }
509
510 std::memcpy(&header, data.data(), data.size());
511 bytes = item->second->dataHandler->copyFrom(header.length);
512 }
513
514 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700515}
Patrick Venture18235e62018-11-08 10:21:09 -0800516
Patrick Venture8c535332018-11-08 15:58:00 -0800517/*
518 * If the active session (image or hash) is over LPC, this allows
519 * configuring it. This option is only available before you start
520 * writing data for the given item (image or hash). This will return
521 * false at any other part. -- the lpc handler portion will know to return
522 * false.
523 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700524bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
525 const std::vector<uint8_t>& data)
526{
Patrick Venture8c535332018-11-08 15:58:00 -0800527 auto item = lookup.find(session);
528 if (item == lookup.end())
529 {
530 return false;
531 }
532
Patrick Venture05abf7e2018-11-09 11:02:56 -0800533 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800534 {
535 return false;
536 }
537
Patrick Ventured56097a2019-05-15 09:47:55 -0700538 /* Prevent writing meta to the verification blob (it has no data handler).
539 */
540 if (item->second->dataHandler)
541 {
542 return item->second->dataHandler->writeMeta(data);
543 }
Patrick Venture699750d2019-05-15 09:24:09 -0700544
Patrick Ventured56097a2019-05-15 09:47:55 -0700545 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700546}
Patrick Venture8c535332018-11-08 15:58:00 -0800547
Patrick Ventured6461d62018-11-09 17:30:25 -0800548/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700549 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800550 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800551 * the image.
552 *
553 * For this file to have opened, the other two must be closed, which means any
554 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800555 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700556bool FirmwareBlobHandler::commit(uint16_t session,
557 const std::vector<uint8_t>& data)
558{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800559 auto item = lookup.find(session);
560 if (item == lookup.end())
561 {
562 return false;
563 }
564
565 /* You can only commit on the verifyBlodId */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700566 if (item->second->activePath != verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800567 {
568 return false;
569 }
570
Patrick Venturebe198702019-05-15 09:46:02 -0700571 /* Calling repeatedly has no effect within an update process. */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800572 if (state == UpdateState::verificationStarted)
573 {
Patrick Venturebe198702019-05-15 09:46:02 -0700574 return true;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800575 }
576
577 /* Set state to committing. */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700578 item->second->flags |= blobs::StateFlags::committing;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800579
580 return triggerVerification();
Patrick Venturec7ca2912018-11-02 11:38:33 -0700581}
Patrick Ventured6461d62018-11-09 17:30:25 -0800582
583/*
584 * Close must be called on the firmware image before triggering
585 * verification via commit. Once the verification is complete, you can
586 * then close the hash file.
587 *
588 * If the `verify_image.service` returned success, closing the hash file
589 * will have a specific behavior depending on the update. If it's UBI,
590 * it'll perform the install. If it's static layout, it'll do nothing. The
591 * verify_image service in the static layout case is responsible for placing
592 * the file in the correct staging position.
593 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700594bool FirmwareBlobHandler::close(uint16_t session)
595{
Patrick Venture68bb1432018-11-09 20:08:48 -0800596 auto item = lookup.find(session);
597 if (item == lookup.end())
598 {
599 return false;
600 }
601
Patrick Ventureffcc5502018-11-16 12:32:38 -0800602 /* Are you closing the verify blob? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700603 if (item->second->activePath == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800604 {
Patrick Ventureffcc5502018-11-16 12:32:38 -0800605 if (state == UpdateState::verificationStarted)
606 {
Patrick Venture12233c52019-05-16 09:26:59 -0700607 /* TODO: If they close this blob before verification finishes,
608 * that's an abort.
609 */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800610 return false;
611 }
Patrick Venture12233c52019-05-16 09:26:59 -0700612 else if (state == UpdateState::verificationCompleted)
613 {
614 /* TODO: Should this delete the verification artifact? */
615 state = UpdateState::notYetStarted;
616
617 /* A this point, delete the active blob IDs from the blob_list. */
618 blobIDs.erase(
Patrick Venture7dad86f2019-05-17 08:52:20 -0700619 std::remove(blobIDs.begin(), blobIDs.end(), activeImageBlobId));
Patrick Venture12233c52019-05-16 09:26:59 -0700620 blobIDs.erase(
Patrick Venture7dad86f2019-05-17 08:52:20 -0700621 std::remove(blobIDs.begin(), blobIDs.end(), activeHashBlobId));
Patrick Venture12233c52019-05-16 09:26:59 -0700622 }
623 /* Must be verificationPending... not yet started, they may re-open and
624 * trigger verification.
625 */
626 }
627 else
628 {
629 /* They are closing a data pathway (image, tarball, hash). */
630 state = UpdateState::verificationPending;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800631 }
632
Patrick Venture68bb1432018-11-09 20:08:48 -0800633 if (item->second->dataHandler)
634 {
635 item->second->dataHandler->close();
636 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800637 if (item->second->imageHandler)
638 {
639 item->second->imageHandler->close();
640 }
641
Patrick Venture68bb1432018-11-09 20:08:48 -0800642 item->second->state = Session::State::closed;
643 /* Do not delete the active blob_id from the list of blob_ids, because that
644 * blob_id indicates there is data stored. Delete will destroy it.
645 */
646
647 lookup.erase(item);
648
Patrick Venture991910a2018-11-09 19:43:48 -0800649 fileOpen = false;
650
Patrick Venture68bb1432018-11-09 20:08:48 -0800651 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700652}
Patrick Ventured6461d62018-11-09 17:30:25 -0800653
Patrick Venturec7ca2912018-11-02 11:38:33 -0700654bool FirmwareBlobHandler::expire(uint16_t session)
655{
656 return false;
657}
Patrick Ventured6461d62018-11-09 17:30:25 -0800658
659/*
660 * Currently, the design does not provide this with a function, however,
661 * it will likely change to support reading data back.
662 */
663std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
664 uint32_t offset,
665 uint32_t requestedSize)
666{
667 return {};
668}
669
Patrick Ventureffcc5502018-11-16 12:32:38 -0800670bool FirmwareBlobHandler::triggerVerification()
671{
Patrick Venture3ecb3502019-05-17 11:03:51 -0700672 bool result = verification->triggerVerification();
673 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800674 {
Patrick Venture453f06a2019-01-17 10:02:12 -0800675 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800676 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800677
Patrick Venture3ecb3502019-05-17 11:03:51 -0700678 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800679}
680
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700681} // namespace ipmi_flash