1 /*
2 
3     File: file_vdi.c
4 
5     Copyright (C) 2010 Christophe GRENIER <grenier@cgsecurity.org>
6 
7     This software is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write the Free Software Foundation, Inc., 51
19     Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29 #include <stdio.h>
30 #include "types.h"
31 #include "filegen.h"
32 #include "common.h"
33 
34 static void register_header_check_vdi(file_stat_t *file_stat);
35 
36 const file_hint_t file_hint_vdi= {
37   .extension="vdi",
38   .description="Virtual desktop infrastructure 1.1",
39   .max_filesize=PHOTOREC_MAX_FILE_SIZE,
40   .recover=1,
41   .enable_by_default=1,
42   .register_header_check=&register_header_check_vdi
43 };
44 
45 /* Image version. */
46 #define VDI_VERSION_1_1 0x00010001
47 
48 /* Image type. */
49 #define VDI_TYPE_DYNAMIC 1
50 #define VDI_TYPE_STATIC  2
51 
52 typedef unsigned char uuid_t[16];
53 
54 typedef struct {
55     char text[0x40];
56     uint32_t signature;
57     uint32_t version;
58     uint32_t header_size;
59     uint32_t image_type;
60     uint32_t image_flags;
61     char description[256];
62     uint32_t offset_bmap;
63     uint32_t offset_data;
64     uint32_t cylinders;         /* disk geometry, unused here */
65     uint32_t heads;             /* disk geometry, unused here */
66     uint32_t sectors;           /* disk geometry, unused here */
67     uint32_t sector_size;
68     uint32_t unused1;
69     uint64_t disk_size;
70     uint32_t block_size;
71     uint32_t block_extra;       /* unused here */
72     uint32_t blocks_in_image;
73     uint32_t blocks_allocated;
74     uuid_t uuid_image;
75     uuid_t uuid_last_snap;
76     uuid_t uuid_link;
77     uuid_t uuid_parent;
78     uint64_t unused2[7];
79 } VdiHeader;
80 
header_check_vdi(const unsigned char * buffer,const unsigned int buffer_size,const unsigned int safe_header_only,const file_recovery_t * file_recovery,file_recovery_t * file_recovery_new)81 static int header_check_vdi(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
82 {
83   const VdiHeader *header=(const VdiHeader *)buffer;
84   if(le32(header->version) == VDI_VERSION_1_1)
85   {
86     if(le32(header->offset_data) < sizeof(VdiHeader))
87       return 0;
88     reset_file_recovery(file_recovery_new);
89     file_recovery_new->extension=file_hint_vdi.extension;
90     if(le32(header->image_type) == VDI_TYPE_STATIC)
91     {
92       file_recovery_new->calculated_file_size=(uint64_t) le32(header->offset_data) + le32(header->blocks_in_image) * le32(header->block_size);
93       file_recovery_new->data_check=&data_check_size;
94       file_recovery_new->file_check=&file_check_size;
95     }
96     return 1;
97   }
98   return 0;
99 }
100 
register_header_check_vdi(file_stat_t * file_stat)101 static void register_header_check_vdi(file_stat_t *file_stat)
102 {
103   static const unsigned char vdi_header[4]= {0x7f, 0x10, 0xda, 0xbe};
104   register_header_check(0x40, vdi_header,sizeof(vdi_header), &header_check_vdi, file_stat);
105 }
106