[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / ex / init.c
index b0e9312..04f8e78 100644 (file)
@@ -211,7 +211,7 @@ ExpInitNls(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
     PLIST_ENTRY ListHead, NextEntry;
     PMEMORY_ALLOCATION_DESCRIPTOR MdBlock;
     ULONG NlsTablesEncountered = 0;
-    ULONG NlsTableSizes[3]; /* 3 NLS tables */
+    SIZE_T NlsTableSizes[3] = {0, 0, 0}; /* 3 NLS tables */
 
     /* Check if this is boot-time phase 0 initialization */
     if (!ExpInitializationPhase)
@@ -298,7 +298,7 @@ ExpInitNls(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
                              NULL,
                              &SectionSize,
                              PAGE_READWRITE,
-                             SEC_COMMIT,
+                             SEC_COMMIT | 0x1,
                              NULL);
     if (!NT_SUCCESS(Status))
     {
@@ -331,7 +331,7 @@ ExpInitNls(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
     }
 
     /* Copy the codepage data in its new location. */
-    ASSERT(SectionBase > MmSystemRangeStart);
+    ASSERT(SectionBase >= MmSystemRangeStart);
     RtlCopyMemory(SectionBase, ExpNlsTableBase, ExpNlsTableSize);
 
     /* Free the previously allocated buffer and set the new location */
@@ -400,17 +400,16 @@ ExpLoadInitialProcess(IN PINIT_BUFFER InitBuffer,
                                      (PVOID*)&ProcessParams,
                                      0,
                                      &Size,
-                                     MEM_COMMIT,
+                                     MEM_RESERVE | MEM_COMMIT,
                                      PAGE_READWRITE);
     if (!NT_SUCCESS(Status))
     {
         /* Failed, display error */
-        p = InitBuffer->DebugBuffer;
-        _snwprintf(p,
-                   256 * sizeof(WCHAR),
+        _snwprintf(InitBuffer->DebugBuffer,
+                   sizeof(InitBuffer->DebugBuffer)/sizeof(WCHAR),
                    L"INIT: Unable to allocate Process Parameters. 0x%lx",
                    Status);
-        RtlInitUnicodeString(&DebugString, p);
+        RtlInitUnicodeString(&DebugString, InitBuffer->DebugBuffer);
         ZwDisplayString(&DebugString);
 
         /* Bugcheck the system */
@@ -418,8 +417,8 @@ ExpLoadInitialProcess(IN PINIT_BUFFER InitBuffer,
     }
 
     /* Setup the basic header, and give the process the low 1MB to itself */
-    ProcessParams->Length = Size;
-    ProcessParams->MaximumLength = Size;
+    ProcessParams->Length = (ULONG)Size;
+    ProcessParams->MaximumLength = (ULONG)Size;
     ProcessParams->Flags = RTL_USER_PROCESS_PARAMETERS_NORMALIZED |
                            RTL_USER_PROCESS_PARAMETERS_RESERVE_1MB;
 
@@ -429,17 +428,16 @@ ExpLoadInitialProcess(IN PINIT_BUFFER InitBuffer,
                                      &EnvironmentPtr,
                                      0,
                                      &Size,
-                                     MEM_COMMIT,
+                                     MEM_RESERVE | MEM_COMMIT,
                                      PAGE_READWRITE);
     if (!NT_SUCCESS(Status))
     {
         /* Failed, display error */
-        p = InitBuffer->DebugBuffer;
-        _snwprintf(p,
-                   256 * sizeof(WCHAR),
+        _snwprintf(InitBuffer->DebugBuffer,
+                   sizeof(InitBuffer->DebugBuffer)/sizeof(WCHAR),
                    L"INIT: Unable to allocate Process Environment. 0x%lx",
                    Status);
-        RtlInitUnicodeString(&DebugString, p);
+        RtlInitUnicodeString(&DebugString, InitBuffer->DebugBuffer);
         ZwDisplayString(&DebugString);
 
         /* Bugcheck the system */
@@ -560,12 +558,11 @@ ExpLoadInitialProcess(IN PINIT_BUFFER InitBuffer,
     if (!NT_SUCCESS(Status))
     {
         /* Failed, display error */
-        p = InitBuffer->DebugBuffer;
-        _snwprintf(p,
-                   256 * sizeof(WCHAR),
+        _snwprintf(InitBuffer->DebugBuffer,
+                   sizeof(InitBuffer->DebugBuffer)/sizeof(WCHAR),
                    L"INIT: Unable to create Session Manager. 0x%lx",
                    Status);
-        RtlInitUnicodeString(&DebugString, p);
+        RtlInitUnicodeString(&DebugString, InitBuffer->DebugBuffer);
         ZwDisplayString(&DebugString);
 
         /* Bugcheck the system */
@@ -577,12 +574,11 @@ ExpLoadInitialProcess(IN PINIT_BUFFER InitBuffer,
     if (!NT_SUCCESS(Status))
     {
         /* Failed, display error */
-        p = InitBuffer->DebugBuffer;
-        _snwprintf(p,
-                   256 * sizeof(WCHAR),
+        _snwprintf(InitBuffer->DebugBuffer,
+                   sizeof(InitBuffer->DebugBuffer)/sizeof(WCHAR),
                    L"INIT: Unable to resume Session Manager. 0x%lx",
                    Status);
-        RtlInitUnicodeString(&DebugString, p);
+        RtlInitUnicodeString(&DebugString, InitBuffer->DebugBuffer);
         ZwDisplayString(&DebugString);
 
         /* Bugcheck the system */
@@ -662,29 +658,68 @@ ExpInitSystemPhase1(VOID)
     ExpInitializePushLocks();
 
     /* Initialize events and event pairs */
-    ExpInitializeEventImplementation();
-    ExpInitializeEventPairImplementation();
-
-    /* Initialize callbacks */
-    ExpInitializeCallbacks();
-
+    if (ExpInitializeEventImplementation() == FALSE)
+    {
+        DPRINT1("Executive: Event initialization failed\n");
+        return FALSE;
+    }
+    if (ExpInitializeEventPairImplementation() == FALSE)
+    {
+        DPRINT1("Executive: Event Pair initialization failed\n");
+        return FALSE;
+    }
+    
     /* Initialize mutants */
-    ExpInitializeMutantImplementation();
-
+    if (ExpInitializeMutantImplementation() == FALSE)
+    {
+        DPRINT1("Executive: Mutant initialization failed\n");
+        return FALSE;
+    }
+    
+    /* Initialize callbacks */
+    if (ExpInitializeCallbacks() == FALSE)
+    {
+        DPRINT1("Executive: Callback initialization failed\n");
+        return FALSE;
+    }
+    
     /* Initialize semaphores */
-    ExpInitializeSemaphoreImplementation();
-
+    if (ExpInitializeSemaphoreImplementation() == FALSE)
+    {
+        DPRINT1("Executive: Semaphore initialization failed\n");
+        return FALSE;
+    }
+    
     /* Initialize timers */
-    ExpInitializeTimerImplementation();
-
+    if (ExpInitializeTimerImplementation() == FALSE)
+    {
+        DPRINT1("Executive: Timer initialization failed\n");
+        return FALSE;
+    }
+    
     /* Initialize profiling */
-    ExpInitializeProfileImplementation();
-
+    if (ExpInitializeProfileImplementation() == FALSE)
+    {
+        DPRINT1("Executive: Profile initialization failed\n");
+        return FALSE;
+    }
+    
     /* Initialize UUIDs */
     ExpInitUuids();
-
+    
+    /* Initialize keyed events */
+    if (ExpInitializeKeyedEventImplementation() == FALSE)
+    {
+        DPRINT1("Executive: Keyed event initialization failed\n");
+        return FALSE;
+    }
+    
     /* Initialize Win32K */
-    ExpWin32kInit();
+    if (ExpWin32kInit() == FALSE)
+    {
+        DPRINT1("Executive: Win32 initialization failed\n");
+        return FALSE;
+    }
     return TRUE;
 }
 
@@ -750,9 +785,9 @@ ExpLoadBootSymbols(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
     ULONG Count, Length;
     PWCHAR Name;
     PLDR_DATA_TABLE_ENTRY LdrEntry;
-    BOOLEAN OverFlow = FALSE;
     CHAR NameBuffer[256];
     STRING SymbolString;
+    NTSTATUS Status;
 
     /* Loop the driver list */
     NextEntry = LoaderBlock->LoadOrderListHead.Flink;
@@ -775,7 +810,7 @@ ExpLoadBootSymbols(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
                 if (sizeof(NameBuffer) < Length + sizeof(ANSI_NULL))
                 {
                     /* It's too long */
-                    OverFlow = TRUE;
+                    Status = STATUS_BUFFER_OVERFLOW;
                 }
                 else
                 {
@@ -789,33 +824,21 @@ ExpLoadBootSymbols(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 
                     /* Null-terminate */
                     NameBuffer[Count] = ANSI_NULL;
+                    Status = STATUS_SUCCESS;
                 }
             }
             else
             {
-                /* This should be a driver, check if it fits */
-                if (sizeof(NameBuffer) <
-                    (sizeof("\\System32\\Drivers\\") +
-                     NtSystemRoot.Length / sizeof(WCHAR) - sizeof(UNICODE_NULL) +
-                     LdrEntry->BaseDllName.Length / sizeof(WCHAR) +
-                     sizeof(ANSI_NULL)))
-                {
-                    /* Buffer too small */
-                    OverFlow = TRUE;
-                    while (TRUE);
-                }
-                else
-                {
-                    /* Otherwise build the name. HACKED for GCC :( */
-                    sprintf(NameBuffer,
-                            "%S\\System32\\Drivers\\%S",
-                            &SharedUserData->NtSystemRoot[2],
-                            LdrEntry->BaseDllName.Buffer);
-                }
+                /* Safely print the string into our buffer */
+                Status = RtlStringCbPrintfA(NameBuffer,
+                                            sizeof(NameBuffer),
+                                            "%S\\System32\\Drivers\\%wZ",
+                                            &SharedUserData->NtSystemRoot[2],
+                                            &LdrEntry->BaseDllName);
             }
 
             /* Check if the buffer was ok */
-            if (!OverFlow)
+            if (NT_SUCCESS(Status))
             {
                 /* Initialize the STRING for the debugger */
                 RtlInitString(&SymbolString, NameBuffer);
@@ -823,7 +846,7 @@ ExpLoadBootSymbols(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
                 /* Load the symbols */
                 DbgLoadImageSymbols(&SymbolString,
                                     LdrEntry->DllBase,
-                                    (ULONG_PTR)ZwCurrentProcess());
+                                    (ULONG_PTR)PsGetCurrentProcessId());
             }
         }
 
@@ -837,13 +860,13 @@ VOID
 NTAPI
 INIT_FUNCTION
 ExBurnMemory(IN PLOADER_PARAMETER_BLOCK LoaderBlock,
-             IN ULONG PagesToDestroy,
+             IN ULONG_PTR PagesToDestroy,
              IN TYPE_OF_MEMORY MemoryType)
 {
     PLIST_ENTRY ListEntry;
     PMEMORY_ALLOCATION_DESCRIPTOR MemDescriptor;
 
-    DPRINT1("Burn RAM amount: %d pages\n", PagesToDestroy);
+    DPRINT1("Burn RAM amount: %lu pages\n", PagesToDestroy);
 
     /* Loop the memory descriptors, beginning at the end */
     for (ListEntry = LoaderBlock->MemoryDescriptorListHead.Blink;
@@ -993,10 +1016,10 @@ ExpInitializeExecutive(IN ULONG Cpu,
     NlsData = LoaderBlock->NlsData;
     ExpNlsTableBase = NlsData->AnsiCodePageData;
     ExpAnsiCodePageDataOffset = 0;
-    ExpOemCodePageDataOffset = ((ULONG_PTR)NlsData->OemCodePageData -
-                                (ULONG_PTR)NlsData->AnsiCodePageData);
-    ExpUnicodeCaseTableDataOffset = ((ULONG_PTR)NlsData->UnicodeCodePageData -
-                                     (ULONG_PTR)NlsData->AnsiCodePageData);
+    ExpOemCodePageDataOffset = (ULONG)((ULONG_PTR)NlsData->OemCodePageData -
+                                       (ULONG_PTR)NlsData->AnsiCodePageData);
+    ExpUnicodeCaseTableDataOffset = (ULONG)((ULONG_PTR)NlsData->UnicodeCodePageData -
+                                            (ULONG_PTR)NlsData->AnsiCodePageData);
 
     /* Initialize the NLS Tables */
     RtlInitNlsTables((PVOID)((ULONG_PTR)ExpNlsTableBase +
@@ -1050,15 +1073,16 @@ ExpInitializeExecutive(IN ULONG Cpu,
     CmGetSystemControlValues(LoaderBlock->RegistryBase, CmControlVector);
 
     /* Load static defaults for Service Pack 1 and add our SVN revision */
+    /* Format of CSD : SPMajor - SPMinor */
     CmNtCSDVersion = 0x100 | (KERNEL_VERSION_BUILD_HEX << 16);
     CmNtCSDReleaseType = 0;
 
     /* Set Service Pack data for Service Pack 1 */
-    CmNtSpBuildNumber = 1830;
+    CmNtSpBuildNumber = VER_PRODUCTBUILD_QFE;
     if (!(CmNtCSDVersion & 0xFFFF0000))
     {
         /* Check the release type */
-        if (CmNtCSDReleaseType == 1) CmNtSpBuildNumber |= 1830 << 16;
+        if (CmNtCSDReleaseType == 1) CmNtSpBuildNumber |= VER_PRODUCTBUILD_QFE << 16;
     }
 
     /* Add loaded CmNtGlobalFlag value */
@@ -1084,8 +1108,13 @@ ExpInitializeExecutive(IN ULONG Cpu,
     }
 
     /* Set system ranges */
+#ifdef _M_AMD64
+    SharedUserData->Reserved1 = MM_HIGHEST_USER_ADDRESS_WOW64;
+    SharedUserData->Reserved3 = MM_SYSTEM_RANGE_START_WOW64;
+#else
     SharedUserData->Reserved1 = (ULONG_PTR)MmHighestUserAddress;
     SharedUserData->Reserved3 = (ULONG_PTR)MmSystemRangeStart;
+#endif
 
     /* Make a copy of the NLS Tables */
     ExpInitNls(LoaderBlock);
@@ -1192,7 +1221,7 @@ ExpInitializeExecutive(IN ULONG Cpu,
         /* Add the version format string */
         Status = RtlStringCbPrintfA(RcEnd,
                                     Remaining,
-                                    "v. %u",
+                                    "r%u",
                                     (CmNtCSDVersion & 0xFFFF0000) >> 16);
         if (!NT_SUCCESS(Status))
         {
@@ -1293,6 +1322,10 @@ ExpInitializeExecutive(IN ULONG Cpu,
     SharedUserData->ImageNumberHigh = IMAGE_FILE_MACHINE_NATIVE;
 }
 
+VOID
+NTAPI
+MmFreeLoaderBlock(IN PLOADER_PARAMETER_BLOCK LoaderBlock);
+
 VOID
 NTAPI
 INIT_FUNCTION
@@ -1450,7 +1483,7 @@ Phase1InitializationDiscard(IN PVOID Context)
     else
     {
         /* Use hard-coded banner message */
-        Status = RtlStringCbCopyA(EndBuffer, Remaining, "REACTOS (R)\n");
+        Status = RtlStringCbCopyA(EndBuffer, Remaining, "REACTOS (R)\r\n");
         if (!NT_SUCCESS(Status))
         {
             /* Bugcheck */
@@ -1553,7 +1586,7 @@ Phase1InitializationDiscard(IN PVOID Context)
                                 sizeof(InitBuffer->VersionBuffer),
                                 NT_SUCCESS(MsgStatus) ?
                                 (PCHAR)MsgEntry->Text :
-                                "%u System Processor [%u MB Memory] %Z\n",
+                                "%u System Processor [%u MB Memory] %Z\r\n",
                                 KeNumberProcessors,
                                 Size,
                                 &TempString);
@@ -1913,14 +1946,12 @@ Phase1InitializationDiscard(IN PVOID Context)
 
     /* Make sure nobody touches the loader block again */
     if (LoaderBlock == KeLoaderBlock) KeLoaderBlock = NULL;
+    MmFreeLoaderBlock(LoaderBlock);
     LoaderBlock = Context = NULL;
 
     /* Update progress bar */
     InbvUpdateProgressBar(100);
 
-    /* Clean the screen */
-    if (InbvBootDriverInstalled) FinalizeBootLogo();
-
     /* Allow strings to be displayed */
     InbvEnableDisplayString(TRUE);
 
@@ -1959,6 +1990,9 @@ Phase1InitializationDiscard(IN PVOID Context)
                         &Size,
                         MEM_RELEASE);
 
+    /* Clean the screen */
+    if (InbvBootDriverInstalled) FinalizeBootLogo();
+
     /* Increase init phase */
     ExpInitializationPhase++;