Partition.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Partition.h - C++ wrapper for universal partition table support
8  *
9  * Original license for IDF code:
10  *
11  * Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
12  *
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  * http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  *
25  *
26  ****/
27 #pragma once
28 
29 #include <Data/BitSet.h>
30 #include <Data/CString.h>
31 #include <memory>
32 #include <cassert>
33 
34 #define PARTITION_APP_SUBTYPE_MAP(XX) \
35  XX(factory, 0x00, "Factory application") \
36  XX(ota0, 0x10, "OTA #0") \
37  XX(ota1, 0x11, "OTA #1") \
38  XX(ota2, 0x12, "OTA #2") \
39  XX(ota3, 0x13, "OTA #3") \
40  XX(ota4, 0x14, "OTA #4") \
41  XX(ota5, 0x15, "OTA #5") \
42  XX(ota6, 0x16, "OTA #6") \
43  XX(ota7, 0x17, "OTA #7") \
44  XX(ota8, 0x18, "OTA #8") \
45  XX(ota9, 0x19, "OTA #9") \
46  XX(ota10, 0x1a, "OTA #10") \
47  XX(ota11, 0x1b, "OTA #11") \
48  XX(ota12, 0x1c, "OTA #12") \
49  XX(ota13, 0x1d, "OTA #13") \
50  XX(ota14, 0x1e, "OTA #14") \
51  XX(ota15, 0x1f, "OTA #15") \
52  XX(test, 0x20, "Test application")
53 
54 #define PARTITION_DATA_SUBTYPE_MAP(XX) \
55  XX(ota, 0x00, "OTA selection") \
56  XX(phy, 0x01, "PHY init data") \
57  XX(nvs, 0x02, "NVS") \
58  XX(coreDump, 0x03, "Core Dump data") \
59  XX(nvsKeys, 0x04, "NVS key information") \
60  XX(eFuseEm, 0x05, "eFuse emulation") \
61  XX(sysParam, 0x40, "System Parameters") \
62  XX(rfCal, 0x41, "RF Calibration") \
63  XX(espHttpd, 0x80, "ESPHTTPD") \
64  XX(fat, 0x81, "FAT") \
65  XX(spiffs, 0x82, "SPIFFS") \
66  XX(fwfs, 0xF1, "FWFS")
67 
68 namespace Storage
69 {
70 class Device;
71 class PartitionTable;
73 
77 class Partition
78 {
79 public:
80  enum class Type : uint8_t {
81  app = 0x00,
82  data = 0x01,
83  storage = 0x02,
84  userMin = 0x40,
85  userMax = 0xFE,
86  invalid = 0xff,
87  any = 0xff,
88  };
89 
90  struct SubType {
91  static constexpr uint8_t any{0xff};
92  static constexpr uint8_t invalid{0xff};
93 
97  enum class App : uint8_t {
98  partitionType = uint8_t(Type::app),
99 #define XX(type, value, desc) type = value,
101 #undef XX
102  ota_min = ota0,
103  ota_max = ota15,
104  any = 0xff
105  };
106 
110  enum class Data : uint8_t {
111  partitionType = uint8_t(Type::data),
112 #define XX(subtype, value, desc) subtype = value,
114 #undef XX
115  any = 0xff
116  };
117  };
118 
119  enum class Flag {
120  encrypted = 0,
121  readOnly = 31,
122  };
123 
124  static constexpr size_t nameSize{16};
125  using Name = char[nameSize];
127 
131  struct Info {
133  uint32_t offset{0};
134  uint32_t size{0};
136  uint8_t subtype{SubType::invalid};
138 
140  {
141  }
142 
143  Info(const String& name, Type type, uint8_t subtype, uint32_t offset, uint32_t size, Flags flags)
144  : name(name), offset(offset), size(size), type(type), subtype(subtype), flags(flags)
145  {
146  }
147  };
148 
150  {
151  }
152 
153  Partition(const Partition& other) : mDevice(other.mDevice), mPart(other.mPart)
154  {
155  }
156 
157  Partition(Device& device, const Info& info) : mDevice(&device), mPart(&info)
158  {
159  }
160 
169  bool verify(Type type, uint8_t subtype) const;
170 
171  bool verify(uint8_t type, uint8_t subtype) const
172  {
173  return verify(Type(type), subtype);
174  }
175 
176  template <typename T> bool verify(T subType) const
177  {
178  return verify(Type(T::partitionType), uint8_t(subType));
179  }
180 
186  static inline SubType::App apptypeOta(uint8_t i)
187  {
188  auto subtype = SubType::App(uint8_t(SubType::App::ota_min) + i);
189  assert(subtype >= SubType::App::ota_min && subtype <= SubType::App::ota_max);
190  return subtype;
191  }
192 
193  explicit operator bool() const
194  {
195  return mDevice != nullptr && mPart != nullptr;
196  }
197 
205  bool read(size_t offset, void* dst, size_t size);
206 
207  template <typename T> typename std::enable_if<std::is_pod<T>::value, bool>::type read(size_t offset, T& value)
208  {
209  return read(offset, &value, sizeof(value));
210  }
211 
220  bool write(size_t offset, const void* src, size_t size);
221 
229  bool erase_range(size_t offset, size_t size);
230 
235  {
237  }
238 
242  uint8_t subType() const
243  {
244  return mPart ? mPart->subtype : SubType::invalid;
245  }
246 
251  uint32_t address() const
252  {
253  return (mPart && mPart->type != Partition::Type::storage) ? mPart->offset : 0;
254  }
255 
260  uint32_t lastAddress() const
261  {
262  return mPart ? (mPart->offset + mPart->size - 1) : 0;
263  }
264 
269  uint32_t size() const
270  {
271  return mPart ? mPart->size : 0;
272  }
273 
277  String name() const
278  {
279  return mPart ? mPart->name.c_str() : nullptr;
280  }
281 
285  Flags flags() const
286  {
287  return mPart ? mPart->flags : 0;
288  }
289 
293  bool isEncrypted() const
294  {
295  return flags()[Flag::encrypted];
296  }
297 
301  bool isReadOnly() const
302  {
303  return mPart ? mPart->flags[Flag::readOnly] : true;
304  }
305 
310  String typeString() const;
311  String longTypeString() const;
321  bool getDeviceAddress(uint32_t& address, size_t size) const;
322 
327  String getDeviceName() const;
328 
333  Device* getDevice() const
334  {
335  return mDevice;
336  }
337 
341  bool contains(uint32_t addr) const
342  {
343  return mPart ? (addr >= mPart->offset && addr <= lastAddress()) : false;
344  }
345 
346  bool operator==(const Partition& other) const
347  {
348  return this == &other;
349  }
350 
351  bool operator==(const char* name) const
352  {
353  return mPart ? mPart->name.equals(name) : false;
354  }
355 
356  bool operator==(const String& name) const
357  {
358  return mPart ? mPart->name.equals(name) : false;
359  }
360 
364  size_t getBlockSize() const;
365 
366 protected:
367  Device* mDevice{nullptr};
368  const Info* mPart{nullptr};
369 
370 private:
371  bool allowRead();
372  bool allowWrite();
373 };
374 
375 } // namespace Storage
376 
378 String toLongString(Storage::Partition::Type type, uint8_t subType);
379 
380 template <typename E> typename std::enable_if<bool(E::partitionType), String>::type toString(E subType)
381 {
382  return toString(Storage::Partition::Type(E::partitionType), uint8_t(subType));
383 }
384 
385 template <typename E> typename std::enable_if<bool(E::partitionType), String>::type toLongString(E subType)
386 {
387  return toLongString(Storage::Partition::Type(E::partitionType), uint8_t(subType));
388 }
uint32_t lastAddress() const
Obtain address of last byte in this this partition.
Definition: Partition.h:260
bool contains(uint32_t addr) const
Determine if given address contained within this partition.
Definition: Partition.h:341
bool operator==(const char *name) const
Definition: Partition.h:351
static constexpr size_t nameSize
Definition: Partition.h:124
#define PARTITION_APP_SUBTYPE_MAP(XX)
Definition: Partition.h:34
std::enable_if< std::is_pod< T >::value, bool >::type read(size_t offset, T &value)
Definition: Partition.h:207
bool operator==(const String &name) const
Definition: Partition.h:356
Device * mDevice
Definition: Partition.h:367
Info(const String &name, Type type, uint8_t subtype, uint32_t offset, uint32_t size, Flags flags)
Definition: Partition.h:143
bool isEncrypted() const
Check state of partition encrypted flag.
Definition: Partition.h:293
bool verify(T subType) const
Definition: Partition.h:176
#define PARTITION_DATA_SUBTYPE_MAP(XX)
Definition: Partition.h:54
bool isReadOnly() const
Check state of partition readOnly flag.
Definition: Partition.h:301
Flag
Definition: Partition.h:119
uint32_t address() const
Obtain partition starting address.
Definition: Partition.h:251
bool getDeviceAddress(uint32_t &address, size_t size) const
Get corresponding storage device address for a given partition offset.
uint8_t subType() const
Obtain partition sub-type.
Definition: Partition.h:242
bool verify(Type type, uint8_t subtype) const
The String class.
Definition: WString.h:136
uint32_t size
Definition: Partition.h:134
uint8_t subtype
Definition: Partition.h:136
Write/erase prohibited.
String getDeviceName() const
Get name of storage device for this partition.
bool write(size_t offset, const void *src, size_t size)
Write data to the partition.
char[nameSize] Name
Definition: Partition.h:125
size_t getBlockSize() const
Obtain smallest allocation unit for erase operations.
bool operator==(const Partition &other) const
Definition: Partition.h:346
Partition::Type type() const
Obtain partition type.
Definition: Partition.h:234
Class to manage a NUL-terminated C-style string When storing persistent strings in RAM the regular St...
Definition: CString.h:26
Type type
Definition: Partition.h:135
String toString(Storage::Partition::Type type, uint8_t subType)
const char * c_str() const
Definition: CString.h:94
Info()
Definition: Partition.h:139
String name() const
Get partition name.
Definition: Partition.h:277
Flags flags() const
Get partition flags.
Definition: Partition.h:285
const Info * mPart
Definition: Partition.h:368
Device * getDevice() const
Get storage device containing this partition.
Definition: Partition.h:333
Type
Definition: Partition.h:80
Represents a storage device (e.g. flash memory)
Definition: Components/Storage/src/include/Storage/Device.h:32
Flags flags
Definition: Partition.h:137
CString name
Definition: Partition.h:132
Partition(const Partition &other)
Definition: Partition.h:153
String toLongString(Storage::Partition::Type type, uint8_t subType)
Partition(Device &device, const Info &info)
Definition: Partition.h:157
Represents a flash partition.
Definition: Partition.h:77
String typeString() const
bool verify(uint8_t type, uint8_t subtype) const
Definition: Partition.h:171
Internal structure describing the binary layout of a partition table entry.
Definition: partition.h:16
String longTypeString() const
uint32_t size() const
Obtain partition size.
Definition: Partition.h:269
bool equals(const CString &other) const
Definition: CString.h:99
static SubType::App apptypeOta(uint8_t i)
Convenience function to get SubType value for the i-th OTA partition.
Definition: Partition.h:186
bool read(size_t offset, void *dst, size_t size)
Read data from the partition.
static constexpr uint8_t invalid
Definition: Partition.h:92
Definition: FileDevice.h:23
App
Application partition type.
Definition: Partition.h:97
uint32_t offset
Definition: Partition.h:133
bool erase_range(size_t offset, size_t size)
Erase part of the partition.
Partition information.
Definition: Partition.h:131
Definition: Partition.h:90
Partition()
Definition: Partition.h:149
Data
Data partition type.
Definition: Partition.h:110
#define XX(name, comment)
Definition: DirectoryTemplate.h:47