GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_utility_FAT_map_flush.c Lines: 27 27 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
/**   Utility                                                             */
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_system.h"
30
#include "fx_utility.h"
31
32
33
/**************************************************************************/
34
/*                                                                        */
35
/*  FUNCTION                                               RELEASE        */
36
/*                                                                        */
37
/*    _fx_utility_FAT_map_flush                           PORTABLE C      */
38
/*                                                           6.1          */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    William E. Lamie, Microsoft Corporation                             */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function updates mirrors changes in the primary FAT to each of */
46
/*    secondary FATs in the media.                                        */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    media_ptr                             Media control block pointer   */
51
/*                                                                        */
52
/*  OUTPUT                                                                */
53
/*                                                                        */
54
/*    return status                                                       */
55
/*                                                                        */
56
/*  CALLS                                                                 */
57
/*                                                                        */
58
/*    _fx_utility_logical_sector_read       Read FAT sector into memory   */
59
/*    _fx_utility_logical_sector_write      Write FAT sector back to disk */
60
/*                                                                        */
61
/*  CALLED BY                                                             */
62
/*                                                                        */
63
/*    FileX System Functions                                              */
64
/*                                                                        */
65
/**************************************************************************/
66
340691
UINT  _fx_utility_FAT_map_flush(FX_MEDIA *media_ptr)
67
{
68
69
ULONG FAT_sector, last_sector;
70
UINT  i, status, FATs;
71
UCHAR sectors_per_bit;
72
73
74
    /* Determine how many FAT sectors each bit in the bit map represents.  Depending on
75
       the number of sectors in the primary FAT, each bit in this map may represent one
76
       or more primary FAT sectors. Because of this, it is possible some FAT sectors that
77
       were not changed may get flushed out to the secondary FAT.  However, this method
78
       provides very nice performance benefits during normal operation and is much more
79
       reasonable than performing a total copy of the primary FAT to each secondary FAT
80
       on media flush and media close.  */
81
340691
    if (media_ptr -> fx_media_sectors_per_FAT % (FX_FAT_MAP_SIZE << 3) == 0)
82
    {
83
4034
        sectors_per_bit =  (UCHAR)(media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3));
84
    }
85
    else
86
    {
87
336657
        sectors_per_bit =  (UCHAR)(media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3) + 1);
88
    }
89
90
    /* Loop through the FAT update map to mirror primary FAT sectors to secondary FAT(s).  */
91
3066195
    for (i = 0; i < FX_FAT_MAP_SIZE << 3; i++)
92
    {
93
94
        /* Determine if there are FAT changes specified by this entry.  */
95
2725507
        if ((media_ptr -> fx_media_fat_secondary_update_map[i >> 3] & (1 << (i & 7))) == 0)
96
        {
97
98
            /* No, look at the next bit map entry.  */
99
2490208
            continue;
100
        }
101
102
        /* Setup the parameters for performing the update.  */
103
235299
        FAT_sector =    i * sectors_per_bit + media_ptr -> fx_media_reserved_sectors;
104
235299
        last_sector =   FAT_sector + sectors_per_bit;
105
106
        /* Make sure the last update sector is within range.  */
107
235299
        if (last_sector > (media_ptr -> fx_media_sectors_per_FAT + media_ptr -> fx_media_reserved_sectors))
108
        {
109
15
            last_sector =  media_ptr -> fx_media_sectors_per_FAT + media_ptr -> fx_media_reserved_sectors;
110
        }
111
112
        /* Loop to mirror primary FAT sectors to secondary FAT(s).  */
113
3529382
        for (; FAT_sector < last_sector; FAT_sector++)
114
        {
115
116
            /* Read the FAT sector.  */
117
3294086
            status =  _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector,
118
3294086
                                                      media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR);
119
120
            /* Determine if an error occurred.  */
121
3294086
            if (status != FX_SUCCESS)
122
            {
123
                /* Return the error status.  */
124
2
                return(status);
125
            }
126
127
            /* Pickup how many secondary FATs there are.  */
128
3294084
            FATs =  media_ptr -> fx_media_number_of_FATs - 1;
129
130
            /* Loop to update additional FAT entries.  */
131
6456936
            while (FATs)
132
            {
133
134
                /* Mirror main FAT sector write into the additional FATs.  */
135
3162853
                status =  _fx_utility_logical_sector_write(media_ptr,
136
3162853
                                                           ((ULONG64) FAT_sector) + ((ULONG64)FATs * (ULONG64)(media_ptr -> fx_media_sectors_per_FAT)),
137
3162853
                                                           media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR);
138
139
                /* Determine if an error occurred.  */
140
3162853
                if (status != FX_SUCCESS)
141
                {
142
143
                    /* Return the error status.  */
144
1
                    return(status);
145
                }
146
147
                /* Decrement the number of FATs.  */
148
3162852
                FATs--;
149
            }
150
        }
151
    }
152
153
    /* Clear the bit map that indicates primary FAT updates.  */
154
681376
    for (i = 0; i < FX_FAT_MAP_SIZE; i++)
155
    {
156
157
        /* Clear each entry in the bit map.  */
158
340688
        media_ptr -> fx_media_fat_secondary_update_map[i] =  0;
159
    }
160
161
    /* Return a successful completion.  */
162
340688
    return(FX_SUCCESS);
163
}
164