Skip to content

Commit 6bc9cd3

Browse files
committed
Merge branch 'feature/spi_flash_write_16bytes' into 'master'
SPI Flash: Allow 16 byte aligned encrypted writes Also includes some improved documentation See merge request !456
2 parents fbe89a0 + d446266 commit 6bc9cd3

File tree

11 files changed

+373
-37
lines changed

11 files changed

+373
-37
lines changed

components/spi_flash/cache_utils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,12 @@ void spi_flash_disable_interrupts_caches_and_other_cpu_no_os();
4848
// This function is implied to be called when other CPU is not running or running code from IRAM.
4949
void spi_flash_enable_interrupts_caches_no_os();
5050

51+
// Mark the pages containing a flash region as having been
52+
// erased or written to. This means the flash cache needs
53+
// to be evicted before these pages can be flash_mmap()ed again,
54+
// as they may contain stale data
55+
//
56+
// Only call this while holding spi_flash_op_lock()
57+
void spi_flash_mark_modified_region(uint32_t start_addr, uint32_t length);
58+
5159
#endif //ESP_SPI_FLASH_CACHE_UTILS_H

components/spi_flash/flash_mmap.c

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,21 @@
3939

4040
#define REGIONS_COUNT 4
4141
#define PAGES_PER_REGION 64
42-
#define FLASH_PAGE_SIZE 0x10000
4342
#define INVALID_ENTRY_VAL 0x100
4443
#define VADDR0_START_ADDR 0x3F400000
4544
#define VADDR1_START_ADDR 0x40000000
4645
#define VADDR1_FIRST_USABLE_ADDR 0x400D0000
47-
#define PRO_IRAM0_FIRST_USABLE_PAGE ((VADDR1_FIRST_USABLE_ADDR - VADDR1_START_ADDR) / FLASH_PAGE_SIZE + 64)
46+
#define PRO_IRAM0_FIRST_USABLE_PAGE ((VADDR1_FIRST_USABLE_ADDR - VADDR1_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE + 64)
4847

48+
/* Ensure pages in a region haven't been marked as written via
49+
spi_flash_mark_modified_region(). If the page has
50+
been written, flush the entire flash cache before returning.
51+
52+
This ensures stale cache entries are never read after fresh calls
53+
to spi_flash_mmap(), while keeping the number of cache flushes to a
54+
minimum.
55+
*/
56+
static void spi_flash_ensure_unmodified_region(size_t start_addr, size_t length);
4957

5058
typedef struct mmap_entry_{
5159
uint32_t handle;
@@ -91,7 +99,11 @@ esp_err_t IRAM_ATTR spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_
9199
if (src_addr + size > g_rom_flashchip.chip_size) {
92100
return ESP_ERR_INVALID_ARG;
93101
}
102+
94103
spi_flash_disable_interrupts_caches_and_other_cpu();
104+
105+
spi_flash_ensure_unmodified_region(src_addr, size);
106+
95107
if (s_mmap_page_refcnt[0] == 0) {
96108
spi_flash_mmap_init();
97109
}
@@ -111,8 +123,8 @@ esp_err_t IRAM_ATTR spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_
111123
region_addr = VADDR1_FIRST_USABLE_ADDR;
112124
}
113125
// region which should be mapped
114-
int phys_page = src_addr / FLASH_PAGE_SIZE;
115-
int page_count = (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE;
126+
int phys_page = src_addr / SPI_FLASH_MMU_PAGE_SIZE;
127+
int page_count = (size + SPI_FLASH_MMU_PAGE_SIZE - 1) / SPI_FLASH_MMU_PAGE_SIZE;
116128
// The following part searches for a range of MMU entries which can be used.
117129
// Algorithm is essentially naïve strstr algorithm, except that unused MMU
118130
// entries are treated as wildcards.
@@ -158,7 +170,7 @@ esp_err_t IRAM_ATTR spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_
158170
new_entry->count = page_count;
159171
new_entry->handle = ++s_mmap_last_handle;
160172
*out_handle = new_entry->handle;
161-
*out_ptr = (void*) (region_addr + start * FLASH_PAGE_SIZE);
173+
*out_ptr = (void*) (region_addr + start * SPI_FLASH_MMU_PAGE_SIZE);
162174
ret = ESP_OK;
163175
}
164176
spi_flash_enable_interrupts_caches_and_other_cpu();
@@ -212,3 +224,59 @@ void spi_flash_mmap_dump()
212224
}
213225
}
214226
}
227+
228+
/* 256-bit (up to 16MB of 64KB pages) bitset of all flash pages
229+
that have been written to since last cache flush.
230+
231+
Before mmaping a page, need to flush caches if that page has been
232+
written to.
233+
234+
Note: It's possible to do some additional performance tweaks to
235+
this algorithm, as we actually only need to flush caches if a page
236+
was first mmapped, then written to, then is about to be mmaped a
237+
second time. This is a fair bit more complex though, so unless
238+
there's an access pattern that this would significantly boost then
239+
it's probably not worth it.
240+
*/
241+
static uint32_t written_pages[256/32];
242+
243+
static void update_written_pages(size_t start_addr, size_t length, bool mark);
244+
245+
void IRAM_ATTR spi_flash_mark_modified_region(size_t start_addr, size_t length)
246+
{
247+
update_written_pages(start_addr, length, true);
248+
}
249+
250+
static void IRAM_ATTR spi_flash_ensure_unmodified_region(size_t start_addr, size_t length)
251+
{
252+
update_written_pages(start_addr, length, false);
253+
}
254+
255+
/* generic implementation for the previous two functions */
256+
static inline IRAM_ATTR void update_written_pages(size_t start_addr, size_t length, bool mark)
257+
{
258+
for (uint32_t addr = start_addr; addr < start_addr + length; addr += SPI_FLASH_MMU_PAGE_SIZE) {
259+
int page = addr / SPI_FLASH_MMU_PAGE_SIZE;
260+
if (page >= 256) {
261+
return; /* invalid address */
262+
}
263+
264+
int idx = page / 32;
265+
uint32_t bit = 1 << (page % 32);
266+
267+
if (mark) {
268+
written_pages[idx] |= bit;
269+
} else if (written_pages[idx] & bit) {
270+
/* it is tempting to write a version of this that only
271+
flushes each CPU's cache as needed. However this is
272+
tricky because mmaped memory can be used on un-pinned
273+
cores, or the pointer passed between CPUs.
274+
*/
275+
Cache_Flush(0);
276+
#ifndef CONFIG_FREERTOS_UNICORE
277+
Cache_Flush(1);
278+
#endif
279+
bzero(written_pages, sizeof(written_pages));
280+
}
281+
}
282+
}

components/spi_flash/flash_ops.c

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ size_t spi_flash_get_chip_size()
9090
return g_rom_flashchip.chip_size;
9191
}
9292

93-
SpiFlashOpResult IRAM_ATTR spi_flash_unlock()
93+
static SpiFlashOpResult IRAM_ATTR spi_flash_unlock()
9494
{
9595
static bool unlocked = false;
9696
if (!unlocked) {
@@ -250,42 +250,80 @@ esp_err_t IRAM_ATTR spi_flash_write(size_t dst, const void *srcv, size_t size)
250250
}
251251
out:
252252
COUNTER_STOP(write);
253+
254+
spi_flash_op_lock();
255+
spi_flash_mark_modified_region(dst, size);
256+
spi_flash_op_unlock();
257+
253258
return spi_flash_translate_rc(rc);
254259
}
255260

256261
esp_err_t IRAM_ATTR spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size)
257262
{
258-
if ((dest_addr % 32) != 0) {
263+
const uint8_t *ssrc = (const uint8_t *)src;
264+
if ((dest_addr % 16) != 0) {
259265
return ESP_ERR_INVALID_ARG;
260266
}
261-
if ((size % 32) != 0) {
267+
if ((size % 16) != 0) {
262268
return ESP_ERR_INVALID_SIZE;
263269
}
264-
if ((uint32_t) src < 0x3ff00000) {
265-
// if source address is in DROM, we won't be able to read it
266-
// from within SPIWrite
267-
// TODO: consider buffering source data using heap and writing it anyway?
268-
return ESP_ERR_INVALID_ARG;
269-
}
270+
270271
COUNTER_START();
271272
spi_flash_disable_interrupts_caches_and_other_cpu();
272273
SpiFlashOpResult rc;
273274
rc = spi_flash_unlock();
275+
spi_flash_enable_interrupts_caches_and_other_cpu();
276+
274277
if (rc == SPI_FLASH_RESULT_OK) {
275278
/* SPI_Encrypt_Write encrypts data in RAM as it writes,
276279
so copy to a temporary buffer - 32 bytes at a time.
280+
281+
Each call to SPI_Encrypt_Write takes a 32 byte "row" of
282+
data to encrypt, and each row is two 16 byte AES blocks
283+
that share a key (as derived from flash address).
277284
*/
278-
uint32_t encrypt_buf[32/sizeof(uint32_t)];
279-
for (size_t i = 0; i < size; i += 32) {
280-
memcpy(encrypt_buf, ((const uint8_t *)src) + i, 32);
281-
rc = SPI_Encrypt_Write((uint32_t) dest_addr + i, encrypt_buf, 32);
285+
uint8_t encrypt_buf[32] __attribute__((aligned(4)));
286+
uint32_t row_size;
287+
for (size_t i = 0; i < size; i += row_size) {
288+
uint32_t row_addr = dest_addr + i;
289+
if (i == 0 && (row_addr % 32) != 0) {
290+
/* writing to second block of a 32 byte row */
291+
row_size = 16;
292+
row_addr -= 16;
293+
/* copy to second block in buffer */
294+
memcpy(encrypt_buf + 16, ssrc + i, 16);
295+
/* decrypt the first block from flash, will reencrypt to same bytes */
296+
spi_flash_read_encrypted(row_addr, encrypt_buf, 16);
297+
}
298+
else if (size - i == 16) {
299+
/* 16 bytes left, is first block of a 32 byte row */
300+
row_size = 16;
301+
/* copy to first block in buffer */
302+
memcpy(encrypt_buf, ssrc + i, 16);
303+
/* decrypt the second block from flash, will reencrypt to same bytes */
304+
spi_flash_read_encrypted(row_addr + 16, encrypt_buf + 16, 16);
305+
}
306+
else {
307+
/* Writing a full 32 byte row (2 blocks) */
308+
row_size = 32;
309+
memcpy(encrypt_buf, ssrc + i, 32);
310+
}
311+
312+
spi_flash_disable_interrupts_caches_and_other_cpu();
313+
rc = SPI_Encrypt_Write(row_addr, (uint32_t *)encrypt_buf, 32);
314+
spi_flash_enable_interrupts_caches_and_other_cpu();
282315
if (rc != SPI_FLASH_RESULT_OK) {
283316
break;
284317
}
285318
}
286319
bzero(encrypt_buf, sizeof(encrypt_buf));
287320
}
288321
COUNTER_ADD_BYTES(write, size);
322+
323+
spi_flash_op_lock();
324+
spi_flash_mark_modified_region(dest_addr, size);
325+
spi_flash_op_unlock();
326+
289327
return spi_flash_translate_rc(rc);
290328
}
291329

@@ -381,6 +419,31 @@ esp_err_t IRAM_ATTR spi_flash_read(size_t src, void *dstv, size_t size)
381419
return spi_flash_translate_rc(rc);
382420
}
383421

422+
esp_err_t IRAM_ATTR spi_flash_read_encrypted(size_t src, void *dstv, size_t size)
423+
{
424+
if (src + size > g_rom_flashchip.chip_size) {
425+
return ESP_ERR_INVALID_SIZE;
426+
}
427+
if (size == 0) {
428+
return ESP_OK;
429+
}
430+
431+
esp_err_t err;
432+
const uint8_t *map;
433+
spi_flash_mmap_handle_t map_handle;
434+
size_t map_src = src & ~(SPI_FLASH_MMU_PAGE_SIZE-1);
435+
size_t map_size = size + (src - map_src);
436+
437+
err = spi_flash_mmap(map_src, map_size, SPI_FLASH_MMAP_DATA, (const void **)&map, &map_handle);
438+
if (err != ESP_OK) {
439+
return err;
440+
}
441+
memcpy(dstv, map + (src - map_src), size);
442+
spi_flash_munmap(map_handle);
443+
return err;
444+
}
445+
446+
384447
static esp_err_t spi_flash_translate_rc(SpiFlashOpResult rc)
385448
{
386449
switch (rc) {

components/spi_flash/include/esp_partition.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ esp_err_t esp_partition_read(const esp_partition_t* partition,
191191
* Before writing data to flash, corresponding region of flash needs to be erased.
192192
* This can be done using esp_partition_erase_range function.
193193
*
194+
* Partitions marked with an encryption flag will automatically be
195+
* written via the spi_flash_write_encrypted() function. If writing to
196+
* an encrypted partition, all write offsets and lengths must be
197+
* multiples of 16 bytes. See the spi_flash_write_encrypted() function
198+
* for more details. Unencrypted partitions do not have this
199+
* restriction.
200+
*
194201
* @param partition Pointer to partition structure obtained using
195202
* esp_partition_find_first or esp_partition_get.
196203
* Must be non-NULL.

components/spi_flash/include/esp_spi_flash.h

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ extern "C" {
3131

3232
#define SPI_FLASH_SEC_SIZE 4096 /**< SPI Flash sector size */
3333

34+
#define SPI_FLASH_MMU_PAGE_SIZE 0x10000 /**< Flash cache MMU mapping page size */
35+
3436
/**
3537
* @brief Initialize SPI flash access driver
3638
*
@@ -92,14 +94,18 @@ esp_err_t spi_flash_write(size_t dest_addr, const void *src, size_t size);
9294
*
9395
* @note Flash encryption must be enabled for this function to work.
9496
*
95-
* @note Address in flash, dest, has to be 32-byte aligned.
97+
* @note Flash encryption must be enabled when calling this function.
98+
* If flash encryption is disabled, the function returns
99+
* ESP_ERR_INVALID_STATE. Use esp_flash_encryption_enabled()
100+
* function to determine if flash encryption is enabled.
96101
*
97-
* @note If source address is in DROM, this function will return
98-
* ESP_ERR_INVALID_ARG.
102+
* @note Both dest_addr and size must be multiples of 16 bytes. For
103+
* absolute best performance, both dest_addr and size arguments should
104+
* be multiples of 32 bytes.
99105
*
100-
* @param dest_addr destination address in Flash. Must be a multiple of 32 bytes.
106+
* @param dest_addr destination address in Flash. Must be a multiple of 16 bytes.
101107
* @param src pointer to the source buffer.
102-
* @param size length of data, in bytes. Must be a multiple of 32 bytes.
108+
* @param size length of data, in bytes. Must be a multiple of 16 bytes.
103109
*
104110
* @return esp_err_t
105111
*/
@@ -116,6 +122,23 @@ esp_err_t spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t si
116122
*/
117123
esp_err_t spi_flash_read(size_t src_addr, void *dest, size_t size);
118124

125+
126+
/**
127+
* @brief Read data from Encrypted Flash.
128+
*
129+
* If flash encryption is enabled, this function will transparently decrypt data as it is read.
130+
* If flash encryption is not enabled, this function behaves the same as spi_flash_read().
131+
*
132+
* See esp_flash_encryption_enabled() for a function to check if flash encryption is enabled.
133+
*
134+
* @param src source address of the data in Flash.
135+
* @param dest pointer to the destination buffer
136+
* @param size length of data
137+
*
138+
* @return esp_err_t
139+
*/
140+
esp_err_t spi_flash_read_encrypted(size_t src, void *dest, size_t size);
141+
119142
/**
120143
* @brief Enumeration which specifies memory space requested in an mmap call
121144
*/
@@ -140,7 +163,8 @@ typedef uint32_t spi_flash_mmap_handle_t;
140163
* page allocation, use spi_flash_mmap_dump function.
141164
*
142165
* @param src_addr Physical address in flash where requested region starts.
143-
* This address *must* be aligned to 64kB boundary.
166+
* This address *must* be aligned to 64kB boundary
167+
* (SPI_FLASH_MMU_PAGE_SIZE).
144168
* @param size Size of region which has to be mapped. This size will be rounded
145169
* up to a 64k boundary.
146170
* @param memory Memory space where the region should be mapped
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2010-2016 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Common header for SPI flash test data
16+
#pragma once
17+
18+
/* Define a region of flash we can mess up for testing...
19+
20+
This is pretty ugly, better to do something with a partition but
21+
this is OK for now.
22+
*/
23+
#define TEST_REGION_START 0x180000
24+
#define TEST_REGION_END 0x1E0000

0 commit comments

Comments
 (0)