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 {
44 class BasicStream : public ReadWriteStream
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 
Represents a flash partition.
Definition: Partition.h:85
Hash VerificationData
Checksum type.
Definition: ChecksumVerifier.h:41
@ NoRomFound
The file did not contain a ROM image suitable for the start address of the slot to upgrade.
bool isFinished() override
Check if all data has been read.
Definition: BasicStream.h:111
@ InvalidFormat
Invalid/unsupported upgrade file format.
The String class.
Definition: WString.h:136
@ DowngradeNotAllowed
Attempt to downgrade to older firmware version.
Error errorCode
Error code. Only relevant if hasError() returns true.
Definition: BasicStream.h:83
Definition: BasicStream.h:23
@ OutOfMemory
Dynamic memory allocation failed.
@ Internal
An unexpected error occurred.
ESP32 OTA Upgrader implementation.
Definition: IdfUpgrader.h:32
@ RomTooLarge
The contained ROM image does not fit into the application firmware slot.
#define OTA_HEADER_MAGIC_NOT_SIGNED
Definition: FileFormat.h:42
void setError(Error ec)
@ UnsupportedData
Some content of the upgrade file is not supported by this version of OtaUpgradeStream.
virtual int available() override
Return the total length of the stream.
Definition: BasicStream.h:107
@ DecryptionFailed
Decryption failed. Probably wrong decryption key.
Definition: FileFormat.h:34
#define OTA_HEADER_MAGIC_SIGNED
Definition: FileFormat.h:40
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
Definition: BasicStream.h:103
@ RomActivationFailed
Error while activating updated ROM slot.
Definition: FileFormat.h:24
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:97
@ FlashWriteFailed
Error while writing to Flash memory.
@ None
No error occurred thus far (default value of errorCode if hasError() returns false)
A write-only stream to parse and apply firmware unencrypted upgrade files generated by otatool....
Definition: BasicStream.h:52
size_t write(const uint8_t *data, size_t size) override
Process chunk of upgrade file.
Error
Error code values.
Definition: BasicStream.h:68
@ VerificationFailed
Signature/checksum verification failed - updated ROM not activated.
Base class for read/write stream.
Definition: ReadWriteStream.h:19