]> code.delx.au - refind/blob - gptsync/lib.c
9a3ad459f797cd21a6422d128f07c16cabb3491d
[refind] / gptsync / lib.c
1 /*
2 * gptsync/lib.c
3 * Platform-independent code common to gptsync and showpart
4 *
5 * Copyright (c) 2006-2007 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * Neither the name of Christoph Pfisterer nor the names of the
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include "gptsync.h"
38
39 // variables
40
41 UINT8 empty_guid[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
42
43 PARTITION_INFO mbr_parts[4];
44 UINTN mbr_part_count = 0;
45 PARTITION_INFO gpt_parts[128];
46 UINTN gpt_part_count = 0;
47
48 PARTITION_INFO new_mbr_parts[4];
49 UINTN new_mbr_part_count = 0;
50
51 UINT8 sector[512];
52
53 MBR_PARTTYPE mbr_types[] = {
54 { 0x01, STR("FAT12 (CHS)") },
55 { 0x04, STR("FAT16 <32M (CHS)") },
56 { 0x05, STR("Extended (CHS)") },
57 { 0x06, STR("FAT16 (CHS)") },
58 { 0x07, STR("NTFS/HPFS") },
59 { 0x0b, STR("FAT32 (CHS)") },
60 { 0x0c, STR("FAT32 (LBA)") },
61 { 0x0e, STR("FAT16 (LBA)") },
62 { 0x0f, STR("Extended (LBA)") },
63 { 0x11, STR("Hidden FAT12 (CHS)") },
64 { 0x14, STR("Hidden FAT16 <32M (CHS)") },
65 { 0x16, STR("Hidden FAT16 (CHS)") },
66 { 0x17, STR("Hidden NTFS/HPFS") },
67 { 0x1b, STR("Hidden FAT32 (CHS)") },
68 { 0x1c, STR("Hidden FAT32 (LBA)") },
69 { 0x1e, STR("Hidden FAT16 (LBA)") },
70 { 0x82, STR("Linux swap / Solaris") },
71 { 0x83, STR("Linux") },
72 { 0x85, STR("Linux Extended") },
73 { 0x86, STR("NT FAT volume set") },
74 { 0x87, STR("NTFS volume set") },
75 { 0x8e, STR("Linux LVM") },
76 { 0xa5, STR("FreeBSD") },
77 { 0xa6, STR("OpenBSD") },
78 { 0xa7, STR("NeXTSTEP") },
79 { 0xa8, STR("Mac OS X UFS") },
80 { 0xa9, STR("NetBSD") },
81 { 0xab, STR("Mac OS X Boot") },
82 { 0xac, STR("Apple RAID") },
83 { 0xaf, STR("Mac OS X HFS+") },
84 { 0xbe, STR("Solaris Boot") },
85 { 0xbf, STR("Solaris") },
86 { 0xeb, STR("BeOS") },
87 { 0xee, STR("EFI Protective") },
88 { 0xef, STR("EFI System (FAT)") },
89 { 0xfd, STR("Linux RAID") },
90 { 0, NULL },
91 };
92
93 GPT_PARTTYPE gpt_types[] = {
94 // Sony uses this one
95 { "\x32\x97\x01\xF4\x6E\x06\x12\x4E\x82\x73\x34\x6C\x56\x41\x49\x4F", 0x00, STR("Sony System (FAT)"), GPT_KIND_FATAL },
96 // Defined by EFI/UEFI specification
97 { "\x28\x73\x2A\xC1\x1F\xF8\xD2\x11\xBA\x4B\x00\xA0\xC9\x3E\xC9\x3B", 0xef, STR("EFI System (FAT)"), GPT_KIND_SYSTEM },
98 { "\x41\xEE\x4D\x02\xE7\x33\xD3\x11\x9D\x69\x00\x08\xC7\x81\xF3\x9F", 0x00, STR("MBR partition scheme"), GPT_KIND_FATAL },
99 // Generally well-known
100 { "\x16\xE3\xC9\xE3\x5C\x0B\xB8\x4D\x81\x7D\xF9\x2D\xF0\x02\x15\xAE", 0x00, STR("MS Reserved"), GPT_KIND_SYSTEM },
101 { "\xA2\xA0\xD0\xEB\xE5\xB9\x33\x44\x87\xC0\x68\xB6\xB7\x26\x99\xC7", 0x00, STR("Basic Data"), GPT_KIND_BASIC_DATA },
102 // From Wikipedia
103 { "\xAA\xC8\x08\x58\x8F\x7E\xE0\x42\x85\xD2\xE1\xE9\x04\x34\xCF\xB3", 0x00, STR("MS LDM Metadata"), GPT_KIND_FATAL },
104 { "\xA0\x60\x9B\xAF\x31\x14\x62\x4F\xBC\x68\x33\x11\x71\x4A\x69\xAD", 0x00, STR("MS LDM Data"), GPT_KIND_FATAL },
105 { "\x1E\x4C\x89\x75\xEB\x3A\xD3\x11\xB7\xC1\x7B\x03\xA0\x00\x00\x00", 0x00, STR("HP/UX Data"), GPT_KIND_DATA },
106 { "\x28\xE7\xA1\xE2\xE3\x32\xD6\x11\xA6\x82\x7B\x03\xA0\x00\x00\x00", 0x00, STR("HP/UX Service"), GPT_KIND_SYSTEM },
107 // From Linux repository, fs/partitions/efi.h
108 { "\x0F\x88\x9D\xA1\xFC\x05\x3B\x4D\xA0\x06\x74\x3F\x0F\x84\x91\x1E", 0xfd, STR("Linux RAID"), GPT_KIND_DATA },
109 { "\x6D\xFD\x57\x06\xAB\xA4\xC4\x43\x84\xE5\x09\x33\xC8\x4B\x4F\x4F", 0x82, STR("Linux Swap"), GPT_KIND_SYSTEM },
110 { "\x79\xD3\xD6\xE6\x07\xF5\xC2\x44\xA2\x3C\x23\x8F\x2A\x3D\xF9\x28", 0x8e, STR("Linux LVM"), GPT_KIND_DATA },
111 { "\xAF\x3D\xC6\x0F\x83\x84\x72\x47\x8E\x79\x3D\x69\xD8\x47\x7D\xE4", 0x83, STR("Linux Filesystem"), GPT_KIND_DATA },
112 // From Wikipedia
113 { "\x39\x33\xA6\x8D\x07\x00\xC0\x60\xC4\x36\x08\x3A\xC8\x23\x09\x08", 0x00, STR("Linux Reserved"), GPT_KIND_SYSTEM },
114 // From grub2 repository, grub/include/grub/gpt_partition.h
115 { "\x48\x61\x68\x21\x49\x64\x6F\x6E\x74\x4E\x65\x65\x64\x45\x46\x49", 0x00, STR("GRUB2 BIOS Boot"), GPT_KIND_SYSTEM },
116 // From FreeBSD repository, sys/sys/gpt.h
117 { "\xB4\x7C\x6E\x51\xCF\x6E\xD6\x11\x8F\xF8\x00\x02\x2D\x09\x71\x2B", 0xa5, STR("FreeBSD Data"), GPT_KIND_DATA },
118 { "\xB5\x7C\x6E\x51\xCF\x6E\xD6\x11\x8F\xF8\x00\x02\x2D\x09\x71\x2B", 0x00, STR("FreeBSD Swap"), GPT_KIND_SYSTEM },
119 { "\xB6\x7C\x6E\x51\xCF\x6E\xD6\x11\x8F\xF8\x00\x02\x2D\x09\x71\x2B", 0xa5, STR("FreeBSD UFS"), GPT_KIND_DATA },
120 { "\xB8\x7C\x6E\x51\xCF\x6E\xD6\x11\x8F\xF8\x00\x02\x2D\x09\x71\x2B", 0x00, STR("FreeBSD Vinum"), GPT_KIND_DATA },
121 { "\xBA\x7C\x6E\x51\xCF\x6E\xD6\x11\x8F\xF8\x00\x02\x2D\x09\x71\x2B", 0xa5, STR("FreeBSD ZFS"), GPT_KIND_DATA },
122 { "\x9D\x6B\xBD\x83\x41\x7F\xDC\x11\xBE\x0B\x00\x15\x60\xB8\x4F\x0F", 0xa5, STR("FreeBSD Boot"), GPT_KIND_DATA },
123 // From NetBSD repository, sys/sys/disklabel_gpt.h
124 { "\x32\x8D\xF4\x49\x0E\xB1\xDC\x11\xB9\x9B\x00\x19\xD1\x87\x96\x48", 0x00, STR("NetBSD Swap"), GPT_KIND_SYSTEM },
125 { "\x5A\x8D\xF4\x49\x0E\xB1\xDC\x11\xB9\x9B\x00\x19\xD1\x87\x96\x48", 0xa9, STR("NetBSD FFS"), GPT_KIND_DATA },
126 { "\x82\x8D\xF4\x49\x0E\xB1\xDC\x11\xB9\x9B\x00\x19\xD1\x87\x96\x48", 0xa9, STR("NetBSD LFS"), GPT_KIND_DATA },
127 { "\xAA\x8D\xF4\x49\x0E\xB1\xDC\x11\xB9\x9B\x00\x19\xD1\x87\x96\x48", 0xa9, STR("NetBSD RAID"), GPT_KIND_DATA },
128 { "\xC4\x19\xB5\x2D\x0E\xB1\xDC\x11\xB9\x9B\x00\x19\xD1\x87\x96\x48", 0xa9, STR("NetBSD CCD"), GPT_KIND_DATA },
129 { "\xEC\x19\xB5\x2D\x0E\xB1\xDC\x11\xB9\x9B\x00\x19\xD1\x87\x96\x48", 0xa9, STR("NetBSD CGD"), GPT_KIND_DATA },
130 // From http://developer.apple.com/mac/library/technotes/tn2006/tn2166.html
131 // { "\x00\x53\x46\x48\x00\x00\xAA\x11\xAA\x11\x00\x30\x65\x43\xEC\xAC", 0x00, STR("Mac OS X HFS+"), GPT_KIND_SYSTEM },
132 { "\x00\x53\x46\x48\x00\x00\xAA\x11\xAA\x11\x00\x30\x65\x43\xEC\xAC", 0xaf, STR("Mac OS X HFS+"), GPT_KIND_DATA },
133 { "\x00\x53\x46\x55\x00\x00\xAA\x11\xAA\x11\x00\x30\x65\x43\xEC\xAC", 0xa8, STR("Mac OS X UFS"), GPT_KIND_DATA },
134 { "\x74\x6F\x6F\x42\x00\x00\xAA\x11\xAA\x11\x00\x30\x65\x43\xEC\xAC", 0xab, STR("Mac OS X Boot"), GPT_KIND_DATA },
135 { "\x44\x49\x41\x52\x00\x00\xAA\x11\xAA\x11\x00\x30\x65\x43\xEC\xAC", 0xac, STR("Apple RAID"), GPT_KIND_DATA },
136 { "\x44\x49\x41\x52\x4F\x5F\xAA\x11\xAA\x11\x00\x30\x65\x43\xEC\xAC", 0xac, STR("Apple RAID (Offline)"), GPT_KIND_DATA },
137 { "\x65\x62\x61\x4C\x00\x6C\xAA\x11\xAA\x11\x00\x30\x65\x43\xEC\xAC", 0x00, STR("Apple Label"), GPT_KIND_SYSTEM },
138 // From Wikipedia
139 { "\x6F\x63\x65\x52\x65\x76\xAA\x11\xAA\x11\x00\x30\x65\x43\xEC\xAC", 0x00, STR("Apple TV Recovery"), GPT_KIND_DATA },
140 // From OpenSolaris repository, usr/src/uts/common/sys/efi_partition.h
141 { "\x7f\x23\x96\x6A\xD2\x1D\xB2\x11\x99\xa6\x08\x00\x20\x73\x66\x31", 0x00, STR("Solaris Reserved"), GPT_KIND_SYSTEM },
142 { "\x45\xCB\x82\x6A\xD2\x1D\xB2\x11\x99\xa6\x08\x00\x20\x73\x66\x31", 0xbf, STR("Solaris Boot"), GPT_KIND_DATA },
143 { "\x4D\xCF\x85\x6A\xD2\x1D\xB2\x11\x99\xa6\x08\x00\x20\x73\x66\x31", 0xbf, STR("Solaris Root"), GPT_KIND_DATA },
144 { "\x6F\xC4\x87\x6A\xD2\x1D\xB2\x11\x99\xa6\x08\x00\x20\x73\x66\x31", 0x00, STR("Solaris Swap"), GPT_KIND_SYSTEM },
145 { "\xC3\x8C\x89\x6A\xD2\x1D\xB2\x11\x99\xa6\x08\x00\x20\x73\x66\x31", 0xbf, STR("Solaris Usr / Apple ZFS"), GPT_KIND_DATA },
146 { "\x2B\x64\x8B\x6A\xD2\x1D\xB2\x11\x99\xa6\x08\x00\x20\x73\x66\x31", 0x00, STR("Solaris Backup"), GPT_KIND_SYSTEM },
147 { "\xC7\x2A\x8D\x6A\xD2\x1D\xB2\x11\x99\xa6\x08\x00\x20\x73\x66\x31", 0x00, STR("Solaris Reserved (Stand)"), GPT_KIND_SYSTEM },
148 { "\xE9\xF2\x8E\x6A\xD2\x1D\xB2\x11\x99\xa6\x08\x00\x20\x73\x66\x31", 0xbf, STR("Solaris Var"), GPT_KIND_DATA },
149 { "\x39\xBA\x90\x6A\xD2\x1D\xB2\x11\x99\xa6\x08\x00\x20\x73\x66\x31", 0xbf, STR("Solaris Home"), GPT_KIND_DATA },
150 { "\xA5\x83\x92\x6A\xD2\x1D\xB2\x11\x99\xa6\x08\x00\x20\x73\x66\x31", 0x00, STR("Solaris Alternate Sector"), GPT_KIND_SYSTEM },
151 { "\x3B\x5A\x94\x6A\xD2\x1D\xB2\x11\x99\xa6\x08\x00\x20\x73\x66\x31", 0x00, STR("Solaris Reserved (Cache)"), GPT_KIND_SYSTEM },
152 { "\xD1\x30\x96\x6A\xD2\x1D\xB2\x11\x99\xa6\x08\x00\x20\x73\x66\x31", 0x00, STR("Solaris Reserved"), GPT_KIND_SYSTEM },
153 { "\x67\x07\x98\x6A\xD2\x1D\xB2\x11\x99\xa6\x08\x00\x20\x73\x66\x31", 0x00, STR("Solaris Reserved"), GPT_KIND_SYSTEM },
154 // List sentinel
155 { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, 0, NULL, 0 },
156 };
157 GPT_PARTTYPE gpt_dummy_type =
158 { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, 0, STR("Unknown"), GPT_KIND_FATAL };
159
160 //
161 // MBR functions
162 //
163
164 CHARN * mbr_parttype_name(UINT8 type)
165 {
166 int i;
167
168 for (i = 0; mbr_types[i].name; i++)
169 if (mbr_types[i].type == type)
170 return mbr_types[i].name;
171 return STR("Unknown");
172 }
173
174 UINTN read_mbr(VOID)
175 {
176 UINTN status;
177 UINTN i;
178 BOOLEAN used;
179 MBR_PART_INFO *table;
180
181 Print(L"\nCurrent MBR partition table:\n");
182
183 // read MBR data
184 status = read_sector(0, sector);
185 if (status != 0)
186 return status;
187
188 // check for validity
189 if (*((UINT16 *)(sector + 510)) != 0xaa55) {
190 Print(L" No MBR partition table present!\n");
191 return 1;
192 }
193 table = (MBR_PART_INFO *)(sector + 446);
194 for (i = 0; i < 4; i++) {
195 if (table[i].flags != 0x00 && table[i].flags != 0x80) {
196 Print(L" MBR partition table is invalid!\n");
197 return 1;
198 }
199 }
200
201 // check if used
202 used = FALSE;
203 for (i = 0; i < 4; i++) {
204 if (table[i].start_lba > 0 && table[i].size > 0) {
205 used = TRUE;
206 break;
207 }
208 }
209 if (!used) {
210 Print(L" No partitions defined\n");
211 return 0;
212 }
213
214 // dump current state & fill internal structures
215 Print(L" # A Start LBA End LBA Type\n");
216 for (i = 0; i < 4; i++) {
217 if (table[i].start_lba == 0 || table[i].size == 0)
218 continue;
219
220 mbr_parts[mbr_part_count].index = i;
221 mbr_parts[mbr_part_count].start_lba = (UINT64)table[i].start_lba;
222 mbr_parts[mbr_part_count].end_lba = (UINT64)table[i].start_lba + (UINT64)table[i].size - 1;
223 mbr_parts[mbr_part_count].mbr_type = table[i].type;
224 mbr_parts[mbr_part_count].active = (table[i].flags == 0x80) ? TRUE : FALSE;
225
226 Print(L" %d %s %12lld %12lld %02x %s\n",
227 mbr_parts[mbr_part_count].index + 1,
228 mbr_parts[mbr_part_count].active ? STR("*") : STR(" "),
229 mbr_parts[mbr_part_count].start_lba,
230 mbr_parts[mbr_part_count].end_lba,
231 mbr_parts[mbr_part_count].mbr_type,
232 mbr_parttype_name(mbr_parts[mbr_part_count].mbr_type));
233
234 mbr_part_count++;
235 }
236
237 return 0;
238 }
239
240 //
241 // GPT functions
242 //
243
244 GPT_PARTTYPE * gpt_parttype(UINT8 *type_guid)
245 {
246 int i;
247
248 for (i = 0; gpt_types[i].name; i++)
249 if (guids_are_equal(gpt_types[i].guid, type_guid))
250 return &(gpt_types[i]);
251 return &gpt_dummy_type;
252 }
253
254 UINTN read_gpt(VOID)
255 {
256 UINTN status;
257 GPT_HEADER *header;
258 GPT_ENTRY *entry;
259 UINT64 entry_lba;
260 UINTN entry_count, entry_size, i;
261
262 Print(L"\nCurrent GUID partition table:\n");
263
264 // read GPT header
265 status = read_sector(1, sector);
266 if (status != 0)
267 return status;
268
269 // check signature
270 header = (GPT_HEADER *)sector;
271 if (header->signature != 0x5452415020494645ULL) {
272 Print(L" No GPT partition table present!\n");
273 return 0;
274 }
275 if (header->spec_revision != 0x00010000UL) {
276 Print(L" Warning: Unknown GPT spec revision 0x%08x\n", header->spec_revision);
277 }
278 if ((512 % header->entry_size) > 0 || header->entry_size > 512) {
279 Print(L" Error: Invalid GPT entry size (misaligned or more than 512 bytes)\n");
280 return 0;
281 }
282
283 // read entries
284 entry_lba = header->entry_lba;
285 entry_size = header->entry_size;
286 entry_count = header->entry_count;
287
288 for (i = 0; i < entry_count; i++) {
289 if (((i * entry_size) % 512) == 0) {
290 status = read_sector(entry_lba, sector);
291 if (status != 0)
292 return status;
293 entry_lba++;
294 }
295 entry = (GPT_ENTRY *)(sector + ((i * entry_size) % 512));
296
297 if (guids_are_equal(entry->type_guid, empty_guid))
298 continue;
299 if (gpt_part_count == 0) {
300 Print(L" # Start LBA End LBA Type\n");
301 }
302
303 gpt_parts[gpt_part_count].index = i;
304 gpt_parts[gpt_part_count].start_lba = entry->start_lba;
305 gpt_parts[gpt_part_count].end_lba = entry->end_lba;
306 gpt_parts[gpt_part_count].mbr_type = 0;
307 copy_guid(gpt_parts[gpt_part_count].gpt_type, entry->type_guid);
308 gpt_parts[gpt_part_count].gpt_parttype = gpt_parttype(gpt_parts[gpt_part_count].gpt_type);
309 gpt_parts[gpt_part_count].active = FALSE;
310
311 Print(L" %d %12lld %12lld %s\n",
312 gpt_parts[gpt_part_count].index + 1,
313 gpt_parts[gpt_part_count].start_lba,
314 gpt_parts[gpt_part_count].end_lba,
315 gpt_parts[gpt_part_count].gpt_parttype->name);
316
317 gpt_part_count++;
318 }
319 if (gpt_part_count == 0) {
320 Print(L" No partitions defined\n");
321 return 0;
322 }
323
324 return 0;
325 }
326
327 //
328 // detect file system type
329 //
330
331 UINTN detect_mbrtype_fs(UINT64 partlba, UINTN *parttype, CHARN **fsname)
332 {
333 UINTN status;
334 UINTN signature, score;
335 UINTN sectsize, clustersize, reserved, fatcount, dirsize, sectcount, fatsize, clustercount;
336
337 *fsname = STR("Unknown");
338 *parttype = 0;
339
340 // READ sector 0 / offset 0K
341 status = read_sector(partlba, sector);
342 if (status != 0)
343 return status;
344
345 // detect XFS
346 signature = *((UINT32 *)(sector));
347 if (signature == 0x42534658) {
348 *parttype = 0x83;
349 *fsname = STR("XFS");
350 return 0;
351 }
352
353 // detect FAT and NTFS
354 sectsize = *((UINT16 *)(sector + 11));
355 clustersize = sector[13];
356 if (sectsize >= 512 && (sectsize & (sectsize - 1)) == 0 &&
357 clustersize > 0 && (clustersize & (clustersize - 1)) == 0) {
358 // preconditions for both FAT and NTFS are now met
359
360 if (CompareMem(sector + 3, "NTFS ", 8) == 0) {
361 *parttype = 0x07;
362 *fsname = STR("NTFS");
363 return 0;
364 }
365
366 score = 0;
367 // boot jump
368 if ((sector[0] == 0xEB && sector[2] == 0x90) ||
369 sector[0] == 0xE9)
370 score++;
371 // boot signature
372 if (sector[510] == 0x55 && sector[511] == 0xAA)
373 score++;
374 // reserved sectors
375 reserved = *((UINT16 *)(sector + 14));
376 if (reserved == 1 || reserved == 32)
377 score++;
378 // number of FATs
379 fatcount = sector[16];
380 if (fatcount == 2)
381 score++;
382 // number of root dir entries
383 dirsize = *((UINT16 *)(sector + 17));
384 // sector count (16-bit and 32-bit versions)
385 sectcount = *((UINT16 *)(sector + 19));
386 if (sectcount == 0)
387 sectcount = *((UINT32 *)(sector + 32));
388 // media byte
389 if (sector[21] == 0xF0 || sector[21] >= 0xF8)
390 score++;
391 // FAT size in sectors
392 fatsize = *((UINT16 *)(sector + 22));
393 if (fatsize == 0)
394 fatsize = *((UINT32 *)(sector + 36));
395
396 // determine FAT type
397 dirsize = ((dirsize * 32) + (sectsize - 1)) / sectsize;
398 clustercount = sectcount - (reserved + (fatcount * fatsize) + dirsize);
399 clustercount /= clustersize;
400
401 if (score >= 3) {
402 if (clustercount < 4085) {
403 *parttype = 0x01;
404 *fsname = STR("FAT12");
405 } else if (clustercount < 65525) {
406 *parttype = 0x0e;
407 *fsname = STR("FAT16");
408 } else {
409 *parttype = 0x0c;
410 *fsname = STR("FAT32");
411 }
412 // TODO: check if 0e and 0c are okay to use, maybe we should use 06 and 0b instead...
413 return 0;
414 }
415 }
416
417 // READ sector 2 / offset 1K
418 status = read_sector(partlba + 2, sector);
419 if (status != 0)
420 return status;
421
422 // detect HFS+
423 signature = *((UINT16 *)(sector));
424 if (signature == 0x4442) {
425 *parttype = 0xaf;
426 if (*((UINT16 *)(sector + 0x7c)) == 0x2B48)
427 *fsname = STR("HFS Extended (HFS+)");
428 else
429 *fsname = STR("HFS Standard");
430 return 0;
431 } else if (signature == 0x2B48) {
432 *parttype = 0xaf;
433 *fsname = STR("HFS Extended (HFS+)");
434 return 0;
435 }
436
437 // detect ext2/ext3/ext4
438 signature = *((UINT16 *)(sector + 56));
439 if (signature == 0xEF53) {
440 *parttype = 0x83;
441 if (*((UINT16 *)(sector + 96)) & 0x02C0 ||
442 *((UINT16 *)(sector + 100)) & 0x0078)
443 *fsname = STR("ext4");
444 else if (*((UINT16 *)(sector + 92)) & 0x0004)
445 *fsname = STR("ext3");
446 else
447 *fsname = STR("ext2");
448 return 0;
449 }
450
451 // READ sector 128 / offset 64K
452 status = read_sector(partlba + 128, sector);
453 if (status != 0)
454 return status;
455
456 // detect btrfs
457 if (CompareMem(sector + 64, "_BHRfS_M", 8) == 0) {
458 *parttype = 0x83;
459 *fsname = STR("btrfs");
460 return 0;
461 }
462
463 // detect ReiserFS
464 if (CompareMem(sector + 52, "ReIsErFs", 8) == 0 ||
465 CompareMem(sector + 52, "ReIsEr2Fs", 9) == 0 ||
466 CompareMem(sector + 52, "ReIsEr3Fs", 9) == 0) {
467 *parttype = 0x83;
468 *fsname = STR("ReiserFS");
469 return 0;
470 }
471
472 // detect Reiser4
473 if (CompareMem(sector, "ReIsEr4", 7) == 0) {
474 *parttype = 0x83;
475 *fsname = STR("Reiser4");
476 return 0;
477 }
478
479 // READ sector 64 / offset 32K
480 status = read_sector(partlba + 64, sector);
481 if (status != 0)
482 return status;
483
484 // detect JFS
485 if (CompareMem(sector, "JFS1", 4) == 0) {
486 *parttype = 0x83;
487 *fsname = STR("JFS");
488 return 0;
489 }
490
491 // READ sector 16 / offset 8K
492 status = read_sector(partlba + 16, sector);
493 if (status != 0)
494 return status;
495
496 // detect ReiserFS
497 if (CompareMem(sector + 52, "ReIsErFs", 8) == 0 ||
498 CompareMem(sector + 52, "ReIsEr2Fs", 9) == 0 ||
499 CompareMem(sector + 52, "ReIsEr3Fs", 9) == 0) {
500 *parttype = 0x83;
501 *fsname = STR("ReiserFS");
502 return 0;
503 }
504
505 return 0;
506 }