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
12 #include "fatfs/diskio.h"
14 static FATFS g_Filesystem
;
15 static int isMounted
= 0;
16 static char buff
[32768];
18 // tool needed by fatfs
21 /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
22 /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
28 timeinfo
= localtime(&rawtime
);
33 DWORD Second
: 5; // div 2
38 DWORD Year
: 7; // year-1980
48 timeinfo
->tm_year
- 1980,
56 int is_command(const char* parg
)
59 return (parg
[0] == '/') || (parg
[0] == '-');
61 return (parg
[0] == '-');
65 #define NEED_PARAMS(_min_,_max_) \
67 if(nargs<_min_) { printf("Too few args for command %s.\n",argv[-1]); PRINT_HELP_AND_QUIT(); } \
68 if(nargs>_max_) { printf("Too many args for command %s.\n",argv[-1]); PRINT_HELP_AND_QUIT(); } \
78 r
= f_mount(&g_Filesystem
, "0:", 0);
86 #define NEED_MOUNT() \
87 do { ret = need_mount(); if(ret) \
89 printf("Error: could not mount disk (%d). \n", ret); \
90 PRINT_HELP_AND_QUIT(); \
93 void print_help(char const * const name
)
95 printf("Syntax: %s image_file [list of commands]\n\n", name
);
96 printf("Commands: [Note: both '/' and '-' are accepted as command prefixes.] \n");
97 printf(" /format <sectors> [<filesystem>] Formats the disk image.\n");
98 printf(" /boot <sector file> Writes a new boot sector.\n");
99 printf(" /add <src path> <dst path> Copies an external file or directory\n"
100 " into the image.\n");
101 printf(" /extract <src path> <dst path> Copies a file or directory from the image\n"
102 " into an external file or directory.\n");
103 printf(" /move <src path> <new path> Moves/renames a file or directory.\n");
104 printf(" /copy <src path> <new path> Copies a file or directory.\n");
105 printf(" /mkdir <src path> <new path> Creates a directory.\n");
106 printf(" /rmdir <src path> <new path> Creates a directory.\n");
107 printf(" /list [<pattern>] Lists files a directory (defaults to root).\n");
108 //printf(" /recursive Enables recursive processing for directories.\n");
111 #define PRINT_HELP_AND_QUIT() \
114 print_help(oargv[0]); \
118 int main(int oargc
, char* oargv
[])
121 int argc
= oargc
- 1;
122 char** argv
= oargv
+ 1;
124 // first parameter must be the image file.
127 PRINT_HELP_AND_QUIT();
130 if (is_command(argv
[0]))
132 printf("Error: first parameter must be a filename, found '%s' instead. \n", argv
[0]);
133 PRINT_HELP_AND_QUIT();
136 if (disk_openimage(0, argv
[0]))
138 printf("Error: could not open image file '%s'. \n", argv
[0]);
139 PRINT_HELP_AND_QUIT();
151 if (!is_command(parg
))
153 printf("Error: Expected a command, found '%s' instead. \n", parg
);
154 PRINT_HELP_AND_QUIT();
161 // find next command, to calculare number of args
162 while ((argv
[i
] != NULL
) && !is_command(argv
[i
++]))
165 if (strcmp(parg
, "format") == 0)
167 // NOTE: The fs driver detects which FAT format fits best based on size
172 // Arg 1: number of sectors
173 sectors
= atoi(argv
[0]);
177 printf("Error: Sectors must be > 0\n");
182 disk_ioctl(0, SET_SECTOR_COUNT
, §ors
);
186 ret
= f_mkfs("0:", 1, sectors
< 4096 ? 1 : 8);
189 printf("ERROR: Formatting drive: %d.\n", ret
);
190 PRINT_HELP_AND_QUIT();
193 else if (strcmp(parg
, "boot") == 0)
196 BYTE
* temp
= buff
+ 1024;
202 fe
= fopen(argv
[0], "rb");
206 printf("Error: unable to open external file '%s' for reading.", argv
[0]);
211 if(!fread(buff
, 512, 1, fe
))
213 printf("Error: unable to read boot sector from file '%s'.", argv
[0]);
220 if(disk_read(0, temp
, 0, 1))
222 printf("Error: unable to read existing boot sector from image.");
227 if (g_Filesystem
.fs_type
== FS_FAT32
)
229 printf("TODO: writing boot sectors for FAT32 images not yet supported.");
235 // Quick&dirty hardcoded length.
236 memcpy(buff
+ 2, temp
+ 2, 0x3E - 0x02);
239 if (disk_write(0, buff
, 0, 1))
241 printf("Error: unable to write new boot sector to image.");
248 else if (strcmp(parg
, "add") == 0)
259 // Arg 1: external file to add
260 // Arg 2: virtual filename
262 fe
= fopen(argv
[0], "rb");
266 printf("Error: unable to open external file '%s' for reading.", argv
[0]);
271 if (f_open(&fv
, argv
[1], FA_WRITE
| FA_CREATE_ALWAYS
))
273 printf("Error: unable to open file '%s' for writing.", argv
[1]);
279 while ((rdlen
= fread(buff
, 1, sizeof(buff
), fe
)) > 0)
281 f_write(&fv
, buff
, rdlen
, &wrlen
);
287 else if (strcmp(parg
, "extract") == 0)
298 // Arg 1: virtual file to extract
299 // Arg 2: external filename
301 if (f_open(&fe
, argv
[0], FA_READ
))
303 printf("Error: unable to open file '%s' for reading.", argv
[0]);
308 fv
= fopen(argv
[1], "wb");
312 printf("Error: unable to open external file '%s' for writing.", argv
[1]);
318 while ((f_read(&fe
, buff
, sizeof(buff
), &rdlen
) == 0) && (rdlen
> 0))
320 fwrite(buff
, 1, rdlen
, fv
);
326 else if (strcmp(parg
, "move") == 0)
331 // Arg 1: src path & filename
332 // Arg 2: new path & filename
334 if (f_rename(argv
[0], argv
[1]))
335 printf("Error moving/renaming '%s' to '%s'", argv
[0], argv
[1]);
337 else if (strcmp(parg
, "copy") == 0)
347 // Arg 1: src path & filename
348 // Arg 2: new path & filename
350 if (f_open(&fe
, argv
[0], FA_READ
))
352 printf("Error: unable to open file '%s' for reading.", argv
[0]);
356 if (f_open(&fv
, argv
[1], FA_WRITE
| FA_CREATE_ALWAYS
))
358 printf("Error: unable to open file '%s' for writing.", argv
[1]);
364 while ((f_read(&fe
, buff
, sizeof(buff
), &rdlen
) == 0) && (rdlen
> 0))
366 f_write(&fv
, buff
, rdlen
, &wrlen
);
372 else if (strcmp(parg
, "mkdir") == 0)
378 // Arg 1: folder path
381 else if (strcmp(parg
, "delete") == 0)
387 // Arg 1: file/folder path (cannot delete non-empty folders)
390 else if (strcmp(parg
, "list") == 0)
394 FILINFO info
= { 0 };
399 // Arg 1: folder path (optional)
406 if (f_opendir(&dir
, root
))
408 printf("Error opening directory '%s'.\n", root
);
413 printf("Listing directory contents of: %s\n", root
);
415 info
.lfname
= lfname
;
417 while ((!f_readdir(&dir
, &info
)) && (strlen(info
.fname
) > 0))
419 if (strlen(info
.lfname
) > 0)
420 printf(" - %s (%s)\n", info
.lfname
, info
.fname
);
422 printf(" - %s\n", info
.fname
);
427 printf("Error: Unknown or invalid command: %s\n", argv
[-1]);
428 PRINT_HELP_AND_QUIT();