blob: fc4438e942da4848f33d1e18b2afa0679a2c6feb [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.
84 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070085bool FirmwareBlobHandler::deleteBlob(const std::string& path)
86{
87 return false;
88}
Patrick Venture53977962018-11-02 18:59:35 -070089
Patrick Ventured6461d62018-11-09 17:30:25 -080090/*
91 * Stat on the files will return information such as what supported
92 * transport mechanisms are available.
93 *
94 * Stat on an active file or hash will return information such as the size
95 * of the data cached, and any additional pertinent information. The
96 * blob_state on the active files will return the state of the update.
97 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070098bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
99{
Patrick Venture46637c82018-11-06 15:20:24 -0800100 /* We know we support this path because canHandle is called ahead */
101 if (path == FirmwareBlobHandler::activeImageBlobID)
102 {
103 /* We need to return information for the image that's staged. */
104 }
105 else if (path == FirmwareBlobHandler::activeHashBlobID)
106 {
107 /* We need to return information for the hash that's staged. */
108 }
109 else
110 {
111 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800112 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800113 meta->size = 0;
114
115 /* The generic blob_ids state is only the bits related to the transport
Patrick Ventured6461d62018-11-09 17:30:25 -0800116 * mechanisms.
117 */
Patrick Venture46637c82018-11-06 15:20:24 -0800118 return true;
119 }
120
Patrick Venturec7ca2912018-11-02 11:38:33 -0700121 return false;
122}
Patrick Venture53977962018-11-02 18:59:35 -0700123
Patrick Venturec02849b2018-11-06 17:31:15 -0800124/*
Patrick Ventured6461d62018-11-09 17:30:25 -0800125 * Return stat information on an open session. It therefore must be an active
126 * handle to either the active image or active hash.
127 *
128 * The stat() and sessionstat() commands will return the same information in
129 * many cases, therefore the logic will be combined.
130 *
131 * TODO: combine the logic for stat and sessionstat().
132 */
133bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
134{
135 /*
136 * Return session specific information.
137 */
138 return false;
139}
140
141/*
Patrick Venturec02849b2018-11-06 17:31:15 -0800142 * If you open /flash/image or /flash/tarball, or /flash/hash it will
143 * interpret the open flags and perform whatever actions are required for
144 * that update process. The session returned can be used immediately for
145 * sending data down, without requiring one to open the new active file.
146 *
147 * If you open the active flash image or active hash it will let you
148 * overwrite pieces, depending on the state.
149 *
150 * Once the verification process has started the active files cannot be
151 * opened.
152 *
153 * You can only have one open session at a time. Which means, you can only
154 * have one file open at a time. Trying to open the hash blob_id while you
155 * still have the flash image blob_id open will fail. Opening the flash
156 * blob_id when it is already open will fail.
157 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700158bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
159 const std::string& path)
160{
Patrick Venturec02849b2018-11-06 17:31:15 -0800161 /* Check that they've opened for writing - read back not supported. */
162 if ((flags & OpenFlags::write) == 0)
163 {
164 return false;
165 }
166
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800167 /* Is the verification process underway? */
168 if (state == UpdateState::verificationStarted)
169 {
170 return false;
171 }
Patrick Venturec02849b2018-11-06 17:31:15 -0800172
173 /* Is there an open session already? We only allow one at a time.
174 * TODO: Temporarily using a simple boolean flag until there's a full
175 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800176 *
177 * Further on this, if there's an active session to the hash we don't allow
178 * re-opening the image, and if we have the image open, we don't allow
179 * opening the hash. This design decision may be re-evaluated, and changed
180 * to only allow one session per object type (of the two types). But,
181 * consider if the hash is open, do we want to allow writing to the image?
182 * And why would we? But, really, the point of no-return is once the
183 * verification process has begun -- which is done via commit() on the hash
184 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700185 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800186 if (fileOpen)
187 {
188 return false;
189 }
190
191 /* There are two abstractions at play, how you get the data and how you
192 * handle that data. such that, whether the data comes from the PCI bridge
193 * or LPC bridge is not connected to whether the data goes into a static
194 * layout flash update or a UBI tarball.
195 */
196
197 /* Check the flags for the transport mechanism: if none match we don't
Patrick Venture18235e62018-11-08 10:21:09 -0800198 * support what they request.
199 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800200 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800201 {
202 return false;
203 }
204
205 /* 2) there isn't, so what are they opening? */
206 if (path == activeImageBlobID)
207 {
208 /* 2a) are they opening the active image? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800209 * already started one (due to canHandleBlob's behavior).
210 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800211 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800212 }
213 else if (path == activeHashBlobID)
214 {
215 /* 2b) are they opening the active hash? this can only happen if they
Patrick Venture18235e62018-11-08 10:21:09 -0800216 * already started one (due to canHandleBlob's behavior).
217 */
Patrick Venture21c62c12018-11-09 17:46:58 -0800218 return false;
Patrick Venturec02849b2018-11-06 17:31:15 -0800219 }
Patrick Venture18235e62018-11-08 10:21:09 -0800220
221 /* How are they expecting to copy this data? */
222 auto d = std::find_if(
223 transports.begin(), transports.end(),
224 [&flags](const auto& iter) { return (iter.bitmask & flags); });
225 if (d == transports.end())
Patrick Venturec02849b2018-11-06 17:31:15 -0800226 {
Patrick Venture18235e62018-11-08 10:21:09 -0800227 return false;
228 }
229
230 /* We found the transport handler they requested, no surprise since
231 * above we verify they selected at least one we wanted.
232 */
233 Session* curr;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800234 const std::string* active;
Patrick Venture18235e62018-11-08 10:21:09 -0800235
236 if (path == hashBlobID)
237 {
Patrick Venturec02849b2018-11-06 17:31:15 -0800238 /* 2c) are they opening the /flash/hash ? (to start the process) */
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800239
Patrick Venture21c62c12018-11-09 17:46:58 -0800240 curr = &activeHash;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800241 active = &activeHashBlobID;
Patrick Venturec02849b2018-11-06 17:31:15 -0800242 }
243 else
244 {
Patrick Venture18235e62018-11-08 10:21:09 -0800245 curr = &activeImage;
Patrick Venturedb253bf2018-11-09 19:36:03 -0800246 active = &activeImageBlobID;
Patrick Venturec02849b2018-11-06 17:31:15 -0800247 }
248
Patrick Venture18235e62018-11-08 10:21:09 -0800249 /* 2d) are they opening the /flash/tarball ? (to start the UBI process)
Patrick Ventured6461d62018-11-09 17:30:25 -0800250 * 2e) are they opening the /flash/image ? (to start the process)
251 * 2...) are they opening the /flash/... ? (to start the process)
Patrick Venture18235e62018-11-08 10:21:09 -0800252 */
Patrick Venture18235e62018-11-08 10:21:09 -0800253 auto h = std::find_if(
254 handlers.begin(), handlers.end(),
255 [&path](const auto& iter) { return (iter.blobName == path); });
256 if (h == handlers.end())
257 {
258 return false;
259 }
260
261 /* Ok, so we found a handler that matched, so call open() */
262 if (!h->handler->open(path))
263 {
264 return false;
265 }
266
267 curr->flags = flags;
268 curr->dataHandler = d->handler;
269 curr->imageHandler = h->handler;
Patrick Venturecd310222018-11-09 18:47:13 -0800270 curr->state = Session::State::open;
Patrick Venture18235e62018-11-08 10:21:09 -0800271
272 lookup[session] = curr;
273
Patrick Venturedb253bf2018-11-09 19:36:03 -0800274 blobIDs.push_back(*active);
275
Patrick Venture18235e62018-11-08 10:21:09 -0800276 return true;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700277}
Patrick Venture53977962018-11-02 18:59:35 -0700278
Patrick Venture18235e62018-11-08 10:21:09 -0800279/**
280 * The write command really just grabs the data from wherever it is and sends it
281 * to the image handler. It's the image handler's responsibility to deal with
282 * the data provided.
Patrick Ventured6461d62018-11-09 17:30:25 -0800283 *
284 * This receives a session from the blob manager, therefore it is always called
285 * between open() and close().
Patrick Venture18235e62018-11-08 10:21:09 -0800286 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700287bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
288 const std::vector<uint8_t>& data)
289{
Patrick Venture18235e62018-11-08 10:21:09 -0800290 auto item = lookup.find(session);
291 if (item == lookup.end())
292 {
293 return false;
294 }
295
Patrick Ventureb1b68fc2018-11-09 09:37:04 -0800296 /* Prevent writing during verification. */
297 if (state == UpdateState::verificationStarted)
298 {
299 return false;
300 }
301
Patrick Venture18235e62018-11-08 10:21:09 -0800302 std::vector<std::uint8_t> bytes;
303
Patrick Venture05abf7e2018-11-09 11:02:56 -0800304 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture18235e62018-11-08 10:21:09 -0800305 {
306 bytes = data;
307 }
308 else
309 {
310 /* little endian required per design, and so on, but TODO: do endianness
311 * with boost.
312 */
313 struct ExtChunkHdr header;
314
315 if (data.size() != sizeof(header))
316 {
317 return false;
318 }
319
320 std::memcpy(&header, data.data(), data.size());
321 bytes = item->second->dataHandler->copyFrom(header.length);
322 }
323
324 return item->second->imageHandler->write(offset, bytes);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700325}
Patrick Venture18235e62018-11-08 10:21:09 -0800326
Patrick Venture8c535332018-11-08 15:58:00 -0800327/*
328 * If the active session (image or hash) is over LPC, this allows
329 * configuring it. This option is only available before you start
330 * writing data for the given item (image or hash). This will return
331 * false at any other part. -- the lpc handler portion will know to return
332 * false.
333 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700334bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
335 const std::vector<uint8_t>& data)
336{
Patrick Venture8c535332018-11-08 15:58:00 -0800337 auto item = lookup.find(session);
338 if (item == lookup.end())
339 {
340 return false;
341 }
342
Patrick Venture05abf7e2018-11-09 11:02:56 -0800343 if (item->second->flags & UpdateFlags::ipmi)
Patrick Venture8c535332018-11-08 15:58:00 -0800344 {
345 return false;
346 }
347
348 return item->second->dataHandler->write(data);
Patrick Venturec7ca2912018-11-02 11:38:33 -0700349}
Patrick Venture8c535332018-11-08 15:58:00 -0800350
Patrick Ventured6461d62018-11-09 17:30:25 -0800351/*
352 * If this command is called on the session for the hash image, it'll
353 * trigger a systemd service `verify_image.service` to attempt to verify
354 * the image. Before doing this, if the transport mechanism is not IPMI
355 * BT, it'll shut down the mechanism used for transport preventing the
356 * host from updating anything.
357 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700358bool FirmwareBlobHandler::commit(uint16_t session,
359 const std::vector<uint8_t>& data)
360{
361 return false;
362}
Patrick Ventured6461d62018-11-09 17:30:25 -0800363
364/*
365 * Close must be called on the firmware image before triggering
366 * verification via commit. Once the verification is complete, you can
367 * then close the hash file.
368 *
369 * If the `verify_image.service` returned success, closing the hash file
370 * will have a specific behavior depending on the update. If it's UBI,
371 * it'll perform the install. If it's static layout, it'll do nothing. The
372 * verify_image service in the static layout case is responsible for placing
373 * the file in the correct staging position.
374 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700375bool FirmwareBlobHandler::close(uint16_t session)
376{
377 return false;
378}
Patrick Ventured6461d62018-11-09 17:30:25 -0800379
Patrick Venturec7ca2912018-11-02 11:38:33 -0700380bool FirmwareBlobHandler::expire(uint16_t session)
381{
382 return false;
383}
Patrick Ventured6461d62018-11-09 17:30:25 -0800384
385/*
386 * Currently, the design does not provide this with a function, however,
387 * it will likely change to support reading data back.
388 */
389std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
390 uint32_t offset,
391 uint32_t requestedSize)
392{
393 return {};
394}
395
Patrick Venturec7ca2912018-11-02 11:38:33 -0700396} // namespace blobs