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_directory.h"
#include "fx_media.h"
#include "fx_utility.h"
#include "fx_directory_exFAT.h"
...
...
_fx_media_volume_set(FX_MEDIA *, CHAR *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesfilexcommon/src/fx_media_volume_set.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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_directory.h" #include "fx_media.h" #include "fx_utility.h" 5 includes#ifdef FX_ENABLE_EXFAT #include "fx_directory_exFAT.h" #endif /* FX_ENABLE_EXFAT */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _fx_media_volume_set PORTABLE C */ /* 6.1.7 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function sets the volume name to the name supplied by the */ /* caller. */ /* */ /* INPUT */ /* */ /* media_ptr Media control block pointer */ /* volume_name New volume name */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _fx_directory_entry_read Read a directory entry */ /* _fx_utility_logical_sector_read Read directory sector */ /* _fx_utility_logical_sector_write Write directory sector */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* 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 */ /* 06-02-2021 Bhupendra Naphade Modified comment(s),updated */ /* the initialization of */ /* dir_entry for exFAT format, */ /* resulting in version 6.1.7 */ /* */... /**************************************************************************/ UINT _fx_media_volume_set(FX_MEDIA *media_ptr, CHAR *volume_name) { ULONG i, j; FX_DIR_ENTRY dir_entry, dir_entry1; UINT status, offset; UCHAR *work_ptr; CHAR alpha; /* Check the media to make sure it is open. */ if (media_ptr -> fx_media_id != FX_MEDIA_ID) { /* Return the media not opened error. */ return(FX_MEDIA_NOT_OPEN); }if (media_ptr -> fx_media_id != FX_MEDIA_ID) { ... } dir_entry.fx_dir_entry_log_sector = 0; dir_entry.fx_dir_entry_byte_offset = 0; /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_MEDIA_VOLUME_SET, media_ptr, volume_name, 0, 0, FX_TRACE_MEDIA_EVENTS, 0, 0) /* Protect against other threads accessing the media. */ FX_PROTECT #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* Setup pointer to media name buffer. */ dir_entry.fx_dir_entry_name = media_ptr -> fx_media_name_buffer; /* Clear the short name string. */ dir_entry.fx_dir_entry_short_name[0] = 0; /* Attempt to find the volume name in the root directory. */ i = 0; do { /* Read an entry from the root directory. */ status = _fx_directory_entry_read(media_ptr, FX_NULL, &i, &dir_entry); /* Check for error status. */ if (status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Return to caller. */ return(status); }if (status != FX_SUCCESS) { ... } /* Check for a volume name. */ if (dir_entry.fx_dir_entry_type == FX_EXFAT_DIR_ENTRY_TYPE_VOLUME_LABEL) { /* Yes, we have found a previously set volume name. */ break; }if (dir_entry.fx_dir_entry_type == FX_EXFAT_DIR_ENTRY_TYPE_VOLUME_LABEL) { ... } /* Move to next directory entry. */ i++; ...} while (i < media_ptr -> fx_media_root_directory_entries); /* Determine if a volume entry has been found. */ if (i == media_ptr -> fx_media_root_directory_entries) { /* Volume entry not found, attempt to find a free entry in the root directory. */ i = 0; do { /* Read an entry from the root directory. */ status = _fx_directory_entry_read(media_ptr, FX_NULL, &i, &dir_entry); /* Check for error status. */ if (status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Return to caller. */ return(status); }if (status != FX_SUCCESS) { ... } /* Check for a volume name. */ if (dir_entry.fx_dir_entry_type == FX_EXFAT_DIR_ENTRY_TYPE_FREE) { /* Yes, we have found a free directory entry. */ break; }if (dir_entry.fx_dir_entry_type == FX_EXFAT_DIR_ENTRY_TYPE_FREE) { ... } /* Move to next directory entry. */ i++; ...} while (i < media_ptr -> fx_media_root_directory_entries); /* Determine if a free entry has been found. */ if (i == media_ptr -> fx_media_root_directory_entries) { /* Release media protection. */ FX_UNPROTECT /* No existing volume name was found, return an error. */ return(FX_NOT_FOUND); }if (i == media_ptr -> fx_media_root_directory_entries) { ... } }if (i == media_ptr -> fx_media_root_directory_entries) { ... } /* Read the logical directory sector. */ status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) dir_entry.fx_dir_entry_log_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DIRECTORY_SECTOR); /* Determine if an error occurred. */ if (status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Return error code. */ return(status); }if (status != FX_SUCCESS) { ... } /* Offset to volume label field. */ offset = dir_entry.fx_dir_entry_byte_offset + FX_EXFAT_VOLUME_LABEL; /* Loop to store the volume name. */ for (i = 0; volume_name[i]; i++) { /* Have we reached the end? */ if (i == 11) { break; }if (i == 11) { ... } /* Pickup volume name byte. */ alpha = volume_name[i]; /* Store a byte of the volume name. */ _fx_utility_16_unsigned_write(&media_ptr -> fx_media_memory_buffer[offset], (UINT)alpha); /* Move to next character. */ offset += 2; }for (i = 0; volume_name[i]; i++) { ... } /* Offset to character count field. */ offset = dir_entry.fx_dir_entry_byte_offset; /* Store volume label entry type. */ media_ptr -> fx_media_memory_buffer[offset] = FX_EXFAT_DIR_ENTRY_TYPE_VOLUME_LABEL; /* Store the character count. */ media_ptr -> fx_media_memory_buffer[offset + FX_EXFAT_CHAR_COUNT] = (UCHAR)i; /* Write the directory sector to the media. */ status = _fx_utility_logical_sector_write(media_ptr, (ULONG64) dir_entry.fx_dir_entry_log_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DIRECTORY_SECTOR); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } else { #endif /* FX_ENABLE_EXFAT */ /* First, check for an invalid volume name. */ if (volume_name[0] == 0) { /* Yes, volume name is invalid. Return an error. */ return(FX_INVALID_NAME); }if (volume_name[0] == 0) { ... } /* Read the logical directory sector 0 - we just do this to get a memory_buffer pointer */ status = _fx_utility_logical_sector_read(media_ptr, ((ULONG64) 0), media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DATA_SECTOR); /* Check the read status. */ if (status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of driver read boot sector requests. */ media_ptr -> fx_media_driver_boot_read_requests++;/* ... */ #endif /* Build a driver request to read the boot record. */ media_ptr -> fx_media_driver_request = FX_DRIVER_BOOT_READ; media_ptr -> fx_media_driver_status = FX_IO_ERROR; media_ptr -> fx_media_driver_buffer = media_ptr -> fx_media_memory_buffer; media_ptr -> fx_media_driver_sectors = 1; media_ptr -> fx_media_driver_sector_type = FX_BOOT_SECTOR; /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_BOOT_READ, media_ptr, media_ptr -> fx_media_memory_buffer, 0, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0) /* Invoke the driver to read the boot sector. */ (media_ptr -> fx_media_driver_entry) (media_ptr); /* Determine if the request is successful. */ if (media_ptr -> fx_media_driver_status) { /* Release media protection. */ FX_UNPROTECT /* An error occurred in the driver. */ return(media_ptr -> fx_media_driver_status); }if (media_ptr -> fx_media_driver_status) { ... } /* Calculate the offset based on the FAT present. */ if (media_ptr -> fx_media_32_bit_FAT) { /* FAT32 is present. */ offset = FX_VOLUME_LABEL_32; }if (media_ptr -> fx_media_32_bit_FAT) { ... } else { /* FAT12/16 is present. */ offset = FX_VOLUME_LABEL; }else { ... } /* Loop to store the volume name. */ for (i = 0; volume_name[i]; i++) { /* Have we reached the end? */ if (i == 11) { break; }if (i == 11) { ... } /* Pickup volume name byte. */ alpha = volume_name[i]; /* Determine if alpha needs to be converted to upper case. */ if ((alpha >= 'a') && (alpha <= 'z')) { /* Convert alpha to upper case. */ alpha = (CHAR)((INT)alpha - 0x20); }if ((alpha >= 'a') && (alpha <= 'z')) { ... } /* Store a byte of the volume name. */ media_ptr -> fx_media_memory_buffer[offset + i] = (UCHAR)alpha; }for (i = 0; volume_name[i]; i++) { ... } /* Now pad with spaces. */ for (; i < 11; i++) { /* Append space character to volume name. */ media_ptr -> fx_media_memory_buffer[offset + i] = 0x20; }for (; i < 11; i++) { ... } #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of driver write boot sector requests. */ media_ptr -> fx_media_driver_boot_write_requests++;/* ... */ #endif /* Write the boot sector with the new volume name. */ media_ptr -> fx_media_driver_request = FX_DRIVER_BOOT_WRITE; media_ptr -> fx_media_driver_status = FX_IO_ERROR; media_ptr -> fx_media_driver_buffer = media_ptr -> fx_media_memory_buffer; media_ptr -> fx_media_driver_sectors = 1; media_ptr -> fx_media_driver_sector_type = FX_BOOT_SECTOR; /* Set the system write flag since we are writing the boot sector. */ media_ptr -> fx_media_driver_system_write = FX_TRUE; /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_BOOT_WRITE, media_ptr, media_ptr -> fx_media_memory_buffer, 0, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0) /* Invoke the driver to write the boot sector. */ (media_ptr -> fx_media_driver_entry) (media_ptr); /* Clear the system write flag. */ media_ptr -> fx_media_driver_system_write = FX_FALSE; /* Determine if the request is successful. */ if (media_ptr -> fx_media_driver_status) { /* Release media protection. */ FX_UNPROTECT /* An error occurred in the driver. */ return(media_ptr -> fx_media_driver_status); }if (media_ptr -> fx_media_driver_status) { ... } /* Setup pointer to media name buffer. */ dir_entry1.fx_dir_entry_name = media_ptr -> fx_media_name_buffer; /* Clear the short name string. */ dir_entry1.fx_dir_entry_short_name[0] = 0; /* Now we need to find the copy of the volume name in the root directory. */ i = 0; j = media_ptr -> fx_media_root_directory_entries + 1; do { /* Read an entry from the root directory. */ status = _fx_directory_entry_read(media_ptr, FX_NULL, &i, &dir_entry1); /* Check for error status. */ if (status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Return to caller. */ return(status); }if (status != FX_SUCCESS) { ... } /* Determine if this is an empty entry. */ if ((dir_entry1.fx_dir_entry_name[0] == (CHAR)FX_DIR_ENTRY_FREE) && (dir_entry1.fx_dir_entry_short_name[0] == 0)) { /* Yes, this is free entry. Is it the first? */ if (i < j) { /* Yes, first free entry - remember it. */ dir_entry = dir_entry1; j = i; }if (i < j) { ... } }if ((dir_entry1.fx_dir_entry_name[0] == (CHAR)FX_DIR_ENTRY_FREE) && (dir_entry1.fx_dir_entry_short_name[0] == 0)) { ... } /* Determine if the directory entries are exhausted. */ else if (dir_entry1.fx_dir_entry_name[0] == FX_DIR_ENTRY_DONE) { /* Yes, this we are at the end of the directory. Have there been any other free entries? *//* ... */ if (i < j) { /* No, we need to remember this as the free entry. */ dir_entry = dir_entry1; j = i; }if (i < j) { ... } break; }else if (dir_entry1.fx_dir_entry_name[0] == FX_DIR_ENTRY_DONE) { ... } /* Check for a volume name. */ else if (dir_entry1.fx_dir_entry_attributes & FX_VOLUME) { /* Yes, we have found a previously set volume name - use this entry. */ dir_entry = dir_entry1; j = i; break; }else if (dir_entry1.fx_dir_entry_attributes & FX_VOLUME) { ... } /* Move to next directory entry. */ i++; ...} while (i < media_ptr -> fx_media_root_directory_entries); /* Determine if a volume entry was not found and there are no more empty slots. */ if (i == media_ptr -> fx_media_root_directory_entries) { /* Determine if there was a free or previous volume name. */ if (j == (media_ptr -> fx_media_root_directory_entries + 1)) { /* No, nothing was available in the root directory. */ /* Release media protection. */ FX_UNPROTECT /* No, existing volume name or space in the root directly was found, return an error. */ return(FX_MEDIA_INVALID); }if (j == (media_ptr -> fx_media_root_directory_entries + 1)) { ... } }if (i == media_ptr -> fx_media_root_directory_entries) { ... } /* Now set the volume name and attribute. */ /* Read the logical directory sector. */ status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) dir_entry.fx_dir_entry_log_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_BOOT_SECTOR); /* Check the status of reading the directory entry. */ if (status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Calculate pointer into directory buffer. */ work_ptr = media_ptr -> fx_media_memory_buffer + (UINT)dir_entry.fx_dir_entry_byte_offset; /* Copy the volume name into the directory entry. */ for (i = 0; volume_name[i]; i++) { /* Have we reached the end? */ if (i == 11) { break; }if (i == 11) { ... } /* Pickup volume name byte. */ alpha = volume_name[i]; /* Determine if alpha needs to be converted to upper case. */ if ((alpha >= 'a') && (alpha <= 'z')) { /* Convert alpha to upper case. */ alpha = (CHAR)((INT)alpha - 0x20); }if ((alpha >= 'a') && (alpha <= 'z')) { ... } /* Store volume name. */ work_ptr[i] = (UCHAR)alpha; }for (i = 0; volume_name[i]; i++) { ... } /* Pad with space characters. */ for (; i < 11; i++) { work_ptr[i] = 0x20; }for (; i < 11; i++) { ... } /* Set the appropriate attributes. */ work_ptr[11] = FX_VOLUME | FX_ARCHIVE; /* Set the other parts of the volume entry. */ /* Clear the hi word of cluster. */ work_ptr[20] = 0; work_ptr[21] = 0; /* Clear the low word of cluster. */ work_ptr[26] = 0; work_ptr[27] = 0; /* Clear the file size. */ work_ptr[28] = 0; work_ptr[29] = 0; work_ptr[30] = 0; work_ptr[31] = 0; /* Write the directory sector to the media. */ status = _fx_utility_logical_sector_write(media_ptr, (ULONG64) dir_entry.fx_dir_entry_log_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DIRECTORY_SECTOR); #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Release media protection. */ FX_UNPROTECT /* Return the status. */ return(status); }{ ... }