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 | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 13 | |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 14 | std::unique_ptr<GenericBlobInterface> |
Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 15 | FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 192d60f | 2018-11-06 11:11:59 -0800 | [diff] [blame] | 16 | const std::vector<std::string>& firmwares, std::uint32_t transports) |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 17 | { |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame^] | 18 | /* There must be at least one. */ |
| 19 | if (!firmwares.size()) |
| 20 | { |
| 21 | return nullptr; |
| 22 | } |
| 23 | |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 24 | std::vector<std::string> blobs = firmwares; |
| 25 | blobs.push_back(hashBlobID); |
| 26 | |
| 27 | return std::make_unique<FirmwareBlobHandler>(blobs, transports); |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 28 | } |
| 29 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 30 | bool FirmwareBlobHandler::canHandleBlob(const std::string& path) |
| 31 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 32 | /* Check if the path is in our supported list (or active list). */ |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 33 | if (std::count(blobIDs.begin(), blobIDs.end(), path)) |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 34 | { |
| 35 | return true; |
| 36 | } |
| 37 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 38 | return false; |
| 39 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 40 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 41 | std::vector<std::string> FirmwareBlobHandler::getBlobIds() |
| 42 | { |
Patrick Venture | fa6c4d9 | 2018-11-02 18:34:53 -0700 | [diff] [blame] | 43 | /* |
| 44 | * Grab the list of supported firmware. |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 45 | * |
| 46 | * If there's an open firmware session, it'll already be present in the |
| 47 | * list as "/flash/active/image", and if the hash has started, |
| 48 | * "/flash/active/hash" regardless of mechanism. This is done in the open |
| 49 | * comamnd, no extra work is required here. |
Patrick Venture | fa6c4d9 | 2018-11-02 18:34:53 -0700 | [diff] [blame] | 50 | */ |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 51 | return blobIDs; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 52 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 53 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 54 | bool FirmwareBlobHandler::deleteBlob(const std::string& path) |
| 55 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 56 | /* |
| 57 | * Per the design, this mean abort, and this will trigger whatever |
| 58 | * appropriate actions are required to abort the process. |
| 59 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 60 | return false; |
| 61 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 62 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 63 | bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta) |
| 64 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 65 | /* |
| 66 | * Stat on the files will return information such as what supported |
| 67 | * transport mechanisms are available. |
| 68 | * |
| 69 | * Stat on an active file or hash will return information such as the size |
| 70 | * of the data cached, and any additional pertinent information. The |
| 71 | * blob_state on the active files will return the state of the update. |
| 72 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 73 | return false; |
| 74 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 75 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 76 | bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags, |
| 77 | const std::string& path) |
| 78 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 79 | /* |
| 80 | * If you open /flash/image or /flash/tarball, or /flash/hash it will |
| 81 | * interpret the open flags and perform whatever actions are required for |
| 82 | * that update process. The session returned can be used immediately for |
| 83 | * sending data down, without requiring one to open the new active file. |
| 84 | * |
| 85 | * If you open the active flash image or active hash it will let you |
| 86 | * overwrite pieces, depending on the state. |
| 87 | * Once the verification process has started the active files cannot be |
| 88 | * opened. |
| 89 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 92 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 93 | std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session, |
| 94 | uint32_t offset, |
| 95 | uint32_t requestedSize) |
| 96 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 97 | /* |
| 98 | * Currently, the design does not provide this with a function, however, |
| 99 | * it will likely change to support reading data back. |
| 100 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 101 | return {}; |
| 102 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 103 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 104 | bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset, |
| 105 | const std::vector<uint8_t>& data) |
| 106 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 107 | /* |
| 108 | * This will do whatever behavior is expected by mechanism - likely will |
| 109 | * just call the specific write handler. |
| 110 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset, |
| 114 | const std::vector<uint8_t>& data) |
| 115 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 116 | /* |
| 117 | * If the active session (image or hash) is over LPC, this allows |
| 118 | * configuring it. This option is only available before you start |
| 119 | * writing data for the given item (image or hash). This will return |
| 120 | * false at any other part. |
| 121 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | bool FirmwareBlobHandler::commit(uint16_t session, |
| 125 | const std::vector<uint8_t>& data) |
| 126 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 127 | /* |
| 128 | * If this command is called on the session for the hash image, it'll |
| 129 | * trigger a systemd service `verify_image.service` to attempt to verify |
| 130 | * the image. Before doing this, if the transport mechanism is not IPMI |
| 131 | * BT, it'll shut down the mechanism used for transport preventing the |
| 132 | * host from updating anything. |
| 133 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | bool FirmwareBlobHandler::close(uint16_t session) |
| 137 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 138 | /* |
| 139 | * Close must be called on the firmware image before triggering |
| 140 | * verification via commit. Once the verification is complete, you can |
| 141 | * then close the hash file. |
| 142 | * |
| 143 | * If the `verify_image.service` returned success, closing the hash file |
| 144 | * will have a specific behavior depending on the update. If it's UBI, |
| 145 | * it'll perform the install. If it's static layout, it'll do nothing. The |
| 146 | * verify_image service in the static layout case is responsible for placing |
| 147 | * the file in the correct staging position. |
| 148 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta) |
| 152 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 153 | /* |
| 154 | * Return the supported mechanisms if it's the handler blob_id, versus |
| 155 | * the active one. |
| 156 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | bool FirmwareBlobHandler::expire(uint16_t session) |
| 160 | { |
| 161 | return false; |
| 162 | } |
| 163 | } // namespace blobs |