blob: 9b35e5f64d7170277be11383890141c4432e3d55 [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 Venture137ad2c2018-11-06 11:30:43 -080025#include <algorithm>
Patrick Venture9efef5d2019-06-19 08:45:44 -070026#include <blobs-ipmid/blobs.hpp>
Patrick Venture192d60f2018-11-06 11:11:59 -080027#include <cstdint>
Patrick Venture18235e62018-11-08 10:21:09 -080028#include <cstring>
Patrick Ventureb3b4db72019-05-15 11:30:24 -070029#include <fstream>
Patrick Venture68cf64f2018-11-06 10:46:51 -080030#include <memory>
Patrick Ventured333a872018-12-03 16:24:26 -080031#include <phosphor-logging/log.hpp>
Patrick Venturefa6c4d92018-11-02 18:34:53 -070032#include <string>
33#include <vector>
34
Patrick Ventured333a872018-12-03 16:24:26 -080035using namespace phosphor::logging;
36
Patrick Venture1d5a31c2019-05-20 11:38:22 -070037namespace ipmi_flash
Patrick Venturec7ca2912018-11-02 11:38:33 -070038{
Patrick Ventureb3b4db72019-05-15 11:30:24 -070039
Patrick Venture1d5a31c2019-05-20 11:38:22 -070040std::unique_ptr<blobs::GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080041 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventured4e20de2019-07-18 12:48:05 -070042 std::vector<HandlerPack>&& firmwares,
Patrick Venturefa06a5f2019-07-01 09:22:38 -070043 const std::vector<DataHandlerPack>& transports, ActionMap&& actionPacks)
Patrick Venture68cf64f2018-11-06 10:46:51 -080044{
Patrick Venture16265382019-11-06 19:31:36 -080045 /* There must be at least one in addition to the hash blob handler. */
46 if (firmwares.size() < 2)
Patrick Venture52854622018-11-06 12:30:00 -080047 {
Patrick Venture16265382019-11-06 19:31:36 -080048 log<level::ERR>("Must provide at least two firmware handlers.");
Patrick Venture52854622018-11-06 12:30:00 -080049 return nullptr;
50 }
Patrick Venture4e2a1432019-11-06 20:06:07 -080051 if (transports.empty())
Patrick Venture1cde5f92018-11-07 08:26:47 -080052 {
53 return nullptr;
54 }
Patrick Venture16265382019-11-06 19:31:36 -080055 if (actionPacks.empty())
56 {
57 return nullptr;
58 }
Patrick Venture52854622018-11-06 12:30:00 -080059
Patrick Venturea78e39f2018-11-06 18:37:06 -080060 std::vector<std::string> blobs;
61 for (const auto& item : firmwares)
62 {
63 blobs.push_back(item.blobName);
64 }
Patrick Venture18235e62018-11-08 10:21:09 -080065
Patrick Venture7dad86f2019-05-17 08:52:20 -070066 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId))
Patrick Venture18235e62018-11-08 10:21:09 -080067 {
68 return nullptr;
69 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080070
Benjamin Fair12901982019-11-12 13:55:46 -080071 return std::make_unique<FirmwareBlobHandler>(
72 std::move(firmwares), blobs, transports, std::move(actionPacks));
Patrick Venture68cf64f2018-11-06 10:46:51 -080073}
74
Patrick Ventured6461d62018-11-09 17:30:25 -080075/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070076bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
77{
Patrick Venture6032dc02019-05-17 11:01:44 -070078 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070079}
Patrick Venture53977962018-11-02 18:59:35 -070080
Patrick Ventured6461d62018-11-09 17:30:25 -080081/*
82 * Grab the list of supported firmware.
83 *
84 * If there's an open firmware session, it'll already be present in the
85 * list as "/flash/active/image", and if the hash has started,
86 * "/flash/active/hash" regardless of mechanism. This is done in the open
87 * comamnd, no extra work is required here.
88 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070089std::vector<std::string> FirmwareBlobHandler::getBlobIds()
90{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080091 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070092}
Patrick Venture53977962018-11-02 18:59:35 -070093
Patrick Ventured6461d62018-11-09 17:30:25 -080094/*
95 * Per the design, this mean abort, and this will trigger whatever
96 * appropriate actions are required to abort the process.
97 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070098bool FirmwareBlobHandler::deleteBlob(const std::string& path)
99{
Patrick Venture72676762019-06-17 11:22:38 -0700100 switch (state)
101 {
102 case UpdateState::notYetStarted:
103 /* Trying to delete anything at this point has no effect and returns
104 * false.
105 */
106 return false;
107 case UpdateState::verificationPending:
Patrick Venture2ca66522019-06-17 11:58:38 -0700108 abortProcess();
109 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700110 case UpdateState::updatePending:
Patrick Venturec9f62392019-06-17 12:17:26 -0700111 abortProcess();
112 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700113 default:
114 break;
115 }
116
Patrick Venturebcc0c772019-06-17 10:42:06 -0700117 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700118}
Patrick Venture53977962018-11-02 18:59:35 -0700119
Patrick Ventured6461d62018-11-09 17:30:25 -0800120/*
121 * Stat on the files will return information such as what supported
122 * transport mechanisms are available.
123 *
124 * Stat on an active file or hash will return information such as the size
125 * of the data cached, and any additional pertinent information. The
126 * blob_state on the active files will return the state of the update.
127 */
Patrick Venturee312f392019-06-04 07:44:37 -0700128bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700129{
Patrick Venture46637c82018-11-06 15:20:24 -0800130 /* We know we support this path because canHandle is called ahead */
Patrick Ventured342a952019-05-29 09:09:10 -0700131 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700132 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800133 {
Patrick Ventured342a952019-05-29 09:09:10 -0700134 /* These blobs are placeholders that indicate things, or allow actions,
135 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800136 */
Patrick Ventured342a952019-05-29 09:09:10 -0700137 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800138 }
139
Patrick Ventured342a952019-05-29 09:09:10 -0700140 /* They are requesting information about the generic blob_id. */
Patrick Ventured342a952019-05-29 09:09:10 -0700141
Benjamin Fair12901982019-11-12 13:55:46 -0800142 /* Older host tools expect the blobState to contain a bitmask of available
143 * transport backends, so report that we support all of them in order to
144 * preserve backwards compatibility.
Patrick Ventured342a952019-05-29 09:09:10 -0700145 */
Benjamin Fair12901982019-11-12 13:55:46 -0800146 meta->blobState = transportMask;
147 meta->size = 0;
Patrick Ventured342a952019-05-29 09:09:10 -0700148 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700149}
Patrick Venture53977962018-11-02 18:59:35 -0700150
Patrick Ventureda66fd82019-06-03 11:11:24 -0700151ActionStatus FirmwareBlobHandler::getActionStatus()
Patrick Venturea2d82392019-06-03 10:55:17 -0700152{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700153 ActionStatus value = ActionStatus::unknown;
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700154 auto* pack = getActionPack();
Patrick Venturea2d82392019-06-03 10:55:17 -0700155
156 switch (state)
157 {
158 case UpdateState::verificationPending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700159 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700160 break;
161 case UpdateState::verificationStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700162 /* If we got here, there must be data AND a hash, not just a hash,
163 * therefore pack will be known. */
164 if (!pack)
165 {
166 break;
167 }
168 value = pack->verification->status();
Patrick Ventureda66fd82019-06-03 11:11:24 -0700169 lastVerificationStatus = value;
Patrick Venturea2d82392019-06-03 10:55:17 -0700170 break;
171 case UpdateState::verificationCompleted:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700172 value = lastVerificationStatus;
Patrick Venturea2d82392019-06-03 10:55:17 -0700173 break;
Patrick Venturea2d82392019-06-03 10:55:17 -0700174 case UpdateState::updatePending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700175 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700176 break;
177 case UpdateState::updateStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700178 if (!pack)
179 {
180 break;
181 }
182 value = pack->update->status();
Patrick Venturea2d82392019-06-03 10:55:17 -0700183 lastUpdateStatus = value;
184 break;
185 case UpdateState::updateCompleted:
186 value = lastUpdateStatus;
187 break;
188 default:
189 break;
190 }
191
192 return value;
193}
194
Patrick Venturec02849b2018-11-06 17:31:15 -0800195/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800196 * Return stat information on an open session. It therefore must be an active
197 * handle to either the active image or active hash.
Patrick Ventured6461d62018-11-09 17:30:25 -0800198 */
Patrick Venturee312f392019-06-04 07:44:37 -0700199bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800200{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800201 auto item = lookup.find(session);
202 if (item == lookup.end())
203 {
204 return false;
205 }
206
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700207 /* The size here refers to the size of the file -- of something analagous.
208 */
209 meta->size = (item->second->imageHandler)
210 ? item->second->imageHandler->getSize()
211 : 0;
212
213 meta->metadata.clear();
214
Patrick Ventureda66fd82019-06-03 11:11:24 -0700215 if (item->second->activePath == verifyBlobId ||
216 item->second->activePath == updateBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700217 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700218 ActionStatus value = getActionStatus();
Patrick Venture699750d2019-05-15 09:24:09 -0700219
Patrick Venturee955e072019-05-15 16:16:46 -0700220 meta->metadata.push_back(static_cast<std::uint8_t>(value));
221
222 /* Change the firmware handler's state and the blob's stat value
223 * depending.
224 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700225 if (value == ActionStatus::success || value == ActionStatus::failed)
Patrick Venturee955e072019-05-15 16:16:46 -0700226 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700227 if (item->second->activePath == verifyBlobId)
Patrick Venturee955e072019-05-15 16:16:46 -0700228 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700229 changeState(UpdateState::verificationCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700230 }
231 else
232 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700233 /* item->second->activePath == updateBlobId */
Patrick Venture75c0c272019-06-21 09:15:08 -0700234 changeState(UpdateState::updateCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700235 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700236
Patrick Venturef1f0f652019-06-03 09:10:19 -0700237 item->second->flags &= ~blobs::StateFlags::committing;
238
Patrick Ventureda66fd82019-06-03 11:11:24 -0700239 if (value == ActionStatus::success)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700240 {
241 item->second->flags |= blobs::StateFlags::committed;
242 }
243 else
244 {
245 item->second->flags |= blobs::StateFlags::commit_error;
246 }
247 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700248 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800249
Patrick Venturee955e072019-05-15 16:16:46 -0700250 /* The blobState here relates to an active sesion, so we should return the
251 * flags used to open this session.
252 */
253 meta->blobState = item->second->flags;
254
Patrick Venturecc7d1602018-11-15 13:58:33 -0800255 /* The metadata blob returned comes from the data handler... it's used for
256 * instance, in P2A bridging to get required information about the mapping,
257 * and is the "opposite" of the lpc writemeta requirement.
258 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800259 if (item->second->dataHandler)
260 {
Patrick Venture74304642019-01-17 09:31:04 -0800261 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800262 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
263 bytes.end());
264 }
265
Patrick Venturecc7d1602018-11-15 13:58:33 -0800266 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800267}
268
269/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800270 * If you open /flash/image or /flash/tarball, or /flash/hash it will
271 * interpret the open flags and perform whatever actions are required for
272 * that update process. The session returned can be used immediately for
273 * sending data down, without requiring one to open the new active file.
274 *
275 * If you open the active flash image or active hash it will let you
276 * overwrite pieces, depending on the state.
277 *
278 * Once the verification process has started the active files cannot be
279 * opened.
280 *
281 * You can only have one open session at a time. Which means, you can only
282 * have one file open at a time. Trying to open the hash blob_id while you
283 * still have the flash image blob_id open will fail. Opening the flash
284 * blob_id when it is already open will fail.
285 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700286bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
287 const std::string& path)
288{
Patrick Venturec02849b2018-11-06 17:31:15 -0800289 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800290 *
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800291 * Further on this, if there's an active session to the hash we don't allow
292 * re-opening the image, and if we have the image open, we don't allow
293 * opening the hash. This design decision may be re-evaluated, and changed
294 * to only allow one session per object type (of the two types). But,
295 * consider if the hash is open, do we want to allow writing to the image?
296 * And why would we? But, really, the point of no-return is once the
297 * verification process has begun -- which is done via commit() on the hash
298 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700299 */
Brandon Kim8fc60872019-10-18 15:15:50 -0700300 if (fileOpen())
Patrick Venturec02849b2018-11-06 17:31:15 -0800301 {
302 return false;
303 }
304
Patrick Venture723113f2019-06-05 09:38:35 -0700305 /* The active blobs are only meant to indicate status that something has
306 * opened the image file or the hash file.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800307 */
Patrick Venture19f5d882019-05-30 14:34:55 -0700308 if (path == activeImageBlobId || path == activeHashBlobId)
309 {
310 /* 2a) are they opening the active image? this can only happen if they
311 * already started one (due to canHandleBlob's behavior).
312 */
313 /* 2b) are they opening the active hash? this can only happen if they
314 * already started one (due to canHandleBlob's behavior).
315 */
316 return false;
317 }
318
Patrick Venture723113f2019-06-05 09:38:35 -0700319 /* Check that they've opened for writing - read back not currently
320 * supported.
321 */
322 if ((flags & blobs::OpenFlags::write) == 0)
323 {
324 return false;
325 }
326
327 /* Because canHandleBlob is called before open, we know that if they try to
328 * open the verifyBlobId, they're in a state where it's present.
329 */
330
331 switch (state)
332 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700333 case UpdateState::notYetStarted:
334 /* Only hashBlobId and firmware BlobIds present. */
335 break;
Patrick Venture723113f2019-06-05 09:38:35 -0700336 case UpdateState::uploadInProgress:
337 /* Unreachable code because if it's started a file is open. */
338 break;
339 case UpdateState::verificationPending:
340 /* Handle opening the verifyBlobId --> we know the image and hash
Brandon Kim8fc60872019-10-18 15:15:50 -0700341 * aren't open because of the fileOpen() check. They can still open
Patrick Venture723113f2019-06-05 09:38:35 -0700342 * other files from this state to transition back into
343 * uploadInProgress.
344 *
345 * The file must be opened for writing, but no transport mechanism
346 * specified since it's irrelevant.
347 */
348 if (path == verifyBlobId)
349 {
350 verifyImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700351
352 lookup[session] = &verifyImage;
353
Patrick Venture723113f2019-06-05 09:38:35 -0700354 return true;
355 }
356 break;
357 case UpdateState::verificationStarted:
358 case UpdateState::verificationCompleted:
359 /* Unreachable code because if it's started a file is open. */
360 return false;
361 case UpdateState::updatePending:
362 {
363 /* When in this state, they can only open the updateBlobId */
364 if (path == updateBlobId)
365 {
366 updateImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700367
368 lookup[session] = &updateImage;
369
Patrick Venture723113f2019-06-05 09:38:35 -0700370 return true;
371 }
372 else
373 {
374 return false;
375 }
376 }
377 case UpdateState::updateStarted:
378 case UpdateState::updateCompleted:
379 /* Unreachable code because if it's started a file is open. */
380 break;
381 default:
382 break;
383 }
384
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700385 /* To support multiple firmware options, we need to make sure they're
386 * opening the one they already opened during this update sequence, or it's
387 * the first time they're opening it.
388 */
389 if (path != hashBlobId)
390 {
391 /* If they're not opening the hashBlobId they must be opening a firmware
392 * handler.
393 */
394 if (openedFirmwareType.empty())
395 {
396 /* First time for this sequence. */
397 openedFirmwareType = path;
398 }
399 else
400 {
401 if (openedFirmwareType != path)
402 {
403 /* Previously, in this sequence they opened /flash/image, and
404 * now they're opening /flash/bios without finishing out
405 * /flash/image (for example).
406 */
407 std::fprintf(stderr, "Trying to open alternate firmware while "
408 "unfinished with other firmware.\n");
409 return false;
410 }
411 }
412 }
413
Patrick Venture723113f2019-06-05 09:38:35 -0700414 /* There are two abstractions at play, how you get the data and how you
415 * handle that data. such that, whether the data comes from the PCI bridge
416 * or LPC bridge is not connected to whether the data goes into a static
417 * layout flash update or a UBI tarball.
418 */
419
Benjamin Fair12901982019-11-12 13:55:46 -0800420 std::uint16_t transportFlag = flags & transportMask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800421
Patrick Venture18235e62018-11-08 10:21:09 -0800422 /* How are they expecting to copy this data? */
Benjamin Fair12901982019-11-12 13:55:46 -0800423 auto d = std::find_if(transports.begin(), transports.end(),
424 [&transportFlag](const auto& iter) {
425 return (iter.bitmask == transportFlag);
426 });
Patrick Venture18235e62018-11-08 10:21:09 -0800427 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800428 {
Patrick Venture18235e62018-11-08 10:21:09 -0800429 return false;
430 }
431
Benjamin Fair12901982019-11-12 13:55:46 -0800432 /* We found the transport handler they requested */
Patrick Venturec02849b2018-11-06 17:31:15 -0800433
Patrick Venture6e307a72018-11-09 18:21:17 -0800434 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
435 * only non-external data pathway -- but this is just a more generic
436 * approach to that.
437 */
438 if (d->handler)
439 {
440 /* If the data handler open call fails, open fails. */
441 if (!d->handler->open())
442 {
443 return false;
444 }
445 }
446
Patrick Venture70e30bf2019-01-17 10:29:28 -0800447 /* Do we have a file handler for the type of file they're opening.
448 * Note: This should only fail if something is somehow crazy wrong.
449 * Since the canHandle() said yes, and that's tied into the list of explicit
450 * firmware handers (and file handlers, like this'll know where to write the
451 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800452 */
Patrick Venture18235e62018-11-08 10:21:09 -0800453 auto h = std::find_if(
454 handlers.begin(), handlers.end(),
455 [&path](const auto& iter) { return (iter.blobName == path); });
456 if (h == handlers.end())
457 {
458 return false;
459 }
460
461 /* Ok, so we found a handler that matched, so call open() */
462 if (!h->handler->open(path))
463 {
464 return false;
465 }
466
Patrick Venture70e30bf2019-01-17 10:29:28 -0800467 Session* curr;
468 const std::string* active;
469
Patrick Venture7dad86f2019-05-17 08:52:20 -0700470 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800471 {
472 /* 2c) are they opening the /flash/hash ? (to start the process) */
473 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700474 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800475 }
476 else
477 {
478 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700479 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800480 }
481
Patrick Venture18235e62018-11-08 10:21:09 -0800482 curr->flags = flags;
483 curr->dataHandler = d->handler;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700484 curr->imageHandler = h->handler.get();
Patrick Venture18235e62018-11-08 10:21:09 -0800485
486 lookup[session] = curr;
487
Patrick Ventureefba42d2019-05-24 10:48:16 -0700488 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700489 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800490
Patrick Venture75c0c272019-06-21 09:15:08 -0700491 changeState(UpdateState::uploadInProgress);
Patrick Venture991910a2018-11-09 19:43:48 -0800492
Patrick Venture18235e62018-11-08 10:21:09 -0800493 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700494}
Patrick Venture53977962018-11-02 18:59:35 -0700495
Patrick Venture18235e62018-11-08 10:21:09 -0800496/**
497 * The write command really just grabs the data from wherever it is and sends it
498 * to the image handler. It's the image handler's responsibility to deal with
499 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800500 *
501 * This receives a session from the blob manager, therefore it is always called
502 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800503 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700504bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
505 const std::vector<uint8_t>& data)
506{
Patrick Venture18235e62018-11-08 10:21:09 -0800507 auto item = lookup.find(session);
508 if (item == lookup.end())
509 {
510 return false;
511 }
512
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800513 /* Prevent writing during verification. */
514 if (state == UpdateState::verificationStarted)
515 {
516 return false;
517 }
518
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700519 /* Prevent writing to the verification or update blobs. */
520 if (item->second->activePath == verifyBlobId ||
521 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700522 {
523 return false;
524 }
Patrick Venture699750d2019-05-15 09:24:09 -0700525
Patrick Venture18235e62018-11-08 10:21:09 -0800526 std::vector<std::uint8_t> bytes;
527
Patrick Venture84778b82019-06-26 20:11:09 -0700528 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800529 {
530 bytes = data;
531 }
532 else
533 {
534 /* little endian required per design, and so on, but TODO: do endianness
535 * with boost.
536 */
537 struct ExtChunkHdr header;
538
539 if (data.size() != sizeof(header))
540 {
541 return false;
542 }
543
544 std::memcpy(&header, data.data(), data.size());
545 bytes = item->second->dataHandler->copyFrom(header.length);
546 }
547
548 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700549}
Patrick Venture18235e62018-11-08 10:21:09 -0800550
Patrick Venture8c535332018-11-08 15:58:00 -0800551/*
552 * If the active session (image or hash) is over LPC, this allows
553 * configuring it. This option is only available before you start
554 * writing data for the given item (image or hash). This will return
555 * false at any other part. -- the lpc handler portion will know to return
556 * false.
557 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700558bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
559 const std::vector<uint8_t>& data)
560{
Patrick Venture8c535332018-11-08 15:58:00 -0800561 auto item = lookup.find(session);
562 if (item == lookup.end())
563 {
564 return false;
565 }
566
Patrick Venture84778b82019-06-26 20:11:09 -0700567 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800568 {
569 return false;
570 }
571
Patrick Ventured56097a2019-05-15 09:47:55 -0700572 /* Prevent writing meta to the verification blob (it has no data handler).
573 */
574 if (item->second->dataHandler)
575 {
576 return item->second->dataHandler->writeMeta(data);
577 }
Patrick Venture699750d2019-05-15 09:24:09 -0700578
Patrick Ventured56097a2019-05-15 09:47:55 -0700579 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700580}
Patrick Venture8c535332018-11-08 15:58:00 -0800581
Patrick Ventured6461d62018-11-09 17:30:25 -0800582/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700583 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800584 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800585 * the image.
586 *
587 * For this file to have opened, the other two must be closed, which means any
588 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800589 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700590bool FirmwareBlobHandler::commit(uint16_t session,
591 const std::vector<uint8_t>& data)
592{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800593 auto item = lookup.find(session);
594 if (item == lookup.end())
595 {
596 return false;
597 }
598
Patrick Venture1a406fe2019-05-31 07:29:56 -0700599 /* You can only commit on the verifyBlodId or updateBlobId */
600 if (item->second->activePath != verifyBlobId &&
601 item->second->activePath != updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800602 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700603 std::fprintf(stderr, "path: '%s' not expected for commit\n",
604 item->second->activePath.c_str());
Patrick Ventureffcc5502018-11-16 12:32:38 -0800605 return false;
606 }
607
Patrick Venture433cbc32019-05-30 09:53:10 -0700608 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800609 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700610 case UpdateState::verificationPending:
611 /* Set state to committing. */
612 item->second->flags |= blobs::StateFlags::committing;
613 return triggerVerification();
Patrick Venture433cbc32019-05-30 09:53:10 -0700614 case UpdateState::verificationStarted:
615 /* Calling repeatedly has no effect within an update process. */
616 return true;
617 case UpdateState::verificationCompleted:
618 /* Calling after the verification process has completed returns
619 * failure. */
620 return false;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700621 case UpdateState::updatePending:
Patrick Venture433cbc32019-05-30 09:53:10 -0700622 item->second->flags |= blobs::StateFlags::committing;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700623 return triggerUpdate();
Patrick Venture0079d892019-05-31 11:27:44 -0700624 case UpdateState::updateStarted:
625 /* Calling repeatedly has no effect within an update process. */
626 return true;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700627 default:
628 return false;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800629 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700630}
Patrick Ventured6461d62018-11-09 17:30:25 -0800631
632/*
633 * Close must be called on the firmware image before triggering
634 * verification via commit. Once the verification is complete, you can
635 * then close the hash file.
636 *
637 * If the `verify_image.service` returned success, closing the hash file
638 * will have a specific behavior depending on the update. If it's UBI,
639 * it'll perform the install. If it's static layout, it'll do nothing. The
640 * verify_image service in the static layout case is responsible for placing
641 * the file in the correct staging position.
642 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700643bool FirmwareBlobHandler::close(uint16_t session)
644{
Patrick Venture68bb1432018-11-09 20:08:48 -0800645 auto item = lookup.find(session);
646 if (item == lookup.end())
647 {
648 return false;
649 }
650
Patrick Venturee95dbb62019-06-05 09:59:29 -0700651 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800652 {
Patrick Venturee95dbb62019-06-05 09:59:29 -0700653 case UpdateState::uploadInProgress:
654 /* They are closing a data pathway (image, tarball, hash). */
Patrick Venture75c0c272019-06-21 09:15:08 -0700655 changeState(UpdateState::verificationPending);
Patrick Venture85ae86b2019-06-05 09:18:40 -0700656
Patrick Venture1999eef2019-07-01 11:44:09 -0700657 /* Add verify blob ID now that we can expect it, IIF they also wrote
658 * some data.
659 */
660 if (std::count(blobIDs.begin(), blobIDs.end(), activeImageBlobId))
661 {
662 addBlobId(verifyBlobId);
663 }
Patrick Venturee95dbb62019-06-05 09:59:29 -0700664 break;
665 case UpdateState::verificationPending:
666 /* They haven't triggered, therefore closing is uninteresting.
667 */
668 break;
669 case UpdateState::verificationStarted:
Patrick Ventured5741022019-06-17 09:08:35 -0700670 /* Abort without checking to see if it happened to finish. Require
671 * the caller to stat() deliberately.
Patrick Venturee95dbb62019-06-05 09:59:29 -0700672 */
Patrick Ventured5741022019-06-17 09:08:35 -0700673 abortVerification();
674 abortProcess();
675 break;
Patrick Venturee95dbb62019-06-05 09:59:29 -0700676 case UpdateState::verificationCompleted:
677 if (lastVerificationStatus == ActionStatus::success)
678 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700679 changeState(UpdateState::updatePending);
Patrick Venturee95dbb62019-06-05 09:59:29 -0700680 addBlobId(updateBlobId);
681 removeBlobId(verifyBlobId);
682 }
683 else
684 {
Patrick Venture5d9fa022019-06-17 13:13:30 -0700685 /* Verification failed, and the host-tool knows this by calling
686 * stat(), which triggered the state change to
687 * verificationCompleted.
688 *
689 * Therefore, let's abort the process at this point.
690 */
691 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700692 }
693 break;
694 case UpdateState::updatePending:
695 /* They haven't triggered the update, therefore this is
696 * uninteresting. */
697 break;
698 case UpdateState::updateStarted:
Patrick Venture49731712019-06-17 10:04:02 -0700699 /* Abort without checking to see if it happened to finish. Require
700 * the caller to stat() deliberately.
701 */
702 abortUpdate();
703 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700704 break;
705 case UpdateState::updateCompleted:
706 if (lastUpdateStatus == ActionStatus::failed)
707 {
708 /* TODO: lOG something? */
Patrick Venture4c7a4202019-06-17 13:06:55 -0700709 std::fprintf(stderr, "Update failed\n");
Patrick Venturee95dbb62019-06-05 09:59:29 -0700710 }
Patrick Venture930c7b72019-05-24 11:11:08 -0700711
Patrick Venture4c7a4202019-06-17 13:06:55 -0700712 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700713 break;
714 default:
715 break;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800716 }
717
Brandon Kima9f674c2019-10-18 15:18:49 -0700718 if (!lookup.empty())
Patrick Venture68bb1432018-11-09 20:08:48 -0800719 {
Brandon Kima9f674c2019-10-18 15:18:49 -0700720 if (item->second->dataHandler)
721 {
722 item->second->dataHandler->close();
723 }
724 if (item->second->imageHandler)
725 {
726 item->second->imageHandler->close();
727 }
728 lookup.erase(item);
Patrick Venture68bb1432018-11-09 20:08:48 -0800729 }
Patrick Venture68bb1432018-11-09 20:08:48 -0800730 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700731}
Patrick Ventured6461d62018-11-09 17:30:25 -0800732
Patrick Venture75c0c272019-06-21 09:15:08 -0700733void FirmwareBlobHandler::changeState(UpdateState next)
734{
735 state = next;
Patrick Venture6d7735d2019-06-21 10:03:19 -0700736
737 if (state == UpdateState::notYetStarted)
738 {
739 /* Going back to notyetstarted, let them trigger preparation again. */
740 preparationTriggered = false;
741 }
742 else if (state == UpdateState::uploadInProgress)
743 {
744 /* Store this transition logic here instead of ::open() */
745 if (!preparationTriggered)
746 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700747 auto* pack = getActionPack();
748 if (pack)
749 {
750 pack->preparation->trigger();
751 preparationTriggered = true;
752 }
Patrick Venture6d7735d2019-06-21 10:03:19 -0700753 }
754 }
Patrick Venture75c0c272019-06-21 09:15:08 -0700755}
756
Patrick Venturec7ca2912018-11-02 11:38:33 -0700757bool FirmwareBlobHandler::expire(uint16_t session)
758{
759 return false;
760}
Patrick Ventured6461d62018-11-09 17:30:25 -0800761
762/*
763 * Currently, the design does not provide this with a function, however,
764 * it will likely change to support reading data back.
765 */
766std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
767 uint32_t offset,
768 uint32_t requestedSize)
769{
770 return {};
771}
772
Patrick Ventured5741022019-06-17 09:08:35 -0700773void FirmwareBlobHandler::abortProcess()
774{
775 /* Closing of open files is handled from close() -- Reaching here from
776 * delete may never be supported.
777 */
778 removeBlobId(verifyBlobId);
779 removeBlobId(updateBlobId);
780 removeBlobId(activeImageBlobId);
781 removeBlobId(activeHashBlobId);
782
Brandon Kima9f674c2019-10-18 15:18:49 -0700783 for (auto item : lookup)
784 {
785 if (item.second->dataHandler)
786 {
787 item.second->dataHandler->close();
788 }
789 if (item.second->imageHandler)
790 {
791 item.second->imageHandler->close();
792 }
793 }
794 lookup.clear();
795
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700796 openedFirmwareType = "";
Patrick Venture75c0c272019-06-21 09:15:08 -0700797 changeState(UpdateState::notYetStarted);
Patrick Ventured5741022019-06-17 09:08:35 -0700798}
799
800void FirmwareBlobHandler::abortVerification()
801{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700802 auto* pack = getActionPack();
803 if (pack)
804 {
805 pack->verification->abort();
806 }
Patrick Ventured5741022019-06-17 09:08:35 -0700807}
808
Patrick Ventureffcc5502018-11-16 12:32:38 -0800809bool FirmwareBlobHandler::triggerVerification()
810{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700811 auto* pack = getActionPack();
812 if (!pack)
813 {
814 return false;
815 }
816
817 bool result = pack->verification->trigger();
Patrick Venture3ecb3502019-05-17 11:03:51 -0700818 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800819 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700820 changeState(UpdateState::verificationStarted);
Patrick Venturecabc1172018-11-16 16:14:26 -0800821 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800822
Patrick Venture3ecb3502019-05-17 11:03:51 -0700823 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800824}
825
Patrick Venture49731712019-06-17 10:04:02 -0700826void FirmwareBlobHandler::abortUpdate()
827{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700828 auto* pack = getActionPack();
829 if (pack)
830 {
831 pack->update->abort();
832 }
Patrick Venture49731712019-06-17 10:04:02 -0700833}
834
Patrick Venture1a406fe2019-05-31 07:29:56 -0700835bool FirmwareBlobHandler::triggerUpdate()
836{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700837 auto* pack = getActionPack();
838 if (!pack)
839 {
840 return false;
841 }
842
843 bool result = pack->update->trigger();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700844 if (result)
845 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700846 changeState(UpdateState::updateStarted);
Patrick Venture1a406fe2019-05-31 07:29:56 -0700847 }
848
849 return result;
850}
851
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700852} // namespace ipmi_flash