- Fix KiDispatchException to unmask KI_EXCEPTION_INTERNAL when setting the exception...
[reactos.git] / reactos / ntoskrnl / mm / sysldr.c
index 8b1d445..d67d4a4 100644 (file)
 \r
 LIST_ENTRY PsLoadedModuleList;\r
 KSPIN_LOCK PsLoadedModuleSpinLock;\r
-PVOID PsNtosImageBase;\r
+ULONG PsNtosImageBase;\r
 KMUTANT MmSystemLoadLock;\r
 extern ULONG NtGlobalFlag;\r
 \r
 /* FUNCTIONS *****************************************************************/\r
 \r
+VOID\r
+NTAPI\r
+MiFreeBootDriverMemory(PVOID BaseAddress,\r
+                       ULONG Length)\r
+{\r
+    ULONG i;\r
+\r
+    /* Loop each page */\r
+    for (i = 0; i < PAGE_ROUND_UP(Length) / PAGE_SIZE; i++)\r
+    {\r
+        /* Free the page */\r
+        MmDeleteVirtualMapping(NULL,\r
+                               (PVOID)((ULONG_PTR)BaseAddress + i * PAGE_SIZE),\r
+                               TRUE,\r
+                               NULL,\r
+                               NULL);\r
+    }\r
+}\r
+\r
 NTSTATUS\r
 NTAPI\r
 MiLoadImageSection(IN OUT PVOID *SectionPtr,\r
@@ -1063,8 +1082,6 @@ MiReloadBootLoadedDrivers(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
     PIMAGE_DATA_DIRECTORY DataDirectory;\r
     PVOID DllBase, NewImageAddress;\r
     NTSTATUS Status;\r
-    ULONG DriverSize = 0, Size;\r
-    PIMAGE_SECTION_HEADER Section;\r
 \r
     /* Loop driver list */\r
     for (NextEntry = LoaderBlock->LoadOrderListHead.Flink;\r
@@ -1091,26 +1108,6 @@ MiReloadBootLoadedDrivers(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
         /* Skip non-drivers */\r
         if (!NtHeader) continue;\r
 \r
-#if 1 // Disable for FreeLDR 2.5\r
-        /*  Get header pointers  */\r
-        Section = IMAGE_FIRST_SECTION(NtHeader);\r
-\r
-        /*  Determine the size of the module  */\r
-        for (i = 0; i < NtHeader->FileHeader.NumberOfSections; i++)\r
-        {\r
-            /* Skip this section if we're not supposed to load it */\r
-            if (!(Section[i].Characteristics & IMAGE_SCN_TYPE_NOLOAD))\r
-            {\r
-                /* Add the size of this section into the total size */\r
-                Size = Section[i].VirtualAddress + Section[i].Misc.VirtualSize;\r
-                DriverSize = max(DriverSize, Size);\r
-            }\r
-        }\r
-\r
-        /* Round up the driver size to section alignment */\r
-        DriverSize = ROUND_UP(DriverSize, NtHeader->OptionalHeader.SectionAlignment);\r
-#endif\r
-\r
         /* Get the file header and make sure we can relocate */\r
         FileHeader = &NtHeader->FileHeader;\r
         if (FileHeader->Characteristics & IMAGE_FILE_RELOCS_STRIPPED) continue;\r
@@ -1132,7 +1129,7 @@ MiReloadBootLoadedDrivers(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
                 LdrEntry->SizeOfImage)\r
             {\r
                 /* They're not, skip */\r
-                 continue;\r
+                continue;\r
             }\r
 \r
             /* We have relocations */\r
@@ -1143,7 +1140,7 @@ MiReloadBootLoadedDrivers(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
         DllBase = LdrEntry->DllBase;\r
 \r
         /*  Allocate a virtual section for the module  */\r
-        NewImageAddress = MmAllocateSection(DriverSize, NULL);\r
+        NewImageAddress = MmAllocateSection(LdrEntry->SizeOfImage, NULL);\r
         if (!NewImageAddress)\r
         {\r
             /* Shouldn't happen */\r
@@ -1155,34 +1152,8 @@ MiReloadBootLoadedDrivers(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
         DPRINT("[Mm0]: Copying from: %p to: %p\n", DllBase, NewImageAddress);\r
         ASSERT(ExpInitializationPhase == 0);\r
 \r
-#if 0 // Enable for FreeLDR 2.5\r
         /* Now copy the entire driver over */\r
-        RtlCopyMemory(NewImageAddress, DllBase, DriverSize);\r
-#else\r
-        /* Copy headers over */\r
-        RtlCopyMemory(NewImageAddress,\r
-                      DllBase,\r
-                      NtHeader->OptionalHeader.SizeOfHeaders);\r
-\r
-        /*  Copy image sections into virtual section  */\r
-        for (i = 0; i < NtHeader->FileHeader.NumberOfSections; i++)\r
-        {\r
-            /* Get the size of this section and check if it's valid and on-disk */\r
-            Size = Section[i].VirtualAddress + Section[i].Misc.VirtualSize;\r
-            if ((Size <= DriverSize) && (Section[i].SizeOfRawData))\r
-            {\r
-                /* Copy the data from the disk to the image */\r
-                RtlCopyMemory((PVOID)((ULONG_PTR)NewImageAddress +\r
-                                      Section[i].VirtualAddress),\r
-                              (PVOID)((ULONG_PTR)DllBase +\r
-                                      Section[i].PointerToRawData),\r
-                              Section[i].Misc.VirtualSize >\r
-                              Section[i].SizeOfRawData ?\r
-                              Section[i].SizeOfRawData :\r
-                              Section[i].Misc.VirtualSize);\r
-            }\r
-        }\r
-#endif\r
+        RtlCopyMemory(NewImageAddress, DllBase, LdrEntry->SizeOfImage);\r
 \r
         /* Sanity check */\r
         ASSERT(*(PULONG)NewImageAddress == *(PULONG)DllBase);\r
@@ -1222,7 +1193,10 @@ MiReloadBootLoadedDrivers(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
         LdrEntry->Flags |= 0x01000000;\r
         LdrEntry->EntryPoint = (PVOID)((ULONG_PTR)NewImageAddress +\r
                                 NtHeader->OptionalHeader.AddressOfEntryPoint);\r
-        LdrEntry->SizeOfImage = DriverSize;\r
+        LdrEntry->SizeOfImage = LdrEntry->SizeOfImage;\r
+\r
+        /* Free the old copy */\r
+        MiFreeBootDriverMemory(DllBase, LdrEntry->SizeOfImage);\r
     }\r
 }\r
 \r
@@ -1244,7 +1218,7 @@ MiInitializeLoadedModuleList(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
     LdrEntry = CONTAINING_RECORD(NextEntry,\r
                                  LDR_DATA_TABLE_ENTRY,\r
                                  InLoadOrderLinks);\r
-    PsNtosImageBase = LdrEntry->DllBase;\r
+    PsNtosImageBase = (ULONG)LdrEntry->DllBase;\r
 \r
     /* Loop the loader block */\r
     while (NextEntry != ListHead)\r
@@ -1543,8 +1517,8 @@ LoaderScan:
         if (!Flags)\r
         {\r
             /* It wasn't, so just return the data */\r
-            *ModuleObject = LdrEntry;\r
-            *ImageBaseAddress = LdrEntry->DllBase;\r
+            if (ModuleObject) *ModuleObject = LdrEntry;\r
+            if (ImageBaseAddress) *ImageBaseAddress = LdrEntry->DllBase;\r
             Status = STATUS_IMAGE_ALREADY_LOADED;\r
         }\r
         else\r