/src/libbde/libbde/libbde_metadata_block_header.c
Line | Count | Source |
1 | | /* |
2 | | * Metadata block header functions |
3 | | * |
4 | | * Copyright (C) 2011-2025, Joachim Metz <joachim.metz@gmail.com> |
5 | | * |
6 | | * Refer to AUTHORS for acknowledgements. |
7 | | * |
8 | | * This program is free software: you can redistribute it and/or modify |
9 | | * it under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation, either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include <common.h> |
23 | | #include <byte_stream.h> |
24 | | #include <memory.h> |
25 | | #include <types.h> |
26 | | |
27 | | #include "libbde_io_handle.h" |
28 | | #include "libbde_libbfio.h" |
29 | | #include "libbde_libcerror.h" |
30 | | #include "libbde_libcnotify.h" |
31 | | #include "libbde_metadata_block_header.h" |
32 | | |
33 | | #include "bde_metadata.h" |
34 | | |
35 | | /* Creates a metadata block header |
36 | | * Make sure the value metadata block header is referencing, is set to NULL |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | int libbde_metadata_block_header_initialize( |
40 | | libbde_metadata_block_header_t **metadata_block_header, |
41 | | libcerror_error_t **error ) |
42 | 4.09k | { |
43 | 4.09k | static char *function = "libbde_metadata_block_header_initialize"; |
44 | | |
45 | 4.09k | if( metadata_block_header == NULL ) |
46 | 0 | { |
47 | 0 | libcerror_error_set( |
48 | 0 | error, |
49 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
50 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
51 | 0 | "%s: invalid metadata block header.", |
52 | 0 | function ); |
53 | |
|
54 | 0 | return( -1 ); |
55 | 0 | } |
56 | 4.09k | if( *metadata_block_header != NULL ) |
57 | 0 | { |
58 | 0 | libcerror_error_set( |
59 | 0 | error, |
60 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
61 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
62 | 0 | "%s: invalid metadata block header value already set.", |
63 | 0 | function ); |
64 | |
|
65 | 0 | return( -1 ); |
66 | 0 | } |
67 | 4.09k | *metadata_block_header = memory_allocate_structure( |
68 | 4.09k | libbde_metadata_block_header_t ); |
69 | | |
70 | 4.09k | if( *metadata_block_header == NULL ) |
71 | 0 | { |
72 | 0 | libcerror_error_set( |
73 | 0 | error, |
74 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
75 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
76 | 0 | "%s: unable to create metadata block header.", |
77 | 0 | function ); |
78 | |
|
79 | 0 | goto on_error; |
80 | 0 | } |
81 | 4.09k | if( memory_set( |
82 | 4.09k | *metadata_block_header, |
83 | 4.09k | 0, |
84 | 4.09k | sizeof( libbde_metadata_block_header_t ) ) == NULL ) |
85 | 0 | { |
86 | 0 | libcerror_error_set( |
87 | 0 | error, |
88 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
89 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
90 | 0 | "%s: unable to clear metadata block header.", |
91 | 0 | function ); |
92 | |
|
93 | 0 | goto on_error; |
94 | 0 | } |
95 | 4.09k | return( 1 ); |
96 | | |
97 | 0 | on_error: |
98 | 0 | if( *metadata_block_header != NULL ) |
99 | 0 | { |
100 | 0 | memory_free( |
101 | 0 | *metadata_block_header ); |
102 | |
|
103 | 0 | *metadata_block_header = NULL; |
104 | 0 | } |
105 | 0 | return( -1 ); |
106 | 4.09k | } |
107 | | |
108 | | /* Frees a metadata block header |
109 | | * Returns 1 if successful or -1 on error |
110 | | */ |
111 | | int libbde_metadata_block_header_free( |
112 | | libbde_metadata_block_header_t **metadata_block_header, |
113 | | libcerror_error_t **error ) |
114 | 4.09k | { |
115 | 4.09k | static char *function = "libbde_metadata_block_header_free"; |
116 | | |
117 | 4.09k | if( metadata_block_header == NULL ) |
118 | 0 | { |
119 | 0 | libcerror_error_set( |
120 | 0 | error, |
121 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
122 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
123 | 0 | "%s: invalid metadata block header.", |
124 | 0 | function ); |
125 | |
|
126 | 0 | return( -1 ); |
127 | 0 | } |
128 | 4.09k | if( *metadata_block_header != NULL ) |
129 | 4.09k | { |
130 | 4.09k | memory_free( |
131 | 4.09k | *metadata_block_header ); |
132 | | |
133 | 4.09k | *metadata_block_header = NULL; |
134 | 4.09k | } |
135 | 4.09k | return( 1 ); |
136 | 4.09k | } |
137 | | |
138 | | /* Reads a metadata block header |
139 | | * Returns 1 if successful or -1 on error |
140 | | */ |
141 | | int libbde_metadata_block_header_read_data( |
142 | | libbde_metadata_block_header_t *metadata_block_header, |
143 | | const uint8_t *data, |
144 | | size_t data_size, |
145 | | libcerror_error_t **error ) |
146 | 4.05k | { |
147 | 4.05k | static char *function = "libbde_metadata_block_header_read_data"; |
148 | 4.05k | uint64_t safe_volume_header_offset = 0; |
149 | | |
150 | | #if defined( HAVE_DEBUG_OUTPUT ) |
151 | | uint32_t value_32bit = 0; |
152 | | uint16_t value_16bit = 0; |
153 | | #endif |
154 | | |
155 | 4.05k | if( metadata_block_header == NULL ) |
156 | 0 | { |
157 | 0 | libcerror_error_set( |
158 | 0 | error, |
159 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
160 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
161 | 0 | "%s: invalid metadata block header.", |
162 | 0 | function ); |
163 | |
|
164 | 0 | return( -1 ); |
165 | 0 | } |
166 | 4.05k | if( data == NULL ) |
167 | 0 | { |
168 | 0 | libcerror_error_set( |
169 | 0 | error, |
170 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
171 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
172 | 0 | "%s: invalid data.", |
173 | 0 | function ); |
174 | |
|
175 | 0 | return( -1 ); |
176 | 0 | } |
177 | 4.05k | if( ( data_size < sizeof( bde_metadata_block_header_v1_t ) ) |
178 | 4.05k | || ( data_size > (size_t) SSIZE_MAX ) ) |
179 | 0 | { |
180 | 0 | libcerror_error_set( |
181 | 0 | error, |
182 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
183 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
184 | 0 | "%s: invalid data size value out of bounds.", |
185 | 0 | function ); |
186 | |
|
187 | 0 | return( -1 ); |
188 | 0 | } |
189 | | #if defined( HAVE_DEBUG_OUTPUT ) |
190 | | if( libcnotify_verbose != 0 ) |
191 | | { |
192 | | libcnotify_printf( |
193 | | "%s: FVE metadata block header:\n", |
194 | | function ); |
195 | | libcnotify_print_data( |
196 | | data, |
197 | | sizeof( bde_metadata_block_header_v1_t ), |
198 | | 0 ); |
199 | | } |
200 | | #endif |
201 | 4.05k | if( memory_compare( |
202 | 4.05k | data, |
203 | 4.05k | bde_signature, |
204 | 4.05k | 8 ) != 0 ) |
205 | 31 | { |
206 | 31 | libcerror_error_set( |
207 | 31 | error, |
208 | 31 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
209 | 31 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
210 | 31 | "%s: invalid metadata block signature.", |
211 | 31 | function ); |
212 | | |
213 | 31 | return( -1 ); |
214 | 31 | } |
215 | 4.02k | byte_stream_copy_to_uint16_little_endian( |
216 | 4.02k | ( (bde_metadata_block_header_v1_t *) data )->version, |
217 | 4.02k | metadata_block_header->version ); |
218 | | |
219 | 4.02k | if( metadata_block_header->version == 1 ) |
220 | 574 | { |
221 | 574 | byte_stream_copy_to_uint64_little_endian( |
222 | 574 | ( (bde_metadata_block_header_v1_t *) data )->mft_mirror_cluster_block, |
223 | 574 | metadata_block_header->mft_mirror_cluster_block_number ); |
224 | 574 | } |
225 | 3.44k | else if( metadata_block_header->version == 2 ) |
226 | 3.43k | { |
227 | 3.43k | byte_stream_copy_to_uint64_little_endian( |
228 | 3.43k | ( (bde_metadata_block_header_v2_t *) data )->encrypted_volume_size, |
229 | 3.43k | metadata_block_header->encrypted_volume_size ); |
230 | | |
231 | 3.43k | byte_stream_copy_to_uint64_little_endian( |
232 | 3.43k | ( (bde_metadata_block_header_v2_t *) data )->volume_header_offset, |
233 | 3.43k | safe_volume_header_offset ); |
234 | | |
235 | 3.43k | byte_stream_copy_to_uint32_little_endian( |
236 | 3.43k | ( (bde_metadata_block_header_v2_t *) data )->number_of_volume_header_sectors, |
237 | 3.43k | metadata_block_header->number_of_volume_header_sectors ); |
238 | 3.43k | } |
239 | 4.02k | byte_stream_copy_to_uint64_little_endian( |
240 | 4.02k | ( (bde_metadata_block_header_v1_t *) data )->first_metadata_offset, |
241 | 4.02k | metadata_block_header->first_metadata_offset ); |
242 | | |
243 | 4.02k | byte_stream_copy_to_uint64_little_endian( |
244 | 4.02k | ( (bde_metadata_block_header_v1_t *) data )->second_metadata_offset, |
245 | 4.02k | metadata_block_header->second_metadata_offset ); |
246 | | |
247 | 4.02k | byte_stream_copy_to_uint64_little_endian( |
248 | 4.02k | ( (bde_metadata_block_header_v1_t *) data )->third_metadata_offset, |
249 | 4.02k | metadata_block_header->third_metadata_offset ); |
250 | | |
251 | | #if defined( HAVE_DEBUG_OUTPUT ) |
252 | | if( libcnotify_verbose != 0 ) |
253 | | { |
254 | | libcnotify_printf( |
255 | | "%s: signature\t\t\t: %c%c%c%c%c%c%c%c\n", |
256 | | function, |
257 | | data[ 0 ], |
258 | | data[ 1 ], |
259 | | data[ 2 ], |
260 | | data[ 3 ], |
261 | | data[ 4 ], |
262 | | data[ 5 ], |
263 | | data[ 6 ], |
264 | | data[ 7 ] ); |
265 | | |
266 | | byte_stream_copy_to_uint16_little_endian( |
267 | | ( (bde_metadata_block_header_v1_t *) data )->size, |
268 | | value_16bit ); |
269 | | libcnotify_printf( |
270 | | "%s: unknown size\t\t\t: %" PRIu16 "\n", |
271 | | function, |
272 | | value_16bit ); |
273 | | |
274 | | libcnotify_printf( |
275 | | "%s: format version\t\t\t: %" PRIu16 "\n", |
276 | | function, |
277 | | metadata_block_header->version ); |
278 | | |
279 | | byte_stream_copy_to_uint16_little_endian( |
280 | | ( (bde_metadata_block_header_v1_t *) data )->unknown1, |
281 | | value_16bit ); |
282 | | libcnotify_printf( |
283 | | "%s: unknown1\t\t\t: %" PRIu16 "\n", |
284 | | function, |
285 | | value_16bit ); |
286 | | |
287 | | byte_stream_copy_to_uint16_little_endian( |
288 | | ( (bde_metadata_block_header_v1_t *) data )->unknown2, |
289 | | value_16bit ); |
290 | | libcnotify_printf( |
291 | | "%s: unknown2\t\t\t: %" PRIu16 "\n", |
292 | | function, |
293 | | value_16bit ); |
294 | | |
295 | | if( metadata_block_header->version == 1 ) |
296 | | { |
297 | | libcnotify_printf( |
298 | | "%s: unknown3:\n", |
299 | | function ); |
300 | | libcnotify_print_data( |
301 | | ( (bde_metadata_block_header_v1_t *) data )->unknown3, |
302 | | 16, |
303 | | 0 ); |
304 | | } |
305 | | else if( metadata_block_header->version == 2 ) |
306 | | { |
307 | | libcnotify_printf( |
308 | | "%s: encrypted volume size\t\t: %" PRIu64 "\n", |
309 | | function, |
310 | | metadata_block_header->encrypted_volume_size ); |
311 | | |
312 | | byte_stream_copy_to_uint32_little_endian( |
313 | | ( (bde_metadata_block_header_v2_t *) data )->unknown3, |
314 | | value_32bit ); |
315 | | libcnotify_printf( |
316 | | "%s: unknown3\t\t\t: %" PRIu32 "\n", |
317 | | function, |
318 | | value_32bit ); |
319 | | |
320 | | libcnotify_printf( |
321 | | "%s: number of volume header sectors\t: %" PRIu32 "\n", |
322 | | function, |
323 | | metadata_block_header->number_of_volume_header_sectors ); |
324 | | } |
325 | | libcnotify_printf( |
326 | | "%s: first metadata offset\t\t: 0x%08" PRIx64 "\n", |
327 | | function, |
328 | | metadata_block_header->first_metadata_offset ); |
329 | | |
330 | | libcnotify_printf( |
331 | | "%s: second metadata offset\t\t: 0x%08" PRIx64 "\n", |
332 | | function, |
333 | | metadata_block_header->second_metadata_offset ); |
334 | | |
335 | | libcnotify_printf( |
336 | | "%s: third metadata offset\t\t: 0x%08" PRIx64 "\n", |
337 | | function, |
338 | | metadata_block_header->third_metadata_offset ); |
339 | | |
340 | | if( metadata_block_header->version == 1 ) |
341 | | { |
342 | | libcnotify_printf( |
343 | | "%s: MFT mirror cluster block\t\t: 0x%08" PRIx64 "\n", |
344 | | function, |
345 | | metadata_block_header->mft_mirror_cluster_block_number ); |
346 | | } |
347 | | else if( metadata_block_header->version == 2 ) |
348 | | { |
349 | | libcnotify_printf( |
350 | | "%s: volume header offset\t\t: 0x%08" PRIx64 "\n", |
351 | | function, |
352 | | safe_volume_header_offset ); |
353 | | } |
354 | | libcnotify_printf( |
355 | | "\n" ); |
356 | | } |
357 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
358 | | |
359 | 4.02k | metadata_block_header->volume_header_offset = (off64_t) safe_volume_header_offset; |
360 | | |
361 | 4.02k | if( ( metadata_block_header->version != 1 ) |
362 | 3.44k | && ( metadata_block_header->version != 2 ) ) |
363 | 16 | { |
364 | 16 | libcerror_error_set( |
365 | 16 | error, |
366 | 16 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
367 | 16 | LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE, |
368 | 16 | "%s: unsupported format version.", |
369 | 16 | function ); |
370 | | |
371 | 16 | return( -1 ); |
372 | 16 | } |
373 | 4.00k | return( 1 ); |
374 | 4.02k | } |
375 | | |
376 | | /* Reads a metadata block header |
377 | | * Returns 1 if successful or -1 on error |
378 | | */ |
379 | | int libbde_metadata_block_header_read_file_io_handle( |
380 | | libbde_metadata_block_header_t *metadata_block_header, |
381 | | libbfio_handle_t *file_io_handle, |
382 | | off64_t file_offset, |
383 | | libcerror_error_t **error ) |
384 | 4.09k | { |
385 | 4.09k | uint8_t metadata_block_header_data[ sizeof( bde_metadata_block_header_v1_t ) ]; |
386 | | |
387 | 4.09k | static char *function = "libbde_metadata_block_header_read_file_io_handle"; |
388 | 4.09k | ssize_t read_count = 0; |
389 | | |
390 | 4.09k | if( metadata_block_header == NULL ) |
391 | 0 | { |
392 | 0 | libcerror_error_set( |
393 | 0 | error, |
394 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
395 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
396 | 0 | "%s: invalid metadata block header.", |
397 | 0 | function ); |
398 | |
|
399 | 0 | return( -1 ); |
400 | 0 | } |
401 | 4.09k | read_count = libbfio_handle_read_buffer_at_offset( |
402 | 4.09k | file_io_handle, |
403 | 4.09k | metadata_block_header_data, |
404 | 4.09k | sizeof( bde_metadata_block_header_v1_t ), |
405 | 4.09k | file_offset, |
406 | 4.09k | error ); |
407 | | |
408 | 4.09k | if( read_count != (ssize_t) sizeof( bde_metadata_block_header_v1_t ) ) |
409 | 43 | { |
410 | 43 | libcerror_error_set( |
411 | 43 | error, |
412 | 43 | LIBCERROR_ERROR_DOMAIN_IO, |
413 | 43 | LIBCERROR_IO_ERROR_READ_FAILED, |
414 | 43 | "%s: unable to read FVE metadata block header data at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
415 | 43 | function, |
416 | 43 | file_offset, |
417 | 43 | file_offset ); |
418 | | |
419 | 43 | return( -1 ); |
420 | 43 | } |
421 | 4.05k | if( libbde_metadata_block_header_read_data( |
422 | 4.05k | metadata_block_header, |
423 | 4.05k | metadata_block_header_data, |
424 | 4.05k | sizeof( bde_metadata_block_header_v1_t ), |
425 | 4.05k | error ) != 1 ) |
426 | 47 | { |
427 | 47 | libcerror_error_set( |
428 | 47 | error, |
429 | 47 | LIBCERROR_ERROR_DOMAIN_IO, |
430 | 47 | LIBCERROR_IO_ERROR_READ_FAILED, |
431 | 47 | "%s: unable to read FVE metadata block header.", |
432 | 47 | function ); |
433 | | |
434 | 47 | return( -1 ); |
435 | 47 | } |
436 | 4.00k | return( 1 ); |
437 | 4.05k | } |
438 | | |