[NTOSKRNL] Rewrite/fix our UUID generation implementation
[reactos.git] / ntoskrnl / ex / init.c
index 186584a..1e31003 100644 (file)
 #define NDEBUG
 #include <debug.h>
 
+/* This is the size that we can expect from the win 2003 loader */
+#define LOADER_PARAMETER_EXTENSION_MIN_SIZE \
+    RTL_SIZEOF_THROUGH_FIELD(LOADER_PARAMETER_EXTENSION, AcpiTableSize)
+
 /* Temporary hack */
 BOOLEAN
 NTAPI
@@ -37,9 +41,9 @@ typedef struct _INIT_BUFFER
 /* NT Version Info */
 ULONG NtMajorVersion = VER_PRODUCTMAJORVERSION;
 ULONG NtMinorVersion = VER_PRODUCTMINORVERSION;
-#if DBG
+#if DBG /* Checked Build */
 ULONG NtBuildNumber = VER_PRODUCTBUILD | 0xC0000000;
-#else
+#else   /* Free Build */
 ULONG NtBuildNumber = VER_PRODUCTBUILD;
 #endif
 
@@ -53,7 +57,9 @@ ULONG CmNtCSDVersion;
 ULONG CmNtCSDReleaseType;
 UNICODE_STRING CmVersionString;
 UNICODE_STRING CmCSDVersionString;
-CHAR NtBuildLab[] = KERNEL_VERSION_BUILD_STR;
+
+CHAR NtBuildLab[] = KERNEL_VERSION_BUILD_STR "."
+                    REACTOS_COMPILER_NAME "_" REACTOS_COMPILER_VERSION;
 
 /* Init flags and settings */
 ULONG ExpInitializationPhase;
@@ -85,9 +91,9 @@ BOOLEAN ExpRealTimeIsUniversal;
 
 /* FUNCTIONS ****************************************************************/
 
+INIT_FUNCTION
 NTSTATUS
 NTAPI
-INIT_FUNCTION
 ExpCreateSystemRootLink(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 {
     UNICODE_STRING LinkName;
@@ -197,9 +203,9 @@ ExpCreateSystemRootLink(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
     return STATUS_SUCCESS;
 }
 
+INIT_FUNCTION
 VOID
 NTAPI
-INIT_FUNCTION
 ExpInitNls(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 {
     LARGE_INTEGER SectionSize;
@@ -373,9 +379,9 @@ ExpInitNls(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
     ExpNlsTableBase = SectionBase;
 }
 
+INIT_FUNCTION
 VOID
 NTAPI
-INIT_FUNCTION
 ExpLoadInitialProcess(IN PINIT_BUFFER InitBuffer,
                       OUT PRTL_USER_PROCESS_PARAMETERS *ProcessParameters,
                       OUT PCHAR *ProcessEnvironment)
@@ -522,7 +528,7 @@ ExpLoadInitialProcess(IN PINIT_BUFFER InitBuffer,
                               (USHORT)Size);
 
     /* Append the DLL path to it */
-    RtlAppendUnicodeToString(&Environment, L"Path=" );
+    RtlAppendUnicodeToString(&Environment, L"Path=");
     RtlAppendUnicodeStringToString(&Environment, &ProcessParams->DllPath);
     RtlAppendUnicodeStringToString(&Environment, &NullString);
 
@@ -590,9 +596,9 @@ ExpLoadInitialProcess(IN PINIT_BUFFER InitBuffer,
     *ProcessEnvironment = EnvironmentPtr;
 }
 
+INIT_FUNCTION
 ULONG
 NTAPI
-INIT_FUNCTION
 ExComputeTickCountMultiplier(IN ULONG ClockIncrement)
 {
     ULONG MsRemainder = 0, MsIncrement;
@@ -623,9 +629,9 @@ ExComputeTickCountMultiplier(IN ULONG ClockIncrement)
     return (MsIncrement << 24) | MsRemainder;
 }
 
+INIT_FUNCTION
 BOOLEAN
 NTAPI
-INIT_FUNCTION
 ExpInitSystemPhase0(VOID)
 {
     /* Initialize EXRESOURCE Support */
@@ -646,9 +652,9 @@ ExpInitSystemPhase0(VOID)
     return TRUE;
 }
 
+INIT_FUNCTION
 BOOLEAN
 NTAPI
-INIT_FUNCTION
 ExpInitSystemPhase1(VOID)
 {
     /* Initialize worker threads */
@@ -668,52 +674,56 @@ ExpInitSystemPhase1(VOID)
         DPRINT1("Executive: Event Pair initialization failed\n");
         return FALSE;
     }
-    
+
     /* Initialize mutants */
     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 */
     if (ExpInitializeSemaphoreImplementation() == FALSE)
     {
         DPRINT1("Executive: Semaphore initialization failed\n");
         return FALSE;
     }
-    
+
     /* Initialize timers */
     if (ExpInitializeTimerImplementation() == FALSE)
     {
         DPRINT1("Executive: Timer initialization failed\n");
         return FALSE;
     }
-    
+
     /* Initialize profiling */
     if (ExpInitializeProfileImplementation() == FALSE)
     {
         DPRINT1("Executive: Profile initialization failed\n");
         return FALSE;
     }
-    
+
     /* Initialize UUIDs */
-    ExpInitUuids();
-    
+    if (ExpUuidInitialization() == FALSE)
+    {
+        DPRINT1("Executive: Uuid initialization failed\n");
+        return FALSE;
+    }
+
     /* Initialize keyed events */
     if (ExpInitializeKeyedEventImplementation() == FALSE)
     {
         DPRINT1("Executive: Keyed event initialization failed\n");
         return FALSE;
     }
-    
+
     /* Initialize Win32K */
     if (ExpWin32kInit() == FALSE)
     {
@@ -723,9 +733,9 @@ ExpInitSystemPhase1(VOID)
     return TRUE;
 }
 
+INIT_FUNCTION
 BOOLEAN
 NTAPI
-INIT_FUNCTION
 ExInitSystem(VOID)
 {
     /* Check the initialization phase */
@@ -749,9 +759,9 @@ ExInitSystem(VOID)
     }
 }
 
+INIT_FUNCTION
 BOOLEAN
 NTAPI
-INIT_FUNCTION
 ExpIsLoaderValid(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 {
     PLOADER_PARAMETER_EXTENSION Extension;
@@ -759,8 +769,8 @@ ExpIsLoaderValid(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
     /* Get the loader extension */
     Extension = LoaderBlock->Extension;
 
-    /* Validate the size (larger structures are OK, we'll just ignore them) */
-    if (Extension->Size < sizeof(LOADER_PARAMETER_EXTENSION)) return FALSE;
+    /* Validate the size (Windows 2003 loader doesn't provide more) */
+    if (Extension->Size < LOADER_PARAMETER_EXTENSION_MIN_SIZE) return FALSE;
 
     /* Don't validate upper versions */
     if (Extension->MajorVersion > VER_PRODUCTMAJORVERSION) return TRUE;
@@ -775,9 +785,9 @@ ExpIsLoaderValid(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
     return TRUE;
 }
 
+INIT_FUNCTION
 VOID
 NTAPI
-INIT_FUNCTION
 ExpLoadBootSymbols(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 {
     ULONG i = 0;
@@ -846,7 +856,7 @@ ExpLoadBootSymbols(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
                 /* Load the symbols */
                 DbgLoadImageSymbols(&SymbolString,
                                     LdrEntry->DllBase,
-                                    (ULONG_PTR)ZwCurrentProcess());
+                                    (ULONG_PTR)PsGetCurrentProcessId());
             }
         }
 
@@ -856,9 +866,9 @@ ExpLoadBootSymbols(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
     }
 }
 
+INIT_FUNCTION
 VOID
 NTAPI
-INIT_FUNCTION
 ExBurnMemory(IN PLOADER_PARAMETER_BLOCK LoaderBlock,
              IN ULONG_PTR PagesToDestroy,
              IN TYPE_OF_MEMORY MemoryType)
@@ -866,7 +876,7 @@ ExBurnMemory(IN PLOADER_PARAMETER_BLOCK LoaderBlock,
     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;
@@ -902,9 +912,9 @@ ExBurnMemory(IN PLOADER_PARAMETER_BLOCK LoaderBlock,
     }
 }
 
+INIT_FUNCTION
 VOID
 NTAPI
-INIT_FUNCTION
 ExpInitializeExecutive(IN ULONG Cpu,
                        IN PLOADER_PARAMETER_BLOCK LoaderBlock)
 {
@@ -916,10 +926,10 @@ ExpInitializeExecutive(IN ULONG Cpu,
     ULONG PerfMemUsed;
     PLDR_DATA_TABLE_ENTRY NtosEntry;
     PMESSAGE_RESOURCE_ENTRY MsgEntry;
-    ANSI_STRING CsdString;
+    ANSI_STRING CSDString;
     size_t Remaining = 0;
     PCHAR RcEnd = NULL;
-    CHAR VersionBuffer [65];
+    CHAR VersionBuffer[65];
 
     /* Validate Loader */
     if (!ExpIsLoaderValid(LoaderBlock))
@@ -957,7 +967,8 @@ ExpInitializeExecutive(IN ULONG Cpu,
     if (LoaderBlock->SetupLdrBlock)
     {
         /* Check if this is text-mode setup */
-        if (LoaderBlock->SetupLdrBlock->Flags & SETUPLDR_TEXT_MODE) ExpInTextModeSetup = TRUE;
+        if (LoaderBlock->SetupLdrBlock->Flags & SETUPLDR_TEXT_MODE)
+            ExpInTextModeSetup = TRUE;
 
         /* Check if this is network boot */
         if (LoaderBlock->SetupLdrBlock->Flags & SETUPLDR_REMOTE_BOOT)
@@ -1072,17 +1083,11 @@ ExpInitializeExecutive(IN ULONG Cpu,
     /* Setup initial system settings */
     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 */
+    /* Set the Service Pack Number and add it to the CSD Version number if needed */
     CmNtSpBuildNumber = VER_PRODUCTBUILD_QFE;
-    if (!(CmNtCSDVersion & 0xFFFF0000))
+    if (((CmNtCSDVersion & 0xFFFF0000) == 0) && (CmNtCSDReleaseType == 1))
     {
-        /* Check the release type */
-        if (CmNtCSDReleaseType == 1) CmNtSpBuildNumber |= VER_PRODUCTBUILD_QFE << 16;
+        CmNtCSDVersion |= (VER_PRODUCTBUILD_QFE << 16);
     }
 
     /* Add loaded CmNtGlobalFlag value */
@@ -1136,22 +1141,22 @@ ExpInitializeExecutive(IN ULONG Cpu,
         if (NT_SUCCESS(Status))
         {
             /* Setup the string */
-            RtlInitAnsiString(&CsdString, (PCHAR)MsgEntry->Text);
+            RtlInitAnsiString(&CSDString, (PCHAR)MsgEntry->Text);
 
             /* Remove trailing newline */
-            while ((CsdString.Length > 0) &&
-                   ((CsdString.Buffer[CsdString.Length - 1] == '\r') ||
-                    (CsdString.Buffer[CsdString.Length - 1] == '\n')))
+            while ((CSDString.Length > 0) &&
+                   ((CSDString.Buffer[CSDString.Length - 1] == '\r') ||
+                    (CSDString.Buffer[CSDString.Length - 1] == '\n')))
             {
                 /* Skip the trailing character */
-                CsdString.Length--;
+                CSDString.Length--;
             }
 
             /* Fill the buffer with version information */
             Status = RtlStringCbPrintfA(Buffer,
                                         sizeof(Buffer),
                                         "%Z %u%c",
-                                        &CsdString,
+                                        &CSDString,
                                         (CmNtCSDVersion & 0xFF00) >> 8,
                                         (CmNtCSDVersion & 0xFF) ?
                                         'A' + (CmNtCSDVersion & 0xFF) - 1 :
@@ -1193,7 +1198,7 @@ ExpInitializeExecutive(IN ULONG Cpu,
     }
 
     /* Check if we have an RC number */
-    if (CmNtCSDVersion & 0xFFFF0000)
+    if ((CmNtCSDVersion & 0xFFFF0000) && (CmNtCSDReleaseType == 1))
     {
         /* Check if we have no version data yet */
         if (!(*Buffer))
@@ -1221,7 +1226,7 @@ ExpInitializeExecutive(IN ULONG Cpu,
         /* Add the version format string */
         Status = RtlStringCbPrintfA(RcEnd,
                                     Remaining,
-                                    "r%u",
+                                    "v.%u",
                                     (CmNtCSDVersion & 0xFFFF0000) >> 16);
         if (!NT_SUCCESS(Status))
         {
@@ -1231,9 +1236,9 @@ ExpInitializeExecutive(IN ULONG Cpu,
     }
 
     /* Now setup the final string */
-    RtlInitAnsiString(&CsdString, Buffer);
+    RtlInitAnsiString(&CSDString, Buffer);
     Status = RtlAnsiStringToUnicodeString(&CmCSDVersionString,
-                                          &CsdString,
+                                          &CSDString,
                                           TRUE);
     if (!NT_SUCCESS(Status))
     {
@@ -1326,9 +1331,9 @@ VOID
 NTAPI
 MmFreeLoaderBlock(IN PLOADER_PARAMETER_BLOCK LoaderBlock);
 
+INIT_FUNCTION
 VOID
 NTAPI
-INIT_FUNCTION
 Phase1InitializationDiscard(IN PVOID Context)
 {
     PLOADER_PARAMETER_BLOCK LoaderBlock = Context;
@@ -1372,17 +1377,17 @@ Phase1InitializationDiscard(IN PVOID Context)
     if (!HalInitSystem(1, LoaderBlock)) KeBugCheck(HAL1_INITIALIZATION_FAILED);
 
     /* Get the command line and upcase it */
-    CommandLine = _strupr(LoaderBlock->LoadOptions);
+    CommandLine = (LoaderBlock->LoadOptions ? _strupr(LoaderBlock->LoadOptions) : NULL);
 
     /* Check if GUI Boot is enabled */
-    NoGuiBoot = (strstr(CommandLine, "NOGUIBOOT")) ? TRUE: FALSE;
+    NoGuiBoot = (CommandLine && strstr(CommandLine, "NOGUIBOOT") != NULL);
 
     /* Get the SOS setting */
-    SosEnabled = strstr(CommandLine, "SOS") ? TRUE: FALSE;
+    SosEnabled = (CommandLine && strstr(CommandLine, "SOS") != NULL);
 
     /* Setup the boot driver */
     InbvEnableBootDriver(!NoGuiBoot);
-    InbvDriverInitialize(LoaderBlock, 18);
+    InbvDriverInitialize(LoaderBlock, IDB_MAX_RESOURCE);
 
     /* Check if GUI boot is enabled */
     if (!NoGuiBoot)
@@ -1401,11 +1406,11 @@ Phase1InitializationDiscard(IN PVOID Context)
     }
 
     /* Check if this is LiveCD (WinPE) mode */
-    if (strstr(CommandLine, "MININT"))
+    if (CommandLine && strstr(CommandLine, "MININT") != NULL)
     {
         /* Setup WinPE Settings */
         InitIsWinPEMode = TRUE;
-        InitWinPEModeType |= (strstr(CommandLine, "INRAM")) ? 0x80000000 : 1;
+        InitWinPEModeType |= (strstr(CommandLine, "INRAM") != NULL) ? 0x80000000 : 0x00000001;
     }
 
     /* Get the kernel's load entry */
@@ -1468,9 +1473,12 @@ Phase1InitializationDiscard(IN PVOID Context)
     if (NT_SUCCESS(MsgStatus))
     {
         /* Create the banner message */
+        /* ReactOS specific: Report ReactOS version, NtBuildLab information and reported NT kernel version */
         Status = RtlStringCbPrintfA(EndBuffer,
                                     Remaining,
                                     (PCHAR)MsgEntry->Text,
+                                    KERNEL_VERSION_STR,
+                                    NtBuildLab,
                                     StringBuffer,
                                     NtBuildNumber & 0xFFFF,
                                     BeginBuffer);
@@ -1483,7 +1491,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 */
@@ -1498,7 +1506,7 @@ Phase1InitializationDiscard(IN PVOID Context)
     if (!PoInitSystem(0)) KeBugCheck(INTERNAL_POWER_ERROR);
 
     /* Check for Y2K hack */
-    Y2KHackRequired = strstr(CommandLine, "YEAR");
+    Y2KHackRequired = CommandLine ? strstr(CommandLine, "YEAR") : NULL;
     if (Y2KHackRequired) Y2KHackRequired = strstr(Y2KHackRequired, "=");
     if (Y2KHackRequired) YearHack = atol(Y2KHackRequired + 1);
 
@@ -1577,8 +1585,9 @@ Phase1InitializationDiscard(IN PVOID Context)
                                WINDOWS_NT_INFO_STRING,
                                &MsgEntry);
 
-    /* Get total RAM size */
-    Size = MmNumberOfPhysicalPages * PAGE_SIZE / 1024 / 1024;
+    /* Get total RAM size, in MiB */
+    /* Round size up. Assumed to better match actual physical RAM size */
+    Size = ALIGN_UP_BY(MmNumberOfPhysicalPages * PAGE_SIZE, 1024 * 1024) / (1024 * 1024);
 
     /* Create the string */
     StringBuffer = InitBuffer->VersionBuffer;
@@ -1586,7 +1595,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 [%Iu MB Memory] %Z\r\n",
                                 KeNumberProcessors,
                                 Size,
                                 &TempString);
@@ -1949,14 +1958,20 @@ Phase1InitializationDiscard(IN PVOID Context)
     MmFreeLoaderBlock(LoaderBlock);
     LoaderBlock = Context = NULL;
 
+    /* Initialize the SRM in phase 1 */
+    if (!SeRmInitPhase1()) KeBugCheck(PROCESS1_INITIALIZATION_FAILED);
+
     /* Update progress bar */
     InbvUpdateProgressBar(100);
 
+    /* Clear the screen */
+    if (InbvBootDriverInstalled) FinalizeBootLogo();
+
     /* Allow strings to be displayed */
     InbvEnableDisplayString(TRUE);
 
     /* Launch initial process */
-    DPRINT1("Free non-cache pages: %lx\n", MmAvailablePages + MiMemoryConsumers[MC_CACHE].PagesUsed);
+    DPRINT("Free non-cache pages: %lx\n", MmAvailablePages + MiMemoryConsumers[MC_CACHE].PagesUsed);
     ProcessInfo = &InitBuffer->ProcessInfo;
     ExpLoadInitialProcess(InitBuffer, &ProcessParameters, &Environment);
 
@@ -1990,15 +2005,12 @@ Phase1InitializationDiscard(IN PVOID Context)
                         &Size,
                         MEM_RELEASE);
 
-    /* Clean the screen */
-    if (InbvBootDriverInstalled) FinalizeBootLogo();
-
     /* Increase init phase */
     ExpInitializationPhase++;
 
     /* Free the boot buffer */
     ExFreePoolWithTag(InitBuffer, TAG_INIT);
-    DPRINT1("Free non-cache pages: %lx\n", MmAvailablePages + MiMemoryConsumers[MC_CACHE].PagesUsed);
+    DPRINT("Free non-cache pages: %lx\n", MmAvailablePages + MiMemoryConsumers[MC_CACHE].PagesUsed);
 }
 
 VOID