blob: 865c045e6af2027c1617a270ad047b9c2e663074 [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.
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 Venturee312f392019-06-04 07:44:37 -0700155bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700156{
Patrick Venture46637c82018-11-06 15:20:24 -0800157 /* We know we support this path because canHandle is called ahead */
Patrick Ventured342a952019-05-29 09:09:10 -0700158 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700159 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800160 {
Patrick Ventured342a952019-05-29 09:09:10 -0700161 /* These blobs are placeholders that indicate things, or allow actions,
162 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800163 */
Patrick Ventured342a952019-05-29 09:09:10 -0700164 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800165 }
166
Patrick Ventured342a952019-05-29 09:09:10 -0700167 /* They are requesting information about the generic blob_id. */
168 meta->blobState = bitmask;
169 meta->size = 0;
170
171 /* The generic blob_ids state is only the bits related to the transport
172 * mechanisms.
173 */
174 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700175}
Patrick Venture53977962018-11-02 18:59:35 -0700176
Patrick Ventureda66fd82019-06-03 11:11:24 -0700177ActionStatus FirmwareBlobHandler::getActionStatus()
Patrick Venturea2d82392019-06-03 10:55:17 -0700178{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700179 ActionStatus value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700180
181 switch (state)
182 {
183 case UpdateState::verificationPending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700184 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700185 break;
186 case UpdateState::verificationStarted:
187 value = verification->status();
Patrick Ventureda66fd82019-06-03 11:11:24 -0700188 lastVerificationStatus = value;
Patrick Venturea2d82392019-06-03 10:55:17 -0700189 break;
190 case UpdateState::verificationCompleted:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700191 value = lastVerificationStatus;
Patrick Venturea2d82392019-06-03 10:55:17 -0700192 break;
Patrick Venturea2d82392019-06-03 10:55:17 -0700193 case UpdateState::updatePending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700194 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700195 break;
196 case UpdateState::updateStarted:
197 value = update->status();
198 lastUpdateStatus = value;
199 break;
200 case UpdateState::updateCompleted:
201 value = lastUpdateStatus;
202 break;
203 default:
204 break;
205 }
206
207 return value;
208}
209
Patrick Venturec02849b2018-11-06 17:31:15 -0800210/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800211 * Return stat information on an open session. It therefore must be an active
212 * handle to either the active image or active hash.
Patrick Ventured6461d62018-11-09 17:30:25 -0800213 */
Patrick Venturee312f392019-06-04 07:44:37 -0700214bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800215{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800216 auto item = lookup.find(session);
217 if (item == lookup.end())
218 {
219 return false;
220 }
221
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700222 /* The size here refers to the size of the file -- of something analagous.
223 */
224 meta->size = (item->second->imageHandler)
225 ? item->second->imageHandler->getSize()
226 : 0;
227
228 meta->metadata.clear();
229
Patrick Ventureda66fd82019-06-03 11:11:24 -0700230 if (item->second->activePath == verifyBlobId ||
231 item->second->activePath == updateBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700232 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700233 ActionStatus value = getActionStatus();
Patrick Venture699750d2019-05-15 09:24:09 -0700234
Patrick Venturee955e072019-05-15 16:16:46 -0700235 meta->metadata.push_back(static_cast<std::uint8_t>(value));
236
237 /* Change the firmware handler's state and the blob's stat value
238 * depending.
239 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700240 if (value == ActionStatus::success || value == ActionStatus::failed)
Patrick Venturee955e072019-05-15 16:16:46 -0700241 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700242 if (item->second->activePath == verifyBlobId)
Patrick Venturee955e072019-05-15 16:16:46 -0700243 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700244 state = UpdateState::verificationCompleted;
Patrick Venturee955e072019-05-15 16:16:46 -0700245 }
246 else
247 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700248 /* item->second->activePath == updateBlobId */
249 state = UpdateState::updateCompleted;
Patrick Venturee955e072019-05-15 16:16:46 -0700250 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700251
Patrick Venturef1f0f652019-06-03 09:10:19 -0700252 item->second->flags &= ~blobs::StateFlags::committing;
253
Patrick Ventureda66fd82019-06-03 11:11:24 -0700254 if (value == ActionStatus::success)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700255 {
256 item->second->flags |= blobs::StateFlags::committed;
257 }
258 else
259 {
260 item->second->flags |= blobs::StateFlags::commit_error;
261 }
262 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700263 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800264
Patrick Venturee955e072019-05-15 16:16:46 -0700265 /* The blobState here relates to an active sesion, so we should return the
266 * flags used to open this session.
267 */
268 meta->blobState = item->second->flags;
269
Patrick Venturecc7d1602018-11-15 13:58:33 -0800270 /* The metadata blob returned comes from the data handler... it's used for
271 * instance, in P2A bridging to get required information about the mapping,
272 * and is the "opposite" of the lpc writemeta requirement.
273 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800274 if (item->second->dataHandler)
275 {
Patrick Venture74304642019-01-17 09:31:04 -0800276 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800277 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
278 bytes.end());
279 }
280
Patrick Venturecc7d1602018-11-15 13:58:33 -0800281 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800282}
283
284/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800285 * If you open /flash/image or /flash/tarball, or /flash/hash it will
286 * interpret the open flags and perform whatever actions are required for
287 * that update process. The session returned can be used immediately for
288 * sending data down, without requiring one to open the new active file.
289 *
290 * If you open the active flash image or active hash it will let you
291 * overwrite pieces, depending on the state.
292 *
293 * Once the verification process has started the active files cannot be
294 * opened.
295 *
296 * You can only have one open session at a time. Which means, you can only
297 * have one file open at a time. Trying to open the hash blob_id while you
298 * still have the flash image blob_id open will fail. Opening the flash
299 * blob_id when it is already open will fail.
300 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700301bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
302 const std::string& path)
303{
Patrick Venturec02849b2018-11-06 17:31:15 -0800304 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800305 *
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800306 * Further on this, if there's an active session to the hash we don't allow
307 * re-opening the image, and if we have the image open, we don't allow
308 * opening the hash. This design decision may be re-evaluated, and changed
309 * to only allow one session per object type (of the two types). But,
310 * consider if the hash is open, do we want to allow writing to the image?
311 * And why would we? But, really, the point of no-return is once the
312 * verification process has begun -- which is done via commit() on the hash
313 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700314 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800315 if (fileOpen)
316 {
317 return false;
318 }
319
Patrick Venture723113f2019-06-05 09:38:35 -0700320 /* The active blobs are only meant to indicate status that something has
321 * opened the image file or the hash file.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800322 */
Patrick Venture19f5d882019-05-30 14:34:55 -0700323 if (path == activeImageBlobId || path == activeHashBlobId)
324 {
325 /* 2a) are they opening the active image? this can only happen if they
326 * already started one (due to canHandleBlob's behavior).
327 */
328 /* 2b) are they opening the active hash? this can only happen if they
329 * already started one (due to canHandleBlob's behavior).
330 */
331 return false;
332 }
333
Patrick Venture723113f2019-06-05 09:38:35 -0700334 /* Check that they've opened for writing - read back not currently
335 * supported.
336 */
337 if ((flags & blobs::OpenFlags::write) == 0)
338 {
339 return false;
340 }
341
342 /* Because canHandleBlob is called before open, we know that if they try to
343 * open the verifyBlobId, they're in a state where it's present.
344 */
345
346 switch (state)
347 {
348 case UpdateState::uploadInProgress:
349 /* Unreachable code because if it's started a file is open. */
350 break;
351 case UpdateState::verificationPending:
352 /* Handle opening the verifyBlobId --> we know the image and hash
353 * aren't open because of the fileOpen check. They can still open
354 * other files from this state to transition back into
355 * uploadInProgress.
356 *
357 * The file must be opened for writing, but no transport mechanism
358 * specified since it's irrelevant.
359 */
360 if (path == verifyBlobId)
361 {
362 verifyImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700363
364 lookup[session] = &verifyImage;
365
366 fileOpen = true;
367 return true;
368 }
369 break;
370 case UpdateState::verificationStarted:
371 case UpdateState::verificationCompleted:
372 /* Unreachable code because if it's started a file is open. */
373 return false;
374 case UpdateState::updatePending:
375 {
376 /* When in this state, they can only open the updateBlobId */
377 if (path == updateBlobId)
378 {
379 updateImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700380
381 lookup[session] = &updateImage;
382
383 fileOpen = true;
384 return true;
385 }
386 else
387 {
388 return false;
389 }
390 }
391 case UpdateState::updateStarted:
392 case UpdateState::updateCompleted:
393 /* Unreachable code because if it's started a file is open. */
394 break;
395 default:
396 break;
397 }
398
399 /* There are two abstractions at play, how you get the data and how you
400 * handle that data. such that, whether the data comes from the PCI bridge
401 * or LPC bridge is not connected to whether the data goes into a static
402 * layout flash update or a UBI tarball.
403 */
404
Patrick Venturec02849b2018-11-06 17:31:15 -0800405 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800406 * support what they request.
407 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800408 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800409 {
410 return false;
411 }
412
Patrick Venture18235e62018-11-08 10:21:09 -0800413 /* How are they expecting to copy this data? */
414 auto d = std::find_if(
415 transports.begin(), transports.end(),
416 [&flags](const auto& iter) { return (iter.bitmask & flags); });
417 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800418 {
Patrick Venture18235e62018-11-08 10:21:09 -0800419 return false;
420 }
421
422 /* We found the transport handler they requested, no surprise since
423 * above we verify they selected at least one we wanted.
424 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800425
Patrick Venture6e307a72018-11-09 18:21:17 -0800426 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
427 * only non-external data pathway -- but this is just a more generic
428 * approach to that.
429 */
430 if (d->handler)
431 {
432 /* If the data handler open call fails, open fails. */
433 if (!d->handler->open())
434 {
435 return false;
436 }
437 }
438
Patrick Venture70e30bf2019-01-17 10:29:28 -0800439 /* Do we have a file handler for the type of file they're opening.
440 * Note: This should only fail if something is somehow crazy wrong.
441 * Since the canHandle() said yes, and that's tied into the list of explicit
442 * firmware handers (and file handlers, like this'll know where to write the
443 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800444 */
Patrick Venture18235e62018-11-08 10:21:09 -0800445 auto h = std::find_if(
446 handlers.begin(), handlers.end(),
447 [&path](const auto& iter) { return (iter.blobName == path); });
448 if (h == handlers.end())
449 {
450 return false;
451 }
452
453 /* Ok, so we found a handler that matched, so call open() */
454 if (!h->handler->open(path))
455 {
456 return false;
457 }
458
Patrick Venture70e30bf2019-01-17 10:29:28 -0800459 Session* curr;
460 const std::string* active;
461
Patrick Venture7dad86f2019-05-17 08:52:20 -0700462 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800463 {
464 /* 2c) are they opening the /flash/hash ? (to start the process) */
465 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700466 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800467 }
468 else
469 {
470 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700471 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800472 }
473
Patrick Venture18235e62018-11-08 10:21:09 -0800474 curr->flags = flags;
475 curr->dataHandler = d->handler;
476 curr->imageHandler = h->handler;
477
478 lookup[session] = curr;
479
Patrick Ventureefba42d2019-05-24 10:48:16 -0700480 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700481 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800482
Patrick Venture12233c52019-05-16 09:26:59 -0700483 state = UpdateState::uploadInProgress;
Patrick Venture991910a2018-11-09 19:43:48 -0800484 fileOpen = true;
485
Patrick Venture18235e62018-11-08 10:21:09 -0800486 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700487}
Patrick Venture53977962018-11-02 18:59:35 -0700488
Patrick Venture18235e62018-11-08 10:21:09 -0800489/**
490 * The write command really just grabs the data from wherever it is and sends it
491 * to the image handler. It's the image handler's responsibility to deal with
492 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800493 *
494 * This receives a session from the blob manager, therefore it is always called
495 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800496 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700497bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
498 const std::vector<uint8_t>& data)
499{
Patrick Venture18235e62018-11-08 10:21:09 -0800500 auto item = lookup.find(session);
501 if (item == lookup.end())
502 {
503 return false;
504 }
505
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800506 /* Prevent writing during verification. */
507 if (state == UpdateState::verificationStarted)
508 {
509 return false;
510 }
511
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700512 /* Prevent writing to the verification or update blobs. */
513 if (item->second->activePath == verifyBlobId ||
514 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700515 {
516 return false;
517 }
Patrick Venture699750d2019-05-15 09:24:09 -0700518
Patrick Venture18235e62018-11-08 10:21:09 -0800519 std::vector<std::uint8_t> bytes;
520
Patrick Venture05abf7e2018-11-09 11:02:56 -0800521 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800522 {
523 bytes = data;
524 }
525 else
526 {
527 /* little endian required per design, and so on, but TODO: do endianness
528 * with boost.
529 */
530 struct ExtChunkHdr header;
531
532 if (data.size() != sizeof(header))
533 {
534 return false;
535 }
536
537 std::memcpy(&header, data.data(), data.size());
538 bytes = item->second->dataHandler->copyFrom(header.length);
539 }
540
541 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700542}
Patrick Venture18235e62018-11-08 10:21:09 -0800543
Patrick Venture8c535332018-11-08 15:58:00 -0800544/*
545 * If the active session (image or hash) is over LPC, this allows
546 * configuring it. This option is only available before you start
547 * writing data for the given item (image or hash). This will return
548 * false at any other part. -- the lpc handler portion will know to return
549 * false.
550 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700551bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
552 const std::vector<uint8_t>& data)
553{
Patrick Venture8c535332018-11-08 15:58:00 -0800554 auto item = lookup.find(session);
555 if (item == lookup.end())
556 {
557 return false;
558 }
559
Patrick Venture05abf7e2018-11-09 11:02:56 -0800560 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800561 {
562 return false;
563 }
564
Patrick Ventured56097a2019-05-15 09:47:55 -0700565 /* Prevent writing meta to the verification blob (it has no data handler).
566 */
567 if (item->second->dataHandler)
568 {
569 return item->second->dataHandler->writeMeta(data);
570 }
Patrick Venture699750d2019-05-15 09:24:09 -0700571
Patrick Ventured56097a2019-05-15 09:47:55 -0700572 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700573}
Patrick Venture8c535332018-11-08 15:58:00 -0800574
Patrick Ventured6461d62018-11-09 17:30:25 -0800575/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700576 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800577 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800578 * the image.
579 *
580 * For this file to have opened, the other two must be closed, which means any
581 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800582 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700583bool FirmwareBlobHandler::commit(uint16_t session,
584 const std::vector<uint8_t>& data)
585{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800586 auto item = lookup.find(session);
587 if (item == lookup.end())
588 {
589 return false;
590 }
591
Patrick Venture1a406fe2019-05-31 07:29:56 -0700592 /* You can only commit on the verifyBlodId or updateBlobId */
593 if (item->second->activePath != verifyBlobId &&
594 item->second->activePath != updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800595 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700596 std::fprintf(stderr, "path: '%s' not expected for commit\n",
597 item->second->activePath.c_str());
Patrick Ventureffcc5502018-11-16 12:32:38 -0800598 return false;
599 }
600
Patrick Venture433cbc32019-05-30 09:53:10 -0700601 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800602 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700603 case UpdateState::verificationPending:
604 /* Set state to committing. */
605 item->second->flags |= blobs::StateFlags::committing;
606 return triggerVerification();
Patrick Venture433cbc32019-05-30 09:53:10 -0700607 case UpdateState::verificationStarted:
608 /* Calling repeatedly has no effect within an update process. */
609 return true;
610 case UpdateState::verificationCompleted:
611 /* Calling after the verification process has completed returns
612 * failure. */
613 return false;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700614 case UpdateState::updatePending:
Patrick Venture433cbc32019-05-30 09:53:10 -0700615 item->second->flags |= blobs::StateFlags::committing;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700616 return triggerUpdate();
Patrick Venture0079d892019-05-31 11:27:44 -0700617 case UpdateState::updateStarted:
618 /* Calling repeatedly has no effect within an update process. */
619 return true;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700620 default:
621 return false;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800622 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700623}
Patrick Ventured6461d62018-11-09 17:30:25 -0800624
625/*
626 * Close must be called on the firmware image before triggering
627 * verification via commit. Once the verification is complete, you can
628 * then close the hash file.
629 *
630 * If the `verify_image.service` returned success, closing the hash file
631 * will have a specific behavior depending on the update. If it's UBI,
632 * it'll perform the install. If it's static layout, it'll do nothing. The
633 * verify_image service in the static layout case is responsible for placing
634 * the file in the correct staging position.
635 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700636bool FirmwareBlobHandler::close(uint16_t session)
637{
Patrick Venture68bb1432018-11-09 20:08:48 -0800638 auto item = lookup.find(session);
639 if (item == lookup.end())
640 {
641 return false;
642 }
643
Patrick Venturee95dbb62019-06-05 09:59:29 -0700644 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800645 {
Patrick Venturee95dbb62019-06-05 09:59:29 -0700646 case UpdateState::uploadInProgress:
647 /* They are closing a data pathway (image, tarball, hash). */
648 state = UpdateState::verificationPending;
Patrick Venture85ae86b2019-06-05 09:18:40 -0700649
Patrick Venturee95dbb62019-06-05 09:59:29 -0700650 /* Add verify blob ID now that we can expect it. */
651 addBlobId(verifyBlobId);
652 break;
653 case UpdateState::verificationPending:
654 /* They haven't triggered, therefore closing is uninteresting.
655 */
656 break;
657 case UpdateState::verificationStarted:
658 /* TODO: If they close this blob before verification finishes,
659 * that's an abort.
660 */
661 return false;
662 case UpdateState::verificationCompleted:
663 if (lastVerificationStatus == ActionStatus::success)
664 {
665 state = UpdateState::updatePending;
666 addBlobId(updateBlobId);
667 removeBlobId(verifyBlobId);
668 }
669 else
670 {
671 /* TODO: Verification failed, what now? */
672 }
673 break;
674 case UpdateState::updatePending:
675 /* They haven't triggered the update, therefore this is
676 * uninteresting. */
677 break;
678 case UpdateState::updateStarted:
679 /* TODO: handle closing while update is running!. */
680 break;
681 case UpdateState::updateCompleted:
682 if (lastUpdateStatus == ActionStatus::failed)
683 {
684 /* TODO: lOG something? */
685 }
Patrick Venture930c7b72019-05-24 11:11:08 -0700686
Patrick Venturee95dbb62019-06-05 09:59:29 -0700687 state = UpdateState::notYetStarted;
688 removeBlobId(updateBlobId);
689 removeBlobId(activeImageBlobId);
690 removeBlobId(activeHashBlobId);
691 break;
692 default:
693 break;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800694 }
695
Patrick Venture68bb1432018-11-09 20:08:48 -0800696 if (item->second->dataHandler)
697 {
698 item->second->dataHandler->close();
699 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800700 if (item->second->imageHandler)
701 {
702 item->second->imageHandler->close();
703 }
704
Patrick Venture68bb1432018-11-09 20:08:48 -0800705 lookup.erase(item);
Patrick Venture991910a2018-11-09 19:43:48 -0800706 fileOpen = false;
Patrick Venture68bb1432018-11-09 20:08:48 -0800707 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700708}
Patrick Ventured6461d62018-11-09 17:30:25 -0800709
Patrick Venturec7ca2912018-11-02 11:38:33 -0700710bool FirmwareBlobHandler::expire(uint16_t session)
711{
712 return false;
713}
Patrick Ventured6461d62018-11-09 17:30:25 -0800714
715/*
716 * Currently, the design does not provide this with a function, however,
717 * it will likely change to support reading data back.
718 */
719std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
720 uint32_t offset,
721 uint32_t requestedSize)
722{
723 return {};
724}
725
Patrick Ventureffcc5502018-11-16 12:32:38 -0800726bool FirmwareBlobHandler::triggerVerification()
727{
Patrick Venture1d66fe62019-06-03 14:57:27 -0700728 bool result = verification->trigger();
Patrick Venture3ecb3502019-05-17 11:03:51 -0700729 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800730 {
Patrick Venture453f06a2019-01-17 10:02:12 -0800731 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800732 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800733
Patrick Venture3ecb3502019-05-17 11:03:51 -0700734 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800735}
736
Patrick Venture1a406fe2019-05-31 07:29:56 -0700737bool FirmwareBlobHandler::triggerUpdate()
738{
Patrick Venture1d66fe62019-06-03 14:57:27 -0700739 bool result = update->trigger();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700740 if (result)
741 {
742 state = UpdateState::updateStarted;
743 }
744
745 return result;
746}
747
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700748} // namespace ipmi_flash