]>
Commit | Line | Data |
---|---|---|
faeb1b64 KZ |
1 | /* |
2 | * No copyright is claimed. This code is in the public domain; do with | |
3 | * it what you wish. | |
4 | */ | |
7e5f8f38 KZ |
5 | #ifndef UTIL_LINUX_PT_MBR_H |
6 | #define UTIL_LINUX_PT_MBR_H | |
7 | ||
fdf8e1f5 SK |
8 | #include <assert.h> |
9 | ||
f917738e KZ |
10 | struct dos_partition { |
11 | unsigned char boot_ind; /* 0x80 - active */ | |
12 | unsigned char bh, bs, bc; /* begin CHS */ | |
71ade4da | 13 | unsigned char sys_ind; |
f917738e KZ |
14 | unsigned char eh, es, ec; /* end CHS */ |
15 | unsigned char start_sect[4]; | |
16 | unsigned char nr_sects[4]; | |
17 | } __attribute__((packed)); | |
18 | ||
19 | #define MBR_PT_OFFSET 0x1be | |
3457d90e | 20 | #define MBR_PT_BOOTBITS_SIZE 440 |
f917738e | 21 | |
7eba8f98 | 22 | static inline struct dos_partition *mbr_get_partition(const unsigned char *mbr, int i) |
e8bf93de KZ |
23 | { |
24 | return (struct dos_partition *) | |
25 | (mbr + MBR_PT_OFFSET + (i * sizeof(struct dos_partition))); | |
26 | } | |
27 | ||
f917738e | 28 | /* assemble badly aligned little endian integer */ |
33b2e302 | 29 | static inline uint32_t __dos_assemble_4le(const unsigned char *p) |
f917738e | 30 | { |
33b2e302 SK |
31 | uint32_t last_byte = p[3]; |
32 | ||
33 | return p[0] | (p[1] << 8) | (p[2] << 16) | (last_byte << 24); | |
f917738e KZ |
34 | } |
35 | ||
b1981115 KZ |
36 | static inline void __dos_store_4le(unsigned char *p, unsigned int val) |
37 | { | |
fdf8e1f5 | 38 | assert(!(p == NULL)); |
b1981115 KZ |
39 | p[0] = (val & 0xff); |
40 | p[1] = ((val >> 8) & 0xff); | |
41 | p[2] = ((val >> 16) & 0xff); | |
42 | p[3] = ((val >> 24) & 0xff); | |
43 | } | |
44 | ||
7eba8f98 | 45 | static inline unsigned int dos_partition_get_start(const struct dos_partition *p) |
f917738e | 46 | { |
b44244cb | 47 | return __dos_assemble_4le(&(p->start_sect[0])); |
f917738e KZ |
48 | } |
49 | ||
b44244cb | 50 | static inline void dos_partition_set_start(struct dos_partition *p, unsigned int n) |
f917738e | 51 | { |
b44244cb | 52 | __dos_store_4le(p->start_sect, n); |
f917738e KZ |
53 | } |
54 | ||
7eba8f98 | 55 | static inline unsigned int dos_partition_get_size(const struct dos_partition *p) |
b44244cb KZ |
56 | { |
57 | return __dos_assemble_4le(&(p->nr_sects[0])); | |
58 | } | |
59 | ||
60 | static inline void dos_partition_set_size(struct dos_partition *p, unsigned int n) | |
61 | { | |
62 | __dos_store_4le(p->nr_sects, n); | |
63 | } | |
64 | ||
e63d9ab1 PR |
65 | static inline void dos_partition_sync_chs(struct dos_partition *p, unsigned long long int part_offset, unsigned int geom_sectors, unsigned int geom_heads) |
66 | { | |
67 | unsigned long long int start = part_offset + dos_partition_get_start(p); | |
68 | unsigned long long int stop = start + dos_partition_get_size(p) - 1; | |
69 | unsigned int spc = geom_heads * geom_sectors; | |
70 | ||
71 | if (start / spc > 1023) | |
72 | start = spc * 1024 - 1; | |
73 | if (stop / spc > 1023) | |
74 | stop = spc * 1024 - 1; | |
75 | ||
76 | p->bc = (start / spc) & 0xff; | |
77 | p->bh = (start / geom_sectors) % geom_heads; | |
78 | p->bs = ((start % geom_sectors + 1) & 0x3f) | | |
79 | (((start / spc) >> 2) & 0xc0); | |
80 | ||
81 | p->ec = (stop / spc) & 0xff; | |
82 | p->eh = (stop / geom_sectors) % geom_heads; | |
83 | p->es = ((stop % geom_sectors + 1) & 0x3f) | | |
84 | (((stop / spc) >> 2) & 0xc0); | |
85 | } | |
86 | ||
b44244cb | 87 | static inline int mbr_is_valid_magic(const unsigned char *mbr) |
f917738e KZ |
88 | { |
89 | return mbr[510] == 0x55 && mbr[511] == 0xaa ? 1 : 0; | |
90 | } | |
91 | ||
b44244cb KZ |
92 | static inline void mbr_set_magic(unsigned char *b) |
93 | { | |
94 | b[510] = 0x55; | |
95 | b[511] = 0xaa; | |
96 | } | |
97 | ||
98 | static inline unsigned int mbr_get_id(const unsigned char *mbr) | |
99 | { | |
100 | return __dos_assemble_4le(&mbr[440]); | |
101 | } | |
102 | ||
103 | static inline void mbr_set_id(unsigned char *b, unsigned int id) | |
f917738e | 104 | { |
b44244cb | 105 | __dos_store_4le(&b[440], id); |
f917738e KZ |
106 | } |
107 | ||
7e5f8f38 KZ |
108 | enum { |
109 | MBR_EMPTY_PARTITION = 0x00, | |
110 | MBR_FAT12_PARTITION = 0x01, | |
111 | MBR_XENIX_ROOT_PARTITION = 0x02, | |
112 | MBR_XENIX_USR_PARTITION = 0x03, | |
113 | MBR_FAT16_LESS32M_PARTITION = 0x04, | |
114 | MBR_DOS_EXTENDED_PARTITION = 0x05, | |
115 | MBR_FAT16_PARTITION = 0x06, /* DOS 16-bit >=32M */ | |
116 | MBR_HPFS_NTFS_PARTITION = 0x07, /* OS/2 IFS, eg, HPFS or NTFS or QNX */ | |
117 | MBR_AIX_PARTITION = 0x08, /* AIX boot (AIX -- PS/2 port) or SplitDrive */ | |
118 | MBR_AIX_BOOTABLE_PARTITION = 0x09, /* AIX data or Coherent */ | |
119 | MBR_OS2_BOOTMNGR_PARTITION = 0x0a, /* OS/2 Boot Manager */ | |
120 | MBR_W95_FAT32_PARTITION = 0x0b, | |
121 | MBR_W95_FAT32_LBA_PARTITION = 0x0c, /* LBA really is `Extended Int 13h' */ | |
122 | MBR_W95_FAT16_LBA_PARTITION = 0x0e, | |
123 | MBR_W95_EXTENDED_PARTITION = 0x0f, | |
124 | MBR_OPUS_PARTITION = 0x10, | |
125 | MBR_HIDDEN_FAT12_PARTITION = 0x11, | |
126 | MBR_COMPAQ_DIAGNOSTICS_PARTITION = 0x12, | |
127 | MBR_HIDDEN_FAT16_L32M_PARTITION = 0x14, | |
128 | MBR_HIDDEN_FAT16_PARTITION = 0x16, | |
129 | MBR_HIDDEN_HPFS_NTFS_PARTITION = 0x17, | |
130 | MBR_AST_SMARTSLEEP_PARTITION = 0x18, | |
131 | MBR_HIDDEN_W95_FAT32_PARTITION = 0x1b, | |
132 | MBR_HIDDEN_W95_FAT32LBA_PARTITION = 0x1c, | |
133 | MBR_HIDDEN_W95_FAT16LBA_PARTITION = 0x1e, | |
134 | MBR_NEC_DOS_PARTITION = 0x24, | |
135 | MBR_PLAN9_PARTITION = 0x39, | |
136 | MBR_PARTITIONMAGIC_PARTITION = 0x3c, | |
137 | MBR_VENIX80286_PARTITION = 0x40, | |
138 | MBR_PPC_PREP_BOOT_PARTITION = 0x41, | |
139 | MBR_SFS_PARTITION = 0x42, | |
140 | MBR_QNX_4X_PARTITION = 0x4d, | |
141 | MBR_QNX_4X_2ND_PARTITION = 0x4e, | |
142 | MBR_QNX_4X_3RD_PARTITION = 0x4f, | |
143 | MBR_DM_PARTITION = 0x50, | |
144 | MBR_DM6_AUX1_PARTITION = 0x51, /* (or Novell) */ | |
145 | MBR_CPM_PARTITION = 0x52, /* CP/M or Microport SysV/AT */ | |
146 | MBR_DM6_AUX3_PARTITION = 0x53, | |
147 | MBR_DM6_PARTITION = 0x54, | |
148 | MBR_EZ_DRIVE_PARTITION = 0x55, | |
149 | MBR_GOLDEN_BOW_PARTITION = 0x56, | |
150 | MBR_PRIAM_EDISK_PARTITION = 0x5c, | |
151 | MBR_SPEEDSTOR_PARTITION = 0x61, | |
152 | MBR_GNU_HURD_PARTITION = 0x63, /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */ | |
153 | MBR_UNIXWARE_PARTITION = MBR_GNU_HURD_PARTITION, | |
154 | MBR_NETWARE_286_PARTITION = 0x64, | |
155 | MBR_NETWARE_386_PARTITION = 0x65, | |
156 | MBR_DISKSECURE_MULTIBOOT_PARTITION = 0x70, | |
157 | MBR_PC_IX_PARTITION = 0x75, | |
158 | MBR_OLD_MINIX_PARTITION = 0x80, /* Minix 1.4a and earlier */ | |
159 | MBR_MINIX_PARTITION = 0x81, /* Minix 1.4b and later */ | |
160 | MBR_LINUX_SWAP_PARTITION = 0x82, | |
161 | MBR_SOLARIS_X86_PARTITION = MBR_LINUX_SWAP_PARTITION, | |
162 | MBR_LINUX_DATA_PARTITION = 0x83, | |
3e9a2bbb JJ |
163 | MBR_OS2_HIDDEN_DRIVE_PARTITION = 0x84, /* also hibernation MS APM, Intel Rapid Start */ |
164 | MBR_INTEL_HIBERNATION_PARTITION = MBR_OS2_HIDDEN_DRIVE_PARTITION, | |
7e5f8f38 KZ |
165 | MBR_LINUX_EXTENDED_PARTITION = 0x85, |
166 | MBR_NTFS_VOL_SET1_PARTITION = 0x86, | |
167 | MBR_NTFS_VOL_SET2_PARTITION = 0x87, | |
168 | MBR_LINUX_PLAINTEXT_PARTITION = 0x88, | |
169 | MBR_LINUX_LVM_PARTITION = 0x8e, | |
170 | MBR_AMOEBA_PARTITION = 0x93, | |
171 | MBR_AMOEBA_BBT_PARTITION = 0x94, /* (bad block table) */ | |
172 | MBR_BSD_OS_PARTITION = 0x9f, /* BSDI */ | |
173 | MBR_THINKPAD_HIBERNATION_PARTITION = 0xa0, | |
174 | MBR_FREEBSD_PARTITION = 0xa5, /* various BSD flavours */ | |
175 | MBR_OPENBSD_PARTITION = 0xa6, | |
176 | MBR_NEXTSTEP_PARTITION = 0xa7, | |
177 | MBR_DARWIN_UFS_PARTITION = 0xa8, | |
178 | MBR_NETBSD_PARTITION = 0xa9, | |
179 | MBR_DARWIN_BOOT_PARTITION = 0xab, | |
180 | MBR_HFS_HFS_PARTITION = 0xaf, | |
181 | MBR_BSDI_FS_PARTITION = 0xb7, | |
182 | MBR_BSDI_SWAP_PARTITION = 0xb8, | |
183 | MBR_BOOTWIZARD_HIDDEN_PARTITION = 0xbb, | |
25156ba1 | 184 | MBR_ACRONIS_FAT32LBA_PARTITION = 0xbc, /* Acronis Secure Zone with ipl for loader F11.SYS */ |
7e5f8f38 KZ |
185 | MBR_SOLARIS_BOOT_PARTITION = 0xbe, |
186 | MBR_SOLARIS_PARTITION = 0xbf, | |
187 | MBR_DRDOS_FAT12_PARTITION = 0xc1, | |
188 | MBR_DRDOS_FAT16_L32M_PARTITION = 0xc4, | |
189 | MBR_DRDOS_FAT16_PARTITION = 0xc6, | |
190 | MBR_SYRINX_PARTITION = 0xc7, | |
191 | MBR_NONFS_DATA_PARTITION = 0xda, | |
192 | MBR_CPM_CTOS_PARTITION = 0xdb, /* CP/M or Concurrent CP/M or Concurrent DOS or CTOS */ | |
193 | MBR_DELL_UTILITY_PARTITION = 0xde, /* Dell PowerEdge Server utilities */ | |
194 | MBR_BOOTIT_PARTITION = 0xdf, /* BootIt EMBRM */ | |
195 | MBR_DOS_ACCESS_PARTITION = 0xe1, /* DOS access or SpeedStor 12-bit FAT extended partition */ | |
196 | MBR_DOS_RO_PARTITION = 0xe3, /* DOS R/O or SpeedStor */ | |
197 | MBR_SPEEDSTOR_EXTENDED_PARTITION = 0xe4, /* SpeedStor 16-bit FAT extended partition < 1024 cyl. */ | |
b12b07e3 | 198 | MBR_RUFUS_EXTRA_PARTITION = 0xea, /* Rufus extra partition for alignment */ |
7e5f8f38 KZ |
199 | MBR_BEOS_FS_PARTITION = 0xeb, |
200 | MBR_GPT_PARTITION = 0xee, /* Intel EFI GUID Partition Table */ | |
201 | MBR_EFI_SYSTEM_PARTITION = 0xef, /* Intel EFI System Partition */ | |
202 | MBR_LINUX_PARISC_BOOT_PARTITION = 0xf0, /* Linux/PA-RISC boot loader */ | |
203 | MBR_SPEEDSTOR1_PARTITION = 0xf1, | |
204 | MBR_SPEEDSTOR2_PARTITION = 0xf4, /* SpeedStor large partition */ | |
205 | MBR_DOS_SECONDARY_PARTITION = 0xf2, /* DOS 3.3+ secondary */ | |
93965a9a | 206 | MBR_EBBR_PROTECTIVE_PARTITION = 0xf8, /* Arm EBBR firmware protective partition */ |
7e5f8f38 KZ |
207 | MBR_VMWARE_VMFS_PARTITION = 0xfb, |
208 | MBR_VMWARE_VMKCORE_PARTITION = 0xfc, /* VMware kernel dump partition */ | |
9f3d0fce | 209 | MBR_LINUX_RAID_PARTITION = 0xfd, /* Linux raid partition with autodetect using persistent superblock */ |
7e5f8f38 KZ |
210 | MBR_LANSTEP_PARTITION = 0xfe, /* SpeedStor >1024 cyl. or LANstep */ |
211 | MBR_XENIX_BBT_PARTITION = 0xff, /* Xenix Bad Block Table */ | |
212 | }; | |
213 | ||
214 | #endif /* UTIL_LINUX_PT_MBR_H */ |