https://git.reactos.org/?p=reactos.git;a=commitdiff;h=63e22b82877ec5cca928c1c16101df4c3209c01c

commit 63e22b82877ec5cca928c1c16101df4c3209c01c
Author:     Hermès Bélusca-Maïto <[email protected]>
AuthorDate: Sun Oct 6 19:22:18 2019 +0200
Commit:     Hermès Bélusca-Maïto <[email protected]>
CommitDate: Tue Oct 8 01:30:25 2019 +0200

    [FREELDR] Add support for loading Linux in x64 FreeLdr. Part 2/2: C code.
    
    - Re-enable custom and Linux boot menu entries.
    - Fix compilation and add address limit checks for x64 build of linux 
loader.
---
 boot/freeldr/freeldr/bootmgr.c          |  5 +++--
 boot/freeldr/freeldr/custom.c           | 14 ++++++-------
 boot/freeldr/freeldr/include/custom.h   |  4 ++--
 boot/freeldr/freeldr/include/linux.h    |  4 ++--
 boot/freeldr/freeldr/include/miscboot.h |  4 ++--
 boot/freeldr/freeldr/linuxboot.c        | 36 +++++++++++++++++----------------
 boot/freeldr/freeldr/miscboot.c         |  4 ++--
 7 files changed, 37 insertions(+), 34 deletions(-)

diff --git a/boot/freeldr/freeldr/bootmgr.c b/boot/freeldr/freeldr/bootmgr.c
index 4a1923dff19..9830c2ec74f 100644
--- a/boot/freeldr/freeldr/bootmgr.c
+++ b/boot/freeldr/freeldr/bootmgr.c
@@ -54,12 +54,13 @@ static const struct
 {
     {"ReactOSSetup", EditCustomBootReactOSSetup, LoadReactOSSetup},
 
-#ifdef _M_IX86
+#if defined(_M_IX86) || defined(_M_AMD64)
     {"Drive"       , EditCustomBootDisk      , LoadAndBootDevice},
     {"Partition"   , EditCustomBootPartition , LoadAndBootDevice},
     {"BootSector"  , EditCustomBootSectorFile, LoadAndBootDevice},
-
     {"Linux"       , EditCustomBootLinux, LoadAndBootLinux  },
+#endif
+#ifdef _M_IX86
     {"WindowsNT40" , EditCustomBootNTOS , LoadAndBootWindows},
 #endif
     {"Windows"     , EditCustomBootNTOS , LoadAndBootWindows},
diff --git a/boot/freeldr/freeldr/custom.c b/boot/freeldr/freeldr/custom.c
index 604d7ac9629..7cfd38c8d40 100644
--- a/boot/freeldr/freeldr/custom.c
+++ b/boot/freeldr/freeldr/custom.c
@@ -23,14 +23,14 @@
 
 /* GLOBALS 
********************************************************************/
 
-#ifdef _M_IX86
+#if defined(_M_IX86) || defined(_M_AMD64)
 
 const CHAR BootSectorFilePrompt[] = "Enter the boot sector file 
path.\n\nExamples:\n\\BOOTSECT.DOS\n/boot/bootsect.dos";
 const CHAR LinuxKernelPrompt[] = "Enter the Linux kernel image 
path.\n\nExamples:\n/vmlinuz\n/boot/vmlinuz-2.4.18";
 const CHAR LinuxInitrdPrompt[] = "Enter the initrd image 
path.\n\nExamples:\n/initrd.gz\n/boot/root.img.gz\n\nLeave blank for no initial 
ram disk.";
 const CHAR LinuxCommandLinePrompt[] = "Enter the Linux kernel command 
line.\n\nExamples:\nroot=/dev/hda1\nroot=/dev/fd0 read-only\nroot=/dev/sdb1 
init=/sbin/init";
 
-#endif // _M_IX86
+#endif /* _M_IX86 || _M_AMD64 */
 
 const CHAR BootDrivePrompt[] = "Enter the boot drive.\n\nExamples:\nfd0 - 
first floppy drive\nhd0 - first hard drive\nhd1 - second hard drive\ncd0 - 
first CD-ROM drive.\n\nBIOS drive numbers may also be used:\n0 - first floppy 
drive\n0x80 - first hard drive\n0x81 - second hard drive";
 const CHAR BootPartitionPrompt[] = "Enter the boot partition.\n\nEnter 0 for 
the active (bootable) partition.";
@@ -46,7 +46,7 @@ const CHAR CustomBootPrompt[] = "Press ENTER to boot your 
custom boot setup.";
 VOID OptionMenuCustomBoot(VOID)
 {
     PCSTR CustomBootMenuList[] = {
-#ifdef _M_IX86
+#if defined(_M_IX86) || defined(_M_AMD64)
         "Disk",
         "Partition",
         "Boot Sector File",
@@ -75,7 +75,7 @@ VOID OptionMenuCustomBoot(VOID)
     OperatingSystem.SectionId = 0;
     switch (SelectedMenuItem)
     {
-#ifdef _M_IX86
+#if defined(_M_IX86) || defined(_M_AMD64)
         case 0: // Disk
             EditCustomBootDisk(&OperatingSystem);
             break;
@@ -101,7 +101,7 @@ VOID OptionMenuCustomBoot(VOID)
         case 1: // ReactOS Setup
             EditCustomBootReactOS(&OperatingSystem, TRUE);
             break;
-#endif
+#endif /* _M_IX86 || _M_AMD64 */
     }
 
     /* And boot it */
@@ -114,7 +114,7 @@ VOID OptionMenuCustomBoot(VOID)
 
 #endif // HAS_OPTION_MENU_CUSTOM_BOOT
 
-#ifdef _M_IX86
+#if defined(_M_IX86) || defined(_M_AMD64)
 
 VOID
 EditCustomBootDisk(
@@ -612,7 +612,7 @@ EditCustomBootLinux(
     OperatingSystem->LoadIdentifier = "Custom Linux Setup";
 }
 
-#endif // _M_IX86
+#endif /* _M_IX86 || _M_AMD64 */
 
 VOID
 EditCustomBootReactOS(
diff --git a/boot/freeldr/freeldr/include/custom.h 
b/boot/freeldr/freeldr/include/custom.h
index 57164b84c8e..8fb4f7bdae3 100644
--- a/boot/freeldr/freeldr/include/custom.h
+++ b/boot/freeldr/freeldr/include/custom.h
@@ -27,7 +27,7 @@
 VOID OptionMenuCustomBoot(VOID);
 #endif
 
-#ifdef _M_IX86
+#if defined(_M_IX86) || defined(_M_AMD64)
 
 VOID
 EditCustomBootDisk(
@@ -45,7 +45,7 @@ VOID
 EditCustomBootLinux(
     IN OUT OperatingSystemItem* OperatingSystem);
 
-#endif // _M_IX86
+#endif /* _M_IX86 || _M_AMD64 */
 
 VOID
 EditCustomBootReactOS(
diff --git a/boot/freeldr/freeldr/include/linux.h 
b/boot/freeldr/freeldr/include/linux.h
index c51f70dab9f..6bb92d946c7 100644
--- a/boot/freeldr/freeldr/include/linux.h
+++ b/boot/freeldr/freeldr/include/linux.h
@@ -23,7 +23,7 @@
 #ifndef __LINUX_H
 #define __LINUX_H
 
-#ifdef _M_IX86
+#if defined(_M_IX86) || defined(_M_AMD64)
 
 #define LINUX_LOADER_TYPE_LILO          0x01
 #define LINUX_LOADER_TYPE_LOADLIN       0x11
@@ -142,6 +142,6 @@ LoadAndBootLinux(
     IN PCHAR Argv[],
     IN PCHAR Envp[]);
 
-#endif // _M_IX86
+#endif /* _M_IX86 || _M_AMD64 */
 
 #endif // defined __LINUX_H
diff --git a/boot/freeldr/freeldr/include/miscboot.h 
b/boot/freeldr/freeldr/include/miscboot.h
index 9ba1cb8448d..0f05fd1ce03 100644
--- a/boot/freeldr/freeldr/include/miscboot.h
+++ b/boot/freeldr/freeldr/include/miscboot.h
@@ -19,7 +19,7 @@
 
 #pragma once
 
-#ifdef _M_IX86
+#if defined(_M_IX86) || defined(_M_AMD64)
 
 ARC_STATUS
 LoadAndBootDevice(
@@ -27,4 +27,4 @@ LoadAndBootDevice(
     IN PCHAR Argv[],
     IN PCHAR Envp[]);
 
-#endif // _M_IX86
+#endif /* _M_IX86 || _M_AMD64 */
diff --git a/boot/freeldr/freeldr/linuxboot.c b/boot/freeldr/freeldr/linuxboot.c
index fa9ad60d442..fae1f8da6a2 100644
--- a/boot/freeldr/freeldr/linuxboot.c
+++ b/boot/freeldr/freeldr/linuxboot.c
@@ -22,9 +22,7 @@
  * https://www.kernel.org/doc/Documentation/x86/boot.txt
  */
 
-#ifndef _M_ARM
-
-#ifdef _M_IX86
+#if defined(_M_IX86) || defined(_M_AMD64)
 
 /* INCLUDES 
*******************************************************************/
 
@@ -439,7 +437,7 @@ static BOOLEAN LinuxReadKernel(ULONG LinuxKernelFile)
     Position.QuadPart = 512 + SetupSectorSize;
     if (ArcSeek(LinuxKernelFile, &Position, SeekAbsolute) != ESUCCESS)
         return FALSE;
-    for (BytesLoaded=0; BytesLoaded<LinuxKernelSize; )
+    for (BytesLoaded = 0; BytesLoaded < LinuxKernelSize; )
     {
         if (ArcRead(LinuxKernelFile, LoadAddress, LINUX_READ_CHUNK_SIZE, NULL) 
!= ESUCCESS)
             return FALSE;
@@ -492,31 +490,37 @@ static BOOLEAN LinuxCheckKernelVersion(VOID)
 
 static BOOLEAN LinuxReadInitrd(ULONG LinuxInitrdFile)
 {
-    ULONG        BytesLoaded;
-    CHAR    StatusText[260];
+    ULONG BytesLoaded;
+    CHAR  StatusText[260];
 
     RtlStringCbPrintfA(StatusText, sizeof(StatusText), "Loading %s", 
LinuxInitrdName);
     UiDrawStatusText(StatusText);
 
-    // Allocate memory for the ramdisk
-    //LinuxInitrdLoadAddress = MmAllocateMemory(LinuxInitrdSize);
-    // Try to align it at the next MB boundary after the kernel
-    //LinuxInitrdLoadAddress = MmAllocateMemoryAtAddress(LinuxInitrdSize, 
(PVOID)ROUND_UP((LINUX_KERNEL_LOAD_ADDRESS + LinuxKernelSize), 0x100000));
+    /* Allocate memory for the ramdisk, below 4GB */
+    // LinuxInitrdLoadAddress = MmAllocateMemory(LinuxInitrdSize);
+    /* Try to align it at the next MB boundary after the kernel */
+    // LinuxInitrdLoadAddress = MmAllocateMemoryAtAddress(LinuxInitrdSize, 
(PVOID)ROUND_UP((LINUX_KERNEL_LOAD_ADDRESS + LinuxKernelSize), 0x100000));
     if (LinuxSetupSector->Version <= 0x0202)
     {
+#ifdef _M_AMD64
+        C_ASSERT(LINUX_MAX_INITRD_ADDRESS < 0x100000000);
+#endif
         LinuxInitrdLoadAddress = 
MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize, 
(PVOID)LINUX_MAX_INITRD_ADDRESS, LoaderSystemCode);
     }
     else
     {
-        LinuxInitrdLoadAddress = 
MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize, 
(PVOID)LinuxSetupSector->InitrdAddressMax, LoaderSystemCode);
+        LinuxInitrdLoadAddress = 
MmAllocateHighestMemoryBelowAddress(LinuxInitrdSize, 
UlongToPtr(LinuxSetupSector->InitrdAddressMax), LoaderSystemCode);
     }
     if (LinuxInitrdLoadAddress == NULL)
     {
         return FALSE;
     }
+#ifdef _M_AMD64
+    ASSERT((ULONG_PTR)LinuxInitrdLoadAddress < 0x100000000);
+#endif
 
     /* Set the information in the setup struct */
-    LinuxSetupSector->RamdiskAddress = (ULONG)LinuxInitrdLoadAddress;
+    LinuxSetupSector->RamdiskAddress = PtrToUlong(LinuxInitrdLoadAddress);
     LinuxSetupSector->RamdiskSize = LinuxInitrdSize;
 
     TRACE("RamdiskAddress: 0x%x\n", LinuxSetupSector->RamdiskAddress);
@@ -528,9 +532,9 @@ static BOOLEAN LinuxReadInitrd(ULONG LinuxInitrdFile)
     }
 
     /* Load the ramdisk */
-    for (BytesLoaded=0; BytesLoaded<LinuxInitrdSize; )
+    for (BytesLoaded = 0; BytesLoaded < LinuxInitrdSize; )
     {
-        if (ArcRead(LinuxInitrdFile, (PVOID)LinuxInitrdLoadAddress, 
LINUX_READ_CHUNK_SIZE, NULL) != ESUCCESS)
+        if (ArcRead(LinuxInitrdFile, LinuxInitrdLoadAddress, 
LINUX_READ_CHUNK_SIZE, NULL) != ESUCCESS)
             return FALSE;
 
         BytesLoaded += LINUX_READ_CHUNK_SIZE;
@@ -542,6 +546,4 @@ static BOOLEAN LinuxReadInitrd(ULONG LinuxInitrdFile)
     return TRUE;
 }
 
-#endif // _M_IX86
-
-#endif
+#endif /* _M_IX86 || _M_AMD64 */
diff --git a/boot/freeldr/freeldr/miscboot.c b/boot/freeldr/freeldr/miscboot.c
index 6ea56631654..a00072d3efd 100644
--- a/boot/freeldr/freeldr/miscboot.c
+++ b/boot/freeldr/freeldr/miscboot.c
@@ -17,7 +17,7 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifdef _M_IX86
+#if defined(_M_IX86) || defined(_M_AMD64)
 
 /* INCLUDES 
*******************************************************************/
 
@@ -331,4 +331,4 @@ LoadAndBootDevice(
     return ESUCCESS;
 }
 
-#endif // _M_IX86
+#endif /* _M_IX86 || _M_AMD64 */

Reply via email to