- Added support for NTLDR style freeloader GUI. To enable, edit freeldr.ini and add:
[reactos.git] / reactos / ntoskrnl / ex / init.c
index 11124e1..2847aaf 100644 (file)
@@ -10,7 +10,6 @@
  */
 
 #include <ntoskrnl.h>
-#include <ntos/bootvid.h>
 #define NDEBUG
 #include <internal/debug.h>
 
@@ -29,9 +28,23 @@ extern PRTL_MESSAGE_RESOURCE_DATA KiBugCodeMessages;
 extern LIST_ENTRY KiProfileListHead;
 extern LIST_ENTRY KiProfileSourceListHead;
 extern KSPIN_LOCK KiProfileLock;
+BOOLEAN SetupMode = TRUE;
+BOOLEAN NoGuiBoot = FALSE;
 
 VOID PspPostInitSystemProcess(VOID);
 
+static VOID INIT_FUNCTION InitSystemSharedUserPage (PCSZ ParameterLine);
+VOID INIT_FUNCTION ExpDisplayNotice(VOID);
+INIT_FUNCTION NTSTATUS ExpLoadInitialProcess(PHANDLE ProcessHandle, PHANDLE ThreadHandle);
+
+#if defined (ALLOC_PRAGMA)
+#pragma alloc_text(INIT, InitSystemSharedUserPage)
+#pragma alloc_text(INIT, ExpDisplayNotice)
+#pragma alloc_text(INIT, ExpLoadInitialProcess)
+#pragma alloc_text(INIT, ExpInitializeExecutive)
+#pragma alloc_text(INIT, ExInit2)
+#endif
+
 /* FUNCTIONS ****************************************************************/
 
 static
@@ -77,7 +90,7 @@ InitSystemSharedUserPage (PCSZ ParameterLine)
 
     /* Create local parameter line copy */
     ParamBuffer = ExAllocatePool(PagedPool, 256);
-    strcpy (ParamBuffer, (char *)ParameterLine);
+    strcpy (ParamBuffer, (const char *)ParameterLine);
     DPRINT("%s\n", ParamBuffer);
 
     /* Cut options off */
@@ -220,7 +233,7 @@ InitSystemSharedUserPage (PCSZ ParameterLine)
     }
 }
 
-inline
+__inline
 VOID
 STDCALL
 ExecuteRuntimeAsserts(VOID)
@@ -229,6 +242,7 @@ ExecuteRuntimeAsserts(VOID)
      * Fail at runtime if someone has changed various structures without
      * updating the offsets used for the assembler code.
      */
+    ASSERT(FIELD_OFFSET(KUSER_SHARED_DATA, SystemCall) == 0x300);
     ASSERT(FIELD_OFFSET(KTHREAD, InitialStack) == KTHREAD_INITIAL_STACK);
     ASSERT(FIELD_OFFSET(KTHREAD, Teb) == KTHREAD_TEB);
     ASSERT(FIELD_OFFSET(KTHREAD, KernelStack) == KTHREAD_KERNEL_STACK);
@@ -241,24 +255,22 @@ ExecuteRuntimeAsserts(VOID)
     ASSERT(FIELD_OFFSET(KPROCESS, DirectoryTableBase) == KPROCESS_DIRECTORY_TABLE_BASE);
     ASSERT(FIELD_OFFSET(KPROCESS, IopmOffset) == KPROCESS_IOPM_OFFSET);
     ASSERT(FIELD_OFFSET(KPROCESS, LdtDescriptor) == KPROCESS_LDT_DESCRIPTOR0);
-    ASSERT(FIELD_OFFSET(KTRAP_FRAME, Reserved9) == KTRAP_FRAME_RESERVED9);
     ASSERT(FIELD_OFFSET(KV86M_TRAP_FRAME, SavedExceptionStack) == TF_SAVED_EXCEPTION_STACK);
     ASSERT(FIELD_OFFSET(KV86M_TRAP_FRAME, regs) == TF_REGS);
     ASSERT(FIELD_OFFSET(KV86M_TRAP_FRAME, orig_ebp) == TF_ORIG_EBP);
     ASSERT(FIELD_OFFSET(KPCR, Tib.ExceptionList) == KPCR_EXCEPTION_LIST);
     ASSERT(FIELD_OFFSET(KPCR, Self) == KPCR_SELF);
-    ASSERT(FIELD_OFFSET(KPCR, PrcbData) + FIELD_OFFSET(KPRCB, CurrentThread) == KPCR_CURRENT_THREAD);
-    ASSERT(FIELD_OFFSET(KPCR, PrcbData) + FIELD_OFFSET(KPRCB, NpxThread) == KPCR_NPX_THREAD);
+    ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, CurrentThread) == KPCR_CURRENT_THREAD);
+    ASSERT(FIELD_OFFSET(KIPCR, PrcbData) + FIELD_OFFSET(KPRCB, NpxThread) == KPCR_NPX_THREAD);
     ASSERT(FIELD_OFFSET(KTSS, Esp0) == KTSS_ESP0);
-    ASSERT(FIELD_OFFSET(KTSS, Eflags) == KTSS_EFLAGS);
     ASSERT(FIELD_OFFSET(KTSS, IoMapBase) == KTSS_IOMAPBASE);
     ASSERT(sizeof(FX_SAVE_AREA) == SIZEOF_FX_SAVE_AREA);
 }
 
-inline
+__inline
 VOID
 STDCALL
-ParseAndCacheLoadedModules(PBOOLEAN SetupBoot)
+ParseAndCacheLoadedModules(VOID)
 {
     ULONG i;
     PCHAR Name;
@@ -294,7 +306,7 @@ ParseAndCacheLoadedModules(PBOOLEAN SetupBoot)
         } else if (!_stricmp(Name, "system") || !_stricmp(Name, "system.hiv")) {
 
             CachedModules[SystemRegistry] = &KeLoaderModules[i];
-            *SetupBoot = FALSE;
+            SetupMode = FALSE;
 
         } else if (!_stricmp(Name, "hardware") || !_stricmp(Name, "hardware.hiv")) {
 
@@ -303,7 +315,7 @@ ParseAndCacheLoadedModules(PBOOLEAN SetupBoot)
     }
 }
 
-inline
+__inline
 VOID
 STDCALL
 ParseCommandLine(PULONG MaxMem,
@@ -369,20 +381,125 @@ ParseCommandLine(PULONG MaxMem,
         p1 = p2;
     }
 }
+   
+VOID
+INIT_FUNCTION
+ExpDisplayNotice(VOID)
+{
+    CHAR str[50];
+   
+    if (SetupMode)
+    {
+        HalDisplayString(
+        "\n\n\n     ReactOS " KERNEL_VERSION_STR " Setup \n");
+        HalDisplayString(
+        "    \xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD");
+        HalDisplayString(
+        "\xCD\xCD\n");
+        return;
+    }
+    
+    HalDisplayString("Starting ReactOS "KERNEL_VERSION_STR" (Build "
+                     KERNEL_VERSION_BUILD_STR")\n");
+    HalDisplayString(RES_STR_LEGAL_COPYRIGHT);
+    HalDisplayString("\n\nReactOS is free software, covered by the GNU General "
+                     "Public License, and you\n");
+    HalDisplayString("are welcome to change it and/or distribute copies of it "
+                     "under certain\n");
+    HalDisplayString("conditions. There is absolutely no warranty for "
+                      "ReactOS.\n\n");
+
+    /* Display number of Processors */
+    sprintf(str,
+            "Found %x system processor(s). [%lu MB Memory]\n",
+            (int)KeNumberProcessors,
+            (KeLoaderBlock.MemHigher + 1088)/ 1024);
+    HalDisplayString(str);
+    
+}
 
+INIT_FUNCTION
+NTSTATUS
+ExpLoadInitialProcess(PHANDLE ProcessHandle,
+                      PHANDLE ThreadHandle)
+{
+    UNICODE_STRING ImagePath = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32\\smss.exe");
+    HANDLE SystemProcessHandle;
+    NTSTATUS Status;
+    PRTL_USER_PROCESS_PARAMETERS Params=NULL;
+    RTL_USER_PROCESS_INFORMATION Info;
+
+    /* Create a handle to the process */
+    Status = ObpCreateHandle(PsInitialSystemProcess,
+                             PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION,
+                             OBJ_KERNEL_HANDLE,
+                             &SystemProcessHandle);
+    if(!NT_SUCCESS(Status))
+    {
+        DPRINT1("Failed to create a handle for the system process!\n");
+        return Status;
+    }
+
+    /* Create the Parameters */
+    Status = RtlCreateProcessParameters(&Params,
+                                        &ImagePath,
+                                        NULL,
+                                        NULL,
+                                        NULL,
+                                        NULL,
+                                        NULL,
+                                        NULL,
+                                        NULL,
+                                        NULL);
+    if(!NT_SUCCESS(Status))
+    {
+        DPRINT1("Failed to create ppb!\n");
+        ZwClose(SystemProcessHandle);
+        return Status;
+    }
+
+    DPRINT("Creating process\n");
+    Status = RtlCreateUserProcess(&ImagePath,
+                                  OBJ_CASE_INSENSITIVE,
+                                  Params,
+                                  NULL,
+                                  NULL,
+                                  SystemProcessHandle,
+                                  FALSE,
+                                  NULL,
+                                  NULL,
+                                  &Info);
+    
+    /* Close the handle and free the params */
+    ZwClose(SystemProcessHandle);
+    RtlDestroyProcessParameters(Params);
+
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("NtCreateProcess() failed (Status %lx)\n", Status);
+        return(Status);
+    }
+
+    /* Start it up */
+    ZwResumeThread(Info.ThreadHandle, NULL);
+
+    /* Return Handles */
+    *ProcessHandle = Info.ProcessHandle;
+    *ThreadHandle = Info.ThreadHandle;
+    DPRINT("Process created successfully\n");
+    return STATUS_SUCCESS;
+}
+   
 VOID
 INIT_FUNCTION
 STDCALL
 ExpInitializeExecutive(VOID)
 {
-    CHAR str[50];
     UNICODE_STRING EventName;
     HANDLE InitDoneEventHandle;
     OBJECT_ATTRIBUTES ObjectAttributes;
-    BOOLEAN NoGuiBoot = FALSE;
     BOOLEAN BootLog = FALSE;
     ULONG MaxMem = 0;
-    BOOLEAN SetupBoot = TRUE;
     BOOLEAN ForceAcpiDisable = FALSE;
     LARGE_INTEGER Timeout;
     HANDLE ProcessHandle;
@@ -413,10 +530,7 @@ ExpInitializeExecutive(VOID)
             MaxMem > 8 ? MaxMem : 4096);
 
     /* Parse the Loaded Modules (by FreeLoader) and cache the ones we'll need */
-    ParseAndCacheLoadedModules(&SetupBoot);
-
-    /* Initialize the kernel debugger parameters */
-    KdInitSystem(0, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
+    ParseAndCacheLoadedModules();
 
     /* Initialize the Dispatcher, Clock and Bug Check Mechanisms. */
     KeInit2();
@@ -436,7 +550,7 @@ ExpInitializeExecutive(VOID)
     ObInit();
 
     /* Initialize Lookaside Lists */
-    ExInit2();
+    ExpInitLookasideLists();
 
     /* Set up Region Maps, Sections and the Paging File */
     MmInit2();
@@ -446,10 +560,10 @@ ExpInitializeExecutive(VOID)
 
     /* Set 1 CPU for now, we'll increment this later */
     KeNumberProcessors = 1;
-
+    
     /* Initalize the Process Manager */
     PiInitProcessManager();
-
+    
     /* Break into the Debugger if requested */
     if (KdPollBreakIn()) DbgBreakPointWithStatus (DBG_STATUS_CONTROL_C);
 
@@ -463,7 +577,7 @@ ExpInitializeExecutive(VOID)
         KeCreateApplicationProcessorIdleThread(KeNumberProcessors);
 
         /* Allocate a stack for use when booting the processor */
-        ProcessorStack = Ki386InitialStackArray[((int)KeNumberProcessors)] + MM_STACK_SIZE;
+        ProcessorStack = RVA(Ki386InitialStackArray[((int)KeNumberProcessors)], MM_STACK_SIZE);
 
         /* Tell HAL a new CPU is being started */
         HalStartNextProcessor(0, (ULONG)ProcessorStack - 2*sizeof(FX_SAVE_AREA));
@@ -474,7 +588,7 @@ ExpInitializeExecutive(VOID)
     HalInitSystem(1, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
 
     /* Initialize Basic System Objects and Worker Threads */
-    ExInit3();
+    ExInit2();
 
     /* Create the system handle table, assign it to the system process, create
        the client id table and assign a PID for the system process. This needs
@@ -516,22 +630,7 @@ ExpInitializeExecutive(VOID)
     HalInitSystem(2, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
 
     /* Display version number and copyright/warranty message */
-    HalDisplayString("Starting ReactOS "KERNEL_VERSION_STR" (Build "
-                     KERNEL_VERSION_BUILD_STR")\n");
-    HalDisplayString(RES_STR_LEGAL_COPYRIGHT);
-    HalDisplayString("\n\nReactOS is free software, covered by the GNU General "
-                     "Public License, and you\n");
-    HalDisplayString("are welcome to change it and/or distribute copies of it "
-                     "under certain\n");
-    HalDisplayString("conditions. There is absolutely no warranty for "
-                      "ReactOS.\n\n");
-
-    /* Display number of Processors */
-    sprintf(str,
-            "Found %d system processor(s). [%lu MB Memory]\n",
-            KeNumberProcessors,
-            (KeLoaderBlock.MemHigher + 1088)/ 1024);
-    HalDisplayString(str);
+    if (NoGuiBoot) ExpDisplayNotice();
 
     /* Call KD Providers at Phase 2 */
     KdInitSystem(2, (PLOADER_PARAMETER_BLOCK)&KeLoaderBlock);
@@ -540,13 +639,14 @@ ExpInitializeExecutive(VOID)
     RtlpInitNls();
 
     /* Import and Load Registry Hives */
-    CmInitHives(SetupBoot);
+    CmInitHives(SetupMode);
 
     /* Initialize the time zone information from the registry */
     ExpInitTimeZoneInfo();
 
-   /* Enter the kernel debugger before starting up the boot drivers */
-    KdbEnter();
+    /* Enter the kernel debugger before starting up the boot drivers */
+    if (KdDebuggerEnabled && KdpEarlyBreak)
+        DbgBreakPoint();
 
     /* Setup Drivers and Root Device Node */
     IoInit2(BootLog);
@@ -558,7 +658,7 @@ ExpInitializeExecutive(VOID)
     IoInit3();
 
     /* Load the System DLL and its Entrypoints */
-    LdrpInitializeSystemDll();
+    PsLocateSystemDll();
 
     /* Initialize the Default Locale */
     PiInitDefaultLocale();
@@ -587,7 +687,7 @@ ExpInitializeExecutive(VOID)
     }
 
     /* Launch initial process */
-    Status = LdrLoadInitialProcess(&ProcessHandle,
+    Status = ExpLoadInitialProcess(&ProcessHandle,
                                    &ThreadHandle);
 
     /* Check for success, Bugcheck if we failed */
@@ -626,7 +726,10 @@ ExpInitializeExecutive(VOID)
             KEBUGCHECKEX(SESSION5_INITIALIZATION_FAILED, Status, 0, 0, 0);
         }
 
-        /* Disable the Boot Logo */
+        /*
+         * FIXME: FILIP!
+         * Disable the Boot Logo
+         */
         if (!NoGuiBoot) InbvEnableBootDriver(FALSE);
 
         /* Signal the Event and close the handle */
@@ -654,25 +757,21 @@ ExpInitializeExecutive(VOID)
     ZwClose(ProcessHandle);
 }
 
-VOID INIT_FUNCTION
+VOID
+STDCALL
+INIT_FUNCTION
 ExInit2(VOID)
 {
-  ExpInitLookasideLists();
-}
-
-VOID INIT_FUNCTION
-ExInit3 (VOID)
-{
-  ExpInitializeEventImplementation();
-  ExpInitializeEventPairImplementation();
-  ExpInitializeMutantImplementation();
-  ExpInitializeSemaphoreImplementation();
-  ExpInitializeTimerImplementation();
-  LpcpInitSystem();
-  ExpInitializeProfileImplementation();
-  ExpWin32kInit();
-  ExpInitUuids();
-  ExpInitializeHandleTables();
+    ExpInitializeEventImplementation();
+    ExpInitializeEventPairImplementation();
+    ExpInitializeMutantImplementation();
+    ExpInitializeSemaphoreImplementation();
+    ExpInitializeTimerImplementation();
+    LpcpInitSystem();
+    ExpInitializeProfileImplementation();
+    ExpWin32kInit();
+    ExpInitUuids();
+    ExpInitializeHandleTables();
 }
 
 /* EOF */