blob: ded5383e9b88dd4e79c5b05268b1f5ba234d3aef [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 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>(
Patrick Venture27ac5822019-05-20 17:39:31 -070074 firmwares, blobs, transports, bitmask, std::move(verification),
75 std::move(update));
Patrick Venture68cf64f2018-11-06 10:46:51 -080076}
77
Patrick Ventured6461d62018-11-09 17:30:25 -080078/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070079bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
80{
Patrick Venture6032dc02019-05-17 11:01:44 -070081 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070082}
Patrick Venture53977962018-11-02 18:59:35 -070083
Patrick Ventured6461d62018-11-09 17:30:25 -080084/*
85 * Grab the list of supported firmware.
86 *
87 * If there's an open firmware session, it'll already be present in the
88 * list as "/flash/active/image", and if the hash has started,
89 * "/flash/active/hash" regardless of mechanism. This is done in the open
90 * comamnd, no extra work is required here.
91 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070092std::vector<std::string> FirmwareBlobHandler::getBlobIds()
93{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080094 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070095}
Patrick Venture53977962018-11-02 18:59:35 -070096
Patrick Ventured6461d62018-11-09 17:30:25 -080097/*
98 * Per the design, this mean abort, and this will trigger whatever
99 * appropriate actions are required to abort the process.
Patrick Venture9e5aab22018-11-09 20:49:28 -0800100 *
101 * You cannot delete a blob that has an open handle in the system, therefore
102 * this is never called if there's an open session. Guaranteed by the blob
103 * manager.
Patrick Ventured6461d62018-11-09 17:30:25 -0800104 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700105bool FirmwareBlobHandler::deleteBlob(const std::string& path)
106{
Patrick Venture9e5aab22018-11-09 20:49:28 -0800107 const std::string* toDelete;
108
Patrick Ventureffcc5502018-11-16 12:32:38 -0800109 /* You cannot delete the verify blob -- trying to delete it, currently has
110 * no impact.
111 * TODO: Should trying to delete this cause an abort?
112 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700113 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800114 {
115 return false;
116 }
117
Patrick Venture7dad86f2019-05-17 08:52:20 -0700118 if (path == hashBlobId || path == activeHashBlobId)
Patrick Venture9e5aab22018-11-09 20:49:28 -0800119 {
120 /* They're deleting the hash. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700121 toDelete = &activeHashBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800122 }
123 else
124 {
125 /* They're deleting the image. */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700126 toDelete = &activeImageBlobId;
Patrick Venture9e5aab22018-11-09 20:49:28 -0800127 }
128
129 auto it = std::find_if(
130 blobIDs.begin(), blobIDs.end(),
131 [toDelete](const auto& iter) { return (iter == *toDelete); });
132 if (it == blobIDs.end())
133 {
134 /* Somehow they've asked to delete something we didn't say we could
135 * handle.
136 */
137 return false;
138 }
139
140 blobIDs.erase(it);
141
142 /* TODO: Handle aborting the process and fixing up the state. */
143
144 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700145}
Patrick Venture53977962018-11-02 18:59:35 -0700146
Patrick Ventured6461d62018-11-09 17:30:25 -0800147/*
148 * Stat on the files will return information such as what supported
149 * transport mechanisms are available.
150 *
151 * Stat on an active file or hash will return information such as the size
152 * of the data cached, and any additional pertinent information. The
153 * blob_state on the active files will return the state of the update.
154 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700155bool FirmwareBlobHandler::stat(const std::string& path,
156 struct blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700157{
Patrick Venture46637c82018-11-06 15:20:24 -0800158 /* We know we support this path because canHandle is called ahead */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700159 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800160 {
Patrick Venture699750d2019-05-15 09:24:09 -0700161 /* TODO: We need to return information for the verify state -- did they
162 * call commit() did things start?
Patrick Ventureffcc5502018-11-16 12:32:38 -0800163 */
164 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700165 else if (path == activeImageBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800166 {
Patrick Venture699750d2019-05-15 09:24:09 -0700167 /* TODO: We need to return information for the image that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800168 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700169 else if (path == activeHashBlobId)
Patrick Venture46637c82018-11-06 15:20:24 -0800170 {
Patrick Venture699750d2019-05-15 09:24:09 -0700171 /* TODO: We need to return information for the hash that's staged. */
Patrick Venture46637c82018-11-06 15:20:24 -0800172 }
173 else
174 {
175 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800176 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800177 meta->size = 0;
178
179 /* The generic blob_ids state is only the bits related to the transport
Patrick Ventured6461d62018-11-09 17:30:25 -0800180 * mechanisms.
181 */
Patrick Venture46637c82018-11-06 15:20:24 -0800182 return true;
183 }
184
Patrick Venturec7ca2912018-11-02 11:38:33 -0700185 return false;
186}
Patrick Venture53977962018-11-02 18:59:35 -0700187
Patrick Venturec02849b2018-11-06 17:31:15 -0800188/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800189 * Return stat information on an open session. It therefore must be an active
190 * handle to either the active image or active hash.
191 *
192 * The stat() and sessionstat() commands will return the same information in
193 * many cases, therefore the logic will be combined.
194 *
195 * TODO: combine the logic for stat and sessionstat().
196 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700197bool FirmwareBlobHandler::stat(uint16_t session, struct blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800198{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800199 auto item = lookup.find(session);
200 if (item == lookup.end())
201 {
202 return false;
203 }
204
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700205 /* The size here refers to the size of the file -- of something analagous.
206 */
207 meta->size = (item->second->imageHandler)
208 ? item->second->imageHandler->getSize()
209 : 0;
210
211 meta->metadata.clear();
212
Patrick Venture699750d2019-05-15 09:24:09 -0700213 /* TODO: Implement this for the verification blob, which is what we expect.
214 * Calling stat() on the verify blob without an active session should not
215 * provide insight.
216 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700217 if (item->second->activePath == verifyBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700218 {
Patrick Venture01a33272019-05-23 19:48:22 -0700219 VerifyCheckResponses value;
220
221 if (state == UpdateState::verificationPending)
222 {
223 value = VerifyCheckResponses::other;
224 }
225 else
226 {
227 value = verification->checkVerificationState();
228 }
Patrick Venture699750d2019-05-15 09:24:09 -0700229
Patrick Venturee955e072019-05-15 16:16:46 -0700230 meta->metadata.push_back(static_cast<std::uint8_t>(value));
231
232 /* Change the firmware handler's state and the blob's stat value
233 * depending.
234 */
235 if (value == VerifyCheckResponses::success ||
236 value == VerifyCheckResponses::failed)
237 {
238 state = UpdateState::verificationCompleted;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700239 item->second->flags &= ~blobs::StateFlags::committing;
Patrick Venturee955e072019-05-15 16:16:46 -0700240
241 if (value == VerifyCheckResponses::success)
242 {
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700243 item->second->flags |= blobs::StateFlags::committed;
Patrick Venturee955e072019-05-15 16:16:46 -0700244 }
245 else
246 {
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700247 item->second->flags |= blobs::StateFlags::commit_error;
Patrick Venturee955e072019-05-15 16:16:46 -0700248 }
249 }
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700250 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800251
Patrick Venturee955e072019-05-15 16:16:46 -0700252 /* The blobState here relates to an active sesion, so we should return the
253 * flags used to open this session.
254 */
255 meta->blobState = item->second->flags;
256
Patrick Venturecc7d1602018-11-15 13:58:33 -0800257 /* The metadata blob returned comes from the data handler... it's used for
258 * instance, in P2A bridging to get required information about the mapping,
259 * and is the "opposite" of the lpc writemeta requirement.
260 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800261 if (item->second->dataHandler)
262 {
Patrick Venture74304642019-01-17 09:31:04 -0800263 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800264 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
265 bytes.end());
266 }
267
Patrick Venturecc7d1602018-11-15 13:58:33 -0800268 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800269}
270
271/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800272 * If you open /flash/image or /flash/tarball, or /flash/hash it will
273 * interpret the open flags and perform whatever actions are required for
274 * that update process. The session returned can be used immediately for
275 * sending data down, without requiring one to open the new active file.
276 *
277 * If you open the active flash image or active hash it will let you
278 * overwrite pieces, depending on the state.
279 *
280 * Once the verification process has started the active files cannot be
281 * opened.
282 *
283 * You can only have one open session at a time. Which means, you can only
284 * have one file open at a time. Trying to open the hash blob_id while you
285 * still have the flash image blob_id open will fail. Opening the flash
286 * blob_id when it is already open will fail.
287 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700288bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
289 const std::string& path)
290{
Patrick Venture6e307a72018-11-09 18:21:17 -0800291 /* Check that they've opened for writing - read back not currently
292 * supported.
293 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700294 if ((flags & blobs::OpenFlags::write) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800295 {
296 return false;
297 }
298
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800299 /* Is the verification process underway? */
300 if (state == UpdateState::verificationStarted)
301 {
302 return false;
303 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800304
305 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800306 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800307 * TODO: Temporarily using a simple boolean flag until there's a full
308 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800309 *
310 * Further on this, if there's an active session to the hash we don't allow
311 * re-opening the image, and if we have the image open, we don't allow
312 * opening the hash. This design decision may be re-evaluated, and changed
313 * to only allow one session per object type (of the two types). But,
314 * consider if the hash is open, do we want to allow writing to the image?
315 * And why would we? But, really, the point of no-return is once the
316 * verification process has begun -- which is done via commit() on the hash
317 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700318 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800319 if (fileOpen)
320 {
321 return false;
322 }
323
Patrick Ventureffcc5502018-11-16 12:32:38 -0800324 /* Handle opening the verifyBlobId --> we know the image and hash aren't
325 * open because of the fileOpen check.
326 *
327 * The file must be opened for writing, but no transport mechanism specified
328 * since it's irrelevant.
329 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700330 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800331 {
332 /* In this case, there's no image handler to use, or data handler,
333 * simply set up a session.
334 */
335 verifyImage.flags = flags;
336 verifyImage.state = Session::State::open;
337
338 lookup[session] = &verifyImage;
339
340 fileOpen = true;
341
342 return true;
343 }
344
Patrick Venturec02849b2018-11-06 17:31:15 -0800345 /* There are two abstractions at play, how you get the data and how you
346 * handle that data. such that, whether the data comes from the PCI bridge
347 * or LPC bridge is not connected to whether the data goes into a static
348 * layout flash update or a UBI tarball.
349 */
350
351 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800352 * support what they request.
353 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800354 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800355 {
356 return false;
357 }
358
359 /* 2) there isn't, so what are they opening? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700360 if (path == activeImageBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800361 {
362 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800363 * already started one (due to canHandleBlob's behavior).
364 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800365 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800366 }
Patrick Venture7dad86f2019-05-17 08:52:20 -0700367 else if (path == activeHashBlobId)
Patrick Venturec02849b2018-11-06 17:31:15 -0800368 {
369 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800370 * already started one (due to canHandleBlob's behavior).
371 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800372 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800373 }
Patrick Venture18235e62018-11-08 10:21:09 -0800374
375 /* How are they expecting to copy this data? */
376 auto d = std::find_if(
377 transports.begin(), transports.end(),
378 [&flags](const auto& iter) { return (iter.bitmask & flags); });
379 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800380 {
Patrick Venture18235e62018-11-08 10:21:09 -0800381 return false;
382 }
383
384 /* We found the transport handler they requested, no surprise since
385 * above we verify they selected at least one we wanted.
386 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800387
Patrick Venture6e307a72018-11-09 18:21:17 -0800388 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
389 * only non-external data pathway -- but this is just a more generic
390 * approach to that.
391 */
392 if (d->handler)
393 {
394 /* If the data handler open call fails, open fails. */
395 if (!d->handler->open())
396 {
397 return false;
398 }
399 }
400
Patrick Venture70e30bf2019-01-17 10:29:28 -0800401 /* Do we have a file handler for the type of file they're opening.
402 * Note: This should only fail if something is somehow crazy wrong.
403 * Since the canHandle() said yes, and that's tied into the list of explicit
404 * firmware handers (and file handlers, like this'll know where to write the
405 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800406 */
Patrick Venture18235e62018-11-08 10:21:09 -0800407 auto h = std::find_if(
408 handlers.begin(), handlers.end(),
409 [&path](const auto& iter) { return (iter.blobName == path); });
410 if (h == handlers.end())
411 {
412 return false;
413 }
414
415 /* Ok, so we found a handler that matched, so call open() */
416 if (!h->handler->open(path))
417 {
418 return false;
419 }
420
Patrick Venture70e30bf2019-01-17 10:29:28 -0800421 Session* curr;
422 const std::string* active;
423
Patrick Venture7dad86f2019-05-17 08:52:20 -0700424 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800425 {
426 /* 2c) are they opening the /flash/hash ? (to start the process) */
427 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700428 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800429 }
430 else
431 {
432 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700433 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800434 }
435
Patrick Venture18235e62018-11-08 10:21:09 -0800436 curr->flags = flags;
437 curr->dataHandler = d->handler;
438 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800439 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800440
441 lookup[session] = curr;
442
Patrick Ventureefba42d2019-05-24 10:48:16 -0700443 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700444 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800445
Patrick Venture12233c52019-05-16 09:26:59 -0700446 state = UpdateState::uploadInProgress;
Patrick Venture991910a2018-11-09 19:43:48 -0800447 fileOpen = true;
448
Patrick Venture18235e62018-11-08 10:21:09 -0800449 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700450}
Patrick Venture53977962018-11-02 18:59:35 -0700451
Patrick Venture18235e62018-11-08 10:21:09 -0800452/**
453 * The write command really just grabs the data from wherever it is and sends it
454 * to the image handler. It's the image handler's responsibility to deal with
455 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800456 *
457 * This receives a session from the blob manager, therefore it is always called
458 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800459 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700460bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
461 const std::vector<uint8_t>& data)
462{
Patrick Venture18235e62018-11-08 10:21:09 -0800463 auto item = lookup.find(session);
464 if (item == lookup.end())
465 {
466 return false;
467 }
468
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800469 /* Prevent writing during verification. */
470 if (state == UpdateState::verificationStarted)
471 {
472 return false;
473 }
474
Patrick Venture8af15eb2019-05-15 09:39:22 -0700475 /* Prevent writing to the verification blob before they trigger
Patrick Venture699750d2019-05-15 09:24:09 -0700476 * verification.
477 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700478 if (item->second->activePath == verifyBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700479 {
480 return false;
481 }
Patrick Venture699750d2019-05-15 09:24:09 -0700482
Patrick Venture18235e62018-11-08 10:21:09 -0800483 std::vector<std::uint8_t> bytes;
484
Patrick Venture05abf7e2018-11-09 11:02:56 -0800485 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800486 {
487 bytes = data;
488 }
489 else
490 {
491 /* little endian required per design, and so on, but TODO: do endianness
492 * with boost.
493 */
494 struct ExtChunkHdr header;
495
496 if (data.size() != sizeof(header))
497 {
498 return false;
499 }
500
501 std::memcpy(&header, data.data(), data.size());
502 bytes = item->second->dataHandler->copyFrom(header.length);
503 }
504
505 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700506}
Patrick Venture18235e62018-11-08 10:21:09 -0800507
Patrick Venture8c535332018-11-08 15:58:00 -0800508/*
509 * If the active session (image or hash) is over LPC, this allows
510 * configuring it. This option is only available before you start
511 * writing data for the given item (image or hash). This will return
512 * false at any other part. -- the lpc handler portion will know to return
513 * false.
514 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700515bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
516 const std::vector<uint8_t>& data)
517{
Patrick Venture8c535332018-11-08 15:58:00 -0800518 auto item = lookup.find(session);
519 if (item == lookup.end())
520 {
521 return false;
522 }
523
Patrick Venture05abf7e2018-11-09 11:02:56 -0800524 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800525 {
526 return false;
527 }
528
Patrick Ventured56097a2019-05-15 09:47:55 -0700529 /* Prevent writing meta to the verification blob (it has no data handler).
530 */
531 if (item->second->dataHandler)
532 {
533 return item->second->dataHandler->writeMeta(data);
534 }
Patrick Venture699750d2019-05-15 09:24:09 -0700535
Patrick Ventured56097a2019-05-15 09:47:55 -0700536 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700537}
Patrick Venture8c535332018-11-08 15:58:00 -0800538
Patrick Ventured6461d62018-11-09 17:30:25 -0800539/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700540 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800541 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800542 * the image.
543 *
544 * For this file to have opened, the other two must be closed, which means any
545 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800546 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700547bool FirmwareBlobHandler::commit(uint16_t session,
548 const std::vector<uint8_t>& data)
549{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800550 auto item = lookup.find(session);
551 if (item == lookup.end())
552 {
553 return false;
554 }
555
556 /* You can only commit on the verifyBlodId */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700557 if (item->second->activePath != verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800558 {
559 return false;
560 }
561
Patrick Venturebe198702019-05-15 09:46:02 -0700562 /* Calling repeatedly has no effect within an update process. */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800563 if (state == UpdateState::verificationStarted)
564 {
Patrick Venturebe198702019-05-15 09:46:02 -0700565 return true;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800566 }
567
568 /* Set state to committing. */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700569 item->second->flags |= blobs::StateFlags::committing;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800570
571 return triggerVerification();
Patrick Venturec7ca2912018-11-02 11:38:33 -0700572}
Patrick Ventured6461d62018-11-09 17:30:25 -0800573
574/*
575 * Close must be called on the firmware image before triggering
576 * verification via commit. Once the verification is complete, you can
577 * then close the hash file.
578 *
579 * If the `verify_image.service` returned success, closing the hash file
580 * will have a specific behavior depending on the update. If it's UBI,
581 * it'll perform the install. If it's static layout, it'll do nothing. The
582 * verify_image service in the static layout case is responsible for placing
583 * the file in the correct staging position.
584 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700585bool FirmwareBlobHandler::close(uint16_t session)
586{
Patrick Venture68bb1432018-11-09 20:08:48 -0800587 auto item = lookup.find(session);
588 if (item == lookup.end())
589 {
590 return false;
591 }
592
Patrick Ventureffcc5502018-11-16 12:32:38 -0800593 /* Are you closing the verify blob? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700594 if (item->second->activePath == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800595 {
Patrick Ventureffcc5502018-11-16 12:32:38 -0800596 if (state == UpdateState::verificationStarted)
597 {
Patrick Venture12233c52019-05-16 09:26:59 -0700598 /* TODO: If they close this blob before verification finishes,
599 * that's an abort.
600 */
Patrick Ventureffcc5502018-11-16 12:32:38 -0800601 return false;
602 }
Patrick Venture12233c52019-05-16 09:26:59 -0700603 else if (state == UpdateState::verificationCompleted)
604 {
605 /* TODO: Should this delete the verification artifact? */
606 state = UpdateState::notYetStarted;
607
608 /* A this point, delete the active blob IDs from the blob_list. */
Patrick Venture930c7b72019-05-24 11:11:08 -0700609 removeBlobId(activeImageBlobId);
610 removeBlobId(activeHashBlobId);
Patrick Venture12233c52019-05-16 09:26:59 -0700611 }
612 /* Must be verificationPending... not yet started, they may re-open and
613 * trigger verification.
614 */
615 }
616 else
617 {
618 /* They are closing a data pathway (image, tarball, hash). */
619 state = UpdateState::verificationPending;
Patrick Venture930c7b72019-05-24 11:11:08 -0700620
621 /* Add verify blob ID now that we can expect it. */
622 addBlobId(verifyBlobId);
Patrick Ventureffcc5502018-11-16 12:32:38 -0800623 }
624
Patrick Venture68bb1432018-11-09 20:08:48 -0800625 if (item->second->dataHandler)
626 {
627 item->second->dataHandler->close();
628 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800629 if (item->second->imageHandler)
630 {
631 item->second->imageHandler->close();
632 }
633
Patrick Venture68bb1432018-11-09 20:08:48 -0800634 item->second->state = Session::State::closed;
635 /* Do not delete the active blob_id from the list of blob_ids, because that
636 * blob_id indicates there is data stored. Delete will destroy it.
637 */
638
639 lookup.erase(item);
640
Patrick Venture991910a2018-11-09 19:43:48 -0800641 fileOpen = false;
642
Patrick Venture68bb1432018-11-09 20:08:48 -0800643 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700644}
Patrick Ventured6461d62018-11-09 17:30:25 -0800645
Patrick Venturec7ca2912018-11-02 11:38:33 -0700646bool FirmwareBlobHandler::expire(uint16_t session)
647{
648 return false;
649}
Patrick Ventured6461d62018-11-09 17:30:25 -0800650
651/*
652 * Currently, the design does not provide this with a function, however,
653 * it will likely change to support reading data back.
654 */
655std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
656 uint32_t offset,
657 uint32_t requestedSize)
658{
659 return {};
660}
661
Patrick Ventureffcc5502018-11-16 12:32:38 -0800662bool FirmwareBlobHandler::triggerVerification()
663{
Patrick Venture3ecb3502019-05-17 11:03:51 -0700664 bool result = verification->triggerVerification();
665 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800666 {
Patrick Venture453f06a2019-01-17 10:02:12 -0800667 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800668 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800669
Patrick Venture3ecb3502019-05-17 11:03:51 -0700670 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800671}
672
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700673} // namespace ipmi_flash