import source from 3.0
[external/parted.git] / libparted / fs / fat / bootsector.h
1 /*
2     libparted
3     Copyright (C) 1998-2000, 2007, 2009-2011 Free Software Foundation, Inc.
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 3 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef PED_FAT_BOOTSECTOR_H
20 #define PED_FAT_BOOTSECTOR_H
21
22 typedef struct _FatBootSector   FatBootSector;
23 typedef struct _FatInfoSector   FatInfoSector;
24
25 #include "fat.h"
26
27 #define FAT32_INFO_MAGIC1       0x41615252
28 #define FAT32_INFO_MAGIC2       0x61417272
29 #define FAT32_INFO_MAGIC3       0xaa55
30
31 /* stolen from mkdosfs, by Dave Hudson */
32
33 #define FAT_BOOT_MESSAGE        \
34 "This partition does not have an operating system loader installed on it.\n\r"\
35 "Press a key to reboot..."
36
37 #define FAT_BOOT_JUMP   "\xeb\x58\x90"          /* jmp  +5a */
38
39 #define FAT_BOOT_CODE   "\x0e"                  /* push cs */           \
40                         "\x1f"                  /* pop ds */            \
41                         "\xbe\x74\x7e"          /* mov si, offset message */ \
42                                         /* write_msg_loop: */           \
43                         "\xac"                  /* lodsb */             \
44                         "\x22\xc0"              /* and al, al */        \
45                         "\x74\x06"              /* jz done (+8) */      \
46                         "\xb4\x0e"              /* mov ah, 0x0e */      \
47                         "\xcd\x10"              /* int 0x10 */          \
48                         "\xeb\xf5"              /* jmp write_msg_loop */ \
49                                         /* done: */                     \
50                         "\xb4\x00"              /* mov ah, 0x00 */      \
51                         "\xcd\x16"              /* int 0x16 */          \
52                         "\xb4\x00"              /* mov ah, 0x00 */      \
53                         "\xcd\x19"              /* int 0x19 */          \
54                         "\xeb\xfe"              /* jmp +0 - in case int 0x19 */ \
55                                                 /* doesn't work */      \
56                                         /* message: */                  \
57                         FAT_BOOT_MESSAGE
58
59 #define FAT_BOOT_CODE_LENGTH 128
60
61 struct __attribute__ ((packed)) _FatBootSector {
62         uint8_t         boot_jump[3];   /* 00: Boot strap short or near jump */
63         uint8_t         system_id[8];   /* 03: system name */
64         uint16_t        sector_size;    /* 0b: bytes per logical sector */
65         uint8_t         cluster_size;   /* 0d: sectors/cluster */
66         uint16_t        reserved;       /* 0e: reserved sectors */
67         uint8_t         fats;           /* 10: number of FATs */
68         uint16_t        dir_entries;    /* 11: number of root directory entries */
69         uint16_t        sectors;        /* 13: if 0, total_sect supersedes */
70         uint8_t         media;          /* 15: media code */
71         uint16_t        fat_length;     /* 16: sectors/FAT for FAT12/16 */
72         uint16_t        secs_track;     /* 18: sectors per track */
73         uint16_t        heads;          /* 1a: number of heads */
74         uint32_t        hidden;         /* 1c: hidden sectors (partition start) */
75         uint32_t        sector_count;   /* 20: no. of sectors (if sectors == 0) */
76
77         union __attribute__ ((packed)) {
78                 /* FAT16 fields */
79                 struct __attribute__ ((packed)) {
80                         uint8_t         drive_num;      /* 24: */
81                         uint8_t         empty_1;        /* 25: */
82                         uint8_t         ext_signature;  /* 26: always 0x29 */
83                         uint32_t        serial_number;  /* 27: */
84                         uint8_t         volume_name [11];       /* 2b: */
85                         uint8_t         fat_name [8];   /* 36: */
86                         uint8_t         boot_code[448]; /* 3f: Boot code (or message) */
87                 } fat16;
88                 /* FAT32 fields */
89                 struct __attribute__ ((packed)) {
90                         uint32_t        fat_length;     /* 24: size of FAT in sectors */
91                         uint16_t        flags;          /* 28: bit8: fat mirroring, low4: active fat */
92                         uint16_t        version;        /* 2a: minor * 256 + major */
93                         uint32_t        root_dir_cluster;       /* 2c: */
94                         uint16_t        info_sector;    /* 30: */
95                         uint16_t        backup_sector;  /* 32: */
96                         uint8_t         empty_1 [12];   /* 34: */
97                         uint16_t        drive_num;      /* 40: */
98                         uint8_t         ext_signature;  /* 42: always 0x29 */
99                         uint32_t        serial_number;  /* 43: */
100                         uint8_t         volume_name [11];       /* 47: */
101                         uint8_t         fat_name [8];   /* 52: */
102                         uint8_t         boot_code[420]; /* 5a: Boot code (or message) */
103                 } fat32;
104         } u;
105
106         uint16_t        boot_sign;      /* 1fe: always 0xAA55 */
107 };
108
109 struct __attribute__ ((packed)) _FatInfoSector {
110         uint32_t        signature_1;    /* should be 0x41615252 */
111         uint8_t         unused [480];
112         uint32_t        signature_2;    /* should be 0x61417272 */
113         uint32_t        free_clusters;
114         uint32_t        next_cluster;   /* most recently allocated cluster */
115         uint8_t         unused2 [0xe];
116         uint16_t        signature_3;    /* should be 0xaa55 */
117 };
118
119 int fat_boot_sector_read (FatBootSector* bs, const PedGeometry* geom);
120 FatType fat_boot_sector_probe_type (const FatBootSector* bs,
121                                     const PedGeometry* geom);
122 int fat_boot_sector_analyse (FatBootSector* bs, PedFileSystem* fs);
123 int fat_boot_sector_set_boot_code (FatBootSector* bs);
124 int fat_boot_sector_generate (FatBootSector* bs, const PedFileSystem* fs);
125 int fat_boot_sector_write (const FatBootSector* bs, PedFileSystem* fs);
126
127 int fat_info_sector_read (FatInfoSector* is, const PedFileSystem* fs);
128 int fat_info_sector_write (const FatInfoSector* is, PedFileSystem* fs);
129
130 #endif /* PED_FAT_BOOTSECTOR_H */