#include "diskio.h"
#include <stdio.h>
-extern char* g_imageFileName;
-
/*-----------------------------------------------------------------------*/
/* Correspondence between physical drive number and image file handles. */
+UINT sectorCount[1] = { 0 };
FILE* driveHandle[1] = { NULL };
const int driveHandleCount = sizeof(driveHandle) / sizeof(FILE*);
/*-----------------------------------------------------------------------*/
-/* Inidialize a Drive */
+/* Open an image file a Drive */
/*-----------------------------------------------------------------------*/
-DSTATUS disk_initialize(
- BYTE pdrv /* Physical drive nmuber (0..) */
- )
+DSTATUS disk_openimage(BYTE pdrv, const char* imageFileName)
{
- if (pdrv == 0) /* only one drive (image file) supported atm. */
+ if (pdrv < driveHandleCount)
{
if (driveHandle[0] != NULL)
return 0;
- driveHandle[0] = fopen(g_imageFileName, "r+b");
- if(!driveHandle[0])
+ driveHandle[0] = fopen(imageFileName, "r+b");
+ if (!driveHandle[0])
{
- driveHandle[0] = fopen(g_imageFileName, "w+");
+ driveHandle[0] = fopen(imageFileName, "w+");
}
if (driveHandle[0] != NULL)
}
-
/*-----------------------------------------------------------------------*/
-/* Get Disk Status */
+/* Cleanup a Drive */
/*-----------------------------------------------------------------------*/
-DSTATUS disk_status(
+VOID disk_cleanup(
BYTE pdrv /* Physical drive nmuber (0..) */
)
{
if (pdrv < driveHandleCount)
{
if (driveHandle[pdrv] != NULL)
- return 0;
+ {
+ fclose(driveHandle[pdrv]);
+ driveHandle[pdrv] = NULL;
+ }
+ }
+}
+
+/*-----------------------------------------------------------------------*/
+/* Inidialize a Drive */
+/*-----------------------------------------------------------------------*/
+
+DSTATUS disk_initialize(
+ BYTE pdrv /* Physical drive nmuber (0..) */
+ )
+{
+ if (pdrv == 0) /* only one drive (image file) supported atm. */
+ {
+ return 0;
}
return STA_NOINIT;
}
/*-----------------------------------------------------------------------*/
-/* Cleanup a Drive */
+/* Get Disk Status */
/*-----------------------------------------------------------------------*/
-VOID disk_cleanup(
+DSTATUS disk_status(
BYTE pdrv /* Physical drive nmuber (0..) */
)
{
if (pdrv < driveHandleCount)
{
if (driveHandle[pdrv] != NULL)
- {
- fclose(driveHandle[pdrv]);
- driveHandle[pdrv] = NULL;
- }
+ return 0;
}
+ return STA_NOINIT;
}
/*-----------------------------------------------------------------------*/
return RES_OK;
case GET_SECTOR_COUNT:
{
- int temp = 0;
- if(fseek(driveHandle[pdrv], 0, SEEK_END))
- printf("fseek failed!\n");
- else
- temp = ftell(driveHandle[pdrv]);
- *(DWORD*)buff = temp/512;
+ if (sectorCount[pdrv] <= 0)
+ {
+ if (fseek(driveHandle[pdrv], 0, SEEK_END))
+ printf("fseek failed!\n");
+ else
+ sectorCount[pdrv] = ftell(driveHandle[pdrv]) / 512;
+ }
+
+ *(DWORD*)buff = sectorCount[pdrv];
return RES_OK;
}
case SET_SECTOR_COUNT:
int count = *(DWORD*)buff;
long size;
+ sectorCount[pdrv] = count;
+
fseek(driveHandle[pdrv], 0, SEEK_END);
size = ftell(driveHandle[pdrv]) / 512;
/*---------------------------------------*/
/* Prototypes for disk control functions */
+DSTATUS disk_openimage(BYTE pdrv, const char* imageFileName);
+VOID disk_cleanup(BYTE pdrv);
DSTATUS disk_initialize (BYTE pdrv);
DSTATUS disk_status (BYTE pdrv);
-VOID disk_cleanup (BYTE pdrv);
DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count);
DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
#define CTRL_ERASE_SECTOR 4 /* Force erased a block of sectors (for only _USE_ERASE) */
/* Custom command for image file resizing */
-#define SET_SECTOR_COUNT 253
+#define SET_SECTOR_COUNT 126
#ifdef __cplusplus
}
#include "fatfs/ff.h"
#include "fatfs/diskio.h"
-char* g_imageFileName;
-
FATFS g_Filesystem;
static int isMounted = 0;
#define NEED_MOUNT() \
do { ret = need_mount(); if(ret) \
{\
- printf("Error: could not mount image file '%s' (%d). \n", g_imageFileName, ret); \
+ printf("Error: could not mount disk (%d). \n", ret); \
PRINT_HELP_AND_QUIT(); \
} } while(0)
PRINT_HELP_AND_QUIT();
}
- g_imageFileName = argv[0];
+ if (disk_openimage(0, argv[0]))
+ {
+ printf("Error: could not open image file '%s'. \n", argv[0]);
+ PRINT_HELP_AND_QUIT();
+ }
argc--;
argv++;
NEED_PARAMS(1, 1);
- NEED_MOUNT();
-
// Arg 1: number of sectors
sectors = atoi(argv[0]);
disk_ioctl(0, SET_SECTOR_COUNT, §ors);
+ NEED_MOUNT();
+
ret = f_mkfs("0:", 1, sectors < 4096 ? 1 : 8);
if (ret)
{