4e317e9ff3f56de9348a6db93a1774853c730592
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"
18 static int isMounted
= 0;
19 static char buff
[32768];
21 // tool needed by fatfs
24 /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
25 /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
31 timeinfo
= localtime(&rawtime
);
36 DWORD Second
: 5; // div 2
41 DWORD Year
: 7; // year-1980
51 timeinfo
->tm_year
- 1980,
59 int is_command(const char* parg
)
62 return (parg
[0] == '/') || (parg
[0] == '-');
64 return (parg
[0] == '-');
68 #define NEED_PARAMS(_min_,_max_) \
70 if(nargs<_min_) { printf("Too few args for command %s.\n",argv[-1]); goto print_help; } \
71 if(nargs>_max_) { printf("Too many args for command %s.\n",argv[-1]); goto print_help; } \
81 r
= f_mount(&g_Filesystem
, "0:", 0);
89 #define NEED_MOUNT() \
90 do { ret = need_mount(); if(ret) \
92 printf("Error: could not mount image file '%s' (%d). \n", imageFileName, ret); \
96 int main(int oargc
, char* oargv
[])
100 char** argv
= oargv
+ 1;
102 // first parameter must be the image file.
108 if (is_command(argv
[0]))
110 printf("Error: first parameter must be a filename, found '%s' instead. \n", argv
[0]);
114 imageFileName
= argv
[0];
116 if (disk_initialize(0))
118 printf("Error: could not open image file '%s'. \n", imageFileName
);
131 if (!is_command(parg
))
133 printf("Error: Expected a command, found '%s' instead. \n", parg
);
141 // find next command, to calculare number of args
142 while ((argv
[i
] != NULL
) && !is_command(argv
[i
++]))
145 if (strcmp(parg
, "format") == 0)
147 // NOTE: The fs driver detects which FAT format fits best based on size
154 // Arg 1: number of sectors
155 sectors
= atoi(argv
[0]);
159 printf("Error: Sectors must be > 0\n");
163 disk_ioctl(0, SET_SECTOR_COUNT
, §ors
);
165 ret
= f_mkfs("0:", 1, 4096);
168 printf("ERROR: Formatting drive: %d.\n", ret
);
172 else if (strcmp(parg
, "boot") == 0)
177 printf("Not Implemented.");
179 else if (strcmp(parg
, "add") == 0)
190 // Arg 1: external file to add
191 // Arg 2: virtual filename
193 fe
= fopen(argv
[0], "rb");
197 printf("Error: unable to open external file '%s' for reading.", argv
[0]);
201 if (f_open(&fv
, argv
[1], FA_WRITE
| FA_CREATE_ALWAYS
))
203 printf("Error: unable to open file '%s' for writing.", argv
[1]);
207 while ((rdlen
= fread(buff
, 1, 32768, fe
)) > 0)
209 f_write(&fv
, buff
, rdlen
, &wrlen
);
215 else if (strcmp(parg
, "extract") == 0)
226 // Arg 1: virtual file to extract
227 // Arg 2: external filename
229 if (f_open(&fe
, argv
[0], FA_READ
))
231 printf("Error: unable to open file '%s' for reading.", argv
[0]);
235 fv
= fopen(argv
[1], "wb");
239 printf("Error: unable to open external file '%s' for writing.", argv
[1]);
243 while ((f_read(&fe
, buff
, 32768, &rdlen
) == 0) && (rdlen
> 0))
245 fwrite(buff
, 1, rdlen
, fv
);
251 else if (strcmp(parg
, "move") == 0)
256 // Arg 1: src path & filename
257 // Arg 2: new path & filename
259 if (f_rename(argv
[0], argv
[1]))
260 printf("Error moving/renaming '%s' to '%s'", argv
[0], argv
[1]);
262 else if (strcmp(parg
, "copy") == 0)
272 // Arg 1: src path & filename
273 // Arg 2: new path & filename
275 if (f_open(&fe
, argv
[0], FA_READ
))
277 printf("Error: unable to open file '%s' for reading.", argv
[0]);
280 if (f_open(&fv
, argv
[1], FA_WRITE
| FA_CREATE_ALWAYS
))
282 printf("Error: unable to open file '%s' for writing.", argv
[1]);
286 while ((f_read(&fe
, buff
, 32768, &rdlen
) == 0) && (rdlen
> 0))
288 f_write(&fv
, buff
, rdlen
, &wrlen
);
294 else if (strcmp(parg
, "mkdir") == 0)
300 // Arg 1: folder path
303 else if (strcmp(parg
, "delete") == 0)
309 // Arg 1: file/folder path (cannot delete non-empty folders)
312 else if (strcmp(parg
, "list") == 0)
316 FILINFO info
= { 0 };
321 // Arg 1: folder path (optional)
328 if (f_opendir(&dir
, root
))
330 printf("Error opening directory '%s'.\n", root
);
334 printf("Listing directory contents of: %s\n", root
);
336 info
.lfname
= lfname
;
338 while ((!f_readdir(&dir
, &info
)) && (strlen(info
.fname
) > 0))
340 if (strlen(info
.lfname
) > 0)
341 printf(" - %s (%s)\n", info
.lfname
, info
.fname
);
343 printf(" - %s\n", info
.fname
);
348 printf("Error: Unknown or invalid command: %s\n", argv
[-1]);
358 printf("Syntax: %s image_file [list of commands]\n\n", oargv
[0]);
359 printf("Commands: [Note: both '/' and '-' are accepted as command prefixes.] \n");
360 printf(" /format <sectors> [<filesystem>] Formats the disk image.\n");
361 printf(" /boot <sector file> Writes a new boot sector.\n");
362 printf(" /add <src path> <dst path> Copies an external file or directory\n"
363 " into the image.\n");
364 printf(" /extract <src path> <dst path> Copies a file or directory from the image\n"
365 " into an external file or directory.\n");
366 printf(" /move <src path> <new path> Moves/renames a file or directory.\n");
367 printf(" /copy <src path> <new path> Copies a file or directory.\n");
368 printf(" /mkdir <src path> <new path> Creates a directory.\n");
369 printf(" /rmdir <src path> <new path> Creates a directory.\n");
370 printf(" /list [<pattern>] Lists files a directory (defaults to root).\n");
371 //printf(" /recursive Enables recursive processing for directories.\n");