blob: e6f0fee397ff1046b2208566a0c04277b2f7f019 [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 Venture1d66fe62019-06-03 14:57:27 -070020#include "status.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -070021#include "util.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 Venture1d66fe62019-06-03 14:57:27 -070041 std::unique_ptr<TriggerableActionInterface> verification,
42 std::unique_ptr<TriggerableActionInterface> 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.
100 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700101bool FirmwareBlobHandler::deleteBlob(const std::string& path)
102{
Patrick Venturebcc0c772019-06-17 10:42:06 -0700103 /* This cannot be called if you have an open session to the path.
104 * You can have an open session to verify/update/hash/image, but not active*
105 *
106 * Therefore, if this is called, it's either on a blob that isn't presently
107 * open. However, there could be open blobs, so we need to close all open
108 * sessions. This closing on our is an invalid handler behavior. Therefore,
109 * we cannot close an active session. To enforce this, we only allow
110 * deleting if there isn't a file open.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800111 */
Patrick Venturebcc0c772019-06-17 10:42:06 -0700112 if (fileOpen)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800113 {
114 return false;
115 }
116
Patrick Venture72676762019-06-17 11:22:38 -0700117 /* only includes states where fileOpen == false */
118 switch (state)
119 {
120 case UpdateState::notYetStarted:
121 /* Trying to delete anything at this point has no effect and returns
122 * false.
123 */
124 return false;
125 case UpdateState::verificationPending:
Patrick Venture2ca66522019-06-17 11:58:38 -0700126 abortProcess();
127 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700128 case UpdateState::updatePending:
Patrick Venturec9f62392019-06-17 12:17:26 -0700129 abortProcess();
130 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700131 default:
132 break;
133 }
134
Patrick Venturebcc0c772019-06-17 10:42:06 -0700135 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700136}
Patrick Venture53977962018-11-02 18:59:35 -0700137
Patrick Ventured6461d62018-11-09 17:30:25 -0800138/*
139 * Stat on the files will return information such as what supported
140 * transport mechanisms are available.
141 *
142 * Stat on an active file or hash will return information such as the size
143 * of the data cached, and any additional pertinent information. The
144 * blob_state on the active files will return the state of the update.
145 */
Patrick Venturee312f392019-06-04 07:44:37 -0700146bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700147{
Patrick Venture46637c82018-11-06 15:20:24 -0800148 /* We know we support this path because canHandle is called ahead */
Patrick Ventured342a952019-05-29 09:09:10 -0700149 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700150 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800151 {
Patrick Ventured342a952019-05-29 09:09:10 -0700152 /* These blobs are placeholders that indicate things, or allow actions,
153 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800154 */
Patrick Ventured342a952019-05-29 09:09:10 -0700155 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800156 }
157
Patrick Ventured342a952019-05-29 09:09:10 -0700158 /* They are requesting information about the generic blob_id. */
159 meta->blobState = bitmask;
160 meta->size = 0;
161
162 /* The generic blob_ids state is only the bits related to the transport
163 * mechanisms.
164 */
165 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700166}
Patrick Venture53977962018-11-02 18:59:35 -0700167
Patrick Ventureda66fd82019-06-03 11:11:24 -0700168ActionStatus FirmwareBlobHandler::getActionStatus()
Patrick Venturea2d82392019-06-03 10:55:17 -0700169{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700170 ActionStatus value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700171
172 switch (state)
173 {
174 case UpdateState::verificationPending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700175 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700176 break;
177 case UpdateState::verificationStarted:
178 value = verification->status();
Patrick Ventureda66fd82019-06-03 11:11:24 -0700179 lastVerificationStatus = value;
Patrick Venturea2d82392019-06-03 10:55:17 -0700180 break;
181 case UpdateState::verificationCompleted:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700182 value = lastVerificationStatus;
Patrick Venturea2d82392019-06-03 10:55:17 -0700183 break;
Patrick Venturea2d82392019-06-03 10:55:17 -0700184 case UpdateState::updatePending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700185 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700186 break;
187 case UpdateState::updateStarted:
188 value = update->status();
189 lastUpdateStatus = value;
190 break;
191 case UpdateState::updateCompleted:
192 value = lastUpdateStatus;
193 break;
194 default:
195 break;
196 }
197
198 return value;
199}
200
Patrick Venturec02849b2018-11-06 17:31:15 -0800201/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800202 * Return stat information on an open session. It therefore must be an active
203 * handle to either the active image or active hash.
Patrick Ventured6461d62018-11-09 17:30:25 -0800204 */
Patrick Venturee312f392019-06-04 07:44:37 -0700205bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800206{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800207 auto item = lookup.find(session);
208 if (item == lookup.end())
209 {
210 return false;
211 }
212
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700213 /* The size here refers to the size of the file -- of something analagous.
214 */
215 meta->size = (item->second->imageHandler)
216 ? item->second->imageHandler->getSize()
217 : 0;
218
219 meta->metadata.clear();
220
Patrick Ventureda66fd82019-06-03 11:11:24 -0700221 if (item->second->activePath == verifyBlobId ||
222 item->second->activePath == updateBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700223 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700224 ActionStatus value = getActionStatus();
Patrick Venture699750d2019-05-15 09:24:09 -0700225
Patrick Venturee955e072019-05-15 16:16:46 -0700226 meta->metadata.push_back(static_cast<std::uint8_t>(value));
227
228 /* Change the firmware handler's state and the blob's stat value
229 * depending.
230 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700231 if (value == ActionStatus::success || value == ActionStatus::failed)
Patrick Venturee955e072019-05-15 16:16:46 -0700232 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700233 if (item->second->activePath == verifyBlobId)
Patrick Venturee955e072019-05-15 16:16:46 -0700234 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700235 state = UpdateState::verificationCompleted;
Patrick Venturee955e072019-05-15 16:16:46 -0700236 }
237 else
238 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700239 /* item->second->activePath == updateBlobId */
240 state = UpdateState::updateCompleted;
Patrick Venturee955e072019-05-15 16:16:46 -0700241 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700242
Patrick Venturef1f0f652019-06-03 09:10:19 -0700243 item->second->flags &= ~blobs::StateFlags::committing;
244
Patrick Ventureda66fd82019-06-03 11:11:24 -0700245 if (value == ActionStatus::success)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700246 {
247 item->second->flags |= blobs::StateFlags::committed;
248 }
249 else
250 {
251 item->second->flags |= blobs::StateFlags::commit_error;
252 }
253 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700254 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800255
Patrick Venturee955e072019-05-15 16:16:46 -0700256 /* The blobState here relates to an active sesion, so we should return the
257 * flags used to open this session.
258 */
259 meta->blobState = item->second->flags;
260
Patrick Venturecc7d1602018-11-15 13:58:33 -0800261 /* The metadata blob returned comes from the data handler... it's used for
262 * instance, in P2A bridging to get required information about the mapping,
263 * and is the "opposite" of the lpc writemeta requirement.
264 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800265 if (item->second->dataHandler)
266 {
Patrick Venture74304642019-01-17 09:31:04 -0800267 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800268 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
269 bytes.end());
270 }
271
Patrick Venturecc7d1602018-11-15 13:58:33 -0800272 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800273}
274
275/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800276 * If you open /flash/image or /flash/tarball, or /flash/hash it will
277 * interpret the open flags and perform whatever actions are required for
278 * that update process. The session returned can be used immediately for
279 * sending data down, without requiring one to open the new active file.
280 *
281 * If you open the active flash image or active hash it will let you
282 * overwrite pieces, depending on the state.
283 *
284 * Once the verification process has started the active files cannot be
285 * opened.
286 *
287 * You can only have one open session at a time. Which means, you can only
288 * have one file open at a time. Trying to open the hash blob_id while you
289 * still have the flash image blob_id open will fail. Opening the flash
290 * blob_id when it is already open will fail.
291 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700292bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
293 const std::string& path)
294{
Patrick Venturec02849b2018-11-06 17:31:15 -0800295 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800296 *
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800297 * Further on this, if there's an active session to the hash we don't allow
298 * re-opening the image, and if we have the image open, we don't allow
299 * opening the hash. This design decision may be re-evaluated, and changed
300 * to only allow one session per object type (of the two types). But,
301 * consider if the hash is open, do we want to allow writing to the image?
302 * And why would we? But, really, the point of no-return is once the
303 * verification process has begun -- which is done via commit() on the hash
304 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700305 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800306 if (fileOpen)
307 {
308 return false;
309 }
310
Patrick Venture723113f2019-06-05 09:38:35 -0700311 /* The active blobs are only meant to indicate status that something has
312 * opened the image file or the hash file.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800313 */
Patrick Venture19f5d882019-05-30 14:34:55 -0700314 if (path == activeImageBlobId || path == activeHashBlobId)
315 {
316 /* 2a) are they opening the active image? this can only happen if they
317 * already started one (due to canHandleBlob's behavior).
318 */
319 /* 2b) are they opening the active hash? this can only happen if they
320 * already started one (due to canHandleBlob's behavior).
321 */
322 return false;
323 }
324
Patrick Venture723113f2019-06-05 09:38:35 -0700325 /* Check that they've opened for writing - read back not currently
326 * supported.
327 */
328 if ((flags & blobs::OpenFlags::write) == 0)
329 {
330 return false;
331 }
332
333 /* Because canHandleBlob is called before open, we know that if they try to
334 * open the verifyBlobId, they're in a state where it's present.
335 */
336
337 switch (state)
338 {
339 case UpdateState::uploadInProgress:
340 /* Unreachable code because if it's started a file is open. */
341 break;
342 case UpdateState::verificationPending:
343 /* Handle opening the verifyBlobId --> we know the image and hash
344 * aren't open because of the fileOpen check. They can still open
345 * other files from this state to transition back into
346 * uploadInProgress.
347 *
348 * The file must be opened for writing, but no transport mechanism
349 * specified since it's irrelevant.
350 */
351 if (path == verifyBlobId)
352 {
353 verifyImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700354
355 lookup[session] = &verifyImage;
356
357 fileOpen = true;
358 return true;
359 }
360 break;
361 case UpdateState::verificationStarted:
362 case UpdateState::verificationCompleted:
363 /* Unreachable code because if it's started a file is open. */
364 return false;
365 case UpdateState::updatePending:
366 {
367 /* When in this state, they can only open the updateBlobId */
368 if (path == updateBlobId)
369 {
370 updateImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700371
372 lookup[session] = &updateImage;
373
374 fileOpen = true;
375 return true;
376 }
377 else
378 {
379 return false;
380 }
381 }
382 case UpdateState::updateStarted:
383 case UpdateState::updateCompleted:
384 /* Unreachable code because if it's started a file is open. */
385 break;
386 default:
387 break;
388 }
389
390 /* 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 Venturec02849b2018-11-06 17:31:15 -0800396 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800397 * support what they request.
398 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800399 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800400 {
401 return false;
402 }
403
Patrick Venture18235e62018-11-08 10:21:09 -0800404 /* How are they expecting to copy this data? */
405 auto d = std::find_if(
406 transports.begin(), transports.end(),
407 [&flags](const auto& iter) { return (iter.bitmask & flags); });
408 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800409 {
Patrick Venture18235e62018-11-08 10:21:09 -0800410 return false;
411 }
412
413 /* We found the transport handler they requested, no surprise since
414 * above we verify they selected at least one we wanted.
415 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800416
Patrick Venture6e307a72018-11-09 18:21:17 -0800417 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
418 * only non-external data pathway -- but this is just a more generic
419 * approach to that.
420 */
421 if (d->handler)
422 {
423 /* If the data handler open call fails, open fails. */
424 if (!d->handler->open())
425 {
426 return false;
427 }
428 }
429
Patrick Venture70e30bf2019-01-17 10:29:28 -0800430 /* Do we have a file handler for the type of file they're opening.
431 * Note: This should only fail if something is somehow crazy wrong.
432 * Since the canHandle() said yes, and that's tied into the list of explicit
433 * firmware handers (and file handlers, like this'll know where to write the
434 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800435 */
Patrick Venture18235e62018-11-08 10:21:09 -0800436 auto h = std::find_if(
437 handlers.begin(), handlers.end(),
438 [&path](const auto& iter) { return (iter.blobName == path); });
439 if (h == handlers.end())
440 {
441 return false;
442 }
443
444 /* Ok, so we found a handler that matched, so call open() */
445 if (!h->handler->open(path))
446 {
447 return false;
448 }
449
Patrick Venture70e30bf2019-01-17 10:29:28 -0800450 Session* curr;
451 const std::string* active;
452
Patrick Venture7dad86f2019-05-17 08:52:20 -0700453 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800454 {
455 /* 2c) are they opening the /flash/hash ? (to start the process) */
456 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700457 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800458 }
459 else
460 {
461 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700462 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800463 }
464
Patrick Venture18235e62018-11-08 10:21:09 -0800465 curr->flags = flags;
466 curr->dataHandler = d->handler;
467 curr->imageHandler = h->handler;
468
469 lookup[session] = curr;
470
Patrick Ventureefba42d2019-05-24 10:48:16 -0700471 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700472 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800473
Patrick Venture12233c52019-05-16 09:26:59 -0700474 state = UpdateState::uploadInProgress;
Patrick Venture991910a2018-11-09 19:43:48 -0800475 fileOpen = true;
476
Patrick Venture18235e62018-11-08 10:21:09 -0800477 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700478}
Patrick Venture53977962018-11-02 18:59:35 -0700479
Patrick Venture18235e62018-11-08 10:21:09 -0800480/**
481 * The write command really just grabs the data from wherever it is and sends it
482 * to the image handler. It's the image handler's responsibility to deal with
483 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800484 *
485 * This receives a session from the blob manager, therefore it is always called
486 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800487 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700488bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
489 const std::vector<uint8_t>& data)
490{
Patrick Venture18235e62018-11-08 10:21:09 -0800491 auto item = lookup.find(session);
492 if (item == lookup.end())
493 {
494 return false;
495 }
496
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800497 /* Prevent writing during verification. */
498 if (state == UpdateState::verificationStarted)
499 {
500 return false;
501 }
502
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700503 /* Prevent writing to the verification or update blobs. */
504 if (item->second->activePath == verifyBlobId ||
505 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700506 {
507 return false;
508 }
Patrick Venture699750d2019-05-15 09:24:09 -0700509
Patrick Venture18235e62018-11-08 10:21:09 -0800510 std::vector<std::uint8_t> bytes;
511
Patrick Venture05abf7e2018-11-09 11:02:56 -0800512 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800513 {
514 bytes = data;
515 }
516 else
517 {
518 /* little endian required per design, and so on, but TODO: do endianness
519 * with boost.
520 */
521 struct ExtChunkHdr header;
522
523 if (data.size() != sizeof(header))
524 {
525 return false;
526 }
527
528 std::memcpy(&header, data.data(), data.size());
529 bytes = item->second->dataHandler->copyFrom(header.length);
530 }
531
532 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700533}
Patrick Venture18235e62018-11-08 10:21:09 -0800534
Patrick Venture8c535332018-11-08 15:58:00 -0800535/*
536 * If the active session (image or hash) is over LPC, this allows
537 * configuring it. This option is only available before you start
538 * writing data for the given item (image or hash). This will return
539 * false at any other part. -- the lpc handler portion will know to return
540 * false.
541 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700542bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
543 const std::vector<uint8_t>& data)
544{
Patrick Venture8c535332018-11-08 15:58:00 -0800545 auto item = lookup.find(session);
546 if (item == lookup.end())
547 {
548 return false;
549 }
550
Patrick Venture05abf7e2018-11-09 11:02:56 -0800551 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800552 {
553 return false;
554 }
555
Patrick Ventured56097a2019-05-15 09:47:55 -0700556 /* Prevent writing meta to the verification blob (it has no data handler).
557 */
558 if (item->second->dataHandler)
559 {
560 return item->second->dataHandler->writeMeta(data);
561 }
Patrick Venture699750d2019-05-15 09:24:09 -0700562
Patrick Ventured56097a2019-05-15 09:47:55 -0700563 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700564}
Patrick Venture8c535332018-11-08 15:58:00 -0800565
Patrick Ventured6461d62018-11-09 17:30:25 -0800566/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700567 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800568 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800569 * the image.
570 *
571 * For this file to have opened, the other two must be closed, which means any
572 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800573 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700574bool FirmwareBlobHandler::commit(uint16_t session,
575 const std::vector<uint8_t>& data)
576{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800577 auto item = lookup.find(session);
578 if (item == lookup.end())
579 {
580 return false;
581 }
582
Patrick Venture1a406fe2019-05-31 07:29:56 -0700583 /* You can only commit on the verifyBlodId or updateBlobId */
584 if (item->second->activePath != verifyBlobId &&
585 item->second->activePath != updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800586 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700587 std::fprintf(stderr, "path: '%s' not expected for commit\n",
588 item->second->activePath.c_str());
Patrick Ventureffcc5502018-11-16 12:32:38 -0800589 return false;
590 }
591
Patrick Venture433cbc32019-05-30 09:53:10 -0700592 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800593 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700594 case UpdateState::verificationPending:
595 /* Set state to committing. */
596 item->second->flags |= blobs::StateFlags::committing;
597 return triggerVerification();
Patrick Venture433cbc32019-05-30 09:53:10 -0700598 case UpdateState::verificationStarted:
599 /* Calling repeatedly has no effect within an update process. */
600 return true;
601 case UpdateState::verificationCompleted:
602 /* Calling after the verification process has completed returns
603 * failure. */
604 return false;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700605 case UpdateState::updatePending:
Patrick Venture433cbc32019-05-30 09:53:10 -0700606 item->second->flags |= blobs::StateFlags::committing;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700607 return triggerUpdate();
Patrick Venture0079d892019-05-31 11:27:44 -0700608 case UpdateState::updateStarted:
609 /* Calling repeatedly has no effect within an update process. */
610 return true;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700611 default:
612 return false;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800613 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700614}
Patrick Ventured6461d62018-11-09 17:30:25 -0800615
616/*
617 * Close must be called on the firmware image before triggering
618 * verification via commit. Once the verification is complete, you can
619 * then close the hash file.
620 *
621 * If the `verify_image.service` returned success, closing the hash file
622 * will have a specific behavior depending on the update. If it's UBI,
623 * it'll perform the install. If it's static layout, it'll do nothing. The
624 * verify_image service in the static layout case is responsible for placing
625 * the file in the correct staging position.
626 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700627bool FirmwareBlobHandler::close(uint16_t session)
628{
Patrick Venture68bb1432018-11-09 20:08:48 -0800629 auto item = lookup.find(session);
630 if (item == lookup.end())
631 {
632 return false;
633 }
634
Patrick Venturee95dbb62019-06-05 09:59:29 -0700635 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800636 {
Patrick Venturee95dbb62019-06-05 09:59:29 -0700637 case UpdateState::uploadInProgress:
638 /* They are closing a data pathway (image, tarball, hash). */
639 state = UpdateState::verificationPending;
Patrick Venture85ae86b2019-06-05 09:18:40 -0700640
Patrick Venturee95dbb62019-06-05 09:59:29 -0700641 /* Add verify blob ID now that we can expect it. */
642 addBlobId(verifyBlobId);
643 break;
644 case UpdateState::verificationPending:
645 /* They haven't triggered, therefore closing is uninteresting.
646 */
647 break;
648 case UpdateState::verificationStarted:
Patrick Ventured5741022019-06-17 09:08:35 -0700649 /* Abort without checking to see if it happened to finish. Require
650 * the caller to stat() deliberately.
Patrick Venturee95dbb62019-06-05 09:59:29 -0700651 */
Patrick Ventured5741022019-06-17 09:08:35 -0700652 abortVerification();
653 abortProcess();
654 break;
Patrick Venturee95dbb62019-06-05 09:59:29 -0700655 case UpdateState::verificationCompleted:
656 if (lastVerificationStatus == ActionStatus::success)
657 {
658 state = UpdateState::updatePending;
659 addBlobId(updateBlobId);
660 removeBlobId(verifyBlobId);
661 }
662 else
663 {
664 /* TODO: Verification failed, what now? */
665 }
666 break;
667 case UpdateState::updatePending:
668 /* They haven't triggered the update, therefore this is
669 * uninteresting. */
670 break;
671 case UpdateState::updateStarted:
Patrick Venture49731712019-06-17 10:04:02 -0700672 /* Abort without checking to see if it happened to finish. Require
673 * the caller to stat() deliberately.
674 */
675 abortUpdate();
676 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700677 break;
678 case UpdateState::updateCompleted:
679 if (lastUpdateStatus == ActionStatus::failed)
680 {
681 /* TODO: lOG something? */
682 }
Patrick Venture930c7b72019-05-24 11:11:08 -0700683
Patrick Venturee95dbb62019-06-05 09:59:29 -0700684 state = UpdateState::notYetStarted;
685 removeBlobId(updateBlobId);
686 removeBlobId(activeImageBlobId);
687 removeBlobId(activeHashBlobId);
688 break;
689 default:
690 break;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800691 }
692
Patrick Venture68bb1432018-11-09 20:08:48 -0800693 if (item->second->dataHandler)
694 {
695 item->second->dataHandler->close();
696 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800697 if (item->second->imageHandler)
698 {
699 item->second->imageHandler->close();
700 }
701
Patrick Venture68bb1432018-11-09 20:08:48 -0800702 lookup.erase(item);
Patrick Venture991910a2018-11-09 19:43:48 -0800703 fileOpen = false;
Patrick Venture68bb1432018-11-09 20:08:48 -0800704 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700705}
Patrick Ventured6461d62018-11-09 17:30:25 -0800706
Patrick Venturec7ca2912018-11-02 11:38:33 -0700707bool FirmwareBlobHandler::expire(uint16_t session)
708{
709 return false;
710}
Patrick Ventured6461d62018-11-09 17:30:25 -0800711
712/*
713 * Currently, the design does not provide this with a function, however,
714 * it will likely change to support reading data back.
715 */
716std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
717 uint32_t offset,
718 uint32_t requestedSize)
719{
720 return {};
721}
722
Patrick Ventured5741022019-06-17 09:08:35 -0700723void FirmwareBlobHandler::abortProcess()
724{
725 /* Closing of open files is handled from close() -- Reaching here from
726 * delete may never be supported.
727 */
728 removeBlobId(verifyBlobId);
729 removeBlobId(updateBlobId);
730 removeBlobId(activeImageBlobId);
731 removeBlobId(activeHashBlobId);
732
733 state = UpdateState::notYetStarted;
734}
735
736void FirmwareBlobHandler::abortVerification()
737{
738 verification->abort();
739}
740
Patrick Ventureffcc5502018-11-16 12:32:38 -0800741bool FirmwareBlobHandler::triggerVerification()
742{
Patrick Venture1d66fe62019-06-03 14:57:27 -0700743 bool result = verification->trigger();
Patrick Venture3ecb3502019-05-17 11:03:51 -0700744 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800745 {
Patrick Venture453f06a2019-01-17 10:02:12 -0800746 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800747 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800748
Patrick Venture3ecb3502019-05-17 11:03:51 -0700749 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800750}
751
Patrick Venture49731712019-06-17 10:04:02 -0700752void FirmwareBlobHandler::abortUpdate()
753{
754 update->abort();
755}
756
Patrick Venture1a406fe2019-05-31 07:29:56 -0700757bool FirmwareBlobHandler::triggerUpdate()
758{
Patrick Venture1d66fe62019-06-03 14:57:27 -0700759 bool result = update->trigger();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700760 if (result)
761 {
762 state = UpdateState::updateStarted;
763 }
764
765 return result;
766}
767
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700768} // namespace ipmi_flash