[FATTEN]
[reactos.git] / reactos / tools / fatten / fatten.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS FAT Image Creator
4 * FILE: tools/fatten/fatten.c
5 * PURPOSE: FAT Image Creator (for EFI Boot)
6 * PROGRAMMERS: David Quintana
7 */
8 #include <stdio.h>
9 #include <string.h>
10 #include <time.h>
11 #include "fatfs/ff.h"
12 #include "fatfs/diskio.h"
13
14 FATFS g_Filesystem;
15
16 static int isMounted = 0;
17 static char buff[32768];
18
19 // tool needed by fatfs
20 DWORD get_fattime()
21 {
22 /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
23 /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
24
25 time_t rawtime;
26 struct tm * timeinfo;
27
28 time(&rawtime);
29 timeinfo = localtime(&rawtime);
30
31 {
32 union FatTime {
33 struct {
34 DWORD Second : 5; // div 2
35 DWORD Minute : 6;
36 DWORD Hour : 5;
37 DWORD Day : 5;
38 DWORD Month : 4;
39 DWORD Year : 7; // year-1980
40 };
41 DWORD whole;
42 } myTime = {
43 {
44 timeinfo->tm_sec / 2,
45 timeinfo->tm_min,
46 timeinfo->tm_hour,
47 timeinfo->tm_mday,
48 timeinfo->tm_mon,
49 timeinfo->tm_year - 1980,
50 }
51 };
52
53 return myTime.whole;
54 }
55 }
56
57 int is_command(const char* parg)
58 {
59 #if _WIN32
60 return (parg[0] == '/') || (parg[0] == '-');
61 #else
62 return (parg[0] == '-');
63 #endif
64 }
65
66 #define NEED_PARAMS(_min_,_max_) \
67 do {\
68 if(nargs<_min_) { printf("Too few args for command %s.\n",argv[-1]); PRINT_HELP_AND_QUIT(); } \
69 if(nargs>_max_) { printf("Too many args for command %s.\n",argv[-1]); PRINT_HELP_AND_QUIT(); } \
70 } while(0)
71
72 int need_mount()
73 {
74 int r;
75
76 if (isMounted)
77 return FR_OK;
78
79 r = f_mount(&g_Filesystem, "0:", 0);
80 if (r)
81 return r;
82
83 isMounted = 1;
84 return FR_OK;
85 }
86
87 #define NEED_MOUNT() \
88 do { ret = need_mount(); if(ret) \
89 {\
90 printf("Error: could not mount disk (%d). \n", ret); \
91 PRINT_HELP_AND_QUIT(); \
92 } } while(0)
93
94 void print_help(char const * const name)
95 {
96 printf("Syntax: %s image_file [list of commands]\n\n", name);
97 printf("Commands: [Note: both '/' and '-' are accepted as command prefixes.] \n");
98 printf(" /format <sectors> [<filesystem>] Formats the disk image.\n");
99 printf(" /boot <sector file> Writes a new boot sector.\n");
100 printf(" /add <src path> <dst path> Copies an external file or directory\n"
101 " into the image.\n");
102 printf(" /extract <src path> <dst path> Copies a file or directory from the image\n"
103 " into an external file or directory.\n");
104 printf(" /move <src path> <new path> Moves/renames a file or directory.\n");
105 printf(" /copy <src path> <new path> Copies a file or directory.\n");
106 printf(" /mkdir <src path> <new path> Creates a directory.\n");
107 printf(" /rmdir <src path> <new path> Creates a directory.\n");
108 printf(" /list [<pattern>] Lists files a directory (defaults to root).\n");
109 //printf(" /recursive Enables recursive processing for directories.\n");
110 }
111
112 #define PRINT_HELP_AND_QUIT() \
113 do { \
114 ret = 1; \
115 print_help(oargv[0]); \
116 goto exit; \
117 } while (0)
118
119 int main(int oargc, char* oargv[])
120 {
121 int ret;
122 int argc = oargc - 1;
123 char** argv = oargv + 1;
124
125 // first parameter must be the image file.
126 if (argc == 0)
127 {
128 PRINT_HELP_AND_QUIT();
129 }
130
131 if (is_command(argv[0]))
132 {
133 printf("Error: first parameter must be a filename, found '%s' instead. \n", argv[0]);
134 PRINT_HELP_AND_QUIT();
135 }
136
137 if (disk_openimage(0, argv[0]))
138 {
139 printf("Error: could not open image file '%s'. \n", argv[0]);
140 PRINT_HELP_AND_QUIT();
141 }
142
143 argc--;
144 argv++;
145
146 while (argc > 0)
147 {
148 char *parg = *argv;
149 int nargs = 0;
150 int i = 0;
151
152 if (!is_command(parg))
153 {
154 printf("Error: Expected a command, found '%s' instead. \n", parg);
155 PRINT_HELP_AND_QUIT();
156 }
157
158 parg++;
159 argv++;
160 argc--;
161
162 // find next command, to calculare number of args
163 while ((argv[i] != NULL) && !is_command(argv[i++]))
164 nargs++;
165
166 if (strcmp(parg, "format") == 0)
167 {
168 // NOTE: The fs driver detects which FAT format fits best based on size
169 int sectors;
170
171 NEED_PARAMS(1, 1);
172
173 // Arg 1: number of sectors
174 sectors = atoi(argv[0]);
175
176 if (sectors <= 0)
177 {
178 printf("Error: Sectors must be > 0\n");
179 ret = 1;
180 goto exit;
181 }
182
183 disk_ioctl(0, SET_SECTOR_COUNT, &sectors);
184
185 NEED_MOUNT();
186
187 ret = f_mkfs("0:", 1, sectors < 4096 ? 1 : 8);
188 if (ret)
189 {
190 printf("ERROR: Formatting drive: %d.\n", ret);
191 PRINT_HELP_AND_QUIT();
192 }
193 }
194 else if (strcmp(parg, "boot") == 0)
195 {
196 NEED_PARAMS(1, 1);
197
198 // Arg 1: boot file
199 printf("Not Implemented.");
200 }
201 else if (strcmp(parg, "add") == 0)
202 {
203 FILE* fe;
204 FIL fv = { 0 };
205 UINT rdlen = 0;
206 UINT wrlen = 0;
207
208 NEED_PARAMS(2, 2);
209
210 NEED_MOUNT();
211
212 // Arg 1: external file to add
213 // Arg 2: virtual filename
214
215 fe = fopen(argv[0], "rb");
216
217 if (!fe)
218 {
219 printf("Error: unable to open external file '%s' for reading.", argv[0]);
220 ret = 1;
221 goto exit;
222 }
223
224 if (f_open(&fv, argv[1], FA_WRITE | FA_CREATE_ALWAYS))
225 {
226 printf("Error: unable to open file '%s' for writing.", argv[1]);
227 fclose(fe);
228 ret = 1;
229 goto exit;
230 }
231
232 while ((rdlen = fread(buff, 1, 32768, fe)) > 0)
233 {
234 f_write(&fv, buff, rdlen, &wrlen);
235 }
236
237 fclose(fe);
238 f_close(&fv);
239 }
240 else if (strcmp(parg, "extract") == 0)
241 {
242 FIL fe = { 0 };
243 FILE* fv;
244 UINT rdlen = 0;
245 UINT wrlen = 0;
246
247 NEED_PARAMS(2, 2);
248
249 NEED_MOUNT();
250
251 // Arg 1: virtual file to extract
252 // Arg 2: external filename
253
254 if (f_open(&fe, argv[0], FA_READ))
255 {
256 printf("Error: unable to open file '%s' for reading.", argv[0]);
257 ret = 1;
258 goto exit;
259 }
260
261 fv = fopen(argv[1], "wb");
262
263 if (!fv)
264 {
265 printf("Error: unable to open external file '%s' for writing.", argv[1]);
266 f_close(&fe);
267 ret = 1;
268 goto exit;
269 }
270
271 while ((f_read(&fe, buff, 32768, &rdlen) == 0) && (rdlen > 0))
272 {
273 fwrite(buff, 1, rdlen, fv);
274 }
275
276 f_close(&fe);
277 fclose(fv);
278 }
279 else if (strcmp(parg, "move") == 0)
280 {
281 NEED_PARAMS(2, 2);
282
283 NEED_MOUNT();
284 // Arg 1: src path & filename
285 // Arg 2: new path & filename
286
287 if (f_rename(argv[0], argv[1]))
288 printf("Error moving/renaming '%s' to '%s'", argv[0], argv[1]);
289 }
290 else if (strcmp(parg, "copy") == 0)
291 {
292 FIL fe = { 0 };
293 FIL fv = { 0 };
294 UINT rdlen = 0;
295 UINT wrlen = 0;
296
297 NEED_PARAMS(2, 2);
298
299 NEED_MOUNT();
300 // Arg 1: src path & filename
301 // Arg 2: new path & filename
302
303 if (f_open(&fe, argv[0], FA_READ))
304 {
305 printf("Error: unable to open file '%s' for reading.", argv[0]);
306 ret = 1;
307 goto exit;
308 }
309 if (f_open(&fv, argv[1], FA_WRITE | FA_CREATE_ALWAYS))
310 {
311 printf("Error: unable to open file '%s' for writing.", argv[1]);
312 f_close(&fe);
313 ret = 1;
314 goto exit;
315 }
316
317 while ((f_read(&fe, buff, 32768, &rdlen) == 0) && (rdlen > 0))
318 {
319 f_write(&fv, buff, rdlen, &wrlen);
320 }
321
322 f_close(&fe);
323 f_close(&fv);
324 }
325 else if (strcmp(parg, "mkdir") == 0)
326 {
327 NEED_PARAMS(1, 1);
328
329 NEED_MOUNT();
330
331 // Arg 1: folder path
332 f_mkdir(argv[0]);
333 }
334 else if (strcmp(parg, "delete") == 0)
335 {
336 NEED_PARAMS(1, 1);
337
338 NEED_MOUNT();
339
340 // Arg 1: file/folder path (cannot delete non-empty folders)
341 f_unlink(argv[0]);
342 }
343 else if (strcmp(parg, "list") == 0)
344 {
345 char* root = "/";
346 DIR dir = { 0 };
347 FILINFO info = { 0 };
348 char lfname[257];
349
350 NEED_PARAMS(0, 1);
351
352 // Arg 1: folder path (optional)
353
354 if (nargs == 1)
355 {
356 root = argv[0];
357 }
358
359 if (f_opendir(&dir, root))
360 {
361 printf("Error opening directory '%s'.\n", root);
362 ret = 1;
363 goto exit;
364 }
365
366 printf("Listing directory contents of: %s\n", root);
367
368 info.lfname = lfname;
369 info.lfsize = 256;
370 while ((!f_readdir(&dir, &info)) && (strlen(info.fname) > 0))
371 {
372 if (strlen(info.lfname) > 0)
373 printf(" - %s (%s)\n", info.lfname, info.fname);
374 else
375 printf(" - %s\n", info.fname);
376 }
377 }
378 else
379 {
380 printf("Error: Unknown or invalid command: %s\n", argv[-1]);
381 PRINT_HELP_AND_QUIT();
382 }
383 argv += nargs;
384 argc -= nargs;
385 }
386
387 ret = 0;
388
389 exit:
390
391 disk_cleanup(0);
392
393 return ret;
394 }
395