GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_media_check_FAT_chain_check.c Lines: 29 29 100.0 %
Date: 2024-01-10 21:53:23 Branches: 16 16 100.0 %

Line Branch Exec Source
1
/**************************************************************************/
2
/*                                                                        */
3
/*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4
/*                                                                        */
5
/*       This software is licensed under the Microsoft Software License   */
6
/*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7
/*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8
/*       and in the root directory of this software.                      */
9
/*                                                                        */
10
/**************************************************************************/
11
12
13
/**************************************************************************/
14
/**************************************************************************/
15
/**                                                                       */
16
/** FileX Component                                                       */
17
/**                                                                       */
18
/**   Media                                                               */
19
/**                                                                       */
20
/**************************************************************************/
21
/**************************************************************************/
22
23
#define FX_SOURCE_CODE
24
25
26
/* Include necessary system files.  */
27
28
#include "fx_api.h"
29
#include "fx_media.h"
30
#include "fx_utility.h"
31
32
33
/**************************************************************************/
34
/*                                                                        */
35
/*  FUNCTION                                               RELEASE        */
36
/*                                                                        */
37
/*    _fx_media_check_FAT_chain_check                     PORTABLE C      */
38
/*                                                           6.1          */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    William E. Lamie, Microsoft Corporation                             */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function walks the supplied FAT chain looking for cross links  */
46
/*    (FAT entries already used in another FAT chain) and abnormal FAT    */
47
/*    entries and invalid chain termination.                              */
48
/*                                                                        */
49
/*  INPUT                                                                 */
50
/*                                                                        */
51
/*    media_ptr                             Pointer to a previously       */
52
/*                                            opened media                */
53
/*    starting_cluster                      Starting cluster of FAT chain */
54
/*    last_valid_cluster                    Last valid cluster of chain   */
55
/*    total_valid_clusters                  Total number of valid clusters*/
56
/*    logical_fat                           Pointer to the logical FAT    */
57
/*                                            bit map                     */
58
/*                                                                        */
59
/*  OUTPUT                                                                */
60
/*                                                                        */
61
/*    error                                 Error code                    */
62
/*                                                                        */
63
/*  CALLS                                                                 */
64
/*                                                                        */
65
/*    _fx_utility_FAT_entry_read            Read a FAT entry              */
66
/*                                                                        */
67
/*  CALLED BY                                                             */
68
/*                                                                        */
69
/*    _fx_check_media                       Check media function          */
70
/*                                                                        */
71
/*  RELEASE HISTORY                                                       */
72
/*                                                                        */
73
/*    DATE              NAME                      DESCRIPTION             */
74
/*                                                                        */
75
/*  05-19-2020     William E. Lamie         Initial Version 6.0           */
76
/*  09-30-2020     William E. Lamie         Modified comment(s),          */
77
/*                                            resulting in version 6.1    */
78
/*                                                                        */
79
/**************************************************************************/
80
14162
ULONG  _fx_media_check_FAT_chain_check(FX_MEDIA *media_ptr, ULONG starting_cluster, ULONG *last_valid_cluster, ULONG *total_valid_clusters, UCHAR *logical_fat)
81
{
82
83
14162
ULONG prev_cluster, cluster, next_clust = 0;
84
ULONG cluster_number;
85
ULONG count, error;
86
UINT  status;
87
88
89
    /* Initialize the error code.  */
90
14162
    error =  0;
91
92
    /* Setup at the first cluster.  */
93
14162
    cluster =  starting_cluster;
94
95
    /* Initialize the previous cluster.  */
96
14162
    prev_cluster =  0;
97
98
    /* Initialize the count to 0. */
99
14162
    count =  0;
100
101
    /* Loop to walk through the FAT chain.  */
102
23757
    while ((cluster >= (ULONG)FX_FAT_ENTRY_START) &&
103
12769
           (cluster < (FX_FAT_ENTRY_START + media_ptr -> fx_media_total_clusters)))
104
    {
105
106
11401
        cluster_number = cluster;
107
108
#ifdef FX_ENABLE_EXFAT
109
110
        /* For the index of the first cluster in exFAT is 2, adjust the number of clusters to fit Allocation Bitmap Table. */
111
        /* We will compare logical_fat with Aollcation Bitmap table later to find out lost clusters. */
112
        if (media_ptr -> fx_media_FAT_type == FX_exFAT)
113
        {
114
            cluster_number = cluster - FX_FAT_ENTRY_START;
115
        }
116
#endif /* FX_ENABLE_EXFAT */
117
118
        /* Determine if this cluster is already in the logical FAT bit map.  */
119
11401
        if (logical_fat[cluster_number >> 3] & (1 << (cluster_number & 7)))
120
        {
121
122
            /* Yes, the cluster is already being used by another file or
123
               sub-directory.  */
124
808
            error =  FX_FAT_CHAIN_ERROR;
125
808
            break;
126
        }
127
128
        /* Now read the contents of the cluster.  */
129
10593
        status =  _fx_utility_FAT_entry_read(media_ptr, cluster, &next_clust);
130
131
        /* Check the return status.  */
132
10593
        if (status != FX_SUCCESS)
133
        {
134
135
            /* Yes, the cluster is already being used by another file or
136
               sub-directory.  */
137
250
            error =  FX_IO_ERROR;
138
250
            break;
139
        }
140
141
        /* Determine if the link is circular or the count is greater than the
142
           total clusters.  */
143
10343
        if ((cluster == next_clust) ||
144
10342
            (next_clust < (ULONG)FX_FAT_ENTRY_START) ||
145
9975
            ((next_clust >= (FX_FAT_ENTRY_START + media_ptr -> fx_media_total_clusters)) &&
146
1748
             (next_clust != media_ptr -> fx_media_fat_last)))
147
        {
148
149
748
            error =  FX_FAT_CHAIN_ERROR;
150
748
            break;
151
        }
152
153
        /* Everything is good with the chain at this point.  Mark it as valid.  */
154
9595
        logical_fat[cluster_number >> 3] = (UCHAR)(logical_fat[cluster_number >> 3] | (1 << (cluster_number & 7)));
155
156
        /* Move the cluster variable forward.  */
157
9595
        prev_cluster =  cluster;
158
9595
        cluster =       next_clust;
159
160
        /* Increment the number of valid clusters.  */
161
9595
        count++;
162
    }
163
164
    /* Return the last valid cluster and the total valid cluster count.  */
165
14162
    *last_valid_cluster =   prev_cluster;
166
14162
    *total_valid_clusters = count;
167
168
    /* Return error code.  */
169
14162
    return(error);
170
}
171