2 * Copyright (c) 2014, 2015 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/endian.h>
32 #include <sys/errno.h>
43 #ifndef __has_extension
44 #define __has_extension(x) 0
49 * o File is in network byte order.
50 * o The timestamp is seconds since 1/1/2000 12:00:00 AM UTC
52 * This file is divided in 3 parts:
53 * 1. Common definitions
54 * 2. Dynamic VHD support
55 * 3. Fixed VHD support
59 * PART 1: Common definitions
62 #define VHD_SECTOR_SIZE 512
63 #define VHD_BLOCK_SIZE (4096 * VHD_SECTOR_SIZE) /* 2MB blocks */
73 #define VHD_FOOTER_COOKIE 0x636f6e6563746978
75 #define VHD_FEATURES_TEMPORARY 0x01
76 #define VHD_FEATURES_RESERVED 0x02
78 #define VHD_VERSION 0x00010000
81 uint32_t creator_tool;
82 #define VHD_CREATOR_TOOL 0x2a696d67 /* FreeBSD mkimg */
83 uint32_t creator_version;
84 #define VHD_CREATOR_VERSION 0x00020000
86 #define VHD_CREATOR_OS 0x5769326b /* Wi2k */
87 uint64_t original_size;
88 uint64_t current_size;
89 struct vhd_geom geometry;
91 #define VHD_DISK_TYPE_FIXED 2
92 #define VHD_DISK_TYPE_DYNAMIC 3
93 #define VHD_DISK_TYPE_DIFF 4
97 uint8_t _reserved[427];
99 #if __has_extension(c_static_assert)
100 _Static_assert(sizeof(struct vhd_footer) == VHD_SECTOR_SIZE,
101 "Wrong size for footer");
105 vhd_checksum(void *buf, size_t sz)
112 for (ofs = 0; ofs < sz; ofs++)
118 vhd_geometry(uint64_t image_size, struct vhd_geom *geom)
123 imgsz = image_size / VHD_SECTOR_SIZE;
125 /* Respect command line options if possible. */
126 if (nheads > 1 && nheads < 256 &&
127 nsecs > 1 && nsecs < 256 &&
129 geom->cylinders = (ncyls != 0) ? ncyls :
130 imgsz / (nheads * nsecs);
131 geom->heads = nheads;
132 geom->sectors = nsecs;
136 if (imgsz > 65536 * 16 * 255)
137 imgsz = 65536 * 16 * 255;
138 if (imgsz >= 65535 * 16 * 63) {
139 geom->cylinders = imgsz / (16 * 255);
146 geom->heads = (cth + 1023) / 1024;
149 if (cth >= (geom->heads * 1024) || geom->heads > 16) {
154 if (cth >= (geom->heads * 1024)) {
159 geom->cylinders = cth / geom->heads;
169 return (t - 0x386d4380);
176 vhd_uuid_enc(void *buf, const uuid_t *uuid)
181 be32enc(p, uuid->time_low);
182 be16enc(p + 4, uuid->time_mid);
183 be16enc(p + 6, uuid->time_hi_and_version);
184 p[8] = uuid->clock_seq_hi_and_reserved;
185 p[9] = uuid->clock_seq_low;
186 for (i = 0; i < _UUID_NODE_LEN; i++)
187 p[10 + i] = uuid->node[i];
191 vhd_make_footer(struct vhd_footer *footer, uint64_t image_size,
192 uint32_t disk_type, uint64_t data_offset)
196 memset(footer, 0, sizeof(*footer));
197 be64enc(&footer->cookie, VHD_FOOTER_COOKIE);
198 be32enc(&footer->features, VHD_FEATURES_RESERVED);
199 be32enc(&footer->version, VHD_VERSION);
200 be64enc(&footer->data_offset, data_offset);
201 be32enc(&footer->timestamp, vhd_timestamp());
202 be32enc(&footer->creator_tool, VHD_CREATOR_TOOL);
203 be32enc(&footer->creator_version, VHD_CREATOR_VERSION);
204 be32enc(&footer->creator_os, VHD_CREATOR_OS);
205 be64enc(&footer->original_size, image_size);
206 be64enc(&footer->current_size, image_size);
207 vhd_geometry(image_size, &footer->geometry);
208 be16enc(&footer->geometry.cylinders, footer->geometry.cylinders);
209 be32enc(&footer->disk_type, disk_type);
211 vhd_uuid_enc(&footer->id, &id);
212 be32enc(&footer->checksum, vhd_checksum(footer, sizeof(*footer)));
216 * PART 2: Dynamic VHD support
220 * copy of disk footer
221 * dynamic disk header
222 * block allocation table (BAT)
227 struct vhd_dyn_header {
229 #define VHD_HEADER_COOKIE 0x6378737061727365
230 uint64_t data_offset;
231 uint64_t table_offset;
233 uint32_t max_entries;
237 uint32_t parent_timestamp;
239 uint16_t parent_name[256]; /* UTF-16 */
243 uint32_t data_length;
245 uint64_t data_offset;
247 char _reserved2[256];
249 #if __has_extension(c_static_assert)
250 _Static_assert(sizeof(struct vhd_dyn_header) == VHD_SECTOR_SIZE * 2,
251 "Wrong size for header");
255 vhd_dyn_resize(lba_t imgsz)
259 imagesz = imgsz * secsz;
260 imagesz = (imagesz + VHD_BLOCK_SIZE - 1) & ~(VHD_BLOCK_SIZE - 1);
261 return (image_set_size(imagesz / secsz));
265 vhd_dyn_write(int fd)
267 struct vhd_footer footer;
268 struct vhd_dyn_header header;
270 lba_t blk, blkcnt, nblks;
275 int bat_entries, error, entry;
277 imgsz = image_get_size() * secsz;
278 bat_entries = imgsz / VHD_BLOCK_SIZE;
280 vhd_make_footer(&footer, imgsz, VHD_DISK_TYPE_DYNAMIC, sizeof(footer));
281 if (sparse_write(fd, &footer, sizeof(footer)) < 0)
284 memset(&header, 0, sizeof(header));
285 be64enc(&header.cookie, VHD_HEADER_COOKIE);
286 be64enc(&header.data_offset, ~0ULL);
287 be64enc(&header.table_offset, sizeof(footer) + sizeof(header));
288 be32enc(&header.version, VHD_VERSION);
289 be32enc(&header.max_entries, bat_entries);
290 be32enc(&header.block_size, VHD_BLOCK_SIZE);
291 be32enc(&header.checksum, vhd_checksum(&header, sizeof(header)));
292 if (sparse_write(fd, &header, sizeof(header)) < 0)
295 batsz = bat_entries * sizeof(uint32_t);
296 batsz = (batsz + VHD_SECTOR_SIZE - 1) & ~(VHD_SECTOR_SIZE - 1);
300 memset(bat, 0xff, batsz);
301 blkcnt = VHD_BLOCK_SIZE / secsz;
302 sector = (sizeof(footer) + sizeof(header) + batsz) / VHD_SECTOR_SIZE;
303 for (entry = 0; entry < bat_entries; entry++) {
304 blk = entry * blkcnt;
305 if (image_data(blk, blkcnt)) {
306 be32enc(&bat[entry], sector);
307 sector += (VHD_BLOCK_SIZE / VHD_SECTOR_SIZE) + 1;
310 if (sparse_write(fd, bat, batsz) < 0) {
316 bitmap = malloc(VHD_SECTOR_SIZE);
319 memset(bitmap, 0xff, VHD_SECTOR_SIZE);
322 blkcnt = VHD_BLOCK_SIZE / secsz;
324 nblks = image_get_size();
325 while (blk < nblks) {
326 if (!image_data(blk, blkcnt)) {
330 if (sparse_write(fd, bitmap, VHD_SECTOR_SIZE) < 0) {
334 error = image_copyout_region(fd, blk, blkcnt);
343 if (sparse_write(fd, &footer, sizeof(footer)) < 0)
349 static struct mkimg_format vhd_dyn_format = {
351 .description = "Virtual Hard Disk",
352 .resize = vhd_dyn_resize,
353 .write = vhd_dyn_write,
356 FORMAT_DEFINE(vhd_dyn_format);
363 vhd_fix_resize(lba_t imgsz)
365 struct vhd_geom geom;
369 * Round the image size to the pre-determined geometry that
370 * matches the image size. This circular dependency implies
371 * that we need to loop to handle boundary conditions.
376 vhd_geometry(imagesz, &geom);
377 imagesz = (int64_t)geom.cylinders * geom.heads *
378 geom.sectors * VHD_SECTOR_SIZE;
379 if (imagesz >= imgsz)
381 imagesz += geom.heads * geom.sectors * VHD_SECTOR_SIZE;
384 * Azure demands that images are a whole number of megabytes.
386 imagesz = (imagesz + 0xfffffULL) & ~0xfffffULL;
387 return (image_set_size(imagesz / secsz));
391 vhd_fix_write(int fd)
393 struct vhd_footer footer;
397 error = image_copyout(fd);
399 imgsz = image_get_size() * secsz;
400 vhd_make_footer(&footer, imgsz, VHD_DISK_TYPE_FIXED, ~0ULL);
401 if (sparse_write(fd, &footer, sizeof(footer)) < 0)
407 static struct mkimg_format vhd_fix_format = {
409 .description = "Fixed Virtual Hard Disk",
410 .resize = vhd_fix_resize,
411 .write = vhd_fix_write,
414 FORMAT_DEFINE(vhd_fix_format);