-
Notifications
You must be signed in to change notification settings - Fork 1
/
genfatfs.c
447 lines (356 loc) · 9.12 KB
/
genfatfs.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// fat filesystem generator for embedded systems
// Copyright (C) 2012 Nathan Huizinga <nathan.huizinga@gmail.com>
//
// Please direct support requests to raspberrypi-openwrt-dev@googlegroups.com
//
// 'genext2fs' portions taken from genext2fs.c in getext2fs:
// Copyright (C) 2000 by Xavier Bestel <xavier.bestel@free.fr>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; version
// 2 of the License.
//
// Changes:
// 4 Jul 2012 Initial release
//
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#include <getopt.h>
#include <ff.h>
static char * app_name;
static char fileBuffer[64*1024];
extern void disk_fileimage_init(FILE * file);
DWORD get_fattime(void)
{
return 0;
}
static void verror_msg(const char *s, va_list p)
{
fflush(stdout);
fprintf(stderr, "%s: ", app_name);
vfprintf(stderr, s, p);
}
static void error_msg(const char *s, ...)
{
va_list p;
va_start(p, s);
verror_msg(s, p);
va_end(p);
putc('\n', stderr);
}
static void error_msg_and_die(const char *s, ...)
{
va_list p;
va_start(p, s);
verror_msg(s, p);
va_end(p);
putc('\n', stderr);
exit(EXIT_FAILURE);
}
static void vperror_msg(const char *s, va_list p)
{
int err = errno;
if (s == 0)
s = "";
verror_msg(s, p);
if (*s)
s = ": ";
fprintf(stderr, "%s%s\n", s, strerror(err));
}
static void perror_msg_and_die(const char *s, ...)
{
va_list p;
va_start(p, s);
vperror_msg(s, p);
va_end(p);
exit(EXIT_FAILURE);
}
// Convert a numerical string to a float, and multiply the result by an
// IEC or SI multiplier if provided; supported multipliers are Ki, Mi, Gi, k, M
// and G.
float SI_atof(const char *nptr)
{
float f = 0;
float m = 1;
char *suffixptr;
f = strtof(nptr, &suffixptr);
if (*suffixptr)
{
if (!strcmp(suffixptr, "Ki"))
m = 1 << 10;
else if (!strcmp(suffixptr, "Mi"))
m = 1 << 20;
else if (!strcmp(suffixptr, "Gi"))
m = 1 << 30;
else if (!strcmp(suffixptr, "k"))
m = 1000;
else if (!strcmp(suffixptr, "M"))
m = 1000 * 1000;
else if (!strcmp(suffixptr, "G"))
m = 1000 * 1000 * 1000;
}
return f * m;
}
static FILE * xfopen(const char *path, const char *mode)
{
FILE *fp;
if ((fp = fopen(path, mode)) == NULL)
perror_msg_and_die("%s", path);
return fp;
}
static void printDirLevel(dirLevel)
{
int i;
for (i = 0; i < dirLevel; i++)
{
printf("+");
}
printf(" ");
}
// Add a single file to the image
static void add2fs_file(const char * filename)
{
FILE * f_in;
FIL f_out;
size_t fileSize;
size_t readSize;
struct stat stbuf;
UINT bytesWritten;
// Open input file
f_in = xfopen(filename, "rb");
if (fstat(fileno(f_in), &stbuf) != 0)
perror_msg_and_die("Error reading file size of \"%s\"", filename);
// Create output file
if (f_open(&f_out, filename, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
perror_msg_and_die("Error creating \"%s\" in image", filename);
// Copy contents
fileSize = stbuf.st_size;
while (fileSize > 0)
{
if (fileSize > sizeof(fileBuffer))
{
readSize = sizeof(fileBuffer);
}
else
{
readSize = fileSize;
}
if (fread(fileBuffer, readSize, 1, f_in) != 1)
perror_msg_and_die("Error reading from file \"%s\"", filename);
if (f_write(&f_out, fileBuffer, readSize, &bytesWritten) != FR_OK)
perror_msg_and_die("Error writing to file \"%s\" in image", filename);
if (bytesWritten != readSize)
perror_msg_and_die("Error writing to file \"%s\" in image (full?!?)", filename);
fileSize -= readSize;
}
// Close input/output files
if (f_close(&f_out) != FR_OK)
perror_msg_and_die("Error closing file \"%s\" in image", filename);
fclose(f_in);
}
// Add a tree of entries to the image from the current directory
static void add2fs_from_dir(int verbose, int dirLevel)
{
DIR *dh;
struct dirent *dent;
struct stat st;
if ((dh = opendir(".")) == NULL)
perror_msg_and_die(".");
while ((dent = readdir(dh)) != NULL)
{
if ((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, "..")))
continue;
lstat(dent->d_name, &st);
switch(st.st_mode & S_IFMT)
{
case S_IFLNK:
// Handle symbolic links as normal files
case S_IFREG:
if (verbose)
{
printDirLevel(dirLevel);
printf("Adding file: %s\n", dent->d_name);
}
add2fs_file(dent->d_name);
break;
case S_IFDIR:
if (verbose)
{
printDirLevel(dirLevel);
printf("Adding dir: %s\n", dent->d_name);
}
if (f_mkdir(dent->d_name) != FR_OK)
perror_msg_and_die("Error creating directory \"%s\" in image", dent->d_name);
// Change to directory one level deeper
if (chdir(dent->d_name) < 0)
perror_msg_and_die(dent->d_name);
if (f_chdir(dent->d_name) != FR_OK)
perror_msg_and_die("Error chdir in image to: %s", dent->d_name);
// Add sub-directory recursive
add2fs_from_dir(verbose, dirLevel + 1);
// Change to directory one level up
chdir("..");
if (f_chdir("..") != FR_OK)
perror_msg_and_die("Error chdir in image to: ..");
break;
default:
error_msg("ignoring entry %s", dent->d_name);
break;
}
}
closedir(dh);
}
static void populate_fs(char **dopt, int didx, int verbose)
{
int i;
char * pdest;
struct stat st;
int pdir;
for (i = 0; i < didx; i++)
{
// If a [:path] is given, change to that directory in the image.
if((pdest = strchr(dopt[i], ':')))
{
*(pdest++) = 0;
if (f_chdir(pdest) != FR_OK)
error_msg_and_die("path %s not found in file system", pdest);
}
stat(dopt[i], &st);
switch(st.st_mode & S_IFMT)
{
case S_IFREG:
error_msg_and_die("Adding from file %s is not supported", dopt[i]);
break;
case S_IFDIR:
if ((pdir = open(".", O_RDONLY)) < 0)
perror_msg_and_die(".");
if (chdir(dopt[i]) < 0)
perror_msg_and_die(dopt[i]);
add2fs_from_dir(verbose, 1);
if (fchdir(pdir) < 0)
perror_msg_and_die("fchdir");
if (close(pdir) < 0)
perror_msg_and_die("close");
break;
default:
error_msg_and_die("%s is neither a file nor a directory", dopt[i]);
}
}
}
// TODO: add version info in cmake file?
#define VERSION "0.2"
static void showversion(void)
{
printf("genfatfs " VERSION "\n");
printf("Using FatFs version R0.09 (http://elm-chan.org/fsw/ff/00index_e.html)\n");
}
static void showhelp(void)
{
fprintf(stderr, "Usage: %s [options] image\n"
"Create a fat file system image from directories/files\n\n"
" -d, --root <directory>\n"
" -b, --size-in-blocks <blocks>\n"
" -h, --help\n"
" -V, --version\n"
" -v, --verbose\n\n"
"Report bugs to raspberrypi-openwrt-dev@googlegroups.com\n", app_name);
}
#define MAX_DOPT 128
int main(int argc, char ** argv)
{
int nbblocks = -1;
char * fsout = "-";
char * dopt[MAX_DOPT];
int didx = 0;
int c;
int verbose = 0;
FATFS Fatfs; /* File system object */
FILE * fp = NULL;
struct option longopts[] = {
{ "root", required_argument, NULL, 'd' },
{ "size-in-blocks", required_argument, NULL, 'b' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ "verbose", no_argument, NULL, 'v' },
{ 0, 0, 0, 0}
} ;
app_name = argv[0];
while ((c = getopt_long(argc, argv, "x:d:b:hVv", longopts, NULL)) != EOF)
{
switch(c)
{
case 'd':
dopt[didx++] = optarg;
break;
case 'b':
nbblocks = SI_atof(optarg);
break;
case 'h':
showhelp();
exit(0);
break;
case 'V':
showversion();
exit(0);
break;
case 'v':
verbose = 1;
showversion();
break;
default:
error_msg_and_die("Note: options have changed, see --help or the man page.");
break;
}
}
if (optind < (argc - 1))
error_msg_and_die("Too many arguments. Try --help.");
if (optind > (argc - 1))
error_msg_and_die("Not enough arguments. Try --help.");
fsout = argv[optind];
// No input file, create one
int fileSize = nbblocks * 1024;
if (nbblocks < 8)
error_msg_and_die("Too few blocks, see --help.");
// Create empty file
fp = xfopen(fsout, "w+b");
if (verbose)
{
printf("Creating: \"%s\" with size: %d bytes\n", fsout, fileSize);
}
// Make it the correct size
if (ftruncate(fileno(fp), fileSize))
perror_msg_and_die("Error creating new file: %s", fsout);
disk_fileimage_init(fp);
// Register volume work area (never fails)
f_mount(0, &Fatfs);
// Make file system in the newly created image
if (verbose)
{
printf("Creating fat file system\n");
}
// Create file system at the beginning of the image
if (f_mkfs(0, 1, 0) != FR_OK)
perror_msg_and_die("Error making fat file system");
if (verbose)
{
printf("Populating fat file system. Please wait...\n");
}
populate_fs(dopt, didx, verbose);
if (verbose)
{
printf("Done\n");
}
// Unregister volume work area
f_mount(0, NULL);
// Close image file
fclose(fp);
return 0;
}