summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSinthu Raja M2018-11-16 04:57:43 -0600
committerM V Pratap Reddy2018-12-04 15:27:56 -0600
commitf87af37241c3f14d0ae5d28f220d9b7d13b4c966 (patch)
treed86b9f3dbac9a84747eea446c05b0c353a6076f3
parent473f3e61f704591b36bfbb44527d0eed68d087b2 (diff)
downloadfatfs-f87af37241c3f14d0ae5d28f220d9b7d13b4c966.tar.gz
fatfs-f87af37241c3f14d0ae5d28f220d9b7d13b4c966.tar.xz
fatfs-f87af37241c3f14d0ae5d28f220d9b7d13b4c966.zip
PRSDK-4440 Address Review comments
Add API to check free index availability in VolToPart structure. Update Fatfs_Close API to verify error cases. Moved MACROS to header files. Replaced magic numbers with macros. Changed logic of accessing VolToPart structure with multiple open and close of Fatfs driver.
-rw-r--r--FATFS.h29
-rw-r--r--src/FATFS_drv.c132
2 files changed, 111 insertions, 50 deletions
diff --git a/FATFS.h b/FATFS.h
index 6d9ddac..15179a6 100644
--- a/FATFS.h
+++ b/FATFS.h
@@ -68,9 +68,36 @@ extern "C" {
68 */ 68 */
69#define FATFS_ERR (-(1)) 69#define FATFS_ERR (-(1))
70 70
71/*Offset macros of MBR table */
72/**
73 * @def MBR_Table
74 * MBR: Partition table offset
75 */
76#define MBR_Table 446
77
78/**
79 * @def SZ_PTE
80 * MBR: Size of a partition table entry
81 */
82#define SZ_PTE 16
83
84/**
85 * @def NUM_OF_PARTITIONS
86 * Number of partitions supported
87 */
88#define NUM_OF_PARTITIONS 4
89
90/**
91 * @def MBR_PT_TYPE_OFFSET
92 * Partition Type offset of each partition entry in MBR
93 */
94#define MBR_PT_TYPE_OFFSET 4
95
71/*@}*/ 96/*@}*/
72/** @} */ 97/** @} */
73 98
99
100
74/*! 101/*!
75 * @brief Return status of FATFS. 102 * @brief Return status of FATFS.
76 */ 103 */
@@ -212,7 +239,7 @@ typedef struct FATFS_Object_s {
212 opened. */ 239 opened. */
213 uint32_t driveNumber; /*!< Drive number used by FatFs */ 240 uint32_t driveNumber; /*!< Drive number used by FatFs */
214 DSTATUS diskState; /*!< Disk status */ 241 DSTATUS diskState; /*!< Disk status */
215 FATFS filesystem[4]; /*!< FATFS data object */ 242 FATFS filesystem[NUM_OF_PARTITIONS]; /*!< FATFS data object */
216 243
217 FATFS_ConfigParams FATFSConfigParams; /*! Stores SD parameters */ 244 FATFS_ConfigParams FATFSConfigParams; /*! Stores SD parameters */
218 245
diff --git a/src/FATFS_drv.c b/src/FATFS_drv.c
index bb9e1ab..16934f1 100644
--- a/src/FATFS_drv.c
+++ b/src/FATFS_drv.c
@@ -50,13 +50,8 @@
50 50
51#define DRIVE_NOT_MOUNTED (~((uint32_t)0U)) 51#define DRIVE_NOT_MOUNTED (~((uint32_t)0U))
52 52
53/*Offset macros of MBR table */
54#define MBR_Table 446 /* MBR: Partition table offset */
55#define SZ_PTE 16 /* MBR: Size of a partition table entry */
56#define BS_55AA 510 /* Signature Word */
57
58/* Table to map multi-partition with multi-storage device*/ 53/* Table to map multi-partition with multi-storage device*/
59PARTITION VolToPart[] = { 54PARTITION VolToPart[NUM_OF_PARTITIONS] = {
60 {0xf /*pd */,0xf /* pt */}, 55 {0xf /*pd */,0xf /* pt */},
61 {0xf /*pd */,0xf /* pt */}, 56 {0xf /*pd */,0xf /* pt */},
62 {0xf /*pd */,0xf /* pt */}, 57 {0xf /*pd */,0xf /* pt */},
@@ -72,15 +67,14 @@ static DRESULT FATFS_diskRead(BYTE drv, BYTE *buf, DWORD sector, BYTE count);
72static DSTATUS FATFS_diskStatus(BYTE drv); 67static DSTATUS FATFS_diskStatus(BYTE drv);
73static DRESULT FATFS_diskWrite(BYTE drv, const BYTE *buf, DWORD sector, 68static DRESULT FATFS_diskWrite(BYTE drv, const BYTE *buf, DWORD sector,
74 BYTE count); 69 BYTE count);
75static FATFS_Error FATFS_readBootSector(BYTE index); 70static FATFS_Error FATFS_readBootSector(BYTE drv);
71static int32_t FATFS_getFreeVolIndex();
76 72
77/* Externs */ 73/* Externs */
78extern const FATFSConfigList FATFS_config; 74extern const FATFSConfigList FATFS_config;
79 75
80/* Used to check status and initialization */ 76/* Used to check status and initialization */
81static int32_t FATFS_count = -1; 77static int32_t FATFS_count = -1;
82/*Used to track the multi-partition VolToPart index*/
83static int32_t Fatfs_volumeIndex = 0;
84 78
85/* Default SD parameters structure */ 79/* Default SD parameters structure */
86const FATFS_ConfigParams FATFS_defaultParams = { 80const FATFS_ConfigParams FATFS_defaultParams = {
@@ -97,6 +91,7 @@ FATFS_Error FATFS_close(FATFS_Handle handle)
97{ 91{
98 DRESULT dresult; 92 DRESULT dresult;
99 FRESULT fresult; 93 FRESULT fresult;
94 uint32_t volIndex = 0;
100 FATFS_Object *object = NULL; 95 FATFS_Object *object = NULL;
101#ifdef LOG_EN 96#ifdef LOG_EN
102 FATFS_HwAttrs const *hwAttrs = NULL; 97 FATFS_HwAttrs const *hwAttrs = NULL;
@@ -114,12 +109,23 @@ FATFS_Error FATFS_close(FATFS_Handle handle)
114 /* Clear Driver handle only when it is opened */ 109 /* Clear Driver handle only when it is opened */
115 if(1 == object->isOpen) 110 if(1 == object->isOpen)
116 { 111 {
117 ((FATFS_Config *) handle)->drvFxnTablePtr->closeDrvFxn(object->drvHandle); 112 ((FATFS_Config *) handle)->drvFxnTablePtr->closeDrvFxn(object->drvHandle);
113 }
114
115 /*Reset */
116 for(volIndex = 0; volIndex < NUM_OF_PARTITIONS; volIndex++)
117 {
118 if(VolToPart[volIndex].pd == object->driveNumber)
119 {
120 /* Unmount the FatFs drive partition(s)*/
121 snprintf(drive_path,sizeof(drive_path),"%d:",(int)volIndex);
122 fresult = f_mount(NULL,drive_path,0);
123
124 VolToPart[volIndex].pd = 0xf;
125 VolToPart[volIndex].pt = 0xf;
126 }
118 } 127 }
119 128
120 /* Unmount the FatFs drive */
121 snprintf(drive_path,sizeof(drive_path),"%d:",(int)object->driveNumber);
122 fresult = f_mount(NULL,drive_path,0);
123 if (fresult != FR_OK) { 129 if (fresult != FR_OK) {
124#ifdef LOG_EN 130#ifdef LOG_EN
125 Log_print2(Diags_USER1, 131 Log_print2(Diags_USER1,
@@ -195,12 +201,14 @@ FATFS_Error FATFS_open(uint32_t index,
195 FATFS_Object *object = NULL; 201 FATFS_Object *object = NULL;
196 FATFS_HwAttrs const *hwAttrs = NULL; 202 FATFS_HwAttrs const *hwAttrs = NULL;
197 TCHAR drive_path[5]; 203 TCHAR drive_path[5];
204 int32_t volIndex = 0;
198 205
199 /* Input parameter validation */ 206 /* Input parameter validation */
200 FATFS_osalAssert((FATFS_Handle)&(FATFS_config[index]) == NULL); 207 FATFS_osalAssert((FATFS_Handle)&(FATFS_config[index]) == NULL);
201 208
202 /*Verify the partition counts which does not goes beyond max volumes*/ 209 /*Verify there is free index available in VolToPart for mapping this disk partitions */
203 if(VolToPart[_VOLUMES - 1].pd != 0xf) 210 volIndex = FATFS_getFreeVolIndex();
211 if(FATFS_ERR == volIndex)
204 { 212 {
205 return ret; 213 return ret;
206 } 214 }
@@ -225,13 +233,9 @@ FATFS_Error FATFS_open(uint32_t index,
225 object->FATFSConfigParams.custom = ((FATFS_ConfigParams *) params)->custom; 233 object->FATFSConfigParams.custom = ((FATFS_ConfigParams *) params)->custom;
226 } 234 }
227 235
228 if( VolToPart[Fatfs_volumeIndex].pd ==0xf) 236 /*Add a new entry for the first default partition*/
229 { 237 VolToPart[volIndex].pt = 1;
230 /*Add a new entry for the first default partition*/ 238 VolToPart[volIndex].pd = hwAttrs->drvInst;
231 VolToPart[Fatfs_volumeIndex].pt = 1;
232 VolToPart[Fatfs_volumeIndex].pd = hwAttrs->drvInst;
233 }
234
235 239
236 if(0 != object->isOpen) 240 if(0 != object->isOpen)
237 { 241 {
@@ -263,8 +267,8 @@ FATFS_Error FATFS_open(uint32_t index,
263 FATFS_close(handle); 267 FATFS_close(handle);
264 268
265 /*Restore VolToPart to remove the previous entry*/ 269 /*Restore VolToPart to remove the previous entry*/
266 VolToPart[Fatfs_volumeIndex].pt = 0xf; 270 VolToPart[volIndex].pt = 0xf;
267 VolToPart[Fatfs_volumeIndex].pd = 0xf; 271 VolToPart[volIndex].pd = 0xf;
268 ret = FATFS_ERR; 272 ret = FATFS_ERR;
269 } 273 }
270 } 274 }
@@ -275,7 +279,7 @@ FATFS_Error FATFS_open(uint32_t index,
275 * Register the filesystem with FatFs. 279 * Register the filesystem with FatFs.
276 */ 280 */
277 { 281 {
278 snprintf(drive_path,sizeof(drive_path),"%d:",(int)Fatfs_volumeIndex++); 282 snprintf(drive_path,sizeof(drive_path),"%d:",(int)volIndex);
279 /*Mount First partition of device to read the MBR*/ 283 /*Mount First partition of device to read the MBR*/
280 fresult = f_mount(&(object->filesystem[0]),drive_path,1); 284 fresult = f_mount(&(object->filesystem[0]),drive_path,1);
281 285
@@ -287,8 +291,8 @@ FATFS_Error FATFS_open(uint32_t index,
287 FATFS_close(handle); 291 FATFS_close(handle);
288 292
289 /*Restore VolToPart to remove the previous entry*/ 293 /*Restore VolToPart to remove the previous entry*/
290 VolToPart[--Fatfs_volumeIndex].pt = 0xf; 294 VolToPart[volIndex].pt = 0xf;
291 VolToPart[Fatfs_volumeIndex].pd = 0xf; 295 VolToPart[volIndex].pd = 0xf;
292 ret = FATFS_ERR; 296 ret = FATFS_ERR;
293 } 297 }
294 298
@@ -587,6 +591,7 @@ static DRESULT FATFS_diskWrite(BYTE drv, const BYTE *buf,
587 } 591 }
588 return res; 592 return res;
589} 593}
594
590/* 595/*
591 * ======== FATFS_readBootSector ======== 596 * ======== FATFS_readBootSector ========
592 * Function to perform a disk read from the storage device to read MBR. 597 * Function to perform a disk read from the storage device to read MBR.
@@ -597,24 +602,26 @@ static DRESULT FATFS_diskWrite(BYTE drv, const BYTE *buf,
597 * 602 *
598*/ 603*/
599 604
600FATFS_Error FATFS_readBootSector(BYTE index) 605static FATFS_Error FATFS_readBootSector(BYTE drv)
601{ 606{
602 BYTE *pt; 607 BYTE *pt;
603 uint32_t i; 608 uint32_t i;
609 int32_t index = 0;
610 FATFS_Error res = FATFS_OK;
604#if defined(__GNUC__) || defined(gcc) || \ 611#if defined(__GNUC__) || defined(gcc) || \
605 defined(__aarch64__) || defined (__TI_ARM_V7R4__) || defined(__ARM_ARCH_7A__) || defined (__TI_ARM_V7M4__) || defined (__TI_ARM_V5__) 612 defined(__aarch64__) || defined (__TI_ARM_V7R4__) || defined(__ARM_ARCH_7A__) || defined (__TI_ARM_V7M4__) || defined (__TI_ARM_V5__)
606 BYTE buffer[_MAX_SS] __attribute__ ((aligned (128U))); 613 BYTE buffer[_MAX_SS] __attribute__ ((aligned (128U)));
607#else 614#else
608 BYTE buffer[_MAX_SS]; 615 BYTE buffer[_MAX_SS];
609#endif 616#endif
610 TCHAR drive_path[5]; 617 TCHAR drive_path[5];
611 DWORD sector =0; 618 DWORD sector =0;
612 FATFS_Object *object = NULL; 619 FATFS_Object *object = NULL;
613 620
614 object = ((FATFS_Config *)&(FATFS_config[index]))->object; 621 object = ((FATFS_Config *)&(FATFS_config[drv]))->object;
615 622
616 /*Read MBR from the given storage device*/ 623 /*Read MBR from the given storage device*/
617 if ((FATFS_diskRead(index,buffer,sector,1))!= RES_OK) 624 if ((FATFS_diskRead(drv,buffer,sector,1))!= RES_OK)
618 { 625 {
619 return FR_DISK_ERR; 626 return FR_DISK_ERR;
620 } 627 }
@@ -622,29 +629,56 @@ FATFS_Error FATFS_readBootSector(BYTE index)
622 /* Parse the MBR to find the number of partition that are available 629 /* Parse the MBR to find the number of partition that are available
623 * and map each partition with its instance/index number. 630 * and map each partition with its instance/index number.
624 */ 631 */
625 for (i = 1; i < 4; i++) 632 for (i = 1; i < NUM_OF_PARTITIONS; i++)
626 { 633 {
634 /*Obtain Free index in VolToPart */
635 index = FATFS_getFreeVolIndex();
636
627 /*Read the partition entry indexed by i in MBR */ 637 /*Read the partition entry indexed by i in MBR */
628 pt = buffer + MBR_Table + i * SZ_PTE; 638 pt = buffer + MBR_Table + i * SZ_PTE;
629 if(pt[4]) 639 if(pt[MBR_PT_TYPE_OFFSET])
630 { 640 {
631 if(Fatfs_volumeIndex < 4) 641 if(index >= 0)
632 { 642 {
633 VolToPart[Fatfs_volumeIndex].pt = VolToPart[Fatfs_volumeIndex -1].pt + 1; 643 VolToPart[index].pt = i + 1;
634 VolToPart[Fatfs_volumeIndex].pd = index; 644 VolToPart[index].pd = drv;
635 }
636 645
637 /*Mount the partition */ 646 /*Mount the partition(s) */
638 snprintf(drive_path,sizeof(drive_path),"%d:",(int)Fatfs_volumeIndex); 647 snprintf(drive_path,sizeof(drive_path),"%d:",(int)index);
639 f_mount(&(object->filesystem[i]),drive_path,0); 648 f_mount(&(object->filesystem[i]),drive_path,0);
640 649 }
641 Fatfs_volumeIndex++; 650 else
651 {
652 res = FR_DISK_ERR;
653 break;
654 }
642 } 655 }
643 else 656 else
644 { 657 {
645 /*Stop looking for next partition as this will be the last partition */ 658 /*Stop looking for next partition as this will be the last partition of the disk */
646 break; 659 break;
647 } 660 }
648 } 661 }
649 return (FATFS_OK); 662
663 return res;
664}
665
666/*
667 * ======== FATFS_getFreeVolIndex ========
668 * Function is to find a free space in VolToPart structure.
669 *
670*/
671static int32_t FATFS_getFreeVolIndex()
672{
673 uint32_t index = 0;
674
675 for (index = 0; index < NUM_OF_PARTITIONS; index++)
676 {
677 if(VolToPart[index].pd == 0xf)
678 {
679 return index;
680 }
681 }
682
683 return FATFS_ERR;
650} 684}