blob: 9923d5b6ca5a990d412a023454d8cb2a8712e622 [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 Venture84778b82019-06-26 20:11:09 -070019#include "data.hpp"
20#include "flags.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -080021#include "image_handler.hpp"
Patrick Venture1d66fe62019-06-03 14:57:27 -070022#include "status.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -070023#include "util.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -080024
Patrick Venture9efef5d2019-06-19 08:45:44 -070025#include <blobs-ipmid/blobs.hpp>
Patrick Venture9b37b092020-05-28 20:58:57 -070026#include <phosphor-logging/log.hpp>
27
28#include <algorithm>
Patrick Venture192d60f2018-11-06 11:11:59 -080029#include <cstdint>
Patrick Venture18235e62018-11-08 10:21:09 -080030#include <cstring>
Patrick Ventureb3b4db72019-05-15 11:30:24 -070031#include <fstream>
Patrick Venture68cf64f2018-11-06 10:46:51 -080032#include <memory>
Patrick Venturefa6c4d92018-11-02 18:34:53 -070033#include <string>
34#include <vector>
35
Patrick Ventured333a872018-12-03 16:24:26 -080036using namespace phosphor::logging;
37
Patrick Venture1d5a31c2019-05-20 11:38:22 -070038namespace ipmi_flash
Patrick Venturec7ca2912018-11-02 11:38:33 -070039{
Patrick Ventureb3b4db72019-05-15 11:30:24 -070040
Patrick Venture1d5a31c2019-05-20 11:38:22 -070041std::unique_ptr<blobs::GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080042 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventured4e20de2019-07-18 12:48:05 -070043 std::vector<HandlerPack>&& firmwares,
Patrick Venturefa06a5f2019-07-01 09:22:38 -070044 const std::vector<DataHandlerPack>& transports, ActionMap&& actionPacks)
Patrick Venture68cf64f2018-11-06 10:46:51 -080045{
Patrick Venture16265382019-11-06 19:31:36 -080046 /* There must be at least one in addition to the hash blob handler. */
47 if (firmwares.size() < 2)
Patrick Venture52854622018-11-06 12:30:00 -080048 {
Patrick Venture16265382019-11-06 19:31:36 -080049 log<level::ERR>("Must provide at least two firmware handlers.");
Patrick Venture52854622018-11-06 12:30:00 -080050 return nullptr;
51 }
Patrick Venture4e2a1432019-11-06 20:06:07 -080052 if (transports.empty())
Patrick Venture1cde5f92018-11-07 08:26:47 -080053 {
54 return nullptr;
55 }
Patrick Venture16265382019-11-06 19:31:36 -080056 if (actionPacks.empty())
57 {
58 return nullptr;
59 }
Patrick Venture52854622018-11-06 12:30:00 -080060
Patrick Venturea78e39f2018-11-06 18:37:06 -080061 std::vector<std::string> blobs;
62 for (const auto& item : firmwares)
63 {
64 blobs.push_back(item.blobName);
65 }
Patrick Venture18235e62018-11-08 10:21:09 -080066
Patrick Venture7dad86f2019-05-17 08:52:20 -070067 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId))
Patrick Venture18235e62018-11-08 10:21:09 -080068 {
69 return nullptr;
70 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080071
Benjamin Fair12901982019-11-12 13:55:46 -080072 return std::make_unique<FirmwareBlobHandler>(
73 std::move(firmwares), blobs, transports, std::move(actionPacks));
Patrick Venture68cf64f2018-11-06 10:46:51 -080074}
75
Patrick Ventured6461d62018-11-09 17:30:25 -080076/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070077bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
78{
Patrick Venture6032dc02019-05-17 11:01:44 -070079 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070080}
Patrick Venture53977962018-11-02 18:59:35 -070081
Patrick Ventured6461d62018-11-09 17:30:25 -080082/*
83 * Grab the list of supported firmware.
84 *
85 * If there's an open firmware session, it'll already be present in the
86 * list as "/flash/active/image", and if the hash has started,
87 * "/flash/active/hash" regardless of mechanism. This is done in the open
88 * comamnd, no extra work is required here.
89 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070090std::vector<std::string> FirmwareBlobHandler::getBlobIds()
91{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080092 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070093}
Patrick Venture53977962018-11-02 18:59:35 -070094
Patrick Ventured6461d62018-11-09 17:30:25 -080095/*
96 * Per the design, this mean abort, and this will trigger whatever
97 * appropriate actions are required to abort the process.
98 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070099bool FirmwareBlobHandler::deleteBlob(const std::string& path)
100{
Patrick Venture72676762019-06-17 11:22:38 -0700101 switch (state)
102 {
103 case UpdateState::notYetStarted:
104 /* Trying to delete anything at this point has no effect and returns
105 * false.
106 */
107 return false;
108 case UpdateState::verificationPending:
Patrick Venture2ca66522019-06-17 11:58:38 -0700109 abortProcess();
110 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700111 case UpdateState::updatePending:
Patrick Venturec9f62392019-06-17 12:17:26 -0700112 abortProcess();
113 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700114 default:
115 break;
116 }
117
Patrick Venturebcc0c772019-06-17 10:42:06 -0700118 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700119}
Patrick Venture53977962018-11-02 18:59:35 -0700120
Patrick Ventured6461d62018-11-09 17:30:25 -0800121/*
122 * Stat on the files will return information such as what supported
123 * transport mechanisms are available.
124 *
125 * Stat on an active file or hash will return information such as the size
126 * of the data cached, and any additional pertinent information. The
127 * blob_state on the active files will return the state of the update.
128 */
Patrick Venturee312f392019-06-04 07:44:37 -0700129bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700130{
Patrick Venture46637c82018-11-06 15:20:24 -0800131 /* We know we support this path because canHandle is called ahead */
Patrick Ventured342a952019-05-29 09:09:10 -0700132 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700133 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800134 {
Patrick Ventured342a952019-05-29 09:09:10 -0700135 /* These blobs are placeholders that indicate things, or allow actions,
136 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800137 */
Patrick Ventured342a952019-05-29 09:09:10 -0700138 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800139 }
140
Patrick Ventured342a952019-05-29 09:09:10 -0700141 /* They are requesting information about the generic blob_id. */
Patrick Ventured342a952019-05-29 09:09:10 -0700142
Benjamin Fair12901982019-11-12 13:55:46 -0800143 /* Older host tools expect the blobState to contain a bitmask of available
144 * transport backends, so report that we support all of them in order to
145 * preserve backwards compatibility.
Patrick Ventured342a952019-05-29 09:09:10 -0700146 */
Benjamin Fair12901982019-11-12 13:55:46 -0800147 meta->blobState = transportMask;
148 meta->size = 0;
Patrick Ventured342a952019-05-29 09:09:10 -0700149 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700150}
Patrick Venture53977962018-11-02 18:59:35 -0700151
Patrick Ventureda66fd82019-06-03 11:11:24 -0700152ActionStatus FirmwareBlobHandler::getActionStatus()
Patrick Venturea2d82392019-06-03 10:55:17 -0700153{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700154 ActionStatus value = ActionStatus::unknown;
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700155 auto* pack = getActionPack();
Patrick Venturea2d82392019-06-03 10:55:17 -0700156
157 switch (state)
158 {
159 case UpdateState::verificationPending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700160 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700161 break;
162 case UpdateState::verificationStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700163 /* If we got here, there must be data AND a hash, not just a hash,
164 * therefore pack will be known. */
165 if (!pack)
166 {
167 break;
168 }
169 value = pack->verification->status();
Patrick Ventureda66fd82019-06-03 11:11:24 -0700170 lastVerificationStatus = value;
Patrick Venturea2d82392019-06-03 10:55:17 -0700171 break;
172 case UpdateState::verificationCompleted:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700173 value = lastVerificationStatus;
Patrick Venturea2d82392019-06-03 10:55:17 -0700174 break;
Patrick Venturea2d82392019-06-03 10:55:17 -0700175 case UpdateState::updatePending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700176 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700177 break;
178 case UpdateState::updateStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700179 if (!pack)
180 {
181 break;
182 }
183 value = pack->update->status();
Patrick Venturea2d82392019-06-03 10:55:17 -0700184 lastUpdateStatus = value;
185 break;
186 case UpdateState::updateCompleted:
187 value = lastUpdateStatus;
188 break;
189 default:
190 break;
191 }
192
193 return value;
194}
195
Patrick Venturec02849b2018-11-06 17:31:15 -0800196/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800197 * Return stat information on an open session. It therefore must be an active
198 * handle to either the active image or active hash.
Patrick Ventured6461d62018-11-09 17:30:25 -0800199 */
Patrick Venturee312f392019-06-04 07:44:37 -0700200bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800201{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800202 auto item = lookup.find(session);
203 if (item == lookup.end())
204 {
205 return false;
206 }
207
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700208 /* The size here refers to the size of the file -- of something analagous.
209 */
210 meta->size = (item->second->imageHandler)
211 ? item->second->imageHandler->getSize()
212 : 0;
213
214 meta->metadata.clear();
215
Patrick Ventureda66fd82019-06-03 11:11:24 -0700216 if (item->second->activePath == verifyBlobId ||
217 item->second->activePath == updateBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700218 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700219 ActionStatus value = getActionStatus();
Patrick Venture699750d2019-05-15 09:24:09 -0700220
Patrick Venturee955e072019-05-15 16:16:46 -0700221 meta->metadata.push_back(static_cast<std::uint8_t>(value));
222
223 /* Change the firmware handler's state and the blob's stat value
224 * depending.
225 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700226 if (value == ActionStatus::success || value == ActionStatus::failed)
Patrick Venturee955e072019-05-15 16:16:46 -0700227 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700228 if (item->second->activePath == verifyBlobId)
Patrick Venturee955e072019-05-15 16:16:46 -0700229 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700230 changeState(UpdateState::verificationCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700231 }
232 else
233 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700234 /* item->second->activePath == updateBlobId */
Patrick Venture75c0c272019-06-21 09:15:08 -0700235 changeState(UpdateState::updateCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700236 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700237
Patrick Venturef1f0f652019-06-03 09:10:19 -0700238 item->second->flags &= ~blobs::StateFlags::committing;
239
Patrick Ventureda66fd82019-06-03 11:11:24 -0700240 if (value == ActionStatus::success)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700241 {
242 item->second->flags |= blobs::StateFlags::committed;
243 }
244 else
245 {
246 item->second->flags |= blobs::StateFlags::commit_error;
247 }
248 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700249 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800250
Patrick Venturee955e072019-05-15 16:16:46 -0700251 /* The blobState here relates to an active sesion, so we should return the
252 * flags used to open this session.
253 */
254 meta->blobState = item->second->flags;
255
Patrick Venturecc7d1602018-11-15 13:58:33 -0800256 /* The metadata blob returned comes from the data handler... it's used for
257 * instance, in P2A bridging to get required information about the mapping,
258 * and is the "opposite" of the lpc writemeta requirement.
259 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800260 if (item->second->dataHandler)
261 {
Patrick Venture74304642019-01-17 09:31:04 -0800262 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800263 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
264 bytes.end());
265 }
266
Patrick Venturecc7d1602018-11-15 13:58:33 -0800267 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800268}
269
270/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800271 * If you open /flash/image or /flash/tarball, or /flash/hash it will
272 * interpret the open flags and perform whatever actions are required for
273 * that update process. The session returned can be used immediately for
274 * sending data down, without requiring one to open the new active file.
275 *
276 * If you open the active flash image or active hash it will let you
277 * overwrite pieces, depending on the state.
278 *
279 * Once the verification process has started the active files cannot be
280 * opened.
281 *
282 * You can only have one open session at a time. Which means, you can only
283 * have one file open at a time. Trying to open the hash blob_id while you
284 * still have the flash image blob_id open will fail. Opening the flash
285 * blob_id when it is already open will fail.
286 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700287bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
288 const std::string& path)
289{
Patrick Venturec02849b2018-11-06 17:31:15 -0800290 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800291 *
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800292 * Further on this, if there's an active session to the hash we don't allow
293 * re-opening the image, and if we have the image open, we don't allow
294 * opening the hash. This design decision may be re-evaluated, and changed
295 * to only allow one session per object type (of the two types). But,
296 * consider if the hash is open, do we want to allow writing to the image?
297 * And why would we? But, really, the point of no-return is once the
298 * verification process has begun -- which is done via commit() on the hash
299 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700300 */
Brandon Kim8fc60872019-10-18 15:15:50 -0700301 if (fileOpen())
Patrick Venturec02849b2018-11-06 17:31:15 -0800302 {
303 return false;
304 }
305
Patrick Venture723113f2019-06-05 09:38:35 -0700306 /* The active blobs are only meant to indicate status that something has
307 * opened the image file or the hash file.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800308 */
Patrick Venture19f5d882019-05-30 14:34:55 -0700309 if (path == activeImageBlobId || path == activeHashBlobId)
310 {
311 /* 2a) are they opening the active image? this can only happen if they
312 * already started one (due to canHandleBlob's behavior).
313 */
314 /* 2b) are they opening the active hash? this can only happen if they
315 * already started one (due to canHandleBlob's behavior).
316 */
317 return false;
318 }
319
Patrick Venture723113f2019-06-05 09:38:35 -0700320 /* Check that they've opened for writing - read back not currently
321 * supported.
322 */
323 if ((flags & blobs::OpenFlags::write) == 0)
324 {
325 return false;
326 }
327
328 /* Because canHandleBlob is called before open, we know that if they try to
329 * open the verifyBlobId, they're in a state where it's present.
330 */
331
332 switch (state)
333 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700334 case UpdateState::notYetStarted:
335 /* Only hashBlobId and firmware BlobIds present. */
336 break;
Patrick Venture723113f2019-06-05 09:38:35 -0700337 case UpdateState::uploadInProgress:
338 /* Unreachable code because if it's started a file is open. */
339 break;
340 case UpdateState::verificationPending:
341 /* Handle opening the verifyBlobId --> we know the image and hash
Brandon Kim8fc60872019-10-18 15:15:50 -0700342 * aren't open because of the fileOpen() check. They can still open
Patrick Venture723113f2019-06-05 09:38:35 -0700343 * other files from this state to transition back into
344 * uploadInProgress.
345 *
346 * The file must be opened for writing, but no transport mechanism
347 * specified since it's irrelevant.
348 */
349 if (path == verifyBlobId)
350 {
351 verifyImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700352
353 lookup[session] = &verifyImage;
354
Patrick Venture723113f2019-06-05 09:38:35 -0700355 return true;
356 }
357 break;
358 case UpdateState::verificationStarted:
359 case UpdateState::verificationCompleted:
360 /* Unreachable code because if it's started a file is open. */
361 return false;
362 case UpdateState::updatePending:
363 {
364 /* When in this state, they can only open the updateBlobId */
365 if (path == updateBlobId)
366 {
367 updateImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700368
369 lookup[session] = &updateImage;
370
Patrick Venture723113f2019-06-05 09:38:35 -0700371 return true;
372 }
373 else
374 {
375 return false;
376 }
377 }
378 case UpdateState::updateStarted:
379 case UpdateState::updateCompleted:
380 /* Unreachable code because if it's started a file is open. */
381 break;
382 default:
383 break;
384 }
385
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700386 /* To support multiple firmware options, we need to make sure they're
387 * opening the one they already opened during this update sequence, or it's
388 * the first time they're opening it.
389 */
390 if (path != hashBlobId)
391 {
392 /* If they're not opening the hashBlobId they must be opening a firmware
393 * handler.
394 */
395 if (openedFirmwareType.empty())
396 {
397 /* First time for this sequence. */
398 openedFirmwareType = path;
399 }
400 else
401 {
402 if (openedFirmwareType != path)
403 {
404 /* Previously, in this sequence they opened /flash/image, and
405 * now they're opening /flash/bios without finishing out
406 * /flash/image (for example).
407 */
408 std::fprintf(stderr, "Trying to open alternate firmware while "
409 "unfinished with other firmware.\n");
410 return false;
411 }
412 }
413 }
414
Patrick Venture723113f2019-06-05 09:38:35 -0700415 /* There are two abstractions at play, how you get the data and how you
416 * handle that data. such that, whether the data comes from the PCI bridge
417 * or LPC bridge is not connected to whether the data goes into a static
418 * layout flash update or a UBI tarball.
419 */
420
Benjamin Fair12901982019-11-12 13:55:46 -0800421 std::uint16_t transportFlag = flags & transportMask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800422
Patrick Venture18235e62018-11-08 10:21:09 -0800423 /* How are they expecting to copy this data? */
Benjamin Fair12901982019-11-12 13:55:46 -0800424 auto d = std::find_if(transports.begin(), transports.end(),
425 [&transportFlag](const auto& iter) {
426 return (iter.bitmask == transportFlag);
427 });
Patrick Venture18235e62018-11-08 10:21:09 -0800428 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800429 {
Patrick Venture18235e62018-11-08 10:21:09 -0800430 return false;
431 }
432
Benjamin Fair12901982019-11-12 13:55:46 -0800433 /* We found the transport handler they requested */
Patrick Venturec02849b2018-11-06 17:31:15 -0800434
Patrick Venture6e307a72018-11-09 18:21:17 -0800435 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
436 * only non-external data pathway -- but this is just a more generic
437 * approach to that.
438 */
439 if (d->handler)
440 {
441 /* If the data handler open call fails, open fails. */
442 if (!d->handler->open())
443 {
444 return false;
445 }
446 }
447
Patrick Venture70e30bf2019-01-17 10:29:28 -0800448 /* Do we have a file handler for the type of file they're opening.
449 * Note: This should only fail if something is somehow crazy wrong.
450 * Since the canHandle() said yes, and that's tied into the list of explicit
451 * firmware handers (and file handlers, like this'll know where to write the
452 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800453 */
Patrick Venture18235e62018-11-08 10:21:09 -0800454 auto h = std::find_if(
455 handlers.begin(), handlers.end(),
456 [&path](const auto& iter) { return (iter.blobName == path); });
457 if (h == handlers.end())
458 {
459 return false;
460 }
461
462 /* Ok, so we found a handler that matched, so call open() */
463 if (!h->handler->open(path))
464 {
465 return false;
466 }
467
Patrick Venture70e30bf2019-01-17 10:29:28 -0800468 Session* curr;
469 const std::string* active;
470
Patrick Venture7dad86f2019-05-17 08:52:20 -0700471 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800472 {
473 /* 2c) are they opening the /flash/hash ? (to start the process) */
474 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700475 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800476 }
477 else
478 {
479 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700480 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800481 }
482
Patrick Venture18235e62018-11-08 10:21:09 -0800483 curr->flags = flags;
484 curr->dataHandler = d->handler;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700485 curr->imageHandler = h->handler.get();
Patrick Venture18235e62018-11-08 10:21:09 -0800486
487 lookup[session] = curr;
488
Patrick Ventureefba42d2019-05-24 10:48:16 -0700489 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700490 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800491
Patrick Venture75c0c272019-06-21 09:15:08 -0700492 changeState(UpdateState::uploadInProgress);
Patrick Venture991910a2018-11-09 19:43:48 -0800493
Patrick Venture18235e62018-11-08 10:21:09 -0800494 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700495}
Patrick Venture53977962018-11-02 18:59:35 -0700496
Patrick Venture18235e62018-11-08 10:21:09 -0800497/**
498 * The write command really just grabs the data from wherever it is and sends it
499 * to the image handler. It's the image handler's responsibility to deal with
500 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800501 *
502 * This receives a session from the blob manager, therefore it is always called
503 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800504 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700505bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
506 const std::vector<uint8_t>& data)
507{
Patrick Venture18235e62018-11-08 10:21:09 -0800508 auto item = lookup.find(session);
509 if (item == lookup.end())
510 {
511 return false;
512 }
513
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800514 /* Prevent writing during verification. */
515 if (state == UpdateState::verificationStarted)
516 {
517 return false;
518 }
519
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700520 /* Prevent writing to the verification or update blobs. */
521 if (item->second->activePath == verifyBlobId ||
522 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700523 {
524 return false;
525 }
Patrick Venture699750d2019-05-15 09:24:09 -0700526
Patrick Venture18235e62018-11-08 10:21:09 -0800527 std::vector<std::uint8_t> bytes;
528
Patrick Venture84778b82019-06-26 20:11:09 -0700529 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800530 {
531 bytes = data;
532 }
533 else
534 {
535 /* little endian required per design, and so on, but TODO: do endianness
536 * with boost.
537 */
538 struct ExtChunkHdr header;
539
540 if (data.size() != sizeof(header))
541 {
542 return false;
543 }
544
545 std::memcpy(&header, data.data(), data.size());
546 bytes = item->second->dataHandler->copyFrom(header.length);
547 }
548
549 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700550}
Patrick Venture18235e62018-11-08 10:21:09 -0800551
Patrick Venture8c535332018-11-08 15:58:00 -0800552/*
553 * If the active session (image or hash) is over LPC, this allows
554 * configuring it. This option is only available before you start
555 * writing data for the given item (image or hash). This will return
556 * false at any other part. -- the lpc handler portion will know to return
557 * false.
558 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700559bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
560 const std::vector<uint8_t>& data)
561{
Patrick Venture8c535332018-11-08 15:58:00 -0800562 auto item = lookup.find(session);
563 if (item == lookup.end())
564 {
565 return false;
566 }
567
Patrick Venture84778b82019-06-26 20:11:09 -0700568 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800569 {
570 return false;
571 }
572
Patrick Ventured56097a2019-05-15 09:47:55 -0700573 /* Prevent writing meta to the verification blob (it has no data handler).
574 */
575 if (item->second->dataHandler)
576 {
577 return item->second->dataHandler->writeMeta(data);
578 }
Patrick Venture699750d2019-05-15 09:24:09 -0700579
Patrick Ventured56097a2019-05-15 09:47:55 -0700580 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700581}
Patrick Venture8c535332018-11-08 15:58:00 -0800582
Patrick Ventured6461d62018-11-09 17:30:25 -0800583/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700584 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800585 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800586 * the image.
587 *
588 * For this file to have opened, the other two must be closed, which means any
589 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800590 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700591bool FirmwareBlobHandler::commit(uint16_t session,
592 const std::vector<uint8_t>& data)
593{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800594 auto item = lookup.find(session);
595 if (item == lookup.end())
596 {
597 return false;
598 }
599
Patrick Venture1a406fe2019-05-31 07:29:56 -0700600 /* You can only commit on the verifyBlodId or updateBlobId */
601 if (item->second->activePath != verifyBlobId &&
602 item->second->activePath != updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800603 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700604 std::fprintf(stderr, "path: '%s' not expected for commit\n",
605 item->second->activePath.c_str());
Patrick Ventureffcc5502018-11-16 12:32:38 -0800606 return false;
607 }
608
Patrick Venture433cbc32019-05-30 09:53:10 -0700609 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800610 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700611 case UpdateState::verificationPending:
612 /* Set state to committing. */
613 item->second->flags |= blobs::StateFlags::committing;
614 return triggerVerification();
Patrick Venture433cbc32019-05-30 09:53:10 -0700615 case UpdateState::verificationStarted:
616 /* Calling repeatedly has no effect within an update process. */
617 return true;
618 case UpdateState::verificationCompleted:
619 /* Calling after the verification process has completed returns
620 * failure. */
621 return false;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700622 case UpdateState::updatePending:
Patrick Venture433cbc32019-05-30 09:53:10 -0700623 item->second->flags |= blobs::StateFlags::committing;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700624 return triggerUpdate();
Patrick Venture0079d892019-05-31 11:27:44 -0700625 case UpdateState::updateStarted:
626 /* Calling repeatedly has no effect within an update process. */
627 return true;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700628 default:
629 return false;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800630 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700631}
Patrick Ventured6461d62018-11-09 17:30:25 -0800632
633/*
634 * Close must be called on the firmware image before triggering
635 * verification via commit. Once the verification is complete, you can
636 * then close the hash file.
637 *
638 * If the `verify_image.service` returned success, closing the hash file
639 * will have a specific behavior depending on the update. If it's UBI,
640 * it'll perform the install. If it's static layout, it'll do nothing. The
641 * verify_image service in the static layout case is responsible for placing
642 * the file in the correct staging position.
643 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700644bool FirmwareBlobHandler::close(uint16_t session)
645{
Patrick Venture68bb1432018-11-09 20:08:48 -0800646 auto item = lookup.find(session);
647 if (item == lookup.end())
648 {
649 return false;
650 }
651
Patrick Venturee95dbb62019-06-05 09:59:29 -0700652 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800653 {
Patrick Venturee95dbb62019-06-05 09:59:29 -0700654 case UpdateState::uploadInProgress:
655 /* They are closing a data pathway (image, tarball, hash). */
Patrick Venture75c0c272019-06-21 09:15:08 -0700656 changeState(UpdateState::verificationPending);
Patrick Venture85ae86b2019-06-05 09:18:40 -0700657
Patrick Venture1999eef2019-07-01 11:44:09 -0700658 /* Add verify blob ID now that we can expect it, IIF they also wrote
659 * some data.
660 */
661 if (std::count(blobIDs.begin(), blobIDs.end(), activeImageBlobId))
662 {
663 addBlobId(verifyBlobId);
664 }
Patrick Venturee95dbb62019-06-05 09:59:29 -0700665 break;
666 case UpdateState::verificationPending:
667 /* They haven't triggered, therefore closing is uninteresting.
668 */
669 break;
670 case UpdateState::verificationStarted:
Patrick Ventured5741022019-06-17 09:08:35 -0700671 /* Abort without checking to see if it happened to finish. Require
672 * the caller to stat() deliberately.
Patrick Venturee95dbb62019-06-05 09:59:29 -0700673 */
Patrick Ventured5741022019-06-17 09:08:35 -0700674 abortVerification();
675 abortProcess();
676 break;
Patrick Venturee95dbb62019-06-05 09:59:29 -0700677 case UpdateState::verificationCompleted:
678 if (lastVerificationStatus == ActionStatus::success)
679 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700680 changeState(UpdateState::updatePending);
Patrick Venturee95dbb62019-06-05 09:59:29 -0700681 addBlobId(updateBlobId);
682 removeBlobId(verifyBlobId);
683 }
684 else
685 {
Patrick Venture5d9fa022019-06-17 13:13:30 -0700686 /* Verification failed, and the host-tool knows this by calling
687 * stat(), which triggered the state change to
688 * verificationCompleted.
689 *
690 * Therefore, let's abort the process at this point.
691 */
692 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700693 }
694 break;
695 case UpdateState::updatePending:
696 /* They haven't triggered the update, therefore this is
697 * uninteresting. */
698 break;
699 case UpdateState::updateStarted:
Patrick Venture49731712019-06-17 10:04:02 -0700700 /* Abort without checking to see if it happened to finish. Require
701 * the caller to stat() deliberately.
702 */
703 abortUpdate();
704 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700705 break;
706 case UpdateState::updateCompleted:
707 if (lastUpdateStatus == ActionStatus::failed)
708 {
709 /* TODO: lOG something? */
Patrick Venture4c7a4202019-06-17 13:06:55 -0700710 std::fprintf(stderr, "Update failed\n");
Patrick Venturee95dbb62019-06-05 09:59:29 -0700711 }
Patrick Venture930c7b72019-05-24 11:11:08 -0700712
Patrick Venture4c7a4202019-06-17 13:06:55 -0700713 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700714 break;
715 default:
716 break;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800717 }
718
Brandon Kima9f674c2019-10-18 15:18:49 -0700719 if (!lookup.empty())
Patrick Venture68bb1432018-11-09 20:08:48 -0800720 {
Brandon Kima9f674c2019-10-18 15:18:49 -0700721 if (item->second->dataHandler)
722 {
723 item->second->dataHandler->close();
724 }
725 if (item->second->imageHandler)
726 {
727 item->second->imageHandler->close();
728 }
729 lookup.erase(item);
Patrick Venture68bb1432018-11-09 20:08:48 -0800730 }
Patrick Venture68bb1432018-11-09 20:08:48 -0800731 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700732}
Patrick Ventured6461d62018-11-09 17:30:25 -0800733
Patrick Venture75c0c272019-06-21 09:15:08 -0700734void FirmwareBlobHandler::changeState(UpdateState next)
735{
736 state = next;
Patrick Venture6d7735d2019-06-21 10:03:19 -0700737
738 if (state == UpdateState::notYetStarted)
739 {
740 /* Going back to notyetstarted, let them trigger preparation again. */
741 preparationTriggered = false;
742 }
743 else if (state == UpdateState::uploadInProgress)
744 {
745 /* Store this transition logic here instead of ::open() */
746 if (!preparationTriggered)
747 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700748 auto* pack = getActionPack();
749 if (pack)
750 {
751 pack->preparation->trigger();
752 preparationTriggered = true;
753 }
Patrick Venture6d7735d2019-06-21 10:03:19 -0700754 }
755 }
Patrick Venture75c0c272019-06-21 09:15:08 -0700756}
757
Patrick Venturec7ca2912018-11-02 11:38:33 -0700758bool FirmwareBlobHandler::expire(uint16_t session)
759{
760 return false;
761}
Patrick Ventured6461d62018-11-09 17:30:25 -0800762
763/*
764 * Currently, the design does not provide this with a function, however,
765 * it will likely change to support reading data back.
766 */
767std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
768 uint32_t offset,
769 uint32_t requestedSize)
770{
771 return {};
772}
773
Patrick Ventured5741022019-06-17 09:08:35 -0700774void FirmwareBlobHandler::abortProcess()
775{
776 /* Closing of open files is handled from close() -- Reaching here from
777 * delete may never be supported.
778 */
779 removeBlobId(verifyBlobId);
780 removeBlobId(updateBlobId);
781 removeBlobId(activeImageBlobId);
782 removeBlobId(activeHashBlobId);
783
Brandon Kima9f674c2019-10-18 15:18:49 -0700784 for (auto item : lookup)
785 {
786 if (item.second->dataHandler)
787 {
788 item.second->dataHandler->close();
789 }
790 if (item.second->imageHandler)
791 {
792 item.second->imageHandler->close();
793 }
794 }
795 lookup.clear();
796
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700797 openedFirmwareType = "";
Patrick Venture75c0c272019-06-21 09:15:08 -0700798 changeState(UpdateState::notYetStarted);
Patrick Ventured5741022019-06-17 09:08:35 -0700799}
800
801void FirmwareBlobHandler::abortVerification()
802{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700803 auto* pack = getActionPack();
804 if (pack)
805 {
806 pack->verification->abort();
807 }
Patrick Ventured5741022019-06-17 09:08:35 -0700808}
809
Patrick Ventureffcc5502018-11-16 12:32:38 -0800810bool FirmwareBlobHandler::triggerVerification()
811{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700812 auto* pack = getActionPack();
813 if (!pack)
814 {
815 return false;
816 }
817
818 bool result = pack->verification->trigger();
Patrick Venture3ecb3502019-05-17 11:03:51 -0700819 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800820 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700821 changeState(UpdateState::verificationStarted);
Patrick Venturecabc1172018-11-16 16:14:26 -0800822 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800823
Patrick Venture3ecb3502019-05-17 11:03:51 -0700824 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800825}
826
Patrick Venture49731712019-06-17 10:04:02 -0700827void FirmwareBlobHandler::abortUpdate()
828{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700829 auto* pack = getActionPack();
830 if (pack)
831 {
832 pack->update->abort();
833 }
Patrick Venture49731712019-06-17 10:04:02 -0700834}
835
Patrick Venture1a406fe2019-05-31 07:29:56 -0700836bool FirmwareBlobHandler::triggerUpdate()
837{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700838 auto* pack = getActionPack();
839 if (!pack)
840 {
841 return false;
842 }
843
844 bool result = pack->update->trigger();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700845 if (result)
846 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700847 changeState(UpdateState::updateStarted);
Patrick Venture1a406fe2019-05-31 07:29:56 -0700848 }
849
850 return result;
851}
852
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700853} // namespace ipmi_flash