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

View file

@ -20,344 +20,344 @@ int isMounted = 0;
// tool needed by fatfs
DWORD get_fattime()
{
/* 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) */
/* 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_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
time(&rawtime);
timeinfo = localtime(&rawtime);
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,
}
};
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;
return myTime.whole;
}
BOOL is_command(const char* parg)
{
return (parg[0]=='/') || (parg[0] == '-');
return (parg[0] == '/') || (parg[0] == '-');
}
#define NEED_PARAMS(_min_,_max_) \
do {\
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; } \
} while(0)
do {\
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; } \
} while(0)
BOOL need_mount()
{
if(isMounted)
return FR_OK;
if (isMounted)
return FR_OK;
int r = f_mount(&g_Filesystem, "0:", 0);
if(r)
return r;
int r = f_mount(&g_Filesystem, "0:", 0);
if (r)
return r;
isMounted = 1;
return FR_OK;
isMounted = 1;
return FR_OK;
}
#define NEED_MOUNT() \
do { ret = need_mount(); if(ret) \
{\
printf("Error: could not mount image file '%s' (%d). \n", imageFileName, ret); \
goto print_help; \
} } while(0)
do { ret = need_mount(); if(ret) \
{\
printf("Error: could not mount image file '%s' (%d). \n", imageFileName, ret); \
goto print_help; \
} } while(0)
int main(int oargc, char* oargv[])
{
int ret;
int argc = oargc-1;
char** argv = oargv+1;
int ret;
int argc = oargc - 1;
char** argv = oargv + 1;
// first parameter must be the image file.
if(argc == 0)
{
goto print_help;
}
// first parameter must be the image file.
if (argc == 0)
{
goto print_help;
}
if(is_command(argv[0]))
{
printf("Error: first parameter must be a filename, found '%s' instead. \n", argv[0]);
goto print_help;
}
if (is_command(argv[0]))
{
printf("Error: first parameter must be a filename, found '%s' instead. \n", argv[0]);
goto print_help;
}
imageFileName = argv[0];
imageFileName = argv[0];
if(disk_initialize(0))
{
printf("Error: could not open image file '%s'. \n", imageFileName);
goto print_help;
}
if (disk_initialize(0))
{
printf("Error: could not open image file '%s'. \n", imageFileName);
goto print_help;
}
argc--;
argv++;
argc--;
argv++;
while(argc>0)
{
char *parg = *argv;
while (argc > 0)
{
char *parg = *argv;
if(!is_command(parg))
{
printf("Error: Expected a command, found '%s' instead. \n",parg);
goto print_help;
}
if (!is_command(parg))
{
printf("Error: Expected a command, found '%s' instead. \n", parg);
goto print_help;
}
parg++;
argv++;
argc--;
parg++;
argv++;
argc--;
// find next command, to calculare number of args
int nargs = 0;
int i=0;
while((argv[i] != NULL) && !is_command(argv[i++]))
nargs++;
// find next command, to calculare number of args
int nargs = 0;
int i = 0;
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
if (strcmp(parg, "format") == 0)
{
// 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
int sectors = atoi(argv[0]);
// Arg 1: number of sectors
int sectors = atoi(argv[0]);
if(sectors <= 0)
{
printf("Error: Sectors must be > 0\n");
return 1;
}
if (sectors <= 0)
{
printf("Error: Sectors must be > 0\n");
return 1;
}
disk_ioctl(0, SET_SECTOR_COUNT, &sectors);
disk_ioctl(0, SET_SECTOR_COUNT, &sectors);
ret = f_mkfs("0:", 1, 4096);
if (ret)
{
printf("ERROR: Formatting drive: %d.\n", ret);
goto print_help;
}
}
else if(strcmp(parg,"boot")==0)
{
NEED_PARAMS(1,1);
ret = f_mkfs("0:", 1, 4096);
if (ret)
{
printf("ERROR: Formatting drive: %d.\n", ret);
goto print_help;
}
}
else if (strcmp(parg, "boot") == 0)
{
NEED_PARAMS(1, 1);
// Arg 1: boot file
printf("Not Implemented.");
}
else if(strcmp(parg,"add")==0)
{
NEED_PARAMS(2,2);
// Arg 1: boot file
printf("Not Implemented.");
}
else if (strcmp(parg, "add") == 0)
{
NEED_PARAMS(2, 2);
NEED_MOUNT();
NEED_MOUNT();
// Arg 1: external file to add
// Arg 2: virtual filename
// Arg 1: external file to add
// Arg 2: virtual filename
FILE* fe;
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;
}
FILE* fe;
FIL fv = { 0 };
char buff[32768];
UINT rdlen = 0;
UINT wrlen = 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;
}
while( (rdlen = fread(buff,1,32768,fe)) > 0 )
{
f_write(&fv,buff, rdlen, &wrlen);
}
char buff[32768];
UINT rdlen = 0;
UINT wrlen = 0;
fclose(fe);
f_close(&fv);
}
else if(strcmp(parg,"extract")==0)
{
NEED_PARAMS(2,2);
while ((rdlen = fread(buff, 1, 32768, fe)) > 0)
{
f_write(&fv, buff, rdlen, &wrlen);
}
NEED_MOUNT();
fclose(fe);
f_close(&fv);
}
else if (strcmp(parg, "extract") == 0)
{
NEED_PARAMS(2, 2);
// Arg 1: virtual file to extract
// Arg 2: external filename
NEED_MOUNT();
FIL fe = {0};
FILE* fv;
// 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]);
return 1;
}
if(fopen_s(&fv, argv[1],"wb"))
{
printf("Error: unable to open external file '%s' for writing.", argv[1]);
return 1;
}
FIL fe = { 0 };
FILE* fv;
char buff[32768];
UINT rdlen = 0;
UINT wrlen = 0;
if (f_open(&fe, argv[0], FA_READ))
{
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) )
{
fwrite(buff, 1, rdlen, fv);
}
char buff[32768];
UINT rdlen = 0;
UINT wrlen = 0;
f_close(&fe);
fclose(fv);
}
else if(strcmp(parg,"move")==0)
{
NEED_PARAMS(2,2);
while ((f_read(&fe, buff, 32768, &rdlen) == 0) && (rdlen > 0))
{
fwrite(buff, 1, rdlen, fv);
}
NEED_MOUNT();
// Arg 1: src path & filename
// Arg 2: new path & filename
f_close(&fe);
fclose(fv);
}
else if (strcmp(parg, "move") == 0)
{
NEED_PARAMS(2, 2);
if(f_rename(argv[0], argv[1]))
printf("Error moving/renaming '%s' to '%s'", argv[0],argv[1]);
}
else if(strcmp(parg,"copy")==0)
{
NEED_PARAMS(2,2)
NEED_MOUNT();
// Arg 1: src path & filename
// Arg 2: new path & filename
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)
{
NEED_PARAMS(2, 2);
FIL fe = {0};
FIL fv = {0};
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]);
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;
}
FIL fe = { 0 };
FIL fv = { 0 };
char buff[32768];
UINT rdlen = 0;
UINT wrlen = 0;
if (f_open(&fe, argv[0], FA_READ))
{
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) )
{
f_write(&fv, buff, rdlen, &wrlen);
}
char buff[32768];
UINT rdlen = 0;
UINT wrlen = 0;
f_close(&fe);
f_close(&fv);
}
else if(strcmp(parg,"mkdir")==0)
{
NEED_PARAMS(1,1);
while ((f_read(&fe, buff, 32768, &rdlen) == 0) && (rdlen > 0))
{
f_write(&fv, buff, rdlen, &wrlen);
}
NEED_MOUNT();
// Arg 1: folder path
f_mkdir(argv[1]);
}
else if(strcmp(parg,"delete")==0)
{
NEED_PARAMS(1,1);
f_close(&fe);
f_close(&fv);
}
else if (strcmp(parg, "mkdir") == 0)
{
NEED_PARAMS(1, 1);
NEED_MOUNT();
// Arg 1: file/folder path (cannot delete non-empty folders)
f_unlink(argv[1]);
}
else if(strcmp(parg,"list")==0)
{
NEED_PARAMS(0,1);
NEED_MOUNT();
// Arg 1: folder path
f_mkdir(argv[1]);
}
else if (strcmp(parg, "delete") == 0)
{
NEED_PARAMS(1, 1);
// Arg 1: folder path (optional)
char* root = "/";
NEED_MOUNT();
// 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)
{
root = argv[0];
}
// Arg 1: folder path (optional)
char* root = "/";
DIR dir = {0};
if (nargs == 1)
{
root = argv[0];
}
if(f_opendir(&dir, root))
{
printf("Error opening directory '%s'.\n",root);
return 1;
}
DIR dir = { 0 };
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};
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;
}
printf("Listing directory contents of: %s\n", root);
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:
printf("Syntax: %s image_file [list of commands]\n\n", oargv[0]);
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");
printf("Syntax: %s image_file [list of commands]\n\n", oargv[0]);
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");
return 1;
return 1;
}