-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathuart_flash.c
More file actions
182 lines (167 loc) · 4.35 KB
/
Copy pathuart_flash.c
File metadata and controls
182 lines (167 loc) · 4.35 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
/* uart_flash.c
*
* Generic implementation of the read/write/erase
* functionalities, on top of the spi_drv_*.c API.
*
* This interface creates the communication to access an emulated
* non-volatile memory, hosted on a remote machine, through the UART
* interface.
*
*
* Copyright (C) 2026 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
*/
#ifdef UART_FLASH
#include "wolfboot/wolfboot.h"
#include "hal.h"
#include <stdint.h>
#include <string.h>
#define CMD_HDR_WOLF 'W'
#define CMD_HDR_WRITE 0x01
#define CMD_HDR_READ 0x02
#define CMD_HDR_ERASE 0x03
#define CMD_ACK 0x06
#define WAIT_CYCLES 500000
#define ERASE_TIMEOUT 5
#define READ_TIMEOUT 1
int uart_tx(const uint8_t c);
int uart_rx(uint8_t *c);
static int wait_ack(void)
{
volatile int count = 0;
while(++count < WAIT_CYCLES) {
uint8_t c;
if ((uart_rx(&c) == 1) && (c == CMD_ACK))
return 0;
}
return -1;
}
static int uart_rx_timeout(uint8_t *c)
{
volatile int count = 0;
while(++count < (WAIT_CYCLES * READ_TIMEOUT)) {
if (uart_rx(c) == 1) /* Success */
return 0;
}
*c = 0x00;
return -1;
}
int ext_flash_write(uintptr_t address, const uint8_t *data, int len)
{
int i;
uint8_t cmd[10];
cmd[0] = CMD_HDR_WOLF;
cmd[1] = CMD_HDR_WRITE;
cmd[2] = address & 0xFF;
cmd[3] = (address >> 8) & 0xFF;
cmd[4] = (address >> 16) & 0xFF;
cmd[5] = (address >> 24) & 0xFF;
cmd[6] = len & 0xFF;
cmd[7] = (len >> 8) & 0xFF;
cmd[8] = (len >> 16) & 0xFF;
cmd[9] = (len >> 24) & 0xFF;
for (i = 0; i < 10; i++) {
uart_tx(cmd[i]);
if (wait_ack() != 0)
return -1;
}
for (i = 0; i < len; i++) {
uart_tx(data[i]);
if (wait_ack() != 0)
return -1;
}
return len;
}
int ext_flash_read(uintptr_t address, uint8_t *data, int len)
{
int i;
uint8_t cmd[10];
cmd[0] = CMD_HDR_WOLF;
cmd[1] = CMD_HDR_READ;
cmd[2] = address & 0xFF;
cmd[3] = (address >> 8) & 0xFF;
cmd[4] = (address >> 16) & 0xFF;
cmd[5] = (address >> 24) & 0xFF;
cmd[6] = len & 0xFF;
cmd[7] = (len >> 8) & 0xFF;
cmd[8] = (len >> 16) & 0xFF;
cmd[9] = (len >> 24) & 0xFF;
for (i = 0; i < 10; i++) {
uart_tx(cmd[i]);
if (wait_ack() != 0)
return -1;
}
for (i = 0; i < len; i++) {
if (uart_rx_timeout(&data[i]) != 0)
return -1;
uart_tx(CMD_ACK);
}
return i;
}
int ext_flash_erase(uintptr_t address, int len)
{
int i;
uint8_t cmd[10];
cmd[0] = CMD_HDR_WOLF;
cmd[1] = CMD_HDR_ERASE;
cmd[2] = address & 0xFF;
cmd[3] = (address >> 8) & 0xFF;
cmd[4] = (address >> 16) & 0xFF;
cmd[5] = (address >> 24) & 0xFF;
cmd[6] = len & 0xFF;
cmd[7] = (len >> 8) & 0xFF;
cmd[8] = (len >> 16) & 0xFF;
cmd[9] = (len >> 24) & 0xFF;
for (i = 0; i < 10; i++) {
uart_tx(cmd[i]);
if (wait_ack() != 0)
return -1;
}
/* Wait for extra ack at the end of Erase */
if (wait_ack() == 0)
return 0;
return -1;
}
void ext_flash_lock(void)
{
wait_ack();
}
void ext_flash_unlock(void)
{
wait_ack();
}
void uart_send_current_version(void)
{
uint32_t version = wolfBoot_current_firmware_version();
uart_tx('V');
if (wait_ack() != 0)
return;
uart_tx(version & 0x000000FF);
if (wait_ack() != 0)
return;
uart_tx((version >> 8) & 0x000000FF);
if (wait_ack() != 0)
return;
uart_tx((version >> 16) & 0x000000FF);
if (wait_ack() != 0)
return;
uart_tx((version >> 24) & 0x000000FF);
if (wait_ack() != 0)
return;
}
#endif /* UART_FLASH */