2015-09-06 17:21:22 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS FAT Image Creator
|
|
|
|
* FILE: tools/fatten/fatten.c
|
|
|
|
* PURPOSE: FAT Image Creator (for EFI Boot)
|
|
|
|
* PROGRAMMERS: David Quintana
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2015-09-06 18:10:59 +00:00
|
|
|
#include <time.h>
|
2015-09-06 17:21:22 +00:00
|
|
|
#include "fatfs/ff.h"
|
|
|
|
#include "fatfs/diskio.h"
|
|
|
|
|
2015-09-09 00:52:01 +00:00
|
|
|
static FATFS g_Filesystem;
|
2015-09-08 15:30:32 +00:00
|
|
|
static int isMounted = 0;
|
|
|
|
static char buff[32768];
|
2015-09-06 17:21:22 +00:00
|
|
|
|
|
|
|
// tool needed by fatfs
|
|
|
|
DWORD get_fattime()
|
|
|
|
{
|
2015-09-06 18:24:44 +00:00
|
|
|
/* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
|
|
|
|
/* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
|
|
|
|
|
|
|
|
time_t rawtime;
|
|
|
|
struct tm * timeinfo;
|
|
|
|
|
|
|
|
time(&rawtime);
|
|
|
|
timeinfo = localtime(&rawtime);
|
|
|
|
|
2015-09-08 08:27:00 +00:00
|
|
|
{
|
2015-09-06 18:24:44 +00:00
|
|
|
union FatTime {
|
|
|
|
struct {
|
|
|
|
DWORD Second : 5; // div 2
|
|
|
|
DWORD Minute : 6;
|
|
|
|
DWORD Hour : 5;
|
|
|
|
DWORD Day : 5;
|
|
|
|
DWORD Month : 4;
|
|
|
|
DWORD Year : 7; // year-1980
|
|
|
|
};
|
|
|
|
DWORD whole;
|
|
|
|
} myTime = {
|
|
|
|
{
|
|
|
|
timeinfo->tm_sec / 2,
|
|
|
|
timeinfo->tm_min,
|
|
|
|
timeinfo->tm_hour,
|
|
|
|
timeinfo->tm_mday,
|
|
|
|
timeinfo->tm_mon,
|
|
|
|
timeinfo->tm_year - 1980,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return myTime.whole;
|
2015-09-08 08:27:00 +00:00
|
|
|
}
|
2015-09-06 17:21:22 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 00:45:35 +00:00
|
|
|
int is_command(const char* parg)
|
2015-09-06 17:21:22 +00:00
|
|
|
{
|
2015-09-08 15:30:32 +00:00
|
|
|
#if _WIN32
|
2015-09-06 18:24:44 +00:00
|
|
|
return (parg[0] == '/') || (parg[0] == '-');
|
2015-09-08 15:30:32 +00:00
|
|
|
#else
|
|
|
|
return (parg[0] == '-');
|
|
|
|
#endif
|
2015-09-06 17:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define NEED_PARAMS(_min_,_max_) \
|
2015-09-06 18:24:44 +00:00
|
|
|
do {\
|
2015-09-08 20:04:04 +00:00
|
|
|
if(nargs<_min_) { printf("Too few args for command %s.\n",argv[-1]); PRINT_HELP_AND_QUIT(); } \
|
|
|
|
if(nargs>_max_) { printf("Too many args for command %s.\n",argv[-1]); PRINT_HELP_AND_QUIT(); } \
|
2015-09-06 18:24:44 +00:00
|
|
|
} while(0)
|
2015-09-06 17:21:22 +00:00
|
|
|
|
2015-09-08 00:45:35 +00:00
|
|
|
int need_mount()
|
2015-09-06 18:10:59 +00:00
|
|
|
{
|
2015-09-08 08:27:00 +00:00
|
|
|
int r;
|
|
|
|
|
2015-09-06 18:24:44 +00:00
|
|
|
if (isMounted)
|
|
|
|
return FR_OK;
|
2015-09-06 18:10:59 +00:00
|
|
|
|
2015-09-08 08:27:00 +00:00
|
|
|
r = f_mount(&g_Filesystem, "0:", 0);
|
2015-09-06 18:24:44 +00:00
|
|
|
if (r)
|
|
|
|
return r;
|
2015-09-06 18:10:59 +00:00
|
|
|
|
2015-09-06 18:24:44 +00:00
|
|
|
isMounted = 1;
|
|
|
|
return FR_OK;
|
2015-09-06 17:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define NEED_MOUNT() \
|
2015-09-06 18:24:44 +00:00
|
|
|
do { ret = need_mount(); if(ret) \
|
|
|
|
{\
|
2015-09-08 22:20:37 +00:00
|
|
|
printf("Error: could not mount disk (%d). \n", ret); \
|
2015-09-08 20:04:04 +00:00
|
|
|
PRINT_HELP_AND_QUIT(); \
|
2015-09-06 18:24:44 +00:00
|
|
|
} } while(0)
|
2015-09-06 17:21:22 +00:00
|
|
|
|
2015-09-08 20:04:04 +00:00
|
|
|
void print_help(char const * const name)
|
|
|
|
{
|
|
|
|
printf("Syntax: %s image_file [list of commands]\n\n", name);
|
|
|
|
printf("Commands: [Note: both '/' and '-' are accepted as command prefixes.] \n");
|
|
|
|
printf(" /format <sectors> [<filesystem>] Formats the disk image.\n");
|
|
|
|
printf(" /boot <sector file> Writes a new boot sector.\n");
|
|
|
|
printf(" /add <src path> <dst path> Copies an external file or directory\n"
|
|
|
|
" into the image.\n");
|
|
|
|
printf(" /extract <src path> <dst path> Copies a file or directory from the image\n"
|
|
|
|
" into an external file or directory.\n");
|
|
|
|
printf(" /move <src path> <new path> Moves/renames a file or directory.\n");
|
|
|
|
printf(" /copy <src path> <new path> Copies a file or directory.\n");
|
|
|
|
printf(" /mkdir <src path> <new path> Creates a directory.\n");
|
|
|
|
printf(" /rmdir <src path> <new path> Creates a directory.\n");
|
|
|
|
printf(" /list [<pattern>] Lists files a directory (defaults to root).\n");
|
|
|
|
//printf(" /recursive Enables recursive processing for directories.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PRINT_HELP_AND_QUIT() \
|
|
|
|
do { \
|
|
|
|
ret = 1; \
|
|
|
|
print_help(oargv[0]); \
|
|
|
|
goto exit; \
|
|
|
|
} while (0)
|
|
|
|
|
2015-09-06 17:21:22 +00:00
|
|
|
int main(int oargc, char* oargv[])
|
|
|
|
{
|
2015-09-06 18:24:44 +00:00
|
|
|
int ret;
|
|
|
|
int argc = oargc - 1;
|
|
|
|
char** argv = oargv + 1;
|
|
|
|
|
|
|
|
// first parameter must be the image file.
|
|
|
|
if (argc == 0)
|
|
|
|
{
|
2015-09-08 20:04:04 +00:00
|
|
|
PRINT_HELP_AND_QUIT();
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_command(argv[0]))
|
|
|
|
{
|
|
|
|
printf("Error: first parameter must be a filename, found '%s' instead. \n", argv[0]);
|
2015-09-08 20:04:04 +00:00
|
|
|
PRINT_HELP_AND_QUIT();
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 22:20:37 +00:00
|
|
|
if (disk_openimage(0, argv[0]))
|
|
|
|
{
|
|
|
|
printf("Error: could not open image file '%s'. \n", argv[0]);
|
|
|
|
PRINT_HELP_AND_QUIT();
|
|
|
|
}
|
2015-09-06 18:24:44 +00:00
|
|
|
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
|
|
|
while (argc > 0)
|
|
|
|
{
|
|
|
|
char *parg = *argv;
|
2015-09-08 08:27:00 +00:00
|
|
|
int nargs = 0;
|
|
|
|
int i = 0;
|
2015-09-06 18:24:44 +00:00
|
|
|
|
|
|
|
if (!is_command(parg))
|
|
|
|
{
|
|
|
|
printf("Error: Expected a command, found '%s' instead. \n", parg);
|
2015-09-08 20:04:04 +00:00
|
|
|
PRINT_HELP_AND_QUIT();
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parg++;
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
|
|
|
|
// find next command, to calculare number of args
|
|
|
|
while ((argv[i] != NULL) && !is_command(argv[i++]))
|
|
|
|
nargs++;
|
|
|
|
|
|
|
|
if (strcmp(parg, "format") == 0)
|
|
|
|
{
|
|
|
|
// NOTE: The fs driver detects which FAT format fits best based on size
|
2015-09-08 08:27:00 +00:00
|
|
|
int sectors;
|
2015-09-06 18:24:44 +00:00
|
|
|
|
|
|
|
NEED_PARAMS(1, 1);
|
|
|
|
|
|
|
|
// Arg 1: number of sectors
|
2015-09-08 08:27:00 +00:00
|
|
|
sectors = atoi(argv[0]);
|
2015-09-06 18:24:44 +00:00
|
|
|
|
|
|
|
if (sectors <= 0)
|
|
|
|
{
|
|
|
|
printf("Error: Sectors must be > 0\n");
|
2015-09-08 20:04:04 +00:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
disk_ioctl(0, SET_SECTOR_COUNT, §ors);
|
|
|
|
|
2015-09-08 22:20:37 +00:00
|
|
|
NEED_MOUNT();
|
|
|
|
|
2015-09-08 22:01:05 +00:00
|
|
|
ret = f_mkfs("0:", 1, sectors < 4096 ? 1 : 8);
|
2015-09-06 18:24:44 +00:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
printf("ERROR: Formatting drive: %d.\n", ret);
|
2015-09-08 20:04:04 +00:00
|
|
|
PRINT_HELP_AND_QUIT();
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(parg, "boot") == 0)
|
|
|
|
{
|
2015-09-09 00:49:47 +00:00
|
|
|
FILE* fe;
|
|
|
|
BYTE* temp = buff + 1024;
|
|
|
|
|
2015-09-06 18:24:44 +00:00
|
|
|
NEED_PARAMS(1, 1);
|
|
|
|
|
|
|
|
// Arg 1: boot file
|
2015-09-09 00:49:47 +00:00
|
|
|
|
|
|
|
fe = fopen(argv[0], "rb");
|
|
|
|
|
|
|
|
if (!fe)
|
|
|
|
{
|
|
|
|
printf("Error: unable to open external file '%s' for reading.", argv[0]);
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!fread(buff, 512, 1, fe))
|
|
|
|
{
|
|
|
|
printf("Error: unable to read boot sector from file '%s'.", argv[0]);
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
NEED_MOUNT();
|
|
|
|
|
|
|
|
if(disk_read(0, temp, 0, 1))
|
|
|
|
{
|
|
|
|
printf("Error: unable to read existing boot sector from image.");
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_Filesystem.fs_type == FS_FAT32)
|
|
|
|
{
|
|
|
|
printf("TODO: writing boot sectors for FAT32 images not yet supported.");
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-09 01:33:27 +00:00
|
|
|
#define FAT16_HEADER_START 3
|
|
|
|
#define FAT16_HEADER_END 62
|
|
|
|
|
|
|
|
memcpy(buff + FAT16_HEADER_START, temp + FAT16_HEADER_START, FAT16_HEADER_END - FAT16_HEADER_START);
|
2015-09-09 00:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (disk_write(0, buff, 0, 1))
|
|
|
|
{
|
|
|
|
printf("Error: unable to write new boot sector to image.");
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fe);
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(parg, "add") == 0)
|
|
|
|
{
|
2015-09-08 08:27:00 +00:00
|
|
|
FILE* fe;
|
|
|
|
FIL fv = { 0 };
|
|
|
|
UINT rdlen = 0;
|
|
|
|
UINT wrlen = 0;
|
|
|
|
|
2015-09-06 18:24:44 +00:00
|
|
|
NEED_PARAMS(2, 2);
|
|
|
|
|
|
|
|
NEED_MOUNT();
|
|
|
|
|
|
|
|
// Arg 1: external file to add
|
|
|
|
// Arg 2: virtual filename
|
|
|
|
|
2015-09-08 03:37:47 +00:00
|
|
|
fe = fopen(argv[0], "rb");
|
|
|
|
|
|
|
|
if (!fe)
|
2015-09-06 18:24:44 +00:00
|
|
|
{
|
2015-09-08 03:39:51 +00:00
|
|
|
printf("Error: unable to open external file '%s' for reading.", argv[0]);
|
2015-09-08 20:04:04 +00:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
2015-09-08 03:37:47 +00:00
|
|
|
|
2015-09-06 18:24:44 +00:00
|
|
|
if (f_open(&fv, argv[1], FA_WRITE | FA_CREATE_ALWAYS))
|
|
|
|
{
|
|
|
|
printf("Error: unable to open file '%s' for writing.", argv[1]);
|
2015-09-08 20:04:04 +00:00
|
|
|
fclose(fe);
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 00:52:01 +00:00
|
|
|
while ((rdlen = fread(buff, 1, sizeof(buff), fe)) > 0)
|
2015-09-06 18:24:44 +00:00
|
|
|
{
|
|
|
|
f_write(&fv, buff, rdlen, &wrlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fe);
|
|
|
|
f_close(&fv);
|
|
|
|
}
|
|
|
|
else if (strcmp(parg, "extract") == 0)
|
|
|
|
{
|
2015-09-08 08:27:00 +00:00
|
|
|
FIL fe = { 0 };
|
|
|
|
FILE* fv;
|
|
|
|
UINT rdlen = 0;
|
|
|
|
UINT wrlen = 0;
|
|
|
|
|
2015-09-06 18:24:44 +00:00
|
|
|
NEED_PARAMS(2, 2);
|
|
|
|
|
|
|
|
NEED_MOUNT();
|
|
|
|
|
|
|
|
// Arg 1: virtual file to extract
|
|
|
|
// Arg 2: external filename
|
|
|
|
|
|
|
|
if (f_open(&fe, argv[0], FA_READ))
|
|
|
|
{
|
|
|
|
printf("Error: unable to open file '%s' for reading.", argv[0]);
|
2015-09-08 20:04:04 +00:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
2015-09-08 03:37:47 +00:00
|
|
|
|
|
|
|
fv = fopen(argv[1], "wb");
|
|
|
|
|
|
|
|
if (!fv)
|
2015-09-06 18:24:44 +00:00
|
|
|
{
|
|
|
|
printf("Error: unable to open external file '%s' for writing.", argv[1]);
|
2015-09-08 20:04:04 +00:00
|
|
|
f_close(&fe);
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 00:52:01 +00:00
|
|
|
while ((f_read(&fe, buff, sizeof(buff), &rdlen) == 0) && (rdlen > 0))
|
2015-09-06 18:24:44 +00:00
|
|
|
{
|
|
|
|
fwrite(buff, 1, rdlen, fv);
|
|
|
|
}
|
|
|
|
|
|
|
|
f_close(&fe);
|
|
|
|
fclose(fv);
|
|
|
|
}
|
|
|
|
else if (strcmp(parg, "move") == 0)
|
|
|
|
{
|
|
|
|
NEED_PARAMS(2, 2);
|
|
|
|
|
|
|
|
NEED_MOUNT();
|
|
|
|
// Arg 1: src path & filename
|
|
|
|
// Arg 2: new path & filename
|
|
|
|
|
|
|
|
if (f_rename(argv[0], argv[1]))
|
|
|
|
printf("Error moving/renaming '%s' to '%s'", argv[0], argv[1]);
|
|
|
|
}
|
|
|
|
else if (strcmp(parg, "copy") == 0)
|
|
|
|
{
|
2015-09-08 08:27:00 +00:00
|
|
|
FIL fe = { 0 };
|
|
|
|
FIL fv = { 0 };
|
|
|
|
UINT rdlen = 0;
|
|
|
|
UINT wrlen = 0;
|
|
|
|
|
2015-09-06 18:24:44 +00:00
|
|
|
NEED_PARAMS(2, 2);
|
|
|
|
|
|
|
|
NEED_MOUNT();
|
|
|
|
// Arg 1: src path & filename
|
|
|
|
// Arg 2: new path & filename
|
|
|
|
|
|
|
|
if (f_open(&fe, argv[0], FA_READ))
|
|
|
|
{
|
|
|
|
printf("Error: unable to open file '%s' for reading.", argv[0]);
|
2015-09-08 20:04:04 +00:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
if (f_open(&fv, argv[1], FA_WRITE | FA_CREATE_ALWAYS))
|
|
|
|
{
|
|
|
|
printf("Error: unable to open file '%s' for writing.", argv[1]);
|
2015-09-08 20:04:04 +00:00
|
|
|
f_close(&fe);
|
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 00:52:01 +00:00
|
|
|
while ((f_read(&fe, buff, sizeof(buff), &rdlen) == 0) && (rdlen > 0))
|
2015-09-06 18:24:44 +00:00
|
|
|
{
|
|
|
|
f_write(&fv, buff, rdlen, &wrlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
f_close(&fe);
|
|
|
|
f_close(&fv);
|
|
|
|
}
|
|
|
|
else if (strcmp(parg, "mkdir") == 0)
|
|
|
|
{
|
|
|
|
NEED_PARAMS(1, 1);
|
|
|
|
|
|
|
|
NEED_MOUNT();
|
2015-09-08 03:37:47 +00:00
|
|
|
|
2015-09-06 18:24:44 +00:00
|
|
|
// Arg 1: folder path
|
2015-09-08 03:37:47 +00:00
|
|
|
f_mkdir(argv[0]);
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(parg, "delete") == 0)
|
|
|
|
{
|
|
|
|
NEED_PARAMS(1, 1);
|
|
|
|
|
|
|
|
NEED_MOUNT();
|
2015-09-08 03:37:47 +00:00
|
|
|
|
2015-09-06 18:24:44 +00:00
|
|
|
// Arg 1: file/folder path (cannot delete non-empty folders)
|
2015-09-08 03:37:47 +00:00
|
|
|
f_unlink(argv[0]);
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(parg, "list") == 0)
|
|
|
|
{
|
2015-09-08 08:27:00 +00:00
|
|
|
char* root = "/";
|
|
|
|
DIR dir = { 0 };
|
|
|
|
FILINFO info = { 0 };
|
|
|
|
char lfname[257];
|
|
|
|
|
2015-09-06 18:24:44 +00:00
|
|
|
NEED_PARAMS(0, 1);
|
|
|
|
|
|
|
|
// Arg 1: folder path (optional)
|
|
|
|
|
|
|
|
if (nargs == 1)
|
|
|
|
{
|
|
|
|
root = argv[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f_opendir(&dir, root))
|
|
|
|
{
|
|
|
|
printf("Error opening directory '%s'.\n", root);
|
2015-09-08 20:04:04 +00:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Listing directory contents of: %s\n", root);
|
|
|
|
|
|
|
|
info.lfname = lfname;
|
|
|
|
info.lfsize = 256;
|
|
|
|
while ((!f_readdir(&dir, &info)) && (strlen(info.fname) > 0))
|
|
|
|
{
|
|
|
|
if (strlen(info.lfname) > 0)
|
|
|
|
printf(" - %s (%s)\n", info.lfname, info.fname);
|
|
|
|
else
|
|
|
|
printf(" - %s\n", info.fname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Error: Unknown or invalid command: %s\n", argv[-1]);
|
2015-09-08 20:04:04 +00:00
|
|
|
PRINT_HELP_AND_QUIT();
|
2015-09-06 18:24:44 +00:00
|
|
|
}
|
|
|
|
argv += nargs;
|
|
|
|
argc -= nargs;
|
|
|
|
}
|
|
|
|
|
2015-09-08 20:04:04 +00:00
|
|
|
ret = 0;
|
2015-09-06 17:21:22 +00:00
|
|
|
|
2015-09-08 20:04:04 +00:00
|
|
|
exit:
|
|
|
|
|
|
|
|
disk_cleanup(0);
|
2015-09-06 18:24:44 +00:00
|
|
|
|
2015-09-08 20:04:04 +00:00
|
|
|
return ret;
|
2015-09-06 17:21:22 +00:00
|
|
|
}
|
|
|
|
|