summaryrefslogtreecommitdiff
path: root/Core/EM/FileSystem/Open.c
blob: cdfdabf0861dccbf98cc2b0b8453233dfd435835 (plain)
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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************

//**********************************************************************
// $Header: /Alaska/SOURCE/Core/Modules/FileSystem/Open.c 25    4/30/12 2:02p Pats $
//
// $Revision: 25 $
//
// $Date: 4/30/12 2:02p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Core/Modules/FileSystem/Open.c $
// 
// 25    4/30/12 2:02p Pats
// [TAG] EIP87352
// [Category] Bug Fix
// [Symptom] FAT Direcotry Opean/Creation Issue
// [RootCause] If a directory open was attempted with EFI_FILE_MODE_CREATE
// set, check for existing directory was not done.
// [Solution] Check for existing directory if opened with
// EFI_FILE_MODE_CREATE set in ProcessOpenFileHandle(). 
// [Files] Open.c
// 
// 24    11/28/11 11:09a Pats
// [TAG] - EIP 75631
// [Category] - BUG FIX
// [Severity] - Major
// [Symptom] - If an application name includes a path, it won't run.
// [Root Cause] - In the function ProcessOpenFileHandle(), the driver
// attempts to replace "\" in the path with a zero, but the code was one
// off, and the character past the "\" was being replaced.
// [Solution] - Modified the function ProcessOpenFileHandle() to replace
// the proper character.
// [Files] - Open.c
// 
// 23    10/24/11 10:55a Artems
// EIP 73254: Remove "magic number" from source
// 
// 22    5/05/11 3:44p Pats
// [TAG] - EIP 58999 
// [Category]- BUG FIX
// [Severity]- Major
// [Symptom] - Cannot launch Shell from USB Filesystem device in Debug
// mode with latest Filesystem driver.
// [RootCause] - Functions using DISKIO were raising TPL to a fixed level.
// [Solution] - Modified above functions to check for the higher of the
// fixed level or current level.
// [Files] - Info.c, MediaAccess.c, Open.c, FileSystem.h
// 
// 21    2/05/11 3:20p Artems
// Bug fix - restore original TPL in all branches of OpenFileHandle
// function
// 
// 20    1/13/11 12:24p Pats
// [TAG] - EIP 51705 
// [Category]- BUG FIX
// [Severity]- Major
// [Symptom] - If the volume label is the same name as a directory or
// file, the directory or file cannot be accessed. Also, a new directory
// cannot be created with the same name as the volume.
// [RootCause] - The functions FindMatchingFH() and FindMatchingDirEntry()
// did not check for ATTR_VOLUME_ID.
// [Solution] - Modified above functions to check for ATTR_VOLUME_ID.
// [Files] - open.c
// 
// 19    1/06/11 5:53p Oleksiyy
// [TAG]  		EIP28607 
// [Category]  	Improvement
// [Description]  	System was hanging for some time if Floppy Media
// removed while writing in meda in progress.
// EFI_TPL_CALLBACK priority level rised during media related calls.
// 
// [Files]  		DiskIo.c, Info.c, MediaAccess.c, Open.c, Partition.c
// 
// 18    1/06/11 5:03p Pats
// [TAG] - EIP 49934
// [Category]- BUG FIX
// [Severity]- Minor
// [Symptom] - The new EDKII shell was not displaying filesystem devices
// properly.
// [RootCause] - Root directory file name was "\", with a size of one.
// Shell expected a null name for the root directory.
// [Solution] - Changed root directory name to null.
// [Files] - open.c, function OpenRoot().
// 
// 17    12/03/10 10:08a Pats
// EIP 44532: USB key is not returning file list as expected.
// Problem: ReadFIleHandle was not resetting pointers when a directory
// read returned an error.
// Solution. Changed ReadFileHandle to reset pointers on error condition.
// 
// 16    7/07/10 3:00p Pats
// EIP 38291: Fails Klocwork test.
// Problem: No error return if file handle is null in
// ProcessOpenFileHandle()
// Solution: Added error return.
// Problem: Variable vi initialized improperly.
// Solution: Changed initialization (moved to after possible error exit).
// 
// 15    6/11/10 5:39p Pats
// EIP 39171: Long name sometimes not displayed, only short name.
// FetchLongName() in Open.c did not handle case where long name spilt
// over 2 clusters. Function modified to handle the case.
// 
// 14    1/22/10 4:39p Yul
// Refer to EIP 32983.
// 
// 13    12/17/09 12:44p Felixp
// SCT workaround: CloseFileHandle function is updated to validate input
// parameter before using it (EIP 32474).
// SCT passes handle of the deleted file.
// 
// 12    7/02/09 5:47p Pats
// Updated to latest coding standard. No code changes.
//
// 11    7/30/08 11:36a Pats
// Bug fix for file renaming.
//
// 10    4/13/07 7:07p Pats
// Edited to conform with coding standards. No code changes.
//
// 9     8/24/06 9:32a Felixp
// bug fix in CreateDirEntryInDisk
//
// 8     8/16/06 12:03p Markw
// Fixed UINTN* and UINT32* 64-bit issues.
//
// Fixed VC7.1 warning msg.
//
// 4     6/21/05 4:00p Pats
// Modified to call MarkVolumeDirty when EFI_VOLUME_CORRUPTED error
// occurs. Removed commented-out debug code.
//
// 3     6/21/05 9:57a Pats
// Corrected some spelling to match changes in Info.c.
//
// 2     5/27/05 10:02a Srinin
// Fix for Media change and free space calculation.
//
// 1     4/26/05 6:05p Srinin
//
//
//
//**********************************************************************

//<AMI_FHDR_START>
//----------------------------------------------------------------------
//
// Name: Open.c
//
// Description:  Handles Opening File Handles.
//
//----------------------------------------------------------------------
//<AMI_FHDR_END>

//----------------------------------------------------------------------

#include "FileSystem.h"
#define EFI_TPL_CALLBACK TPL_CALLBACK
//----------------------------------------------------------------------

extern EFI_GUID gEfiFileSystemVolumeLabelGuid;
extern EFI_GUID gEfiFileInfoGuid;
extern EFI_GUID gEfiFileSystemInfoGuid;

//----------------------------------------------------------------------


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   OpenVolume
//
// Description: Opens a file system Volume.
//
// Parameters:
//  IN  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This - File System Protocol Instance
//  OUT EFI_FILE_PROTOCOL **Root
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
OpenVolume (
    IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,
    OUT EFI_FILE_PROTOCOL              **Root
)
{

    EFI_STATUS           Status;
    FILE_HANDLE          *RootHandle = NULL;
    FILE_HANDLE_INSTANCE *RootInstance = NULL;
    VOLUME_INFO          *VolumeInfo = (VOLUME_INFO *)This;
    FAT_TYPE             Old_FAT_Type = VolumeInfo->FatType;
    UINT32               Length;
    
    if (VolumeInfo->RootFH && (VolumeInfo->VolumeStatus == EFI_MEDIA_CHANGED || VolumeInfo->VolumeStatus == EFI_NO_MEDIA)) {
        FreeUpResources (VolumeInfo, VolumeInfo->VolumeStatus); // Media changed occurred earlier
    }
    
    if (VolumeInfo->RootFH) {
        if (VolumeInfo->BlkIo->Media->RemovableMedia) {
            Status = DetectVolume(VolumeInfo, TRUE); // Just reads the sector 0
            
            if (EFI_ERROR(Status)) {
                FreeUpResources (VolumeInfo, Status);
                goto ContinueOpen;
            }
        }
        
        RootHandle = VolumeInfo->RootFH;  // Everything is fine
        
    } else {
ContinueOpen:
        VolumeInfo->VolumeStatus = EFI_UNSUPPORTED;
        VolumeInfo->BlockSize = VolumeInfo->BlkIo->Media->BlockSize;
        VolumeInfo->ReadOnly = VolumeInfo->BlkIo->Media->ReadOnly;
        VolumeInfo->MediaID = VolumeInfo->BlkIo->Media->MediaId;
        Status = DetectVolume(VolumeInfo, FALSE);
        
        if (EFI_ERROR(Status)) return Status;
        
        if (Old_FAT_Type && Old_FAT_Type != VolumeInfo->FatType) return EFI_UNSUPPORTED;
        
//  Create a FILE_HANDLE for Root Directory. Called for the first time.
//  VolumeInfo->VolumeSize = VolumeInfo->TotalSectors * VolumeInfo->VolumeBPB.BPB_BytePerSec;
//  Only count Data Sectors when determining Volume Size, to match DOS
        VolumeInfo->VolumeSize = Mul64((UINT64)VolumeInfo->DataSectors, (UINT32)VolumeInfo->VolumeBPB.BPB_BytePerSec);
        VolumeInfo->FreeSpaceinClusters = (UINTN)-1; // Means not calculated yet
        VolumeInfo->FreeSpaceinSector1 = (UINTN)-1;
        Status= OpenRoot(VolumeInfo, &RootHandle);
        VolumeInfo->LastFreeCluster = (UINTN)-1;
        
        if (EFI_ERROR(Status)) return Status;
        
        VolumeInfo->RootFH = RootHandle;
		DListAdd((DLIST *)(UINTN)&(VolumeInfo->OpenFHs),(DLINK *)(UINTN)&(RootHandle->ViFHLink)); 
    }
    
    if (!VolumeInfo->FileSystemName) {
        Status = pBS->AllocatePool (EfiBootServicesData, 80, (VOID**)&VolumeInfo->FileSystemName);
        Wcscpy (VolumeInfo->FileSystemName, L"FAT");
        Length = (UINT32) Wcslen(VolumeInfo->FileSystemName);
        
        switch (VolumeInfo->FatType) {
            case  FAT12:
                Wcscpy (VolumeInfo->FileSystemName + Length, L"12 ");
                break;
            case  FAT16:
                Wcscpy (VolumeInfo->FileSystemName + Length, L"16 ");
                break;
            case  FAT32:
                Wcscpy (VolumeInfo->FileSystemName + Length, L"32 ");
                break;
            default:
                Wcscpy (VolumeInfo->FileSystemName + Length, L"UnKnown ");
        };
        
        Length = (UINT32) Wcslen(VolumeInfo->FileSystemName);
        
        Wcscpy (VolumeInfo->FileSystemName + Length, L"File System [");
        
        Length = (UINT32) Wcslen(VolumeInfo->FileSystemName);
        
        if (VolumeInfo->VolumeSize < 0x100000)  { //Less than 1MB
            ItowEx(Shr64 (VolumeInfo->VolumeSize, 10), VolumeInfo->FileSystemName + Length, 10, FALSE);
            Length = (UINT32) Wcslen(VolumeInfo->FileSystemName);
            Wcscpy (VolumeInfo->FileSystemName + Length, L"KB]");
            
        } else if (VolumeInfo->VolumeSize < 0x40000000)  { //Less than 1GB
            ItowEx(Shr64 (VolumeInfo->VolumeSize, 20), VolumeInfo->FileSystemName + Length, 10, FALSE);
            Length = (UINT32) Wcslen(VolumeInfo->FileSystemName);
            Wcscpy (VolumeInfo->FileSystemName + Length, L"MB]");
            
        } else {
            ItowEx(Shr64 (VolumeInfo->VolumeSize, 30), VolumeInfo->FileSystemName + Length, 10, FALSE);
            Length = (UINT32) Wcslen(VolumeInfo->FileSystemName);
            Wcscpy (VolumeInfo->FileSystemName + Length, L"GB]");
        }
    }
    
    
//  Create a FILE_HANDLE_INSTANCE
    Status = CreateFileHandleInstance(RootHandle, &RootInstance, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE);
    
//  Return FILE_HANDLE_INSTANCE
    if (Status == EFI_SUCCESS) *Root = (EFI_FILE_PROTOCOL *)RootInstance;
    
    VolumeInfo->VolumeStatus = Status;
    return Status;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure: OpenRoot
//
// Description: Opens the Root Directory on the Volume.
//
// Parameters:
//  IN VOLUME_INFO        *VolumeInfo - Volume Info Structure
//  IN OUT FILE_HANDLE    **Root - Root directory handle
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
OpenRoot(
    IN VOLUME_INFO     *VolumeInfo,
    IN OUT FILE_HANDLE **Root
)
{

    EFI_STATUS  Status;
    FILE_HANDLE *RootHandle;
    
    Status = fsAllocateMemory(VolumeInfo, sizeof(FILE_HANDLE), (VOID**)&RootHandle, TRUE);
    
    if (EFI_ERROR(Status)) return EFI_OUT_OF_RESOURCES;
    
    Status = fsAllocateMemory(VolumeInfo, 4, (VOID**)&(RootHandle->FileNameLFN), FALSE);
    
    RootHandle->FileNameLFN[0]= 0;
    RootHandle->ROOTDIR = TRUE;
    RootHandle->VolumeInterface = VolumeInfo;
    RootHandle->DirectoryEntry.Dir_Attr =  EFI_FILE_DIRECTORY;
    RootHandle->HandleStatus=  EFI_SUCCESS;
    
    if (VolumeInfo->FatType == FAT32) {
        RootHandle->DirCluster = VolumeInfo->VolumeBPB.BPB_FAT.BPB_FAT32.BPB_RootClus;
        RootHandle->DirOffset = 0;
    }
    
	DListInit((DLIST *)(UINTN)&(RootHandle->DirList));
    *Root = RootHandle;
    
    return EFI_SUCCESS;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   CreateFileHandleInstance
//
// Description: Creates an instance of a file handle
//
// Parameters:
//  IN FILE_HANDLE              *fh - Handle of the file to work on
//  IN OUT FILE_HANDLE_INSTANCE **fhi - File handle instance
//  IN UINT64                   OpenMode - Open mode (read, write, both)
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
CreateFileHandleInstance (
    IN FILE_HANDLE              *fh,
    IN OUT FILE_HANDLE_INSTANCE **fhi,
    IN UINT64                   OpenMode
)
{

    EFI_STATUS           Status;
    VOLUME_INFO          *Vi = fh->VolumeInterface;
    FILE_HANDLE_INSTANCE *nfi;
    
    Status = fsAllocateMemory(Vi, sizeof(FILE_HANDLE_INSTANCE), (VOID**)&nfi, TRUE);
    
    if (EFI_ERROR(Status)) return EFI_OUT_OF_RESOURCES;
    
    nfi->Position = 0;
    nfi->HandleInstanceStatus = EFI_SUCCESS;
    nfi->CurrentCluster = FIRSTCLUSTER(fh->DirectoryEntry);
    nfi->pFH = fh;
    nfi->OpenMode = OpenMode;
    InitEFIFileInterface(&(nfi->FileHandle));
    fh->InstancesCount++;
	DListAdd((DLIST *)(UINTN)&(Vi->OpenFIs), (DLINK *)(UINTN)&(nfi->ViFILink));
    *fhi = nfi;
    return EFI_SUCCESS;
    
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   InitEFIFileInterface
//
// Description: Initializes the EFI_FILE_PROTOCOL interface
//
// Parameters:
//  EFI_FILE_PROTOCOL *EFIFileInterface - Interface structure
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: OpenProtocol CloseProtocol
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

void
InitEFIFileInterface (
    EFI_FILE_PROTOCOL *EFIFileInterface
)
{

    EFIFileInterface->Revision = SIMPLE_FILE_SYSTEM_DRIVER_VERSION;
    EFIFileInterface->Open = OpenFileHandle;
    EFIFileInterface->Close = CloseFileHandle;
    EFIFileInterface->Delete = DeleteFileHandle;
    EFIFileInterface->Read = ReadFileHandle;
    EFIFileInterface->Write = WriteFileHandle;
    EFIFileInterface->GetPosition = GetPositionFileHandle;
    EFIFileInterface->SetPosition = SetPositionFileHandleThis;
    EFIFileInterface->GetInfo = GetInfoFileHandle;
    EFIFileInterface->SetInfo = SetInfoFileHandle;
    EFIFileInterface->Flush = FlushFileHandle;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   OpenFileHandle
//
// Description: Opens a file handle (when a file is opened)
//
// Parameters:
//  IN EFI_FILE_PROTOCOL  *This - File System Protocol instance
//  OUT EFI_FILE_PROTOCOL **NewHandle - Handle of open file
//  IN CHAR16             *FileName - Unicode name of file
//  IN UINT64             OpenMode - Open mode (read, write, both)
//  IN UINT64             Attributes - Attributes of open file
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
OpenFileHandle (
    IN EFI_FILE_PROTOCOL  *This,
    OUT EFI_FILE_PROTOCOL **NewHandle,
    IN CHAR16             *FileName,
    IN UINT64             OpenMode,
    IN UINT64             Attributes
)
{

    EFI_STATUS           Status;
    FILE_HANDLE_INSTANCE *Fi= (FILE_HANDLE_INSTANCE *) This;
	FILE_HANDLE				*ParentFh;
	VOLUME_INFO				*Vi;
    EFI_TPL                 SaveTpl;
    EFI_TPL                 NextTpl;
#ifdef Debug_OpenFile
    EfiDebugPrint(-1,"OpenFile: %S OpenMode %lx Attribute %lx\n", FileName, OpenMode, Attributes);
#endif
    
//  Validate Inputs
    switch (OpenMode) {
        case (EFI_FILE_MODE_READ):
        case (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE):
        case (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE):
            break;
        default:
            return EFI_INVALID_PARAMETER;
    }
    NextTpl = FindNextTpl (EFI_TPL_CALLBACK);
    SaveTpl = pBS->RaiseTPL (NextTpl);
	Status = ValidateCurrentStatus (Fi);
	if (EFI_ERROR(Status)) 
    {
        pBS->RestoreTPL(SaveTpl);
        return Status;
    }
    ParentFh = Fi->pFH;
    Vi = ParentFh->VolumeInterface;
	if (Vi->ReadOnly && OpenMode & EFI_FILE_MODE_WRITE) 
    {
        pBS->RestoreTPL(SaveTpl);
        return EFI_WRITE_PROTECTED;
    }
    if (FileName == NULL) 
    {
        pBS->RestoreTPL(SaveTpl);
        return EFI_NOT_FOUND;
    }
    if ( Attributes & ~EFI_FILE_VALID_ATTR) 
    {
        pBS->RestoreTPL(SaveTpl);
        return EFI_NOT_FOUND;
    }
    Status = ProcessOpenFileHandle (Fi, &FileName, NewHandle, OpenMode, Attributes);
    pBS->RestoreTPL(SaveTpl);    
#ifdef Debug_OpenFile
    EfiDebugPrint(-1,"OpenFile Status:  %x\n", Status);
#endif
    return Status;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   ReadFileHandle
//
// Description: Read from a file (normal file or directory)
//
// Parameters:
//  IN EFI_FILE_PROTOCOL *This - File System Protocol instance
//  IN OUT UINTN         *BufferSize - Size of read buffer
//  OUT VOID             *Buffer - Points to read buffer
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
ReadFileHandle (
    IN EFI_FILE_PROTOCOL *This,
    IN OUT UINTN         *BufferSize,
    OUT VOID             *Buffer
)
{

    EFI_STATUS              Status = EFI_UNSUPPORTED;
    FILE_HANDLE_INSTANCE    *fhi = (FILE_HANDLE_INSTANCE *)This;
	FILE_HANDLE				*Pfh;
	FILE_HANDLE				*fh1; 
	DIR_ENTRY_LIST			*Del;
	UINT32					Position;
    FILE_HANDLE_INSTANCE    TempFhi;
    FILE_HANDLE             TempPfh;
    EFI_TPL                 SaveTpl;
    EFI_TPL                 NextTpl;

    if (*BufferSize > 0xffffffff) return EFI_INVALID_PARAMETER;

    TempFhi = *fhi;
	Status = ValidateCurrentStatus (fhi);
	if (EFI_ERROR(Status)) return Status;
    Pfh = fhi->pFH;
    TempPfh = *Pfh;
#ifdef Debug_ReadFile
	EfiDebugPrint(TRACE_ALWAYS,"ReadFile: %S Position %lx Length %x \n", Pfh->FileNameLFN, fhi->Position, *BufferSize);
#endif
    NextTpl = FindNextTpl (EFI_TPL_CALLBACK);
    SaveTpl = pBS->RaiseTPL (NextTpl);

//  Check if this Instance is a Directory.
    if (Pfh->DirectoryEntry.Dir_Attr & ATTR_DIRECTORY) {
    
//  Check if Dir List has been changed. If so, release the list and read it again.
        if (Pfh->DirListChanged == TRUE) {
            RemoveAllDirList (Pfh);
            Pfh->DirListChanged = FALSE;
        }
        
//  Check if the file Handle has a valid DIR_ENTRY_LIST
        if (!Pfh->DirList.pHead) {
            Status = ReadAllDirectoryEntries (Pfh);
            
            if (EFI_ERROR(Status)) {
                pBS->RestoreTPL(SaveTpl);
                return Status;
            }
        }
        
//  If Dir_List has been changed, this should take care of it.
        if (fhi->Position != 0) {
            Status = LocateDel (Pfh->DirList.pHead, fhi->CurrentCluster, fhi->CurrentClusterOffset, &Del, &Position);
            fhi->Position = Position;
        }
        
        
        for (;; fhi->Position++) {
            Status = GetDirListAtPosition (Pfh, fhi->Position, &Del);
            
            if (!Del) {
                *BufferSize = 0;    //  End of entries
                pBS->RestoreTPL(SaveTpl);
                return EFI_SUCCESS;
            }
            
//    Check whether it is a Label
            if ((Del->DirectoryEntry.Dir_Attr & (ATTR_DIRECTORY | ATTR_VOLUME_ID)) != ATTR_VOLUME_ID)
                break;
        }
        
///////////////////////////////////////////////
//A long Process. Do we need to reduce it
        Status = CreateFHFromDirEntryList(Del, Pfh, &fh1);
        
        if (EFI_ERROR(Status)){
            pBS->RestoreTPL(SaveTpl);
            return Status;
        }
        
        Status = GetFileInfoFromFH (fh1, BufferSize, Buffer);

        if (EFI_ERROR(Status)) {
            *fhi = TempFhi;
            *Pfh = TempPfh;
            pBS->RestoreTPL(SaveTpl);
            return Status;
        }

//A long Process. Do we need to reduce it
///////////////////////////////////////////////

        fhi->Position++;
        
//  Make sure CurrentCluster and CurrentClusterOffset gets updated to the new position
        for (;; fhi->Position++) {
            Status = GetDirListAtPosition (Pfh, fhi->Position, &Del);
            
            if (!Del) break;
            
//    Check whether it is a Label
            if ((Del->DirectoryEntry.Dir_Attr & (ATTR_DIRECTORY | ATTR_VOLUME_ID)) != ATTR_VOLUME_ID)
                break;
        }
        
//  Update the CurrentCluster, CurrentClusterOffset for the new position
        if (Del) {
            fhi->CurrentCluster =  Del->Cluster;
            fhi->CurrentClusterOffset = Del->Offset;
            
        } else {
            fhi->CurrentCluster = (UINTN) -1;
            fhi->CurrentClusterOffset = (UINTN)-1;
            Status = EFI_SUCCESS;
        }
        
        RemoveFH(fh1);
        
    } else {
//  It is File.
        //Buffer size is on input is required to be less than 4G.
        Status = ReadFromFile (fhi, Buffer, (UINT32*)BufferSize, DATA_REGION);
    }
    pBS->RestoreTPL(SaveTpl);
    return Status;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   WriteFileHandle
//
// Description: Writes to a file
//
// Parameters:
//  IN EFI_FILE_PROTOCOL *This,
//  IN OUT UINTN         *BufferSize,
//  IN VOID              *Buffer
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
WriteFileHandle (
    IN EFI_FILE_PROTOCOL *This,
    IN OUT UINTN         *BufferSize,
    IN VOID              *Buffer
)
{
    EFI_TPL              SaveTpl;
    EFI_TPL              NextTpl;
    EFI_STATUS           Status;
    FILE_HANDLE_INSTANCE *fhi = (FILE_HANDLE_INSTANCE *)This;
    
#ifdef Debug_WriteFile
	FILE_HANDLE				*Pfh;
	VOLUME_INFO				*vi;
#endif

    if (*BufferSize > 0xffffffff) return EFI_INVALID_PARAMETER;

	Status = ValidateCurrentStatus (fhi);
	if (EFI_ERROR(Status)) return Status;
    NextTpl = FindNextTpl (EFI_TPL_CALLBACK);
    SaveTpl = pBS->RaiseTPL (NextTpl);
#ifdef Debug_WriteFile
	Pfh = fhi->pFH;
	vi = Pfh->VolumeInterface;	
	EfiDebugPrint(TRACE_ALWAYS,"WriteFile: %S FSize %x Pos %lx Len %x LoCluster %x\n", 
				Pfh->FileNameLFN, Pfh->DirectoryEntry.Dir_FileSize, fhi->Position, *BufferSize, Pfh->DirectoryEntry.Dir_FstClusLO);
#endif
     
	Status = CheckFileWrite (fhi, FALSE);
    if (EFI_ERROR(Status)){
        pBS->RestoreTPL(SaveTpl); 
        return Status;
    }
    
    //Buffer size is on input is required to be less than 4G.
    Status = WriteToFile (fhi, Buffer, (UINT32*)BufferSize, DATA_REGION);
    pBS->RestoreTPL(SaveTpl);
    return Status;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   SetInfoFileHandle
//
// Description: Sets file info
//
// Parameters:
//  IN EFI_FILE_PROTOCOL  *This,
//  IN EFI_GUID           *InformationType,
//  IN UINTN              BufferSize,
//  IN VOID               *Buffer
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
SetInfoFileHandle(
    IN EFI_FILE_PROTOCOL *This,
    IN EFI_GUID          *InformationType,
    IN UINTN             BufferSize,
    IN VOID              *Buffer
)
{


    EFI_STATUS           Status = EFI_UNSUPPORTED;
    FILE_HANDLE_INSTANCE *fhi = (FILE_HANDLE_INSTANCE *)This;
	FILE_HANDLE				*Pfh;
	VOLUME_INFO				*vi;	
    EFI_TPL                 SaveTpl;
    EFI_TPL                 NextTpl;

	Status = ValidateCurrentStatus (fhi);
	if (EFI_ERROR(Status)) return Status;
    Pfh = fhi->pFH;
    vi = Pfh->VolumeInterface;
	if (vi->ReadOnly) return EFI_WRITE_PROTECTED;
    
//  Compare the GUID
    NextTpl = FindNextTpl (EFI_TPL_CALLBACK);
    SaveTpl = pBS->RaiseTPL (NextTpl);
    if (!guidcmp(InformationType, &gEfiFileInfoGuid)) {
        Status = SetfileInfo (This, fhi, BufferSize, Buffer);
        
        if (EFI_ERROR(Status)){
            pBS->RestoreTPL(SaveTpl);
            return Status;
        }
    }
    
    else {
        if (!guidcmp(InformationType, &gEfiFileSystemInfoGuid)) {
            Status = SetSystemInfo (vi, BufferSize, (EFI_FILE_SYSTEM_INFO *)Buffer);
            
            if (EFI_ERROR(Status)){
                pBS->RestoreTPL(SaveTpl);
                return Status;
            }
            
            Status = FlushFileHandle(This);
            
            if (EFI_ERROR(Status)){
                pBS->RestoreTPL(SaveTpl);
                return Status;
            }
            
        } else {
            if (!guidcmp(InformationType, &gEfiFileSystemVolumeLabelGuid)) {
                Status = SetVolumeLabel (vi, BufferSize, Buffer);
                
                if (EFI_ERROR(Status)){
                    pBS->RestoreTPL(SaveTpl);
                    return Status;
                }
                
                Status = FlushFileHandle(This);
                
                if (EFI_ERROR(Status)) {
                    pBS->RestoreTPL(SaveTpl);
                    return Status;
                }
                
            } else {
                pBS->RestoreTPL(SaveTpl);
                return EFI_UNSUPPORTED;
            }
        }
    }
    
    pBS->RestoreTPL(SaveTpl);
    return Status;
    
}



//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   DeleteFileHandle
//
// Description: Deletes a file
//
// Parameters:
//  IN EFI_FILE_PROTOCOL *This - File Protocol instance
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes: For FAT32, Cluster(s) occupied by root directory will not be released
//      as Del/rm command will not give a call to delete RootHandle.
//      In a empty FAt32 disk, free space will be less by clusters occupied by ROOT Directory
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
DeleteFileHandle (
    IN EFI_FILE_PROTOCOL *This
)
{

    EFI_STATUS           Status;
    FILE_HANDLE_INSTANCE *fhi = (FILE_HANDLE_INSTANCE *)This, *fhi1;
	FILE_HANDLE				*fh;
	FILE_HANDLE				*PPfh;
	VOLUME_INFO				*vi;	
	UINT32					ClusterCount;
//	UINT32					Sector, SectorOffset, ClusterCount;
	UINT8					DirErase = DIR_ENTRY_ERASE;
	DLINK					*dlink;
	DIR_ENTRY_LIST			*Del;
	UINT32					Position;
    EFI_TPL                 SaveTpl;
    EFI_TPL                 NextTpl;

	Status = ValidateCurrentStatus (fhi);
	if (EFI_ERROR(Status)) return Status;

    fh = fhi->pFH;
    vi = fh->VolumeInterface;
    PPfh = fh->Parent;
    dlink = PPfh->DirList.pHead ;

#ifdef Debug_DeleteFile
	EfiDebugPrint(TRACE_ALWAYS,"Del File: %S Dir Cluster %x Dir Offset %x\n", fh->FileNameLFN, fh->DirCluster, fh->DirOffset);
#endif
    

	if ( vi->ReadOnly || fh->ROOTDIR 
       || fh->DirectoryEntry.Dir_Attr & ATTR_READ_ONLY
       || !(fhi->OpenMode & EFI_FILE_MODE_WRITE)){

        CloseFileHandle (This);
        
#ifdef Debug_DeleteFile
        EfiDebugPrint(-1,"DEL WARN FAILURE Instances Open %x\n", fh->InstancesCount);
#endif
        return EFI_WARN_DELETE_FAILURE;
    }
    
    if (fh->DirectoryEntry.Dir_Attr & ATTR_DIRECTORY) {
        if (fh->DirList.Size > 2) {
            CloseFileHandle (This);
#ifdef Debug_DeleteFile
            EfiDebugPrint(-1,"DEL WARN FAILURE Dir Not Empty\n");
#endif
            return EFI_WARN_DELETE_FAILURE;
        }
        
//###DEBUG
//  TODO TODO TODO
//  Before removing the Directory Entry, check if it is empty.
//  TODO TODO TODO
//##DEBUG END
//    Release all Dir_List
        RemoveAllDirList(fh);
    }
    
    NextTpl = FindNextTpl (EFI_TPL_CALLBACK);
    SaveTpl = pBS->RaiseTPL (NextTpl);
    
    if (fh->DirCluster || fh->DirOffset) {
        fh->DirectoryEntry.Dir_Name[0] = DirErase;
        fh->DirEntryChanged = TRUE;
//  Update the FAT Chain
        UnlinkFat(vi, FIRSTCLUSTER(fh->DirectoryEntry), &ClusterCount);
    }
    
//  Check if DIR_LIST is valid. If yes, remove from the list.
    Status = LocateDel (fh->Parent->DirList.pHead, fh->DirCluster, fh->DirOffset, &Del, &Position);
    
    if (!EFI_ERROR(Status)) RemoveDirList(PPfh, Del);
    
//  Force next Scan for the parent directory to begin from the beginning
    fh->Parent->FreeDirEntryCluster = 0;
    fh->Parent->FreeDirEntryOffset = 0;
    
//  Remove File Handle and File List
    CloseFileHandle (This);
    
//  Check for multiple instances
    if (fh->InstancesCount ) {
        dlink = vi->OpenFIs.pHead;
        
        for ( ; dlink; ) {
            fhi1 = OUTTER(dlink, ViFILink, FILE_HANDLE_INSTANCE);
            
            if (fhi1->pFH == fh) {
                fhi1->HandleInstanceStatus = EFI_NOT_FOUND;
                fhi1->pFH = NULL;
                fh->InstancesCount--;
            }
            
            dlink = dlink->pNext;
        }
    }
    
    Status = RemoveFH(fh);
    pBS->RestoreTPL(SaveTpl);    
#ifdef Debug_DeleteFile
    EfiDebugPrint(-1,"Del File Success\n");
#endif
    
    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   FlushFileHandle
//
// Description: Flushes a file (writes any portion left in cache)
//
// Parameters:
//  IN EFI_FILE_PROTOCOL *This - File Protocol instance
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
FlushFileHandle (
    IN EFI_FILE_PROTOCOL *This
)
{

    EFI_STATUS           Status;
    FILE_HANDLE_INSTANCE *fhi = (FILE_HANDLE_INSTANCE *)This;
	FILE_HANDLE				*Pfh;
	VOLUME_INFO				*vi;
	DLINK					*dlink;
	CACHE_HEADER			*ch;
    EFI_TPL                 SaveTpl;
    EFI_TPL                 NextTpl;

	Status = ValidateCurrentStatus (fhi);
	if (EFI_ERROR(Status)) return Status;

    Pfh =fhi->pFH;
    vi = Pfh->VolumeInterface;
    dlink = vi->CacheList.pHead;
    if (fhi->OpenMode == EFI_FILE_MODE_READ) return EFI_ACCESS_DENIED;
    
    if (vi->ReadOnly) return EFI_ACCESS_DENIED;
    NextTpl = FindNextTpl (EFI_TPL_CALLBACK);
    SaveTpl = pBS->RaiseTPL (NextTpl);
    for ( ; dlink; ) {
    
        ch = OUTTER(dlink, CacheLink, CACHE_HEADER);
        
        if (ch->DIRTY_FLAG) {
            Status= vi->DiskIo->WriteDisk (vi->DiskIo, vi->MediaID, ch->AbsoluteOffset, (UINT32) (ch->AbsoluteOffsetEnd - ch->AbsoluteOffset), ch->Buffer);
            
            if (EFI_ERROR(Status)) {
                HandleDiskIoError(vi, Status);
                pBS->RestoreTPL(SaveTpl);
                return Status;
            }
            
            ch->DIRTY_FLAG = FALSE;
        }
        
        dlink = dlink->pNext;
        
    }
    pBS->RestoreTPL(SaveTpl);
    return EFI_SUCCESS;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   ProcessOpenFileHandle
//
// Description: Performs whatever is needed to process an open handle
//              (create dir. entry, create instance, allocate clusters)
//
// Parameters:
//  IN  FILE_HANDLE_INSTANCE  *ParentFi - Parent file handle instance
//  IN  CHAR16                **FileName - Unicode file name
//  OUT EFI_FILE_PROTOCOL     **NewHandle - New file handle
//  IN UINT64                 OpenMode - Open mode (read, write, both)
//  IN UINT64                 Attributes - File attributes
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
ProcessOpenFileHandle(
    IN FILE_HANDLE_INSTANCE *Fi,
    IN CHAR16               **FileName,
    OUT EFI_FILE_PROTOCOL   **NewHandle,
    IN UINT64               OpenMode,
    IN UINT64               Attributes
)
{

    EFI_STATUS           Status;
    FILE_HANDLE          *fh = Fi->pFH, *fh1;
    VOLUME_INFO          *Vi = fh->VolumeInterface;
    CHAR16               *Next;
    UINT32               Length;
    CHAR16               NextCompName[MAX_TOTAL_PATH_LENGTH];
    FILE_HANDLE_INSTANCE *fhi;
    DIR_ENTRY_LIST       *Del;
    CHAR16               CurrentDir[] = {'.', 0};
    CHAR16               ParentDir[] = {'.', '.', 0};
    UINT8                *Buffer;
    BOOLEAN              DirFound = FALSE;

    if (EFI_ERROR(fh->HandleStatus)) return fh->HandleStatus;
    
    Next = *FileName;
    
    if ((*FileName)[0] == '\\') {
        fh = Vi->RootFH;
        (*FileName) += 1;
    }
    
//  Check for '\\' at the end of the FileName. If so remove it. Eg: \windows\system
    Length = (UINT32) Wcslen(*FileName);
    
    if (Length > 2 && (*FileName)[Length-1] == '\\') (*FileName)[Length-1] = 0;
    
    Status = EFI_SUCCESS;
    
    while (!          EFI_ERROR(Status)) {
    
        if (          EFI_ERROR(GetNextCompName (FileName, NextCompName))) return EFI_NOT_FOUND;
        
        if (NextCompName[0] == 0) break;
        
        if (!FatLfnIsValid (NextCompName)) return EFI_NOT_FOUND;
        
        if (!Wcscmp(NextCompName, CurrentDir)) continue;
        
        if (!Wcscmp(NextCompName, ParentDir)) {
            if (fh == Vi->RootFH) {
                Status = EFI_NOT_FOUND;
                break;
            }
            
            fh = fh->Parent;
            
            if (!fh) {
                Status = EFI_NOT_FOUND;
                break;
            }
            
            continue;
        }
        
//  A component name is present. Parent should be directory. Else return error
        if (!(fh->DirectoryEntry.Dir_Attr & ATTR_DIRECTORY)) {
            Status = EFI_NOT_FOUND;
            break;
        }
        
//  Check whether a match can be found for nextcompname
        if (FindMatchingFH(Vi, NextCompName, fh, &fh1)) {
            fh = fh1;
            Status = EFI_SUCCESS;
            continue;
        }
        
//  See if the Linked List is Valid
        if (!fh->DirList.pHead) {
            ReadAllDirectoryEntries (fh);
            
            if (EFI_ERROR(Status)) return Status;
        }
        
        Status = EFI_NOT_FOUND;
        
        DirFound = FALSE;
        if (FindMatchingDirEntry (fh, NextCompName, &Del)) {
            DirFound = TRUE; // Flag existing directory found
            Status = CreateFHFromDirEntryList(Del, fh, &fh1);
            
            //  Add to the List in Volume Interface
            if (Status == EFI_SUCCESS) {
                fh = fh1;
            }
        }
    }
    
    if (EFI_ERROR(Status)) {
    
//  Make sure NextCompName is the last compname.
        if ((*FileName[0]) != 0) return EFI_NOT_FOUND;
        
//  If it is a file that needs to be openend, and not found create one. For directories return error.
        if (OpenMode & EFI_FILE_MODE_CREATE) {

            if (!fh) return EFI_NOT_FOUND;

            if (!(fh->DirectoryEntry.Dir_Attr & ATTR_DIRECTORY)) {
                // if this is not a directory then the input is just a filename. So get the parent handle of this instance
                fh = fh->Parent;
            }

//    Create a file entry in the parent directory
            Status = CreateDirectoryEntry (fh, NextCompName, &fh1, Attributes);
            
            if (EFI_ERROR(Status)) return Status;
            
            fh = fh1;
            
        } else return EFI_NOT_FOUND;
    }
    
//  Create an instance
    Status = CreateFileHandleInstance (fh, &fhi, OpenMode);
    *NewHandle = (EFI_FILE_PROTOCOL *) fhi;
    
//  If asked to create a directory, and it already exists, the UEFI spec says to
//  just open and return EFI_SUCCESS.
    if (DirFound && (OpenMode & EFI_FILE_MODE_CREATE)) {
        return EFI_SUCCESS;
    }

    if ((fhi->pFH->DirectoryEntry.Dir_Attr == ATTR_DIRECTORY) &&
            (OpenMode & EFI_FILE_MODE_CREATE)) { // If creating a new directory, create dot and dotdot entries

//  Allocate Cluster for the sub Directory that was created before.
//  The new allocated cluster should be cleared.
//  fsAllocateMemory (Vi, Vi->BytesPerCluster, (void **)&Buffer, TRUE);
        GetTempBuffer (Vi, &Buffer);
        Zeromemory (Buffer, Vi->BytesPerCluster);
        Length = Vi->BytesPerCluster;
        Status = WriteToFile (fhi, Buffer, &Length, DIRECTORY_REGION);
        
        if (EFI_ERROR(Status)) return Status;
        
//  fsDeAllocateMemory(Vi, Buffer);
        ReleaseTempBuffer(Vi);
        
        fhi->Position = 0;
        fhi->pFH->DirectoryEntry.Dir_FileSize = 0;
        
        if (fhi->pFH->DirEntryChanged == TRUE) UpdateDirListFromFHDir(fhi->pFH);
        
        fhi->pFH->DirEntryChanged = FALSE;
        fhi->pFH->FreeDirEntryCluster = FIRSTCLUSTER(fh->DirectoryEntry);
        fhi->pFH->FreeDirEntryOffset = 0;
        
//  Create '.' entry
        Status = CreateDirectoryEntry(fh, CurrentDir, &fh1, ATTR_DIRECTORY);
        
        if (EFI_ERROR(Status)) return Status;
        
        RemoveFH(fh1);
        
//  Create'..' entry
        Status = CreateDirectoryEntry(fh, ParentDir, &fh1, ATTR_DIRECTORY);
        
        if (EFI_ERROR(Status)) return Status;
        
        RemoveFH(fh1);
        
    }
    
    return Status;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   ReadAllDirectoryEntries
//
// Description: Reads entries in a directory
//
// Parameters:
//  IN FILE_HANDLE    *fh - File handle structure
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: ReadFileHandle, ProcessOpenFileHandle
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
ReadAllDirectoryEntries (
    IN FILE_HANDLE *fh
)
{

    EFI_STATUS  Status;
    CHAR16      lfn[256], *lfnBuffer;
    UINT32      Offset, SlotNumber = 0, BufferSize, OrgBufferSize, Slots;
    UINT32      BytesReadPerLoop;
    UINT8       *Buffer;
    VOLUME_INFO *Vi = fh->VolumeInterface;
    UINT32      ClusterNumber, Cluster;
    
    if (Vi->TotalDirEntryList > DIRECTORTY_ENTRIES_CACHED) FreeUpDirEntries (Vi, NULL);
    
// Allocate space for a temp buffer.
    BytesReadPerLoop = Vi->BytesPerCluster;
    
    if (fh->ROOTDIR && (Vi->FatType != FAT32))
        BytesReadPerLoop = Vi->RootDirSectorCount << Vi->BytesPerSecPowerof2;
        
    GetTempBuffer (Vi, &Buffer);
    
    for (ClusterNumber = 0; ; ClusterNumber++, Cluster = 0) {
    
        Status = ReadClusterOfDirectoryEntries(fh, ClusterNumber, &Cluster, Buffer, &BytesReadPerLoop);
        
        if (Status == EFI_SUCCESS && BytesReadPerLoop == 0) {
            break;      // Empty Directory or End of Directory reached
        }
        
        if (EFI_ERROR(Status)) break;
        
        OrgBufferSize = BytesReadPerLoop;
        BufferSize = BytesReadPerLoop;
        Offset = 0;
        
        for ( ; BufferSize; Offset += sizeof (DIR_ENTRY_32), BufferSize = OrgBufferSize - Offset) {
        
            if (!(FindShortName (Buffer, &Offset, BufferSize))) break;
            
//    Find a matching LFN
            Slots = FetchLongName(fh, (DIR_ENTRY_32 *)(Buffer), Offset, lfn, ClusterNumber, BytesReadPerLoop);
            
            if (Slots == 0) ExtractShortFileName (Vi, lfn, Buffer + Offset);
            
            Status = fsAllocateMemory (Vi, (UINT32)((Wcslen(lfn) * sizeof (CHAR16))+ 2), (VOID**)&lfnBuffer, FALSE);
            pBS->CopyMem (lfnBuffer, lfn, (Wcslen(lfn) * sizeof (CHAR16))+ 2);
            
//    Create a Dir Entry List and add it to the Parent File Handle
//    Valid Entry has been found. *lfn, DirectoryEntry, Offset
            AddToDirList (fh, *(DIR_ENTRY_32 *)(Buffer + Offset), lfnBuffer, Cluster, Offset, SlotNumber);
            SlotNumber++;
        }
        
        if (Offset == -1) break;          // No more directory Entries.
        
        if (fh->ROOTDIR && (Vi->FatType != FAT32)) break; // Only one loop.
    }
    
    ReleaseTempBuffer (Vi);
    
    return Status;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   ReadClusterOfDirectoryEntries
//
// Description: Reads one cluster of directory entries
//
// Parameters:
//  IN FILE_HANDLE    *fh,
//  IN UINT32         ClusterNumber -- 0-based number of which cluster to read
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: ReadAllDirectoryEntries
//
// Notes: For end of cluster, BufferSize = 0 and EFI_SUCCESS returned.
//      For FAT12/16, All the Root Directories are read in one shot.
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
ReadClusterOfDirectoryEntries (
    IN FILE_HANDLE *fh,
    IN UINT32      ClusterNumber,
    OUT UINT32     *ClusterRead,
    UINT8          *Buffer,
    UINT32         *BufferSize
)
{
    EFI_STATUS  Status;
    VOLUME_INFO *Vi = fh->VolumeInterface;
    UINT32      Cluster;
    UINT32      Sector;
    UINT32      i;
    
// Find the starting sector of the cluster to read.
    Cluster = FIRSTCLUSTER(fh->DirectoryEntry);
    
    if (fh->ROOTDIR && (Vi->FatType != FAT32)) {
//        Offset = ClusterNumber * (UINT32)Vi->VolumeBPB.BPB_SecPerClus;
//        if(Offset >= Vi->RootDirSectorCount) return EFI_NOT_FOUND; // Requested cluster is beyond directory
        if (ClusterNumber) {
            *BufferSize = 0;
            return EFI_SUCCESS; // For FAT12/16 root dir, Clusternumber should be always zero.
        }
        
        Sector = Vi->VolumeBPB.BPB_RsvdSecCnt + Vi->FATSz * Vi->VolumeBPB.BPB_NumFATs;
        *ClusterRead  = Sector;
        
    } else {
        if (Vi->FatType == FAT32 && fh->ROOTDIR) Cluster = Vi->VolumeBPB.BPB_FAT.BPB_FAT32.BPB_RootClus;
        
        if (ClusterNumber != 0) {
            for (i=0; i<ClusterNumber ; i++) { // Follow the fat chain to find the desired cluster
                Status = ReadFatClusterNumber(Vi, Cluster, 0, &Cluster);
                
                if (EFI_ERROR(Status)) return EFI_NOT_FOUND;
                
                if (Cluster < 2) {
                    MarkVolumeDirty (Vi);
                    return EFI_VOLUME_CORRUPTED;
                }
                
                if (Cluster >= Vi->EOCMark) {
                    *BufferSize = 0; // End of Directory reached.
                    return EFI_SUCCESS; // Requested cluster is beyond directory end
                }
            }
        }
        
//    Convert this cluster to a sector.
        Sector = ClusterToSector(Vi, Cluster);
        *ClusterRead = Cluster;
    }
    
// Read one cluster of the directory into the temp buffer.
    Status = FsReadMedia (Vi,
                          Buffer,
                          (UINT64)Sector,
                          0,
                          *BufferSize,
                          DIRECTORY_REGION);
                          
    return Status;
    
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   AddToDirList
//
// Description: Adds a directory entry to DirList
//
// Parameters:
//  IN FILE_HANDLE    *fh - File Handle
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: ReadAllDirectoryEntries
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
AddToDirList (
    FILE_HANDLE  *fh,
    DIR_ENTRY_32 DirectoryEntry,
    CHAR16       *lfn,
    UINT32       Cluster,
    UINT32       Offset,
    UINT32       SlotNumber

)
{
    DIR_ENTRY_LIST *Del;
    EFI_STATUS     Status;
    
    Status = fsAllocateMemory(fh->VolumeInterface, sizeof(DIR_ENTRY_LIST), (VOID**)&Del, FALSE);
    
    if (EFI_ERROR(Status)) return Status;
    
    pBS->CopyMem (&(Del->DirectoryEntry), &DirectoryEntry, sizeof(DIR_ENTRY_32));
    Del->FileNameLFN = lfn;
    Del->Cluster = Cluster;
    Del->Offset= Offset;
    Del->SlotNumber= SlotNumber;
	DListAdd((DLIST *)(UINTN)&(fh->DirList), (DLINK *)(UINTN)&(Del->DirLink));
    fh->VolumeInterface->TotalDirEntryList++;
    return EFI_SUCCESS;
    
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   CreateFHFromDirEntryList
//
// Description: Creates a file handle from directory entry list
//
// Parameters:
//  IN DIR_ENTRY_LIST *Del - Directory entry list
//  IN FILE_HANDLE    *fh - Directory file handle
//  OUT FILE_HANDLE   **fh1 - New file handle
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: ReadFileHandle, ProcessOpenFileHandle
//
// Notes: Should not link the FH to any LIST
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
CreateFHFromDirEntryList(
    IN DIR_ENTRY_LIST *Del,
    IN FILE_HANDLE    *fh,
    OUT FILE_HANDLE   **fh1
)
{

    FILE_HANDLE *nfh;
    EFI_STATUS  Status;
    
    Status = fsAllocateMemory(fh->VolumeInterface, sizeof(FILE_HANDLE), (VOID**)&nfh, TRUE);
    
    if (EFI_ERROR(Status)) return Status;
    
    Status = fsAllocateMemory(fh->VolumeInterface, (UINT32)((Wcslen(Del->FileNameLFN) * sizeof(CHAR16)) + 2), (VOID**)&(nfh->FileNameLFN), FALSE);
    
    if (EFI_ERROR(Status)) return Status;
    
//  Initialize FILE_HANDLE_STRUCTURE
    nfh->VolumeInterface = fh->VolumeInterface;
    nfh->HandleStatus = EFI_SUCCESS;
    nfh->ROOTDIR = FALSE;
    pBS->CopyMem (&(nfh->DirectoryEntry), &(Del->DirectoryEntry), sizeof(DIR_ENTRY_32));
    Wcscpy(nfh->FileNameLFN, Del->FileNameLFN);
    nfh->DirCluster = Del->Cluster;
    nfh->DirOffset = Del->Offset;
    nfh->SlotNumber = Del->SlotNumber;
    nfh->Parent = fh;
    *fh1 = nfh;
    
//  Add the new FH to the parent's Child List
	DListAdd((DLIST *)(UINTN)&(fh->ChildList), (DLINK *)(UINTN)&(nfh->ChildLink));

//	Add to VolumeInterface List
	DListAdd((DLIST *)(UINTN)&(fh->VolumeInterface->OpenFHs), (DLINK *)(UINTN)&(nfh->ViFHLink));
    
    return EFI_SUCCESS;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   FindMatchingFH
//
// Description: Finds the Child FH, whose name matches with the input.
//              In the Attributes, check whther both are file or Directory
//
// Parameters:
//  IN VOLUME_INFO  *Vi - Volume info structure
//  IN CHAR16       *NextCompName - Unicode name to compare
//  IN FILE_HANDLE  *fh - Parent file handle
//  OUT FILE_HANDLE **fh1 - Child file handle
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: ProcessOpenFileHandle
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

BOOLEAN
FindMatchingFH (
    IN VOLUME_INFO  *Vi,
    IN CHAR16       *NextCompName,
    IN FILE_HANDLE  *fh,
    OUT FILE_HANDLE **fh1
)
{


    DLINK       *fhlink = fh->ChildList.pHead;
    FILE_HANDLE *fh2;
    
    for ( ; fhlink; ) {
        fh2 = OUTTER(fhlink, ChildLink, FILE_HANDLE);
        
        if (!Wcscmpcaseinsensitive (fh2->FileNameLFN, NextCompName)) {
            if (!(fh2->DirectoryEntry.Dir_Attr & ATTR_VOLUME_ID)) {
                if (fh2->HandleStatus == EFI_SUCCESS) {
                    *fh1 = fh2;
                    return TRUE;
                }
            }
        }
        
        fhlink = fhlink->pNext;
    }
    
    return FALSE;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   FindMatchingDirEntry
//
// Description: Finds a directory entry matching the input name
//
// Parameters:
//  IN FILE_HANDLE     *fh - Directory file handle
//  IN CHAR16          *NextCompName - Unicode name to match
//  OUT DIR_ENTRY_LIST **Del - Directory list where found
//
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: ProcessOpenFileHandle
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

BOOLEAN
FindMatchingDirEntry (
    IN FILE_HANDLE     *fh,
    IN CHAR16          *NextCompName,
    OUT DIR_ENTRY_LIST **Del
)
{

    DLINK          *dlink = fh->DirList.pHead;
    DIR_ENTRY_LIST *del;
    
    for ( ; dlink; ) {
        del = OUTTER(dlink, DirLink, DIR_ENTRY_LIST);
        
        if (!Wcscmpcaseinsensitive (del->FileNameLFN, NextCompName)) {
            if (!(del->DirectoryEntry.Dir_Attr & ATTR_VOLUME_ID)) {
                *Del = del;
                return TRUE;
            }
        }
        
        dlink = dlink->pNext;
    }
    
    return FALSE;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   FindMatchingSFNDirEntry
//
// Description: Finds short file name directory entry matching input
//
// Parameters:
//  IN FILE_HANDLE     *fh - Directory file handle
//  IN CHAR8           *ShortFileName - Name to find
//  OUT DIR_ENTRY_LIST **Del - Directory list where found
//
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

BOOLEAN
FindMatchingSFNDirEntry (
    IN FILE_HANDLE     *fh,
    IN CHAR8           *ShortFileName,
    OUT DIR_ENTRY_LIST **Del
)
{

    DLINK          *dlink = fh->DirList.pHead;
    DIR_ENTRY_LIST *del;
    UINT8          Attrb;
    
    for ( ; dlink; ) {
        del = OUTTER(dlink, DirLink, DIR_ENTRY_LIST);
        Attrb = del->DirectoryEntry.Dir_Attr;
        del->DirectoryEntry.Dir_Attr = 0; // Generate a NULL terminated string
        
        if (!Strcmp((CHAR8 *)&del->DirectoryEntry, ShortFileName)) {
            del->DirectoryEntry.Dir_Attr = Attrb; // restore Attribute
            *Del = del;
            return TRUE;
        }
        
        del->DirectoryEntry.Dir_Attr = Attrb; // restore Attribute
        dlink = dlink->pNext;
    }
    
    return FALSE;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   Wcscmpcaseinsensitive
//
// Description: Case insensitive unicode string compare
//
// Parameters:
//  CHAR16 *string1 - First string
//  CHAR16 *string2 - Second string
//
// Return Value:
//  INT - Difference where mis-matched
//
// Modified:
//
// Referrals: FindMatchingFH, FindMatchingDirEntry
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

int Wcscmpcaseinsensitive(
    CHAR16 *string1,
    CHAR16 *string2
)
{

    CHAR16 ch1, ch2;
    
    while (*string1) {
        if (*string1 >=0x61 && *string1<=0x7a) ch1 = *string1 - 0x20;
        
        else ch1 = *string1;
        
        if (*string2 >=0x61 && *string2<=0x7a) ch2 = *string2 - 0x20;
        
        else ch2 = *string2;
        
        if (ch1 != ch2) break;
        
        *string1++;
        *string2++;
    }
    
    return *string1 - *string2;     // Don't use ch1 - ch2
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   GetDirListAtPosition
//
// Description: Get directory list at current position in directory file
//
// Parameters:
//  FILE_HANDLE       *Pfh - Parent file handle
//  UINT64            Position - Current position
//  DIR_ENTRY_LIST    **Del - Directory entry list
//
// Return Value:
//
// Modified:
//
// Referrals: ReadFileHandle
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
GetDirListAtPosition(
    FILE_HANDLE    *Pfh,
    UINT64         Position,
    DIR_ENTRY_LIST **Del
)
{

    EFI_STATUS Status = EFI_NOT_FOUND;
    DLINK      *dlink = Pfh->DirList.pHead;
    
    *Del = NULL;
    
    if (!dlink) return Status;
    
    while (Position && dlink) {
        if (!dlink->pNext) break;
        
        dlink = dlink->pNext;
        Position--;
    };
    
    if (Position == 0 && dlink) {
        Status = EFI_SUCCESS;
        *Del = OUTTER(dlink, DirLink, DIR_ENTRY_LIST);
    }
    
    return Status;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   CloseFileHandle
//
// Description: Closes an open file
//
// Parameters:
//  IN EFI_FILE_PROTOCOL *This - File Protocol instance
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: DeleteFileHandle
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
CloseFileHandle (
    IN EFI_FILE_PROTOCOL *This
)
{

    EFI_STATUS           Status;
    FILE_HANDLE_INSTANCE *fhi = (FILE_HANDLE_INSTANCE *)This;
    FILE_HANDLE          *Pfh = fhi->pFH;
    VOLUME_INFO          *vi;	
    void                 *Buffer;
    UINT32               BufferSize, Difference;
    EFI_TPL              SaveTpl;
    EFI_TPL              NextTpl;

	Status = ValidateCurrentStatus (fhi);
	if (EFI_ERROR(Status)) return Status;
    
#ifdef Debug_CloseFileHandle
    EfiDebugPrint(-1,"CloseFileHandle: %S Dir Cluster %x Dir Offset %x\n", Pfh->FileNameLFN, Pfh->DirCluster, Pfh->DirOffset);
#endif
    
//  Device Removal or Media Not Present
    if (!Pfh) {
        Status = pBS->FreePool(fhi);
        return EFI_SUCCESS;
    }
    NextTpl = FindNextTpl (EFI_TPL_CALLBACK);
    SaveTpl = pBS->RaiseTPL (NextTpl);    
    vi = Pfh->VolumeInterface;

//  Check if FreeSpace and Last Free Cluster needs to be updated for FAT32
    if (vi->FatType == FAT32 && vi->FreeSpaceinClusters != -1) {
        Difference = vi->FreeSpaceinSector1 - vi->FreeSpaceinClusters;
        
        if (vi->FreeSpaceinSector1 < vi->FreeSpaceinClusters) Difference = vi->FreeSpaceinClusters - vi->FreeSpaceinSector1;
        
        if (Difference > 0x100) {
//      ReadFSInfo
            BufferSize = vi->VolumeBPB.BPB_BytePerSec;
            GetTempBuffer (vi, &Buffer);
            
            Status = FsReadMedia (vi, Buffer, (UINT64)vi->VolumeBPB.BPB_FAT.BPB_FAT32.BPB_FSInfo, FSI_Free_Count, 8, FAT_REGION);
            
            if (EFI_ERROR(Status)) goto Memallocate_error;
            
            *(UINT32 *) ((UINT8 *)Buffer) = vi->FreeSpaceinClusters;
            *(UINT32 *) ((UINT8 *)Buffer + 4) = vi->LastFreeCluster;
            Status = FsWriteMedia (vi, Buffer, (UINT64)vi->VolumeBPB.BPB_FAT.BPB_FAT32.BPB_FSInfo, FSI_Free_Count, 8, FAT_REGION);
            vi->FreeSpaceinSector1 = vi->FreeSpaceinClusters;
            ReleaseTempBuffer(vi);
        }
    }
    
Memallocate_error:

    if (Pfh->DirEntryChanged == TRUE) {
        UpdateDirListFromFHDir(Pfh);
        Pfh->DirEntryChanged = FALSE;
    }
    
	DListDelete ((DLIST *)(UINTN)&(vi->OpenFIs), (DLINK *)(UINTN)&(fhi->ViFILink));
    fsDeAllocateMemory (vi, fhi);
    
    if (vi->TotalDirEntryList > DIRECTORTY_ENTRIES_CACHED) {
        if (vi->OpenFHs.Size > MAX_OPEN_FHS) FreeUpFHEntries (vi, NULL);
        
        FreeUpDirEntries (vi, Pfh->Parent);
    }
    
//  This should be always after FreeUpDirEntries. When called from 
//  DeleteFileHandle will have problem otherwise.
    if (Pfh) Pfh->InstancesCount -= 1;  
    
    FlushFileHandle(This);
    
    pBS->RestoreTPL(SaveTpl);

    return EFI_SUCCESS;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   FreeUpFHEntries
//
// Description: Release all file handles whose instance count is 0
//
// Parameters:
//  VOLUME_INFO       *vi - Volume info
//  FILE_HANDLE       *pfh - Parent file handle
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: CloseFileHandle
//
// Notes:
//  Don't remove pfh
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
FreeUpFHEntries (
    VOLUME_INFO *vi,
    FILE_HANDLE *pfh
)
{

    DLINK       *dlink = vi->OpenFHs.pHead;
    FILE_HANDLE *lfh;
    
//  Go through all the FHs in Volume Interface and release all whose Instance
//  count is 0, no DIR list is present and childlist is NULL
//  Don't delete:
//  1. If Instance count is more than 0 
//  2. If Dir List is present,
//  3. If it is a Root Dir, 
//  4. If Child List is present, 
//  5. If it is a i/p fh
//  6. If the i/p FH is the parent of the fh.
    for ( ; dlink; ) {
        lfh = OUTTER(dlink, ViFHLink, FILE_HANDLE);
        dlink = dlink->pNext;
        
        if (lfh->InstancesCount == 0 && !(lfh->DirList.pHead) &&
                !(lfh->ROOTDIR) && !(lfh->ChildList.pHead) && lfh != pfh) RemoveFH(lfh);
    }
    
    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   FreeUpDirEntries
//
// Description: Free up cached directory entries
//
// Parameters:
//  VOLUME_INFO       *vi - Volume info
//  FILE_HANDLE       *pfh - Parent file handle
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: ReadAllDirectoryEntries, CloseFileHandle
//
// Notes:
//  Don't remove pfh
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
FreeUpDirEntries (
    VOLUME_INFO *vi,
    FILE_HANDLE *pfh
)
{

    DLINK       *dlink = vi->OpenFHs.pHead;
    FILE_HANDLE *lfh;
    
    for ( ; dlink; ) {
        if (vi->TotalDirEntryList <= DIRECTORTY_ENTRIES_CACHED) break;
        
        lfh = OUTTER(dlink, ViFHLink, FILE_HANDLE);
        
        if (lfh != pfh && lfh->DirList.pHead) RemoveAllDirList(lfh);
        
        dlink = dlink->pNext;
    }
    
    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   RemoveAllDirList
//
// Description: Removes all directory lists from cache (needs to be re-read)
//
// Parameters:
//  FILE_HANDLE *lfh - list file handle
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: ReadFileHandle, DeleteFileHandle, FreeUpDirEntries
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
RemoveAllDirList(
    FILE_HANDLE *lfh
)
{

    DLINK          *dlink = lfh->DirList.pHead;
    DIR_ENTRY_LIST *Del;
    
    for ( ; dlink; ) {
        Del = OUTTER(dlink, DirLink, DIR_ENTRY_LIST);
        dlink = dlink->pNext;
        RemoveDirList (lfh, Del);
    }
    
    return EFI_SUCCESS;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   RemoveDirList
//
// Description: Removes a directory list from cache
//
// Parameters:
//  FILE_HANDLE    *fh - Directory file handle
//  DIR_ENTRY_LIST *Del - Directory list
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: DeleteFileHandle, RemoveAllDirList
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
RemoveDirList (
    FILE_HANDLE    *fh,
    DIR_ENTRY_LIST *Del
)
{

    fh->VolumeInterface->TotalDirEntryList--;
    
    fsDeAllocateMemory (fh->VolumeInterface, Del->FileNameLFN);
	DListDelete ((DLIST *)(UINTN)&(fh->DirList), (DLINK *)(UINTN)&(Del->DirLink));
    fsDeAllocateMemory (fh->VolumeInterface, Del);
    return EFI_SUCCESS;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   FetchLongName
//
// Description: Fetch a long name from a directory entry
//
// Parameters:
//  FILE_HANDLE  *fh - File handle of the directory
//  DIR_ENTRY_32 *Buffer - Buffer containing directory entry
//  UINT32       BufferPos - Position in buffer
//  UINT16       *Name - Unicode name placed here
//  UINT32       ClusterNumber - Current directory cluster
//  UINT32       ClusterSize - Cluster size in bytes
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: ReadAllDirectoryEntries
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

UINT8
FetchLongName (
    FILE_HANDLE  *fh,
    DIR_ENTRY_32 *Buffer,
    UINT32       BufferPos,
    UINT16       *Name,
    UINT32       ClusterNumber,
    UINT32       ClusterSize
)
{

    DIR_ENTRY_LFN *LfnSlot  = (DIR_ENTRY_LFN *) ((UINT8 *)Buffer + BufferPos);
    UINT8         Checksum = LfnChecksum((UINT8 *) Buffer + BufferPos);
    CHAR8         Order    = 0;
    UINT32        NamePos  = 0;
    UINT32        k;
    UINT32        Cluster = 0;
    EFI_STATUS    Status;
    BOOLEAN       Backup = FALSE;
    
    do {
        if (--LfnSlot < (DIR_ENTRY_LFN *) Buffer) { // Long name is split over 2 clusters
            Status = ReadClusterOfDirectoryEntries(fh, ClusterNumber-1, &Cluster, (UINT8*)Buffer, &ClusterSize);
            Backup = TRUE; // We had to back up into the previous cluster to get the whole name
            if (EFI_ERROR(Status) || (Status == EFI_SUCCESS && ClusterSize == 0)) { Order = 0; goto exit; };
            LfnSlot  = (DIR_ENTRY_LFN *) ((UINT8 *)Buffer + ClusterSize);
            LfnSlot--;
        }
        if (Order > MAX_LFN_SLOTS) { Order = 0; goto exit; };    // Max of 20 LFN slots are possible 255 chars max.
        
        if ((LfnSlot->Dir_Attr & ATTR_LONG_NAME_MASK) != (ATTR_LONG_NAME)) { Order = 0; goto exit; };
        
        if (++Order != (LfnSlot->Dir_Order & 0x1F)) { Order = 0; goto exit; };
        
        if (Checksum != LfnSlot->Dir_Checksum) { Order = 0; goto exit; };
        
        // Ok, the LFN slot is valid, attach it to the long name
        for (k = 0; k < 5; k++) Name[NamePos++] = LfnSlot->Dir_Name0_4[k];
        
        for (k = 0; k < 6; k++) Name[NamePos++] = LfnSlot->Dir_Name5_10[k];
        
        for (k = 0; k < 2; k++) Name[NamePos++] = LfnSlot->Dir_Name11_12[k];
    } while (!(LfnSlot->Dir_Order & 0x40));
    
// If the name was exactly 13 characters long in the last slot, add a terminating zero.
// Otherwise, it will already end in a zero.
    if (Name[NamePos - 1] != 0x0000) Name[NamePos] = 0x0000;
exit:
    if (Backup) { // Re-read the old cluster if we had to back up
        Status = ReadClusterOfDirectoryEntries(fh, ClusterNumber, &Cluster, (UINT8*)Buffer, &ClusterSize);
        if (EFI_ERROR(Status)) Order = 0;
    }
    return Order;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   LfnChecksum
//
// Description: Calculate long file name entry section checksum
//
// Parameters:
//  UINT8 *D - Pointer to long name entry
//
//
// Return Value:
//  UINT8 - Checksum
//
// Modified:
//
// Referrals: FetchLongName
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

UINT8
LfnChecksum(
    UINT8 *D
)
{
    UINT8 Sum = 0;
    UINT8 i;
    
    for (i = 11; i != 0; i--) {
        Sum = ((Sum & 1)  ? 0x80 : 0) + (Sum >> 1) + *D++;
    }
    
    return Sum;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   FindShortName
//
// Description: Return a valid short name Directory Entry from DirCluster
//              beginning from ReadPosition.
//
// Parameters:
//    UINT8       *Buffer - Buffer to search in
//    UINT32      *Offset - Offset in buffer where entry found
//    UINT32      BufferSize - Size of buffer
//
//
// Return Value:
//  BOOLEAN - True = name found
//
// Modified:
//
// Referrals: ReadAllDirectoryEntries
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

BOOLEAN
FindShortName (
    UINT8  *Buffer,
    UINT32 *Offset,
    UINT32 BufferSize
)
{
    DIR_ENTRY_32 *Entry  = (DIR_ENTRY_32 *)(Buffer + *Offset);
    UINT32       i;
    
    for (i = *Offset; BufferSize; i+=sizeof(DIR_ENTRY_32),BufferSize-=sizeof(DIR_ENTRY_32)) {
        if (Entry->Dir_Name[0] == 0) {
			*Offset = (UINTN)-1;
            return FALSE; // no more directory Entries.
        }
        
        if (((Entry->Dir_Attr & ATTR_LONG_NAME_MASK) != (ATTR_LONG_NAME)) &&
                (Entry->Dir_Name[0] > 0x1f) &&
                (Entry->Dir_Name[0] != 0xe5)) {
            *Offset = i; // Point offset to short name found
            
            return TRUE;
        }
        
        Entry++;
    }
    
    return FALSE;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   CreateDirectoryEntry
//
// Description: Creates a directory entry
//
// Parameters:
//  IN  FILE_HANDLE       *fh - File handle
//  IN  CHAR16            *NextCompName - Unicode name to create
//  OUT FILE_HANDLE       **fh1 - File handle for new name
//  IN UINT64             Attributes
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: ProcessOpenFileHandle
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
CreateDirectoryEntry (
    IN FILE_HANDLE  *fh,
    IN CHAR16       *NextCompName,
    OUT FILE_HANDLE **fh1,
    IN UINT64       Attributes
)
{

    EFI_STATUS     Status;
    UINT32         DirCluster, DirClusterOffset;
    UINT32         Length;
    CHAR8          DirEntry[32];
    DIR_ENTRY_32   *Entry = (DIR_ENTRY_32 *) &DirEntry;
    CHAR16         *lfn;
    DIR_ENTRY_LIST *Del;
    
    Status = CreateDirEntryInDisk (fh, NextCompName, Attributes, &DirCluster, &DirClusterOffset, Entry);
    
    if (EFI_ERROR(Status)) return Status;
    
//  Create a Dir_List Entry
    Length = (UINT32)(Wcslen(NextCompName) * sizeof(CHAR16)) + 2;
    Status = fsAllocateMemory(fh->VolumeInterface, (UINT32) Length, (VOID**)&lfn, FALSE);
    pBS->CopyMem(lfn, NextCompName, Length);
    AddToDirList (fh, *Entry, lfn, DirCluster, DirClusterOffset, (UINTN)-1);
    
    Del = OUTTER(fh->DirList.pTail, DirLink, DIR_ENTRY_LIST);
//  Create a File Handle
    Status = CreateFHFromDirEntryList(Del, fh, fh1);
    return Status;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   CreateDirEntryInDisk
//
// Description: Creates a directory entry on the disk
//
// Parameters:
//  IN  FILE_HANDLE     *fh - File handle
//  IN  CHAR16          *NextCompName - Name to create
//  IN  UINT64          Attributes - Attributes for entry
//  OUT UINT32          *DirCluster - Cluster of entry created
//  OUT UINT32          *DirClusterOffset - Offset in cluster
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: CreateDirectoryEntry
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
CreateDirEntryInDisk (
    IN FILE_HANDLE *fh,
    IN CHAR16      *NextCompName,
    IN UINT64      Attributes,
    OUT UINT32     *DirCluster,
    OUT UINT32     *DirClusterOffset,
    DIR_ENTRY_32   *Entry
)
{

    EFI_STATUS            Status;
    VOLUME_INFO           *Vi = fh->VolumeInterface;
    CHAR8                 ShortFileName[11];
    EFI_TIME              EfiTime;
    EFI_TIME_CAPABILITIES Capabilities;
    UINT32                Sector, SectorOffset;
    BOOLEAN               LastDirEntry = FALSE;
    UINT32                Length;
    BOOLEAN               LfnNeeded = FALSE;
    UINT8                 *Slots;
    UINT32                NumSlots = 0;
    
    if (Vi->ReadOnly) return EFI_WRITE_PROTECTED;
    
    Status = GenUniqueShortFname (fh, NextCompName, ShortFileName, &LfnNeeded);
    
    if (EFI_ERROR(Status)) return Status;
    
//  Create Short Directory entry
    Zeromemory (Entry, sizeof(DIR_ENTRY_32));
    pBS->CopyMem(Entry, ShortFileName, 11);
    Entry->Dir_Attr         = (UINT8)Attributes;
    pRS->GetTime(&EfiTime, &Capabilities);
    EfiToFatTime (EfiTime, &Entry->Dir_CrtDate, &Entry->Dir_CrtTime);
    Entry->Dir_LstAccDate   = Entry->Dir_CrtDate;
    Entry->Dir_WrtTime      = Entry->Dir_CrtTime;
    Entry->Dir_WrtDate      = Entry->Dir_CrtDate;
    
//  Update the Cluster number in case of '.' entry.
    if (NextCompName[0] == '.' && NextCompName[1] == 0) {
        Length = FIRSTCLUSTER(fh->DirectoryEntry);
        Entry->Dir_FstClusHI = (UINT16)  (Length >> 16);
        Entry->Dir_FstClusLO = (UINT16)  Length;
    }
    
    
    fsAllocateMemory (Vi, (MAX_LFN_SLOTS + 2) * sizeof(DIR_ENTRY_32), (void **)&Slots, FALSE);
    
    if (LfnNeeded) {
        Status = SplitLFN (NextCompName, Entry, (DIR_ENTRY_LFN *)Slots, &NumSlots);
        
        if (EFI_ERROR(Status)) goto Error;
    }
    
// Assuming a short filename FILENAME.EXT (FILENAMEEXT), generate a
// name in the form FILENA~1.EXT, where n is a count beginning with 1.
//  GenUniqueFileName(fh, NextCompName, &ShortFileName);

//  Append Short Directory Entry at the End of Long File Name slots
    Length = NumSlots * sizeof(DIR_ENTRY_32);
    pBS->CopyMem(Slots + Length, Entry, sizeof(DIR_ENTRY_32));
    Length += sizeof(DIR_ENTRY_32);   // This is the total length
    
    Status = LocateFreeDirEntry(fh, Length, DirCluster,
                                DirClusterOffset,  &LastDirEntry);
                                
    if (EFI_ERROR(Status)) goto Error;
    
    GetSectorAddressDir(fh, *DirCluster, *DirClusterOffset, &Sector, &SectorOffset);
    Status = FsWriteMedia (Vi, Slots, Sector, SectorOffset, Length, DIRECTORY_REGION);
    
    if (EFI_ERROR(Status)) goto Error;
    
    *DirClusterOffset += Length;
    
    if (LastDirEntry == TRUE) {
        fh->FreeDirEntryCluster = *DirCluster;
        fh->FreeDirEntryOffset = *DirClusterOffset;
        
    } else {
        fh->DirListChanged = TRUE;
    }
    
    *DirClusterOffset -= sizeof(DIR_ENTRY_32);
    
Error:

    fsDeAllocateMemory (Vi, Slots);
    
    return Status;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   GetTempBuffer
//
// Description: Gets a temporary buffer
//
// Parameters:
//    VOLUME_INFO     *Vi - Volume info
//    VOID            **Buffer - Buffer
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: ProcessOpenFileHandle, ReadAllDirectoryEntries, CloseFileHandle
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

VOID
GetTempBuffer (
    VOLUME_INFO *Vi,
    VOID        **Buffer
)
{

    *Buffer = (UINT8 *)(Vi->TempBuffer);
    Vi->TempBufferInUse = TRUE;
    
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------
//
// Procedure:   ReleaseTempBuffer
//
// Description: Releases a temporary buffer
//
// Parameters:
//  VOLUME_INFO     *Vi - Volume Info
//
// Return Value:
//  EFI_STATUS
//
// Modified:
//
// Referrals: ProcessOpenFileHandle, ReadAllDirectoryEntries, CloseFileHandle
//
// Notes:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>

VOID
ReleaseTempBuffer (
    VOLUME_INFO *Vi
)
{
    Vi->TempBufferInUse = FALSE;
}

//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************