blob: 3545fd29351fc8763de2cdb945c1622dc97ed92f [file] [log] [blame]
Patrick Venturec7ca2912018-11-02 11:38:33 -07001#include "firmware_handler.hpp"
2
Patrick Venturea78e39f2018-11-06 18:37:06 -08003#include "image_handler.hpp"
4
Patrick Venture137ad2c2018-11-06 11:30:43 -08005#include <algorithm>
Patrick Venture192d60f2018-11-06 11:11:59 -08006#include <cstdint>
Patrick Venture18235e62018-11-08 10:21:09 -08007#include <cstring>
Patrick Venture68cf64f2018-11-06 10:46:51 -08008#include <memory>
Patrick Venturefa6c4d92018-11-02 18:34:53 -07009#include <string>
10#include <vector>
11
Patrick Venturec7ca2912018-11-02 11:38:33 -070012namespace blobs
13{
14
Patrick Venture21be45a2018-11-06 12:08:52 -080015const std::string FirmwareBlobHandler::hashBlobID = "/flash/hash";
Patrick Venture7b9256f2018-11-06 15:06:04 -080016const std::string FirmwareBlobHandler::activeImageBlobID =
17 "/flash/active/image";
18const std::string FirmwareBlobHandler::activeHashBlobID = "/flash/active/hash";
Patrick Venture4cceb8e2018-11-06 11:56:48 -080019
Patrick Venture68cf64f2018-11-06 10:46:51 -080020std::unique_ptr<GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080021 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture1cde5f92018-11-07 08:26:47 -080022 const std::vector<HandlerPack>& firmwares,
23 const std::vector<DataHandlerPack>& transports)
Patrick Venture68cf64f2018-11-06 10:46:51 -080024{
Patrick Venture52854622018-11-06 12:30:00 -080025 /* There must be at least one. */
26 if (!firmwares.size())
27 {
28 return nullptr;
29 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080030 if (!transports.size())
31 {
32 return nullptr;
33 }
Patrick Venture52854622018-11-06 12:30:00 -080034
Patrick Venturea78e39f2018-11-06 18:37:06 -080035 std::vector<std::string> blobs;
36 for (const auto& item : firmwares)
37 {
38 blobs.push_back(item.blobName);
39 }
Patrick Venture18235e62018-11-08 10:21:09 -080040
41 if (0 == std::count(blobs.begin(), blobs.end(), hashBlobID))
42 {
43 return nullptr;
44 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080045
Patrick Venture1cde5f92018-11-07 08:26:47 -080046 std::uint16_t bitmask = 0;
47 for (const auto& item : transports)
48 {
49 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
50 bitmask |= item.bitmask;
51 }
52
53 return std::make_unique<FirmwareBlobHandler>(firmwares, blobs, transports,
54 bitmask);
Patrick Venture68cf64f2018-11-06 10:46:51 -080055}
56
Patrick Ventured6461d62018-11-09 17:30:25 -080057/* Check if the path is in our supported list (or active list). */
Patrick Venturec7ca2912018-11-02 11:38:33 -070058bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
59{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080060 if (std::count(blobIDs.begin(), blobIDs.end(), path))
Patrick Venture137ad2c2018-11-06 11:30:43 -080061 {
62 return true;
63 }
64
Patrick Venturec7ca2912018-11-02 11:38:33 -070065 return false;
66}
Patrick Venture53977962018-11-02 18:59:35 -070067
Patrick Ventured6461d62018-11-09 17:30:25 -080068/*
69 * Grab the list of supported firmware.
70 *
71 * If there's an open firmware session, it'll already be present in the
72 * list as "/flash/active/image", and if the hash has started,
73 * "/flash/active/hash" regardless of mechanism. This is done in the open
74 * comamnd, no extra work is required here.
75 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070076std::vector<std::string> FirmwareBlobHandler::getBlobIds()
77{
Patrick Venture4cceb8e2018-11-06 11:56:48 -080078 return blobIDs;
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 * Per the design, this mean abort, and this will trigger whatever
83 * appropriate actions are required to abort the process.
Patrick Venture9e5aab22018-11-09 20:49:28 -080084 *
85 * You cannot delete a blob that has an open handle in the system, therefore
86 * this is never called if there's an open session. Guaranteed by the blob
87 * manager.
Patrick Ventured6461d62018-11-09 17:30:25 -080088 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070089bool FirmwareBlobHandler::deleteBlob(const std::string& path)
90{
Patrick Venture9e5aab22018-11-09 20:49:28 -080091 const std::string* toDelete;
92
93 if (path == hashBlobID || path == activeHashBlobID)
94 {
95 /* They're deleting the hash. */
96 toDelete = &activeHashBlobID;
97 }
98 else
99 {
100 /* They're deleting the image. */
101 toDelete = &activeImageBlobID;
102 }
103
104 auto it = std::find_if(
105 blobIDs.begin(), blobIDs.end(),
106 [toDelete](const auto& iter) { return (iter == *toDelete); });
107 if (it == blobIDs.end())
108 {
109 /* Somehow they've asked to delete something we didn't say we could
110 * handle.
111 */
112 return false;
113 }
114
115 blobIDs.erase(it);
116
117 /* TODO: Handle aborting the process and fixing up the state. */
118
119 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700120}
Patrick Venture53977962018-11-02 18:59:35 -0700121
Patrick Ventured6461d62018-11-09 17:30:25 -0800122/*
123 * Stat on the files will return information such as what supported
124 * transport mechanisms are available.
125 *
126 * Stat on an active file or hash will return information such as the size
127 * of the data cached, and any additional pertinent information. The
128 * blob_state on the active files will return the state of the update.
129 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700130bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
131{
Patrick Venture46637c82018-11-06 15:20:24 -0800132 /* We know we support this path because canHandle is called ahead */
133 if (path == FirmwareBlobHandler::activeImageBlobID)
134 {
135 /* We need to return information for the image that's staged. */
136 }
137 else if (path == FirmwareBlobHandler::activeHashBlobID)
138 {
139 /* We need to return information for the hash that's staged. */
140 }
141 else
142 {
143 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800144 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800145 meta->size = 0;
146
147 /* The generic blob_ids state is only the bits related to the transport
Patrick Ventured6461d62018-11-09 17:30:25 -0800148 * mechanisms.
149 */
Patrick Venture46637c82018-11-06 15:20:24 -0800150 return true;
151 }
152
Patrick Venturec7ca2912018-11-02 11:38:33 -0700153 return false;
154}
Patrick Venture53977962018-11-02 18:59:35 -0700155
Patrick Venturec02849b2018-11-06 17:31:15 -0800156/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800157 * Return stat information on an open session. It therefore must be an active
158 * handle to either the active image or active hash.
159 *
160 * The stat() and sessionstat() commands will return the same information in
161 * many cases, therefore the logic will be combined.
162 *
163 * TODO: combine the logic for stat and sessionstat().
164 */
165bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
166{
167 /*
168 * Return session specific information.
169 */
170 return false;
171}
172
173/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800174 * If you open /flash/image or /flash/tarball, or /flash/hash it will
175 * interpret the open flags and perform whatever actions are required for
176 * that update process. The session returned can be used immediately for
177 * sending data down, without requiring one to open the new active file.
178 *
179 * If you open the active flash image or active hash it will let you
180 * overwrite pieces, depending on the state.
181 *
182 * Once the verification process has started the active files cannot be
183 * opened.
184 *
185 * You can only have one open session at a time. Which means, you can only
186 * have one file open at a time. Trying to open the hash blob_id while you
187 * still have the flash image blob_id open will fail. Opening the flash
188 * blob_id when it is already open will fail.
189 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700190bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
191 const std::string& path)
192{
Patrick Venture6e307a72018-11-09 18:21:17 -0800193 /* Check that they've opened for writing - read back not currently
194 * supported.
195 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800196 if ((flags & OpenFlags::write) == 0)
197 {
198 return false;
199 }
200
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800201 /* Is the verification process underway? */
202 if (state == UpdateState::verificationStarted)
203 {
204 return false;
205 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800206
207 /* Is there an open session already? We only allow one at a time.
Patrick Venture6e307a72018-11-09 18:21:17 -0800208 *
Patrick Venturec02849b2018-11-06 17:31:15 -0800209 * TODO: Temporarily using a simple boolean flag until there's a full
210 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800211 *
212 * Further on this, if there's an active session to the hash we don't allow
213 * re-opening the image, and if we have the image open, we don't allow
214 * opening the hash. This design decision may be re-evaluated, and changed
215 * to only allow one session per object type (of the two types). But,
216 * consider if the hash is open, do we want to allow writing to the image?
217 * And why would we? But, really, the point of no-return is once the
218 * verification process has begun -- which is done via commit() on the hash
219 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700220 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800221 if (fileOpen)
222 {
223 return false;
224 }
225
226 /* There are two abstractions at play, how you get the data and how you
227 * handle that data. such that, whether the data comes from the PCI bridge
228 * or LPC bridge is not connected to whether the data goes into a static
229 * layout flash update or a UBI tarball.
230 */
231
232 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800233 * support what they request.
234 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800235 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800236 {
237 return false;
238 }
239
240 /* 2) there isn't, so what are they opening? */
241 if (path == activeImageBlobID)
242 {
243 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800244 * already started one (due to canHandleBlob's behavior).
245 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800246 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800247 }
248 else if (path == activeHashBlobID)
249 {
250 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800251 * already started one (due to canHandleBlob's behavior).
252 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800253 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800254 }
Patrick Venture18235e62018-11-08 10:21:09 -0800255
256 /* How are they expecting to copy this data? */
257 auto d = std::find_if(
258 transports.begin(), transports.end(),
259 [&flags](const auto& iter) { return (iter.bitmask & flags); });
260 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800261 {
Patrick Venture18235e62018-11-08 10:21:09 -0800262 return false;
263 }
264
265 /* We found the transport handler they requested, no surprise since
266 * above we verify they selected at least one we wanted.
267 */
268 Session* curr;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800269 const std::string* active;
Patrick Venture18235e62018-11-08 10:21:09 -0800270
271 if (path == hashBlobID)
272 {
Patrick Venturec02849b2018-11-06 17:31:15 -0800273 /* 2c) are they opening the /flash/hash ? (to start the process) */
Patrick Venture21c62c12018-11-09 17:46:58 -0800274 curr = &activeHash;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800275 active = &activeHashBlobID;
Patrick Venturec02849b2018-11-06 17:31:15 -0800276 }
277 else
278 {
Patrick Venture18235e62018-11-08 10:21:09 -0800279 curr = &activeImage;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800280 active = &activeImageBlobID;
Patrick Venturec02849b2018-11-06 17:31:15 -0800281 }
282
Patrick Venture6e307a72018-11-09 18:21:17 -0800283 /* Elsewhere I do this check by checking "if ::ipmi" because that's the
284 * only non-external data pathway -- but this is just a more generic
285 * approach to that.
286 */
287 if (d->handler)
288 {
289 /* If the data handler open call fails, open fails. */
290 if (!d->handler->open())
291 {
292 return false;
293 }
294 }
295
Patrick Venture18235e62018-11-08 10:21:09 -0800296 /* 2d) are they opening the /flash/tarball ? (to start the UBI process)
Patrick Ventured6461d62018-11-09 17:30:25 -0800297 * 2e) are they opening the /flash/image ? (to start the process)
298 * 2...) are they opening the /flash/... ? (to start the process)
Patrick Venture18235e62018-11-08 10:21:09 -0800299 */
Patrick Venture18235e62018-11-08 10:21:09 -0800300 auto h = std::find_if(
301 handlers.begin(), handlers.end(),
302 [&path](const auto& iter) { return (iter.blobName == path); });
303 if (h == handlers.end())
304 {
305 return false;
306 }
307
308 /* Ok, so we found a handler that matched, so call open() */
309 if (!h->handler->open(path))
310 {
311 return false;
312 }
313
314 curr->flags = flags;
315 curr->dataHandler = d->handler;
316 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800317 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800318
319 lookup[session] = curr;
320
Patrick Venturedb253bf2018-11-09 19:36:03 -0800321 blobIDs.push_back(*active);
322
Patrick Venture991910a2018-11-09 19:43:48 -0800323 fileOpen = true;
324
Patrick Venture18235e62018-11-08 10:21:09 -0800325 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700326}
Patrick Venture53977962018-11-02 18:59:35 -0700327
Patrick Venture18235e62018-11-08 10:21:09 -0800328/**
329 * The write command really just grabs the data from wherever it is and sends it
330 * to the image handler. It's the image handler's responsibility to deal with
331 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800332 *
333 * This receives a session from the blob manager, therefore it is always called
334 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800335 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700336bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
337 const std::vector<uint8_t>& data)
338{
Patrick Venture18235e62018-11-08 10:21:09 -0800339 auto item = lookup.find(session);
340 if (item == lookup.end())
341 {
342 return false;
343 }
344
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800345 /* Prevent writing during verification. */
346 if (state == UpdateState::verificationStarted)
347 {
348 return false;
349 }
350
Patrick Venture18235e62018-11-08 10:21:09 -0800351 std::vector<std::uint8_t> bytes;
352
Patrick Venture05abf7e2018-11-09 11:02:56 -0800353 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800354 {
355 bytes = data;
356 }
357 else
358 {
359 /* little endian required per design, and so on, but TODO: do endianness
360 * with boost.
361 */
362 struct ExtChunkHdr header;
363
364 if (data.size() != sizeof(header))
365 {
366 return false;
367 }
368
369 std::memcpy(&header, data.data(), data.size());
370 bytes = item->second->dataHandler->copyFrom(header.length);
371 }
372
373 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700374}
Patrick Venture18235e62018-11-08 10:21:09 -0800375
Patrick Venture8c535332018-11-08 15:58:00 -0800376/*
377 * If the active session (image or hash) is over LPC, this allows
378 * configuring it. This option is only available before you start
379 * writing data for the given item (image or hash). This will return
380 * false at any other part. -- the lpc handler portion will know to return
381 * false.
382 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700383bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
384 const std::vector<uint8_t>& data)
385{
Patrick Venture8c535332018-11-08 15:58:00 -0800386 auto item = lookup.find(session);
387 if (item == lookup.end())
388 {
389 return false;
390 }
391
Patrick Venture05abf7e2018-11-09 11:02:56 -0800392 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800393 {
394 return false;
395 }
396
397 return item->second->dataHandler->write(data);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700398}
Patrick Venture8c535332018-11-08 15:58:00 -0800399
Patrick Ventured6461d62018-11-09 17:30:25 -0800400/*
401 * If this command is called on the session for the hash image, it'll
402 * trigger a systemd service `verify_image.service` to attempt to verify
403 * the image. Before doing this, if the transport mechanism is not IPMI
404 * BT, it'll shut down the mechanism used for transport preventing the
405 * host from updating anything.
406 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700407bool FirmwareBlobHandler::commit(uint16_t session,
408 const std::vector<uint8_t>& data)
409{
410 return false;
411}
Patrick Ventured6461d62018-11-09 17:30:25 -0800412
413/*
414 * Close must be called on the firmware image before triggering
415 * verification via commit. Once the verification is complete, you can
416 * then close the hash file.
417 *
418 * If the `verify_image.service` returned success, closing the hash file
419 * will have a specific behavior depending on the update. If it's UBI,
420 * it'll perform the install. If it's static layout, it'll do nothing. The
421 * verify_image service in the static layout case is responsible for placing
422 * the file in the correct staging position.
423 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700424bool FirmwareBlobHandler::close(uint16_t session)
425{
Patrick Venture68bb1432018-11-09 20:08:48 -0800426 auto item = lookup.find(session);
427 if (item == lookup.end())
428 {
429 return false;
430 }
431
432 if (item->second->dataHandler)
433 {
434 item->second->dataHandler->close();
435 }
436 item->second->imageHandler->close();
437 item->second->state = Session::State::closed;
438 /* Do not delete the active blob_id from the list of blob_ids, because that
439 * blob_id indicates there is data stored. Delete will destroy it.
440 */
441
442 lookup.erase(item);
443
Patrick Venture991910a2018-11-09 19:43:48 -0800444 fileOpen = false;
445
Patrick Venture68bb1432018-11-09 20:08:48 -0800446 /* TODO: implement other aspects of closing out a session, such as changing
447 * global firmware state.
448 */
449 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700450}
Patrick Ventured6461d62018-11-09 17:30:25 -0800451
Patrick Venturec7ca2912018-11-02 11:38:33 -0700452bool FirmwareBlobHandler::expire(uint16_t session)
453{
454 return false;
455}
Patrick Ventured6461d62018-11-09 17:30:25 -0800456
457/*
458 * Currently, the design does not provide this with a function, however,
459 * it will likely change to support reading data back.
460 */
461std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
462 uint32_t offset,
463 uint32_t requestedSize)
464{
465 return {};
466}
467
Patrick Venturec7ca2912018-11-02 11:38:33 -0700468} // namespace blobs