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
|
/*++ @file
POSIX Pthreads to emulate APs and implement threads
Copyright (c) 2011, Apple Inc. All rights reserved.
Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "Host.h"
#define EMU_SIMPLE_FILE_SYSTEM_PRIVATE_SIGNATURE SIGNATURE_32 ('E', 'P', 'f', 's')
typedef struct {
UINTN Signature;
EMU_IO_THUNK_PROTOCOL *Thunk;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL SimpleFileSystem;
CHAR8 *FilePath;
CHAR16 *VolumeLabel;
BOOLEAN FileHandlesOpen;
} EMU_SIMPLE_FILE_SYSTEM_PRIVATE;
#define EMU_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS(a) \
CR (a, \
EMU_SIMPLE_FILE_SYSTEM_PRIVATE, \
SimpleFileSystem, \
EMU_SIMPLE_FILE_SYSTEM_PRIVATE_SIGNATURE \
)
#define EMU_EFI_FILE_PRIVATE_SIGNATURE SIGNATURE_32 ('E', 'P', 'f', 'i')
typedef struct {
UINTN Signature;
EMU_IO_THUNK_PROTOCOL *Thunk;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem;
EFI_FILE_PROTOCOL EfiFile;
int fd;
DIR *Dir;
BOOLEAN IsRootDirectory;
BOOLEAN IsDirectoryPath;
BOOLEAN IsOpenedByRead;
char *FileName;
struct dirent *Dirent;
} EMU_EFI_FILE_PRIVATE;
#define EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS(a) \
CR (a, \
EMU_EFI_FILE_PRIVATE, \
EfiFile, \
EMU_EFI_FILE_PRIVATE_SIGNATURE \
)
EFI_STATUS
PosixFileGetInfo (
IN EFI_FILE_PROTOCOL *This,
IN EFI_GUID *InformationType,
IN OUT UINTN *BufferSize,
OUT VOID *Buffer
);
EFI_STATUS
PosixFileSetInfo (
IN EFI_FILE_PROTOCOL *This,
IN EFI_GUID *InformationType,
IN UINTN BufferSize,
IN VOID *Buffer
);
EFI_FILE_PROTOCOL gPosixFileProtocol = {
EFI_FILE_REVISION,
GasketPosixFileOpen,
GasketPosixFileCLose,
GasketPosixFileDelete,
GasketPosixFileRead,
GasketPosixFileWrite,
GasketPosixFileGetPossition,
GasketPosixFileSetPossition,
GasketPosixFileGetInfo,
GasketPosixFileSetInfo,
GasketPosixFileFlush
};
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL gPosixFileSystemProtocol = {
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION,
GasketPosixOpenVolume,
};
/**
Open the root directory on a volume.
@param This Protocol instance pointer.
@param Root Returns an Open file handle for the root directory
@retval EFI_SUCCESS The device was opened.
@retval EFI_UNSUPPORTED This volume does not support the file system.
@retval EFI_NO_MEDIA The device has no media.
@retval EFI_DEVICE_ERROR The device reported an error.
@retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
@retval EFI_ACCESS_DENIED The service denied access to the file.
@retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of resources.
**/
EFI_STATUS
PosixOpenVolume (
IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,
OUT EFI_FILE_PROTOCOL **Root
)
{
EFI_STATUS Status;
EMU_SIMPLE_FILE_SYSTEM_PRIVATE *Private;
EMU_EFI_FILE_PRIVATE *PrivateFile;
Private = EMU_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (This);
Status = EFI_OUT_OF_RESOURCES;
PrivateFile = malloc (sizeof (EMU_EFI_FILE_PRIVATE));
if (PrivateFile == NULL) {
goto Done;
}
PrivateFile->FileName = malloc (AsciiStrSize (Private->FilePath));
if (PrivateFile->FileName == NULL) {
goto Done;
}
AsciiStrCpyS (
PrivateFile->FileName,
AsciiStrSize (Private->FilePath),
Private->FilePath
);
PrivateFile->Signature = EMU_EFI_FILE_PRIVATE_SIGNATURE;
PrivateFile->Thunk = Private->Thunk;
PrivateFile->SimpleFileSystem = This;
PrivateFile->IsRootDirectory = TRUE;
PrivateFile->IsDirectoryPath = TRUE;
PrivateFile->IsOpenedByRead = TRUE;
CopyMem (&PrivateFile->EfiFile, &gPosixFileProtocol, sizeof (EFI_FILE_PROTOCOL));
PrivateFile->fd = -1;
PrivateFile->Dir = NULL;
PrivateFile->Dirent = NULL;
*Root = &PrivateFile->EfiFile;
PrivateFile->Dir = opendir (PrivateFile->FileName);
if (PrivateFile->Dir == NULL) {
Status = EFI_ACCESS_DENIED;
} else {
Status = EFI_SUCCESS;
}
Done:
if (EFI_ERROR (Status)) {
if (PrivateFile != NULL) {
if (PrivateFile->FileName != NULL) {
free (PrivateFile->FileName);
}
free (PrivateFile);
}
*Root = NULL;
}
return Status;
}
EFI_STATUS
ErrnoToEfiStatus (
)
{
switch (errno) {
case EACCES:
return EFI_ACCESS_DENIED;
case EDQUOT:
case ENOSPC:
return EFI_VOLUME_FULL;
default:
return EFI_DEVICE_ERROR;
}
}
VOID
CutPrefix (
IN CHAR8 *Str,
IN UINTN Count
)
{
CHAR8 *Pointer;
if (AsciiStrLen (Str) < Count) {
ASSERT (0);
}
for (Pointer = Str; *(Pointer + Count); Pointer++) {
*Pointer = *(Pointer + Count);
}
*Pointer = *(Pointer + Count);
}
VOID
PosixSystemTimeToEfiTime (
IN time_t SystemTime,
OUT EFI_TIME *Time
)
{
struct tm *tm;
tm = gmtime (&SystemTime);
Time->Year = tm->tm_year;
Time->Month = tm->tm_mon + 1;
Time->Day = tm->tm_mday;
Time->Hour = tm->tm_hour;
Time->Minute = tm->tm_min;
Time->Second = tm->tm_sec;
Time->Nanosecond = 0;
Time->TimeZone = timezone / 60;
Time->Daylight = (daylight ? EFI_TIME_ADJUST_DAYLIGHT : 0) | (tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
}
EFI_STATUS
UnixSimpleFileSystemFileInfo (
EMU_EFI_FILE_PRIVATE *PrivateFile,
IN CHAR8 *FileName,
IN OUT UINTN *BufferSize,
OUT VOID *Buffer
)
{
EFI_STATUS Status;
UINTN Size;
UINTN NameSize;
UINTN ResultSize;
EFI_FILE_INFO *Info;
CHAR8 *RealFileName;
CHAR8 *TempPointer;
CHAR16 *BufferFileName;
struct stat buf;
if (FileName != NULL) {
RealFileName = FileName;
} else if (PrivateFile->IsRootDirectory) {
RealFileName = "";
} else {
RealFileName = PrivateFile->FileName;
}
TempPointer = RealFileName;
while (*TempPointer) {
if (*TempPointer == '/') {
RealFileName = TempPointer + 1;
}
TempPointer++;
}
Size = SIZE_OF_EFI_FILE_INFO;
NameSize = AsciiStrSize (RealFileName) * 2;
ResultSize = Size + NameSize;
if (*BufferSize < ResultSize) {
*BufferSize = ResultSize;
return EFI_BUFFER_TOO_SMALL;
}
if (stat ((FileName == NULL) ? PrivateFile->FileName : FileName, &buf) < 0) {
return EFI_DEVICE_ERROR;
}
Status = EFI_SUCCESS;
Info = Buffer;
ZeroMem (Info, ResultSize);
Info->Size = ResultSize;
Info->FileSize = buf.st_size;
Info->PhysicalSize = MultU64x32 (buf.st_blocks, buf.st_blksize);
PosixSystemTimeToEfiTime (buf.st_ctime, &Info->CreateTime);
PosixSystemTimeToEfiTime (buf.st_atime, &Info->LastAccessTime);
PosixSystemTimeToEfiTime (buf.st_mtime, &Info->ModificationTime);
if (!(buf.st_mode & S_IWUSR)) {
Info->Attribute |= EFI_FILE_READ_ONLY;
}
if (S_ISDIR (buf.st_mode)) {
Info->Attribute |= EFI_FILE_DIRECTORY;
}
BufferFileName = (CHAR16 *)((CHAR8 *)Buffer + Size);
while (*RealFileName) {
*BufferFileName++ = *RealFileName++;
}
*BufferFileName = 0;
*BufferSize = ResultSize;
return Status;
}
BOOLEAN
IsZero (
IN VOID *Buffer,
IN UINTN Length
)
{
if ((Buffer == NULL) || (Length == 0)) {
return FALSE;
}
if (*(UINT8 *)Buffer != 0) {
return FALSE;
}
if (Length > 1) {
if (!CompareMem (Buffer, (UINT8 *)Buffer + 1, Length - 1)) {
return FALSE;
}
}
return TRUE;
}
/**
Opens a new file relative to the source file's location.
@param This The protocol instance pointer.
@param NewHandle Returns File Handle for FileName.
@param FileName Null terminated string. "\", ".", and ".." are supported.
@param OpenMode Open mode for file.
@param Attributes Only used for EFI_FILE_MODE_CREATE.
@retval EFI_SUCCESS The device was opened.
@retval EFI_NOT_FOUND The specified file could not be found on the device.
@retval EFI_NO_MEDIA The device has no media.
@retval EFI_MEDIA_CHANGED The media has changed.
@retval EFI_DEVICE_ERROR The device reported an error.
@retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
@retval EFI_ACCESS_DENIED The service denied access to the file.
@retval EFI_OUT_OF_RESOURCES The volume was not opened due to lack of resources.
@retval EFI_VOLUME_FULL The volume is full.
**/
EFI_STATUS
PosixFileOpen (
IN EFI_FILE_PROTOCOL *This,
OUT EFI_FILE_PROTOCOL **NewHandle,
IN CHAR16 *FileName,
IN UINT64 OpenMode,
IN UINT64 Attributes
)
{
EFI_FILE_PROTOCOL *Root;
EMU_EFI_FILE_PRIVATE *PrivateFile;
EMU_EFI_FILE_PRIVATE *NewPrivateFile;
EMU_SIMPLE_FILE_SYSTEM_PRIVATE *PrivateRoot;
EFI_STATUS Status;
CHAR16 *Src;
char *Dst;
CHAR8 *RealFileName;
char *ParseFileName;
char *GuardPointer;
CHAR8 TempChar;
UINTN Count;
BOOLEAN TrailingDash;
BOOLEAN LoopFinish;
UINTN InfoSize;
EFI_FILE_INFO *Info;
struct stat finfo;
int res;
UINTN Size;
PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
PrivateRoot = EMU_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile->SimpleFileSystem);
NewPrivateFile = NULL;
Status = EFI_OUT_OF_RESOURCES;
//
// BUGBUG: assume an open of root
// if current location, return current data
//
TrailingDash = FALSE;
if ((StrCmp (FileName, L"\\") == 0) ||
((StrCmp (FileName, L".") == 0) && PrivateFile->IsRootDirectory))
{
OpenRoot:
Status = PosixOpenVolume (PrivateFile->SimpleFileSystem, &Root);
NewPrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (Root);
goto Done;
}
if (FileName[StrLen (FileName) - 1] == L'\\') {
TrailingDash = TRUE;
FileName[StrLen (FileName) - 1] = 0;
}
//
// Attempt to open the file
//
NewPrivateFile = malloc (sizeof (EMU_EFI_FILE_PRIVATE));
if (NewPrivateFile == NULL) {
goto Done;
}
CopyMem (NewPrivateFile, PrivateFile, sizeof (EMU_EFI_FILE_PRIVATE));
Size = AsciiStrSize (PrivateFile->FileName) + 1 + StrLen (FileName) + 1;
NewPrivateFile->FileName = malloc (Size);
if (NewPrivateFile->FileName == NULL) {
goto Done;
}
if (*FileName == L'\\') {
AsciiStrCpyS (NewPrivateFile->FileName, Size, PrivateRoot->FilePath);
// Skip first '\'.
Src = FileName + 1;
} else {
AsciiStrCpyS (NewPrivateFile->FileName, Size, PrivateFile->FileName);
Src = FileName;
}
Dst = NewPrivateFile->FileName + AsciiStrLen (NewPrivateFile->FileName);
GuardPointer = NewPrivateFile->FileName + AsciiStrLen (PrivateRoot->FilePath);
*Dst++ = '/';
// Convert unicode to ascii and '\' to '/'
while (*Src) {
if (*Src == '\\') {
*Dst++ = '/';
} else {
*Dst++ = *Src;
}
Src++;
}
*Dst = 0;
//
// Get rid of . and .., except leading . or ..
//
//
// GuardPointer protect simplefilesystem root path not be destroyed
//
LoopFinish = FALSE;
while (!LoopFinish) {
LoopFinish = TRUE;
for (ParseFileName = GuardPointer; *ParseFileName; ParseFileName++) {
if ((*ParseFileName == '.') &&
((*(ParseFileName + 1) == 0) || (*(ParseFileName + 1) == '/')) &&
(*(ParseFileName - 1) == '/')
)
{
//
// cut /.
//
CutPrefix (ParseFileName - 1, 2);
LoopFinish = FALSE;
break;
}
if ((*ParseFileName == '.') &&
(*(ParseFileName + 1) == '.') &&
((*(ParseFileName + 2) == 0) || (*(ParseFileName + 2) == '/')) &&
(*(ParseFileName - 1) == '/')
)
{
ParseFileName--;
Count = 3;
while (ParseFileName != GuardPointer) {
ParseFileName--;
Count++;
if (*ParseFileName == '/') {
break;
}
}
//
// cut /.. and its left directory
//
CutPrefix (ParseFileName, Count);
LoopFinish = FALSE;
break;
}
}
}
if (AsciiStrCmp (NewPrivateFile->FileName, PrivateRoot->FilePath) == 0) {
NewPrivateFile->IsRootDirectory = TRUE;
free (NewPrivateFile->FileName);
free (NewPrivateFile);
goto OpenRoot;
}
RealFileName = NewPrivateFile->FileName + AsciiStrLen (NewPrivateFile->FileName) - 1;
while (RealFileName > NewPrivateFile->FileName && *RealFileName != '/') {
RealFileName--;
}
TempChar = *(RealFileName - 1);
*(RealFileName - 1) = 0;
*(RealFileName - 1) = TempChar;
//
// Test whether file or directory
//
NewPrivateFile->IsRootDirectory = FALSE;
NewPrivateFile->fd = -1;
NewPrivateFile->Dir = NULL;
if (OpenMode & EFI_FILE_MODE_CREATE) {
if (Attributes & EFI_FILE_DIRECTORY) {
NewPrivateFile->IsDirectoryPath = TRUE;
} else {
NewPrivateFile->IsDirectoryPath = FALSE;
}
} else {
res = stat (NewPrivateFile->FileName, &finfo);
if ((res == 0) && S_ISDIR (finfo.st_mode)) {
NewPrivateFile->IsDirectoryPath = TRUE;
} else {
NewPrivateFile->IsDirectoryPath = FALSE;
}
}
if (OpenMode & EFI_FILE_MODE_WRITE) {
NewPrivateFile->IsOpenedByRead = FALSE;
} else {
NewPrivateFile->IsOpenedByRead = TRUE;
}
Status = EFI_SUCCESS;
//
// deal with directory
//
if (NewPrivateFile->IsDirectoryPath) {
if ((OpenMode & EFI_FILE_MODE_CREATE)) {
//
// Create a directory
//
if (mkdir (NewPrivateFile->FileName, 0777) != 0) {
if (errno != EEXIST) {
// free (TempFileName);
Status = EFI_ACCESS_DENIED;
goto Done;
}
}
}
NewPrivateFile->Dir = opendir (NewPrivateFile->FileName);
if (NewPrivateFile->Dir == NULL) {
if (errno == EACCES) {
Status = EFI_ACCESS_DENIED;
} else {
Status = EFI_NOT_FOUND;
}
goto Done;
}
} else {
//
// deal with file
//
NewPrivateFile->fd = open (
NewPrivateFile->FileName,
((OpenMode & EFI_FILE_MODE_CREATE) ? O_CREAT : 0) | (NewPrivateFile->IsOpenedByRead ? O_RDONLY : O_RDWR),
0666
);
if (NewPrivateFile->fd < 0) {
if (errno == ENOENT) {
Status = EFI_NOT_FOUND;
} else {
Status = EFI_ACCESS_DENIED;
}
}
}
if ((OpenMode & EFI_FILE_MODE_CREATE) && (Status == EFI_SUCCESS)) {
//
// Set the attribute
//
InfoSize = 0;
Info = NULL;
Status = PosixFileGetInfo (&NewPrivateFile->EfiFile, &gEfiFileInfoGuid, &InfoSize, Info);
if (Status != EFI_BUFFER_TOO_SMALL) {
Status = EFI_DEVICE_ERROR;
goto Done;
}
Info = malloc (InfoSize);
if (Info == NULL) {
goto Done;
}
Status = PosixFileGetInfo (&NewPrivateFile->EfiFile, &gEfiFileInfoGuid, &InfoSize, Info);
if (EFI_ERROR (Status)) {
goto Done;
}
Info->Attribute = Attributes;
PosixFileSetInfo (&NewPrivateFile->EfiFile, &gEfiFileInfoGuid, InfoSize, Info);
free (Info);
}
Done:;
if (TrailingDash) {
FileName[StrLen (FileName) + 1] = 0;
FileName[StrLen (FileName)] = L'\\';
}
if (EFI_ERROR (Status)) {
if (NewPrivateFile) {
if (NewPrivateFile->FileName) {
free (NewPrivateFile->FileName);
}
free (NewPrivateFile);
}
} else {
*NewHandle = &NewPrivateFile->EfiFile;
}
return Status;
}
/**
Close the file handle
@param This Protocol instance pointer.
@retval EFI_SUCCESS The device was opened.
**/
EFI_STATUS
PosixFileCLose (
IN EFI_FILE_PROTOCOL *This
)
{
EMU_EFI_FILE_PRIVATE *PrivateFile;
PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
if (PrivateFile->fd >= 0) {
close (PrivateFile->fd);
}
if (PrivateFile->Dir != NULL) {
closedir (PrivateFile->Dir);
}
PrivateFile->fd = -1;
PrivateFile->Dir = NULL;
if (PrivateFile->FileName) {
free (PrivateFile->FileName);
}
free (PrivateFile);
return EFI_SUCCESS;
}
/**
Close and delete the file handle.
@param This Protocol instance pointer.
@retval EFI_SUCCESS The device was opened.
@retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.
**/
EFI_STATUS
PosixFileDelete (
IN EFI_FILE_PROTOCOL *This
)
{
EFI_STATUS Status;
EMU_EFI_FILE_PRIVATE *PrivateFile;
PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
Status = EFI_WARN_DELETE_FAILURE;
if (PrivateFile->IsDirectoryPath) {
if (PrivateFile->Dir != NULL) {
closedir (PrivateFile->Dir);
PrivateFile->Dir = NULL;
}
if (rmdir (PrivateFile->FileName) == 0) {
Status = EFI_SUCCESS;
}
} else {
close (PrivateFile->fd);
PrivateFile->fd = -1;
if (!PrivateFile->IsOpenedByRead) {
if (!unlink (PrivateFile->FileName)) {
Status = EFI_SUCCESS;
}
}
}
free (PrivateFile->FileName);
free (PrivateFile);
return Status;
}
/**
Read data from the file.
@param This Protocol instance pointer.
@param BufferSize On input size of buffer, on output amount of data in buffer.
@param Buffer The buffer in which data is read.
@retval EFI_SUCCESS Data was read.
@retval EFI_NO_MEDIA The device has no media.
@retval EFI_DEVICE_ERROR The device reported an error.
@retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
@retval EFI_BUFFER_TO_SMALL BufferSize is too small. BufferSize contains required size.
**/
EFI_STATUS
PosixFileRead (
IN EFI_FILE_PROTOCOL *This,
IN OUT UINTN *BufferSize,
OUT VOID *Buffer
)
{
EMU_EFI_FILE_PRIVATE *PrivateFile;
EFI_STATUS Status;
int Res;
UINTN Size;
UINTN NameSize;
UINTN ResultSize;
CHAR8 *FullFileName;
UINTN FullFileNameSize;
PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
if (!PrivateFile->IsDirectoryPath) {
if (PrivateFile->fd < 0) {
Status = EFI_DEVICE_ERROR;
goto Done;
}
Res = read (PrivateFile->fd, Buffer, *BufferSize);
if (Res < 0) {
Status = EFI_DEVICE_ERROR;
goto Done;
}
*BufferSize = Res;
Status = EFI_SUCCESS;
goto Done;
}
//
// Read on a directory.
//
if (PrivateFile->Dir == NULL) {
Status = EFI_DEVICE_ERROR;
goto Done;
}
if (PrivateFile->Dirent == NULL) {
PrivateFile->Dirent = readdir (PrivateFile->Dir);
if (PrivateFile->Dirent == NULL) {
*BufferSize = 0;
Status = EFI_SUCCESS;
goto Done;
}
}
Size = SIZE_OF_EFI_FILE_INFO;
NameSize = AsciiStrLen (PrivateFile->Dirent->d_name) + 1;
ResultSize = Size + 2 * NameSize;
if (*BufferSize < ResultSize) {
*BufferSize = ResultSize;
Status = EFI_BUFFER_TOO_SMALL;
goto Done;
}
Status = EFI_SUCCESS;
*BufferSize = ResultSize;
FullFileNameSize = AsciiStrLen (PrivateFile->FileName) + 1 + NameSize;
FullFileName = malloc (FullFileNameSize);
if (FullFileName == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto Done;
}
AsciiStrCpyS (FullFileName, FullFileNameSize, PrivateFile->FileName);
AsciiStrCatS (FullFileName, FullFileNameSize, "/");
AsciiStrCatS (FullFileName, FullFileNameSize, PrivateFile->Dirent->d_name);
Status = UnixSimpleFileSystemFileInfo (
PrivateFile,
FullFileName,
BufferSize,
Buffer
);
free (FullFileName);
PrivateFile->Dirent = NULL;
Done:
return Status;
}
/**
Write data to a file.
@param This Protocol instance pointer.
@param BufferSize On input size of buffer, on output amount of data in buffer.
@param Buffer The buffer in which data to write.
@retval EFI_SUCCESS Data was written.
@retval EFI_UNSUPPORTED Writes to Open directory are not supported.
@retval EFI_NO_MEDIA The device has no media.
@retval EFI_DEVICE_ERROR The device reported an error.
@retval EFI_DEVICE_ERROR An attempt was made to write to a deleted file.
@retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
@retval EFI_WRITE_PROTECTED The device is write protected.
@retval EFI_ACCESS_DENIED The file was open for read only.
@retval EFI_VOLUME_FULL The volume is full.
**/
EFI_STATUS
PosixFileWrite (
IN EFI_FILE_PROTOCOL *This,
IN OUT UINTN *BufferSize,
IN VOID *Buffer
)
{
EMU_EFI_FILE_PRIVATE *PrivateFile;
int Res;
PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
if (PrivateFile->fd < 0) {
return EFI_DEVICE_ERROR;
}
if (PrivateFile->IsDirectoryPath) {
return EFI_UNSUPPORTED;
}
if (PrivateFile->IsOpenedByRead) {
return EFI_ACCESS_DENIED;
}
Res = write (PrivateFile->fd, Buffer, *BufferSize);
if (Res == (UINTN)-1) {
return ErrnoToEfiStatus ();
}
*BufferSize = Res;
return EFI_SUCCESS;
}
/**
Set a files current position
@param This Protocol instance pointer.
@param Position Byte position from the start of the file.
@retval EFI_SUCCESS Data was written.
@retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open.
**/
EFI_STATUS
PosixFileSetPossition (
IN EFI_FILE_PROTOCOL *This,
IN UINT64 Position
)
{
EMU_EFI_FILE_PRIVATE *PrivateFile;
off_t Pos;
PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
if (PrivateFile->IsDirectoryPath) {
if (Position != 0) {
return EFI_UNSUPPORTED;
}
if (PrivateFile->Dir == NULL) {
return EFI_DEVICE_ERROR;
}
rewinddir (PrivateFile->Dir);
return EFI_SUCCESS;
} else {
if (Position == (UINT64)-1) {
Pos = lseek (PrivateFile->fd, 0, SEEK_END);
} else {
Pos = lseek (PrivateFile->fd, Position, SEEK_SET);
}
if (Pos == (off_t)-1) {
return ErrnoToEfiStatus ();
}
return EFI_SUCCESS;
}
}
/**
Get a file's current position
@param This Protocol instance pointer.
@param Position Byte position from the start of the file.
@retval EFI_SUCCESS Data was written.
@retval EFI_UNSUPPORTED Seek request for non-zero is not valid on open..
**/
EFI_STATUS
PosixFileGetPossition (
IN EFI_FILE_PROTOCOL *This,
OUT UINT64 *Position
)
{
EFI_STATUS Status;
EMU_EFI_FILE_PRIVATE *PrivateFile;
PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
if (PrivateFile->IsDirectoryPath) {
Status = EFI_UNSUPPORTED;
} else {
*Position = (UINT64)lseek (PrivateFile->fd, 0, SEEK_CUR);
Status = (*Position == (UINT64)-1) ? ErrnoToEfiStatus () : EFI_SUCCESS;
}
return Status;
}
/**
Get information about a file.
@param This Protocol instance pointer.
@param InformationType Type of information to return in Buffer.
@param BufferSize On input size of buffer, on output amount of data in buffer.
@param Buffer The buffer to return data.
@retval EFI_SUCCESS Data was returned.
@retval EFI_UNSUPPORTED InformationType is not supported.
@retval EFI_NO_MEDIA The device has no media.
@retval EFI_DEVICE_ERROR The device reported an error.
@retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
@retval EFI_WRITE_PROTECTED The device is write protected.
@retval EFI_ACCESS_DENIED The file was open for read only.
@retval EFI_BUFFER_TOO_SMALL Buffer was too small; required size returned in BufferSize.
**/
EFI_STATUS
PosixFileGetInfo (
IN EFI_FILE_PROTOCOL *This,
IN EFI_GUID *InformationType,
IN OUT UINTN *BufferSize,
OUT VOID *Buffer
)
{
EFI_STATUS Status;
EMU_EFI_FILE_PRIVATE *PrivateFile;
EFI_FILE_SYSTEM_INFO *FileSystemInfoBuffer;
int UnixStatus;
EMU_SIMPLE_FILE_SYSTEM_PRIVATE *PrivateRoot;
struct statfs buf;
PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
PrivateRoot = EMU_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile->SimpleFileSystem);
Status = EFI_SUCCESS;
if (CompareGuid (InformationType, &gEfiFileInfoGuid)) {
Status = UnixSimpleFileSystemFileInfo (PrivateFile, NULL, BufferSize, Buffer);
} else if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {
if (*BufferSize < SIZE_OF_EFI_FILE_SYSTEM_INFO + StrSize (PrivateRoot->VolumeLabel)) {
*BufferSize = SIZE_OF_EFI_FILE_SYSTEM_INFO + StrSize (PrivateRoot->VolumeLabel);
return EFI_BUFFER_TOO_SMALL;
}
UnixStatus = statfs (PrivateFile->FileName, &buf);
if (UnixStatus < 0) {
return EFI_DEVICE_ERROR;
}
FileSystemInfoBuffer = (EFI_FILE_SYSTEM_INFO *)Buffer;
FileSystemInfoBuffer->Size = SIZE_OF_EFI_FILE_SYSTEM_INFO + StrSize (PrivateRoot->VolumeLabel);
FileSystemInfoBuffer->ReadOnly = FALSE;
//
// Succeeded
//
FileSystemInfoBuffer->VolumeSize = MultU64x32 (buf.f_blocks, buf.f_bsize);
FileSystemInfoBuffer->FreeSpace = MultU64x32 (buf.f_bavail, buf.f_bsize);
FileSystemInfoBuffer->BlockSize = buf.f_bsize;
StrCpyS (
(CHAR16 *)FileSystemInfoBuffer->VolumeLabel,
(*BufferSize - SIZE_OF_EFI_FILE_SYSTEM_INFO) / sizeof (CHAR16),
PrivateRoot->VolumeLabel
);
*BufferSize = SIZE_OF_EFI_FILE_SYSTEM_INFO + StrSize (PrivateRoot->VolumeLabel);
} else if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {
if (*BufferSize < StrSize (PrivateRoot->VolumeLabel)) {
*BufferSize = StrSize (PrivateRoot->VolumeLabel);
return EFI_BUFFER_TOO_SMALL;
}
StrCpyS (
(CHAR16 *)Buffer,
*BufferSize / sizeof (CHAR16),
PrivateRoot->VolumeLabel
);
*BufferSize = StrSize (PrivateRoot->VolumeLabel);
}
return Status;
}
/**
Set information about a file
@param File Protocol instance pointer.
@param InformationType Type of information in Buffer.
@param BufferSize Size of buffer.
@param Buffer The data to write.
@retval EFI_SUCCESS Data was returned.
@retval EFI_UNSUPPORTED InformationType is not supported.
@retval EFI_NO_MEDIA The device has no media.
@retval EFI_DEVICE_ERROR The device reported an error.
@retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
@retval EFI_WRITE_PROTECTED The device is write protected.
@retval EFI_ACCESS_DENIED The file was open for read only.
**/
EFI_STATUS
PosixFileSetInfo (
IN EFI_FILE_PROTOCOL *This,
IN EFI_GUID *InformationType,
IN UINTN BufferSize,
IN VOID *Buffer
)
{
EMU_SIMPLE_FILE_SYSTEM_PRIVATE *PrivateRoot;
EMU_EFI_FILE_PRIVATE *PrivateFile;
EFI_FILE_INFO *OldFileInfo;
EFI_FILE_INFO *NewFileInfo;
EFI_STATUS Status;
UINTN OldInfoSize;
mode_t NewAttr;
struct stat OldAttr;
CHAR8 *OldFileName;
CHAR8 *NewFileName;
CHAR8 *CharPointer;
BOOLEAN AttrChangeFlag;
BOOLEAN NameChangeFlag;
BOOLEAN SizeChangeFlag;
BOOLEAN TimeChangeFlag;
struct tm NewLastAccessSystemTime;
struct tm NewLastWriteSystemTime;
EFI_FILE_SYSTEM_INFO *NewFileSystemInfo;
CHAR8 *AsciiFilePtr;
CHAR16 *UnicodeFilePtr;
int UnixStatus;
struct utimbuf Utime;
UINTN Size;
PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
PrivateRoot = EMU_SIMPLE_FILE_SYSTEM_PRIVATE_DATA_FROM_THIS (PrivateFile->SimpleFileSystem);
errno = 0;
Status = EFI_UNSUPPORTED;
OldFileInfo = NewFileInfo = NULL;
OldFileName = NewFileName = NULL;
AttrChangeFlag = NameChangeFlag = SizeChangeFlag = TimeChangeFlag = FALSE;
//
// Set file system information.
//
if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {
if (BufferSize < (SIZE_OF_EFI_FILE_SYSTEM_INFO + StrSize (PrivateRoot->VolumeLabel))) {
Status = EFI_BAD_BUFFER_SIZE;
goto Done;
}
NewFileSystemInfo = (EFI_FILE_SYSTEM_INFO *)Buffer;
free (PrivateRoot->VolumeLabel);
PrivateRoot->VolumeLabel = malloc (StrSize (NewFileSystemInfo->VolumeLabel));
if (PrivateRoot->VolumeLabel == NULL) {
goto Done;
}
StrCpyS (
PrivateRoot->VolumeLabel,
StrSize (NewFileSystemInfo->VolumeLabel) / sizeof (CHAR16),
NewFileSystemInfo->VolumeLabel
);
Status = EFI_SUCCESS;
goto Done;
}
//
// Set volume label information.
//
if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {
if (BufferSize < StrSize (PrivateRoot->VolumeLabel)) {
Status = EFI_BAD_BUFFER_SIZE;
goto Done;
}
StrCpyS (
PrivateRoot->VolumeLabel,
StrSize (PrivateRoot->VolumeLabel) / sizeof (CHAR16),
(CHAR16 *)Buffer
);
Status = EFI_SUCCESS;
goto Done;
}
if (!CompareGuid (InformationType, &gEfiFileInfoGuid)) {
Status = EFI_UNSUPPORTED;
goto Done;
}
if (BufferSize < SIZE_OF_EFI_FILE_INFO) {
Status = EFI_BAD_BUFFER_SIZE;
goto Done;
}
//
// Set file/directory information.
//
//
// Check for invalid set file information parameters.
//
NewFileInfo = (EFI_FILE_INFO *)Buffer;
if ((NewFileInfo->Size <= sizeof (EFI_FILE_INFO)) ||
(NewFileInfo->Attribute &~(EFI_FILE_VALID_ATTR)) ||
((sizeof (UINTN) == 4) && (NewFileInfo->Size > 0xFFFFFFFF))
)
{
Status = EFI_INVALID_PARAMETER;
goto Done;
}
//
// Get current file information so we can determine what kind
// of change request this is.
//
OldInfoSize = 0;
Status = UnixSimpleFileSystemFileInfo (PrivateFile, NULL, &OldInfoSize, NULL);
if (Status != EFI_BUFFER_TOO_SMALL) {
Status = EFI_DEVICE_ERROR;
goto Done;
}
OldFileInfo = malloc (OldInfoSize);
if (OldFileInfo == NULL) {
goto Done;
}
Status = UnixSimpleFileSystemFileInfo (PrivateFile, NULL, &OldInfoSize, OldFileInfo);
if (EFI_ERROR (Status)) {
goto Done;
}
OldFileName = malloc (AsciiStrSize (PrivateFile->FileName));
if (OldFileName == NULL) {
goto Done;
}
AsciiStrCpyS (
OldFileName,
AsciiStrSize (PrivateFile->FileName),
PrivateFile->FileName
);
//
// Make full pathname from new filename and rootpath.
//
if (NewFileInfo->FileName[0] == '\\') {
Size = AsciiStrLen (PrivateRoot->FilePath) + 1 + StrLen (NewFileInfo->FileName) + 1;
NewFileName = malloc (Size);
if (NewFileName == NULL) {
goto Done;
}
AsciiStrCpyS (NewFileName, Size, PrivateRoot->FilePath);
AsciiFilePtr = NewFileName + AsciiStrLen (NewFileName);
UnicodeFilePtr = NewFileInfo->FileName + 1;
*AsciiFilePtr++ = '/';
} else {
Size = AsciiStrLen (PrivateFile->FileName) + 2 + StrLen (NewFileInfo->FileName) + 1;
NewFileName = malloc (Size);
if (NewFileName == NULL) {
goto Done;
}
AsciiStrCpyS (NewFileName, Size, PrivateRoot->FilePath);
AsciiFilePtr = NewFileName + AsciiStrLen (NewFileName);
if ((AsciiFilePtr[-1] != '/') && (NewFileInfo->FileName[0] != '/')) {
// make sure there is a / between Root FilePath and NewFileInfo Filename
AsciiFilePtr[0] = '/';
AsciiFilePtr[1] = '\0';
AsciiFilePtr++;
}
UnicodeFilePtr = NewFileInfo->FileName;
}
// Convert to ascii.
while (*UnicodeFilePtr) {
*AsciiFilePtr++ = *UnicodeFilePtr++;
}
*AsciiFilePtr = 0;
//
// Is there an attribute change request?
//
if (NewFileInfo->Attribute != OldFileInfo->Attribute) {
if ((NewFileInfo->Attribute & EFI_FILE_DIRECTORY) != (OldFileInfo->Attribute & EFI_FILE_DIRECTORY)) {
Status = EFI_INVALID_PARAMETER;
goto Done;
}
AttrChangeFlag = TRUE;
}
//
// Is there a name change request?
// bugbug: - Should really use EFI_UNICODE_COLLATION_PROTOCOL
//
if (StrCmp (NewFileInfo->FileName, OldFileInfo->FileName)) {
NameChangeFlag = TRUE;
}
//
// Is there a size change request?
//
if (NewFileInfo->FileSize != OldFileInfo->FileSize) {
SizeChangeFlag = TRUE;
}
//
// Is there a time stamp change request?
//
if (!IsZero (&NewFileInfo->CreateTime, sizeof (EFI_TIME)) &&
CompareMem (&NewFileInfo->CreateTime, &OldFileInfo->CreateTime, sizeof (EFI_TIME))
)
{
TimeChangeFlag = TRUE;
} else if (!IsZero (&NewFileInfo->LastAccessTime, sizeof (EFI_TIME)) &&
CompareMem (&NewFileInfo->LastAccessTime, &OldFileInfo->LastAccessTime, sizeof (EFI_TIME))
)
{
TimeChangeFlag = TRUE;
} else if (!IsZero (&NewFileInfo->ModificationTime, sizeof (EFI_TIME)) &&
CompareMem (&NewFileInfo->ModificationTime, &OldFileInfo->ModificationTime, sizeof (EFI_TIME))
)
{
TimeChangeFlag = TRUE;
}
//
// All done if there are no change requests being made.
//
if (!(AttrChangeFlag || NameChangeFlag || SizeChangeFlag || TimeChangeFlag)) {
Status = EFI_SUCCESS;
goto Done;
}
//
// Set file or directory information.
//
if (stat (OldFileName, &OldAttr) != 0) {
Status = ErrnoToEfiStatus ();
goto Done;
}
//
// Name change.
//
if (NameChangeFlag) {
//
// Close the handles first
//
if (PrivateFile->IsOpenedByRead) {
Status = EFI_ACCESS_DENIED;
goto Done;
}
for (CharPointer = NewFileName; *CharPointer != 0 && *CharPointer != L'/'; CharPointer++) {
}
if (*CharPointer != 0) {
Status = EFI_ACCESS_DENIED;
goto Done;
}
UnixStatus = rename (OldFileName, NewFileName);
if (UnixStatus == 0) {
//
// modify file name
//
free (PrivateFile->FileName);
PrivateFile->FileName = malloc (AsciiStrSize (NewFileName));
if (PrivateFile->FileName == NULL) {
goto Done;
}
AsciiStrCpyS (
PrivateFile->FileName,
AsciiStrSize (NewFileName),
NewFileName
);
} else {
Status = EFI_DEVICE_ERROR;
goto Done;
}
}
//
// Size change
//
if (SizeChangeFlag) {
if (PrivateFile->IsDirectoryPath) {
Status = EFI_UNSUPPORTED;
goto Done;
}
if (PrivateFile->IsOpenedByRead || OldFileInfo->Attribute & EFI_FILE_READ_ONLY) {
Status = EFI_ACCESS_DENIED;
goto Done;
}
if (ftruncate (PrivateFile->fd, NewFileInfo->FileSize) != 0) {
Status = ErrnoToEfiStatus ();
goto Done;
}
}
//
// Time change
//
if (TimeChangeFlag) {
NewLastAccessSystemTime.tm_year = NewFileInfo->LastAccessTime.Year;
NewLastAccessSystemTime.tm_mon = NewFileInfo->LastAccessTime.Month;
NewLastAccessSystemTime.tm_mday = NewFileInfo->LastAccessTime.Day;
NewLastAccessSystemTime.tm_hour = NewFileInfo->LastAccessTime.Hour;
NewLastAccessSystemTime.tm_min = NewFileInfo->LastAccessTime.Minute;
NewLastAccessSystemTime.tm_sec = NewFileInfo->LastAccessTime.Second;
NewLastAccessSystemTime.tm_isdst = 0;
Utime.actime = mktime (&NewLastAccessSystemTime);
NewLastWriteSystemTime.tm_year = NewFileInfo->ModificationTime.Year;
NewLastWriteSystemTime.tm_mon = NewFileInfo->ModificationTime.Month;
NewLastWriteSystemTime.tm_mday = NewFileInfo->ModificationTime.Day;
NewLastWriteSystemTime.tm_hour = NewFileInfo->ModificationTime.Hour;
NewLastWriteSystemTime.tm_min = NewFileInfo->ModificationTime.Minute;
NewLastWriteSystemTime.tm_sec = NewFileInfo->ModificationTime.Second;
NewLastWriteSystemTime.tm_isdst = 0;
Utime.modtime = mktime (&NewLastWriteSystemTime);
if ((Utime.actime == (time_t)-1) || (Utime.modtime == (time_t)-1)) {
goto Done;
}
if (utime (PrivateFile->FileName, &Utime) == -1) {
Status = ErrnoToEfiStatus ();
goto Done;
}
}
//
// No matter about AttrChangeFlag, Attribute must be set.
// Because operation before may cause attribute change.
//
NewAttr = OldAttr.st_mode;
if (NewFileInfo->Attribute & EFI_FILE_READ_ONLY) {
NewAttr &= ~(S_IRUSR | S_IRGRP | S_IROTH);
} else {
NewAttr |= S_IRUSR;
}
if (chmod (NewFileName, NewAttr) != 0) {
Status = ErrnoToEfiStatus ();
}
Done:
if (OldFileInfo != NULL) {
free (OldFileInfo);
}
if (OldFileName != NULL) {
free (OldFileName);
}
if (NewFileName != NULL) {
free (NewFileName);
}
return Status;
}
/**
Flush data back for the file handle.
@param This Protocol instance pointer.
@retval EFI_SUCCESS Data was written.
@retval EFI_UNSUPPORTED Writes to Open directory are not supported.
@retval EFI_NO_MEDIA The device has no media.
@retval EFI_DEVICE_ERROR The device reported an error.
@retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
@retval EFI_WRITE_PROTECTED The device is write protected.
@retval EFI_ACCESS_DENIED The file was open for read only.
@retval EFI_VOLUME_FULL The volume is full.
**/
EFI_STATUS
PosixFileFlush (
IN EFI_FILE_PROTOCOL *This
)
{
EMU_EFI_FILE_PRIVATE *PrivateFile;
PrivateFile = EMU_EFI_FILE_PRIVATE_DATA_FROM_THIS (This);
if (PrivateFile->IsDirectoryPath) {
return EFI_UNSUPPORTED;
}
if (PrivateFile->IsOpenedByRead) {
return EFI_ACCESS_DENIED;
}
if (PrivateFile->fd < 0) {
return EFI_DEVICE_ERROR;
}
if (fsync (PrivateFile->fd) != 0) {
return ErrnoToEfiStatus ();
}
return EFI_SUCCESS;
}
EFI_STATUS
PosixFileSystmeThunkOpen (
IN EMU_IO_THUNK_PROTOCOL *This
)
{
EMU_SIMPLE_FILE_SYSTEM_PRIVATE *Private;
UINTN i;
if (This->Private != NULL) {
return EFI_ALREADY_STARTED;
}
if (!CompareGuid (This->Protocol, &gEfiSimpleFileSystemProtocolGuid)) {
return EFI_UNSUPPORTED;
}
Private = malloc (sizeof (EMU_SIMPLE_FILE_SYSTEM_PRIVATE));
if (Private == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Private->FilePath = malloc (StrLen (This->ConfigString) + 1);
if (Private->FilePath == NULL) {
free (Private);
return EFI_OUT_OF_RESOURCES;
}
// Convert Unicode to Ascii
for (i = 0; This->ConfigString[i] != 0; i++) {
Private->FilePath[i] = This->ConfigString[i];
}
Private->FilePath[i] = 0;
Private->VolumeLabel = malloc (StrSize (L"EFI_EMULATED"));
if (Private->VolumeLabel == NULL) {
free (Private->FilePath);
free (Private);
return EFI_OUT_OF_RESOURCES;
}
StrCpyS (
Private->VolumeLabel,
StrSize (L"EFI_EMULATED") / sizeof (CHAR16),
L"EFI_EMULATED"
);
Private->Signature = EMU_SIMPLE_FILE_SYSTEM_PRIVATE_SIGNATURE;
Private->Thunk = This;
CopyMem (&Private->SimpleFileSystem, &gPosixFileSystemProtocol, sizeof (Private->SimpleFileSystem));
Private->FileHandlesOpen = FALSE;
This->Interface = &Private->SimpleFileSystem;
This->Private = Private;
return EFI_SUCCESS;
}
EFI_STATUS
PosixFileSystmeThunkClose (
IN EMU_IO_THUNK_PROTOCOL *This
)
{
EMU_SIMPLE_FILE_SYSTEM_PRIVATE *Private;
if (!CompareGuid (This->Protocol, &gEfiSimpleFileSystemProtocolGuid)) {
return EFI_UNSUPPORTED;
}
Private = This->Private;
if (Private->FileHandlesOpen) {
//
// Close only supported if all the EFI_FILE_HANDLEs have been closed.
//
return EFI_NOT_READY;
}
if (This->Private != NULL) {
if (Private->VolumeLabel != NULL) {
free (Private->VolumeLabel);
}
free (This->Private);
This->Private = NULL;
}
return EFI_SUCCESS;
}
EMU_IO_THUNK_PROTOCOL gPosixFileSystemThunkIo = {
&gEfiSimpleFileSystemProtocolGuid,
NULL,
NULL,
0,
GasketPosixFileSystmeThunkOpen,
GasketPosixFileSystmeThunkClose,
NULL
};
|