mirror of https://github.com/wolfSSL/wolfBoot.git
hal: add additional flash hal tests
parent
2d6cf95c20
commit
16c160146f
168
hal/hal.c
168
hal/hal.c
|
@ -33,6 +33,8 @@
|
|||
#ifndef TEST_ADDRESS
|
||||
#define TEST_SZ WOLFBOOT_SECTOR_SIZE
|
||||
#define TEST_ADDRESS WOLFBOOT_PARTITION_UPDATE_ADDRESS
|
||||
#define TEST_ADDRESS_BANKA WOLFBOOT_PARTITION_BOOT_ADDRESS
|
||||
#define TEST_ADDRESS_BANKB WOLFBOOT_PARTITION_UPDATE_ADDRESS
|
||||
#endif
|
||||
|
||||
int hal_flash_test(void)
|
||||
|
@ -60,8 +62,12 @@ int hal_flash_test(void)
|
|||
}
|
||||
|
||||
/* Write Page */
|
||||
hal_flash_unlock();
|
||||
ret = hal_flash_write(TEST_ADDRESS, (uint8_t*)pageData, sizeof(pageData));
|
||||
hal_flash_lock();
|
||||
wolfBoot_printf("Write Page: Ret %d\n", ret);
|
||||
if (ret != 0)
|
||||
return ret;
|
||||
#endif /* !TEST_FLASH_READONLY */
|
||||
|
||||
/* Compare Page */
|
||||
|
@ -74,4 +80,166 @@ int hal_flash_test(void)
|
|||
wolfBoot_printf("Internal Flash Test Passed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef TEST_FLASH_READONLY
|
||||
int hal_flash_test_write_once(void)
|
||||
{
|
||||
uint8_t test_byte, expected_byte;
|
||||
unsigned int i, b;
|
||||
int ret = 0;
|
||||
|
||||
/* Erase the test sector */
|
||||
hal_flash_unlock();
|
||||
ret = hal_flash_erase(TEST_ADDRESS, TEST_SZ);
|
||||
hal_flash_lock();
|
||||
if (ret != 0) {
|
||||
wolfBoot_printf("Erase Sector failed: Ret %d\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
expected_byte = 0xFF;
|
||||
/* For each bit in the byte (from LSB to MSB) */
|
||||
for (b = 0; b < 8; b++) {
|
||||
/* Toggle bit from 1 to 0 */
|
||||
test_byte = 0xFF & ~(1 << b);
|
||||
expected_byte &= ~(1 << b);
|
||||
|
||||
/* Write the data */
|
||||
hal_flash_unlock();
|
||||
ret = hal_flash_write(TEST_ADDRESS, &test_byte, sizeof(test_byte));
|
||||
hal_flash_lock();
|
||||
|
||||
if (ret != 0) {
|
||||
wolfBoot_printf("Write failed at byte %d, bit %d: Ret %d\n", i, b, ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Verify the write by direct comparison */
|
||||
if (memcmp((void*)TEST_ADDRESS, &expected_byte, sizeof(expected_byte)) != 0) {
|
||||
wolfBoot_printf("Verification failed at byte %d, bit %d\n", i, b);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
wolfBoot_printf("Write-once test passed\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Test if unaligned write works. First test writing 1 bytes at SECTOR + 1.
|
||||
* Then test writing 2 bytes that span the sector boundary.
|
||||
*/
|
||||
int hal_flash_test_align(void)
|
||||
{
|
||||
int ret = 0;
|
||||
uint8_t test_data_1 = 0xAA;
|
||||
uint8_t test_data_2[2] = {0xBB, 0xCC};
|
||||
uint8_t read_back[2];
|
||||
|
||||
/* erase both sectors */
|
||||
hal_flash_unlock();
|
||||
ret = hal_flash_erase(TEST_ADDRESS, TEST_SZ * 2);
|
||||
hal_flash_lock();
|
||||
if (ret != 0) {
|
||||
wolfBoot_printf("Erase Sector failed: Ret %d\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Write 1 byte at SECTOR + 1 */
|
||||
hal_flash_unlock();
|
||||
ret = hal_flash_write(TEST_ADDRESS + 1, &test_data_1, 1);
|
||||
hal_flash_lock();
|
||||
if (ret != 0) {
|
||||
wolfBoot_printf("Unaligned write (1 byte) failed: Ret %d\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Verify 1 byte write */
|
||||
if (*(uint8_t*)(TEST_ADDRESS + 1) != test_data_1) {
|
||||
wolfBoot_printf("Unaligned write verification (1 byte) failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Write 2 bytes spanning sector boundary */
|
||||
hal_flash_unlock();
|
||||
ret = hal_flash_write(TEST_ADDRESS + TEST_SZ - 1, test_data_2, 2);
|
||||
hal_flash_lock();
|
||||
if (ret != 0) {
|
||||
wolfBoot_printf("Unaligned write (2 bytes) failed: Ret %d\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Verify 2 bytes write */
|
||||
memcpy(read_back, (void*)(TEST_ADDRESS + TEST_SZ - 1), 2);
|
||||
if (read_back[0] != test_data_2[0] || read_back[1] != test_data_2[1]) {
|
||||
wolfBoot_printf("Unaligned write verification (2 bytes) failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
wolfBoot_printf("Unaligned write test passed\n");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* This test can be run only if swapping the flash do not reboot the board */
|
||||
#if defined(DUALBANK_SWAP) && !defined(TEST_FLASH_READONLY)
|
||||
int hal_flash_test_dualbank(void)
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t i, len;
|
||||
uint8_t cur_fill = 0xb0;
|
||||
uint8_t new_fill = 0xf0;
|
||||
uint8_t fill;
|
||||
uint32_t bankA = TEST_ADDRESS_BANKA;
|
||||
uint32_t bankB = TEST_ADDRESS_BANKB;
|
||||
uint32_t pagePtr;
|
||||
|
||||
wolfBoot_printf("swap flash test at 0x%x\n", TEST_ADDRESS);
|
||||
|
||||
for (i = 0; i < 2; i++) {
|
||||
fill = (i == 0) ? cur_fill : new_fill;
|
||||
pagePtr = (i == 0) ? TEST_ADDRESS_BANKA : TEST_ADDRESS_BANKB;
|
||||
|
||||
/* Erase sector */
|
||||
hal_flash_unlock();
|
||||
ret = hal_flash_erase(pagePtr, WOLFBOOT_SECTOR_SIZE);
|
||||
hal_flash_lock();
|
||||
if (ret != 0) {
|
||||
wolfBoot_printf("Erase Sector failed: Ret %d\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Write Page */
|
||||
hal_flash_unlock();
|
||||
ret = hal_flash_write(pagePtr, (uint8_t*)&fill, sizeof(fill));
|
||||
hal_flash_lock();
|
||||
if (ret != 0) {
|
||||
wolfBoot_printf("Write Page failed: Ret %d\n", ret);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (*((uint8_t*)(TEST_ADDRESS_BANKA)) != cur_fill) {
|
||||
wolfBoot_printf("Bank A data mismatch: %x != %x\n", bankA[0], cur_fill);
|
||||
return -1;
|
||||
}
|
||||
if (*((uint8_t*)(TEST_ADDRESS_BANKB)) != new_fill) {
|
||||
wolfBoot_printf("Bank B data mismatch: %x != %x\n", bankB[0], new_fill);
|
||||
return -1;
|
||||
}
|
||||
hal_flash_dualbank_swap();
|
||||
|
||||
if (*((uint8_t*)(TEST_ADDRESS_BANKA)) != new_fill) {
|
||||
wolfBoot_printf("Bank A data mismatch after swap: %x != %x\n", bankA[0], new_fill);
|
||||
return -1;
|
||||
}
|
||||
if (*((uint8_t*)(TEST_ADDRESS_BANKB)) != cur_fill) {
|
||||
wolfBoot_printf("Bank B data mismatch after swap: %x != %x\n", bankB[0], cur_fill);
|
||||
return -1;
|
||||
}
|
||||
|
||||
wolfBoot_printf("DUALBANK_SWAP test passed");
|
||||
return 0;
|
||||
}
|
||||
#endif /* DUALBANK_SWAP */
|
||||
|
||||
#endif /* TEST_FLASH */
|
||||
|
|
Loading…
Reference in New Issue