Debian Codegraph @

unix.c

raw symbols SCIP
1 /*
2 * unix.c
3 * Detection of general Unix file systems
4 *
5 * Copyright (c) 2003 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 * JFS (for Linux)
32 */
33
34 void detect_jfs(SECTION *section, int level)
35 {
36 unsigned char *buf;
37 int version;
38 char s[256];
39 u4 blocksize;
40 u8 blockcount;
41
42 if (get_buffer(section, 32768, 512, (void **)&buf) < 512)
43 return;
44
45 /* check signature */
46 if (memcmp(buf, "JFS1", 4) != 0)
47 return;
48
49 /* tell the user */
50 version = get_le_long(buf + 4);
51 print_line(level, "JFS file system, version %d", version);
52
53 get_string(buf + 101, 11, s);
54 print_line(level + 1, "Volume name \"%s\"", s);
55
56 blocksize = get_le_long(buf + 24);
57 blockcount = get_le_quad(buf + 8);
58 format_blocky_size(s, blockcount, blocksize, "h/w blocks", NULL);
59 print_line(level + 1, "Volume size %s", s);
60 }
61
62 /*
63 * XFS
64 */
65
66 void detect_xfs(SECTION *section, int level)
67 {
68 unsigned char *buf;
69 u4 raw_version, blocksize;
70 u8 blockcount;
71 int version;
72 char s[256];
73
74 if (get_buffer(section, 0, 512, (void **)&buf) < 512)
75 return;
76
77 /* check signature */
78 if (get_be_long(buf) != 0x58465342)
79 return;
80
81 /* tell the user */
82 raw_version = get_be_short(buf + 0x64);
83 version = raw_version & 0x000f;
84 print_line(level, "XFS file system, version %d", version);
85
86 get_string(buf + 0x6c, 12, s);
87 print_line(level + 1, "Volume name \"%s\"", s);
88
89 format_uuid(buf + 32, s);
90 print_line(level + 1, "UUID %s", s);
91
92 blocksize = get_be_long(buf + 4);
93 blockcount = get_be_quad(buf + 8);
94 format_blocky_size(s, blockcount, blocksize, "blocks", NULL);
95 print_line(level + 1, "Volume size %s", s);
96 }
97
98 /*
99 * UFS file system from various BSD strains
100 */
101
102 void detect_ufs(SECTION *section, int level)
103 {
104 unsigned char *buf;
105 int i, at, en, namelen, offsets[5] = { 0, 8, 64, 256, -1 };
106 u4 magic;
107 char s[256];
108
109 /* NextStep/OpenStep apparently can move the superblock further into
110 the device. Linux uses steps of 8 blocks (of the applicable block
111 size) to scan for it. We only scan at the canonical location for now. */
112 /* Possible block sizes: 512 (old formats), 1024 (most), 2048 (CD media) */
113
114 for (i = 0; offsets[i] >= 0; i++) {
115 at = offsets[i];
116 if (get_buffer(section, at * 1024, 1536, (void **)&buf) < 1536)
117 break;
118
119 for (en = 0; en < 2; en++) {
120 magic = get_ve_long(en, buf + 1372);
121
122 if (magic == 0x00011954) {
123 print_line(level, "UFS file system, %d KiB offset, %s",
124 at, get_ve_name(en));
125 } else if (magic == 0x00095014) {
126 print_line(level, "UFS file system, %d KiB offset, long file names, %s",
127 at, get_ve_name(en));
128 } else if (magic == 0x00195612) {
129 print_line(level, "UFS file system, %d KiB offset, fs_featurebits, %s",
130 at, get_ve_name(en));
131 } else if (magic == 0x05231994) {
132 print_line(level, "UFS file system, %d KiB offset, fs_featurebits, >4GB support, %s",
133 at, get_ve_name(en));
134 } else if (magic == 0x19540119) {
135 print_line(level, "UFS2 file system, %d KiB offset, %s",
136 at, get_ve_name(en));
137 } else
138 continue;
139
140 /* volume name by FreeBSD convention */
141 get_string(buf + 680, 32, s);
142 if (s[0])
143 print_line(level + 1, "Volume name \"%s\" (in superblock)", s);
144
145 /* last mount point */
146 get_string(buf + 212, 255, s); /* actually longer, but varies */
147 if (s[0])
148 print_line(level + 1, "Last mounted at \"%s\"", s);
149
150 /* volume name by Darwin convention */
151 if (get_buffer(section, 7 * 1024, 1024, (void **)&buf) == 1024) {
152 if (get_ve_long(en, buf) == 0x4c41424c && /* "LABL" */
153 get_ve_long(en, buf + 8) == 1) { /* version 1 */
154 namelen = get_ve_short(en, buf + 16);
155 get_string(buf + 18, namelen, s); /* automatically limits to 255 */
156 print_line(level + 1, "Volume name \"%s\" (in label v%lu)",
157 s, get_ve_long(en, buf + 8));
158 }
159 }
160
161 return;
162 }
163 }
164 }
165
166 /*
167 * System V file system
168 */
169
170 void detect_sysv(SECTION *section, int level)
171 {
172 unsigned char *buf;
173 int i, at, en, offsets[5] = { 512, 1024, -1 };
174 s4 blocksize_code;
175 char s[256];
176
177 for (i = 0; offsets[i] >= 0; i++) {
178 at = offsets[i];
179 if (get_buffer(section, at, 1024, (void **)&buf) < 1024)
180 break;
181
182 for (en = 0; en < 2; en++) {
183 if (get_ve_long(en, buf + 1016) == 0x2b5544) {
184 blocksize_code = get_ve_long(en, buf + 1020);
185 s[0] = 0;
186 if (blocksize_code == 1)
187 strcpy(s, "512 byte blocks");
188 else if (blocksize_code == 2)
189 strcpy(s, "1 KiB blocks");
190 else if (blocksize_code == 3)
191 strcpy(s, "2 KiB blocks");
192 else
193 snprintf(s, 255, "unknown block size code %d", (int)blocksize_code);
194
195 print_line(level, "XENIX file system (SysV variant), %s, %s",
196 get_ve_name(en), s);
197 return;
198 }
199
200 if (get_ve_long(en, buf + 504) == 0xfd187e20) {
201 blocksize_code = get_ve_long(en, buf + 508);
202 s[0] = 0;
203 if (blocksize_code == 1)
204 strcpy(s, "512 byte blocks");
205 else if (blocksize_code == 2)
206 strcpy(s, "1 KiB blocks");
207 else
208 snprintf(s, 255, "unknown block size code %d", (int)blocksize_code);
209
210 print_line(level, "SysV file system, %s, %s",
211 get_ve_name(en), s);
212 return;
213 }
214 }
215 }
216 }
217
218 /*
219 * BSD disklabel
220 */
221
222 static char * bsdtype_names[] = {
223 "Unused",
224 "swap",
225 "Sixth Edition",
226 "Seventh Edition",
227 "System V",
228 "V7 with 1 KiB blocks",
229 "Eighth Edition, 4 KiB blocks",
230 "4.2BSD fast file system",
231 "ext2 or MS-DOS",
232 "4.4BSD log-structured file system",
233 "\"Other\"",
234 "HPFS",
235 "ISO9660",
236 "bootstrap",
237 "AmigaDOS fast file system",
238 "Macintosh HFS",
239 "Digital Unix AdvFS",
240 };
241
242 static char * get_name_for_bsdtype(int type)
243 {
244 if (type >= 0 && type <= 16)
245 return bsdtype_names[type];
246 return "Unknown";
247 }
248
249 void detect_bsd_disklabel(SECTION *section, int level)
250 {
251 unsigned char *buf;
252 int i, off, partcount, types[16], min_offset_valid, did_recurse;
253 u4 starts[16], sizes[16];
254 u4 sectsize, nsectors, ntracks, ncylinders, secpercyl, secperunit;
255 u8 offset, min_offset, base_offset;
256 char s[256], append[64], pn;
257
258 if (section->flags & FLAG_IN_DISKLABEL)
259 return;
260
261 if (get_buffer(section, 512, 512, (void **)&buf) < 512)
262 return;
263
264 if (get_le_long(buf) != 0x82564557 ||
265 get_le_long(buf + 132) != 0x82564557)
266 return;
267
268 sectsize = get_le_long(buf + 40);
269 nsectors = get_le_long(buf + 44);
270 ntracks = get_le_long(buf + 48);
271 ncylinders = get_le_long(buf + 52);
272 secpercyl = get_le_long(buf + 56);
273 secperunit = get_le_long(buf + 60);
274
275 partcount = get_le_short(buf + 138);
276
277 if (partcount <= 8) {
278 print_line(level, "BSD disklabel (at sector 1), %d partitions", partcount);
279 } else if (partcount > 8 && partcount <= 16) {
280 print_line(level, "BSD disklabel (at sector 1), %d partitions (more than usual, but valid)",
281 partcount);
282 } else if (partcount > 16) {
283 print_line(level, "BSD disklabel (at sector 1), %d partitions (broken, limiting to 16)",
284 partcount);
285 partcount = 16;
286 }
287 if (sectsize != 512) {
288 print_line(level + 1, "Unusual sector size %d bytes, your mileage may vary");
289 }
290
291 min_offset = 0;
292 min_offset_valid = 0;
293 for (i = 0, off = 148; i < partcount; i++, off += 16) {
294 starts[i] = get_le_long(buf + off + 4);
295 sizes[i] = get_le_long(buf + off);
296 types[i] = buf[off + 12];
297
298 if (types[i] != 0 || i == 2) {
299 offset = (u8)starts[i] * 512;
300 if (!min_offset_valid || offset < min_offset) {
301 min_offset = offset;
302 min_offset_valid = 1;
303 }
304 }
305 }
306 /* if min_offset_valid is still 0, the default of min_offset=0 is okay */
307
308 if (section->pos == min_offset) {
309 /* either min_offset is zero, or we were analyzing the whole disk */
310 base_offset = section->pos;
311 } else if (section->pos == 0) {
312 /* are we analyzing the slice alone? */
313 print_line(level + 1, "Adjusting offsets for disklabel in a DOS partition at sector %llu", min_offset >> 9);
314 base_offset = min_offset;
315 } else if (min_offset == 0) {
316 /* assume relative offsets after all */
317 base_offset = 0;
318 } else {
319 print_line(level + 1, "Warning: Unable to adjust offsets, your mileage may vary");
320 base_offset = section->pos;
321 }
322
323 /* loop over partitions: print and analyze */
324 did_recurse = 0;
325 for (i = 0; i < partcount; i++) {
326 pn = 'a' + i;
327 if (types[i] == 0 && i != 2)
328 continue;
329
330 sprintf(append, " from %lu", starts[i]);
331 format_blocky_size(s, sizes[i], 512, "sectors", append);
332 print_line(level, "Partition %c: %s",
333 pn, s);
334
335 print_line(level + 1, "Type %d (%s)",
336 types[i], get_name_for_bsdtype(types[i]));
337
338 if (types[i] == 0 || sizes[i] == 0)
339 continue;
340
341 offset = (u8)starts[i] * 512;
342 if (offset < base_offset) {
343 print_line(level + 1, "(Illegal start offset, no detection)");
344 } else if (offset == base_offset) {
345 print_line(level + 1, "Includes the disklabel and boot code");
346
347 /* recurse for content detection, but carefully */
348 analyze_recursive(section, level + 1,
349 offset - base_offset, (u8)sizes[i] * 512,
350 FLAG_IN_DISKLABEL);
351 did_recurse = 1;
352 } else {
353 /* recurse for content detection */
354 analyze_recursive(section, level + 1,
355 offset - base_offset, (u8)sizes[i] * 512,
356 0);
357 }
358 }
359
360 if (did_recurse)
361 stop_detect(); /* don't run other detectors; we already did that
362 for an overlapping partition. */
363 }
364
365 /*
366 * FreeBSD boot loader (?)
367 */
368
369 void detect_bsd_loader(SECTION *section, int level)
370 {
371 unsigned char *buf;
372
373 if (section->flags & FLAG_IN_DISKLABEL)
374 return;
375
376 if (get_buffer(section, 0, 512, (void **)&buf) == 512) {
377 if (get_le_short(buf + 0x1b0) == 0xbb66) {
378 print_line(level, "FreeBSD boot manager (i386 boot0 at sector 0)");
379 } else if (get_le_long(buf + 0x1f6) == 0 &&
380 get_le_long(buf + 0x1fa) == 50000 &&
381 get_le_short(buf + 0x1fe) == 0xaa55) {
382 print_line(level, "FreeBSD boot loader (i386 boot1 at sector 0)");
383 }
384 }
385
386 if (get_buffer(section, 1024, 512, (void **)&buf) == 512) {
387 if (memcmp(buf + 2, "BTX", 3) == 0) {
388 print_line(level, "FreeBSD boot loader (i386 boot2/BTX %d.%02d at sector 2)",
389 (int)buf[5], (int)buf[6]);
390 }
391 }
392 }
393
394 /*
395 * Solaris SPARC disklabel
396 */
397
398 void detect_solaris_disklabel(SECTION *section, int level)
399 {
400 unsigned char *buf;
401 int i, off1, off2, types[8], did_recurse;
402 u4 sizes[8];
403 u8 starts[8], cylsize, offset;
404 char s[256], append[256], pn;
405
406 if (section->flags & FLAG_IN_DISKLABEL)
407 return;
408
409 if (get_buffer(section, 0, 512, (void **)&buf) < 512)
410 return;
411
412 if (get_be_short(buf + 508) != 0xDABE)
413 return;
414
415 print_line(level, "Solaris SPARC disklabel");
416
417 cylsize = (u8)get_be_short(buf + 436) * (u8)get_be_short(buf + 438);
418 for (i = 0, off1 = 142, off2 = 444; i < 8; i++, off1 += 4, off2 += 8) {
419 types[i] = get_be_short(buf + off1);
420 starts[i] = get_be_long(buf + off2) * cylsize;
421 sizes[i] = get_be_long(buf + off2 + 4);
422 }
423
424 /* loop over partitions: print and analyze */
425 did_recurse = 0;
426 for (i = 0; i < 8; i++) {
427 pn = '0' + i;
428 if (sizes[i] == 0)
429 continue;
430
431 sprintf(append, " from %llu", starts[i]);
432 format_blocky_size(s, sizes[i], 512, "sectors", append);
433 print_line(level, "Partition %c: %s", pn, s);
434
435 print_line(level + 1, "Type %d",
436 types[i]);
437
438 offset = starts[i] * 512;
439 if (offset == 0) {
440 print_line(level + 1, "Includes the disklabel");
441
442 /* recurse for content detection, but carefully */
443 analyze_recursive(section, level + 1,
444 offset, (u8)sizes[i] * 512,
445 FLAG_IN_DISKLABEL);
446 did_recurse = 1;
447 } else {
448 /* recurse for content detection */
449 analyze_recursive(section, level + 1,
450 offset, (u8)sizes[i] * 512,
451 0);
452 }
453 }
454
455 if (did_recurse)
456 stop_detect(); /* don't run other detectors; we already did that
457 for the first partition, which overlaps with
458 the disklabel itself. */
459 }
460
461 /*
462 * Solaris x86 vtoc
463 */
464
465 static char * vtoctype_names[] = {
466 "Unused",
467 "Boot",
468 "Root",
469 "Swap",
470 "Usr",
471 "Overlap",
472 "Stand",
473 "Var",
474 "Home",
475 "Alternate sector",
476 "Cache"
477 };
478
479 static char * get_name_for_vtoctype(int type)
480 {
481 if (type >= 0 && type <= 10)
482 return vtoctype_names[type];
483 return "Unknown";
484 }
485
486 void detect_solaris_vtoc(SECTION *section, int level)
487 {
488 unsigned char *buf;
489 int i, off, partcount, sectorsize, types[16], did_recurse;
490 u4 starts[16], sizes[16];
491 u4 version;
492 u8 offset;
493 char s[256], append[64];
494
495 if (section->flags & FLAG_IN_DISKLABEL)
496 return;
497
498 if (get_buffer(section, 512, 512, (void **)&buf) < 512)
499 return;
500
501 if (get_le_long(buf + 12) != 0x600DDEEE)
502 return;
503 version = get_le_long(buf + 16);
504 if (version != 1) {
505 print_line(level, "Solaris x86 disklabel, unknown version %lu", version);
506 return;
507 }
508 partcount = get_le_short(buf + 30);
509 if (partcount > 16) {
510 print_line(level, "Solaris x86 disklabel, version 1, %d partitions (limiting to 16)",
511 partcount);
512 partcount = 16;
513 } else {
514 print_line(level, "Solaris x86 disklabel, version 1, %d partitions",
515 partcount);
516 }
517
518 sectorsize = get_le_short(buf + 28);
519 if (sectorsize != 512)
520 print_line(level + 1, "Unusual sector size %d bytes, your mileage may vary",
521 sectorsize);
522
523 get_string(buf + 20, 8, s);
524 if (s[0])
525 print_line(level + 1, "Volume name \"%s\"", s);
526
527 for (i = 0, off = 72; i < partcount; i++, off += 12) {
528 types[i] = get_le_short(buf + off);
529 starts[i] = get_le_long(buf + off + 4);
530 sizes[i] = get_le_long(buf + off + 8);
531 }
532
533 /* loop over partitions: print and analyze */
534 did_recurse = 0;
535 for (i = 0; i < partcount; i++) {
536 if (sizes[i] == 0)
537 continue;
538
539 sprintf(append, " from %lu", starts[i]);
540 format_blocky_size(s, sizes[i], 512, "sectors", append);
541 print_line(level, "Partition %d: %s",
542 i, s);
543
544 print_line(level + 1, "Type %d (%s)",
545 types[i], get_name_for_vtoctype(types[i]));
546
547 offset = (u8)starts[i] * 512;
548 if (offset == 0) {
549 print_line(level + 1, "Includes the disklabel");
550
551 /* recurse for content detection, but carefully */
552 analyze_recursive(section, level + 1,
553 offset, (u8)sizes[i] * 512,
554 FLAG_IN_DISKLABEL);
555 did_recurse = 1;
556 } else {
557 /* recurse for content detection */
558 analyze_recursive(section, level + 1,
559 offset, (u8)sizes[i] * 512,
560 0);
561 }
562 }
563
564 if (did_recurse)
565 stop_detect(); /* don't run other detectors; we already did that
566 for an overlapping partition. */
567 }
568
569 /*
570 * QNX4 file system
571 */
572
573 void detect_qnx(SECTION *section, int level)
574 {
575 unsigned char *buf;
576
577 if (get_buffer(section, 512, 512, (void **)&buf) < 512)
578 return;
579
580 /* check signature */
581 if (get_le_long(buf) != 0x0000002f)
582 return;
583 /* NOTE: This is actually the string "/", the file name of the root
584 directory. QNX4 fs does not have a real superblock, just an
585 aggregate of 4 inodes for certain special files. */
586
587 /* tell the user */
588 print_line(level, "QNX4 file system");
589 }
590
591 /*
592 * Veritas VxFS
593 */
594
595 void detect_vxfs(SECTION *section, int level)
596 {
597 unsigned char *buf;
598 int en, version;
599 u4 blocksize, blockcount;
600 char s[256];
601
602 if (get_buffer(section, 1024, 1024, (void **)&buf) < 1024)
603 return;
604
605 /* check signature */
606 for (en = 0; en < 2; en++) {
607 if (get_ve_long(en, buf) == 0xA501FCF5) {
608 version = get_ve_long(en, buf + 4);
609 print_line(level, "Veritas VxFS file system, version %d, %s",
610 version, get_ve_name(en));
611
612 blocksize = get_ve_long(en, buf + 32);
613 blockcount = get_ve_long(en, buf + 36);
614 format_blocky_size(s, blockcount, blocksize, "blocks", NULL);
615 print_line(level + 1, "Volume size %s", s);
616 }
617 }
618 }
619
620 /* EOF */