OSDN Git Service

doc: reflect removal of FS-related commands
[android-x86/external-parted.git] / doc / FAT
1 ===============================================================================
2                 GNU Parted FAT file system documentation
3 ===============================================================================
4
5       by Andrew Clausen <clausen@gnu.org>
6
7       Copyright (C) 2000, 2001 Free Software Foundation, Inc.
8
9       Permission is granted to copy, distribute and/or modify this document
10       under the terms of the GNU Free Documentation License, Version 1.3
11       or any later version published by the Free Software Foundation;
12       with the no Invariant Sections, with the no Front-Cover Texts, and
13       with no Back-Cover Texts.  A copy of the license is included in the
14       file, COPYING.DOC.
15
16
17 CONTENTS
18 --------
19
20                 PART I - THE FAT FILE SYSTEM
21
22 1       Introduction
23
24 2       Overview
25
26 3       The Boot Sector
27 3.1     Data Layout
28 3.2     Descriptions of Fields
29 3.3     Calculating the ignored fields
30 3.4     DOS/Windows bootstrap process
31
32 4       The Info Sector
33 4.1     Data Layout
34 4.2     Descriptions of Fields
35
36 5       File Allocation Tables
37
38 6       Directory tree
39 6.1     Directory entries
40
41                 PART II - GNU PARTED'S FAT IMPLEMENTATION
42
43 7       Resizing "issues"
44
45 8       Overview of GNU Parted's Strategy
46
47 ===============================================================================
48                         PART I  -  THE FAT FILE SYSTEM
49 ===============================================================================
50
51 -------------------------------------------------------------------------------
52 1       INTRODUCTION
53 -------------------------------------------------------------------------------
54
55 This document describes the FAT filesystem, and GNU Parted's support for it.
56
57 Unfortunately, there are no particularly good sources of information on the FAT
58 filesystem.  The information here was deduced from the source code of about 10
59 different programs, documentation from about 20 different sources and testing.
60 There are many cases where documentation for FAT from various sources
61 (including Microsoft) are misleading, or just plain wrong.  For us,
62 documentation is correct if it matches the behaviour of Microsoft's
63 implementation.
64
65
66 -------------------------------------------------------------------------------
67 2       OVERVIEW
68 -------------------------------------------------------------------------------
69
70 FAT is a filesystem that is mainly used by Microsoft DOS, Windows 95,
71 Windows 98 and Windows 2000.  FAT also stands for File Allocation Table - a
72 part of the FAT filesystem.
73
74 FAT comes in three flavors: FAT12, FAT16 and FAT32.
75 FAT12 is used on floppy disks, and REALLY old hard drives <32Mb.  FAT16 is
76 typically used on small hard drives <500Mb, and is very inefficient for large
77 hard drives.  FAT32 is used on hard drives >500Mb under Windows 95 OSR2 and
78 later and Windows 2000.  These three flavors have important differences (not
79 JUST an increase in the maximum possible number of clusters).  On FAT12 and
80 FAT16 cluster size directly relates to the filesystem size: the number of
81 clusters is always between 2041 and 4080 resp. 32761 and 65520.
82
83 The FAT filesystem has these parts (on disk, in this order):
84   * a bootsector.  This contains all of the information about the filesystem
85 - whether it's FAT12, FAT16 or FAT32, how big it is, etc.
86
87   * an information sector (FAT32 only).  This contains additional information
88 that couldn't fit in the boot sector.
89
90   * file allocation tables (FATs).  There are usually two identical copies.
91 This is used to store the sequence of clusters that make of files.  Essentially,
92 if you want to know the number of the cluster that comes after cluster X
93 in a file, you look up the number for X.  If X is a magic number, it means
94 it's the end of the file.
95
96   * clusters.  The data inside files are stored in clusters.  Most directory
97 information is stored in clusters (except the root directory in FAT12 and
98 FAT16).   Clusters may be 1, 2, 4, 8, etc. sectors.  Clusters are numbered,
99 counting from 2.
100
101   * directory tree.  The FAT filesystem has files and directories (no named
102 pipes, links, or anything fancy like that), that are stored in clusters.  The
103 root directory on FAT12 and FAT16 is stored separately.  In FAT32, the root
104 directory is stored inside a normal cluster, just like everything else.
105
106
107 -------------------------------------------------------------------------------
108 3       THE BOOT SECTOR
109 -------------------------------------------------------------------------------
110
111 The boot sector contains all of the information about the filesystem
112 - whether it's FAT12, FAT16 or FAT32, how big it is, etc.  It also contains
113 the boot loader for the operating system, if there is an operating system
114 on the file system.  It is always the first thing to appear in the filesystem
115 - i.e. it's found at sector 0.
116
117 A word of warning: while the values inside the boot sector will always be
118 consistent with the file system, many of these values are not read by
119 Microsoft's implementation - they are calculated independently.
120
121
122 3.1     The Data Layout
123 -------------------------------------------------------------------------------
124
125 Taken from libparted/fs_fat/bootsector.h:
126
127 struct __attribute__ ((packed)) _FatBootSector {
128         __u8    boot_jump[3];   /* 00: Boot strap short or near jump */
129         __u8    system_id[8];   /* 03: system name */
130         __u16   sector_size;    /* 0b: bytes per logical sector */
131         __u8    cluster_size;   /* 0d: sectors/cluster */
132         __u16   reserved;       /* 0e: reserved sectors */
133         __u8    fats;           /* 10: number of FATs */
134         __u16   dir_entries;    /* 11: number of root directory entries */
135         __u16   sectors;        /* 13: if 0, sector_count supersedes */
136         __u8    media;          /* 15: media code */
137         __u16   fat_length;     /* 16: sectors/FAT for FAT12/16 */
138         __u16   secs_track;     /* 18: sectors per track */
139         __u16   heads;          /* 1a: number of heads */
140         __u32   hidden;         /* 1c: hidden sectors (partition start) */
141         __u32   sector_count;   /* 20: no. of sectors (if sectors == 0) */
142
143         union __attribute__ ((packed)) {
144                 /* FAT16 fields */
145                 struct __attribute__ ((packed)) {
146                         __u8    drive_num;      /* 24: */
147                         __u8    empty_1;        /* 25: */
148                         __u8    ext_signature;  /* 26: always 0x29 */
149                         __u32   serial_number;  /* 27: */
150                         __u8    volume_name [11];       /* 2b: */
151                         __u8    fat_name [8];   /* 37: */
152                         __u8    boot_code[448]; /* 3f: Boot code (or message) */
153                 } fat16;
154                 /* FAT32 fields */
155                 struct __attribute__ ((packed)) {
156                         __u32   fat_length;     /* 24: size of FAT in sectors */
157                         __u16   flags;          /* 28: bit8: fat mirroring, low4: active fat */
158                         __u16   version;        /* 2a: minor * 256 + major */
159                         __u32   root_dir_cluster;       /* 2c: */
160                         __u16   info_sector;    /* 30: */
161                         __u16   backup_sector;  /* 32: */
162                         __u8    empty_1 [12];   /* 34: */
163                         __u16   drive_num;      /* 40: */
164                         __u8    ext_signature;  /* 42: always 0x29 */
165                         __u32   serial_number;  /* 43: */
166                         __u8    volume_name [11];       /* 47: */
167                         __u8    fat_name [8];   /* 52: */
168                         __u8    boot_code[420]; /* 5a: Boot code (or message) */
169                 } fat32;
170         } u;
171
172         __u16   boot_sign;      /* 1fe: always 0xAA55 */
173 };
174
175
176 3.2     Descriptions of Fields
177 -------------------------------------------------------------------------------
178
179 3.2.1   Fields common to FAT12, FAT16 and FAT32
180 -----------------------------------------------
181         __u8    boot_jump[3];   /* 00: Boot strap short or near jump */
182 This contains the Intel x86 instruction to "jump" to further down in the
183 boot sector.  This is necessary, because on PC systems, the first sector of
184 the disk is loaded and executed.  On hard disks of PC systems, the first
185 sector of the disk is in fact the Master Boot Record - which contains the
186 partition table.  The master boot record loads the first sector of the boot
187 partition, so the end result is the same for floppy's and hard disks.
188
189
190         __u8    system_id[8];   /* 03: system name */
191 This contains the name of the program or operatings system that created the
192 file system.  For FAT32, it seems you must have "MSWIN4.1" here.
193 If this is "MSDMF3.2" (afaik only the "MSDMF" is checked") the partition
194 can't be written under Windows 9x, Windows NT and Windows 2000.  This is
195 how Microsoft realizes read-only installation or distribution floppy disks.
196
197
198         __u16   sector_size;    /* 0b: bytes per logical sector */
199 This is bizarre.  Sectors are always 512 bytes.  However, a "logical" sector
200 is a hack that allows sectors to be bigger (a multiple of 512 bytes).  This
201 is rarely used, and untested in GNU Parted at the moment.  (Side note: is
202 it possible to use this to avoid requiring a cluster resize?)
203
204
205         __u8    cluster_size;   /* 0d: sectors/cluster */
206 THIS IS IGNORED BY MICROSOFT'S IMPLEMENTATION OF FAT12 AND FAT16!  (See section
207 3.3)   This contains the size of all clusters, given in sectors.  This value is
208 "read-only".
209
210
211         __u16   reserved;       /* 0e: reserved sectors */
212 The number of sectors before the file allocation tables begin.  i.e. The
213 number of the first sector of the first file allocation table.
214
215
216         __u8    fats;           /* 10: number of FATs */
217 The number of file allocation tables (usually 2).
218
219
220         __u16   dir_entries;    /* 11: number of root directory entries */
221 The size of the root directory (FAT12 and FAT16 only), in "directory entries"
222 (32 bytes).  The root directory is immediately after the FATs (FAT12 and
223 FAT16 only).  The first cluster (i.e. cluster number 2) starts immediately
224 after the root directory (or after the FATs for FAT32).
225
226
227         __u16   sectors;        /* 13: if 0, sector_count supersedes */
228 THIS IS IGNORED BY MICROSOFT'S IMPLEMENTATION!  The total size of the file
229 system.  If the file system is bigger than 65536 sectors, this is set to 0,
230 and a 32 bit field is used instead.  Microsoft's implementation gets this
231 values from hardware (for floppy disks), or the partition table (for hard
232 disks), rather than reading it off disk.
233
234
235         __u8    media;          /* 15: media code */
236 For hard disks, this should always be 0xf8.
237
238
239         __u16   fat_length;     /* 16: sectors/FAT for FAT12/16 */
240 THIS IS IGNORED BY MICROSOFT'S IMPLEMENTATION!  (See section 3.3)  The size in
241 sectors of each file allocation table (FAT12 and FAT16 only).  A 32-bit field
242 is used for FAT32.  This value is "read-only".
243
244
245
246         __u16   secs_track;     /* 18: sectors per track */
247         __u16   heads;          /* 1a: number of heads */
248 These should match the BIOS geometry.  The GNU Parted README file explains
249 BIOS geometry.
250
251
252         __u32   hidden;         /* 1c: hidden sectors (partition start) */
253 On a hard disk, this should be the number of sectors from the start of the
254 head (in terms of BIOS geometry) to the start of the partition.  i.e. the
255 S in the CHS partition start.
256
257
258         __u32   sector_count;   /* 20: no. of sectors (if sectors == 0) */
259 The size of the file system in sectors (if sectors is 0).
260
261
262         __u16   boot_sign;      /* 1fe: always 0xAA55 */
263 Boot sector signature.  Don't use this exclusively to detect FAT file systems!
264 It's also the signature for partition table sectors (and it appears in the
265 same place too!)  Idiots.
266
267 3.2.2   Fields in FAT12 and FAT16
268 ---------------------------------
269
270         __u8    drive_num;      /* 24: */
271 Always 0x80.
272
273
274         __u8    ext_signature;  /* 26: always 0x29 */
275 Always 0x29.
276
277
278         __u32   serial_number;  /* 27: */
279 Serial number: Used to detect media change on floppy disk and removable drives.
280
281
282         __u8    volume_name [11];       /* 2b: */
283 The disk label.
284
285
286         __u8    fat_name [8];   /* 37: */
287 "FAT12\0\0\0" or "FAT16\0\0\0".
288
289
290 3.2.3   Fields in FAT32
291 -----------------------
292
293         __u32   fat_length;     /* 24: size of FAT in sectors */
294 The size in sectors of each file allocation table.
295
296
297         __u16   flags;          /* 28: bit8: fat mirroring, low4: active fat */
298 No idea what these are.
299
300
301         __u16   version;        /* 2a: minor * 256 + major */
302 Seems to be 0 (?)
303
304
305         __u32   root_dir_cluster;       /* 2c: */
306 The number of the first cluster in the root directory.
307
308
309         __u16   info_sector;    /* 30: */
310 The number of the information sector.
311
312
313         __u16   backup_sector;  /* 32: */
314 The number of the backup of the boot sector (i.e. this sector).
315
316
317         __u16   drive_num;      /* 40: */
318 Always 0x80.
319
320
321         __u8    ext_signature;  /* 42: always 0x29 */
322 Always 0x29.
323
324
325         __u32   serial_number;  /* 43: */
326 Serial number (for Echelon, or something)
327
328
329         __u8    volume_name [11];       /* 47: */
330 The disk label.
331
332
333         __u8    fat_name [8];   /* 52: */
334 "FAT32\0\0\0".
335
336
337 3.3     Calculating the ignored fields
338 -------------------------------------------------------------------------------
339 The cluster_size and fat_length fields are ignored by Microsoft's
340 implementation of FAT12 and FAT16, but NOT FAT32.  That is, they are written out
341 correctly, but NOT READ IN.  (Note: if FAT32 file system is configured to
342 have less than 65520 clusters, then Windows assumes it's FAT16)
343
344 Since these values don't usually change unless you resize a filesystem, this
345 causes no problems.  However, if you want to resize the filesystem, you have to
346 calculate these values to what Microsoft calculates them to, from the size of
347 the filesystem.  It took me 2 months to figure this out (I want to KILL
348 somebody...)
349
350 Here's the algorithm I came up with that seemed to match all my test data:
351 (from libparted/fs_fat/calc.c)
352
353 FatCluster
354 fat_max_cluster_count (FatType fat_type) {
355         switch (fat_type) {
356                 case FAT_TYPE_FAT12: return 0xff0;
357                 case FAT_TYPE_FAT16: return 0xfff0;
358                 case FAT_TYPE_FAT32: return 0x0ffffff0;
359         }
360         return 0;
361 }
362
363 FatCluster
364 fat_min_cluster_count (FatType fat_type) {
365         switch (fat_type) {
366                 case FAT_TYPE_FAT12:
367                 case FAT_TYPE_FAT16:
368                         return fat_max_cluster_count (fat_type) / 2;
369
370                 case FAT_TYPE_FAT32: return 0xfff0;
371         }
372         return 0;
373 }
374
375 static int
376 calc_sizes (PedGeometry* geom, PedSector align, int cluster_size,
377             PedSector root_dir_sectors, FatCluster* out_cluster_count,
378             PedSector* out_fat_size, FatType fat_type)
379 {
380         PedSector       data_fat_size;
381         PedSector       fat_sectors;
382         PedSector       cluster_sectors;
383         FatCluster      cluster_count;
384         int             i;
385
386         data_fat_size = geom->length - fat_min_reserved_sector_count (fat_type)
387                         - align;
388         if (fat_type == FAT_TYPE_FAT16)
389                 data_fat_size -= root_dir_sectors;
390
391         fat_sectors = 0;
392         for (i = 0; i < 2; i++) {
393                 if (fat_type == FAT_TYPE_FAT32)
394                         cluster_sectors = data_fat_size - fat_sectors;
395                 else
396                         cluster_sectors = data_fat_size - 2 * fat_sectors;
397
398                 cluster_count = cluster_sectors / (cluster_size / 512);
399                 fat_sectors = div_round_up (cluster_count + 2,
400                                             entries_per_sector (fat_type));
401         }
402
403         cluster_sectors = data_fat_size - 2 * fat_sectors;
404         cluster_count = cluster_sectors / (cluster_size / 512);
405
406         if (cluster_count > fat_max_cluster_count (fat_type)
407             || cluster_count < fat_min_cluster_count (fat_type))
408                 return 0;
409
410         *out_cluster_count = cluster_count;
411         *out_fat_size = fat_sectors;
412
413         return 1;
414 }
415
416 FIXME: this is the "trial and error" algorithm.  What happened to my simple,
417 test one?
418
419 If the implications of the above code aren't that clear, here are some of
420 them:
421   * for FAT16, the minimum number of clusters is 32760.
422   * the cluster size is completely determined by the size of the file system,
423 for FAT16.  That means, if a file system is to be resized, it is quite
424 possible that the cluster size must be changed just to be compatible
425 with Microsoft's implementation  (Linux, for example, doesn't calculate the
426 numbers independently, so it would work fine.  So always test your code on
427 Microsoft as well as Linux)
428
429
430 3.4     DOS/Windows bootstrap process
431 -------------------------------------------------------------------------------
432 All of the information that follows is from me reverse-engineering different
433 versions of Microsoft's boot sectors.  It's pretty weird code (as you can
434 imagine...), so there might be mistakes here.
435         There are many different versions of the boot sector:
436 * Windows 98/2000/ME FAT12/FAT16 (supports CHS and LBA)
437 * Windows 98/2000/ME FAT32 (supports CHS and LBA)
438
439 (1) The MBR, LILO, or whatever loads in the first sector of the FAT
440 partition into 0000:7c00, and executes it.
441
442 (2) The first sector of the FAT partition (the "boot sector") does:
443     (a) loads the Master Boot Record (sector 0 of the disk) using the
444     BIOS's CHS calls, and finds the boot partition.  If the boot partition
445     has the LBA flag marked, it writes 0xe to [bp+2] (0000:7c02).  The "read
446     sectors" function in the boot loader checks for 0xe here, and uses LBA if
447     it finds it, and CHS otherwise.
448
449     (b) If it is the FAT32 version of the boot sector, it loads sectors 1
450     through 3 of the partition (it finds this from the "hidden" field in the
451     FAT boot sector, that was loaded by the MBR/LILO) at address 0000:7e00, and
452     continues executing at 0000:8000.  Note: the FAT16 version doesn't require
453     any more sectors to be read (they crammed it all in!), and it goes
454     directly to step 3.
455
456 (3) The code loads IO.SYS (starting at address 0000:0700), off the same
457 partition, and executes it, beginning execution at 0070:0200.  (Note:
458 According to the x86 real mode segmentation scheme, 0070:0200 refers to the
459 same physical memory as 0000:0900)
460
461
462 -------------------------------------------------------------------------------
463 4       THE INFO SECTOR
464 -------------------------------------------------------------------------------
465
466 The info sector is used in FAT32 to store additional information about the
467 file system.
468
469
470 4.1     Data Layout
471 -------------------------------------------------------------------------------
472
473 struct __attribute__ ((packed)) _FatInfoSector {
474         __u32   signature_1;    /* should be 0x41615252 */
475         __u8    unused [480];
476         __u32   signature_2;    /* should be 0x61417272 */
477         __u32   free_clusters;
478         __u32   next_cluster;   /* most recently allocated cluster */
479         __u8    unused2 [0xe];
480         __u16   signature_3;    /* should be 0xaa55 */
481 };
482
483
484 4.2     Descriptions of Fields
485 -------------------------------------------------------------------------------
486
487         __u32   signature_1;    /* should be 0x41615252 */
488 Always 0x41615252  ("AaRR")
489
490
491         __u32   signature_2;    /* should be 0x61417272 */
492 Always 0x61417272  ("aArr")
493
494
495         __u32   free_clusters;
496 The number of free clusters.  This could be calculated by going through the
497 FATs, but this is stored on shutdown to speed things up.
498
499
500         __u32   next_cluster;   /* most recently allocated cluster */
501 This contains the number of the last cluster allocated.  This speeds up
502 cluster allocation, because free clusters usually come in chunks, so you
503 can scan right for free clusters in the FAT.
504
505
506         __u16   signature_3;    /* should be 0xaa55 */
507 Always 0xaa55.
508
509
510 -------------------------------------------------------------------------------
511 5       FILE ALLOCATION TABLES
512 -------------------------------------------------------------------------------
513
514 File allocation table (FAT) is a strange name, come to think of it.  Perhaps it
515 should be called cluster allocation table, or something (?).  Essentially,
516 it is used to represent file chains (i.e. linked lists) for files and
517 directories.  There are usually two FATs (one is a backup, and should be
518 identical).
519
520 Anyway, a FAT is essentially an array.  In FAT12, each entry is 12 bits,
521 FAT16 - 16 bits, FAT32 - 32 bits.  Hence the names.
522
523 The first byte of each FAT must match the "media" field in the boot sector.
524 The rest of the first 2 entries are filled with 0xff.
525
526 All remaining entries - from 2 onwards - correspond to a cluster.  i.e.
527 the second entry corresponds to cluster 2.  Clusters are numbered from 2 onwards
528 (i.e. there is no cluster 1).
529
530 The number in each entry gives the number of the cluster that occurs next in
531 the file chain (linked list).  However, there are a few magic numbers:
532
533   * unused (0).  Indicates the cluster is unused.
534   * end of file (0xff0 for FAT12, 0xfff0 for FAT16, 0x0ffffff0 for FAT32).
535 Indicates this is the last cluster in the file or directory.  Obviouslly for
536 FAT32, the number of clusters must be < 0x0ffffff0.  So it should be called
537 FAT28, perhaps...
538   * bad cluster (0xff7 for FAT12, 0xfff7 for FAT16, 0x0ffffff7 for FAT32).
539 Indicates the disk is physically damaged where the cluster is stored.
540
541
542 -------------------------------------------------------------------------------
543 6       DIRECTORY TREE
544 -------------------------------------------------------------------------------
545
546 The directory tree is simple: there are files and directories.  Files and
547 directories are stored in clusters (except the root directory on FAT12 and
548 FAT16).  Directories are essentially special files that contain directory
549 entries.
550
551 In FAT12 and FAT16, the root directory is stored immediately after the FATs.
552 In FAT32, the first cluster of the root directory is given in the FAT.  (To
553 get the second cluster - if there is one - you just look up the FAT)
554
555
556 6.1     Directory Entries
557 ------------------------------------------------------------------------------
558 Directories are made up of directory entries, each of which represent a file,
559 a directory or part of a file name (the VFAT extension - YUCK!!!).
560
561 Each directory (except the root directory) contains a '.' (this directory) and
562 '..' (parent directory) entry.
563
564 6.1.1   Fields
565 --------------
566
567 From libparted/fs_fat/fat.h:
568
569 struct __attribute__ ((packed)) _FatDirEntry {
570         __u8            name[8];
571         __u8            extension[3];
572         __u8            attributes;
573         __u8            is_upper_case_name;
574         __u8            creation_time_low;      /* milliseconds */
575         __u16           creation_time_high;
576         __u16           creation_date;
577         __u16           access_date;
578         __u16           first_cluster_high;     /* for FAT32 */
579         __u16           time;
580         __u16           date;
581         __u16           first_cluster;
582         __u32           length;
583 };
584
585 6.1.2   Field Descriptions
586 --------------------------
587
588         __u8            name[8];
589 The first part of the file name.  Eg, for a file called README.TXT, this is
590 README.  Files with names longer than 8 characters use the VFAT extension (not
591 described here).  When a file is deleted, the first character is set to 0xe5.
592 If the first character is 0x0, then the entire directory entry is unused.
593
594
595         __u8            extension[3];
596 The last part of the file name.  Eg, for a file called README.TXT, this is TXT.
597 This explains all those .HTM files around the place...
598
599
600         __u8            attributes;
601 If this is 0x0f, then this directory entry is a VFAT entry, and stores part
602 of a file name.  Otherwise, it's treated as various bit fields:
603
604                 0x1             read-only
605                 0x2             hidden
606                 0x4             system
607                 0x8             volume label
608                 0x10            directory
609                 0x20            archived
610
611
612         __u8            is_upper_case_name;
613 A Microsoft cludge: create a file with 8.3 name BUT containing small letters
614 (like ReadMe.Txt) which is treated as an LFN (long file name) and occupies
615 three directory entries.  Now when you rename this file to all uppercase
616 README.TXT,- under Windows NT 4 the then superfluous LFN-VFAT entries are
617 removed, resulting in a smaller directory, but under Windows 9x the LFN-VFAT
618 entries are just upd- and this flag is set.  Executing DEFRAG on such entries
619 MIGHT then remove the superfluous LFN-VFAT entries and shrink the directory.
620
621
622         __u8            creation_time_low;      /* milliseconds */
623         __u16           creation_time_high;
624         __u16           creation_date;
625 Creation time and date.  Not used wih MS-DOS <7.0!
626
627
628         __u16           access_date;
629 Last access date.  Not used wih MS-DOS <7.0!
630
631
632         __u16           first_cluster_high;     /* for FAT32 */
633 High 32 bits of the first cluster in the file or directory (FAT32 only)
634
635
636         __u16           time;
637         __u16           date;
638 ?
639
640
641         __u16           first_cluster;
642 Low 16 bits of first cluster.
643
644
645         __u32           length;
646 Length of file in bytes.
647
648
649
650 ===============================================================================
651                 PART II - GNU PARTED'S FAT IMPLEMENTATION
652 ===============================================================================
653
654 -------------------------------------------------------------------------------
655 7       RESIZING "ISSUES"
656 -------------------------------------------------------------------------------
657
658 To resize a FAT file system, a program must:
659   * copy all used clusters that lie outside of the new partition onto free
660 space on that partition.  The directory tree and FATs must be updated to
661 reflect this.
662   * grow or shrink the file allocation table(s) to the size corresponding
663 to the size of the partition.
664   * convert between FAT16 and FAT32 if necessary.  This involves:
665     - changing the form of the root directory (FAT16 has it's before the
666       clusters whereas FAT32 stores it in normal clusters).
667     - creating space for the backup boot sector and info sector.
668     - updating the directory tree to use 32 bit first cluster entries.
669   * align the start of the clusters (using the "reserved" field in the
670 boot sector), so that the clusters that are common to the old and new
671 partition can be preserved.
672   * re-number clusters.  e.g. if you chop out some clusters from the beginning
673 (i.e. move the start forward), then the first cluster (i.e. number 2) will
674 be refer to a different cluster on the disk.  The directory tree and FATs must
675 be updated to reflect this.
676   * create a new boot sector (and the info sector and backup boot sector for
677 FAT32)
678
679
680 -------------------------------------------------------------------------------
681 8       OVERVIEW OF GNU PARTED'S STRATEGY
682 -------------------------------------------------------------------------------
683
684 GNU Parted copies all clusters that are not accessible from the new file system
685 (either because it lies outside the file system, or file system meta-data must
686 reside there instead) to an accessible place.
687
688 Since all clusters must be renumbered (in most cases), the entire directory
689 tree must be updated.  However converting the directory tree from one numbering
690 system to another would break the file system if it was interrupted halfway
691 through.  Instead, GNU Parted duplicates the directory tree (except the root
692 directory for FAT16) along with clusters that need to be copied because they
693 lie outside the new file system.  The directory tree is duplicated at the same
694 time as inaccessible clusters are.  The relevant function,
695 needs_duplicating() in libparted/fs_fat/clstdup.c is:
696
697         static int
698         needs_duplicating (FatOpContext* ctx, FatCluster cluster)
699         {
700                 FatSpecific*    fs_info = FAT_SPECIFIC (ctx->old_fs);
701
702                 return (fs_info->fat_flag_map [cluster] == FAT_FLAG_FILE
703                         && !fat_op_context_map_static_cluster (ctx, cluster))
704                         || fs_info->fat_flag_map [cluster]
705                                 == FAT_FLAG_DIRECTORY;
706         }
707
708
709
710 A good overview of this implementation is in the fat_resize() function, in
711 libparted/fs_fat/resize.c (slightly edited):
712
713         int
714         fat_resize (PedFileSystem* fs, PedGeometry* geom)
715         {
716                 FatSpecific*    fs_info = FAT_SPECIFIC (fs);
717                 FatSpecific*    new_fs_info;
718                 FatOpContext*   ctx;
719                 PedFileSystem*  new_fs;
720
721                 ctx = create_resize_context (fs, geom);
722                 if (!ctx)
723                         return 0;
724                 new_fs = ctx->new_fs;
725                 new_fs_info = FAT_SPECIFIC (new_fs);
726
727                 if (!fat_duplicate_clusters (ctx))
728                         return 0;
729                 if (fs_info->fat_type == FAT_TYPE_FAT16
730                                 && new_fs_info->fat_type == FAT_TYPE_FAT32) {
731                         if (!alloc_root_dir (ctx))
732                                 return 0;
733                 }
734                 if (!fat_construct_new_fat (ctx))
735                         return 0;
736                 if (!fat_construct_dir_tree (ctx))
737                         return 0;
738                 if (!fat_table_write_all (new_fs_info->fat, new_fs))
739                         return 0;
740
741                 if (!fat_boot_sector_generate (&new_fs_info->boot_sector,
742                                                new_fs))
743                         return 0;
744                 if (!fat_boot_sector_write (&new_fs_info->boot_sector, new_fs))
745                         return 0;
746                 if (new_fs_info->fat_type == FAT_TYPE_FAT32) {
747                         if (!fat_info_sector_generate (
748                                         &new_fs_info->info_sector, new_fs))
749                                 return 0;
750                         if (!fat_info_sector_write (&new_fs_info->info_sector,
751                                                     new_fs))
752                                 return 0;
753                 }
754
755                 if (!resize_context_assimilate (ctx))
756                         return 0;
757
758                 return 1;
759         }