blob: 172281ce4275f59a12488d508548d87f9ac1cef4 [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:
126 break;
127 case UpdateState::updatePending:
128 break;
129 default:
130 break;
131 }
132
Patrick Venturebcc0c772019-06-17 10:42:06 -0700133 /* TODO: implement. */
134 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700135}
Patrick Venture53977962018-11-02 18:59:35 -0700136
Patrick Ventured6461d62018-11-09 17:30:25 -0800137/*
138 * Stat on the files will return information such as what supported
139 * transport mechanisms are available.
140 *
141 * Stat on an active file or hash will return information such as the size
142 * of the data cached, and any additional pertinent information. The
143 * blob_state on the active files will return the state of the update.
144 */
Patrick Venturee312f392019-06-04 07:44:37 -0700145bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700146{
Patrick Venture46637c82018-11-06 15:20:24 -0800147 /* We know we support this path because canHandle is called ahead */
Patrick Ventured342a952019-05-29 09:09:10 -0700148 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700149 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800150 {
Patrick Ventured342a952019-05-29 09:09:10 -0700151 /* These blobs are placeholders that indicate things, or allow actions,
152 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800153 */
Patrick Ventured342a952019-05-29 09:09:10 -0700154 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800155 }
156
Patrick Ventured342a952019-05-29 09:09:10 -0700157 /* They are requesting information about the generic blob_id. */
158 meta->blobState = bitmask;
159 meta->size = 0;
160
161 /* The generic blob_ids state is only the bits related to the transport
162 * mechanisms.
163 */
164 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700165}
Patrick Venture53977962018-11-02 18:59:35 -0700166
Patrick Ventureda66fd82019-06-03 11:11:24 -0700167ActionStatus FirmwareBlobHandler::getActionStatus()
Patrick Venturea2d82392019-06-03 10:55:17 -0700168{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700169 ActionStatus value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700170
171 switch (state)
172 {
173 case UpdateState::verificationPending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700174 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700175 break;
176 case UpdateState::verificationStarted:
177 value = verification->status();
Patrick Ventureda66fd82019-06-03 11:11:24 -0700178 lastVerificationStatus = value;
Patrick Venturea2d82392019-06-03 10:55:17 -0700179 break;
180 case UpdateState::verificationCompleted:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700181 value = lastVerificationStatus;
Patrick Venturea2d82392019-06-03 10:55:17 -0700182 break;
Patrick Venturea2d82392019-06-03 10:55:17 -0700183 case UpdateState::updatePending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700184 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700185 break;
186 case UpdateState::updateStarted:
187 value = update->status();
188 lastUpdateStatus = value;
189 break;
190 case UpdateState::updateCompleted:
191 value = lastUpdateStatus;
192 break;
193 default:
194 break;
195 }
196
197 return value;
198}
199
Patrick Venturec02849b2018-11-06 17:31:15 -0800200/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800201 * Return stat information on an open session. It therefore must be an active
202 * handle to either the active image or active hash.
Patrick Ventured6461d62018-11-09 17:30:25 -0800203 */
Patrick Venturee312f392019-06-04 07:44:37 -0700204bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800205{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800206 auto item = lookup.find(session);
207 if (item == lookup.end())
208 {
209 return false;
210 }
211
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700212 /* The size here refers to the size of the file -- of something analagous.
213 */
214 meta->size = (item->second->imageHandler)
215 ? item->second->imageHandler->getSize()
216 : 0;
217
218 meta->metadata.clear();
219
Patrick Ventureda66fd82019-06-03 11:11:24 -0700220 if (item->second->activePath == verifyBlobId ||
221 item->second->activePath == updateBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700222 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700223 ActionStatus value = getActionStatus();
Patrick Venture699750d2019-05-15 09:24:09 -0700224
Patrick Venturee955e072019-05-15 16:16:46 -0700225 meta->metadata.push_back(static_cast<std::uint8_t>(value));
226
227 /* Change the firmware handler's state and the blob's stat value
228 * depending.
229 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700230 if (value == ActionStatus::success || value == ActionStatus::failed)
Patrick Venturee955e072019-05-15 16:16:46 -0700231 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700232 if (item->second->activePath == verifyBlobId)
Patrick Venturee955e072019-05-15 16:16:46 -0700233 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700234 state = UpdateState::verificationCompleted;
Patrick Venturee955e072019-05-15 16:16:46 -0700235 }
236 else
237 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700238 /* item->second->activePath == updateBlobId */
239 state = UpdateState::updateCompleted;
Patrick Venturee955e072019-05-15 16:16:46 -0700240 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700241
Patrick Venturef1f0f652019-06-03 09:10:19 -0700242 item->second->flags &= ~blobs::StateFlags::committing;
243
Patrick Ventureda66fd82019-06-03 11:11:24 -0700244 if (value == ActionStatus::success)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700245 {
246 item->second->flags |= blobs::StateFlags::committed;
247 }
248 else
249 {
250 item->second->flags |= blobs::StateFlags::commit_error;
251 }
252 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700253 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800254
Patrick Venturee955e072019-05-15 16:16:46 -0700255 /* The blobState here relates to an active sesion, so we should return the
256 * flags used to open this session.
257 */
258 meta->blobState = item->second->flags;
259
Patrick Venturecc7d1602018-11-15 13:58:33 -0800260 /* The metadata blob returned comes from the data handler... it's used for
261 * instance, in P2A bridging to get required information about the mapping,
262 * and is the "opposite" of the lpc writemeta requirement.
263 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800264 if (item->second->dataHandler)
265 {
Patrick Venture74304642019-01-17 09:31:04 -0800266 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800267 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
268 bytes.end());
269 }
270
Patrick Venturecc7d1602018-11-15 13:58:33 -0800271 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800272}
273
274/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800275 * If you open /flash/image or /flash/tarball, or /flash/hash it will
276 * interpret the open flags and perform whatever actions are required for
277 * that update process. The session returned can be used immediately for
278 * sending data down, without requiring one to open the new active file.
279 *
280 * If you open the active flash image or active hash it will let you
281 * overwrite pieces, depending on the state.
282 *
283 * Once the verification process has started the active files cannot be
284 * opened.
285 *
286 * You can only have one open session at a time. Which means, you can only
287 * have one file open at a time. Trying to open the hash blob_id while you
288 * still have the flash image blob_id open will fail. Opening the flash
289 * blob_id when it is already open will fail.
290 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700291bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
292 const std::string& path)
293{
Patrick Venturec02849b2018-11-06 17:31:15 -0800294 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800295 *
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800296 * Further on this, if there's an active session to the hash we don't allow
297 * re-opening the image, and if we have the image open, we don't allow
298 * opening the hash. This design decision may be re-evaluated, and changed
299 * to only allow one session per object type (of the two types). But,
300 * consider if the hash is open, do we want to allow writing to the image?
301 * And why would we? But, really, the point of no-return is once the
302 * verification process has begun -- which is done via commit() on the hash
303 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700304 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800305 if (fileOpen)
306 {
307 return false;
308 }
309
Patrick Venture723113f2019-06-05 09:38:35 -0700310 /* The active blobs are only meant to indicate status that something has
311 * opened the image file or the hash file.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800312 */
Patrick Venture19f5d882019-05-30 14:34:55 -0700313 if (path == activeImageBlobId || path == activeHashBlobId)
314 {
315 /* 2a) are they opening the active image? this can only happen if they
316 * already started one (due to canHandleBlob's behavior).
317 */
318 /* 2b) are they opening the active hash? this can only happen if they
319 * already started one (due to canHandleBlob's behavior).
320 */
321 return false;
322 }
323
Patrick Venture723113f2019-06-05 09:38:35 -0700324 /* Check that they've opened for writing - read back not currently
325 * supported.
326 */
327 if ((flags & blobs::OpenFlags::write) == 0)
328 {
329 return false;
330 }
331
332 /* Because canHandleBlob is called before open, we know that if they try to
333 * open the verifyBlobId, they're in a state where it's present.
334 */
335
336 switch (state)
337 {
338 case UpdateState::uploadInProgress:
339 /* Unreachable code because if it's started a file is open. */
340 break;
341 case UpdateState::verificationPending:
342 /* Handle opening the verifyBlobId --> we know the image and hash
343 * aren't open because of the fileOpen check. They can still open
344 * other files from this state to transition back into
345 * uploadInProgress.
346 *
347 * The file must be opened for writing, but no transport mechanism
348 * specified since it's irrelevant.
349 */
350 if (path == verifyBlobId)
351 {
352 verifyImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700353
354 lookup[session] = &verifyImage;
355
356 fileOpen = true;
357 return true;
358 }
359 break;
360 case UpdateState::verificationStarted:
361 case UpdateState::verificationCompleted:
362 /* Unreachable code because if it's started a file is open. */
363 return false;
364 case UpdateState::updatePending:
365 {
366 /* When in this state, they can only open the updateBlobId */
367 if (path == updateBlobId)
368 {
369 updateImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700370
371 lookup[session] = &updateImage;
372
373 fileOpen = true;
374 return true;
375 }
376 else
377 {
378 return false;
379 }
380 }
381 case UpdateState::updateStarted:
382 case UpdateState::updateCompleted:
383 /* Unreachable code because if it's started a file is open. */
384 break;
385 default:
386 break;
387 }
388
389 /* There are two abstractions at play, how you get the data and how you
390 * handle that data. such that, whether the data comes from the PCI bridge
391 * or LPC bridge is not connected to whether the data goes into a static
392 * layout flash update or a UBI tarball.
393 */
394
Patrick Venturec02849b2018-11-06 17:31:15 -0800395 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800396 * support what they request.
397 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800398 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800399 {
400 return false;
401 }
402
Patrick Venture18235e62018-11-08 10:21:09 -0800403 /* How are they expecting to copy this data? */
404 auto d = std::find_if(
405 transports.begin(), transports.end(),
406 [&flags](const auto& iter) { return (iter.bitmask & flags); });
407 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800408 {
Patrick Venture18235e62018-11-08 10:21:09 -0800409 return false;
410 }
411
412 /* We found the transport handler they requested, no surprise since
413 * above we verify they selected at least one we wanted.
414 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800415
Patrick Venture6e307a72018-11-09 18:21:17 -0800416 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
417 * only non-external data pathway -- but this is just a more generic
418 * approach to that.
419 */
420 if (d->handler)
421 {
422 /* If the data handler open call fails, open fails. */
423 if (!d->handler->open())
424 {
425 return false;
426 }
427 }
428
Patrick Venture70e30bf2019-01-17 10:29:28 -0800429 /* Do we have a file handler for the type of file they're opening.
430 * Note: This should only fail if something is somehow crazy wrong.
431 * Since the canHandle() said yes, and that's tied into the list of explicit
432 * firmware handers (and file handlers, like this'll know where to write the
433 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800434 */
Patrick Venture18235e62018-11-08 10:21:09 -0800435 auto h = std::find_if(
436 handlers.begin(), handlers.end(),
437 [&path](const auto& iter) { return (iter.blobName == path); });
438 if (h == handlers.end())
439 {
440 return false;
441 }
442
443 /* Ok, so we found a handler that matched, so call open() */
444 if (!h->handler->open(path))
445 {
446 return false;
447 }
448
Patrick Venture70e30bf2019-01-17 10:29:28 -0800449 Session* curr;
450 const std::string* active;
451
Patrick Venture7dad86f2019-05-17 08:52:20 -0700452 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800453 {
454 /* 2c) are they opening the /flash/hash ? (to start the process) */
455 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700456 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800457 }
458 else
459 {
460 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700461 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800462 }
463
Patrick Venture18235e62018-11-08 10:21:09 -0800464 curr->flags = flags;
465 curr->dataHandler = d->handler;
466 curr->imageHandler = h->handler;
467
468 lookup[session] = curr;
469
Patrick Ventureefba42d2019-05-24 10:48:16 -0700470 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700471 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800472
Patrick Venture12233c52019-05-16 09:26:59 -0700473 state = UpdateState::uploadInProgress;
Patrick Venture991910a2018-11-09 19:43:48 -0800474 fileOpen = true;
475
Patrick Venture18235e62018-11-08 10:21:09 -0800476 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700477}
Patrick Venture53977962018-11-02 18:59:35 -0700478
Patrick Venture18235e62018-11-08 10:21:09 -0800479/**
480 * The write command really just grabs the data from wherever it is and sends it
481 * to the image handler. It's the image handler's responsibility to deal with
482 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800483 *
484 * This receives a session from the blob manager, therefore it is always called
485 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800486 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700487bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
488 const std::vector<uint8_t>& data)
489{
Patrick Venture18235e62018-11-08 10:21:09 -0800490 auto item = lookup.find(session);
491 if (item == lookup.end())
492 {
493 return false;
494 }
495
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800496 /* Prevent writing during verification. */
497 if (state == UpdateState::verificationStarted)
498 {
499 return false;
500 }
501
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700502 /* Prevent writing to the verification or update blobs. */
503 if (item->second->activePath == verifyBlobId ||
504 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700505 {
506 return false;
507 }
Patrick Venture699750d2019-05-15 09:24:09 -0700508
Patrick Venture18235e62018-11-08 10:21:09 -0800509 std::vector<std::uint8_t> bytes;
510
Patrick Venture05abf7e2018-11-09 11:02:56 -0800511 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800512 {
513 bytes = data;
514 }
515 else
516 {
517 /* little endian required per design, and so on, but TODO: do endianness
518 * with boost.
519 */
520 struct ExtChunkHdr header;
521
522 if (data.size() != sizeof(header))
523 {
524 return false;
525 }
526
527 std::memcpy(&header, data.data(), data.size());
528 bytes = item->second->dataHandler->copyFrom(header.length);
529 }
530
531 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700532}
Patrick Venture18235e62018-11-08 10:21:09 -0800533
Patrick Venture8c535332018-11-08 15:58:00 -0800534/*
535 * If the active session (image or hash) is over LPC, this allows
536 * configuring it. This option is only available before you start
537 * writing data for the given item (image or hash). This will return
538 * false at any other part. -- the lpc handler portion will know to return
539 * false.
540 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700541bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
542 const std::vector<uint8_t>& data)
543{
Patrick Venture8c535332018-11-08 15:58:00 -0800544 auto item = lookup.find(session);
545 if (item == lookup.end())
546 {
547 return false;
548 }
549
Patrick Venture05abf7e2018-11-09 11:02:56 -0800550 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800551 {
552 return false;
553 }
554
Patrick Ventured56097a2019-05-15 09:47:55 -0700555 /* Prevent writing meta to the verification blob (it has no data handler).
556 */
557 if (item->second->dataHandler)
558 {
559 return item->second->dataHandler->writeMeta(data);
560 }
Patrick Venture699750d2019-05-15 09:24:09 -0700561
Patrick Ventured56097a2019-05-15 09:47:55 -0700562 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700563}
Patrick Venture8c535332018-11-08 15:58:00 -0800564
Patrick Ventured6461d62018-11-09 17:30:25 -0800565/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700566 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800567 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800568 * the image.
569 *
570 * For this file to have opened, the other two must be closed, which means any
571 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800572 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700573bool FirmwareBlobHandler::commit(uint16_t session,
574 const std::vector<uint8_t>& data)
575{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800576 auto item = lookup.find(session);
577 if (item == lookup.end())
578 {
579 return false;
580 }
581
Patrick Venture1a406fe2019-05-31 07:29:56 -0700582 /* You can only commit on the verifyBlodId or updateBlobId */
583 if (item->second->activePath != verifyBlobId &&
584 item->second->activePath != updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800585 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700586 std::fprintf(stderr, "path: '%s' not expected for commit\n",
587 item->second->activePath.c_str());
Patrick Ventureffcc5502018-11-16 12:32:38 -0800588 return false;
589 }
590
Patrick Venture433cbc32019-05-30 09:53:10 -0700591 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800592 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700593 case UpdateState::verificationPending:
594 /* Set state to committing. */
595 item->second->flags |= blobs::StateFlags::committing;
596 return triggerVerification();
Patrick Venture433cbc32019-05-30 09:53:10 -0700597 case UpdateState::verificationStarted:
598 /* Calling repeatedly has no effect within an update process. */
599 return true;
600 case UpdateState::verificationCompleted:
601 /* Calling after the verification process has completed returns
602 * failure. */
603 return false;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700604 case UpdateState::updatePending:
Patrick Venture433cbc32019-05-30 09:53:10 -0700605 item->second->flags |= blobs::StateFlags::committing;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700606 return triggerUpdate();
Patrick Venture0079d892019-05-31 11:27:44 -0700607 case UpdateState::updateStarted:
608 /* Calling repeatedly has no effect within an update process. */
609 return true;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700610 default:
611 return false;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800612 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700613}
Patrick Ventured6461d62018-11-09 17:30:25 -0800614
615/*
616 * Close must be called on the firmware image before triggering
617 * verification via commit. Once the verification is complete, you can
618 * then close the hash file.
619 *
620 * If the `verify_image.service` returned success, closing the hash file
621 * will have a specific behavior depending on the update. If it's UBI,
622 * it'll perform the install. If it's static layout, it'll do nothing. The
623 * verify_image service in the static layout case is responsible for placing
624 * the file in the correct staging position.
625 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700626bool FirmwareBlobHandler::close(uint16_t session)
627{
Patrick Venture68bb1432018-11-09 20:08:48 -0800628 auto item = lookup.find(session);
629 if (item == lookup.end())
630 {
631 return false;
632 }
633
Patrick Venturee95dbb62019-06-05 09:59:29 -0700634 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800635 {
Patrick Venturee95dbb62019-06-05 09:59:29 -0700636 case UpdateState::uploadInProgress:
637 /* They are closing a data pathway (image, tarball, hash). */
638 state = UpdateState::verificationPending;
Patrick Venture85ae86b2019-06-05 09:18:40 -0700639
Patrick Venturee95dbb62019-06-05 09:59:29 -0700640 /* Add verify blob ID now that we can expect it. */
641 addBlobId(verifyBlobId);
642 break;
643 case UpdateState::verificationPending:
644 /* They haven't triggered, therefore closing is uninteresting.
645 */
646 break;
647 case UpdateState::verificationStarted:
Patrick Ventured5741022019-06-17 09:08:35 -0700648 /* Abort without checking to see if it happened to finish. Require
649 * the caller to stat() deliberately.
Patrick Venturee95dbb62019-06-05 09:59:29 -0700650 */
Patrick Ventured5741022019-06-17 09:08:35 -0700651 abortVerification();
652 abortProcess();
653 break;
Patrick Venturee95dbb62019-06-05 09:59:29 -0700654 case UpdateState::verificationCompleted:
655 if (lastVerificationStatus == ActionStatus::success)
656 {
657 state = UpdateState::updatePending;
658 addBlobId(updateBlobId);
659 removeBlobId(verifyBlobId);
660 }
661 else
662 {
663 /* TODO: Verification failed, what now? */
664 }
665 break;
666 case UpdateState::updatePending:
667 /* They haven't triggered the update, therefore this is
668 * uninteresting. */
669 break;
670 case UpdateState::updateStarted:
Patrick Venture49731712019-06-17 10:04:02 -0700671 /* Abort without checking to see if it happened to finish. Require
672 * the caller to stat() deliberately.
673 */
674 abortUpdate();
675 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700676 break;
677 case UpdateState::updateCompleted:
678 if (lastUpdateStatus == ActionStatus::failed)
679 {
680 /* TODO: lOG something? */
681 }
Patrick Venture930c7b72019-05-24 11:11:08 -0700682
Patrick Venturee95dbb62019-06-05 09:59:29 -0700683 state = UpdateState::notYetStarted;
684 removeBlobId(updateBlobId);
685 removeBlobId(activeImageBlobId);
686 removeBlobId(activeHashBlobId);
687 break;
688 default:
689 break;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800690 }
691
Patrick Venture68bb1432018-11-09 20:08:48 -0800692 if (item->second->dataHandler)
693 {
694 item->second->dataHandler->close();
695 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800696 if (item->second->imageHandler)
697 {
698 item->second->imageHandler->close();
699 }
700
Patrick Venture68bb1432018-11-09 20:08:48 -0800701 lookup.erase(item);
Patrick Venture991910a2018-11-09 19:43:48 -0800702 fileOpen = false;
Patrick Venture68bb1432018-11-09 20:08:48 -0800703 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700704}
Patrick Ventured6461d62018-11-09 17:30:25 -0800705
Patrick Venturec7ca2912018-11-02 11:38:33 -0700706bool FirmwareBlobHandler::expire(uint16_t session)
707{
708 return false;
709}
Patrick Ventured6461d62018-11-09 17:30:25 -0800710
711/*
712 * Currently, the design does not provide this with a function, however,
713 * it will likely change to support reading data back.
714 */
715std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
716 uint32_t offset,
717 uint32_t requestedSize)
718{
719 return {};
720}
721
Patrick Ventured5741022019-06-17 09:08:35 -0700722void FirmwareBlobHandler::abortProcess()
723{
724 /* Closing of open files is handled from close() -- Reaching here from
725 * delete may never be supported.
726 */
727 removeBlobId(verifyBlobId);
728 removeBlobId(updateBlobId);
729 removeBlobId(activeImageBlobId);
730 removeBlobId(activeHashBlobId);
731
732 state = UpdateState::notYetStarted;
733}
734
735void FirmwareBlobHandler::abortVerification()
736{
737 verification->abort();
738}
739
Patrick Ventureffcc5502018-11-16 12:32:38 -0800740bool FirmwareBlobHandler::triggerVerification()
741{
Patrick Venture1d66fe62019-06-03 14:57:27 -0700742 bool result = verification->trigger();
Patrick Venture3ecb3502019-05-17 11:03:51 -0700743 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800744 {
Patrick Venture453f06a2019-01-17 10:02:12 -0800745 state = UpdateState::verificationStarted;
Patrick Venturecabc1172018-11-16 16:14:26 -0800746 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800747
Patrick Venture3ecb3502019-05-17 11:03:51 -0700748 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800749}
750
Patrick Venture49731712019-06-17 10:04:02 -0700751void FirmwareBlobHandler::abortUpdate()
752{
753 update->abort();
754}
755
Patrick Venture1a406fe2019-05-31 07:29:56 -0700756bool FirmwareBlobHandler::triggerUpdate()
757{
Patrick Venture1d66fe62019-06-03 14:57:27 -0700758 bool result = update->trigger();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700759 if (result)
760 {
761 state = UpdateState::updateStarted;
762 }
763
764 return result;
765}
766
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700767} // namespace ipmi_flash