Move freeldr to reactos\boot\freeldr.
[reactos.git] / reactos / boot / freeldr / freeldr / inifile / ini_init.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <freeldr.h>
21 #include "ini.h"
22 #include <fs.h>
23 #include <rtl.h>
24 #include <mm.h>
25 #include <debug.h>
26
27
28 BOOL IniFileInitialize(VOID)
29 {
30 PFILE Freeldr_Ini; // File handle for freeldr.ini
31 PUCHAR FreeLoaderIniFileData;
32 U32 FreeLoaderIniFileSize;
33 BOOL Success;
34
35 // Open freeldr.ini
36 // BootDrive & BootPartition are passed
37 // in from the boot sector code in the
38 // DL & DH registers.
39 Freeldr_Ini = IniOpenIniFile(BootDrive, BootPartition);
40
41 // If we couldn't open freeldr.ini on the partition
42 // they specified in the boot sector then try
43 // opening the active (boot) partition.
44 if ((Freeldr_Ini == NULL) && (BootPartition != 0))
45 {
46 BootPartition = 0;
47
48 Freeldr_Ini = IniOpenIniFile(BootDrive, BootPartition);
49
50 return FALSE;
51 }
52
53 if (Freeldr_Ini == NULL)
54 {
55 printf("Error opening freeldr.ini or file not found.\n");
56 printf("You need to re-install FreeLoader.\n");
57 return FALSE;
58 }
59
60 // Get the file size & allocate enough memory for it
61 FreeLoaderIniFileSize = FsGetFileSize(Freeldr_Ini);
62 FreeLoaderIniFileData = MmAllocateMemory(FreeLoaderIniFileSize);
63
64 // If we are out of memory then return FALSE
65 if (FreeLoaderIniFileData == NULL)
66 {
67 printf("Out of memory while loading freeldr.ini.\n");
68 FsCloseFile(Freeldr_Ini);
69 return FALSE;
70 }
71
72 // Read freeldr.ini off the disk
73 if (!FsReadFile(Freeldr_Ini, FreeLoaderIniFileSize, NULL, FreeLoaderIniFileData))
74 {
75 FsCloseFile(Freeldr_Ini);
76 MmFreeMemory(FreeLoaderIniFileData);
77 return FALSE;
78 }
79
80 FsCloseFile(Freeldr_Ini);
81
82 // Parse the .ini file data
83 Success = IniParseFile(FreeLoaderIniFileData, FreeLoaderIniFileSize);
84
85 MmFreeMemory(FreeLoaderIniFileData);
86
87 return Success;
88 }
89
90 PFILE IniOpenIniFile(U8 BootDriveNumber, U8 BootPartitionNumber)
91 {
92 PFILE IniFileHandle; // File handle for freeldr.ini
93
94 if (!FsOpenVolume(BootDriveNumber, BootPartitionNumber))
95 {
96 if (BootPartitionNumber == 0)
97 {
98 printf("Error opening active (bootable) partition on boot drive 0x%x for file access.\n", BootDriveNumber);
99 }
100 else
101 {
102 printf("Error opening partition %d on boot drive 0x%x for file access.\n", BootPartitionNumber, BootDriveNumber);
103 }
104
105 return NULL;
106 }
107
108 // Try to open freeldr.ini
109 IniFileHandle = FsOpenFile("freeldr.ini");
110
111 return IniFileHandle;
112 }