1 /* 2 * Block driver for Hyper-V VHDX Images 3 * 4 * Copyright (c) 2013 Red Hat, Inc., 5 * 6 * Authors: 7 * Jeff Cody <jcody@redhat.com> 8 * 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 12 * 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. 15 * 16 */ 17 18 #include "qemu/osdep.h" 19 #include "qemu-common.h" 20 #include "block/block_int.h" 21 #include "block/vhdx.h" 22 23 #include <uuid/uuid.h> 24 25 26 /* 27 * All the VHDX formats on disk are little endian - the following 28 * are helper import/export functions to correctly convert 29 * endianness from disk read to native cpu format, and back again. 30 */ 31 32 33 /* VHDX File Header */ 34 35 36 void vhdx_header_le_import(VHDXHeader *h) 37 { 38 assert(h != NULL); 39 40 le32_to_cpus(&h->signature); 41 le32_to_cpus(&h->checksum); 42 le64_to_cpus(&h->sequence_number); 43 44 leguid_to_cpus(&h->file_write_guid); 45 leguid_to_cpus(&h->data_write_guid); 46 leguid_to_cpus(&h->log_guid); 47 48 le16_to_cpus(&h->log_version); 49 le16_to_cpus(&h->version); 50 le32_to_cpus(&h->log_length); 51 le64_to_cpus(&h->log_offset); 52 } 53 54 void vhdx_header_le_export(VHDXHeader *orig_h, VHDXHeader *new_h) 55 { 56 assert(orig_h != NULL); 57 assert(new_h != NULL); 58 59 new_h->signature = cpu_to_le32(orig_h->signature); 60 new_h->checksum = cpu_to_le32(orig_h->checksum); 61 new_h->sequence_number = cpu_to_le64(orig_h->sequence_number); 62 63 new_h->file_write_guid = orig_h->file_write_guid; 64 new_h->data_write_guid = orig_h->data_write_guid; 65 new_h->log_guid = orig_h->log_guid; 66 67 cpu_to_leguids(&new_h->file_write_guid); 68 cpu_to_leguids(&new_h->data_write_guid); 69 cpu_to_leguids(&new_h->log_guid); 70 71 new_h->log_version = cpu_to_le16(orig_h->log_version); 72 new_h->version = cpu_to_le16(orig_h->version); 73 new_h->log_length = cpu_to_le32(orig_h->log_length); 74 new_h->log_offset = cpu_to_le64(orig_h->log_offset); 75 } 76 77 78 /* VHDX Log Headers */ 79 80 81 void vhdx_log_desc_le_import(VHDXLogDescriptor *d) 82 { 83 assert(d != NULL); 84 85 le32_to_cpus(&d->signature); 86 le64_to_cpus(&d->file_offset); 87 le64_to_cpus(&d->sequence_number); 88 } 89 90 void vhdx_log_desc_le_export(VHDXLogDescriptor *d) 91 { 92 assert(d != NULL); 93 94 cpu_to_le32s(&d->signature); 95 cpu_to_le32s(&d->trailing_bytes); 96 cpu_to_le64s(&d->leading_bytes); 97 cpu_to_le64s(&d->file_offset); 98 cpu_to_le64s(&d->sequence_number); 99 } 100 101 void vhdx_log_data_le_import(VHDXLogDataSector *d) 102 { 103 assert(d != NULL); 104 105 le32_to_cpus(&d->data_signature); 106 le32_to_cpus(&d->sequence_high); 107 le32_to_cpus(&d->sequence_low); 108 } 109 110 void vhdx_log_data_le_export(VHDXLogDataSector *d) 111 { 112 assert(d != NULL); 113 114 cpu_to_le32s(&d->data_signature); 115 cpu_to_le32s(&d->sequence_high); 116 cpu_to_le32s(&d->sequence_low); 117 } 118 119 void vhdx_log_entry_hdr_le_import(VHDXLogEntryHeader *hdr) 120 { 121 assert(hdr != NULL); 122 123 le32_to_cpus(&hdr->signature); 124 le32_to_cpus(&hdr->checksum); 125 le32_to_cpus(&hdr->entry_length); 126 le32_to_cpus(&hdr->tail); 127 le64_to_cpus(&hdr->sequence_number); 128 le32_to_cpus(&hdr->descriptor_count); 129 leguid_to_cpus(&hdr->log_guid); 130 le64_to_cpus(&hdr->flushed_file_offset); 131 le64_to_cpus(&hdr->last_file_offset); 132 } 133 134 void vhdx_log_entry_hdr_le_export(VHDXLogEntryHeader *hdr) 135 { 136 assert(hdr != NULL); 137 138 cpu_to_le32s(&hdr->signature); 139 cpu_to_le32s(&hdr->checksum); 140 cpu_to_le32s(&hdr->entry_length); 141 cpu_to_le32s(&hdr->tail); 142 cpu_to_le64s(&hdr->sequence_number); 143 cpu_to_le32s(&hdr->descriptor_count); 144 cpu_to_leguids(&hdr->log_guid); 145 cpu_to_le64s(&hdr->flushed_file_offset); 146 cpu_to_le64s(&hdr->last_file_offset); 147 } 148 149 150 /* Region table entries */ 151 void vhdx_region_header_le_import(VHDXRegionTableHeader *hdr) 152 { 153 assert(hdr != NULL); 154 155 le32_to_cpus(&hdr->signature); 156 le32_to_cpus(&hdr->checksum); 157 le32_to_cpus(&hdr->entry_count); 158 } 159 160 void vhdx_region_header_le_export(VHDXRegionTableHeader *hdr) 161 { 162 assert(hdr != NULL); 163 164 cpu_to_le32s(&hdr->signature); 165 cpu_to_le32s(&hdr->checksum); 166 cpu_to_le32s(&hdr->entry_count); 167 } 168 169 void vhdx_region_entry_le_import(VHDXRegionTableEntry *e) 170 { 171 assert(e != NULL); 172 173 leguid_to_cpus(&e->guid); 174 le64_to_cpus(&e->file_offset); 175 le32_to_cpus(&e->length); 176 le32_to_cpus(&e->data_bits); 177 } 178 179 void vhdx_region_entry_le_export(VHDXRegionTableEntry *e) 180 { 181 assert(e != NULL); 182 183 cpu_to_leguids(&e->guid); 184 cpu_to_le64s(&e->file_offset); 185 cpu_to_le32s(&e->length); 186 cpu_to_le32s(&e->data_bits); 187 } 188 189 190 /* Metadata headers & table */ 191 void vhdx_metadata_header_le_import(VHDXMetadataTableHeader *hdr) 192 { 193 assert(hdr != NULL); 194 195 le64_to_cpus(&hdr->signature); 196 le16_to_cpus(&hdr->entry_count); 197 } 198 199 void vhdx_metadata_header_le_export(VHDXMetadataTableHeader *hdr) 200 { 201 assert(hdr != NULL); 202 203 cpu_to_le64s(&hdr->signature); 204 cpu_to_le16s(&hdr->entry_count); 205 } 206 207 void vhdx_metadata_entry_le_import(VHDXMetadataTableEntry *e) 208 { 209 assert(e != NULL); 210 211 leguid_to_cpus(&e->item_id); 212 le32_to_cpus(&e->offset); 213 le32_to_cpus(&e->length); 214 le32_to_cpus(&e->data_bits); 215 } 216 void vhdx_metadata_entry_le_export(VHDXMetadataTableEntry *e) 217 { 218 assert(e != NULL); 219 220 cpu_to_leguids(&e->item_id); 221 cpu_to_le32s(&e->offset); 222 cpu_to_le32s(&e->length); 223 cpu_to_le32s(&e->data_bits); 224 } 225