blob: e8795298557332b50cf5db4bd278983f0df37dd0 [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 Venture9efef5d2019-06-19 08:45:44 -070024#include <blobs-ipmid/blobs.hpp>
Patrick Venture192d60f2018-11-06 11:11:59 -080025#include <cstdint>
Patrick Venture18235e62018-11-08 10:21:09 -080026#include <cstring>
Patrick Ventureb3b4db72019-05-15 11:30:24 -070027#include <fstream>
Patrick Venture68cf64f2018-11-06 10:46:51 -080028#include <memory>
Patrick Ventured333a872018-12-03 16:24:26 -080029#include <phosphor-logging/log.hpp>
Patrick Venturefa6c4d92018-11-02 18:34:53 -070030#include <string>
31#include <vector>
32
Patrick Ventured333a872018-12-03 16:24:26 -080033using namespace phosphor::logging;
34
Patrick Venture1d5a31c2019-05-20 11:38:22 -070035namespace ipmi_flash
Patrick Venturec7ca2912018-11-02 11:38:33 -070036{
Patrick Ventureb3b4db72019-05-15 11:30:24 -070037
Patrick Venture1d5a31c2019-05-20 11:38:22 -070038std::unique_ptr<blobs::GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080039 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070040 const std::vector<HandlerPack>& firmwares,
Patrick Venture74059d62019-05-17 10:40:26 -070041 const std::vector<DataHandlerPack>& transports,
Patrick Venture1d66fe62019-06-03 14:57:27 -070042 std::unique_ptr<TriggerableActionInterface> verification,
43 std::unique_ptr<TriggerableActionInterface> update)
Patrick Venture68cf64f2018-11-06 10:46:51 -080044{
Patrick Venture52854622018-11-06 12:30:00 -080045 /* There must be at least one. */
46 if (!firmwares.size())
47 {
Patrick Ventured333a872018-12-03 16:24:26 -080048 log<level::ERR>("Must provide at least one firmware handler.");
Patrick Venture52854622018-11-06 12:30:00 -080049 return nullptr;
50 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080051 if (!transports.size())
52 {
53 return nullptr;
54 }
Patrick Venture52854622018-11-06 12:30:00 -080055
Patrick Venturea78e39f2018-11-06 18:37:06 -080056 std::vector<std::string> blobs;
57 for (const auto& item : firmwares)
58 {
59 blobs.push_back(item.blobName);
60 }
Patrick Venture18235e62018-11-08 10:21:09 -080061
Patrick Venture7dad86f2019-05-17 08:52:20 -070062 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId))
Patrick Venture18235e62018-11-08 10:21:09 -080063 {
64 return nullptr;
65 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080066
Patrick Venture1cde5f92018-11-07 08:26:47 -080067 std::uint16_t bitmask = 0;
68 for (const auto& item : transports)
69 {
70 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
71 bitmask |= item.bitmask;
72 }
73
Patrick Venture3ecb3502019-05-17 11:03:51 -070074 return std::make_unique<FirmwareBlobHandler>(
Patrick Venture27ac5822019-05-20 17:39:31 -070075 firmwares, blobs, transports, bitmask, std::move(verification),
76 std::move(update));
Patrick Venture68cf64f2018-11-06 10:46:51 -080077}
78
Patrick Ventured6461d62018-11-09 17:30:25 -080079/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070080bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
81{
Patrick Venture6032dc02019-05-17 11:01:44 -070082 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070083}
Patrick Venture53977962018-11-02 18:59:35 -070084
Patrick Ventured6461d62018-11-09 17:30:25 -080085/*
86 * Grab the list of supported firmware.
87 *
88 * If there's an open firmware session, it'll already be present in the
89 * list as "/flash/active/image", and if the hash has started,
90 * "/flash/active/hash" regardless of mechanism. This is done in the open
91 * comamnd, no extra work is required here.
92 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070093std::vector<std::string> FirmwareBlobHandler::getBlobIds()
94{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080095 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070096}
Patrick Venture53977962018-11-02 18:59:35 -070097
Patrick Ventured6461d62018-11-09 17:30:25 -080098/*
99 * Per the design, this mean abort, and this will trigger whatever
100 * appropriate actions are required to abort the process.
101 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700102bool FirmwareBlobHandler::deleteBlob(const std::string& path)
103{
Patrick Venturebcc0c772019-06-17 10:42:06 -0700104 /* This cannot be called if you have an open session to the path.
105 * You can have an open session to verify/update/hash/image, but not active*
106 *
107 * Therefore, if this is called, it's either on a blob that isn't presently
108 * open. However, there could be open blobs, so we need to close all open
109 * sessions. This closing on our is an invalid handler behavior. Therefore,
110 * we cannot close an active session. To enforce this, we only allow
111 * deleting if there isn't a file open.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800112 */
Patrick Venturebcc0c772019-06-17 10:42:06 -0700113 if (fileOpen)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800114 {
115 return false;
116 }
117
Patrick Venture72676762019-06-17 11:22:38 -0700118 /* only includes states where fileOpen == false */
119 switch (state)
120 {
121 case UpdateState::notYetStarted:
122 /* Trying to delete anything at this point has no effect and returns
123 * false.
124 */
125 return false;
126 case UpdateState::verificationPending:
Patrick Venture2ca66522019-06-17 11:58:38 -0700127 abortProcess();
128 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700129 case UpdateState::updatePending:
Patrick Venturec9f62392019-06-17 12:17:26 -0700130 abortProcess();
131 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700132 default:
133 break;
134 }
135
Patrick Venturebcc0c772019-06-17 10:42:06 -0700136 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700137}
Patrick Venture53977962018-11-02 18:59:35 -0700138
Patrick Ventured6461d62018-11-09 17:30:25 -0800139/*
140 * Stat on the files will return information such as what supported
141 * transport mechanisms are available.
142 *
143 * Stat on an active file or hash will return information such as the size
144 * of the data cached, and any additional pertinent information. The
145 * blob_state on the active files will return the state of the update.
146 */
Patrick Venturee312f392019-06-04 07:44:37 -0700147bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700148{
Patrick Venture46637c82018-11-06 15:20:24 -0800149 /* We know we support this path because canHandle is called ahead */
Patrick Ventured342a952019-05-29 09:09:10 -0700150 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700151 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800152 {
Patrick Ventured342a952019-05-29 09:09:10 -0700153 /* These blobs are placeholders that indicate things, or allow actions,
154 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800155 */
Patrick Ventured342a952019-05-29 09:09:10 -0700156 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800157 }
158
Patrick Ventured342a952019-05-29 09:09:10 -0700159 /* They are requesting information about the generic blob_id. */
160 meta->blobState = bitmask;
161 meta->size = 0;
162
163 /* The generic blob_ids state is only the bits related to the transport
164 * mechanisms.
165 */
166 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700167}
Patrick Venture53977962018-11-02 18:59:35 -0700168
Patrick Ventureda66fd82019-06-03 11:11:24 -0700169ActionStatus FirmwareBlobHandler::getActionStatus()
Patrick Venturea2d82392019-06-03 10:55:17 -0700170{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700171 ActionStatus value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700172
173 switch (state)
174 {
175 case UpdateState::verificationPending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700176 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700177 break;
178 case UpdateState::verificationStarted:
179 value = verification->status();
Patrick Ventureda66fd82019-06-03 11:11:24 -0700180 lastVerificationStatus = value;
Patrick Venturea2d82392019-06-03 10:55:17 -0700181 break;
182 case UpdateState::verificationCompleted:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700183 value = lastVerificationStatus;
Patrick Venturea2d82392019-06-03 10:55:17 -0700184 break;
Patrick Venturea2d82392019-06-03 10:55:17 -0700185 case UpdateState::updatePending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700186 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700187 break;
188 case UpdateState::updateStarted:
189 value = update->status();
190 lastUpdateStatus = value;
191 break;
192 case UpdateState::updateCompleted:
193 value = lastUpdateStatus;
194 break;
195 default:
196 break;
197 }
198
199 return value;
200}
201
Patrick Venturec02849b2018-11-06 17:31:15 -0800202/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800203 * Return stat information on an open session. It therefore must be an active
204 * handle to either the active image or active hash.
Patrick Ventured6461d62018-11-09 17:30:25 -0800205 */
Patrick Venturee312f392019-06-04 07:44:37 -0700206bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800207{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800208 auto item = lookup.find(session);
209 if (item == lookup.end())
210 {
211 return false;
212 }
213
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700214 /* The size here refers to the size of the file -- of something analagous.
215 */
216 meta->size = (item->second->imageHandler)
217 ? item->second->imageHandler->getSize()
218 : 0;
219
220 meta->metadata.clear();
221
Patrick Ventureda66fd82019-06-03 11:11:24 -0700222 if (item->second->activePath == verifyBlobId ||
223 item->second->activePath == updateBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700224 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700225 ActionStatus value = getActionStatus();
Patrick Venture699750d2019-05-15 09:24:09 -0700226
Patrick Venturee955e072019-05-15 16:16:46 -0700227 meta->metadata.push_back(static_cast<std::uint8_t>(value));
228
229 /* Change the firmware handler's state and the blob's stat value
230 * depending.
231 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700232 if (value == ActionStatus::success || value == ActionStatus::failed)
Patrick Venturee955e072019-05-15 16:16:46 -0700233 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700234 if (item->second->activePath == verifyBlobId)
Patrick Venturee955e072019-05-15 16:16:46 -0700235 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700236 state = UpdateState::verificationCompleted;
Patrick Venturee955e072019-05-15 16:16:46 -0700237 }
238 else
239 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700240 /* item->second->activePath == updateBlobId */
241 state = UpdateState::updateCompleted;
Patrick Venturee955e072019-05-15 16:16:46 -0700242 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700243
Patrick Venturef1f0f652019-06-03 09:10:19 -0700244 item->second->flags &= ~blobs::StateFlags::committing;
245
Patrick Ventureda66fd82019-06-03 11:11:24 -0700246 if (value == ActionStatus::success)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700247 {
248 item->second->flags |= blobs::StateFlags::committed;
249 }
250 else
251 {
252 item->second->flags |= blobs::StateFlags::commit_error;
253 }
254 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700255 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800256
Patrick Venturee955e072019-05-15 16:16:46 -0700257 /* The blobState here relates to an active sesion, so we should return the
258 * flags used to open this session.
259 */
260 meta->blobState = item->second->flags;
261
Patrick Venturecc7d1602018-11-15 13:58:33 -0800262 /* The metadata blob returned comes from the data handler... it's used for
263 * instance, in P2A bridging to get required information about the mapping,
264 * and is the "opposite" of the lpc writemeta requirement.
265 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800266 if (item->second->dataHandler)
267 {
Patrick Venture74304642019-01-17 09:31:04 -0800268 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800269 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
270 bytes.end());
271 }
272
Patrick Venturecc7d1602018-11-15 13:58:33 -0800273 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800274}
275
276/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800277 * If you open /flash/image or /flash/tarball, or /flash/hash it will
278 * interpret the open flags and perform whatever actions are required for
279 * that update process. The session returned can be used immediately for
280 * sending data down, without requiring one to open the new active file.
281 *
282 * If you open the active flash image or active hash it will let you
283 * overwrite pieces, depending on the state.
284 *
285 * Once the verification process has started the active files cannot be
286 * opened.
287 *
288 * You can only have one open session at a time. Which means, you can only
289 * have one file open at a time. Trying to open the hash blob_id while you
290 * still have the flash image blob_id open will fail. Opening the flash
291 * blob_id when it is already open will fail.
292 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700293bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
294 const std::string& path)
295{
Patrick Venturec02849b2018-11-06 17:31:15 -0800296 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800297 *
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800298 * Further on this, if there's an active session to the hash we don't allow
299 * re-opening the image, and if we have the image open, we don't allow
300 * opening the hash. This design decision may be re-evaluated, and changed
301 * to only allow one session per object type (of the two types). But,
302 * consider if the hash is open, do we want to allow writing to the image?
303 * And why would we? But, really, the point of no-return is once the
304 * verification process has begun -- which is done via commit() on the hash
305 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700306 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800307 if (fileOpen)
308 {
309 return false;
310 }
311
Patrick Venture723113f2019-06-05 09:38:35 -0700312 /* The active blobs are only meant to indicate status that something has
313 * opened the image file or the hash file.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800314 */
Patrick Venture19f5d882019-05-30 14:34:55 -0700315 if (path == activeImageBlobId || path == activeHashBlobId)
316 {
317 /* 2a) are they opening the active image? this can only happen if they
318 * already started one (due to canHandleBlob's behavior).
319 */
320 /* 2b) are they opening the active hash? this can only happen if they
321 * already started one (due to canHandleBlob's behavior).
322 */
323 return false;
324 }
325
Patrick Venture723113f2019-06-05 09:38:35 -0700326 /* Check that they've opened for writing - read back not currently
327 * supported.
328 */
329 if ((flags & blobs::OpenFlags::write) == 0)
330 {
331 return false;
332 }
333
334 /* Because canHandleBlob is called before open, we know that if they try to
335 * open the verifyBlobId, they're in a state where it's present.
336 */
337
338 switch (state)
339 {
340 case UpdateState::uploadInProgress:
341 /* Unreachable code because if it's started a file is open. */
342 break;
343 case UpdateState::verificationPending:
344 /* Handle opening the verifyBlobId --> we know the image and hash
345 * aren't open because of the fileOpen check. They can still open
346 * other files from this state to transition back into
347 * uploadInProgress.
348 *
349 * The file must be opened for writing, but no transport mechanism
350 * specified since it's irrelevant.
351 */
352 if (path == verifyBlobId)
353 {
354 verifyImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700355
356 lookup[session] = &verifyImage;
357
358 fileOpen = true;
359 return true;
360 }
361 break;
362 case UpdateState::verificationStarted:
363 case UpdateState::verificationCompleted:
364 /* Unreachable code because if it's started a file is open. */
365 return false;
366 case UpdateState::updatePending:
367 {
368 /* When in this state, they can only open the updateBlobId */
369 if (path == updateBlobId)
370 {
371 updateImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700372
373 lookup[session] = &updateImage;
374
375 fileOpen = true;
376 return true;
377 }
378 else
379 {
380 return false;
381 }
382 }
383 case UpdateState::updateStarted:
384 case UpdateState::updateCompleted:
385 /* Unreachable code because if it's started a file is open. */
386 break;
387 default:
388 break;
389 }
390
391 /* There are two abstractions at play, how you get the data and how you
392 * handle that data. such that, whether the data comes from the PCI bridge
393 * or LPC bridge is not connected to whether the data goes into a static
394 * layout flash update or a UBI tarball.
395 */
396
Patrick Venturec02849b2018-11-06 17:31:15 -0800397 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800398 * support what they request.
399 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800400 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800401 {
402 return false;
403 }
404
Patrick Venture18235e62018-11-08 10:21:09 -0800405 /* How are they expecting to copy this data? */
406 auto d = std::find_if(
407 transports.begin(), transports.end(),
408 [&flags](const auto& iter) { return (iter.bitmask & flags); });
409 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800410 {
Patrick Venture18235e62018-11-08 10:21:09 -0800411 return false;
412 }
413
414 /* We found the transport handler they requested, no surprise since
415 * above we verify they selected at least one we wanted.
416 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800417
Patrick Venture6e307a72018-11-09 18:21:17 -0800418 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
419 * only non-external data pathway -- but this is just a more generic
420 * approach to that.
421 */
422 if (d->handler)
423 {
424 /* If the data handler open call fails, open fails. */
425 if (!d->handler->open())
426 {
427 return false;
428 }
429 }
430
Patrick Venture70e30bf2019-01-17 10:29:28 -0800431 /* Do we have a file handler for the type of file they're opening.
432 * Note: This should only fail if something is somehow crazy wrong.
433 * Since the canHandle() said yes, and that's tied into the list of explicit
434 * firmware handers (and file handlers, like this'll know where to write the
435 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800436 */
Patrick Venture18235e62018-11-08 10:21:09 -0800437 auto h = std::find_if(
438 handlers.begin(), handlers.end(),
439 [&path](const auto& iter) { return (iter.blobName == path); });
440 if (h == handlers.end())
441 {
442 return false;
443 }
444
445 /* Ok, so we found a handler that matched, so call open() */
446 if (!h->handler->open(path))
447 {
448 return false;
449 }
450
Patrick Venture70e30bf2019-01-17 10:29:28 -0800451 Session* curr;
452 const std::string* active;
453
Patrick Venture7dad86f2019-05-17 08:52:20 -0700454 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800455 {
456 /* 2c) are they opening the /flash/hash ? (to start the process) */
457 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700458 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800459 }
460 else
461 {
462 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700463 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800464 }
465
Patrick Venture18235e62018-11-08 10:21:09 -0800466 curr->flags = flags;
467 curr->dataHandler = d->handler;
468 curr->imageHandler = h->handler;
469
470 lookup[session] = curr;
471
Patrick Ventureefba42d2019-05-24 10:48:16 -0700472 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700473 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800474
Patrick Venture12233c52019-05-16 09:26:59 -0700475 state = UpdateState::uploadInProgress;
Patrick Venture991910a2018-11-09 19:43:48 -0800476 fileOpen = true;
477
Patrick Venture18235e62018-11-08 10:21:09 -0800478 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700479}
Patrick Venture53977962018-11-02 18:59:35 -0700480
Patrick Venture18235e62018-11-08 10:21:09 -0800481/**
482 * The write command really just grabs the data from wherever it is and sends it
483 * to the image handler. It's the image handler's responsibility to deal with
484 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800485 *
486 * This receives a session from the blob manager, therefore it is always called
487 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800488 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700489bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
490 const std::vector<uint8_t>& data)
491{
Patrick Venture18235e62018-11-08 10:21:09 -0800492 auto item = lookup.find(session);
493 if (item == lookup.end())
494 {
495 return false;
496 }
497
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800498 /* Prevent writing during verification. */
499 if (state == UpdateState::verificationStarted)
500 {
501 return false;
502 }
503
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700504 /* Prevent writing to the verification or update blobs. */
505 if (item->second->activePath == verifyBlobId ||
506 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700507 {
508 return false;
509 }
Patrick Venture699750d2019-05-15 09:24:09 -0700510
Patrick Venture18235e62018-11-08 10:21:09 -0800511 std::vector<std::uint8_t> bytes;
512
Patrick Venture05abf7e2018-11-09 11:02:56 -0800513 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800514 {
515 bytes = data;
516 }
517 else
518 {
519 /* little endian required per design, and so on, but TODO: do endianness
520 * with boost.
521 */
522 struct ExtChunkHdr header;
523
524 if (data.size() != sizeof(header))
525 {
526 return false;
527 }
528
529 std::memcpy(&header, data.data(), data.size());
530 bytes = item->second->dataHandler->copyFrom(header.length);
531 }
532
533 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700534}
Patrick Venture18235e62018-11-08 10:21:09 -0800535
Patrick Venture8c535332018-11-08 15:58:00 -0800536/*
537 * If the active session (image or hash) is over LPC, this allows
538 * configuring it. This option is only available before you start
539 * writing data for the given item (image or hash). This will return
540 * false at any other part. -- the lpc handler portion will know to return
541 * false.
542 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700543bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
544 const std::vector<uint8_t>& data)
545{
Patrick Venture8c535332018-11-08 15:58:00 -0800546 auto item = lookup.find(session);
547 if (item == lookup.end())
548 {
549 return false;
550 }
551
Patrick Venture05abf7e2018-11-09 11:02:56 -0800552 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800553 {
554 return false;
555 }
556
Patrick Ventured56097a2019-05-15 09:47:55 -0700557 /* Prevent writing meta to the verification blob (it has no data handler).
558 */
559 if (item->second->dataHandler)
560 {
561 return item->second->dataHandler->writeMeta(data);
562 }
Patrick Venture699750d2019-05-15 09:24:09 -0700563
Patrick Ventured56097a2019-05-15 09:47:55 -0700564 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700565}
Patrick Venture8c535332018-11-08 15:58:00 -0800566
Patrick Ventured6461d62018-11-09 17:30:25 -0800567/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700568 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800569 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800570 * the image.
571 *
572 * For this file to have opened, the other two must be closed, which means any
573 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800574 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700575bool FirmwareBlobHandler::commit(uint16_t session,
576 const std::vector<uint8_t>& data)
577{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800578 auto item = lookup.find(session);
579 if (item == lookup.end())
580 {
581 return false;
582 }
583
Patrick Venture1a406fe2019-05-31 07:29:56 -0700584 /* You can only commit on the verifyBlodId or updateBlobId */
585 if (item->second->activePath != verifyBlobId &&
586 item->second->activePath != updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800587 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700588 std::fprintf(stderr, "path: '%s' not expected for commit\n",
589 item->second->activePath.c_str());
Patrick Ventureffcc5502018-11-16 12:32:38 -0800590 return false;
591 }
592
Patrick Venture433cbc32019-05-30 09:53:10 -0700593 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800594 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700595 case UpdateState::verificationPending:
596 /* Set state to committing. */
597 item->second->flags |= blobs::StateFlags::committing;
598 return triggerVerification();
Patrick Venture433cbc32019-05-30 09:53:10 -0700599 case UpdateState::verificationStarted:
600 /* Calling repeatedly has no effect within an update process. */
601 return true;
602 case UpdateState::verificationCompleted:
603 /* Calling after the verification process has completed returns
604 * failure. */
605 return false;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700606 case UpdateState::updatePending:
Patrick Venture433cbc32019-05-30 09:53:10 -0700607 item->second->flags |= blobs::StateFlags::committing;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700608 return triggerUpdate();
Patrick Venture0079d892019-05-31 11:27:44 -0700609 case UpdateState::updateStarted:
610 /* Calling repeatedly has no effect within an update process. */
611 return true;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700612 default:
613 return false;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800614 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700615}
Patrick Ventured6461d62018-11-09 17:30:25 -0800616
617/*
618 * Close must be called on the firmware image before triggering
619 * verification via commit. Once the verification is complete, you can
620 * then close the hash file.
621 *
622 * If the `verify_image.service` returned success, closing the hash file
623 * will have a specific behavior depending on the update. If it's UBI,
624 * it'll perform the install. If it's static layout, it'll do nothing. The
625 * verify_image service in the static layout case is responsible for placing
626 * the file in the correct staging position.
627 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700628bool FirmwareBlobHandler::close(uint16_t session)
629{
Patrick Venture68bb1432018-11-09 20:08:48 -0800630 auto item = lookup.find(session);
631 if (item == lookup.end())
632 {
633 return false;
634 }
635
Patrick Venturee95dbb62019-06-05 09:59:29 -0700636 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800637 {
Patrick Venturee95dbb62019-06-05 09:59:29 -0700638 case UpdateState::uploadInProgress:
639 /* They are closing a data pathway (image, tarball, hash). */
640 state = UpdateState::verificationPending;
Patrick Venture85ae86b2019-06-05 09:18:40 -0700641
Patrick Venturee95dbb62019-06-05 09:59:29 -0700642 /* Add verify blob ID now that we can expect it. */
643 addBlobId(verifyBlobId);
644 break;
645 case UpdateState::verificationPending:
646 /* They haven't triggered, therefore closing is uninteresting.
647 */
648 break;
649 case UpdateState::verificationStarted:
Patrick Ventured5741022019-06-17 09:08:35 -0700650 /* Abort without checking to see if it happened to finish. Require
651 * the caller to stat() deliberately.
Patrick Venturee95dbb62019-06-05 09:59:29 -0700652 */
Patrick Ventured5741022019-06-17 09:08:35 -0700653 abortVerification();
654 abortProcess();
655 break;
Patrick Venturee95dbb62019-06-05 09:59:29 -0700656 case UpdateState::verificationCompleted:
657 if (lastVerificationStatus == ActionStatus::success)
658 {
659 state = UpdateState::updatePending;
660 addBlobId(updateBlobId);
661 removeBlobId(verifyBlobId);
662 }
663 else
664 {
Patrick Venture5d9fa022019-06-17 13:13:30 -0700665 /* Verification failed, and the host-tool knows this by calling
666 * stat(), which triggered the state change to
667 * verificationCompleted.
668 *
669 * Therefore, let's abort the process at this point.
670 */
671 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700672 }
673 break;
674 case UpdateState::updatePending:
675 /* They haven't triggered the update, therefore this is
676 * uninteresting. */
677 break;
678 case UpdateState::updateStarted:
Patrick Venture49731712019-06-17 10:04:02 -0700679 /* Abort without checking to see if it happened to finish. Require
680 * the caller to stat() deliberately.
681 */
682 abortUpdate();
683 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700684 break;
685 case UpdateState::updateCompleted:
686 if (lastUpdateStatus == ActionStatus::failed)
687 {
688 /* TODO: lOG something? */
Patrick Venture4c7a4202019-06-17 13:06:55 -0700689 std::fprintf(stderr, "Update failed\n");
Patrick Venturee95dbb62019-06-05 09:59:29 -0700690 }
Patrick Venture930c7b72019-05-24 11:11:08 -0700691
Patrick Venture4c7a4202019-06-17 13:06:55 -0700692 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700693 break;
694 default:
695 break;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800696 }
697
Patrick Venture68bb1432018-11-09 20:08:48 -0800698 if (item->second->dataHandler)
699 {
700 item->second->dataHandler->close();
701 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800702 if (item->second->imageHandler)
703 {
704 item->second->imageHandler->close();
705 }
706
Patrick Venture68bb1432018-11-09 20:08:48 -0800707 lookup.erase(item);
Patrick Venture991910a2018-11-09 19:43:48 -0800708 fileOpen = false;
Patrick Venture68bb1432018-11-09 20:08:48 -0800709 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700710}
Patrick Ventured6461d62018-11-09 17:30:25 -0800711
Patrick Venturec7ca2912018-11-02 11:38:33 -0700712bool FirmwareBlobHandler::expire(uint16_t session)
713{
714 return false;
715}
Patrick Ventured6461d62018-11-09 17:30:25 -0800716
717/*
718 * Currently, the design does not provide this with a function, however,
719 * it will likely change to support reading data back.
720 */
721std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
722 uint32_t offset,
723 uint32_t requestedSize)
724{
725 return {};
726}
727
Patrick Ventured5741022019-06-17 09:08:35 -0700728void FirmwareBlobHandler::abortProcess()
729{
730 /* Closing of open files is handled from close() -- Reaching here from
731 * delete may never be supported.
732 */
733 removeBlobId(verifyBlobId);
734 removeBlobId(updateBlobId);
735 removeBlobId(activeImageBlobId);
736 removeBlobId(activeHashBlobId);
737
738 state = UpdateState::notYetStarted;
739}
740
741void FirmwareBlobHandler::abortVerification()
742{
743 verification->abort();
744}
745
Patrick Ventureffcc5502018-11-16 12:32:38 -0800746bool FirmwareBlobHandler::triggerVerification()
747{
Patrick Venture1d66fe62019-06-03 14:57:27 -0700748 bool result = verification->trigger();
Patrick Venture3ecb3502019-05-17 11:03:51 -0700749 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800750 {
Patrick Venture453f06a2019-01-17 10:02:12 -0800751 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800752 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800753
Patrick Venture3ecb3502019-05-17 11:03:51 -0700754 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800755}
756
Patrick Venture49731712019-06-17 10:04:02 -0700757void FirmwareBlobHandler::abortUpdate()
758{
759 update->abort();
760}
761
Patrick Venture1a406fe2019-05-31 07:29:56 -0700762bool FirmwareBlobHandler::triggerUpdate()
763{
Patrick Venture1d66fe62019-06-03 14:57:27 -0700764 bool result = update->trigger();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700765 if (result)
766 {
767 state = UpdateState::updateStarted;
768 }
769
770 return result;
771}
772
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700773} // namespace ipmi_flash