1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
/*
File: partnone.c
Copyright (C) 1998-2009 Christophe GRENIER <grenier@cgsecurity.org>
This software is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write the Free Software Foundation, Inc., 51
Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <ctype.h> /* tolower */
#include "types.h"
#include "common.h"
#include "fnctdsk.h"
#if !defined(DISABLED_FOR_FRAMAC)
#include "analyse.h"
#endif
#include "lang.h"
#include "intrf.h"
#include "fat_common.h"
#if !defined(DISABLED_FOR_FRAMAC)
#include "apfs.h"
#include "bfs.h"
#include "bsd.h"
#include "btrfs.h"
#include "cramfs.h"
#include "exfat.h"
#include "ext2.h"
#include "fat.h"
#include "fatx.h"
#include "f2fs_fs.h"
#include "f2fs.h"
#include "iso9660.h"
#include "iso.h"
#include "gfs2.h"
#include "hfs.h"
#include "hfsp.h"
#include "hpfs.h"
#include "jfs_superblock.h"
#include "jfs.h"
#include "luks.h"
#include "lvm.h"
#include "md.h"
#include "netware.h"
#include "ntfs.h"
#include "refs.h"
#include "rfs.h"
#include "sun.h"
#include "sysv.h"
#include "swap.h"
#include "ufs.h"
#include "xfs.h"
#include "vmfs.h"
#include "wbfs.h"
#include "zfs.h"
#endif
#include "log.h"
/*@
@ requires \valid(disk_car);
@ requires \valid(partition);
@*/
static int check_part_none(disk_t *disk_car, const int verbose,partition_t *partition,const int saveheader);
/*@
@ requires \valid_read(buffer + (0 .. 0x200-1));
@ requires \valid(geometry);
@ requires \separated(buffer + (0 .. 0x200-1), geometry);
@ assigns geometry->sectors_per_head, geometry->heads_per_cylinder, geometry->bytes_per_sector;
@*/
static int get_geometry_from_nonembr(const unsigned char *buffer, const int verbose, CHSgeometry_t *geometry);
/*@
@ requires \valid(disk_car);
@ requires valid_disk(disk_car);
@ ensures valid_list_part(\result);
@*/
static list_part_t *read_part_none(disk_t *disk_car, const int verbose, const int saveheader);
/*@
@ requires \valid_read(disk_car);
@ assigns \nothing;
@*/
static list_part_t *init_part_order_none(const disk_t *disk_car, list_part_t *list_part);
/*@
@ requires \valid_read(disk_car);
@ assigns \nothing;
@*/
static void set_next_status_none(const disk_t *disk_car, partition_t *partition);
/*@
@ requires list_part == \null || \valid_read(list_part);
@ assigns \nothing;
@*/
static int test_structure_none(const list_part_t *list_part);
/*@
@ requires \valid(partition);
@ assigns partition->upart_type;
@*/
static int set_part_type_none(partition_t *partition, unsigned int part_type);
/*@
@ requires \valid_read(partition);
@ assigns \nothing;
@*/
static int is_part_known_none(const partition_t *partition);
/*@
@ requires \valid_read(disk_car);
@ requires list_part==\null || \valid(list_part);
@*/
static void init_structure_none(const disk_t *disk_car,list_part_t *list_part, const int verbose);
/*@
@ requires \valid_read(partition);
@ assigns \nothing;
@*/
static unsigned int get_part_type_none(const partition_t *partition);
/*@
@ requires \valid_read(partition);
@ assigns \nothing;
@*/
static const char *get_partition_typename_none(const partition_t *partition);
static const struct systypes none_sys_types[] = {
{UP_APFS, "APFS"},
{UP_BEOS, "BeFS"},
{UP_BTRFS, "btrfs"},
{UP_CRAMFS, "CramFS"},
{UP_EXT2, "ext2"},
{UP_EXT3, "ext3"},
{UP_EXT4, "ext4"},
/* {UP_EXTENDED, "Extended"}, */
{UP_EXFAT, "exFAT"},
{UP_FAT12, "FAT12"},
{UP_FAT16, "FAT16"},
{UP_FAT32, "FAT32"},
{UP_FREEBSD, "FreeBSD"},
{UP_F2FS, "f2fs"},
{UP_GFS2, "GFS2"},
{UP_HFS, "HFS"},
{UP_HFSP, "HFS+"},
{UP_HFSX, "HFSX"},
{UP_HPFS, "HPFS"},
{UP_ISO, "ISO"},
{UP_JFS, "JFS"},
{UP_LINSWAP, "Linux SWAP"},
{UP_LINSWAP2, "Linux SWAP 2"},
{UP_LINSWAP_8K, "Linux SWAP"},
{UP_LINSWAP2_8K, "Linux SWAP 2"},
{UP_LINSWAP2_8KBE, "Linux SWAP 2"},
{UP_LUKS, "Linux LUKS"},
{UP_LVM, "Linux LVM"},
{UP_LVM2, "Linux LVM2"},
{UP_MD, "Linux md 0.9 RAID"},
{UP_MD1, "Linux md 1.x RAID"},
{UP_NETWARE, "Netware"},
{UP_NTFS, "NTFS"},
{UP_OPENBSD, "OpenBSD"},
{UP_OS2MB, "OS2 Multiboot"},
{UP_ReFS, "ReFS"},
{UP_RFS, "ReiserFS 3.5"},
{UP_RFS2, "ReiserFS 3.6"},
{UP_RFS3, "ReiserFS 3.x"},
{UP_RFS4, "ReiserFS 4"},
{UP_SUN, "Sun"},
{UP_SYSV4, "SysV 4"},
{UP_UFS, "UFS"},
{UP_UFS2, "UFS 2"},
{UP_UFS_LE, "UFS - Little Endian"},
{UP_UFS2_LE, "UFS 2 - Little Endian"},
{UP_UNK, "Unknown"},
{UP_VMFS, "VMFS"},
{UP_WBFS, "WBFS"},
{UP_XFS, "XFS"},
{UP_XFS2, "XFS 2"},
{UP_XFS3, "XFS 3"},
{UP_XFS4, "XFS 4"},
{UP_XFS5, "XFS 5"},
{UP_ZFS, "ZFS"},
{ 0,NULL }
};
arch_fnct_t arch_none=
{
.part_name="None",
.part_name_option="partition_none",
.msg_part_type=NULL,
.read_part=&read_part_none,
.write_part=NULL,
.init_part_order=&init_part_order_none,
.get_geometry_from_mbr=&get_geometry_from_nonembr,
.check_part=&check_part_none,
.write_MBR_code=NULL,
.set_prev_status=&set_next_status_none,
.set_next_status=&set_next_status_none,
.test_structure=&test_structure_none,
.get_part_type=&get_part_type_none,
.set_part_type=&set_part_type_none,
.init_structure=&init_structure_none,
.erase_list_part=NULL,
.get_partition_typename=&get_partition_typename_none,
.is_part_known=&is_part_known_none
};
static unsigned int get_part_type_none(const partition_t *partition)
{
return partition->upart_type;
}
static int get_geometry_from_nonembr(const unsigned char *buffer, const int verbose, CHSgeometry_t *geometry)
{
{
/* Ugly hack to get geometry from FAT and NTFS */
const struct fat_boot_sector *fat_header=(const struct fat_boot_sector *)buffer;
/*@ assert \valid_read(fat_header); */
if(le16(fat_header->marker)==0xAA55)
{
if(le16(fat_header->secs_track)>0 && le16(fat_header->secs_track)<=63 &&
le16(fat_header->heads)>0 && le16(fat_header->heads)<=255 &&
fat_sector_size(fat_header)>0 && fat_sector_size(fat_header)%512==0)
{
geometry->sectors_per_head=le16(fat_header->secs_track);
geometry->heads_per_cylinder=le16(fat_header->heads);
geometry->bytes_per_sector=fat_sector_size(fat_header);
}
}
}
return 0;
}
static list_part_t *read_part_none(disk_t *disk, const int verbose, const int saveheader)
{
int insert_error=0;
unsigned char *buffer_disk;
list_part_t *list_part;
partition_t *partition;
int res=0;
partition=partition_new(&arch_none);
buffer_disk=(unsigned char *)MALLOC(16*DEFAULT_SECTOR_SIZE);
partition->part_size=disk->disk_size;
#if !defined(DISABLED_FOR_FRAMAC)
if(recover_MD_from_partition(disk, partition, verbose)==0)
res=1;
else
partition_reset(partition,&arch_none);
if(res<=0)
{
if(disk->pread(disk, buffer_disk, 16 * DEFAULT_SECTOR_SIZE, partition->part_offset) == 16 * DEFAULT_SECTOR_SIZE)
res=search_type_2(buffer_disk, disk, partition,verbose,0);
}
if(res<=0)
{
res=search_type_1(buffer_disk, disk, partition,verbose,0);
}
if(res<=0)
{
res=search_type_0(buffer_disk, disk, partition,verbose,0);
}
if(res<=0)
{
res=search_type_8(buffer_disk, disk, partition,verbose,0);
}
if(res<=0)
{
if(disk->pread(disk, buffer_disk, 3 * DEFAULT_SECTOR_SIZE, partition->part_offset + 16 * 512) == 3 * DEFAULT_SECTOR_SIZE)
res=search_type_16(buffer_disk, disk, partition,verbose,0);
}
if(res<=0)
{
if(disk->pread(disk, buffer_disk, 3 * DEFAULT_SECTOR_SIZE, partition->part_offset + 63 * 512) == 3 * DEFAULT_SECTOR_SIZE)
res=search_type_64(buffer_disk, disk, partition,verbose,0);
}
if(res<=0)
res=(recover_ISO((const struct iso_primary_descriptor*)(buffer_disk+0x200), partition)==0);
if(res<=0)
{
/* 64k offset */
if(disk->pread(disk, buffer_disk, 11 * DEFAULT_SECTOR_SIZE, partition->part_offset + 126 * 512) == 11 * DEFAULT_SECTOR_SIZE)
res=search_type_128(buffer_disk, disk, partition,verbose,0);
}
if(res<=0)
{
res=search_type_2048(buffer_disk, disk, partition,verbose,0);
}
if(res<=0)
{ /* Search FAT32 backup */
partition->part_offset = 6*512;
res=search_FAT_backup(buffer_disk, disk, partition, verbose, 0);
}
if(res<=0)
{ /* Search exFAT backup */
partition->part_offset = 12 * disk->sector_size;
res=search_exFAT_backup(buffer_disk, disk, partition);
}
if(res<=0)
{ /* Search NTFS backup */
if(disk->disk_size > disk->sector_size)
{
partition->part_offset = disk->disk_size - disk->sector_size;
res=search_NTFS_backup(buffer_disk, disk, partition, verbose, 0);
if(res>0 && partition->part_offset!=0)
res=0;
}
}
if(res<=0)
{
int s_log_block_size;
for(s_log_block_size=0; s_log_block_size<=2 && res<=0; s_log_block_size++)
{
/* sparse superblock feature: The groups chosen are 0, 1 and powers of 3, 5 and 7. */
/* Checking group 3 */
const uint64_t hd_offset=3*(EXT2_MIN_BLOCK_SIZE<<s_log_block_size)*8*(EXT2_MIN_BLOCK_SIZE<<s_log_block_size)+(s_log_block_size==0?2*DEFAULT_SECTOR_SIZE:0);
if(disk->pread(disk, buffer_disk, 1024, hd_offset)==1024)
{
const struct ext2_super_block *sb=(const struct ext2_super_block*)buffer_disk;
partition->part_offset = hd_offset;
if(le16(sb->s_block_group_nr)>0 &&
le16(sb->s_magic)==EXT2_SUPER_MAGIC &&
recover_EXT2(disk, sb, partition, 0, 0)==0)
res=1;
if(res>0 && partition->part_offset!=0)
res=0;
}
}
}
#endif
free(buffer_disk);
if(res<=0)
partition_reset(partition,&arch_none);
partition->part_offset=0;
partition->part_size=disk->disk_size;
partition->order=NO_ORDER;
partition->status=STATUS_PRIM;
screen_buffer_reset();
check_part_none(disk, verbose,partition,saveheader);
#ifndef DISABLED_FOR_FRAMAC
aff_part_buffer(AFF_PART_ORDER|AFF_PART_STATUS,disk, partition);
#endif
list_part=insert_new_partition(NULL, partition, 0, &insert_error);
/*@ assert valid_list_part(list_part); */
if(insert_error>0)
free(partition);
return list_part;
}
static list_part_t *init_part_order_none(const disk_t *disk_car, list_part_t *list_part)
{
return list_part;
}
static void set_next_status_none(const disk_t *disk_car, partition_t *partition)
{
}
static int test_structure_none(const list_part_t *list_part)
{
return 0;
}
static int set_part_type_none(partition_t *partition, unsigned int part_type)
{
partition->upart_type=(upart_type_t)part_type;
return 0;
}
static int is_part_known_none(const partition_t *partition)
{
return 1;
}
static void init_structure_none(const disk_t *disk_car,list_part_t *list_part, const int verbose)
{
list_part_t *element;
for(element=list_part;element!=NULL;element=element->next)
{
/*@ assert \valid_read(element); */
/*@ assert \valid(element->part); */
element->part->status=STATUS_PRIM;
}
}
static int check_part_none(disk_t *disk_car,const int verbose,partition_t *partition, const int saveheader)
{
int ret=0;
#if !defined(DISABLED_FOR_FRAMAC)
switch(partition->upart_type)
{
case UP_APFS:
ret=check_APFS(disk_car, partition);
break;
case UP_BEOS:
ret=check_BeFS(disk_car,partition);
break;
case UP_BTRFS:
ret=check_btrfs(disk_car, partition);
break;
case UP_CRAMFS:
ret=check_cramfs(disk_car,partition,verbose);
break;
case UP_EXT2:
case UP_EXT3:
case UP_EXT4:
ret=check_EXT2(disk_car,partition,verbose);
break;
case UP_EXTENDED:
break;
case UP_EXFAT:
ret=check_exFAT(disk_car, partition);
break;
case UP_FAT12:
case UP_FAT16:
case UP_FAT32:
ret=check_FAT(disk_car,partition,verbose);
break;
case UP_FATX:
ret=check_FATX(disk_car, partition);
break;
case UP_FREEBSD:
ret=check_BSD(disk_car,partition,verbose,BSD_MAXPARTITIONS);
break;
case UP_F2FS:
ret=check_f2fs(disk_car, partition);
break;
case UP_GFS2:
ret=check_gfs2(disk_car, partition);
break;
case UP_HFS:
ret=check_HFS(disk_car,partition,verbose);
break;
case UP_HFSP:
case UP_HFSX:
ret=check_HFSP(disk_car,partition,verbose);
break;
case UP_HPFS:
ret=check_HPFS(disk_car,partition,verbose);
break;
case UP_ISO:
ret=check_ISO(disk_car, partition);
break;
case UP_JFS:
ret=check_JFS(disk_car, partition);
break;
case UP_LINSWAP:
case UP_LINSWAP2:
case UP_LINSWAP_8K:
case UP_LINSWAP2_8K:
case UP_LINSWAP2_8KBE:
ret=check_Linux_SWAP(disk_car, partition);
break;
case UP_LUKS:
ret=check_LUKS(disk_car, partition);
break;
case UP_LVM:
ret=check_LVM(disk_car,partition,verbose);
break;
case UP_LVM2:
ret=check_LVM2(disk_car,partition,verbose);
break;
case UP_NETWARE:
ret=check_netware(disk_car, partition);
break;
case UP_NTFS:
ret=check_NTFS(disk_car,partition,verbose,0);
if(ret!=0)
{ screen_buffer_add("Invalid NTFS boot\n"); }
break;
case UP_OPENBSD:
ret=check_BSD(disk_car,partition,verbose,OPENBSD_MAXPARTITIONS);
break;
case UP_OS2MB:
ret=check_OS2MB(disk_car,partition,verbose);
break;
case UP_MD:
case UP_MD1:
ret=check_MD(disk_car,partition,verbose);
if(ret!=0)
{ screen_buffer_add("Invalid RAID superblock\n"); }
break;
case UP_ReFS:
ret=check_ReFS(disk_car, partition);
break;
case UP_RFS:
case UP_RFS2:
case UP_RFS3:
case UP_RFS4:
ret=check_rfs(disk_car,partition,verbose);
break;
case UP_SUN:
ret=check_sun_i386(disk_car,partition,verbose);
break;
case UP_SYSV4:
ret=check_sysv(disk_car,partition,verbose);
break;
case UP_UFS:
case UP_UFS2:
case UP_UFS_LE:
case UP_UFS2_LE:
ret=check_ufs(disk_car,partition,verbose);
break;
case UP_VMFS:
ret=check_VMFS(disk_car, partition);
break;
case UP_WBFS:
ret=check_WBFS(disk_car, partition);
break;
case UP_XFS:
case UP_XFS2:
case UP_XFS3:
case UP_XFS4:
case UP_XFS5:
ret=check_xfs(disk_car,partition,verbose);
break;
case UP_ZFS:
ret=check_ZFS(disk_car, partition);
break;
case UP_UNK:
break;
}
#endif
return ret;
}
/*@
@ assigns \nothing;
@*/
static const char *get_partition_typename_none_aux(const unsigned int part_type_none)
{
unsigned int i;
/*@
@ loop assigns i;
@ loop variant sizeof(none_sys_types)/sizeof(struct systypes) - i;
@*/
for (i=0; none_sys_types[i].name!=NULL; i++)
{
if (none_sys_types[i].part_type == part_type_none)
return none_sys_types[i].name;
}
return NULL;
}
/*@
@ requires \valid_read(partition);
@ assigns \nothing;
@*/
static const char *get_partition_typename_none(const partition_t *partition)
{
return get_partition_typename_none_aux(partition->upart_type);
}
|