blob: fda533e7e40907cc5f3d26d6e456356ac885b1ee [file] [log] [blame]
Patrick Venture22e38752018-11-21 08:52:49 -08001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Patrick Venturec7ca2912018-11-02 11:38:33 -070017#include "firmware_handler.hpp"
18
Patrick Venturea78e39f2018-11-06 18:37:06 -080019#include "image_handler.hpp"
20
Patrick Venture137ad2c2018-11-06 11:30:43 -080021#include <algorithm>
Patrick Venture192d60f2018-11-06 11:11:59 -080022#include <cstdint>
Patrick Venture18235e62018-11-08 10:21:09 -080023#include <cstring>
Patrick Venture68cf64f2018-11-06 10:46:51 -080024#include <memory>
Patrick Venturefa6c4d92018-11-02 18:34:53 -070025#include <string>
26#include <vector>
27
Patrick Venturec7ca2912018-11-02 11:38:33 -070028namespace blobs
29{
30
Patrick Venture21be45a2018-11-06 12:08:52 -080031const std::string FirmwareBlobHandler::hashBlobID = "/flash/hash";
Patrick Venture7b9256f2018-11-06 15:06:04 -080032const std::string FirmwareBlobHandler::activeImageBlobID =
33 "/flash/active/image";
34const std::string FirmwareBlobHandler::activeHashBlobID = "/flash/active/hash";
Patrick Venture4cceb8e2018-11-06 11:56:48 -080035
Patrick Venture68cf64f2018-11-06 10:46:51 -080036std::unique_ptr<GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080037 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture1cde5f92018-11-07 08:26:47 -080038 const std::vector<HandlerPack>& firmwares,
39 const std::vector<DataHandlerPack>& transports)
Patrick Venture68cf64f2018-11-06 10:46:51 -080040{
Patrick Venture52854622018-11-06 12:30:00 -080041 /* There must be at least one. */
42 if (!firmwares.size())
43 {
44 return nullptr;
45 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080046 if (!transports.size())
47 {
48 return nullptr;
49 }
Patrick Venture52854622018-11-06 12:30:00 -080050
Patrick Venturea78e39f2018-11-06 18:37:06 -080051 std::vector<std::string> blobs;
52 for (const auto& item : firmwares)
53 {
54 blobs.push_back(item.blobName);
55 }
Patrick Venture18235e62018-11-08 10:21:09 -080056
57 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobID))
58 {
59 return nullptr;
60 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080061
Patrick Venture1cde5f92018-11-07 08:26:47 -080062 std::uint16_t bitmask = 0;
63 for (const auto& item : transports)
64 {
65 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
66 bitmask |= item.bitmask;
67 }
68
69 return std::make_unique<FirmwareBlobHandler>(firmwares, blobs, transports,
70 bitmask);
Patrick Venture68cf64f2018-11-06 10:46:51 -080071}
72
Patrick Ventured6461d62018-11-09 17:30:25 -080073/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070074bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
75{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080076 if (std::count(blobIDs.begin(), blobIDs.end(), path))
Patrick Venture137ad2c2018-11-06 11:30:43 -080077 {
78 return true;
79 }
80
Patrick Venturec7ca2912018-11-02 11:38:33 -070081 return false;
82}
Patrick Venture53977962018-11-02 18:59:35 -070083
Patrick Ventured6461d62018-11-09 17:30:25 -080084/*
85 * Grab the list of supported firmware.
86 *
87 * If there's an open firmware session, it'll already be present in the
88 * list as "/flash/active/image", and if the hash has started,
89 * "/flash/active/hash" regardless of mechanism. This is done in the open
90 * comamnd, no extra work is required here.
91 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070092std::vector<std::string> FirmwareBlobHandler::getBlobIds()
93{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080094 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070095}
Patrick Venture53977962018-11-02 18:59:35 -070096
Patrick Ventured6461d62018-11-09 17:30:25 -080097/*
98 * Per the design, this mean abort, and this will trigger whatever
99 * appropriate actions are required to abort the process.
Patrick Venture9e5aab22018-11-09 20:49:28 -0800100 *
101 * You cannot delete a blob that has an open handle in the system, therefore
102 * this is never called if there's an open session. Guaranteed by the blob
103 * manager.
Patrick Ventured6461d62018-11-09 17:30:25 -0800104 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700105bool FirmwareBlobHandler::deleteBlob(const std::string& path)
106{
Patrick Venture9e5aab22018-11-09 20:49:28 -0800107 const std::string* toDelete;
108
109 if (path == hashBlobID || path == activeHashBlobID)
110 {
111 /* They're deleting the hash. */
112 toDelete = &activeHashBlobID;
113 }
114 else
115 {
116 /* They're deleting the image. */
117 toDelete = &activeImageBlobID;
118 }
119
120 auto it = std::find_if(
121 blobIDs.begin(), blobIDs.end(),
122 [toDelete](const auto& iter) { return (iter == *toDelete); });
123 if (it == blobIDs.end())
124 {
125 /* Somehow they've asked to delete something we didn't say we could
126 * handle.
127 */
128 return false;
129 }
130
131 blobIDs.erase(it);
132
133 /* TODO: Handle aborting the process and fixing up the state. */
134
135 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700136}
Patrick Venture53977962018-11-02 18:59:35 -0700137
Patrick Ventured6461d62018-11-09 17:30:25 -0800138/*
139 * Stat on the files will return information such as what supported
140 * transport mechanisms are available.
141 *
142 * Stat on an active file or hash will return information such as the size
143 * of the data cached, and any additional pertinent information. The
144 * blob_state on the active files will return the state of the update.
145 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700146bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
147{
Patrick Venture46637c82018-11-06 15:20:24 -0800148 /* We know we support this path because canHandle is called ahead */
149 if (path == FirmwareBlobHandler::activeImageBlobID)
150 {
151 /* We need to return information for the image that's staged. */
152 }
153 else if (path == FirmwareBlobHandler::activeHashBlobID)
154 {
155 /* We need to return information for the hash that's staged. */
156 }
157 else
158 {
159 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800160 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800161 meta->size = 0;
162
163 /* The generic blob_ids state is only the bits related to the transport
Patrick Ventured6461d62018-11-09 17:30:25 -0800164 * mechanisms.
165 */
Patrick Venture46637c82018-11-06 15:20:24 -0800166 return true;
167 }
168
Patrick Venturec7ca2912018-11-02 11:38:33 -0700169 return false;
170}
Patrick Venture53977962018-11-02 18:59:35 -0700171
Patrick Venturec02849b2018-11-06 17:31:15 -0800172/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800173 * Return stat information on an open session. It therefore must be an active
174 * handle to either the active image or active hash.
175 *
176 * The stat() and sessionstat() commands will return the same information in
177 * many cases, therefore the logic will be combined.
178 *
179 * TODO: combine the logic for stat and sessionstat().
180 */
181bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
182{
Patrick Venturecc7d1602018-11-15 13:58:33 -0800183 auto item = lookup.find(session);
184 if (item == lookup.end())
185 {
186 return false;
187 }
188
189 /* The blobState here relates to an active sesion, so we should return the
190 * flags used to open this session.
Patrick Ventured6461d62018-11-09 17:30:25 -0800191 */
Patrick Venturecc7d1602018-11-15 13:58:33 -0800192 meta->blobState = item->second->flags;
193 /* The size here refers to the size of the file -- of something analagous.
194 */
195 meta->size = item->second->imageHandler->getSize();
196
197 /* The metadata blob returned comes from the data handler... it's used for
198 * instance, in P2A bridging to get required information about the mapping,
199 * and is the "opposite" of the lpc writemeta requirement.
200 */
201 meta->metadata.clear();
202 if (item->second->dataHandler)
203 {
204 auto bytes = item->second->dataHandler->read();
205 meta->metadata.insert(meta->metadata.begin(), bytes.begin(),
206 bytes.end());
207 }
208
209 /* TODO: During things like verification, etc, we can report the state as
210 * committed, etc, so we'll need to do that.
211 */
212
213 return true;
Patrick Ventured6461d62018-11-09 17:30:25 -0800214}
215
216/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800217 * If you open /flash/image or /flash/tarball, or /flash/hash it will
218 * interpret the open flags and perform whatever actions are required for
219 * that update process. The session returned can be used immediately for
220 * sending data down, without requiring one to open the new active file.
221 *
222 * If you open the active flash image or active hash it will let you
223 * overwrite pieces, depending on the state.
224 *
225 * Once the verification process has started the active files cannot be
226 * opened.
227 *
228 * You can only have one open session at a time. Which means, you can only
229 * have one file open at a time. Trying to open the hash blob_id while you
230 * still have the flash image blob_id open will fail. Opening the flash
231 * blob_id when it is already open will fail.
232 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700233bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
234 const std::string& path)
235{
Patrick Venture6e307a72018-11-09 18:21:17 -0800236 /* Check that they've opened for writing - read back not currently
237 * supported.
238 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800239 if ((flags & OpenFlags::write) == 0)
240 {
241 return false;
242 }
243
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800244 /* Is the verification process underway? */
245 if (state == UpdateState::verificationStarted)
246 {
247 return false;
248 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800249
250 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800251 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800252 * TODO: Temporarily using a simple boolean flag until there's a full
253 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800254 *
255 * Further on this, if there's an active session to the hash we don't allow
256 * re-opening the image, and if we have the image open, we don't allow
257 * opening the hash. This design decision may be re-evaluated, and changed
258 * to only allow one session per object type (of the two types). But,
259 * consider if the hash is open, do we want to allow writing to the image?
260 * And why would we? But, really, the point of no-return is once the
261 * verification process has begun -- which is done via commit() on the hash
262 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700263 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800264 if (fileOpen)
265 {
266 return false;
267 }
268
269 /* There are two abstractions at play, how you get the data and how you
270 * handle that data. such that, whether the data comes from the PCI bridge
271 * or LPC bridge is not connected to whether the data goes into a static
272 * layout flash update or a UBI tarball.
273 */
274
275 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800276 * support what they request.
277 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800278 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800279 {
280 return false;
281 }
282
283 /* 2) there isn't, so what are they opening? */
284 if (path == activeImageBlobID)
285 {
286 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800287 * already started one (due to canHandleBlob's behavior).
288 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800289 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800290 }
291 else if (path == activeHashBlobID)
292 {
293 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800294 * already started one (due to canHandleBlob's behavior).
295 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800296 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800297 }
Patrick Venture18235e62018-11-08 10:21:09 -0800298
299 /* How are they expecting to copy this data? */
300 auto d = std::find_if(
301 transports.begin(), transports.end(),
302 [&flags](const auto& iter) { return (iter.bitmask & flags); });
303 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800304 {
Patrick Venture18235e62018-11-08 10:21:09 -0800305 return false;
306 }
307
308 /* We found the transport handler they requested, no surprise since
309 * above we verify they selected at least one we wanted.
310 */
311 Session* curr;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800312 const std::string* active;
Patrick Venture18235e62018-11-08 10:21:09 -0800313
314 if (path == hashBlobID)
315 {
Patrick Venturec02849b2018-11-06 17:31:15 -0800316 /* 2c) are they opening the /flash/hash ? (to start the process) */
Patrick Venture21c62c12018-11-09 17:46:58 -0800317 curr = &activeHash;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800318 active = &activeHashBlobID;
Patrick Venturec02849b2018-11-06 17:31:15 -0800319 }
320 else
321 {
Patrick Venture18235e62018-11-08 10:21:09 -0800322 curr = &activeImage;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800323 active = &activeImageBlobID;
Patrick Venturec02849b2018-11-06 17:31:15 -0800324 }
325
Patrick Venture6e307a72018-11-09 18:21:17 -0800326 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
327 * only non-external data pathway -- but this is just a more generic
328 * approach to that.
329 */
330 if (d->handler)
331 {
332 /* If the data handler open call fails, open fails. */
333 if (!d->handler->open())
334 {
335 return false;
336 }
337 }
338
Patrick Venture18235e62018-11-08 10:21:09 -0800339 /* 2d) are they opening the /flash/tarball ? (to start the UBI process)
Patrick Ventured6461d62018-11-09 17:30:25 -0800340 * 2e) are they opening the /flash/image ? (to start the process)
341 * 2...) are they opening the /flash/... ? (to start the process)
Patrick Venture18235e62018-11-08 10:21:09 -0800342 */
Patrick Venture18235e62018-11-08 10:21:09 -0800343 auto h = std::find_if(
344 handlers.begin(), handlers.end(),
345 [&path](const auto& iter) { return (iter.blobName == path); });
346 if (h == handlers.end())
347 {
348 return false;
349 }
350
351 /* Ok, so we found a handler that matched, so call open() */
352 if (!h->handler->open(path))
353 {
354 return false;
355 }
356
357 curr->flags = flags;
358 curr->dataHandler = d->handler;
359 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800360 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800361
362 lookup[session] = curr;
363
Patrick Venturedb253bf2018-11-09 19:36:03 -0800364 blobIDs.push_back(*active);
365
Patrick Venture991910a2018-11-09 19:43:48 -0800366 fileOpen = true;
367
Patrick Venture18235e62018-11-08 10:21:09 -0800368 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700369}
Patrick Venture53977962018-11-02 18:59:35 -0700370
Patrick Venture18235e62018-11-08 10:21:09 -0800371/**
372 * The write command really just grabs the data from wherever it is and sends it
373 * to the image handler. It's the image handler's responsibility to deal with
374 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800375 *
376 * This receives a session from the blob manager, therefore it is always called
377 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800378 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700379bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
380 const std::vector<uint8_t>& data)
381{
Patrick Venture18235e62018-11-08 10:21:09 -0800382 auto item = lookup.find(session);
383 if (item == lookup.end())
384 {
385 return false;
386 }
387
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800388 /* Prevent writing during verification. */
389 if (state == UpdateState::verificationStarted)
390 {
391 return false;
392 }
393
Patrick Venture18235e62018-11-08 10:21:09 -0800394 std::vector<std::uint8_t> bytes;
395
Patrick Venture05abf7e2018-11-09 11:02:56 -0800396 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800397 {
398 bytes = data;
399 }
400 else
401 {
402 /* little endian required per design, and so on, but TODO: do endianness
403 * with boost.
404 */
405 struct ExtChunkHdr header;
406
407 if (data.size() != sizeof(header))
408 {
409 return false;
410 }
411
412 std::memcpy(&header, data.data(), data.size());
413 bytes = item->second->dataHandler->copyFrom(header.length);
414 }
415
416 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700417}
Patrick Venture18235e62018-11-08 10:21:09 -0800418
Patrick Venture8c535332018-11-08 15:58:00 -0800419/*
420 * If the active session (image or hash) is over LPC, this allows
421 * configuring it. This option is only available before you start
422 * writing data for the given item (image or hash). This will return
423 * false at any other part. -- the lpc handler portion will know to return
424 * false.
425 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700426bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
427 const std::vector<uint8_t>& data)
428{
Patrick Venture8c535332018-11-08 15:58:00 -0800429 auto item = lookup.find(session);
430 if (item == lookup.end())
431 {
432 return false;
433 }
434
Patrick Venture05abf7e2018-11-09 11:02:56 -0800435 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800436 {
437 return false;
438 }
439
440 return item->second->dataHandler->write(data);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700441}
Patrick Venture8c535332018-11-08 15:58:00 -0800442
Patrick Ventured6461d62018-11-09 17:30:25 -0800443/*
444 * If this command is called on the session for the hash image, it'll
445 * trigger a systemd service `verify_image.service` to attempt to verify
446 * the image. Before doing this, if the transport mechanism is not IPMI
447 * BT, it'll shut down the mechanism used for transport preventing the
448 * host from updating anything.
449 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700450bool FirmwareBlobHandler::commit(uint16_t session,
451 const std::vector<uint8_t>& data)
452{
453 return false;
454}
Patrick Ventured6461d62018-11-09 17:30:25 -0800455
456/*
457 * Close must be called on the firmware image before triggering
458 * verification via commit. Once the verification is complete, you can
459 * then close the hash file.
460 *
461 * If the `verify_image.service` returned success, closing the hash file
462 * will have a specific behavior depending on the update. If it's UBI,
463 * it'll perform the install. If it's static layout, it'll do nothing. The
464 * verify_image service in the static layout case is responsible for placing
465 * the file in the correct staging position.
466 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700467bool FirmwareBlobHandler::close(uint16_t session)
468{
Patrick Venture68bb1432018-11-09 20:08:48 -0800469 auto item = lookup.find(session);
470 if (item == lookup.end())
471 {
472 return false;
473 }
474
475 if (item->second->dataHandler)
476 {
477 item->second->dataHandler->close();
478 }
479 item->second->imageHandler->close();
480 item->second->state = Session::State::closed;
481 /* Do not delete the active blob_id from the list of blob_ids, because that
482 * blob_id indicates there is data stored. Delete will destroy it.
483 */
484
485 lookup.erase(item);
486
Patrick Venture991910a2018-11-09 19:43:48 -0800487 fileOpen = false;
488
Patrick Venture68bb1432018-11-09 20:08:48 -0800489 /* TODO: implement other aspects of closing out a session, such as changing
490 * global firmware state.
491 */
492 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700493}
Patrick Ventured6461d62018-11-09 17:30:25 -0800494
Patrick Venturec7ca2912018-11-02 11:38:33 -0700495bool FirmwareBlobHandler::expire(uint16_t session)
496{
497 return false;
498}
Patrick Ventured6461d62018-11-09 17:30:25 -0800499
500/*
501 * Currently, the design does not provide this with a function, however,
502 * it will likely change to support reading data back.
503 */
504std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
505 uint32_t offset,
506 uint32_t requestedSize)
507{
508 return {};
509}
510
Patrick Venturec7ca2912018-11-02 11:38:33 -0700511} // namespace blobs