[FREELDR] Unload freeldr.ini file before booting.
[reactos.git] / boot / freeldr / freeldr / bootmgr.c
index 5856128..5df8182 100644 (file)
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+/* INCLUDES *******************************************************************/
+
 #include <freeldr.h>
+#include <debug.h>
 
-ARC_DISK_SIGNATURE reactos_arc_disk_info[32]; // ARC Disk Information
-ULONG reactos_disk_count = 0;
-CHAR reactos_arc_strings[32][256];
+/* GLOBALS ********************************************************************/
 
 typedef
 VOID
 (*OS_LOADING_METHOD)(IN OperatingSystemItem* OperatingSystem,
                      IN USHORT OperatingSystemVersion);
 
-struct
+static const struct
 {
-    CHAR BootType[80];
+    PCHAR BootType;
     USHORT OperatingSystemVersion;
     OS_LOADING_METHOD Load;
 } OSLoadingMethods[] =
 {
-#ifdef FREELDR_REACTOS_SETUP
     {"ReactOSSetup", 0                , LoadReactOSSetup     },
-#endif
 
 #ifdef _M_IX86
     {"BootSector"  , 0                , LoadAndBootBootSector},
@@ -50,10 +49,10 @@ struct
     {"WindowsNT40" , _WIN32_WINNT_NT4 , LoadAndBootWindows   },
 #endif
     {"Windows2003" , _WIN32_WINNT_WS03, LoadAndBootWindows   },
-
-//  {"Not found"   , 0                , NULL                 }
 };
 
+/* FUNCTIONS ******************************************************************/
+
 VOID LoadOperatingSystem(IN OperatingSystemItem* OperatingSystem)
 {
     ULONG_PTR SectionId;
@@ -61,10 +60,10 @@ VOID LoadOperatingSystem(IN OperatingSystemItem* OperatingSystem)
     CHAR BootType[80];
     ULONG i;
 
-    // Try to open the operating system section in the .ini file
+    /* Try to open the operating system section in the .ini file */
     if (IniOpenSection(SectionName, &SectionId))
     {
-        // Try to read the boot type
+        /* Try to read the boot type */
         IniReadSettingByName(SectionId, "BootType", BootType, sizeof(BootType));
     }
     else
@@ -74,7 +73,7 @@ VOID LoadOperatingSystem(IN OperatingSystemItem* OperatingSystem)
 
     if (BootType[0] == ANSI_NULL && SectionName[0] != ANSI_NULL)
     {
-        // Try to infere the boot type value
+        /* Try to infer the boot type value */
 #ifdef _M_IX86
         ULONG FileId;
         if (ArcOpen((PSTR)SectionName, OpenReadOnly, &FileId) == ESUCCESS)
@@ -89,12 +88,12 @@ VOID LoadOperatingSystem(IN OperatingSystemItem* OperatingSystem)
         }
     }
 
-    // Install the drive mapper according to this section drive mappings
-#if defined(_M_IX86) && !defined(_MSC_VER)
+#if defined(_M_IX86)
+    /* Install the drive mapper according to this section drive mappings */
     DriveMapMapDrivesInSection(SectionName);
 #endif
 
-    // Loop through the OS loading method table and find a suitable OS to boot
+    /* Loop through the OS loading method table and find a suitable OS to boot */
     for (i = 0; i < sizeof(OSLoadingMethods) / sizeof(OSLoadingMethods[0]); ++i)
     {
         if (_stricmp(BootType, OSLoadingMethods[i].BootType) == 0)
@@ -108,19 +107,17 @@ VOID LoadOperatingSystem(IN OperatingSystemItem* OperatingSystem)
 
 ULONG GetDefaultOperatingSystem(OperatingSystemItem* OperatingSystemList, ULONG OperatingSystemCount)
 {
-    CHAR    DefaultOSText[80];
-    PCSTR    DefaultOSName;
-    ULONG_PTR    SectionId;
-    ULONG    DefaultOS = 0;
-    ULONG    Idx;
+    CHAR      DefaultOSText[80];
+    PCSTR     DefaultOSName;
+    ULONG_PTR SectionId;
+    ULONG     DefaultOS = 0;
+    ULONG     Idx;
 
     if (!IniOpenSection("FreeLoader", &SectionId))
-    {
         return 0;
-    }
 
     DefaultOSName = CmdLineGetDefaultOS();
-    if (NULL == DefaultOSName)
+    if (DefaultOSName == NULL)
     {
         if (IniReadSettingByName(SectionId, "DefaultOS", DefaultOSText, sizeof(DefaultOSText)))
         {
@@ -128,9 +125,9 @@ ULONG GetDefaultOperatingSystem(OperatingSystemItem* OperatingSystemList, ULONG
         }
     }
 
-    if (NULL != DefaultOSName)
+    if (DefaultOSName != NULL)
     {
-        for (Idx=0; Idx<OperatingSystemCount; Idx++)
+        for (Idx = 0; Idx < OperatingSystemCount; Idx++)
         {
             if (_stricmp(DefaultOSName, OperatingSystemList[Idx].SystemPartition) == 0)
             {
@@ -150,24 +147,16 @@ LONG GetTimeOut(VOID)
     ULONG_PTR    SectionId;
 
     TimeOut = CmdLineGetTimeOut();
-    if (0 <= TimeOut)
-    {
+    if (TimeOut >= 0)
         return TimeOut;
-    }
 
     if (!IniOpenSection("FreeLoader", &SectionId))
-    {
         return -1;
-    }
 
     if (IniReadSettingByName(SectionId, "TimeOut", TimeOutText, sizeof(TimeOutText)))
-    {
         TimeOut = atoi(TimeOutText);
-    }
     else
-    {
         TimeOut = -1;
-    }
 
     return TimeOut;
 }
@@ -177,52 +166,56 @@ BOOLEAN MainBootMenuKeyPressFilter(ULONG KeyPress)
     if (KeyPress == KEY_F8)
     {
         DoOptionsMenu();
-
         return TRUE;
     }
 
-    // We didn't handle the key
+    /* We didn't handle the key */
     return FALSE;
 }
 
 VOID RunLoader(VOID)
 {
-    ULONG_PTR    SectionId;
-    ULONG        OperatingSystemCount;
-    OperatingSystemItem*    OperatingSystemList;
-    PCSTR*        OperatingSystemDisplayNames;
-    ULONG        DefaultOperatingSystem;
-    LONG        TimeOut;
-    ULONG        SelectedOperatingSystem;
-    ULONG    i;
+    ULONG_PTR SectionId;
+    ULONG     OperatingSystemCount;
+    OperatingSystemItem* OperatingSystemList;
+    PCSTR*    OperatingSystemDisplayNames;
+    ULONG     DefaultOperatingSystem;
+    LONG      TimeOut;
+    ULONG     SelectedOperatingSystem;
+    ULONG     i;
 
     if (!MachInitializeBootDevices())
     {
-        UiMessageBoxCritical("Error when detecting hardware");
+        UiMessageBoxCritical("Error when detecting hardware.");
         return;
     }
 
 #ifdef _M_IX86
-    // Load additional SCSI driver (if any)
+    /* Load additional SCSI driver (if any) */
     if (LoadBootDeviceDriver() != ESUCCESS)
     {
-        UiMessageBoxCritical("Unable to load additional boot device driver");
+        UiMessageBoxCritical("Unable to load additional boot device drivers.");
     }
 #endif
 
     if (!IniFileInitialize())
     {
-        UiMessageBoxCritical("Error initializing .ini file");
+        UiMessageBoxCritical("Error initializing .ini file.");
         return;
     }
 
+    /* Debugger main initialization */
+    DebugInit(TRUE);
+
     if (!IniOpenSection("FreeLoader", &SectionId))
     {
         UiMessageBoxCritical("Section [FreeLoader] not found in freeldr.ini.");
         return;
     }
+
     TimeOut = GetTimeOut();
 
+    /* UI main initialization */
     if (!UiInitialize(TRUE))
     {
         UiMessageBoxCritical("Unable to initialize UI.");
@@ -233,41 +226,36 @@ VOID RunLoader(VOID)
     if (!OperatingSystemList)
     {
         UiMessageBox("Unable to read operating systems section in freeldr.ini.\nPress ENTER to reboot.");
-        goto reboot;
+        goto Reboot;
     }
 
     if (OperatingSystemCount == 0)
     {
         UiMessageBox("There were no operating systems listed in freeldr.ini.\nPress ENTER to reboot.");
-        goto reboot;
+        goto Reboot;
     }
 
     DefaultOperatingSystem = GetDefaultOperatingSystem(OperatingSystemList, OperatingSystemCount);
 
-    //
-    // Create list of display names
-    //
+    /* Create list of display names */
     OperatingSystemDisplayNames = FrLdrTempAlloc(sizeof(PCSTR) * OperatingSystemCount, 'mNSO');
     if (!OperatingSystemDisplayNames)
-    {
-        goto reboot;
-    }
+        goto Reboot;
+
     for (i = 0; i < OperatingSystemCount; i++)
     {
         OperatingSystemDisplayNames[i] = OperatingSystemList[i].LoadIdentifier;
     }
 
-    //
-    // Find all the message box settings and run them
-    //
+    /* Find all the message box settings and run them */
     UiShowMessageBoxesInSection("FreeLoader");
 
     for (;;)
     {
-        // Redraw the backdrop
+        /* Redraw the backdrop */
         UiDrawBackdrop();
 
-        // Show the operating system list menu
+        /* Show the operating system list menu */
         if (!UiDisplayMenu("Please select the operating system to start:",
                            "For troubleshooting and advanced startup options for "
                                "ReactOS, press F8.",
@@ -281,16 +269,17 @@ VOID RunLoader(VOID)
                            MainBootMenuKeyPressFilter))
         {
             UiMessageBox("Press ENTER to reboot.");
-            goto reboot;
+            goto Reboot;
         }
 
         TimeOut = -1;
 
-        // Load the chosen operating system
+        /* Load the chosen operating system */
         LoadOperatingSystem(&OperatingSystemList[SelectedOperatingSystem]);
     }
 
-reboot:
+Reboot:
     UiUnInitialize("Rebooting...");
+    IniCleanup();
     return;
 }