blob: 7b60b05497ac6c9b1992cb9035a3a186e974e219 [file] [log] [blame]
Patrick Venture3d1786b2018-08-01 11:19:24 -07001#pragma once
2
Patrick Venture8ec019f2018-08-07 11:22:33 -07003#include <string>
Patrick Venture79e131f2018-08-01 13:34:35 -07004#include <vector>
5
Patrick Venture54c3b532018-08-01 11:45:49 -07006#include "host-ipmid/ipmid-api.h"
7
8/* Clearer way to represent the subcommand size. */
9#define SUBCMD_SZ sizeof(uint8_t)
10
Patrick Venture3d1786b2018-08-01 11:19:24 -070011/*
12 * flashStartTransfer -- starts file upload.
13 * flashDataBlock -- adds data to image file.
14 * flashDataFinish -- closes the file.
15 *
16 * flashStartHash -- starts uploading hash.
17 * flashDataHash -- adds data to the hash.
18 * flashDataVerify -- triggers verification.
19 *
20 * flashAbort -- abort everything.
21 *
22 * flashVerifyCheck -- Check if the verification has completed.
23 *
24 * flashVersion -- Get the version of this OEM Handler.
25 *
26 * flashRequestRegion -- Request the physical address for decode (bridge)
27 * flashDataExtBlock -- Provide image data via a bridge
28 * flashHashExtData -- Provide hash data via a bridge
29 */
30enum FlashSubCmds
31{
32 /* Start a transfer. */
33 flashStartTransfer = 0,
34 /* Data block. */
35 flashDataBlock = 1,
36 /* Close file. */
37 flashDataFinish = 2,
38
39 /* Start a hash transfer. */
40 flashStartHash = 3,
41 /* Add data to the hash. */
42 flashHashData = 4,
43 /* Close out the hash file. */
44 flashHashFinish = 5,
45
46 /* Verify the flash image against the hast sent. */
47 flashDataVerify = 6,
48
49 /* Abort. */
50 flashAbort = 7,
51
52 /*
53 * Check if the verification is ready and was successful.
54 * If the response from the IPMI command is OK, check the
55 * response bytes to know if it's ready or still computing,
56 * or failed.
57 */
58 flashVerifyCheck = 8,
59
60 flashVersion = 9,
61 flashRequestRegion = 10,
62 flashDataExtBlock = 11,
63 flashHashExtData = 12,
64 flashMapRegionLpc = 13,
65};
Patrick Venture54c3b532018-08-01 11:45:49 -070066
67/*
68 * StartTransfer expects a basic structure providing some information.
69 */
70struct StartTx
71{
72 uint8_t cmd;
73 uint32_t length; /* Maximum image length is 4GiB (little-endian) */
74} __attribute__((packed));
75
Patrick Venture79e131f2018-08-01 13:34:35 -070076struct ChunkHdr
77{
78 uint8_t cmd;
79 uint32_t offset; /* 0-based write offset */
80 uint8_t data[];
81} __attribute__((packed));
82
Patrick Venture54c3b532018-08-01 11:45:49 -070083class UpdateInterface
84{
85 public:
86 virtual ~UpdateInterface() = default;
87
Patrick Venture2c1205d2018-08-03 10:23:14 -070088 /**
89 * Prepare to receive a BMC image and then a signature.
90 *
91 * @param[in] length - the size of the flash image.
92 * @return true on success, false otherwise.
93 */
Patrick Venture54c3b532018-08-01 11:45:49 -070094 virtual bool start(uint32_t length) = 0;
Patrick Venture2c1205d2018-08-03 10:23:14 -070095
96 /**
97 * Attempt to write the bytes at the offset.
98 *
99 * @param[in] offset - the 0-based byte offset into the flash image.
100 * @param[in] bytes - the bytes to write.
101 * @return true on success, false otherwise.
102 */
Patrick Venture79e131f2018-08-01 13:34:35 -0700103 virtual bool flashData(uint32_t offset,
104 const std::vector<uint8_t>& bytes) = 0;
Patrick Venture2c1205d2018-08-03 10:23:14 -0700105
106 /**
107 * Called to indicate the host is done sending the flash bytes.
108 */
109 virtual bool flashFinish() = 0;
Patrick Venture8d9f7322018-08-03 10:39:13 -0700110
111 /**
112 * Prepare to receive a BMC image signature.
113 *
114 * @param[in] length - length of signature in bytes.
115 * @return true on success, false otherwise.
116 */
117 virtual bool startHash(uint32_t length) = 0;
Patrick Venturecfe66872018-08-03 13:32:33 -0700118
119 /**
120 * Attempt to write the bytes at the offset.
121 *
122 * @param[in] offset - the 0-based byte offset into the flash image.
123 * @param[in] bytes - the bytes to write.
124 * @return true on success, false otherwise.
125 */
126 virtual bool hashData(uint32_t offset,
127 const std::vector<uint8_t>& bytes) = 0;
Patrick Venturefbc7d192018-08-03 13:54:21 -0700128
129 /**
130 * Called to indicate the host is done sending the hash bytes.
131 */
132 virtual bool hashFinish() = 0;
Patrick Venture1cb87d22018-08-03 18:22:09 -0700133
134 /**
135 * Kick off the flash image verification process.
136 *
137 * @return true if it was started succesfully.
138 */
139 virtual bool startDataVerification() = 0;
Patrick Venture5c251ca2018-08-03 18:31:01 -0700140
141 /**
142 * Attempt to abort everything.
143 *
144 * @return true if aborted, false if unable or failed.
145 */
146 virtual bool abortUpdate() = 0;
Patrick Venture54c3b532018-08-01 11:45:49 -0700147};
148
149class FlashUpdate : public UpdateInterface
150{
151 public:
Patrick Venture8ec019f2018-08-07 11:22:33 -0700152 FlashUpdate(const std::string& stagingPath) :
153 flashLength(0), flashFd(nullptr), tmpPath(stagingPath){};
154 ~FlashUpdate();
155
Patrick Venture54c3b532018-08-01 11:45:49 -0700156 FlashUpdate(const FlashUpdate&) = default;
157 FlashUpdate& operator=(const FlashUpdate&) = default;
158 FlashUpdate(FlashUpdate&&) = default;
159 FlashUpdate& operator=(FlashUpdate&&) = default;
160
Patrick Venture54c3b532018-08-01 11:45:49 -0700161 bool start(uint32_t length) override;
Patrick Venture79e131f2018-08-01 13:34:35 -0700162 bool flashData(uint32_t offset, const std::vector<uint8_t>& bytes) override;
Patrick Venture2c1205d2018-08-03 10:23:14 -0700163 bool flashFinish() override;
164
Patrick Venture8d9f7322018-08-03 10:39:13 -0700165 bool startHash(uint32_t length) override;
Patrick Venturecfe66872018-08-03 13:32:33 -0700166 bool hashData(uint32_t offset, const std::vector<uint8_t>& bytes) override;
Patrick Venturefbc7d192018-08-03 13:54:21 -0700167 bool hashFinish() override;
Patrick Venture8d9f7322018-08-03 10:39:13 -0700168
Patrick Venture1cb87d22018-08-03 18:22:09 -0700169 bool startDataVerification() override;
Patrick Venture5c251ca2018-08-03 18:31:01 -0700170 bool abortUpdate() override;
Patrick Venture1cb87d22018-08-03 18:22:09 -0700171
Patrick Venture54c3b532018-08-01 11:45:49 -0700172 private:
173 /**
Patrick Venture8ec019f2018-08-07 11:22:33 -0700174 * Tries to close out everything.
175 */
176 void closeEverything();
177
178 /**
Patrick Venture54c3b532018-08-01 11:45:49 -0700179 * Tries to close out and delete anything staged.
180 */
181 void abortEverything();
182
183 /**
184 * Open all staged file handles you expect to use.
185 *
186 * @return false on failure.
187 */
188 bool openEverything();
Patrick Venture8ec019f2018-08-07 11:22:33 -0700189
190 /* The length of the flash image in bytes. */
191 uint32_t flashLength;
192
193 /* The file handle to the flash staging file. */
194 std::FILE* flashFd;
195
196 /* Where the bytes are written before verification. */
197 const std::string tmpPath;
Patrick Venture54c3b532018-08-01 11:45:49 -0700198};