Consolidate duplicate TEST_FLASH code.

pull/507/head
David Garske 2024-09-27 15:31:49 -07:00 committed by Daniele Lacamera
parent e3b98f1d7d
commit 322aa325b6
9 changed files with 88 additions and 288 deletions

View File

@ -33,6 +33,7 @@ OBJS:= \
./src/string.o \
./src/image.o \
./src/libwolfboot.o \
./hal/hal.o \
./hal/$(TARGET).o
ifeq ($(SIGN),NONE)

77
hal/hal.c 100644
View File

@ -0,0 +1,77 @@
/* hal.c
*
* Copyright (C) 2024 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/* Code shared between all HAL's */
#include <stdint.h>
#include "hal.h"
#include "string.h"
#include "printf.h"
/* Test for internal flash erase/write */
/* Use TEST_EXT_FLASH to test ext flash (see spi_flash.c or qspi_flash.c) */
#ifdef TEST_FLASH
#ifndef TEST_ADDRESS
#define TEST_SZ WOLFBOOT_SECTOR_SIZE
#define TEST_ADDRESS WOLFBOOT_PARTITION_UPDATE_ADDRESS
#endif
int hal_flash_test(void)
{
int ret = 0;
uint32_t i, len;
uint8_t* pagePtr = (uint8_t*)TEST_ADDRESS;
static uint8_t pageData[TEST_SZ];
wolfBoot_printf("Internal flash test at 0x%x\n", TEST_ADDRESS);
/* Setup test data */
for (i=0; i<sizeof(pageData); i++) {
((uint8_t*)pageData)[i] = (i & 0xff);
}
#ifndef TEST_FLASH_READONLY
/* Erase sector */
hal_flash_unlock();
ret = hal_flash_erase(TEST_ADDRESS, sizeof(pageData));
hal_flash_lock();
if (ret != 0) {
wolfBoot_printf("Erase Sector failed: Ret %d\n", ret);
return ret;
}
/* Write Page */
ret = hal_flash_write(TEST_ADDRESS, (uint8_t*)pageData, sizeof(pageData));
wolfBoot_printf("Write Page: Ret %d\n", ret);
#endif /* !TEST_FLASH_READONLY */
/* Compare Page */
ret = memcmp((void*)TEST_ADDRESS, pageData, sizeof(pageData));
if (ret != 0) {
wolfBoot_printf("Check Data @ %d failed\n", ret);
return ret;
}
wolfBoot_printf("Internal Flash Test Passed\n");
return ret;
}
#endif /* TEST_FLASH */

View File

@ -66,12 +66,6 @@
#include "xip/fsl_flexspi_nor_boot.h"
/* #define DEBUG_EXT_FLASH */
/* #define TEST_FLASH */
#ifdef TEST_FLASH
static int test_flash(void);
#endif
#ifdef __WOLFBOOT
@ -891,11 +885,6 @@ void hal_init(void)
uart_write("wolfBoot HAL Init\n", 18);
#endif
hal_flash_init();
#ifdef TEST_FLASH
if (test_flash() != 0) {
wolfBoot_printf("Flash Test Failed!\n");
}
#endif
}
void hal_prepare_boot(void)
@ -976,7 +965,7 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
/**
* Flash is memory mapped, so the address range must be invalidated in data cache
* to ensure coherency between flash and cache.
*
*
* Also, both the address and size must be 32-byte aligned as cache-lines are 32 bytes
* (see definition of DCACHE_InvalidateByRange).
* To ensure all data is included we align the address downwards, and the length upwards.
@ -1020,7 +1009,7 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
/**
* Flash is memory mapped, so the address range must be invalidated in data cache
* to ensure coherency between flash and cache.
*
*
* Also, both the address and size must be 32-byte aligned as cache-lines are 32 bytes
* (see definition of DCACHE_InvalidateByRange).
* To ensure all data is included we align the address downwards, and the length upwards.
@ -1034,43 +1023,3 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
return -1;
return 0;
}
#ifdef TEST_FLASH
#ifndef TEST_ADDRESS
#define TEST_ADDRESS (FLASH_BASE + 0x700000) /* 7MB */
#endif
/* #define TEST_FLASH_READONLY */
static uint32_t pageData[WOLFBOOT_SECTOR_SIZE/4]; /* force 32-bit alignment */
static int test_flash(void)
{
int ret;
uint32_t i;
#ifndef TEST_FLASH_READONLY
/* Erase sector */
ret = hal_flash_erase(TEST_ADDRESS, WOLFBOOT_SECTOR_SIZE);
wolfBoot_printf("Erase Sector: Ret %d\n", ret);
/* Fill data into the page_buffer */
for (i=0; i<sizeof(pageData)/sizeof(pageData[0]); i++) {
pageData[i] = (i << 24) | (i << 16) | (i << 8) | i;
}
/* Write Page */
ret = hal_flash_write(TEST_ADDRESS, (uint8_t*)pageData, sizeof(pageData));
wolfBoot_printf("Write Page: Ret %d\n", ret);
#endif /* !TEST_FLASH_READONLY */
/* Compare Page */
ret = memcmp((void*)TEST_ADDRESS, pageData, sizeof(pageData));
if (ret != 0) {
wolfBoot_printf("Check Data @ %d failed\n", ret);
return ret;
}
wolfBoot_printf("Flash Test Passed\n");
return ret;
}
#endif /* TEST_FLASH */

View File

@ -38,10 +38,6 @@
* Any key size greater than 128 bits must be divided and distributed over multiple key slot instances.
*/
#ifdef TEST_FLASH
static int test_flash(void);
#endif
/* Network updates can be signed with "--id 2" and placed into the normal update partition,
* or they can be placed into the external flash at offset 0x100000 */
#ifndef PART_NET_ID
@ -478,12 +474,7 @@ void hal_init(void)
(SPU_EXTDOMAIN_PERM_SECATTR_SECURE | SPU_EXTDOMAIN_PERM_UNLOCK);
#endif
#ifdef TEST_FLASH
if (test_flash() != 0) {
wolfBoot_printf("Internal flash Test Failed!\n");
}
#endif
/* need early init of external flash to support checking network core */
spi_flash_probe();
hal_net_check_version();
@ -505,56 +496,4 @@ void hal_prepare_boot(void)
#endif
}
/* Test for internal flash erase/write */
/* Use TEST_EXT_FLASH to test external QSPI flash (see qspi_flash.c) */
#ifdef TEST_FLASH
#ifndef TEST_ADDRESS
#define TEST_SZ (WOLFBOOT_SECTOR_SIZE * 2)
#define TEST_ADDRESS (FLASH_BASE_ADDR + (FLASH_SIZE - TEST_SZ))
#endif
/* #define TEST_FLASH_READONLY */
static int test_flash(void)
{
int ret = 0;
uint32_t i, len;
uint8_t* pagePtr = (uint8_t*)TEST_ADDRESS;
static uint8_t pageData[TEST_SZ];
wolfBoot_printf("Internal flash test at 0x%x\n", TEST_ADDRESS);
/* Setup test data */
for (i=0; i<sizeof(pageData); i++) {
((uint8_t*)pageData)[i] = (i & 0xff);
}
#ifndef TEST_FLASH_READONLY
/* Erase sector */
hal_flash_unlock();
ret = hal_flash_erase(TEST_ADDRESS, sizeof(pageData));
hal_flash_lock();
if (ret != 0) {
wolfBoot_printf("Erase Sector failed: Ret %d\n", ret);
return ret;
}
/* Write Page */
ret = hal_flash_write(TEST_ADDRESS, (uint8_t*)pageData, sizeof(pageData));
wolfBoot_printf("Write Page: Ret %d\n", ret);
#endif /* !TEST_FLASH_READONLY */
/* Compare Page */
ret = memcmp((void*)TEST_ADDRESS, pageData, sizeof(pageData));
if (ret != 0) {
wolfBoot_printf("Check Data @ %d failed\n", ret);
return ret;
}
wolfBoot_printf("Internal Flash Test Passed\n");
return ret;
}
#endif /* TEST_FLASH */
#endif /* TARGET_* */

View File

@ -37,7 +37,6 @@
/* Tests */
#if 0
#define TEST_DDR
#define TEST_FLASH
#define TEST_TPM
#endif
#define ENABLE_PCIE
@ -56,9 +55,6 @@
#if defined(ENABLE_DDR) && defined(TEST_DDR)
static int test_ddr(void);
#endif
#if defined(ENABLE_ELBC) && defined(TEST_FLASH)
static int test_flash(void);
#endif
#if defined(ENABLE_ESPI) && defined(TEST_TPM)
static int test_tpm(void);
#endif
@ -1569,12 +1565,6 @@ void hal_init(void)
}
#endif
#if defined(ENABLE_ELBC) && defined(TEST_FLASH)
if (test_flash() != 0) {
wolfBoot_printf("Flash Test Failed!\n");
}
#endif
#if defined(ENABLE_ESPI) && defined(TEST_TPM)
if (test_tpm() != 0) {
wolfBoot_printf("TPM Test Failed!\n");
@ -1895,52 +1885,6 @@ static int test_ddr(void)
}
#endif /* ENABLE_DDR && TEST_DDR */
#if defined(ENABLE_ELBC) && defined(TEST_FLASH)
#ifndef TEST_ADDRESS
#define TEST_ADDRESS 0x2800000 /* 40MB */
#endif
/* #define TEST_FLASH_READONLY */
static int test_flash(void)
{
int ret;
uint32_t i;
uint32_t pageData[WOLFBOOT_SECTOR_SIZE/4]; /* force 32-bit alignment */
#ifndef TEST_FLASH_READONLY
/* Erase sector */
ret = ext_flash_erase(TEST_ADDRESS, WOLFBOOT_SECTOR_SIZE);
wolfBoot_printf("Erase Sector: Ret %d\n", ret);
/* Write Pages */
for (i=0; i<sizeof(pageData); i++) {
((uint8_t*)pageData)[i] = (i & 0xff);
}
ret = ext_flash_write(TEST_ADDRESS, (uint8_t*)pageData, sizeof(pageData));
wolfBoot_printf("Write Page: Ret %d\n", ret);
#endif /* !TEST_FLASH_READONLY */
/* Read page */
memset(pageData, 0, sizeof(pageData));
ret = ext_flash_read(TEST_ADDRESS, (uint8_t*)pageData, sizeof(pageData));
wolfBoot_printf("Read Page: Ret %d\n", ret);
wolfBoot_printf("Checking...\n");
/* Check data */
for (i=0; i<sizeof(pageData); i++) {
wolfBoot_printf("check[%3d] %02x\n", i, pageData[i]);
if (((uint8_t*)pageData)[i] != (i & 0xff)) {
wolfBoot_printf("Check Data @ %d failed\n", i);
return -i;
}
}
wolfBoot_printf("Flash Test Passed\n");
return ret;
}
#endif /* ENABLE_ELBC && TEST_FLASH */
#if defined(ENABLE_ESPI) && defined(TEST_TPM)
int test_tpm(void)
{

View File

@ -43,7 +43,6 @@
/* Tests */
#if 0
#define TEST_DDR
#define TEST_FLASH
#define TEST_TPM
#endif
@ -72,9 +71,6 @@
#if defined(ENABLE_DDR) && defined(TEST_DDR)
static int test_ddr(void);
#endif
#if defined(ENABLE_IFC) && defined(TEST_FLASH)
static int test_flash(void);
#endif
#if defined(ENABLE_ESPI) && defined(TEST_TPM)
static int test_tpm(void);
#endif
@ -3013,12 +3009,6 @@ void hal_init(void)
}
#endif
#if defined(ENABLE_IFC) && defined(TEST_FLASH)
if (test_flash() != 0) {
wolfBoot_printf("Flash Test Failed!\n");
}
#endif
#if defined(ENABLE_ESPI) && defined(TEST_TPM)
if (test_tpm() != 0) {
wolfBoot_printf("TPM Test Failed!\n");
@ -3449,51 +3439,6 @@ static int test_ddr(void)
}
#endif /* ENABLE_DDR && TEST_DDR */
#if defined(ENABLE_IFC) && defined(TEST_FLASH)
#ifndef TEST_ADDRESS
/* 0xEC100000 (1MB offset) */
#define TEST_ADDRESS (FLASH_BASE_ADDR + (1 * 0x100000))
#endif
/* #define TEST_FLASH_READONLY */
static uint32_t pageData[FLASH_PAGE_SIZE/sizeof(uint32_t)]; /* force 32-bit alignment */
static int test_flash(void)
{
int ret;
uint32_t i;
uint8_t* pagePtr = (uint8_t*)TEST_ADDRESS;
#ifndef TEST_FLASH_READONLY
/* Erase sector */
ret = hal_flash_erase(TEST_ADDRESS, sizeof(pageData));
wolfBoot_printf("Erase Sector: Ret %d\n", ret);
/* Write Pages */
for (i=0; i<sizeof(pageData); i++) {
((uint8_t*)pageData)[i] = (i & 0xff);
}
ret = hal_flash_write(TEST_ADDRESS, (uint8_t*)pageData, sizeof(pageData));
wolfBoot_printf("Write Page: Ret %d\n", ret);
#endif /* !TEST_FLASH_READONLY */
/* invalidate cache */
flush_cache((uint32_t)pagePtr, sizeof(pageData));
wolfBoot_printf("Checking...\n");
ret = memcmp(pageData, pagePtr, sizeof(pageData));
if (ret != 0) {
wolfBoot_printf("Check Data @ %d failed\n", ret);
return -ret;
}
wolfBoot_printf("Flash Test Passed\n");
return ret;
}
#endif /* ENABLE_IFC && TEST_FLASH */
#if defined(ENABLE_ESPI) && defined(TEST_TPM)
int test_tpm(void)
{

View File

@ -61,9 +61,6 @@
/* forward declaration */
int hal_flash_init(void);
#ifdef TEST_FLASH
static int test_flash(void);
#endif
static void hal_panic(void)
{
@ -395,10 +392,6 @@ void hal_init(void)
hal_flash_init();
#ifdef TEST_FLASH
test_flash();
#endif
#if defined(WOLFBOOT_RENESAS_TSIP) && \
!defined(WOLFBOOT_RENESAS_APP)
err = wolfCrypt_Init();
@ -636,58 +629,3 @@ void* hal_get_update_address(void)
{
return (void*)WOLFBOOT_PARTITION_UPDATE_ADDRESS;
}
#ifdef TEST_FLASH
#ifndef TEST_ADDRESS
/* 3,840 KB offset in 4MB flash */
/* 1,792 KB offset in 2MB flash */
#define TEST_ADDRESS (0xFFFC0000)
#endif
/* #define TEST_FLASH_READONLY */
static uint8_t pageData[1024];
static int test_flash(void)
{
int ret;
uint32_t i, len;
uint8_t* pagePtr = (uint8_t*)TEST_ADDRESS;
/* Setup test data */
for (i=0; i<sizeof(pageData); i++) {
((uint8_t*)pageData)[i] = (i & 0xff);
}
/* Test writting 1 - 1024 */
for (len=1; len<(int)sizeof(pageData); len++) {
#ifndef TEST_FLASH_READONLY
/* Erase sector */
hal_flash_unlock();
ret = hal_flash_erase(TEST_ADDRESS, WOLFBOOT_SECTOR_SIZE);
hal_flash_lock();
if (ret != 0) {
wolfBoot_printf("Erase Sector failed: Ret %d\n", ret);
break;
}
/* Write variable length sector */
hal_flash_unlock();
ret = hal_flash_write(TEST_ADDRESS, (uint8_t*)pageData, len);
hal_flash_lock();
wolfBoot_printf("Write Page (len %d): Ret %d\n", len, ret);
#endif /* !TEST_FLASH_READONLY */
for (i=0; i<len; i++) {
if (pageData[i] != pagePtr[i]) {
wolfBoot_printf("Check Data @ %d with len %d failed\n", i, len);
return -ret;
}
}
}
wolfBoot_printf("Flash Test Passed\n");
return ret;
}
#endif /* TEST_FLASH */

View File

@ -138,6 +138,10 @@ int hal_flash_otp_read(uint32_t flashAddress, void* data, uint32_t length);
#endif
#ifdef TEST_FLASH
int hal_flash_test(void);
#endif
#ifdef __cplusplus
}
#endif

View File

@ -100,6 +100,9 @@ int main(void)
#endif
hal_init();
#ifdef TEST_FLASH
hal_flash_test();
#endif
spi_flash_probe();
#ifdef UART_FLASH
uart_init(UART_FLASH_BITRATE, 8, 'N', 1);