[FREELDR] Some ARC-spec compatibility refactoring + simplifications & fixes.
[reactos.git] / boot / freeldr / freeldr / linuxboot.c
index d59ae16..6ddc8b9 100644 (file)
@@ -38,53 +38,55 @@ ULONG   SetupSectorSize = 0;
 BOOLEAN NewStyleLinuxKernel = FALSE;
 ULONG   LinuxKernelSize = 0;
 ULONG   LinuxInitrdSize = 0;
-CHAR    LinuxKernelName[260];
-CHAR    LinuxInitrdName[260];
-BOOLEAN LinuxHasInitrd = FALSE;
-CHAR    LinuxCommandLine[260] = "";
+PCSTR   LinuxKernelName = NULL;
+PCSTR   LinuxInitrdName = NULL;
+PSTR    LinuxCommandLine = NULL;
 ULONG   LinuxCommandLineSize = 0;
 PVOID   LinuxKernelLoadAddress = NULL;
 PVOID   LinuxInitrdLoadAddress = NULL;
 CHAR    LinuxBootDescription[80];
-CHAR    LinuxBootPath[260] = "";
+PCSTR   LinuxBootPath = NULL;
 
 /* FUNCTIONS ******************************************************************/
 
-BOOLEAN RemoveQuotes(PCHAR QuotedString)
+VOID
+RemoveQuotes(
+    IN OUT PSTR QuotedString)
 {
-    CHAR  TempString[200];
-    PCHAR p;
-    PSTR  Start;
+    PCHAR  p;
+    PSTR   Start;
+    SIZE_T Size;
 
     /* Skip spaces up to " */
     p = QuotedString;
-    while (*p == ' ' || *p == '"')
-        p++;
+    while (*p == ' ' || *p == '\t' || *p == '"')
+        ++p;
     Start = p;
 
     /* Go up to next " */
-    while (*p != '"' && *p != ANSI_NULL)
-        p++;
+    while (*p != ANSI_NULL && *p != '"')
+        ++p;
+    /* NULL-terminate */
     *p = ANSI_NULL;
 
-    /* Copy result */
-    strcpy(TempString, Start);
-    strcpy(QuotedString, TempString);
-
-    return TRUE;
+    /* Move the NULL-terminated string back into 'QuotedString' in place */
+    Size = (strlen(Start) + 1) * sizeof(CHAR);
+    memmove(QuotedString, Start, Size);
 }
 
-VOID
-LoadAndBootLinux(IN OperatingSystemItem* OperatingSystem,
-                 IN USHORT OperatingSystemVersion)
+ARC_STATUS
+LoadAndBootLinux(
+    IN ULONG Argc,
+    IN PCHAR Argv[],
+    IN PCHAR Envp[])
 {
-    PCSTR SectionName = OperatingSystem->SystemPartition;
-    PCSTR Description = OperatingSystem->LoadIdentifier;
+    PCSTR Description;
     PFILE LinuxKernel = 0;
     PFILE LinuxInitrdFile = 0;
 
+    Description = GetArgumentValue(Argc, Argv, "LoadIdentifier");
     if (Description)
-        sprintf(LinuxBootDescription, "Loading %s...", Description);
+        RtlStringCbPrintfA(LinuxBootDescription, sizeof(LinuxBootDescription), "Loading %s...", Description);
     else
         strcpy(LinuxBootDescription, "Loading Linux...");
 
@@ -92,8 +94,11 @@ LoadAndBootLinux(IN OperatingSystemItem* OperatingSystem,
     UiDrawStatusText(LinuxBootDescription);
     UiDrawProgressBarCenter(0, 100, LinuxBootDescription);
 
+    /* Find all the message box settings and run them */
+    UiShowMessageBoxesInArgv(Argc, Argv);
+
     /* Parse the .ini file section */
-    if (!LinuxParseIniSection(SectionName))
+    if (!LinuxParseIniSection(Argc, Argv))
         goto LinuxBootFailed;
 
     /* Open the kernel */
@@ -105,7 +110,7 @@ LoadAndBootLinux(IN OperatingSystemItem* OperatingSystem,
     }
 
     /* Open the initrd file image (if necessary) */
-    if (LinuxHasInitrd)
+    if (LinuxInitrdName)
     {
         LinuxInitrdFile = FsOpenFile(LinuxInitrdName);
         if (!LinuxInitrdFile)
@@ -134,7 +139,7 @@ LoadAndBootLinux(IN OperatingSystemItem* OperatingSystem,
         goto LinuxBootFailed;
 
     /* Read the initrd (if necessary) */
-    if (LinuxHasInitrd)
+    if (LinuxInitrdName)
     {
         if (!LinuxReadInitrd(LinuxInitrdFile))
             goto LinuxBootFailed;
@@ -161,7 +166,9 @@ LoadAndBootLinux(IN OperatingSystemItem* OperatingSystem,
 
     RtlCopyMemory((PVOID)0x90000, LinuxBootSector, 512);
     RtlCopyMemory((PVOID)0x90200, LinuxSetupSector, SetupSectorSize);
-    RtlCopyMemory((PVOID)0x99000, LinuxCommandLine, LinuxCommandLineSize);
+    RtlCopyMemory((PVOID)0x99000,
+                  LinuxCommandLine ? LinuxCommandLine : "",
+                  LinuxCommandLine ? LinuxCommandLineSize : sizeof(ANSI_NULL));
 
     UiUnInitialize("Booting Linux...");
     IniCleanup();
@@ -196,54 +203,52 @@ LinuxBootFailed:
 
     LinuxBootSector = NULL;
     LinuxSetupSector = NULL;
-    LinuxKernelLoadAddress = NULL;
-    LinuxInitrdLoadAddress = NULL;
     SetupSectorSize = 0;
     NewStyleLinuxKernel = FALSE;
     LinuxKernelSize = 0;
-    LinuxHasInitrd = FALSE;
-    strcpy(LinuxCommandLine, "");
+    LinuxInitrdSize = 0;
+    LinuxKernelName = NULL;
+    LinuxInitrdName = NULL;
+    LinuxCommandLine = NULL;
     LinuxCommandLineSize = 0;
+    LinuxKernelLoadAddress = NULL;
+    LinuxInitrdLoadAddress = NULL;
+    *LinuxBootDescription = ANSI_NULL;
+    LinuxBootPath = NULL;
+
+    return ENOEXEC;
 }
 
-BOOLEAN LinuxParseIniSection(PCSTR SectionName)
+BOOLEAN
+LinuxParseIniSection(
+    IN ULONG Argc,
+    IN PCHAR Argv[])
 {
-    ULONG_PTR SectionId;
-
-    /* Find all the message box settings and run them */
-    UiShowMessageBoxesInSection(SectionName);
-
-    /* Try to open the operating system section in the .ini file */
-    if (!IniOpenSection(SectionName, &SectionId))
-    {
-        UiMessageBox("Section [%s] not found in freeldr.ini.", SectionName);
-        return FALSE;
-    }
-
-    if (!IniReadSettingByName(SectionId, "BootPath", LinuxBootPath, sizeof(LinuxBootPath)))
+    LinuxBootPath = GetArgumentValue(Argc, Argv, "BootPath");
+    if (!LinuxBootPath)
     {
         UiMessageBox("Boot path not specified for selected OS!");
         return FALSE;
     }
 
     /* Get the kernel name */
-    if (!IniReadSettingByName(SectionId, "Kernel", LinuxKernelName, sizeof(LinuxKernelName)))
+    LinuxKernelName = GetArgumentValue(Argc, Argv, "Kernel");
+    if (!LinuxKernelName)
     {
         UiMessageBox("Linux kernel filename not specified for selected OS!");
         return FALSE;
     }
 
-    /* Get the initrd name */
-    if (IniReadSettingByName(SectionId, "Initrd", LinuxInitrdName, sizeof(LinuxInitrdName)))
-    {
-        LinuxHasInitrd = TRUE;
-    }
+    /* Get the initrd name (optional) */
+    LinuxInitrdName = GetArgumentValue(Argc, Argv, "Initrd");
 
-    /* Get the command line */
-    if (IniReadSettingByName(SectionId, "CommandLine", LinuxCommandLine, sizeof(LinuxCommandLine)))
+    /* Get the command line (optional) */
+    LinuxCommandLineSize = 0;
+    LinuxCommandLine = GetArgumentValue(Argc, Argv, "CommandLine");
+    if (LinuxCommandLine)
     {
         RemoveQuotes(LinuxCommandLine);
-        LinuxCommandLineSize = strlen(LinuxCommandLine) + 1;
+        LinuxCommandLineSize = min(strlen(LinuxCommandLine) + 1, 260);
     }
 
     return TRUE;
@@ -270,14 +275,14 @@ BOOLEAN LinuxReadBootSector(PFILE LinuxKernelFile)
 
     // DbgDumpBuffer(DPRINT_LINUX, LinuxBootSector, 512);
 
-    TRACE("SetupSectors: %d\n", LinuxBootSector->SetupSectors);
-    TRACE("RootFlags: 0x%x\n", LinuxBootSector->RootFlags);
-    TRACE("SystemSize: 0x%x\n", LinuxBootSector->SystemSize);
-    TRACE("SwapDevice: 0x%x\n", LinuxBootSector->SwapDevice);
-    TRACE("RamSize: 0x%x\n", LinuxBootSector->RamSize);
-    TRACE("VideoMode: 0x%x\n", LinuxBootSector->VideoMode);
-    TRACE("RootDevice: 0x%x\n", LinuxBootSector->RootDevice);
-    TRACE("BootFlag: 0x%x\n", LinuxBootSector->BootFlag);
+    TRACE("SetupSectors: %d\n"  , LinuxBootSector->SetupSectors);
+    TRACE("RootFlags:    0x%x\n", LinuxBootSector->RootFlags);
+    TRACE("SystemSize:   0x%x\n", LinuxBootSector->SystemSize);
+    TRACE("SwapDevice:   0x%x\n", LinuxBootSector->SwapDevice);
+    TRACE("RamSize:      0x%x\n", LinuxBootSector->RamSize);
+    TRACE("VideoMode:    0x%x\n", LinuxBootSector->VideoMode);
+    TRACE("RootDevice:   0x%x\n", LinuxBootSector->RootDevice);
+    TRACE("BootFlag:     0x%x\n", LinuxBootSector->BootFlag);
 
     return TRUE;
 }
@@ -286,14 +291,13 @@ BOOLEAN LinuxReadSetupSector(PFILE LinuxKernelFile)
 {
     UCHAR TempLinuxSetupSector[512];
 
-    LinuxSetupSector = (PLINUX_SETUPSECTOR)TempLinuxSetupSector;
-
     /* Read first linux setup sector */
     FsSetFilePointer(LinuxKernelFile, 512);
     if (!FsReadFile(LinuxKernelFile, 512, NULL, TempLinuxSetupSector))
         return FALSE;
 
     /* Check the kernel version */
+    LinuxSetupSector = (PLINUX_SETUPSECTOR)TempLinuxSetupSector;
     if (!LinuxCheckKernelVersion())
         return FALSE;
 
@@ -318,18 +322,18 @@ BOOLEAN LinuxReadSetupSector(PFILE LinuxKernelFile)
     // DbgDumpBuffer(DPRINT_LINUX, LinuxSetupSector, SetupSectorSize);
 
     TRACE("SetupHeaderSignature: 0x%x (HdrS)\n", LinuxSetupSector->SetupHeaderSignature);
-    TRACE("Version: 0x%x\n", LinuxSetupSector->Version);
+    TRACE("Version:              0x%x\n", LinuxSetupSector->Version);
     TRACE("RealModeSwitch: 0x%x\n", LinuxSetupSector->RealModeSwitch);
-    TRACE("SetupSeg: 0x%x\n", LinuxSetupSector->SetupSeg);
+    TRACE("SetupSeg:       0x%x\n", LinuxSetupSector->SetupSeg);
     TRACE("StartSystemSeg: 0x%x\n", LinuxSetupSector->StartSystemSeg);
-    TRACE("KernelVersion: 0x%x\n", LinuxSetupSector->KernelVersion);
-    TRACE("TypeOfLoader: 0x%x\n", LinuxSetupSector->TypeOfLoader);
-    TRACE("LoadFlags: 0x%x\n", LinuxSetupSector->LoadFlags);
-    TRACE("SetupMoveSize: 0x%x\n", LinuxSetupSector->SetupMoveSize);
-    TRACE("Code32Start: 0x%x\n", LinuxSetupSector->Code32Start);
+    TRACE("KernelVersion:  0x%x\n", LinuxSetupSector->KernelVersion);
+    TRACE("TypeOfLoader:   0x%x\n", LinuxSetupSector->TypeOfLoader);
+    TRACE("LoadFlags:      0x%x\n", LinuxSetupSector->LoadFlags);
+    TRACE("SetupMoveSize:  0x%x\n", LinuxSetupSector->SetupMoveSize);
+    TRACE("Code32Start:    0x%x\n", LinuxSetupSector->Code32Start);
     TRACE("RamdiskAddress: 0x%x\n", LinuxSetupSector->RamdiskAddress);
-    TRACE("RamdiskSize: 0x%x\n", LinuxSetupSector->RamdiskSize);
-    TRACE("BootSectKludgeOffset: 0x%x\n", LinuxSetupSector->BootSectKludgeOffset);
+    TRACE("RamdiskSize:    0x%x\n", LinuxSetupSector->RamdiskSize);
+    TRACE("BootSectKludgeOffset:  0x%x\n", LinuxSetupSector->BootSectKludgeOffset);
     TRACE("BootSectKludgeSegment: 0x%x\n", LinuxSetupSector->BootSectKludgeSegment);
     TRACE("HeapEnd: 0x%x\n", LinuxSetupSector->HeapEnd);
 
@@ -398,7 +402,7 @@ BOOLEAN LinuxCheckKernelVersion(VOID)
         LinuxSetupSector->LoadFlags |= LINUX_FLAG_CAN_USE_HEAP;
     }
 
-    if ((NewStyleLinuxKernel == FALSE) && (LinuxHasInitrd))
+    if ((NewStyleLinuxKernel == FALSE) && (LinuxInitrdName))
     {
         UiMessageBox("Error: Cannot load a ramdisk (initrd) with an old kernel image.");
         return FALSE;
@@ -437,7 +441,7 @@ BOOLEAN LinuxReadInitrd(PFILE LinuxInitrdFile)
     LinuxSetupSector->RamdiskSize = LinuxInitrdSize;
 
     TRACE("RamdiskAddress: 0x%x\n", LinuxSetupSector->RamdiskAddress);
-    TRACE("RamdiskSize: 0x%x\n", LinuxSetupSector->RamdiskSize);
+    TRACE("RamdiskSize:    0x%x\n", LinuxSetupSector->RamdiskSize);
 
     if (LinuxSetupSector->Version >= 0x0203)
     {