-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
210 lines (177 loc) · 4.42 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* PROJECT: ReactOS RAMDisk Creator
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* PURPOSE: main.c file
* COPYRIGHT: Copyright 2021 Arnav Bhatt ([email protected])
*/
#define countof(array) (sizeof(array) / sizeof(array[0]))
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arg.h"
#include "bool.h"
#include "fs_data.h"
#include "ini.h"
#include "ini_data.h"
#ifdef _WIN32
#include <windows.h>
#include "win32_volume.h"
#else
#include "volume.h"
#endif
char *argv0;
bool InstallBootSector(FileHandle* fhandle, char* VolumeType);
static void ProgramUsage(void);
int main(int argc, char *argv[])
{
bool Ret = false;
bool bootcd = false;
bool livecd = false;
char* drive = "";
char* IniDest = "";
char* fs_type = "";
FileHandle* fhandle;
ARGBEGIN
{
case 'b':
{
bootcd = true;
break;
}
case 'd':
{
drive = EARGF(ProgramUsage());
break;
}
case 'f':
{
fs_type = EARGF(ProgramUsage());
break;
}
case 'i':
{
IniDest = EARGF(ProgramUsage());
break;
}
case 'l':
{
livecd = true;
break;
}
default:
{
ProgramUsage();
break;
}
} ARGEND
if (strcmp(drive, "") != 0 && strcmp(fs_type, "") != 0)
{
fhandle = OpenVolume(drive);
if (fhandle == NULL)
{
return -1;
}
if (!InstallBootSector(fhandle, fs_type))
{
CloseVolume(fhandle);
return -1;
}
CloseVolume(fhandle);
}
else if((strcmp(IniDest, "") != 0))
{
for (int i = 0; i < countof(MainIniData); i++)
{
Ret = CreateINI(IniDest, MainIniData[i].pacTopic, MainIniData[i].pacItem, MainIniData[i].pacValue);
if (!Ret)
{
return -1;
}
}
if (bootcd)
{
for (int i = 0; i < countof(BootCDIniData); i++)
{
Ret = CreateINI(IniDest, BootCDIniData[i].pacTopic, BootCDIniData[i].pacItem, BootCDIniData[i].pacValue);
if (!Ret)
{
return -1;
}
}
}
if (livecd)
{
for (int i = 0; i < countof(LiveCDIniData); i++)
{
Ret = CreateINI(IniDest, LiveCDIniData[i].pacTopic, LiveCDIniData[i].pacItem, LiveCDIniData[i].pacValue);
if (!Ret)
{
return -1;
}
}
}
printf("INI sucessfully created.\n");
}
else
{
ProgramUsage();
}
return 0;
}
bool InstallBootSector(FileHandle* fhandle, char* VolumeType)
{
unsigned char BootSectorBuffer[512];
if (!ReadVolumeSector(fhandle, 0, BootSectorBuffer))
{
return false;
}
if (strcasecmp(VolumeType, "fat32") == 0)
{
//
// Update the BPB in the new boot sector
//
memcpy((fat32_data+3), (BootSectorBuffer+3), 87 /*fat32 BPB length*/);
//
// Write out new boot sector
//
if (!WriteVolumeSector(fhandle, 0, fat32_data))
{
return false;
}
//
// Write out new extra sector
//
if (!WriteVolumeSector(fhandle, 14, (fat32_data+512)))
{
return false;
}
}
else if (strcasecmp(VolumeType, "fat") == 0)
{
//
// Update the BPB in the new boot sector
//
memcpy((fat_data+3), (BootSectorBuffer+3), 59 /*fat BPB length*/);
//
// Write out new boot sector
//
if (!WriteVolumeSector(fhandle, 0, fat_data))
{
return false;
}
}
else
{
printf("File system type %s unknown.\n", VolumeType);
return false;
}
printf("%s boot sector installed.\n", VolumeType);
return true;
}
static void ProgramUsage(void)
{
const char *opts1 = "[-d drive] [-f fs]";
const char *opts2 = "[-i] path-to-freeldr.ini [-b] [-l]";
printf("usage: %s %s for installing bootloader\n", argv0, opts1);
printf("usage: %s %s for generating freeldr.ini.\n", argv0, opts2);
}