[BOOTMGFW]
authorAlex Ionescu <aionescu@gmail.com>
Mon, 7 Sep 2015 23:48:21 +0000 (23:48 +0000)
committerAlex Ionescu <aionescu@gmail.com>
Mon, 7 Sep 2015 23:48:21 +0000 (23:48 +0000)
- And again with the fucking non-recursion.

svn path=/trunk/; revision=69104

reactos/boot/environ/app/bootmgr/bootmgr.c
reactos/boot/environ/app/bootmgr/efiemu.c
reactos/boot/environ/include/bl.h

index 849b699..69f0902 100644 (file)
 
 /* DATA VARIABLES ************************************************************/
 
+#include <initguid.h>
+DEFINE_GUID(GUID_WINDOWS_BOOTMGR,
+            0x9DEA862C,
+            0x5CDD,
+            0x4E70,
+            0xAC, 0xC1, 0xF3, 0x2B, 0x34, 0x4D, 0x47, 0x95);
+
 ULONGLONG ApplicationStartTime;
 ULONGLONG PostTime;
+GUID BmApplicationIdentifier;
 
 /* FUNCTIONS *****************************************************************/
 
+PGUID
+BlGetApplicationIdentifier (
+    VOID
+    )
+{
+    return NULL;
+}
+
+PWCHAR BootDirectory;
+
+NTSTATUS
+BmFwInitializeBootDirectoryPath()
+{
+#if 0
+    PWCHAR FinalPath;
+    NTSTATUS Status;
+    PWCHAR BcdDirectory;
+    UNICODE_STRING BcdPath;
+    ULONG FinalSize, FileHandle, DeviceHandle;
+
+    BcdPath.MaximumLength = 0;
+    BcdPath.Buffer = NULL;
+
+    FinalPath = NULL;
+
+    FileHandle = -1;
+    DeviceHandle = -1;
+
+    Status = BlpDeviceOpen(BlpBootDevice, 1u, 0, &DeviceHandle);
+    if (!NT_SUCCESS(Status))
+    {
+        goto Quickie;
+    }
+
+    Status = BmpFwGetApplicationDirectoryPath(&BcdPath);
+    BcdDirectory = BcdPath.Buffer;
+    if (!NT_SUCCESS(Status))
+    {
+        goto Quickie;
+    }
+
+    FinalSize = BcdPath.MaximumLength + sizeof(L"\\BCD") - sizeof(UNICODE_NULL);
+    if (FinalSize < BcdPath.MaximumLength)
+    {
+        goto Quickie;
+    }
+
+    FinalPath = BlMmAllocateHeap(FinalSize);
+    if (!FinalPath)
+    {
+        goto Quickie;
+    }
+
+    RtlZeroMemory(FinalPath, FinalSize);
+    RtlCopyMemory(FinalPath, BcdDirectory, BcdPath.MaximumLength);
+    wcsncat(FinalPath, L"\\BCD", FinalSize / sizeof(WCHAR));
+
+    EfiPrintf(L"Opening: %s\r\n", FinalPath);
+    Status = BlFileOpen(DeviceHandle, FinalPath, 1u, &FileHandle);
+    if (!NT_SUCCESS(Status))
+    {
+        BootDirectory = BcdDirectory;
+        goto Quickie;
+    }
+
+    BootDirectory = L"\\EFI\\Microsoft\\Boot";
+
+Quickie:
+    if (BcdDirectory)
+    {
+        Status = BlMmFreeHeap(BcdDirectory);
+    }
+    if (FinalPath)
+    {
+        Status = BlMmFreeHeap(FinalPath);
+    }
+    if (FileHandle != -1)
+    {
+        Status = BlFileClose(FileHandle);
+    }
+    if (DeviceHandle != -1)
+    {
+        Status = BlDeviceClose(DeviceHandle);
+    }
+    return Status;
+#else
+    return STATUS_NOT_IMPLEMENTED;
+#endif
+}
+
+
 /*++
  * @name BmMain
  *
@@ -37,8 +136,15 @@ BmMain (
 {
     NTSTATUS Status;
     BL_LIBRARY_PARAMETERS LibraryParameters;
+    PBL_RETURN_ARGUMENTS ReturnArguments;
+    BOOLEAN RebootOnError;
+    PGUID AppIdentifier;
+//    HANDLE BcdHandle;
 
-    EarlyPrint(L"ReactOS UEFI Boot Manager Initializing...\n");
+    EfiPrintf(L"ReactOS UEFI Boot Manager Initializing...\n");
+
+    /* Reading the BCD can change this later on */
+    RebootOnError = FALSE;
 
     /* Save the start/end-of-POST time */
     ApplicationStartTime = __rdtsc();
@@ -53,9 +159,59 @@ BmMain (
 
     /* Initialize the boot library */
     Status = BlInitializeLibrary(BootParameters, &LibraryParameters);
+    if (!NT_SUCCESS(Status))
+    {
+        /* Check for failure due to invalid application entry */
+        if (Status != STATUS_INVALID_PARAMETER_9)
+        {
+            /* Specifically print out what happened */
+            EfiPrintf(L"BlInitializeLibrary failed 0x%x\r\n", Status);
+        }
+
+        /* Go to exit path */
+        goto Quickie;
+    }
+
+    /* Get the application identifier */
+    AppIdentifier = BlGetApplicationIdentifier();
+    if (!AppIdentifier)
+    {
+        /* None was given, so set our default one */
+        AppIdentifier = (PGUID)&GUID_WINDOWS_BOOTMGR;
+    }
+    
+    /* Save our identifier */
+    BmApplicationIdentifier = *AppIdentifier;
+
+    /* Initialize the file system to open a handle to our root boot directory */
+    BmFwInitializeBootDirectoryPath();
+
+    //Status = BmOpenDataStore(&BcdHandle);
+
+    EfiPrintf(L"We are A-OK!\n");
+    EfiStall(10000000);
+
+Quickie:
+    /* Check if we should reboot */
+    if ((RebootOnError) ||
+        (BlpApplicationEntry.Flags & BL_APPLICATION_ENTRY_REBOOT_ON_ERROR))
+    {
+        /* Reboot the box */
+        BlFwReboot();
+        Status = STATUS_SUCCESS;
+    }
+    else
+    {
+        /* Return back to the caller with the error argument encoded */
+        ReturnArguments = (PVOID)((ULONG_PTR)BootParameters + BootParameters->ReturnArgumentsOffset);
+        ReturnArguments->Version = BL_RETURN_ARGUMENTS_VERSION;
+        ReturnArguments->Status = Status;
+
+        /* Tear down the boot library*/
+        BlDestroyLibrary();
+    }
 
-    EarlyPrint(L"ReactOS UEFI Boot Manager Exiting: %lx\n", Status);
-    EfiStall(3000000);
+    /* Return back status */
     return Status;
 }
 
index 2edc2ea..a249c66 100644 (file)
@@ -665,7 +665,6 @@ EfiInitpCreateApplicationEntry (
     RemainingSize = MaximumLength;
     if (RemainingSize < sizeof(BL_APPLICATION_ENTRY))
     {
-        EarlyPrint(L"Remaining size too small!\n");
         Status = STATUS_INVALID_PARAMETER;
         goto Quickie;
     }
@@ -709,7 +708,6 @@ EfiInitpCreateApplicationEntry (
     if (!NT_SUCCESS(Status))
     {
         /* We failed, so mark the option as such and return an empty one */
-        EarlyPrint(L"Failed to convert device path: %lx\n", Status);
         Entry->BcdData.Empty = TRUE;
         TotalOptionSize = sizeof(BL_BCD_OPTION);
         goto Quickie;
@@ -731,7 +729,6 @@ EfiInitpCreateApplicationEntry (
     {
         /* lol */
         Status = STATUS_NOT_IMPLEMENTED;
-        EarlyPrint(L"UDP Boot not supported!\n");
     }
     else
     {
@@ -745,7 +742,6 @@ EfiInitpCreateApplicationEntry (
     /* Bail out on failure */
     if (!NT_SUCCESS(Status))
     {
-        EarlyPrint(L"Failed to convert file path: %lx\n", Status);
         goto Quickie;
     }
 
@@ -784,7 +780,6 @@ EfiInitpCreateApplicationEntry (
                                                   RemainingSize);
             if (!NT_SUCCESS(Status))
             {
-                EarlyPrint(L"Failed to convert OS device path: %lx\n", Status);
                 goto Quickie;
             }
 
@@ -811,7 +806,6 @@ EfiInitpCreateApplicationEntry (
                                                 RemainingSize);
             if (!NT_SUCCESS(Status))
             {
-                EarlyPrint(L"Failed to convert OS file path: %lx\n", Status);
                 goto Quickie;
             }
 
@@ -898,7 +892,6 @@ EfiInitCreateInputParametersEx (
                                           (VOID**)&LoadedImage);
     if (Status != EFI_SUCCESS)
     {
-        EarlyPrint(L"Loaded image failed: %lx\n", Status);
         return NULL;
     }
 
@@ -912,7 +905,6 @@ EfiInitCreateInputParametersEx (
                                           (VOID**)&DevicePath);
     if (Status != EFI_SUCCESS)
     {
-        EarlyPrint(L"Device Path failed: %lx\n", Status);
         return NULL;
     }
 
@@ -1020,10 +1012,6 @@ EfiEntry (
 {
     NTSTATUS Status;
     PBOOT_APPLICATION_PARAMETER_BLOCK BootParameters;
-    extern EFI_SYSTEM_TABLE *g_SystemTable;
-
-    /* Temporary debugging string */
-    g_SystemTable = SystemTable;
 
     /* Convert EFI parameters to Windows Boot Application parameters */
     BootParameters = EfiInitCreateInputParametersEx(ImageHandle, SystemTable);
@@ -1035,7 +1023,6 @@ EfiEntry (
     else
     {
         /* Conversion failed, bail out */
-        EarlyPrint(L"EFI Input Conversion failed\n");
         Status = STATUS_INVALID_PARAMETER;
     }
 
index 81b47dc..41f94a0 100644 (file)
@@ -29,9 +29,6 @@
 #include <GraphicsOutput.h>
 #include <UgaDraw.h>
 
-VOID
-EarlyPrint(_In_ PWCHAR Format, ...);
-
 /* DEFINES *******************************************************************/
 
 #define BL_APPLICATION_FLAG_CONVERTED_FROM_EFI          0x01
@@ -50,6 +47,7 @@ EarlyPrint(_In_ PWCHAR Format, ...);
 #define BL_FIRMWARE_DESCRIPTOR_VERSION                  2
 
 #define BL_APPLICATION_ENTRY_FLAG_NO_GUID               0x01
+#define BL_APPLICATION_ENTRY_REBOOT_ON_ERROR            0x20
 
 #define BL_CONTEXT_PAGING_ON                            1
 #define BL_CONTEXT_INTERRUPTS_ON                        2
@@ -407,7 +405,8 @@ typedef struct _BL_FIRMWARE_DESCRIPTOR
 typedef struct _BL_RETURN_ARGUMENTS
 {
     ULONG Version;
-    ULONG ReturnArgumentData[6];
+    NTSTATUS Status;
+    ULONG ReturnArgumentData[5];
 } BL_RETURN_ARGUMENTS, *PBL_RETURN_ARGUMENTS;
 
 typedef struct _BL_MEMORY_DESCRIPTOR
@@ -784,8 +783,19 @@ BlpDisplayInitialize (
     _In_ ULONG Flags
     );
 
+VOID
+BlDestroyLibrary (
+    VOID
+    );
+
 /* FIRMWARE ROUTINES *********************************************************/
 
+VOID
+EfiPrintf (
+    _In_ PWCHAR Format,
+    ...
+    );
+
 NTSTATUS
 EfiAllocatePages (
     _In_ ULONG Type,
@@ -878,6 +888,11 @@ EfiGopGetFrameBuffer (
     _Out_ UINTN *FrameBufferSize
     );
 
+VOID
+EfiResetSystem (
+    _In_ EFI_RESET_TYPE ResetType
+    );
+
 /* PLATFORM TIMER ROUTINES ***************************************************/
 
 NTSTATUS
@@ -902,6 +917,11 @@ BlUtlInitialize (
     VOID
     );
 
+VOID
+BlFwReboot (
+    VOID
+    );
+
 /* BCD ROUTINES **************************************************************/
 
 ULONG