blob: eccac7317c6301107d436a6ddccaff8815b44648 [file] [log] [blame]
Patrick Venturec7ca2912018-11-02 11:38:33 -07001#include "firmware_handler.hpp"
2
Patrick Venture137ad2c2018-11-06 11:30:43 -08003#include <algorithm>
Patrick Venture192d60f2018-11-06 11:11:59 -08004#include <cstdint>
Patrick Venture68cf64f2018-11-06 10:46:51 -08005#include <memory>
Patrick Venturefa6c4d92018-11-02 18:34:53 -07006#include <string>
7#include <vector>
8
Patrick Venturec7ca2912018-11-02 11:38:33 -07009namespace blobs
10{
11
Patrick Venture21be45a2018-11-06 12:08:52 -080012const std::string FirmwareBlobHandler::hashBlobID = "/flash/hash";
Patrick Venture7b9256f2018-11-06 15:06:04 -080013const std::string FirmwareBlobHandler::activeImageBlobID =
14 "/flash/active/image";
15const std::string FirmwareBlobHandler::activeHashBlobID = "/flash/active/hash";
Patrick Venture4cceb8e2018-11-06 11:56:48 -080016
Patrick Venture68cf64f2018-11-06 10:46:51 -080017std::unique_ptr<GenericBlobInterface>
Patrick Venture148cd652018-11-06 10:59:47 -080018 FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture46637c82018-11-06 15:20:24 -080019 const std::vector<std::string>& firmwares, std::uint16_t transports)
Patrick Venture68cf64f2018-11-06 10:46:51 -080020{
Patrick Venture52854622018-11-06 12:30:00 -080021 /* There must be at least one. */
22 if (!firmwares.size())
23 {
24 return nullptr;
25 }
26
Patrick Venture4cceb8e2018-11-06 11:56:48 -080027 std::vector<std::string> blobs = firmwares;
28 blobs.push_back(hashBlobID);
29
30 return std::make_unique<FirmwareBlobHandler>(blobs, transports);
Patrick Venture68cf64f2018-11-06 10:46:51 -080031}
32
Patrick Venturec7ca2912018-11-02 11:38:33 -070033bool FirmwareBlobHandler::canHandleBlob(const std::string& path)
34{
Patrick Venture53977962018-11-02 18:59:35 -070035 /* Check if the path is in our supported list (or active list). */
Patrick Venture4cceb8e2018-11-06 11:56:48 -080036 if (std::count(blobIDs.begin(), blobIDs.end(), path))
Patrick Venture137ad2c2018-11-06 11:30:43 -080037 {
38 return true;
39 }
40
Patrick Venturec7ca2912018-11-02 11:38:33 -070041 return false;
42}
Patrick Venture53977962018-11-02 18:59:35 -070043
Patrick Venturec7ca2912018-11-02 11:38:33 -070044std::vector<std::string> FirmwareBlobHandler::getBlobIds()
45{
Patrick Venturefa6c4d92018-11-02 18:34:53 -070046 /*
47 * Grab the list of supported firmware.
Patrick Venture137ad2c2018-11-06 11:30:43 -080048 *
49 * If there's an open firmware session, it'll already be present in the
50 * list as "/flash/active/image", and if the hash has started,
51 * "/flash/active/hash" regardless of mechanism. This is done in the open
52 * comamnd, no extra work is required here.
Patrick Venturefa6c4d92018-11-02 18:34:53 -070053 */
Patrick Venture4cceb8e2018-11-06 11:56:48 -080054 return blobIDs;
Patrick Venturec7ca2912018-11-02 11:38:33 -070055}
Patrick Venture53977962018-11-02 18:59:35 -070056
Patrick Venturec7ca2912018-11-02 11:38:33 -070057bool FirmwareBlobHandler::deleteBlob(const std::string& path)
58{
Patrick Venture53977962018-11-02 18:59:35 -070059 /*
60 * Per the design, this mean abort, and this will trigger whatever
61 * appropriate actions are required to abort the process.
62 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070063 return false;
64}
Patrick Venture53977962018-11-02 18:59:35 -070065
Patrick Venturec7ca2912018-11-02 11:38:33 -070066bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
67{
Patrick Venture53977962018-11-02 18:59:35 -070068 /*
69 * Stat on the files will return information such as what supported
70 * transport mechanisms are available.
71 *
72 * Stat on an active file or hash will return information such as the size
73 * of the data cached, and any additional pertinent information. The
74 * blob_state on the active files will return the state of the update.
75 */
Patrick Venture46637c82018-11-06 15:20:24 -080076
77 /* We know we support this path because canHandle is called ahead */
78 if (path == FirmwareBlobHandler::activeImageBlobID)
79 {
80 /* We need to return information for the image that's staged. */
81 }
82 else if (path == FirmwareBlobHandler::activeHashBlobID)
83 {
84 /* We need to return information for the hash that's staged. */
85 }
86 else
87 {
88 /* They are requesting information about the generic blob_id. */
89 meta->blobState = transports;
90 meta->size = 0;
91
92 /* The generic blob_ids state is only the bits related to the transport
93 * mechanisms. */
94 return true;
95 }
96
Patrick Venturec7ca2912018-11-02 11:38:33 -070097 return false;
98}
Patrick Venture53977962018-11-02 18:59:35 -070099
Patrick Venturec02849b2018-11-06 17:31:15 -0800100/*
101 * If you open /flash/image or /flash/tarball, or /flash/hash it will
102 * interpret the open flags and perform whatever actions are required for
103 * that update process. The session returned can be used immediately for
104 * sending data down, without requiring one to open the new active file.
105 *
106 * If you open the active flash image or active hash it will let you
107 * overwrite pieces, depending on the state.
108 *
109 * Once the verification process has started the active files cannot be
110 * opened.
111 *
112 * You can only have one open session at a time. Which means, you can only
113 * have one file open at a time. Trying to open the hash blob_id while you
114 * still have the flash image blob_id open will fail. Opening the flash
115 * blob_id when it is already open will fail.
116 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700117bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
118 const std::string& path)
119{
Patrick Venturec02849b2018-11-06 17:31:15 -0800120 /* Check that they've opened for writing - read back not supported. */
121 if ((flags & OpenFlags::write) == 0)
122 {
123 return false;
124 }
125
126 /* TODO: Is the verification process underway? */
127
128 /* Is there an open session already? We only allow one at a time.
129 * TODO: Temporarily using a simple boolean flag until there's a full
130 * session object to check.
Patrick Venture53977962018-11-02 18:59:35 -0700131 */
Patrick Venturec02849b2018-11-06 17:31:15 -0800132 if (fileOpen)
133 {
134 return false;
135 }
136
137 /* There are two abstractions at play, how you get the data and how you
138 * handle that data. such that, whether the data comes from the PCI bridge
139 * or LPC bridge is not connected to whether the data goes into a static
140 * layout flash update or a UBI tarball.
141 */
142
143 /* Check the flags for the transport mechanism: if none match we don't
144 * support what they request. */
145 if ((flags & transports) == 0)
146 {
147 return false;
148 }
149
150 /* 2) there isn't, so what are they opening? */
151 if (path == activeImageBlobID)
152 {
153 /* 2a) are they opening the active image? this can only happen if they
154 * already started one (due to canHandleBlob's behavior). */
155 }
156 else if (path == activeHashBlobID)
157 {
158 /* 2b) are they opening the active hash? this can only happen if they
159 * already started one (due to canHandleBlob's behavior). */
160 }
161 else if (path == hashBlobID)
162 {
163 /* 2c) are they opening the /flash/hash ? (to start the process) */
164 }
165 else
166 {
167 /* 2d) are they opening the /flash/tarball ? (to start the UBI process)
168 */
169 /* 2e) are they opening the /flash/image ? (to start the process) */
170 /* 2...) are they opening the /flash/... ? (to start the process) */
171 }
172
Patrick Venturec7ca2912018-11-02 11:38:33 -0700173 return false;
174}
Patrick Venture53977962018-11-02 18:59:35 -0700175
Patrick Venturec7ca2912018-11-02 11:38:33 -0700176std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
177 uint32_t offset,
178 uint32_t requestedSize)
179{
Patrick Venture53977962018-11-02 18:59:35 -0700180 /*
181 * Currently, the design does not provide this with a function, however,
182 * it will likely change to support reading data back.
183 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700184 return {};
185}
Patrick Venture53977962018-11-02 18:59:35 -0700186
Patrick Venturec7ca2912018-11-02 11:38:33 -0700187bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
188 const std::vector<uint8_t>& data)
189{
Patrick Venture53977962018-11-02 18:59:35 -0700190 /*
191 * This will do whatever behavior is expected by mechanism - likely will
192 * just call the specific write handler.
193 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700194 return false;
195}
196bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
197 const std::vector<uint8_t>& data)
198{
Patrick Venture53977962018-11-02 18:59:35 -0700199 /*
200 * If the active session (image or hash) is over LPC, this allows
201 * configuring it. This option is only available before you start
202 * writing data for the given item (image or hash). This will return
203 * false at any other part.
204 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700205 return false;
206}
207bool FirmwareBlobHandler::commit(uint16_t session,
208 const std::vector<uint8_t>& data)
209{
Patrick Venture53977962018-11-02 18:59:35 -0700210 /*
211 * If this command is called on the session for the hash image, it'll
212 * trigger a systemd service `verify_image.service` to attempt to verify
213 * the image. Before doing this, if the transport mechanism is not IPMI
214 * BT, it'll shut down the mechanism used for transport preventing the
215 * host from updating anything.
216 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700217 return false;
218}
219bool FirmwareBlobHandler::close(uint16_t session)
220{
Patrick Venture53977962018-11-02 18:59:35 -0700221 /*
222 * Close must be called on the firmware image before triggering
223 * verification via commit. Once the verification is complete, you can
224 * then close the hash file.
225 *
226 * If the `verify_image.service` returned success, closing the hash file
227 * will have a specific behavior depending on the update. If it's UBI,
228 * it'll perform the install. If it's static layout, it'll do nothing. The
229 * verify_image service in the static layout case is responsible for placing
230 * the file in the correct staging position.
231 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700232 return false;
233}
234bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
235{
Patrick Venture53977962018-11-02 18:59:35 -0700236 /*
Patrick Venture46637c82018-11-06 15:20:24 -0800237 * Return session specific information.
Patrick Venture53977962018-11-02 18:59:35 -0700238 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700239 return false;
240}
241bool FirmwareBlobHandler::expire(uint16_t session)
242{
243 return false;
244}
245} // namespace blobs