1
2
3
7
8
9
10
11
12
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
114
115
118
119
120
126
127
128
133
134
135
140
141
142
148
149
150
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
192
193
197
198
199
200
201
208
209
210
211
212
213
214
215
216
217
218
219
220
227
228
229
230
231
232
233
234
238
239
240
241
242
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
359
360
361
362
363
364
371
372
373
374
375
376
380
381
382
383
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
430
436
437
438
439
440
441
442
448
449
450
454
455
456
457
458
459
460
461
462
463
469
470
471
472
473
474
475
476
477
478
479
480
481
482
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
552
553
554
555
560
561
562
563
564
565
566
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
589
590
591
592
593
594
595
596
599
600
601
602
603
604
605
606
607
608
609
610
611
612
616
617
618
619
620
624
625
626
627
628
629
630
631
632
633
634
635
636
637
639
643
650
651
652
653
654
655
656
657
658
659
660
661
662
667
668
672
673
674
675
676
677
678
683
684
688
689
690
691
692
693
694
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
716
717
718
719
720
721
722
728
729
730
731
732
747
748
749
762
763
764
765
766
767
771
772
773
774
775
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
816
817
818
819
820
821
822
823
824
825
832
833
834
835
836
837
838
839
840
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
884
885
886
887
888
889
890
891
892
893
894
895
896
900
901
902
903
904
905
906
907
912
913
914
915
920
921
922
923
924
925
926
927
928
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
954
955
956
957
958
964
965
966
972
973
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
996
997
998
999
1000
1001
1002
1003
1005
1006
1014
1015
1016
1017
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1121
1122
1123
1124
1125
1127
1128
1129
1130
1131
1133
1134
1135
1136
1137
1138
1139
1140
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1330
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1371
1372
1373
1374
1375
1376
1379
1380
1381
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1409
1410
1411
1412
1413
1414
1415
1416
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1448
1449
1450
1451
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1470
1471
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1495
1496
1497
1498
1499
1500
1501
1502
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1526
1527
1530
1531
1532
1533
1534
1535
1536
1537
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include <helper/binarybuffer.h>
#include <target/algorithm.h>
#include <target/arm.h>
#include <target/image.h>
5 includes
#define KiB 1024
#define FLASH_PAGE_SIZE 512
#define FLASH_ERASE_TIME 100000
#define FLASH_PROGRAM_TIME 1000
#define CHIPID 0xE0000000
#define FEAT0 0xE0000100
#define FEAT1 0xE0000104
#define FEAT2 0xE0000108
#define FEAT3 0xE000010C
#define EXPECTED_CHIPID 0x209CE02B
#define FCTR 0x20200000
#define FPTR 0x20200008
#define FTCTR 0x2020000C
#define FBWST 0x20200010
#define FCRA 0x2020001C
#define FMSSTART 0x20200020
#define FMSSTOP 0x20200024
#define FMS16 0x20200028
#define FMSW0 0x2020002C
#define FMSW1 0x20200030
#define FMSW2 0x20200034
#define FMSW3 0x20200038
#define EECMD 0x20200080
#define EEADDR 0x20200084
#define EEWDATA 0x20200088
#define EERDATA 0x2020008C
#define EEWSTATE 0x20200090
#define EECLKDIV 0x20200094
#define EEPWRDWN 0x20200098
#define EEMSSTART 0x2020009C
#define EEMSSTOP 0x202000A0
#define EEMSSIG 0x202000A4
#define INT_CLR_ENABLE 0x20200FD8
#define INT_SET_ENABLE 0x20200FDC
#define INT_STATUS 0x20200FE0
#define INT_ENABLE 0x20200FE4
#define INT_CLR_STATUS 0x20200FE8
#define INT_SET_STATUS 0x20200FEC
#define INTSRC_END_OF_PROG (1 << 28)
#define INTSRC_END_OF_BIST (1 << 27)
#define INTSRC_END_OF_RDWR (1 << 26)
#define INTSRC_END_OF_MISR (1 << 2)
#define INTSRC_END_OF_BURN (1 << 1)
#define INTSRC_END_OF_ERASE (1 << 0)
#define FCTR_FS_LOADREQ (1 << 15)
#define FCTR_FS_CACHECLR (1 << 14)
#define FCTR_FS_CACHEBYP (1 << 13)
#define FCTR_FS_PROGREQ (1 << 12)
#define FCTR_FS_RLS (1 << 11)
#define FCTR_FS_PDL (1 << 10)
#define FCTR_FS_PD (1 << 9)
#define FCTR_FS_WPB (1 << 7)
#define FCTR_FS_ISS (1 << 6)
#define FCTR_FS_RLD (1 << 5)
#define FCTR_FS_DCR (1 << 4)
#define FCTR_FS_WEB (1 << 2)
#define FCTR_FS_WRE (1 << 1)
#define FCTR_FS_CS (1 << 0)
#define FPTR_EN_T (1 << 15)
#define FTCTR_FS_BYPASS_R (1 << 29)
#define FTCTR_FS_BYPASS_W (1 << 28)
#define FMSSTOP_MISR_START (1 << 17)
#define EEMSSTOP_STRTBIST (1 << 31)
#define ISS_CUSTOMER_START1 (0x830)
#define ISS_CUSTOMER_END1 (0xA00)
#define ISS_CUSTOMER_SIZE1 (ISS_CUSTOMER_END1 - ISS_CUSTOMER_START1)
#define ISS_CUSTOMER_NWORDS1 (ISS_CUSTOMER_SIZE1 / 4)
#define ISS_CUSTOMER_START2 (0xA40)
#define ISS_CUSTOMER_END2 (0xC00)
#define ISS_CUSTOMER_SIZE2 (ISS_CUSTOMER_END2 - ISS_CUSTOMER_START2)
#define ISS_CUSTOMER_NWORDS2 (ISS_CUSTOMER_SIZE2 / 4)
#define ISS_CUSTOMER_SIZE (ISS_CUSTOMER_SIZE1 + ISS_CUSTOMER_SIZE2)
72 defines
/* ... */
struct lpc2900_flash_bank {
/* ... */
bool is_probed;
/* ... */
uint32_t chipid;
/* ... */
char *target_name;
/* ... */
uint32_t clk_sys_fmc;
/* ... */
uint32_t risky;
/* ... */
uint32_t max_ram_block;
...};
static uint32_t lpc2900_wait_status(struct flash_bank *bank, uint32_t mask, int timeout);
static void lpc2900_setup(struct flash_bank *bank);
static uint32_t lpc2900_is_ready(struct flash_bank *bank);
static uint32_t lpc2900_read_security_status(struct flash_bank *bank);
static uint32_t lpc2900_run_bist128(struct flash_bank *bank,
uint32_t addr_from, uint32_t addr_to,
uint32_t signature[4]);
static unsigned int lpc2900_address2sector(struct flash_bank *bank, uint32_t offset);
static uint32_t lpc2900_calc_tr(uint32_t clock_var, uint32_t time_var);
/* ... */
static uint32_t lpc2900_wait_status(struct flash_bank *bank,
uint32_t mask,
int timeout)
{
uint32_t int_status;
struct target *target = bank->target;
do {
alive_sleep(1);
timeout--;
target_read_u32(target, INT_STATUS, &int_status);
...} while (((int_status & mask) == 0) && (timeout != 0));
if (timeout == 0) {
LOG_DEBUG("Timeout!");
return ERROR_FLASH_OPERATION_FAILED;
}if (timeout == 0) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static void lpc2900_setup(struct flash_bank *bank)
{
uint32_t fcra;
struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
target_write_u32(bank->target, FCTR, FCTR_FS_WEB | FCTR_FS_CS);
fcra = (lpc2900_info->clk_sys_fmc / (3 * 66000)) - 1;
target_write_u32(bank->target, FCRA, fcra);
}{ ... }
/* ... */
static uint32_t lpc2900_is_ready(struct flash_bank *bank)
{
struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
if (!lpc2900_info->is_probed)
return ERROR_FLASH_BANK_NOT_PROBED;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static uint32_t lpc2900_read_security_status(struct flash_bank *bank)
{
uint32_t status = lpc2900_is_ready(bank);
if (status != ERROR_OK)
return status;
struct target *target = bank->target;
target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB | FCTR_FS_ISS);
uint32_t iss_secured_field[0x230/16][4];
target_read_memory(target, bank->base + 0xC00, 4, 0x230/4,
(uint8_t *)iss_secured_field);
target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
/* ... */
for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
unsigned int index_t;
if (sector <= 4)
index_t = sector + 11;
else if (sector <= 7)
index_t = sector + 27;
else
index_t = sector - 8;
bank->sectors[sector].is_protected = -1;
if ((iss_secured_field[index_t][0] == 0x00000000) &&
(iss_secured_field[index_t][1] == 0x00000000) &&
(iss_secured_field[index_t][2] == 0x00000000) &&
(iss_secured_field[index_t][3] == 0x00000000))
bank->sectors[sector].is_protected = 1;
if ((iss_secured_field[index_t][0] == 0xFFFFFFFF) &&
(iss_secured_field[index_t][1] == 0xFFFFFFFF) &&
(iss_secured_field[index_t][2] == 0xFFFFFFFF) &&
(iss_secured_field[index_t][3] == 0xFFFFFFFF))
bank->sectors[sector].is_protected = 0;
}for (unsigned int sector = 0; sector < bank->num_sectors; sector++) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static uint32_t lpc2900_run_bist128(struct flash_bank *bank,
uint32_t addr_from,
uint32_t addr_to,
uint32_t signature[4])
{
struct target *target = bank->target;
target_write_u32(target, INT_CLR_STATUS, INTSRC_END_OF_MISR);
target_write_u32(target, FMSSTART, addr_from >> 4);
target_write_u32(target, FMSSTOP, (addr_to >> 4) | FMSSTOP_MISR_START);
if (lpc2900_wait_status(bank, INTSRC_END_OF_MISR, 1000) != ERROR_OK)
return ERROR_FLASH_OPERATION_FAILED;
uint8_t sig_buf[4 * 4];
target_read_memory(target, FMSW0, 4, 4, sig_buf);
target_buffer_get_u32_array(target, sig_buf, 4, signature);
return ERROR_OK;
}{ ... }
/* ... */
static unsigned int lpc2900_address2sector(struct flash_bank *bank,
uint32_t offset)
{
uint32_t address = bank->base + offset;
for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
if (address < (bank->sectors[sector].offset + bank->sectors[sector].size))
return sector;
}for (unsigned int sector = 0; sector < bank->num_sectors; sector++) { ... }
return 0;
}{ ... }
/* ... */
static int lpc2900_write_index_page(struct flash_bank *bank,
int pagenum,
uint8_t page[FLASH_PAGE_SIZE])
{
if ((pagenum < 4) || (pagenum > 7)) {
LOG_ERROR("Refuse to burn index sector page %d", pagenum);
return ERROR_COMMAND_ARGUMENT_INVALID;
}if ((pagenum < 4) || (pagenum > 7)) { ... }
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
lpc2900_setup(bank);
target_write_u32(target, bank->base, 0);
target_write_u32(target, FCTR,
FCTR_FS_LOADREQ | FCTR_FS_WPB | FCTR_FS_ISS |
FCTR_FS_WEB | FCTR_FS_WRE | FCTR_FS_CS);
target_write_u32(target, FCTR,
FCTR_FS_ISS | FCTR_FS_WEB | FCTR_FS_WRE | FCTR_FS_CS);
if (target_write_memory(target,
bank->base + pagenum * FLASH_PAGE_SIZE,
4, FLASH_PAGE_SIZE / 4, page) != ERROR_OK) {
LOG_ERROR("Index sector write failed @ page %d", pagenum);
target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
return ERROR_FLASH_OPERATION_FAILED;
}if (target_write_memory(target, bank->base + pagenum * FLASH_PAGE_SIZE, 4, FLASH_PAGE_SIZE / 4, page) != ERROR_OK) { ... }
target_write_u32(target, INT_CLR_STATUS, INTSRC_END_OF_BURN);
target_write_u32(target, FPTR,
FPTR_EN_T | lpc2900_calc_tr(lpc2900_info->clk_sys_fmc,
FLASH_PROGRAM_TIME));
target_write_u32(target, FCTR,
FCTR_FS_PROGREQ | FCTR_FS_ISS |
FCTR_FS_WPB | FCTR_FS_WRE | FCTR_FS_CS);
/* ... */
if (lpc2900_wait_status(bank, INTSRC_END_OF_BURN, 1000) != ERROR_OK) {
LOG_ERROR("Index sector write failed @ page %d", pagenum);
target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
return ERROR_FLASH_OPERATION_FAILED;
}if (lpc2900_wait_status(bank, INTSRC_END_OF_BURN, 1000) != ERROR_OK) { ... }
target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
return ERROR_OK;
}{ ... }
/* ... */
static uint32_t lpc2900_calc_tr(uint32_t clock_var, uint32_t time_var)
{
/* ... */
uint32_t tr_val = (uint32_t)((((time_var / 1e6) * clock_var) + 511.0) / 512.0);
return tr_val;
}{ ... }
Helper functions
/* ... */
COMMAND_HANDLER(lpc2900_handle_signature_command)
{
uint32_t status;
uint32_t signature[4];
if (CMD_ARGC < 1)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
status = lpc2900_run_bist128(bank, bank->base, bank->base + (bank->size - 1), signature);
if (status != ERROR_OK)
return status;
command_print(CMD, "signature: 0x%8.8" PRIx32
":0x%8.8" PRIx32
":0x%8.8" PRIx32
":0x%8.8" PRIx32,
signature[3], signature[2], signature[1], signature[0]);
return ERROR_OK;
}{ ... }
/* ... */
COMMAND_HANDLER(lpc2900_handle_read_custom_command)
{
if (CMD_ARGC < 2)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
lpc2900_info->risky = 0;
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
uint8_t customer[4 * (ISS_CUSTOMER_NWORDS1 + ISS_CUSTOMER_NWORDS2)];
target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB | FCTR_FS_ISS);
target_read_memory(target, bank->base+ISS_CUSTOMER_START1, 4,
ISS_CUSTOMER_NWORDS1,
&customer[0]);
target_read_memory(target, bank->base+ISS_CUSTOMER_START2, 4,
ISS_CUSTOMER_NWORDS2,
&customer[4 * ISS_CUSTOMER_NWORDS1]);
target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
struct fileio *fileio;
const char *filename = CMD_ARGV[1];
int ret = fileio_open(&fileio, filename, FILEIO_WRITE, FILEIO_BINARY);
if (ret != ERROR_OK) {
LOG_WARNING("Could not open file %s", filename);
return ret;
}if (ret != ERROR_OK) { ... }
size_t nwritten;
ret = fileio_write(fileio, sizeof(customer), customer, &nwritten);
if (ret != ERROR_OK) {
LOG_ERROR("Write operation to file %s failed", filename);
fileio_close(fileio);
return ret;
}if (ret != ERROR_OK) { ... }
fileio_close(fileio);
return ERROR_OK;
}{ ... }
/* ... */
COMMAND_HANDLER(lpc2900_handle_password_command)
{
if (CMD_ARGC < 2)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
#define ISS_PASSWORD "I_know_what_I_am_doing"
lpc2900_info->risky = !strcmp(CMD_ARGV[1], ISS_PASSWORD);
if (!lpc2900_info->risky) {
command_print(CMD, "Wrong password (use '%s')", ISS_PASSWORD);
return ERROR_COMMAND_ARGUMENT_INVALID;
}if (!lpc2900_info->risky) { ... }
command_print(CMD,
"Potentially dangerous operation allowed in next command!");
return ERROR_OK;
}{ ... }
/* ... */
COMMAND_HANDLER(lpc2900_handle_write_custom_command)
{
if (CMD_ARGC < 2)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
if (!lpc2900_info->risky) {
command_print(CMD, "Command execution not allowed!");
return ERROR_COMMAND_ARGUMENT_INVALID;
}if (!lpc2900_info->risky) { ... }
lpc2900_info->risky = 0;
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
struct image image;
image.base_address_set = true;
image.base_address = 0;
image.start_address_set = false;
const char *filename = CMD_ARGV[1];
const char *type = (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL;
retval = image_open(&image, filename, type);
if (retval != ERROR_OK)
return retval;
/* ... */
if (image.num_sections != 1) {
LOG_ERROR("Only one section allowed in image file.");
return ERROR_COMMAND_SYNTAX_ERROR;
}if (image.num_sections != 1) { ... }
if ((image.sections[0].base_address != 0) ||
(image.sections[0].size != ISS_CUSTOMER_SIZE)) {
LOG_ERROR("Incorrect image file size. Expected %d, "
"got %" PRIu32,
ISS_CUSTOMER_SIZE, image.sections[0].size);
return ERROR_COMMAND_SYNTAX_ERROR;
}if ((image.sections[0].base_address != 0) || (image.sections[0].size != ISS_CUSTOMER_SIZE)) { ... }
uint8_t page[FLASH_PAGE_SIZE];
uint32_t offset = ISS_CUSTOMER_START1 % FLASH_PAGE_SIZE;
memset(page, 0xff, FLASH_PAGE_SIZE);
size_t size_read;
retval = image_read_section(&image, 0, 0,
ISS_CUSTOMER_SIZE1, &page[offset], &size_read);
if (retval != ERROR_OK) {
LOG_ERROR("couldn't read from file '%s'", filename);
image_close(&image);
return retval;
}if (retval != ERROR_OK) { ... }
retval = lpc2900_write_index_page(bank, 4, page);
if (retval != ERROR_OK) {
image_close(&image);
return retval;
}if (retval != ERROR_OK) { ... }
offset = ISS_CUSTOMER_START2 % FLASH_PAGE_SIZE;
memset(page, 0xff, FLASH_PAGE_SIZE);
retval = image_read_section(&image, 0, ISS_CUSTOMER_SIZE1,
ISS_CUSTOMER_SIZE2, &page[offset], &size_read);
if (retval != ERROR_OK) {
LOG_ERROR("couldn't read from file '%s'", filename);
image_close(&image);
return retval;
}if (retval != ERROR_OK) { ... }
retval = lpc2900_write_index_page(bank, 5, page);
if (retval != ERROR_OK) {
image_close(&image);
return retval;
}if (retval != ERROR_OK) { ... }
image_close(&image);
return ERROR_OK;
}{ ... }
/* ... */
COMMAND_HANDLER(lpc2900_handle_secure_sector_command)
{
if (CMD_ARGC < 3)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
if (!lpc2900_info->risky) {
command_print(CMD, "Command execution not allowed! "
"(use 'password' command first)");
return ERROR_COMMAND_ARGUMENT_INVALID;
}if (!lpc2900_info->risky) { ... }
lpc2900_info->risky = 0;
unsigned int first, last;
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], first);
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], last);
if ((first >= bank->num_sectors) ||
(last >= bank->num_sectors) ||
(first > last)) {
command_print(CMD, "Illegal sector range");
return ERROR_COMMAND_ARGUMENT_INVALID;
}if ((first >= bank->num_sectors) || (last >= bank->num_sectors) || (first > last)) { ... }
uint8_t page[FLASH_PAGE_SIZE];
if ((first <= 4) || (last >= 8)) {
memset(&page, 0xff, FLASH_PAGE_SIZE);
for (unsigned int sector = first; sector <= last; sector++) {
if (sector <= 4)
memset(&page[0xB0 + 16*sector], 0, 16);
else if (sector >= 8)
memset(&page[0x00 + 16*(sector - 8)], 0, 16);
}for (unsigned int sector = first; sector <= last; sector++) { ... }
retval = lpc2900_write_index_page(bank, 6, page);
if (retval != ERROR_OK) {
LOG_ERROR("failed to update index sector page 6");
return retval;
}if (retval != ERROR_OK) { ... }
}if ((first <= 4) || (last >= 8)) { ... }
if ((first <= 7) && (last >= 5)) {
memset(&page, 0xff, FLASH_PAGE_SIZE);
for (unsigned int sector = first; sector <= last; sector++) {
if ((sector >= 5) && (sector <= 7))
memset(&page[0x00 + 16*(sector - 5)], 0, 16);
}for (unsigned int sector = first; sector <= last; sector++) { ... }
retval = lpc2900_write_index_page(bank, 7, page);
if (retval != ERROR_OK) {
LOG_ERROR("failed to update index sector page 7");
return retval;
}if (retval != ERROR_OK) { ... }
}if ((first <= 7) && (last >= 5)) { ... }
command_print(CMD,
"Sectors security will become effective after next power cycle");
if (lpc2900_read_security_status(bank) != ERROR_OK) {
LOG_ERROR("Cannot determine sector security status");
return ERROR_FLASH_OPERATION_FAILED;
}if (lpc2900_read_security_status(bank) != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
/* ... */
COMMAND_HANDLER(lpc2900_handle_secure_jtag_command)
{
if (CMD_ARGC < 1)
return ERROR_COMMAND_SYNTAX_ERROR;
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (retval != ERROR_OK)
return retval;
struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
if (!lpc2900_info->risky) {
command_print(CMD, "Command execution not allowed! "
"(use 'password' command first)");
return ERROR_COMMAND_ARGUMENT_INVALID;
}if (!lpc2900_info->risky) { ... }
lpc2900_info->risky = 0;
uint8_t page[FLASH_PAGE_SIZE];
memset(&page, 0xff, FLASH_PAGE_SIZE);
page[0x30 + 15] = 0x7F;
page[0x30 + 11] = 0x7F;
page[0x30 + 7] = 0x7F;
page[0x30 + 3] = 0x7F;
retval = lpc2900_write_index_page(bank, 5, page);
if (retval != ERROR_OK) {
LOG_ERROR("failed to update index sector page 5");
return retval;
}if (retval != ERROR_OK) { ... }
LOG_INFO("JTAG security set. Good bye!");
return ERROR_OK;
}{ ... }
Private flash commands
static const struct command_registration lpc2900_exec_command_handlers[] = {
{
.name = "signature",
.usage = "<bank>",
.handler = lpc2900_handle_signature_command,
.mode = COMMAND_EXEC,
.help = "Calculate and display signature of flash bank.",
...},
{
.name = "read_custom",
.handler = lpc2900_handle_read_custom_command,
.mode = COMMAND_EXEC,
.usage = "bank_id filename",
.help = "Copies 912 bytes of customer information "
"from index sector into file.",
...},
{
.name = "password",
.handler = lpc2900_handle_password_command,
.mode = COMMAND_EXEC,
.usage = "bank_id password",
.help = "Enter fixed password to enable 'dangerous' options.",
...},
{
.name = "write_custom",
.handler = lpc2900_handle_write_custom_command,
.mode = COMMAND_EXEC,
.usage = "bank_id filename ('bin'|'ihex'|'elf'|'s19')",
.help = "Copies 912 bytes of customer info from file "
"to index sector.",
...},
{
.name = "secure_sector",
.handler = lpc2900_handle_secure_sector_command,
.mode = COMMAND_EXEC,
.usage = "bank_id first_sector last_sector",
.help = "Activate sector security for a range of sectors. "
"It will be effective after a power cycle.",
...},
{
.name = "secure_jtag",
.handler = lpc2900_handle_secure_jtag_command,
.mode = COMMAND_EXEC,
.usage = "bank_id",
.help = "Disable the JTAG port. "
"It will be effective after a power cycle.",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration lpc2900_command_handlers[] = {
{
.name = "lpc2900",
.mode = COMMAND_ANY,
.help = "LPC2900 flash command group",
.usage = "",
.chain = lpc2900_exec_command_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
FLASH_BANK_COMMAND_HANDLER(lpc2900_flash_bank_command)
{
struct lpc2900_flash_bank *lpc2900_info;
if (CMD_ARGC < 6)
return ERROR_COMMAND_SYNTAX_ERROR;
lpc2900_info = malloc(sizeof(struct lpc2900_flash_bank));
bank->driver_priv = lpc2900_info;
/* ... */
uint32_t clk_sys_fmc;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], clk_sys_fmc);
lpc2900_info->clk_sys_fmc = clk_sys_fmc * 1000;
uint32_t clock_limit;
clock_limit = 512000000l / FLASH_PROGRAM_TIME;
if (lpc2900_info->clk_sys_fmc < clock_limit) {
LOG_WARNING("flash clock must be at least %" PRIu32 " kHz",
(clock_limit / 1000));
return ERROR_FLASH_BANK_INVALID;
}if (lpc2900_info->clk_sys_fmc < clock_limit) { ... }
clock_limit = (uint32_t)((32767.0 * 512.0 * 1e6) / FLASH_ERASE_TIME);
if (lpc2900_info->clk_sys_fmc > clock_limit) {
LOG_WARNING("flash clock must be a maximum of %" PRIu32 " kHz",
(clock_limit / 1000));
return ERROR_FLASH_BANK_INVALID;
}if (lpc2900_info->clk_sys_fmc > clock_limit) { ... }
lpc2900_info->chipid = 0;
lpc2900_info->is_probed = false;
return ERROR_OK;
}{ ... }
/* ... */
static int lpc2900_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
uint32_t status;
unsigned int last_unsecured_sector;
bool has_unsecured_sector;
struct target *target = bank->target;
struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
status = lpc2900_is_ready(bank);
if (status != ERROR_OK)
return status;
if ((last < first) || (last >= bank->num_sectors)) {
LOG_INFO("Bad sector range");
return ERROR_FLASH_SECTOR_INVALID;
}if ((last < first) || (last >= bank->num_sectors)) { ... }
lpc2900_read_security_status(bank);
/* ... */
last_unsecured_sector = -1;
has_unsecured_sector = false;
for (unsigned int sector = first; sector <= last; sector++) {
if (!bank->sectors[sector].is_protected) {
last_unsecured_sector = sector;
has_unsecured_sector = true;
}if (!bank->sectors[sector].is_protected) { ... }
}for (unsigned int sector = first; sector <= last; sector++) { ... }
/* ... */
if (!has_unsecured_sector)
return ERROR_OK;
lpc2900_setup(bank);
target_write_u32(target, INT_CLR_STATUS, INTSRC_END_OF_ERASE);
target_write_u32(target, FPTR,
FPTR_EN_T | lpc2900_calc_tr(lpc2900_info->clk_sys_fmc,
FLASH_ERASE_TIME));
for (unsigned int sector = first; sector <= last_unsecured_sector; sector++) {
/* ... */
if (!bank->sectors[sector].is_protected) {
target_write_u32(target, bank->sectors[sector].offset, 0);
target_write_u32(target, FCTR,
FCTR_FS_LOADREQ | FCTR_FS_WPB |
FCTR_FS_WEB | FCTR_FS_WRE | FCTR_FS_CS);
/* ... */
target_write_u32(target, bank->sectors[sector].offset, 0);
if (sector == last_unsecured_sector) {
target_write_u32(target, FCTR,
FCTR_FS_PROGREQ | FCTR_FS_WPB | FCTR_FS_CS);
}if (sector == last_unsecured_sector) { ... } else {
target_write_u32(target, FCTR,
FCTR_FS_LOADREQ | FCTR_FS_WPB |
FCTR_FS_WEB | FCTR_FS_CS);
}else { ... }
}if (!bank->sectors[sector].is_protected) { ... }
}for (unsigned int sector = first; sector <= last_unsecured_sector; sector++) { ... }
/* ... */
if (lpc2900_wait_status(bank, INTSRC_END_OF_ERASE, 2000) != ERROR_OK)
return ERROR_FLASH_OPERATION_FAILED;
target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
return ERROR_OK;
}{ ... }
/* ... */
/* ... */
static int lpc2900_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
uint8_t page[FLASH_PAGE_SIZE];
uint32_t status;
uint32_t num_bytes;
struct target *target = bank->target;
struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
int retval;
static const uint32_t write_target_code[] = {
0xe3a0a007,
0xe583a000,
0xe3a06020,
0xe8b00f00,
0xe8a10f00,
0xe2566001,
0x1afffffb,
0xe3a0a002,
0xe583afe8,
0xe5834008,
/* ... */
0xe3a0a083,
0xe38aaa01,
0xe583a000,
0xe593afe0,
0xe21aa002,
0x0afffffc,
0xe2522001,
0x1affffed,
0xeafffffe
...};
status = lpc2900_is_ready(bank);
if (status != ERROR_OK)
return status;
lpc2900_setup(bank);
lpc2900_read_security_status(bank);
for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
/* ... */
if (((bank->base + offset) <
(bank->sectors[sector].offset + bank->sectors[sector].size)) &&
((bank->base + (offset + count - 1)) >= bank->sectors[sector].offset)) {
/* ... */
if (!bank->sectors[sector].is_protected) {
target_write_u32(target, bank->sectors[sector].offset, 0);
target_write_u32(target, FCTR,
FCTR_FS_LOADREQ | FCTR_FS_WPB |
FCTR_FS_WEB | FCTR_FS_WRE | FCTR_FS_CS);
}if (!bank->sectors[sector].is_protected) { ... }
}if (((bank->base + offset) < (bank->sectors[sector].offset + bank->sectors[sector].size)) && ((bank->base + (offset + count - 1)) >= bank->sectors[sector].offset)) { ... }
}for (unsigned int sector = 0; sector < bank->num_sectors; sector++) { ... }
uint32_t prog_time = FPTR_EN_T | lpc2900_calc_tr(lpc2900_info->clk_sys_fmc, FLASH_PROGRAM_TIME);
/* ... */
const uint32_t target_code_size = sizeof(write_target_code);
/* ... */
struct working_area *warea;
uint32_t buffer_size = lpc2900_info->max_ram_block - 1 * KiB;
while (target_alloc_working_area_try(target,
buffer_size + target_code_size,
&warea) != ERROR_OK) {
buffer_size -= 1 * KiB;
if (buffer_size < 2 * KiB) {
LOG_INFO("no (large enough) working area, falling back to host mode");
warea = NULL;
break;
}if (buffer_size < 2 * KiB) { ... }
}while (target_alloc_working_area_try(target, buffer_size + target_code_size, &warea) != ERROR_OK) { ... }
if (warea) {
struct reg_param reg_params[5];
struct arm_algorithm arm_algo;
uint8_t code[sizeof(write_target_code)];
target_buffer_set_u32_array(target, code, ARRAY_SIZE(write_target_code),
write_target_code);
retval = target_write_buffer(target, (warea->address) + buffer_size, sizeof(code), code);
if (retval != ERROR_OK) {
LOG_ERROR("Unable to write block write code to target");
target_free_all_working_areas(target);
return ERROR_FLASH_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
init_reg_param(®_params[3], "r3", 32, PARAM_OUT);
init_reg_param(®_params[4], "r4", 32, PARAM_OUT);
while (count != 0) {
uint32_t this_npages;
const uint8_t *this_buffer;
unsigned int start_sector = lpc2900_address2sector(bank, offset);
if (offset % FLASH_PAGE_SIZE) {
/* ... */
memset(&page, 0xff, sizeof(page));
memcpy(&page[offset % FLASH_PAGE_SIZE],
buffer,
FLASH_PAGE_SIZE - (offset % FLASH_PAGE_SIZE));
this_npages = 1;
this_buffer = &page[0];
count = count + (offset % FLASH_PAGE_SIZE);
offset = offset - (offset % FLASH_PAGE_SIZE);
}if (offset % FLASH_PAGE_SIZE) { ... } else if (count < FLASH_PAGE_SIZE) {
memset(&page, 0xff, sizeof(page));
memcpy(&page, buffer, count);
this_npages = 1;
this_buffer = &page[0];
count = FLASH_PAGE_SIZE;
}else if (count < FLASH_PAGE_SIZE) { ... } else {
this_npages = (count < buffer_size) ?
count / FLASH_PAGE_SIZE :
buffer_size / FLASH_PAGE_SIZE;
this_buffer = buffer;
unsigned int sector = start_sector + 1;
while (sector < bank->num_sectors) {
if (bank->sectors[sector].is_protected) {
if ((bank->sectors[sector].offset - bank->base) <
(offset + (this_npages * FLASH_PAGE_SIZE))) {
this_npages =
(bank->sectors[sector].offset -
bank->base - offset)
/ FLASH_PAGE_SIZE;
break;
}if ((bank->sectors[sector].offset - bank->base) < (offset + (this_npages * FLASH_PAGE_SIZE))) { ... }
}if (bank->sectors[sector].is_protected) { ... }
sector++;
}while (sector < bank->num_sectors) { ... }
}else { ... }
if (bank->sectors[start_sector].is_protected) {
LOG_DEBUG("Skip secured sector %u",
start_sector);
if (start_sector == bank->num_sectors - 1)
break;
uint32_t nskip = bank->sectors[start_sector].size -
(offset % bank->sectors[start_sector].size);
offset += nskip;
buffer += nskip;
count = (count >= nskip) ? (count - nskip) : 0;
continue;
}if (bank->sectors[start_sector].is_protected) { ... }
retval = target_write_buffer(target, warea->address,
this_npages * FLASH_PAGE_SIZE, this_buffer);
if (retval != ERROR_OK) {
LOG_ERROR("Unable to write data to target");
target_free_all_working_areas(target);
return ERROR_FLASH_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
buf_set_u32(reg_params[0].value, 0, 32, warea->address);
buf_set_u32(reg_params[1].value, 0, 32, offset);
buf_set_u32(reg_params[2].value, 0, 32, this_npages);
buf_set_u32(reg_params[3].value, 0, 32, FCTR);
buf_set_u32(reg_params[4].value, 0, 32, FPTR_EN_T | prog_time);
arm_algo.common_magic = ARM_COMMON_MAGIC;
arm_algo.core_mode = ARM_MODE_SVC;
arm_algo.core_state = ARM_STATE_ARM;
retval = target_run_algorithm(target, 0, NULL, 5, reg_params,
(warea->address) + buffer_size,
(warea->address) + buffer_size + target_code_size - 4,
10000,
&arm_algo);
if (retval != ERROR_OK) {
LOG_ERROR("Execution of flash algorithm failed.");
target_free_all_working_areas(target);
retval = ERROR_FLASH_OPERATION_FAILED;
break;
}if (retval != ERROR_OK) { ... }
count -= this_npages * FLASH_PAGE_SIZE;
buffer += this_npages * FLASH_PAGE_SIZE;
offset += this_npages * FLASH_PAGE_SIZE;
}while (count != 0) { ... }
destroy_reg_param(®_params[0]);
destroy_reg_param(®_params[1]);
destroy_reg_param(®_params[2]);
destroy_reg_param(®_params[3]);
destroy_reg_param(®_params[4]);
target_free_all_working_areas(target);
}if (warea) { ... } else {
while (count != 0) {
num_bytes = (count >= FLASH_PAGE_SIZE) ?
FLASH_PAGE_SIZE - (offset % FLASH_PAGE_SIZE) :
count;
if (!bank->sectors[lpc2900_address2sector(bank, offset)].is_protected) {
target_write_u32(target, FCTR,
FCTR_FS_CS | FCTR_FS_WRE | FCTR_FS_WEB);
memset(page, 0xFF, FLASH_PAGE_SIZE);
memcpy(&page[offset % FLASH_PAGE_SIZE],
&buffer[offset % FLASH_PAGE_SIZE],
num_bytes);
if (target_write_memory(target,
bank->base + (offset - (offset % FLASH_PAGE_SIZE)),
4, FLASH_PAGE_SIZE / 4, page) != ERROR_OK) {
LOG_ERROR("Write failed @ 0x%8.8" PRIx32, offset);
target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
return ERROR_FLASH_OPERATION_FAILED;
}if (target_write_memory(target, bank->base + (offset - (offset % FLASH_PAGE_SIZE)), 4, FLASH_PAGE_SIZE / 4, page) != ERROR_OK) { ... }
target_write_u32(target, INT_CLR_STATUS, INTSRC_END_OF_BURN);
target_write_u32(target, FPTR, FPTR_EN_T | prog_time);
target_write_u32(target, FCTR,
FCTR_FS_CS | FCTR_FS_WRE | FCTR_FS_WPB | FCTR_FS_PROGREQ);
/* ... */
if (lpc2900_wait_status(bank, INTSRC_END_OF_BURN, 1000) != ERROR_OK) {
LOG_ERROR("Write failed @ 0x%8.8" PRIx32, offset);
target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
return ERROR_FLASH_OPERATION_FAILED;
}if (lpc2900_wait_status(bank, INTSRC_END_OF_BURN, 1000) != ERROR_OK) { ... }
}if (!bank->sectors[lpc2900_address2sector(bank, offset)].is_protected) { ... }
offset += num_bytes;
buffer += num_bytes;
count -= num_bytes;
}while (count != 0) { ... }
retval = ERROR_OK;
}else { ... }
target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
return retval;
}{ ... }
/* ... */
static int lpc2900_probe(struct flash_bank *bank)
{
struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
struct target *target = bank->target;
uint32_t offset;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
if (lpc2900_info->is_probed)
return ERROR_OK;
/* ... */
target_read_u32(target, CHIPID, &lpc2900_info->chipid);
if (lpc2900_info->chipid != EXPECTED_CHIPID) {
LOG_WARNING("Device is not an LPC29xx");
return ERROR_FLASH_OPERATION_FAILED;
}if (lpc2900_info->chipid != EXPECTED_CHIPID) { ... }
uint32_t feat0, feat1, feat2, feat3;
target_read_u32(target, FEAT0, &feat0);
target_read_u32(target, FEAT1, &feat1);
target_read_u32(target, FEAT2, &feat2);
target_read_u32(target, FEAT3, &feat3);
bank->base = 0x20000000;
uint32_t num_64k_sectors = (feat2 >> 16) & 0xFF;
uint32_t num_8k_sectors = (feat2 >> 0) & 0xFF;
bank->num_sectors = num_64k_sectors + num_8k_sectors;
bank->size = KiB * (64 * num_64k_sectors + 8 * num_8k_sectors);
lpc2900_info->max_ram_block = 16 * KiB;
if ((feat1 & 0x30) == 0x30) {
lpc2900_info->max_ram_block = 32 * KiB;
if ((feat1 & 0x0C) == 0x0C)
lpc2900_info->max_ram_block = 48 * KiB;
}if ((feat1 & 0x30) == 0x30) { ... }
uint32_t package_code = feat0 & 0x0F;
uint32_t itcm_code = (feat1 >> 16) & 0x1F;
uint32_t found = 1;
if ((package_code == 4) && (itcm_code == 5)) {
lpc2900_info->target_name = (bank->size == 768*KiB) ? "LPC2919" : "LPC2917";
}if ((package_code == 4) && (itcm_code == 5)) { ... } else {
if (package_code == 2) {
if (bank->size == 128*KiB)
lpc2900_info->target_name = "LPC2921";
else if (bank->size == 256*KiB)
lpc2900_info->target_name = "LPC2923";
else if (bank->size == 512*KiB)
lpc2900_info->target_name = "LPC2925";
else
found = 0;
}if (package_code == 2) { ... } else if (package_code == 4) {
if ((bank->size == 256*KiB) && (feat3 == 0xFFFFFFE9))
lpc2900_info->target_name = "LPC2926";
else if ((bank->size == 512*KiB) && (feat3 == 0xFFFFFCF0))
lpc2900_info->target_name = "LPC2917/01";
else if ((bank->size == 512*KiB) && (feat3 == 0xFFFFFFF1))
lpc2900_info->target_name = "LPC2927";
else if ((bank->size == 768*KiB) && (feat3 == 0xFFFFFCF8))
lpc2900_info->target_name = "LPC2919/01";
else if ((bank->size == 768*KiB) && (feat3 == 0xFFFFFFF9))
lpc2900_info->target_name = "LPC2929";
else
found = 0;
}else if (package_code == 4) { ... } else if (package_code == 5) {
lpc2900_info->target_name = (bank->size == 0) ? "LPC2930" : "LPC2939";
}else if (package_code == 5) { ... } else
found = 0;
}else { ... }
if (!found) {
LOG_WARNING("Unknown LPC29xx derivative (FEATx="
"%08" PRIx32 ":%08" PRIx32 ":%08" PRIx32 ":%08" PRIx32 ")",
feat0, feat1, feat2, feat3);
return ERROR_FLASH_OPERATION_FAILED;
}if (!found) { ... }
LOG_INFO("Flash bank %u: Device %s, %" PRIu32
" KiB in %u sectors",
bank->bank_number,
lpc2900_info->target_name, bank->size / KiB,
bank->num_sectors);
if (bank->num_sectors == 0) {
LOG_WARNING("Flashless device cannot be handled");
return ERROR_FLASH_OPERATION_FAILED;
}if (bank->num_sectors == 0) { ... }
/* ... */
bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
offset = 0;
for (unsigned int i = 0; i < bank->num_sectors; i++) {
bank->sectors[i].offset = offset;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = -1;
if (i <= 7)
bank->sectors[i].size = 8 * KiB;
else if (i <= 18)
bank->sectors[i].size = 64 * KiB;
else {
/* ... */
bank->sectors[i].size = 0;
LOG_ERROR("Never heard about sector %u", i);
}else { ... }
offset += bank->sectors[i].size;
}for (unsigned int i = 0; i < bank->num_sectors; i++) { ... }
lpc2900_info->is_probed = true;
if (lpc2900_read_security_status(bank) != ERROR_OK) {
LOG_ERROR("Cannot determine sector security status");
return ERROR_FLASH_OPERATION_FAILED;
}if (lpc2900_read_security_status(bank) != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int lpc2900_erase_check(struct flash_bank *bank)
{
uint32_t status = lpc2900_is_ready(bank);
if (status != ERROR_OK) {
LOG_INFO("Processor not halted/not probed");
return status;
}if (status != ERROR_OK) { ... }
/* ... */
for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
uint32_t signature[4];
status = lpc2900_run_bist128(bank, bank->sectors[sector].offset,
bank->sectors[sector].offset + (bank->sectors[sector].size - 1), signature);
if (status != ERROR_OK)
return status;
/* ... */
if (bank->sectors[sector].size == 8*KiB) {
bank->sectors[sector].is_erased =
(signature[3] == 0x01ABAAAA) &&
(signature[2] == 0xAAAAAAAA) &&
(signature[1] == 0xAAAAAAAA) &&
(signature[0] == 0xAAA00AAA);
}if (bank->sectors[sector].size == 8*KiB) { ... }
if (bank->sectors[sector].size == 64*KiB) {
bank->sectors[sector].is_erased =
(signature[3] == 0x11801222) &&
(signature[2] == 0xB88844FF) &&
(signature[1] == 0x11A22008) &&
(signature[0] == 0x2B1BFE44);
}if (bank->sectors[sector].size == 64*KiB) { ... }
}for (unsigned int sector = 0; sector < bank->num_sectors; sector++) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int lpc2900_protect_check(struct flash_bank *bank)
{
return lpc2900_read_security_status(bank);
}{ ... }
const struct flash_driver lpc2900_flash = {
.name = "lpc2900",
.commands = lpc2900_command_handlers,
.flash_bank_command = lpc2900_flash_bank_command,
.erase = lpc2900_erase,
.write = lpc2900_write,
.read = default_flash_read,
.probe = lpc2900_probe,
.auto_probe = lpc2900_probe,
.erase_check = lpc2900_erase_check,
.protect_check = lpc2900_protect_check,
.free_driver_priv = default_flash_free_driver_priv,
...};