Debian Codegraph @

linux.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * linux.c
3 * Detection of Linux file systems and boot codes
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 * ext2/ext3/ext4 file system
32 */
33
•34 void detect_ext234(SECTION *section, int level)
35 {
36 unsigned char *buf;
37 char s[256];
•38 int fslevel, is_journal, is_dev;
39 u4 blocksize;
40 u8 blockcount;
41
42 if (get_buffer(section, 1024, 1024, (void **)&buf) < 1024)
43 return;
44
45 if (get_le_short(buf + 56) == 0xEF53) {
•46 fslevel = 2;
47 is_journal = 0;
48 is_dev = 0;
49 /* Ext3/4 external journal: INCOMPAT feature JOURNAL_DEV */
50 if (get_le_long(buf + 96) & 0x0008) {
51 is_journal = 1;
52 fslevel = 3; /* at least ext3, ext2 has no journalling */
53 }
54 /* Ext3/4 COMPAT feature: HAS_JOURNAL */
55 if (get_le_long(buf + 92) & 0x0004)
56 fslevel = 3;
57 /* Ext4 INCOMPAT features: EXTENTS, 64BIT, FLEX_BG */
58 if (get_le_long(buf + 96) & 0x02C0)
59 fslevel = 4;
60 /* Ext4 RO_COMPAT features: HUGE_FILE, GDT_CSUM, DIR_NLINK, EXTRA_ISIZE */
61 if (get_le_long(buf + 100) & 0x0078)
62 fslevel = 4;
63 /* Ext4 sets min_extra_isize even on external journals */
64 if (get_le_short(buf + 348) >= 0x1c)
65 fslevel = 4;
66 /* Ext4dev TEST_FILESYS flag */
67 if (get_le_long(buf + 352) & 0x0004)
68 is_dev = 1;
69
70 print_line(level, "Ext%d%s %s", fslevel, is_dev ? "dev" : "",
71 is_journal ? "external journal" : "file system");
72
73 get_string(buf + 120, 16, s);
74 if (s[0])
75 print_line(level + 1, "Volume name \"%s\"", s);
76
77 format_uuid(buf + 104, s);
78 print_line(level + 1, "UUID %s", s);
79
80 get_string(buf + 136, 64, s);
81 if (s[0])
82 print_line(level + 1, "Last mounted at \"%s\"", s);
83
84 blocksize = 1024 << get_le_long(buf + 24);
85 blockcount = get_le_long(buf + 4);
86 format_blocky_size(s, blockcount, blocksize, "blocks", NULL);
87 print_line(level + 1, "Volume size %s", s);
88
89 /* 76 4 s_rev_level */
90 /* 62 2 s_minor_rev_level */
91 /* 72 4 s_creator_os */
92 /* 92 3x4 s_feature_compat, s_feature_incompat, s_feature_ro_compat */
93 }
94 }
95
96 /*
•97 * btrfs file system
98 */
99
100 void detect_btrfs(SECTION *section, int level)
101 {
102 unsigned char *buf;
103 char s[258];
104
105 if (get_buffer(section, 64 * 1024, 1024, (void **)&buf) < 1024)
106 return;
107
108 if (memcmp(buf + 64, "_BHRfS_M", 8) == 0) {
109 print_line(level, "Btrfs file system");
110
111 get_string(buf + 299, 256, s);
112 if (s[0])
113 print_line(level + 1, "Volume name \"%s\"", s);
114
115 format_uuid(buf + 32, s);
116 print_line(level + 1, "UUID %s", s);
117
118 format_size(s, get_le_quad(buf + 0x70));
119 print_line(level + 1, "Volume size %s", s);
120 }
121 }
122
123 /*
124 * ReiserFS file system
125 */
126
127 void detect_reiser(SECTION *section, int level)
128 {
129 unsigned char *buf;
130 int i, at, newformat;
131 int offsets[3] = { 8, 64, -1 };
132 char s[256];
133 u8 blockcount;
134 u4 blocksize;
135
136 for (i = 0; offsets[i] >= 0; i++) {
137 at = offsets[i];
138 if (get_buffer(section, at * 1024, 1024, (void **)&buf) < 1024)
139 continue;
140
141 /* check signature */
142 if (memcmp(buf + 52, "ReIsErFs", 8) == 0) {
143 print_line(level, "ReiserFS file system (old 3.5 format, standard journal, starts at %d KiB)", at);
144 newformat = 0;
145 } else if (memcmp(buf + 52, "ReIsEr2Fs", 9) == 0) {
146 print_line(level, "ReiserFS file system (new 3.6 format, standard journal, starts at %d KiB)", at);
147 newformat = 1;
148 } else if (memcmp(buf + 52, "ReIsEr3Fs", 9) == 0) {
149 newformat = get_le_short(buf + 72);
150 if (newformat == 0) {
•151 print_line(level, "ReiserFS file system (old 3.5 format, non-standard journal, starts at %d KiB)", at);
152 } else if (newformat == 2) {
•153 print_line(level, "ReiserFS file system (new 3.6 format, non-standard journal, starts at %d KiB)", at);
154 newformat = 1;
155 } else {
•156 print_line(level, "ReiserFS file system (v3 magic, but unknown version %d, starts at %d KiB)", newformat, at);
157 continue;
158 }
159 } else
160 continue;
161
162 /* get data */
163 blockcount = get_le_long(buf);
164 blocksize = get_le_short(buf + 44);
165 /* for new format only:
166 hashtype = get_le_long(buf + 64);
167 */
168
169 /* get label */
170 get_string(buf + 100, 16, s);
171 if (s[0])
172 print_line(level + 1, "Volume name \"%s\"", s);
173
174 format_uuid(buf + 84, s);
175 print_line(level + 1, "UUID %s", s);
176
177 /* print size */
178 format_blocky_size(s, blockcount, blocksize, "blocks", NULL);
179 print_line(level + 1, "Volume size %s", s);
180
181 /* TODO: print hash code */
182 }
183 }
184
185 /*
186 * Reiser4 file system
187 */
188
189 void detect_reiser4(SECTION *section, int level)
190 {
191 unsigned char *buf;
192 char s[256];
193 int layout_id;
194 char layout_name[64];
195 u4 blocksize;
196 u8 blockcount;
197
198 if (get_buffer(section, 16 * 4096, 1024, (void **)&buf) < 1024)
199 return;
200
201 /* check signature */
202 if (memcmp(buf, "ReIsEr4", 7) != 0)
203 return;
204
205 /* get data from master superblock */
206 layout_id = get_le_short(buf + 16);
207 blocksize = get_le_short(buf + 18);
208 if (layout_id == 0)
209 strcpy(layout_name, "4.0 layout");
210 else
211 sprintf(layout_name, "Unknown layout with ID %d", layout_id);
212
213 format_size(s, blocksize);
214 print_line(level, "Reiser4 file system (%s, block size %s)",
•215 layout_name, s);
216
217 /* get label and UUID */
218 get_string(buf + 36, 16, s);
219 if (s[0])
220 print_line(level + 1, "Volume name \"%s\"", s);
221
222 format_uuid(buf + 20, s);
223 print_line(level + 1, "UUID %s", s);
224
225 if (layout_id == 0) {
226 /* read 4.0 superblock */
227 if (get_buffer(section, 17 * 4096, 1024, (void **)&buf) < 1024)
228 return;
229 if (memcmp(buf + 52, "ReIsEr40FoRmAt", 14) != 0) {
230 print_line(level + 1, "Superblock for 4.0 format missing");
231 return;
232 }
233
234 blockcount = get_le_quad(buf);
235 format_blocky_size(s, blockcount, blocksize, "blocks", NULL);
236 print_line(level + 1, "Volume size %s", s);
237 }
238 }
239
240 /*
241 * Linux RAID persistent superblock
242 */
243
244 static char *levels[] = {
245 "Multipath",
246 "\'HSM\'",
247 "\'translucent\'",
248 "Linear",
249 "RAID0",
250 "RAID1",
251 NULL, NULL,
252 "RAID4(?)",
253 "RAID5"
254 };
255
256 void detect_linux_raid(SECTION *section, int level)
257 {
258 unsigned char *buf;
259 u8 pos;
260 int rlevel, nr_disks, raid_disks, spare;
261 u1 uuid[16];
262 char s[256];
263
264 /* don't do this if:
265 * - the size is unknown (0)
266 * - the size is too small for the calculation
267 * - it is inefficient to read from the end of the source
268 */
269 if (section->size < 65536 || section->source->sequential)
270 return;
271
272 /* get RAID superblock from the end of the device */
273 pos = (section->size & ~65535) - 65536;
274 if (get_buffer(section, pos, 4096, (void **)&buf) < 4096)
275 return;
276
277 /* signature */
278 if (get_le_long(buf) != 0xa92b4efc)
279 return;
280
281 print_line(level, "Linux RAID disk, version %lu.%lu.%lu",
•282 get_le_long(buf + 4), get_le_long(buf + 8),
283 get_le_long(buf + 12));
284
285 /* get some data */
286 rlevel = (int)(long)get_le_long(buf + 28); /* is signed, actually */
287 nr_disks = get_le_long(buf + 36);
288 raid_disks = get_le_long(buf + 40);
289 spare = nr_disks - raid_disks;
290
291 /* find the name for the personality in the table */
292 if (rlevel < -4 || rlevel > 5 || levels[rlevel+4] == NULL) {
293 print_line(level + 1, "Unknown RAID level %d using %d regular %d spare disks",
•294 rlevel, raid_disks, spare);
295 } else {
296 print_line(level + 1, "%s set using %d regular %d spare disks",
•297 levels[rlevel+4], raid_disks, spare);
298 }
299
300 /* get the UUID */
301 memcpy(uuid, buf + 5*4, 4);
302 memcpy(uuid + 4, buf + 13*4, 3*4);
303 format_uuid(uuid, s);
304 print_line(level + 1, "RAID set UUID %s", s);
305 }
306
307 /*
308 * Linux LVM1
309 */
310
311 void detect_linux_lvm(SECTION *section, int level)
312 {
313 unsigned char *buf;
314 char s[256];
315 int minor_version, pv_number;
316 u8 pe_size, pe_count, pe_start;
317
318 if (get_buffer(section, 0, 1024, (void **)&buf) < 1024)
319 return;
320
321 /* signature */
322 if (buf[0] != 'H' || buf[1] != 'M')
323 return;
324 /* helpful sanity check... */
325 if (get_le_long(buf + 36) == 0 || get_le_long(buf + 40) == 0)
326 return;
327
328 minor_version = get_le_short(buf + 2);
329 print_line(level, "Linux LVM1 volume, version %d%s",
•330 minor_version,
331 (minor_version < 1 || minor_version > 2) ? " (unknown)" : "");
332
333 /* volume group name */
334 get_string(buf + 172, 128, s);
335 print_line(level + 1, "Volume group name \"%s\"", s);
336
337 /* "UUID" of this physical volume */
338 format_uuid_lvm(buf + 0x2c, s);
339 print_line(level + 1, "PV UUID %s", s);
340
341 /* number of this physical volume */
342 pv_number = get_le_long(buf + 432);
343 print_line(level + 1, "PV number %d", pv_number);
344
345 /* volume size */
346 pe_size = get_le_long(buf + 452);
347 pe_count = get_le_long(buf + 456);
348 format_blocky_size(s, pe_count, pe_size * 512, "PEs", NULL);
349 print_line(level + 1, "Useable size %s", s);
350
351 /* get start of first PE */
352 if (minor_version == 1) {
353 /* minor format 1: first PE starts after the declared length of the PE tables */
354 pe_start = get_le_long(buf + 36) + get_le_long(buf + 40);
355 } else if (minor_version == 2) {
356 /* minor format 2: a field in the header indicates this */
357 pe_start = get_le_long(buf + 464) << 9;
358 } else {
359 /* unknown minor format */
360 pe_start = 0;
361 }
362
363 /* try to detect from first PE */
364 if (pe_start > 0) {
365 analyze_recursive(section, level + 1,
•366 pe_start, 0, 0);
367 /* TODO: elaborate on this by reading the PE allocation map */
368 }
369 }
370
371 /*
372 * Linux LVM2
373 */
374
375 void detect_linux_lvm2(SECTION *section, int level)
376 {
377 unsigned char *buf;
378 int at, i;
379 char s[256];
380 u8 labelsector;
381 u4 labeloffset;
382 u8 pvsize, mdoffset, mdsize;
383 int mda_version;
384
385 for (at = 0; at < 4; at++) {
386 if (get_buffer(section, at * 512, 512, (void **)&buf) < 512)
387 continue;
388
389 /* check signature */
390 if (memcmp(buf, "LABELONE", 8) != 0)
391 continue;
392
393 labelsector = get_le_quad(buf + 8);
394 labeloffset = get_le_long(buf + 20);
395
396 if (memcmp(buf + 24, "LVM2 001", 8) != 0) {
397 get_string(buf + 24, 8, s);
398 print_line(level, "LABELONE label at sector %d, unknown type \"%s\"",
•399 at, s);
400 return;
401 }
402
403 print_line(level, "Linux LVM2 volume, version 001");
404 print_line(level + 1, "LABELONE label at sector %d",
•405 at);
406
407 if (labeloffset >= 512 || labelsector > 256 ||
•408 labelsector != at) {
409 print_line(level + 1, "LABELONE data inconsistent, aborting analysis");
410 return;
411 }
412
413 /* "UUID" of this physical volume */
414 format_uuid_lvm(buf + labeloffset, s);
415 print_line(level + 1, "PV UUID %s", s);
416
417 /* raw volume size */
418 pvsize = get_le_quad(buf + labeloffset + 32);
419 format_size_verbose(s, pvsize);
420 print_line(level + 1, "Volume size %s", s);
421
422 /* find first metadata area in list */
423 mdoffset = 0;
424 for (i = 0; i < 16; i++)
425 if (get_le_quad(buf + labeloffset + 40 + i * 16) == 0) {
•426 i++;
427 mdoffset = get_le_quad(buf + labeloffset + 40 + i * 16);
428 mdsize = get_le_quad(buf + labeloffset + 40 + i * 16 + 8);
429 break;
430 }
431 if (mdoffset == 0)
432 return;
433
434 if (get_buffer(section, mdoffset, mdsize, (void **)&buf) < mdsize)
435 return;
436
437 if (memcmp(buf + 4, " LVM2 x[5A%r0N*>", 16) != 0)
438 return;
439 mda_version = get_le_long(buf + 20);
440
441 print_line(level + 1, "Meta-data version %d", mda_version);
442
443 /* TODO: parse the metadata area (big task...) */
444
445 return;
446 }
447 }
448
449 /*
450 * Linux swap area
451 */
452
453 void detect_linux_swap(SECTION *section, int level)
454 {
455 int i, en, pagesize;
456 unsigned char *buf;
457 u4 version, pages;
458 char s[256];
459 int pagesizes[] = { 4096, 8192, 0 };
460
461 for (i = 0; pagesizes[i]; i++) {
462 pagesize = pagesizes[i];
463
464 if (get_buffer(section, pagesize - 512, 512, (void **)&buf) != 512)
465 break; /* assumes page sizes increase through the loop */
466
467 if (memcmp((char *)buf + 512 - 10, "SWAP-SPACE", 10) == 0) {
468 print_line(level, "Linux swap, version 1, %d KiB pages",
•469 pagesize >> 10);
470 }
471 if (memcmp((char *)buf + 512 - 10, "SWAPSPACE2", 10) == 0) {
472 if (get_buffer(section, 1024, 512, (void **)&buf) != 512)
•473 break; /* really shouldn't happen */
474
475 for (en = 0; en < 2; en++) {
•476 version = get_ve_long(en, buf);
477 if (version >= 1 && version < 10)
478 break;
479 }
480 if (en < 2) {
•481 print_line(level, "Linux swap, version 2, subversion %d, %d KiB pages, %s",
482 (int)version, pagesize >> 10, get_ve_name(en));
483 if (version == 1) {
484 pages = get_ve_long(en, buf + 4) - 1;
485 format_blocky_size(s, pages, pagesize, "pages", NULL);
486 print_line(level + 1, "Swap size %s", s);
487 }
488 } else {
•489 print_line(level, "Linux swap, version 2, illegal subversion, %d KiB pages",
490 pagesize >> 10);
491 }
492 }
493 }
494 }
495
496 /*
497 * various file systems
498 */
499
500 void detect_linux_misc(SECTION *section, int level)
501 {
502 int magic, fill, off, en;
503 unsigned char *buf;
504 char s[256];
505 u8 size, blocks, blocksize;
506
507 fill = get_buffer(section, 0, 2048, (void **)&buf);
508 if (fill < 512)
509 return;
510
511 /* minix file system */
512 if (fill >= 2048) {
513 int version = 0, namesize = 14;
514
515 magic = get_le_short(buf + 1024 + 16);
516 if (magic == 0x137F)
517 version = 1;
518 if (magic == 0x138F) {
519 version = 1;
520 namesize = 30;
521 }
522 if (magic == 0x2468)
523 version = 2;
524 if (magic == 0x2478) {
525 version = 2;
526 namesize = 30;
527 }
528 if (version) {
529 print_line(level, "Minix file system (v%d, %d chars)",
•530 version, namesize);
531 if (version == 1)
•532 blocks = get_le_short(buf + 1024 + 2);
533 else
•534 blocks = get_le_long(buf + 1024 + 20);
535 blocks = (blocks - get_le_short(buf + 1024 + 8))
•536 << get_le_short(buf + 1024 + 10);
537 format_blocky_size(s, blocks, 1024, "blocks", NULL);
538 print_line(level + 1, "Volume size %s", s);
539 }
540 }
541
542 /* Linux romfs */
543 if (memcmp(buf, "-rom1fs-", 8) == 0) {
544 size = get_be_long(buf + 8);
545 print_line(level, "Linux romfs");
546 print_line(level+1, "Volume name \"%.300s\"", (char *)(buf + 16));
547 format_size_verbose(s, size);
548 print_line(level+1, "Volume size %s", s);
549 }
550
551 /* Linux cramfs */
552 for (off = 0; off <= 512; off += 512) {
553 if (fill < off + 512)
554 break;
555 for (en = 0; en < 2; en++) {
556 if (get_ve_long(en, buf + off) == 0x28cd3d45) {
•557 print_line(level, "Linux cramfs, starts sector %d, %s",
558 off >> 9, get_ve_name(en));
559
•560 get_string(buf + off + 48, 16, s);
561 print_line(level + 1, "Volume name \"%s\"", s);
562
•563 size = get_ve_long(en, buf + off + 4);
564 blocks = get_ve_long(en, buf + off + 40);
565 format_size_verbose(s, size);
566 print_line(level + 1, "Compressed size %s", s);
567 format_blocky_size(s, blocks, 4096, "blocks", " -assumed-");
568 print_line(level + 1, "Data size %s", s);
569 }
570 }
571 }
572
573 /* Linux squashfs */
574 for (en = 0; en < 2; en++) {
575 if (get_ve_long(en, buf) == 0x73717368) {
576 int major, minor;
577
578 major = get_ve_short(en, buf + 28);
579 minor = get_ve_short(en, buf + 30);
580 print_line(level, "Linux squashfs, version %d.%d, %s",
•581 major, minor, get_ve_name(en));
582
583 if (major > 2)
•584 size = get_ve_quad(en, buf + 63);
585 else
•586 size = get_ve_long(en, buf + 8);
587 if (major > 1)
•588 blocksize = get_ve_long(en, buf + 51);
589 else
•590 blocksize = get_ve_short(en, buf + 32);
591
592 format_size_verbose(s, size);
593 print_line(level + 1, "Compressed size %s", s);
594 format_size(s, blocksize);
595 print_line(level + 1, "Block size %s", s);
596 }
597 }
598 }
599
600 /*
601 * various boot code signatures
602 */
603
604 void detect_linux_loader(SECTION *section, int level)
605 {
606 int fill, executable, id;
607 unsigned char *buf;
608
609 if (section->flags & FLAG_IN_DISKLABEL)
610 return;
611
612 fill = get_buffer(section, 0, 2048, (void **)&buf);
613 if (fill < 512)
614 return;
615
616 executable = (get_le_short(buf + 510) == 0xaa55) ? 1 : 0;
617
618 /* boot sector stuff */
619 if (executable && (memcmp(buf + 2, "LILO", 4) == 0 ||
•620 memcmp(buf + 6, "LILO", 4) == 0))
621 print_line(level, "LILO boot loader");
622 if (executable && memcmp(buf + 3, "SYSLINUX", 8) == 0)
623 print_line(level, "SYSLINUX boot loader");
624 if (fill >= 1024 && find_memory(buf, fill, "ISOLINUX", 8) >= 0)
625 print_line(level, "ISOLINUX boot loader");
626
627 /* we know GRUB a little better... */
628 if (executable &&
•629 find_memory(buf, 512, "Geom\0Hard Disk\0Read\0 Error", 26) >= 0) {
630 if (buf[0x3e] == 3) {
631 print_line(level, "GRUB boot loader, compat version %d.%d, boot drive 0x%02x",
•632 (int)buf[0x3e], (int)buf[0x3f], (int)buf[0x40]);
633 } else if (executable && buf[0x1bc] == 2 && buf[0x1bd] <= 2) {
634 id = buf[0x3e];
635 if (id == 0x10) {
•636 print_line(level, "GRUB boot loader, compat version %d.%d, normal version",
637 (int)buf[0x1bc], (int)buf[0x1bd]);
638 } else if (id == 0x20) {
•639 print_line(level, "GRUB boot loader, compat version %d.%d, LBA version",
640 (int)buf[0x1bc], (int)buf[0x1bd]);
641 } else {
•642 print_line(level, "GRUB boot loader, compat version %d.%d",
643 (int)buf[0x1bc], (int)buf[0x1bd]);
644 }
645 } else {
646 print_line(level, "GRUB boot loader, unknown compat version %d",
•647 buf[0x3e]);
648 }
649 }
650
651 /* Linux kernel loader */
652 if (fill >= 1024 && memcmp(buf + 512 + 2, "HdrS", 4) == 0) {
653 print_line(level, "Linux kernel build-in loader");
654 }
655
656 /* Debian install floppy splitter */
657 /* (not exactly boot code, but should be detected before gzip/tar) */
658 if (memcmp(buf, "Floppy split ", 13) == 0) {
659 char *name = (char *)buf + 32;
660 char *number = (char *)buf + 164;
661 char *total = (char *)buf + 172;
662 print_line(level, "Debian floppy split, name \"%s\", disk %s of %s",
•663 name, number, total);
664 }
665 }
666
667 /* EOF */