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
);
34 DWORD Second
: 5; // div 2
39 DWORD Year
: 7; // year-1980
49 timeinfo
->tm_year
- 1980,
56 BOOL
is_command(const char* parg
)
58 return (parg
[0]=='/') || (parg
[0] == '-');
61 #define NEED_PARAMS(_min_,_max_) \
63 if(nargs<_min_) { printf("Too few args for command %s.\n",argv[-1]); goto print_help; } \
64 if(nargs>_max_) { printf("Too many args for command %s.\n",argv[-1]); goto print_help; } \
72 int r
= f_mount(&g_Filesystem
, "0:", 0);
80 #define NEED_MOUNT() \
81 do { ret = need_mount(); if(ret) \
83 printf("Error: could not mount image file '%s' (%d). \n", imageFileName, ret); \
87 int main(int oargc
, char* oargv
[])
91 char** argv
= oargv
+1;
93 // first parameter must be the image file.
99 if(is_command(argv
[0]))
101 printf("Error: first parameter must be a filename, found '%s' instead. \n", argv
[0]);
105 imageFileName
= argv
[0];
107 if(disk_initialize(0))
109 printf("Error: could not open image file '%s'. \n", imageFileName
);
120 if(!is_command(parg
))
122 printf("Error: Expected a command, found '%s' instead. \n",parg
);
130 // find next command, to calculare number of args
133 while((argv
[i
] != NULL
) && !is_command(argv
[i
++]))
136 if(strcmp(parg
,"format")==0)
138 // NOTE: The fs driver detects which FAT format fits best based on size
144 // Arg 1: number of sectors
145 int sectors
= atoi(argv
[0]);
149 printf("Error: Sectors must be > 0\n");
153 disk_ioctl(0, SET_SECTOR_COUNT
, §ors
);
155 ret
= f_mkfs("0:", 1, 4096);
158 printf("ERROR: Formatting drive: %d.\n", ret
);
162 else if(strcmp(parg
,"boot")==0)
167 printf("Not Implemented.");
169 else if(strcmp(parg
,"add")==0)
175 // Arg 1: external file to add
176 // Arg 2: virtual filename
181 if(fopen_s(&fe
, argv
[0],"rb"))
183 printf("Error: unable to open external file '%s' for reading.", argv
[0]);
186 if(f_open(&fv
, argv
[1], FA_WRITE
| FA_CREATE_ALWAYS
))
188 printf("Error: unable to open file '%s' for writing.", argv
[1]);
196 while( (rdlen
= fread(buff
,1,32768,fe
)) > 0 )
198 f_write(&fv
,buff
, rdlen
, &wrlen
);
204 else if(strcmp(parg
,"extract")==0)
210 // Arg 1: virtual file to extract
211 // Arg 2: external filename
216 if(f_open(&fe
, argv
[0], FA_READ
))
218 printf("Error: unable to open file '%s' for reading.", argv
[0]);
221 if(fopen_s(&fv
, argv
[1],"wb"))
223 printf("Error: unable to open external file '%s' for writing.", argv
[1]);
231 while( (f_read(&fe
, buff
, 32768, &rdlen
) == 0 ) && ( rdlen
> 0) )
233 fwrite(buff
, 1, rdlen
, fv
);
239 else if(strcmp(parg
,"move")==0)
244 // Arg 1: src path & filename
245 // Arg 2: new path & filename
247 if(f_rename(argv
[0], argv
[1]))
248 printf("Error moving/renaming '%s' to '%s'", argv
[0],argv
[1]);
250 else if(strcmp(parg
,"copy")==0)
255 // Arg 1: src path & filename
256 // Arg 2: new path & filename
261 if(f_open(&fe
, argv
[0], FA_READ
))
263 printf("Error: unable to open file '%s' for reading.", argv
[0]);
266 if(f_open(&fv
, argv
[1], FA_WRITE
|FA_CREATE_ALWAYS
))
268 printf("Error: unable to open file '%s' for writing.", argv
[1]);
276 while( (f_read(&fe
, buff
, 32768, &rdlen
) == 0) && (rdlen
> 0) )
278 f_write(&fv
, buff
, rdlen
, &wrlen
);
284 else if(strcmp(parg
,"mkdir")==0)
289 // Arg 1: folder path
292 else if(strcmp(parg
,"delete")==0)
297 // Arg 1: file/folder path (cannot delete non-empty folders)
300 else if(strcmp(parg
,"list")==0)
304 // Arg 1: folder path (optional)
314 if(f_opendir(&dir
, root
))
316 printf("Error opening directory '%s'.\n",root
);
320 printf("Listing directory contents of: %s\n", root
);
324 info
.lfname
= lfname
;
326 while( (!f_readdir(&dir
,&info
)) && (strlen(info
.fname
)>0))
328 if(strlen(info
.lfname
) > 0)
329 printf(" - %s (%s)\n", info
.lfname
, info
.fname
);
331 printf(" - %s\n", info
.fname
);
336 printf("Error: Unknown or invalid command: %s\n",argv
[-1]);
346 printf("Syntax: %s image_file [list of commands]\n\n", oargv
[0]);
347 printf("Commands: [Note: both '/' and '-' are accepted as command prefixes.] \n");
348 printf(" /format <sectors> [<filesystem>] Formats the disk image.\n");
349 printf(" /boot <sector file> Writes a new boot sector.\n");
350 printf(" /add <src path> <dst path> Copies an external file or directory\n"
351 " into the image.\n");
352 printf(" /extract <src path> <dst path> Copies a file or directory from the image\n"
353 " into an external file or directory.\n");
354 printf(" /move <src path> <new path> Moves/renames a file or directory.\n");
355 printf(" /copy <src path> <new path> Copies a file or directory.\n");
356 printf(" /mkdir <src path> <new path> Creates a directory.\n");
357 printf(" /rmdir <src path> <new path> Creates a directory.\n");
358 printf(" /list [<pattern>] Lists files a directory (defaults to root).\n");
359 //printf(" /recursive Enables recursive processing for directories.\n");