[BOOTLIB] Additional EfiPrintf conversion like r73736 for MSVC 2013
[reactos.git] / reactos / boot / environ / lib / mm / i386 / mmx86.c
index 630e9b2..48632a9 100644 (file)
@@ -171,39 +171,6 @@ MmDefZeroVirtualAddressRange (
     return STATUS_NOT_IMPLEMENTED;
 }
 
-NTSTATUS
-Mmx86pMapMemoryRegions (
-    _In_ ULONG Phase,
-    _In_ PBL_MEMORY_DATA MemoryData
-    )
-{
-    BOOLEAN DoDeferred;
-
-    /* In phase 1 we don't initialize deferred mappings*/
-    if (Phase == 1)
-    {
-        DoDeferred = 0;
-    }
-    else
-    {
-        /* Don't do anything if there's nothing to initialize */
-        if (!MmDeferredMappingCount)
-        {
-            return STATUS_SUCCESS;
-        }
-
-        DoDeferred = 1;
-    }
-
-    if (DoDeferred)
-    {
-        EfiPrintf(L"Deferred todo\r\n");
-    }
-
-    EfiPrintf(L"Phase 1 TODO\r\n");
-    return STATUS_NOT_IMPLEMENTED;
-}
-
 BOOLEAN
 MmArchTranslateVirtualAddress (
     _In_ PVOID VirtualAddress, 
@@ -540,10 +507,8 @@ MmSelectMappingAddress (
     }
 
     /* We don't support virtual memory yet @TODO */
-#ifdef _MSC_VER // Fuck gcc.
-    EfiPrintf(L"not yet implemented in " __FUNCTION__ "\r\n");
+    EfiPrintf(L"not yet implemented in %S\r\n", __FUNCTION__);
     EfiStall(1000000);
-#endif
     return STATUS_NOT_IMPLEMENTED;
 }
 
@@ -696,6 +661,158 @@ Mmx86MapInitStructure (
     return Status;
 }
 
+NTSTATUS
+Mmx86pMapMemoryRegions (
+    _In_ ULONG Phase,
+    _In_ PBL_MEMORY_DATA MemoryData
+    )
+{
+    BOOLEAN DoDeferred;
+    ULONG DescriptorCount;
+    PBL_MEMORY_DESCRIPTOR Descriptor;
+    ULONG FinalOffset;
+    PHYSICAL_ADDRESS PhysicalAddress;
+    ULONGLONG Size;
+    NTSTATUS Status;
+    PVOID VirtualAddress;
+    BL_MEMORY_DESCRIPTOR_LIST FirmwareMdl;
+    PLIST_ENTRY Head, NextEntry;
+
+    /* Check which phase this is */
+    if (Phase == 1)
+    {
+        /* In phase 1 we don't initialize deferred mappings */
+        DoDeferred = FALSE;
+    }
+    else
+    {
+        /* Don't do anything if there's nothing to initialize */
+        if (!MmDeferredMappingCount)
+        {
+            return STATUS_SUCCESS;
+        }
+
+        /* We'll do deferred descriptors in phase 2 */
+        DoDeferred = TRUE;
+    }
+
+    /*
+    * Because BL supports cross x86-x64 application launches and a LIST_ENTRY
+    * is of variable size, care must be taken here to ensure that we see a
+    * consistent view of descriptors. BL uses some offset magic to figure out
+    * where the data actually starts, since everything is ULONGLONG past the
+    * LIST_ENTRY itself
+    */
+    FinalOffset = MemoryData->MdListOffset + MemoryData->DescriptorOffset;
+    Descriptor = (PBL_MEMORY_DESCRIPTOR)((ULONG_PTR)MemoryData + FinalOffset -
+                                         FIELD_OFFSET(BL_MEMORY_DESCRIPTOR, BasePage));
+
+    /* Scan all of them */
+    DescriptorCount = MemoryData->DescriptorCount;
+    while (DescriptorCount != 0)
+    {
+        /* Ignore application data */
+        if (Descriptor->Type != BlApplicationData)
+        {
+            /* If this is a ramdisk, do it in phase 2 */
+            if ((Descriptor->Type == BlLoaderRamDisk) == DoDeferred)
+            {
+                /* Get the current physical address and size */
+                PhysicalAddress.QuadPart = Descriptor->BasePage << PAGE_SHIFT;
+                Size = Descriptor->PageCount << PAGE_SHIFT;
+
+                /* Check if it was already mapped */
+                if (Descriptor->VirtualPage)
+                {
+                    /* Use the existing address */
+                    VirtualAddress = (PVOID)(ULONG_PTR)(Descriptor->VirtualPage << PAGE_SHIFT);
+                }
+                else
+                {
+                    /* Use the physical address */
+                    VirtualAddress = (PVOID)(ULONG_PTR)PhysicalAddress.QuadPart;
+                }
+
+                /* Crete the mapping */
+                Status = Mmx86MapInitStructure(VirtualAddress,
+                                               Size,
+                                               PhysicalAddress);
+                if (!NT_SUCCESS(Status))
+                {
+                    return Status;
+                }
+            }
+
+            /* Check if we're in phase 1 and deferring RAM disk */
+            if ((Phase == 1) && (Descriptor->Type == BlLoaderRamDisk))
+            {
+                MmDeferredMappingCount++;
+            }
+        }
+
+        /* Move on to the next descriptor */
+        DescriptorCount--;
+        Descriptor = (PBL_MEMORY_DESCRIPTOR)((ULONG_PTR)Descriptor + MemoryData->DescriptorSize);
+    }
+
+    /* In phase 1, also do UEFI mappings */
+    if (Phase != 2)
+    {
+        /* Get the memory map */
+        MmMdInitializeListHead(&FirmwareMdl);
+        Status = MmFwGetMemoryMap(&FirmwareMdl, BL_MM_FLAG_REQUEST_COALESCING);
+        if (!NT_SUCCESS(Status))
+        {
+            return Status;
+        }
+
+        /* Iterate over it */
+        Head = FirmwareMdl.First;
+        NextEntry = Head->Flink;
+        while (NextEntry != Head)
+        {
+            /* Check if this is a UEFI-related descriptor, unless it's the self-map page */
+            Descriptor = CONTAINING_RECORD(NextEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
+            if (((Descriptor->Type == BlEfiBootMemory) ||
+                 (Descriptor->Type == BlEfiRuntimeMemory) ||
+                 (Descriptor->Type == BlLoaderMemory)) &&
+                ((Descriptor->BasePage << PAGE_SHIFT) != Mmx86SelfMapBase.QuadPart))
+            {
+                /* Identity-map it */
+                PhysicalAddress.QuadPart = Descriptor->BasePage << PAGE_SHIFT;
+                Status = Mmx86MapInitStructure((PVOID)((ULONG_PTR)Descriptor->BasePage << PAGE_SHIFT),
+                                               Descriptor->PageCount << PAGE_SHIFT,
+                                               PhysicalAddress);
+                if (!NT_SUCCESS(Status))
+                {
+                    return Status;
+                }
+            }
+
+            /* Move to the next descriptor */
+            NextEntry = NextEntry->Flink;
+        }
+
+        /* Reset */
+        NextEntry = Head->Flink;
+        while (NextEntry != Head)
+        {
+            /* Get the descriptor */
+            Descriptor = CONTAINING_RECORD(NextEntry, BL_MEMORY_DESCRIPTOR, ListEntry);
+
+            /* Skip to the next entry before we free */
+            NextEntry = NextEntry->Flink;
+
+            /* Remove and free it */
+            MmMdRemoveDescriptorFromList(&FirmwareMdl, Descriptor);
+            MmMdFreeDescriptor(Descriptor);
+        }
+    }
+
+    /* All library mappings identity mapped now */
+    return STATUS_SUCCESS;
+}
+
 NTSTATUS
 Mmx86InitializeMemoryMap (
     _In_ ULONG Phase,
@@ -766,8 +883,7 @@ Mmx86InitializeMemoryMap (
     }
 
     /* More to do */
-    EfiPrintf(L"VM more work\r\n");
-    return STATUS_NOT_IMPLEMENTED;
+    return Mmx86pMapMemoryRegions(Phase, MemoryData);
 }
 
 NTSTATUS
@@ -1049,14 +1165,17 @@ MmArchInitialize (
 
         case BlPae:
 
+            /* We don't support PAE */
             Status = STATUS_NOT_SUPPORTED;
             break;
 
         default:
+
+            /* Invalid architecture type*/
             Status = STATUS_INVALID_PARAMETER;
             break;
     }
 
+    /* Back to caller */
     return Status;
-
 }