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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
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
1075
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
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
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
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
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
1328
1329
1330
1331
1332
1333
1334
1335
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
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
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
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
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
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
|
/*
* Copyright 2015 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
* This utility is used to generate/modify the firmware header which holds
* data used by NPCX ROM code (booter).
*/
#include "ecst.h"
/* Global Variables */
enum verbose_level g_verbose;
char input_file_name[NAME_SIZE];
char output_file_name[NAME_SIZE];
char arg_file_name[NAME_SIZE];
char g_hdr_input_name[NAME_SIZE];
char hdr_args[MAX_ARGS][ARG_SIZE];
char tmp_hdr_args[MAX_ARGS][ARG_SIZE];
FILE *input_file_pointer;
FILE *g_hfd_pointer;
FILE *arg_file_pointer;
FILE *api_file_pointer;
FILE *g_hdr_pointer;
void *gh_console;
unsigned short g_text_atrib;
unsigned short g_bg_atrib;
enum calc_type g_calc_type;
unsigned int ptr_fw_addr;
unsigned int fw_offset;
int is_ptr_merge;
unsigned int g_ram_start_address;
unsigned int g_ram_size;
int api_file_size_bytes;
int is_mrider15 = FALSE;
/* Chips information, RAM start address and RAM size. */
struct chip_info chip_info[] = {{NPCX5M5G_RAM_ADDR, NPCX5M5G_RAM_SIZE},
{NPCX5M6G_RAM_ADDR, NPCX5M6G_RAM_SIZE},
{NPCX7M5X_RAM_ADDR, NPCX7M5X_RAM_SIZE},
{NPCX7M6X_RAM_ADDR, NPCX7M6X_RAM_SIZE},
{NPCX7M7X_RAM_ADDR, NPCX7M7X_RAM_SIZE},};
/* Support chips name strings */
const char *supported_chips = "npcx5m5g, npcx5m6g, npcx7m5g, npcx7m6g, "
"npcx7m6f, npcx7m6fb, npcx7m6fc, npcx7m7wb, or npcx7m7wc";
static unsigned int calc_api_csum_bin(void);
static unsigned int initialize_crc_32(void);
static unsigned int update_crc_32(unsigned int crc, char c);
static unsigned int finalize_crc_32(unsigned int crc);
/*
* Expects a path in `path`, returning a transformation as follows in `result`
*
* The last element of `path` is prefixed with `prefix` if the resulting
* string fits in an array of `resultsz` characters (incl 0-termination).
*
* On success returns TRUE,
* on error (path too long) prints an error on the TERR channel
* and returns FALSE.
*/
static int splice_into_path(char *result, const char *path, int resultsz,
const char *prefix) {
char *last_delim, *result_last_delim;
if (strlen(path) + strlen(prefix) + 1 > resultsz) {
my_printf(TERR,
"\n\nfilename '%s' with prefix '%s' too long\n\n",
path, prefix);
my_printf(TINF,
"\n\n%zu + %zu + 1 needs to fit in %d bytes\n\n",
strlen(path), strlen(prefix), resultsz);
return FALSE;
}
last_delim = strrchr(path, '/');
if (last_delim == NULL) {
/* no delimiter: prefix and exit */
sprintf(result, "%s%s", prefix, path);
return TRUE;
}
/* delimiter: copy, then patch in the prefix */
strcpy(result, path);
result_last_delim = result + (last_delim - path);
sprintf(result_last_delim + 1, "%s%s", prefix, last_delim + 1);
return TRUE;
}
/*
*----------------------------------------------------------------------
* Function: main()
* Parameters: argc, argv
* Return: 0
* Description: Parse the arguments
* Chose manipulation routine according to arguments
*
* In case of bin, save optional parameters given by user
*----------------------------------------------------------------------
*/
int main(int argc, char *argv[])
{
int mode_choose = FALSE;
/* Do we get a bin File? */
int main_fw_hdr_flag = FALSE;
/* Do we get a API bin File? */
int main_api_flag = FALSE;
/* Do we need to create a BootLoader Header file? */
int main_hdr_flag = FALSE;
/* Following variables: common to all modes */
int main_status = TRUE;
unsigned int main_temp = 0L;
char main_str_temp[TMP_STR_SIZE];
char *end_ptr;
int arg_num;
int arg_ind;
int tmp_ind;
int tmp_arg_num;
int cur_arg_index;
FILE *tmp_file;
/* Following variables are used when bin file is provided */
struct tbinparams bin_params;
bin_params.bin_params = 0;
input_file_name[0] = '\0';
memset(input_file_name, 0, NAME_SIZE);
output_file_name[0] = '\0';
memset(output_file_name, 0, NAME_SIZE);
arg_file_name[0] = '\0';
memset(arg_file_name, 0, NAME_SIZE);
g_hdr_input_name[0] = '\0';
memset(g_hdr_input_name, 0, NAME_SIZE);
/* Initialize Global variables */
g_verbose = NO_VERBOSE;
g_ram_start_address = chip_info[DEFAULT_CHIP].ram_addr;
g_ram_size = chip_info[DEFAULT_CHIP].ram_size;
/* Set default values */
g_calc_type = CALC_TYPE_NONE;
bin_params.spi_max_clk = SPI_MAX_CLOCK_DEFAULT;
bin_params.spi_clk_ratio = 0x00;
bin_params.spi_read_mode = SPI_READ_MODE_DEFAULT;
bin_params.fw_load_addr =
chip_info[DEFAULT_CHIP].ram_addr;
bin_params.fw_ep =
chip_info[DEFAULT_CHIP].ram_addr;
bin_params.fw_err_detec_s_addr = FW_CRC_START_ADDR;
bin_params.fw_err_detec_e_addr = FW_CRC_START_ADDR;
bin_params.flash_size = FLASH_SIZE_DEFAULT;
bin_params.fw_hdr_offset = 0;
ptr_fw_addr = 0x00000000;
fw_offset = 0x00000000;
is_ptr_merge = FALSE;
/* Get Standard Output Handler */
/* Wrong Number of Arguments ? No problem */
if (argc < 3)
exit_with_usage();
/* Read all arguments to local array. */
for (arg_num = 0; arg_num < argc; arg_num++)
strncpy(hdr_args[arg_num], argv[arg_num], ARG_SIZE);
/* Loop all arguments. */
/* Parse the Arguments - supports ecrp and bin */
for (arg_ind = 1; arg_ind < arg_num; arg_ind++) {
/* -h display help screen */
if (str_cmp_no_case(hdr_args[arg_ind], "-h") == 0)
exit_with_usage();
/* -v verbose */
else if (str_cmp_no_case(hdr_args[arg_ind], "-v") == 0)
g_verbose = REGULAR_VERBOSE;
/* Super verbose. */
else if (str_cmp_no_case(hdr_args[arg_ind], "-vv") == 0)
g_verbose = SUPER_VERBOSE;
else if (str_cmp_no_case(hdr_args[arg_ind],
"-mode") == 0) {
mode_choose = TRUE;
arg_ind++;
if ((hdr_args[arg_ind] == NULL) ||
(sscanf(hdr_args[arg_ind],
"%s", main_str_temp) != 1)) {
my_printf(TERR, "\nCannot read operation mode");
my_printf(TERR, ", bt, bh or api. !\n");
main_status = FALSE;
} else {
/* bt, bh and api should not coexist */
if (main_fw_hdr_flag ||
main_api_flag ||
main_hdr_flag) {
my_printf(TERR, "\nOperation modes bt");
my_printf(TERR, ", bh, and api should");
my_printf(TERR, " not coexist.\n");
main_status = FALSE;
}
if (str_cmp_no_case(main_str_temp, "bt") == 0)
main_fw_hdr_flag = TRUE;
else if (str_cmp_no_case(main_str_temp,
"bh") == 0)
main_hdr_flag = TRUE;
else if (str_cmp_no_case(main_str_temp,
"api") == 0)
main_api_flag = TRUE;
else {
my_printf(TERR,
"\nInvalid operation mode ");
my_printf(TERR,
"(%s)\n", main_str_temp);
main_status = FALSE;
}
}
}
else if (str_cmp_no_case(hdr_args[arg_ind], "-chip") == 0) {
arg_ind++;
if ((hdr_args[arg_ind] == NULL) ||
(sscanf(hdr_args[arg_ind],
"%s",
main_str_temp) != 1)) {
my_printf(TERR, "\nCannot read chip name %s.\n",
supported_chips);
main_status = FALSE;
} else {
if ((str_cmp_no_case(main_str_temp,
"npcx7m7wb") == 0) ||
(str_cmp_no_case(main_str_temp,
"npcx7m7wc") == 0)) {
if ((bin_params.bin_params
& BIN_FW_LOAD_START_ADDR) ==
0x00000000)
bin_params.fw_load_addr =
chip_info[NPCX7M7].ram_addr;
if ((bin_params.bin_params
& BIN_FW_ENTRY_POINT) ==
0x00000000)
bin_params.fw_ep =
chip_info[NPCX7M7].ram_addr;
g_ram_start_address =
chip_info[NPCX7M7].ram_addr;
g_ram_size =
chip_info[NPCX7M7].ram_size;
} else if ((str_cmp_no_case(main_str_temp,
"npcx7m6f") == 0) ||
(str_cmp_no_case(main_str_temp,
"npcx7m6fb") == 0) ||
(str_cmp_no_case(main_str_temp,
"npcx7m6fc") == 0) ||
(str_cmp_no_case(main_str_temp,
"npcx7m6g") == 0)) {
if ((bin_params.bin_params
& BIN_FW_LOAD_START_ADDR) ==
0x00000000)
bin_params.fw_load_addr =
chip_info[NPCX7M6].ram_addr;
if ((bin_params.bin_params &
BIN_FW_ENTRY_POINT) ==
0x00000000)
bin_params.fw_ep =
chip_info[NPCX7M6].ram_addr;
g_ram_start_address =
chip_info[NPCX7M6].ram_addr;
g_ram_size =
chip_info[NPCX7M6].ram_size;
} else if (str_cmp_no_case(main_str_temp,
"npcx7m5g") == 0) {
if ((bin_params.bin_params
& BIN_FW_LOAD_START_ADDR) ==
0x00000000)
bin_params.fw_load_addr =
chip_info[NPCX7M5].ram_addr;
if ((bin_params.bin_params &
BIN_FW_ENTRY_POINT) ==
0x00000000)
bin_params.fw_ep =
chip_info[NPCX7M5].ram_addr;
g_ram_start_address =
chip_info[NPCX7M5].ram_addr;
g_ram_size =
chip_info[NPCX7M5].ram_size;
} else if (str_cmp_no_case(main_str_temp,
"npcx5m5g") == 0) {
if ((bin_params.bin_params
& BIN_FW_LOAD_START_ADDR) ==
0x00000000)
bin_params.fw_load_addr =
chip_info[NPCX5M5G].ram_addr;
if ((bin_params.bin_params &
BIN_FW_ENTRY_POINT) == 0x00000000)
bin_params.fw_ep =
chip_info[NPCX5M5G].ram_addr;
g_ram_start_address =
chip_info[NPCX5M5G].ram_addr;
g_ram_size =
chip_info[NPCX5M5G].ram_size;
is_mrider15 = TRUE;
} else if (str_cmp_no_case(main_str_temp,
"npcx5m6g") == 0) {
if ((bin_params.bin_params &
BIN_FW_LOAD_START_ADDR) ==
0x00000000)
bin_params.fw_load_addr =
chip_info[NPCX5M6G].ram_addr;
if ((bin_params.bin_params &
BIN_FW_ENTRY_POINT) ==
0x00000000)
bin_params.fw_ep =
chip_info[NPCX5M6G].ram_addr;
g_ram_start_address =
chip_info[NPCX5M6G].ram_addr;
g_ram_size =
chip_info[NPCX5M6G].ram_size;
is_mrider15 = TRUE;
} else {
my_printf(TERR,
"\nInvalid chip name (%s) ",
main_str_temp);
my_printf(TERR, ", it should be %s.\n",
supported_chips);
main_status = FALSE;
}
}
/* -argfile Read argument file. File name must be after it.*/
} else if (str_cmp_no_case(hdr_args[arg_ind],
"-argfile") == 0) {
arg_ind++;
if (arg_ind < arg_num) {
strncpy(arg_file_name,
hdr_args[arg_ind],
sizeof(arg_file_name) - 1);
arg_file_pointer = fopen(arg_file_name, "rt");
if (arg_file_pointer == NULL) {
my_printf(TERR,
"\n\nCannot open %s\n\n",
arg_file_name);
main_status = FALSE;
} else {
cur_arg_index = arg_ind;
/* Copy the arguments to temp array. */
for (tmp_ind = 0;
(tmp_ind + arg_ind + 1) < arg_num;
tmp_ind++)
strncpy(tmp_hdr_args[tmp_ind],
hdr_args
[tmp_ind+arg_ind+1],
ARG_SIZE);
tmp_arg_num = tmp_ind;
/* Read arguments from file to array */
for (arg_ind++;
fscanf(arg_file_pointer,
"%s",
hdr_args[arg_ind]) == 1;
arg_ind++)
;
fclose(arg_file_pointer);
arg_file_pointer = NULL;
/* Copy back the restored arguments. */
for (tmp_ind = 0;
tmp_ind < tmp_arg_num;
tmp_ind++) {
strncpy(hdr_args[arg_ind++],
tmp_hdr_args[tmp_ind],
ARG_SIZE);
}
arg_num = arg_ind;
arg_ind = cur_arg_index;
}
} else {
my_printf(TERR,
"\nMissing Argument File Name\n");
main_status = FALSE;
}
/* -i get input file name. */
} else if (str_cmp_no_case(hdr_args[arg_ind], "-i") == 0) {
arg_ind++;
if (arg_ind < arg_num) {
strncpy(input_file_name,
hdr_args[arg_ind],
sizeof(input_file_name) - 1);
} else {
my_printf(TERR, "\nMissing Input File Name\n");
main_status = FALSE;
}
/* -o Get output file name. */
} else if (str_cmp_no_case(hdr_args[arg_ind], "-o") == 0) {
arg_ind++;
if (arg_ind < arg_num) {
strncpy(output_file_name,
hdr_args[arg_ind],
sizeof(output_file_name) - 1);
} else {
my_printf(TERR,
"\nMissing Output File Name.\n");
main_status = FALSE;
}
/* -usearmrst get FW entry point from FW image offset 4.*/
} else if (str_cmp_no_case(hdr_args[arg_ind],
"-usearmrst") == 0) {
if ((bin_params.bin_params &
BIN_FW_ENTRY_POINT) != 0x00000000) {
my_printf(TERR, "\n-usearmrst not allowed, ");
my_printf(TERR, "FW entry point already set ");
my_printf(TERR, "using -fwep !\n");
main_status = FALSE;
} else
bin_params.bin_params |=
BIN_FW_USER_ARM_RESET;
/* -nohcrs disable header CRC*/
} else if (str_cmp_no_case(hdr_args[arg_ind], "-nohcrc") == 0)
bin_params.bin_params |=
BIN_FW_HDR_CRC_DISABLE;
/* -ph merg header in BIN file. */
else if (str_cmp_no_case(hdr_args[arg_ind], "-ph") == 0) {
bin_params.bin_params |=
BIN_FW_HDR_OFFSET;
if ((strlen(hdr_args[arg_ind+1]) == 0) ||
(sscanf(hdr_args[arg_ind+1],
"%x",
&main_temp) != 1))
bin_params.fw_hdr_offset = 0;
else {
arg_ind++;
bin_params.fw_hdr_offset = main_temp;
}
/* -spimaxclk Get SPI flash max clock. */
} else if (str_cmp_no_case(hdr_args[arg_ind],
"-spimaxclk") == 0) {
arg_ind++;
if ((hdr_args[arg_ind] == NULL) ||
(sscanf(hdr_args[arg_ind],
"%d", &main_temp) != 1)) {
my_printf(TERR, "\nCannot read SPI Flash Max");
my_printf(TERR, " Clock !\n");
main_status = FALSE;
} else
bin_params.spi_max_clk =
(unsigned char) main_temp;
/* -spiclkratio Get SPI flash max clock ratio. */
} else if (str_cmp_no_case(hdr_args[arg_ind],
"-spiclkratio") == 0) {
arg_ind++;
if ((hdr_args[arg_ind] == NULL) ||
(sscanf(hdr_args[arg_ind],
"%d", &main_temp) != 1)) {
my_printf(TERR,
"\nCannot read SPI Clock Ratio\n");
main_status = FALSE;
} else
bin_params.spi_clk_ratio =
(unsigned char)main_temp;
/* spireadmode get SPI read mode. */
} else if (str_cmp_no_case(hdr_args[arg_ind],
"-spireadmode") == 0) {
arg_ind++;
if ((hdr_args[arg_ind] == NULL) ||
(sscanf(hdr_args[arg_ind],
"%20s",
main_str_temp) != 1)) {
my_printf(TERR, "\nCannot read SPI Flash");
my_printf(TERR, " Read Mode !\n");
main_status = FALSE;
} else {
if (str_cmp_no_case(main_str_temp,
SPI_NORMAL_MODE_VAL) == 0)
bin_params.spi_read_mode =
(unsigned char) SPI_NORMAL_MODE;
else if (str_cmp_no_case(main_str_temp,
SPI_SINGLE_MODE_VAL) == 0)
bin_params.spi_read_mode =
(unsigned char)
SPI_SINGLE_MODE;
else if (str_cmp_no_case(main_str_temp,
SPI_DUAL_MODE_VAL) == 0)
bin_params.spi_read_mode =
(unsigned char)
SPI_DUAL_MODE;
else if (str_cmp_no_case(main_str_temp,
SPI_QUAD_MODE_VAL) == 0)
bin_params.spi_read_mode =
(unsigned char)
SPI_QUAD_MODE;
else {
my_printf(TERR,
"\nInvalid SPI Flash Read ");
my_printf(TERR,
"Mode (%s), it should be ",
main_str_temp);
my_printf(TERR,
"normal, singleMode, ");
my_printf(TERR,
"dualMode or quadMode !\n");
main_status = FALSE;
}
}
}
/* -unlimburst enable unlimited burst */
else if (str_cmp_no_case(hdr_args[arg_ind], "-unlimburst") == 0)
bin_params.bin_params |= BIN_UNLIM_BURST_ENABLE;
/* -nofcrc disable FW CRC. */
else if (str_cmp_no_case(hdr_args[arg_ind], "-nofcrc") == 0)
bin_params.bin_params |= BIN_FW_CRC_DISABLE;
/* -fwloadaddr, Get the FW load address. */
else if (str_cmp_no_case(hdr_args[arg_ind],
"-fwloadaddr") == 0) {
arg_ind++;
if ((hdr_args[arg_ind] == NULL) ||
(sscanf(hdr_args[arg_ind],
"%x",
&main_temp) != 1)) {
my_printf(TERR, "\nCannot read FW Load ");
my_printf(TERR, "\nstart address !\n");
main_status = FALSE;
} else {
/* Check that the address is 16-bytes aligned */
if ((main_temp &
ADDR_16_BYTES_ALIGNED_MASK) != 0) {
my_printf(TERR,
"\nFW load address start ");
my_printf(TERR,
"address (0x%08X) is not ",
main_temp);
my_printf(TERR,
"16-bytes aligned !\n");
main_status = FALSE;
} else {
bin_params.fw_load_addr =
main_temp;
bin_params.bin_params |=
BIN_FW_LOAD_START_ADDR;
}
}
/* -fwep, Get the FW entry point. */
} else if (str_cmp_no_case(hdr_args[arg_ind], "-fwep") == 0) {
if ((bin_params.bin_params & BIN_FW_USER_ARM_RESET)
!= 0x00000000) {
my_printf(TERR,
"\n-fwep not allowed, FW entry point");
my_printf(TERR,
" already set using -usearmrst!\n");
main_status = FALSE;
} else {
arg_ind++;
if ((hdr_args[arg_ind] == NULL) ||
(sscanf(hdr_args[arg_ind],
"%x",
&main_temp) != 1)) {
my_printf(TERR,
"\nCan't read FW E-Point\n");
main_status = FALSE;
} else {
bin_params.fw_ep =
main_temp;
bin_params.bin_params |=
BIN_FW_ENTRY_POINT;
}
}
/*
* -crcstart, Get the address from where to calculate
* the FW CRC.
*/
} else if (str_cmp_no_case(hdr_args[arg_ind],
"-crcstart") == 0) {
arg_ind++;
if ((hdr_args[arg_ind] == NULL) ||
(sscanf(hdr_args[arg_ind],
"%x",
&main_temp) != 1)) {
my_printf(TERR,
"\nCannot read FW CRC");
my_printf(TERR,
" start address !\n");
main_status = FALSE;
} else {
bin_params.fw_err_detec_e_addr =
bin_params.fw_err_detec_e_addr -
bin_params.fw_err_detec_s_addr
+ main_temp;
bin_params.fw_err_detec_s_addr =
main_temp;
bin_params.bin_params |= BIN_FW_CKS_START;
}
/* -crcsize, Get the area size that need to be CRCed. */
} else if (str_cmp_no_case(hdr_args[arg_ind],
"-crcsize") == 0) {
arg_ind++;
main_temp = 0x00;
if (hdr_args[arg_ind] == NULL)
end_ptr = NULL;
else
main_temp = strtol(hdr_args[arg_ind],
&end_ptr, 16);
if (hdr_args[arg_ind] == end_ptr) {
my_printf(TERR,
"\nCannot read FW CRC area size !\n");
main_status = FALSE;
} else {
bin_params.fw_err_detec_e_addr =
bin_params.fw_err_detec_s_addr
+ main_temp - 1;
bin_params.bin_params |= BIN_FW_CKS_SIZE;
}
}
/* -fwlen, Get the FW length. */
else if (str_cmp_no_case(hdr_args[arg_ind], "-fwlen") == 0) {
arg_ind++;
if ((hdr_args[arg_ind] == NULL) ||
(sscanf(hdr_args[arg_ind],
"%x",
&main_temp) != 1)) {
my_printf(TERR, "\nCannot read FW length !\n");
main_status = FALSE;
} else {
bin_params.fw_len = main_temp;
bin_params.bin_params |= BIN_FW_LENGTH;
}
}
/* flashsize, Get the flash size. */
else if (str_cmp_no_case(hdr_args[arg_ind],
"-flashsize") == 0) {
arg_ind++;
if ((hdr_args[arg_ind] == NULL) ||
(sscanf(hdr_args[arg_ind],
"%d",
&main_temp) != 1)) {
my_printf(TERR, "\nCannot read Flash size !\n");
main_status = FALSE;
} else
bin_params.flash_size = main_temp;
/* -apisign, Get the method for error detect calculation. */
} else if (str_cmp_no_case(hdr_args[arg_ind],
"-apisign") == 0) {
arg_ind++;
if ((hdr_args[arg_ind] == NULL) ||
(sscanf(hdr_args[arg_ind],
"%s",
main_str_temp) != 1)) {
my_printf(TERR, "\nCannot read API sign, CRC,");
my_printf(TERR, " CheckSum or None. !\n");
main_status = FALSE;
} else {
if (!main_api_flag) {
my_printf(TERR, "\n-apisign is valid ");
my_printf(TERR, "-only with -api.\n");
main_status = FALSE;
}
if (str_cmp_no_case(main_str_temp, "crc") == 0)
g_calc_type = CALC_TYPE_CRC;
else if (str_cmp_no_case(main_str_temp,
"checksum") == 0)
g_calc_type = CALC_TYPE_CHECKSUM;
else {
my_printf(TERR,
"\nInvalid API sign (%s)\n",
main_str_temp);
main_status = FALSE;
}
}
/* -pointer, Get the FW image address. */
} else if (str_cmp_no_case(hdr_args[arg_ind],
"-pointer") == 0) {
arg_ind++;
if ((hdr_args[arg_ind] == NULL) ||
(sscanf(hdr_args[arg_ind],
"%x",
&main_temp) != 1)) {
my_printf(TERR,
"\nCannot read FW Image address !\n");
main_status = FALSE;
} else {
/* Check that the address is 16-bytes aligned */
if ((main_temp & ADDR_16_BYTES_ALIGNED_MASK)
!= 0) {
my_printf(TERR,
"\nFW Image address (0x%08X)"
" isn't 16-bytes aligned !\n",
main_temp);
main_status = FALSE;
}
if (main_temp > MAX_FLASH_SIZE) {
my_printf(TERR,
"\nPointer address (0x%08X) ",
main_temp);
my_printf(TERR,
"is higher from flash size");
my_printf(TERR,
" (0x%08X) !\n",
MAX_FLASH_SIZE);
main_status = FALSE;
} else {
ptr_fw_addr = main_temp;
is_ptr_merge = FALSE;
}
}
}
/* -bhoffset, BootLoader Header Offset (BH location in BT). */
else if (str_cmp_no_case(hdr_args[arg_ind], "-bhoffset") == 0) {
arg_ind++;
main_temp = 0x00;
if (hdr_args[arg_ind] == NULL)
end_ptr = NULL;
else
main_temp = strtol(hdr_args[arg_ind],
&end_ptr, 16);
if (hdr_args[arg_ind] == end_ptr) {
my_printf(TERR, "\nCannot read BootLoader");
my_printf(TERR, " Header Offset !\n");
main_status = FALSE;
} else {
/* Check that the address is 16-bytes aligned */
if ((main_temp & ADDR_16_BYTES_ALIGNED_MASK)
!= 0) {
my_printf(TERR,
"\nFW Image address (0x%08X) ",
main_temp);
my_printf(TERR,
"is not 16-bytes aligned!\n");
}
if (main_temp > MAX_FLASH_SIZE) {
my_printf(TERR,
"\nFW Image address (0x%08X)",
main_temp);
my_printf(TERR,
" is higher from flash size");
my_printf(TERR,
" (0x%08X) !\n",
MAX_FLASH_SIZE);
main_status = FALSE;
} else {
fw_offset = main_temp;
is_ptr_merge = TRUE;
}
}
} else {
my_printf(TERR,
"\nUnknown flag: %s\n",
hdr_args[arg_ind]);
main_status = FALSE;
}
}
/*
* If the input and output file have the same name then exit with error.
*/
if (strcmp(output_file_name, input_file_name) == 0) {
my_printf(TINF,
"Input file name (%s) should be differed from\n",
input_file_name);
my_printf(TINF, "Output file name (%s).\n", output_file_name);
main_status = FALSE;
}
/* No problems reading argv? So go on... */
if (main_status) {
/* if output file already exist, then delete it. */
tmp_file = fopen(output_file_name, "w");
if (tmp_file != NULL)
fclose(tmp_file);
/* If no mode choose than "bt" is the default mode.*/
if (mode_choose == FALSE)
main_fw_hdr_flag = TRUE;
/* Chose manipulation routine according to arguments */
if (main_fw_hdr_flag)
main_status = main_bin(bin_params);
else if (main_api_flag)
main_status = main_api();
else if (main_hdr_flag)
main_status = main_hdr();
else
exit_with_usage();
}
/* Be sure there's no open file before you leave */
if (input_file_pointer)
fclose(input_file_pointer);
if (g_hfd_pointer)
fclose(g_hfd_pointer);
if (api_file_pointer)
fclose(api_file_pointer);
/* Delete temprary header file. */
remove(g_hdr_input_name);
/* Say Bye Bye */
if (main_status) {
my_printf(TPAS, "\n\n******************************");
my_printf(TPAS, "\n*** SUCCESS ***");
my_printf(TPAS, "\n******************************\n");
exit(EXIT_SUCCESS);
} else {
my_printf(TERR, "\n\n******************************");
my_printf(TERR, "\n*** FAILED ***");
my_printf(TERR, "\n******************************\n");
exit(EXIT_FAILURE);
}
}
/*
*-----------------------------------------------------------------------
* Function: exit_with_usage()
* Parameters: none
* Return: none
* Description: No Words...
*-----------------------------------------------------------------------
*/
void exit_with_usage(void)
{
my_printf(TUSG,
"\nECST, Embedded Controller Sign Tool, version %d.%d.%d",
T_VER, T_REV_MAJOR, T_REV_MINOR);
my_printf(TUSG, "\n");
my_printf(TUSG, "\nUsage:");
my_printf(TUSG, "\n ");
my_printf(TUSG, "\n ECST -mode <bt|bh|api> -i <filename> [Flags]");
my_printf(TUSG, "\n ");
my_printf(TUSG, "\nOperation Modes: ");
my_printf(TUSG, "\n bt - BootLoader Table");
my_printf(TUSG, "\n bh - BootLoader Header");
my_printf(TUSG, "\n api - Download from Flash API");
my_printf(TUSG, "\n ");
my_printf(TUSG, "\nCommon flags:");
my_printf(TUSG, "\n -mode <type> - Operation mode: ");
my_printf(TUSG, "bt|bh|api (default is bt)");
my_printf(TUSG, "\n -i <filename> - Input file name; ");
my_printf(TUSG, "must differ from the output file name");
my_printf(TUSG, "\n -o <filename> - Output file name ");
my_printf(TUSG, "(default is out_<input_filename>.bin)");
my_printf(TUSG, "\n -argfile <filename> - Arguments file name; ");
my_printf(TUSG, "includes multiple flags");
my_printf(TUSG, "\n -chip <name> - Supported EC Chip Name: ");
my_printf(TUSG, "%s. ", supported_chips);
my_printf(TUSG, "(default is npcx5m5g)");
my_printf(TUSG, "\n -v - Verbose; prints ");
my_printf(TUSG, "information messages");
my_printf(TUSG, "\n -vv - Super Verbose; prints ");
my_printf(TUSG, "intermediate calculations");
my_printf(TUSG, "\n -h - Show this help screen");
my_printf(TUSG, "\n ");
my_printf(TUSG, "\nBootLoader Table mode flags:");
my_printf(TUSG, "\n -nohcrc - Disable CRC on header ");
my_printf(TUSG, "(default is ON)");
my_printf(TUSG, "\n -nofcrc - Disable CRC on firmware ");
my_printf(TUSG, "(default is ON)");
my_printf(TUSG, "\n -spimaxclk <val> - SPI Flash Maximum Clock, in");
my_printf(TUSG, " MHz: 20|25|33|40|50 (default is 20)");
my_printf(TUSG, "\n -spiclkratio <val> - Core Clock / SPI Flash ");
my_printf(TUSG, "Clocks Ratio: 1 | 2 (default is 1)");
my_printf(TUSG, "\n ");
my_printf(TUSG, "Note: Not relevant for npcx5mng chips family");
my_printf(TUSG, "\n -spireadmode <type> - SPI Flash Read Mode: ");
my_printf(TUSG, "normal|fast|dual|quad (default is normal)");
my_printf(TUSG, "\n -unlimburst - Enable FIU Unlimited ");
my_printf(TUSG, "\n ");
my_printf(TUSG, "Note: Not relevant for npcx5mng chips family");
my_printf(TUSG, "Burst for SPI Flash Accesses (default is disable).");
my_printf(TUSG, "\n -fwloadaddr <addr> - Firmware load start ");
my_printf(TUSG, "address (default is Start-of-RAM)");
my_printf(TUSG, "\n Located in code RAM, ");
my_printf(TUSG, "16-bytes aligned, hex format");
my_printf(TUSG, "\n -usearmrst - Use the ARM reset table ");
my_printf(TUSG, "entry as the Firmware Entry Point");
my_printf(TUSG, "\n Can't be used with -fwep");
my_printf(TUSG, "\n -fwep <addr> - Firmware entry ");
my_printf(TUSG, "point (default is Firmware Entry Point)");
my_printf(TUSG, "\n Located in firmware area,");
my_printf(TUSG, " hex format");
my_printf(TUSG, "\n -crcstart <offset> - Firmware CRC start offset ");
my_printf(TUSG, "(default is 00000000)");
my_printf(TUSG, "\n Offset from firmware image,");
my_printf(TUSG, " 4B-aligned, for partial CRC, hex format");
my_printf(TUSG, "\n -crcsize <val> - Firmware CRC size ");
my_printf(TUSG, "(default is entire firmware size)");
my_printf(TUSG, "\n 4B-aligned, for partial ");
my_printf(TUSG, "CRC, hex format");
my_printf(TUSG, "\n -fwlen <val> - Firmware length, ");
my_printf(TUSG, "16B-aligned, hex format (default is file size).");
my_printf(TUSG, "\n -flashsize <val> - Flash size, in MB: ");
my_printf(TUSG, "1|2|4|8|16 (default is 16)");
my_printf(TUSG, "\n -ph <offset> - Paste the Firmware ");
my_printf(TUSG, "Header in the input file copy at the selected");
my_printf(TUSG, "\n offset ");
my_printf(TUSG, "(default is 00000000), hex format.");
my_printf(TUSG, "\n The firmware itself is ");
my_printf(TUSG, "expected to start at offset + 64 bytes.");
my_printf(TUSG, "\n ");
my_printf(TUSG, "\nBootLoader Header mode flags:");
my_printf(TUSG, "\n -pointer <offset> - BootLoader Table location");
my_printf(TUSG, " in the flash, hex format");
my_printf(TUSG, "\n -bhoffset <offset> - BootLoader Header Offset");
my_printf(TUSG, " in file, hex format (BH location in BT)");
my_printf(TUSG, "\n ");
my_printf(TUSG, "\nAPI mode flags:");
my_printf(TUSG, "\n -apisign <type> - Signature type: ");
my_printf(TUSG, "crc|checksum (default is OFF)");
my_printf(TUSG, "\n\n");
exit(EXIT_FAILURE);
}
/*
*--------------------------------------------------------------------------
* Function: copy_file_to_file()
* Parameters: dst_file_name - Destination file name.
* src_file_name - Source file name.
* offset - number of bytes from the origin.
* origin - From where to seek, START, END, or CURRENT of
* file.
* Return: Number of copied bytes
* Description: Copy the source file to the end of the destination file.
*--------------------------------------------------------------------------
*/
int copy_file_to_file(char *dst_file_name,
char *src_file_name,
int offset,
int origin)
{
int index = 0;
int result = 0;
unsigned char local_val;
int src_file_size;
FILE *dst_file;
FILE *src_file;
/* Open the destination file for append. */
dst_file = fopen(dst_file_name, "r+b");
if (dst_file == NULL) {
/* destination file not exist, create it. */
dst_file = fopen(dst_file_name, "ab");
if (dst_file == NULL)
return 0;
}
/* Open the source file for read. */
src_file = fopen(src_file_name, "rb");
if (src_file == NULL) {
fclose(dst_file);
return 0;
}
/* Get the source file length in bytes. */
src_file_size = get_file_length(src_file);
/* Point to the end of the destination file, and to the start */
/* of the source file. */
if (fseek(dst_file, offset, origin) < 0)
goto out;
if (fseek(src_file, 0, SEEK_SET) < 0)
goto out;
/* Loop over all destination file and write it to the source file.*/
for (index = 0; index < src_file_size; index++) {
/* Read byte from source file. */
result = (int)(fread(&local_val, 1, 1, src_file));
/* If byte reading pass than write it to the destination, */
/* else exit from the reading loop. */
if (result) {
/* Read pass, so write it to destination file.*/
result = fwrite(&local_val, 1, 1, dst_file);
if (!result)
/*
* Write failed,
* return with the copied bytes number.
*/
break;
} else
/* Read failed, return with the copied bytes number. */
break;
}
out:
/* Close the files. */
fclose(dst_file);
fclose(src_file);
/* Copy ended, return with the number of bytes that were copied. */
return index;
}
/*
*--------------------------------------------------------------------------
* Function: my_printf()
* Parameters: as in printf + error level
* Return: none
* Description: No Words...
*--------------------------------------------------------------------------
*/
void my_printf(int error_level, char *fmt, ...)
{
va_list argptr;
if ((g_verbose == NO_VERBOSE) && (error_level == TINF))
return;
if ((g_verbose != SUPER_VERBOSE) && (error_level == TDBG))
return;
va_start(argptr, fmt);
vprintf(fmt, argptr);
va_end(argptr);
}
/*
*--------------------------------------------------------------------------
* Function: write_to_file
* Parameters: TBD
* Return: TRUE on successful write
* Description: Writes to ELF or BIN files - whatever is open
*--------------------------------------------------------------------------
*/
int write_to_file(unsigned int write_value,
unsigned int offset,
unsigned char num_of_bytes,
char *print_string)
{
int result = 0;
int index;
unsigned int localValue4;
unsigned short localValue2;
unsigned char localValue1;
if (fseek(g_hfd_pointer, offset, SEEK_SET) < 0)
return FALSE;
switch (num_of_bytes) {
case(1):
localValue1 = (unsigned char)write_value;
result = (int)(fwrite(&localValue1, 1,
1, g_hfd_pointer));
break;
case(2):
localValue2 = (unsigned short)write_value;
result = (int)(fwrite(&localValue2,
2,
1,
g_hfd_pointer));
break;
case(4):
localValue4 = write_value;
result = (int)(fwrite(&localValue4,
4,
1,
g_hfd_pointer));
break;
default:
/* Pad the same value N times. */
localValue1 = (unsigned char)write_value;
for (index = 0; index < num_of_bytes; index++)
result = (int)(fwrite(&localValue1,
1,
1,
g_hfd_pointer));
break;
}
my_printf(TINF, "\nIn write_to_file - %s", print_string);
if (result) {
my_printf(TINF,
" - Offset %2d - value 0x%x",
offset, write_value);
} else {
my_printf(TERR,
"\n\nCouldn't write %x to file at %x\n\n",
write_value, offset);
return FALSE;
}
return TRUE;
}
/*
*--------------------------------------------------------------------------
* Function: read_from_file
* Parameters: TBD
* Return: TRUE on successful read
* Description : Reads from open BIN file
*--------------------------------------------------------------------------
*/
int read_from_file(unsigned int offset,
unsigned char size_to_read,
unsigned int *read_value,
char *print_string)
{
int result;
unsigned int localValue4;
unsigned short localValue2;
unsigned char localValue1;
if (fseek(input_file_pointer, offset, SEEK_SET) < 0)
return FALSE;
switch (size_to_read) {
case(1):
result = (int)(fread(&localValue1,
1,
1,
input_file_pointer));
*read_value = localValue1;
break;
case(2):
result = (int)(fread(&localValue2,
2,
1,
input_file_pointer));
*read_value = localValue2;
break;
case(4):
result = (int)(fread(&localValue4,
4,
1,
input_file_pointer));
*read_value = localValue4;
break;
default:
my_printf(TERR, "\nIn read_from_file - %s", print_string);
my_printf(TERR, "\n\nInvalid call to read_from_file\n\n");
return FALSE;
}
my_printf(TINF, "\nIn read_from_file - %s", print_string);
if (result) {
my_printf(TINF,
" - Offset %d - value %x",
offset, *read_value);
} else {
my_printf(TERR,
"\n\nCouldn't read from file at %x\n\n",
offset);
return FALSE;
}
return TRUE;
}
/*
*--------------------------------------------------------------------------
* Function: init_calculation
* Parameters: unsigned int check_sum_crc (I\O)
* Return:
* Description: Initialize the variable according to the selected
* calculation
*--------------------------------------------------------------------------
*/
void init_calculation(unsigned int *check_sum_crc)
{
switch (g_calc_type) {
case CALC_TYPE_NONE:
case CALC_TYPE_CHECKSUM:
*check_sum_crc = 0;
break;
case CALC_TYPE_CRC:
*check_sum_crc = initialize_crc_32();
break;
}
}
/*
*--------------------------------------------------------------------------
* Function: finalize_calculation
* Parameters: unsigned int check_sum_crc (I\O)
* Return:
* Description: Finalize the variable according to the selected calculation
*--------------------------------------------------------------------------
*/
void finalize_calculation(unsigned int *check_sum_crc)
{
switch (g_calc_type) {
case CALC_TYPE_NONE:
case CALC_TYPE_CHECKSUM:
/* Do nothing */
break;
case CALC_TYPE_CRC:
*check_sum_crc = finalize_crc_32(*check_sum_crc);
break;
}
}
/*--------------------------------------------------------------------------
* Function: update_calculation
* Parameters: unsigned int check_sum_crc (I\O)
* unsigned int byte_to_add (I)
* Return:
* Description: Calculate a new checksum\crc with the new byte_to_add
* given the previous checksum\crc
*--------------------------------------------------------------------------
*/
void update_calculation(unsigned int *check_sum_crc,
unsigned char byte_to_add)
{
switch (g_calc_type) {
case CALC_TYPE_NONE:
/* Do nothing */
break;
case CALC_TYPE_CHECKSUM:
*check_sum_crc += byte_to_add;
break;
case CALC_TYPE_CRC:
*check_sum_crc = update_crc_32(*check_sum_crc, byte_to_add);
break;
}
}
/*
*--------------------------------------------------------------------------
* Function: str_cmp_no_case
* Parameters: s1, s2: Strings to compare.
* Return: function returns an integer less than, equal to, or
* greater than zero if s1 (or the first n bytes thereof) is
* found, respectively, to be less than, to match, or be
* greater than s2.
* Description: Compare two string without case sensitive.
*--------------------------------------------------------------------------
*/
int str_cmp_no_case(const char *s1, const char *s2)
{
return strcasecmp(s1, s2);
}
/*
*--------------------------------------------------------------------------
* Function: get_file_length
* Parameters: stream - Pointer to a FILE object
* Return: File length in bytes or -1 on error
* Description: Gets the file length in bytes.
*--------------------------------------------------------------------------
*/
int get_file_length(FILE *stream)
{
int current_position;
int file_len;
/* Store current position. */
current_position = ftell(stream);
if (current_position < 0)
return -1;
/* End position of the file is its length. */
if (fseek(stream, 0, SEEK_END) < 0)
return -1;
file_len = ftell(stream);
/* Restore the original position. */
if (fseek(stream, current_position, SEEK_SET) < 0)
return -1;
/* return file length. */
return file_len;
}
/*
***************************************************************************
* "bt" mode Handler
***************************************************************************
*/
/*
***************************************************************************
* Function: main_bin
* Parameters: TBD
* Return: True for success
* Description:
* TBD
***************************************************************************
*/
int main_bin(struct tbinparams binary_params)
{
unsigned int bin_file_size_bytes;
unsigned int bin_fw_offset = 0;
unsigned int tmp_param;
FILE *output_file_pointer;
/* If input file was not declared, then print error message. */
if (strlen(input_file_name) == 0) {
my_printf(TERR, "\n\nDefine input file, using -i flag\n\n");
return FALSE;
}
/* Open input file */
input_file_pointer = fopen(input_file_name, "r+b");
if (input_file_pointer == NULL) {
my_printf(TERR, "\n\nCannot open %s\n\n", input_file_name);
return FALSE;
}
/*
* Check Binary file size, this file contain the image itself,
* without any header.
*/
bin_file_size_bytes = get_file_length(input_file_pointer);
if (bin_file_size_bytes == 0) {
my_printf(TINF,
"\nBIN Input file name %s is empty (size is %d)\n",
input_file_name, bin_file_size_bytes);
return FALSE;
}
/*
* If the binary file contains also place for the header, then the FW
* size is the length of the file minus the header length
*/
if ((binary_params.bin_params & BIN_FW_HDR_OFFSET) != 0)
bin_fw_offset = binary_params.fw_hdr_offset + HEADER_SIZE;
my_printf(TINF, "\nBIN file: %s, size: %d (0x%x) bytes\n",
input_file_name,
bin_file_size_bytes,
bin_file_size_bytes);
/* Check validity of FW header offset. */
if (((int)binary_params.fw_hdr_offset < 0) ||
(binary_params.fw_hdr_offset > bin_file_size_bytes)) {
my_printf(TERR,
"\nFW header offset 0x%08x (%d) should be in the"
" range of 0 and file size (%d).\n",
binary_params.fw_hdr_offset,
binary_params.fw_hdr_offset,
bin_file_size_bytes);
return FALSE;
}
/* Create the header file in the same directory as the input file. */
if (!splice_into_path(g_hdr_input_name, input_file_name,
sizeof(g_hdr_input_name), "hdr_"))
return FALSE;
g_hfd_pointer = fopen(g_hdr_input_name, "w+b");
if (g_hfd_pointer == NULL) {
my_printf(TERR, "\n\nCannot open %s\n\n", g_hdr_input_name);
return FALSE;
}
if (strlen(output_file_name) == 0) {
if (!splice_into_path(output_file_name, input_file_name,
sizeof(output_file_name), "out_"))
return FALSE;
}
my_printf(TINF, "Output file name: %s\n", output_file_name);
/*
*********************************************************************
* Set the ANCHOR & Extended-ANCHOR
*********************************************************************
*/
/* Write the ancore. */
if (!write_to_file(FW_HDR_ANCHOR,
HDR_ANCHOR_OFFSET,
4,
"HDR - FW Header ANCHOR "))
return FALSE;
/* Write the extended anchor. */
if (binary_params.bin_params & BIN_FW_HDR_CRC_DISABLE) {
/* Write the ancore and the extended anchor. */
if (!write_to_file(FW_HDR_EXT_ANCHOR_DISABLE,
HDR_EXTENDED_ANCHOR_OFFSET, 2,
"HDR - Header EXTENDED ANCHOR "))
return FALSE;
} else {
/* Write the anchor and the extended anchor. */
if (!write_to_file(FW_HDR_EXT_ANCHOR_ENABLE,
HDR_EXTENDED_ANCHOR_OFFSET, 2,
"HDR - Header EXTENDED ANCHOR "))
return FALSE;
}
/* Write the SPI flash MAX clock. */
switch (binary_params.spi_max_clk) {
case SPI_MAX_CLOCK_20_MHZ_VAL:
tmp_param = SPI_MAX_CLOCK_20_MHZ;
break;
case SPI_MAX_CLOCK_25_MHZ_VAL:
tmp_param = SPI_MAX_CLOCK_25_MHZ;
break;
case SPI_MAX_CLOCK_33_MHZ_VAL:
tmp_param = SPI_MAX_CLOCK_33_MHZ;
break;
case SPI_MAX_CLOCK_40_MHZ_VAL:
tmp_param = SPI_MAX_CLOCK_40_MHZ;
break;
case SPI_MAX_CLOCK_50_MHZ_VAL:
tmp_param = SPI_MAX_CLOCK_50_MHZ;
break;
default:
my_printf(TERR, "\n\nInvalid SPI Flash MAX clock (%d MHz) ",
binary_params.spi_max_clk);
my_printf(TERR, "- it should be 20, 25, 33, 40 or 50 MHz");
return FALSE;
}
/* If SPI clock ratio set for MRIDER15, then it is error. */
if ((binary_params.spi_clk_ratio != 0x00) && (is_mrider15 == TRUE)) {
my_printf(TERR, "\nspiclkratio is not relevant for");
my_printf(TERR, " npcx5mng chips family !\n");
return FALSE;
}
/*
* In case SPIU clock ratio didn't set by the user,
* set it to its default value.
*/
if (binary_params.spi_clk_ratio == 0x00)
binary_params.spi_clk_ratio = SPI_CLOCK_RATIO_1_VAL;
switch (binary_params.spi_clk_ratio) {
case SPI_CLOCK_RATIO_1_VAL:
tmp_param &= SPI_CLOCK_RATIO_1;
break;
case SPI_CLOCK_RATIO_2_VAL:
tmp_param |= SPI_CLOCK_RATIO_2;
break;
default:
my_printf(TERR, "\n\nInvalid SPI Core Clock Ratio (%d) ",
binary_params.spi_clk_ratio);
my_printf(TERR, "- it should be 1 or 2");
return FALSE;
}
if (!write_to_file(tmp_param, HDR_SPI_MAX_CLK_OFFSET, 1,
"HDR - SPI flash MAX Clock "))
return FALSE;
/* Write the SPI flash Read Mode. */
tmp_param = binary_params.spi_read_mode;
/* If needed, set the unlimited burst bit. */
if (binary_params.bin_params & BIN_UNLIM_BURST_ENABLE) {
if (is_mrider15 == TRUE) {
my_printf(TERR, "\nunlimburst is not relevant for");
my_printf(TERR, " npcx5mng chips family !\n");
return FALSE;
}
tmp_param |= SPI_UNLIMITED_BURST_ENABLE;
}
if (!write_to_file(tmp_param,
HDR_SPI_READ_MODE_OFFSET, 1,
"HDR - SPI flash Read Mode "))
return FALSE;
/* Write the error detection configuration. */
if (binary_params.bin_params & BIN_FW_CRC_DISABLE) {
if (!write_to_file(FW_CRC_DISABLE,
HDR_ERR_DETECTION_CONF_OFFSET,
1,
"HDR - FW CRC Disabled "))
return FALSE;
} else {
/* Write the ancore and the extended anchor. */
if (!write_to_file(FW_CRC_ENABLE,
HDR_ERR_DETECTION_CONF_OFFSET, 1,
"HDR - FW CRC Enabled "))
return FALSE;
}
/* FW entry point should be between the FW load address and RAM size */
if ((binary_params.fw_load_addr >
(g_ram_start_address + g_ram_size)) ||
(binary_params.fw_load_addr < g_ram_start_address)) {
my_printf(TERR,
"\nFW load address (0x%08x) should be between ",
binary_params.fw_load_addr);
my_printf(TERR,
"start (0x%08x) and end (0x%08x) of RAM ).",
g_ram_start_address,
(g_ram_start_address + g_ram_size));
return FALSE;
}
/* Write the FW load start address */
if (!write_to_file(binary_params.fw_load_addr,
HDR_FW_LOAD_START_ADDR_OFFSET, 4,
"HDR - FW load start address "))
return FALSE;
/*
* Write the FW length. (MUST BE SET BEFORE fw_err_detec_e_addr)
*/
if ((binary_params.bin_params & BIN_FW_LENGTH) == 0x00000000) {
/*
* In case the FW length was not set, then the FW length is the
* size of the binary file minus the offset of the start of the
* FW.
*/
binary_params.fw_len = bin_file_size_bytes-bin_fw_offset;
}
if ((int)binary_params.fw_len < 0) {
my_printf(TERR,
"\nFW length %d (0x%08x) should be greater than 0x0.",
binary_params.fw_len,
binary_params.fw_len);
return FALSE;
}
if (((int)binary_params.fw_len >
(bin_file_size_bytes - bin_fw_offset)) ||
((int)binary_params.fw_len > g_ram_size)) {
my_printf(TERR,
"\nFW length %d (0x%08x) should be within the",
binary_params.fw_len, binary_params.fw_len);
my_printf(TERR,
" input-file (related to the FW offset)");
my_printf(TERR,
"\n (0x%08x) and within the RAM (RAM size: 0x%08x).",
(bin_file_size_bytes - bin_fw_offset), g_ram_size);
return FALSE;
}
if ((binary_params.bin_params & BIN_FW_USER_ARM_RESET) != 0x00000000) {
read_from_file((bin_fw_offset + ARM_FW_ENTRY_POINT_OFFSET),
4,
&binary_params.fw_ep,
"read FW entry point for FW image ");
if ((binary_params.fw_ep <
binary_params.fw_load_addr) ||
(binary_params.fw_ep >
(binary_params.fw_load_addr +
binary_params.fw_len))) {
my_printf(TERR,
"\nFW entry point (0x%08x) should be between",
binary_params.fw_ep);
my_printf(TERR,
" the FW load address (0x%08x) ",
binary_params.fw_load_addr);
my_printf(TERR,
"and FW length (0x%08x).\n",
(binary_params.fw_load_addr +
binary_params.fw_len));
return FALSE;
}
}
/* FW entry point should be between the FW load address and RAM size */
if ((binary_params.fw_ep <
binary_params.fw_load_addr) ||
(binary_params.fw_ep >
(binary_params.fw_load_addr +
binary_params.fw_len))) {
if (((binary_params.bin_params & BIN_FW_ENTRY_POINT) ==
0x00000000) &&
((binary_params.bin_params &
BIN_FW_LOAD_START_ADDR) != 0x00000000)) {
binary_params.fw_ep =
binary_params.fw_load_addr;
} else {
my_printf(TERR,
"\nFW entry point (0x%08x) should be ",
binary_params.fw_ep);
my_printf(TERR,
"\between the FW load address (0x%08x)",
binary_params.fw_load_addr);
my_printf(TERR,
" and FW length (0x%08x).\n",
(binary_params.fw_load_addr +
binary_params.fw_len));
return FALSE;
}
}
/* Write the FW entry point */
if (!write_to_file(binary_params.fw_ep,
HDR_FW_ENTRY_POINT_OFFSET,
4,
"HDR - FW Entry point "))
return FALSE;
/* Calculate the CRC end address. */
if ((binary_params.bin_params & BIN_FW_CKS_SIZE) == 0x00000000) {
/*
* In case the size was not set, then CRC end address is
* the size of the binary file.
*/
binary_params.fw_err_detec_e_addr =
binary_params.fw_len - 1;
} else {
/* CRC end address should be less than FW length. */
if (binary_params.fw_err_detec_e_addr >
(binary_params.fw_len - 1)) {
my_printf(TERR,
"\nCRC end address (0x%08x) should be less ",
binary_params.fw_err_detec_e_addr);
my_printf(TERR,
"than the FW length %d (0x%08x)",
(binary_params.fw_len),
(binary_params.fw_len));
return FALSE;
}
}
/* Check CRC start and end addresses. */
if (binary_params.fw_err_detec_s_addr >
binary_params.fw_err_detec_e_addr) {
my_printf(TERR,
"\nCRC start address (0x%08x) should be less or ",
binary_params.fw_err_detec_s_addr);
my_printf(TERR,
"equal to CRC end address (0x%08x)\nPlease check ",
binary_params.fw_err_detec_e_addr);
my_printf(TERR,
"CRC start address and CRC size arguments.");
return FALSE;
}
/* CRC start addr should be between the FW load address and RAM size */
if (binary_params.fw_err_detec_s_addr >
binary_params.fw_len) {
my_printf(TERR, "\nCRC start address (0x%08x) should ",
binary_params.fw_err_detec_s_addr);
my_printf(TERR, "be FW length (0x%08x).",
binary_params.fw_len);
return FALSE;
}
/* Write the CRC start address */
if (!write_to_file(binary_params.fw_err_detec_s_addr,
HDR_FW_ERR_DETECT_START_ADDR_OFFSET,
4,
"HDR - FW CRC Start "))
return FALSE;
/* CRC end addr should be between the CRC start address and RAM size */
if ((binary_params.fw_err_detec_e_addr <
binary_params.fw_err_detec_s_addr) ||
(binary_params.fw_err_detec_e_addr >
binary_params.fw_len)) {
my_printf(TERR,
"\nCRC end address (0x%08x) should be between the ",
binary_params.fw_err_detec_e_addr);
my_printf(TERR,
"CRC start address (0x%08x) and FW length (0x%08x).",
binary_params.fw_err_detec_s_addr,
binary_params.fw_len);
return FALSE;
}
/* Write the CRC end address */
if (!write_to_file(binary_params.fw_err_detec_e_addr,
HDR_FW_ERR_DETECT_END_ADDR_OFFSET,
4,
"HDR - FW CRC End "))
return FALSE;
/* Let the FW length to be aligned to 16 */
tmp_param = binary_params.fw_len % 16;
if (tmp_param)
binary_params.fw_len += (16 - tmp_param);
/* FW load address + FW length should be less than the RAM size. */
if ((binary_params.fw_load_addr +
binary_params.fw_len) >
(g_ram_start_address + g_ram_size)) {
my_printf(TERR,
"\nFW load address + FW length should (0x%08x) be ",
(binary_params.fw_load_addr + binary_params.fw_len));
my_printf(TERR,
"less than the RAM size (0x%08x).",
(g_ram_start_address + g_ram_size));
return FALSE;
}
/* Write the FW length */
if (!write_to_file(binary_params.fw_len,
HDR_FW_LENGTH_OFFSET,
4,
"HDR - FW Length "))
return FALSE;
/* Write the SPI flash MAX clock. */
switch (binary_params.flash_size) {
case FLASH_SIZE_1_MBYTES_VAL:
tmp_param = FLASH_SIZE_1_MBYTES;
break;
case FLASH_SIZE_2_MBYTES_VAL:
tmp_param = FLASH_SIZE_2_MBYTES;
break;
case FLASH_SIZE_4_MBYTES_VAL:
tmp_param = FLASH_SIZE_4_MBYTES;
break;
case FLASH_SIZE_8_MBYTES_VAL:
tmp_param = FLASH_SIZE_8_MBYTES;
break;
case FLASH_SIZE_16_MBYTES_VAL:
tmp_param = FLASH_SIZE_16_MBYTES;
break;
default:
my_printf(TERR, "\n\nInvalid Flash size (%d MBytes) -",
binary_params.flash_size);
my_printf(TERR, " it should be 1, 2, 4, 8 or 16 MBytes\n");
return FALSE;
}
if (!write_to_file(tmp_param,
HDR_FLASH_SIZE_OFFSET,
1,
"HDR - Flash size "))
return FALSE;
/* Write the reserved bytes. */
if (!write_to_file(PAD_VALUE, HDR_RESERVED, 26,
"HDR - Reserved (26 bytes) "))
return FALSE;
/* Refresh the FW header bin file in order to calculate CRC */
if (g_hfd_pointer) {
fclose(g_hfd_pointer);
g_hfd_pointer = fopen(g_hdr_input_name, "r+b");
if (g_hfd_pointer == NULL) {
my_printf(TERR,
"\n\nCannot open %s\n\n",
input_file_name);
return FALSE;
}
}
/* Calculate the FW header CRC */
if ((binary_params.bin_params & BIN_FW_HDR_CRC_DISABLE) == 0) {
/* Calculate ... */
g_calc_type = CALC_TYPE_CRC;
if (!calc_header_crc_bin(&binary_params.hdr_crc))
return FALSE;
g_calc_type = CALC_TYPE_NONE;
} else
binary_params.hdr_crc = 0;
/* Write FW header CRC to header file */
if (!write_to_file(binary_params.hdr_crc,
HDR_FW_HEADER_SIG_OFFSET,
4,
"HDR - Header CRC "))
return FALSE;
/* Calculate the FW CRC */
if ((binary_params.bin_params & BIN_FW_CRC_DISABLE) == 0) {
/* Calculate ... */
g_calc_type = CALC_TYPE_CRC;
if (!calc_firmware_csum_bin(&binary_params.fw_crc,
(bin_fw_offset +
binary_params.fw_err_detec_s_addr),
(binary_params.fw_err_detec_e_addr -
binary_params.fw_err_detec_s_addr+1)))
return FALSE;
g_calc_type = CALC_TYPE_NONE;
} else
binary_params.fw_crc = 0;
/* Write the FW CRC into file header file */
if (!write_to_file(binary_params.fw_crc,
HDR_FW_IMAGE_SIG_OFFSET,
4,
"HDR - FW CRC "))
return FALSE;
/* Close if needed... */
if (input_file_pointer) {
fclose(input_file_pointer);
input_file_pointer = NULL;
}
if (g_hfd_pointer) {
fclose(g_hfd_pointer);
g_hfd_pointer = NULL;
}
/* Create empty output file. */
output_file_pointer = fopen(output_file_name, "wb");
if (output_file_pointer)
fclose(output_file_pointer);
if ((binary_params.bin_params & BIN_FW_HDR_OFFSET) != 0) {
copy_file_to_file(output_file_name,
input_file_name,
0,
SEEK_SET);
copy_file_to_file(output_file_name,
g_hdr_input_name,
binary_params.fw_hdr_offset,
SEEK_SET);
} else {
copy_file_to_file(output_file_name,
g_hdr_input_name,
0,
SEEK_END);
copy_file_to_file(output_file_name,
input_file_name,
0,
SEEK_END);
}
my_printf(TINF, "\n\n");
return TRUE;
}
/*******************************************************************
* Function: calc_header_crc_bin
* Parameters: unsigned short header checksum (O)
* unsigned int header offset from first byte in
* the binary (I)
* Return: TRUE if successful
* Description: Go thru bin file and calculate checksum
*******************************************************************
*/
int calc_header_crc_bin(unsigned int *p_cksum)
{
int i;
unsigned int calc_header_checksum_crc = 0;
unsigned char g_header_array[HEADER_SIZE];
int line_print_size = 32;
init_calculation(&calc_header_checksum_crc);
/* Go thru the BIN File and calculate the Checksum */
if (fseek(g_hfd_pointer, 0x00000000, SEEK_SET) < 0)
return FALSE;
if (fread(g_header_array,
HEADER_SIZE,
1,
g_hfd_pointer) != 1)
return FALSE;
for (i = 0; i < (HEADER_SIZE - HEADER_CRC_FIELDS_SIZE); i++) {
/*
* I had once the Verbose check inside the my_printf, but
* it made ECST run sloooowwwwwly....
*/
if (g_verbose == SUPER_VERBOSE) {
if (i%line_print_size == 0)
my_printf(TDBG, "\n[%.4x]: ", i);
my_printf(TDBG, "%.2x ", g_header_array[i]);
}
update_calculation(&calc_header_checksum_crc,
g_header_array[i]);
if (g_verbose == SUPER_VERBOSE) {
if ((i + 1) % line_print_size == 0)
my_printf(TDBG,
"FW Header ChecksumCRC = %.8x",
calc_header_checksum_crc);
}
}
finalize_calculation(&calc_header_checksum_crc);
*p_cksum = calc_header_checksum_crc;
return TRUE;
}
/*
*******************************************************************
* Function: calc_firmware_csum_bin
* Parameters: unsigned int fwStart (I)
* unsigned int firmware size in words (I)
* unsigned int - firmware checksum (O)
* Return:
* Description: TBD
*******************************************************************
*/
int calc_firmware_csum_bin(unsigned int *p_cksum,
unsigned int fw_offset,
unsigned int fw_length)
{
unsigned int i;
unsigned int calc_read_bytes;
unsigned int calc_num_of_bytes_to_read;
unsigned int calc_curr_position;
unsigned int calc_fw_checksum_crc = 0;
unsigned char g_fw_array[BUFF_SIZE];
int line_print_size = 32;
calc_num_of_bytes_to_read = fw_length;
calc_curr_position = fw_offset;
if (g_verbose == REGULAR_VERBOSE) {
my_printf(TINF,
"\nFW Error Detect Start Dddress: 0x%08x",
calc_curr_position);
my_printf(TINF,
"\nFW Error Detect End Dddress: 0x%08x",
calc_curr_position + calc_num_of_bytes_to_read - 1);
my_printf(TINF,
"\nFW Error Detect Size: %d (0x%X)",
calc_num_of_bytes_to_read,
calc_num_of_bytes_to_read);
}
init_calculation(&calc_fw_checksum_crc);
while (calc_num_of_bytes_to_read > 0) {
if (calc_num_of_bytes_to_read > BUFF_SIZE)
calc_read_bytes = BUFF_SIZE;
else
calc_read_bytes = calc_num_of_bytes_to_read;
if (fseek(input_file_pointer,
calc_curr_position, SEEK_SET) < 0)
return 0;
if (fread(g_fw_array,
calc_read_bytes,
1,
input_file_pointer) != 1)
return 0;
for (i = 0; i < calc_read_bytes; i++) {
/*
* I had once the Verbose check inside the my_printf,
* but it made ECST run sloooowwwwwly....
*/
if (g_verbose == SUPER_VERBOSE) {
if (i%line_print_size == 0)
my_printf(TDBG,
"\n[%.4x]: ",
calc_curr_position + i);
my_printf(TDBG, "%.2x ", g_fw_array[i]);
}
update_calculation(&calc_fw_checksum_crc,
g_fw_array[i]);
if (g_verbose == SUPER_VERBOSE) {
if ((i + 1) % line_print_size == 0)
my_printf(TDBG,
"FW Checksum= %.8x",
calc_fw_checksum_crc);
}
}
calc_num_of_bytes_to_read -= calc_read_bytes;
calc_curr_position += calc_read_bytes;
} /* end of while(calc_num_of_bytes_to_read > 0) */
finalize_calculation(&calc_fw_checksum_crc);
*p_cksum = calc_fw_checksum_crc;
return TRUE;
}
/*
***************************************************************************
* "bh" mode Handler
***************************************************************************
*/
/*
*******************************************************************
* Function: main_hdr
* Parameters: TBD
* Return: True for success
* Description:
*******************************************************************
*/
int main_hdr(void)
{
int result = 0;
char tmp_file_name[NAME_SIZE + 1];
unsigned int tmp_long_val;
unsigned int bin_file_size_bytes;
tmp_file_name[NAME_SIZE] = '\0';
if (is_ptr_merge) {
if (strlen(input_file_name) == 0) {
my_printf(TERR, "\n\nNo input BIN file selected for");
my_printf(TERR, " BootLoader header file.\n\n");
return FALSE;
}
if (strlen(output_file_name) == 0)
strncpy(tmp_file_name,
input_file_name,
sizeof(tmp_file_name) - 1);
else {
copy_file_to_file(output_file_name,
input_file_name,
0,
SEEK_END);
strncpy(tmp_file_name,
output_file_name,
sizeof(tmp_file_name) - 1);
}
/* Open Header file */
g_hdr_pointer = fopen(tmp_file_name, "r+b");
if (g_hdr_pointer == NULL) {
my_printf(TERR,
"\n\nCannot open %s file.\n\n",
tmp_file_name);
return FALSE;
}
bin_file_size_bytes = get_file_length(g_hdr_pointer);
/* Offset should be less than file size. */
if (fw_offset > bin_file_size_bytes) {
my_printf(TERR,
"\n\nFW offset 0x%08x should be less than ",
fw_offset);
my_printf(TERR,
"file size 0x%x (%d).\n\n",
bin_file_size_bytes, bin_file_size_bytes);
return FALSE;
}
/* FW table should be less than file size. */
if (ptr_fw_addr > bin_file_size_bytes) {
my_printf(TERR, "\n\nFW table 0x%08x should be less ",
ptr_fw_addr);
my_printf(TERR, "than file size 0x%x (%d).\n\n",
bin_file_size_bytes, bin_file_size_bytes);
return FALSE;
}
if (fseek(g_hdr_pointer, fw_offset, SEEK_SET) < 0)
return FALSE;
tmp_long_val = HDR_PTR_SIGNATURE;
result = (int)(fwrite(&tmp_long_val,
4,
1,
g_hdr_pointer));
result |= (int)(fwrite(&ptr_fw_addr,
4,
1,
g_hdr_pointer));
if (result) {
my_printf(TINF,
"\nBootLoader Header file: %s\n",
tmp_file_name);
my_printf(TINF,
" Offset: 0x%08X, Signature: 0x%08X,",
fw_offset, HDR_PTR_SIGNATURE);
my_printf(TINF,
" Pointer: 0x%08X\n",
ptr_fw_addr);
} else {
my_printf(TERR,
"\n\nCouldn't write signature (%x) and "
"pointer to BootLoader header file (%s)\n\n",
tmp_long_val, tmp_file_name);
return FALSE;
}
} else {
if (strlen(output_file_name) == 0) {
my_printf(TERR, "\n\nNo output file selected ");
my_printf(TERR, "for BootLoader header file.\n\n");
return FALSE;
}
/* Open Output file */
g_hdr_pointer = fopen(output_file_name, "w+b");
if (g_hdr_pointer == NULL) {
my_printf(TERR,
"\n\nCannot open %s file.\n\n",
output_file_name);
return FALSE;
}
if (fseek(g_hdr_pointer, 0L, SEEK_SET) < 0)
return FALSE;
tmp_long_val = HDR_PTR_SIGNATURE;
result = (int)(fwrite(&tmp_long_val,
4,
1,
g_hdr_pointer));
result |= (int)(fwrite(&ptr_fw_addr,
4,
1,
g_hdr_pointer));
if (result) {
my_printf(TINF,
"\nBootLoader Header file: %s\n",
output_file_name);
my_printf(TINF,
" Signature: 0x%08X, Pointer: 0x%08X\n",
HDR_PTR_SIGNATURE,
ptr_fw_addr);
} else {
my_printf(TERR,
"\n\nCouldn't write signature (%x) and ",
tmp_long_val);
my_printf(TERR,
"pointer to BootLoader header file (%s)\n\n",
output_file_name);
return FALSE;
}
}
/* Close if needed... */
if (g_hdr_pointer) {
fclose(g_hdr_pointer);
g_hdr_pointer = NULL;
}
return TRUE;
}
/*
***************************************************************************
* "api" mode Handler
***************************************************************************
*/
/*
*******************************************************************
* Function: main_api
* Parameters: TBD
* Return: True for success
* Description:
* TBD
*******************************************************************
*/
int main_api(void)
{
char tmp_file_name[NAME_SIZE + 1];
int result = 0;
unsigned int crc_checksum;
tmp_file_name[NAME_SIZE] = '\0';
api_file_size_bytes = 0;
/* If API input file was not declared, then print error message. */
if (strlen(input_file_name) == 0) {
my_printf(TERR,
"\n\nNeed to define API input file, using -i flag\n\n");
return FALSE;
}
if (strlen(output_file_name) == 0) {
if (!splice_into_path(tmp_file_name, input_file_name,
sizeof(tmp_file_name), "api_"))
return FALSE;
} else
strncpy(tmp_file_name, output_file_name,
sizeof(tmp_file_name) - 1);
/* Make sure that new empty file is created. */
api_file_pointer = fopen(tmp_file_name, "w");
if (api_file_pointer == NULL) {
my_printf(TERR, "\n\nCannot open %s\n\n", tmp_file_name);
return FALSE;
}
fclose(api_file_pointer);
copy_file_to_file(tmp_file_name, input_file_name, 0, SEEK_END);
/* Open API input file */
api_file_pointer = fopen(tmp_file_name, "r+b");
if (api_file_pointer == NULL) {
my_printf(TERR, "\n\nCannot open %s\n\n", tmp_file_name);
return FALSE;
}
/*
* Check Binary file size, this file contain the image itself,
* without any header.
*/
api_file_size_bytes = get_file_length(api_file_pointer);
if (api_file_size_bytes < 0)
return FALSE;
my_printf(TINF,
"\nAPI file: %s, size: %d bytes (0x%x)\n",
tmp_file_name,
api_file_size_bytes,
api_file_size_bytes);
crc_checksum = calc_api_csum_bin();
if (fseek(api_file_pointer, api_file_size_bytes, SEEK_SET) < 0)
return FALSE;
result = (int)(fwrite(&crc_checksum,
4,
1,
api_file_pointer));
if (result)
my_printf(TINF,
"\nIn API BIN file - Offset 0x%08X - value 0x%08X",
api_file_size_bytes,
crc_checksum);
else {
my_printf(TERR,
"\n\nCouldn't write %x to API BIN file at %08x\n\n",
crc_checksum, api_file_size_bytes);
return FALSE;
}
/* Close if needed... */
if (api_file_pointer) {
fclose(api_file_pointer);
api_file_pointer = NULL;
}
return TRUE;
}
/*
*******************************************************************
* Function: calc_api_csum_bin
* Parameters:
*
* Return: Return the CRC \ checksum, or "0" in case of fail.
* Description: TBD
*******************************************************************
*/
unsigned int calc_api_csum_bin(void)
{
unsigned int i;
unsigned int calc_read_bytes;
int calc_num_of_bytes_to_read;
unsigned int calc_curr_position;
unsigned int calc_fw_checksum_crc = 0;
unsigned char g_fw_array[BUFF_SIZE];
int line_print_size = 32;
calc_num_of_bytes_to_read = api_file_size_bytes;
calc_curr_position = 0;
if (g_verbose == SUPER_VERBOSE) {
my_printf(TDBG,
"\nAPI CRC \\ Checksum First Byte Address: 0x%08x",
calc_curr_position);
my_printf(TDBG,
"\nAPI CRC \\ Checksum Size: %d (0x%X)",
calc_num_of_bytes_to_read,
calc_num_of_bytes_to_read);
}
init_calculation(&calc_fw_checksum_crc);
while (calc_num_of_bytes_to_read > 0) {
if (calc_num_of_bytes_to_read > BUFF_SIZE)
calc_read_bytes = BUFF_SIZE;
else
calc_read_bytes = calc_num_of_bytes_to_read;
if (fseek(api_file_pointer,
calc_curr_position, SEEK_SET) < 0)
return 0;
if (fread(g_fw_array,
calc_read_bytes,
1,
api_file_pointer) != 1)
return 0;
for (i = 0; i < calc_read_bytes; i++) {
/*
* I had once the Verbose check inside the my_printf,
* but it made ecst run sloooowwwwwly....
*/
if (g_verbose == SUPER_VERBOSE) {
if (i%line_print_size == 0)
my_printf(TDBG,
"\n[%.4x]: ",
calc_curr_position + i);
my_printf(TDBG, "%.2x ", g_fw_array[i]);
}
update_calculation(&calc_fw_checksum_crc,
g_fw_array[i]);
if (g_verbose == SUPER_VERBOSE) {
if ((i + 1) % line_print_size == 0)
my_printf(TDBG,
"FW Checksum= %.8x",
calc_fw_checksum_crc);
}
}
calc_num_of_bytes_to_read -= calc_read_bytes;
calc_curr_position += calc_read_bytes;
} /* end of while(calc_num_of_bytes_to_read > 0) */
finalize_calculation(&calc_fw_checksum_crc);
return calc_fw_checksum_crc;
}
/*
**************************************************************************
* CRC Handler
**************************************************************************
*/
/*
*******************************************************************
*
* #define P_xxxx
*
* The CRC's are computed using polynomials. The coefficients
* for the algorithms are defined by the following constants.
*
*******************************************************************
*/
#define P_32 0xEDB88320L
/*
*******************************************************************
*
* static int crc_tab...init
* static unsigned ... crc_tab...[]
*
* The algorithms use tables with pre-calculated values. This
* speeds up the calculation dramatically. The first time the
* CRC function is called, the table for that specific calcu-
* lation is set up. The ...init variables are used to deter-
* mine if the initialization has taken place. The calculated
* values are stored in the crc_tab... arrays.
*
* The variables are declared static. This makes them invisible
* for other modules of the program.
*
*******************************************************************
*/
static int crc_tab32_init = FALSE;
static unsigned int crc_tab32[256];
/*
********************************************************************
*
* static void init_crc...tab();
*
* Three local functions are used to initialize the tables
* with values for the algorithm.
*
*******************************************************************
*/
static void init_crc32_tab(void);
/*
*******************************************************************
*
* unsigned int initialize_crc_32( void );
*
* The function update_crc_32 calculates a new CRC-32 value
* based on the previous value of the CRC and the next byte
* of the data to be checked.
*
*******************************************************************
*/
unsigned int initialize_crc_32(void)
{
return 0xffffffffL;
} /* initialize_crc_32 */
/*
*******************************************************************
*
* unsigned int update_crc_32( unsigned int crc, char c );
*
* The function update_crc_32 calculates a new CRC-32 value
* based on the previous value of the CRC and the next byte
* of the data to be checked.
*
*******************************************************************
*/
unsigned int update_crc_32(unsigned int crc, char c)
{
unsigned int tmp, long_c;
long_c = 0x000000ffL & (unsigned int)c;
if (!crc_tab32_init)
init_crc32_tab();
tmp = crc ^ long_c;
crc = (crc >> 8) ^ crc_tab32[tmp & 0xff];
return crc;
} /* update_crc_32 */
/*
*******************************************************************
*
* static void init_crc32_tab( void );
*
* The function init_crc32_tab() is used to fill the array
* for calculation of the CRC-32 with values.
*
*******************************************************************
*/
static void init_crc32_tab(void)
{
int i, j;
unsigned int crc;
for (i = 0; i < 256; i++) {
crc = (unsigned int)i;
for (j = 0; j < 8; j++) {
if (crc & 0x00000001L)
crc = (crc >> 1) ^ P_32;
else
crc = crc >> 1;
}
crc_tab32[i] = crc;
}
crc_tab32_init = TRUE;
} /* init_crc32_tab */
/*
*******************************************************************
*
* unsigned int finalize_crc_32( unsigned int crc );
*
* The function finalize_crc_32 finalizes a CRC-32 calculation
* by performing a bit convolution (bit 0 is bit 31, etc').
*
*******************************************************************
*/
unsigned int finalize_crc_32(unsigned int crc)
{
int i;
unsigned int result = 0;
for (i = 0; i < NUM_OF_BYTES; i++)
SET_VAR_BIT(result, NUM_OF_BYTES - (i+1), READ_VAR_BIT(crc, i));
return result;
} /* finalize_crc_32 */
|