Debian Codegraph @

dos.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * dos.c
3 * Detection of DOS parition maps and file systems
4 *
5 * Copyright (c) 2003-2006 Christoph Pfisterer
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy,
11 * modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include "global.h"
29
30 /*
31 * DOS partition types
32 *
33 * Taken from fdisk/i386_sys_types.c and fdisk/common.h of
34 * util-linux 2.11n (as packaged by Debian), Feb 08, 2003.
35 */
36
37 struct systypes {
38 unsigned char type;
39 char *name;
40 };
41
42 struct systypes i386_sys_types[] = {
43 { 0x00, "Empty" },
44 { 0x01, "FAT12" },
45 { 0x02, "XENIX root" },
46 { 0x03, "XENIX usr" },
47 { 0x04, "FAT16 <32M" },
48 { 0x05, "Extended" },
49 { 0x06, "FAT16" },
50 { 0x07, "HPFS/NTFS" },
51 { 0x08, "AIX" },
52 { 0x09, "AIX bootable" },
53 { 0x0a, "OS/2 Boot Manager" },
54 { 0x0b, "Win95 FAT32" },
55 { 0x0c, "Win95 FAT32 (LBA)" },
56 { 0x0e, "Win95 FAT16 (LBA)" },
57 { 0x0f, "Win95 Ext'd (LBA)" },
58 { 0x10, "OPUS" },
59 { 0x11, "Hidden FAT12" },
60 { 0x12, "Compaq diagnostics" },
61 { 0x14, "Hidden FAT16 <32M" },
62 { 0x16, "Hidden FAT16" },
63 { 0x17, "Hidden HPFS/NTFS" },
64 { 0x18, "AST SmartSleep" },
65 { 0x1b, "Hidden Win95 FAT32" },
66 { 0x1c, "Hidden Win95 FAT32 (LBA)" },
67 { 0x1e, "Hidden Win95 FAT16 (LBA)" },
68 { 0x24, "NEC DOS" },
69 { 0x39, "Plan 9" },
70 { 0x3c, "PartitionMagic recovery" },
71 { 0x40, "Venix 80286" },
72 { 0x41, "PPC PReP Boot" },
73 { 0x42, "SFS / MS LDM" },
74 { 0x4d, "QNX4.x" },
75 { 0x4e, "QNX4.x 2nd part" },
76 { 0x4f, "QNX4.x 3rd part" },
77 { 0x50, "OnTrack DM" },
78 { 0x51, "OnTrack DM6 Aux1" },
79 { 0x52, "CP/M" },
80 { 0x53, "OnTrack DM6 Aux3" },
81 { 0x54, "OnTrackDM6" },
82 { 0x55, "EZ-Drive" },
83 { 0x56, "Golden Bow" },
84 { 0x5c, "Priam Edisk" },
85 { 0x61, "SpeedStor" },
86 { 0x63, "GNU HURD or SysV" },
87 { 0x64, "Novell Netware 286" },
88 { 0x65, "Novell Netware 386" },
89 { 0x70, "DiskSecure Multi-Boot" },
90 { 0x75, "PC/IX" },
91 { 0x78, "XOSL" },
92 { 0x80, "Old Minix" },
93 { 0x81, "Minix / old Linux" },
94 { 0x82, "Linux swap / Solaris" },
95 { 0x83, "Linux" },
96 { 0x84, "OS/2 hidden C: drive" },
97 { 0x85, "Linux extended" },
98 { 0x86, "NTFS volume set" },
99 { 0x87, "NTFS volume set" },
100 { 0x8e, "Linux LVM" },
101 { 0x93, "Amoeba" },
102 { 0x94, "Amoeba BBT" },
103 { 0x9f, "BSD/OS" },
104 { 0xa0, "IBM Thinkpad hibernation" },
105 { 0xa5, "FreeBSD" },
106 { 0xa6, "OpenBSD" },
107 { 0xa7, "NeXTSTEP" },
108 { 0xa9, "NetBSD" },
109 { 0xaf, "Mac OS X" },
110 { 0xb7, "BSDI fs" },
111 { 0xb8, "BSDI swap" },
112 { 0xbb, "Boot Wizard hidden" },
113 { 0xc1, "DRDOS/sec (FAT-12)" },
114 { 0xc4, "DRDOS/sec (FAT-16 < 32M)" },
115 { 0xc6, "DRDOS/sec (FAT-16)" },
116 { 0xc7, "Syrinx" },
117 { 0xda, "Non-FS data" },
118 { 0xdb, "CP/M / CTOS / ..." },
119 { 0xde, "Dell Utility" },
120 { 0xdf, "BootIt" },
121 { 0xe1, "DOS access" },
122 { 0xe3, "DOS R/O" },
123 { 0xe4, "SpeedStor" },
124 { 0xeb, "BeOS fs" },
125 { 0xee, "EFI GPT protective" },
126 { 0xef, "EFI System (FAT)" },
127 { 0xf0, "Linux/PA-RISC boot" },
128 { 0xf1, "SpeedStor" },
129 { 0xf4, "SpeedStor" },
130 { 0xf2, "DOS secondary" },
131 { 0xfd, "Linux raid autodetect" },
132 { 0xfe, "LANstep" },
133 { 0xff, "BBT" },
134 { 0, 0 }
135 };
136
137
138 char * get_name_for_mbrtype(int type)
139 {
140 int i;
141
142 for (i = 0; i386_sys_types[i].name; i++)
143 if (i386_sys_types[i].type == type)
144 return i386_sys_types[i].name;
145 return "Unknown";
146 }
147
148 /*
149 * DOS-style partition map / MBR
150 */
151
152 static void detect_dos_partmap_ext(SECTION *section, u8 extbase,
153 int level, int *extpartnum);
154
155 void detect_dos_partmap(SECTION *section, int level)
156 {
157 unsigned char *buf;
158 int i, off, used, type, types[4], bootflags[4];
159 u4 start, size, starts[4], sizes[4];
160 int extpartnum = 5;
161 char s[256], append[64];
162
163 /* partition maps only occur at the start of a device */
164 if (section->pos != 0)
165 return;
166
167 if (get_buffer(section, 0, 512, (void **)&buf) < 512)
168 return;
169
170 /* check signature */
171 if (buf[510] != 0x55 || buf[511] != 0xAA)
172 return;
173
174 /* get entries and check */
175 used = 0;
176 for (off = 446, i = 0; i < 4; i++, off += 16) {
177 /* get data */
178 bootflags[i] = buf[off];
179 types[i] = buf[off + 4];
180 starts[i] = get_le_long(buf + off + 8);
181 sizes[i] = get_le_long(buf + off + 12);
182
183 /* bootable flag: either on or off */
184 if (bootflags[i] != 0x00 && bootflags[i] != 0x80)
185 return;
186 /* size non-zero -> entry in use */
187 if (starts[i] && sizes[i])
188 used = 1;
189 }
190 if (!used)
191 return;
192
193 /* parse the data for real */
194 print_line(level, "DOS/MBR partition map");
195 for (i = 0; i < 4; i++) {
196 start = starts[i];
197 size = sizes[i];
198 type = types[i];
199 if (start == 0 || size == 0)
200 continue;
201
202 sprintf(append, " from %lu", start);
203 if (bootflags[i] == 0x80)
204 strcat(append, ", bootable");
205 format_blocky_size(s, size, 512, "sectors", append);
206 print_line(level, "Partition %d: %s",
207 i+1, s);
208
209 print_line(level + 1, "Type 0x%02X (%s)", type, get_name_for_mbrtype(type));
210
211 if (type == 0x05 || type == 0x0f || type == 0x85) {
212 /* extended partition */
213 detect_dos_partmap_ext(section, start, level + 1, &extpartnum);
214 } else if (type != 0xee) {
215 /* recurse for content detection */
216 analyze_recursive(section, level + 1,
217 (u8)start * 512, (u8)size * 512, 0);
218 }
219 }
220 }
221
222 static void detect_dos_partmap_ext(SECTION *section, u8 extbase,
223 int level, int *extpartnum)
224 {
225 unsigned char *buf;
226 u8 tablebase, nexttablebase;
227 int i, off, type, types[4];
228 u4 start, size, starts[4], sizes[4];
229 char s[256], append[64];
230
231 for (tablebase = extbase; tablebase; tablebase = nexttablebase) {
232 /* read sector from linked list */
233 if (get_buffer(section, tablebase << 9, 512, (void **)&buf) < 512)
234 return;
235
236 /* check signature */
237 if (buf[510] != 0x55 || buf[511] != 0xAA) {
238 print_line(level, "Signature missing");
239 return;
240 }
241
242 /* get entries */
243 for (off = 446, i = 0; i < 4; i++, off += 16) {
244 types[i] = buf[off + 4];
245 starts[i] = get_le_long(buf + off + 8);
246 sizes[i] = get_le_long(buf + off + 12);
247 }
248
249 /* parse the data for real */
250 nexttablebase = 0;
251 for (i = 0; i < 4; i++) {
252 start = starts[i];
253 size = sizes[i];
254 type = types[i];
255 if (size == 0)
256 continue;
257
258 if (type == 0x05 || type == 0x85) {
259 /* inner extended partition */
260
261 nexttablebase = extbase + start;
262
263 } else {
264 /* logical partition */
265
266 sprintf(append, " from %llu+%lu", tablebase, start);
267 format_blocky_size(s, size, 512, "sectors", append);
268 print_line(level, "Partition %d: %s",
269 *extpartnum, s);
270 (*extpartnum)++;
271 print_line(level + 1, "Type 0x%02X (%s)", type, get_name_for_mbrtype(type));
272
273 /* recurse for content detection */
274 if (type != 0xee) {
275 analyze_recursive(section, level + 1,
276 (tablebase + start) * 512, (u8)size * 512, 0);
277 }
278 }
279 }
280 }
281 }
282
283 /*
284 * EFI GPT partition map
285 */
286
287 struct gpttypes {
288 char *guid;
289 char *name;
290 };
291
292 struct gpttypes gpt_types[] = {
293 { "\x28\x73\x2A\xC1\x1F\xF8\xD2\x11\xBA\x4B\x00\xA0\xC9\x3E\xC9\x3B", "EFI System (FAT)" },
294 { "\x41\xEE\x4D\x02\xE7\x33\xD3\x11\x9D\x69\x00\x08\xC7\x81\xF3\x9F", "MBR partition scheme" },
295 { "\x16\xE3\xC9\xE3\x5C\x0B\xB8\x4D\x81\x7D\xF9\x2D\xF0\x02\x15\xAE", "MS Reserved" },
296 { "\xA2\xA0\xD0\xEB\xE5\xB9\x33\x44\x87\xC0\x68\xB6\xB7\x26\x99\xC7", "Basic Data" },
297 { "\xAA\xC8\x08\x58\x8F\x7E\xE0\x42\x85\xD2\xE1\xE9\x04\x34\xCF\xB3", "MS LDM Metadata" },
298 { "\xA0\x60\x9B\xAF\x31\x14\x62\x4F\xBC\x68\x33\x11\x71\x4A\x69\xAD", "MS LDM Data" },
299 { "\x1E\x4C\x89\x75\xEB\x3A\xD3\x11\xB7\xC1\x7B\x03\xA0\x00\x00\x00", "HP/UX Data" },
300 { "\x28\xE7\xA1\xE2\xE3\x32\xD6\x11\xA6\x82\x7B\x03\xA0\x00\x00\x00", "HP/UX Service" },
301 { "\x0F\x88\x9D\xA1\xFC\x05\x3B\x4D\xA0\x06\x74\x3F\x0F\x84\x91\x1E", "Linux RAID" },
302 { "\x6D\xFD\x57\x06\xAB\xA4\xC4\x43\x84\xE5\x09\x33\xC8\x4B\x4F\x4F", "Linux Swap" },
303 { "\x79\xD3\xD6\xE6\x07\xF5\xC2\x44\xA2\x3C\x23\x8F\x2A\x3D\xF9\x28", "Linux LVM" },
304 { "\x39\x33\xA6\x8D\x07\x00\xC0\x60\xC4\x36\x08\x3A\xC8\x23\x09\x08", "Linux Reserved" },
305 { "\xB4\x7C\x6E\x51\xCF\x6E\xD6\x11\x8F\xF8\x00\x02\x2D\x09\x71\x2B", "FreeBSD Data" },
306 { "\xB5\x7C\x6E\x51\xCF\x6E\xD6\x11\x8F\xF8\x00\x02\x2D\x09\x71\x2B", "FreeBSD Swap" },
307 { "\xB6\x7C\x6E\x51\xCF\x6E\xD6\x11\x8F\xF8\x00\x02\x2D\x09\x71\x2B", "FreeBSD UFS" },
308 { "\xB8\x7C\x6E\x51\xCF\x6E\xD6\x11\x8F\xF8\x00\x02\x2D\x09\x71\x2B", "FreeBSD Vinum" },
309 { "\x00\x53\x46\x48\x00\x00\xAA\x11\xAA\x11\x00\x30\x65\x43\xEC\xAC", "Mac HFS+" },
310 { 0, 0 }
311 };
312
313 static char * get_name_for_guid(void *guid)
314 {
315 int i;
316
317 for (i = 0; gpt_types[i].name; i++)
318 if (memcmp(gpt_types[i].guid, guid, 16) == 0)
319 return gpt_types[i].name;
320 return "Unknown";
321 }
322
323 void detect_gpt_partmap(SECTION *section, int level)
324 {
325 unsigned char *buf;
326 u8 diskblocks, partmap_start, start, end, size;
327 u4 partmap_count, partmap_entry_size;
328 u4 i;
329 char s[256], append[64];
330 int last_unused;
331
332 /* partition maps only occur at the start of a device */
333 if (section->pos != 0)
334 return;
335
336 /* get LBA 1: GPT header */
337 if (get_buffer(section, 512, 512, (void **)&buf) < 512)
338 return;
339
340 /* check signature */
341 if (memcmp(buf, "EFI PART", 8) != 0)
342 return;
343
344 /* get header information */
345 if (get_le_quad(buf + 0x18) != 1)
346 return;
347 diskblocks = get_le_quad(buf + 0x20) + 1;
348 partmap_start = get_le_quad(buf + 0x48);
349 partmap_count = get_le_long(buf + 0x50);
350 partmap_entry_size = get_le_long(buf + 0x54);
351
352 print_line(level, "GPT partition map, %d entries", (int)partmap_count);
353 format_blocky_size(s, diskblocks, 512, "sectors", NULL);
354 print_line(level+1, "Disk size %s", s);
355 format_guid(buf + 0x38, s);
356 print_line(level+1, "Disk GUID %s", s);
357
358 /* get entries */
359 last_unused = 0;
360 for (i = 0; i < partmap_count; i++) {
361 if (get_buffer(section, (partmap_start * 512) + i * partmap_entry_size, partmap_entry_size, (void **)&buf) < partmap_entry_size)
362 return;
363
364 if (memcmp(buf, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16) == 0) {
365 if (last_unused == 0)
366 print_line(level, "Partition %d: unused", i+1);
367 last_unused = 1;
368 continue;
369 }
370 last_unused = 0;
371
372 /* size */
373 start = get_le_quad(buf + 0x20);
374 end = get_le_quad(buf + 0x28);
375 size = end + 1 - start;
376
377 sprintf(append, " from %llu", start);
378 format_blocky_size(s, size, 512, "sectors", append);
379 print_line(level, "Partition %d: %s", i+1, s);
380
381 /* type */
382 format_guid(buf, s);
383 print_line(level+1, "Type %s (GUID %s)", get_name_for_guid(buf), s);
384
385 /* partition name */
386 format_utf16_le(buf + 0x38, 72, s);
387 print_line(level+1, "Partition Name \"%s\"", s);
388
389 /* GUID */
390 format_guid(buf + 0x10, s);
391 print_line(level+1, "Partition GUID %s", s);
392
393 /* recurse for content detection */
394 if (start > 0 && size > 0) { /* avoid recursion on self */
395 analyze_recursive(section, level + 1,
396 start * 512, size * 512, 0);
397 }
398 }
399 }
400
401 /*
402 * FAT12/FAT16/FAT32 file systems
403 */
404
405 static char *fatnames[] = { "FAT12", "FAT16", "FAT32" };
406
407 void detect_fat(SECTION *section, int level)
408 {
409 int i, score, fattype;
410 u4 sectsize, clustersize, reserved, fatcount, dirsize, fatsize;
411 u8 sectcount, clustercount;
412 u2 atari_csum;
413 unsigned char *buf;
414 char s[256];
415
416 if (get_buffer(section, 0, 512, (void **)&buf) < 512)
417 return;
418
419 /* first, some hard tests */
420 /* sector size has four allowed values */
421 sectsize = get_le_short(buf + 11);
422 if (sectsize != 512 && sectsize != 1024 &&
423 sectsize != 2048 && sectsize != 4096)
424 return;
425 /* sectors per cluster: must be a power of two */
426 clustersize = buf[13];
427 if (clustersize == 0 || (clustersize & (clustersize - 1)))
428 return;
429 /* since the above is also present on NTFS, make sure it's not NTFS... */
430 if (memcmp(buf + 3, "NTFS ", 8) == 0)
431 return;
432
433 /* next, some soft tests, taking score */
434 score = 0;
435
436 /* boot jump */
437 if ((buf[0] == 0xEB && buf[2] == 0x90) ||
438 buf[0] == 0xE9)
439 score++;
440 /* boot signature */
441 if (buf[510] == 0x55 && buf[511] == 0xAA)
442 score++;
443 /* reserved sectors */
444 reserved = get_le_short(buf + 14);
445 if (reserved == 1 || reserved == 32)
446 score++;
447 /* number of FATs */
448 fatcount = buf[16];
449 if (fatcount == 2)
450 score++;
451 /* number of root dir entries */
452 dirsize = get_le_short(buf + 17);
453 /* sector count (16-bit and 32-bit versions) */
454 sectcount = get_le_short(buf + 19);
455 if (sectcount == 0)
456 sectcount = get_le_long(buf + 32);
457 /* media byte */
458 if (buf[21] == 0xF0 || buf[21] >= 0xF8)
459 score++;
460 /* FAT size in sectors */
461 fatsize = get_le_short(buf + 22);
462 if (fatsize == 0)
463 fatsize = get_le_long(buf + 36);
464
465 /* determine FAT type */
466 dirsize = ((dirsize * 32) + (sectsize - 1)) / sectsize;
467 clustercount = sectcount - (reserved + (fatcount * fatsize) + dirsize);
468 clustercount /= clustersize;
469
470 if (clustercount < 4085)
471 fattype = 0;
472 else if (clustercount < 65525)
473 fattype = 1;
474 else
475 fattype = 2;
476
477 /* check for ATARI ST boot checksum */
478 atari_csum = 0;
479 for (i = 0; i < 512; i += 2)
480 atari_csum += get_be_short(buf + i);
481
482 /* tell the user */
483 s[0] = 0;
484 if (atari_csum == 0x1234)
485 strcpy(s, ", ATARI ST bootable");
486 print_line(level, "%s file system (hints score %d of %d%s)",
487 fatnames[fattype], score, 5, s);
488
489 if (sectsize > 512)
490 print_line(level + 1, "Unusual sector size %lu bytes", sectsize);
491
492 format_blocky_size(s, clustercount, clustersize * sectsize,
493 "clusters", NULL);
494 print_line(level + 1, "Volume size %s", s);
495
496 /* get the cached volume name if present */
497 if (fattype < 2) {
498 if (buf[38] == 0x29) {
499 memcpy(s, buf + 43, 11);
500 s[11] = 0;
501 for (i = 10; i >= 0 && s[i] == ' '; i--)
502 s[i] = 0;
503 if (strcmp(s, "NO NAME") != 0)
504 print_line(level + 1, "Volume name \"%s\"", s);
505 }
506 } else {
507 if (buf[66] == 0x29) {
508 memcpy(s, buf + 71, 11);
509 s[11] = 0;
510 for (i = 10; i >= 0 && s[i] == ' '; i--)
511 s[i] = 0;
512 if (strcmp(s, "NO NAME") != 0)
513 print_line(level + 1, "Volume name \"%s\"", s);
514 }
515 }
516 }
517
518 /*
519 * NTFS file system
520 */
521
522 void detect_ntfs(SECTION *section, int level)
523 {
524 u4 sectsize, clustersize;
525 u8 sectcount;
526 unsigned char *buf;
527 char s[256];
528
529 if (get_buffer(section, 0, 512, (void **)&buf) < 512)
530 return;
531
532 /* check signatures */
533 if (memcmp(buf + 3, "NTFS ", 8) != 0)
534 return;
535 /* disabled for now, mkntfs(8) doesn't generate it
536 if (memcmp(buf + 0x24, "\x80\x00\x80\x00", 4) != 0)
537 return;
538 */
539
540 /* sector size: must be a power of two */
541 sectsize = get_le_short(buf + 11);
542 if (sectsize < 512 || (sectsize & (sectsize - 1)))
543 return;
544 /* sectors per cluster: must be a power of two */
545 clustersize = buf[13];
546 if (clustersize == 0 || (clustersize & (clustersize - 1)))
547 return;
548
549 /* get size in sectors */
550 sectcount = get_le_quad(buf + 0x28);
551
552 /* tell the user */
553 print_line(level, "NTFS file system");
554
555 format_blocky_size(s, sectcount, sectsize, "sectors", NULL);
556 print_line(level + 1, "Volume size %s", s);
557 }
558
559 /*
560 * HPFS file system
561 */
562
563 void detect_hpfs(SECTION *section, int level)
564 {
565 unsigned char *buf;
566 char s[256];
567 u8 sectcount;
568
569 if (get_buffer(section, 16*512, 512, (void **)&buf) < 512)
570 return;
571
572 if (memcmp(buf, "\xF9\x95\xE8\x49\xFA\x53\xE9\xC5", 8) != 0)
573 return;
574
575 print_line(level, "HPFS file system (version %d, functional version %d)",
576 (int)buf[8], (int)buf[9]);
577
578 sectcount = get_le_long(buf + 16);
579 format_blocky_size(s, sectcount, 512, "sectors", NULL);
580 print_line(level + 1, "Volume size %s", s);
581
582 /* TODO: BPB in boot sector, volume label -- information? */
583 }
584
585 /*
586 * DOS/Windows boot loaders
587 */
588
589 void detect_dos_loader(SECTION *section, int level)
590 {
591 int fill;
592 unsigned char *buf;
593
594 if (section->flags & FLAG_IN_DISKLABEL)
595 return;
596
597 fill = get_buffer(section, 0, 2048, (void **)&buf);
598 if (fill < 512)
599 return;
600
601 if (find_memory(buf, fill, "NTLDR", 5) >= 0)
602 print_line(level, "Windows NTLDR boot loader");
603 else if (find_memory(buf, 512, "WINBOOT SYS", 11) >= 0)
604 print_line(level, "Windows 95/98/ME boot loader");
605 else if (find_memory(buf, 512, "MSDOS SYS", 11) >= 0)
606 print_line(level, "Windows / MS-DOS boot loader");
607 }
608
609 /* EOF */