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:
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 
92  int available() override
93  {
94  return 0;
95  }
96 
97  bool isFinished() override
98  {
99  return true;
100  }
101 
102 protected:
103  void setError(Error ec);
104 
105 private:
107  struct Slot {
109  Slot();
110 
111  Storage::Partition partition;
112  bool updated{false};
113  };
114  Slot slot;
115 
116  // 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.
117  OtaUpgrader ota;
118 
119  enum class State {
120  Error,
121  Header,
122  RomHeader,
123  SkipRom,
124  WriteRom,
125  VerifyRoms,
126  RomsComplete,
127  };
128  State state{State::Header};
129 
130 #ifdef ENABLE_OTA_SIGNING
131  using Verifier = SignatureVerifier;
132  static const uint32_t expectedHeaderMagic{OTA_HEADER_MAGIC_SIGNED};
133 #else
134  using Verifier = ChecksumVerifier;
135  static const uint32_t expectedHeaderMagic{OTA_HEADER_MAGIC_NOT_SIGNED};
136 #endif
137  Verifier verifier;
138 
139  OtaFileHeader fileHeader;
140  OtaRomHeader romHeader;
141 
142  Verifier::VerificationData verificationData;
143 
144  size_t remainingBytes{0};
145  uint8_t* destinationPtr{nullptr};
146  uint8_t romIndex{0};
147 
148  void setupChunk(State nextState, size_t size, void* destination = nullptr)
149  {
150  state = nextState;
151  remainingBytes = size;
152  destinationPtr = reinterpret_cast<uint8_t*>(destination);
153  }
154  template <typename T> void setupChunk(State nextState, T& destination)
155  {
156  setupChunk(nextState, sizeof(T), &destination);
157  }
158 
166  bool consume(const uint8_t*& data, size_t& size);
167 
171  void nextRom();
175  void processRomHeader();
179  void verifyRoms();
180 };
181 
182 } // namespace OtaUpgrade
183 
String toString(OtaUpgrade::BasicStream::Error code)
Convert error code to string.
#define OTA_HEADER_MAGIC_SIGNED
Definition: FileFormat.h:40
#define OTA_HEADER_MAGIC_NOT_SIGNED
Definition: FileFormat.h:42
A write-only stream to parse and apply firmware unencrypted upgrade files generated by otatool....
Definition: BasicStream.h:45
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
Definition: BasicStream.h:87
void setError(Error ec)
int available() override
Return the total length of the stream.
Definition: BasicStream.h:92
Error errorCode
Error code. Only relevant if hasError() returns true.
Definition: BasicStream.h:67
Error
Error code values.
Definition: BasicStream.h:52
@ RomActivationFailed
Error while activating updated ROM slot.
@ None
No error occurred thus far (default value of errorCode if hasError() returns false)
@ UnsupportedData
Some content of the upgrade file is not supported by this version of OtaUpgradeStream.
@ Internal
An unexpected error occurred.
@ OutOfMemory
Dynamic memory allocation failed.
@ RomTooLarge
The contained ROM image does not fit into the application firmware slot.
@ DecryptionFailed
Decryption failed. Probably wrong decryption key.
@ VerificationFailed
Signature/checksum verification failed - updated ROM not activated.
@ InvalidFormat
Invalid/unsupported upgrade file format.
@ NoRomFound
The file did not contain a ROM image suitable for the start address of the slot to upgrade.
@ FlashWriteFailed
Error while writing to Flash memory.
@ DowngradeNotAllowed
Attempt to downgrade to older firmware version.
bool isFinished() override
Check if all data has been read.
Definition: BasicStream.h:97
size_t write(const uint8_t *data, size_t size) override
Process chunk of upgrade file.
bool hasError() const
Returns true if an error happened during the upgrade process.
Definition: BasicStream.h:81
Hash VerificationData
Checksum type.
Definition: ChecksumVerifier.h:25
ESP32 OTA Upgrader implementation.
Definition: IdfUpgrader.h:23
Base class for read/write stream.
Definition: ReadWriteStream.h:20
Represents a flash partition.
Definition: Partition.h:86
The String class.
Definition: WString.h:137
Definition: BasicStream.h:24
Definition: FileFormat.h:24
Definition: FileFormat.h:34