blob: b828002e64f5154ad8885ca4d19498010fb152a2 [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 Venture192d60f2018-11-06 11:11:59 -080019 const std::vector<std::string>& firmwares, std::uint32_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 Venturec7ca2912018-11-02 11:38:33 -070076 return false;
77}
Patrick Venture53977962018-11-02 18:59:35 -070078
Patrick Venturec7ca2912018-11-02 11:38:33 -070079bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags,
80 const std::string& path)
81{
Patrick Venture53977962018-11-02 18:59:35 -070082 /*
83 * If you open /flash/image or /flash/tarball, or /flash/hash it will
84 * interpret the open flags and perform whatever actions are required for
85 * that update process. The session returned can be used immediately for
86 * sending data down, without requiring one to open the new active file.
87 *
88 * If you open the active flash image or active hash it will let you
89 * overwrite pieces, depending on the state.
90 * Once the verification process has started the active files cannot be
91 * opened.
92 */
Patrick Venturec7ca2912018-11-02 11:38:33 -070093 return false;
94}
Patrick Venture53977962018-11-02 18:59:35 -070095
Patrick Venturec7ca2912018-11-02 11:38:33 -070096std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session,
97 uint32_t offset,
98 uint32_t requestedSize)
99{
Patrick Venture53977962018-11-02 18:59:35 -0700100 /*
101 * Currently, the design does not provide this with a function, however,
102 * it will likely change to support reading data back.
103 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700104 return {};
105}
Patrick Venture53977962018-11-02 18:59:35 -0700106
Patrick Venturec7ca2912018-11-02 11:38:33 -0700107bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset,
108 const std::vector<uint8_t>& data)
109{
Patrick Venture53977962018-11-02 18:59:35 -0700110 /*
111 * This will do whatever behavior is expected by mechanism - likely will
112 * just call the specific write handler.
113 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700114 return false;
115}
116bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset,
117 const std::vector<uint8_t>& data)
118{
Patrick Venture53977962018-11-02 18:59:35 -0700119 /*
120 * If the active session (image or hash) is over LPC, this allows
121 * configuring it. This option is only available before you start
122 * writing data for the given item (image or hash). This will return
123 * false at any other part.
124 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700125 return false;
126}
127bool FirmwareBlobHandler::commit(uint16_t session,
128 const std::vector<uint8_t>& data)
129{
Patrick Venture53977962018-11-02 18:59:35 -0700130 /*
131 * If this command is called on the session for the hash image, it'll
132 * trigger a systemd service `verify_image.service` to attempt to verify
133 * the image. Before doing this, if the transport mechanism is not IPMI
134 * BT, it'll shut down the mechanism used for transport preventing the
135 * host from updating anything.
136 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700137 return false;
138}
139bool FirmwareBlobHandler::close(uint16_t session)
140{
Patrick Venture53977962018-11-02 18:59:35 -0700141 /*
142 * Close must be called on the firmware image before triggering
143 * verification via commit. Once the verification is complete, you can
144 * then close the hash file.
145 *
146 * If the `verify_image.service` returned success, closing the hash file
147 * will have a specific behavior depending on the update. If it's UBI,
148 * it'll perform the install. If it's static layout, it'll do nothing. The
149 * verify_image service in the static layout case is responsible for placing
150 * the file in the correct staging position.
151 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700152 return false;
153}
154bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
155{
Patrick Venture53977962018-11-02 18:59:35 -0700156 /*
157 * Return the supported mechanisms if it's the handler blob_id, versus
158 * the active one.
159 */
Patrick Venturec7ca2912018-11-02 11:38:33 -0700160 return false;
161}
162bool FirmwareBlobHandler::expire(uint16_t session)
163{
164 return false;
165}
166} // namespace blobs