Skip to content

Commit 7555031

Browse files
committed
add api get valid mmu table pages number
1 parent 7e268ad commit 7555031

File tree

4 files changed

+89
-21
lines changed

4 files changed

+89
-21
lines changed

components/bootloader_support/src/bootloader_flash.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
static const char *TAG = "bootloader_mmap";
2525

26-
static spi_flash_mmap_memory_t map;
26+
static spi_flash_mmap_handle_t map;
2727

2828
const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
2929
{
@@ -36,7 +36,8 @@ const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
3636
size += (src_addr - src_page);
3737
esp_err_t err = spi_flash_mmap(src_page, size, SPI_FLASH_MMAP_DATA, &result, &map);
3838
if (err != ESP_OK) {
39-
result = NULL;
39+
ESP_LOGE(TAG, "spi_flash_mmap failed: 0x%x", err);
40+
return NULL;
4041
}
4142
return (void *)((intptr_t)result + (src_addr - src_page));
4243
}

components/bootloader_support/src/esp_image_format.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <esp_image_format.h>
2020
#include <esp_secure_boot.h>
2121
#include <esp_log.h>
22+
#include <esp_spi_flash.h>
2223
#include <bootloader_flash.h>
2324
#include <bootloader_random.h>
2425
#include <bootloader_sha.h>
@@ -48,6 +49,9 @@ static bool should_map(uint32_t load_addr);
4849
/* Load or verify a segment */
4950
static esp_err_t process_segment(int index, uint32_t flash_addr, esp_image_segment_header_t *header, bool silent, bool do_load, bootloader_sha256_handle_t sha_handle, uint32_t *checksum);
5051

52+
/* split segment and verify if data_len is too long */
53+
static esp_err_t process_segment_data(intptr_t load_addr, uint32_t data_addr, uint32_t data_len, bool do_load, bootloader_sha256_handle_t sha_handle, uint32_t *checksum);
54+
5155
/* Verify the main image header */
5256
static esp_err_t verify_image_header(uint32_t src_addr, const esp_image_header_t *image, bool silent);
5357

@@ -291,7 +295,34 @@ static esp_err_t process_segment(int index, uint32_t flash_addr, esp_image_segme
291295
}
292296
}
293297
}
298+
#ifndef BOOTLOADER_BUILD
299+
uint32_t page_count = spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
300+
ESP_LOGI(TAG, "free data page_count 0x%08x",page_count);
301+
while (data_len >= page_count * SPI_FLASH_MMU_PAGE_SIZE) {
302+
err = process_segment_data(load_addr, data_addr, page_count * SPI_FLASH_MMU_PAGE_SIZE, do_load, sha_handle, checksum);
303+
if (err != ESP_OK) {
304+
return err;
305+
}
306+
data_addr += page_count * SPI_FLASH_MMU_PAGE_SIZE;
307+
data_len -= page_count * SPI_FLASH_MMU_PAGE_SIZE;
308+
}
309+
#endif
310+
err = process_segment_data(load_addr, data_addr, data_len, do_load, sha_handle, checksum);
311+
if (err != ESP_OK) {
312+
return err;
313+
}
314+
return ESP_OK;
294315

316+
err:
317+
if (err == ESP_OK) {
318+
err = ESP_ERR_IMAGE_INVALID;
319+
}
320+
321+
return err;
322+
}
323+
324+
static esp_err_t process_segment_data(intptr_t load_addr, uint32_t data_addr, uint32_t data_len, bool do_load, bootloader_sha256_handle_t sha_handle, uint32_t *checksum)
325+
{
295326
const uint32_t *data = (const uint32_t *)bootloader_mmap(data_addr, data_len);
296327
if(!data) {
297328
ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed",
@@ -332,12 +363,6 @@ static esp_err_t process_segment(int index, uint32_t flash_addr, esp_image_segme
332363
bootloader_munmap(data);
333364

334365
return ESP_OK;
335-
336-
err:
337-
if (err == ESP_OK) {
338-
err = ESP_ERR_IMAGE_INVALID;
339-
}
340-
return err;
341366
}
342367

343368
static esp_err_t verify_segment_header(int index, const esp_image_segment_header_t *segment, uint32_t segment_data_offs, bool silent)

components/spi_flash/flash_mmap.c

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ static void IRAM_ATTR spi_flash_mmap_init()
100100
DPORT_STALL_OTHER_CPU_END();
101101
}
102102

103+
static void IRAM_ATTR get_mmu_region(spi_flash_mmap_memory_t memory, int* out_begin, int* out_size,uint32_t* region_addr)
104+
{
105+
if (memory == SPI_FLASH_MMAP_DATA) {
106+
// Vaddr0
107+
*out_begin = 0;
108+
*out_size = 64;
109+
*region_addr = VADDR0_START_ADDR;
110+
} else {
111+
// only part of VAddr1 is usable, so adjust for that
112+
*out_begin = PRO_IRAM0_FIRST_USABLE_PAGE;
113+
*out_size = 3 * 64 - *out_begin;
114+
*region_addr = VADDR1_FIRST_USABLE_ADDR;
115+
}
116+
}
117+
103118
esp_err_t IRAM_ATTR spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory,
104119
const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
105120
{
@@ -157,17 +172,7 @@ esp_err_t IRAM_ATTR spi_flash_mmap_pages(int *pages, size_t page_count, spi_flas
157172
int region_begin; // first page to check
158173
int region_size; // number of pages to check
159174
uint32_t region_addr; // base address of memory region
160-
if (memory == SPI_FLASH_MMAP_DATA) {
161-
// Vaddr0
162-
region_begin = 0;
163-
region_size = 64;
164-
region_addr = VADDR0_START_ADDR;
165-
} else {
166-
// only part of VAddr1 is usable, so adjust for that
167-
region_begin = PRO_IRAM0_FIRST_USABLE_PAGE;
168-
region_size = 3 * 64 - region_begin;
169-
region_addr = VADDR1_FIRST_USABLE_ADDR;
170-
}
175+
get_mmu_region(memory,&region_begin,&region_size,&region_addr);
171176
if (region_size < page_count) {
172177
return ESP_ERR_NO_MEM;
173178
}
@@ -176,7 +181,7 @@ esp_err_t IRAM_ATTR spi_flash_mmap_pages(int *pages, size_t page_count, spi_flas
176181
// entries are treated as wildcards.
177182
int start;
178183
int end = region_begin + region_size - page_count;
179-
for (start = region_begin; start < end; ++start) {
184+
for (start = region_begin; start <= end; ++start) {
180185
int pageno = 0;
181186
int pos;
182187
DPORT_STALL_OTHER_CPU_START();
@@ -194,7 +199,7 @@ esp_err_t IRAM_ATTR spi_flash_mmap_pages(int *pages, size_t page_count, spi_flas
194199
}
195200
}
196201
// checked all the region(s) and haven't found anything?
197-
if (start == end) {
202+
if (start > end) {
198203
*out_handle = 0;
199204
*out_ptr = NULL;
200205
ret = ESP_ERR_NO_MEM;
@@ -292,6 +297,28 @@ void spi_flash_mmap_dump()
292297
}
293298
}
294299

300+
uint32_t spi_flash_mmap_get_free_pages(spi_flash_mmap_memory_t memory)
301+
{
302+
spi_flash_mmap_init();
303+
int count = 0;
304+
int region_begin; // first page to check
305+
int region_size; // number of pages to check
306+
uint32_t region_addr; // base address of memory region
307+
get_mmu_region(memory,&region_begin,&region_size,&region_addr);
308+
DPORT_STALL_OTHER_CPU_START();
309+
for (int i = region_begin; i < region_begin + region_size; ++i) {
310+
if (s_mmap_page_refcnt[i] == 0 && DPORT_PRO_FLASH_MMU_TABLE[i] == INVALID_ENTRY_VAL) {
311+
count++;
312+
}
313+
}
314+
DPORT_STALL_OTHER_CPU_END();
315+
if(count >= 1) {
316+
return count - 1; //don't sure mmap src_addr,if src_addr not align 64K bytes,max need one more pages
317+
} else {
318+
return 0;
319+
}
320+
}
321+
295322
/* 256-bit (up to 16MB of 64KB pages) bitset of all flash pages
296323
that have been written to since last cache flush.
297324

components/spi_flash/include/esp_spi_flash.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,21 @@ void spi_flash_munmap(spi_flash_mmap_handle_t handle);
236236
*/
237237
void spi_flash_mmap_dump();
238238

239+
/**
240+
* @brief get free pages number which can be mmap
241+
*
242+
* @note This function will return free page number of the mmu table which can mmap,
243+
* when you want to call spi_flash_mmap to mmap an ranger of flash data to Dcache or Icache
244+
* memmory region, maybe the size of MMU table will exceed,so if you are not sure the
245+
* size need mmap is ok, can call the interface and watch how many MMU table page can be
246+
* mmaped.
247+
*
248+
* @param memory memmory type of MMU table free page
249+
*
250+
* @return number of free pages which can be mmaped
251+
*/
252+
uint32_t spi_flash_mmap_get_free_pages(spi_flash_mmap_memory_t memory);
253+
239254

240255
#define SPI_FLASH_CACHE2PHYS_FAIL UINT32_MAX /*<! Result from spi_flash_cache2phys() if flash cache address is invalid */
241256

0 commit comments

Comments
 (0)