-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch32v_flipper_flasher.c
390 lines (312 loc) · 11.7 KB
/
ch32v_flipper_flasher.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
MIT License
Copyright (c) 2023 Vojtech Suk (https://github.com/sukvojte)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "ch32v_flipper_flasher.h"
#include <storage/storage.h>
#include "utils.h"
#define TAG "WCH_CFF"
struct WchSwioFlasher_Ch32vFlipperFlasher {
struct {
WchSwioFlasher_CFF_Callback cb;
void* cb_context;
} callbacks;
struct {
WchSwioFlasher_RiscVDebug* debugger;
WchSwioFlasher_WchFlasher* flasher;
} helpers;
struct {
WchSwioFlasher_CFF_ChipInfo chip_info;
WchSwioFlasher_CFF_EraseChip erase_chip;
WchSwioFlasher_CFF_WriteChip write_chip;
} history;
FuriThread* worker_thread;
FuriMessageQueue* event_queue;
};
typedef struct {
enum {
Ev_ChipInfo = 0,
Ev_OpenFile,
Ev_EraseChip,
Ev_WriteChip,
Ev_Stop,
} type;
} Event;
static void fire_event(WchSwioFlasher_Ch32vFlipperFlasher* handle, ViewFlasher_Action type) {
if(handle->callbacks.cb) {
handle->callbacks.cb(handle->callbacks.cb_context, type);
}
}
static WchSwioFlasher_CFF_ResultStatus map_error_status(WchSwioFlasher_Error err) {
switch(err) {
case WchSwioFlasher_Ok:
return VWchSwioFlasher_CFF_Ok;
case WchSwioFlasher_Error_Timeout:
case WchSwioFlasher_Error_SwdResetDetected:
case WchSwioFlasher_Error_SwdParityCheckError:
case WchSwioFlasher_Error_TargetNotKnown:
case WchSwioFlasher_Error_TargetInInvalidState:
case WchSwioFlasher_Error_ProgramNotFinishedYet:
case WchSwioFlasher_Error_ProgramRunError:
case WchSwioFlasher_Error_DirtyRegs: {
return VWchSwioFlasher_CFF_ChipNotConnected;
}
default:
return VWchSwioFlasher_CFF_UnknownError;
}
}
static void handle_chip_info(WchSwioFlasher_Ch32vFlipperFlasher* handle) {
UNUSED(handle);
FURI_LOG_D(TAG, "handle_chip_info");
WchSwioFlasher_CFF_ChipInfo* ci = &handle->history.chip_info;
WchSwioFlasher_RiscVDebug_ChipInfo info = {
.esig_uniid = {0, 0, 0},
.flash_size = 0,
};
WchSwioFlasher_Error err =
WchSwioFlasher_RiscVDebug_get_chip_info(handle->helpers.debugger, &info);
if(err != WchSwioFlasher_Ok) {
LOG_ERR(err);
}
ci->status = map_error_status(err);
ci->flash_size = info.flash_size;
memcpy(ci->esig_uniid, info.esig_uniid, sizeof(ci->esig_uniid));
fire_event(handle, WchSwioFlasher_CFF_ChipInfoCompleted);
}
static void handle_erase_chip(WchSwioFlasher_Ch32vFlipperFlasher* handle) {
UNUSED(handle);
FURI_LOG_D(TAG, "handle_erase_chip");
WchSwioFlasher_CFF_EraseChip* ec = &handle->history.erase_chip;
WchSwioFlasher_Error err = WchSwioFlasher_WchFlasher_wipe_chip(handle->helpers.flasher);
if(err != WchSwioFlasher_Ok) {
LOG_ERR(err);
}
ec->status = map_error_status(err);
fire_event(handle, WchSwioFlasher_CFF_EraseChipCompleted);
}
static void handle_write_chip(WchSwioFlasher_Ch32vFlipperFlasher* handle) {
WchSwioFlasher_CFF_WriteChip* wc = &handle->history.write_chip;
FURI_LOG_D(TAG, "handle_write_chip");
WchSwioFlasher_Error err;
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);
// Open file
FURI_LOG_D(TAG, "Open bin file to write");
if(!storage_file_open(file, handle->history.write_chip.path, FSAM_READ, FSOM_OPEN_EXISTING)) {
FURI_LOG_E(TAG, "Failed to open bin file");
wc->status = WchSwioFlasher_CFF_UnableToOpenFile;
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return;
}
// Get file size
FURI_LOG_D(TAG, "Get file size");
uint64_t file_size = 0;
if(storage_file_seek(file, 32 * 1024, false)) { // TODO: proper seek to end
file_size = storage_file_tell(file);
storage_file_seek(file, 0, true);
}
FURI_LOG_D(TAG, "File size is %llu", file_size);
// Erase chip
FURI_LOG_D(TAG, "Wipe chip");
err = WchSwioFlasher_WchFlasher_wipe_chip(handle->helpers.flasher);
if(err != WchSwioFlasher_Ok) {
LOG_ERR(err);
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
wc->status = map_error_status(err);
fire_event(handle, WchSwioFlasher_CFF_WriteChipCompleted);
return;
}
wc->percent = 0.1;
// Load data
FURI_LOG_D(TAG, "Flash data");
if(file_size > 32 * 1024 || file_size == 0) {
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
wc->status = WchSwioFlasher_CFF_EmptyOrTooBigFile;
fire_event(handle, WchSwioFlasher_CFF_WriteChipCompleted);
return;
}
char* data = malloc(file_size);
uint32_t address = 0;
uint32_t to_write = 0;
// Write page
to_write = storage_file_read(file, data, file_size);
if(to_write == file_size) {
FURI_LOG_D(TAG, "Write %lu on " FMT_4HEX, to_write, _UI(address));
err = WchSwioFlasher_WchFlasher_write_flash(
handle->helpers.flasher, address, data, to_write);
address += to_write;
if(err != WchSwioFlasher_Ok) {
LOG_ERR(err);
}
}
FURI_LOG_D(TAG, "Writed %lu of %llu", address, file_size);
wc->percent = 0.9;
free(data);
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
if(err != WchSwioFlasher_Ok) {
LOG_ERR(err);
wc->status = map_error_status(err);
fire_event(handle, WchSwioFlasher_CFF_WriteChipCompleted);
return;
}
// Finally start chip
FURI_LOG_D(TAG, "Reset to run");
err = WchSwioFlasher_RiscVDebug_reset(
handle->helpers.debugger, WchSwioFlasher_RVD_ResetToRunNoCheck);
wc->status = map_error_status(err);
wc->percent = 1;
fire_event(handle, WchSwioFlasher_CFF_WriteChipCompleted);
}
static int32_t cff_create_thread_callback(void* context) {
furi_assert(context);
WchSwioFlasher_Ch32vFlipperFlasher* handle = context;
for(;;) {
Event event;
FuriStatus status = furi_message_queue_get(handle->event_queue, &event, FuriWaitForever);
if(status != FuriStatusOk) {
FURI_LOG_E(TAG, "Message queue get error: %d", status);
furi_delay_ms(100);
continue;
}
if(event.type == Ev_Stop) {
break;
}
switch(event.type) {
case Ev_ChipInfo:
handle_chip_info(handle);
break;
case Ev_EraseChip:
handle_erase_chip(handle);
break;
case Ev_WriteChip:
handle_write_chip(handle);
break;
default:
break;
}
}
return 0;
}
WchSwioFlasher_Ch32vFlipperFlasher* WchSwioFlasher_Ch32vFlipperFlasher_create(
WchSwioFlasher_RiscVDebug* debugger,
WchSwioFlasher_WchFlasher* flasher) {
WchSwioFlasher_Ch32vFlipperFlasher* handle =
malloc(sizeof(WchSwioFlasher_Ch32vFlipperFlasher));
handle->helpers.debugger = debugger;
handle->helpers.flasher = flasher;
handle->event_queue = furi_message_queue_alloc(10, sizeof(Event));
handle->worker_thread =
furi_thread_alloc_ex("MusicWorker", 1024, cff_create_thread_callback, handle);
return handle;
}
static void send_stop(WchSwioFlasher_Ch32vFlipperFlasher* handle) {
Event event = {
.type = Ev_Stop,
};
FuriStatus result = furi_message_queue_put(handle->event_queue, &event, 0);
if(result != FuriStatusOk) {
FURI_LOG_E(TAG, "Message queue put error: %d", result);
}
}
void WchSwioFlasher_Ch32vFlipperFlasher_detach(WchSwioFlasher_Ch32vFlipperFlasher* handle) {
if(furi_thread_get_state(handle->worker_thread) != FuriThreadStateStopped) {
send_stop(handle);
furi_thread_join(handle->worker_thread);
}
if(handle->history.write_chip.path != NULL) {
free(handle->history.write_chip.path);
handle->history.write_chip.path = NULL;
}
}
void WchSwioFlasher_Ch32vFlipperFlasher_attach(WchSwioFlasher_Ch32vFlipperFlasher* handle) {
// Just for sure
WchSwioFlasher_Ch32vFlipperFlasher_detach(handle);
handle->history.chip_info.status = VWchSwioFlasher_CFF_NoData;
furi_thread_start(handle->worker_thread);
}
void WchSwioFlasher_Ch32vFlipperFlasher_destroy(WchSwioFlasher_Ch32vFlipperFlasher* handle) {
WchSwioFlasher_Ch32vFlipperFlasher_detach(handle);
furi_thread_free(handle->worker_thread);
furi_message_queue_free(handle->event_queue);
free(handle);
}
void WchSwioFlasher_Ch32vFlipperFlasher_event_callback(
WchSwioFlasher_Ch32vFlipperFlasher* handle,
WchSwioFlasher_CFF_Callback cb,
void* cb_context) {
handle->callbacks.cb = cb;
handle->callbacks.cb_context = cb_context;
}
void WchSwioFlasher_Ch32vFlipperFlasher_chip_info_data(
WchSwioFlasher_Ch32vFlipperFlasher* handle,
WchSwioFlasher_CFF_ChipInfo* data) {
memcpy(data, &handle->history.chip_info, sizeof(WchSwioFlasher_CFF_ChipInfo));
}
void WchSwioFlasher_Ch32vFlipperFlasher_chip_info(WchSwioFlasher_Ch32vFlipperFlasher* handle) {
Event event = {
.type = Ev_ChipInfo,
};
FuriStatus result = furi_message_queue_put(handle->event_queue, &event, 0);
if(result != FuriStatusOk) {
FURI_LOG_E(TAG, "Message queue put error: %d", result);
}
}
void WchSwioFlasher_Ch32vFlipperFlasher_erase_chip_data(
WchSwioFlasher_Ch32vFlipperFlasher* handle,
WchSwioFlasher_CFF_EraseChip* data) {
memcpy(data, &handle->history.erase_chip, sizeof(WchSwioFlasher_CFF_EraseChip));
}
void WchSwioFlasher_Ch32vFlipperFlasher_erase_chip(WchSwioFlasher_Ch32vFlipperFlasher* handle) {
Event event = {
.type = Ev_EraseChip,
};
FuriStatus result = furi_message_queue_put(handle->event_queue, &event, 0);
if(result != FuriStatusOk) {
FURI_LOG_E(TAG, "Message queue put error: %d", result);
}
}
void WchSwioFlasher_Ch32vFlipperFlasher_write_chip(
WchSwioFlasher_Ch32vFlipperFlasher* handle,
char* path) {
Event event = {
.type = Ev_WriteChip,
};
if(handle->history.write_chip.path != NULL) {
free(handle->history.write_chip.path);
handle->history.write_chip.path = NULL;
}
uint32_t len = strlen(path);
handle->history.write_chip.path = malloc(len + 1);
strcpy(handle->history.write_chip.path, path);
FuriStatus result = furi_message_queue_put(handle->event_queue, &event, 0);
if(result != FuriStatusOk) {
FURI_LOG_E(TAG, "Message queue put error: %d", result);
}
}
void WchSwioFlasher_Ch32vFlipperFlasher_write_chip_data(
WchSwioFlasher_Ch32vFlipperFlasher* handle,
WchSwioFlasher_CFF_WriteChip* data) {
memcpy(data, &handle->history.write_chip, sizeof(WchSwioFlasher_CFF_WriteChip));
}