GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_media_boot_info_extract.c Lines: 30 30 100.0 %
Date: 2026-03-06 18:49:02 Branches: 16 16 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_system.h"
30
#include "fx_media.h"
31
#include "fx_utility.h"
32
33
34
/**************************************************************************/
35
/*                                                                        */
36
/*  FUNCTION                                               RELEASE        */
37
/*                                                                        */
38
/*    _fx_media_boot_info_extract                         PORTABLE C      */
39
/*                                                           6.1.10       */
40
/*  AUTHOR                                                                */
41
/*                                                                        */
42
/*    William E. Lamie, Microsoft Corporation                             */
43
/*                                                                        */
44
/*  DESCRIPTION                                                           */
45
/*                                                                        */
46
/*    This function extracts and validates the information from the boot  */
47
/*    record found in the memory buffer.  If the boot record is invalid,  */
48
/*    an FX_MEDIA_INVALID status is returned to the caller.               */
49
/*                                                                        */
50
/*    The FAT boot sector (512 bytes) that is operated on by this         */
51
/*    function must look like the following:                              */
52
/*                                                                        */
53
/*          Byte Offset         Meaning             Size                  */
54
/*                                                                        */
55
/*            0x000         Jump Instructions        3                    */
56
/*            0x003         OEM Name                 8                    */
57
/*            0x00B        *Bytes per Sector         2                    */
58
/*            0x00D        *Sectors per Cluster      1                    */
59
/*            0x00E        *Reserved Sectors         2                    */
60
/*            0x010        *Number of FATs           1                    */
61
/*            0x011        *Max Root Dir Entries     2                    */
62
/*            0x013        *Number of Sectors        2                    */
63
/*            0x015         Media Type               1                    */
64
/*            0x016        *Sectors per FAT          2                    */
65
/*            0x018        *Sectors per Track        2                    */
66
/*            0x01A        *Number of Heads          2                    */
67
/*            0x01C        *Hidden Sectors           4                    */
68
/*            0x020        *Huge Sectors             4                    */
69
/*            0x024         Drive Number             1                    */
70
/*            0x025         Reserved                 1                    */
71
/*            0x026         Boot Signature           1                    */
72
/*            0x027         Volume ID                4                    */
73
/*            0x02B         Volume Label             11                   */
74
/*            0x036         File System Type         8                    */
75
/*             ...              ...                 ...                   */
76
/*            0x1FE       **Signature (0x55aa)       2                    */
77
/*                                                                        */
78
/*            * Denotes which elements of the boot record                 */
79
/*              FileX uses.                                               */
80
/*                                                                        */
81
/*            **Denotes the element is checked by the I/O                 */
82
/*              driver.  This eliminates the need for a minimum           */
83
/*              512-byte buffer for FileX.                                */
84
/*                                                                        */
85
/*  Note: All values above are in little endian format, i.e. the LSB is   */
86
/*        in the lowest address.                                          */
87
/*                                                                        */
88
/*  INPUT                                                                 */
89
/*                                                                        */
90
/*    media_ptr                             Media control block pointer   */
91
/*                                                                        */
92
/*  OUTPUT                                                                */
93
/*                                                                        */
94
/*    return status                                                       */
95
/*                                                                        */
96
/*  CALLS                                                                 */
97
/*                                                                        */
98
/*    _fx_utility_16_unsigned_read          Read a UINT from buffer       */
99
/*    _fx_utility_32_unsigned_read          Read a ULONG from buffer      */
100
/*    _fx_utility_64_unsigned_read          Read a ULONG64 from memory    */
101
/*                                                                        */
102
/*  CALLED BY                                                             */
103
/*                                                                        */
104
/*    _fx_media_open                        Media open function           */
105
/*                                                                        */
106
/**************************************************************************/
107
6028
UINT  _fx_media_boot_info_extract(FX_MEDIA *media_ptr)
108
{
109
110
UCHAR *boot_sector;
111
112
113
    /* Move the buffer pointer into a local copy.  */
114
6028
    boot_sector =  media_ptr -> fx_media_driver_buffer;
115
116
    /* Extract the number of bytes per sector.  */
117
6028
    media_ptr -> fx_media_bytes_per_sector =    _fx_utility_16_unsigned_read(&boot_sector[FX_BYTES_SECTOR]);
118
6028
    if (media_ptr -> fx_media_bytes_per_sector == 0)
119
1
        return(FX_MEDIA_INVALID);
120
121
122
    /* FAT12/16/32 volume.  */
123
    /* Extract the number of sectors per track.  */
124
6027
    media_ptr -> fx_media_sectors_per_track =   _fx_utility_16_unsigned_read(&boot_sector[FX_SECTORS_PER_TRK]);
125
126
    /* Extract the number of heads.  */
127
6027
    media_ptr -> fx_media_heads =               _fx_utility_16_unsigned_read(&boot_sector[FX_HEADS]);
128
129
    /* Extract the total number of sectors.  */
130
6027
    media_ptr -> fx_media_total_sectors =       _fx_utility_16_unsigned_read(&boot_sector[FX_SECTORS]);
131
6027
    if (media_ptr -> fx_media_total_sectors == 0)
132
    {
133
3834
        media_ptr -> fx_media_total_sectors = _fx_utility_32_unsigned_read(&boot_sector[FX_HUGE_SECTORS]);
134
    }
135
136
6027
    if (media_ptr -> fx_media_total_sectors == 0)
137
    {
138
1
        return(FX_MEDIA_INVALID);
139
    }
140
141
    /* Extract the number of reserved sectors before the first FAT.  */
142
6026
    media_ptr -> fx_media_reserved_sectors =    _fx_utility_16_unsigned_read(&boot_sector[FX_RESERVED_SECTORS]);
143
6026
    if (media_ptr -> fx_media_reserved_sectors == 0)
144
    {
145
1
        return(FX_MEDIA_INVALID);
146
    }
147
148
    /* Extract the number of sectors per cluster.  */
149
6025
    media_ptr -> fx_media_sectors_per_cluster = ((UINT)boot_sector[FX_SECTORS_CLUSTER] & 0xFF);
150
151
    /* There should always be at least one reserved sector, representing the boot record itself.  */
152
6025
    if (media_ptr -> fx_media_sectors_per_cluster == 0)
153
    {
154
1
        return(FX_MEDIA_INVALID);
155
    }
156
157
    /* Extract the number of sectors per FAT.  */
158
6024
    media_ptr -> fx_media_sectors_per_FAT =     _fx_utility_16_unsigned_read(&boot_sector[FX_SECTORS_PER_FAT]);
159
6024
    if (media_ptr -> fx_media_sectors_per_FAT == 0)
160
    {
161
3833
        media_ptr -> fx_media_sectors_per_FAT = _fx_utility_32_unsigned_read(&boot_sector[FX_SECTORS_PER_FAT_32]);
162
    }
163
164
6024
    if (media_ptr -> fx_media_sectors_per_FAT == 0)
165
    {
166
1
        return(FX_MEDIA_INVALID);
167
    }
168
169
    /* Extract the number of FATs.  */
170
6023
    media_ptr -> fx_media_number_of_FATs =      ((UINT)boot_sector[FX_NUMBER_OF_FATS] & 0xFF);
171
6023
    if (media_ptr -> fx_media_number_of_FATs == 0)
172
    {
173
1
        return(FX_BOOT_ERROR);
174
    }
175
176
    /* Extract the number of hidden sectors.  */
177
#ifdef FX_DRIVER_USE_64BIT_LBA
178
    media_ptr -> fx_media_hidden_sectors =      _fx_utility_64_unsigned_read(&boot_sector[FX_HIDDEN_SECTORS]);
179
#else
180
6022
    media_ptr -> fx_media_hidden_sectors =      _fx_utility_32_unsigned_read(&boot_sector[FX_HIDDEN_SECTORS]);
181
#endif
182
    /* Extract the number of root directory entries.  */
183
6022
    media_ptr -> fx_media_root_directory_entries =  _fx_utility_16_unsigned_read(&boot_sector[FX_ROOT_DIR_ENTRIES]);
184
185
    /* Extract root directory starting cluster (32 bit only) and compute start sector */
186
6022
    media_ptr -> fx_media_root_cluster_32 = _fx_utility_32_unsigned_read(&boot_sector[FX_ROOT_CLUSTER_32]);
187
188
189
    /* Return a successful status.  */
190
6022
    return(FX_SUCCESS);
191
}
192