changeset 5:209a1a162228

FSOpen: Open '\kernel' Success
author kono
date Fri, 19 Jan 2024 11:16:05 +0900
parents cc535678286d
children 117ef669f2ce
files gnu-efi-3.0.12/apps/bootloader.c
diffstat 1 files changed, 64 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/gnu-efi-3.0.12/apps/bootloader.c	Fri Jan 19 10:14:55 2024 +0900
+++ b/gnu-efi-3.0.12/apps/bootloader.c	Fri Jan 19 11:16:05 2024 +0900
@@ -23,63 +23,80 @@
 };
 
 
-EFI_STATUS LoadFile(EFI_HANDLE ImageHandle, CHAR16 *FileName, EFI_PHYSICAL_ADDRESS *FileAddr, UINTN *FilePageSize) {
+EFI_STATUS LoadFile(CHAR16 *FileName, EFI_PHYSICAL_ADDRESS *FileAddr, UINTN *FilePageSize) {
     EFI_STATUS Status;
     EFI_FILE_IO_INTERFACE *IOVolume;
     EFI_FILE_HANDLE Root;
     EFI_FILE_HANDLE File;
     Print(L"LoadFile\n");
 
-    Status = uefi_call_wrapper(BS->HandleProtocol, 3, ImageHandle, &FileSystemProtocol, (VOID **)&IOVolume);
-    if (EFI_ERROR(Status)) {
-        Print(L"Failed to locate File System Protocol.\n");
-        return Status;
-    }
-
-    Status = uefi_call_wrapper(IOVolume->OpenVolume, 2, IOVolume, &Root);
+    // Locate all handles that support the Simple File System Protocol.
+    UINTN HandleCount = 5;
+    void **HandleBuffer;
+    Status = LibLocateHandle(ByProtocol, &FileSystemProtocol, NULL, &HandleCount, &HandleBuffer);
     if (EFI_ERROR(Status)) {
-        Print(L"Failed to open volume.\n");
-        return Status;
-    }
-    Print(L"Loading File: %s\n", FileName);
-
-    Status = uefi_call_wrapper(Root->Open, 5, Root, &File, FileName, EFI_FILE_MODE_READ, 0);
-    if (EFI_ERROR(Status)) {
-        Print(L"Failed to open %s\n", FileName);
+        Print(L"Failed to locate handles for file system protocol: %r\n", Status);
         return Status;
     }
 
-    EFI_FILE_INFO *FileInfo;
-    UINTN FileInfoSize;
-    FileInfoSize = sizeof(EFI_FILE_INFO) + 1024;
-    FileInfo = AllocatePool(FileInfoSize);
-    Status = uefi_call_wrapper(File->GetInfo, 4, File, &gEfiFileInfoGuid, &FileInfoSize, FileInfo);
-    if (EFI_ERROR(Status)) {
-        Print(L"Failed to get file info.\n");
-        FreePool(FileInfo);
-        return Status;
+    for (UINTN Index = 0; Index < HandleCount; Index++) {
+        // Try to open the file system
+        Status = uefi_call_wrapper(BS->HandleProtocol, 3, HandleBuffer[Index], &FileSystemProtocol, (void **)&IOVolume);
+
+        if (!EFI_ERROR(Status)) {
+            // Open the root directory
+            Status = uefi_call_wrapper(IOVolume->OpenVolume, 2, IOVolume, &Root);
+            if (EFI_ERROR(Status)) {
+                Print(L"Failed to open root directory: %r\n", Status);
+                continue;
+            }
+
+            // Now open a file
+            Status = uefi_call_wrapper(Root->Open, 5, Root, &File,FileName , EFI_FILE_MODE_READ, 0);
+            if (EFI_ERROR(Status)) {
+                Print(L"Failed to open file: %r\n", Status);
+                uefi_call_wrapper(Root->Close, 1, Root);
+                continue;
+            }
+
+            EFI_FILE_INFO *FileInfo;
+            UINTN FileInfoSize;
+            FileInfoSize = sizeof(EFI_FILE_INFO) + 1024;
+            FileInfo = AllocatePool(FileInfoSize);
+            Status = uefi_call_wrapper(File->GetInfo, 4, File, &gEfiFileInfoGuid, &FileInfoSize, FileInfo);
+            if (EFI_ERROR(Status)) {
+                Print(L"Failed to get file info.\n");
+                FreePool(FileInfo);
+                return Status;
+            }
+
+            UINTN FileSize = FileInfo->FileSize;
+            Print(L"FileSize = %d\n", FileSize);
+            *FilePageSize = (FileSize + 4095) / 4096;
+            *FileAddr = 0;
+            Status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData, *FilePageSize, FileAddr);
+            if (EFI_ERROR(Status)) {
+                Print(L"Failed to allocate pages.\n");
+                FreePool(FileInfo);
+                return Status;
+            }
+
+            Status = uefi_call_wrapper(File->Read, 3, File, &FileSize, (VOID *)*FileAddr);
+            if (EFI_ERROR(Status)) {
+                Print(L"Failed to load file.\n");
+                FreePool(FileInfo);
+                return Status;
+            }
+            Print(L"Successfully loaded file: %s\n", FileName);
+
+            FreePool(FileInfo);
+            // Close the file and root directory
+            uefi_call_wrapper(File->Close, 1, File);
+            uefi_call_wrapper(Root->Close, 1, Root);
+        }
     }
 
-    UINTN FileSize = FileInfo->FileSize;
-    Print(L"FileSize = %d\n", FileSize);
-    *FilePageSize = (FileSize + 4095) / 4096;
-    *FileAddr = 0;
-    Status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData, *FilePageSize, FileAddr);
-    if (EFI_ERROR(Status)) {
-        Print(L"Failed to allocate pages.\n");
-        FreePool(FileInfo);
-        return Status;
-    }
 
-    Status = uefi_call_wrapper(File->Read, 3, File, &FileSize, (VOID *)*FileAddr);
-    if (EFI_ERROR(Status)) {
-        Print(L"Failed to load file.\n");
-        FreePool(FileInfo);
-        return Status;
-    }
-    Print(L"Successfully loaded file: %s\n", FileName);
-
-    FreePool(FileInfo);
     return Status;
 }
 
@@ -92,8 +109,10 @@
     InitializeLib(ImageHandle, SystemTable);
     Print(L"Initializeing\n");
     UINTN BufferPageSize;
+
     Print(L"LoadFile: %s\n", KernelFileName);
-    Status = LoadFile(ImageHandle, KernelFileName, &KernelBaseAddr, &BufferPageSize);
+
+    Status = LoadFile(KernelFileName, &KernelBaseAddr, &BufferPageSize);
     if (EFI_ERROR(Status)) {
         return Status;
     }