GCC Code Coverage Report


Directory: common/src/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 30 / 0 / 30
Functions: 100.0% 1 / 0 / 1
Branches: 100.0% 16 / 0 / 16

fx_media_boot_info_extract.c
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 56784 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 56784 boot_sector = media_ptr -> fx_media_driver_buffer;
115
116 /* Extract the number of bytes per sector. */
117 56784 media_ptr -> fx_media_bytes_per_sector = _fx_utility_16_unsigned_read(&boot_sector[FX_BYTES_SECTOR]);
118
2/2
✓ Branch 3 → 4 taken 9 times.
✓ Branch 3 → 5 taken 56775 times.
56784 if (media_ptr -> fx_media_bytes_per_sector == 0)
119 9 return(FX_MEDIA_INVALID);
120
121
122 /* FAT12/16/32 volume. */
123 /* Extract the number of sectors per track. */
124 56775 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 56775 media_ptr -> fx_media_heads = _fx_utility_16_unsigned_read(&boot_sector[FX_HEADS]);
128
129 /* Extract the total number of sectors. */
130 56775 media_ptr -> fx_media_total_sectors = _fx_utility_16_unsigned_read(&boot_sector[FX_SECTORS]);
131
2/2
✓ Branch 8 → 9 taken 35456 times.
✓ Branch 8 → 11 taken 21319 times.
56775 if (media_ptr -> fx_media_total_sectors == 0)
132 {
133 35456 media_ptr -> fx_media_total_sectors = _fx_utility_32_unsigned_read(&boot_sector[FX_HUGE_SECTORS]);
134 }
135
136
2/2
✓ Branch 11 → 12 taken 9 times.
✓ Branch 11 → 13 taken 56766 times.
56775 if (media_ptr -> fx_media_total_sectors == 0)
137 {
138 9 return(FX_MEDIA_INVALID);
139 }
140
141 /* Extract the number of reserved sectors before the first FAT. */
142 56766 media_ptr -> fx_media_reserved_sectors = _fx_utility_16_unsigned_read(&boot_sector[FX_RESERVED_SECTORS]);
143
2/2
✓ Branch 14 → 15 taken 9 times.
✓ Branch 14 → 16 taken 56757 times.
56766 if (media_ptr -> fx_media_reserved_sectors == 0)
144 {
145 9 return(FX_MEDIA_INVALID);
146 }
147
148 /* Extract the number of sectors per cluster. */
149 56757 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
2/2
✓ Branch 16 → 17 taken 9 times.
✓ Branch 16 → 18 taken 56748 times.
56757 if (media_ptr -> fx_media_sectors_per_cluster == 0)
153 {
154 9 return(FX_MEDIA_INVALID);
155 }
156
157 /* Extract the number of sectors per FAT. */
158 56748 media_ptr -> fx_media_sectors_per_FAT = _fx_utility_16_unsigned_read(&boot_sector[FX_SECTORS_PER_FAT]);
159
2/2
✓ Branch 19 → 20 taken 35379 times.
✓ Branch 19 → 22 taken 21369 times.
56748 if (media_ptr -> fx_media_sectors_per_FAT == 0)
160 {
161 35379 media_ptr -> fx_media_sectors_per_FAT = _fx_utility_32_unsigned_read(&boot_sector[FX_SECTORS_PER_FAT_32]);
162 }
163
164
2/2
✓ Branch 22 → 23 taken 9 times.
✓ Branch 22 → 24 taken 56739 times.
56748 if (media_ptr -> fx_media_sectors_per_FAT == 0)
165 {
166 9 return(FX_MEDIA_INVALID);
167 }
168
169 /* Extract the number of FATs. */
170 56739 media_ptr -> fx_media_number_of_FATs = ((UINT)boot_sector[FX_NUMBER_OF_FATS] & 0xFF);
171
2/2
✓ Branch 24 → 25 taken 9 times.
✓ Branch 24 → 26 taken 56730 times.
56739 if (media_ptr -> fx_media_number_of_FATs == 0)
172 {
173 9 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 56730 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 56730 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 56730 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 56730 return(FX_SUCCESS);
191 }
192
193