BasicStream.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2017 by Slavey Karadzhov
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * BasicOtaUpgradeStream.h
8  *
9  ****/
10 
11 #pragma once
12 
14 #include <rboot-api.h>
15 #include "FileFormat.h"
16 #ifdef ENABLE_OTA_SIGNING
17 #include "SignatureVerifier.h"
18 #else
19 #include "ChecksumVerifier.h"
20 #endif
21 
22 namespace OtaUpgrade
23 {
44 {
45 public:
46  BasicStream();
47 
51  enum Error {
52  None,
64  };
65 
66  Error errorCode = Error::None;
67 
71  static String errorToString(Error code);
72 
80  size_t write(const uint8_t* data, size_t size) override;
81 
85  bool hasError() const
86  {
87  return (state == State::Error);
88  }
89 
90  // overrides from IDataSourceStream
91  uint16_t readMemoryBlock(char* data, int bufSize) override
92  {
93  return 0;
94  }
95  virtual int available() override
96  {
97  return 0;
98  }
99  bool isFinished() override
100  {
101  return true;
102  }
103 
104 protected:
105  void setError(Error ec);
106 
107 private:
109  struct Slot {
111  Slot();
112 
113  uint32_t address;
114  uint32_t size;
115  uint8_t index;
116  bool updated = false;
117  } slot;
118 
119  // Instead of RbootOutputStream, the rboot write API is used directly because in a future extension the OTA file may contain data for multiple FLASH regions.
120  rboot_write_status rbootWriteStatus = {};
121 
122  enum class State {
123  Error,
124  Header,
125  RomHeader,
126  SkipRom,
127  WriteRom,
128  VerifyRoms,
129  RomsComplete,
130  };
131  State state = State::Header;
132 
133 #ifdef ENABLE_OTA_SIGNING
134  typedef SignatureVerifier Verifier;
135  static const uint32_t expectedHeaderMagic = OTA_HEADER_MAGIC_SIGNED;
136 #else
137  typedef ChecksumVerifier Verifier;
138  static const uint32_t expectedHeaderMagic = OTA_HEADER_MAGIC_NOT_SIGNED;
139 #endif
140  Verifier verifier;
141 
142  OtaFileHeader fileHeader;
143  OtaRomHeader romHeader;
144 
145  Verifier::VerificationData verificationData;
146 
147  size_t remainingBytes = 0;
148  uint8_t* destinationPtr = nullptr;
149  uint8_t romIndex = 0;
150 
151  void setupChunk(State nextState, size_t size, void* destination = nullptr)
152  {
153  state = nextState;
154  remainingBytes = size;
155  destinationPtr = reinterpret_cast<uint8_t*>(destination);
156  }
157  template <typename T> void setupChunk(State nextState, T& destination)
158  {
159  setupChunk(nextState, sizeof(T), &destination);
160  }
161 
169  bool consume(const uint8_t*& data, size_t& size);
170 
174  void nextRom();
178  void processRomHeader();
182  void verifyRoms();
183 };
184 
185 } // namespace OtaUpgrade
Error
Error code values.
Definition: BasicStream.h:51
Signature/checksum verification failed - updated ROM not activated.
Definition: BasicStream.h:59
bool isFinished() override
Check if all data has been read.
Definition: BasicStream.h:99
Hash VerificationData
Checksum type.
Definition: ChecksumVerifier.h:25
An unexpected error occured.
Definition: BasicStream.h:63
Definition: FileFormat.h:24
Error errorCode
Error code. Only relevant if hasError() returns true.
Definition: BasicStream.h:66
Definition: FileFormat.h:34
static String errorToString(Error code)
Convert error code to string.
Decryption failed. Probably wrong decryption key.
Definition: BasicStream.h:55
Some content of the upgrade file is not supported by this version of OtaUpgradeStream.
Definition: BasicStream.h:54
Definition: BasicStream.h:22
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
Definition: BasicStream.h:91
The String class.
Definition: WString.h:136
Dynamic memory allocation failed.
Definition: BasicStream.h:62
Signature verifier for BasicStream.
Definition: SignatureVerifier.h:26
Error while activating updated ROM slot.
Definition: BasicStream.h:61
A write-only stream to parse and apply firmware unencrypted upgrade files generated by otatool...
Definition: BasicStream.h:43
Error while writing to Flash memory.
Definition: BasicStream.h:60
#define OTA_HEADER_MAGIC_NOT_SIGNED
Definition: FileFormat.h:42
Checksum verifier used by BasicStream if signature verification is disabled.
Definition: ChecksumVerifier.h:22
No error occured thus far (default value of errorCode if hasError() returns false) ...
Definition: BasicStream.h:52
The file did not contain a ROM image suitable for the start address of the slot to upgrade...
Definition: BasicStream.h:56
void setError(Error ec)
Structure defining flash write status.
Definition: rboot-api.h:26
bool hasError() const
Returns true if an error happened during the upgrade process.
Definition: BasicStream.h:85
#define OTA_HEADER_MAGIC_SIGNED
Definition: FileFormat.h:40
The contained ROM image does not fit into the application firmware slot.
Definition: BasicStream.h:57
virtual int available() override
Return the total length of the stream.
Definition: BasicStream.h:95
Attempt to downgrade to older firmware version.
Definition: BasicStream.h:58
Base class for read/write stream.
Definition: ReadWriteStream.h:22
Invalid/unsupported upgrade file format.
Definition: BasicStream.h:53
size_t write(const uint8_t *data, size_t size) override
Process chunk of upgrade file.