Merge branch 'bugfix/partition_and_mmap_issues' into 'master'

Fix partition- and mmap-related issues

- Fix unit tests failing to start up due to a mismatch between sdkconfig flash size and size required by partition table.
- Fix a bug that partition APIs loaded the partition table in reverse order. As such, `esp_partition_next` and `esp_partition_find_first` did not work as expected. Add a test.
- Update the workaround for stale cache reads issue: do Cache_Flush for non-encrypted flash as well. Add a test.

See merge request !555
This commit is contained in:
Ivan Grokhotkov
2017-03-06 15:59:28 +08:00
5 changed files with 54 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "unity.h"
#include "test_utils.h"
#include "esp_partition.h"
@@ -9,26 +10,30 @@ TEST_CASE("Can read partition table", "[partition]")
const esp_partition_t *p = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, NULL);
TEST_ASSERT_NOT_NULL(p);
TEST_ASSERT_EQUAL(p->address, 0x10000);
TEST_ASSERT_EQUAL(p->subtype, ESP_PARTITION_SUBTYPE_APP_FACTORY);
TEST_ASSERT_EQUAL(0x10000, p->address);
TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, p->subtype);
esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, NULL);
TEST_ASSERT_NOT_NULL(it);
int count = 0;
const esp_partition_t* prev = NULL;
for (; it != NULL; it = esp_partition_next(it)) {
const esp_partition_t *p = esp_partition_get(it);
TEST_ASSERT_NOT_NULL(p);
if (prev) {
TEST_ASSERT_TRUE_MESSAGE(prev->address < p->address, "incorrect partition order");
}
prev = p;
++count;
}
esp_partition_iterator_release(it);
TEST_ASSERT_EQUAL(count, 2);
printf("%d\n", __builtin_clz(count));
TEST_ASSERT_EQUAL(3, count);
}
TEST_CASE("Can write, read, mmap partition", "[partition][ignore]")
{
const esp_partition_t *p = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, NULL);
const esp_partition_t *p = get_test_data_partition();
printf("Using partition %s at 0x%x, size 0x%x\n", p->label, p->address, p->size);
TEST_ASSERT_NOT_NULL(p);
const size_t max_size = 2 * SPI_FLASH_SEC_SIZE;
uint8_t *data = (uint8_t *) malloc(max_size);
@@ -46,9 +51,6 @@ TEST_CASE("Can write, read, mmap partition", "[partition][ignore]")
}
for (size_t i = 0; i < block_size / 4; ++i) {
((uint32_t *) (data))[i] = rand();
if (i == 0 && offset == 0) {
printf("write: %08x\n", ((uint32_t *) (data))[i]);
}
}
TEST_ASSERT_EQUAL(ESP_OK, esp_partition_write(p, offset, data, block_size));
}

View File

@@ -187,12 +187,12 @@ esp_err_t IRAM_ATTR spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_
}
/* This is a temporary fix for an issue where some
encrypted cache reads may see stale data.
cache reads may see stale data.
Working on a long term fix that doesn't require invalidating
entire cache.
*/
if (esp_flash_encryption_enabled() && !did_flush && need_flush) {
if (!did_flush && need_flush) {
Cache_Flush(0);
Cache_Flush(1);
}

View File

@@ -184,6 +184,7 @@ static esp_err_t load_partitions()
} else {
SLIST_INSERT_AFTER(last, item, next);
}
last = item;
}
spi_flash_munmap(handle);
return ESP_OK;

View File

@@ -138,6 +138,8 @@ TEST_CASE("Can mmap into data address space", "[spi_flash]")
TEST_CASE("Can mmap into instruction address space", "[mmap]")
{
setup_mmap_tests();
printf("Mapping %x (+%x)\n", start, end - start);
spi_flash_mmap_handle_t handle1;
const void *ptr1;
@@ -288,3 +290,23 @@ TEST_CASE("mmap consistent with phys2cache/cache2phys", "[spi_flash]")
TEST_ASSERT_EQUAL_HEX(SPI_FLASH_CACHE2PHYS_FAIL, spi_flash_cache2phys(ptr));
}
TEST_CASE("munmap followed by mmap flushes cache", "[spi_flash]")
{
setup_mmap_tests();
const esp_partition_t *p = get_test_data_partition();
const uint32_t* data;
spi_flash_mmap_handle_t handle;
TEST_ESP_OK( esp_partition_mmap(p, 0, SPI_FLASH_MMU_PAGE_SIZE,
SPI_FLASH_MMAP_DATA, (const void **) &data, &handle) );
uint32_t buf[16];
memcpy(buf, data, sizeof(buf));
spi_flash_munmap(handle);
TEST_ESP_OK( esp_partition_mmap(p, SPI_FLASH_MMU_PAGE_SIZE, SPI_FLASH_MMU_PAGE_SIZE,
SPI_FLASH_MMAP_DATA, (const void **) &data, &handle) );
TEST_ASSERT_NOT_EQUAL(0, memcmp(buf, data, sizeof(buf)));
}