This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genfat.c
298 lines (264 loc) · 8.74 KB
/
genfat.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#define IMAGE_SIZE (1 << 24) // 16 megabytes
#define IMAGE_SECTORS (IMAGE_SIZE / 512)
#define SECTORS_PER_CLUSTER 4
#define CLUSTER_SIZE (SECTORS_PER_CLUSTER * 512)
#define ROOT_DIR_SECTORS 64
#define ROOT_DIR_ENTRIES (ROOT_DIR_SECTORS * 16)
#define FAT_SECTORS (((IMAGE_SECTORS / SECTORS_PER_CLUSTER + 2) * 2 + 511) / 512)
#define FAT_SIZE (FAT_SECTORS * 512)
#define FIRST_ROOT_DIR_SECTOR (1 + 2 * FAT_SECTORS)
#define FIRST_DATA_SECTOR (FIRST_ROOT_DIR_SECTOR + ROOT_DIR_SECTORS)
#define DATA_SECTORS (IMAGE_SECTORS - FIRST_DATA_SECTOR)
#define DATA_CLUSTERS (DATA_SECTORS / SECTORS_PER_CLUSTER)
void error(const char *message)
{
fprintf(stderr, "Error: %s\n", message);
exit(1);
}
void *xmalloc(size_t size)
{
char *buf = malloc(size);
if (!buf) error("Out of memory");
for (size_t i = 0; i < size; i++) buf[i] = '\0';
return buf;
}
uint8_t *image;
void image_write8(int offset, uint8_t value)
{
image[offset] = value;
}
void image_write16(int offset, uint16_t value)
{
image_write8(offset, value & 0xFF);
image_write8(offset + 1, (value >> 8) & 0xFF);
}
void image_write32(int offset, uint32_t value)
{
image_write8(offset, value & 0xFF);
image_write8(offset + 1, (value >> 8) & 0xFF);
image_write8(offset + 2, (value >> 16) & 0xFF);
image_write8(offset + 3, (value >> 24) & 0xFF);
}
void image_writestr(int offset, const char *s)
{
for (int i = 0; s[i]; i++) {
image_write8(offset + i, s[i]);
}
}
void generate_boot_record()
{
image_write8(0, 0xEB); // instruction for an infinite loop
image_write8(1, 0xFE); // (our partition is not bootable)
image_write8(2, 0x90);
image_writestr(3, "BOOT "); // OEM string (8 characters)
image_write16(11, 512); // bytes / sector
image_write8(13, SECTORS_PER_CLUSTER);
image_write16(14, 1); // reserved sectors
image_write8(16, 2); // # FATs
image_write16(17, ROOT_DIR_ENTRIES);
if (IMAGE_SECTORS >= 65536) {
image_write16(19, 0);
image_write32(32, IMAGE_SECTORS);
} else {
image_write16(19, IMAGE_SECTORS);
image_write32(32, 0);
}
image_write8(21, 0xF8); // media descriptor
image_write16(22, FAT_SECTORS);
image_write16(24, 0); // fields we don't know/care about
image_write16(26, 0);
image_write32(28, 0);
// Extended Boot Record
image_write8(36, 0); // drive number -- we don't know!
image_write8(37, 0); // reserved
image_write8(38, 0x29); // extended boot signature
image_write32(39, 0); // serial number
image_writestr(43, "NO NAME "); // label string (11 characters)
image_writestr(54, "FAT16 "); // filesystem type (8 characters)
image_write16(510, 0xAA55); // boot signature
}
uint8_t *fat_buffer;
int next_cluster = 2;
void init_fat()
{
fat_buffer = xmalloc(FAT_SIZE);
fat_buffer[0] = 0xF8;
fat_buffer[1] = fat_buffer[2] = fat_buffer[3] = 0xFF;
}
void fat_write(uint16_t index, uint16_t value)
{
fat_buffer[2 * index] = value & 0xFF;
fat_buffer[2 * index + 1] = (value >> 8) & 0xFF;
}
uint16_t allocate_cluster(uint16_t prev)
{
if (next_cluster - 2 >= DATA_CLUSTERS) {
error("Out of space on image");
}
if (prev) {
fat_write(prev, next_cluster);
}
fat_write(next_cluster, 0xFFFF);
return next_cluster++;
}
void write_cluster(uint16_t cluster, void *buffer)
{
int offset = FIRST_DATA_SECTOR * 512 + (cluster - 2) * CLUSTER_SIZE;
memcpy(image + offset, buffer, CLUSTER_SIZE);
}
void writeout_fat()
{
memcpy(image + 512, fat_buffer, FAT_SIZE);
memcpy(image + 512 + FAT_SIZE, fat_buffer, FAT_SIZE);
}
int next_root_entry = 0;
int allocate_root_entry()
{
if (next_root_entry >= ROOT_DIR_ENTRIES)
error("Out of root directory entries");
return next_root_entry++;
}
void generate_lfn_entry(const char *name, uint8_t seqnum)
{
int entry = allocate_root_entry();
int offset = FIRST_ROOT_DIR_SECTOR * 512 + entry * 32;
image_write8(offset, seqnum);
bool reached_end = false;
for (int i = 0; i < 5; i++) {
if (reached_end) {
image_write16(offset + 1 + 2*i, 0xFFFF);
} else {
image_write16(offset + 1 + 2*i, name[i]);
if (!name[i]) reached_end = true;
}
}
image_write8(offset + 11, 0x0F); // attributes
image_write8(offset + 12, 0); // type
image_write8(offset + 13, 0); // DOS filename checksum, but we don't use it...
for (int i = 5; i < 11; i++) {
if (reached_end) {
image_write16(offset + 14 + 2*(i-5), 0xFFFF);
} else {
image_write16(offset + 14 + 2*(i-5), name[i]);
if (!name[i]) reached_end = true;
}
}
image_write16(offset + 26, 0);
for (int i = 11; i < 13; i++) {
if (reached_end) {
image_write16(offset + 28 + 2*(i-11), 0xFFFF);
} else {
image_write16(offset + 28 + 2*(i-11), name[i]);
if (!name[i]) reached_end = true;
}
}
}
void generate_reg_entry(int file_size, uint16_t first_cluster)
{
int entry = allocate_root_entry();
int offset = FIRST_ROOT_DIR_SECTOR * 512 + entry * 32;
time_t t = time(NULL);
struct tm *curtime = localtime(&t);
uint16_t date_field = ((curtime->tm_year + 1900 - 1980) << 9) |
((curtime->tm_mon + 1) << 5) | (curtime->tm_mday);
uint16_t time_field = (curtime->tm_hour << 11) |
(curtime->tm_min << 5) | (curtime->tm_sec / 2);
image_writestr(offset, " "); // 8.3 name, we don't use!
image_write8(offset + 11, 0); // attributes
image_write8(offset + 12, 0); // reserved
// fractional part of the time
image_write8(offset + 13, ((curtime->tm_sec) % 2) * 100);
image_write16(offset + 14, time_field);
image_write16(offset + 16, date_field);
image_write16(offset + 18, date_field);
image_write16(offset + 20, 0); // high part of cluster #
image_write16(offset + 22, time_field);
image_write16(offset + 24, date_field);
image_write16(offset + 26, first_cluster);
image_write32(offset + 28, file_size);
}
void generate_root_entry(const char *name, int file_size, uint16_t first_cluster)
{
int namelen = strlen(name);
if (namelen >= 256) error("File name too long");
if (!namelen) error("File name is empty");
bool first = true;
for (int i = (namelen - 1) / 13; i >= 0; i--) {
generate_lfn_entry(name + i * 13, (i + 1) | (first ? 0x40 : 0));
first = false;
}
generate_reg_entry(file_size, first_cluster);
}
void add_file(FILE *file, const char *name)
{
if (fseek(file, 0, SEEK_END)) error("fseek failed");
int file_size = ftell(file);
if (file_size == EOF) error("ftell failed");
rewind(file);
if (file_size == 0) {
generate_root_entry(name, 0, 0); return;
}
uint16_t first_cluster = 0;
uint16_t cur_cluster = 0;
int num_clusters = (file_size + CLUSTER_SIZE - 1) / CLUSTER_SIZE;
void *buf = xmalloc(CLUSTER_SIZE);
for (int i = 0; i < num_clusters; i++) {
int expected_length = (i == num_clusters - 1 ?
file_size % CLUSTER_SIZE : CLUSTER_SIZE);
if (expected_length == 0) expected_length = CLUSTER_SIZE;
cur_cluster = allocate_cluster(cur_cluster);
if (!i) first_cluster = cur_cluster;
if (!fread(buf, expected_length, 1, file))
error("fread failed");
if (expected_length < CLUSTER_SIZE) {
for (int j = expected_length; j < CLUSTER_SIZE; j++) {
((char *)buf)[j] = '\0';
}
}
write_cluster(cur_cluster, buf);
}
generate_root_entry(name, file_size, first_cluster);
}
void writeout_image(const char *path)
{
FILE* file = fopen(path, "wb");
if (!file) error("Failed to open image file");
if (!fwrite(image, IMAGE_SIZE, 1, file))
error("Failed to write to image file");
fclose(file);
}
int main(int argc, char **argv)
{
if (argc < 2) {
error("Usage: genfat image [files...]");
}
if (DATA_CLUSTERS < 4085 || DATA_CLUSTERS >= 65525) {
error("Disk size not appropriate for FAT16");
}
image = xmalloc(IMAGE_SIZE);
generate_boot_record();
init_fat();
for (int arg = 2; arg < argc; arg++) {
const char *path = argv[arg];
const char *filename = path;
for (int i = 0; path[i]; i++) {
if (path[i] == '/' || path[i] == '\\')
filename = path + i + 1;
}
FILE *file = fopen(path, "rb");
fprintf(stderr, "Adding file %s...\n", path);
if (!file) error("Failed to open file");
add_file(file, filename);
fclose(file);
}
writeout_fat();
writeout_image(argv[1]);
return 0;
}