2 * Block driver for Hyper-V VHDX Images
4 * Copyright (c) 2013 Red Hat, Inc.,
7 * Jeff Cody <jcody@redhat.com>
9 * This is based on the "VHDX Format Specification v1.00", published 8/25/2012
11 * https://www.microsoft.com/en-us/download/details.aspx?id=34750
13 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
14 * See the COPYING.LIB file in the top-level directory.
18 #include "qemu/osdep.h"
19 #include "qemu-common.h"
20 #include "block/block_int.h"
21 #include "qemu/bswap.h"
25 * All the VHDX formats on disk are little endian - the following
26 * are helper import/export functions to correctly convert
27 * endianness from disk read to native cpu format, and back again.
31 /* VHDX File Header */
34 void vhdx_header_le_import(VHDXHeader
*h
)
38 h
->signature
= le32_to_cpu(h
->signature
);
39 h
->checksum
= le32_to_cpu(h
->checksum
);
40 h
->sequence_number
= le64_to_cpu(h
->sequence_number
);
42 leguid_to_cpus(&h
->file_write_guid
);
43 leguid_to_cpus(&h
->data_write_guid
);
44 leguid_to_cpus(&h
->log_guid
);
46 h
->log_version
= le16_to_cpu(h
->log_version
);
47 h
->version
= le16_to_cpu(h
->version
);
48 h
->log_length
= le32_to_cpu(h
->log_length
);
49 h
->log_offset
= le64_to_cpu(h
->log_offset
);
52 void vhdx_header_le_export(VHDXHeader
*orig_h
, VHDXHeader
*new_h
)
54 assert(orig_h
!= NULL
);
55 assert(new_h
!= NULL
);
57 new_h
->signature
= cpu_to_le32(orig_h
->signature
);
58 new_h
->checksum
= cpu_to_le32(orig_h
->checksum
);
59 new_h
->sequence_number
= cpu_to_le64(orig_h
->sequence_number
);
61 new_h
->file_write_guid
= orig_h
->file_write_guid
;
62 new_h
->data_write_guid
= orig_h
->data_write_guid
;
63 new_h
->log_guid
= orig_h
->log_guid
;
65 cpu_to_leguids(&new_h
->file_write_guid
);
66 cpu_to_leguids(&new_h
->data_write_guid
);
67 cpu_to_leguids(&new_h
->log_guid
);
69 new_h
->log_version
= cpu_to_le16(orig_h
->log_version
);
70 new_h
->version
= cpu_to_le16(orig_h
->version
);
71 new_h
->log_length
= cpu_to_le32(orig_h
->log_length
);
72 new_h
->log_offset
= cpu_to_le64(orig_h
->log_offset
);
76 /* VHDX Log Headers */
79 void vhdx_log_desc_le_import(VHDXLogDescriptor
*d
)
83 d
->signature
= le32_to_cpu(d
->signature
);
84 d
->file_offset
= le64_to_cpu(d
->file_offset
);
85 d
->sequence_number
= le64_to_cpu(d
->sequence_number
);
88 void vhdx_log_desc_le_export(VHDXLogDescriptor
*d
)
92 d
->signature
= cpu_to_le32(d
->signature
);
93 d
->trailing_bytes
= cpu_to_le32(d
->trailing_bytes
);
94 d
->leading_bytes
= cpu_to_le64(d
->leading_bytes
);
95 d
->file_offset
= cpu_to_le64(d
->file_offset
);
96 d
->sequence_number
= cpu_to_le64(d
->sequence_number
);
99 void vhdx_log_data_le_import(VHDXLogDataSector
*d
)
103 d
->data_signature
= le32_to_cpu(d
->data_signature
);
104 d
->sequence_high
= le32_to_cpu(d
->sequence_high
);
105 d
->sequence_low
= le32_to_cpu(d
->sequence_low
);
108 void vhdx_log_data_le_export(VHDXLogDataSector
*d
)
112 d
->data_signature
= cpu_to_le32(d
->data_signature
);
113 d
->sequence_high
= cpu_to_le32(d
->sequence_high
);
114 d
->sequence_low
= cpu_to_le32(d
->sequence_low
);
117 void vhdx_log_entry_hdr_le_import(VHDXLogEntryHeader
*hdr
)
121 hdr
->signature
= le32_to_cpu(hdr
->signature
);
122 hdr
->checksum
= le32_to_cpu(hdr
->checksum
);
123 hdr
->entry_length
= le32_to_cpu(hdr
->entry_length
);
124 hdr
->tail
= le32_to_cpu(hdr
->tail
);
125 hdr
->sequence_number
= le64_to_cpu(hdr
->sequence_number
);
126 hdr
->descriptor_count
= le32_to_cpu(hdr
->descriptor_count
);
127 leguid_to_cpus(&hdr
->log_guid
);
128 hdr
->flushed_file_offset
= le64_to_cpu(hdr
->flushed_file_offset
);
129 hdr
->last_file_offset
= le64_to_cpu(hdr
->last_file_offset
);
132 void vhdx_log_entry_hdr_le_export(VHDXLogEntryHeader
*hdr
)
136 hdr
->signature
= cpu_to_le32(hdr
->signature
);
137 hdr
->checksum
= cpu_to_le32(hdr
->checksum
);
138 hdr
->entry_length
= cpu_to_le32(hdr
->entry_length
);
139 hdr
->tail
= cpu_to_le32(hdr
->tail
);
140 hdr
->sequence_number
= cpu_to_le64(hdr
->sequence_number
);
141 hdr
->descriptor_count
= cpu_to_le32(hdr
->descriptor_count
);
142 cpu_to_leguids(&hdr
->log_guid
);
143 hdr
->flushed_file_offset
= cpu_to_le64(hdr
->flushed_file_offset
);
144 hdr
->last_file_offset
= cpu_to_le64(hdr
->last_file_offset
);
148 /* Region table entries */
149 void vhdx_region_header_le_import(VHDXRegionTableHeader
*hdr
)
153 hdr
->signature
= le32_to_cpu(hdr
->signature
);
154 hdr
->checksum
= le32_to_cpu(hdr
->checksum
);
155 hdr
->entry_count
= le32_to_cpu(hdr
->entry_count
);
158 void vhdx_region_header_le_export(VHDXRegionTableHeader
*hdr
)
162 hdr
->signature
= cpu_to_le32(hdr
->signature
);
163 hdr
->checksum
= cpu_to_le32(hdr
->checksum
);
164 hdr
->entry_count
= cpu_to_le32(hdr
->entry_count
);
167 void vhdx_region_entry_le_import(VHDXRegionTableEntry
*e
)
171 leguid_to_cpus(&e
->guid
);
172 e
->file_offset
= le64_to_cpu(e
->file_offset
);
173 e
->length
= le32_to_cpu(e
->length
);
174 e
->data_bits
= le32_to_cpu(e
->data_bits
);
177 void vhdx_region_entry_le_export(VHDXRegionTableEntry
*e
)
181 cpu_to_leguids(&e
->guid
);
182 e
->file_offset
= cpu_to_le64(e
->file_offset
);
183 e
->length
= cpu_to_le32(e
->length
);
184 e
->data_bits
= cpu_to_le32(e
->data_bits
);
188 /* Metadata headers & table */
189 void vhdx_metadata_header_le_import(VHDXMetadataTableHeader
*hdr
)
193 hdr
->signature
= le64_to_cpu(hdr
->signature
);
194 hdr
->entry_count
= le16_to_cpu(hdr
->entry_count
);
197 void vhdx_metadata_header_le_export(VHDXMetadataTableHeader
*hdr
)
201 hdr
->signature
= cpu_to_le64(hdr
->signature
);
202 hdr
->entry_count
= cpu_to_le16(hdr
->entry_count
);
205 void vhdx_metadata_entry_le_import(VHDXMetadataTableEntry
*e
)
209 leguid_to_cpus(&e
->item_id
);
210 e
->offset
= le32_to_cpu(e
->offset
);
211 e
->length
= le32_to_cpu(e
->length
);
212 e
->data_bits
= le32_to_cpu(e
->data_bits
);
214 void vhdx_metadata_entry_le_export(VHDXMetadataTableEntry
*e
)
218 cpu_to_leguids(&e
->item_id
);
219 e
->offset
= cpu_to_le32(e
->offset
);
220 e
->length
= cpu_to_le32(e
->length
);
221 e
->data_bits
= cpu_to_le32(e
->data_bits
);