a9121ff7fcb0899f1ef33111bd91ffed197314aa
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 char* g_imageFileName
;
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]); PRINT_HELP_AND_QUIT(); } \
71 if(nargs>_max_) { printf("Too many args for command %s.\n",argv[-1]); PRINT_HELP_AND_QUIT(); } \
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", g_imageFileName, ret); \
93 PRINT_HELP_AND_QUIT(); \
96 void print_help(char const * const name
)
98 printf("Syntax: %s image_file [list of commands]\n\n", name
);
99 printf("Commands: [Note: both '/' and '-' are accepted as command prefixes.] \n");
100 printf(" /format <sectors> [<filesystem>] Formats the disk image.\n");
101 printf(" /boot <sector file> Writes a new boot sector.\n");
102 printf(" /add <src path> <dst path> Copies an external file or directory\n"
103 " into the image.\n");
104 printf(" /extract <src path> <dst path> Copies a file or directory from the image\n"
105 " into an external file or directory.\n");
106 printf(" /move <src path> <new path> Moves/renames a file or directory.\n");
107 printf(" /copy <src path> <new path> Copies a file or directory.\n");
108 printf(" /mkdir <src path> <new path> Creates a directory.\n");
109 printf(" /rmdir <src path> <new path> Creates a directory.\n");
110 printf(" /list [<pattern>] Lists files a directory (defaults to root).\n");
111 //printf(" /recursive Enables recursive processing for directories.\n");
114 #define PRINT_HELP_AND_QUIT() \
117 print_help(oargv[0]); \
121 int main(int oargc
, char* oargv
[])
124 int argc
= oargc
- 1;
125 char** argv
= oargv
+ 1;
127 // first parameter must be the image file.
130 PRINT_HELP_AND_QUIT();
133 if (is_command(argv
[0]))
135 printf("Error: first parameter must be a filename, found '%s' instead. \n", argv
[0]);
136 PRINT_HELP_AND_QUIT();
139 g_imageFileName
= argv
[0];
150 if (!is_command(parg
))
152 printf("Error: Expected a command, found '%s' instead. \n", parg
);
153 PRINT_HELP_AND_QUIT();
160 // find next command, to calculare number of args
161 while ((argv
[i
] != NULL
) && !is_command(argv
[i
++]))
164 if (strcmp(parg
, "format") == 0)
166 // NOTE: The fs driver detects which FAT format fits best based on size
173 // Arg 1: number of sectors
174 sectors
= atoi(argv
[0]);
178 printf("Error: Sectors must be > 0\n");
183 disk_ioctl(0, SET_SECTOR_COUNT
, §ors
);
185 ret
= f_mkfs("0:", 1, sectors
< 4096 ? 1 : 8);
188 printf("ERROR: Formatting drive: %d.\n", ret
);
189 PRINT_HELP_AND_QUIT();
192 else if (strcmp(parg
, "boot") == 0)
197 printf("Not Implemented.");
199 else if (strcmp(parg
, "add") == 0)
210 // Arg 1: external file to add
211 // Arg 2: virtual filename
213 fe
= fopen(argv
[0], "rb");
217 printf("Error: unable to open external file '%s' for reading.", argv
[0]);
222 if (f_open(&fv
, argv
[1], FA_WRITE
| FA_CREATE_ALWAYS
))
224 printf("Error: unable to open file '%s' for writing.", argv
[1]);
230 while ((rdlen
= fread(buff
, 1, 32768, fe
)) > 0)
232 f_write(&fv
, buff
, rdlen
, &wrlen
);
238 else if (strcmp(parg
, "extract") == 0)
249 // Arg 1: virtual file to extract
250 // Arg 2: external filename
252 if (f_open(&fe
, argv
[0], FA_READ
))
254 printf("Error: unable to open file '%s' for reading.", argv
[0]);
259 fv
= fopen(argv
[1], "wb");
263 printf("Error: unable to open external file '%s' for writing.", argv
[1]);
269 while ((f_read(&fe
, buff
, 32768, &rdlen
) == 0) && (rdlen
> 0))
271 fwrite(buff
, 1, rdlen
, fv
);
277 else if (strcmp(parg
, "move") == 0)
282 // Arg 1: src path & filename
283 // Arg 2: new path & filename
285 if (f_rename(argv
[0], argv
[1]))
286 printf("Error moving/renaming '%s' to '%s'", argv
[0], argv
[1]);
288 else if (strcmp(parg
, "copy") == 0)
298 // Arg 1: src path & filename
299 // Arg 2: new path & filename
301 if (f_open(&fe
, argv
[0], FA_READ
))
303 printf("Error: unable to open file '%s' for reading.", argv
[0]);
307 if (f_open(&fv
, argv
[1], FA_WRITE
| FA_CREATE_ALWAYS
))
309 printf("Error: unable to open file '%s' for writing.", argv
[1]);
315 while ((f_read(&fe
, buff
, 32768, &rdlen
) == 0) && (rdlen
> 0))
317 f_write(&fv
, buff
, rdlen
, &wrlen
);
323 else if (strcmp(parg
, "mkdir") == 0)
329 // Arg 1: folder path
332 else if (strcmp(parg
, "delete") == 0)
338 // Arg 1: file/folder path (cannot delete non-empty folders)
341 else if (strcmp(parg
, "list") == 0)
345 FILINFO info
= { 0 };
350 // Arg 1: folder path (optional)
357 if (f_opendir(&dir
, root
))
359 printf("Error opening directory '%s'.\n", root
);
364 printf("Listing directory contents of: %s\n", root
);
366 info
.lfname
= lfname
;
368 while ((!f_readdir(&dir
, &info
)) && (strlen(info
.fname
) > 0))
370 if (strlen(info
.lfname
) > 0)
371 printf(" - %s (%s)\n", info
.lfname
, info
.fname
);
373 printf(" - %s\n", info
.fname
);
378 printf("Error: Unknown or invalid command: %s\n", argv
[-1]);
379 PRINT_HELP_AND_QUIT();