1 //-----------------------------------------------------------------------------
2 //-----------------------------------------------------------------------------
3 // FAT16/32 File IO Library
6 // Copyright 2003 - 2012
8 // Email: admin@ultra-embedded.com
11 // If you would like a version with a more permissive license for use in
12 // closed source commercial applications please contact me for details.
13 //-----------------------------------------------------------------------------
15 // This file is part of FAT File IO Library.
17 // FAT File IO Library is free software; you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation; either version 2 of the License, or
20 // (at your option) any later version.
22 // FAT File IO Library is distributed in the hope that it will be useful,
23 // but WITHOUT ANY WARRANTY; without even the implied warranty of
24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 // GNU General Public License for more details.
27 // You should have received a copy of the GNU General Public License
28 // along with FAT File IO Library; if not, write to the Free Software
29 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
34 #include "fat_access.h"
35 #include "fat_table.h"
36 #include "fat_write.h"
37 #include "fat_string.h"
39 #include "fat_format.h"
41 #if FATFS_INC_FORMAT_SUPPORT
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 struct sec_per_clus_table
49 uint8 sectors_per_cluster
;
52 struct sec_per_clus_table _cluster_size_table16
[] =
54 { 32680, 2}, // 16MB - 1K
55 { 262144, 4}, // 128MB - 2K
56 { 524288, 8}, // 256MB - 4K
57 { 1048576, 16}, // 512MB - 8K
58 { 2097152, 32}, // 1GB - 16K
59 { 4194304, 64}, // 2GB - 32K
60 { 8388608, 128},// 2GB - 64K [Warning only supported by Windows XP onwards]
64 struct sec_per_clus_table _cluster_size_table32
[] =
66 { 532480, 1}, // 260MB - 512b
67 { 16777216, 8}, // 8GB - 4K
68 { 33554432, 16}, // 16GB - 8K
69 { 67108864, 32}, // 32GB - 16K
70 { 0xFFFFFFFF, 64},// >32GB - 32K
74 //-----------------------------------------------------------------------------
75 // fatfs_calc_cluster_size: Calculate what cluster size should be used
76 //-----------------------------------------------------------------------------
77 static uint8
fatfs_calc_cluster_size(uint32 sectors
, int is_fat32
)
83 for (i
=0; _cluster_size_table16
[i
].sectors_per_cluster
!= 0;i
++)
84 if (sectors
<= _cluster_size_table16
[i
].sectors
)
85 return _cluster_size_table16
[i
].sectors_per_cluster
;
89 for (i
=0; _cluster_size_table32
[i
].sectors_per_cluster
!= 0;i
++)
90 if (sectors
<= _cluster_size_table32
[i
].sectors
)
91 return _cluster_size_table32
[i
].sectors_per_cluster
;
96 //-----------------------------------------------------------------------------
97 // fatfs_erase_sectors: Erase a number of sectors
98 //-----------------------------------------------------------------------------
99 static int fatfs_erase_sectors(struct fatfs
*fs
, uint32 lba
, int count
)
104 memset(fs
->currentsector
.sector
, 0, FAT_SECTOR_SIZE
);
106 for (i
=0;i
<count
;i
++)
107 if (!fs
->disk_io
.write_media(lba
+ i
, fs
->currentsector
.sector
, 1))
112 //-----------------------------------------------------------------------------
113 // fatfs_create_boot_sector: Create the boot sector
114 //-----------------------------------------------------------------------------
115 static int fatfs_create_boot_sector(struct fatfs
*fs
, uint32 boot_sector_lba
, uint32 vol_sectors
, const char *name
, int is_fat32
)
117 uint32 total_clusters
;
120 // Zero sector initially
121 memset(fs
->currentsector
.sector
, 0, FAT_SECTOR_SIZE
);
123 // OEM Name & Jump Code
124 fs
->currentsector
.sector
[0] = 0xEB;
125 fs
->currentsector
.sector
[1] = 0x3C;
126 fs
->currentsector
.sector
[2] = 0x90;
127 fs
->currentsector
.sector
[3] = 0x4D;
128 fs
->currentsector
.sector
[4] = 0x53;
129 fs
->currentsector
.sector
[5] = 0x44;
130 fs
->currentsector
.sector
[6] = 0x4F;
131 fs
->currentsector
.sector
[7] = 0x53;
132 fs
->currentsector
.sector
[8] = 0x35;
133 fs
->currentsector
.sector
[9] = 0x2E;
134 fs
->currentsector
.sector
[10] = 0x30;
137 fs
->currentsector
.sector
[11] = (FAT_SECTOR_SIZE
>> 0) & 0xFF;
138 fs
->currentsector
.sector
[12] = (FAT_SECTOR_SIZE
>> 8) & 0xFF;
140 // Get sectors per cluster size for the disk
141 fs
->sectors_per_cluster
= fatfs_calc_cluster_size(vol_sectors
, is_fat32
);
142 if (!fs
->sectors_per_cluster
)
143 return 0; // Invalid disk size
145 // Sectors per cluster
146 fs
->currentsector
.sector
[13] = fs
->sectors_per_cluster
;
150 fs
->reserved_sectors
= 8;
152 fs
->reserved_sectors
= 32;
153 fs
->currentsector
.sector
[14] = (fs
->reserved_sectors
>> 0) & 0xFF;
154 fs
->currentsector
.sector
[15] = (fs
->reserved_sectors
>> 8) & 0xFF;
158 fs
->currentsector
.sector
[16] = fs
->num_of_fats
;
160 // Max entries in root dir (FAT16 only)
163 fs
->root_entry_count
= 512;
164 fs
->currentsector
.sector
[17] = (fs
->root_entry_count
>> 0) & 0xFF;
165 fs
->currentsector
.sector
[18] = (fs
->root_entry_count
>> 8) & 0xFF;
169 fs
->root_entry_count
= 0;
170 fs
->currentsector
.sector
[17] = 0;
171 fs
->currentsector
.sector
[18] = 0;
174 // [FAT16] Total sectors (use FAT32 count instead)
175 fs
->currentsector
.sector
[19] = 0x00;
176 fs
->currentsector
.sector
[20] = 0x00;
179 fs
->currentsector
.sector
[21] = 0xF8;
185 // Count of sectors used by the FAT table (FAT16 only)
186 total_clusters
= (vol_sectors
/ fs
->sectors_per_cluster
) + 1;
187 fs
->fat_sectors
= (total_clusters
/(FAT_SECTOR_SIZE
/2)) + 1;
188 fs
->currentsector
.sector
[22] = (uint8
)((fs
->fat_sectors
>> 0) & 0xFF);
189 fs
->currentsector
.sector
[23] = (uint8
)((fs
->fat_sectors
>> 8) & 0xFF);
192 fs
->currentsector
.sector
[24] = 0x00;
193 fs
->currentsector
.sector
[25] = 0x00;
196 fs
->currentsector
.sector
[26] = 0x00;
197 fs
->currentsector
.sector
[27] = 0x00;
200 fs
->currentsector
.sector
[28] = 0x20;
201 fs
->currentsector
.sector
[29] = 0x00;
202 fs
->currentsector
.sector
[30] = 0x00;
203 fs
->currentsector
.sector
[31] = 0x00;
205 // Total sectors for this volume
206 fs
->currentsector
.sector
[32] = (uint8
)((vol_sectors
>>0)&0xFF);
207 fs
->currentsector
.sector
[33] = (uint8
)((vol_sectors
>>8)&0xFF);
208 fs
->currentsector
.sector
[34] = (uint8
)((vol_sectors
>>16)&0xFF);
209 fs
->currentsector
.sector
[35] = (uint8
)((vol_sectors
>>24)&0xFF);
212 fs
->currentsector
.sector
[36] = 0x00;
215 fs
->currentsector
.sector
[37] = 0x00;
218 fs
->currentsector
.sector
[38] = 0x29;
221 fs
->currentsector
.sector
[39] = 0x12;
222 fs
->currentsector
.sector
[40] = 0x34;
223 fs
->currentsector
.sector
[41] = 0x56;
224 fs
->currentsector
.sector
[42] = 0x78;
229 if (i
< (int)strlen(name
))
230 fs
->currentsector
.sector
[i
+43] = name
[i
];
232 fs
->currentsector
.sector
[i
+43] = ' ';
236 fs
->currentsector
.sector
[54] = 'F';
237 fs
->currentsector
.sector
[55] = 'A';
238 fs
->currentsector
.sector
[56] = 'T';
239 fs
->currentsector
.sector
[57] = '1';
240 fs
->currentsector
.sector
[58] = '6';
241 fs
->currentsector
.sector
[59] = ' ';
242 fs
->currentsector
.sector
[60] = ' ';
243 fs
->currentsector
.sector
[61] = ' ';
246 fs
->currentsector
.sector
[510] = 0x55;
247 fs
->currentsector
.sector
[511] = 0xAA;
252 // Count of sectors used by the FAT table (FAT16 only)
253 fs
->currentsector
.sector
[22] = 0;
254 fs
->currentsector
.sector
[23] = 0;
256 // Sectors per track (default)
257 fs
->currentsector
.sector
[24] = 0x3F;
258 fs
->currentsector
.sector
[25] = 0x00;
261 fs
->currentsector
.sector
[26] = 0xFF;
262 fs
->currentsector
.sector
[27] = 0x00;
265 fs
->currentsector
.sector
[28] = 0x00;
266 fs
->currentsector
.sector
[29] = 0x00;
267 fs
->currentsector
.sector
[30] = 0x00;
268 fs
->currentsector
.sector
[31] = 0x00;
270 // Total sectors for this volume
271 fs
->currentsector
.sector
[32] = (uint8
)((vol_sectors
>>0)&0xFF);
272 fs
->currentsector
.sector
[33] = (uint8
)((vol_sectors
>>8)&0xFF);
273 fs
->currentsector
.sector
[34] = (uint8
)((vol_sectors
>>16)&0xFF);
274 fs
->currentsector
.sector
[35] = (uint8
)((vol_sectors
>>24)&0xFF);
276 total_clusters
= (vol_sectors
/ fs
->sectors_per_cluster
) + 1;
277 fs
->fat_sectors
= (total_clusters
/(FAT_SECTOR_SIZE
/4)) + 1;
280 fs
->currentsector
.sector
[36] = (uint8
)((fs
->fat_sectors
>>0)&0xFF);
281 fs
->currentsector
.sector
[37] = (uint8
)((fs
->fat_sectors
>>8)&0xFF);
282 fs
->currentsector
.sector
[38] = (uint8
)((fs
->fat_sectors
>>16)&0xFF);
283 fs
->currentsector
.sector
[39] = (uint8
)((fs
->fat_sectors
>>24)&0xFF);
286 fs
->currentsector
.sector
[40] = 0;
287 fs
->currentsector
.sector
[41] = 0;
290 fs
->currentsector
.sector
[42] = 0;
291 fs
->currentsector
.sector
[43] = 0;
294 fs
->currentsector
.sector
[44] = (uint8
)((fs
->rootdir_first_cluster
>>0)&0xFF);
295 fs
->currentsector
.sector
[45] = (uint8
)((fs
->rootdir_first_cluster
>>8)&0xFF);
296 fs
->currentsector
.sector
[46] = (uint8
)((fs
->rootdir_first_cluster
>>16)&0xFF);
297 fs
->currentsector
.sector
[47] = (uint8
)((fs
->rootdir_first_cluster
>>24)&0xFF);
300 fs
->currentsector
.sector
[48] = (uint8
)((fs
->fs_info_sector
>>0)&0xFF);
301 fs
->currentsector
.sector
[49] = (uint8
)((fs
->fs_info_sector
>>8)&0xFF);
304 fs
->currentsector
.sector
[50] = 6;
305 fs
->currentsector
.sector
[51] = 0;
308 fs
->currentsector
.sector
[64] = 0x00;
311 fs
->currentsector
.sector
[66] = 0x29;
314 fs
->currentsector
.sector
[67] = 0x12;
315 fs
->currentsector
.sector
[68] = 0x34;
316 fs
->currentsector
.sector
[69] = 0x56;
317 fs
->currentsector
.sector
[70] = 0x78;
322 if (i
< (int)strlen(name
))
323 fs
->currentsector
.sector
[i
+71] = name
[i
];
325 fs
->currentsector
.sector
[i
+71] = ' ';
329 fs
->currentsector
.sector
[82] = 'F';
330 fs
->currentsector
.sector
[83] = 'A';
331 fs
->currentsector
.sector
[84] = 'T';
332 fs
->currentsector
.sector
[85] = '3';
333 fs
->currentsector
.sector
[86] = '2';
334 fs
->currentsector
.sector
[87] = ' ';
335 fs
->currentsector
.sector
[88] = ' ';
336 fs
->currentsector
.sector
[89] = ' ';
339 fs
->currentsector
.sector
[510] = 0x55;
340 fs
->currentsector
.sector
[511] = 0xAA;
343 if (fs
->disk_io
.write_media(boot_sector_lba
, fs
->currentsector
.sector
, 1))
348 //-----------------------------------------------------------------------------
349 // fatfs_create_fsinfo_sector: Create the FSInfo sector (FAT32)
350 //-----------------------------------------------------------------------------
351 static int fatfs_create_fsinfo_sector(struct fatfs
*fs
, uint32 sector_lba
)
353 // Zero sector initially
354 memset(fs
->currentsector
.sector
, 0, FAT_SECTOR_SIZE
);
357 fs
->currentsector
.sector
[0] = 0x52;
358 fs
->currentsector
.sector
[1] = 0x52;
359 fs
->currentsector
.sector
[2] = 0x61;
360 fs
->currentsector
.sector
[3] = 0x41;
363 fs
->currentsector
.sector
[484] = 0x72;
364 fs
->currentsector
.sector
[485] = 0x72;
365 fs
->currentsector
.sector
[486] = 0x41;
366 fs
->currentsector
.sector
[487] = 0x61;
369 fs
->currentsector
.sector
[488] = 0xFF;
370 fs
->currentsector
.sector
[489] = 0xFF;
371 fs
->currentsector
.sector
[490] = 0xFF;
372 fs
->currentsector
.sector
[491] = 0xFF;
375 fs
->currentsector
.sector
[492] = 0xFF;
376 fs
->currentsector
.sector
[493] = 0xFF;
377 fs
->currentsector
.sector
[494] = 0xFF;
378 fs
->currentsector
.sector
[495] = 0xFF;
381 fs
->currentsector
.sector
[510] = 0x55;
382 fs
->currentsector
.sector
[511] = 0xAA;
384 if (fs
->disk_io
.write_media(sector_lba
, fs
->currentsector
.sector
, 1))
389 //-----------------------------------------------------------------------------
390 // fatfs_erase_fat: Erase FAT table using fs details in fs struct
391 //-----------------------------------------------------------------------------
392 static int fatfs_erase_fat(struct fatfs
*fs
, int is_fat32
)
396 // Zero sector initially
397 memset(fs
->currentsector
.sector
, 0, FAT_SECTOR_SIZE
);
399 // Initialise default allocate / reserved clusters
402 SET_16BIT_WORD(fs
->currentsector
.sector
, 0, 0xFFF8);
403 SET_16BIT_WORD(fs
->currentsector
.sector
, 2, 0xFFFF);
407 SET_32BIT_WORD(fs
->currentsector
.sector
, 0, 0x0FFFFFF8);
408 SET_32BIT_WORD(fs
->currentsector
.sector
, 4, 0xFFFFFFFF);
409 SET_32BIT_WORD(fs
->currentsector
.sector
, 8, 0x0FFFFFFF);
412 if (!fs
->disk_io
.write_media(fs
->fat_begin_lba
+ 0, fs
->currentsector
.sector
, 1))
415 // Zero remaining FAT sectors
416 memset(fs
->currentsector
.sector
, 0, FAT_SECTOR_SIZE
);
417 for (i
=1;i
<fs
->fat_sectors
*fs
->num_of_fats
;i
++)
418 if (!fs
->disk_io
.write_media(fs
->fat_begin_lba
+ i
, fs
->currentsector
.sector
, 1))
423 //-----------------------------------------------------------------------------
424 // fatfs_format_fat16: Format a FAT16 partition
425 //-----------------------------------------------------------------------------
426 int fatfs_format_fat16(struct fatfs
*fs
, uint32 volume_sectors
, const char *name
)
428 fs
->currentsector
.address
= FAT32_INVALID_CLUSTER
;
429 fs
->currentsector
.dirty
= 0;
431 fs
->next_free_cluster
= 0; // Invalid
435 // Make sure we have read + write functions
436 if (!fs
->disk_io
.read_media
|| !fs
->disk_io
.write_media
)
437 return FAT_INIT_MEDIA_ACCESS_ERROR
;
440 fs
->fat_type
= FAT_TYPE_16
;
442 // Not valid for FAT16
443 fs
->fs_info_sector
= 0;
444 fs
->rootdir_first_cluster
= 0;
446 // Sector 0: Boot sector
447 // NOTE: We don't need an MBR, it is a waste of a good sector!
449 if (!fatfs_create_boot_sector(fs
, fs
->lba_begin
, volume_sectors
, name
, 0))
452 // For FAT16 (which this may be), rootdir_first_cluster is actuall rootdir_first_sector
453 fs
->rootdir_first_sector
= fs
->reserved_sectors
+ (fs
->num_of_fats
* fs
->fat_sectors
);
454 fs
->rootdir_sectors
= ((fs
->root_entry_count
* 32) + (FAT_SECTOR_SIZE
- 1)) / FAT_SECTOR_SIZE
;
456 // First FAT LBA address
457 fs
->fat_begin_lba
= fs
->lba_begin
+ fs
->reserved_sectors
;
459 // The address of the first data cluster on this volume
460 fs
->cluster_begin_lba
= fs
->fat_begin_lba
+ (fs
->num_of_fats
* fs
->fat_sectors
);
462 // Initialise FAT sectors
463 if (!fatfs_erase_fat(fs
, 0))
466 // Erase Root directory
467 if (!fatfs_erase_sectors(fs
, fs
->lba_begin
+ fs
->rootdir_first_sector
, fs
->rootdir_sectors
))
472 //-----------------------------------------------------------------------------
473 // fatfs_format_fat32: Format a FAT32 partition
474 //-----------------------------------------------------------------------------
475 int fatfs_format_fat32(struct fatfs
*fs
, uint32 volume_sectors
, const char *name
)
477 fs
->currentsector
.address
= FAT32_INVALID_CLUSTER
;
478 fs
->currentsector
.dirty
= 0;
480 fs
->next_free_cluster
= 0; // Invalid
484 // Make sure we have read + write functions
485 if (!fs
->disk_io
.read_media
|| !fs
->disk_io
.write_media
)
486 return FAT_INIT_MEDIA_ACCESS_ERROR
;
489 fs
->fat_type
= FAT_TYPE_32
;
491 // Basic defaults for normal FAT32 partitions
492 fs
->fs_info_sector
= 1;
493 fs
->rootdir_first_cluster
= 2;
495 // Sector 0: Boot sector
496 // NOTE: We don't need an MBR, it is a waste of a good sector!
498 if (!fatfs_create_boot_sector(fs
, fs
->lba_begin
, volume_sectors
, name
, 1))
501 // First FAT LBA address
502 fs
->fat_begin_lba
= fs
->lba_begin
+ fs
->reserved_sectors
;
504 // The address of the first data cluster on this volume
505 fs
->cluster_begin_lba
= fs
->fat_begin_lba
+ (fs
->num_of_fats
* fs
->fat_sectors
);
507 // Initialise FSInfo sector
508 if (!fatfs_create_fsinfo_sector(fs
, fs
->fs_info_sector
))
511 // Initialise FAT sectors
512 if (!fatfs_erase_fat(fs
, 1))
515 // Erase Root directory
516 if (!fatfs_erase_sectors(fs
, fatfs_lba_of_cluster(fs
, fs
->rootdir_first_cluster
), fs
->sectors_per_cluster
))
521 //-----------------------------------------------------------------------------
522 // fatfs_format: Format a partition with either FAT16 or FAT32 based on size
523 //-----------------------------------------------------------------------------
524 int fatfs_format(struct fatfs
*fs
, uint32 volume_sectors
, const char *name
)
526 // 2GB - 32K limit for safe behaviour for FAT16
527 if (volume_sectors
<= 4194304)
528 return fatfs_format_fat16(fs
, volume_sectors
, name
);
530 return fatfs_format_fat32(fs
, volume_sectors
, name
);
532 #endif /*FATFS_INC_FORMAT_SUPPORT*/