Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 1 | #include "firmware_handler.hpp" |
| 2 | |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 3 | #include <algorithm> |
Patrick Venture | 192d60f | 2018-11-06 11:11:59 -0800 | [diff] [blame] | 4 | #include <cstdint> |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 5 | #include <memory> |
Patrick Venture | fa6c4d9 | 2018-11-02 18:34:53 -0700 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 9 | namespace blobs |
| 10 | { |
| 11 | |
Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 12 | const std::string FirmwareBlobHandler::hashBlobID = "/flash/hash"; |
Patrick Venture | 7b9256f | 2018-11-06 15:06:04 -0800 | [diff] [blame^] | 13 | const std::string FirmwareBlobHandler::activeImageBlobID = |
| 14 | "/flash/active/image"; |
| 15 | const std::string FirmwareBlobHandler::activeHashBlobID = "/flash/active/hash"; |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 16 | |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 17 | std::unique_ptr<GenericBlobInterface> |
Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 18 | FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 192d60f | 2018-11-06 11:11:59 -0800 | [diff] [blame] | 19 | const std::vector<std::string>& firmwares, std::uint32_t transports) |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 20 | { |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 21 | /* There must be at least one. */ |
| 22 | if (!firmwares.size()) |
| 23 | { |
| 24 | return nullptr; |
| 25 | } |
| 26 | |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 27 | std::vector<std::string> blobs = firmwares; |
| 28 | blobs.push_back(hashBlobID); |
| 29 | |
| 30 | return std::make_unique<FirmwareBlobHandler>(blobs, transports); |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 33 | bool FirmwareBlobHandler::canHandleBlob(const std::string& path) |
| 34 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 35 | /* Check if the path is in our supported list (or active list). */ |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 36 | if (std::count(blobIDs.begin(), blobIDs.end(), path)) |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 37 | { |
| 38 | return true; |
| 39 | } |
| 40 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 41 | return false; |
| 42 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 43 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 44 | std::vector<std::string> FirmwareBlobHandler::getBlobIds() |
| 45 | { |
Patrick Venture | fa6c4d9 | 2018-11-02 18:34:53 -0700 | [diff] [blame] | 46 | /* |
| 47 | * Grab the list of supported firmware. |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 48 | * |
| 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 Venture | fa6c4d9 | 2018-11-02 18:34:53 -0700 | [diff] [blame] | 53 | */ |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 54 | return blobIDs; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 55 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 56 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 57 | bool FirmwareBlobHandler::deleteBlob(const std::string& path) |
| 58 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 59 | /* |
| 60 | * Per the design, this mean abort, and this will trigger whatever |
| 61 | * appropriate actions are required to abort the process. |
| 62 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 63 | return false; |
| 64 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 65 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 66 | bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta) |
| 67 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 68 | /* |
| 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 Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 76 | return false; |
| 77 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 78 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 79 | bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags, |
| 80 | const std::string& path) |
| 81 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 82 | /* |
| 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 Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 93 | return false; |
| 94 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 95 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 96 | std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session, |
| 97 | uint32_t offset, |
| 98 | uint32_t requestedSize) |
| 99 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 100 | /* |
| 101 | * Currently, the design does not provide this with a function, however, |
| 102 | * it will likely change to support reading data back. |
| 103 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 104 | return {}; |
| 105 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 106 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 107 | bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset, |
| 108 | const std::vector<uint8_t>& data) |
| 109 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 110 | /* |
| 111 | * This will do whatever behavior is expected by mechanism - likely will |
| 112 | * just call the specific write handler. |
| 113 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset, |
| 117 | const std::vector<uint8_t>& data) |
| 118 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 119 | /* |
| 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 Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | bool FirmwareBlobHandler::commit(uint16_t session, |
| 128 | const std::vector<uint8_t>& data) |
| 129 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 130 | /* |
| 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 Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | bool FirmwareBlobHandler::close(uint16_t session) |
| 140 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 141 | /* |
| 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 Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta) |
| 155 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 156 | /* |
| 157 | * Return the supported mechanisms if it's the handler blob_id, versus |
| 158 | * the active one. |
| 159 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | bool FirmwareBlobHandler::expire(uint16_t session) |
| 163 | { |
| 164 | return false; |
| 165 | } |
| 166 | } // namespace blobs |