[FATTEN]
authorDavid Quintana <gigaherz@gmail.com>
Tue, 8 Sep 2015 22:20:37 +0000 (22:20 +0000)
committerDavid Quintana <gigaherz@gmail.com>
Tue, 8 Sep 2015 22:20:37 +0000 (22:20 +0000)
* Fix the breakage I commited. Hopefully.

svn path=/trunk/; revision=69132

reactos/tools/fatten/fatfs/diskio.c
reactos/tools/fatten/fatfs/diskio.h
reactos/tools/fatten/fatten.c

index c90c65e..c77dd20 100644 (file)
@@ -9,31 +9,28 @@
 #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)
@@ -43,19 +40,35 @@ DSTATUS disk_initialize(
 }
 
 
-
 /*-----------------------------------------------------------------------*/
-/* 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;
 }
@@ -63,21 +76,19 @@ DSTATUS disk_status(
 
 
 /*-----------------------------------------------------------------------*/
-/* 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;
 }
 
 /*-----------------------------------------------------------------------*/
@@ -177,12 +188,15 @@ DRESULT disk_ioctl(
                 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:
@@ -190,6 +204,8 @@ DRESULT disk_ioctl(
                 int count = *(DWORD*)buff;
                 long size;
 
+                sectorCount[pdrv] = count;
+
                 fseek(driveHandle[pdrv], 0, SEEK_END);
                 size = ftell(driveHandle[pdrv]) / 512;
 
index 4952f22..b60585a 100644 (file)
@@ -31,10 +31,11 @@ typedef enum {
 /*---------------------------------------*/
 /* 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);
@@ -57,7 +58,7 @@ 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
 }
index a9121ff..90ed625 100644 (file)
@@ -11,8 +11,6 @@
 #include "fatfs/ff.h"
 #include "fatfs/diskio.h"
 
-char* g_imageFileName;
-
 FATFS g_Filesystem;
 
 static int isMounted = 0;
@@ -89,7 +87,7 @@ int need_mount()
 #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)
 
@@ -136,7 +134,11 @@ int main(int oargc, char* oargv[])
         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++;
@@ -168,8 +170,6 @@ int main(int oargc, char* oargv[])
 
             NEED_PARAMS(1, 1);
 
-            NEED_MOUNT();
-
             // Arg 1: number of sectors
             sectors = atoi(argv[0]);
 
@@ -182,6 +182,8 @@ int main(int oargc, char* oargv[])
 
             disk_ioctl(0, SET_SECTOR_COUNT, &sectors);
 
+            NEED_MOUNT();
+
             ret = f_mkfs("0:", 1, sectors < 4096 ? 1 : 8);
             if (ret)
             {