Index: reactos/boot/freeldr/freeldr/arch/i386/pcmem.c
--- reactos/boot/freeldr/freeldr/arch/i386/pcmem.c	(revision 71366)
+++ reactos/boot/freeldr/freeldr/arch/i386/pcmem.c	(working copy)
@@ -27,6 +27,12 @@
 
 DBG_DEFAULT_CHANNEL(MEMORY);
 
+#define ULONGLONG_ALIGN_DOWN_BY(size, align) \
+    ((ULONGLONG)(size) & ~((ULONGLONG)(align) - 1))
+
+#define ULONGLONG_ALIGN_UP_BY(size, align) \
+    (ULONGLONG_ALIGN_DOWN_BY(((ULONGLONG)(size) + align - 1), align))
+
 #define MAX_BIOS_DESCRIPTORS 80
 
 BIOS_MEMORY_MAP PcBiosMemoryMap[MAX_BIOS_DESCRIPTORS];
@@ -129,7 +135,7 @@
     *pMemoryAtOneMB = (*pMemoryAtOneMB << 8);
 
     TRACE("Int15h Failed\n");
-    TRACE("CMOS reports: 0x%x\n", *pMemoryAtOneMB);
+    TRACE("CMOS reports: 0x%lx\n", *pMemoryAtOneMB);
 
     if (*pMemoryAtOneMB != 0)
     {
@@ -144,7 +150,7 @@
 {
     REGS Regs;
 
-    TRACE("GetConventionalMemorySize()\n");
+    TRACE("PcMemGetConventionalMemorySize()\n");
 
     /* Int 12h
      * BIOS - GET MEMORY SIZE
@@ -204,9 +210,10 @@
     ULONGLONG RealBaseAddress, EndAddress, RealSize;
     TYPE_OF_MEMORY MemoryType;
     ULONG Size, RequiredSize;
+
     ASSERT(PcBiosMapCount == 0);
 
-    TRACE("GetBiosMemoryMap()\n");
+    TRACE("PcMemGetBiosMemoryMap()\n");
 
     /* Make sure the usable memory is large enough. To do this we check the 16
        bit value at address 0x413 inside the BDA, which gives us the usable size
@@ -219,8 +226,8 @@
             MEMORY_INIT_FAILURE,
             __FILE__,
             __LINE__,
-            "The BIOS reported a usable memory range up to 0x%x, which is too small!\n"
-            "Required size is 0x%x\n\n"
+            "The BIOS reported a usable memory range up to 0x%lx, which is too small!\n"
+            "Required size is 0x%lx\n\n"
             "If you see this, please report to the ReactOS team!",
             Size, RequiredSize);
     }
@@ -256,20 +263,29 @@
         Regs.w.di = BIOSCALLBUFOFFSET;
         Int386(0x15, &Regs, &Regs);
 
-        TRACE("Memory Map Entry %d\n", PcBiosMapCount);
+        TRACE("Memory Map Entry %lu\n", PcBiosMapCount);
         TRACE("Int15h AX=E820h\n");
-        TRACE("EAX = 0x%x\n", Regs.x.eax);
-        TRACE("EBX = 0x%x\n", Regs.x.ebx);
-        TRACE("ECX = 0x%x\n", Regs.x.ecx);
+        TRACE("EAX = 0x%lx\n", Regs.x.eax);
+        TRACE("EBX = 0x%lx\n", Regs.x.ebx);
+        TRACE("ECX = 0x%lx\n", Regs.x.ecx);
         TRACE("CF set = %s\n", (Regs.x.eflags & EFLAGS_CF) ? "TRUE" : "FALSE");
 
         /* If the BIOS didn't return 'SMAP' in EAX then
-         * it doesn't support this call. If CF is set, we're done */
-        if (Regs.x.eax != 0x534D4150 || !INT386_SUCCESS(Regs))
+         * it doesn't support this call. */
+        if (Regs.x.eax != 0x534D4150)
         {
+            WARN("BIOS doesn't support Int15h AX=E820h!\n\n");
             break;
         }
 
+        /* If the carry flag is set,
+         * then this call was past the last entry, so we're done. */
+        if (!INT386_SUCCESS(Regs))
+        {
+            TRACE("End of System Memory Map! (Past last)\n\n");
+            break;
+        }
+
         /* Copy data to global buffer */
         RtlCopyMemory(&PcBiosMemoryMap[PcBiosMapCount], (PVOID)BIOSCALLBUFFER, Regs.x.ecx);
 
@@ -285,19 +301,24 @@
             MemoryType = LoaderFree;
 
             /* Align up base of memory range */
-            RealBaseAddress = ALIGN_UP_BY(PcBiosMemoryMap[PcBiosMapCount].BaseAddress,
-                                          PAGE_SIZE);
+            RealBaseAddress = ULONGLONG_ALIGN_UP_BY(
+                                PcBiosMemoryMap[PcBiosMapCount].BaseAddress,
+                                PAGE_SIZE);
 
             /* Calculate aligned EndAddress */
             EndAddress = PcBiosMemoryMap[PcBiosMapCount].BaseAddress +
                          PcBiosMemoryMap[PcBiosMapCount].Length;
-            EndAddress = ALIGN_DOWN_BY(EndAddress, PAGE_SIZE);
+            EndAddress = ULONGLONG_ALIGN_DOWN_BY(EndAddress, PAGE_SIZE);
 
             /* Check if there is anything left */
             if (EndAddress <= RealBaseAddress)
             {
                 /* This doesn't span any page, so continue with next range */
-                continue;
+                TRACE("Skipping aligned range < PAGE_SIZE. (PcBiosMapCount = %lu, BaseAddress = %lu, Length = %lu)\n",
+                      PcBiosMapCount,
+                      PcBiosMemoryMap[PcBiosMapCount].BaseAddress,
+                      PcBiosMemoryMap[PcBiosMapCount].Length);
+                goto nextRange;
             }
 
             /* Calculate the length of the aligned range */
@@ -311,18 +332,31 @@
                 MemoryType = LoaderSpecialMemory;
 
             /* Align down base of memory area */
-            RealBaseAddress = ALIGN_DOWN_BY(PcBiosMemoryMap[PcBiosMapCount].BaseAddress,
-                                            PAGE_SIZE);
+            RealBaseAddress = ULONGLONG_ALIGN_DOWN_BY(
+                                PcBiosMemoryMap[PcBiosMapCount].BaseAddress,
+                                PAGE_SIZE);
 
             /* Calculate the length after aligning the base */
             RealSize = PcBiosMemoryMap[PcBiosMapCount].BaseAddress +
                        PcBiosMemoryMap[PcBiosMapCount].Length - RealBaseAddress;
-            RealSize = ALIGN_UP_BY(RealSize, PAGE_SIZE);
+            RealSize = ULONGLONG_ALIGN_UP_BY(RealSize, PAGE_SIZE);
         }
 
         /* Check if we can add this descriptor */
-        if ((RealSize >= MM_PAGE_SIZE) && (PcMapCount < MaxMemoryMapSize))
+        if (RealSize < MM_PAGE_SIZE)
         {
+            TRACE("Skipping aligned range < MM_PAGE_SIZE. (PcBiosMapCount = %lu, BaseAddress = %lu, Length = %lu)\n",
+                  PcBiosMapCount,
+                  PcBiosMemoryMap[PcBiosMapCount].BaseAddress,
+                  PcBiosMemoryMap[PcBiosMapCount].Length);
+        }
+        else if (PcMapCount >= MaxMemoryMapSize)
+        {
+            WARN("PcMemoryMap is already full! (PcBiosMapCount = %lu, PcMapCount = %lu (>= %lu))\n",
+                 PcBiosMapCount, PcMapCount, MaxMemoryMapSize);
+        }
+        else
+        {
             /* Add the descriptor */
             PcMapCount = AddMemoryDescriptor(PcMemoryMap,
                                            MAX_BIOS_DESCRIPTORS,
@@ -333,17 +367,17 @@
 
         PcBiosMapCount++;
 
-        /* If the continuation value is zero or the
-         * carry flag is set then this was
-         * the last entry so we're done */
+nextRange:
+        /* If the continuation value is zero,
+         * then this was the last entry, so we're done. */
         if (Regs.x.ebx == 0x00000000)
         {
-            TRACE("End Of System Memory Map!\n\n");
+            TRACE("End of System Memory Map! (Reset)\n\n");
             break;
         }
     }
 
-    TRACE("GetBiosMemoryMap end, PcBiosMapCount = %ld\n", PcBiosMapCount);
+    TRACE("PcMemGetBiosMemoryMap end: PcBiosMapCount = %lu\n", PcBiosMapCount);
     return PcBiosMapCount;
 }
 
@@ -415,6 +449,7 @@
     ULONG ExtendedMemorySizeAtOneMB;
     ULONG ExtendedMemorySizeAtSixteenMB;
     ULONG EbdaBase, EbdaSize;
+
     TRACE("PcMemGetMemoryMap()\n");
 
     EntryCount = PcMemGetBiosMemoryMap(PcMemoryMap, MAX_BIOS_DESCRIPTORS);
