blob: b51849d95c5b6b337fc58b511a080254ec09492e [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 Venture68cf64f2018-11-06 10:46:51 -08007#include <memory>
Patrick Venturefa6c4d92018-11-02 18:34:53 -07008#include <string>
9#include <vector>
10
Patrick Venturec7ca2912018-11-02 11:38:33 -070011namespace blobs
12{
13
Patrick Venture21be45a2018-11-06 12:08:52 -080014const std::string FirmwareBlobHandler::hashBlobID = "/flash/hash";
Patrick Venture7b9256f2018-11-06 15:06:04 -080015const std::string FirmwareBlobHandler::activeImageBlobID =
16 "/flash/active/image";
17const std::string FirmwareBlobHandler::activeHashBlobID = "/flash/active/hash";
Patrick Venture4cceb8e2018-11-06 11:56:48 -080018
Patrick Venture68cf64f2018-11-06 10:46:51 -080019std::unique_ptr<GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080020 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture1cde5f92018-11-07 08:26:47 -080021 const std::vector<HandlerPack>& firmwares,
22 const std::vector<DataHandlerPack>& transports)
Patrick Venture68cf64f2018-11-06 10:46:51 -080023{
Patrick Venture52854622018-11-06 12:30:00 -080024 /* There must be at least one. */
25 if (!firmwares.size())
26 {
27 return nullptr;
28 }
Patrick Venture1cde5f92018-11-07 08:26:47 -080029 if (!transports.size())
30 {
31 return nullptr;
32 }
Patrick Venture52854622018-11-06 12:30:00 -080033
Patrick Venturea78e39f2018-11-06 18:37:06 -080034 std::vector<std::string> blobs;
35 for (const auto& item : firmwares)
36 {
37 blobs.push_back(item.blobName);
38 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080039 blobs.push_back(hashBlobID);
40
Patrick Venture1cde5f92018-11-07 08:26:47 -080041 std::uint16_t bitmask = 0;
42 for (const auto& item : transports)
43 {
44 /* TODO: can use std::accumulate() unless I'm mistaken. :D */
45 bitmask |= item.bitmask;
46 }
47
48 return std::make_unique<FirmwareBlobHandler>(firmwares, blobs, transports,
49 bitmask);
Patrick Venture68cf64f2018-11-06 10:46:51 -080050}
51
Patrick Venturec7ca2912018-11-02 11:38:33 -070052bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
53{
Patrick Venture53977962018-11-02 18:59:35 -070054 /* Check if the path is in our supported list (or active list). */
Patrick Venture4cceb8e2018-11-06 11:56:48 -080055 if (std::count(blobIDs.begin(), blobIDs.end(), path))
Patrick Venture137ad2c2018-11-06 11:30:43 -080056 {
57 return true;
58 }
59
Patrick Venturec7ca2912018-11-02 11:38:33 -070060 return false;
61}
Patrick Venture53977962018-11-02 18:59:35 -070062
Patrick Venturec7ca2912018-11-02 11:38:33 -070063std::vector<std::string> FirmwareBlobHandler::getBlobIds()
64{
Patrick Venturefa6c4d92018-11-02 18:34:53 -070065 /*
66 * Grab the list of supported firmware.
Patrick Venture137ad2c2018-11-06 11:30:43 -080067 *
68 * If there's an open firmware session, it'll already be present in the
69 * list as "/flash/active/image", and if the hash has started,
70 * "/flash/active/hash" regardless of mechanism. This is done in the open
71 * comamnd, no extra work is required here.
Patrick Venturefa6c4d92018-11-02 18:34:53 -070072 */
Patrick Venture4cceb8e2018-11-06 11:56:48 -080073 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070074}
Patrick Venture53977962018-11-02 18:59:35 -070075
Patrick Venturec7ca2912018-11-02 11:38:33 -070076bool FirmwareBlobHandler::deleteBlob(const std::string& path)
77{
Patrick Venture53977962018-11-02 18:59:35 -070078 /*
79 * Per the design, this mean abort, and this will trigger whatever
80 * appropriate actions are required to abort the process.
81 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070082 return false;
83}
Patrick Venture53977962018-11-02 18:59:35 -070084
Patrick Venturec7ca2912018-11-02 11:38:33 -070085bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
86{
Patrick Venture53977962018-11-02 18:59:35 -070087 /*
88 * Stat on the files will return information such as what supported
89 * transport mechanisms are available.
90 *
91 * Stat on an active file or hash will return information such as the size
92 * of the data cached, and any additional pertinent information. The
93 * blob_state on the active files will return the state of the update.
94 */
Patrick Venture46637c82018-11-06 15:20:24 -080095
96 /* We know we support this path because canHandle is called ahead */
97 if (path == FirmwareBlobHandler::activeImageBlobID)
98 {
99 /* We need to return information for the image that's staged. */
100 }
101 else if (path == FirmwareBlobHandler::activeHashBlobID)
102 {
103 /* We need to return information for the hash that's staged. */
104 }
105 else
106 {
107 /* They are requesting information about the generic blob_id. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800108 meta->blobState = bitmask;
Patrick Venture46637c82018-11-06 15:20:24 -0800109 meta->size = 0;
110
111 /* The generic blob_ids state is only the bits related to the transport
112 * mechanisms. */
113 return true;
114 }
115
Patrick Venturec7ca2912018-11-02 11:38:33 -0700116 return false;
117}
Patrick Venture53977962018-11-02 18:59:35 -0700118
Patrick Venturec02849b2018-11-06 17:31:15 -0800119/*
120 * If you open /flash/image or /flash/tarball, or /flash/hash it will
121 * interpret the open flags and perform whatever actions are required for
122 * that update process. The session returned can be used immediately for
123 * sending data down, without requiring one to open the new active file.
124 *
125 * If you open the active flash image or active hash it will let you
126 * overwrite pieces, depending on the state.
127 *
128 * Once the verification process has started the active files cannot be
129 * opened.
130 *
131 * You can only have one open session at a time. Which means, you can only
132 * have one file open at a time. Trying to open the hash blob_id while you
133 * still have the flash image blob_id open will fail. Opening the flash
134 * blob_id when it is already open will fail.
135 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700136bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
137 const std::string& path)
138{
Patrick Venturec02849b2018-11-06 17:31:15 -0800139 /* Check that they've opened for writing - read back not supported. */
140 if ((flags & OpenFlags::write) == 0)
141 {
142 return false;
143 }
144
145 /* TODO: Is the verification process underway? */
146
147 /* Is there an open session already? We only allow one at a time.
148 * TODO: Temporarily using a simple boolean flag until there's a full
149 * session object to check.
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800150 *
151 * Further on this, if there's an active session to the hash we don't allow
152 * re-opening the image, and if we have the image open, we don't allow
153 * opening the hash. This design decision may be re-evaluated, and changed
154 * to only allow one session per object type (of the two types). But,
155 * consider if the hash is open, do we want to allow writing to the image?
156 * And why would we? But, really, the point of no-return is once the
157 * verification process has begun -- which is done via commit() on the hash
158 * blob_id, we no longer want to allow updating the contents.
Patrick Venture53977962018-11-02 18:59:35 -0700159 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800160 if (fileOpen)
161 {
162 return false;
163 }
164
165 /* There are two abstractions at play, how you get the data and how you
166 * handle that data. such that, whether the data comes from the PCI bridge
167 * or LPC bridge is not connected to whether the data goes into a static
168 * layout flash update or a UBI tarball.
169 */
170
171 /* Check the flags for the transport mechanism: if none match we don't
172 * support what they request. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800173 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800174 {
175 return false;
176 }
177
178 /* 2) there isn't, so what are they opening? */
179 if (path == activeImageBlobID)
180 {
181 /* 2a) are they opening the active image? this can only happen if they
182 * already started one (due to canHandleBlob's behavior). */
183 }
184 else if (path == activeHashBlobID)
185 {
186 /* 2b) are they opening the active hash? this can only happen if they
187 * already started one (due to canHandleBlob's behavior). */
188 }
189 else if (path == hashBlobID)
190 {
191 /* 2c) are they opening the /flash/hash ? (to start the process) */
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800192
193 /* Add active hash blob_id to canHandle list. */
194 blobIDs.push_back(activeHashBlobID);
Patrick Venturec02849b2018-11-06 17:31:15 -0800195 }
196 else
197 {
Patrick Venture1cde5f92018-11-07 08:26:47 -0800198 /* How are they expecting to copy this data? */
199 auto d = std::find_if(
200 transports.begin(), transports.end(),
201 [&flags](const auto& iter) { return (iter.bitmask & flags); });
Patrick Venturebf3399d2018-11-07 09:20:20 -0800202 if (d == transports.end())
Patrick Venture1cde5f92018-11-07 08:26:47 -0800203 {
Patrick Venturebf3399d2018-11-07 09:20:20 -0800204 return false;
Patrick Venture1cde5f92018-11-07 08:26:47 -0800205 }
206
Patrick Venturebf3399d2018-11-07 09:20:20 -0800207 /* We found the transport handler they requested, no surprise since
208 * above we verify they selected at least one we wanted.
209 */
210
Patrick Venturec02849b2018-11-06 17:31:15 -0800211 /* 2d) are they opening the /flash/tarball ? (to start the UBI process)
212 */
213 /* 2e) are they opening the /flash/image ? (to start the process) */
214 /* 2...) are they opening the /flash/... ? (to start the process) */
Patrick Venturea78e39f2018-11-06 18:37:06 -0800215
216 auto h = std::find_if(
217 handlers.begin(), handlers.end(),
218 [&path](const auto& iter) { return (iter.blobName == path); });
Patrick Venturebf3399d2018-11-07 09:20:20 -0800219 if (h == handlers.end())
Patrick Venturea78e39f2018-11-06 18:37:06 -0800220 {
Patrick Venturebf3399d2018-11-07 09:20:20 -0800221 return false;
Patrick Venturea78e39f2018-11-06 18:37:06 -0800222 }
223
Patrick Venturebf3399d2018-11-07 09:20:20 -0800224 /* Ok, so we found a handler that matched, so call open() */
225 if (!h->handler->open(path))
226 {
227 return false;
228 }
229
230 /* open() succeeded. */
231
Patrick Venturea78e39f2018-11-06 18:37:06 -0800232 /* TODO: Actually handle storing this information. */
Patrick Venture72388d72018-11-07 15:06:36 -0800233
Patrick Venture7c8b97e2018-11-08 09:14:30 -0800234 /* add active image blob_id to canHandle list. */
235 blobIDs.push_back(activeImageBlobID);
236
Patrick Venture72388d72018-11-07 15:06:36 -0800237 return true;
Patrick Venturec02849b2018-11-06 17:31:15 -0800238 }
239
Patrick Venturec7ca2912018-11-02 11:38:33 -0700240 return false;
241}
Patrick Venture53977962018-11-02 18:59:35 -0700242
Patrick Venturec7ca2912018-11-02 11:38:33 -0700243std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
244 uint32_t offset,
245 uint32_t requestedSize)
246{
Patrick Venture53977962018-11-02 18:59:35 -0700247 /*
248 * Currently, the design does not provide this with a function, however,
249 * it will likely change to support reading data back.
250 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700251 return {};
252}
Patrick Venture53977962018-11-02 18:59:35 -0700253
Patrick Venturec7ca2912018-11-02 11:38:33 -0700254bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
255 const std::vector<uint8_t>& data)
256{
Patrick Venture53977962018-11-02 18:59:35 -0700257 /*
258 * This will do whatever behavior is expected by mechanism - likely will
259 * just call the specific write handler.
260 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700261 return false;
262}
263bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
264 const std::vector<uint8_t>& data)
265{
Patrick Venture53977962018-11-02 18:59:35 -0700266 /*
267 * If the active session (image or hash) is over LPC, this allows
268 * configuring it. This option is only available before you start
269 * writing data for the given item (image or hash). This will return
270 * false at any other part.
271 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700272 return false;
273}
274bool FirmwareBlobHandler::commit(uint16_t session,
275 const std::vector<uint8_t>& data)
276{
Patrick Venture53977962018-11-02 18:59:35 -0700277 /*
278 * If this command is called on the session for the hash image, it'll
279 * trigger a systemd service `verify_image.service` to attempt to verify
280 * the image. Before doing this, if the transport mechanism is not IPMI
281 * BT, it'll shut down the mechanism used for transport preventing the
282 * host from updating anything.
283 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700284 return false;
285}
286bool FirmwareBlobHandler::close(uint16_t session)
287{
Patrick Venture53977962018-11-02 18:59:35 -0700288 /*
289 * Close must be called on the firmware image before triggering
290 * verification via commit. Once the verification is complete, you can
291 * then close the hash file.
292 *
293 * If the `verify_image.service` returned success, closing the hash file
294 * will have a specific behavior depending on the update. If it's UBI,
295 * it'll perform the install. If it's static layout, it'll do nothing. The
296 * verify_image service in the static layout case is responsible for placing
297 * the file in the correct staging position.
298 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700299 return false;
300}
301bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
302{
Patrick Venture53977962018-11-02 18:59:35 -0700303 /*
Patrick Venture46637c82018-11-06 15:20:24 -0800304 * Return session specific information.
Patrick Venture53977962018-11-02 18:59:35 -0700305 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700306 return false;
307}
308bool FirmwareBlobHandler::expire(uint16_t session)
309{
310 return false;
311}
312} // namespace blobs