[freeldr] Rework the ramdisk driver to let it be a full device, instead of a deprecat...
[reactos.git] / reactos / boot / freeldr / freeldr / windows / winldr.c
index a90a51b..76622ee 100644 (file)
 #include <ndk/ldrtypes.h>
 #include <debug.h>
 
+// TODO: Move to .h
+void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock,
+                      PVOID *GdtIdt,
+                      ULONG *PcrBasePage,
+                      ULONG *TssBasePage);
+
 //FIXME: Do a better way to retrieve Arc disk information
 extern ULONG reactos_disk_count;
 extern ARC_DISK_SIGNATURE reactos_arc_disk_info[];
@@ -75,7 +81,7 @@ AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK *OutLoaderBlock)
 VOID
 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock,
                        PCHAR Options,
-                       PCHAR SystemPath,
+                       PCHAR SystemRoot,
                        PCHAR BootPath,
                        USHORT VersionToBoot)
 {
@@ -86,20 +92,15 @@ WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock,
        //CHAR  ArcBoot[] = "multi(0)disk(0)rdisk(0)partition(1)";
 
        CHAR    HalPath[] = "\\";
-       CHAR    SystemRoot[256];
        CHAR    ArcBoot[256];
        CHAR    MiscFiles[256];
        ULONG i, PathSeparator;
        PLOADER_PARAMETER_EXTENSION Extension;
 
-       LoaderBlock->u.I386.CommonDataArea = NULL; // Force No ABIOS support
-
        /* Construct SystemRoot and ArcBoot from SystemPath */
-       PathSeparator = strstr(SystemPath, "\\") - SystemPath;
-       strncpy(ArcBoot, SystemPath, PathSeparator);
+       PathSeparator = strstr(BootPath, "\\") - BootPath;
+       strncpy(ArcBoot, BootPath, PathSeparator);
        ArcBoot[PathSeparator] = 0;
-       strcpy(SystemRoot, &SystemPath[PathSeparator]);
-       strcat(SystemRoot, "\\");
 
        DPRINTM(DPRINT_WINDOWS, "ArcBoot: %s\n", ArcBoot);
        DPRINTM(DPRINT_WINDOWS, "SystemRoot: %s\n", SystemRoot);
@@ -208,54 +209,6 @@ WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock,
                LoaderBlock->SetupLdrBlock = PaToVa(LoaderBlock->SetupLdrBlock);
 }
 
-// Last step before going virtual
-void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock,
-                      PVOID *GdtIdt,
-                      ULONG *PcrBasePage,
-                      ULONG *TssBasePage)
-{
-       ULONG TssSize;
-       ULONG TssPages;
-       ULONG_PTR Pcr = 0;
-       ULONG_PTR Tss = 0;
-       ULONG BlockSize, NumPages;
-
-       LoaderBlock->u.I386.CommonDataArea = NULL; //CommonDataArea;
-       LoaderBlock->u.I386.MachineType = 0; // ntldr sets this to 0
-
-       /* Allocate 2 pages for PCR */
-       Pcr = (ULONG_PTR)MmAllocateMemoryWithType(2 * MM_PAGE_SIZE, LoaderStartupPcrPage);
-       *PcrBasePage = Pcr >> MM_PAGE_SHIFT;
-
-       if (Pcr == 0)
-       {
-               UiMessageBox("Can't allocate PCR\n");
-               return;
-       }
-
-       /* Allocate TSS */
-       TssSize = (sizeof(KTSS) + MM_PAGE_SIZE) & ~(MM_PAGE_SIZE - 1);
-       TssPages = TssSize / MM_PAGE_SIZE;
-
-       Tss = (ULONG_PTR)MmAllocateMemoryWithType(TssSize, LoaderMemoryData);
-
-       *TssBasePage = Tss >> MM_PAGE_SHIFT;
-
-       /* Allocate space for new GDT + IDT */
-       BlockSize = NUM_GDT*sizeof(KGDTENTRY) + NUM_IDT*sizeof(KIDTENTRY);//FIXME: Use GDT/IDT limits here?
-       NumPages = (BlockSize + MM_PAGE_SIZE - 1) >> MM_PAGE_SHIFT;
-       *GdtIdt = (PKGDTENTRY)MmAllocateMemoryWithType(NumPages * MM_PAGE_SIZE, LoaderMemoryData);
-
-       if (*GdtIdt == NULL)
-       {
-               UiMessageBox("Can't allocate pages for GDT+IDT!\n");
-               return;
-       }
-
-       /* Zero newly prepared GDT+IDT */
-       RtlZeroMemory(*GdtIdt, NumPages << MM_PAGE_SHIFT);
-}
-
 BOOLEAN
 WinLdrLoadDeviceDriver(PLOADER_PARAMETER_BLOCK LoaderBlock,
                        LPSTR BootPath,
@@ -434,10 +387,11 @@ VOID
 LoadAndBootWindows(PCSTR OperatingSystemName, USHORT OperatingSystemVersion)
 {
        CHAR  MsgBuffer[256];
-       CHAR  SystemPath[512], SearchPath[512];
-       CHAR  FileName[512];
-       CHAR  BootPath[512];
+       CHAR  FullPath[MAX_PATH], SystemRoot[MAX_PATH], BootPath[MAX_PATH];
+       CHAR  FileName[MAX_PATH];
        CHAR  BootOptions[256];
+       PCHAR File;
+       PCHAR PathSeparator;
        PVOID NtosBase = NULL, HalBase = NULL, KdComBase = NULL;
        BOOLEAN Status;
        ULONG SectionId;
@@ -466,12 +420,25 @@ LoadAndBootWindows(PCSTR OperatingSystemName, USHORT OperatingSystemVersion)
        UiDrawProgressBarCenter(1, 100, "Loading Windows...");
 
        /* Make sure the system path is set in the .ini file */
-       if (!IniReadSettingByName(SectionId, "SystemPath", SystemPath, sizeof(SystemPath)))
+       if (!IniReadSettingByName(SectionId, "SystemPath", FullPath, sizeof(FullPath)))
        {
                UiMessageBox("System path not specified for selected operating system.");
                return;
        }
 
+       /* Special case for LiveCD */
+       if (!_strnicmp(FullPath, "LiveCD", strlen("LiveCD")))
+       {
+               strcpy(BootPath, FullPath + strlen("LiveCD"));
+               MachDiskGetBootPath(FullPath, sizeof(FullPath));
+               strcat(FullPath, BootPath);
+       }
+
+       /* Convert FullPath to SystemRoot */
+       PathSeparator = strstr(FullPath, "\\");
+       strcpy(SystemRoot, PathSeparator);
+       strcat(SystemRoot, "\\");
+
        /* Read booting options */
        if (!IniReadSettingByName(SectionId, "Options", BootOptions, sizeof(BootOptions)))
        {
@@ -479,24 +446,38 @@ LoadAndBootWindows(PCSTR OperatingSystemName, USHORT OperatingSystemVersion)
                strcpy(BootOptions, "");
        }
 
-       /* Special case for LiveCD */
-       if (!_strnicmp(SystemPath, "LiveCD", strlen("LiveCD")))
+       //
+       // Check if a ramdisk file was given
+       //
+       File = strstr(BootOptions, "/RDPATH=");
+       if (File)
        {
-               strcpy(BootPath, SystemPath + strlen("LiveCD"));
-               MachDiskGetBootPath(SystemPath, sizeof(SystemPath));
-               strcat(SystemPath, BootPath);
+               //
+               // Copy the file name and everything else after it
+               //
+               strcpy(FileName, File + 8);
+
+               //
+               // Null-terminate
+               //
+               *strstr(FileName, " ") = ANSI_NULL;
+
+               //
+               // Load the ramdisk
+               //
+               RamDiskLoadVirtualFile(FileName);
        }
 
        /* Let user know we started loading */
        UiDrawStatusText("Loading...");
 
        /* append a backslash */
-       strcpy(BootPath, SystemPath);
+       strcpy(BootPath, FullPath);
        if ((strlen(BootPath)==0) ||
            BootPath[strlen(BootPath)] != '\\')
                strcat(BootPath, "\\");
 
-       DPRINTM(DPRINT_WINDOWS,"SystemRoot: '%s'\n", BootPath);
+       DPRINTM(DPRINT_WINDOWS,"BootPath: '%s'\n", BootPath);
 
        /* Allocate and minimalistic-initialize LPB */
        AllocateAndInitLPB(&LoaderBlock);
@@ -538,12 +519,12 @@ LoadAndBootWindows(PCSTR OperatingSystemName, USHORT OperatingSystemVersion)
        }
 
        /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
-       strcpy(SearchPath, BootPath);
-       strcat(SearchPath, "SYSTEM32\\");
-       WinLdrScanImportDescriptorTable(LoaderBlock, SearchPath, KernelDTE);
-       WinLdrScanImportDescriptorTable(LoaderBlock, SearchPath, HalDTE);
+       strcpy(FileName, BootPath);
+       strcat(FileName, "SYSTEM32\\");
+       WinLdrScanImportDescriptorTable(LoaderBlock, FileName, KernelDTE);
+       WinLdrScanImportDescriptorTable(LoaderBlock, FileName, HalDTE);
        if (KdComDTE)
-               WinLdrScanImportDescriptorTable(LoaderBlock, SearchPath, KdComDTE);
+               WinLdrScanImportDescriptorTable(LoaderBlock, FileName, KdComDTE);
 
        /* Load Hive, and then NLS data, OEM font, and prepare boot drivers list */
        Status = WinLdrLoadAndScanSystemHive(LoaderBlock, BootPath);
@@ -557,7 +538,7 @@ LoadAndBootWindows(PCSTR OperatingSystemName, USHORT OperatingSystemVersion)
        WinLdrSetupForNt(LoaderBlock, &GdtIdt, &PcrBasePage, &TssBasePage);
 
        /* Initialize Phase 1 - no drivers loading anymore */
-       WinLdrInitializePhase1(LoaderBlock, BootOptions, SystemPath, BootPath, OperatingSystemVersion);
+       WinLdrInitializePhase1(LoaderBlock, BootOptions, SystemRoot, BootPath, OperatingSystemVersion);
 
        /* Save entry-point pointer and Loader block VAs */
        KiSystemStartup = (KERNEL_ENTRY_POINT)KernelDTE->EntryPoint;