2016-11-17 03:36:10 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-01-04 23:51:02 -05:00
|
|
|
#include <string.h>
|
2016-11-17 03:36:10 -05:00
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
|
#include <freertos/task.h>
|
|
|
|
#include <freertos/semphr.h>
|
|
|
|
|
|
|
|
#include <unity.h>
|
2022-06-27 03:24:07 -04:00
|
|
|
#include <spi_flash_mmap.h>
|
2016-11-17 03:36:10 -05:00
|
|
|
#include <esp_attr.h>
|
2017-02-17 02:26:43 -05:00
|
|
|
#include <esp_partition.h>
|
2017-01-04 23:51:02 -05:00
|
|
|
#include <esp_flash_encrypt.h>
|
2022-10-14 08:15:32 -04:00
|
|
|
#include "esp_flash.h"
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2017-02-20 21:40:42 -05:00
|
|
|
#include "test_utils.h"
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2022-05-20 06:16:47 -04:00
|
|
|
|
|
|
|
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
|
|
|
|
//IDF-5138
|
2017-01-04 23:51:02 -05:00
|
|
|
static uint32_t buffer[1024];
|
|
|
|
|
2017-02-20 21:40:42 -05:00
|
|
|
/* read-only region used for mmap tests, intialised in setup_mmap_tests() */
|
|
|
|
static uint32_t start;
|
|
|
|
static uint32_t end;
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2017-02-19 22:24:08 -05:00
|
|
|
static spi_flash_mmap_handle_t handle1, handle2, handle3;
|
2022-05-20 06:16:47 -04:00
|
|
|
#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
|
2016-11-17 03:36:10 -05:00
|
|
|
|
2019-08-23 00:37:55 -04:00
|
|
|
static esp_err_t spi_flash_read_maybe_encrypted(size_t src_addr, void *des_addr, size_t size)
|
|
|
|
{
|
|
|
|
if (!esp_flash_encryption_enabled()) {
|
2022-06-27 03:24:07 -04:00
|
|
|
return esp_flash_read(NULL, des_addr, src_addr, size);
|
2019-08-23 00:37:55 -04:00
|
|
|
} else {
|
2022-06-27 03:24:07 -04:00
|
|
|
return esp_flash_read_encrypted(NULL, src_addr, des_addr, size);
|
2019-08-23 00:37:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 06:16:47 -04:00
|
|
|
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
|
|
|
|
//IDF-5138
|
2019-08-23 00:37:55 -04:00
|
|
|
static esp_err_t spi_flash_write_maybe_encrypted(size_t des_addr, const void *src_addr, size_t size)
|
|
|
|
{
|
|
|
|
if (!esp_flash_encryption_enabled()) {
|
2022-06-27 03:24:07 -04:00
|
|
|
return esp_flash_write(NULL, src_addr, des_addr, size);
|
2019-08-23 00:37:55 -04:00
|
|
|
} else {
|
2022-06-27 03:24:07 -04:00
|
|
|
return esp_flash_write_encrypted(NULL, des_addr, src_addr, size);
|
2019-08-23 00:37:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void setup_mmap_tests(void)
|
2016-11-17 03:36:10 -05:00
|
|
|
{
|
2017-02-20 21:40:42 -05:00
|
|
|
if (start == 0) {
|
|
|
|
const esp_partition_t *part = get_test_data_partition();
|
|
|
|
start = part->address;
|
|
|
|
end = part->address + part->size;
|
|
|
|
printf("Test data partition @ 0x%x - 0x%x\n", start, end);
|
|
|
|
}
|
|
|
|
TEST_ASSERT(end > start);
|
|
|
|
TEST_ASSERT(end - start >= 512*1024);
|
|
|
|
|
2017-02-19 22:24:08 -05:00
|
|
|
/* clean up any mmap handles left over from failed tests */
|
|
|
|
if (handle1) {
|
|
|
|
spi_flash_munmap(handle1);
|
|
|
|
handle1 = 0;
|
|
|
|
}
|
|
|
|
if (handle2) {
|
|
|
|
spi_flash_munmap(handle2);
|
|
|
|
handle2 = 0;
|
|
|
|
}
|
|
|
|
if (handle3) {
|
|
|
|
spi_flash_munmap(handle3);
|
|
|
|
handle3 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prepare flash contents */
|
2016-11-17 03:36:10 -05:00
|
|
|
srand(0);
|
|
|
|
for (int block = start / 0x10000; block < end / 0x10000; ++block) {
|
|
|
|
for (int sector = 0; sector < 16; ++sector) {
|
2017-02-20 21:40:42 -05:00
|
|
|
uint32_t abs_sector = (block * 16) + sector;
|
2017-02-19 22:24:08 -05:00
|
|
|
uint32_t sector_offs = abs_sector * SPI_FLASH_SEC_SIZE;
|
|
|
|
bool sector_needs_write = false;
|
|
|
|
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( spi_flash_read_maybe_encrypted(sector_offs, buffer, sizeof(buffer)) );
|
2017-02-19 22:24:08 -05:00
|
|
|
|
2016-11-17 03:36:10 -05:00
|
|
|
for (uint32_t word = 0; word < 1024; ++word) {
|
|
|
|
uint32_t val = rand();
|
|
|
|
if (block == start / 0x10000 && sector == 0 && word == 0) {
|
2017-02-20 21:40:42 -05:00
|
|
|
printf("setup_mmap_tests(): first prepped word: 0x%08x (flash holds 0x%08x)\n", val, buffer[word]);
|
2017-02-19 22:24:08 -05:00
|
|
|
}
|
|
|
|
if (buffer[word] != val) {
|
|
|
|
buffer[word] = val;
|
|
|
|
sector_needs_write = true;
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
}
|
2017-02-19 22:24:08 -05:00
|
|
|
/* Only rewrite the sector if it has changed */
|
|
|
|
if (sector_needs_write) {
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( esp_flash_erase_region(NULL, (uint16_t) abs_sector * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE) );
|
|
|
|
TEST_ESP_OK( spi_flash_write_maybe_encrypted(sector_offs, (const uint8_t *) buffer, sizeof(buffer)) );
|
2017-02-19 22:24:08 -05:00
|
|
|
}
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 01:25:33 -05:00
|
|
|
TEST_CASE("Can mmap into data address space", "[spi_flash][mmap]")
|
2016-11-17 03:36:10 -05:00
|
|
|
{
|
2017-02-19 22:24:08 -05:00
|
|
|
setup_mmap_tests();
|
2016-11-17 03:36:10 -05:00
|
|
|
|
|
|
|
printf("Mapping %x (+%x)\n", start, end - start);
|
|
|
|
const void *ptr1;
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( spi_flash_mmap(start, end - start, SPI_FLASH_MMAP_DATA, &ptr1, &handle1) );
|
2016-11-17 03:36:10 -05:00
|
|
|
printf("mmap_res: handle=%d ptr=%p\n", handle1, ptr1);
|
|
|
|
|
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
srand(0);
|
|
|
|
const uint32_t *data = (const uint32_t *) ptr1;
|
|
|
|
for (int block = 0; block < (end - start) / 0x10000; ++block) {
|
2017-02-20 21:40:42 -05:00
|
|
|
printf("block %d\n", block);
|
2016-11-17 03:36:10 -05:00
|
|
|
for (int sector = 0; sector < 16; ++sector) {
|
2017-02-20 21:40:42 -05:00
|
|
|
printf("sector %d\n", sector);
|
2016-11-17 03:36:10 -05:00
|
|
|
for (uint32_t word = 0; word < 1024; ++word) {
|
2017-02-20 21:40:42 -05:00
|
|
|
TEST_ASSERT_EQUAL_HEX32(rand(), data[(block * 16 + sector) * 1024 + word]);
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("Mapping %x (+%x)\n", start - 0x10000, 0x20000);
|
|
|
|
const void *ptr2;
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( spi_flash_mmap(start - 0x10000, 0x20000, SPI_FLASH_MMAP_DATA, &ptr2, &handle2) );
|
2016-11-17 03:36:10 -05:00
|
|
|
printf("mmap_res: handle=%d ptr=%p\n", handle2, ptr2);
|
2017-02-17 02:26:43 -05:00
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_HEX32(start - 0x10000, spi_flash_cache2phys(ptr2));
|
|
|
|
TEST_ASSERT_EQUAL_PTR(ptr2, spi_flash_phys2cache(start - 0x10000, SPI_FLASH_MMAP_DATA));
|
|
|
|
|
2016-11-17 03:36:10 -05:00
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
printf("Mapping %x (+%x)\n", start, 0x10000);
|
|
|
|
const void *ptr3;
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( spi_flash_mmap(start, 0x10000, SPI_FLASH_MMAP_DATA, &ptr3, &handle3) );
|
2016-11-17 03:36:10 -05:00
|
|
|
printf("mmap_res: handle=%d ptr=%p\n", handle3, ptr3);
|
2017-02-17 02:26:43 -05:00
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_HEX32(start, spi_flash_cache2phys(ptr3));
|
|
|
|
TEST_ASSERT_EQUAL_PTR(ptr3, spi_flash_phys2cache(start, SPI_FLASH_MMAP_DATA));
|
|
|
|
TEST_ASSERT_EQUAL_PTR((intptr_t)ptr3 + 0x4444, spi_flash_phys2cache(start + 0x4444, SPI_FLASH_MMAP_DATA));
|
|
|
|
|
2016-11-17 03:36:10 -05:00
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
printf("Unmapping handle1\n");
|
|
|
|
spi_flash_munmap(handle1);
|
2017-02-19 22:24:08 -05:00
|
|
|
handle1 = 0;
|
2016-11-17 03:36:10 -05:00
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
printf("Unmapping handle2\n");
|
|
|
|
spi_flash_munmap(handle2);
|
2017-02-19 22:24:08 -05:00
|
|
|
handle2 = 0;
|
2016-11-17 03:36:10 -05:00
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
printf("Unmapping handle3\n");
|
|
|
|
spi_flash_munmap(handle3);
|
2017-02-19 22:24:08 -05:00
|
|
|
handle3 = 0;
|
2017-02-17 02:26:43 -05:00
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_PTR(NULL, spi_flash_phys2cache(start, SPI_FLASH_MMAP_DATA));
|
2016-11-17 03:36:10 -05:00
|
|
|
}
|
2017-01-04 23:51:02 -05:00
|
|
|
|
2021-06-09 04:38:22 -04:00
|
|
|
#if !DISABLED_FOR_TARGETS(ESP32S3, ESP32C3)
|
|
|
|
/* On S3/C3 the cache is programmatically split between Icache and dcache and with the default setup we dont leave a lot pages
|
2021-01-26 22:24:27 -05:00
|
|
|
available for additional mmaps into instruction space. Disabling this test for now since any hypothetical use case for this
|
|
|
|
is no longer supported "out of the box"
|
|
|
|
*/
|
2020-12-15 22:50:13 -05:00
|
|
|
|
2020-01-02 01:25:33 -05:00
|
|
|
TEST_CASE("Can mmap into instruction address space", "[spi_flash][mmap]")
|
2017-02-24 08:50:05 -05:00
|
|
|
{
|
2017-03-04 04:48:44 -05:00
|
|
|
setup_mmap_tests();
|
|
|
|
|
2017-02-24 08:50:05 -05:00
|
|
|
printf("Mapping %x (+%x)\n", start, end - start);
|
|
|
|
spi_flash_mmap_handle_t handle1;
|
|
|
|
const void *ptr1;
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( spi_flash_mmap(start, end - start, SPI_FLASH_MMAP_INST, &ptr1, &handle1) );
|
2017-02-24 08:50:05 -05:00
|
|
|
printf("mmap_res: handle=%d ptr=%p\n", handle1, ptr1);
|
|
|
|
|
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
srand(0);
|
|
|
|
const uint32_t *data = (const uint32_t *) ptr1;
|
|
|
|
for (int block = 0; block < (end - start) / 0x10000; ++block) {
|
|
|
|
for (int sector = 0; sector < 16; ++sector) {
|
|
|
|
for (uint32_t word = 0; word < 1024; ++word) {
|
|
|
|
TEST_ASSERT_EQUAL_UINT32(rand(), data[(block * 16 + sector) * 1024 + word]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("Mapping %x (+%x)\n", start - 0x10000, 0x20000);
|
|
|
|
spi_flash_mmap_handle_t handle2;
|
|
|
|
const void *ptr2;
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( spi_flash_mmap(start - 0x10000, 0x20000, SPI_FLASH_MMAP_INST, &ptr2, &handle2) );
|
2017-02-24 08:50:05 -05:00
|
|
|
printf("mmap_res: handle=%d ptr=%p\n", handle2, ptr2);
|
2019-06-27 10:35:06 -04:00
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_HEX32(start - 0x10000, spi_flash_cache2phys(ptr2));
|
|
|
|
TEST_ASSERT_EQUAL_PTR(ptr2, spi_flash_phys2cache(start - 0x10000, SPI_FLASH_MMAP_INST));
|
|
|
|
|
2017-02-24 08:50:05 -05:00
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
printf("Mapping %x (+%x)\n", start, 0x10000);
|
|
|
|
spi_flash_mmap_handle_t handle3;
|
|
|
|
const void *ptr3;
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( spi_flash_mmap(start, 0x10000, SPI_FLASH_MMAP_INST, &ptr3, &handle3) );
|
2017-02-24 08:50:05 -05:00
|
|
|
printf("mmap_res: handle=%d ptr=%p\n", handle3, ptr3);
|
2019-06-27 10:35:06 -04:00
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_HEX32(start, spi_flash_cache2phys(ptr3));
|
|
|
|
TEST_ASSERT_EQUAL_PTR(ptr3, spi_flash_phys2cache(start, SPI_FLASH_MMAP_INST));
|
|
|
|
|
2017-02-24 08:50:05 -05:00
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
printf("Unmapping handle1\n");
|
|
|
|
spi_flash_munmap(handle1);
|
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
printf("Unmapping handle2\n");
|
|
|
|
spi_flash_munmap(handle2);
|
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
printf("Unmapping handle3\n");
|
|
|
|
spi_flash_munmap(handle3);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-09 04:38:22 -04:00
|
|
|
#endif //!DISABLED_FOR_TARGETS(ESP32S3, ESP32C3)
|
2020-12-15 22:50:13 -05:00
|
|
|
|
|
|
|
|
2020-01-02 01:25:33 -05:00
|
|
|
TEST_CASE("Can mmap unordered pages into contiguous memory", "[spi_flash][mmap]")
|
2017-06-14 15:33:44 -04:00
|
|
|
{
|
|
|
|
int nopages;
|
|
|
|
int *pages;
|
|
|
|
int startpage;
|
|
|
|
|
|
|
|
setup_mmap_tests();
|
|
|
|
nopages=(end-start)/SPI_FLASH_MMU_PAGE_SIZE;
|
|
|
|
pages=alloca(sizeof(int)*nopages);
|
|
|
|
|
|
|
|
startpage=start/SPI_FLASH_MMU_PAGE_SIZE;
|
|
|
|
|
|
|
|
//make inverse mapping: virt 0 -> page (nopages-1), virt 1 -> page (nopages-2), ...
|
|
|
|
for (int i=0; i<nopages; i++) {
|
|
|
|
pages[i]=startpage+(nopages-1)-i;
|
|
|
|
printf("Offset %x page %d\n", i*0x10000, pages[i]);
|
|
|
|
}
|
2019-01-08 05:29:25 -05:00
|
|
|
|
2017-06-14 15:33:44 -04:00
|
|
|
printf("Attempting mapping of unordered pages to contiguous memory area\n");
|
|
|
|
|
|
|
|
spi_flash_mmap_handle_t handle1;
|
|
|
|
const void *ptr1;
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( spi_flash_mmap_pages(pages, nopages, SPI_FLASH_MMAP_DATA, &ptr1, &handle1) );
|
2017-06-14 15:33:44 -04:00
|
|
|
printf("mmap_res: handle=%d ptr=%p\n", handle1, ptr1);
|
|
|
|
|
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
srand(0);
|
|
|
|
const uint32_t *data = (const uint32_t *) ptr1;
|
|
|
|
for (int block = 0; block < nopages; ++block) {
|
|
|
|
for (int sector = 0; sector < 16; ++sector) {
|
|
|
|
for (uint32_t word = 0; word < 1024; ++word) {
|
|
|
|
TEST_ASSERT_EQUAL_UINT32(rand(), data[(((nopages-1)-block) * 16 + sector) * 1024 + word]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Unmapping handle1\n");
|
|
|
|
spi_flash_munmap(handle1);
|
|
|
|
spi_flash_mmap_dump();
|
|
|
|
}
|
|
|
|
|
2019-08-23 00:37:55 -04:00
|
|
|
TEST_CASE("flash_mmap invalidates just-written data", "[spi_flash][mmap]")
|
2017-01-04 23:51:02 -05:00
|
|
|
{
|
|
|
|
const void *ptr1;
|
|
|
|
|
|
|
|
const size_t test_size = 128;
|
|
|
|
|
2017-02-19 22:24:08 -05:00
|
|
|
setup_mmap_tests();
|
|
|
|
|
2017-01-04 23:51:02 -05:00
|
|
|
if (esp_flash_encryption_enabled()) {
|
|
|
|
TEST_IGNORE_MESSAGE("flash encryption enabled, spi_flash_write_encrypted() test won't pass as-is");
|
|
|
|
}
|
|
|
|
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( esp_flash_erase_region(NULL, start, SPI_FLASH_SEC_SIZE) );
|
2017-01-04 23:51:02 -05:00
|
|
|
|
|
|
|
/* map erased test region to ptr1 */
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( spi_flash_mmap(start, test_size, SPI_FLASH_MMAP_DATA, &ptr1, &handle1) );
|
2017-01-04 23:51:02 -05:00
|
|
|
printf("mmap_res ptr1: handle=%d ptr=%p\n", handle1, ptr1);
|
|
|
|
|
|
|
|
/* verify it's all 0xFF */
|
|
|
|
for (int i = 0; i < test_size; i++) {
|
|
|
|
TEST_ASSERT_EQUAL_HEX(0xFF, ((uint8_t *)ptr1)[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unmap the erased region */
|
|
|
|
spi_flash_munmap(handle1);
|
2017-02-19 22:24:08 -05:00
|
|
|
handle1 = 0;
|
2017-01-04 23:51:02 -05:00
|
|
|
|
|
|
|
/* write flash region to 0xEE */
|
|
|
|
uint8_t buf[test_size];
|
|
|
|
memset(buf, 0xEE, test_size);
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( esp_flash_write(NULL, buf, start, test_size) );
|
2017-01-04 23:51:02 -05:00
|
|
|
|
|
|
|
/* re-map the test region at ptr1.
|
|
|
|
|
|
|
|
this is a fresh mmap call so should trigger a cache flush,
|
|
|
|
ensuring we see the updated flash.
|
|
|
|
*/
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( spi_flash_mmap(start, test_size, SPI_FLASH_MMAP_DATA, &ptr1, &handle1) );
|
2017-01-04 23:51:02 -05:00
|
|
|
printf("mmap_res ptr1 #2: handle=%d ptr=%p\n", handle1, ptr1);
|
|
|
|
|
|
|
|
/* assert that ptr1 now maps to the new values on flash,
|
|
|
|
ie contents of buf array.
|
|
|
|
*/
|
|
|
|
TEST_ASSERT_EQUAL_HEX8_ARRAY(buf, ptr1, test_size);
|
|
|
|
|
|
|
|
spi_flash_munmap(handle1);
|
2017-02-19 22:24:08 -05:00
|
|
|
handle1 = 0;
|
2017-01-04 23:51:02 -05:00
|
|
|
}
|
2017-02-17 02:26:43 -05:00
|
|
|
|
2019-08-23 00:37:55 -04:00
|
|
|
TEST_CASE("flash_mmap can mmap after get enough free MMU pages", "[spi_flash][mmap]")
|
2018-03-16 03:47:31 -04:00
|
|
|
{
|
2019-01-08 05:29:25 -05:00
|
|
|
//this test case should make flash size >= 4MB, because max size of Dcache can mapped is 4MB
|
2018-03-16 03:47:31 -04:00
|
|
|
setup_mmap_tests();
|
|
|
|
|
|
|
|
printf("Mapping %x (+%x)\n", start, end - start);
|
|
|
|
const void *ptr1;
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( spi_flash_mmap(start, end - start, SPI_FLASH_MMAP_DATA, &ptr1, &handle1) );
|
2018-03-16 03:47:31 -04:00
|
|
|
printf("mmap_res: handle=%d ptr=%p\n", handle1, ptr1);
|
|
|
|
|
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
srand(0);
|
|
|
|
const uint32_t *data = (const uint32_t *) ptr1;
|
|
|
|
for (int block = 0; block < (end - start) / 0x10000; ++block) {
|
|
|
|
printf("block %d\n", block);
|
|
|
|
for (int sector = 0; sector < 16; ++sector) {
|
|
|
|
printf("sector %d\n", sector);
|
|
|
|
for (uint32_t word = 0; word < 1024; ++word) {
|
|
|
|
TEST_ASSERT_EQUAL_HEX32(rand(), data[(block * 16 + sector) * 1024 + word]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint32_t free_pages = spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
|
2022-06-27 03:24:07 -04:00
|
|
|
uint32_t flash_size;
|
|
|
|
TEST_ESP_OK(esp_flash_get_size(NULL, &flash_size));
|
|
|
|
uint32_t flash_pages = flash_size / SPI_FLASH_MMU_PAGE_SIZE;
|
2020-08-27 23:53:28 -04:00
|
|
|
free_pages = (free_pages > flash_pages) ? flash_pages : free_pages;
|
2018-03-29 05:48:59 -04:00
|
|
|
|
2018-03-16 03:47:31 -04:00
|
|
|
printf("Mapping %x (+%x)\n", 0, free_pages * SPI_FLASH_MMU_PAGE_SIZE);
|
|
|
|
const void *ptr2;
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( spi_flash_mmap(0, free_pages * SPI_FLASH_MMU_PAGE_SIZE, SPI_FLASH_MMAP_DATA, &ptr2, &handle2) );
|
2018-03-16 03:47:31 -04:00
|
|
|
printf("mmap_res: handle=%d ptr=%p\n", handle2, ptr2);
|
|
|
|
|
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
|
|
|
|
printf("Unmapping handle1\n");
|
|
|
|
spi_flash_munmap(handle1);
|
|
|
|
handle1 = 0;
|
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
printf("Unmapping handle2\n");
|
|
|
|
spi_flash_munmap(handle2);
|
|
|
|
handle2 = 0;
|
|
|
|
spi_flash_mmap_dump();
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_PTR(NULL, spi_flash_phys2cache(start, SPI_FLASH_MMAP_DATA));
|
|
|
|
}
|
2022-05-20 06:16:47 -04:00
|
|
|
#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
|
2018-03-16 03:47:31 -04:00
|
|
|
|
2020-01-02 01:25:33 -05:00
|
|
|
TEST_CASE("phys2cache/cache2phys basic checks", "[spi_flash][mmap]")
|
2017-02-17 02:26:43 -05:00
|
|
|
{
|
2017-02-20 21:40:42 -05:00
|
|
|
uint8_t buf[64];
|
|
|
|
|
2020-12-15 22:50:13 -05:00
|
|
|
/* Avoid put constant data in the sdata/sdata2 section */
|
|
|
|
static const uint8_t constant_data[] = { 1, 2, 3, 7, 11, 16, 3, 88, 99};
|
2017-02-17 02:26:43 -05:00
|
|
|
|
|
|
|
/* esp_partition_find is in IROM */
|
|
|
|
uint32_t phys = spi_flash_cache2phys(esp_partition_find);
|
|
|
|
TEST_ASSERT_NOT_EQUAL(SPI_FLASH_CACHE2PHYS_FAIL, phys);
|
|
|
|
TEST_ASSERT_EQUAL_PTR(esp_partition_find, spi_flash_phys2cache(phys, SPI_FLASH_MMAP_INST));
|
2023-07-02 23:46:51 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
|
2017-02-17 02:26:43 -05:00
|
|
|
TEST_ASSERT_EQUAL_PTR(NULL, spi_flash_phys2cache(phys, SPI_FLASH_MMAP_DATA));
|
2023-07-02 23:46:51 -04:00
|
|
|
#endif //#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
|
2017-02-17 02:26:43 -05:00
|
|
|
|
2017-02-20 21:40:42 -05:00
|
|
|
/* Read the flash @ 'phys' and compare it to the data we get via regular cache access */
|
2019-08-23 00:37:55 -04:00
|
|
|
spi_flash_read_maybe_encrypted(phys, buf, sizeof(buf));
|
2017-02-20 21:40:42 -05:00
|
|
|
TEST_ASSERT_EQUAL_HEX32_ARRAY((void *)esp_partition_find, buf, sizeof(buf)/sizeof(uint32_t));
|
2017-02-17 02:26:43 -05:00
|
|
|
|
|
|
|
/* spi_flash_mmap is in IRAM */
|
|
|
|
printf("%p\n", spi_flash_mmap);
|
|
|
|
TEST_ASSERT_EQUAL_HEX32(SPI_FLASH_CACHE2PHYS_FAIL,
|
|
|
|
spi_flash_cache2phys(spi_flash_mmap));
|
|
|
|
|
2017-02-20 21:40:42 -05:00
|
|
|
/* 'constant_data' should be in DROM */
|
|
|
|
phys = spi_flash_cache2phys(&constant_data);
|
2017-02-17 02:26:43 -05:00
|
|
|
TEST_ASSERT_NOT_EQUAL(SPI_FLASH_CACHE2PHYS_FAIL, phys);
|
2017-02-20 21:40:42 -05:00
|
|
|
TEST_ASSERT_EQUAL_PTR(&constant_data,
|
2017-02-17 02:26:43 -05:00
|
|
|
spi_flash_phys2cache(phys, SPI_FLASH_MMAP_DATA));
|
2023-07-02 23:46:51 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
|
2017-02-17 02:26:43 -05:00
|
|
|
TEST_ASSERT_EQUAL_PTR(NULL, spi_flash_phys2cache(phys, SPI_FLASH_MMAP_INST));
|
2023-07-02 23:46:51 -04:00
|
|
|
#endif
|
2017-02-17 02:26:43 -05:00
|
|
|
|
2017-02-20 21:40:42 -05:00
|
|
|
/* Read the flash @ 'phys' and compare it to the data we get via normal cache access */
|
2019-08-23 00:37:55 -04:00
|
|
|
spi_flash_read_maybe_encrypted(phys, buf, sizeof(constant_data));
|
2017-02-20 21:40:42 -05:00
|
|
|
TEST_ASSERT_EQUAL_HEX8_ARRAY(constant_data, buf, sizeof(constant_data));
|
2017-02-17 02:26:43 -05:00
|
|
|
}
|
|
|
|
|
2022-05-20 06:16:47 -04:00
|
|
|
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
|
|
|
|
//IDF-5138
|
2019-08-23 00:37:55 -04:00
|
|
|
TEST_CASE("mmap consistent with phys2cache/cache2phys", "[spi_flash][mmap]")
|
2017-02-17 02:26:43 -05:00
|
|
|
{
|
|
|
|
const void *ptr = NULL;
|
|
|
|
const size_t test_size = 2 * SPI_FLASH_MMU_PAGE_SIZE;
|
|
|
|
|
2017-02-19 22:24:08 -05:00
|
|
|
setup_mmap_tests();
|
|
|
|
|
2017-02-17 02:26:43 -05:00
|
|
|
TEST_ASSERT_EQUAL_HEX(SPI_FLASH_CACHE2PHYS_FAIL, spi_flash_cache2phys(ptr));
|
|
|
|
|
2022-06-27 03:24:07 -04:00
|
|
|
TEST_ESP_OK( spi_flash_mmap(start, test_size, SPI_FLASH_MMAP_DATA, &ptr, &handle1) );
|
2017-02-17 02:26:43 -05:00
|
|
|
TEST_ASSERT_NOT_NULL(ptr);
|
2017-02-19 22:24:08 -05:00
|
|
|
TEST_ASSERT_NOT_EQUAL(0, handle1);
|
2017-02-17 02:26:43 -05:00
|
|
|
|
2017-02-20 21:40:42 -05:00
|
|
|
TEST_ASSERT_EQUAL_HEX(start, spi_flash_cache2phys(ptr));
|
|
|
|
TEST_ASSERT_EQUAL_HEX(start + 1024, spi_flash_cache2phys((void *)((intptr_t)ptr + 1024)));
|
|
|
|
TEST_ASSERT_EQUAL_HEX(start + 3000, spi_flash_cache2phys((void *)((intptr_t)ptr + 3000)));
|
2017-02-17 02:26:43 -05:00
|
|
|
/* this pointer lands in a different MMU table entry */
|
2017-02-20 21:40:42 -05:00
|
|
|
TEST_ASSERT_EQUAL_HEX(start + test_size - 4, spi_flash_cache2phys((void *)((intptr_t)ptr + test_size - 4)));
|
2017-02-17 02:26:43 -05:00
|
|
|
|
2017-02-19 22:24:08 -05:00
|
|
|
spi_flash_munmap(handle1);
|
|
|
|
handle1 = 0;
|
2017-02-17 02:26:43 -05:00
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_HEX(SPI_FLASH_CACHE2PHYS_FAIL, spi_flash_cache2phys(ptr));
|
2017-01-04 23:51:02 -05:00
|
|
|
}
|
2017-03-04 04:48:44 -05:00
|
|
|
|
2019-08-23 00:37:55 -04:00
|
|
|
TEST_CASE("munmap followed by mmap flushes cache", "[spi_flash][mmap]")
|
2017-03-04 04:48:44 -05:00
|
|
|
{
|
|
|
|
setup_mmap_tests();
|
|
|
|
|
|
|
|
const esp_partition_t *p = get_test_data_partition();
|
|
|
|
|
2022-11-07 06:31:29 -05:00
|
|
|
const uint32_t *data;
|
|
|
|
esp_partition_mmap_handle_t handle;
|
2017-03-04 04:48:44 -05:00
|
|
|
TEST_ESP_OK( esp_partition_mmap(p, 0, SPI_FLASH_MMU_PAGE_SIZE,
|
2022-11-07 06:31:29 -05:00
|
|
|
ESP_PARTITION_MMAP_DATA, (const void **) &data, &handle) );
|
2017-03-04 04:48:44 -05:00
|
|
|
uint32_t buf[16];
|
|
|
|
memcpy(buf, data, sizeof(buf));
|
|
|
|
|
2022-11-07 06:31:29 -05:00
|
|
|
esp_partition_munmap(handle);
|
2017-03-04 04:48:44 -05:00
|
|
|
TEST_ESP_OK( esp_partition_mmap(p, SPI_FLASH_MMU_PAGE_SIZE, SPI_FLASH_MMU_PAGE_SIZE,
|
2022-11-07 06:31:29 -05:00
|
|
|
ESP_PARTITION_MMAP_DATA, (const void **) &data, &handle) );
|
2017-03-04 04:48:44 -05:00
|
|
|
TEST_ASSERT_NOT_EQUAL(0, memcmp(buf, data, sizeof(buf)));
|
|
|
|
}
|
|
|
|
|
2020-01-02 01:25:33 -05:00
|
|
|
TEST_CASE("no stale data read post mmap and write partition", "[spi_flash][mmap]")
|
2019-03-08 00:37:10 -05:00
|
|
|
{
|
2019-08-23 00:37:55 -04:00
|
|
|
/* Buffer size is set to 32 to allow encrypted flash writes */
|
|
|
|
const char buf[32] = "Test buffer data for partition";
|
2019-03-08 00:37:10 -05:00
|
|
|
char read_data[sizeof(buf)];
|
|
|
|
setup_mmap_tests();
|
|
|
|
|
|
|
|
const esp_partition_t *p = get_test_data_partition();
|
|
|
|
|
2022-11-07 06:31:29 -05:00
|
|
|
const uint32_t *data;
|
|
|
|
esp_partition_mmap_handle_t handle;
|
2019-03-08 00:37:10 -05:00
|
|
|
TEST_ESP_OK(esp_partition_mmap(p, 0, SPI_FLASH_MMU_PAGE_SIZE,
|
2022-11-07 06:31:29 -05:00
|
|
|
ESP_PARTITION_MMAP_DATA, (const void **) &data, &handle) );
|
2019-03-08 00:37:10 -05:00
|
|
|
memcpy(read_data, data, sizeof(read_data));
|
|
|
|
TEST_ESP_OK(esp_partition_erase_range(p, 0, SPI_FLASH_MMU_PAGE_SIZE));
|
2019-08-23 00:37:55 -04:00
|
|
|
/* not using esp_partition_write here, since the partition in not marked as "encrypted"
|
|
|
|
in the partition table */
|
|
|
|
TEST_ESP_OK(spi_flash_write_maybe_encrypted(p->address + 0, buf, sizeof(buf)));
|
2019-03-08 00:37:10 -05:00
|
|
|
/* This should retrigger actual flash content read */
|
|
|
|
memcpy(read_data, data, sizeof(read_data));
|
|
|
|
|
2022-11-07 06:31:29 -05:00
|
|
|
esp_partition_munmap(handle);
|
2019-03-08 00:37:10 -05:00
|
|
|
TEST_ASSERT_EQUAL(0, memcmp(buf, read_data, sizeof(buf)));
|
|
|
|
}
|
2022-05-20 06:16:47 -04:00
|
|
|
#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
|