forked from arnavbhatt288/installfreeldr-linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.c
214 lines (190 loc) · 11.4 KB
/
install.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
211
212
213
214
/*
* PROJECT: ReactOS FreeLoader installer for Linux
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* PURPOSE: Installation functions
* COPYRIGHT: Copyright 2001 Brian Palmer ([email protected])
* Copyright 2019 Arnav Bhatt ([email protected])
*/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdbool.h>
#include "install.h"
#include "volume.h"
/* fat32_data and fat_data arrays */
unsigned char fat32_data[] =
{
0xeb, 0x58, 0x90, 0x46, 0x72, 0x4c, 0x64, 0x72, 0x31, 0x2e, 0x30, 0x00, 0x02, 0x00, 0x20, 0x00,
0x02, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x4e, 0x4f, 0x20, 0x4e, 0x41, 0x4d, 0x45, 0x20, 0x20,
0x20, 0x20, 0x46, 0x41, 0x54, 0x33, 0x32, 0x20, 0x20, 0x20, 0x31, 0xc0, 0x8e, 0xd8, 0x8e, 0xc0,
0x8e, 0xd0, 0xbd, 0x00, 0x7c, 0xbc, 0x00, 0x7c, 0x80, 0xbe, 0x40, 0x00, 0xff, 0x75, 0x04, 0x88,
0x96, 0x40, 0x00, 0x83, 0xbe, 0x16, 0x00, 0x00, 0x75, 0x0f, 0x66, 0x83, 0xbe, 0x11, 0x00, 0x00,
0x75, 0x07, 0x83, 0xbe, 0x2a, 0x00, 0x00, 0x76, 0x03, 0xe9, 0x08, 0x01, 0xb8, 0x00, 0x08, 0x8a,
0x96, 0x40, 0x00, 0xcd, 0x13, 0x73, 0x05, 0xb9, 0xff, 0xff, 0x88, 0xce, 0x88, 0xeb, 0x88, 0xcf,
0xc0, 0xef, 0x06, 0x80, 0xe1, 0x3f, 0x66, 0x0f, 0xb6, 0xc6, 0x66, 0x0f, 0xb7, 0xdb, 0x66, 0x0f,
0xb6, 0xc9, 0x66, 0x40, 0x66, 0x43, 0x66, 0xf7, 0xe1, 0x66, 0xf7, 0xe3, 0x66, 0xa3, 0xb5, 0x7d,
0x66, 0xb8, 0x0e, 0x00, 0x00, 0x00, 0x66, 0x03, 0x86, 0x1c, 0x00, 0xb9, 0x01, 0x00, 0x31, 0xdb,
0x8e, 0xc3, 0xbb, 0x00, 0x7e, 0xe8, 0x03, 0x00, 0xe9, 0x25, 0x01, 0x06, 0x66, 0x3b, 0x06, 0xb5,
0x7d, 0x73, 0x1c, 0x66, 0x60, 0xb4, 0x41, 0xbb, 0xaa, 0x55, 0x8a, 0x96, 0x40, 0x00, 0xcd, 0x13,
0x72, 0x57, 0x81, 0xfb, 0x55, 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, 0x10, 0xeb, 0xf2, 0xc3, 0x00, 0x00, 0x00, 0x00, 0x44, 0x69, 0x73, 0x6b, 0x20, 0x65, 0x72,
0x72, 0x6f, 0x72, 0x0d, 0x0a, 0x00, 0x46, 0x69, 0x6c, 0x65, 0x20, 0x73, 0x79, 0x73, 0x74, 0x65,
0x6d, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x0d, 0x0a, 0x00, 0x50, 0x72, 0x65, 0x73, 0x73, 0x20,
0x61, 0x6e, 0x79, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x74, 0x6f, 0x20, 0x72, 0x65, 0x73, 0x74, 0x61,
0x72, 0x74, 0x0d, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa,
0x66, 0x8b, 0x86, 0x2c, 0x00, 0x66, 0x3d, 0xf8, 0xff, 0xff, 0x0f, 0x72, 0x03, 0xe9, 0x6c, 0x01,
0xbb, 0x00, 0x20, 0x8e, 0xc3, 0xe8, 0x26, 0x01, 0x31, 0xdb, 0x8a, 0x9e, 0x0d, 0x00, 0xc1, 0xe3,
0x04, 0xb8, 0x00, 0x20, 0x8e, 0xc0, 0x31, 0xff, 0xbe, 0x9d, 0x7f, 0xb9, 0x0b, 0x00, 0xf3, 0xa6,
0x74, 0x2b, 0x4b, 0x75, 0x03, 0xe9, 0x44, 0x01, 0x8c, 0xc0, 0x83, 0xc0, 0x02, 0x8e, 0xc0, 0x31,
0xff, 0xbe, 0x9d, 0x7f, 0xb9, 0x0b, 0x00, 0xf3, 0xa6, 0x74, 0x12, 0x4b, 0x75, 0xea, 0x66, 0x8b,
0x86, 0x2c, 0x00, 0xe8, 0x6c, 0x00, 0x66, 0x89, 0x86, 0x2c, 0x00, 0xeb, 0xa3, 0xbe, 0xa8, 0x7f,
0xe8, 0x43, 0xff, 0x31, 0xff, 0x31, 0xd2, 0x26, 0x8b, 0x45, 0x14, 0x66, 0xc1, 0xe0, 0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, 0xaf, 0xfd, 0x66, 0x59, 0x26, 0x67,
0x66, 0x8b, 0x01, 0x66, 0x25, 0xff, 0xff, 0xff, 0x0f, 0xc3, 0xff, 0xff, 0xff, 0xff, 0x66, 0x48,
0x66, 0x48, 0x66, 0x31, 0xd2, 0x66, 0x0f, 0xb6, 0x9e, 0x0d, 0x00, 0x66, 0xf7, 0xe3, 0x66, 0x50,
0x66, 0x31, 0xd2, 0x66, 0x0f, 0xb6, 0x86, 0x10, 0x00, 0x66, 0xf7, 0xa6, 0x24, 0x00, 0x66, 0x0f,
0xb7, 0x9e, 0x0e, 0x00, 0x66, 0x01, 0xd8, 0x66, 0x03, 0x86, 0x1c, 0x00, 0x66, 0x5b, 0x66, 0x01,
0xd8, 0x31, 0xdb, 0x0f, 0xb6, 0x8e, 0x0d, 0x00, 0xe8, 0x60, 0xfd, 0xc3, 0xbe, 0x85, 0x7f, 0xe8,
0x24, 0xfe, 0xe9, 0x15, 0xfe, 0x66, 0x72, 0x65, 0x65, 0x6c, 0x64, 0x72, 0x2e, 0x73, 0x79, 0x73,
0x20, 0x6e, 0x6f, 0x74, 0x20, 0x66, 0x6f, 0x75, 0x6e, 0x64, 0x0d, 0x0a, 0x00, 0x46, 0x52, 0x45,
0x45, 0x4c, 0x44, 0x52, 0x20, 0x53, 0x59, 0x53, 0x4c, 0x6f, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x20,
0x46, 0x72, 0x65, 0x65, 0x4c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x2e, 0x2e, 0x2e, 0x0d, 0x0a, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa,
};
unsigned char fat_data[] =
{
0xeb, 0x3c, 0x90, 0x46, 0x72, 0x4c, 0x64, 0x72, 0x31, 0x2e, 0x30, 0x00, 0x02, 0x01, 0x01, 0x00,
0x02, 0xe0, 0x00, 0x40, 0x0b, 0xf0, 0x09, 0x00, 0x12, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x4e, 0x4f, 0x20, 0x4e, 0x41,
0x4d, 0x45, 0x20, 0x20, 0x20, 0x20, 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0x31, 0xc0,
0x8e, 0xd0, 0xbd, 0x00, 0x7c, 0xbc, 0xf0, 0x7b, 0x8e, 0xd8, 0x8e, 0xc0, 0x80, 0xbe, 0x24, 0x00,
0xff, 0x75, 0x04, 0x88, 0x96, 0x24, 0x00, 0xb4, 0x08, 0x8a, 0x96, 0x24, 0x00, 0xcd, 0x13, 0x73,
0x05, 0xb9, 0xff, 0xff, 0x88, 0xce, 0x88, 0xeb, 0x88, 0xcf, 0xc0, 0xef, 0x06, 0x80, 0xe1, 0x3f,
0x66, 0x0f, 0xb6, 0xc6, 0x66, 0x0f, 0xb7, 0xdb, 0x66, 0x0f, 0xb6, 0xc9, 0x66, 0x40, 0x66, 0x43,
0x66, 0xf7, 0xe1, 0x66, 0xf7, 0xe3, 0x66, 0x89, 0x46, 0xf8, 0x31, 0xc0, 0x31, 0xc9, 0x8a, 0x86,
0x10, 0x00, 0xf7, 0xa6, 0x16, 0x00, 0x03, 0x86, 0x1c, 0x00, 0x13, 0x96, 0x1e, 0x00, 0x03, 0x86,
0x0e, 0x00, 0x11, 0xca, 0x89, 0x46, 0xfc, 0x89, 0x56,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0x00, 0x52, 0x50, 0x06, 0x53, 0x6a, 0x01, 0x6a, 0x10, 0x89, 0xe6, 0x8a, 0x96,
0x24, 0x00, 0xb4, 0x42, 0xcd, 0x13, 0x72, 0xb9, 0x83, 0xc4, 0x10, 0x61, 0x40, 0x83, 0xd2, 0x00,
0x53, 0x8c, 0xc3, 0x83, 0xc3, 0x20, 0x8e, 0xc3, 0x5b, 0xe2, 0xd4, 0xc3, 0x61, 0x60, 0x91, 0x92,
0x31, 0xd2, 0xf7, 0xb6, 0x18, 0x00, 0x91, 0xf7, 0xb6, 0x18, 0x00, 0x42, 0x87, 0xd1, 0xf7, 0xb6,
0x1a, 0x00, 0x88, 0xd6, 0x8a, 0x96, 0x24, 0x00, 0x88, 0xc5, 0xc0, 0xcc, 0x02, 0x08, 0xe1, 0xb8,
0x01, 0x02, 0xcd, 0x13, 0x0f, 0x82, 0x79, 0xff, 0x61, 0x40, 0x75, 0x01, 0x42, 0x53, 0x8c, 0xc3,
0x83, 0xc3, 0x20, 0x8e, 0xc3, 0x5b, 0xe2, 0xc5, 0xc3, 0x44, 0x69, 0x73, 0x6b, 0x20, 0x65, 0x72,
0x72, 0x6f, 0x72, 0x0d, 0x0a, 0x00, 0x4c, 0x64, 0x72, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x66, 0x6f,
0x75, 0x6e, 0x64, 0x0d, 0x0a, 0x00, 0x46, 0x52, 0x45, 0x45, 0x4c, 0x44, 0x52, 0x20, 0x53, 0x59,
0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa,
};
bool InstallBootSector(char* lpszVolumeType);
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("syntax: installfreeldr /dev/[disk_device][partition_number] [fs_type] (fs_type can be fat or fat32)\n");
return -1;
}
if (!OpenVolume(argv[1]))
{
printf("Exiting program...\n");
return -1;
}
if (!InstallBootSector(argv[2]))
{
printf("Exiting program...\n");
return -1;
}
CloseVolume();
printf("You must now copy freeldr.sys & freeldr.ini to %s.\n", argv[1]);
return 0;
}
bool InstallBootSector(char* lpszVolumeType)
{
unsigned char BootSectorBuffer[512];
if (!ReadVolumeSector(0, BootSectorBuffer))
{
return false;
}
if (strcasecmp(lpszVolumeType, "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(0, fat32_data))
{
return false;
}
//
// Write out new extra sector
//
if (!WriteVolumeSector(14, (fat32_data+512)))
{
return false;
}
}
else if (strcasecmp(lpszVolumeType, "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(0, fat_data))
{
return false;
}
}
else
{
printf("%s:%d: ", __FILE__, __LINE__);
printf("File system type %s unknown.\n", lpszVolumeType);
return false;
}
printf("%s boot sector installed.\n", lpszVolumeType);
return true;
}