Debian Codegraph @

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