blob: 13e5d72ca35d7d43471bdd38256cebedd0477b4e [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 Ventured342a952019-05-29 09:09:10 -0700159 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700160 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800161 {
Patrick Ventured342a952019-05-29 09:09:10 -0700162 /* These blobs are placeholders that indicate things, or allow actions,
163 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800164 */
Patrick Ventured342a952019-05-29 09:09:10 -0700165 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800166 }
167
Patrick Ventured342a952019-05-29 09:09:10 -0700168 /* They are requesting information about the generic blob_id. */
169 meta->blobState = bitmask;
170 meta->size = 0;
171
172 /* The generic blob_ids state is only the bits related to the transport
173 * mechanisms.
174 */
175 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700176}
Patrick Venture53977962018-11-02 18:59:35 -0700177
Patrick Venturec02849b2018-11-06 17:31:15 -0800178/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800179 * Return stat information on an open session. It therefore must be an active
180 * handle to either the active image or active hash.
181 *
182 * The stat() and sessionstat() commands will return the same information in
183 * many cases, therefore the logic will be combined.
184 *
185 * TODO: combine the logic for stat and sessionstat().
186 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700187bool FirmwareBlobHandler::stat(uint16_t session, struct blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800188{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800189 auto item = lookup.find(session);
190 if (item == lookup.end())
191 {
192 return false;
193 }
194
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700195 /* The size here refers to the size of the file -- of something analagous.
196 */
197 meta->size = (item->second->imageHandler)
198 ? item->second->imageHandler->getSize()
199 : 0;
200
201 meta->metadata.clear();
202
Patrick Venture7dad86f2019-05-17 08:52:20 -0700203 if (item->second->activePath == verifyBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700204 {
Patrick Venturef1f0f652019-06-03 09:10:19 -0700205 VerifyCheckResponses value = VerifyCheckResponses::other;
Patrick Venture01a33272019-05-23 19:48:22 -0700206
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700207 switch (state)
Patrick Venture01a33272019-05-23 19:48:22 -0700208 {
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700209 case UpdateState::verificationPending:
210 value = VerifyCheckResponses::other;
211 break;
Patrick Venturef1f0f652019-06-03 09:10:19 -0700212 case UpdateState::verificationStarted:
213 value = verification->status();
214 lastVerificationResponse = value;
215 break;
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700216 case UpdateState::verificationCompleted:
217 value = lastVerificationResponse;
218 break;
219 default:
Patrick Venturef1f0f652019-06-03 09:10:19 -0700220 break;
Patrick Venture01a33272019-05-23 19:48:22 -0700221 }
Patrick Venture699750d2019-05-15 09:24:09 -0700222
Patrick Venturee955e072019-05-15 16:16:46 -0700223 meta->metadata.push_back(static_cast<std::uint8_t>(value));
224
225 /* Change the firmware handler's state and the blob's stat value
226 * depending.
227 */
228 if (value == VerifyCheckResponses::success ||
229 value == VerifyCheckResponses::failed)
230 {
231 state = UpdateState::verificationCompleted;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700232 item->second->flags &= ~blobs::StateFlags::committing;
Patrick Venturee955e072019-05-15 16:16:46 -0700233
234 if (value == VerifyCheckResponses::success)
235 {
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700236 item->second->flags |= blobs::StateFlags::committed;
Patrick Venturee955e072019-05-15 16:16:46 -0700237 }
238 else
239 {
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700240 item->second->flags |= blobs::StateFlags::commit_error;
Patrick Venturee955e072019-05-15 16:16:46 -0700241 }
242 }
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700243 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700244 else if (item->second->activePath == updateBlobId)
245 {
Patrick Venturef1f0f652019-06-03 09:10:19 -0700246 UpdateStatus value = UpdateStatus::unknown;
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700247
248 switch (state)
249 {
250 case UpdateState::updatePending:
251 value = UpdateStatus::unknown;
252 break;
Patrick Venturef1f0f652019-06-03 09:10:19 -0700253 case UpdateState::updateStarted:
254 value = update->status();
255 break;
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700256 default:
257 break;
258 }
259
260 meta->metadata.push_back(static_cast<std::uint8_t>(value));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700261
262 if (value == UpdateStatus::success || value == UpdateStatus::failed)
263 {
264 state = UpdateState::updateCompleted;
265 item->second->flags &= ~blobs::StateFlags::committing;
266
267 if (value == UpdateStatus::success)
268 {
269 item->second->flags |= blobs::StateFlags::committed;
270 }
271 else
272 {
273 item->second->flags |= blobs::StateFlags::commit_error;
274 }
275 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700276 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800277
Patrick Venturee955e072019-05-15 16:16:46 -0700278 /* The blobState here relates to an active sesion, so we should return the
279 * flags used to open this session.
280 */
281 meta->blobState = item->second->flags;
282
Patrick Venturecc7d1602018-11-15 13:58:33 -0800283 /* The metadata blob returned comes from the data handler... it's used for
284 * instance, in P2A bridging to get required information about the mapping,
285 * and is the "opposite" of the lpc writemeta requirement.
286 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800287 if (item->second->dataHandler)
288 {
Patrick Venture74304642019-01-17 09:31:04 -0800289 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800290 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
291 bytes.end());
292 }
293
Patrick Venturecc7d1602018-11-15 13:58:33 -0800294 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800295}
296
297/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800298 * If you open /flash/image or /flash/tarball, or /flash/hash it will
299 * interpret the open flags and perform whatever actions are required for
300 * that update process. The session returned can be used immediately for
301 * sending data down, without requiring one to open the new active file.
302 *
303 * If you open the active flash image or active hash it will let you
304 * overwrite pieces, depending on the state.
305 *
306 * Once the verification process has started the active files cannot be
307 * opened.
308 *
309 * You can only have one open session at a time. Which means, you can only
310 * have one file open at a time. Trying to open the hash blob_id while you
311 * still have the flash image blob_id open will fail. Opening the flash
312 * blob_id when it is already open will fail.
313 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700314bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
315 const std::string& path)
316{
Patrick Venture6e307a72018-11-09 18:21:17 -0800317 /* Check that they've opened for writing - read back not currently
318 * supported.
319 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700320 if ((flags & blobs::OpenFlags::write) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800321 {
322 return false;
323 }
324
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800325 /* Is the verification process underway? */
326 if (state == UpdateState::verificationStarted)
327 {
328 return false;
329 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800330
331 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800332 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800333 * TODO: Temporarily using a simple boolean flag until there's a full
334 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800335 *
336 * Further on this, if there's an active session to the hash we don't allow
337 * re-opening the image, and if we have the image open, we don't allow
338 * opening the hash. This design decision may be re-evaluated, and changed
339 * to only allow one session per object type (of the two types). But,
340 * consider if the hash is open, do we want to allow writing to the image?
341 * And why would we? But, really, the point of no-return is once the
342 * verification process has begun -- which is done via commit() on the hash
343 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700344 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800345 if (fileOpen)
346 {
347 return false;
348 }
349
Patrick Venture19f5d882019-05-30 14:34:55 -0700350 /* When in this state, they can only open the updateBlobId */
351 if (state == UpdateState::updatePending)
352 {
353 if (path != updateBlobId)
354 {
355 return false;
356 }
357 else
358 {
359 /* Similarly to verifyBlodId, this is special. */
360 updateImage.flags = flags;
361 updateImage.state = Session::State::open;
362
363 lookup[session] = &updateImage;
364
365 fileOpen = true;
366 return true;
367 }
368 }
369
Patrick Ventureffcc5502018-11-16 12:32:38 -0800370 /* Handle opening the verifyBlobId --> we know the image and hash aren't
371 * open because of the fileOpen check.
372 *
373 * The file must be opened for writing, but no transport mechanism specified
374 * since it's irrelevant.
375 */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700376 if (path == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800377 {
378 /* In this case, there's no image handler to use, or data handler,
379 * simply set up a session.
380 */
381 verifyImage.flags = flags;
382 verifyImage.state = Session::State::open;
383
384 lookup[session] = &verifyImage;
385
386 fileOpen = true;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800387 return true;
388 }
389
Patrick Venturec02849b2018-11-06 17:31:15 -0800390 /* There are two abstractions at play, how you get the data and how you
391 * handle that data. such that, whether the data comes from the PCI bridge
392 * or LPC bridge is not connected to whether the data goes into a static
393 * layout flash update or a UBI tarball.
394 */
395
Patrick Venture19f5d882019-05-30 14:34:55 -0700396 /* 2) there isn't, so what are they opening? */
397 if (path == activeImageBlobId || path == activeHashBlobId)
398 {
399 /* 2a) are they opening the active image? this can only happen if they
400 * already started one (due to canHandleBlob's behavior).
401 */
402 /* 2b) are they opening the active hash? this can only happen if they
403 * already started one (due to canHandleBlob's behavior).
404 */
405 return false;
406 }
407
Patrick Venturec02849b2018-11-06 17:31:15 -0800408 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800409 * support what they request.
410 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800411 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800412 {
413 return false;
414 }
415
Patrick Venture18235e62018-11-08 10:21:09 -0800416 /* How are they expecting to copy this data? */
417 auto d = std::find_if(
418 transports.begin(), transports.end(),
419 [&flags](const auto& iter) { return (iter.bitmask & flags); });
420 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800421 {
Patrick Venture18235e62018-11-08 10:21:09 -0800422 return false;
423 }
424
425 /* We found the transport handler they requested, no surprise since
426 * above we verify they selected at least one we wanted.
427 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800428
Patrick Venture6e307a72018-11-09 18:21:17 -0800429 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
430 * only non-external data pathway -- but this is just a more generic
431 * approach to that.
432 */
433 if (d->handler)
434 {
435 /* If the data handler open call fails, open fails. */
436 if (!d->handler->open())
437 {
438 return false;
439 }
440 }
441
Patrick Venture70e30bf2019-01-17 10:29:28 -0800442 /* Do we have a file handler for the type of file they're opening.
443 * Note: This should only fail if something is somehow crazy wrong.
444 * Since the canHandle() said yes, and that's tied into the list of explicit
445 * firmware handers (and file handlers, like this'll know where to write the
446 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800447 */
Patrick Venture18235e62018-11-08 10:21:09 -0800448 auto h = std::find_if(
449 handlers.begin(), handlers.end(),
450 [&path](const auto& iter) { return (iter.blobName == path); });
451 if (h == handlers.end())
452 {
453 return false;
454 }
455
456 /* Ok, so we found a handler that matched, so call open() */
457 if (!h->handler->open(path))
458 {
459 return false;
460 }
461
Patrick Venture70e30bf2019-01-17 10:29:28 -0800462 Session* curr;
463 const std::string* active;
464
Patrick Venture7dad86f2019-05-17 08:52:20 -0700465 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800466 {
467 /* 2c) are they opening the /flash/hash ? (to start the process) */
468 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700469 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800470 }
471 else
472 {
473 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700474 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800475 }
476
Patrick Venture18235e62018-11-08 10:21:09 -0800477 curr->flags = flags;
478 curr->dataHandler = d->handler;
479 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800480 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800481
482 lookup[session] = curr;
483
Patrick Ventureefba42d2019-05-24 10:48:16 -0700484 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700485 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800486
Patrick Venture12233c52019-05-16 09:26:59 -0700487 state = UpdateState::uploadInProgress;
Patrick Venture991910a2018-11-09 19:43:48 -0800488 fileOpen = true;
489
Patrick Venture18235e62018-11-08 10:21:09 -0800490 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700491}
Patrick Venture53977962018-11-02 18:59:35 -0700492
Patrick Venture18235e62018-11-08 10:21:09 -0800493/**
494 * The write command really just grabs the data from wherever it is and sends it
495 * to the image handler. It's the image handler's responsibility to deal with
496 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800497 *
498 * This receives a session from the blob manager, therefore it is always called
499 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800500 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700501bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
502 const std::vector<uint8_t>& data)
503{
Patrick Venture18235e62018-11-08 10:21:09 -0800504 auto item = lookup.find(session);
505 if (item == lookup.end())
506 {
507 return false;
508 }
509
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800510 /* Prevent writing during verification. */
511 if (state == UpdateState::verificationStarted)
512 {
513 return false;
514 }
515
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700516 /* Prevent writing to the verification or update blobs. */
517 if (item->second->activePath == verifyBlobId ||
518 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700519 {
520 return false;
521 }
Patrick Venture699750d2019-05-15 09:24:09 -0700522
Patrick Venture18235e62018-11-08 10:21:09 -0800523 std::vector<std::uint8_t> bytes;
524
Patrick Venture05abf7e2018-11-09 11:02:56 -0800525 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800526 {
527 bytes = data;
528 }
529 else
530 {
531 /* little endian required per design, and so on, but TODO: do endianness
532 * with boost.
533 */
534 struct ExtChunkHdr header;
535
536 if (data.size() != sizeof(header))
537 {
538 return false;
539 }
540
541 std::memcpy(&header, data.data(), data.size());
542 bytes = item->second->dataHandler->copyFrom(header.length);
543 }
544
545 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700546}
Patrick Venture18235e62018-11-08 10:21:09 -0800547
Patrick Venture8c535332018-11-08 15:58:00 -0800548/*
549 * If the active session (image or hash) is over LPC, this allows
550 * configuring it. This option is only available before you start
551 * writing data for the given item (image or hash). This will return
552 * false at any other part. -- the lpc handler portion will know to return
553 * false.
554 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700555bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
556 const std::vector<uint8_t>& data)
557{
Patrick Venture8c535332018-11-08 15:58:00 -0800558 auto item = lookup.find(session);
559 if (item == lookup.end())
560 {
561 return false;
562 }
563
Patrick Venture05abf7e2018-11-09 11:02:56 -0800564 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800565 {
566 return false;
567 }
568
Patrick Ventured56097a2019-05-15 09:47:55 -0700569 /* Prevent writing meta to the verification blob (it has no data handler).
570 */
571 if (item->second->dataHandler)
572 {
573 return item->second->dataHandler->writeMeta(data);
574 }
Patrick Venture699750d2019-05-15 09:24:09 -0700575
Patrick Ventured56097a2019-05-15 09:47:55 -0700576 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700577}
Patrick Venture8c535332018-11-08 15:58:00 -0800578
Patrick Ventured6461d62018-11-09 17:30:25 -0800579/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700580 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800581 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800582 * the image.
583 *
584 * For this file to have opened, the other two must be closed, which means any
585 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800586 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700587bool FirmwareBlobHandler::commit(uint16_t session,
588 const std::vector<uint8_t>& data)
589{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800590 auto item = lookup.find(session);
591 if (item == lookup.end())
592 {
593 return false;
594 }
595
Patrick Venture1a406fe2019-05-31 07:29:56 -0700596 /* You can only commit on the verifyBlodId or updateBlobId */
597 if (item->second->activePath != verifyBlobId &&
598 item->second->activePath != updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800599 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700600 std::fprintf(stderr, "path: '%s' not expected for commit\n",
601 item->second->activePath.c_str());
Patrick Ventureffcc5502018-11-16 12:32:38 -0800602 return false;
603 }
604
Patrick Venture433cbc32019-05-30 09:53:10 -0700605 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800606 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700607 case UpdateState::verificationPending:
608 /* Set state to committing. */
609 item->second->flags |= blobs::StateFlags::committing;
610 return triggerVerification();
Patrick Venture433cbc32019-05-30 09:53:10 -0700611 case UpdateState::verificationStarted:
612 /* Calling repeatedly has no effect within an update process. */
613 return true;
614 case UpdateState::verificationCompleted:
615 /* Calling after the verification process has completed returns
616 * failure. */
617 return false;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700618 case UpdateState::updatePending:
Patrick Venture433cbc32019-05-30 09:53:10 -0700619 item->second->flags |= blobs::StateFlags::committing;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700620 return triggerUpdate();
Patrick Venture0079d892019-05-31 11:27:44 -0700621 case UpdateState::updateStarted:
622 /* Calling repeatedly has no effect within an update process. */
623 return true;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700624 default:
625 return false;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800626 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700627}
Patrick Ventured6461d62018-11-09 17:30:25 -0800628
629/*
630 * Close must be called on the firmware image before triggering
631 * verification via commit. Once the verification is complete, you can
632 * then close the hash file.
633 *
634 * If the `verify_image.service` returned success, closing the hash file
635 * will have a specific behavior depending on the update. If it's UBI,
636 * it'll perform the install. If it's static layout, it'll do nothing. The
637 * verify_image service in the static layout case is responsible for placing
638 * the file in the correct staging position.
639 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700640bool FirmwareBlobHandler::close(uint16_t session)
641{
Patrick Venture68bb1432018-11-09 20:08:48 -0800642 auto item = lookup.find(session);
643 if (item == lookup.end())
644 {
645 return false;
646 }
647
Patrick Ventureffcc5502018-11-16 12:32:38 -0800648 /* Are you closing the verify blob? */
Patrick Venture7dad86f2019-05-17 08:52:20 -0700649 if (item->second->activePath == verifyBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800650 {
Patrick Venture1c6d8f52019-05-30 10:53:49 -0700651 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800652 {
Patrick Venture1c6d8f52019-05-30 10:53:49 -0700653 case UpdateState::verificationStarted:
654 /* TODO: If they close this blob before verification finishes,
655 * that's an abort.
656 */
657 return false;
658 case UpdateState::verificationCompleted:
659 if (lastVerificationResponse == VerifyCheckResponses::success)
660 {
661 state = UpdateState::updatePending;
662 addBlobId(updateBlobId);
Patrick Venturead933832019-05-30 14:13:29 -0700663 removeBlobId(verifyBlobId);
Patrick Venture1c6d8f52019-05-30 10:53:49 -0700664 }
665 else
666 {
667 /* TODO: Verification failed, what now? */
668 state = UpdateState::notYetStarted;
669 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700670 break;
Patrick Venture1c6d8f52019-05-30 10:53:49 -0700671 default:
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700672 break;
Patrick Venture12233c52019-05-16 09:26:59 -0700673 }
674 /* Must be verificationPending... not yet started, they may re-open and
675 * trigger verification.
676 */
677 }
Patrick Venture6b0aa182019-05-30 14:47:32 -0700678 else if (item->second->activePath == updateBlobId)
679 {
680 /* nothing interesting. */
681 }
Patrick Venture12233c52019-05-16 09:26:59 -0700682 else
683 {
684 /* They are closing a data pathway (image, tarball, hash). */
685 state = UpdateState::verificationPending;
Patrick Venture930c7b72019-05-24 11:11:08 -0700686
687 /* Add verify blob ID now that we can expect it. */
688 addBlobId(verifyBlobId);
Patrick Ventureffcc5502018-11-16 12:32:38 -0800689 }
690
Patrick Venture68bb1432018-11-09 20:08:48 -0800691 if (item->second->dataHandler)
692 {
693 item->second->dataHandler->close();
694 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800695 if (item->second->imageHandler)
696 {
697 item->second->imageHandler->close();
698 }
699
Patrick Venture68bb1432018-11-09 20:08:48 -0800700 item->second->state = Session::State::closed;
Patrick Venture68bb1432018-11-09 20:08:48 -0800701 lookup.erase(item);
Patrick Venture991910a2018-11-09 19:43:48 -0800702 fileOpen = false;
Patrick Venture68bb1432018-11-09 20:08:48 -0800703 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700704}
Patrick Ventured6461d62018-11-09 17:30:25 -0800705
Patrick Venturec7ca2912018-11-02 11:38:33 -0700706bool FirmwareBlobHandler::expire(uint16_t session)
707{
708 return false;
709}
Patrick Ventured6461d62018-11-09 17:30:25 -0800710
711/*
712 * Currently, the design does not provide this with a function, however,
713 * it will likely change to support reading data back.
714 */
715std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
716 uint32_t offset,
717 uint32_t requestedSize)
718{
719 return {};
720}
721
Patrick Ventureffcc5502018-11-16 12:32:38 -0800722bool FirmwareBlobHandler::triggerVerification()
723{
Patrick Venture3ecb3502019-05-17 11:03:51 -0700724 bool result = verification->triggerVerification();
725 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800726 {
Patrick Venture453f06a2019-01-17 10:02:12 -0800727 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800728 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800729
Patrick Venture3ecb3502019-05-17 11:03:51 -0700730 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800731}
732
Patrick Venture1a406fe2019-05-31 07:29:56 -0700733bool FirmwareBlobHandler::triggerUpdate()
734{
735 bool result = update->triggerUpdate();
736 if (result)
737 {
738 state = UpdateState::updateStarted;
739 }
740
741 return result;
742}
743
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700744} // namespace ipmi_flash