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 
76  size_t write(const uint8_t* data, size_t size) override;
77 
81  bool hasError() const
82  {
83  return (state == State::Error);
84  }
85 
86  // overrides from IDataSourceStream
87  uint16_t readMemoryBlock(char* data, int bufSize) override
88  {
89  return 0;
90  }
91  virtual int available() override
92  {
93  return 0;
94  }
95  bool isFinished() override
96  {
97  return true;
98  }
99 
100 protected:
101  void setError(Error ec);
102 
103 private:
105  struct Slot {
107  Slot();
108 
109  Storage::Partition partition;
110  bool updated{false};
111  };
112  Slot slot;
113 
114  // 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.
115  OtaUpgrader ota;
116 
117  enum class State {
118  Error,
119  Header,
120  RomHeader,
121  SkipRom,
122  WriteRom,
123  VerifyRoms,
124  RomsComplete,
125  };
126  State state{State::Header};
127 
128 #ifdef ENABLE_OTA_SIGNING
129  using Verifier = SignatureVerifier;
130  static const uint32_t expectedHeaderMagic{OTA_HEADER_MAGIC_SIGNED};
131 #else
132  using Verifier = ChecksumVerifier;
133  static const uint32_t expectedHeaderMagic{OTA_HEADER_MAGIC_NOT_SIGNED};
134 #endif
135  Verifier verifier;
136 
137  OtaFileHeader fileHeader;
138  OtaRomHeader romHeader;
139 
140  Verifier::VerificationData verificationData;
141 
142  size_t remainingBytes{0};
143  uint8_t* destinationPtr{nullptr};
144  uint8_t romIndex{0};
145 
146  void setupChunk(State nextState, size_t size, void* destination = nullptr)
147  {
148  state = nextState;
149  remainingBytes = size;
150  destinationPtr = reinterpret_cast<uint8_t*>(destination);
151  }
152  template <typename T> void setupChunk(State nextState, T& destination)
153  {
154  setupChunk(nextState, sizeof(T), &destination);
155  }
156 
164  bool consume(const uint8_t*& data, size_t& size);
165 
169  void nextRom();
173  void processRomHeader();
177  void verifyRoms();
178 };
179 
180 } // namespace OtaUpgrade
181 
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:95
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
Definition: AdvancedPayloadParser.h:18
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
Definition: BasicStream.h:87
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.
The file did not contain a ROM image suitable for the start address of the slot to upgrade...
No error occurred 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.
ESP32 OTA Upgrader implementation.
Definition: IdfUpgrader.h:22
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:81
#define OTA_HEADER_MAGIC_SIGNED
Definition: FileFormat.h:40
virtual int available() override
Return the total length of the stream.
Definition: BasicStream.h:91
Represents a flash partition.
Definition: Partition.h:85
An unexpected error occurred.
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.