blob: 5578b186d54f31243647dc0f5531389e5424d0da [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{
183 /*
184 * Return session specific information.
185 */
186 return false;
187}
188
189/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800190 * If you open /flash/image or /flash/tarball, or /flash/hash it will
191 * interpret the open flags and perform whatever actions are required for
192 * that update process. The session returned can be used immediately for
193 * sending data down, without requiring one to open the new active file.
194 *
195 * If you open the active flash image or active hash it will let you
196 * overwrite pieces, depending on the state.
197 *
198 * Once the verification process has started the active files cannot be
199 * opened.
200 *
201 * You can only have one open session at a time. Which means, you can only
202 * have one file open at a time. Trying to open the hash blob_id while you
203 * still have the flash image blob_id open will fail. Opening the flash
204 * blob_id when it is already open will fail.
205 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700206bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
207 const std::string& path)
208{
Patrick Venture6e307a72018-11-09 18:21:17 -0800209 /* Check that they've opened for writing - read back not currently
210 * supported.
211 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800212 if ((flags & OpenFlags::write) == 0)
213 {
214 return false;
215 }
216
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800217 /* Is the verification process underway? */
218 if (state == UpdateState::verificationStarted)
219 {
220 return false;
221 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800222
223 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800224 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800225 * TODO: Temporarily using a simple boolean flag until there's a full
226 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800227 *
228 * Further on this, if there's an active session to the hash we don't allow
229 * re-opening the image, and if we have the image open, we don't allow
230 * opening the hash. This design decision may be re-evaluated, and changed
231 * to only allow one session per object type (of the two types). But,
232 * consider if the hash is open, do we want to allow writing to the image?
233 * And why would we? But, really, the point of no-return is once the
234 * verification process has begun -- which is done via commit() on the hash
235 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700236 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800237 if (fileOpen)
238 {
239 return false;
240 }
241
242 /* There are two abstractions at play, how you get the data and how you
243 * handle that data. such that, whether the data comes from the PCI bridge
244 * or LPC bridge is not connected to whether the data goes into a static
245 * layout flash update or a UBI tarball.
246 */
247
248 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800249 * support what they request.
250 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800251 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800252 {
253 return false;
254 }
255
256 /* 2) there isn't, so what are they opening? */
257 if (path == activeImageBlobID)
258 {
259 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800260 * already started one (due to canHandleBlob's behavior).
261 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800262 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800263 }
264 else if (path == activeHashBlobID)
265 {
266 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800267 * already started one (due to canHandleBlob's behavior).
268 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800269 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800270 }
Patrick Venture18235e62018-11-08 10:21:09 -0800271
272 /* How are they expecting to copy this data? */
273 auto d = std::find_if(
274 transports.begin(), transports.end(),
275 [&flags](const auto& iter) { return (iter.bitmask & flags); });
276 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800277 {
Patrick Venture18235e62018-11-08 10:21:09 -0800278 return false;
279 }
280
281 /* We found the transport handler they requested, no surprise since
282 * above we verify they selected at least one we wanted.
283 */
284 Session* curr;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800285 const std::string* active;
Patrick Venture18235e62018-11-08 10:21:09 -0800286
287 if (path == hashBlobID)
288 {
Patrick Venturec02849b2018-11-06 17:31:15 -0800289 /* 2c) are they opening the /flash/hash ? (to start the process) */
Patrick Venture21c62c12018-11-09 17:46:58 -0800290 curr = &activeHash;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800291 active = &activeHashBlobID;
Patrick Venturec02849b2018-11-06 17:31:15 -0800292 }
293 else
294 {
Patrick Venture18235e62018-11-08 10:21:09 -0800295 curr = &activeImage;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800296 active = &activeImageBlobID;
Patrick Venturec02849b2018-11-06 17:31:15 -0800297 }
298
Patrick Venture6e307a72018-11-09 18:21:17 -0800299 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
300 * only non-external data pathway -- but this is just a more generic
301 * approach to that.
302 */
303 if (d->handler)
304 {
305 /* If the data handler open call fails, open fails. */
306 if (!d->handler->open())
307 {
308 return false;
309 }
310 }
311
Patrick Venture18235e62018-11-08 10:21:09 -0800312 /* 2d) are they opening the /flash/tarball ? (to start the UBI process)
Patrick Ventured6461d62018-11-09 17:30:25 -0800313 * 2e) are they opening the /flash/image ? (to start the process)
314 * 2...) are they opening the /flash/... ? (to start the process)
Patrick Venture18235e62018-11-08 10:21:09 -0800315 */
Patrick Venture18235e62018-11-08 10:21:09 -0800316 auto h = std::find_if(
317 handlers.begin(), handlers.end(),
318 [&path](const auto& iter) { return (iter.blobName == path); });
319 if (h == handlers.end())
320 {
321 return false;
322 }
323
324 /* Ok, so we found a handler that matched, so call open() */
325 if (!h->handler->open(path))
326 {
327 return false;
328 }
329
330 curr->flags = flags;
331 curr->dataHandler = d->handler;
332 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800333 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800334
335 lookup[session] = curr;
336
Patrick Venturedb253bf2018-11-09 19:36:03 -0800337 blobIDs.push_back(*active);
338
Patrick Venture991910a2018-11-09 19:43:48 -0800339 fileOpen = true;
340
Patrick Venture18235e62018-11-08 10:21:09 -0800341 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700342}
Patrick Venture53977962018-11-02 18:59:35 -0700343
Patrick Venture18235e62018-11-08 10:21:09 -0800344/**
345 * The write command really just grabs the data from wherever it is and sends it
346 * to the image handler. It's the image handler's responsibility to deal with
347 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800348 *
349 * This receives a session from the blob manager, therefore it is always called
350 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800351 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700352bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
353 const std::vector<uint8_t>& data)
354{
Patrick Venture18235e62018-11-08 10:21:09 -0800355 auto item = lookup.find(session);
356 if (item == lookup.end())
357 {
358 return false;
359 }
360
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800361 /* Prevent writing during verification. */
362 if (state == UpdateState::verificationStarted)
363 {
364 return false;
365 }
366
Patrick Venture18235e62018-11-08 10:21:09 -0800367 std::vector<std::uint8_t> bytes;
368
Patrick Venture05abf7e2018-11-09 11:02:56 -0800369 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800370 {
371 bytes = data;
372 }
373 else
374 {
375 /* little endian required per design, and so on, but TODO: do endianness
376 * with boost.
377 */
378 struct ExtChunkHdr header;
379
380 if (data.size() != sizeof(header))
381 {
382 return false;
383 }
384
385 std::memcpy(&header, data.data(), data.size());
386 bytes = item->second->dataHandler->copyFrom(header.length);
387 }
388
389 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700390}
Patrick Venture18235e62018-11-08 10:21:09 -0800391
Patrick Venture8c535332018-11-08 15:58:00 -0800392/*
393 * If the active session (image or hash) is over LPC, this allows
394 * configuring it. This option is only available before you start
395 * writing data for the given item (image or hash). This will return
396 * false at any other part. -- the lpc handler portion will know to return
397 * false.
398 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700399bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
400 const std::vector<uint8_t>& data)
401{
Patrick Venture8c535332018-11-08 15:58:00 -0800402 auto item = lookup.find(session);
403 if (item == lookup.end())
404 {
405 return false;
406 }
407
Patrick Venture05abf7e2018-11-09 11:02:56 -0800408 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800409 {
410 return false;
411 }
412
413 return item->second->dataHandler->write(data);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700414}
Patrick Venture8c535332018-11-08 15:58:00 -0800415
Patrick Ventured6461d62018-11-09 17:30:25 -0800416/*
417 * If this command is called on the session for the hash image, it'll
418 * trigger a systemd service `verify_image.service` to attempt to verify
419 * the image. Before doing this, if the transport mechanism is not IPMI
420 * BT, it'll shut down the mechanism used for transport preventing the
421 * host from updating anything.
422 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700423bool FirmwareBlobHandler::commit(uint16_t session,
424 const std::vector<uint8_t>& data)
425{
426 return false;
427}
Patrick Ventured6461d62018-11-09 17:30:25 -0800428
429/*
430 * Close must be called on the firmware image before triggering
431 * verification via commit. Once the verification is complete, you can
432 * then close the hash file.
433 *
434 * If the `verify_image.service` returned success, closing the hash file
435 * will have a specific behavior depending on the update. If it's UBI,
436 * it'll perform the install. If it's static layout, it'll do nothing. The
437 * verify_image service in the static layout case is responsible for placing
438 * the file in the correct staging position.
439 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700440bool FirmwareBlobHandler::close(uint16_t session)
441{
Patrick Venture68bb1432018-11-09 20:08:48 -0800442 auto item = lookup.find(session);
443 if (item == lookup.end())
444 {
445 return false;
446 }
447
448 if (item->second->dataHandler)
449 {
450 item->second->dataHandler->close();
451 }
452 item->second->imageHandler->close();
453 item->second->state = Session::State::closed;
454 /* Do not delete the active blob_id from the list of blob_ids, because that
455 * blob_id indicates there is data stored. Delete will destroy it.
456 */
457
458 lookup.erase(item);
459
Patrick Venture991910a2018-11-09 19:43:48 -0800460 fileOpen = false;
461
Patrick Venture68bb1432018-11-09 20:08:48 -0800462 /* TODO: implement other aspects of closing out a session, such as changing
463 * global firmware state.
464 */
465 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700466}
Patrick Ventured6461d62018-11-09 17:30:25 -0800467
Patrick Venturec7ca2912018-11-02 11:38:33 -0700468bool FirmwareBlobHandler::expire(uint16_t session)
469{
470 return false;
471}
Patrick Ventured6461d62018-11-09 17:30:25 -0800472
473/*
474 * Currently, the design does not provide this with a function, however,
475 * it will likely change to support reading data back.
476 */
477std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
478 uint32_t offset,
479 uint32_t requestedSize)
480{
481 return {};
482}
483
Patrick Venturec7ca2912018-11-02 11:38:33 -0700484} // namespace blobs