Select one of the symbols to view example projects that use it.
 
Outline
#include "config.h"
#include <helper/binarybuffer.h>
#include <target/algorithm.h>
#include <target/armv7m.h>
#include <target/breakpoints.h>
#include <target/image.h>
#include "advanced_elf_image.h"
#include "nor/imp.h"
WorkAreaInfo
plugin_flash_bank
memory_backup
plugin_timeouts
image_plugin_timeouts
loaded_plugin
save_region(struct target *, struct memory_backup *, Elf32_Addr, Elf32_Size, int)
plugin_make_return_addr(uint32_t)
loaded_plugin_load(struct target *, struct advanced_elf_image *, struct loaded_plugin *, uint32_t, unsigned int)
loaded_plugin_unload(struct loaded_plugin *, uint32_t)
loaded_plugin_backup_workarea(struct loaded_plugin *, uint32_t, uint32_t)
locate_symbol(struct advanced_elf_image *, const char *, uint32_t *, const char *)
plugin_flash_bank_command(struct command_invocation *, struct flash_bank *)
plugin_write_sync(struct target *, struct plugin_flash_bank *, struct loaded_plugin *, uint32_t, const uint8_t *, uint32_t)
plugin_write_async(struct target *, struct plugin_flash_bank *, struct loaded_plugin *, uint32_t, const uint8_t *, uint32_t)
plugin_write(struct flash_bank *, const uint8_t *, uint32_t, uint32_t)
call_plugin_func(struct target *, int, uint32_t, uint32_t, int32_t *, int, ...)
FLASHBankInfo
plugin_probe(struct flash_bank *)
plugin_auto_probe(struct flash_bank *)
get_plugin_info(struct flash_bank *, struct command_invocation *)
plugin_erase(struct flash_bank *, unsigned int, unsigned int)
plugin_protect(struct flash_bank *, int, unsigned int, unsigned int)
plugin_protect_check(struct flash_bank *)
plugin_flash
Files
loading...
SourceVuDevelopment ToolsOpenOCDsrc/flash/FLASHPlugin.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/*************************************************************************** * Copyright (C) 2016 by Sysprogs * * sysprogs@sysprogs.com * * * * This program 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 2 of the License, or * * (at your option) any later version. * * * * This program 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-1301 USA. * ***************************************************************************//* ... */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include <helper/binarybuffer.h> #include <target/algorithm.h> #include <target/armv7m.h> #include <target/breakpoints.h> #include <target/image.h> #include "advanced_elf_image.h" #include "nor/imp.h" 7 includes struct WorkAreaInfo { uint32_t Address; uint32_t Size; ...}; struct plugin_flash_bank { int probed; struct advanced_elf_image image; uint32_t FLASHPlugin_InitDone; uint32_t FLASHPlugin_Probe; uint32_t FLASHPlugin_FindWorkArea; uint32_t FLASHPlugin_EraseSectors; uint32_t FLASHPlugin_Unload; uint32_t FLASHPlugin_ProgramSync; uint32_t FLASHPlugin_ProgramAsync; uint32_t FLASHPlugin_NotImplemented; //Optional entries uint32_t FLASHPlugin_ProtectSectors; uint32_t FLASHPlugin_CheckSectorProtection; struct WorkAreaInfo WorkArea; unsigned int stack_size; unsigned int write_block_size; char *plugin_file; ...}; static int call_plugin_func(struct target *target, int timeout, uint32_t function, uint32_t sp, int32_t *result, int argc, ...); struct memory_backup { long long base_address; int original_section; unsigned size; void *original_contents; ...}; struct plugin_timeouts { unsigned erase; unsigned write; unsigned init; unsigned load; unsigned protect; ...}; struct image_plugin_timeouts { unsigned size; struct plugin_timeouts timeouts; ...}; struct loaded_plugin { int region_count; uint32_t sp; long long entry; struct memory_backup *regions; struct target *target; uint32_t usable_stack_pointer; struct memory_backup work_area_backup; struct plugin_timeouts timeouts; ...}; int save_region(struct target *target, struct memory_backup *region, Elf32_Addr sh_addr, Elf32_Size sh_size, int sectionNumber) { region->base_address = sh_addr; region->size = sh_size; region->original_contents = malloc(sh_size); region->original_section = sectionNumber; return target_read_memory(target, region->base_address, 4, (region->size + 3) / 4, (uint8_t *)region->original_contents); }{ ... } static uint32_t plugin_make_return_addr(uint32_t addr) { return addr | 1; //ARM-specific: Force THUMB mode }{ ... } static int loaded_plugin_load(struct target *target, struct advanced_elf_image *image, struct loaded_plugin *plugin, uint32_t init_done_address, unsigned stackSize) { int retval; memset(plugin, 0, sizeof(*plugin)); plugin->region_count = 0; plugin->regions = (struct memory_backup *)calloc(image->num_sections + 1, sizeof(struct memory_backup)); plugin->target = target; plugin->entry = image->header.e_entry; plugin->timeouts.erase = 60000; plugin->timeouts.write = 1000; plugin->timeouts.init = 1000; plugin->timeouts.load = 10000; plugin->timeouts.protect = 1000; unsigned lastSectionEnd = 0; uint32_t timeoutTable = advanced_elf_image_find_symbol(image, "FLASHPlugin_TimeoutTable"); unsigned maxSize = 0; for (int i = 0; i < image->num_sections; i++) { if (!(image->sections[i].sh_flags & 2 /*SHF_ALLOC*/)) continue; struct memory_backup *region = &plugin->regions[plugin->region_count]; retval = save_region(target, region, image->sections[i].sh_addr, image->sections[i].sh_size, i); if (retval != ERROR_OK) break; if (region->size > maxSize) maxSize = region->size; plugin->region_count++; if ((region->base_address + region->size) > lastSectionEnd) lastSectionEnd = region->base_address + region->size; }for (int i = 0; i < image->num_sections; i++) { ... } lastSectionEnd = ((lastSectionEnd + 15) & ~15); plugin->sp = lastSectionEnd + stackSize; LOG_DEBUG("FLASH plugin: placing the stack at 0x%08x-0x%08x", lastSectionEnd, lastSectionEnd + stackSize); save_region(target, &plugin->regions[plugin->region_count], lastSectionEnd, stackSize, -1); plugin->region_count++; if (retval == ERROR_OK) { void *pBuf = malloc(maxSize); for (int i = 0; i < plugin->region_count; i++) { if (plugin->regions[i].original_section < 0 || image->sections[plugin->regions[i].original_section].sh_type == 8 /* NOBITS */) continue; size_t done; retval = advanced_elf_image_read_section(image, plugin->regions[i].original_section, (uint8_t *)pBuf, plugin->regions[i].size, &done); if (retval != ERROR_OK) break; if (done != plugin->regions[i].size) { LOG_ERROR("Failed to read FLASH plugin contents\n"); retval = ERROR_FILEIO_OPERATION_FAILED; }if (done != plugin->regions[i].size) { ... } if (timeoutTable && timeoutTable >= plugin->regions[i].base_address && timeoutTable < (plugin->regions[i].base_address + plugin->regions[i].size)) { void *timeouts_from_image = ((char *)pBuf + (timeoutTable - plugin->regions[i].base_address)); uint32_t timeout_struct_size = 0; memcpy(&timeout_struct_size, timeouts_from_image, sizeof(timeout_struct_size)); if (timeout_struct_size > sizeof(struct plugin_timeouts)) LOG_WARNING("Invalid size of timeouts structure: %d\n", timeout_struct_size); else memcpy(&plugin->timeouts, &((struct image_plugin_timeouts *)timeouts_from_image)->timeouts, timeout_struct_size); }if (timeoutTable && timeoutTable >= plugin->regions[i].base_address && timeoutTable < (plugin->regions[i].base_address + plugin->regions[i].size)) { ... } retval = target_write_memory(target, plugin->regions[i].base_address, 4, (plugin->regions[i].size + 3) / 4, (uint8_t *)pBuf); if (retval != ERROR_OK) break; }for (int i = 0; i < plugin->region_count; i++) { ... } free(pBuf); }if (retval == ERROR_OK) { ... } if (retval == ERROR_OK && init_done_address) { struct reg_param reg_params[1]; init_reg_param(&reg_params[0], "sp", 32, PARAM_IN); buf_set_u32(reg_params[0].value, 0, 32, plugin->sp); init_done_address &= ~1; struct armv7m_algorithm armv7m_info; armv7m_info.common_magic = ARMV7M_COMMON_MAGIC; armv7m_info.core_mode = ARM_MODE_THREAD; int bp = breakpoint_add(target, init_done_address, 2, BKPT_SOFT); (void)bp; retval = target_run_algorithm(target, 0, NULL, sizeof(reg_params) / sizeof(reg_params[0]), reg_params, plugin->entry, init_done_address, plugin->timeouts.load, &armv7m_info); breakpoint_remove(target, init_done_address); if (retval != ERROR_OK) LOG_ERROR("FLASH plugin did not call FLASHPlugin_InitDone(). Ensure it is declared as non-inline and try increasing load timeout setting inside the plugin timeout table."); destroy_reg_param(&reg_params[0]); }if (retval == ERROR_OK && init_done_address) { ... } return retval; }{ ... } static int loaded_plugin_unload(struct loaded_plugin *plugin, uint32_t unload_func) { int retval = ERROR_OK; if (!plugin->target || !plugin->regions) return ERROR_OK; if (unload_func) { int32_t result; retval = call_plugin_func(plugin->target, plugin->timeouts.init, unload_func, plugin->sp, &result, 0); if (retval == ERROR_OK && result != 0) { LOG_ERROR("Plugin's Unload() function returned error %d\n", result); retval = ERROR_FLASH_BANK_INVALID; }if (retval == ERROR_OK && result != 0) { ... } }if (unload_func) { ... } for (int i = 0; i < plugin->region_count; i++) { if (!plugin->regions[i].original_contents) continue; int r = target_write_memory(plugin->target, plugin->regions[i].base_address, 4, (plugin->regions[i].size + 3) / 4, (uint8_t *)plugin->regions[i].original_contents); free(plugin->regions[i].original_contents); plugin->regions[i].original_contents = NULL; if (r != ERROR_OK) retval = r; }for (int i = 0; i < plugin->region_count; i++) { ... } if (plugin->work_area_backup.size && plugin->work_area_backup.original_contents) { int r = target_write_memory(plugin->target, plugin->work_area_backup.base_address, 4, plugin->work_area_backup.size / 4, plugin->work_area_backup.original_contents); free(plugin->work_area_backup.original_contents); if (r != ERROR_OK) retval = r; }if (plugin->work_area_backup.size && plugin->work_area_backup.original_contents) { ... } free(plugin->regions); plugin->regions = NULL; return retval; }{ ... } static int loaded_plugin_backup_workarea(struct loaded_plugin *plugin, uint32_t start, uint32_t size) { if (!plugin) return ERROR_COMMAND_ARGUMENT_INVALID; if (plugin->work_area_backup.size && plugin->work_area_backup.base_address != start) { LOG_ERROR("Inconsistent work area address: expected 0x%x, got 0x%x\n", (uint32_t)plugin->work_area_backup.base_address, start); return ERROR_COMMAND_ARGUMENT_INVALID; }if (plugin->work_area_backup.size && plugin->work_area_backup.base_address != start) { ... } size = (size + 3) & ~3; uint32_t baseoff = plugin->work_area_backup.size; if (size <= baseoff) return ERROR_OK; //nothing to do plugin->work_area_backup.original_contents = realloc(plugin->work_area_backup.original_contents, size); int retval = target_read_memory(plugin->target, start + baseoff, 4, size / 4, ((uint8_t *)plugin->work_area_backup.original_contents) + baseoff); if (retval != ERROR_OK) return retval; plugin->work_area_backup.base_address = start; plugin->work_area_backup.original_section = -1; plugin->work_area_backup.size = size; return ERROR_OK; }{ ... } static int locate_symbol(struct advanced_elf_image *image, const char *URL, uint32_t *ptr, const char *symbol) { *ptr = advanced_elf_image_find_symbol(image, symbol); if (!*ptr) { LOG_ERROR("%s: invalid FLASH plugin. No '%s' symbol found.", URL, symbol); return ERROR_IMAGE_FORMAT_ERROR; }if (!*ptr) { ... } return ERROR_OK; }{ ... } /* flash bank <name> plugin <base> <size> 0 0 <target#> <plugin ELF file> [stack size = 512] *//* ... */ FLASH_BANK_COMMAND_HANDLER(plugin_flash_bank_command) { struct plugin_flash_bank *info; int retval; if (CMD_ARGC < 7) return ERROR_COMMAND_SYNTAX_ERROR; unsigned stackSize = 512; if (CMD_ARGC >= 8) { COMMAND_PARSE_NUMBER(uint, CMD_ARGV[7], stackSize); }if (CMD_ARGC >= 8) { ... } info = malloc(sizeof(struct plugin_flash_bank)); memset(info, 0, sizeof(struct plugin_flash_bank)); const char *URL = CMD_ARGV[6]; retval = advanced_elf_image_open(&info->image, URL); if (retval != ERROR_OK) return retval; retval = locate_symbol(&info->image, URL, &info->FLASHPlugin_InitDone, "FLASHPlugin_InitDone"); if (retval != ERROR_OK) return retval; retval = locate_symbol(&info->image, URL, &info->FLASHPlugin_Probe, "FLASHPlugin_Probe"); if (retval != ERROR_OK) return retval; retval = locate_symbol(&info->image, URL, &info->FLASHPlugin_FindWorkArea, "FLASHPlugin_FindWorkArea"); if (retval != ERROR_OK) return retval; retval = locate_symbol(&info->image, URL, &info->FLASHPlugin_EraseSectors, "FLASHPlugin_EraseSectors"); if (retval != ERROR_OK) return retval; retval = locate_symbol(&info->image, URL, &info->FLASHPlugin_Unload, "FLASHPlugin_Unload"); if (retval != ERROR_OK) return retval; info->FLASHPlugin_ProgramSync = advanced_elf_image_find_symbol(&info->image, "FLASHPlugin_ProgramSync"); info->FLASHPlugin_ProgramAsync = advanced_elf_image_find_symbol(&info->image, "FLASHPlugin_ProgramAsync"); info->FLASHPlugin_NotImplemented = advanced_elf_image_find_symbol(&info->image, "FLASHPlugin_NotImplemented"); if (!info->FLASHPlugin_ProgramAsync && !info->FLASHPlugin_ProgramSync) { LOG_ERROR("%s: invalid FLASH plugin. Neither 'FLASHPlugin_ProgramSync' nor 'FLASHPlugin_ProgramAsync' is defined.", URL); return ERROR_IMAGE_FORMAT_ERROR; }if (!info->FLASHPlugin_ProgramAsync && !info->FLASHPlugin_ProgramSync) { ... } info->FLASHPlugin_CheckSectorProtection = advanced_elf_image_find_symbol(&info->image, "FLASHPlugin_CheckSectorProtection"); info->FLASHPlugin_ProtectSectors = advanced_elf_image_find_symbol(&info->image, "FLASHPlugin_ProtectSectors"); info->stack_size = stackSize; bank->driver_priv = info; info->probed = 0; info->plugin_file = strdup(URL); return ERROR_OK; }{ ... } int plugin_write_sync(struct target *target, struct plugin_flash_bank *plugin_info, struct loaded_plugin *loaded_plugin, uint32_t offset, const uint8_t *buffer, uint32_t size) { uint32_t todo = MIN(size, plugin_info->WorkArea.Size); int retval = loaded_plugin_backup_workarea(loaded_plugin, plugin_info->WorkArea.Address, todo); if (retval != ERROR_OK) return -1; retval = target_write_memory(target, plugin_info->WorkArea.Address, 4, todo / 4, buffer); if (retval != ERROR_OK) return -1; if (todo & 3) { retval = target_write_memory(target, plugin_info->WorkArea.Address + (todo & ~3), 1, todo & 3, buffer + (todo & ~3)); if (retval != ERROR_OK) return -1; }if (todo & 3) { ... } int32_t result = 0; retval = call_plugin_func(target, loaded_plugin->timeouts.write, plugin_info->FLASHPlugin_ProgramSync, loaded_plugin->sp, &result, 3, offset, plugin_info->WorkArea.Address, todo); if (retval != ERROR_OK) return -1; return result; }{ ... } int plugin_write_async(struct target *target, struct plugin_flash_bank *plugin_info, struct loaded_plugin *loaded_plugin, uint32_t offset, const uint8_t *buffer, uint32_t size) { const int fifo_header_size = 8; unsigned workAreaSize = MIN(MAX(size, plugin_info->write_block_size) + plugin_info->write_block_size + fifo_header_size, plugin_info->WorkArea.Size); workAreaSize -= fifo_header_size; workAreaSize = (workAreaSize / plugin_info->write_block_size) * plugin_info->write_block_size; workAreaSize += fifo_header_size; if (workAreaSize > plugin_info->WorkArea.Size) { LOG_ERROR("Computed worka area size (0x%x) is smaller then the available size (0x%x)", workAreaSize, plugin_info->WorkArea.Size); return ERROR_FLASH_BANK_INVALID; }if (workAreaSize > plugin_info->WorkArea.Size) { ... } int retval = loaded_plugin_backup_workarea(loaded_plugin, plugin_info->WorkArea.Address, workAreaSize); if (retval != ERROR_OK) return retval; struct armv7m_algorithm armv7m_info; struct reg_param reg_params[6]; unsigned sp = (loaded_plugin->sp - 4) & ~3; unsigned return_addr = sp; armv7m_info.common_magic = ARMV7M_COMMON_MAGIC; armv7m_info.core_mode = ARM_MODE_THREAD; init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT); init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT); init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT); init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT); init_reg_param(&reg_params[4], "sp", 32, PARAM_OUT); init_reg_param(&reg_params[5], "lr", 32, PARAM_OUT); buf_set_u32(reg_params[0].value, 0, 32, offset); buf_set_u32(reg_params[1].value, 0, 32, plugin_info->WorkArea.Address); buf_set_u32(reg_params[2].value, 0, 32, plugin_info->WorkArea.Address + workAreaSize); buf_set_u32(reg_params[3].value, 0, 32, size); buf_set_u32(reg_params[4].value, 0, 32, sp); buf_set_u32(reg_params[5].value, 0, 32, plugin_make_return_addr(return_addr)); int bp = breakpoint_add(target, return_addr, 2, BKPT_SOFT); (void)bp; retval = target_run_flash_async_algorithm(target, buffer, (size + plugin_info->write_block_size - 1) / plugin_info->write_block_size, plugin_info->write_block_size, 0, NULL, sizeof(reg_params)/sizeof(reg_params[0]), reg_params, plugin_info->WorkArea.Address, workAreaSize, plugin_info->FLASHPlugin_ProgramAsync, return_addr, &armv7m_info); breakpoint_remove(target, return_addr); unsigned result = 0; if (retval == ERROR_OK) { result = buf_get_u32(reg_params[0].value, 0, 32); if (result < size) { LOG_ERROR("FLASHPlugin_ProgramAsync returned %d\n", result); retval = ERROR_FLASH_BANK_INVALID; }if (result < size) { ... } }if (retval == ERROR_OK) { ... } for (int i = 0; i < 6; i++) destroy_reg_param(&reg_params[i]); return retval; }{ ... } static int plugin_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count) { struct target *target = bank->target; struct plugin_flash_bank *plugin_info = bank->driver_priv; struct loaded_plugin loaded_plugin; int retval = loaded_plugin_load(target, &plugin_info->image, &loaded_plugin, plugin_info->FLASHPlugin_InitDone, plugin_info->stack_size); if (retval == ERROR_OK) { if (plugin_info->FLASHPlugin_ProgramAsync && plugin_info->FLASHPlugin_ProgramAsync != plugin_info->FLASHPlugin_NotImplemented) retval = plugin_write_async(target, plugin_info, &loaded_plugin, offset, buffer, count); else if (plugin_info->FLASHPlugin_ProgramSync && plugin_info->FLASHPlugin_ProgramSync != plugin_info->FLASHPlugin_NotImplemented) { uint32_t done = 0; while (done < count) { int doneNow = plugin_write_sync(target, plugin_info, &loaded_plugin, offset + done, buffer + done, count - done); if (doneNow < 0) { retval = ERROR_FLASH_BANK_INVALID; break; }if (doneNow < 0) { ... } if (target->report_flash_progress) report_flash_progress("flash_write_progress_sync", bank->base + offset + done, bank->base + offset + done + doneNow, bank->name); done += doneNow; }while (done < count) { ... } }else if (plugin_info->FLASHPlugin_ProgramSync && plugin_info->FLASHPlugin_ProgramSync != plugin_info->FLASHPlugin_NotImplemented) { ... } else { LOG_ERROR("Neither FLASHPlugin_ProgramAsync() or FLASHPlugin_ProgramSync() are properly defined in the FLASH plugin. Cannot program FLASH memory."); retval = ERROR_FLASH_BANK_INVALID; }else { ... } }if (retval == ERROR_OK) { ... } loaded_plugin_unload(&loaded_plugin, plugin_info->FLASHPlugin_Unload); return retval; }{ ... } static int call_plugin_func(struct target *target, int timeout, uint32_t function, uint32_t sp, int32_t *result, int argc, ...) { sp = (sp - 4) & ~3; const int r0ParamIndex = 2; char *arg_reg_names[] = { "r0", "r1", "r2", "r3" }; uint32_t return_addr = sp; struct reg_param reg_params[3 + 4]; init_reg_param(&reg_params[0], "sp", 32, PARAM_IN_OUT); init_reg_param(&reg_params[1], "lr", 32, PARAM_IN_OUT); //ARM-specific! init_reg_param(&reg_params[r0ParamIndex], arg_reg_names[0], 32, PARAM_IN_OUT); //ARM-specific! buf_set_u32(reg_params[1].value, 0, 32, plugin_make_return_addr(sp)); //Forced thumb mode! int reg_param_count = r0ParamIndex; sp -= 4; va_list ap; va_start(ap, argc); for (int arg = 0; arg < argc; arg++) { uint32_t argVal = va_arg(ap, uint32_t); if ((unsigned)arg >= (sizeof(arg_reg_names) / sizeof(arg_reg_names[0]))) { sp -= 4; target_write_memory(target, sp, 4, 1, (uint8_t *)&argVal); }if ((unsigned)arg >= (sizeof(arg_reg_names) / sizeof(arg_reg_names[0]))) { ... } else { if (arg == 0) reg_params[r0ParamIndex].direction = PARAM_IN_OUT; else { init_reg_param(&reg_params[r0ParamIndex + arg], arg_reg_names[arg], 32, PARAM_IN_OUT); reg_param_count = r0ParamIndex + arg + 1; }else { ... } buf_set_u32(reg_params[r0ParamIndex + arg].value, 0, 32, argVal); }else { ... } }for (int arg = 0; arg < argc; arg++) { ... } va_end(ap); if (argc == 0) reg_param_count = r0ParamIndex + 1; buf_set_u32(reg_params[0].value, 0, 32, sp); int bp = breakpoint_add(target, return_addr, 2, BKPT_SOFT); (void)bp; struct armv7m_algorithm armv7m_info; armv7m_info.common_magic = ARMV7M_COMMON_MAGIC; armv7m_info.core_mode = ARM_MODE_THREAD; int retval = target_run_algorithm(target, 0, NULL, reg_param_count, reg_params, function, return_addr, timeout, &armv7m_info); breakpoint_remove(target, return_addr); if (retval == ERROR_OK && result) *result = (int32_t)buf_get_u32(reg_params[r0ParamIndex].value, 0, 32); for (int i = 0; i < reg_param_count; i++) destroy_reg_param(&reg_params[i]); return retval; }{ ... } struct FLASHBankInfo { unsigned BaseAddress; unsigned BlockCount; unsigned BlockSize; unsigned WriteBlockSize; ...}; static int plugin_probe(struct flash_bank *bank) { struct target *target = bank->target; struct plugin_flash_bank *plugin_info = bank->driver_priv; struct FLASHBankInfo bankInfo; struct loaded_plugin loaded_plugin; plugin_info->probed = 1; int retval = loaded_plugin_load(target, &plugin_info->image, &loaded_plugin, plugin_info->FLASHPlugin_InitDone, plugin_info->stack_size); if (retval == ERROR_OK) { int32_t result; uint32_t sp = (loaded_plugin.sp - 4) & ~3; sp -= sizeof(struct FLASHBankInfo); retval = call_plugin_func(target, loaded_plugin.timeouts.init, plugin_info->FLASHPlugin_Probe, sp, &result, 5, sp, (uint32_t)bank->base, (uint32_t)bank->size, (uint32_t)bank->chip_width, (uint32_t)bank->bus_width); if (retval == ERROR_OK) { retval = target_read_memory(target, sp, 4, sizeof(bankInfo) / 4, (uint8_t *)&bankInfo); }if (retval == ERROR_OK) { ... } }if (retval == ERROR_OK) { ... } if (plugin_info->FLASHPlugin_FindWorkArea && retval == ERROR_OK) { int32_t result; uint32_t sp = (loaded_plugin.sp - 4) & ~3; sp -= sizeof(struct WorkAreaInfo); retval = call_plugin_func(target, loaded_plugin.timeouts.init, plugin_info->FLASHPlugin_FindWorkArea, sp, &result, (uint32_t)2, sp, loaded_plugin.sp); if (retval == ERROR_OK) { retval = target_read_memory(target, sp, 4, sizeof(plugin_info->WorkArea) / 4, (uint8_t *)&plugin_info->WorkArea); }if (retval == ERROR_OK) { ... } }if (plugin_info->FLASHPlugin_FindWorkArea && retval == ERROR_OK) { ... } if (retval == ERROR_OK) { bank->base = bankInfo.BaseAddress; bank->size = bankInfo.BlockCount * bankInfo.BlockSize; bank->num_sectors = bankInfo.BlockCount; bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors); memset(bank->sectors, 0, sizeof(struct flash_sector) * bank->num_sectors); plugin_info->write_block_size = bankInfo.WriteBlockSize; if (!plugin_info->write_block_size) { LOG_ERROR("FLASH plugin returned invalid WriteBlockSize. Writing external FLASH will not be possible."); retval = ERROR_FLASH_BANK_INVALID; }if (!plugin_info->write_block_size) { ... } for (unsigned i = 0; i < bank->num_sectors; i++) { bank->sectors[i].offset = i * bankInfo.BlockSize; bank->sectors[i].size = bankInfo.BlockSize; }for (unsigned i = 0; i < bank->num_sectors; i++) { ... } }if (retval == ERROR_OK) { ... } loaded_plugin_unload(&loaded_plugin, plugin_info->FLASHPlugin_Unload); return retval; }{ ... } static int plugin_auto_probe(struct flash_bank *bank) { struct plugin_flash_bank *plugin_info = bank->driver_priv; if (plugin_info->probed) return ERROR_OK; return plugin_probe(bank); }{ ... } static int get_plugin_info(struct flash_bank *bank, struct command_invocation *cmd) { struct plugin_flash_bank *plugin_info = bank->driver_priv; command_print(cmd, "Plugin-managed FLASH\r\nPlugin file: %s", plugin_info->plugin_file); return ERROR_OK; }{ ... } static int plugin_erase(struct flash_bank *bank, unsigned first, unsigned last) { struct target *target = bank->target; struct plugin_flash_bank *plugin_info = bank->driver_priv; struct loaded_plugin loaded_plugin; int retval = loaded_plugin_load(target, &plugin_info->image, &loaded_plugin, plugin_info->FLASHPlugin_InitDone, plugin_info->stack_size); if (retval == ERROR_OK) { while (first <= last) { int32_t result; retval = call_plugin_func(target, loaded_plugin.timeouts.erase, plugin_info->FLASHPlugin_EraseSectors, loaded_plugin.sp, &result, 2, (uint32_t)first, (uint32_t)last - (uint32_t)first + 1); if (retval != ERROR_OK) break; if (result < 0) { LOG_ERROR("FLASHPlugin_EraseSectors() returned %d", result); retval = ERROR_FLASH_BANK_INVALID; }if (result < 0) { ... } if (target->report_flash_progress) report_flash_progress("flash_erase_progress", bank->base + bank->sectors[first].offset, bank->base + bank->sectors[first + result - 1].offset + bank->sectors[first + result - 1].size, bank->name); first += result; }while (first <= last) { ... } }if (retval == ERROR_OK) { ... } loaded_plugin_unload(&loaded_plugin, plugin_info->FLASHPlugin_Unload); return retval; }{ ... } static int plugin_protect(struct flash_bank *bank, int set, unsigned first, unsigned last) { struct target *target = bank->target; struct plugin_flash_bank *plugin_info = bank->driver_priv; struct loaded_plugin loaded_plugin; if (!plugin_info->FLASHPlugin_ProtectSectors || plugin_info->FLASHPlugin_ProtectSectors == plugin_info->FLASHPlugin_NotImplemented) return ERROR_OK; //Nothing to do int retval = loaded_plugin_load(target, &plugin_info->image, &loaded_plugin, plugin_info->FLASHPlugin_InitDone, plugin_info->stack_size); if (retval == ERROR_OK) { while (first <= last) { int32_t result; retval = call_plugin_func(target, loaded_plugin.timeouts.protect, plugin_info->FLASHPlugin_ProtectSectors, loaded_plugin.sp, &result, 3, (uint32_t)set, (uint32_t)first, (uint32_t)last - (uint32_t)first + 1); if (retval != ERROR_OK) break; if (result < 0) { LOG_ERROR("FLASHPlugin_ProtectSectors() returned %d", result); retval = ERROR_FLASH_BANK_INVALID; }if (result < 0) { ... } first += result; }while (first <= last) { ... } }if (retval == ERROR_OK) { ... } loaded_plugin_unload(&loaded_plugin, plugin_info->FLASHPlugin_Unload); return retval; }{ ... } static int plugin_protect_check(struct flash_bank *bank) { struct target *target = bank->target; struct plugin_flash_bank *plugin_info = bank->driver_priv; struct loaded_plugin loaded_plugin; if (!plugin_info->FLASHPlugin_CheckSectorProtection || plugin_info->FLASHPlugin_CheckSectorProtection == plugin_info->FLASHPlugin_NotImplemented) return ERROR_OK; //Nothing to do unsigned workAreaSize = MIN((bank->num_sectors + 7) / 8, plugin_info->WorkArea.Size); uint8_t *pBuf = malloc(workAreaSize); int retval = loaded_plugin_load(target, &plugin_info->image, &loaded_plugin, plugin_info->FLASHPlugin_InitDone, plugin_info->stack_size); if (retval == ERROR_OK) { retval = loaded_plugin_backup_workarea(&loaded_plugin, plugin_info->WorkArea.Address, workAreaSize); if (retval == ERROR_OK) { for (unsigned sector = 0; sector < bank->num_sectors;) { int32_t sectors_to_check = MIN(workAreaSize * 8, bank->num_sectors - sector); int32_t result; retval = call_plugin_func(target, loaded_plugin.timeouts.protect, plugin_info->FLASHPlugin_CheckSectorProtection, loaded_plugin.sp, &result, 3, sector, sectors_to_check, plugin_info->WorkArea.Address); if (retval != ERROR_OK) break; if (result < 0 || result > sectors_to_check) { LOG_ERROR("FLASHPlugin_ProtectSectors() returned %d", result); retval = ERROR_FLASH_BANK_INVALID; break; }if (result < 0 || result > sectors_to_check) { ... } retval = target_read_buffer(target, plugin_info->WorkArea.Address,(sectors_to_check + 7) / 8, pBuf); if (retval != ERROR_OK) break; for (int i = 0; i < result; i++) { bank->sectors[sector + i].is_protected = pBuf[i / 8] & (1 << (i % 8)); }for (int i = 0; i < result; i++) { ... } sector += result; }for (unsigned sector = 0; sector < bank->num_sectors;) { ... } }if (retval == ERROR_OK) { ... } }if (retval == ERROR_OK) { ... } free(pBuf); loaded_plugin_unload(&loaded_plugin, plugin_info->FLASHPlugin_Unload); return retval; }{ ... } struct flash_driver plugin_flash = { .name = "plugin", .flash_bank_command = plugin_flash_bank_command, .erase = plugin_erase, .write = plugin_write, .read = default_flash_read, .probe = plugin_probe, .auto_probe = plugin_auto_probe, .erase_check = default_flash_blank_check, .info = get_plugin_info, .protect_check = plugin_protect_check, .protect = plugin_protect ...};
Details
Show:
from
Types: Columns:
Click anywhere in the source to view detailed information here...