blob: 0c4014eb85267e86da4753e3e9db0b2ad2f9dd81 [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 Venture53977962018-11-02 18:59:35 -0700150 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800151 if (fileOpen)
152 {
153 return false;
154 }
155
156 /* There are two abstractions at play, how you get the data and how you
157 * handle that data. such that, whether the data comes from the PCI bridge
158 * or LPC bridge is not connected to whether the data goes into a static
159 * layout flash update or a UBI tarball.
160 */
161
162 /* Check the flags for the transport mechanism: if none match we don't
163 * support what they request. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800164 if ((flags & bitmask) == 0)
Patrick Venturec02849b2018-11-06 17:31:15 -0800165 {
166 return false;
167 }
168
169 /* 2) there isn't, so what are they opening? */
170 if (path == activeImageBlobID)
171 {
172 /* 2a) are they opening the active image? this can only happen if they
173 * already started one (due to canHandleBlob's behavior). */
174 }
175 else if (path == activeHashBlobID)
176 {
177 /* 2b) are they opening the active hash? this can only happen if they
178 * already started one (due to canHandleBlob's behavior). */
179 }
180 else if (path == hashBlobID)
181 {
182 /* 2c) are they opening the /flash/hash ? (to start the process) */
183 }
184 else
185 {
Patrick Venture1cde5f92018-11-07 08:26:47 -0800186 /* How are they expecting to copy this data? */
187 auto d = std::find_if(
188 transports.begin(), transports.end(),
189 [&flags](const auto& iter) { return (iter.bitmask & flags); });
Patrick Venturebf3399d2018-11-07 09:20:20 -0800190 if (d == transports.end())
Patrick Venture1cde5f92018-11-07 08:26:47 -0800191 {
Patrick Venturebf3399d2018-11-07 09:20:20 -0800192 return false;
Patrick Venture1cde5f92018-11-07 08:26:47 -0800193 }
194
Patrick Venturebf3399d2018-11-07 09:20:20 -0800195 /* We found the transport handler they requested, no surprise since
196 * above we verify they selected at least one we wanted.
197 */
198
Patrick Venturec02849b2018-11-06 17:31:15 -0800199 /* 2d) are they opening the /flash/tarball ? (to start the UBI process)
200 */
201 /* 2e) are they opening the /flash/image ? (to start the process) */
202 /* 2...) are they opening the /flash/... ? (to start the process) */
Patrick Venturea78e39f2018-11-06 18:37:06 -0800203
204 auto h = std::find_if(
205 handlers.begin(), handlers.end(),
206 [&path](const auto& iter) { return (iter.blobName == path); });
Patrick Venturebf3399d2018-11-07 09:20:20 -0800207 if (h == handlers.end())
Patrick Venturea78e39f2018-11-06 18:37:06 -0800208 {
Patrick Venturebf3399d2018-11-07 09:20:20 -0800209 return false;
Patrick Venturea78e39f2018-11-06 18:37:06 -0800210 }
211
Patrick Venturebf3399d2018-11-07 09:20:20 -0800212 /* Ok, so we found a handler that matched, so call open() */
213 if (!h->handler->open(path))
214 {
215 return false;
216 }
217
218 /* open() succeeded. */
219
Patrick Venturea78e39f2018-11-06 18:37:06 -0800220 /* TODO: Actually handle storing this information. */
Patrick Venture72388d72018-11-07 15:06:36 -0800221
222 return true;
Patrick Venturec02849b2018-11-06 17:31:15 -0800223 }
224
Patrick Venturec7ca2912018-11-02 11:38:33 -0700225 return false;
226}
Patrick Venture53977962018-11-02 18:59:35 -0700227
Patrick Venturec7ca2912018-11-02 11:38:33 -0700228std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
229 uint32_t offset,
230 uint32_t requestedSize)
231{
Patrick Venture53977962018-11-02 18:59:35 -0700232 /*
233 * Currently, the design does not provide this with a function, however,
234 * it will likely change to support reading data back.
235 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700236 return {};
237}
Patrick Venture53977962018-11-02 18:59:35 -0700238
Patrick Venturec7ca2912018-11-02 11:38:33 -0700239bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
240 const std::vector<uint8_t>& data)
241{
Patrick Venture53977962018-11-02 18:59:35 -0700242 /*
243 * This will do whatever behavior is expected by mechanism - likely will
244 * just call the specific write handler.
245 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700246 return false;
247}
248bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
249 const std::vector<uint8_t>& data)
250{
Patrick Venture53977962018-11-02 18:59:35 -0700251 /*
252 * If the active session (image or hash) is over LPC, this allows
253 * configuring it. This option is only available before you start
254 * writing data for the given item (image or hash). This will return
255 * false at any other part.
256 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700257 return false;
258}
259bool FirmwareBlobHandler::commit(uint16_t session,
260 const std::vector<uint8_t>& data)
261{
Patrick Venture53977962018-11-02 18:59:35 -0700262 /*
263 * If this command is called on the session for the hash image, it'll
264 * trigger a systemd service `verify_image.service` to attempt to verify
265 * the image. Before doing this, if the transport mechanism is not IPMI
266 * BT, it'll shut down the mechanism used for transport preventing the
267 * host from updating anything.
268 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700269 return false;
270}
271bool FirmwareBlobHandler::close(uint16_t session)
272{
Patrick Venture53977962018-11-02 18:59:35 -0700273 /*
274 * Close must be called on the firmware image before triggering
275 * verification via commit. Once the verification is complete, you can
276 * then close the hash file.
277 *
278 * If the `verify_image.service` returned success, closing the hash file
279 * will have a specific behavior depending on the update. If it's UBI,
280 * it'll perform the install. If it's static layout, it'll do nothing. The
281 * verify_image service in the static layout case is responsible for placing
282 * the file in the correct staging position.
283 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700284 return false;
285}
286bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
287{
Patrick Venture53977962018-11-02 18:59:35 -0700288 /*
Patrick Venture46637c82018-11-06 15:20:24 -0800289 * Return session specific information.
Patrick Venture53977962018-11-02 18:59:35 -0700290 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700291 return false;
292}
293bool FirmwareBlobHandler::expire(uint16_t session)
294{
295 return false;
296}
297} // namespace blobs