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"
20 // tool needed by fatfs
23 /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
24 /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
30 timeinfo
= localtime(&rawtime
);
35 DWORD Second
: 5; // div 2
40 DWORD Year
: 7; // year-1980
50 timeinfo
->tm_year
- 1980,
58 int is_command(const char* parg
)
60 return (parg
[0] == '/') || (parg
[0] == '-');
63 #define NEED_PARAMS(_min_,_max_) \
65 if(nargs<_min_) { printf("Too few args for command %s.\n",argv[-1]); goto print_help; } \
66 if(nargs>_max_) { printf("Too many args for command %s.\n",argv[-1]); goto print_help; } \
76 r
= f_mount(&g_Filesystem
, "0:", 0);
84 #define NEED_MOUNT() \
85 do { ret = need_mount(); if(ret) \
87 printf("Error: could not mount image file '%s' (%d). \n", imageFileName, ret); \
91 int main(int oargc
, char* oargv
[])
95 char** argv
= oargv
+ 1;
97 // first parameter must be the image file.
103 if (is_command(argv
[0]))
105 printf("Error: first parameter must be a filename, found '%s' instead. \n", argv
[0]);
109 imageFileName
= argv
[0];
111 if (disk_initialize(0))
113 printf("Error: could not open image file '%s'. \n", imageFileName
);
126 if (!is_command(parg
))
128 printf("Error: Expected a command, found '%s' instead. \n", parg
);
136 // find next command, to calculare number of args
137 while ((argv
[i
] != NULL
) && !is_command(argv
[i
++]))
140 if (strcmp(parg
, "format") == 0)
142 // NOTE: The fs driver detects which FAT format fits best based on size
149 // Arg 1: number of sectors
150 sectors
= atoi(argv
[0]);
154 printf("Error: Sectors must be > 0\n");
158 disk_ioctl(0, SET_SECTOR_COUNT
, §ors
);
160 ret
= f_mkfs("0:", 1, 4096);
163 printf("ERROR: Formatting drive: %d.\n", ret
);
167 else if (strcmp(parg
, "boot") == 0)
172 printf("Not Implemented.");
174 else if (strcmp(parg
, "add") == 0)
186 // Arg 1: external file to add
187 // Arg 2: virtual filename
189 fe
= fopen(argv
[0], "rb");
193 printf("Error: unable to open external file '%s' for reading.", argv
[0]);
197 if (f_open(&fv
, argv
[1], FA_WRITE
| FA_CREATE_ALWAYS
))
199 printf("Error: unable to open file '%s' for writing.", argv
[1]);
203 while ((rdlen
= fread(buff
, 1, 32768, fe
)) > 0)
205 f_write(&fv
, buff
, rdlen
, &wrlen
);
211 else if (strcmp(parg
, "extract") == 0)
223 // Arg 1: virtual file to extract
224 // Arg 2: external filename
226 if (f_open(&fe
, argv
[0], FA_READ
))
228 printf("Error: unable to open file '%s' for reading.", argv
[0]);
232 fv
= fopen(argv
[1], "wb");
236 printf("Error: unable to open external file '%s' for writing.", argv
[1]);
240 while ((f_read(&fe
, buff
, 32768, &rdlen
) == 0) && (rdlen
> 0))
242 fwrite(buff
, 1, rdlen
, fv
);
248 else if (strcmp(parg
, "move") == 0)
253 // Arg 1: src path & filename
254 // Arg 2: new path & filename
256 if (f_rename(argv
[0], argv
[1]))
257 printf("Error moving/renaming '%s' to '%s'", argv
[0], argv
[1]);
259 else if (strcmp(parg
, "copy") == 0)
270 // Arg 1: src path & filename
271 // Arg 2: new path & filename
273 if (f_open(&fe
, argv
[0], FA_READ
))
275 printf("Error: unable to open file '%s' for reading.", argv
[0]);
278 if (f_open(&fv
, argv
[1], FA_WRITE
| FA_CREATE_ALWAYS
))
280 printf("Error: unable to open file '%s' for writing.", argv
[1]);
284 while ((f_read(&fe
, buff
, 32768, &rdlen
) == 0) && (rdlen
> 0))
286 f_write(&fv
, buff
, rdlen
, &wrlen
);
292 else if (strcmp(parg
, "mkdir") == 0)
298 // Arg 1: folder path
301 else if (strcmp(parg
, "delete") == 0)
307 // Arg 1: file/folder path (cannot delete non-empty folders)
310 else if (strcmp(parg
, "list") == 0)
314 FILINFO info
= { 0 };
319 // Arg 1: folder path (optional)
326 if (f_opendir(&dir
, root
))
328 printf("Error opening directory '%s'.\n", root
);
332 printf("Listing directory contents of: %s\n", root
);
334 info
.lfname
= lfname
;
336 while ((!f_readdir(&dir
, &info
)) && (strlen(info
.fname
) > 0))
338 if (strlen(info
.lfname
) > 0)
339 printf(" - %s (%s)\n", info
.lfname
, info
.fname
);
341 printf(" - %s\n", info
.fname
);
346 printf("Error: Unknown or invalid command: %s\n", argv
[-1]);
356 printf("Syntax: %s image_file [list of commands]\n\n", oargv
[0]);
357 printf("Commands: [Note: both '/' and '-' are accepted as command prefixes.] \n");
358 printf(" /format <sectors> [<filesystem>] Formats the disk image.\n");
359 printf(" /boot <sector file> Writes a new boot sector.\n");
360 printf(" /add <src path> <dst path> Copies an external file or directory\n"
361 " into the image.\n");
362 printf(" /extract <src path> <dst path> Copies a file or directory from the image\n"
363 " into an external file or directory.\n");
364 printf(" /move <src path> <new path> Moves/renames a file or directory.\n");
365 printf(" /copy <src path> <new path> Copies a file or directory.\n");
366 printf(" /mkdir <src path> <new path> Creates a directory.\n");
367 printf(" /rmdir <src path> <new path> Creates a directory.\n");
368 printf(" /list [<pattern>] Lists files a directory (defaults to root).\n");
369 //printf(" /recursive Enables recursive processing for directories.\n");