blob: 56c32da951e3ab2844ee0e5aebdae4703c71745e [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 Venture54c3b532018-08-01 11:45:49 -0700109};
110
111class 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 Venture54c3b532018-08-01 11:45:49 -0700121 bool start(uint32_t length) override;
122
Patrick Venture79e131f2018-08-01 13:34:35 -0700123 bool flashData(uint32_t offset, const std::vector<uint8_t>& bytes) override;
124
Patrick Venture2c1205d2018-08-03 10:23:14 -0700125 bool flashFinish() override;
126
Patrick Venture54c3b532018-08-01 11:45:49 -0700127 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};