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
| #include <Guid/FileInfo.h> #include <Library/BaseMemoryLib.h> #include <Library/CacheMaintenanceLib.h> #include <Library/MemoryAllocationLib.h> #include <Library/UefiBootServicesTableLib.h> #include <Library/UefiLib.h> #include <Protocol/SimpleFileSystem.h>
EFI_STATUS ReadFileByName(IN CHAR16 *FileName, OUT UINT8 **FileData, OUT UINTN *FileSize) { EFI_FILE_INFO *FileInfo; UINTN FileInfoSize; EFI_FILE_PROTOCOL *FileHandle = NULL; EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem; EFI_FILE_PROTOCOL *Root; EFI_STATUS Status; UINTN HandleCount = 0; UINTN HandleIndex = 0; EFI_HANDLE *HandleBuffer = NULL; Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiSimpleFileSystemProtocolGuid, NULL, &HandleCount, &HandleBuffer); if (EFI_ERROR(Status) || HandleCount == 0) { Print(L"Locate SimpleFileSystemProtocolHandle Buffer error"); return Status; }
for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) { Status = gBS->OpenProtocol(HandleBuffer[HandleIndex], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&SimpleFileSystem, gImageHandle, NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); if (EFI_ERROR(Status)) { Print(L"Open SimpleFileSystemProtocol error,at HandleIndex %d", HandleIndex); continue; } Status = SimpleFileSystem->OpenVolume(SimpleFileSystem, &Root); if (EFI_ERROR(Status)) { Print(L"Open Root Volume error at HandleIndex%d", HandleIndex); gBS->CloseProtocol(HandleBuffer[HandleIndex], &gEfiSimpleFileSystemProtocolGuid, gImageHandle, NULL); continue; } Status = Root->Open(Root, &FileHandle, FileName, EFI_FILE_MODE_READ, 0); if ((FileHandle == NULL) || (EFI_ERROR(Status))) { Print(L"Open file %s failed at HandleIndex%d\n", FileName, HandleIndex); Root->Close(Root); gBS->CloseProtocol(HandleBuffer[HandleIndex], &gEfiSimpleFileSystemProtocolGuid, gImageHandle, NULL); continue; } Print(L"FileHandle: 0x%p at HandleIndex%d\n", FileHandle, HandleIndex); FileInfoSize = sizeof(EFI_FILE_INFO) + 1024; FileInfo = AllocateZeroPool(FileInfoSize); if (FileInfo == NULL) { Print(L"can not allocate %d size for FineInfo at HandleIndex%d\n", FileInfoSize, HandleIndex); FileHandle->Close(FileHandle); Root->Close(Root); gBS->CloseProtocol(HandleBuffer[HandleIndex], &gEfiSimpleFileSystemProtocolGuid, gImageHandle, NULL); continue; } Status = FileHandle->GetInfo(FileHandle, &gEfiFileInfoGuid, &FileInfoSize, FileInfo); if (EFI_ERROR(Status)) { Print(L"Get FileInfo failed at HandleIndex%d\n", HandleIndex); FileHandle->Close(FileHandle); Root->Close(Root); gBS->CloseProtocol(HandleBuffer[HandleIndex], &gEfiSimpleFileSystemProtocolGuid, gImageHandle, NULL); continue; } Print(L"FileInfo: 0x%p\n", FileInfo); *FileSize = (UINTN)FileInfo->FileSize + sizeof(CHAR16); *FileData = AllocateZeroPool(*FileSize); if (*FileData == NULL) { Print(L"locate file data size %d failed at HandleIndex%d\n", *FileSize, HandleIndex); FileHandle->Close(FileHandle); gBS->FreePool(FileInfo); Root->Close(Root); gBS->CloseProtocol(HandleBuffer[HandleIndex], &gEfiSimpleFileSystemProtocolGuid, gImageHandle, NULL); continue; } else { Print(L"size of %s is %d\n", FileName, *FileSize); Print(L"FileData: 0x%p\n", *FileData); }
Status = FileHandle->Read(FileHandle, FileSize, (VOID *)*FileData); if (EFI_ERROR(Status)) { Print(L"open %s file failed at HandleIndex%d\n", FileName, HandleIndex); FileHandle->Close(FileHandle); gBS->FreePool(FileInfo); Root->Close(Root); gBS->CloseProtocol(HandleBuffer[HandleIndex], &gEfiSimpleFileSystemProtocolGuid, gImageHandle, NULL); continue; } else { Print(L"open %s file success at HandleIndex%d\n", FileName, HandleIndex); } FileHandle->Close(FileHandle); Print(L"FileHandle is freed normally at HandleIndex%d\n", HandleIndex); gBS->FreePool(FileInfo); Print(L"FileInfo is freed normally at HandleIndex%d\n", HandleIndex); Root->Close(Root); gBS->CloseProtocol(HandleBuffer[HandleIndex], &gEfiSimpleFileSystemProtocolGuid, gImageHandle, NULL); Print(L"Operation Success at HandleIndex%d\n", HandleIndex); break; }
if (HandleBuffer != NULL) { gBS->FreePool(HandleBuffer); } return Status; }
|