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 <Storage/Partition.h>
15 #include <Ota/Manager.h>
16 #include "FileFormat.h"
17 #ifdef ENABLE_OTA_SIGNING
18 #include "SignatureVerifier.h"
19 #else
20 #include "ChecksumVerifier.h"
21 #endif
22 
23 namespace OtaUpgrade
24 {
45 {
46 public:
47  BasicStream();
48 
52  enum class Error {
53  None,
57  NoRomFound,
58  RomTooLarge,
63  OutOfMemory,
64  Internal,
65  };
66 
68 
74 
82  size_t write(const uint8_t* data, size_t size) override;
83 
87  bool hasError() const
88  {
89  return (state == State::Error);
90  }
91 
92  // overrides from IDataSourceStream
93  uint16_t readMemoryBlock(char* data, int bufSize) override
94  {
95  return 0;
96  }
97  virtual int available() override
98  {
99  return 0;
100  }
101  bool isFinished() override
102  {
103  return true;
104  }
105 
106 protected:
107  void setError(Error ec);
108 
109 private:
111  struct Slot {
113  Slot();
114 
115  Storage::Partition partition;
116  bool updated{false};
117  };
118  Slot slot;
119 
120  // 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.
121  OtaUpgrader ota;
122 
123  enum class State {
124  Error,
125  Header,
126  RomHeader,
127  SkipRom,
128  WriteRom,
129  VerifyRoms,
130  RomsComplete,
131  };
132  State state{State::Header};
133 
134 #ifdef ENABLE_OTA_SIGNING
135  using Verifier = SignatureVerifier;
136  static const uint32_t expectedHeaderMagic{OTA_HEADER_MAGIC_SIGNED};
137 #else
138  using Verifier = ChecksumVerifier;
139  static const uint32_t expectedHeaderMagic{OTA_HEADER_MAGIC_NOT_SIGNED};
140 #endif
141  Verifier verifier;
142 
143  OtaFileHeader fileHeader;
144  OtaRomHeader romHeader;
145 
146  Verifier::VerificationData verificationData;
147 
148  size_t remainingBytes{0};
149  uint8_t* destinationPtr{nullptr};
150  uint8_t romIndex{0};
151 
152  void setupChunk(State nextState, size_t size, void* destination = nullptr)
153  {
154  state = nextState;
155  remainingBytes = size;
156  destinationPtr = reinterpret_cast<uint8_t*>(destination);
157  }
158  template <typename T> void setupChunk(State nextState, T& destination)
159  {
160  setupChunk(nextState, sizeof(T), &destination);
161  }
162 
170  bool consume(const uint8_t*& data, size_t& size);
171 
175  void nextRom();
179  void processRomHeader();
183  void verifyRoms();
184 };
185 
186 } // namespace OtaUpgrade
187 
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:52
bool isFinished() override
Check if all data has been read.
Definition: BasicStream.h:101
Error while writing to Flash memory.
Definition: FileFormat.h:24
Error errorCode
Error code. Only relevant if hasError() returns true.
Definition: BasicStream.h:67
Definition: FileFormat.h:34
static String errorToString(Error code)
Convert error code to string.
Definition: BasicStream.h:23
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
Definition: BasicStream.h:93
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:44
#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.
bool hasError() const
Returns true if an error happened during the upgrade process.
Definition: BasicStream.h:87
#define OTA_HEADER_MAGIC_SIGNED
Definition: FileFormat.h:40
virtual int available() override
Return the total length of the stream.
Definition: BasicStream.h:97
Represents a flash partition.
Definition: Partition.h:78
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.