Change tabs to spaces, but only for the files I write. The rest come directly from the fatfs package and are best left untouched.

svn path=/trunk/; revision=69065
This commit is contained in:
David Quintana 2015-09-06 18:24:44 +00:00
parent 163427942d
commit 1a2ad3befe
2 changed files with 379 additions and 379 deletions

View file

@ -14,28 +14,28 @@ extern char* imageFileName;
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Correspondence between physical drive number and image file handles. */ /* Correspondence between physical drive number and image file handles. */
FILE* driveHandle[1] = {NULL}; FILE* driveHandle[1] = { NULL };
const int driveHandleCount = sizeof(driveHandle) / sizeof(FILE*); const int driveHandleCount = sizeof(driveHandle) / sizeof(FILE*);
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Inidialize a Drive */ /* Inidialize a Drive */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
DSTATUS disk_initialize ( DSTATUS disk_initialize(
BYTE pdrv /* Physical drive nmuber (0..) */ BYTE pdrv /* Physical drive nmuber (0..) */
) )
{ {
if(pdrv == 0) // only one drive (image file) supported atm. if (pdrv == 0) /* only one drive (image file) supported atm. */
{ {
if(driveHandle[0]!=NULL) if (driveHandle[0] != NULL)
return 0; return 0;
driveHandle[0]=fopen(imageFileName, "r+b"); driveHandle[0] = fopen(imageFileName, "r+b");
if(driveHandle[0]!=NULL) if (driveHandle[0] != NULL)
return 0; return 0;
} }
return STA_NOINIT; return STA_NOINIT;
} }
@ -44,16 +44,16 @@ DSTATUS disk_initialize (
/* Get Disk Status */ /* Get Disk Status */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
DSTATUS disk_status ( DSTATUS disk_status(
BYTE pdrv /* Physical drive nmuber (0..) */ BYTE pdrv /* Physical drive nmuber (0..) */
) )
{ {
if(pdrv < driveHandleCount) if (pdrv < driveHandleCount)
{ {
if(driveHandle[pdrv] != NULL) if (driveHandle[pdrv] != NULL)
return 0; return 0;
} }
return STA_NOINIT; return STA_NOINIT;
} }
@ -62,32 +62,32 @@ DSTATUS disk_status (
/* Read Sector(s) */ /* Read Sector(s) */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
DRESULT disk_read ( DRESULT disk_read(
BYTE pdrv, /* Physical drive nmuber (0..) */ BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE *buff, /* Data buffer to store read data */ BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Sector address (LBA) */ DWORD sector, /* Sector address (LBA) */
UINT count /* Number of sectors to read (1..128) */ UINT count /* Number of sectors to read (1..128) */
) )
{ {
DWORD result; DWORD result;
if(pdrv < driveHandleCount) if (pdrv < driveHandleCount)
{ {
if(driveHandle[pdrv] != NULL) if (driveHandle[pdrv] != NULL)
{ {
if(fseek(driveHandle[pdrv], sector * 512, SEEK_SET)) if (fseek(driveHandle[pdrv], sector * 512, SEEK_SET))
return RES_ERROR; return RES_ERROR;
result = fread(buff, 512, count, driveHandle[pdrv]); result = fread(buff, 512, count, driveHandle[pdrv]);
if(result == count) if (result == count)
return RES_OK; return RES_OK;
return RES_ERROR; return RES_ERROR;
} }
} }
return RES_PARERR; return RES_PARERR;
} }
@ -97,33 +97,33 @@ DRESULT disk_read (
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
#if _USE_WRITE #if _USE_WRITE
DRESULT disk_write ( DRESULT disk_write(
BYTE pdrv, /* Physical drive nmuber (0..) */ BYTE pdrv, /* Physical drive nmuber (0..) */
const BYTE *buff, /* Data to be written */ const BYTE *buff, /* Data to be written */
DWORD sector, /* Sector address (LBA) */ DWORD sector, /* Sector address (LBA) */
UINT count /* Number of sectors to write (1..128) */ UINT count /* Number of sectors to write (1..128) */
) )
{ {
DWORD result; DWORD result;
if(pdrv < driveHandleCount) if (pdrv < driveHandleCount)
{ {
if(driveHandle[pdrv] != NULL) if (driveHandle[pdrv] != NULL)
{ {
if(fseek(driveHandle[pdrv], sector * 512, SEEK_SET)) if (fseek(driveHandle[pdrv], sector * 512, SEEK_SET))
return RES_ERROR; return RES_ERROR;
result = fwrite(buff, 512, count, driveHandle[pdrv]); result = fwrite(buff, 512, count, driveHandle[pdrv]);
return RES_ERROR; return RES_ERROR;
if(result != (512 * count)) if (result != (512 * count))
return RES_ERROR; return RES_ERROR;
return RES_OK; return RES_OK;
} }
} }
return RES_PARERR; return RES_PARERR;
} }
#endif #endif
@ -133,58 +133,58 @@ DRESULT disk_write (
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
#if _USE_IOCTL #if _USE_IOCTL
DRESULT disk_ioctl ( DRESULT disk_ioctl(
BYTE pdrv, /* Physical drive nmuber (0..) */ BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */ BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */ void *buff /* Buffer to send/receive control data */
) )
{ {
if(pdrv < driveHandleCount) if (pdrv < driveHandleCount)
{ {
if(driveHandle[pdrv] != NULL) if (driveHandle[pdrv] != NULL)
{ {
switch(cmd) switch (cmd)
{ {
case CTRL_SYNC: case CTRL_SYNC:
fflush(driveHandle[pdrv]); fflush(driveHandle[pdrv]);
return RES_OK; return RES_OK;
case GET_SECTOR_SIZE: case GET_SECTOR_SIZE:
*(DWORD*)buff = 512; *(DWORD*)buff = 512;
return RES_OK; return RES_OK;
case GET_BLOCK_SIZE: case GET_BLOCK_SIZE:
*(DWORD*)buff = 512; *(DWORD*)buff = 512;
return RES_OK; return RES_OK;
case GET_SECTOR_COUNT: case GET_SECTOR_COUNT:
fseek(driveHandle[pdrv], 0, SEEK_END); fseek(driveHandle[pdrv], 0, SEEK_END);
*(DWORD*)buff = ftell(driveHandle[pdrv]) / 512; *(DWORD*)buff = ftell(driveHandle[pdrv]) / 512;
return RES_OK; return RES_OK;
case SET_SECTOR_COUNT: case SET_SECTOR_COUNT:
{ {
int count = *(DWORD*)buff; int count = *(DWORD*)buff;
long size; long size;
fseek(driveHandle[pdrv], 0, SEEK_END);
size = ftell(driveHandle[pdrv]) / 512;
if(size < count)
{
if(fseek(driveHandle[pdrv], count * 512 - 1, SEEK_SET))
return RES_ERROR;
fwrite(buff, 1, 1, driveHandle[pdrv]); fseek(driveHandle[pdrv], 0, SEEK_END);
size = ftell(driveHandle[pdrv]) / 512;
return RES_OK; if (size < count)
} {
else if (fseek(driveHandle[pdrv], count * 512 - 1, SEEK_SET))
{ return RES_ERROR;
// SHRINKING NOT IMPLEMENTED
return RES_OK;
}
}
}
}
}
return RES_PARERR; fwrite(buff, 1, 1, driveHandle[pdrv]);
return RES_OK;
}
else
{
// SHRINKING NOT IMPLEMENTED
return RES_OK;
}
}
}
}
}
return RES_PARERR;
} }
#endif #endif

View file

@ -20,344 +20,344 @@ int isMounted = 0;
// tool needed by fatfs // tool needed by fatfs
DWORD get_fattime() DWORD get_fattime()
{ {
/* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */ /* 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) */ /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
time_t rawtime; time_t rawtime;
struct tm * timeinfo; struct tm * timeinfo;
time (&rawtime); time(&rawtime);
timeinfo = localtime (&rawtime); timeinfo = localtime(&rawtime);
union FatTime { union FatTime {
struct { struct {
DWORD Second : 5; // div 2 DWORD Second : 5; // div 2
DWORD Minute : 6; DWORD Minute : 6;
DWORD Hour : 5; DWORD Hour : 5;
DWORD Day : 5; DWORD Day : 5;
DWORD Month : 4; DWORD Month : 4;
DWORD Year : 7; // year-1980 DWORD Year : 7; // year-1980
}; };
DWORD whole; DWORD whole;
} myTime = { } myTime = {
{ {
timeinfo->tm_sec / 2, timeinfo->tm_sec / 2,
timeinfo->tm_min, timeinfo->tm_min,
timeinfo->tm_hour, timeinfo->tm_hour,
timeinfo->tm_mday, timeinfo->tm_mday,
timeinfo->tm_mon, timeinfo->tm_mon,
timeinfo->tm_year - 1980, timeinfo->tm_year - 1980,
} }
}; };
return myTime.whole; return myTime.whole;
} }
BOOL is_command(const char* parg) BOOL is_command(const char* parg)
{ {
return (parg[0]=='/') || (parg[0] == '-'); return (parg[0] == '/') || (parg[0] == '-');
} }
#define NEED_PARAMS(_min_,_max_) \ #define NEED_PARAMS(_min_,_max_) \
do {\ do {\
if(nargs<_min_) { printf("Too few args for command %s.\n",argv[-1]); goto print_help; } \ if(nargs<_min_) { printf("Too few args for command %s.\n",argv[-1]); goto print_help; } \
if(nargs>_max_) { printf("Too many args for command %s.\n",argv[-1]); goto print_help; } \ if(nargs>_max_) { printf("Too many args for command %s.\n",argv[-1]); goto print_help; } \
} while(0) } while(0)
BOOL need_mount() BOOL need_mount()
{ {
if(isMounted) if (isMounted)
return FR_OK; return FR_OK;
int r = f_mount(&g_Filesystem, "0:", 0); int r = f_mount(&g_Filesystem, "0:", 0);
if(r) if (r)
return r; return r;
isMounted = 1; isMounted = 1;
return FR_OK; return FR_OK;
} }
#define NEED_MOUNT() \ #define NEED_MOUNT() \
do { ret = need_mount(); if(ret) \ do { ret = need_mount(); if(ret) \
{\ {\
printf("Error: could not mount image file '%s' (%d). \n", imageFileName, ret); \ printf("Error: could not mount image file '%s' (%d). \n", imageFileName, ret); \
goto print_help; \ goto print_help; \
} } while(0) } } while(0)
int main(int oargc, char* oargv[]) int main(int oargc, char* oargv[])
{ {
int ret; int ret;
int argc = oargc-1; int argc = oargc - 1;
char** argv = oargv+1; char** argv = oargv + 1;
// first parameter must be the image file. // first parameter must be the image file.
if(argc == 0) if (argc == 0)
{ {
goto print_help; goto print_help;
} }
if(is_command(argv[0])) if (is_command(argv[0]))
{ {
printf("Error: first parameter must be a filename, found '%s' instead. \n", argv[0]); printf("Error: first parameter must be a filename, found '%s' instead. \n", argv[0]);
goto print_help; goto print_help;
} }
imageFileName = argv[0]; imageFileName = argv[0];
if(disk_initialize(0)) if (disk_initialize(0))
{ {
printf("Error: could not open image file '%s'. \n", imageFileName); printf("Error: could not open image file '%s'. \n", imageFileName);
goto print_help; goto print_help;
} }
argc--; argc--;
argv++; argv++;
while(argc>0) while (argc > 0)
{ {
char *parg = *argv; char *parg = *argv;
if(!is_command(parg)) if (!is_command(parg))
{ {
printf("Error: Expected a command, found '%s' instead. \n",parg); printf("Error: Expected a command, found '%s' instead. \n", parg);
goto print_help; goto print_help;
} }
parg++; parg++;
argv++; argv++;
argc--; argc--;
// find next command, to calculare number of args // find next command, to calculare number of args
int nargs = 0; int nargs = 0;
int i=0; int i = 0;
while((argv[i] != NULL) && !is_command(argv[i++])) while ((argv[i] != NULL) && !is_command(argv[i++]))
nargs++; nargs++;
if(strcmp(parg,"format")==0) if (strcmp(parg, "format") == 0)
{ {
// NOTE: The fs driver detects which FAT format fits best based on size // NOTE: The fs driver detects which FAT format fits best based on size
NEED_PARAMS(1,1); NEED_PARAMS(1, 1);
NEED_MOUNT(); NEED_MOUNT();
// Arg 1: number of sectors // Arg 1: number of sectors
int sectors = atoi(argv[0]); int sectors = atoi(argv[0]);
if(sectors <= 0) if (sectors <= 0)
{ {
printf("Error: Sectors must be > 0\n"); printf("Error: Sectors must be > 0\n");
return 1; return 1;
} }
disk_ioctl(0, SET_SECTOR_COUNT, &sectors); disk_ioctl(0, SET_SECTOR_COUNT, &sectors);
ret = f_mkfs("0:", 1, 4096); ret = f_mkfs("0:", 1, 4096);
if (ret) if (ret)
{ {
printf("ERROR: Formatting drive: %d.\n", ret); printf("ERROR: Formatting drive: %d.\n", ret);
goto print_help; goto print_help;
} }
} }
else if(strcmp(parg,"boot")==0) else if (strcmp(parg, "boot") == 0)
{ {
NEED_PARAMS(1,1); NEED_PARAMS(1, 1);
// Arg 1: boot file // Arg 1: boot file
printf("Not Implemented."); printf("Not Implemented.");
} }
else if(strcmp(parg,"add")==0) else if (strcmp(parg, "add") == 0)
{ {
NEED_PARAMS(2,2); NEED_PARAMS(2, 2);
NEED_MOUNT(); NEED_MOUNT();
// Arg 1: external file to add // Arg 1: external file to add
// Arg 2: virtual filename // Arg 2: virtual filename
FILE* fe; FILE* fe;
FIL fv = {0}; FIL fv = { 0 };
if(fopen_s(&fe, argv[0],"rb"))
{
printf("Error: unable to open external file '%s' for reading.", argv[0]);
return 1;
}
if(f_open(&fv, argv[1], FA_WRITE | FA_CREATE_ALWAYS))
{
printf("Error: unable to open file '%s' for writing.", argv[1]);
return 1;
}
char buff[32768]; if (fopen_s(&fe, argv[0], "rb"))
UINT rdlen = 0; {
UINT wrlen = 0; printf("Error: unable to open external file '%s' for reading.", argv[0]);
return 1;
}
if (f_open(&fv, argv[1], FA_WRITE | FA_CREATE_ALWAYS))
{
printf("Error: unable to open file '%s' for writing.", argv[1]);
return 1;
}
while( (rdlen = fread(buff,1,32768,fe)) > 0 ) char buff[32768];
{ UINT rdlen = 0;
f_write(&fv,buff, rdlen, &wrlen); UINT wrlen = 0;
}
fclose(fe); while ((rdlen = fread(buff, 1, 32768, fe)) > 0)
f_close(&fv); {
} f_write(&fv, buff, rdlen, &wrlen);
else if(strcmp(parg,"extract")==0) }
{
NEED_PARAMS(2,2);
NEED_MOUNT(); fclose(fe);
f_close(&fv);
}
else if (strcmp(parg, "extract") == 0)
{
NEED_PARAMS(2, 2);
// Arg 1: virtual file to extract NEED_MOUNT();
// Arg 2: external filename
FIL fe = {0}; // Arg 1: virtual file to extract
FILE* fv; // Arg 2: external filename
if(f_open(&fe, argv[0], FA_READ)) FIL fe = { 0 };
{ FILE* fv;
printf("Error: unable to open file '%s' for reading.", argv[0]);
return 1;
}
if(fopen_s(&fv, argv[1],"wb"))
{
printf("Error: unable to open external file '%s' for writing.", argv[1]);
return 1;
}
char buff[32768]; if (f_open(&fe, argv[0], FA_READ))
UINT rdlen = 0; {
UINT wrlen = 0; printf("Error: unable to open file '%s' for reading.", argv[0]);
return 1;
}
if (fopen_s(&fv, argv[1], "wb"))
{
printf("Error: unable to open external file '%s' for writing.", argv[1]);
return 1;
}
while( (f_read(&fe, buff, 32768, &rdlen) == 0 ) && ( rdlen > 0) ) char buff[32768];
{ UINT rdlen = 0;
fwrite(buff, 1, rdlen, fv); UINT wrlen = 0;
}
f_close(&fe); while ((f_read(&fe, buff, 32768, &rdlen) == 0) && (rdlen > 0))
fclose(fv); {
} fwrite(buff, 1, rdlen, fv);
else if(strcmp(parg,"move")==0) }
{
NEED_PARAMS(2,2);
NEED_MOUNT(); f_close(&fe);
// Arg 1: src path & filename fclose(fv);
// Arg 2: new path & filename }
else if (strcmp(parg, "move") == 0)
{
NEED_PARAMS(2, 2);
if(f_rename(argv[0], argv[1])) NEED_MOUNT();
printf("Error moving/renaming '%s' to '%s'", argv[0],argv[1]); // Arg 1: src path & filename
} // Arg 2: new path & filename
else if(strcmp(parg,"copy")==0)
{
NEED_PARAMS(2,2)
NEED_MOUNT(); if (f_rename(argv[0], argv[1]))
// Arg 1: src path & filename printf("Error moving/renaming '%s' to '%s'", argv[0], argv[1]);
// Arg 2: new path & filename }
else if (strcmp(parg, "copy") == 0)
{
NEED_PARAMS(2, 2);
FIL fe = {0}; NEED_MOUNT();
FIL fv = {0}; // Arg 1: src path & filename
// Arg 2: new path & filename
if(f_open(&fe, argv[0], FA_READ)) FIL fe = { 0 };
{ FIL fv = { 0 };
printf("Error: unable to open file '%s' for reading.", argv[0]);
return 1;
}
if(f_open(&fv, argv[1], FA_WRITE|FA_CREATE_ALWAYS))
{
printf("Error: unable to open file '%s' for writing.", argv[1]);
return 1;
}
char buff[32768]; if (f_open(&fe, argv[0], FA_READ))
UINT rdlen = 0; {
UINT wrlen = 0; printf("Error: unable to open file '%s' for reading.", argv[0]);
return 1;
}
if (f_open(&fv, argv[1], FA_WRITE | FA_CREATE_ALWAYS))
{
printf("Error: unable to open file '%s' for writing.", argv[1]);
return 1;
}
while( (f_read(&fe, buff, 32768, &rdlen) == 0) && (rdlen > 0) ) char buff[32768];
{ UINT rdlen = 0;
f_write(&fv, buff, rdlen, &wrlen); UINT wrlen = 0;
}
f_close(&fe); while ((f_read(&fe, buff, 32768, &rdlen) == 0) && (rdlen > 0))
f_close(&fv); {
} f_write(&fv, buff, rdlen, &wrlen);
else if(strcmp(parg,"mkdir")==0) }
{
NEED_PARAMS(1,1);
NEED_MOUNT(); f_close(&fe);
// Arg 1: folder path f_close(&fv);
f_mkdir(argv[1]); }
} else if (strcmp(parg, "mkdir") == 0)
else if(strcmp(parg,"delete")==0) {
{ NEED_PARAMS(1, 1);
NEED_PARAMS(1,1);
NEED_MOUNT(); NEED_MOUNT();
// Arg 1: file/folder path (cannot delete non-empty folders) // Arg 1: folder path
f_unlink(argv[1]); f_mkdir(argv[1]);
} }
else if(strcmp(parg,"list")==0) else if (strcmp(parg, "delete") == 0)
{ {
NEED_PARAMS(0,1); NEED_PARAMS(1, 1);
// Arg 1: folder path (optional) NEED_MOUNT();
char* root = "/"; // Arg 1: file/folder path (cannot delete non-empty folders)
f_unlink(argv[1]);
}
else if (strcmp(parg, "list") == 0)
{
NEED_PARAMS(0, 1);
if(nargs == 1) // Arg 1: folder path (optional)
{ char* root = "/";
root = argv[0];
}
DIR dir = {0}; if (nargs == 1)
{
root = argv[0];
}
if(f_opendir(&dir, root)) DIR dir = { 0 };
{
printf("Error opening directory '%s'.\n",root);
return 1;
}
printf("Listing directory contents of: %s\n", root); if (f_opendir(&dir, root))
{
printf("Error opening directory '%s'.\n", root);
return 1;
}
FILINFO info = {0}; printf("Listing directory contents of: %s\n", root);
CHAR lfname[257];
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]);
goto print_help;
}
argv+=nargs;
argc-=nargs;
}
return 0; FILINFO info = { 0 };
CHAR lfname[257];
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]);
goto print_help;
}
argv += nargs;
argc -= nargs;
}
return 0;
print_help: print_help:
printf("Syntax: %s image_file [list of commands]\n\n", oargv[0]); printf("Syntax: %s image_file [list of commands]\n\n", oargv[0]);
printf("Commands: [Note: both '/' and '-' are accepted as command prefixes.] \n"); printf("Commands: [Note: both '/' and '-' are accepted as command prefixes.] \n");
printf(" /format <sectors> [<filesystem>] Formats the disk image.\n"); printf(" /format <sectors> [<filesystem>] Formats the disk image.\n");
printf(" /boot <sector file> Writes a new boot sector.\n"); printf(" /boot <sector file> Writes a new boot sector.\n");
printf(" /add <src path> <dst path> Copies an external file or directory\n" printf(" /add <src path> <dst path> Copies an external file or directory\n"
" into the image.\n"); " into the image.\n");
printf(" /extract <src path> <dst path> Copies a file or directory from 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"); " into an external file or directory.\n");
printf(" /move <src path> <new path> Moves/renames a 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(" /copy <src path> <new path> Copies a file or directory.\n");
printf(" /mkdir <src path> <new path> Creates a directory.\n"); printf(" /mkdir <src path> <new path> Creates a directory.\n");
printf(" /rmdir <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(" /list [<pattern>] Lists files a directory (defaults to root).\n");
//printf(" /recursive Enables recursive processing for directories.\n"); //printf(" /recursive Enables recursive processing for directories.\n");
return 1; return 1;
} }