Merge from amd64 branch:
[reactos.git] / reactos / boot / freeldr / freeldr / bootmgr.c
index eaa69eb..a83f294 100644 (file)
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <freeldr.h>
 
+ULONG   GetDefaultOperatingSystem(OperatingSystemItem* OperatingSystemList, ULONG       OperatingSystemCount)
+{
+       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 (IniReadSettingByName(SectionId, "DefaultOS", DefaultOSText, sizeof(DefaultOSText)))
+               {
+                       DefaultOSName = DefaultOSText;
+               }
+       }
+
+       if (NULL != DefaultOSName)
+       {
+               for (Idx=0; Idx<OperatingSystemCount; Idx++)
+               {
+                       if (_stricmp(DefaultOSName, OperatingSystemList[Idx].SystemPartition) == 0)
+                       {
+                               DefaultOS = Idx;
+                               break;
+                       }
+               }
+       }
+
+       return DefaultOS;
+}
+
+LONG GetTimeOut(VOID)
+{
+       CHAR    TimeOutText[20];
+       LONG            TimeOut;
+       ULONG_PTR       SectionId;
+
+       TimeOut = CmdLineGetTimeOut();
+       if (0 <= TimeOut)
+       {
+               return TimeOut;
+       }
+
+       if (!IniOpenSection("FreeLoader", &SectionId))
+       {
+               return -1;
+       }
+
+       if (IniReadSettingByName(SectionId, "TimeOut", TimeOutText, sizeof(TimeOutText)))
+       {
+               TimeOut = atoi(TimeOutText);
+       }
+       else
+       {
+               TimeOut = -1;
+       }
+
+       return TimeOut;
+}
+
+BOOLEAN MainBootMenuKeyPressFilter(ULONG KeyPress)
+{
+       if (KeyPress == KEY_F8)
+       {
+               DoOptionsMenu();
+
+               return TRUE;
+       }
+
+       // We didn't handle the key
+       return FALSE;
+}
+
 VOID RunLoader(VOID)
 {
        CHAR    SettingValue[80];
+       CHAR BootType[80];
        ULONG_PTR       SectionId;
        ULONG           OperatingSystemCount;
-       PCSTR   *OperatingSystemSectionNames;
+       OperatingSystemItem*    OperatingSystemList;
        PCSTR   *OperatingSystemDisplayNames;
        PCSTR SectionName;
+       ULONG   i;
        ULONG           DefaultOperatingSystem;
        LONG            TimeOut;
        ULONG           SelectedOperatingSystem;
@@ -57,10 +138,10 @@ VOID RunLoader(VOID)
                return;
        }
 
-
-       if (!InitOperatingSystemList(&OperatingSystemSectionNames, &OperatingSystemDisplayNames, &OperatingSystemCount))
+       OperatingSystemList = InitOperatingSystemList(&OperatingSystemCount);
+       if (!OperatingSystemList)
        {
-               UiMessageBox("Press ENTER to reboot.");
+               UiMessageBox("Unable to read operating systems section in freeldr.ini.\nPress ENTER to reboot.");
                goto reboot;
        }
 
@@ -70,7 +151,20 @@ VOID RunLoader(VOID)
                goto reboot;
        }
 
-       DefaultOperatingSystem = GetDefaultOperatingSystem(OperatingSystemSectionNames, OperatingSystemCount);
+       DefaultOperatingSystem = GetDefaultOperatingSystem(OperatingSystemList, OperatingSystemCount);
+
+       //
+       // Create list of display names
+       //
+       OperatingSystemDisplayNames = MmHeapAlloc(sizeof(PCSTR) * OperatingSystemCount);
+       if (!OperatingSystemDisplayNames)
+       {
+               goto reboot;
+       }
+       for (i = 0; i < OperatingSystemCount; i++)
+       {
+               OperatingSystemDisplayNames[i] = OperatingSystemList[i].LoadIdentifier;
+       }
 
        //
        // Find all the message box settings and run them
@@ -94,57 +188,53 @@ VOID RunLoader(VOID)
 
                // Try to open the operating system section in the .ini file
                SettingValue[0] = ANSI_NULL;
-               SectionName = OperatingSystemSectionNames[SelectedOperatingSystem];
+               SectionName = OperatingSystemList[SelectedOperatingSystem].SystemPartition;
                if (IniOpenSection(SectionName, &SectionId))
                {
                        // Try to read the boot type
-                       IniReadSettingByName(SectionId, "BootType", SettingValue, sizeof(SettingValue));
+                       IniReadSettingByName(SectionId, "BootType", BootType, sizeof(BootType));
                }
+               else
+                       BootType[0] = ANSI_NULL;
 
-               if (SettingValue[0] == ANSI_NULL && SectionName[0] != ANSI_NULL)
+               if (BootType[0] == ANSI_NULL && SectionName[0] != ANSI_NULL)
                {
                        // Try to infere boot type value
 #ifdef __i386__
-                       CHAR LastChar;
-                       LastChar = SectionName[strlen(SectionName) - 1];
-                       if (LastChar == '\\' ||
-                           (strstr(SectionName, ")partition(") != NULL &&
-                            strstr(SectionName, ")partition(0)") == NULL))
-                       {
-                               strcpy(SettingValue, "Partition");
-                       }
-                       else if (LastChar == ')' || LastChar == ':')
+                       ULONG FileId;
+                       if (ArcOpen((CHAR*)SectionName, OpenReadOnly, &FileId) == ESUCCESS)
                        {
-                               strcpy(SettingValue, "Drive");
-                       }
-                       else if (TRUE)
-                       {
-                               strcpy(SettingValue, "BootSector");
+                               ArcClose(FileId);
+                               strcpy(BootType, "BootSector");
                        }
                        else
 #endif
                        {
-                               strcpy(SettingValue, "Windows2003");
+                               strcpy(BootType, "Windows");
                        }
                }
 
+               // Get OS setting value
+               IniOpenSection("Operating Systems", &SectionId);
+               IniReadSettingByName(SectionId, SectionName, SettingValue, sizeof(SettingValue));
+
                // Install the drive mapper according to this sections drive mappings
 #ifdef __i386__
                DriveMapMapDrivesInSection(SectionName);
 #endif
-               if (_stricmp(SettingValue, "ReactOS") == 0)
+               if (_stricmp(BootType, "ReactOS") == 0)
                {
                        LoadAndBootReactOS(SectionName);
                }
 #ifdef FREELDR_REACTOS_SETUP
-               else if (_stricmp(SettingValue, "ReactOSSetup") == 0)
+               else if (_stricmp(BootType, "ReactOSSetup") == 0)
                {
                        // In future we could pass the selected OS details through this
                        // to have different install methods, etc.
                        LoadReactOSSetup();
                }
 #if defined(__i386__) || defined(__x86_64__)
-               else if (_stricmp(SettingValue, "ReactOSSetup2") == 0)
+               else if (_stricmp(BootType, "ReactOSSetup2") == 0)
                {
                        // WinLdr-style boot
                        LoadReactOSSetup2();
@@ -152,27 +242,31 @@ VOID RunLoader(VOID)
 #endif
 #endif
 #ifdef __i386__
-               else if (_stricmp(SettingValue, "WindowsNT40") == 0)
+               else if (_stricmp(BootType, "Windows") == 0)
                {
-                       LoadAndBootWindows(SectionName, _WIN32_WINNT_NT4);
+                       LoadAndBootWindows(SectionName, SettingValue, 0);
                }
-               else if (_stricmp(SettingValue, "Windows2003") == 0)
+               else if (_stricmp(BootType, "WindowsNT40") == 0)
                {
-                       LoadAndBootWindows(SectionName, _WIN32_WINNT_WS03);
+                       LoadAndBootWindows(SectionName, SettingValue, _WIN32_WINNT_NT4);
                }
-               else if (_stricmp(SettingValue, "Linux") == 0)
+               else if (_stricmp(BootType, "Windows2003") == 0)
+               {
+                       LoadAndBootWindows(SectionName, SettingValue, _WIN32_WINNT_WS03);
+               }
+               else if (_stricmp(BootType, "Linux") == 0)
                {
                        LoadAndBootLinux(SectionName, OperatingSystemDisplayNames[SelectedOperatingSystem]);
                }
-               else if (_stricmp(SettingValue, "BootSector") == 0)
+               else if (_stricmp(BootType, "BootSector") == 0)
                {
                        LoadAndBootBootSector(SectionName);
                }
-               else if (_stricmp(SettingValue, "Partition") == 0)
+               else if (_stricmp(BootType, "Partition") == 0)
                {
                        LoadAndBootPartition(SectionName);
                }
-               else if (_stricmp(SettingValue, "Drive") == 0)
+               else if (_stricmp(BootType, "Drive") == 0)
                {
                        LoadAndBootDrive(SectionName);
                }
@@ -184,82 +278,3 @@ reboot:
        UiUnInitialize("Rebooting...");
        return;
 }
-
-ULONG   GetDefaultOperatingSystem(PCSTR OperatingSystemList[], ULONG    OperatingSystemCount)
-{
-       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 (IniReadSettingByName(SectionId, "DefaultOS", DefaultOSText, sizeof(DefaultOSText)))
-               {
-                       DefaultOSName = DefaultOSText;
-               }
-       }
-
-       if (NULL != DefaultOSName)
-       {
-               for (Idx=0; Idx<OperatingSystemCount; Idx++)
-               {
-                       if (_stricmp(DefaultOSName, OperatingSystemList[Idx]) == 0)
-                       {
-                               DefaultOS = Idx;
-                               break;
-                       }
-               }
-       }
-
-       return DefaultOS;
-}
-
-LONG GetTimeOut(VOID)
-{
-       CHAR    TimeOutText[20];
-       LONG            TimeOut;
-       ULONG_PTR       SectionId;
-
-       TimeOut = CmdLineGetTimeOut();
-       if (0 <= TimeOut)
-       {
-               return TimeOut;
-       }
-
-       if (!IniOpenSection("FreeLoader", &SectionId))
-       {
-               return -1;
-       }
-
-       if (IniReadSettingByName(SectionId, "TimeOut", TimeOutText, sizeof(TimeOutText)))
-       {
-               TimeOut = atoi(TimeOutText);
-       }
-       else
-       {
-               TimeOut = -1;
-       }
-
-       return TimeOut;
-}
-
-BOOLEAN MainBootMenuKeyPressFilter(ULONG KeyPress)
-{
-       if (KeyPress == KEY_F8)
-       {
-               DoOptionsMenu();
-
-               return TRUE;
-       }
-
-       // We didn't handle the key
-       return FALSE;
-}