blob: 96b4eba4713656011b96267d0eaca2fbae16859b [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 Venture52854622018-11-06 12:30:00 -080045 /* There must be at least one. */
46 if (!firmwares.size())
47 {
Patrick Ventured333a872018-12-03 16:24:26 -080048 log<level::ERR>("Must provide at least one firmware handler.");
Patrick Venture52854622018-11-06 12:30:00 -080049 return nullptr;
50 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080051 if (!transports.size())
52 {
53 return nullptr;
54 }
Patrick Venture52854622018-11-06 12:30:00 -080055
Patrick Venturea78e39f2018-11-06 18:37:06 -080056 std::vector<std::string> blobs;
57 for (const auto& item : firmwares)
58 {
59 blobs.push_back(item.blobName);
60 }
Patrick Venture18235e62018-11-08 10:21:09 -080061
Patrick Venture7dad86f2019-05-17 08:52:20 -070062 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId))
Patrick Venture18235e62018-11-08 10:21:09 -080063 {
64 return nullptr;
65 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080066
Patrick Venture1cde5f92018-11-07 08:26:47 -080067 std::uint16_t bitmask = 0;
68 for (const auto& item : transports)
69 {
70 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
71 bitmask |= item.bitmask;
72 }
73
Patrick Ventured4e20de2019-07-18 12:48:05 -070074 return std::make_unique<FirmwareBlobHandler>(std::move(firmwares), blobs,
75 transports, bitmask,
76 std::move(actionPacks));
Patrick Venture68cf64f2018-11-06 10:46:51 -080077}
78
Patrick Ventured6461d62018-11-09 17:30:25 -080079/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070080bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
81{
Patrick Venture6032dc02019-05-17 11:01:44 -070082 return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0);
Patrick Venturec7ca2912018-11-02 11:38:33 -070083}
Patrick Venture53977962018-11-02 18:59:35 -070084
Patrick Ventured6461d62018-11-09 17:30:25 -080085/*
86 * Grab the list of supported firmware.
87 *
88 * If there's an open firmware session, it'll already be present in the
89 * list as "/flash/active/image", and if the hash has started,
90 * "/flash/active/hash" regardless of mechanism. This is done in the open
91 * comamnd, no extra work is required here.
92 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070093std::vector<std::string> FirmwareBlobHandler::getBlobIds()
94{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080095 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070096}
Patrick Venture53977962018-11-02 18:59:35 -070097
Patrick Ventured6461d62018-11-09 17:30:25 -080098/*
99 * Per the design, this mean abort, and this will trigger whatever
100 * appropriate actions are required to abort the process.
101 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700102bool FirmwareBlobHandler::deleteBlob(const std::string& path)
103{
Patrick Venturebcc0c772019-06-17 10:42:06 -0700104 /* This cannot be called if you have an open session to the path.
105 * You can have an open session to verify/update/hash/image, but not active*
106 *
107 * Therefore, if this is called, it's either on a blob that isn't presently
108 * open. However, there could be open blobs, so we need to close all open
109 * sessions. This closing on our is an invalid handler behavior. Therefore,
110 * we cannot close an active session. To enforce this, we only allow
111 * deleting if there isn't a file open.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800112 */
Brandon Kim8fc60872019-10-18 15:15:50 -0700113 if (fileOpen())
Patrick Ventureffcc5502018-11-16 12:32:38 -0800114 {
115 return false;
116 }
117
Brandon Kim8fc60872019-10-18 15:15:50 -0700118 /* only includes states where fileOpen() == false */
Patrick Venture72676762019-06-17 11:22:38 -0700119 switch (state)
120 {
121 case UpdateState::notYetStarted:
122 /* Trying to delete anything at this point has no effect and returns
123 * false.
124 */
125 return false;
126 case UpdateState::verificationPending:
Patrick Venture2ca66522019-06-17 11:58:38 -0700127 abortProcess();
128 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700129 case UpdateState::updatePending:
Patrick Venturec9f62392019-06-17 12:17:26 -0700130 abortProcess();
131 return true;
Patrick Venture72676762019-06-17 11:22:38 -0700132 default:
133 break;
134 }
135
Patrick Venturebcc0c772019-06-17 10:42:06 -0700136 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700137}
Patrick Venture53977962018-11-02 18:59:35 -0700138
Patrick Ventured6461d62018-11-09 17:30:25 -0800139/*
140 * Stat on the files will return information such as what supported
141 * transport mechanisms are available.
142 *
143 * Stat on an active file or hash will return information such as the size
144 * of the data cached, and any additional pertinent information. The
145 * blob_state on the active files will return the state of the update.
146 */
Patrick Venturee312f392019-06-04 07:44:37 -0700147bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta)
Patrick Venturec7ca2912018-11-02 11:38:33 -0700148{
Patrick Venture46637c82018-11-06 15:20:24 -0800149 /* We know we support this path because canHandle is called ahead */
Patrick Ventured342a952019-05-29 09:09:10 -0700150 if (path == verifyBlobId || path == activeImageBlobId ||
Patrick Venture5f562692019-05-30 16:49:46 -0700151 path == activeHashBlobId || path == updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800152 {
Patrick Ventured342a952019-05-29 09:09:10 -0700153 /* These blobs are placeholders that indicate things, or allow actions,
154 * but are not stat-able as-is.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800155 */
Patrick Ventured342a952019-05-29 09:09:10 -0700156 return false;
Patrick Venture46637c82018-11-06 15:20:24 -0800157 }
158
Patrick Ventured342a952019-05-29 09:09:10 -0700159 /* They are requesting information about the generic blob_id. */
160 meta->blobState = bitmask;
161 meta->size = 0;
162
163 /* The generic blob_ids state is only the bits related to the transport
164 * mechanisms.
165 */
166 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700167}
Patrick Venture53977962018-11-02 18:59:35 -0700168
Patrick Ventureda66fd82019-06-03 11:11:24 -0700169ActionStatus FirmwareBlobHandler::getActionStatus()
Patrick Venturea2d82392019-06-03 10:55:17 -0700170{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700171 ActionStatus value = ActionStatus::unknown;
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700172 auto* pack = getActionPack();
Patrick Venturea2d82392019-06-03 10:55:17 -0700173
174 switch (state)
175 {
176 case UpdateState::verificationPending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700177 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700178 break;
179 case UpdateState::verificationStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700180 /* If we got here, there must be data AND a hash, not just a hash,
181 * therefore pack will be known. */
182 if (!pack)
183 {
184 break;
185 }
186 value = pack->verification->status();
Patrick Ventureda66fd82019-06-03 11:11:24 -0700187 lastVerificationStatus = value;
Patrick Venturea2d82392019-06-03 10:55:17 -0700188 break;
189 case UpdateState::verificationCompleted:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700190 value = lastVerificationStatus;
Patrick Venturea2d82392019-06-03 10:55:17 -0700191 break;
Patrick Venturea2d82392019-06-03 10:55:17 -0700192 case UpdateState::updatePending:
Patrick Ventureda66fd82019-06-03 11:11:24 -0700193 value = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700194 break;
195 case UpdateState::updateStarted:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700196 if (!pack)
197 {
198 break;
199 }
200 value = pack->update->status();
Patrick Venturea2d82392019-06-03 10:55:17 -0700201 lastUpdateStatus = value;
202 break;
203 case UpdateState::updateCompleted:
204 value = lastUpdateStatus;
205 break;
206 default:
207 break;
208 }
209
210 return value;
211}
212
Patrick Venturec02849b2018-11-06 17:31:15 -0800213/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800214 * Return stat information on an open session. It therefore must be an active
215 * handle to either the active image or active hash.
Patrick Ventured6461d62018-11-09 17:30:25 -0800216 */
Patrick Venturee312f392019-06-04 07:44:37 -0700217bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta)
Patrick Ventured6461d62018-11-09 17:30:25 -0800218{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800219 auto item = lookup.find(session);
220 if (item == lookup.end())
221 {
222 return false;
223 }
224
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700225 /* The size here refers to the size of the file -- of something analagous.
226 */
227 meta->size = (item->second->imageHandler)
228 ? item->second->imageHandler->getSize()
229 : 0;
230
231 meta->metadata.clear();
232
Patrick Ventureda66fd82019-06-03 11:11:24 -0700233 if (item->second->activePath == verifyBlobId ||
234 item->second->activePath == updateBlobId)
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700235 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700236 ActionStatus value = getActionStatus();
Patrick Venture699750d2019-05-15 09:24:09 -0700237
Patrick Venturee955e072019-05-15 16:16:46 -0700238 meta->metadata.push_back(static_cast<std::uint8_t>(value));
239
240 /* Change the firmware handler's state and the blob's stat value
241 * depending.
242 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700243 if (value == ActionStatus::success || value == ActionStatus::failed)
Patrick Venturee955e072019-05-15 16:16:46 -0700244 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700245 if (item->second->activePath == verifyBlobId)
Patrick Venturee955e072019-05-15 16:16:46 -0700246 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700247 changeState(UpdateState::verificationCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700248 }
249 else
250 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700251 /* item->second->activePath == updateBlobId */
Patrick Venture75c0c272019-06-21 09:15:08 -0700252 changeState(UpdateState::updateCompleted);
Patrick Venturee955e072019-05-15 16:16:46 -0700253 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700254
Patrick Venturef1f0f652019-06-03 09:10:19 -0700255 item->second->flags &= ~blobs::StateFlags::committing;
256
Patrick Ventureda66fd82019-06-03 11:11:24 -0700257 if (value == ActionStatus::success)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700258 {
259 item->second->flags |= blobs::StateFlags::committed;
260 }
261 else
262 {
263 item->second->flags |= blobs::StateFlags::commit_error;
264 }
265 }
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700266 }
Patrick Venturecc7d1602018-11-15 13:58:33 -0800267
Patrick Venturee955e072019-05-15 16:16:46 -0700268 /* The blobState here relates to an active sesion, so we should return the
269 * flags used to open this session.
270 */
271 meta->blobState = item->second->flags;
272
Patrick Venturecc7d1602018-11-15 13:58:33 -0800273 /* The metadata blob returned comes from the data handler... it's used for
274 * instance, in P2A bridging to get required information about the mapping,
275 * and is the "opposite" of the lpc writemeta requirement.
276 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800277 if (item->second->dataHandler)
278 {
Patrick Venture74304642019-01-17 09:31:04 -0800279 auto bytes = item->second->dataHandler->readMeta();
Patrick Venturecc7d1602018-11-15 13:58:33 -0800280 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
281 bytes.end());
282 }
283
Patrick Venturecc7d1602018-11-15 13:58:33 -0800284 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800285}
286
287/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800288 * If you open /flash/image or /flash/tarball, or /flash/hash it will
289 * interpret the open flags and perform whatever actions are required for
290 * that update process. The session returned can be used immediately for
291 * sending data down, without requiring one to open the new active file.
292 *
293 * If you open the active flash image or active hash it will let you
294 * overwrite pieces, depending on the state.
295 *
296 * Once the verification process has started the active files cannot be
297 * opened.
298 *
299 * You can only have one open session at a time. Which means, you can only
300 * have one file open at a time. Trying to open the hash blob_id while you
301 * still have the flash image blob_id open will fail. Opening the flash
302 * blob_id when it is already open will fail.
303 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700304bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
305 const std::string& path)
306{
Patrick Venturec02849b2018-11-06 17:31:15 -0800307 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800308 *
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800309 * Further on this, if there's an active session to the hash we don't allow
310 * re-opening the image, and if we have the image open, we don't allow
311 * opening the hash. This design decision may be re-evaluated, and changed
312 * to only allow one session per object type (of the two types). But,
313 * consider if the hash is open, do we want to allow writing to the image?
314 * And why would we? But, really, the point of no-return is once the
315 * verification process has begun -- which is done via commit() on the hash
316 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700317 */
Brandon Kim8fc60872019-10-18 15:15:50 -0700318 if (fileOpen())
Patrick Venturec02849b2018-11-06 17:31:15 -0800319 {
320 return false;
321 }
322
Patrick Venture723113f2019-06-05 09:38:35 -0700323 /* The active blobs are only meant to indicate status that something has
324 * opened the image file or the hash file.
Patrick Ventureffcc5502018-11-16 12:32:38 -0800325 */
Patrick Venture19f5d882019-05-30 14:34:55 -0700326 if (path == activeImageBlobId || path == activeHashBlobId)
327 {
328 /* 2a) are they opening the active image? this can only happen if they
329 * already started one (due to canHandleBlob's behavior).
330 */
331 /* 2b) are they opening the active hash? this can only happen if they
332 * already started one (due to canHandleBlob's behavior).
333 */
334 return false;
335 }
336
Patrick Venture723113f2019-06-05 09:38:35 -0700337 /* Check that they've opened for writing - read back not currently
338 * supported.
339 */
340 if ((flags & blobs::OpenFlags::write) == 0)
341 {
342 return false;
343 }
344
345 /* Because canHandleBlob is called before open, we know that if they try to
346 * open the verifyBlobId, they're in a state where it's present.
347 */
348
349 switch (state)
350 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700351 case UpdateState::notYetStarted:
352 /* Only hashBlobId and firmware BlobIds present. */
353 break;
Patrick Venture723113f2019-06-05 09:38:35 -0700354 case UpdateState::uploadInProgress:
355 /* Unreachable code because if it's started a file is open. */
356 break;
357 case UpdateState::verificationPending:
358 /* Handle opening the verifyBlobId --> we know the image and hash
Brandon Kim8fc60872019-10-18 15:15:50 -0700359 * aren't open because of the fileOpen() check. They can still open
Patrick Venture723113f2019-06-05 09:38:35 -0700360 * other files from this state to transition back into
361 * uploadInProgress.
362 *
363 * The file must be opened for writing, but no transport mechanism
364 * specified since it's irrelevant.
365 */
366 if (path == verifyBlobId)
367 {
368 verifyImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700369
370 lookup[session] = &verifyImage;
371
Patrick Venture723113f2019-06-05 09:38:35 -0700372 return true;
373 }
374 break;
375 case UpdateState::verificationStarted:
376 case UpdateState::verificationCompleted:
377 /* Unreachable code because if it's started a file is open. */
378 return false;
379 case UpdateState::updatePending:
380 {
381 /* When in this state, they can only open the updateBlobId */
382 if (path == updateBlobId)
383 {
384 updateImage.flags = flags;
Patrick Venture723113f2019-06-05 09:38:35 -0700385
386 lookup[session] = &updateImage;
387
Patrick Venture723113f2019-06-05 09:38:35 -0700388 return true;
389 }
390 else
391 {
392 return false;
393 }
394 }
395 case UpdateState::updateStarted:
396 case UpdateState::updateCompleted:
397 /* Unreachable code because if it's started a file is open. */
398 break;
399 default:
400 break;
401 }
402
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700403 /* To support multiple firmware options, we need to make sure they're
404 * opening the one they already opened during this update sequence, or it's
405 * the first time they're opening it.
406 */
407 if (path != hashBlobId)
408 {
409 /* If they're not opening the hashBlobId they must be opening a firmware
410 * handler.
411 */
412 if (openedFirmwareType.empty())
413 {
414 /* First time for this sequence. */
415 openedFirmwareType = path;
416 }
417 else
418 {
419 if (openedFirmwareType != path)
420 {
421 /* Previously, in this sequence they opened /flash/image, and
422 * now they're opening /flash/bios without finishing out
423 * /flash/image (for example).
424 */
425 std::fprintf(stderr, "Trying to open alternate firmware while "
426 "unfinished with other firmware.\n");
427 return false;
428 }
429 }
430 }
431
Patrick Venture723113f2019-06-05 09:38:35 -0700432 /* There are two abstractions at play, how you get the data and how you
433 * handle that data. such that, whether the data comes from the PCI bridge
434 * or LPC bridge is not connected to whether the data goes into a static
435 * layout flash update or a UBI tarball.
436 */
437
Patrick Venturec02849b2018-11-06 17:31:15 -0800438 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800439 * support what they request.
440 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800441 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800442 {
443 return false;
444 }
445
Patrick Venture18235e62018-11-08 10:21:09 -0800446 /* How are they expecting to copy this data? */
447 auto d = std::find_if(
448 transports.begin(), transports.end(),
449 [&flags](const auto& iter) { return (iter.bitmask & flags); });
450 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800451 {
Patrick Venture18235e62018-11-08 10:21:09 -0800452 return false;
453 }
454
455 /* We found the transport handler they requested, no surprise since
456 * above we verify they selected at least one we wanted.
457 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800458
Patrick Venture6e307a72018-11-09 18:21:17 -0800459 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
460 * only non-external data pathway -- but this is just a more generic
461 * approach to that.
462 */
463 if (d->handler)
464 {
465 /* If the data handler open call fails, open fails. */
466 if (!d->handler->open())
467 {
468 return false;
469 }
470 }
471
Patrick Venture70e30bf2019-01-17 10:29:28 -0800472 /* Do we have a file handler for the type of file they're opening.
473 * Note: This should only fail if something is somehow crazy wrong.
474 * Since the canHandle() said yes, and that's tied into the list of explicit
475 * firmware handers (and file handlers, like this'll know where to write the
476 * tarball, etc).
Patrick Venture18235e62018-11-08 10:21:09 -0800477 */
Patrick Venture18235e62018-11-08 10:21:09 -0800478 auto h = std::find_if(
479 handlers.begin(), handlers.end(),
480 [&path](const auto& iter) { return (iter.blobName == path); });
481 if (h == handlers.end())
482 {
483 return false;
484 }
485
486 /* Ok, so we found a handler that matched, so call open() */
487 if (!h->handler->open(path))
488 {
489 return false;
490 }
491
Patrick Venture70e30bf2019-01-17 10:29:28 -0800492 Session* curr;
493 const std::string* active;
494
Patrick Venture7dad86f2019-05-17 08:52:20 -0700495 if (path == hashBlobId)
Patrick Venture70e30bf2019-01-17 10:29:28 -0800496 {
497 /* 2c) are they opening the /flash/hash ? (to start the process) */
498 curr = &activeHash;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700499 active = &activeHashBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800500 }
501 else
502 {
503 curr = &activeImage;
Patrick Venture7dad86f2019-05-17 08:52:20 -0700504 active = &activeImageBlobId;
Patrick Venture70e30bf2019-01-17 10:29:28 -0800505 }
506
Patrick Venture18235e62018-11-08 10:21:09 -0800507 curr->flags = flags;
508 curr->dataHandler = d->handler;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700509 curr->imageHandler = h->handler.get();
Patrick Venture18235e62018-11-08 10:21:09 -0800510
511 lookup[session] = curr;
512
Patrick Ventureefba42d2019-05-24 10:48:16 -0700513 addBlobId(*active);
Patrick Venture930c7b72019-05-24 11:11:08 -0700514 removeBlobId(verifyBlobId);
Patrick Venturedb253bf2018-11-09 19:36:03 -0800515
Patrick Venture75c0c272019-06-21 09:15:08 -0700516 changeState(UpdateState::uploadInProgress);
Patrick Venture991910a2018-11-09 19:43:48 -0800517
Patrick Venture18235e62018-11-08 10:21:09 -0800518 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700519}
Patrick Venture53977962018-11-02 18:59:35 -0700520
Patrick Venture18235e62018-11-08 10:21:09 -0800521/**
522 * The write command really just grabs the data from wherever it is and sends it
523 * to the image handler. It's the image handler's responsibility to deal with
524 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800525 *
526 * This receives a session from the blob manager, therefore it is always called
527 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800528 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700529bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
530 const std::vector<uint8_t>& data)
531{
Patrick Venture18235e62018-11-08 10:21:09 -0800532 auto item = lookup.find(session);
533 if (item == lookup.end())
534 {
535 return false;
536 }
537
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800538 /* Prevent writing during verification. */
539 if (state == UpdateState::verificationStarted)
540 {
541 return false;
542 }
543
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700544 /* Prevent writing to the verification or update blobs. */
545 if (item->second->activePath == verifyBlobId ||
546 item->second->activePath == updateBlobId)
Patrick Venture8af15eb2019-05-15 09:39:22 -0700547 {
548 return false;
549 }
Patrick Venture699750d2019-05-15 09:24:09 -0700550
Patrick Venture18235e62018-11-08 10:21:09 -0800551 std::vector<std::uint8_t> bytes;
552
Patrick Venture84778b82019-06-26 20:11:09 -0700553 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800554 {
555 bytes = data;
556 }
557 else
558 {
559 /* little endian required per design, and so on, but TODO: do endianness
560 * with boost.
561 */
562 struct ExtChunkHdr header;
563
564 if (data.size() != sizeof(header))
565 {
566 return false;
567 }
568
569 std::memcpy(&header, data.data(), data.size());
570 bytes = item->second->dataHandler->copyFrom(header.length);
571 }
572
573 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700574}
Patrick Venture18235e62018-11-08 10:21:09 -0800575
Patrick Venture8c535332018-11-08 15:58:00 -0800576/*
577 * If the active session (image or hash) is over LPC, this allows
578 * configuring it. This option is only available before you start
579 * writing data for the given item (image or hash). This will return
580 * false at any other part. -- the lpc handler portion will know to return
581 * false.
582 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700583bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
584 const std::vector<uint8_t>& data)
585{
Patrick Venture8c535332018-11-08 15:58:00 -0800586 auto item = lookup.find(session);
587 if (item == lookup.end())
588 {
589 return false;
590 }
591
Patrick Venture84778b82019-06-26 20:11:09 -0700592 if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800593 {
594 return false;
595 }
596
Patrick Ventured56097a2019-05-15 09:47:55 -0700597 /* Prevent writing meta to the verification blob (it has no data handler).
598 */
599 if (item->second->dataHandler)
600 {
601 return item->second->dataHandler->writeMeta(data);
602 }
Patrick Venture699750d2019-05-15 09:24:09 -0700603
Patrick Ventured56097a2019-05-15 09:47:55 -0700604 return false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700605}
Patrick Venture8c535332018-11-08 15:58:00 -0800606
Patrick Ventured6461d62018-11-09 17:30:25 -0800607/*
Patrick Venture7dad86f2019-05-17 08:52:20 -0700608 * If this command is called on the session for the verifyBlobId, it'll
Patrick Ventured6461d62018-11-09 17:30:25 -0800609 * trigger a systemd service `verify_image.service` to attempt to verify
Patrick Ventureffcc5502018-11-16 12:32:38 -0800610 * the image.
611 *
612 * For this file to have opened, the other two must be closed, which means any
613 * out-of-band transport mechanism involved is closed.
Patrick Ventured6461d62018-11-09 17:30:25 -0800614 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700615bool FirmwareBlobHandler::commit(uint16_t session,
616 const std::vector<uint8_t>& data)
617{
Patrick Ventureffcc5502018-11-16 12:32:38 -0800618 auto item = lookup.find(session);
619 if (item == lookup.end())
620 {
621 return false;
622 }
623
Patrick Venture1a406fe2019-05-31 07:29:56 -0700624 /* You can only commit on the verifyBlodId or updateBlobId */
625 if (item->second->activePath != verifyBlobId &&
626 item->second->activePath != updateBlobId)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800627 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700628 std::fprintf(stderr, "path: '%s' not expected for commit\n",
629 item->second->activePath.c_str());
Patrick Ventureffcc5502018-11-16 12:32:38 -0800630 return false;
631 }
632
Patrick Venture433cbc32019-05-30 09:53:10 -0700633 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800634 {
Patrick Venture1a406fe2019-05-31 07:29:56 -0700635 case UpdateState::verificationPending:
636 /* Set state to committing. */
637 item->second->flags |= blobs::StateFlags::committing;
638 return triggerVerification();
Patrick Venture433cbc32019-05-30 09:53:10 -0700639 case UpdateState::verificationStarted:
640 /* Calling repeatedly has no effect within an update process. */
641 return true;
642 case UpdateState::verificationCompleted:
643 /* Calling after the verification process has completed returns
644 * failure. */
645 return false;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700646 case UpdateState::updatePending:
Patrick Venture433cbc32019-05-30 09:53:10 -0700647 item->second->flags |= blobs::StateFlags::committing;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700648 return triggerUpdate();
Patrick Venture0079d892019-05-31 11:27:44 -0700649 case UpdateState::updateStarted:
650 /* Calling repeatedly has no effect within an update process. */
651 return true;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700652 default:
653 return false;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800654 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700655}
Patrick Ventured6461d62018-11-09 17:30:25 -0800656
657/*
658 * Close must be called on the firmware image before triggering
659 * verification via commit. Once the verification is complete, you can
660 * then close the hash file.
661 *
662 * If the `verify_image.service` returned success, closing the hash file
663 * will have a specific behavior depending on the update. If it's UBI,
664 * it'll perform the install. If it's static layout, it'll do nothing. The
665 * verify_image service in the static layout case is responsible for placing
666 * the file in the correct staging position.
667 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700668bool FirmwareBlobHandler::close(uint16_t session)
669{
Patrick Venture68bb1432018-11-09 20:08:48 -0800670 auto item = lookup.find(session);
671 if (item == lookup.end())
672 {
673 return false;
674 }
675
Patrick Venturee95dbb62019-06-05 09:59:29 -0700676 switch (state)
Patrick Ventureffcc5502018-11-16 12:32:38 -0800677 {
Patrick Venturee95dbb62019-06-05 09:59:29 -0700678 case UpdateState::uploadInProgress:
679 /* They are closing a data pathway (image, tarball, hash). */
Patrick Venture75c0c272019-06-21 09:15:08 -0700680 changeState(UpdateState::verificationPending);
Patrick Venture85ae86b2019-06-05 09:18:40 -0700681
Patrick Venture1999eef2019-07-01 11:44:09 -0700682 /* Add verify blob ID now that we can expect it, IIF they also wrote
683 * some data.
684 */
685 if (std::count(blobIDs.begin(), blobIDs.end(), activeImageBlobId))
686 {
687 addBlobId(verifyBlobId);
688 }
Patrick Venturee95dbb62019-06-05 09:59:29 -0700689 break;
690 case UpdateState::verificationPending:
691 /* They haven't triggered, therefore closing is uninteresting.
692 */
693 break;
694 case UpdateState::verificationStarted:
Patrick Ventured5741022019-06-17 09:08:35 -0700695 /* Abort without checking to see if it happened to finish. Require
696 * the caller to stat() deliberately.
Patrick Venturee95dbb62019-06-05 09:59:29 -0700697 */
Patrick Ventured5741022019-06-17 09:08:35 -0700698 abortVerification();
699 abortProcess();
700 break;
Patrick Venturee95dbb62019-06-05 09:59:29 -0700701 case UpdateState::verificationCompleted:
702 if (lastVerificationStatus == ActionStatus::success)
703 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700704 changeState(UpdateState::updatePending);
Patrick Venturee95dbb62019-06-05 09:59:29 -0700705 addBlobId(updateBlobId);
706 removeBlobId(verifyBlobId);
707 }
708 else
709 {
Patrick Venture5d9fa022019-06-17 13:13:30 -0700710 /* Verification failed, and the host-tool knows this by calling
711 * stat(), which triggered the state change to
712 * verificationCompleted.
713 *
714 * Therefore, let's abort the process at this point.
715 */
716 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700717 }
718 break;
719 case UpdateState::updatePending:
720 /* They haven't triggered the update, therefore this is
721 * uninteresting. */
722 break;
723 case UpdateState::updateStarted:
Patrick Venture49731712019-06-17 10:04:02 -0700724 /* Abort without checking to see if it happened to finish. Require
725 * the caller to stat() deliberately.
726 */
727 abortUpdate();
728 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700729 break;
730 case UpdateState::updateCompleted:
731 if (lastUpdateStatus == ActionStatus::failed)
732 {
733 /* TODO: lOG something? */
Patrick Venture4c7a4202019-06-17 13:06:55 -0700734 std::fprintf(stderr, "Update failed\n");
Patrick Venturee95dbb62019-06-05 09:59:29 -0700735 }
Patrick Venture930c7b72019-05-24 11:11:08 -0700736
Patrick Venture4c7a4202019-06-17 13:06:55 -0700737 abortProcess();
Patrick Venturee95dbb62019-06-05 09:59:29 -0700738 break;
739 default:
740 break;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800741 }
742
Patrick Venture68bb1432018-11-09 20:08:48 -0800743 if (item->second->dataHandler)
744 {
745 item->second->dataHandler->close();
746 }
Patrick Ventureffcc5502018-11-16 12:32:38 -0800747 if (item->second->imageHandler)
748 {
749 item->second->imageHandler->close();
750 }
751
Patrick Venture68bb1432018-11-09 20:08:48 -0800752 lookup.erase(item);
Patrick Venture68bb1432018-11-09 20:08:48 -0800753 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700754}
Patrick Ventured6461d62018-11-09 17:30:25 -0800755
Patrick Venture75c0c272019-06-21 09:15:08 -0700756void FirmwareBlobHandler::changeState(UpdateState next)
757{
758 state = next;
Patrick Venture6d7735d2019-06-21 10:03:19 -0700759
760 if (state == UpdateState::notYetStarted)
761 {
762 /* Going back to notyetstarted, let them trigger preparation again. */
763 preparationTriggered = false;
764 }
765 else if (state == UpdateState::uploadInProgress)
766 {
767 /* Store this transition logic here instead of ::open() */
768 if (!preparationTriggered)
769 {
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700770 auto* pack = getActionPack();
771 if (pack)
772 {
773 pack->preparation->trigger();
774 preparationTriggered = true;
775 }
Patrick Venture6d7735d2019-06-21 10:03:19 -0700776 }
777 }
Patrick Venture75c0c272019-06-21 09:15:08 -0700778}
779
Patrick Venturec7ca2912018-11-02 11:38:33 -0700780bool FirmwareBlobHandler::expire(uint16_t session)
781{
782 return false;
783}
Patrick Ventured6461d62018-11-09 17:30:25 -0800784
785/*
786 * Currently, the design does not provide this with a function, however,
787 * it will likely change to support reading data back.
788 */
789std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
790 uint32_t offset,
791 uint32_t requestedSize)
792{
793 return {};
794}
795
Patrick Ventured5741022019-06-17 09:08:35 -0700796void FirmwareBlobHandler::abortProcess()
797{
798 /* Closing of open files is handled from close() -- Reaching here from
799 * delete may never be supported.
800 */
801 removeBlobId(verifyBlobId);
802 removeBlobId(updateBlobId);
803 removeBlobId(activeImageBlobId);
804 removeBlobId(activeHashBlobId);
805
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700806 openedFirmwareType = "";
Patrick Venture75c0c272019-06-21 09:15:08 -0700807 changeState(UpdateState::notYetStarted);
Patrick Ventured5741022019-06-17 09:08:35 -0700808}
809
810void FirmwareBlobHandler::abortVerification()
811{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700812 auto* pack = getActionPack();
813 if (pack)
814 {
815 pack->verification->abort();
816 }
Patrick Ventured5741022019-06-17 09:08:35 -0700817}
818
Patrick Ventureffcc5502018-11-16 12:32:38 -0800819bool FirmwareBlobHandler::triggerVerification()
820{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700821 auto* pack = getActionPack();
822 if (!pack)
823 {
824 return false;
825 }
826
827 bool result = pack->verification->trigger();
Patrick Venture3ecb3502019-05-17 11:03:51 -0700828 if (result)
Patrick Venturecabc1172018-11-16 16:14:26 -0800829 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700830 changeState(UpdateState::verificationStarted);
Patrick Venturecabc1172018-11-16 16:14:26 -0800831 }
Patrick Venturecabc1172018-11-16 16:14:26 -0800832
Patrick Venture3ecb3502019-05-17 11:03:51 -0700833 return result;
Patrick Ventureffcc5502018-11-16 12:32:38 -0800834}
835
Patrick Venture49731712019-06-17 10:04:02 -0700836void FirmwareBlobHandler::abortUpdate()
837{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700838 auto* pack = getActionPack();
839 if (pack)
840 {
841 pack->update->abort();
842 }
Patrick Venture49731712019-06-17 10:04:02 -0700843}
844
Patrick Venture1a406fe2019-05-31 07:29:56 -0700845bool FirmwareBlobHandler::triggerUpdate()
846{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700847 auto* pack = getActionPack();
848 if (!pack)
849 {
850 return false;
851 }
852
853 bool result = pack->update->trigger();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700854 if (result)
855 {
Patrick Venture75c0c272019-06-21 09:15:08 -0700856 changeState(UpdateState::updateStarted);
Patrick Venture1a406fe2019-05-31 07:29:56 -0700857 }
858
859 return result;
860}
861
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700862} // namespace ipmi_flash