blob: 5e5fb22898adb56dc42166bd6dce5a427ab4a4f8 [file] [log] [blame]
Patrick Venture3d1786b2018-08-01 11:19:24 -07001#pragma once
2
Patrick Venture79e131f2018-08-01 13:34:35 -07003#include <vector>
4
Patrick Venture54c3b532018-08-01 11:45:49 -07005#include "host-ipmid/ipmid-api.h"
6
7/* Clearer way to represent the subcommand size. */
8#define SUBCMD_SZ sizeof(uint8_t)
9
Patrick Venture3d1786b2018-08-01 11:19:24 -070010/*
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 */
29enum 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 Venture54c3b532018-08-01 11:45:49 -070065
66/*
67 * StartTransfer expects a basic structure providing some information.
68 */
69struct StartTx
70{
71 uint8_t cmd;
72 uint32_t length; /* Maximum image length is 4GiB (little-endian) */
73} __attribute__((packed));
74
Patrick Venture79e131f2018-08-01 13:34:35 -070075struct ChunkHdr
76{
77 uint8_t cmd;
78 uint32_t offset; /* 0-based write offset */
79 uint8_t data[];
80} __attribute__((packed));
81
Patrick Venture54c3b532018-08-01 11:45:49 -070082class UpdateInterface
83{
84 public:
85 virtual ~UpdateInterface() = default;
86
Patrick Venture2c1205d2018-08-03 10:23:14 -070087 /**
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 Venture54c3b532018-08-01 11:45:49 -070093 virtual bool start(uint32_t length) = 0;
Patrick Venture2c1205d2018-08-03 10:23:14 -070094
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 Venture79e131f2018-08-01 13:34:35 -0700102 virtual bool flashData(uint32_t offset,
103 const std::vector<uint8_t>& bytes) = 0;
Patrick Venture2c1205d2018-08-03 10:23:14 -0700104
105 /**
106 * Called to indicate the host is done sending the flash bytes.
107 */
108 virtual bool flashFinish() = 0;
Patrick Venture8d9f7322018-08-03 10:39:13 -0700109
110 /**
111 * Prepare to receive a BMC image signature.
112 *
113 * @param[in] length - length of signature in bytes.
114 * @return true on success, false otherwise.
115 */
116 virtual bool startHash(uint32_t length) = 0;
Patrick Venturecfe66872018-08-03 13:32:33 -0700117
118 /**
119 * Attempt to write the bytes at the offset.
120 *
121 * @param[in] offset - the 0-based byte offset into the flash image.
122 * @param[in] bytes - the bytes to write.
123 * @return true on success, false otherwise.
124 */
125 virtual bool hashData(uint32_t offset,
126 const std::vector<uint8_t>& bytes) = 0;
Patrick Venturefbc7d192018-08-03 13:54:21 -0700127
128 /**
129 * Called to indicate the host is done sending the hash bytes.
130 */
131 virtual bool hashFinish() = 0;
Patrick Venture1cb87d22018-08-03 18:22:09 -0700132
133 /**
134 * Kick off the flash image verification process.
135 *
136 * @return true if it was started succesfully.
137 */
138 virtual bool startDataVerification() = 0;
Patrick Venture5c251ca2018-08-03 18:31:01 -0700139
140 /**
141 * Attempt to abort everything.
142 *
143 * @return true if aborted, false if unable or failed.
144 */
145 virtual bool abortUpdate() = 0;
Patrick Venture54c3b532018-08-01 11:45:49 -0700146};
147
148class FlashUpdate : public UpdateInterface
149{
150 public:
151 FlashUpdate() = default;
152 ~FlashUpdate() = default;
153 FlashUpdate(const FlashUpdate&) = default;
154 FlashUpdate& operator=(const FlashUpdate&) = default;
155 FlashUpdate(FlashUpdate&&) = default;
156 FlashUpdate& operator=(FlashUpdate&&) = default;
157
Patrick Venture54c3b532018-08-01 11:45:49 -0700158 bool start(uint32_t length) override;
Patrick Venture79e131f2018-08-01 13:34:35 -0700159 bool flashData(uint32_t offset, const std::vector<uint8_t>& bytes) override;
Patrick Venture2c1205d2018-08-03 10:23:14 -0700160 bool flashFinish() override;
161
Patrick Venture8d9f7322018-08-03 10:39:13 -0700162 bool startHash(uint32_t length) override;
Patrick Venturecfe66872018-08-03 13:32:33 -0700163 bool hashData(uint32_t offset, const std::vector<uint8_t>& bytes) override;
Patrick Venturefbc7d192018-08-03 13:54:21 -0700164 bool hashFinish() override;
Patrick Venture8d9f7322018-08-03 10:39:13 -0700165
Patrick Venture1cb87d22018-08-03 18:22:09 -0700166 bool startDataVerification() override;
Patrick Venture5c251ca2018-08-03 18:31:01 -0700167 bool abortUpdate() override;
Patrick Venture1cb87d22018-08-03 18:22:09 -0700168
Patrick Venture54c3b532018-08-01 11:45:49 -0700169 private:
170 /**
171 * Tries to close out and delete anything staged.
172 */
173 void abortEverything();
174
175 /**
176 * Open all staged file handles you expect to use.
177 *
178 * @return false on failure.
179 */
180 bool openEverything();
181};