Detect hardware before ntoskrnl.exe will be loaded.
[reactos.git] / freeldr / freeldr / reactos / reactos.c
index 3f58a83..498a79e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  FreeLoader
  *
- *  Copyright (C) 1998-2002  Brian Palmer  <brianp@sginet.com>
+ *  Copyright (C) 1998-2003  Brian Palmer  <brianp@sginet.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -23,6 +23,7 @@
 #include <arch.h>
 #include <reactos.h>
 #include <rtl.h>
+#include <disk.h>
 #include <fs.h>
 #include <ui.h>
 #include <multiboot.h>
 #include <inifile.h>
 
 #include "registry.h"
-#include "hwdetect.h"
 
 
 #define NDEBUG
 
+#define IsRecognizedPartition(P)  \
+    ((P) == PARTITION_FAT_12       || \
+     (P) == PARTITION_FAT_16       || \
+     (P) == PARTITION_HUGE         || \
+     (P) == PARTITION_IFS          || \
+     (P) == PARTITION_FAT32        || \
+     (P) == PARTITION_FAT32_XINT13 || \
+     (P) == PARTITION_XINT13)
 
 static BOOL
 LoadKernel(PCHAR szFileName, int nPos)
@@ -49,7 +57,7 @@ LoadKernel(PCHAR szFileName, int nPos)
   else
     szShortName = szShortName + 1;
 
-  FilePointer = OpenFile(szFileName);
+  FilePointer = FsOpenFile(szFileName);
   if (FilePointer == NULL)
     {
       strcpy(szBuffer, szShortName);
@@ -70,11 +78,74 @@ LoadKernel(PCHAR szFileName, int nPos)
    */
   MultiBootLoadKernel(FilePointer);
 
-  UiDrawProgressBarCenter(nPos, 100);
+  UiDrawProgressBarCenter(nPos, 100, "Loading ReactOS...");
 
   return(TRUE);
 }
 
+static BOOL
+LoadSymbolFile(PCHAR szSystemRoot,
+  PCHAR ModuleName,
+  int nPos)
+{
+  CHAR SymbolFileName[1024];
+  PFILE FilePointer;
+  U32 Length;
+  PCHAR Start;
+  PCHAR Ext;
+  char value[256];
+  char *p;
+
+  /* Get the path to the symbol store */
+  strcpy(SymbolFileName, szSystemRoot);
+  strcat(SymbolFileName, "symbols\\");
+
+  /* Get the symbol filename from the module name */
+  Start = strrchr(ModuleName, '\\');
+  if (Start == NULL)
+    Start = ModuleName;
+  else
+    Start++;
+
+  Ext = strrchr(ModuleName, '.');
+  if (Ext != NULL)
+    Length = Ext - Start;
+  else
+    Length = strlen(Start);
+
+  strncat(SymbolFileName, Start, Length);
+  strcat(SymbolFileName, ".sym");
+
+  FilePointer = FsOpenFile((PCHAR)&SymbolFileName[0]);
+  if (FilePointer == NULL)
+    {
+      DbgPrint((DPRINT_REACTOS, "Symbol file %s not loaded.\n", SymbolFileName));
+      /* This is not critical */
+      return FALSE;
+    }
+
+  DbgPrint((DPRINT_REACTOS, "Symbol file %s is loaded.\n", SymbolFileName));
+
+  /*
+   * Update the status bar with the current file
+   */
+  strcpy(value, "Reading ");
+  p = strrchr(SymbolFileName, '\\');
+  if (p == NULL)
+    strcat(value, SymbolFileName);
+  else
+    strcat(value, p + 1);
+  UiDrawStatusText(value);
+
+  /*
+   * Load the symbol file
+   */
+  MultiBootLoadModule(FilePointer, SymbolFileName, NULL);
+
+  UiDrawProgressBarCenter(nPos, 100, "Loading ReactOS...");
+
+  return (TRUE);
+}
 
 static BOOL
 LoadDriver(PCHAR szFileName, int nPos)
@@ -83,7 +154,7 @@ LoadDriver(PCHAR szFileName, int nPos)
   char value[256];
   char *p;
 
-  FilePointer = OpenFile(szFileName);
+  FilePointer = FsOpenFile(szFileName);
   if (FilePointer == NULL)
     {
       strcpy(value, szFileName);
@@ -108,7 +179,7 @@ LoadDriver(PCHAR szFileName, int nPos)
    */
   MultiBootLoadModule(FilePointer, szFileName, NULL);
 
-  UiDrawProgressBarCenter(nPos, 100);
+  UiDrawProgressBarCenter(nPos, 100, "Loading ReactOS...");
 
   return(TRUE);
 }
@@ -122,7 +193,7 @@ LoadNlsFile(PCHAR szFileName, PCHAR szModuleName)
   char value[256];
   char *p;
 
-  FilePointer = OpenFile(szFileName);
+  FilePointer = FsOpenFile(szFileName);
   if (FilePointer == NULL)
     {
       strcpy(value, szFileName);
@@ -155,19 +226,19 @@ LoadNlsFile(PCHAR szFileName, PCHAR szModuleName)
 static VOID
 LoadBootDrivers(PCHAR szSystemRoot, int nPos)
 {
-  LONG rc = 0;
+  S32 rc = 0;
   HKEY hGroupKey, hServiceKey, hDriverKey;
   char ValueBuffer[512];
   char ServiceName[256];
-  ULONG BufferSize;
-  ULONG Index;
+  U32 BufferSize;
+  U32 Index;
   char *GroupName;
 
-  ULONG ValueSize;
-  ULONG ValueType;
-  ULONG StartValue;
+  U32 ValueSize;
+  U32 ValueType;
+  U32 StartValue;
   UCHAR DriverGroup[256];
-  ULONG DriverGroupSize;
+  U32 DriverGroupSize;
 
   UCHAR ImagePath[256];
 
@@ -175,19 +246,21 @@ LoadBootDrivers(PCHAR szSystemRoot, int nPos)
   rc = RegOpenKey(NULL,
                  "\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\ServiceGroupOrder",
                  &hGroupKey);
-  DbgPrint((DPRINT_REACTOS, "RegOpenKey(): rc %d\n", (int)rc));
   if (rc != ERROR_SUCCESS)
-    return;
+    {
+      DbgPrint((DPRINT_REACTOS, "Failed to open the 'ServiceGroupOrder key (rc %d)\n", (int)rc));
+      return;
+    }
 
   /* enumerate drivers */
   rc = RegOpenKey(NULL,
                  "\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services",
                  &hServiceKey);
-  DbgPrint((DPRINT_REACTOS, "RegOpenKey(): rc %d\n", (int)rc));
   if (rc != ERROR_SUCCESS)
-    return;
-
-  DbgPrint((DPRINT_REACTOS, "hServiceKey: %x\n", (int)hServiceKey));
+    {
+      DbgPrint((DPRINT_REACTOS, "Failed to open the 'Services' key (rc %d)\n", (int)rc));
+      return;
+    }
 
   BufferSize = sizeof(ValueBuffer);
   rc = RegQueryValue(hGroupKey, "List", NULL, (PUCHAR)ValueBuffer, &BufferSize);
@@ -220,7 +293,7 @@ LoadBootDrivers(PCHAR szSystemRoot, int nPos)
          /* open driver Key */
          rc = RegOpenKey(hServiceKey, ServiceName, &hDriverKey);
 
-         ValueSize = sizeof(ULONG);
+         ValueSize = sizeof(U32);
          rc = RegQueryValue(hDriverKey, "Start", &ValueType, (PUCHAR)&StartValue, &ValueSize);
          DbgPrint((DPRINT_REACTOS, "  Start: %x  \n", (int)StartValue));
 
@@ -248,18 +321,19 @@ LoadBootDrivers(PCHAR szSystemRoot, int nPos)
                {
                  DbgPrint((DPRINT_REACTOS, "  ImagePath: '%s'\n", ImagePath));
                }
-      DbgPrint((DPRINT_REACTOS, "  Loading driver: '%s'\n", ImagePath));
+             DbgPrint((DPRINT_REACTOS, "  Loading driver: '%s'\n", ImagePath));
 
              if (nPos < 100)
                nPos += 5;
 
              LoadDriver(ImagePath, nPos);
+        LoadSymbolFile(szSystemRoot, ImagePath, nPos);
+           }
+         else
+           {
+             DbgPrint((DPRINT_REACTOS, "  Skipping driver '%s' with Start %d and Group '%s' (Current group '%s')\n",
+                 ImagePath, StartValue, DriverGroup, GroupName));
            }
-               else
-                       {
-        DbgPrint((DPRINT_REACTOS, "  Skipping driver '%s' with Start %d and Group '%s' (Current group '%s')\n",
-          ImagePath, StartValue, DriverGroup, GroupName));
-                       }
          Index++;
        }
 
@@ -271,12 +345,12 @@ LoadBootDrivers(PCHAR szSystemRoot, int nPos)
 static BOOL
 LoadNlsFiles(PCHAR szSystemRoot)
 {
-  LONG rc = ERROR_SUCCESS;
+  S32 rc = ERROR_SUCCESS;
   HKEY hKey;
   char szIdBuffer[80];
   char szNameBuffer[80];
   char szFileName[256];
-  ULONG BufferSize;
+  U32 BufferSize;
 
   /* open the codepage key */
   rc = RegOpenKey(NULL,
@@ -362,16 +436,22 @@ LoadAndBootReactOS(PUCHAR OperatingSystemName)
        PFILE FilePointer;
        char  name[1024];
        char  value[1024];
+       char  szKernelName[1024];
+       char  szHalName[1024];
        char  szFileName[1024];
        char  szBootPath[256];
-//     int             i;
+       int             i;
 //     int             nNumDriverFiles=0;
 //     int             nNumFilesLoaded=0;
        char  MsgBuffer[256];
-       ULONG SectionId;
+       U32 SectionId;
 
        char* Base;
-       ULONG Size;
+       U32 Size;
+
+        PARTITION_TABLE_ENTRY PartitionTableEntry;
+        U32 rosPartition;
+       BOOL TextHive = FALSE;
 
        //
        // Open the operating system section
@@ -394,22 +474,29 @@ LoadAndBootReactOS(PUCHAR OperatingSystemName)
        mb_info.cmdline = (unsigned long)multiboot_kernel_cmdline;
        mb_info.mods_count = 0;
        mb_info.mods_addr = (unsigned long)multiboot_modules;
-       mb_info.mmap_length = (unsigned long)GetBiosMemoryMap((PBIOS_MEMORY_MAP_ARRAY)&multiboot_memory_map);
+       mb_info.mmap_length = (unsigned long)GetBiosMemoryMap((PBIOS_MEMORY_MAP)&multiboot_memory_map, 32) * sizeof(memory_map_t);
        if (mb_info.mmap_length)
        {
                mb_info.mmap_addr = (unsigned long)&multiboot_memory_map;
                mb_info.flags |= MB_INFO_FLAG_MEMORY_MAP;
-               //printf("memory map length: %d\n", mb_info.mmap_length);
-               //printf("dumping memory map:\n");
-               //for (i=0; i<(mb_info.mmap_length / 4); i++)
-               //{
-               //      printf("0x%x\n", ((unsigned long *)&multiboot_memory_map)[i]);
-               //}
-               //getch();
+               multiboot_memory_map_descriptor_size = sizeof(memory_map_t); // GetBiosMemoryMap uses a fixed value of 24
+               DbgPrint((DPRINT_REACTOS, "memory map length: %d\n", mb_info.mmap_length));
+               DbgPrint((DPRINT_REACTOS, "dumping memory map:\n"));
+               for (i=0; i<(mb_info.mmap_length/sizeof(memory_map_t)); i++)
+               {
+                       DbgPrint((DPRINT_REACTOS, "start: %x\t size: %x\t type %d\n", 
+                                 multiboot_memory_map[i].base_addr_low, 
+                                 multiboot_memory_map[i].length_low,
+                                 multiboot_memory_map[i].type));
+               }
        }
-       //printf("low_mem = %d\n", mb_info.mem_lower);
-       //printf("high_mem = %d\n", mb_info.mem_upper);
-       //getch();
+       DbgPrint((DPRINT_REACTOS, "low_mem = %d\n", mb_info.mem_lower));
+       DbgPrint((DPRINT_REACTOS, "high_mem = %d\n", mb_info.mem_upper));
+
+       /*
+        * Initialize the registry
+        */
+       RegInitializeRegistry();
 
        /*
         * Make sure the system path is set in the .ini file
@@ -434,6 +521,32 @@ LoadAndBootReactOS(PUCHAR OperatingSystemName)
        ((char *)(&mb_info.boot_device))[0] = (char)BootDrive;
        ((char *)(&mb_info.boot_device))[1] = (char)BootPartition;
 
+       /* recalculate the boot partition for freeldr */
+       i = 0;
+       rosPartition = 0;
+       while (1)
+       {
+          if (!DiskGetPartitionEntry(BootDrive, ++i, &PartitionTableEntry))
+          {
+             BootPartition = 0;
+             break;
+          }
+          if (IsRecognizedPartition(PartitionTableEntry.SystemIndicator))
+          {
+             if (++rosPartition == BootPartition)
+             {
+                BootPartition = i;
+                break;
+             }
+          }
+       }
+       if (BootPartition == 0)
+       {
+               sprintf(MsgBuffer,"Invalid system path: '%s'", value);
+               UiMessageBox(MsgBuffer);
+               return;
+       }
+
        /* copy ARC path into kernel command line */
        strcpy(multiboot_kernel_cmdline, value);
 
@@ -451,17 +564,25 @@ LoadAndBootReactOS(PUCHAR OperatingSystemName)
            szBootPath[strlen(szBootPath)] != '\\')
                strcat(szBootPath, "\\");
 
-       DebugPrint(DPRINT_REACTOS,"SystemRoot: '%s'", szBootPath);
+       DbgPrint((DPRINT_REACTOS,"SystemRoot: '%s'\n", szBootPath));
+
 
        UiDrawBackdrop();
+       UiDrawStatusText("Detecting Hardware...");
+
+       /*
+        * Detect hardware
+        */
+       DetectHardware();
+
 
        UiDrawStatusText("Loading...");
-       UiDrawProgressBarCenter(0, 100);
+       UiDrawProgressBarCenter(0, 100, "Loading ReactOS...");
 
        /*
         * Try to open boot drive
         */
-       if (!OpenDiskDrive(BootDrive, BootPartition))
+       if (!FsOpenVolume(BootDrive, BootPartition))
        {
                UiMessageBox("Failed to open boot drive.");
                return;
@@ -478,24 +599,24 @@ LoadAndBootReactOS(PUCHAR OperatingSystemName)
                 */
                if (value[0] == '\\')
                {
-                       strcpy(szFileName, value);
+                       strcpy(szKernelName, value);
                }
                else
                {
-                       strcpy(szFileName, szBootPath);
-                       strcat(szFileName, "SYSTEM32\\");
-                       strcat(szFileName, value);
+                       strcpy(szKernelName, szBootPath);
+                       strcat(szKernelName, "SYSTEM32\\");
+                       strcat(szKernelName, value);
                }
        }
        else
        {
                strcpy(value, "NTOSKRNL.EXE");
-               strcpy(szFileName, szBootPath);
-               strcat(szFileName, "SYSTEM32\\");
-               strcat(szFileName, value);
+               strcpy(szKernelName, szBootPath);
+               strcat(szKernelName, "SYSTEM32\\");
+               strcat(szKernelName, value);
        }
 
-       if (!LoadKernel(szFileName, 5))
+       if (!LoadKernel(szKernelName, 5))
                return;
 
        /*
@@ -509,63 +630,53 @@ LoadAndBootReactOS(PUCHAR OperatingSystemName)
                 */
                if (value[0] == '\\')
                {
-                       strcpy(szFileName, value);
+                       strcpy(szHalName, value);
                }
                else
                {
-                       strcpy(szFileName, szBootPath);
-                       strcat(szFileName, "SYSTEM32\\");
-                       strcat(szFileName, value);
+                       strcpy(szHalName, szBootPath);
+                       strcat(szHalName, "SYSTEM32\\");
+                       strcat(szHalName, value);
                }
        }
        else
        {
                strcpy(value, "HAL.DLL");
-               strcpy(szFileName, szBootPath);
-               strcat(szFileName, "SYSTEM32\\");
-               strcat(szFileName, value);
+               strcpy(szHalName, szBootPath);
+               strcat(szHalName, "SYSTEM32\\");
+               strcat(szHalName, value);
        }
 
-       if (!LoadDriver(szFileName, 10))
+       if (!LoadDriver(szHalName, 10))
                return;
 
        /*
-        * Find the System hive image name
-        * and try to load it off the disk
+        * Load the System hive from disk
         */
-       if(IniReadSettingByName(SectionId, "SystemHive", value, 1024))
+       strcpy(szFileName, szBootPath);
+       strcat(szFileName, "SYSTEM32\\CONFIG\\SYSTEM");
+
+       DbgPrint((DPRINT_REACTOS, "SystemHive: '%s'", szFileName));
+
+       FilePointer = FsOpenFile(szFileName);
+       if (FilePointer == NULL)
        {
-               /*
-                * Set the name and
-                */
-               if (value[0] == '\\')
+               strcpy(szFileName, szBootPath);
+               strcat(szFileName, "SYSTEM32\\CONFIG\\SYSTEM.HIV");
+
+               DbgPrint((DPRINT_REACTOS, "SystemHive: '%s'", szFileName));
+
+               FilePointer = FsOpenFile(szFileName);
+               if (FilePointer == NULL)
                {
-                       strcpy(szFileName, value);
+                       UiMessageBox("Could not find the System hive!");
+                       return;
                }
                else
                {
-                       strcpy(szFileName, szBootPath);
-                       strcat(szFileName, "SYSTEM32\\CONFIG\\");
-                       strcat(szFileName, value);
+                       TextHive = TRUE;
                }
        }
-       else
-       {
-               strcpy(value, "SYSTEM.HIV");
-               strcpy(szFileName, szBootPath);
-               strcat(szFileName, "SYSTEM32\\CONFIG\\");
-               strcat(szFileName, value);
-       }
-
-       DebugPrint(DPRINT_REACTOS, "SystemHive: '%s'", szFileName);
-
-       FilePointer = OpenFile(szFileName);
-       if (FilePointer == NULL)
-       {
-               strcat(value, " not found.");
-               UiMessageBox(value);
-               return;
-       }
 
        /*
         * Update the status bar with the current file
@@ -577,26 +688,43 @@ LoadAndBootReactOS(PUCHAR OperatingSystemName)
        UiDrawStatusText(name);
 
        /*
-        * Load the system hive
+        * Load the System hive
         */
        Base = MultiBootLoadModule(FilePointer, szFileName, &Size);
-       RegInitializeRegistry();
-       RegImportHive(Base, Size);
+       if (Base == NULL || Size == 0)
+       {
+               UiMessageBox("Could not load the System hive!\n");
+               return;
+       }
+       DbgPrint((DPRINT_REACTOS, "SystemHive loaded at 0x%x size %u", (unsigned)Base, (unsigned)Size));
+
+       /*
+        * Import the loaded system hive
+        */
+       if (TextHive)
+       {
+               RegImportTextHive(Base, Size);
+       }
+       else
+       {
+               RegImportBinaryHive(Base, Size);
+       }
 
-       UiDrawProgressBarCenter(15, 100);
+       /*
+        * Initialize the 'CurrentControlSet' link
+        */
+       RegInitCurrentControlSet(FALSE);
 
-       DebugPrint(DPRINT_REACTOS, "SystemHive loaded at 0x%x size %u", (unsigned)Base, (unsigned)Size);
+       UiDrawProgressBarCenter(15, 100, "Loading ReactOS...");
 
        /*
-        * Retrieve hardware information and create the hardware hive
+        * Export the hardware hive
         */
-       DetectHardware();
-#if 0
-       Base = MultiBootCreateModule(HARDWARE.HIV);
-       RegExportHive("\\Registry\\Machine\\HARDWARE", Base, &Size);
-       MultiBootCloseModule(Base, Size);
-#endif
-       UiDrawProgressBarCenter(20, 100);
+       Base = MultiBootCreateModule ("HARDWARE");
+       RegExportBinaryHive ("\\Registry\\Machine\\HARDWARE", Base, &Size);
+       MultiBootCloseModule (Base, Size);
+
+       UiDrawProgressBarCenter(20, 100, "Loading ReactOS...");
 
        /*
         * Load NLS files
@@ -604,12 +732,15 @@ LoadAndBootReactOS(PUCHAR OperatingSystemName)
 #if 0
        if (!LoadNlsFiles(szBootPath))
        {
-               MessageBox("Failed to load NLS files\n");
+               UiMessageBox("Failed to load NLS files\n");
                return;
        }
 #endif
 
-       UiDrawProgressBarCenter(25, 100);
+       LoadSymbolFile(szBootPath, szKernelName, 25);
+       LoadSymbolFile(szBootPath, szHalName, 25);
+
+       UiDrawProgressBarCenter(25, 100, "Loading ReactOS...");
 
        /*
         * Load boot drivers
@@ -623,18 +754,23 @@ LoadAndBootReactOS(PUCHAR OperatingSystemName)
        UiDrawBackdrop();
        UiDrawStatusText("Press any key to boot");
 
+#if 0
        /*
         * Wait for user
         */
        strcpy(name, "Kernel and Drivers loaded.\nPress any key to boot ");
        strcat(name, OperatingSystemName);
        strcat(name, ".");
-       //MessageBox(name);
+       MessageBox(name);
+#endif
+
+       UiUnInitialize("Booting ReactOS...");
 
        /*
         * Now boot the kernel
         */
-       StopFloppyMotor();
+       DiskStopFloppyMotor();
        boot_reactos();
 }
 
+/* EOF */