blob: 21556e1a9fa9cd180be73a7d34e11abbe6b04e4b [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 Venturea78e39f2018-11-06 18:37:06 -080021 const std::vector<HandlerPack>& firmwares, std::uint16_t transports)
Patrick Venture68cf64f2018-11-06 10:46:51 -080022{
Patrick Venture52854622018-11-06 12:30:00 -080023 /* There must be at least one. */
24 if (!firmwares.size())
25 {
26 return nullptr;
27 }
28
Patrick Venturea78e39f2018-11-06 18:37:06 -080029 std::vector<std::string> blobs;
30 for (const auto& item : firmwares)
31 {
32 blobs.push_back(item.blobName);
33 }
Patrick Venture4cceb8e2018-11-06 11:56:48 -080034 blobs.push_back(hashBlobID);
35
Patrick Venturea78e39f2018-11-06 18:37:06 -080036 return std::make_unique<FirmwareBlobHandler>(firmwares, blobs, transports);
Patrick Venture68cf64f2018-11-06 10:46:51 -080037}
38
Patrick Venturec7ca2912018-11-02 11:38:33 -070039bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
40{
Patrick Venture53977962018-11-02 18:59:35 -070041 /* Check if the path is in our supported list (or active list). */
Patrick Venture4cceb8e2018-11-06 11:56:48 -080042 if (std::count(blobIDs.begin(), blobIDs.end(), path))
Patrick Venture137ad2c2018-11-06 11:30:43 -080043 {
44 return true;
45 }
46
Patrick Venturec7ca2912018-11-02 11:38:33 -070047 return false;
48}
Patrick Venture53977962018-11-02 18:59:35 -070049
Patrick Venturec7ca2912018-11-02 11:38:33 -070050std::vector<std::string> FirmwareBlobHandler::getBlobIds()
51{
Patrick Venturefa6c4d92018-11-02 18:34:53 -070052 /*
53 * Grab the list of supported firmware.
Patrick Venture137ad2c2018-11-06 11:30:43 -080054 *
55 * If there's an open firmware session, it'll already be present in the
56 * list as "/flash/active/image", and if the hash has started,
57 * "/flash/active/hash" regardless of mechanism. This is done in the open
58 * comamnd, no extra work is required here.
Patrick Venturefa6c4d92018-11-02 18:34:53 -070059 */
Patrick Venture4cceb8e2018-11-06 11:56:48 -080060 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070061}
Patrick Venture53977962018-11-02 18:59:35 -070062
Patrick Venturec7ca2912018-11-02 11:38:33 -070063bool FirmwareBlobHandler::deleteBlob(const std::string& path)
64{
Patrick Venture53977962018-11-02 18:59:35 -070065 /*
66 * Per the design, this mean abort, and this will trigger whatever
67 * appropriate actions are required to abort the process.
68 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070069 return false;
70}
Patrick Venture53977962018-11-02 18:59:35 -070071
Patrick Venturec7ca2912018-11-02 11:38:33 -070072bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
73{
Patrick Venture53977962018-11-02 18:59:35 -070074 /*
75 * Stat on the files will return information such as what supported
76 * transport mechanisms are available.
77 *
78 * Stat on an active file or hash will return information such as the size
79 * of the data cached, and any additional pertinent information. The
80 * blob_state on the active files will return the state of the update.
81 */
Patrick Venture46637c82018-11-06 15:20:24 -080082
83 /* We know we support this path because canHandle is called ahead */
84 if (path == FirmwareBlobHandler::activeImageBlobID)
85 {
86 /* We need to return information for the image that's staged. */
87 }
88 else if (path == FirmwareBlobHandler::activeHashBlobID)
89 {
90 /* We need to return information for the hash that's staged. */
91 }
92 else
93 {
94 /* They are requesting information about the generic blob_id. */
95 meta->blobState = transports;
96 meta->size = 0;
97
98 /* The generic blob_ids state is only the bits related to the transport
99 * mechanisms. */
100 return true;
101 }
102
Patrick Venturec7ca2912018-11-02 11:38:33 -0700103 return false;
104}
Patrick Venture53977962018-11-02 18:59:35 -0700105
Patrick Venturec02849b2018-11-06 17:31:15 -0800106/*
107 * If you open /flash/image or /flash/tarball, or /flash/hash it will
108 * interpret the open flags and perform whatever actions are required for
109 * that update process. The session returned can be used immediately for
110 * sending data down, without requiring one to open the new active file.
111 *
112 * If you open the active flash image or active hash it will let you
113 * overwrite pieces, depending on the state.
114 *
115 * Once the verification process has started the active files cannot be
116 * opened.
117 *
118 * You can only have one open session at a time. Which means, you can only
119 * have one file open at a time. Trying to open the hash blob_id while you
120 * still have the flash image blob_id open will fail. Opening the flash
121 * blob_id when it is already open will fail.
122 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700123bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
124 const std::string& path)
125{
Patrick Venturec02849b2018-11-06 17:31:15 -0800126 /* Check that they've opened for writing - read back not supported. */
127 if ((flags & OpenFlags::write) == 0)
128 {
129 return false;
130 }
131
132 /* TODO: Is the verification process underway? */
133
134 /* Is there an open session already? We only allow one at a time.
135 * TODO: Temporarily using a simple boolean flag until there's a full
136 * session object to check.
Patrick Venture53977962018-11-02 18:59:35 -0700137 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800138 if (fileOpen)
139 {
140 return false;
141 }
142
143 /* There are two abstractions at play, how you get the data and how you
144 * handle that data. such that, whether the data comes from the PCI bridge
145 * or LPC bridge is not connected to whether the data goes into a static
146 * layout flash update or a UBI tarball.
147 */
148
149 /* Check the flags for the transport mechanism: if none match we don't
150 * support what they request. */
151 if ((flags & transports) == 0)
152 {
153 return false;
154 }
155
156 /* 2) there isn't, so what are they opening? */
157 if (path == activeImageBlobID)
158 {
159 /* 2a) are they opening the active image? this can only happen if they
160 * already started one (due to canHandleBlob's behavior). */
161 }
162 else if (path == activeHashBlobID)
163 {
164 /* 2b) are they opening the active hash? this can only happen if they
165 * already started one (due to canHandleBlob's behavior). */
166 }
167 else if (path == hashBlobID)
168 {
169 /* 2c) are they opening the /flash/hash ? (to start the process) */
170 }
171 else
172 {
173 /* 2d) are they opening the /flash/tarball ? (to start the UBI process)
174 */
175 /* 2e) are they opening the /flash/image ? (to start the process) */
176 /* 2...) are they opening the /flash/... ? (to start the process) */
Patrick Venturea78e39f2018-11-06 18:37:06 -0800177
178 auto h = std::find_if(
179 handlers.begin(), handlers.end(),
180 [&path](const auto& iter) { return (iter.blobName == path); });
181 if (h != handlers.end())
182 {
183 /* Ok, so we found a handler that matched, so call open() */
184 if (h->handler->open(path))
185 {
186 /* open() succeeded. */
187 }
188 }
189
190 /* TODO: Actually handle storing this information. */
Patrick Venturec02849b2018-11-06 17:31:15 -0800191 }
192
Patrick Venturec7ca2912018-11-02 11:38:33 -0700193 return false;
194}
Patrick Venture53977962018-11-02 18:59:35 -0700195
Patrick Venturec7ca2912018-11-02 11:38:33 -0700196std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
197 uint32_t offset,
198 uint32_t requestedSize)
199{
Patrick Venture53977962018-11-02 18:59:35 -0700200 /*
201 * Currently, the design does not provide this with a function, however,
202 * it will likely change to support reading data back.
203 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700204 return {};
205}
Patrick Venture53977962018-11-02 18:59:35 -0700206
Patrick Venturec7ca2912018-11-02 11:38:33 -0700207bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
208 const std::vector<uint8_t>& data)
209{
Patrick Venture53977962018-11-02 18:59:35 -0700210 /*
211 * This will do whatever behavior is expected by mechanism - likely will
212 * just call the specific write handler.
213 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700214 return false;
215}
216bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
217 const std::vector<uint8_t>& data)
218{
Patrick Venture53977962018-11-02 18:59:35 -0700219 /*
220 * If the active session (image or hash) is over LPC, this allows
221 * configuring it. This option is only available before you start
222 * writing data for the given item (image or hash). This will return
223 * false at any other part.
224 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700225 return false;
226}
227bool FirmwareBlobHandler::commit(uint16_t session,
228 const std::vector<uint8_t>& data)
229{
Patrick Venture53977962018-11-02 18:59:35 -0700230 /*
231 * If this command is called on the session for the hash image, it'll
232 * trigger a systemd service `verify_image.service` to attempt to verify
233 * the image. Before doing this, if the transport mechanism is not IPMI
234 * BT, it'll shut down the mechanism used for transport preventing the
235 * host from updating anything.
236 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700237 return false;
238}
239bool FirmwareBlobHandler::close(uint16_t session)
240{
Patrick Venture53977962018-11-02 18:59:35 -0700241 /*
242 * Close must be called on the firmware image before triggering
243 * verification via commit. Once the verification is complete, you can
244 * then close the hash file.
245 *
246 * If the `verify_image.service` returned success, closing the hash file
247 * will have a specific behavior depending on the update. If it's UBI,
248 * it'll perform the install. If it's static layout, it'll do nothing. The
249 * verify_image service in the static layout case is responsible for placing
250 * the file in the correct staging position.
251 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700252 return false;
253}
254bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
255{
Patrick Venture53977962018-11-02 18:59:35 -0700256 /*
Patrick Venture46637c82018-11-06 15:20:24 -0800257 * Return session specific information.
Patrick Venture53977962018-11-02 18:59:35 -0700258 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700259 return false;
260}
261bool FirmwareBlobHandler::expire(uint16_t session)
262{
263 return false;
264}
265} // namespace blobs