vhd.c (242b24828472137ec4411826b86e753d49bd2c39) vhd.c (429971147386e142afa18df15e878df6735f364b)
1/*-
2 * Copyright (c) 2014, 2015 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 19 unchanged lines hidden (view full) ---

28__FBSDID("$FreeBSD$");
29
30#include <sys/errno.h>
31#include <stdint.h>
32#include <stdlib.h>
33#include <string.h>
34#include <time.h>
35#include <unistd.h>
1/*-
2 * Copyright (c) 2014, 2015 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 19 unchanged lines hidden (view full) ---

28__FBSDID("$FreeBSD$");
29
30#include <sys/errno.h>
31#include <stdint.h>
32#include <stdlib.h>
33#include <string.h>
34#include <time.h>
35#include <unistd.h>
36#include <uuid.h>
37
38#include "endian.h"
39#include "image.h"
40#include "format.h"
41#include "mkimg.h"
42
43#ifndef __has_extension
44#define __has_extension(x) 0

--- 42 unchanged lines hidden (view full) ---

87 uint64_t original_size;
88 uint64_t current_size;
89 struct vhd_geom geometry;
90 uint32_t disk_type;
91#define VHD_DISK_TYPE_FIXED 2
92#define VHD_DISK_TYPE_DYNAMIC 3
93#define VHD_DISK_TYPE_DIFF 4
94 uint32_t checksum;
36
37#include "endian.h"
38#include "image.h"
39#include "format.h"
40#include "mkimg.h"
41
42#ifndef __has_extension
43#define __has_extension(x) 0

--- 42 unchanged lines hidden (view full) ---

86 uint64_t original_size;
87 uint64_t current_size;
88 struct vhd_geom geometry;
89 uint32_t disk_type;
90#define VHD_DISK_TYPE_FIXED 2
91#define VHD_DISK_TYPE_DYNAMIC 3
92#define VHD_DISK_TYPE_DIFF 4
93 uint32_t checksum;
95 uuid_t id;
94 mkimg_uuid_t id;
96 uint8_t saved_state;
97 uint8_t _reserved[427];
98};
99#if __has_extension(c_static_assert)
100_Static_assert(sizeof(struct vhd_footer) == VHD_SECTOR_SIZE,
101 "Wrong size for footer");
102#endif
103

--- 92 unchanged lines hidden (view full) ---

196 t = time(NULL);
197 return (t - 0x386d4380);
198 }
199
200 return (0x01234567);
201}
202
203static void
95 uint8_t saved_state;
96 uint8_t _reserved[427];
97};
98#if __has_extension(c_static_assert)
99_Static_assert(sizeof(struct vhd_footer) == VHD_SECTOR_SIZE,
100 "Wrong size for footer");
101#endif
102

--- 92 unchanged lines hidden (view full) ---

195 t = time(NULL);
196 return (t - 0x386d4380);
197 }
198
199 return (0x01234567);
200}
201
202static void
204vhd_uuid_enc(void *buf, const uuid_t *uuid)
205{
206 uint8_t *p = buf;
207 int i;
208
209 be32enc(p, uuid->time_low);
210 be16enc(p + 4, uuid->time_mid);
211 be16enc(p + 6, uuid->time_hi_and_version);
212 p[8] = uuid->clock_seq_hi_and_reserved;
213 p[9] = uuid->clock_seq_low;
214 for (i = 0; i < _UUID_NODE_LEN; i++)
215 p[10 + i] = uuid->node[i];
216}
217
218static void
219vhd_make_footer(struct vhd_footer *footer, uint64_t image_size,
220 uint32_t disk_type, uint64_t data_offset)
221{
203vhd_make_footer(struct vhd_footer *footer, uint64_t image_size,
204 uint32_t disk_type, uint64_t data_offset)
205{
222 uuid_t id;
206 mkimg_uuid_t id;
223
224 memset(footer, 0, sizeof(*footer));
225 be64enc(&footer->cookie, VHD_FOOTER_COOKIE);
226 be32enc(&footer->features, VHD_FEATURES_RESERVED);
227 be32enc(&footer->version, VHD_VERSION);
228 be64enc(&footer->data_offset, data_offset);
229 be32enc(&footer->timestamp, vhd_timestamp());
230 be32enc(&footer->creator_tool, VHD_CREATOR_TOOL);
231 be32enc(&footer->creator_version, VHD_CREATOR_VERSION);
232 be32enc(&footer->creator_os, VHD_CREATOR_OS);
233 be64enc(&footer->original_size, image_size);
234 be64enc(&footer->current_size, image_size);
235 vhd_geometry(image_size, &footer->geometry);
236 be16enc(&footer->geometry.cylinders, footer->geometry.cylinders);
237 be32enc(&footer->disk_type, disk_type);
238 mkimg_uuid(&id);
207
208 memset(footer, 0, sizeof(*footer));
209 be64enc(&footer->cookie, VHD_FOOTER_COOKIE);
210 be32enc(&footer->features, VHD_FEATURES_RESERVED);
211 be32enc(&footer->version, VHD_VERSION);
212 be64enc(&footer->data_offset, data_offset);
213 be32enc(&footer->timestamp, vhd_timestamp());
214 be32enc(&footer->creator_tool, VHD_CREATOR_TOOL);
215 be32enc(&footer->creator_version, VHD_CREATOR_VERSION);
216 be32enc(&footer->creator_os, VHD_CREATOR_OS);
217 be64enc(&footer->original_size, image_size);
218 be64enc(&footer->current_size, image_size);
219 vhd_geometry(image_size, &footer->geometry);
220 be16enc(&footer->geometry.cylinders, footer->geometry.cylinders);
221 be32enc(&footer->disk_type, disk_type);
222 mkimg_uuid(&id);
239 vhd_uuid_enc(&footer->id, &id);
223 mkimg_uuid_enc(&footer->id, &id);
240 be32enc(&footer->checksum, vhd_checksum(footer, sizeof(*footer)));
241}
242
243/*
244 * PART 2: Dynamic VHD support
245 *
246 * Notes:
247 * o File layout:

--- 8 unchanged lines hidden (view full) ---

256 uint64_t cookie;
257#define VHD_HEADER_COOKIE 0x6378737061727365ULL
258 uint64_t data_offset;
259 uint64_t table_offset;
260 uint32_t version;
261 uint32_t max_entries;
262 uint32_t block_size;
263 uint32_t checksum;
224 be32enc(&footer->checksum, vhd_checksum(footer, sizeof(*footer)));
225}
226
227/*
228 * PART 2: Dynamic VHD support
229 *
230 * Notes:
231 * o File layout:

--- 8 unchanged lines hidden (view full) ---

240 uint64_t cookie;
241#define VHD_HEADER_COOKIE 0x6378737061727365ULL
242 uint64_t data_offset;
243 uint64_t table_offset;
244 uint32_t version;
245 uint32_t max_entries;
246 uint32_t block_size;
247 uint32_t checksum;
264 uuid_t parent_id;
248 mkimg_uuid_t parent_id;
265 uint32_t parent_timestamp;
266 char _reserved1[4];
267 uint16_t parent_name[256]; /* UTF-16 */
268 struct {
269 uint32_t code;
270 uint32_t data_space;
271 uint32_t data_length;
272 uint32_t _reserved;

--- 160 unchanged lines hidden ---
249 uint32_t parent_timestamp;
250 char _reserved1[4];
251 uint16_t parent_name[256]; /* UTF-16 */
252 struct {
253 uint32_t code;
254 uint32_t data_space;
255 uint32_t data_length;
256 uint32_t _reserved;

--- 160 unchanged lines hidden ---