crypto: add compat cast5_set_key with nettle < 3.0.0
[qemu/ar7.git] / block / vhdx.h
blob7944f9320d8d590ea90821448e5f838862815f4b
1 /*
2 * Block driver for Hyper-V VHDX Images
4 * Copyright (c) 2013 Red Hat, Inc.,
6 * Authors:
7 * Jeff Cody <jcody@redhat.com>
9 * This is based on the "VHDX Format Specification v1.00", published 8/25/2012
10 * by Microsoft:
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 #ifndef BLOCK_VHDX_H
19 #define BLOCK_VHDX_H
21 #if !defined(KiB)
22 #define KiB (1 * 1024)
23 #endif
24 #if !defined(MiB)
25 #define MiB (KiB * 1024)
26 #endif
27 #define GiB (MiB * 1024)
28 #define TiB ((uint64_t) GiB * 1024)
30 #define DEFAULT_LOG_SIZE 1048576 /* 1MiB */
31 /* Structures and fields present in the VHDX file */
33 /* The header section has the following blocks,
34 * each block is 64KB:
36 * _____________________________________________________________________________
37 * | File Id. | Header 1 | Header 2 | Region Table | Reserved (768KB) |
38 * |----------|---------------|------------|--------------|--------------------|
39 * | | | | | |
40 * 0.........64KB...........128KB........192KB..........256KB................1MB
43 #define VHDX_HEADER_BLOCK_SIZE (64 * 1024)
45 #define VHDX_FILE_ID_OFFSET 0
46 #define VHDX_HEADER1_OFFSET (VHDX_HEADER_BLOCK_SIZE * 1)
47 #define VHDX_HEADER2_OFFSET (VHDX_HEADER_BLOCK_SIZE * 2)
48 #define VHDX_REGION_TABLE_OFFSET (VHDX_HEADER_BLOCK_SIZE * 3)
49 #define VHDX_REGION_TABLE2_OFFSET (VHDX_HEADER_BLOCK_SIZE * 4)
51 #define VHDX_HEADER_SECTION_END (1 * MiB)
53 * A note on the use of MS-GUID fields. For more details on the GUID,
54 * please see: https://en.wikipedia.org/wiki/Globally_unique_identifier.
56 * The VHDX specification only states that these are MS GUIDs, and which
57 * bytes are data1-data4. It makes no mention of what algorithm should be used
58 * to generate the GUID, nor what standard. However, looking at the specified
59 * known GUID fields, it appears the GUIDs are:
60 * Standard/DCE GUID type (noted by 10b in the MSB of byte 0 of .data4)
61 * Random algorithm (noted by 0x4XXX for .data3)
64 /* ---- HEADER SECTION STRUCTURES ---- */
66 /* These structures are ones that are defined in the VHDX specification
67 * document */
69 #define VHDX_FILE_SIGNATURE 0x656C696678646876ULL /* "vhdxfile" in ASCII */
70 typedef struct VHDXFileIdentifier {
71 uint64_t signature; /* "vhdxfile" in ASCII */
72 uint16_t creator[256]; /* optional; utf-16 string to identify
73 the vhdx file creator. Diagnostic
74 only */
75 } VHDXFileIdentifier;
78 /* the guid is a 16 byte unique ID - the definition for this used by
79 * Microsoft is not just 16 bytes though - it is a structure that is defined,
80 * so we need to follow it here so that endianness does not trip us up */
82 typedef struct QEMU_PACKED MSGUID {
83 uint32_t data1;
84 uint16_t data2;
85 uint16_t data3;
86 uint8_t data4[8];
87 } MSGUID;
89 #define guid_eq(a, b) \
90 (memcmp(&(a), &(b), sizeof(MSGUID)) == 0)
92 #define VHDX_HEADER_SIZE (4 * 1024) /* although the vhdx_header struct in disk
93 is only 582 bytes, for purposes of crc
94 the header is the first 4KB of the 64KB
95 block */
97 /* The full header is 4KB, although the actual header data is much smaller.
98 * But for the checksum calculation, it is over the entire 4KB structure,
99 * not just the defined portion of it */
100 #define VHDX_HEADER_SIGNATURE 0x64616568
101 typedef struct QEMU_PACKED VHDXHeader {
102 uint32_t signature; /* "head" in ASCII */
103 uint32_t checksum; /* CRC-32C hash of the whole header */
104 uint64_t sequence_number; /* Seq number of this header. Each
105 VHDX file has 2 of these headers,
106 and only the header with the highest
107 sequence number is valid */
108 MSGUID file_write_guid; /* 128 bit unique identifier. Must be
109 updated to new, unique value before
110 the first modification is made to
111 file */
112 MSGUID data_write_guid; /* 128 bit unique identifier. Must be
113 updated to new, unique value before
114 the first modification is made to
115 visible data. Visbile data is
116 defined as:
117 - system & user metadata
118 - raw block data
119 - disk size
120 - any change that will
121 cause the virtual disk
122 sector read to differ
124 This does not need to change if
125 blocks are re-arranged */
126 MSGUID log_guid; /* 128 bit unique identifier. If zero,
127 there is no valid log. If non-zero,
128 log entries with this guid are
129 valid. */
130 uint16_t log_version; /* version of the log format. Must be
131 set to zero */
132 uint16_t version; /* version of the vhdx file. Currently,
133 only supported version is "1" */
134 uint32_t log_length; /* length of the log. Must be multiple
135 of 1MB */
136 uint64_t log_offset; /* byte offset in the file of the log.
137 Must also be a multiple of 1MB */
138 } VHDXHeader;
140 /* Header for the region table block */
141 #define VHDX_REGION_SIGNATURE 0x69676572 /* "regi" in ASCII */
142 typedef struct QEMU_PACKED VHDXRegionTableHeader {
143 uint32_t signature; /* "regi" in ASCII */
144 uint32_t checksum; /* CRC-32C hash of the 64KB table */
145 uint32_t entry_count; /* number of valid entries */
146 uint32_t reserved;
147 } VHDXRegionTableHeader;
149 /* Individual region table entry. There may be a maximum of 2047 of these
151 * There are two known region table properties. Both are required.
152 * BAT (block allocation table): 2DC27766F62342009D64115E9BFD4A08
153 * Metadata: 8B7CA20647904B9AB8FE575F050F886E
155 #define VHDX_REGION_ENTRY_REQUIRED 0x01 /* if set, parser must understand
156 this entry in order to open
157 file */
158 typedef struct QEMU_PACKED VHDXRegionTableEntry {
159 MSGUID guid; /* 128-bit unique identifier */
160 uint64_t file_offset; /* offset of the object in the file.
161 Must be multiple of 1MB */
162 uint32_t length; /* length, in bytes, of the object */
163 uint32_t data_bits;
164 } VHDXRegionTableEntry;
167 /* ---- LOG ENTRY STRUCTURES ---- */
168 #define VHDX_LOG_MIN_SIZE (1024 * 1024)
169 #define VHDX_LOG_SECTOR_SIZE 4096
170 #define VHDX_LOG_HDR_SIZE 64
171 #define VHDX_LOG_SIGNATURE 0x65676f6c
172 typedef struct QEMU_PACKED VHDXLogEntryHeader {
173 uint32_t signature; /* "loge" in ASCII */
174 uint32_t checksum; /* CRC-32C hash of the 64KB table */
175 uint32_t entry_length; /* length in bytes, multiple of 1MB */
176 uint32_t tail; /* byte offset of first log entry of a
177 seq, where this entry is the last
178 entry */
179 uint64_t sequence_number; /* incremented with each log entry.
180 May not be zero. */
181 uint32_t descriptor_count; /* number of descriptors in this log
182 entry, must be >= 0 */
183 uint32_t reserved;
184 MSGUID log_guid; /* value of the log_guid from
185 vhdx_header. If not found in
186 vhdx_header, it is invalid */
187 uint64_t flushed_file_offset; /* see spec for full details - this
188 should be vhdx file size in bytes */
189 uint64_t last_file_offset; /* size in bytes that all allocated
190 file structures fit into */
191 } VHDXLogEntryHeader;
193 #define VHDX_LOG_DESC_SIZE 32
194 #define VHDX_LOG_DESC_SIGNATURE 0x63736564
195 #define VHDX_LOG_ZERO_SIGNATURE 0x6f72657a
196 typedef struct QEMU_PACKED VHDXLogDescriptor {
197 uint32_t signature; /* "zero" or "desc" in ASCII */
198 union {
199 uint32_t reserved; /* zero desc */
200 uint32_t trailing_bytes; /* data desc: bytes 4092-4096 of the
201 data sector */
203 union {
204 uint64_t zero_length; /* zero desc: length of the section to
205 zero */
206 uint64_t leading_bytes; /* data desc: bytes 0-7 of the data
207 sector */
209 uint64_t file_offset; /* file offset to write zeros - multiple
210 of 4kB */
211 uint64_t sequence_number; /* must match same field in
212 vhdx_log_entry_header */
213 } VHDXLogDescriptor;
215 #define VHDX_LOG_DATA_SIGNATURE 0x61746164
216 typedef struct QEMU_PACKED VHDXLogDataSector {
217 uint32_t data_signature; /* "data" in ASCII */
218 uint32_t sequence_high; /* 4 MSB of 8 byte sequence_number */
219 uint8_t data[4084]; /* raw data, bytes 8-4091 (inclusive).
220 see the data descriptor field for the
221 other mising bytes */
222 uint32_t sequence_low; /* 4 LSB of 8 byte sequence_number */
223 } VHDXLogDataSector;
227 /* block states - different state values depending on whether it is a
228 * payload block, or a sector block. */
230 #define PAYLOAD_BLOCK_NOT_PRESENT 0
231 #define PAYLOAD_BLOCK_UNDEFINED 1
232 #define PAYLOAD_BLOCK_ZERO 2
233 #define PAYLOAD_BLOCK_UNMAPPED 3
234 #define PAYLOAD_BLOCK_UNMAPPED_v095 5
235 #define PAYLOAD_BLOCK_FULLY_PRESENT 6
236 #define PAYLOAD_BLOCK_PARTIALLY_PRESENT 7
238 #define SB_BLOCK_NOT_PRESENT 0
239 #define SB_BLOCK_PRESENT 6
241 /* per the spec */
242 #define VHDX_MAX_SECTORS_PER_BLOCK (1 << 23)
244 /* upper 44 bits are the file offset in 1MB units lower 3 bits are the state
245 other bits are reserved */
246 #define VHDX_BAT_STATE_BIT_MASK 0x07
247 #define VHDX_BAT_FILE_OFF_MASK 0xFFFFFFFFFFF00000ULL /* upper 44 bits */
248 typedef uint64_t VHDXBatEntry;
250 /* ---- METADATA REGION STRUCTURES ---- */
252 #define VHDX_METADATA_ENTRY_SIZE 32
253 #define VHDX_METADATA_MAX_ENTRIES 2047 /* not including the header */
254 #define VHDX_METADATA_TABLE_MAX_SIZE \
255 (VHDX_METADATA_ENTRY_SIZE * (VHDX_METADATA_MAX_ENTRIES+1))
256 #define VHDX_METADATA_SIGNATURE 0x617461646174656DULL /* "metadata" in ASCII */
257 typedef struct QEMU_PACKED VHDXMetadataTableHeader {
258 uint64_t signature; /* "metadata" in ASCII */
259 uint16_t reserved;
260 uint16_t entry_count; /* number table entries. <= 2047 */
261 uint32_t reserved2[5];
262 } VHDXMetadataTableHeader;
264 #define VHDX_META_FLAGS_IS_USER 0x01 /* max 1024 entries */
265 #define VHDX_META_FLAGS_IS_VIRTUAL_DISK 0x02 /* virtual disk metadata if set,
266 otherwise file metdata */
267 #define VHDX_META_FLAGS_IS_REQUIRED 0x04 /* parse must understand this
268 entry to open the file */
269 typedef struct QEMU_PACKED VHDXMetadataTableEntry {
270 MSGUID item_id; /* 128-bit identifier for metadata */
271 uint32_t offset; /* byte offset of the metadata. At
272 least 64kB. Relative to start of
273 metadata region */
274 /* note: if length = 0, so is offset */
275 uint32_t length; /* length of metadata. <= 1MB. */
276 uint32_t data_bits; /* least-significant 3 bits are flags,
277 the rest are reserved (see above) */
278 uint32_t reserved2;
279 } VHDXMetadataTableEntry;
281 #define VHDX_PARAMS_LEAVE_BLOCKS_ALLOCED 0x01 /* Do not change any blocks to
282 be BLOCK_NOT_PRESENT.
283 If set indicates a fixed
284 size VHDX file */
285 #define VHDX_PARAMS_HAS_PARENT 0x02 /* has parent / backing file */
286 #define VHDX_BLOCK_SIZE_MIN (1 * MiB)
287 #define VHDX_BLOCK_SIZE_MAX (256 * MiB)
288 typedef struct QEMU_PACKED VHDXFileParameters {
289 uint32_t block_size; /* size of each payload block, always
290 power of 2, <= 256MB and >= 1MB. */
291 uint32_t data_bits; /* least-significant 2 bits are flags,
292 the rest are reserved (see above) */
293 } VHDXFileParameters;
295 #define VHDX_MAX_IMAGE_SIZE ((uint64_t) 64 * TiB)
296 typedef struct QEMU_PACKED VHDXVirtualDiskSize {
297 uint64_t virtual_disk_size; /* Size of the virtual disk, in bytes.
298 Must be multiple of the sector size,
299 max of 64TB */
300 } VHDXVirtualDiskSize;
302 typedef struct QEMU_PACKED VHDXPage83Data {
303 MSGUID page_83_data; /* unique id for scsi devices that
304 support page 0x83 */
305 } VHDXPage83Data;
307 typedef struct QEMU_PACKED VHDXVirtualDiskLogicalSectorSize {
308 uint32_t logical_sector_size; /* virtual disk sector size (in bytes).
309 Can only be 512 or 4096 bytes */
310 } VHDXVirtualDiskLogicalSectorSize;
312 typedef struct QEMU_PACKED VHDXVirtualDiskPhysicalSectorSize {
313 uint32_t physical_sector_size; /* physical sector size (in bytes).
314 Can only be 512 or 4096 bytes */
315 } VHDXVirtualDiskPhysicalSectorSize;
317 typedef struct QEMU_PACKED VHDXParentLocatorHeader {
318 MSGUID locator_type; /* type of the parent virtual disk. */
319 uint16_t reserved;
320 uint16_t key_value_count; /* number of key/value pairs for this
321 locator */
322 } VHDXParentLocatorHeader;
324 /* key and value strings are UNICODE strings, UTF-16 LE encoding, no NULs */
325 typedef struct QEMU_PACKED VHDXParentLocatorEntry {
326 uint32_t key_offset; /* offset in metadata for key, > 0 */
327 uint32_t value_offset; /* offset in metadata for value, >0 */
328 uint16_t key_length; /* length of entry key, > 0 */
329 uint16_t value_length; /* length of entry value, > 0 */
330 } VHDXParentLocatorEntry;
333 /* ----- END VHDX SPECIFICATION STRUCTURES ---- */
335 typedef struct VHDXMetadataEntries {
336 VHDXMetadataTableEntry file_parameters_entry;
337 VHDXMetadataTableEntry virtual_disk_size_entry;
338 VHDXMetadataTableEntry page83_data_entry;
339 VHDXMetadataTableEntry logical_sector_size_entry;
340 VHDXMetadataTableEntry phys_sector_size_entry;
341 VHDXMetadataTableEntry parent_locator_entry;
342 uint16_t present;
343 } VHDXMetadataEntries;
345 typedef struct VHDXLogEntries {
346 uint64_t offset;
347 uint64_t length;
348 uint32_t write;
349 uint32_t read;
350 VHDXLogEntryHeader *hdr;
351 void *desc_buffer;
352 uint64_t sequence;
353 uint32_t tail;
354 } VHDXLogEntries;
356 typedef struct VHDXRegionEntry {
357 uint64_t start;
358 uint64_t end;
359 QLIST_ENTRY(VHDXRegionEntry) entries;
360 } VHDXRegionEntry;
362 typedef struct BDRVVHDXState {
363 CoMutex lock;
365 int curr_header;
366 VHDXHeader *headers[2];
368 VHDXRegionTableHeader rt;
369 VHDXRegionTableEntry bat_rt; /* region table for the BAT */
370 VHDXRegionTableEntry metadata_rt; /* region table for the metadata */
372 VHDXMetadataTableHeader metadata_hdr;
373 VHDXMetadataEntries metadata_entries;
375 VHDXFileParameters params;
376 uint32_t block_size;
377 uint32_t block_size_bits;
378 uint32_t sectors_per_block;
379 uint32_t sectors_per_block_bits;
381 uint64_t virtual_disk_size;
382 uint32_t logical_sector_size;
383 uint32_t physical_sector_size;
385 uint64_t chunk_ratio;
386 uint32_t chunk_ratio_bits;
387 uint32_t logical_sector_size_bits;
389 uint32_t bat_entries;
390 VHDXBatEntry *bat;
391 uint64_t bat_offset;
393 bool first_visible_write;
394 MSGUID session_guid;
396 VHDXLogEntries log;
398 VHDXParentLocatorHeader parent_header;
399 VHDXParentLocatorEntry *parent_entries;
401 Error *migration_blocker;
403 bool log_replayed_on_open;
405 QLIST_HEAD(VHDXRegionHead, VHDXRegionEntry) regions;
406 } BDRVVHDXState;
408 void vhdx_guid_generate(MSGUID *guid);
410 int vhdx_update_headers(BlockDriverState *bs, BDRVVHDXState *s, bool rw,
411 MSGUID *log_guid);
413 uint32_t vhdx_update_checksum(uint8_t *buf, size_t size, int crc_offset);
414 uint32_t vhdx_checksum_calc(uint32_t crc, uint8_t *buf, size_t size,
415 int crc_offset);
417 bool vhdx_checksum_is_valid(uint8_t *buf, size_t size, int crc_offset);
419 int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s, bool *flushed,
420 Error **errp);
422 int vhdx_log_write_and_flush(BlockDriverState *bs, BDRVVHDXState *s,
423 void *data, uint32_t length, uint64_t offset);
425 static inline void leguid_to_cpus(MSGUID *guid)
427 le32_to_cpus(&guid->data1);
428 le16_to_cpus(&guid->data2);
429 le16_to_cpus(&guid->data3);
432 static inline void cpu_to_leguids(MSGUID *guid)
434 cpu_to_le32s(&guid->data1);
435 cpu_to_le16s(&guid->data2);
436 cpu_to_le16s(&guid->data3);
439 void vhdx_header_le_import(VHDXHeader *h);
440 void vhdx_header_le_export(VHDXHeader *orig_h, VHDXHeader *new_h);
441 void vhdx_log_desc_le_import(VHDXLogDescriptor *d);
442 void vhdx_log_desc_le_export(VHDXLogDescriptor *d);
443 void vhdx_log_data_le_import(VHDXLogDataSector *d);
444 void vhdx_log_data_le_export(VHDXLogDataSector *d);
445 void vhdx_log_entry_hdr_le_import(VHDXLogEntryHeader *hdr);
446 void vhdx_log_entry_hdr_le_export(VHDXLogEntryHeader *hdr);
447 void vhdx_region_header_le_import(VHDXRegionTableHeader *hdr);
448 void vhdx_region_header_le_export(VHDXRegionTableHeader *hdr);
449 void vhdx_region_entry_le_import(VHDXRegionTableEntry *e);
450 void vhdx_region_entry_le_export(VHDXRegionTableEntry *e);
451 void vhdx_metadata_header_le_import(VHDXMetadataTableHeader *hdr);
452 void vhdx_metadata_header_le_export(VHDXMetadataTableHeader *hdr);
453 void vhdx_metadata_entry_le_import(VHDXMetadataTableEntry *e);
454 void vhdx_metadata_entry_le_export(VHDXMetadataTableEntry *e);
455 int vhdx_user_visible_write(BlockDriverState *bs, BDRVVHDXState *s);
457 #endif