Patrick Venture | 3d1786b | 2018-08-01 11:19:24 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 3 | #include <vector> |
| 4 | |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 5 | #include "host-ipmid/ipmid-api.h" |
| 6 | |
| 7 | /* Clearer way to represent the subcommand size. */ |
| 8 | #define SUBCMD_SZ sizeof(uint8_t) |
| 9 | |
Patrick Venture | 3d1786b | 2018-08-01 11:19:24 -0700 | [diff] [blame] | 10 | /* |
| 11 | * flashStartTransfer -- starts file upload. |
| 12 | * flashDataBlock -- adds data to image file. |
| 13 | * flashDataFinish -- closes the file. |
| 14 | * |
| 15 | * flashStartHash -- starts uploading hash. |
| 16 | * flashDataHash -- adds data to the hash. |
| 17 | * flashDataVerify -- triggers verification. |
| 18 | * |
| 19 | * flashAbort -- abort everything. |
| 20 | * |
| 21 | * flashVerifyCheck -- Check if the verification has completed. |
| 22 | * |
| 23 | * flashVersion -- Get the version of this OEM Handler. |
| 24 | * |
| 25 | * flashRequestRegion -- Request the physical address for decode (bridge) |
| 26 | * flashDataExtBlock -- Provide image data via a bridge |
| 27 | * flashHashExtData -- Provide hash data via a bridge |
| 28 | */ |
| 29 | enum FlashSubCmds |
| 30 | { |
| 31 | /* Start a transfer. */ |
| 32 | flashStartTransfer = 0, |
| 33 | /* Data block. */ |
| 34 | flashDataBlock = 1, |
| 35 | /* Close file. */ |
| 36 | flashDataFinish = 2, |
| 37 | |
| 38 | /* Start a hash transfer. */ |
| 39 | flashStartHash = 3, |
| 40 | /* Add data to the hash. */ |
| 41 | flashHashData = 4, |
| 42 | /* Close out the hash file. */ |
| 43 | flashHashFinish = 5, |
| 44 | |
| 45 | /* Verify the flash image against the hast sent. */ |
| 46 | flashDataVerify = 6, |
| 47 | |
| 48 | /* Abort. */ |
| 49 | flashAbort = 7, |
| 50 | |
| 51 | /* |
| 52 | * Check if the verification is ready and was successful. |
| 53 | * If the response from the IPMI command is OK, check the |
| 54 | * response bytes to know if it's ready or still computing, |
| 55 | * or failed. |
| 56 | */ |
| 57 | flashVerifyCheck = 8, |
| 58 | |
| 59 | flashVersion = 9, |
| 60 | flashRequestRegion = 10, |
| 61 | flashDataExtBlock = 11, |
| 62 | flashHashExtData = 12, |
| 63 | flashMapRegionLpc = 13, |
| 64 | }; |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * StartTransfer expects a basic structure providing some information. |
| 68 | */ |
| 69 | struct StartTx |
| 70 | { |
| 71 | uint8_t cmd; |
| 72 | uint32_t length; /* Maximum image length is 4GiB (little-endian) */ |
| 73 | } __attribute__((packed)); |
| 74 | |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 75 | struct ChunkHdr |
| 76 | { |
| 77 | uint8_t cmd; |
| 78 | uint32_t offset; /* 0-based write offset */ |
| 79 | uint8_t data[]; |
| 80 | } __attribute__((packed)); |
| 81 | |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 82 | class UpdateInterface |
| 83 | { |
| 84 | public: |
| 85 | virtual ~UpdateInterface() = default; |
| 86 | |
Patrick Venture | 2c1205d | 2018-08-03 10:23:14 -0700 | [diff] [blame^] | 87 | /** |
| 88 | * Prepare to receive a BMC image and then a signature. |
| 89 | * |
| 90 | * @param[in] length - the size of the flash image. |
| 91 | * @return true on success, false otherwise. |
| 92 | */ |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 93 | virtual bool start(uint32_t length) = 0; |
Patrick Venture | 2c1205d | 2018-08-03 10:23:14 -0700 | [diff] [blame^] | 94 | |
| 95 | /** |
| 96 | * Attempt to write the bytes at the offset. |
| 97 | * |
| 98 | * @param[in] offset - the 0-based byte offset into the flash image. |
| 99 | * @param[in] bytes - the bytes to write. |
| 100 | * @return true on success, false otherwise. |
| 101 | */ |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 102 | virtual bool flashData(uint32_t offset, |
| 103 | const std::vector<uint8_t>& bytes) = 0; |
Patrick Venture | 2c1205d | 2018-08-03 10:23:14 -0700 | [diff] [blame^] | 104 | |
| 105 | /** |
| 106 | * Called to indicate the host is done sending the flash bytes. |
| 107 | */ |
| 108 | virtual bool flashFinish() = 0; |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | class FlashUpdate : public UpdateInterface |
| 112 | { |
| 113 | public: |
| 114 | FlashUpdate() = default; |
| 115 | ~FlashUpdate() = default; |
| 116 | FlashUpdate(const FlashUpdate&) = default; |
| 117 | FlashUpdate& operator=(const FlashUpdate&) = default; |
| 118 | FlashUpdate(FlashUpdate&&) = default; |
| 119 | FlashUpdate& operator=(FlashUpdate&&) = default; |
| 120 | |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 121 | bool start(uint32_t length) override; |
| 122 | |
Patrick Venture | 79e131f | 2018-08-01 13:34:35 -0700 | [diff] [blame] | 123 | bool flashData(uint32_t offset, const std::vector<uint8_t>& bytes) override; |
| 124 | |
Patrick Venture | 2c1205d | 2018-08-03 10:23:14 -0700 | [diff] [blame^] | 125 | bool flashFinish() override; |
| 126 | |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 127 | private: |
| 128 | /** |
| 129 | * Tries to close out and delete anything staged. |
| 130 | */ |
| 131 | void abortEverything(); |
| 132 | |
| 133 | /** |
| 134 | * Open all staged file handles you expect to use. |
| 135 | * |
| 136 | * @return false on failure. |
| 137 | */ |
| 138 | bool openEverything(); |
| 139 | }; |