GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_media_check_lost_cluster_check.c Lines: 23 23 100.0 %
Date: 2026-03-06 18:49:02 Branches: 18 18 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
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_lost_cluster_check                  PORTABLE C      */
38
/*                                                           6.1          */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    William E. Lamie, Microsoft Corporation                             */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function examines all clusters to see if there are any unused  */
46
/*    clusters that are also unavailable. If specified, this routine will */
47
/*    also mark the cluster as available.                                 */
48
/*                                                                        */
49
/*  INPUT                                                                 */
50
/*                                                                        */
51
/*    media_ptr                             Pointer to a previously       */
52
/*                                            opened media                */
53
/*    logical_fat                           Pointer to the logical FAT    */
54
/*                                            bit map                     */
55
/*    total_clusters                        Total number of clusters      */
56
/*    error_correction_option               Option for correcting lost    */
57
/*                                            cluster errors              */
58
/*                                                                        */
59
/*  OUTPUT                                                                */
60
/*                                                                        */
61
/*    error                                 Error code                    */
62
/*                                                                        */
63
/*  CALLS                                                                 */
64
/*                                                                        */
65
/*    _fx_utility_FAT_entry_read            Read a FAT entry              */
66
/*    _fx_utility_FAT_entry_write           Write a FAT entry             */
67
/*                                                                        */
68
/*  CALLED BY                                                             */
69
/*                                                                        */
70
/*    _fx_check_media                       Check media function          */
71
/*                                                                        */
72
/**************************************************************************/
73
23
ULONG  _fx_media_check_lost_cluster_check(FX_MEDIA *media_ptr, UCHAR *logical_fat, ULONG total_clusters, ULONG error_correction_option)
74
{
75
76
23
ULONG cluster, next_cluster = 0;
77
ULONG fat_last;
78
ULONG error;
79
UINT  status;
80
81
82
    /* Calculate the FAT reserved and last sector values.  */
83
23
    if (media_ptr -> fx_media_32_bit_FAT)
84
    {
85
3
        fat_last =      FX_LAST_CLUSTER_1_32;
86
    }
87
    else
88
    {
89
20
        fat_last =      FX_LAST_CLUSTER_1;
90
    }
91
92
    /* Initialize the error.  */
93
23
    error =  0;
94
95
    /* Loop through all the clusters to see if any clusters NOT in the logical sector FAT have
96
       a non zero value.  */
97
248207
    for (cluster = FX_FAT_ENTRY_START; cluster < total_clusters; cluster++)
98
    {
99
100
        /* Determine if this cluster is in the logical FAT.  */
101
248187
        if (logical_fat[cluster >> 3] & (1 << (cluster & 7)))
102
        {
103
104
            /* Yes, the cluster is in use by a file or sub-directory.  Just continue the loop.  */
105
5925
            continue;
106
        }
107
108
        /* Otherwise, the cluster is not in use.  */
109
110
        /* Read the contents of what should be a free cluster.  */
111
242262
        status = _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster);
112
113
        /* Check for a good status.  */
114
242262
        if (status)
115
        {
116
117
            /* Set the error code.  */
118
2
            error = error | FX_IO_ERROR;
119
120
            /* Return error code.  */
121
2
            return(error);
122
        }
123
124
        /* Determine if the contents of the cluster is valid.  */
125

242260
        if (((next_cluster > (ULONG)FX_FREE_CLUSTER) && (next_cluster < media_ptr -> fx_media_fat_reserved)) ||
126
236299
            (next_cluster >= fat_last))
127
        {
128
129
            /* Lost cluster is present.  */
130
131
            /* Set the error code status.  */
132
5967
            error =  FX_LOST_CLUSTER_ERROR;
133
134
            /* Determine if the lost cluster should be recovered.  */
135
5967
            if (error_correction_option & FX_LOST_CLUSTER_ERROR)
136
            {
137
138
                /* Make the cluster available again.  */
139
5942
                status =  _fx_utility_FAT_entry_write(media_ptr, cluster, FX_FREE_CLUSTER);
140
141
                /* Check for a good status.  */
142
5942
                if (status)
143
                {
144
145
                    /* Increment the available clusters.  */
146
1
                    media_ptr -> fx_media_available_clusters++;
147
148
                    /* Set the error code.  */
149
1
                    error =  error | FX_IO_ERROR;
150
151
                    /* Return error code.  */
152
1
                    return(error);
153
                }
154
            }
155
        }
156
    }
157
158
    /* Return error code.  */
159
20
    return(error);
160
}
161