ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
block_device.cpp
Go to the documentation of this file.
1#include "block_device.h"
2
3#include <cinttypes>
4
7
8namespace esphome::storage {
9
10ESPHOME_LOG_TAG(TAG, "storage");
11
12// Indexed by the negated StorageError value; the last entry is the fallback
13PROGMEM_STRING_TABLE(StorageErrorStrings, "none", "I/O error", "no media", "read only", "invalid address or size",
14 "timeout", "unknown");
15
16const LogString *storage_error_to_string(StorageError error) {
17 return StorageErrorStrings::get_log_str(-static_cast<int>(error));
18}
19
21 bool write) const {
22 if (!this->has_media())
24 if (write && (this->geometry_.flags & BLOCK_FLAG_READ_ONLY))
26 if (offset % align != 0 || len % align != 0)
28 const uint64_t start = static_cast<uint64_t>(block) * this->geometry_.block_size + offset;
29 if (block >= this->geometry_.block_count || start + len > this->get_capacity())
32}
33
35 if (!this->has_media())
39 if (count == 0 || block >= this->geometry_.block_count || count > this->geometry_.block_count - block)
42}
43
45 // A single filesystem call, such as a format, can make thousands of transfers. Only a task the watchdog watches
46 // feeds it: the main loop, also during setup(), but not a task using a filesystem through the C library. The feed
47 // can run other code, such as a status LED, so it comes before the lock.
49 const std::scoped_lock lock{this->mutex_};
50 if (len == 0)
52 StorageError err = this->check_access_(block, offset, len, this->geometry_.read_size, false);
54 return err;
55 return this->device_read(block, offset, buf, len);
56}
57
60 const std::scoped_lock lock{this->mutex_};
61 if (len == 0)
63 StorageError err = this->check_access_(block, offset, len, this->geometry_.program_size, true);
65 return err;
66 return this->device_program(block, offset, buf, len);
67}
68
71 const std::scoped_lock lock{this->mutex_};
72 StorageError err = this->check_range_(block, count);
74 return err;
75 return this->device_erase(block, count);
76}
77
79 const std::scoped_lock lock{this->mutex_};
80 if (!this->has_media())
82 return this->device_sync();
83}
84
86 const std::scoped_lock lock{this->mutex_};
87 StorageError err = this->check_range_(block, count);
89 return err;
90 return this->device_trim(block, count);
91}
92
93void BlockDevice::dump_geometry_(const char *tag) const {
94 if (!this->has_media()) {
95 ESP_LOGCONFIG(tag, " No media");
96 return;
97 }
98 const BlockGeometry &g = this->geometry_;
99 ESP_LOGCONFIG(tag,
100 " Capacity: %" PRIu32 " KiB\n"
101 " Block size: %" PRIu32 "\n"
102 " Block count: %" PRIu32 "\n"
103 " Read size: %u\n"
104 " Program size: %u\n"
105 " Needs erase: %s\n"
106 " Removable: %s\n"
107 " Read only: %s",
108 static_cast<uint32_t>(this->get_capacity() / 1024), g.block_size, g.block_count, g.read_size,
110 YESNO(g.flags & BLOCK_FLAG_READ_ONLY));
111}
112
113} // namespace esphome::storage
void feed_wdt_if_watched()
Feed from code that may run off the loop task (a long download or image decode): on ESP32 only a task...
StorageError sync()
Return once all earlier writes are stored on the media.
virtual StorageError device_program(uint32_t block, uint32_t offset, const void *buf, uint32_t len)=0
StorageError trim(uint32_t block, uint32_t count)
Tell the device that the blocks no longer hold data it needs to keep.
StorageError check_access_(uint32_t block, uint32_t offset, uint32_t len, uint16_t align, bool write) const
virtual StorageError device_read(uint32_t block, uint32_t offset, void *buf, uint32_t len)=0
StorageError read(uint32_t block, uint32_t offset, void *buf, uint32_t len)
void dump_geometry_(const char *tag) const
virtual StorageError device_trim(uint32_t block, uint32_t count)
StorageError check_range_(uint32_t block, uint32_t count) const
uint64_t get_capacity() const
StorageError program(uint32_t block, uint32_t offset, const void *buf, uint32_t len)
virtual StorageError device_erase(uint32_t block, uint32_t count)
StorageError erase(uint32_t block, uint32_t count)
virtual StorageError device_sync()
unsigned count
Definition gif.cpp:51
ESPHOME_LOG_TAG(TAG, "storage")
const LogString * storage_error_to_string(StorageError error)
PROGMEM_STRING_TABLE(StorageErrorStrings, "none", "I/O error", "no media", "read only", "invalid address or size", "timeout", "unknown")
const void size_t len
Definition hal.h:68
Application App
Global storage of Application pointer - only one Application can exist.
void const char * tag
Definition log.h:110
static void uint32_t
The shape of a block device. A block_count of 0 means no media is present.
SemaphoreHandle_t lock