Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_system.h"
#include "fx_media.h"
#include "fx_utility.h"
...
...
_fx_media_boot_info_extract(FX_MEDIA *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesfilexcommon/src/fx_media_boot_info_extract.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** FileX Component */ /** */ /** Media */ /** */... /**************************************************************************/ /**************************************************************************/ #define FX_SOURCE_CODE /* Include necessary system files. */ #include "fx_api.h" #include "fx_system.h" #include "fx_media.h" #include "fx_utility.h" ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _fx_media_boot_info_extract PORTABLE C */ /* 6.1.10 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function extracts and validates the information from the boot */ /* record found in the memory buffer. If the boot record is invalid, */ /* an FX_MEDIA_INVALID status is returned to the caller. */ /* */ /* The FAT boot sector (512 bytes) that is operated on by this */ /* function must look like the following: */ /* */ /* Byte Offset Meaning Size */ /* */ /* 0x000 Jump Instructions 3 */ /* 0x003 OEM Name 8 */ /* 0x00B *Bytes per Sector 2 */ /* 0x00D *Sectors per Cluster 1 */ /* 0x00E *Reserved Sectors 2 */ /* 0x010 *Number of FATs 1 */ /* 0x011 *Max Root Dir Entries 2 */ /* 0x013 *Number of Sectors 2 */ /* 0x015 Media Type 1 */ /* 0x016 *Sectors per FAT 2 */ /* 0x018 *Sectors per Track 2 */ /* 0x01A *Number of Heads 2 */ /* 0x01C *Hidden Sectors 4 */ /* 0x020 *Huge Sectors 4 */ /* 0x024 Drive Number 1 */ /* 0x025 Reserved 1 */ /* 0x026 Boot Signature 1 */ /* 0x027 Volume ID 4 */ /* 0x02B Volume Label 11 */ /* 0x036 File System Type 8 */ /* ... ... ... */ /* 0x1FE **Signature (0x55aa) 2 */ /* */ /* * Denotes which elements of the boot record */ /* FileX uses. */ /* */ /* **Denotes the element is checked by the I/O */ /* driver. This eliminates the need for a minimum */ /* 512-byte buffer for FileX. */ /* */ /* Note: All values above are in little endian format, i.e. the LSB is */ /* in the lowest address. */ /* */ /* INPUT */ /* */ /* media_ptr Media control block pointer */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _fx_utility_16_unsigned_read Read a UINT from buffer */ /* _fx_utility_32_unsigned_read Read a ULONG from buffer */ /* _fx_utility_64_unsigned_read Read a ULONG64 from memory */ /* */ /* CALLED BY */ /* */ /* _fx_media_open Media open function */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 William E. Lamie Initial Version 6.0 */ /* 09-30-2020 William E. Lamie Modified comment(s), */ /* resulting in version 6.1 */ /* 01-31-2022 Bhupendra Naphade Modified comment(s), added */ /* check for bimap cache size, */ /* resulting in version 6.1.10 */ /* */... /**************************************************************************/ UINT _fx_media_boot_info_extract(FX_MEDIA *media_ptr) { UCHAR *boot_sector; /* Move the buffer pointer into a local copy. */ boot_sector = media_ptr -> fx_media_driver_buffer; /* Extract the number of bytes per sector. */ media_ptr -> fx_media_bytes_per_sector = _fx_utility_16_unsigned_read(&boot_sector[FX_BYTES_SECTOR]); if (media_ptr -> fx_media_bytes_per_sector == 0) #ifdef FX_ENABLE_EXFAT { /* Treat as exFAT volume. */ /* Extract the number of bytes per sector. */ media_ptr -> fx_media_exfat_bytes_per_sector_shift = boot_sector[FX_EF_BYTE_PER_SECTOR_SHIFT]; /* exFAT requires minimal value 9 (512 bytes) and maximum value 12(4096 bytes) for bytes_per_sector_shift */ if((media_ptr -> fx_media_exfat_bytes_per_sector_shift < 9) || (media_ptr -> fx_media_exfat_bytes_per_sector_shift > 12)) { return(FX_MEDIA_INVALID); }if ((media_ptr -> fx_media_exfat_bytes_per_sector_shift < 9) || (media_ptr -> fx_media_exfat_bytes_per_sector_shift > 12)) { ... } media_ptr -> fx_media_bytes_per_sector = (UINT)(1 << media_ptr -> fx_media_exfat_bytes_per_sector_shift); /* Validate bytes per sector value: no more than bitmap cache size */ if (media_ptr -> fx_media_bytes_per_sector > sizeof(media_ptr -> fx_media_exfat_bitmap_cache)) { return(FX_NOT_ENOUGH_MEMORY); }if (media_ptr -> fx_media_bytes_per_sector > sizeof(media_ptr -> fx_media_exfat_bitmap_cache)) { ... } media_ptr -> fx_media_total_sectors = _fx_utility_64_unsigned_read(&boot_sector[FX_EF_VOLUME_LENGTH]); if (media_ptr -> fx_media_total_sectors == 0) { return(FX_MEDIA_INVALID); }if (media_ptr -> fx_media_total_sectors == 0) { ... } media_ptr -> fx_media_reserved_sectors = _fx_utility_32_unsigned_read(&boot_sector[FX_EF_FAT_OFFSET]); if (media_ptr -> fx_media_reserved_sectors == 0) { return(FX_MEDIA_INVALID); }if (media_ptr -> fx_media_reserved_sectors == 0) { ... } media_ptr -> fx_media_sectors_per_FAT = _fx_utility_32_unsigned_read(&boot_sector[FX_EF_FAT_LENGTH]); if (media_ptr -> fx_media_sectors_per_FAT == 0) { return(FX_MEDIA_INVALID); }if (media_ptr -> fx_media_sectors_per_FAT == 0) { ... } media_ptr -> fx_media_data_sector_start = _fx_utility_32_unsigned_read(&boot_sector[FX_EF_CLUSTER_HEAP_OFFSET]); if (media_ptr -> fx_media_data_sector_start == 0) { return(FX_MEDIA_INVALID); }if (media_ptr -> fx_media_data_sector_start == 0) { ... } media_ptr -> fx_media_total_clusters = _fx_utility_32_unsigned_read(&boot_sector[FX_EF_CLUSTER_COUNT]); if (media_ptr -> fx_media_total_clusters == 0) { return(FX_MEDIA_INVALID); }if (media_ptr -> fx_media_total_clusters == 0) { ... } media_ptr -> fx_media_exfat_sector_per_clusters_shift = boot_sector[FX_EF_SECTOR_PER_CLUSTER_SHIFT]; if (media_ptr -> fx_media_exfat_sector_per_clusters_shift > 25 - media_ptr -> fx_media_exfat_bytes_per_sector_shift) { return(FX_MEDIA_INVALID); }if (media_ptr -> fx_media_exfat_sector_per_clusters_shift > 25 - media_ptr -> fx_media_exfat_bytes_per_sector_shift) { ... } media_ptr -> fx_media_sectors_per_cluster = (UINT)(1 << media_ptr -> fx_media_exfat_sector_per_clusters_shift); media_ptr -> fx_media_number_of_FATs = boot_sector[FX_EF_NUMBER_OF_FATS]; if (media_ptr -> fx_media_number_of_FATs == 0) { return(FX_MEDIA_INVALID); }if (media_ptr -> fx_media_number_of_FATs == 0) { ... } media_ptr -> fx_media_root_cluster_32 = _fx_utility_32_unsigned_read(&boot_sector[FX_EF_FIRST_CLUSTER_OF_ROOT_DIR]); /* Root cluster starts from at least FX_FAT_ENTRY_START (2), or higher. */ if (media_ptr -> fx_media_root_cluster_32 < FX_FAT_ENTRY_START) { return(FX_MEDIA_INVALID); }if (media_ptr -> fx_media_root_cluster_32 < FX_FAT_ENTRY_START) { ... } /* Overflow check. */ if (((ULONG64)media_ptr -> fx_media_data_sector_start + (ULONG64)(media_ptr -> fx_media_root_cluster_32 - FX_FAT_ENTRY_START) * media_ptr -> fx_media_sectors_per_cluster) > 0xFFFFFFFF) { /* Return the invalid media error status. */ return(FX_MEDIA_INVALID); }if (((ULONG64)media_ptr -> fx_media_data_sector_start + (ULONG64)(media_ptr -> fx_media_root_cluster_32 - FX_FAT_ENTRY_START) * media_ptr -> fx_media_sectors_per_cluster) > 0xFFFFFFFF) { ... } /* Calculate logical number of root dir sector. */ media_ptr -> fx_media_root_sector_start = media_ptr -> fx_media_data_sector_start + (media_ptr -> fx_media_root_cluster_32 - FX_FAT_ENTRY_START) * media_ptr -> fx_media_sectors_per_cluster; media_ptr -> fx_media_exfat_volume_serial_number = _fx_utility_32_unsigned_read(&boot_sector[FX_EF_VOLUME_SERIAL_NUMBER]); media_ptr -> fx_media_exfat_file_system_revision = _fx_utility_16_unsigned_read(&boot_sector[FX_EF_FILE_SYSTEM_REVISION]); media_ptr -> fx_media_exfat_volume_flag = _fx_utility_16_unsigned_read(&boot_sector[FX_EF_VOLUME_FLAGS]); media_ptr -> fx_media_number_of_FATs = boot_sector[FX_EF_NUMBER_OF_FATS]; if (0 == media_ptr -> fx_media_number_of_FATs) { return(FX_MEDIA_INVALID); }if (0 == media_ptr -> fx_media_number_of_FATs) { ... } /* Extract the number of hidden sectors. */ #ifdef FX_DRIVER_USE_64BIT_LBA media_ptr -> fx_media_hidden_sectors = _fx_utility_64_unsigned_read(&boot_sector[FX_EF_PARTITION_OFFSET]); #else media_ptr -> fx_media_hidden_sectors = _fx_utility_32_unsigned_read(&boot_sector[FX_EF_PARTITION_OFFSET]); #endif media_ptr -> fx_media_exfat_drive_select = boot_sector[FX_EF_DRIVE_SELECT]; media_ptr -> fx_media_exfat_percent_in_use = boot_sector[FX_EF_PERCENT_IN_USE]; media_ptr -> fx_media_12_bit_FAT = FX_FALSE; media_ptr -> fx_media_32_bit_FAT = FX_FALSE; /* Legacy code support: We will use fx_media_FAT_type for determine FAT type instead of fx_media_12_bit_FAT and fx_media_32_bit_FAT. *//* ... */ media_ptr -> fx_media_12_bit_FAT = FX_FALSE; media_ptr -> fx_media_32_bit_FAT = FX_FALSE; media_ptr -> fx_media_FAT_type = FX_exFAT; return(_fx_utility_exFAT_geometry_check(media_ptr, boot_sector)); ...} else { #else return(FX_MEDIA_INVALID); #endif /* FX_ENABLE_EXFAT */ /* FAT12/16/32 volume. */ /* Extract the number of sectors per track. */ media_ptr -> fx_media_sectors_per_track = _fx_utility_16_unsigned_read(&boot_sector[FX_SECTORS_PER_TRK]); /* Extract the number of heads. */ media_ptr -> fx_media_heads = _fx_utility_16_unsigned_read(&boot_sector[FX_HEADS]); /* Extract the total number of sectors. */ media_ptr -> fx_media_total_sectors = _fx_utility_16_unsigned_read(&boot_sector[FX_SECTORS]); if (media_ptr -> fx_media_total_sectors == 0) { media_ptr -> fx_media_total_sectors = _fx_utility_32_unsigned_read(&boot_sector[FX_HUGE_SECTORS]); }if (media_ptr -> fx_media_total_sectors == 0) { ... } if (media_ptr -> fx_media_total_sectors == 0) { return(FX_MEDIA_INVALID); }if (media_ptr -> fx_media_total_sectors == 0) { ... } /* Extract the number of reserved sectors before the first FAT. */ media_ptr -> fx_media_reserved_sectors = _fx_utility_16_unsigned_read(&boot_sector[FX_RESERVED_SECTORS]); if (media_ptr -> fx_media_reserved_sectors == 0) { return(FX_MEDIA_INVALID); }if (media_ptr -> fx_media_reserved_sectors == 0) { ... } /* Extract the number of sectors per cluster. */ media_ptr -> fx_media_sectors_per_cluster = ((UINT)boot_sector[FX_SECTORS_CLUSTER] & 0xFF); /* There should always be at least one reserved sector, representing the boot record itself. */ if (media_ptr -> fx_media_sectors_per_cluster == 0) { return(FX_MEDIA_INVALID); }if (media_ptr -> fx_media_sectors_per_cluster == 0) { ... } /* Extract the number of sectors per FAT. */ media_ptr -> fx_media_sectors_per_FAT = _fx_utility_16_unsigned_read(&boot_sector[FX_SECTORS_PER_FAT]); if (media_ptr -> fx_media_sectors_per_FAT == 0) { media_ptr -> fx_media_sectors_per_FAT = _fx_utility_32_unsigned_read(&boot_sector[FX_SECTORS_PER_FAT_32]); }if (media_ptr -> fx_media_sectors_per_FAT == 0) { ... } if (media_ptr -> fx_media_sectors_per_FAT == 0) { return(FX_MEDIA_INVALID); }if (media_ptr -> fx_media_sectors_per_FAT == 0) { ... } /* Extract the number of FATs. */ media_ptr -> fx_media_number_of_FATs = ((UINT)boot_sector[FX_NUMBER_OF_FATS] & 0xFF); if (media_ptr -> fx_media_number_of_FATs == 0) { return(FX_BOOT_ERROR); }if (media_ptr -> fx_media_number_of_FATs == 0) { ... } /* Extract the number of hidden sectors. */ #ifdef FX_DRIVER_USE_64BIT_LBA media_ptr -> fx_media_hidden_sectors = _fx_utility_64_unsigned_read(&boot_sector[FX_HIDDEN_SECTORS]); #else media_ptr -> fx_media_hidden_sectors = _fx_utility_32_unsigned_read(&boot_sector[FX_HIDDEN_SECTORS]); #endif /* Extract the number of root directory entries. */ media_ptr -> fx_media_root_directory_entries = _fx_utility_16_unsigned_read(&boot_sector[FX_ROOT_DIR_ENTRIES]); /* Extract root directory starting cluster (32 bit only) and compute start sector */ media_ptr -> fx_media_root_cluster_32 = _fx_utility_32_unsigned_read(&boot_sector[FX_ROOT_CLUSTER_32]); #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Return a successful status. */ return(FX_SUCCESS); }{ ... }