Skip to content

Commit 3f2b682

Browse files
committed
Consolidate duplicate TEST_FLASH code.
1 parent 2e6c27e commit 3f2b682

File tree

8 files changed

+87
-288
lines changed

8 files changed

+87
-288
lines changed

hal/hal.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* hal.c
2+
*
3+
* Copyright (C) 2024 wolfSSL Inc.
4+
*
5+
* This file is part of wolfBoot.
6+
*
7+
* wolfBoot is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfBoot is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
/* Code shared between all HAL's */
23+
24+
#include <stdint.h>
25+
#include "hal.h"
26+
#include "string.h"
27+
#include "printf.h"
28+
29+
/* Test for internal flash erase/write */
30+
/* Use TEST_EXT_FLASH to test ext flash (see spi_flash.c or qspi_flash.c) */
31+
#ifdef TEST_FLASH
32+
33+
#ifndef TEST_ADDRESS
34+
#define TEST_SZ WOLFBOOT_SECTOR_SIZE
35+
#define TEST_ADDRESS WOLFBOOT_PARTITION_UPDATE_ADDRESS
36+
#endif
37+
38+
int hal_flash_test(void)
39+
{
40+
int ret = 0;
41+
uint32_t i, len;
42+
uint8_t* pagePtr = (uint8_t*)TEST_ADDRESS;
43+
static uint8_t pageData[TEST_SZ];
44+
45+
wolfBoot_printf("Internal flash test at 0x%x\n", TEST_ADDRESS);
46+
47+
/* Setup test data */
48+
for (i=0; i<sizeof(pageData); i++) {
49+
((uint8_t*)pageData)[i] = (i & 0xff);
50+
}
51+
52+
#ifndef TEST_FLASH_READONLY
53+
/* Erase sector */
54+
hal_flash_unlock();
55+
ret = hal_flash_erase(TEST_ADDRESS, sizeof(pageData));
56+
hal_flash_lock();
57+
if (ret != 0) {
58+
wolfBoot_printf("Erase Sector failed: Ret %d\n", ret);
59+
return ret;
60+
}
61+
62+
/* Write Page */
63+
ret = hal_flash_write(TEST_ADDRESS, (uint8_t*)pageData, sizeof(pageData));
64+
wolfBoot_printf("Write Page: Ret %d\n", ret);
65+
#endif /* !TEST_FLASH_READONLY */
66+
67+
/* Compare Page */
68+
ret = memcmp((void*)TEST_ADDRESS, pageData, sizeof(pageData));
69+
if (ret != 0) {
70+
wolfBoot_printf("Check Data @ %d failed\n", ret);
71+
return ret;
72+
}
73+
74+
wolfBoot_printf("Internal Flash Test Passed\n");
75+
return ret;
76+
}
77+
#endif /* TEST_FLASH */

hal/imx_rt.c

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@
6666
#include "xip/fsl_flexspi_nor_boot.h"
6767

6868
/* #define DEBUG_EXT_FLASH */
69-
/* #define TEST_FLASH */
70-
71-
#ifdef TEST_FLASH
72-
static int test_flash(void);
73-
#endif
74-
7569

7670
#ifdef __WOLFBOOT
7771

@@ -891,11 +885,6 @@ void hal_init(void)
891885
uart_write("wolfBoot HAL Init\n", 18);
892886
#endif
893887
hal_flash_init();
894-
#ifdef TEST_FLASH
895-
if (test_flash() != 0) {
896-
wolfBoot_printf("Flash Test Failed!\n");
897-
}
898-
#endif
899888
}
900889

901890
void hal_prepare_boot(void)
@@ -976,7 +965,7 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
976965
/**
977966
* Flash is memory mapped, so the address range must be invalidated in data cache
978967
* to ensure coherency between flash and cache.
979-
*
968+
*
980969
* Also, both the address and size must be 32-byte aligned as cache-lines are 32 bytes
981970
* (see definition of DCACHE_InvalidateByRange).
982971
* 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)
10201009
/**
10211010
* Flash is memory mapped, so the address range must be invalidated in data cache
10221011
* to ensure coherency between flash and cache.
1023-
*
1012+
*
10241013
* Also, both the address and size must be 32-byte aligned as cache-lines are 32 bytes
10251014
* (see definition of DCACHE_InvalidateByRange).
10261015
* 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)
10341023
return -1;
10351024
return 0;
10361025
}
1037-
1038-
#ifdef TEST_FLASH
1039-
1040-
#ifndef TEST_ADDRESS
1041-
#define TEST_ADDRESS (FLASH_BASE + 0x700000) /* 7MB */
1042-
#endif
1043-
/* #define TEST_FLASH_READONLY */
1044-
1045-
static uint32_t pageData[WOLFBOOT_SECTOR_SIZE/4]; /* force 32-bit alignment */
1046-
1047-
static int test_flash(void)
1048-
{
1049-
int ret;
1050-
uint32_t i;
1051-
1052-
#ifndef TEST_FLASH_READONLY
1053-
/* Erase sector */
1054-
ret = hal_flash_erase(TEST_ADDRESS, WOLFBOOT_SECTOR_SIZE);
1055-
wolfBoot_printf("Erase Sector: Ret %d\n", ret);
1056-
1057-
/* Fill data into the page_buffer */
1058-
for (i=0; i<sizeof(pageData)/sizeof(pageData[0]); i++) {
1059-
pageData[i] = (i << 24) | (i << 16) | (i << 8) | i;
1060-
}
1061-
/* Write Page */
1062-
ret = hal_flash_write(TEST_ADDRESS, (uint8_t*)pageData, sizeof(pageData));
1063-
wolfBoot_printf("Write Page: Ret %d\n", ret);
1064-
#endif /* !TEST_FLASH_READONLY */
1065-
1066-
/* Compare Page */
1067-
ret = memcmp((void*)TEST_ADDRESS, pageData, sizeof(pageData));
1068-
if (ret != 0) {
1069-
wolfBoot_printf("Check Data @ %d failed\n", ret);
1070-
return ret;
1071-
}
1072-
1073-
wolfBoot_printf("Flash Test Passed\n");
1074-
return ret;
1075-
}
1076-
#endif /* TEST_FLASH */

hal/nrf5340.c

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838
* Any key size greater than 128 bits must be divided and distributed over multiple key slot instances.
3939
*/
4040

41-
#ifdef TEST_FLASH
42-
static int test_flash(void);
43-
#endif
44-
4541
/* Network updates can be signed with "--id 2" and placed into the normal update partition,
4642
* or they can be placed into the external flash at offset 0x100000 */
4743
#ifndef PART_NET_ID
@@ -478,12 +474,7 @@ void hal_init(void)
478474
(SPU_EXTDOMAIN_PERM_SECATTR_SECURE | SPU_EXTDOMAIN_PERM_UNLOCK);
479475
#endif
480476

481-
#ifdef TEST_FLASH
482-
if (test_flash() != 0) {
483-
wolfBoot_printf("Internal flash Test Failed!\n");
484-
}
485-
#endif
486-
477+
/* need early init of external flash to support checking network core */
487478
spi_flash_probe();
488479

489480
hal_net_check_version();
@@ -505,56 +496,4 @@ void hal_prepare_boot(void)
505496
#endif
506497
}
507498

508-
/* Test for internal flash erase/write */
509-
/* Use TEST_EXT_FLASH to test external QSPI flash (see qspi_flash.c) */
510-
#ifdef TEST_FLASH
511-
512-
#ifndef TEST_ADDRESS
513-
#define TEST_SZ (WOLFBOOT_SECTOR_SIZE * 2)
514-
#define TEST_ADDRESS (FLASH_BASE_ADDR + (FLASH_SIZE - TEST_SZ))
515-
#endif
516-
517-
/* #define TEST_FLASH_READONLY */
518-
519-
static int test_flash(void)
520-
{
521-
int ret = 0;
522-
uint32_t i, len;
523-
uint8_t* pagePtr = (uint8_t*)TEST_ADDRESS;
524-
static uint8_t pageData[TEST_SZ];
525-
526-
wolfBoot_printf("Internal flash test at 0x%x\n", TEST_ADDRESS);
527-
528-
/* Setup test data */
529-
for (i=0; i<sizeof(pageData); i++) {
530-
((uint8_t*)pageData)[i] = (i & 0xff);
531-
}
532-
533-
#ifndef TEST_FLASH_READONLY
534-
/* Erase sector */
535-
hal_flash_unlock();
536-
ret = hal_flash_erase(TEST_ADDRESS, sizeof(pageData));
537-
hal_flash_lock();
538-
if (ret != 0) {
539-
wolfBoot_printf("Erase Sector failed: Ret %d\n", ret);
540-
return ret;
541-
}
542-
543-
/* Write Page */
544-
ret = hal_flash_write(TEST_ADDRESS, (uint8_t*)pageData, sizeof(pageData));
545-
wolfBoot_printf("Write Page: Ret %d\n", ret);
546-
#endif /* !TEST_FLASH_READONLY */
547-
548-
/* Compare Page */
549-
ret = memcmp((void*)TEST_ADDRESS, pageData, sizeof(pageData));
550-
if (ret != 0) {
551-
wolfBoot_printf("Check Data @ %d failed\n", ret);
552-
return ret;
553-
}
554-
555-
wolfBoot_printf("Internal Flash Test Passed\n");
556-
return ret;
557-
}
558-
#endif /* TEST_FLASH */
559-
560499
#endif /* TARGET_* */

hal/nxp_p1021.c

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
/* Tests */
3838
#if 0
3939
#define TEST_DDR
40-
#define TEST_FLASH
4140
#define TEST_TPM
4241
#endif
4342
#define ENABLE_PCIE
@@ -56,9 +55,6 @@
5655
#if defined(ENABLE_DDR) && defined(TEST_DDR)
5756
static int test_ddr(void);
5857
#endif
59-
#if defined(ENABLE_ELBC) && defined(TEST_FLASH)
60-
static int test_flash(void);
61-
#endif
6258
#if defined(ENABLE_ESPI) && defined(TEST_TPM)
6359
static int test_tpm(void);
6460
#endif
@@ -1569,12 +1565,6 @@ void hal_init(void)
15691565
}
15701566
#endif
15711567

1572-
#if defined(ENABLE_ELBC) && defined(TEST_FLASH)
1573-
if (test_flash() != 0) {
1574-
wolfBoot_printf("Flash Test Failed!\n");
1575-
}
1576-
#endif
1577-
15781568
#if defined(ENABLE_ESPI) && defined(TEST_TPM)
15791569
if (test_tpm() != 0) {
15801570
wolfBoot_printf("TPM Test Failed!\n");
@@ -1895,52 +1885,6 @@ static int test_ddr(void)
18951885
}
18961886
#endif /* ENABLE_DDR && TEST_DDR */
18971887

1898-
#if defined(ENABLE_ELBC) && defined(TEST_FLASH)
1899-
1900-
#ifndef TEST_ADDRESS
1901-
#define TEST_ADDRESS 0x2800000 /* 40MB */
1902-
#endif
1903-
/* #define TEST_FLASH_READONLY */
1904-
1905-
static int test_flash(void)
1906-
{
1907-
int ret;
1908-
uint32_t i;
1909-
uint32_t pageData[WOLFBOOT_SECTOR_SIZE/4]; /* force 32-bit alignment */
1910-
1911-
#ifndef TEST_FLASH_READONLY
1912-
/* Erase sector */
1913-
ret = ext_flash_erase(TEST_ADDRESS, WOLFBOOT_SECTOR_SIZE);
1914-
wolfBoot_printf("Erase Sector: Ret %d\n", ret);
1915-
1916-
/* Write Pages */
1917-
for (i=0; i<sizeof(pageData); i++) {
1918-
((uint8_t*)pageData)[i] = (i & 0xff);
1919-
}
1920-
ret = ext_flash_write(TEST_ADDRESS, (uint8_t*)pageData, sizeof(pageData));
1921-
wolfBoot_printf("Write Page: Ret %d\n", ret);
1922-
#endif /* !TEST_FLASH_READONLY */
1923-
1924-
/* Read page */
1925-
memset(pageData, 0, sizeof(pageData));
1926-
ret = ext_flash_read(TEST_ADDRESS, (uint8_t*)pageData, sizeof(pageData));
1927-
wolfBoot_printf("Read Page: Ret %d\n", ret);
1928-
1929-
wolfBoot_printf("Checking...\n");
1930-
/* Check data */
1931-
for (i=0; i<sizeof(pageData); i++) {
1932-
wolfBoot_printf("check[%3d] %02x\n", i, pageData[i]);
1933-
if (((uint8_t*)pageData)[i] != (i & 0xff)) {
1934-
wolfBoot_printf("Check Data @ %d failed\n", i);
1935-
return -i;
1936-
}
1937-
}
1938-
1939-
wolfBoot_printf("Flash Test Passed\n");
1940-
return ret;
1941-
}
1942-
#endif /* ENABLE_ELBC && TEST_FLASH */
1943-
19441888
#if defined(ENABLE_ESPI) && defined(TEST_TPM)
19451889
int test_tpm(void)
19461890
{

0 commit comments

Comments
 (0)