119119/ Fixed null pointer dereference on attempting to delete the root direcotry.
120120/---------------------------------------------------------------------------*/
121121
122+ #include <string.h>
123+
122124#include "ff.h" /* Declarations of FatFs API */
123125#include "diskio.h" /* Declarations of disk I/O functions */
124126
@@ -506,7 +508,8 @@ static FATFS *FatFs[_VOLUMES]; /* Pointer to the file system objects (logical dr
506508static WORD Fsid ; /* File system mount ID */
507509
508510#if _FS_RPATH && _VOLUMES >= 2
509- static BYTE CurrVol ; /* Current drive */
511+ // dpgeorge: changed name and made non-static
512+ BYTE ff_CurrVol ; /* Current drive */
510513#endif
511514
512515#if _FS_LOCK
@@ -559,6 +562,12 @@ static const BYTE ExCvt[] = _EXCVT; /* Upper conversion table for extended chara
559562/* String functions */
560563/*-----------------------------------------------------------------------*/
561564
565+ // dpgeorge:
566+ // We use stdlib version of memset and memcmp to reduce code size.
567+ // We use the original custom version of mem_cpy so that gcc doesn't
568+ // recognise the builtin function and then inline it. Allowing gcc
569+ // to use the builtin memcpy increases code size by 72 bytes.
570+
562571/* Copy memory to memory */
563572static
564573void mem_cpy (void * dst , const void * src , UINT cnt ) {
@@ -577,15 +586,21 @@ void mem_cpy (void* dst, const void* src, UINT cnt) {
577586}
578587
579588/* Fill memory */
589+ #if 0
580590static
581591void mem_set (void * dst , int val , UINT cnt ) {
582592 BYTE * d = (BYTE * )dst ;
583593
584594 while (cnt -- )
585595 * d ++ = (BYTE )val ;
586596}
597+ #else
598+ // use stdlib
599+ #define mem_set memset
600+ #endif
587601
588602/* Compare memory to memory */
603+ #if 0
589604static
590605int mem_cmp (const void * dst , const void * src , UINT cnt ) {
591606 const BYTE * d = (const BYTE * )dst , * s = (const BYTE * )src ;
@@ -594,6 +609,10 @@ int mem_cmp (const void* dst, const void* src, UINT cnt) {
594609 while (cnt -- && (r = * d ++ - * s ++ ) == 0 ) ;
595610 return r ;
596611}
612+ #else
613+ // use stdlib
614+ #define mem_cmp memcmp
615+ #endif
597616
598617/* Check if chr is contained in the string */
599618static
@@ -854,6 +873,8 @@ FRESULT sync_fs ( /* FR_OK: successful, FR_DISK_ERR: failed */
854873/*-----------------------------------------------------------------------*/
855874/* Hidden API for hacks and disk tools */
856875
876+ // dpgeorge: made static
877+ static
857878DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */
858879 FATFS * fs , /* File system object */
859880 DWORD clst /* Cluster# to be converted */
@@ -872,6 +893,8 @@ DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */
872893/*-----------------------------------------------------------------------*/
873894/* Hidden API for hacks and disk tools */
874895
896+ // dpgeorge: made static
897+ static
875898DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x0FFFFFFF:Cluster status */
876899 FATFS * fs , /* File system object */
877900 DWORD clst /* FAT item index (cluster#) to get the value */
@@ -927,6 +950,8 @@ DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x0FFFFFFF:Cluste
927950/* Hidden API for hacks and disk tools */
928951
929952#if !_FS_READONLY
953+ // dpgeorge: made static
954+ static
930955FRESULT put_fat (
931956 FATFS * fs , /* File system object */
932957 DWORD clst , /* FAT item index (cluster#) to be set */
@@ -2093,6 +2118,8 @@ FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */
20932118/* Get logical drive number from path name */
20942119/*-----------------------------------------------------------------------*/
20952120
2121+ // dpgeorge: replaced with custom ff_get_ldnumber
2122+ #if 0
20962123static
20972124int get_ldnumber ( /* Returns logical drive number (-1:invalid drive) */
20982125 const TCHAR * * path /* Pointer to pointer to the path name */
@@ -2144,6 +2171,7 @@ int get_ldnumber ( /* Returns logical drive number (-1:invalid drive) */
21442171 }
21452172 return vol ;
21462173}
2174+ #endif
21472175
21482176
21492177
@@ -2197,7 +2225,7 @@ FRESULT find_volume ( /* FR_OK(0): successful, !=0: any error occurred */
21972225
21982226 /* Get logical drive number from the path name */
21992227 * rfs = 0 ;
2200- vol = get_ldnumber (path );
2228+ vol = ff_get_ldnumber (path );
22012229 if (vol < 0 ) return FR_INVALID_DRIVE ;
22022230
22032231 /* Check if the file system object is valid or not */
@@ -2397,7 +2425,7 @@ FRESULT f_mount (
23972425 const TCHAR * rp = path ;
23982426
23992427
2400- vol = get_ldnumber (& rp );
2428+ vol = ff_get_ldnumber (& rp );
24012429 if (vol < 0 ) return FR_INVALID_DRIVE ;
24022430 cfs = FatFs [vol ]; /* Pointer to fs object */
24032431
@@ -2885,10 +2913,10 @@ FRESULT f_chdrive (
28852913 int vol ;
28862914
28872915
2888- vol = get_ldnumber (& path );
2916+ vol = ff_get_ldnumber (& path );
28892917 if (vol < 0 ) return FR_INVALID_DRIVE ;
28902918
2891- CurrVol = (BYTE )vol ;
2919+ ff_CurrVol = (BYTE )vol ;
28922920
28932921 return FR_OK ;
28942922}
@@ -2984,11 +3012,19 @@ FRESULT f_getcwd (
29843012 tp = buff ;
29853013 if (res == FR_OK ) {
29863014#if _VOLUMES >= 2
3015+ // dpgeorge: change to use our volume names
3016+ #if 0
29873017 * tp ++ = '0' + CurrVol ; /* Put drive number */
29883018 * tp ++ = ':' ;
3019+ #else
3020+ ff_get_volname (ff_CurrVol , & tp );
3021+ #endif
29893022#endif
29903023 if (i == len ) { /* Root-directory */
3024+ // dpgeorge: not needed with volume names
3025+ #if 0
29913026 * tp ++ = '/' ;
3027+ #endif
29923028 } else { /* Sub-directroy */
29933029 do /* Add stacked path str */
29943030 * tp ++ = buff [i ++ ];
@@ -3671,7 +3707,7 @@ FRESULT f_rename (
36713707 } else {
36723708 mem_cpy (buf , djo .dir + DIR_Attr , 21 ); /* Save the object information except name */
36733709 mem_cpy (& djn , & djo , sizeof (DIR )); /* Duplicate the directory object */
3674- if (get_ldnumber (& path_new ) >= 0 ) /* Snip drive number off and ignore it */
3710+ if (ff_get_ldnumber (& path_new ) >= 0 ) /* Snip drive number off and ignore it */
36753711 res = follow_path (& djn , path_new ); /* and make sure if new object name is not conflicting */
36763712 else
36773713 res = FR_INVALID_DRIVE ;
@@ -3850,6 +3886,7 @@ FRESULT f_setlabel (
38503886
38513887 /* Create a volume label in directory form */
38523888 vn [0 ] = 0 ;
3889+ if (label [0 ] == '/' ) label ++ ; // dpgeorge: skip '/' to handle our volume names
38533890 for (sl = 0 ; label [sl ]; sl ++ ) ; /* Get name length */
38543891 for ( ; sl && label [sl - 1 ] == ' ' ; sl -- ) ; /* Remove trailing spaces */
38553892 if (sl ) { /* Create volume label in directory form */
@@ -4010,7 +4047,7 @@ FRESULT f_mkfs (
40104047
40114048 /* Check mounted drive and clear work area */
40124049 if (sfd > 1 ) return FR_INVALID_PARAMETER ;
4013- vol = get_ldnumber (& path );
4050+ vol = ff_get_ldnumber (& path );
40144051 if (vol < 0 ) return FR_INVALID_DRIVE ;
40154052 fs = FatFs [vol ];
40164053 if (!fs ) return FR_NOT_ENABLED ;
0 commit comments