forked from wolfSSL/wolfBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhal.c
More file actions
282 lines (241 loc) · 8 KB
/
hal.c
File metadata and controls
282 lines (241 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* 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
#define TEST_ADDRESS_BANKA WOLFBOOT_PARTITION_BOOT_ADDRESS
#define TEST_ADDRESS_BANKB WOLFBOOT_PARTITION_UPDATE_ADDRESS
#endif
int hal_flash_test(void)
{
int ret = 0;
uint32_t i;
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 */
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 */
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;
}
#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 bit %d: Ret %d\n", 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;
}
int hal_flash_test_unaligned_src(void)
{
uint32_t src[9];
unsigned int i;
uint8_t *ptr;
int ret;
/* force unaligned pointer */
ptr = (uint8_t*)(uintptr_t)src;
ptr++;
for (i = 0; i < sizeof(src); i++) {
ptr[i] = i & 0xff;
}
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;
}
hal_flash_unlock();
ret = hal_flash_write(TEST_ADDRESS, ptr, sizeof(src) - 1);
hal_flash_lock();
if (ret != 0) {
wolfBoot_printf("writing for unaligned source failed: Ret %d\n", ret);
return -1;
}
if (memcmp(ptr, (uint8_t*)TEST_ADDRESS, sizeof(src) - 1) != 0) {
wolfBoot_printf("unaligned source verification failed\n");
return -1;
}
return 0;
}
#endif /* TEST_FLASH_READONLY */
/* 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;
uint8_t cur_fill = 0xb0;
uint8_t new_fill = 0xf0;
uint8_t fill;
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", *((uint8_t*)TEST_ADDRESS_BANKA), cur_fill);
return -1;
}
if (*((uint8_t*)(TEST_ADDRESS_BANKB)) != new_fill) {
wolfBoot_printf("Bank B data mismatch: %x != %x\n", *((uint8_t*)TEST_ADDRESS_BANKB), 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", *((uint8_t*)TEST_ADDRESS_BANKA), new_fill);
return -1;
}
if (*((uint8_t*)(TEST_ADDRESS_BANKB)) != cur_fill) {
wolfBoot_printf("Bank B data mismatch after swap: %x != %x\n", *((uint8_t*)TEST_ADDRESS_BANKB), cur_fill);
return -1;
}
wolfBoot_printf("DUALBANK_SWAP test passed");
return 0;
}
#endif /* DUALBANK_SWAP */
#endif /* TEST_FLASH */