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 class Error {
52  None,
56  NoRomFound,
57  RomTooLarge,
62  OutOfMemory,
63  Internal,
64  };
65 
67 
73 
81  size_t write(const uint8_t* data, size_t size) override;
82 
86  bool hasError() const
87  {
88  return (state == State::Error);
89  }
90 
91  // overrides from IDataSourceStream
92  uint16_t readMemoryBlock(char* data, int bufSize) override
93  {
94  return 0;
95  }
96  virtual int available() override
97  {
98  return 0;
99  }
100  bool isFinished() override
101  {
102  return true;
103  }
104 
105 protected:
106  void setError(Error ec);
107 
108 private:
110  struct Slot {
112  Slot();
113 
114  uint32_t address;
115  uint32_t size;
116  uint8_t index;
117  bool updated{false};
118  };
119  Slot slot;
120 
121  // 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.
122  rboot_write_status rbootWriteStatus{};
123 
124  enum class State {
125  Error,
126  Header,
127  RomHeader,
128  SkipRom,
129  WriteRom,
130  VerifyRoms,
131  RomsComplete,
132  };
133  State state{State::Header};
134 
135 #ifdef ENABLE_OTA_SIGNING
136  using Verifier = SignatureVerifier;
137  static const uint32_t expectedHeaderMagic{OTA_HEADER_MAGIC_SIGNED};
138 #else
139  using Verifier = ChecksumVerifier;
140  static const uint32_t expectedHeaderMagic{OTA_HEADER_MAGIC_NOT_SIGNED};
141 #endif
142  Verifier verifier;
143 
144  OtaFileHeader fileHeader;
145  OtaRomHeader romHeader;
146 
147  Verifier::VerificationData verificationData;
148 
149  size_t remainingBytes{0};
150  uint8_t* destinationPtr{nullptr};
151  uint8_t romIndex{0};
152 
153  void setupChunk(State nextState, size_t size, void* destination = nullptr)
154  {
155  state = nextState;
156  remainingBytes = size;
157  destinationPtr = reinterpret_cast<uint8_t*>(destination);
158  }
159  template <typename T> void setupChunk(State nextState, T& destination)
160  {
161  setupChunk(nextState, sizeof(T), &destination);
162  }
163 
171  bool consume(const uint8_t*& data, size_t& size);
172 
176  void nextRom();
180  void processRomHeader();
184  void verifyRoms();
185 };
186 
187 } // namespace OtaUpgrade
188 
Error while activating updated ROM slot.
Some content of the upgrade file is not supported by this version of OtaUpgradeStream.
Error
Error code values.
Definition: BasicStream.h:51
bool isFinished() override
Check if all data has been read.
Definition: BasicStream.h:100
Error while writing to Flash memory.
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.
Definition: BasicStream.h:22
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
Definition: BasicStream.h:92
The String class.
Definition: WString.h:136
Signature verifier for BasicStream.
Definition: SignatureVerifier.h:26
A write-only stream to parse and apply firmware unencrypted upgrade files generated by otatool...
Definition: BasicStream.h:43
#define OTA_HEADER_MAGIC_NOT_SIGNED
Definition: FileFormat.h:42
Checksum verifier used by BasicStream if signature verification is disabled.
Definition: ChecksumVerifier.h:22
void setError(Error ec)
Signature/checksum verification failed - updated ROM not activated.
#define SMING_DEPRECATED
Definition: sming_attr.h:30
The file did not contain a ROM image suitable for the start address of the slot to upgrade...
No error occured thus far (default value of errorCode if hasError() returns false) ...
Attempt to downgrade to older firmware version.
Hash VerificationData
Checksum type.
Definition: ChecksumVerifier.h:25
Dynamic memory allocation failed.
String toString(OtaUpgrade::BasicStream::Error code)
Convert error code to string.
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:86
#define OTA_HEADER_MAGIC_SIGNED
Definition: FileFormat.h:40
virtual int available() override
Return the total length of the stream.
Definition: BasicStream.h:96
An unexpected error occured.
Invalid/unsupported upgrade file format.
Decryption failed. Probably wrong decryption key.
Base class for read/write stream.
Definition: ReadWriteStream.h:19
The contained ROM image does not fit into the application firmware slot.
size_t write(const uint8_t *data, size_t size) override
Process chunk of upgrade file.