2014-07-03 20:31:43 +00:00
|
|
|
/*-
|
2015-06-11 15:18:54 +00:00
|
|
|
* Copyright (c) 2014, 2015 Marcel Moolenaar
|
2014-07-03 20:31:43 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/endian.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <uuid.h>
|
|
|
|
|
|
|
|
#include "image.h"
|
|
|
|
#include "format.h"
|
|
|
|
#include "mkimg.h"
|
|
|
|
|
2014-07-23 18:05:39 +00:00
|
|
|
#ifndef __has_extension
|
|
|
|
#define __has_extension(x) 0
|
|
|
|
#endif
|
|
|
|
|
2014-07-03 20:31:43 +00:00
|
|
|
/*
|
2014-07-17 16:33:38 +00:00
|
|
|
* General notes:
|
2014-07-03 20:31:43 +00:00
|
|
|
* o File is in network byte order.
|
|
|
|
* o The timestamp is seconds since 1/1/2000 12:00:00 AM UTC
|
2014-07-17 16:33:38 +00:00
|
|
|
*
|
|
|
|
* This file is divided in 3 parts:
|
|
|
|
* 1. Common definitions
|
|
|
|
* 2. Dynamic VHD support
|
|
|
|
* 3. Fixed VHD support
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PART 1: Common definitions
|
2014-07-03 20:31:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define VHD_SECTOR_SIZE 512
|
|
|
|
#define VHD_BLOCK_SIZE (4096 * VHD_SECTOR_SIZE) /* 2MB blocks */
|
|
|
|
|
2015-06-11 14:30:31 +00:00
|
|
|
struct vhd_geom {
|
|
|
|
uint16_t cylinders;
|
|
|
|
uint8_t heads;
|
|
|
|
uint8_t sectors;
|
|
|
|
};
|
|
|
|
|
2014-07-03 20:31:43 +00:00
|
|
|
struct vhd_footer {
|
|
|
|
uint64_t cookie;
|
2014-10-01 20:37:15 +00:00
|
|
|
#define VHD_FOOTER_COOKIE 0x636f6e6563746978ULL
|
2014-07-03 20:31:43 +00:00
|
|
|
uint32_t features;
|
|
|
|
#define VHD_FEATURES_TEMPORARY 0x01
|
|
|
|
#define VHD_FEATURES_RESERVED 0x02
|
|
|
|
uint32_t version;
|
|
|
|
#define VHD_VERSION 0x00010000
|
|
|
|
uint64_t data_offset;
|
|
|
|
uint32_t timestamp;
|
|
|
|
uint32_t creator_tool;
|
|
|
|
#define VHD_CREATOR_TOOL 0x2a696d67 /* FreeBSD mkimg */
|
|
|
|
uint32_t creator_version;
|
2015-06-21 02:45:31 +00:00
|
|
|
#define VHD_CREATOR_VERSION 0x00020000
|
2014-07-03 20:31:43 +00:00
|
|
|
uint32_t creator_os;
|
2015-06-21 02:45:31 +00:00
|
|
|
#define VHD_CREATOR_OS 0x5769326b /* Wi2k */
|
2014-07-03 20:31:43 +00:00
|
|
|
uint64_t original_size;
|
|
|
|
uint64_t current_size;
|
2015-06-11 14:30:31 +00:00
|
|
|
struct vhd_geom geometry;
|
2014-07-03 20:31:43 +00:00
|
|
|
uint32_t disk_type;
|
|
|
|
#define VHD_DISK_TYPE_FIXED 2
|
|
|
|
#define VHD_DISK_TYPE_DYNAMIC 3
|
|
|
|
#define VHD_DISK_TYPE_DIFF 4
|
|
|
|
uint32_t checksum;
|
|
|
|
uuid_t id;
|
|
|
|
uint8_t saved_state;
|
|
|
|
uint8_t _reserved[427];
|
|
|
|
};
|
2014-07-23 18:05:39 +00:00
|
|
|
#if __has_extension(c_static_assert)
|
2014-07-03 20:31:43 +00:00
|
|
|
_Static_assert(sizeof(struct vhd_footer) == VHD_SECTOR_SIZE,
|
|
|
|
"Wrong size for footer");
|
2014-07-23 18:05:39 +00:00
|
|
|
#endif
|
2014-07-03 20:31:43 +00:00
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
vhd_checksum(void *buf, size_t sz)
|
|
|
|
{
|
|
|
|
uint8_t *p = buf;
|
|
|
|
uint32_t sum;
|
|
|
|
size_t ofs;
|
|
|
|
|
|
|
|
sum = 0;
|
|
|
|
for (ofs = 0; ofs < sz; ofs++)
|
|
|
|
sum += p[ofs];
|
|
|
|
return (~sum);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-06-11 14:30:31 +00:00
|
|
|
vhd_geometry(uint64_t image_size, struct vhd_geom *geom)
|
2014-07-03 20:31:43 +00:00
|
|
|
{
|
|
|
|
lba_t imgsz;
|
|
|
|
long cth;
|
|
|
|
|
2015-06-11 15:18:54 +00:00
|
|
|
imgsz = image_size / VHD_SECTOR_SIZE;
|
|
|
|
|
2014-07-03 20:31:43 +00:00
|
|
|
/* Respect command line options if possible. */
|
|
|
|
if (nheads > 1 && nheads < 256 &&
|
|
|
|
nsecs > 1 && nsecs < 256 &&
|
|
|
|
ncyls < 65536) {
|
2015-06-11 15:18:54 +00:00
|
|
|
geom->cylinders = (ncyls != 0) ? ncyls :
|
|
|
|
imgsz / (nheads * nsecs);
|
2015-06-11 14:30:31 +00:00
|
|
|
geom->heads = nheads;
|
|
|
|
geom->sectors = nsecs;
|
2014-07-03 20:31:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imgsz > 65536 * 16 * 255)
|
|
|
|
imgsz = 65536 * 16 * 255;
|
|
|
|
if (imgsz >= 65535 * 16 * 63) {
|
2015-06-11 14:30:31 +00:00
|
|
|
geom->cylinders = imgsz / (16 * 255);
|
|
|
|
geom->heads = 16;
|
|
|
|
geom->sectors = 255;
|
2014-07-03 20:31:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-06-11 14:30:31 +00:00
|
|
|
geom->sectors = 17;
|
2014-07-03 20:31:43 +00:00
|
|
|
cth = imgsz / 17;
|
2015-06-11 14:30:31 +00:00
|
|
|
geom->heads = (cth + 1023) / 1024;
|
|
|
|
if (geom->heads < 4)
|
|
|
|
geom->heads = 4;
|
|
|
|
if (cth >= (geom->heads * 1024) || geom->heads > 16) {
|
|
|
|
geom->heads = 16;
|
|
|
|
geom->sectors = 31;
|
2014-07-03 20:31:43 +00:00
|
|
|
cth = imgsz / 31;
|
|
|
|
}
|
2015-06-11 14:30:31 +00:00
|
|
|
if (cth >= (geom->heads * 1024)) {
|
|
|
|
geom->heads = 16;
|
|
|
|
geom->sectors = 63;
|
2014-07-03 20:31:43 +00:00
|
|
|
cth = imgsz / 63;
|
|
|
|
}
|
2015-06-11 14:30:31 +00:00
|
|
|
geom->cylinders = cth / geom->heads;
|
2014-07-03 20:31:43 +00:00
|
|
|
}
|
|
|
|
|
2014-07-17 16:33:38 +00:00
|
|
|
static uint32_t
|
|
|
|
vhd_timestamp(void)
|
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
if (!unit_testing) {
|
|
|
|
t = time(NULL);
|
|
|
|
return (t - 0x386d4380);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0x01234567);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vhd_uuid_enc(void *buf, const uuid_t *uuid)
|
|
|
|
{
|
|
|
|
uint8_t *p = buf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
be32enc(p, uuid->time_low);
|
|
|
|
be16enc(p + 4, uuid->time_mid);
|
|
|
|
be16enc(p + 6, uuid->time_hi_and_version);
|
|
|
|
p[8] = uuid->clock_seq_hi_and_reserved;
|
|
|
|
p[9] = uuid->clock_seq_low;
|
|
|
|
for (i = 0; i < _UUID_NODE_LEN; i++)
|
|
|
|
p[10 + i] = uuid->node[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vhd_make_footer(struct vhd_footer *footer, uint64_t image_size,
|
|
|
|
uint32_t disk_type, uint64_t data_offset)
|
|
|
|
{
|
|
|
|
uuid_t id;
|
|
|
|
|
|
|
|
memset(footer, 0, sizeof(*footer));
|
|
|
|
be64enc(&footer->cookie, VHD_FOOTER_COOKIE);
|
|
|
|
be32enc(&footer->features, VHD_FEATURES_RESERVED);
|
|
|
|
be32enc(&footer->version, VHD_VERSION);
|
|
|
|
be64enc(&footer->data_offset, data_offset);
|
|
|
|
be32enc(&footer->timestamp, vhd_timestamp());
|
|
|
|
be32enc(&footer->creator_tool, VHD_CREATOR_TOOL);
|
|
|
|
be32enc(&footer->creator_version, VHD_CREATOR_VERSION);
|
|
|
|
be32enc(&footer->creator_os, VHD_CREATOR_OS);
|
|
|
|
be64enc(&footer->original_size, image_size);
|
|
|
|
be64enc(&footer->current_size, image_size);
|
2015-06-11 14:30:31 +00:00
|
|
|
vhd_geometry(image_size, &footer->geometry);
|
|
|
|
be16enc(&footer->geometry.cylinders, footer->geometry.cylinders);
|
2014-07-17 16:33:38 +00:00
|
|
|
be32enc(&footer->disk_type, disk_type);
|
|
|
|
mkimg_uuid(&id);
|
|
|
|
vhd_uuid_enc(&footer->id, &id);
|
|
|
|
be32enc(&footer->checksum, vhd_checksum(footer, sizeof(*footer)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PART 2: Dynamic VHD support
|
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
* o File layout:
|
|
|
|
* copy of disk footer
|
|
|
|
* dynamic disk header
|
|
|
|
* block allocation table (BAT)
|
|
|
|
* data blocks
|
|
|
|
* disk footer
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct vhd_dyn_header {
|
|
|
|
uint64_t cookie;
|
2014-10-01 20:37:15 +00:00
|
|
|
#define VHD_HEADER_COOKIE 0x6378737061727365ULL
|
2014-07-17 16:33:38 +00:00
|
|
|
uint64_t data_offset;
|
|
|
|
uint64_t table_offset;
|
|
|
|
uint32_t version;
|
|
|
|
uint32_t max_entries;
|
|
|
|
uint32_t block_size;
|
|
|
|
uint32_t checksum;
|
|
|
|
uuid_t parent_id;
|
|
|
|
uint32_t parent_timestamp;
|
|
|
|
char _reserved1[4];
|
|
|
|
uint16_t parent_name[256]; /* UTF-16 */
|
|
|
|
struct {
|
|
|
|
uint32_t code;
|
|
|
|
uint32_t data_space;
|
|
|
|
uint32_t data_length;
|
|
|
|
uint32_t _reserved;
|
|
|
|
uint64_t data_offset;
|
|
|
|
} parent_locator[8];
|
|
|
|
char _reserved2[256];
|
|
|
|
};
|
2014-07-23 18:05:39 +00:00
|
|
|
#if __has_extension(c_static_assert)
|
2014-07-17 16:33:38 +00:00
|
|
|
_Static_assert(sizeof(struct vhd_dyn_header) == VHD_SECTOR_SIZE * 2,
|
|
|
|
"Wrong size for header");
|
2014-07-23 18:05:39 +00:00
|
|
|
#endif
|
2014-07-17 16:33:38 +00:00
|
|
|
|
2015-06-11 14:30:31 +00:00
|
|
|
static int
|
|
|
|
vhd_dyn_resize(lba_t imgsz)
|
|
|
|
{
|
|
|
|
uint64_t imagesz;
|
|
|
|
|
|
|
|
imagesz = imgsz * secsz;
|
|
|
|
imagesz = (imagesz + VHD_BLOCK_SIZE - 1) & ~(VHD_BLOCK_SIZE - 1);
|
|
|
|
return (image_set_size(imagesz / secsz));
|
|
|
|
}
|
|
|
|
|
2014-07-17 16:33:38 +00:00
|
|
|
static int
|
|
|
|
vhd_dyn_write(int fd)
|
2014-07-03 20:31:43 +00:00
|
|
|
{
|
|
|
|
struct vhd_footer footer;
|
|
|
|
struct vhd_dyn_header header;
|
|
|
|
uint64_t imgsz;
|
2014-07-15 04:39:23 +00:00
|
|
|
lba_t blk, blkcnt, nblks;
|
2014-07-03 20:31:43 +00:00
|
|
|
uint32_t *bat;
|
|
|
|
void *bitmap;
|
|
|
|
size_t batsz;
|
|
|
|
uint32_t sector;
|
|
|
|
int bat_entries, error, entry;
|
|
|
|
|
|
|
|
imgsz = image_get_size() * secsz;
|
|
|
|
bat_entries = imgsz / VHD_BLOCK_SIZE;
|
|
|
|
|
2014-07-17 16:33:38 +00:00
|
|
|
vhd_make_footer(&footer, imgsz, VHD_DISK_TYPE_DYNAMIC, sizeof(footer));
|
2014-07-03 20:31:43 +00:00
|
|
|
if (sparse_write(fd, &footer, sizeof(footer)) < 0)
|
|
|
|
return (errno);
|
|
|
|
|
|
|
|
memset(&header, 0, sizeof(header));
|
|
|
|
be64enc(&header.cookie, VHD_HEADER_COOKIE);
|
|
|
|
be64enc(&header.data_offset, ~0ULL);
|
|
|
|
be64enc(&header.table_offset, sizeof(footer) + sizeof(header));
|
|
|
|
be32enc(&header.version, VHD_VERSION);
|
|
|
|
be32enc(&header.max_entries, bat_entries);
|
|
|
|
be32enc(&header.block_size, VHD_BLOCK_SIZE);
|
|
|
|
be32enc(&header.checksum, vhd_checksum(&header, sizeof(header)));
|
|
|
|
if (sparse_write(fd, &header, sizeof(header)) < 0)
|
|
|
|
return (errno);
|
|
|
|
|
|
|
|
batsz = bat_entries * sizeof(uint32_t);
|
|
|
|
batsz = (batsz + VHD_SECTOR_SIZE - 1) & ~(VHD_SECTOR_SIZE - 1);
|
|
|
|
bat = malloc(batsz);
|
|
|
|
if (bat == NULL)
|
|
|
|
return (errno);
|
|
|
|
memset(bat, 0xff, batsz);
|
2014-07-15 04:39:23 +00:00
|
|
|
blkcnt = VHD_BLOCK_SIZE / secsz;
|
2014-07-03 20:31:43 +00:00
|
|
|
sector = (sizeof(footer) + sizeof(header) + batsz) / VHD_SECTOR_SIZE;
|
|
|
|
for (entry = 0; entry < bat_entries; entry++) {
|
2014-07-15 04:39:23 +00:00
|
|
|
blk = entry * blkcnt;
|
|
|
|
if (image_data(blk, blkcnt)) {
|
|
|
|
be32enc(&bat[entry], sector);
|
|
|
|
sector += (VHD_BLOCK_SIZE / VHD_SECTOR_SIZE) + 1;
|
|
|
|
}
|
2014-07-03 20:31:43 +00:00
|
|
|
}
|
|
|
|
if (sparse_write(fd, bat, batsz) < 0) {
|
|
|
|
free(bat);
|
|
|
|
return (errno);
|
|
|
|
}
|
|
|
|
free(bat);
|
|
|
|
|
|
|
|
bitmap = malloc(VHD_SECTOR_SIZE);
|
|
|
|
if (bitmap == NULL)
|
|
|
|
return (errno);
|
|
|
|
memset(bitmap, 0xff, VHD_SECTOR_SIZE);
|
|
|
|
|
|
|
|
blk = 0;
|
2014-07-15 04:39:23 +00:00
|
|
|
blkcnt = VHD_BLOCK_SIZE / secsz;
|
2014-07-15 23:35:06 +00:00
|
|
|
error = 0;
|
2014-07-03 20:31:43 +00:00
|
|
|
nblks = image_get_size();
|
|
|
|
while (blk < nblks) {
|
2014-07-15 04:39:23 +00:00
|
|
|
if (!image_data(blk, blkcnt)) {
|
|
|
|
blk += blkcnt;
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-03 20:31:43 +00:00
|
|
|
if (sparse_write(fd, bitmap, VHD_SECTOR_SIZE) < 0) {
|
|
|
|
error = errno;
|
|
|
|
break;
|
|
|
|
}
|
2014-07-15 04:39:23 +00:00
|
|
|
error = image_copyout_region(fd, blk, blkcnt);
|
2014-07-03 20:31:43 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
2014-07-15 04:39:23 +00:00
|
|
|
blk += blkcnt;
|
2014-07-03 20:31:43 +00:00
|
|
|
}
|
|
|
|
free(bitmap);
|
|
|
|
if (blk != nblks)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
if (sparse_write(fd, &footer, sizeof(footer)) < 0)
|
|
|
|
return (errno);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-07-17 16:33:38 +00:00
|
|
|
static struct mkimg_format vhd_dyn_format = {
|
2014-07-03 20:31:43 +00:00
|
|
|
.name = "vhd",
|
|
|
|
.description = "Virtual Hard Disk",
|
2015-06-11 14:30:31 +00:00
|
|
|
.resize = vhd_dyn_resize,
|
2014-07-17 16:33:38 +00:00
|
|
|
.write = vhd_dyn_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
FORMAT_DEFINE(vhd_dyn_format);
|
|
|
|
|
|
|
|
/*
|
2015-06-11 14:30:31 +00:00
|
|
|
* PART 3: Fixed VHD
|
2014-07-17 16:33:38 +00:00
|
|
|
*/
|
|
|
|
|
2015-06-11 14:30:31 +00:00
|
|
|
static int
|
|
|
|
vhd_fix_resize(lba_t imgsz)
|
|
|
|
{
|
|
|
|
struct vhd_geom geom;
|
|
|
|
int64_t imagesz;
|
|
|
|
|
2015-06-21 01:44:27 +00:00
|
|
|
/*
|
|
|
|
* Round the image size to the pre-determined geometry that
|
|
|
|
* matches the image size. This circular dependency implies
|
|
|
|
* that we need to loop to handle boundary conditions.
|
|
|
|
*/
|
2015-06-11 14:30:31 +00:00
|
|
|
imgsz *= secsz;
|
|
|
|
imagesz = imgsz;
|
|
|
|
while (1) {
|
|
|
|
vhd_geometry(imagesz, &geom);
|
|
|
|
imagesz = (int64_t)geom.cylinders * geom.heads *
|
|
|
|
geom.sectors * VHD_SECTOR_SIZE;
|
|
|
|
if (imagesz >= imgsz)
|
|
|
|
break;
|
|
|
|
imagesz += geom.heads * geom.sectors * VHD_SECTOR_SIZE;
|
|
|
|
}
|
2015-06-21 01:44:27 +00:00
|
|
|
/*
|
|
|
|
* Azure demands that images are a whole number of megabytes.
|
|
|
|
*/
|
|
|
|
imagesz = (imagesz + 0xfffffULL) & ~0xfffffULL;
|
2015-06-11 14:30:31 +00:00
|
|
|
return (image_set_size(imagesz / secsz));
|
|
|
|
}
|
|
|
|
|
2014-07-17 16:33:38 +00:00
|
|
|
static int
|
|
|
|
vhd_fix_write(int fd)
|
|
|
|
{
|
|
|
|
struct vhd_footer footer;
|
|
|
|
uint64_t imgsz;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = image_copyout(fd);
|
|
|
|
if (!error) {
|
|
|
|
imgsz = image_get_size() * secsz;
|
|
|
|
vhd_make_footer(&footer, imgsz, VHD_DISK_TYPE_FIXED, ~0ULL);
|
|
|
|
if (sparse_write(fd, &footer, sizeof(footer)) < 0)
|
|
|
|
error = errno;
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct mkimg_format vhd_fix_format = {
|
|
|
|
.name = "vhdf",
|
|
|
|
.description = "Fixed Virtual Hard Disk",
|
2015-06-11 14:30:31 +00:00
|
|
|
.resize = vhd_fix_resize,
|
2014-07-17 16:33:38 +00:00
|
|
|
.write = vhd_fix_write,
|
2014-07-03 20:31:43 +00:00
|
|
|
};
|
|
|
|
|
2014-07-17 16:33:38 +00:00
|
|
|
FORMAT_DEFINE(vhd_fix_format);
|