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
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
|
/*++
Copyright (c) 2004-2006 Intel Corporation. All rights reserved
This program and the accompanying materials are licensed and made available
under the terms and conditions of the BSD License which accompanies this
distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
FlashDefFile.c
Abstract:
Utility for flash management in the Intel Platform Innovation Framework
for EFI build environment.
--*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <Common/UefiBaseTypes.h>
#include <Common/FirmwareVolumeHeader.h>
#include <Common/MultiPhase.h>
#include "EfiUtilityMsgs.h"
#include "FlashDefFile.h"
#include "SimpleFileParsing.h"
#include "Symbols.h"
//
// #include "TrackMallocFree.h"
//
#define WCHAR_T char
#define MAX_STRING_LEN 256
#define MAX_NAME_LEN 128
#define BUFFER_SIZE 1024
#define MAX_ATTR_LEN 128
#define MAX_AREATYPE_LEN 128
#define COLUMN2_START 60
#define COLUMN3_START 70
//
// Information for each subregions defined in the fdf file will be saved in these
//
typedef struct _FLASH_SUBREGION_DESCRIPTION {
struct _FLASH_SUBREGION_DESCRIPTION *Next;
int CreateHob; // to add to the auto-created HOB array
WCHAR_T Name[MAX_NAME_LEN]; // each subregion within a region must have a unique name
unsigned int Size; // size, in bytes, of this subregion
unsigned int SizeLeft; // used when creating the image
WCHAR_T Attributes[MAX_ATTR_LEN]; // subregion attributes used in the output HOB
WCHAR_T AreaType[MAX_AREATYPE_LEN]; // subregion area type used in the output HOB
EFI_GUID NameGuid; // used in the output HOB
WCHAR_T NameGuidString[MAX_NAME_LEN];
EFI_GUID AreaTypeGuid; // used in the output HOB
WCHAR_T AreaTypeGuidString[MAX_NAME_LEN];
EFI_GUID FileSystemGuid; // used in the output HOB
WCHAR_T FileSystemGuidString[MAX_NAME_LEN];
} FLASH_SUBREGION_DESCRIPTION;
//
// Information for each block in a flash device will be saved in one of these.
// We'll also use it for region definitions.
//
typedef struct _FLASH_BLOCK_DESCRIPTION {
struct _FLASH_BLOCK_DESCRIPTION *Next; // next block in the linked list
WCHAR_T Name[MAX_NAME_LEN]; // each block must have a unique name
unsigned int Size; // size, in bytes, of this block
unsigned int SizeLeft; // for use when creating image
unsigned int Flags; // user-defined flags for the block
unsigned int Alignment; // power of 2 alignment
WCHAR_T Attributes[MAX_ATTR_LEN]; // only used for Region definitions
WCHAR_T AreaType[MAX_AREATYPE_LEN]; // only used for Region definitions
EFI_GUID AreaTypeGuid;
WCHAR_T AreaTypeGuidString[MAX_NAME_LEN];
FLASH_SUBREGION_DESCRIPTION *Subregions;
FLASH_SUBREGION_DESCRIPTION *LastSubregion;
} FLASH_BLOCK_DESCRIPTION;
//
// Information for each flash device will be saved in one of these
//
typedef struct _FLASH_DEVICE_DESCRIPTION {
struct _FLASH_DEVICE_DESCRIPTION *Next; // next flash device in our linked list
int ErasePolarity; // erase polarity of the flash device
unsigned int BaseAddress; // base address of the flash device
unsigned int Size; // total size, in bytes, of the flash device
WCHAR_T Name[MAX_NAME_LEN]; // name of the flash device
FLASH_BLOCK_DESCRIPTION *PBlocks; // linked list of physical block descriptors
FLASH_BLOCK_DESCRIPTION *LastPBlock; // last block in the linked list
FLASH_BLOCK_DESCRIPTION *Regions; // linked list of flash region descriptors
FLASH_BLOCK_DESCRIPTION *LastRegion; // last region in the linked list
} FLASH_DEVICE_DESCRIPTION;
//
// For image definitions, they can specify a file name or raw data bytes. Keep a linked list.
//
typedef struct _IMAGE_DEFINITION_ENTRY {
struct _IMAGE_DEFINITION_ENTRY *Next;
WCHAR_T RegionName[MAX_NAME_LEN];
WCHAR_T SubregionName[MAX_NAME_LEN];
WCHAR_T Name[MAX_NAME_LEN]; // file or data name
int IsRawData; // non-zero if raw data bytes
unsigned int RawDataSize;
char *RawData;
int Optional; // optional file (don't include if it doesn't exist)
} IMAGE_DEFINITION_ENTRY;
//
// When we parse an image definition, save all the data for each in one of these
//
typedef struct _IMAGE_DEFINITION {
struct _IMAGE_DEFINITION *Next;
WCHAR_T Name[MAX_NAME_LEN];
IMAGE_DEFINITION_ENTRY *Entries;
IMAGE_DEFINITION_ENTRY *LastEntry;
} IMAGE_DEFINITION;
typedef struct {
char *BufferStart;
char *BufferEnd;
char *BufferPos;
} BUFFER_DATA;
static const char *CIncludeHeader = "/*++\n\n"
" DO NOT EDIT -- file auto-generated by FlashMap utility\n\n""--*/\n""\n""#ifndef _FLASH_MAP_H_\n"
"#define _FLASH_MAP_H_\n\n";
//
// "#include \"EfiFlashMap.h\"\n\n";
//
static const char *CIncludeFooter = "#endif // #ifndef _FLASH_MAP_H_\n\n";
static const char *CFlashMapDataFileHeader = "/*++\n\n"
" DO NOT EDIT -- file auto-generated by FlashMap utility\n\n""--*/\n""\n";
static FLASH_DEVICE_DESCRIPTION *mFlashDevices = NULL;
static IMAGE_DEFINITION *mImageDefinitions = NULL;
//
// Local function prototypes
//
static
BUFFER_DATA *
CreateBufferData (
VOID
);
static
BOOLEAN
AddBufferDataByte (
BUFFER_DATA *Buffer,
char Data
);
static
void
FreeBufferData (
BUFFER_DATA *Buffer,
BOOLEAN FreeData
);
static
char *
GetBufferData (
BUFFER_DATA *Buffer,
int *BufferSize
);
static
FLASH_SUBREGION_DESCRIPTION *
ParseSubregionDefinition (
unsigned int SizeLeft
);
void
FDFConstructor (
VOID
)
/*++
Routine Description:
Initialization routine for the services that operate on a flash
definition file.
Arguments:
None.
Returns:
NA
--*/
{
mFlashDevices = NULL;
mImageDefinitions = NULL;
}
void
FDFDestructor (
VOID
)
/*++
Routine Description:
Finalization/cleanup routine for the services that operate on a flash
definition file.
Arguments:
None.
Returns:
NA
--*/
{
FLASH_BLOCK_DESCRIPTION *FBNext;
FLASH_DEVICE_DESCRIPTION *FDNext;
IMAGE_DEFINITION *IDNext;
IMAGE_DEFINITION_ENTRY *IDENext;
FLASH_SUBREGION_DESCRIPTION *SubNext;
//
// Go through all our flash devices and free the memory
//
while (mFlashDevices != NULL) {
//
// Free the physical block definitions
//
while (mFlashDevices->PBlocks != NULL) {
FBNext = mFlashDevices->PBlocks->Next;
_free (mFlashDevices->PBlocks);
mFlashDevices->PBlocks = FBNext;
}
//
// Free the region definitions
//
while (mFlashDevices->Regions != NULL) {
FBNext = mFlashDevices->Regions->Next;
//
// First free the subregion definitions
//
while (mFlashDevices->Regions->Subregions != NULL) {
SubNext = mFlashDevices->Regions->Subregions->Next;
_free (mFlashDevices->Regions->Subregions);
mFlashDevices->Regions->Subregions = SubNext;
}
_free (mFlashDevices->Regions);
mFlashDevices->Regions = FBNext;
}
FDNext = mFlashDevices->Next;
_free (mFlashDevices);
mFlashDevices = FDNext;
}
//
// Free up the image definitions, and the data
//
while (mImageDefinitions != NULL) {
//
// Free the entries
//
while (mImageDefinitions->Entries != NULL) {
IDENext = mImageDefinitions->Entries->Next;
if (mImageDefinitions->Entries->RawData != NULL) {
_free (mImageDefinitions->Entries->RawData);
}
_free (mImageDefinitions->Entries);
mImageDefinitions->Entries = IDENext;
}
IDNext = mImageDefinitions->Next;
_free (mImageDefinitions);
mImageDefinitions = IDNext;
}
}
STATUS
FDFParseFile (
char *FileName
)
/*++
Routine Description:
Parse the specified flash definition file, saving the definitions in
file-static variables for use by other functions.
Arguments:
FileName - name of the input flash definition text file.
Returns:
STATUS_SUCCESS - file parsed with no errors or warnings
STATUS_WARNING - warnings, but no errors, were encountered while parsing
STATUS_ERROR - errors were encountered while parsing
--*/
{
FILE *Fptr;
STATUS Status;
unsigned int Num;
FLASH_DEVICE_DESCRIPTION *FDDesc;
FLASH_BLOCK_DESCRIPTION *FBlockDesc;
FLASH_BLOCK_DESCRIPTION *TempBlockDesc;
FLASH_SUBREGION_DESCRIPTION *Subregion;
FLASH_SUBREGION_DESCRIPTION *TempSubregion;
unsigned int BlockSizeLeft;
unsigned int RegionSizeLeft;
unsigned int SubregionSizeLeft;
int ErrorCount;
int WarningCount;
IMAGE_DEFINITION *ImageDef;
IMAGE_DEFINITION_ENTRY *ImageDefEntry;
IMAGE_DEFINITION_ENTRY *TempImageDefEntry;
BUFFER_DATA *BufferData;
char Str[100];
BOOLEAN PreviousComma;
if ((Fptr = fopen (FileName, "r")) == NULL) {
Error (NULL, 0, 0, FileName, "failed to open input flash definition file for reading");
return STATUS_ERROR;
}
fclose (Fptr);
Status = STATUS_SUCCESS;
ErrorCount = 0;
WarningCount = 0;
//
// Initialize the simple-file-parsing routines
//
SFPInit ();
//
// Open the file
//
if ((Status = SFPOpenFile (FileName)) != STATUS_SUCCESS) {
return Status;
}
//
// Parse the file. Should start with a series of these:
// FlashDevice {
// Name = "FLASH_1234", Size = 0x2004, BaseAddress = 0xFFF0000, ErasePolarity = 1,
// Block { Name = "BLOCK1", Size = 0x1000, Flags = 0x0001 }
// Block { Name = "BLOCK2", Size = 0x1004, Flags = 0x0002 }
// Region { Name = "REGION_NAME", Size = 0x2004, Align= 4 }
// }
//
while (SFPIsKeyword ("FlashDevice")) {
//
// Allocate memory for new flash device description block
//
FDDesc = (FLASH_DEVICE_DESCRIPTION *) _malloc (sizeof (FLASH_DEVICE_DESCRIPTION));
if (FDDesc == NULL) {
Error (__FILE__, __LINE__, 0, "memory allocation failure", NULL);
ErrorCount++;
goto Done;
}
memset (FDDesc, 0, sizeof (FLASH_DEVICE_DESCRIPTION));
//
// Open brace -- warning if not there
//
if (!SFPIsToken ("{")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected {", NULL);
WarningCount++;
}
//
// Parse: Name = "DeviceName",
//
if (!SFPIsKeyword ("Name")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Name'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetQuotedString (FDDesc->Name, sizeof (FDDesc->Name))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted name of flash device", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected comma following flash device name", NULL);
WarningCount++;
}
//
// Parse: Size = 0x20000,
//
if (!SFPIsKeyword ("Size")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Size'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetNumber (&FDDesc->Size)) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected numeric Size value", NULL);
ErrorCount++;
goto Done;
}
//
// Check for 0 size
//
if (FDDesc->Size == 0) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, FDDesc->Name, "Size field cannot be 0", NULL);
ErrorCount++;
goto Done;
}
SFPIsToken (",");
//
// Parse: BaseAddress = 0xFFF0000,
//
if (!SFPIsKeyword ("BaseAddress")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'BaseAddress'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetNumber (&FDDesc->BaseAddress)) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected numeric value for BaseAddress", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected comma following BaseAddress value", NULL);
WarningCount++;
}
//
// Parse: ErasePolarity = 1,
//
if (!SFPIsKeyword ("ErasePolarity")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'ErasePolarity'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetNumber (&Num) || ((Num != 0) && (Num != 1))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected numeric erase polarity value 1 or 0", NULL);
ErrorCount++;
goto Done;
}
FDDesc->ErasePolarity = Num;
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected comma following erase polarity value", NULL);
WarningCount++;
}
//
// Parse array of:
// Block { Name = "BLOCK1", Size = 0x1000, Flags = 0x0001 }
//
// Keep track of size to make sure the sum of the physical blocks and region sizes do not
// exceed the size of the flash device.
//
BlockSizeLeft = FDDesc->Size;
RegionSizeLeft = FDDesc->Size;
while (SFPIsKeyword ("Block")) {
//
// Allocate memory for a new physical block descriptor
//
FBlockDesc = (FLASH_BLOCK_DESCRIPTION *) _malloc (sizeof (FLASH_BLOCK_DESCRIPTION));
if (FBlockDesc == NULL) {
Error (__FILE__, __LINE__, 0, "memory allocation failure", NULL);
ErrorCount++;
goto Done;
}
memset (FBlockDesc, 0, sizeof (FLASH_BLOCK_DESCRIPTION));
//
// Open brace -- warning if not there
//
if (!SFPIsToken ("{")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected {", NULL);
WarningCount++;
}
//
// Parse: Name = "BlockName",
//
if (!SFPIsKeyword ("Name")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Name'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetQuotedString (FBlockDesc->Name, sizeof (FBlockDesc->Name))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted name of physical block", NULL);
ErrorCount++;
goto Done;
}
//
// Make sure there are no other physical block names with this same name
//
for (TempBlockDesc = FDDesc->PBlocks; TempBlockDesc != NULL; TempBlockDesc = TempBlockDesc->Next) {
if (strcmp (TempBlockDesc->Name, FBlockDesc->Name) == 0) {
Error (
SFPGetFileName (),
SFPGetLineNumber (),
0,
TempBlockDesc->Name,
"physical block with this name already defined"
);
ErrorCount++;
}
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected comma following physical block name", NULL);
WarningCount++;
}
//
// Parse: Size = 0x2000,
//
if (!SFPIsKeyword ("Size")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Size'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetNumber (&FBlockDesc->Size)) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected numeric Size value", NULL);
ErrorCount++;
goto Done;
}
//
// Make sure the sum of physical blocks so far does not exceed flash device size
//
if (BlockSizeLeft < FBlockDesc->Size) {
Error (
SFPGetFileName (),
SFPGetLineNumber (),
0,
"sum of physical block sizes exceeds flash device size",
NULL
);
ErrorCount++;
}
BlockSizeLeft -= FBlockDesc->Size;
SFPIsToken (",");
//
// Optional parse: Flags = 0xFFF0000,
//
if (SFPIsKeyword ("Flags")) {
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetNumber (&FBlockDesc->Flags)) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected numeric value for Flags", NULL);
ErrorCount++;
goto Done;
}
}
if (!SFPIsToken ("}")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected PhysicalBlock closing brace '}'", NULL);
WarningCount++;
}
//
// Add the physical block descriptor to the end of the linked list
//
if (FDDesc->LastPBlock != NULL) {
FDDesc->LastPBlock->Next = FBlockDesc;
} else {
FDDesc->PBlocks = FBlockDesc;
}
FDDesc->LastPBlock = FBlockDesc;
}
//
// Make sure sum of sizes of physical blocks added up to size of flash device
//
if (BlockSizeLeft != 0) {
Error (
SFPGetFileName (),
SFPGetLineNumber (),
0,
NULL,
"sum of sizes of physical blocks (0x%08X) != flash device size (0x%08X) : delta = 0x%08X",
FDDesc->Size - BlockSizeLeft,
FDDesc->Size,
BlockSizeLeft
);
ErrorCount++;
}
//
// Parse array of:
// Region { Name = "REGION_1", Size = 0x2000, Flags = 0x1234, Alignment = 4, Attributes = "str", AreaType = "str" }
//
while (SFPIsKeyword ("Region")) {
//
// Allocate memory for a new physical block descriptor
//
FBlockDesc = (FLASH_BLOCK_DESCRIPTION *) _malloc (sizeof (FLASH_BLOCK_DESCRIPTION));
if (FBlockDesc == NULL) {
Error (__FILE__, __LINE__, 0, "memory allocation failure", NULL);
ErrorCount++;
goto Done;
}
memset (FBlockDesc, 0, sizeof (FLASH_BLOCK_DESCRIPTION));
//
// Open brace -- warning if not there
//
if (!SFPIsToken ("{")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected {", NULL);
WarningCount++;
}
//
// Parse: Name = "BlockName",
//
if (!SFPIsKeyword ("Name")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Name'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetQuotedString (FBlockDesc->Name, sizeof (FBlockDesc->Name))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted Region name", NULL);
ErrorCount++;
goto Done;
}
//
// Make sure there are no other region names with this same name
//
for (TempBlockDesc = FDDesc->Regions; TempBlockDesc != NULL; TempBlockDesc = TempBlockDesc->Next) {
if (strcmp (TempBlockDesc->Name, FBlockDesc->Name) == 0) {
Error (
SFPGetFileName (),
SFPGetLineNumber (),
0,
TempBlockDesc->Name,
"Region with this name already defined"
);
ErrorCount++;
}
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected comma following Region name", NULL);
WarningCount++;
}
//
// Parse: Size = 0x2000,
//
if (!SFPIsKeyword ("Size")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Size'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetNumber (&FBlockDesc->Size)) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected numeric Size value", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected ','", NULL);
}
//
// Make sure the sum of regions so far does not exceed flash device size
//
if (RegionSizeLeft < FBlockDesc->Size) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "sum of Region sizes exceeds flash device size", NULL);
ErrorCount++;
}
RegionSizeLeft -= FBlockDesc->Size;
//
// Optional parse: Flags = 0xFFF0000,
//
if (SFPIsKeyword ("Flags")) {
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetNumber (&FBlockDesc->Flags)) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected numeric value for Flags", NULL);
ErrorCount++;
goto Done;
}
//
// comma
//
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected ','", NULL);
}
}
//
// Optional parse: Alignment = 4
//
if (SFPIsKeyword ("Alignment")) {
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetNumber (&FBlockDesc->Alignment)) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected numeric Alignment value", NULL);
ErrorCount++;
goto Done;
}
//
// comma
//
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected ','", NULL);
}
}
//
// Parse: Attributes = "String",
//
if (!SFPIsKeyword ("Attributes")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Attributes'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetQuotedString (FBlockDesc->Attributes, sizeof (FBlockDesc->Attributes))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted Attributes string", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected ','", NULL);
}
//
// Parse: AreaType = "String",
//
if (!SFPIsKeyword ("AreaType")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'AreaType'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetQuotedString (FBlockDesc->AreaType, sizeof (FBlockDesc->AreaType))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted AreaType string", NULL);
ErrorCount++;
goto Done;
}
PreviousComma = SFPIsToken (",");
//
// Parse optional attribute "AreaTypeGuid"
//
if (SFPIsKeyword ("AreaTypeGuid")) {
//
// Check for preceeding comma now
//
if (!PreviousComma) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected ',' before 'AreaTypeGuid'", NULL);
WarningCount++;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (SFPGetQuotedString (FBlockDesc->AreaTypeGuidString, sizeof (FBlockDesc->AreaTypeGuidString))) {
//
// Nothing else to do
//
} else if (!SFPGetGuid (PARSE_GUID_STYLE_5_FIELDS, &FBlockDesc->AreaTypeGuid)) {
Error (
SFPGetFileName (),
SFPGetLineNumber (),
0,
"expected AreaTypeGuid quoted string or GUID of form 12345678-1234-1234-1234-123456789ABC",
NULL
);
ErrorCount++;
goto Done;
}
PreviousComma = SFPIsToken (",");
}
//
// Parse optional Subregion definitions
//
SubregionSizeLeft = FBlockDesc->Size;
while (SFPIsToken ("Subregion")) {
if (!PreviousComma) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected ',' before 'Subregion'", NULL);
WarningCount++;
PreviousComma = TRUE;
}
Subregion = ParseSubregionDefinition (SubregionSizeLeft);
if (Subregion == NULL) {
ErrorCount++;
goto Done;
}
SubregionSizeLeft -= Subregion->Size;
//
// Add it to the end of our list
//
if (FBlockDesc->Subregions == NULL) {
FBlockDesc->Subregions = Subregion;
} else {
FBlockDesc->LastSubregion->Next = Subregion;
}
FBlockDesc->LastSubregion = Subregion;
//
// Make sure all subregion names are unique. We do this each time
// through so that we catch the error immediately after it happens, in
// which case the reported line number is at least close to where the
// problem lies. We don't exit on the error because we can continue parsing
// the script to perhaps catch other errors or warnings.
//
for (Subregion = FBlockDesc->Subregions; Subregion != NULL; Subregion = Subregion->Next) {
for (TempSubregion = Subregion->Next; TempSubregion != NULL; TempSubregion = TempSubregion->Next) {
if (strcmp (Subregion->Name, TempSubregion->Name) == 0) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, Subregion->Name, "duplicate Subregion name");
ErrorCount++;
}
}
}
}
if (!SFPIsToken ("}")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected Region closing brace '}'", NULL);
WarningCount++;
}
//
// Add the region descriptor to the end of the linked list
//
if (FDDesc->LastRegion != NULL) {
FDDesc->LastRegion->Next = FBlockDesc;
} else {
FDDesc->Regions = FBlockDesc;
}
FDDesc->LastRegion = FBlockDesc;
}
//
// Make sure sum of sizes of regions adds up to size of flash device
//
if (RegionSizeLeft != 0) {
Error (
SFPGetFileName (),
SFPGetLineNumber (),
0,
NULL,
"sum of sizes of Regions (0x%08X) != flash device size (0x%08X) : delta = 0x%08X",
FDDesc->Size - RegionSizeLeft,
FDDesc->Size,
RegionSizeLeft
);
ErrorCount++;
}
//
// Look for closing brace
//
if (!SFPIsToken ("}")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected FlashDevice closing brace '}'", NULL);
WarningCount++;
}
//
// Add this flash description to the list
//
FDDesc->Next = mFlashDevices;
mFlashDevices = FDDesc;
}
while (SFPIsKeyword ("FlashDeviceImage")) {
//
// Allocate memory for a new FD image definition
//
ImageDef = (IMAGE_DEFINITION *) _malloc (sizeof (IMAGE_DEFINITION));
if (ImageDef == NULL) {
Error (__FILE__, __LINE__, 0, "memory allocation failure", NULL);
ErrorCount++;
goto Done;
}
memset (ImageDef, 0, sizeof (IMAGE_DEFINITION));
//
// Open brace -- warning if not there
//
if (!SFPIsToken ("{")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected {", NULL);
WarningCount++;
}
//
// Parse: Name = "ImageName",
//
if (!SFPIsKeyword ("Name")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Name'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetQuotedString (ImageDef->Name, sizeof (ImageDef->Name))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted name of image", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected comma following image name", NULL);
WarningCount++;
}
while (1) {
//
// Parse: File { Name = "FV\FvOem.fv", Region = "REGION_OEM", Optional = TRUE }
//
if (SFPIsKeyword ("File")) {
ImageDefEntry = (IMAGE_DEFINITION_ENTRY *) _malloc (sizeof (IMAGE_DEFINITION_ENTRY));
if (ImageDefEntry == NULL) {
Error (__FILE__, __LINE__, 0, "memory allocation failure", NULL);
ErrorCount++;
goto Done;
}
memset (ImageDefEntry, 0, sizeof (IMAGE_DEFINITION_ENTRY));
//
// Open brace -- warning if not there
//
if (!SFPIsToken ("{")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected {", NULL);
WarningCount++;
}
//
// Parse: Name = "FileName.txt"
//
if (!SFPIsKeyword ("Name")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Name'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetQuotedString (ImageDefEntry->Name, sizeof (ImageDefEntry->Name))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted name of file", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected comma following file name", NULL);
WarningCount++;
}
//
// Parse: Region = "REGION_NAME"
//
if (!SFPIsKeyword ("Region")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Region'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetQuotedString (ImageDefEntry->RegionName, sizeof (ImageDefEntry->RegionName))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted Region name", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected comma following Region name", NULL);
WarningCount++;
}
//
// Parse optional: Subregion = "SUBREGION_NAME"
//
if (SFPIsKeyword ("Subregion")) {
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetQuotedString (ImageDefEntry->SubregionName, sizeof (ImageDefEntry->SubregionName))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted Subegion name", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected comma following Subregion name", NULL);
WarningCount++;
}
//
// For a given region, you can only place data using the region name, or the subregion names.
// In other words, you can't say File1->Region1 and File2->Region1.Subregion1. Enforce that
// here by checking that any previous entries with the same Region name had a Subregion specified
// as well.
//
for (TempImageDefEntry = ImageDef->Entries;
TempImageDefEntry != NULL;
TempImageDefEntry = TempImageDefEntry->Next
) {
if (strcmp (TempImageDefEntry->Name, ImageDefEntry->Name) == 0) {
if (TempImageDefEntry->SubregionName[0] == 0) {
Error (
SFPGetFileName (),
SFPGetLineNumber (),
0,
TempImageDefEntry->RegionName,
"data already placed on a region-basis in the region, can't place data using subregions"
);
ErrorCount++;
}
}
}
}
//
// Optional parse: Optional = TRUE | FALSE
//
if (SFPIsKeyword ("Optional")) {
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPIsKeyword ("TRUE")) {
ImageDefEntry->Optional = 1;
} else if (SFPIsKeyword ("FALSE")) {
//
// Already set to 0
//
} else {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'TRUE' or 'FALSE'", NULL);
ErrorCount++;
goto Done;
}
SFPIsToken (",");
}
//
// Closing brace
//
if (!SFPIsToken ("}")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '}' closing brace to File entry", NULL);
ErrorCount++;
goto Done;
}
//
// Add the entry to the end of the list
//
if (ImageDef->LastEntry != NULL) {
ImageDef->LastEntry->Next = ImageDefEntry;
} else {
ImageDef->Entries = ImageDefEntry;
}
ImageDef->LastEntry = ImageDefEntry;
} else if (SFPIsKeyword ("RawData")) {
//
// Parse: RawData { Name = "PadBytes", Region = "REGION_1", Data = { 0x78, 0x56, 0x34, 0x12 }}
//
ImageDefEntry = (IMAGE_DEFINITION_ENTRY *) _malloc (sizeof (IMAGE_DEFINITION_ENTRY));
if (ImageDefEntry == NULL) {
Error (__FILE__, __LINE__, 0, "memory allocation failure", NULL);
ErrorCount++;
goto Done;
}
memset (ImageDefEntry, 0, sizeof (IMAGE_DEFINITION_ENTRY));
ImageDefEntry->IsRawData = 1;
//
// Open brace -- warning if not there
//
if (!SFPIsToken ("{")) {
Warning (
SFPGetFileName (),
SFPGetLineNumber (),
0,
"expected '{' opening brace for RawData definition",
NULL
);
WarningCount++;
}
//
// Parse: Name = "PadBytes"
//
if (!SFPIsKeyword ("Name")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Name'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetQuotedString (ImageDefEntry->Name, sizeof (ImageDefEntry->Name))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted name of raw data", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected comma following raw data name", NULL);
WarningCount++;
}
//
// Parse: Region = "REGION_NAME"
//
if (!SFPIsKeyword ("Region")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Region'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetQuotedString (ImageDefEntry->RegionName, sizeof (ImageDefEntry->RegionName))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted Region name", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected comma following Region name", NULL);
WarningCount++;
}
//
// Parse optional: Subregion = "SUBREGION_NAME"
//
if (SFPIsKeyword ("Subregion")) {
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetQuotedString (ImageDefEntry->SubregionName, sizeof (ImageDefEntry->SubregionName))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted Subegion name", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected comma following Subregion name", NULL);
WarningCount++;
}
//
// For a given region, you can only place data using the region name, or the subregion names.
// In other words, you can't say File1->Region1 and File2->Region1.Subregion1. Enforce that
// here by checking that any previous entries with the same Region name had a Subregion specified
// as well.
//
for (TempImageDefEntry = ImageDef->Entries;
TempImageDefEntry != NULL;
TempImageDefEntry = TempImageDefEntry->Next
) {
if (strcmp (TempImageDefEntry->Name, ImageDefEntry->Name) == 0) {
if (TempImageDefEntry->SubregionName[0] == 0) {
Error (
SFPGetFileName (),
SFPGetLineNumber (),
0,
TempImageDefEntry->RegionName,
"data already placed on a region-basis in the region, can't place data using subregions"
);
ErrorCount++;
}
}
}
}
//
// Parse: Data = { 0x78, 0x56, 0x34, 0x12 }
//
if (!SFPIsKeyword ("Data")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Data'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPIsToken ("{")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '{' preceeding data list", NULL);
WarningCount++;
}
if ((BufferData = CreateBufferData ()) == NULL) {
ErrorCount++;
goto Done;
}
//
// Read bytes from input file until closing brace
//
while (!SFPIsToken ("}")) {
if (!SFPGetNumber (&Num)) {
SFPGetNextToken (Str, sizeof (Str));
Error (SFPGetFileName (), SFPGetLineNumber (), 0, Str, "expected data value", Str);
ErrorCount++;
FreeBufferData (BufferData, TRUE);
goto Done;
} else {
//
// Only allow bytes
//
if (Num > 0xFF) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "only values 0-255 (0x00-0xFF) allowed", NULL);
ErrorCount++;
FreeBufferData (BufferData, TRUE);
goto Done;
}
AddBufferDataByte (BufferData, (char) Num);
SFPIsToken (",");
}
}
//
// Now get the data and save it in our image entry
//
ImageDefEntry->RawData = GetBufferData (BufferData, &ImageDefEntry->RawDataSize);
FreeBufferData (BufferData, 0);
//
// Closing brace for RawData {}
//
if (!SFPIsToken ("}")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '}' closing brace for RawData", NULL);
ErrorCount++;
goto Done;
}
//
// Add the entry to the end of the list
//
if (ImageDef->LastEntry != NULL) {
ImageDef->LastEntry->Next = ImageDefEntry;
} else {
ImageDef->Entries = ImageDefEntry;
}
ImageDef->LastEntry = ImageDefEntry;
} else if (SFPIsToken ("}")) {
//
// Closing brace for FDImage {}
//
break;
} else {
SFPGetNextToken (Str, sizeof (Str));
Error (SFPGetFileName (), SFPGetLineNumber (), 0, Str, "unrecognized token", Str);
ErrorCount++;
goto Done;
}
}
//
// Add this image definition to our global list
//
ImageDef->Next = mImageDefinitions;
mImageDefinitions = ImageDef;
}
//
// Check for end-of-file
//
if (!SFPIsEOF ()) {
SFPGetNextToken (Str, sizeof (Str));
Error (SFPGetFileName (), SFPGetLineNumber (), 0, Str, "expected end-of-file", Str);
ErrorCount++;
}
Done:
SFPCloseFile ();
if (ErrorCount != 0) {
return STATUS_ERROR;
} else if (WarningCount != 0) {
return STATUS_WARNING;
}
return STATUS_SUCCESS;
}
static
FLASH_SUBREGION_DESCRIPTION *
ParseSubregionDefinition (
unsigned int SizeLeft
)
/*++
Routine Description:
Parse Subregion definitions from the input flash definition file. Format:
Subregion {
CreateHob = TRUE,
Name = "FOO",
Size = 0xA000,
Attributes = "EFI_FLASH_AREA_SUBFV | EFI_FLASH_AREA_MEMMAPPED_FV",
AreaType = "EFI_FLASH_AREA_EFI_VARIABLES",
NameGuid = 12345678-1234-5678-AAAA-BBBBCCCCDDDD (or "EFI_SOME_GUID"),
AreaTypeGuid = 11111111-2222-3333-4444-1, (or "EFI_SOME_GUID") (optional)
FileSystemGuid = 11111111-2222-3333-4444-1, (or "EFI_SOME_GUID") (optional)
}
NOTE: The caller has already parsed the "Subregion" token, so start with the opening brace.
Arguments:
SizeLeft - in the flash definition file, a Region can be broken up into
one or more subregions. As we parse the subregion definitions,
the caller keeps track of how much space is left in the region
that we're parsing subregions for. SizeLeft is that size, and
so the size of the subregion we're now parsing better not
exceed the size left.
Returns:
NULL - unrecoverable errors detected while parsing the subregion definition
pointer to a subregion definition created from the parsed subregion
--*/
{
FLASH_SUBREGION_DESCRIPTION *Subregion;
int ErrorCount;
int WarningCount;
unsigned int Number;
BOOLEAN PreviousComma;
//
// Allocate memory for the new subregion descriptor
//
ErrorCount = 0;
WarningCount = 0;
Subregion = (FLASH_SUBREGION_DESCRIPTION *) _malloc (sizeof (FLASH_SUBREGION_DESCRIPTION));
if (Subregion == NULL) {
Error (__FILE__, __LINE__, 0, "memory allocation failure", NULL);
ErrorCount++;
goto Done;
}
memset (Subregion, 0, sizeof (FLASH_SUBREGION_DESCRIPTION));
//
// Open brace -- warning if not there
//
if (!SFPIsToken ("{")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected {", NULL);
WarningCount++;
}
//
// Parse: CreateHob = TRUE | FALSE,
//
if (!SFPIsKeyword ("CreateHob")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'CreateHob'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (SFPIsToken ("TRUE")) {
Subregion->CreateHob = 1;
} else if (SFPIsToken ("FALSE")) {
//
// Subregion->CreateHob = 0; -- not required since we did a memset earlier
//
} else {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'TRUE' or 'FALSE'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected ',' following CreateHob value", NULL);
WarningCount++;
}
//
// Parse: Name = "Name",
//
if (!SFPIsKeyword ("Name")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Name'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetQuotedString (Subregion->Name, sizeof (Subregion->Name))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected Subregion name", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected comma following Region name", NULL);
WarningCount++;
}
//
// Parse: Size = 0x2000,
//
if (!SFPIsKeyword ("Size")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Size'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (!SFPGetNumber (&Subregion->Size)) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected numeric Size value", NULL);
ErrorCount++;
goto Done;
}
//
// Check that the size does not exceed the size left passed in
//
if (Subregion->Size > SizeLeft) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "sum of Subregion sizes exceeds Region size", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected ',' following Size value", NULL);
}
//
// Parse: Attributes = Number | "String",
//
if (!SFPIsKeyword ("Attributes")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'Attributes'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (SFPGetNumber (&Number)) {
sprintf (Subregion->Attributes, "0x%X", Number);
} else if (!SFPGetQuotedString (Subregion->Attributes, sizeof (Subregion->Attributes))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted Attributes string", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected ','", NULL);
}
//
// Parse: AreaType = Number | "String",
// AreaType is a UINT8, so error if it exceeds the size
//
if (!SFPIsKeyword ("AreaType")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'AreaType'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (SFPGetNumber (&Number)) {
if (Number > 0xFF) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "AreaType value exceeds 255", NULL);
ErrorCount++;
}
sprintf (Subregion->AreaType, "0x%X", Number & 0x00FF);
} else if (!SFPGetQuotedString (Subregion->AreaType, sizeof (Subregion->AreaType))) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected quoted AreaType string", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken (",")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected ',' following AreaType value", NULL);
}
//
// Parse the three GUIDs (last two are optional)
//
// NameGuid = 12345678-1234-5678-AAAA-BBBBCCCCDDDD, (or "EFI_SOME_GUID")
// AreaTypeGuid = 11111111-2222-3333-4444-1, (or "EFI_SOME_GUID")
// FileSysteGuid = 11111111-2222-3333-4444-1, (or "EFI_SOME_GUID")
//
if (!SFPIsKeyword ("NameGuid")) {
Error (SFPGetFileName (), SFPGetLineNumber (), 0, "expected 'NameGuid'", NULL);
ErrorCount++;
goto Done;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
//
// Allow a GUID or a quoted string identifier, which we'll just copy as a string
//
if (SFPGetQuotedString (Subregion->NameGuidString, sizeof (Subregion->NameGuidString))) {
//
// Nothing else to do
//
} else if (!SFPGetGuid (PARSE_GUID_STYLE_5_FIELDS, &Subregion->NameGuid)) {
Error (
SFPGetFileName (),
SFPGetLineNumber (),
0,
"expected NameGuid quoted string or GUID of form 12345678-1234-1234-1234-123456789ABC",
NULL
);
ErrorCount++;
goto Done;
}
//
// Comma following NameGuid is optional if they don't specify AreaTypeGuid or FileSystemGuid
//
PreviousComma = SFPIsToken (",");
if (SFPIsKeyword ("AreaTypeGuid")) {
//
// Check for preceeding comma now
//
if (!PreviousComma) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected ',' before 'AreaTypeGuid'", NULL);
WarningCount++;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
if (SFPGetQuotedString (Subregion->AreaTypeGuidString, sizeof (Subregion->AreaTypeGuidString))) {
//
// Nothing else to do
//
} else if (!SFPGetGuid (PARSE_GUID_STYLE_5_FIELDS, &Subregion->AreaTypeGuid)) {
Error (
SFPGetFileName (),
SFPGetLineNumber (),
0,
"expected AreaTypeGuid quoted string or GUID of form 12345678-1234-1234-1234-123456789ABC",
NULL
);
ErrorCount++;
goto Done;
}
PreviousComma = SFPIsToken (",");
}
if (SFPIsKeyword ("FileSystemGuid")) {
//
// Check for preceeding comma now
//
if (!PreviousComma) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected ',' before 'FileSystemGuid'", NULL);
WarningCount++;
}
if (!SFPIsToken ("=")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected '='", NULL);
WarningCount++;
}
//
// Allow a GUID or a quoted string identifier, which we'll just copy as a string
//
if (SFPGetQuotedString (Subregion->FileSystemGuidString, sizeof (Subregion->FileSystemGuidString))) {
//
// Nothing else to do
//
} else if (!SFPGetGuid (PARSE_GUID_STYLE_5_FIELDS, &Subregion->FileSystemGuid)) {
Error (
SFPGetFileName (),
SFPGetLineNumber (),
0,
"expected FileSystemGuid quoted string or GUID of form 12345678-1234-1234-1234-123456789ABC",
NULL
);
ErrorCount++;
goto Done;
}
SFPIsToken (",");
}
//
// Look for subregion closing brace
//
if (!SFPIsToken ("}")) {
Warning (SFPGetFileName (), SFPGetLineNumber (), 0, "expected Subregion closing brace '}'", NULL);
WarningCount++;
}
Done:
//
// If any errors were encountered, then delete the subregion definition
//
if (ErrorCount != 0) {
_free (Subregion);
Subregion = NULL;
}
return Subregion;
}
STATUS
FDFCreateCIncludeFile (
char *FlashDeviceName,
char *FileName
)
/*++
Routine Description:
Create a header file with #define definitions per an already-parsed
flash definition file.
Arguments:
FlashDeviceName - name of flash device (from the flash definition file)
to use
FileName - name of output file to create
Returns:
STATUS_SUCCESS - no errors or warnings
STATUS_WARNING - warnings, but no errors, were encountered
STATUS_ERROR - errors were encountered
--*/
{
FILE *OutFptr;
FLASH_BLOCK_DESCRIPTION *FBlock;
FLASH_DEVICE_DESCRIPTION *FDev;
FLASH_SUBREGION_DESCRIPTION *Subregion;
unsigned int Offset;
unsigned int SubregionOffset;
int CreateHobs;
//
// Find the definition we're supposed to use
//
for (FDev = mFlashDevices; FDev != NULL; FDev = FDev->Next) {
if (strcmp (FDev->Name, FlashDeviceName) == 0) {
break;
}
}
if (FDev == NULL) {
Error (NULL, 0, 0, NULL, FlashDeviceName, "flash device not found in flash definitions");
return STATUS_ERROR;
}
if ((OutFptr = fopen (FileName, "w")) == NULL) {
Error (NULL, 0, 0, FileName, "failed to open output file for writing");
return STATUS_ERROR;
}
//
// Write a header
//
fprintf (OutFptr, CIncludeHeader);
//
// Write flash block base and size defines
//
fprintf (OutFptr, "#define FLASH_BASE 0x%08X\n", FDev->BaseAddress);
fprintf (OutFptr, "#define FLASH_SIZE 0x%08X\n\n", FDev->Size);
//
// Write flash regions base, size and offset defines
//
Offset = 0;
CreateHobs = 0;
for (FBlock = FDev->Regions; FBlock != NULL; FBlock = FBlock->Next) {
fprintf (
OutFptr,
"#define FLASH_REGION_%s_BASE %*c0x%08X\n",
FBlock->Name,
COLUMN2_START - 40 - strlen (FBlock->Name),
' ',
Offset + FDev->BaseAddress
);
fprintf (
OutFptr,
"#define FLASH_REGION_%s_SIZE %*c0x%08X\n",
FBlock->Name,
COLUMN2_START - 40 - strlen (FBlock->Name),
' ',
FBlock->Size
);
fprintf (
OutFptr,
"#define FLASH_REGION_%s_OFFSET %*c0x%08X\n",
FBlock->Name,
COLUMN2_START - 40 - strlen (FBlock->Name),
' ',
Offset
);
//
// Create defines for any subregions
//
SubregionOffset = 0;
for (Subregion = FBlock->Subregions; Subregion != NULL; Subregion = Subregion->Next) {
fprintf (
OutFptr,
"#define FLASH_REGION_%s_SUBREGION_%s_BASE %*c0x%08X\n",
FBlock->Name,
Subregion->Name,
COLUMN3_START - 43 - strlen (FBlock->Name) - strlen (Subregion->Name),
' ',
FDev->BaseAddress + Offset + SubregionOffset
);
fprintf (
OutFptr,
"#define FLASH_REGION_%s_SUBREGION_%s_SIZE %*c0x%08X\n",
FBlock->Name,
Subregion->Name,
COLUMN3_START - 43 - strlen (FBlock->Name) - strlen (Subregion->Name),
' ',
Subregion->Size
);
fprintf (
OutFptr,
"#define FLASH_REGION_%s_SUBREGION_%s_OFFSET %*c0x%08X\n",
FBlock->Name,
Subregion->Name,
COLUMN3_START - 43 - strlen (FBlock->Name) - strlen (Subregion->Name),
' ',
Offset + SubregionOffset
);
SubregionOffset += Subregion->Size;
if (Subregion->CreateHob != 0) {
CreateHobs = 1;
}
}
Offset += FBlock->Size;
}
//
// Now create a #define for the flash map data definition
//
fprintf (OutFptr, "\n\n#define EFI_FLASH_AREA_DATA_DEFINITION \\\n");
//
// Emit entry for each region
//
Offset = 0;
for (FBlock = FDev->Regions; FBlock != NULL; FBlock = FBlock->Next) {
fprintf (OutFptr, " /* %s region */\\\n", FBlock->Name);
fprintf (OutFptr, " {\\\n");
fprintf (OutFptr, " FLASH_REGION_%s_BASE,\\\n", FBlock->Name);
fprintf (OutFptr, " FLASH_REGION_%s_SIZE,\\\n", FBlock->Name);
fprintf (OutFptr, " %s,\\\n", FBlock->Attributes);
fprintf (OutFptr, " %s,\\\n", FBlock->AreaType);
fprintf (OutFptr, " 0, 0, 0,\\\n");
//
// The AreaTypeGuid may have been specified in the input flash definition file as a GUID, or
// as a quoted string. Do the right one.
//
if (FBlock->AreaTypeGuidString[0] != 0) {
fprintf (OutFptr, " %s, \\\n", FBlock->AreaTypeGuidString);
} else {
fprintf (
OutFptr,
" { 0x%08X, 0x%04X, 0x%04X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X },\\\n",
FBlock->AreaTypeGuid.Data1,
(unsigned int) FBlock->AreaTypeGuid.Data2,
(unsigned int) FBlock->AreaTypeGuid.Data3,
(unsigned int) FBlock->AreaTypeGuid.Data4[0],
(unsigned int) FBlock->AreaTypeGuid.Data4[1],
(unsigned int) FBlock->AreaTypeGuid.Data4[2],
(unsigned int) FBlock->AreaTypeGuid.Data4[3],
(unsigned int) FBlock->AreaTypeGuid.Data4[4],
(unsigned int) FBlock->AreaTypeGuid.Data4[5],
(unsigned int) FBlock->AreaTypeGuid.Data4[6],
(unsigned int) FBlock->AreaTypeGuid.Data4[7]
);
}
fprintf (OutFptr, " },\\\n");
}
fprintf (OutFptr, "\n\n");
//
// Now walk the list again to create the EFI_HOB_FLASH_MAP_ENTRY_TYPE definition
//
if (CreateHobs != 0) {
fprintf (OutFptr, "//\n// EFI_HOB_FLASH_MAP_ENTRY_TYPE definition\n//\n");
fprintf (OutFptr, "#define EFI_HOB_FLASH_MAP_ENTRY_TYPE_DATA_DEFINITION");
for (FBlock = FDev->Regions; FBlock != NULL; FBlock = FBlock->Next) {
//
// See if the block has subregions, and that the CreateHobs flag is set
// for any of them.
//
CreateHobs = 0;
for (Subregion = FBlock->Subregions; Subregion != NULL; Subregion = Subregion->Next) {
if (Subregion->CreateHob != 0) {
CreateHobs = 1;
break;
}
}
//
// If any of the subregions had the CreateHobs flag set, then create the entries in the
// output file
//
if (CreateHobs != 0) {
for (Subregion = FBlock->Subregions; Subregion != NULL; Subregion = Subregion->Next) {
if (Subregion->CreateHob != 0) {
fprintf (OutFptr, " \\\n");
fprintf (OutFptr, " /* %s.%s Subregion */\\\n", FBlock->Name, Subregion->Name);
fprintf (OutFptr, " {\\\n");
fprintf (OutFptr, " {EFI_HOB_TYPE_GUID_EXTENSION,\\\n");
fprintf (OutFptr, " sizeof (EFI_HOB_FLASH_MAP_ENTRY_TYPE ),\\\n");
fprintf (OutFptr, " 0},\\\n");
//
// The NameGuid may have been specified in the input flash definition file as a GUID, or
// as a quoted string. Do the right one.
//
if (Subregion->NameGuidString[0] != 0) {
fprintf (OutFptr, " %s, \\\n", Subregion->NameGuidString);
} else {
fprintf (
OutFptr,
" { 0x%08X, 0x%04X, 0x%04X, {0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X}},\\\n",
Subregion->NameGuid.Data1,
(unsigned int) Subregion->NameGuid.Data2,
(unsigned int) Subregion->NameGuid.Data3,
(unsigned int) Subregion->NameGuid.Data4[0],
(unsigned int) Subregion->NameGuid.Data4[1],
(unsigned int) Subregion->NameGuid.Data4[2],
(unsigned int) Subregion->NameGuid.Data4[3],
(unsigned int) Subregion->NameGuid.Data4[4],
(unsigned int) Subregion->NameGuid.Data4[5],
(unsigned int) Subregion->NameGuid.Data4[6],
(unsigned int) Subregion->NameGuid.Data4[7]
);
}
fprintf (OutFptr, " {0, 0, 0},\\\n");
fprintf (OutFptr, " %s,\\\n", Subregion->AreaType);
//
// The AreaTypeGuid may have been specified in the input flash definition file as a GUID, or
// as a quoted string. Do the right one.
//
if (Subregion->AreaTypeGuidString[0] != 0) {
fprintf (OutFptr, " %s, \\\n", Subregion->AreaTypeGuidString);
} else {
fprintf (
OutFptr,
" { 0x%08X, 0x%04X, 0x%04X, {0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X}},\\\n",
Subregion->AreaTypeGuid.Data1,
(unsigned int) Subregion->AreaTypeGuid.Data2,
(unsigned int) Subregion->AreaTypeGuid.Data3,
(unsigned int) Subregion->AreaTypeGuid.Data4[0],
(unsigned int) Subregion->AreaTypeGuid.Data4[1],
(unsigned int) Subregion->AreaTypeGuid.Data4[2],
(unsigned int) Subregion->AreaTypeGuid.Data4[3],
(unsigned int) Subregion->AreaTypeGuid.Data4[4],
(unsigned int) Subregion->AreaTypeGuid.Data4[5],
(unsigned int) Subregion->AreaTypeGuid.Data4[6],
(unsigned int) Subregion->AreaTypeGuid.Data4[7]
);
}
fprintf (OutFptr, " 1,\\\n");
fprintf (OutFptr, " {{\\\n");
fprintf (OutFptr, " %s,\\\n", Subregion->Attributes);
fprintf (OutFptr, " 0,\\\n");
fprintf (OutFptr, " FLASH_REGION_%s_SUBREGION_%s_BASE,\\\n", FBlock->Name, Subregion->Name);
fprintf (OutFptr, " FLASH_REGION_%s_SUBREGION_%s_SIZE,\\\n", FBlock->Name, Subregion->Name);
//
// The FileSystemGuid may have been specified in the input flash definition file as a GUID, or
// as a quoted string. Do the right one.
//
if (Subregion->FileSystemGuidString[0] != 0) {
fprintf (OutFptr, " %s, \\\n", Subregion->FileSystemGuidString);
} else {
fprintf (
OutFptr,
" { 0x%08X, 0x%04X, 0x%04X, {0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X}},\\\n",
Subregion->FileSystemGuid.Data1,
(unsigned int) Subregion->FileSystemGuid.Data2,
(unsigned int) Subregion->FileSystemGuid.Data3,
(unsigned int) Subregion->FileSystemGuid.Data4[0],
(unsigned int) Subregion->FileSystemGuid.Data4[1],
(unsigned int) Subregion->FileSystemGuid.Data4[2],
(unsigned int) Subregion->FileSystemGuid.Data4[3],
(unsigned int) Subregion->FileSystemGuid.Data4[4],
(unsigned int) Subregion->FileSystemGuid.Data4[5],
(unsigned int) Subregion->FileSystemGuid.Data4[6],
(unsigned int) Subregion->FileSystemGuid.Data4[7]
);
}
fprintf (OutFptr, " }},\\\n");
fprintf (OutFptr, " },");
}
}
}
}
fprintf (OutFptr, "\n\n");
}
//
// Write the file's closing #endif
//
fprintf (OutFptr, CIncludeFooter);
fclose (OutFptr);
return STATUS_SUCCESS;
}
STATUS
FDFCreateAsmIncludeFile (
char *FlashDeviceName,
char *FileName
)
/*++
Routine Description:
Create an assembly header file with equate definitions per an already-parsed
flash definition file.
Arguments:
FlashDeviceName - name of flash device (from the flash definition file)
to use
FileName - name of output file to create
Returns:
STATUS_SUCCESS - no errors or warnings
STATUS_WARNING - warnings, but no errors, were encountered
STATUS_ERROR - errors were encountered
--*/
{
FILE *OutFptr;
FLASH_BLOCK_DESCRIPTION *FBlock;
FLASH_DEVICE_DESCRIPTION *FDev;
unsigned int Offset;
FLASH_SUBREGION_DESCRIPTION *Subregion;
unsigned int SubregionOffset;
//
// Find the definition we're supposed to use
//
for (FDev = mFlashDevices; FDev != NULL; FDev = FDev->Next) {
if (strcmp (FDev->Name, FlashDeviceName) == 0) {
break;
}
}
if (FDev == NULL) {
Error (NULL, 0, 0, NULL, FlashDeviceName, "flash device not found in flash definitions");
return STATUS_ERROR;
}
if ((OutFptr = fopen (FileName, "w")) == NULL) {
Error (NULL, 0, 0, FileName, "failed to open output file for writing");
return STATUS_ERROR;
}
//
// Write a header
//
fprintf (OutFptr, "\n\n");
//
// Write flash block size and offset defines
//
fprintf (
OutFptr,
"FLASH_BASE %*cequ 0%08Xh\n",
COLUMN2_START - 40,
' ',
FDev->BaseAddress
);
fprintf (OutFptr, "FLASH_SIZE %*cequ 0%08Xh\n", COLUMN2_START - 40, ' ', FDev->Size);
//
// Write flash region size and offset defines
//
fprintf (OutFptr, "\n");
Offset = 0;
for (FBlock = FDev->Regions; FBlock != NULL; FBlock = FBlock->Next) {
fprintf (
OutFptr,
"FLASH_REGION_%s_BASE %*cequ 0%08Xh\n",
FBlock->Name,
COLUMN2_START - 20 - strlen (FBlock->Name),
' ',
FDev->BaseAddress + Offset
);
fprintf (
OutFptr,
"FLASH_REGION_%s_SIZE %*cequ 0%08Xh\n",
FBlock->Name,
COLUMN2_START - 20 - strlen (FBlock->Name),
' ',
FBlock->Size
);
fprintf (
OutFptr,
"FLASH_REGION_%s_OFFSET %*cequ 0%08Xh\n",
FBlock->Name,
COLUMN2_START - 20 - strlen (FBlock->Name),
' ',
Offset
);
//
// Create defines for any subregions
//
SubregionOffset = 0;
for (Subregion = FBlock->Subregions; Subregion != NULL; Subregion = Subregion->Next) {
fprintf (
OutFptr,
"FLASH_REGION_%s_SUBREGION_%s_BASE %*cequ 0%08Xh\n",
FBlock->Name,
Subregion->Name,
COLUMN3_START - 39 - strlen (FBlock->Name) - strlen (Subregion->Name),
' ',
FDev->BaseAddress + Offset + SubregionOffset
);
fprintf (
OutFptr,
"FLASH_REGION_%s_SUBREGION_%s_SIZE %*cequ 0%08Xh\n",
FBlock->Name,
Subregion->Name,
COLUMN3_START - 39 - strlen (FBlock->Name) - strlen (Subregion->Name),
' ',
Subregion->Size
);
fprintf (
OutFptr,
"FLASH_REGION_%s_SUBREGION_%s_OFFSET %*cequ 0%08Xh\n",
FBlock->Name,
Subregion->Name,
COLUMN3_START - 39 - strlen (FBlock->Name) - strlen (Subregion->Name),
' ',
Offset + SubregionOffset
);
SubregionOffset += Subregion->Size;
}
Offset += FBlock->Size;
}
//
// Write closing \n
//
fprintf (OutFptr, "\n\n");
fclose (OutFptr);
return STATUS_SUCCESS;
}
STATUS
FDFCreateSymbols (
char *FlashDeviceName
)
/*++
Routine Description:
Using the given flash device name, add symbols to the global symbol table. This
allows other functions to use the symbol definitions for other purposes.
Arguments:
FlashDeviceName - name of flash device (from the flash definition file)
to use
Returns:
STATUS_SUCCESS - no errors or warnings
STATUS_WARNING - warnings, but no errors, were encountered
STATUS_ERROR - errors were encountered
--*/
{
FLASH_BLOCK_DESCRIPTION *FBlock;
FLASH_DEVICE_DESCRIPTION *FDev;
unsigned int Offset;
char SymName[120];
char SymValue[120];
FLASH_SUBREGION_DESCRIPTION *Subregion;
unsigned int SubregionOffset;
//
// Find the definition we're supposed to use
//
for (FDev = mFlashDevices; FDev != NULL; FDev = FDev->Next) {
if (strcmp (FDev->Name, FlashDeviceName) == 0) {
break;
}
}
if (FDev == NULL) {
Error (NULL, 0, 0, NULL, FlashDeviceName, "flash device not found in flash definitions");
return STATUS_ERROR;
}
sprintf (SymValue, "0x%08X", FDev->BaseAddress);
SymbolAdd ("FLASH_BASE", SymValue, 0);
sprintf (SymValue, "0x%08X", FDev->Size);
SymbolAdd ("FLASH_SIZE", SymValue, 0);
//
// Add flash block size and offset defines
//
// Offset = 0;
// for (FBlock = FDev->PBlocks; FBlock != NULL; FBlock = FBlock->Next) {
// sprintf (SymName, "FLASH_BLOCK_%s_BASE", FBlock->Name);
// sprintf (SymValue, "0x%08X", FDev->BaseAddress + Offset);
// SymbolAdd (SymName, SymValue, 0);
// sprintf (SymName, "FLASH_BLOCK_%s_SIZE", FBlock->Name);
// sprintf (SymValue, "0x%08X", FBlock->Size);
// SymbolAdd (SymName, SymValue, 0);
// sprintf (SymName, "FLASH_BLOCK_%s_OFFSET", FBlock->Name);
// sprintf (SymValue, "0x%08X", Offset);
// SymbolAdd (SymName, SymValue, 0);
// Offset += FBlock->Size;
// }
//
// Add flash region block base, size, and offset defines
//
Offset = 0;
for (FBlock = FDev->Regions; FBlock != NULL; FBlock = FBlock->Next) {
sprintf (SymName, "FLASH_REGION_%s_BASE", FBlock->Name);
sprintf (SymValue, "0x%08X", FDev->BaseAddress + Offset);
SymbolAdd (SymName, SymValue, 0);
sprintf (SymName, "FLASH_REGION_%s_SIZE", FBlock->Name);
sprintf (SymValue, "0x%08X", FBlock->Size);
SymbolAdd (SymName, SymValue, 0);
sprintf (SymName, "FLASH_REGION_%s_OFFSET", FBlock->Name);
sprintf (SymValue, "0x%08X", Offset);
SymbolAdd (SymName, SymValue, 0);
//
// Add subregion symbols
//
if (FBlock->Subregions != NULL) {
SubregionOffset = 0;
for (Subregion = FBlock->Subregions; Subregion != NULL; Subregion = Subregion->Next) {
sprintf (SymName, "FLASH_REGION_%s_SUBREGION_%s_BASE", FBlock->Name, Subregion->Name);
sprintf (SymValue, "0x%08X", FDev->BaseAddress + Offset + SubregionOffset);
SymbolAdd (SymName, SymValue, 0);
sprintf (SymName, "FLASH_REGION_%s_SUBREGION_%s_SIZE", FBlock->Name, Subregion->Name);
sprintf (SymValue, "0x%08X", Subregion->Size);
SymbolAdd (SymName, SymValue, 0);
sprintf (SymName, "FLASH_REGION_%s_SUBREGION_%s_OFFSET", FBlock->Name, Subregion->Name);
sprintf (SymValue, "0x%08X", Offset + SubregionOffset);
SymbolAdd (SymName, SymValue, 0);
SubregionOffset += Subregion->Size;
}
}
Offset += FBlock->Size;
}
return STATUS_SUCCESS;
}
STATUS
FDFCreateImage (
char *FlashDeviceName,
char *ImageName,
char *FileName
)
/*++
Routine Description:
Create a flash image using the given device and image names.
Arguments:
FlashDeviceName - name of flash device (from the flash definition file)
to use
ImageName - name of image (from the flash definition file) to create
FileName - name of output file to create
Returns:
STATUS_SUCCESS - no errors or warnings
STATUS_WARNING - warnings, but no errors, were encountered
STATUS_ERROR - errors were encountered
--*/
{
STATUS Status;
FILE *OutFptr;
FLASH_BLOCK_DESCRIPTION *RegionDef;
FLASH_DEVICE_DESCRIPTION *FDev;
IMAGE_DEFINITION *ImageDef;
unsigned int Offset;
char *Buffer;
FILE *InFptr;
long FileSize;
IMAGE_DEFINITION_ENTRY *IDefEntry;
FLASH_SUBREGION_DESCRIPTION *SubregionDef;
//
// Find the flash definition we're supposed to use
//
InFptr = NULL;
Status = STATUS_ERROR;
for (FDev = mFlashDevices; FDev != NULL; FDev = FDev->Next) {
if (strcmp (FDev->Name, FlashDeviceName) == 0) {
break;
}
}
if (FDev == NULL) {
Error (NULL, 0, 0, FlashDeviceName, "flash device not found in flash definitions");
return STATUS_ERROR;
}
//
// Find the image name we're supposed to create
//
for (ImageDef = mImageDefinitions; ImageDef != NULL; ImageDef = ImageDef->Next) {
if (strcmp (ImageDef->Name, ImageName) == 0) {
break;
}
}
if (ImageDef == NULL) {
Error (NULL, 0, 0, ImageName, "image definition not found in image definitions");
return STATUS_ERROR;
}
//
// Open the output file
//
if ((OutFptr = fopen (FileName, "wb")) == NULL) {
Error (NULL, 0, 0, FileName, "failed to open output file for writing");
return STATUS_ERROR;
}
//
// Allocate a buffer to copy the input data to
//
Buffer = (char *) _malloc (FDev->Size);
if (Buffer == NULL) {
Error (NULL, 0, 0, (INT8 *) "failed to allocate memory", NULL);
goto Done;
}
//
// Set contents of buffer to the erased value
//
if (FDev->ErasePolarity) {
memset (Buffer, 0xFF, FDev->Size);
} else {
memset (Buffer, 0, FDev->Size);
}
//
// Set all region and subregion size-left fields to the size of the region/subregion
//
for (RegionDef = FDev->Regions; RegionDef != NULL; RegionDef = RegionDef->Next) {
RegionDef->SizeLeft = RegionDef->Size;
for (SubregionDef = RegionDef->Subregions; SubregionDef != NULL; SubregionDef = SubregionDef->Next) {
SubregionDef->SizeLeft = SubregionDef->Size;
}
}
//
// Now go through the image list, read files into the buffer.
//
for (IDefEntry = ImageDef->Entries; IDefEntry != NULL; IDefEntry = IDefEntry->Next) {
//
// If it's a file name, open the file, get the size, find the corresponding
// flash region it's in, and copy the data.
//
if (IDefEntry->IsRawData == 0) {
if ((InFptr = fopen (IDefEntry->Name, "rb")) == NULL) {
Error (NULL, 0, 0, IDefEntry->Name, "failed to open input file for reading");
goto Done;
}
fseek (InFptr, 0, SEEK_END);
FileSize = ftell (InFptr);
fseek (InFptr, 0, SEEK_SET);
} else {
FileSize = IDefEntry->RawDataSize;
}
//
// Find the region/subregion it's in, see if we have space left
//
Offset = 0;
for (RegionDef = FDev->Regions; RegionDef != NULL; RegionDef = RegionDef->Next) {
if (strcmp (RegionDef->Name, IDefEntry->RegionName) == 0) {
break;
}
Offset += RegionDef->Size;
}
if (RegionDef == NULL) {
Error (NULL, 0, 0, IDefEntry->RegionName, "Region name not found in FlashDevice %s definition", FDev->Name);
goto Done;
}
//
// Check for subregion
//
if (IDefEntry->SubregionName[0] != 0) {
for (SubregionDef = RegionDef->Subregions; SubregionDef != NULL; SubregionDef = SubregionDef->Next) {
if (strcmp (SubregionDef->Name, IDefEntry->SubregionName) == 0) {
break;
}
Offset += SubregionDef->Size;
}
if (SubregionDef == NULL) {
Error (
NULL,
0,
0,
IDefEntry->SubregionName,
"Subregion name not found in FlashDevice %s.%s Region definition",
FDev->Name,
RegionDef->Name
);
goto Done;
}
//
// Enough space in the subregion?
//
if (SubregionDef->SizeLeft < (unsigned int) FileSize) {
Error (
NULL,
0,
0,
IDefEntry->Name,
"insufficient space in Subregion (at least 0x%X additional bytes required)",
FileSize - SubregionDef->SizeLeft
);
goto Done;
}
//
// Read the file into the buffer if it's a file. Otherwise copy the raw data
//
if (IDefEntry->IsRawData == 0) {
if (fread (Buffer + Offset + (SubregionDef->Size - SubregionDef->SizeLeft), FileSize, 1, InFptr) != 1) {
Error (NULL, 0, 0, IDefEntry->Name, "failed to read file contents");
goto Done;
}
fclose (InFptr);
InFptr = NULL;
} else {
memcpy (
Buffer + Offset + (SubregionDef->Size - SubregionDef->SizeLeft),
IDefEntry->RawData,
IDefEntry->RawDataSize
);
}
SubregionDef->SizeLeft -= FileSize;
//
// Align based on the Region alignment requirements.
//
if (RegionDef->Alignment != 0) {
while (((unsigned int) (SubregionDef->Size - SubregionDef->SizeLeft) &~RegionDef->Alignment) != 0) {
if (SubregionDef->SizeLeft == 0) {
break;
}
SubregionDef->SizeLeft--;
}
}
} else {
//
// Placing data in a region. Check for enough space in the region left.
//
if (RegionDef->SizeLeft < (unsigned int) FileSize) {
Error (
NULL,
0,
0,
IDefEntry->Name,
"insufficient space in Region (at least 0x%X additional bytes required)",
FileSize - RegionDef->SizeLeft
);
goto Done;
}
//
// Read the file into the buffer if it's a file. Otherwise copy the raw data
//
if (IDefEntry->IsRawData == 0) {
if (fread (Buffer + Offset + (RegionDef->Size - RegionDef->SizeLeft), FileSize, 1, InFptr) != 1) {
Error (NULL, 0, 0, IDefEntry->Name, "failed to read file contents");
goto Done;
}
fclose (InFptr);
InFptr = NULL;
} else {
memcpy (Buffer + Offset + (RegionDef->Size - RegionDef->SizeLeft), IDefEntry->RawData, IDefEntry->RawDataSize);
}
RegionDef->SizeLeft -= FileSize;
//
// Align
//
if (RegionDef->Alignment != 0) {
while (((unsigned int) (RegionDef->Size - RegionDef->SizeLeft) &~RegionDef->Alignment) != 0) {
if (RegionDef->SizeLeft == 0) {
break;
}
RegionDef->SizeLeft--;
}
}
}
}
if (fwrite (Buffer, FDev->Size, 1, OutFptr) != 1) {
Error (NULL, 0, 0, "failed to write buffer contents to output file", NULL);
goto Done;
}
Status = STATUS_SUCCESS;
Done:
if (InFptr != NULL) {
fclose (InFptr);
}
if (Buffer != NULL) {
_free (Buffer);
}
if (OutFptr != NULL) {
fclose (OutFptr);
if (Status == STATUS_ERROR) {
remove (FileName);
}
}
return Status;
}
STATUS
FDFCreateDscFile (
char *FlashDeviceName,
char *FileName
)
/*++
Routine Description:
Create a DSC-style output file with equates for flash management.
Arguments:
FlashDeviceName - name of flash device (from the flash definition file)
to use
FileName - name of output file to create
Returns:
STATUS_SUCCESS - no errors or warnings
STATUS_WARNING - warnings, but no errors, were encountered
STATUS_ERROR - errors were encountered
--*/
{
FILE *OutFptr;
FLASH_BLOCK_DESCRIPTION *FBlock;
FLASH_DEVICE_DESCRIPTION *FDev;
unsigned int Offset;
FLASH_SUBREGION_DESCRIPTION *Subregion;
unsigned int SubregionOffset;
//
// Find the definition we're supposed to use
//
for (FDev = mFlashDevices; FDev != NULL; FDev = FDev->Next) {
if (strcmp (FDev->Name, FlashDeviceName) == 0) {
break;
}
}
if (FDev == NULL) {
Error (NULL, 0, 0, FlashDeviceName, "flash device not found in flash definitions");
return STATUS_ERROR;
}
if ((OutFptr = fopen (FileName, "w")) == NULL) {
Error (NULL, 0, 0, FileName, "failed to open output file for writing");
return STATUS_ERROR;
}
//
// Write the flash base address and size
//
fprintf (OutFptr, "\n");
fprintf (OutFptr, "FLASH_BASE = 0x%08X\n", FDev->BaseAddress);
fprintf (OutFptr, "FLASH_SIZE = 0x%08X\n\n", FDev->Size);
//
// Write flash block size and offset defines
//
Offset = 0;
for (FBlock = FDev->Regions; FBlock != NULL; FBlock = FBlock->Next) {
fprintf (
OutFptr,
"FLASH_REGION_%s_BASE %*c= 0x%08X\n",
FBlock->Name,
COLUMN2_START - 40 - strlen (FBlock->Name),
' ',
Offset + FDev->BaseAddress
);
fprintf (
OutFptr,
"FLASH_REGION_%s_SIZE %*c= 0x%08X\n",
FBlock->Name,
COLUMN2_START - 40 - strlen (FBlock->Name),
' ',
FBlock->Size
);
fprintf (
OutFptr,
"FLASH_REGION_%s_SIZE_BLOCKS %*c= 0x%x\n",
FBlock->Name,
COLUMN2_START - 40 - strlen (FBlock->Name),
' ',
(FBlock->Size)/(FDev->PBlocks->Size)
);
//
// Create defines for any subregions
//
SubregionOffset = 0;
for (Subregion = FBlock->Subregions; Subregion != NULL; Subregion = Subregion->Next) {
fprintf (
OutFptr,
"FLASH_REGION_%s_SUBREGION_%s_BASE %*c= 0x%08X\n",
FBlock->Name,
Subregion->Name,
COLUMN3_START - 48 - strlen (FBlock->Name) - strlen (Subregion->Name),
' ',
FDev->BaseAddress + Offset + SubregionOffset
);
fprintf (
OutFptr,
"FLASH_REGION_%s_SUBREGION_%s_SIZE %*c= 0x%08X\n",
FBlock->Name,
Subregion->Name,
COLUMN3_START - 48 - strlen (FBlock->Name) - strlen (Subregion->Name),
' ',
Subregion->Size
);
fprintf (
OutFptr,
"FLASH_REGION_%s_SUBREGION_%s_OFFSET %*c= 0x%08X\n",
FBlock->Name,
Subregion->Name,
COLUMN3_START - 48 - strlen (FBlock->Name) - strlen (Subregion->Name),
' ',
Offset + SubregionOffset
);
SubregionOffset += Subregion->Size;
}
Offset += FBlock->Size;
}
//
// Close file
//
fprintf (OutFptr, "\n");
fclose (OutFptr);
return STATUS_SUCCESS;
}
/*++
Routine Description:
The following buffer management routines are used to encapsulate functionality
for managing a growable data buffer.
Arguments:
BUFFER_DATA - structure that is used to maintain a data buffer
Returns:
NA
--*/
static
BUFFER_DATA *
CreateBufferData (
VOID
)
/*++
Routine Description:
Create a growable data buffer with default buffer length.
Arguments:
None
Returns:
NULL - error occured during data buffer creation
Not NULL - the pointer to the newly created data buffer
--*/
{
BUFFER_DATA *BD;
BD = (BUFFER_DATA *) _malloc (sizeof (BUFFER_DATA));
if (BD == NULL) {
Error (__FILE__, __LINE__, 0, "memory allocation failure", NULL);
return NULL;
}
memset (BD, 0, sizeof (BUFFER_DATA));
BD->BufferStart = (char *) _malloc (BUFFER_SIZE);
if (BD->BufferStart == NULL) {
_free (BD);
Error (__FILE__, __LINE__, 0, "memory allocation failure", NULL);
return NULL;
}
BD->BufferEnd = BD->BufferStart + BUFFER_SIZE;
BD->BufferPos = BD->BufferStart;
return BD;
}
static
BOOLEAN
AddBufferDataByte (
BUFFER_DATA *Buffer,
char Data
)
/*++
Routine Description:
Add a single byte to a growable data buffer, growing the buffer if required.
Arguments:
Buffer - pointer to the growable data buffer to add a single byte to
Data - value of the single byte data to be added
Returns:
TRUE - the single byte data was successfully added
FALSE - error occurred, the single byte data was not added
--*/
{
int Size;
char *NewBuffer;
//
// See if we have to grow the buffer
//
if (Buffer->BufferPos >= Buffer->BufferEnd) {
Size = (int) Buffer->BufferEnd - (int) Buffer->BufferStart;
NewBuffer = (char *) _malloc (Size + BUFFER_SIZE);
if (NewBuffer == NULL) {
Error (__FILE__, __LINE__, 0, "memory allocation failure", NULL);
return FALSE;
}
memcpy (NewBuffer, Buffer->BufferStart, Size);
_free (Buffer->BufferStart);
Buffer->BufferStart = NewBuffer;
Buffer->BufferPos = Buffer->BufferStart + Size;
Buffer->BufferEnd = Buffer->BufferStart + Size + BUFFER_SIZE;
}
*Buffer->BufferPos = Data;
Buffer->BufferPos++;
return TRUE;
}
static
void
FreeBufferData (
BUFFER_DATA *Buffer,
BOOLEAN FreeData
)
/*++
Routine Description:
Free memory used to manage a growable data buffer.
Arguments:
Buffer - pointer to the growable data buffer to be destructed
FreeData - TRUE, free memory containing the buffered data
FALSE, do not free the buffered data memory
Returns:
None
--*/
{
if (Buffer != NULL) {
if (FreeData && (Buffer->BufferStart != NULL)) {
_free (Buffer->BufferStart);
}
_free (Buffer);
}
}
static
char *
GetBufferData (
BUFFER_DATA *Buffer,
int *BufferSize
)
/*++
Routine Description:
Return a pointer and size of the data in a growable data buffer.
Arguments:
Buffer - pointer to the growable data buffer
BufferSize - size of the data in the growable data buffer
Returns:
pointer of the data in the growable data buffer
--*/
{
*BufferSize = (int) Buffer->BufferPos - (int) Buffer->BufferStart;
return Buffer->BufferStart;
}
STATUS
FDDiscover (
char *FDFileName,
unsigned int BaseAddr
)
/*++
Routine Description:
Walk a binary image and see if you find anything that looks like a
firmware volume.
Arguments:
FDFileName - name of input FD image to parse
BaseAddr - base address of input FD image
Returns:
STATUS_SUCCESS - no errors or warnings
STATUS_WARNING - warnings, but no errors, were encountered
STATUS_ERROR - errors were encountered
NOTE:
This routine is used for debug purposes only.
--*/
{
FILE *InFptr;
long FileSize;
long Offset;
EFI_FIRMWARE_VOLUME_HEADER FVHeader;
EFI_GUID
FileSystemGuid = { 0x7A9354D9, 0x0468, 0x444a, 0x81, 0xCE, 0x0B, 0xF6, 0x17, 0xD8, 0x90, 0xDF };
if ((InFptr = fopen (FDFileName, "rb")) == NULL) {
Error (NULL, 0, 0, FDFileName, "failed to open file for reading");
return STATUS_ERROR;
}
fseek (InFptr, 0, SEEK_END);
FileSize = ftell (InFptr);
fseek (InFptr, 0, SEEK_SET);
Offset = 0;
while (Offset < FileSize) {
fseek (InFptr, Offset, SEEK_SET);
//
// Read the contents of the file, see if it's an FV header
//
if (fread (&FVHeader, sizeof (EFI_FIRMWARE_VOLUME_HEADER), 1, InFptr) == 1) {
//
// Check version and GUID
//
if ((FVHeader.Revision == EFI_FVH_REVISION) && (FVHeader.Signature == EFI_FVH_SIGNATURE)) {
fprintf (stdout, "FV header at 0x%08X FVSize=0x%08X ", Offset + BaseAddr, (UINT32) FVHeader.FvLength);
if (memcmp (&FVHeader.FileSystemGuid, &FileSystemGuid, sizeof (EFI_GUID)) == 0) {
fprintf (stdout, "standard FFS file system\n");
} else {
fprintf (stdout, "non-standard FFS file system\n");
}
}
}
Offset += 16 * 1024;
}
fclose (InFptr);
return STATUS_SUCCESS;
}
|