blob: f6c5472cdc42e3c3523edce45183699adef2c1ed [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); });
190 if (d != transports.end())
191 {
192 /* We found the transport handler they requested, no surprise since
193 * above we verify they selected at least one we wanted. */
194 }
195
Patrick Venturec02849b2018-11-06 17:31:15 -0800196 /* 2d) are they opening the /flash/tarball ? (to start the UBI process)
197 */
198 /* 2e) are they opening the /flash/image ? (to start the process) */
199 /* 2...) are they opening the /flash/... ? (to start the process) */
Patrick Venturea78e39f2018-11-06 18:37:06 -0800200
201 auto h = std::find_if(
202 handlers.begin(), handlers.end(),
203 [&path](const auto& iter) { return (iter.blobName == path); });
204 if (h != handlers.end())
205 {
206 /* Ok, so we found a handler that matched, so call open() */
207 if (h->handler->open(path))
208 {
209 /* open() succeeded. */
210 }
211 }
212
213 /* TODO: Actually handle storing this information. */
Patrick Venturec02849b2018-11-06 17:31:15 -0800214 }
215
Patrick Venturec7ca2912018-11-02 11:38:33 -0700216 return false;
217}
Patrick Venture53977962018-11-02 18:59:35 -0700218
Patrick Venturec7ca2912018-11-02 11:38:33 -0700219std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
220 uint32_t offset,
221 uint32_t requestedSize)
222{
Patrick Venture53977962018-11-02 18:59:35 -0700223 /*
224 * Currently, the design does not provide this with a function, however,
225 * it will likely change to support reading data back.
226 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700227 return {};
228}
Patrick Venture53977962018-11-02 18:59:35 -0700229
Patrick Venturec7ca2912018-11-02 11:38:33 -0700230bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
231 const std::vector<uint8_t>& data)
232{
Patrick Venture53977962018-11-02 18:59:35 -0700233 /*
234 * This will do whatever behavior is expected by mechanism - likely will
235 * just call the specific write handler.
236 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700237 return false;
238}
239bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
240 const std::vector<uint8_t>& data)
241{
Patrick Venture53977962018-11-02 18:59:35 -0700242 /*
243 * If the active session (image or hash) is over LPC, this allows
244 * configuring it. This option is only available before you start
245 * writing data for the given item (image or hash). This will return
246 * false at any other part.
247 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700248 return false;
249}
250bool FirmwareBlobHandler::commit(uint16_t session,
251 const std::vector<uint8_t>& data)
252{
Patrick Venture53977962018-11-02 18:59:35 -0700253 /*
254 * If this command is called on the session for the hash image, it'll
255 * trigger a systemd service `verify_image.service` to attempt to verify
256 * the image. Before doing this, if the transport mechanism is not IPMI
257 * BT, it'll shut down the mechanism used for transport preventing the
258 * host from updating anything.
259 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700260 return false;
261}
262bool FirmwareBlobHandler::close(uint16_t session)
263{
Patrick Venture53977962018-11-02 18:59:35 -0700264 /*
265 * Close must be called on the firmware image before triggering
266 * verification via commit. Once the verification is complete, you can
267 * then close the hash file.
268 *
269 * If the `verify_image.service` returned success, closing the hash file
270 * will have a specific behavior depending on the update. If it's UBI,
271 * it'll perform the install. If it's static layout, it'll do nothing. The
272 * verify_image service in the static layout case is responsible for placing
273 * the file in the correct staging position.
274 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700275 return false;
276}
277bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
278{
Patrick Venture53977962018-11-02 18:59:35 -0700279 /*
Patrick Venture46637c82018-11-06 15:20:24 -0800280 * Return session specific information.
Patrick Venture53977962018-11-02 18:59:35 -0700281 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700282 return false;
283}
284bool FirmwareBlobHandler::expire(uint16_t session)
285{
286 return false;
287}
288} // namespace blobs