GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_utility_FAT_map_flush.c Lines: 27 27 100.0 %
Date: 2024-01-10 21:53:23 Branches: 18 18 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
/**   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
/*  RELEASE HISTORY                                                       */
66
/*                                                                        */
67
/*    DATE              NAME                      DESCRIPTION             */
68
/*                                                                        */
69
/*  05-19-2020     William E. Lamie         Initial Version 6.0           */
70
/*  09-30-2020     William E. Lamie         Modified comment(s),          */
71
/*                                            resulting in version 6.1    */
72
/*                                                                        */
73
/**************************************************************************/
74
340691
UINT  _fx_utility_FAT_map_flush(FX_MEDIA *media_ptr)
75
{
76
77
ULONG FAT_sector, last_sector;
78
UINT  i, status, FATs;
79
UCHAR sectors_per_bit;
80
81
82
    /* Determine how many FAT sectors each bit in the bit map represents.  Depending on
83
       the number of sectors in the primary FAT, each bit in this map may represent one
84
       or more primary FAT sectors. Because of this, it is possible some FAT sectors that
85
       were not changed may get flushed out to the secondary FAT.  However, this method
86
       provides very nice performance benefits during normal operation and is much more
87
       reasonable than performing a total copy of the primary FAT to each secondary FAT
88
       on media flush and media close.  */
89
340691
    if (media_ptr -> fx_media_sectors_per_FAT % (FX_FAT_MAP_SIZE << 3) == 0)
90
    {
91
4034
        sectors_per_bit =  (UCHAR)(media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3));
92
    }
93
    else
94
    {
95
336657
        sectors_per_bit =  (UCHAR)(media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3) + 1);
96
    }
97
98
    /* Loop through the FAT update map to mirror primary FAT sectors to secondary FAT(s).  */
99
3066195
    for (i = 0; i < FX_FAT_MAP_SIZE << 3; i++)
100
    {
101
102
        /* Determine if there are FAT changes specified by this entry.  */
103
2725507
        if ((media_ptr -> fx_media_fat_secondary_update_map[i >> 3] & (1 << (i & 7))) == 0)
104
        {
105
106
            /* No, look at the next bit map entry.  */
107
2490208
            continue;
108
        }
109
110
        /* Setup the parameters for performing the update.  */
111
235299
        FAT_sector =    i * sectors_per_bit + media_ptr -> fx_media_reserved_sectors;
112
235299
        last_sector =   FAT_sector + sectors_per_bit;
113
114
        /* Make sure the last update sector is within range.  */
115
235299
        if (last_sector > (media_ptr -> fx_media_sectors_per_FAT + media_ptr -> fx_media_reserved_sectors))
116
        {
117
15
            last_sector =  media_ptr -> fx_media_sectors_per_FAT + media_ptr -> fx_media_reserved_sectors;
118
        }
119
120
        /* Loop to mirror primary FAT sectors to secondary FAT(s).  */
121
3529382
        for (; FAT_sector < last_sector; FAT_sector++)
122
        {
123
124
            /* Read the FAT sector.  */
125
3294086
            status =  _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector,
126
3294086
                                                      media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR);
127
128
            /* Determine if an error occurred.  */
129
3294086
            if (status != FX_SUCCESS)
130
            {
131
                /* Return the error status.  */
132
2
                return(status);
133
            }
134
135
            /* Pickup how many secondary FATs there are.  */
136
3294084
            FATs =  media_ptr -> fx_media_number_of_FATs - 1;
137
138
            /* Loop to update additional FAT entries.  */
139
6456936
            while (FATs)
140
            {
141
142
                /* Mirror main FAT sector write into the additional FATs.  */
143
3162853
                status =  _fx_utility_logical_sector_write(media_ptr,
144
3162853
                                                           ((ULONG64) FAT_sector) + ((ULONG64)FATs * (ULONG64)(media_ptr -> fx_media_sectors_per_FAT)),
145
3162853
                                                           media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR);
146
147
                /* Determine if an error occurred.  */
148
3162853
                if (status != FX_SUCCESS)
149
                {
150
151
                    /* Return the error status.  */
152
1
                    return(status);
153
                }
154
155
                /* Decrement the number of FATs.  */
156
3162852
                FATs--;
157
            }
158
        }
159
    }
160
161
    /* Clear the bit map that indicates primary FAT updates.  */
162
681376
    for (i = 0; i < FX_FAT_MAP_SIZE; i++)
163
    {
164
165
        /* Clear each entry in the bit map.  */
166
340688
        media_ptr -> fx_media_fat_secondary_update_map[i] =  0;
167
    }
168
169
    /* Return a successful completion.  */
170
340688
    return(FX_SUCCESS);
171
}
172