Index: head/usr.bin/mkimg/apm.c =================================================================== --- head/usr.bin/mkimg/apm.c (revision 306329) +++ head/usr.bin/mkimg/apm.c (revision 306330) @@ -1,123 +1,123 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * 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 __FBSDID("$FreeBSD$"); #include #include -#include #include #include #include #include +#include "endian.h" #include "image.h" #include "mkimg.h" #include "scheme.h" #ifndef APM_ENT_TYPE_APPLE_BOOT #define APM_ENT_TYPE_APPLE_BOOT "Apple_Bootstrap" #endif #ifndef APM_ENT_TYPE_FREEBSD_NANDFS #define APM_ENT_TYPE_FREEBSD_NANDFS "FreeBSD-nandfs" #endif static struct mkimg_alias apm_aliases[] = { { ALIAS_FREEBSD, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD) }, { ALIAS_FREEBSD_BOOT, ALIAS_PTR2TYPE(APM_ENT_TYPE_APPLE_BOOT) }, { ALIAS_FREEBSD_NANDFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_NANDFS) }, { ALIAS_FREEBSD_SWAP, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_SWAP) }, { ALIAS_FREEBSD_UFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_UFS) }, { ALIAS_FREEBSD_VINUM, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_VINUM) }, { ALIAS_FREEBSD_ZFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_ZFS) }, { ALIAS_NONE, 0 } }; static lba_t apm_metadata(u_int where, lba_t blk) { blk += (where == SCHEME_META_IMG_START) ? nparts + 2 : 0; return (round_block(blk)); } static int apm_write(lba_t imgsz, void *bootcode __unused) { u_char *buf; struct apm_ddr *ddr; struct apm_ent *ent; struct part *part; int error; buf = calloc(nparts + 2, secsz); if (buf == NULL) return (ENOMEM); ddr = (void *)buf; be16enc(&ddr->ddr_sig, APM_DDR_SIG); be16enc(&ddr->ddr_blksize, secsz); be32enc(&ddr->ddr_blkcount, imgsz); /* partition entry for the partition table itself. */ ent = (void *)(buf + secsz); be16enc(&ent->ent_sig, APM_ENT_SIG); be32enc(&ent->ent_pmblkcnt, nparts + 1); be32enc(&ent->ent_start, 1); be32enc(&ent->ent_size, nparts + 1); strncpy(ent->ent_type, APM_ENT_TYPE_SELF, sizeof(ent->ent_type)); strncpy(ent->ent_name, "Apple", sizeof(ent->ent_name)); STAILQ_FOREACH(part, &partlist, link) { ent = (void *)(buf + (part->index + 2) * secsz); be16enc(&ent->ent_sig, APM_ENT_SIG); be32enc(&ent->ent_pmblkcnt, nparts + 1); be32enc(&ent->ent_start, part->block); be32enc(&ent->ent_size, part->size); strncpy(ent->ent_type, ALIAS_TYPE2PTR(part->type), sizeof(ent->ent_type)); if (part->label != NULL) strncpy(ent->ent_name, part->label, sizeof(ent->ent_name)); } error = image_write(0, buf, nparts + 2); free(buf); return (error); } static struct mkimg_scheme apm_scheme = { .name = "apm", .description = "Apple Partition Map", .aliases = apm_aliases, .metadata = apm_metadata, .write = apm_write, .nparts = 4096, .labellen = APM_ENT_NAMELEN - 1, .maxsecsz = 4096 }; SCHEME_DEFINE(apm_scheme); Index: head/usr.bin/mkimg/bsd.c =================================================================== --- head/usr.bin/mkimg/bsd.c (revision 306329) +++ head/usr.bin/mkimg/bsd.c (revision 306330) @@ -1,139 +1,139 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * 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 __FBSDID("$FreeBSD$"); #include #include -#include #include #include #include #include +#include "endian.h" #include "image.h" #include "mkimg.h" #include "scheme.h" #ifndef FS_NANDFS #define FS_NANDFS 30 #endif static struct mkimg_alias bsd_aliases[] = { { ALIAS_FREEBSD_NANDFS, ALIAS_INT2TYPE(FS_NANDFS) }, { ALIAS_FREEBSD_SWAP, ALIAS_INT2TYPE(FS_SWAP) }, { ALIAS_FREEBSD_UFS, ALIAS_INT2TYPE(FS_BSDFFS) }, { ALIAS_FREEBSD_VINUM, ALIAS_INT2TYPE(FS_VINUM) }, { ALIAS_FREEBSD_ZFS, ALIAS_INT2TYPE(FS_ZFS) }, { ALIAS_NONE, 0 } }; static lba_t bsd_metadata(u_int where, lba_t blk) { if (where == SCHEME_META_IMG_START) blk += BBSIZE / secsz; else if (where == SCHEME_META_IMG_END) blk = round_cylinder(blk); else blk = round_block(blk); return (blk); } static int bsd_write(lba_t imgsz, void *bootcode) { u_char *buf, *p; struct disklabel *d; struct partition *dp; struct part *part; int bsdparts, error, n; uint16_t checksum; buf = malloc(BBSIZE); if (buf == NULL) return (ENOMEM); if (bootcode != NULL) { memcpy(buf, bootcode, BBSIZE); memset(buf + secsz, 0, sizeof(struct disklabel)); } else memset(buf, 0, BBSIZE); bsdparts = nparts + 1; /* Account for c partition */ if (bsdparts < MAXPARTITIONS) bsdparts = MAXPARTITIONS; d = (void *)(buf + secsz); le32enc(&d->d_magic, DISKMAGIC); le32enc(&d->d_secsize, secsz); le32enc(&d->d_nsectors, nsecs); le32enc(&d->d_ntracks, nheads); le32enc(&d->d_ncylinders, ncyls); le32enc(&d->d_secpercyl, nsecs * nheads); le32enc(&d->d_secperunit, imgsz); le16enc(&d->d_rpm, 3600); le32enc(&d->d_magic2, DISKMAGIC); le16enc(&d->d_npartitions, bsdparts); le32enc(&d->d_bbsize, BBSIZE); dp = &d->d_partitions[RAW_PART]; le32enc(&dp->p_size, imgsz); STAILQ_FOREACH(part, &partlist, link) { n = part->index + ((part->index >= RAW_PART) ? 1 : 0); dp = &d->d_partitions[n]; le32enc(&dp->p_size, part->size); le32enc(&dp->p_offset, part->block); le32enc(&dp->p_fsize, 0); dp->p_fstype = ALIAS_TYPE2INT(part->type); dp->p_frag = 0; le16enc(&dp->p_cpg, 0); } dp = &d->d_partitions[bsdparts]; checksum = 0; for (p = (void *)d; p < (u_char *)dp; p += 2) checksum ^= le16dec(p); le16enc(&d->d_checksum, checksum); error = image_write(0, buf, BBSIZE / secsz); free(buf); return (error); } static struct mkimg_scheme bsd_scheme = { .name = "bsd", .description = "BSD disk label", .aliases = bsd_aliases, .metadata = bsd_metadata, .write = bsd_write, .nparts = 19, .bootcode = BBSIZE, .maxsecsz = 512 }; SCHEME_DEFINE(bsd_scheme); Index: head/usr.bin/mkimg/ebr.c =================================================================== --- head/usr.bin/mkimg/ebr.c (revision 306329) +++ head/usr.bin/mkimg/ebr.c (revision 306330) @@ -1,136 +1,136 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * 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 __FBSDID("$FreeBSD$"); #include #include -#include #include #include #include #include +#include "endian.h" #include "image.h" #include "mkimg.h" #include "scheme.h" #ifndef DOSPTYP_FAT16B #define DOSPTYP_FAT16B 0x06 #endif #ifndef DOSPTYP_FAT32 #define DOSPTYP_FAT32 0x0b #endif static struct mkimg_alias ebr_aliases[] = { { ALIAS_FAT16B, ALIAS_INT2TYPE(DOSPTYP_FAT16B) }, { ALIAS_FAT32, ALIAS_INT2TYPE(DOSPTYP_FAT32) }, { ALIAS_FREEBSD, ALIAS_INT2TYPE(DOSPTYP_386BSD) }, { ALIAS_NONE, 0 } }; static lba_t ebr_metadata(u_int where, lba_t blk) { blk += (where == SCHEME_META_PART_BEFORE) ? 1 : 0; return (round_track(blk)); } static void ebr_chs(u_char *cylp, u_char *hdp, u_char *secp, lba_t lba) { u_int cyl, hd, sec; mkimg_chs(lba, 1023, &cyl, &hd, &sec); *cylp = cyl; *hdp = hd; *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0); } static int ebr_write(lba_t imgsz __unused, void *bootcode __unused) { u_char *ebr; struct dos_partition *dp; struct part *part, *next; lba_t block, size; int error; ebr = malloc(secsz); if (ebr == NULL) return (ENOMEM); memset(ebr, 0, secsz); le16enc(ebr + DOSMAGICOFFSET, DOSMAGIC); error = 0; STAILQ_FOREACH_SAFE(part, &partlist, link, next) { block = part->block - nsecs; size = round_track(part->size); dp = (void *)(ebr + DOSPARTOFF); ebr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect, nsecs); dp->dp_typ = ALIAS_TYPE2INT(part->type); ebr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect, part->block + size - 1); le32enc(&dp->dp_start, nsecs); le32enc(&dp->dp_size, size); /* Add link entry */ if (next != NULL) { size = round_track(next->size); dp++; ebr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect, next->block - nsecs); dp->dp_typ = DOSPTYP_EXT; ebr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect, next->block + size - 1); le32enc(&dp->dp_start, next->block - nsecs); le32enc(&dp->dp_size, size + nsecs); } error = image_write(block, ebr, 1); if (error) break; memset(ebr + DOSPARTOFF, 0, 2 * DOSPARTSIZE); } free(ebr); return (error); } static struct mkimg_scheme ebr_scheme = { .name = "ebr", .description = "Extended Boot Record", .aliases = ebr_aliases, .metadata = ebr_metadata, .write = ebr_write, .nparts = 4096, .maxsecsz = 4096 }; SCHEME_DEFINE(ebr_scheme); Index: head/usr.bin/mkimg/endian.h =================================================================== --- head/usr.bin/mkimg/endian.h (nonexistent) +++ head/usr.bin/mkimg/endian.h (revision 306330) @@ -0,0 +1,106 @@ +/*- + * Copyright (c) 2002 Thomas Moestl + * 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. + * + * $FreeBSD$ + */ + +#ifndef _MKIMG_ENDIAN_H_ +#define _MKIMG_ENDIAN_H_ + +static __inline uint16_t +be16dec(const void *pp) +{ + uint8_t const *p = (uint8_t const *)pp; + + return ((p[0] << 8) | p[1]); +} + +static __inline void +be16enc(void *pp, uint16_t u) +{ + uint8_t *p = (uint8_t *)pp; + + p[0] = (u >> 8) & 0xff; + p[1] = u & 0xff; +} + +static __inline void +be32enc(void *pp, uint32_t u) +{ + uint8_t *p = (uint8_t *)pp; + + p[0] = (u >> 24) & 0xff; + p[1] = (u >> 16) & 0xff; + p[2] = (u >> 8) & 0xff; + p[3] = u & 0xff; +} + +static __inline void +be64enc(void *pp, uint64_t u) +{ + uint8_t *p = (uint8_t *)pp; + + be32enc(p, (uint32_t)(u >> 32)); + be32enc(p + 4, (uint32_t)(u & 0xffffffffU)); +} + +static __inline uint16_t +le16dec(const void *pp) +{ + uint8_t const *p = (uint8_t const *)pp; + + return ((p[1] << 8) | p[0]); +} + +static __inline void +le16enc(void *pp, uint16_t u) +{ + uint8_t *p = (uint8_t *)pp; + + p[0] = u & 0xff; + p[1] = (u >> 8) & 0xff; +} + +static __inline void +le32enc(void *pp, uint32_t u) +{ + uint8_t *p = (uint8_t *)pp; + + p[0] = u & 0xff; + p[1] = (u >> 8) & 0xff; + p[2] = (u >> 16) & 0xff; + p[3] = (u >> 24) & 0xff; +} + +static __inline void +le64enc(void *pp, uint64_t u) +{ + uint8_t *p = (uint8_t *)pp; + + le32enc(p, (uint32_t)(u & 0xffffffffU)); + le32enc(p + 4, (uint32_t)(u >> 32)); +} + +#endif /* _MKIMG_ENDIAN_H_ */ Property changes on: head/usr.bin/mkimg/endian.h ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/usr.bin/mkimg/gpt.c =================================================================== --- head/usr.bin/mkimg/gpt.c (revision 306329) +++ head/usr.bin/mkimg/gpt.c (revision 306330) @@ -1,310 +1,310 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * 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 __FBSDID("$FreeBSD$"); #include #include -#include #include #include #include #include #include #include #include #include +#include "endian.h" #include "image.h" #include "mkimg.h" #include "scheme.h" #ifndef GPT_ENT_TYPE_FREEBSD_NANDFS #define GPT_ENT_TYPE_FREEBSD_NANDFS \ {0x74ba7dd9,0xa689,0x11e1,0xbd,0x04,{0x00,0xe0,0x81,0x28,0x6a,0xcf}} #endif static uuid_t gpt_uuid_efi = GPT_ENT_TYPE_EFI; static uuid_t gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; static uuid_t gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; static uuid_t gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS; static uuid_t gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; static uuid_t gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; static uuid_t gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; static uuid_t gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; static uuid_t gpt_uuid_mbr = GPT_ENT_TYPE_MBR; static uuid_t gpt_uuid_ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA; static struct mkimg_alias gpt_aliases[] = { { ALIAS_EFI, ALIAS_PTR2TYPE(&gpt_uuid_efi) }, { ALIAS_FREEBSD, ALIAS_PTR2TYPE(&gpt_uuid_freebsd) }, { ALIAS_FREEBSD_BOOT, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_boot) }, { ALIAS_FREEBSD_NANDFS, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_nandfs) }, { ALIAS_FREEBSD_SWAP, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_swap) }, { ALIAS_FREEBSD_UFS, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_ufs) }, { ALIAS_FREEBSD_VINUM, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_vinum) }, { ALIAS_FREEBSD_ZFS, ALIAS_PTR2TYPE(&gpt_uuid_freebsd_zfs) }, { ALIAS_MBR, ALIAS_PTR2TYPE(&gpt_uuid_mbr) }, { ALIAS_NTFS, ALIAS_PTR2TYPE(&gpt_uuid_ms_basic_data) }, { ALIAS_NONE, 0 } /* Keep last! */ }; /* CRC32 code derived from work by Gary S. Brown. */ static const uint32_t crc32_tab[] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d }; static uint32_t crc32(const void *buf, size_t sz) { const uint8_t *p = (const uint8_t *)buf; uint32_t crc = ~0U; while (sz--) crc = crc32_tab[(crc ^ *p++) & 0xff] ^ (crc >> 8); return (crc ^ ~0U); } static void gpt_uuid_enc(void *buf, const uuid_t *uuid) { uint8_t *p = buf; int i; le32enc(p, uuid->time_low); le16enc(p + 4, uuid->time_mid); le16enc(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 u_int gpt_tblsz(void) { u_int ents; ents = secsz / sizeof(struct gpt_ent); return ((nparts + ents - 1) / ents); } static lba_t gpt_metadata(u_int where, lba_t blk) { if (where == SCHEME_META_IMG_START || where == SCHEME_META_IMG_END) { blk += gpt_tblsz(); blk += (where == SCHEME_META_IMG_START) ? 2 : 1; } return (round_block(blk)); } static int gpt_write_pmbr(lba_t blks, void *bootcode) { u_char *pmbr; uint32_t secs; int error; secs = (blks > UINT32_MAX) ? UINT32_MAX : (uint32_t)blks; pmbr = malloc(secsz); if (pmbr == NULL) return (errno); if (bootcode != NULL) { memcpy(pmbr, bootcode, DOSPARTOFF); memset(pmbr + DOSPARTOFF, 0, secsz - DOSPARTOFF); } else memset(pmbr, 0, secsz); pmbr[DOSPARTOFF + 2] = 2; pmbr[DOSPARTOFF + 4] = 0xee; pmbr[DOSPARTOFF + 5] = 0xff; pmbr[DOSPARTOFF + 6] = 0xff; pmbr[DOSPARTOFF + 7] = 0xff; le32enc(pmbr + DOSPARTOFF + 8, 1); le32enc(pmbr + DOSPARTOFF + 12, secs); le16enc(pmbr + DOSMAGICOFFSET, DOSMAGIC); error = image_write(0, pmbr, 1); free(pmbr); return (error); } static struct gpt_ent * gpt_mktbl(u_int tblsz) { uuid_t uuid; struct gpt_ent *tbl, *ent; struct part *part; int c, idx; tbl = calloc(tblsz, secsz); if (tbl == NULL) return (NULL); STAILQ_FOREACH(part, &partlist, link) { ent = tbl + part->index; gpt_uuid_enc(&ent->ent_type, ALIAS_TYPE2PTR(part->type)); mkimg_uuid(&uuid); gpt_uuid_enc(&ent->ent_uuid, &uuid); le64enc(&ent->ent_lba_start, part->block); le64enc(&ent->ent_lba_end, part->block + part->size - 1); if (part->label != NULL) { idx = 0; while ((c = part->label[idx]) != '\0') { le16enc(ent->ent_name + idx, c); idx++; } } } return (tbl); } static int gpt_write_hdr(struct gpt_hdr *hdr, uint64_t self, uint64_t alt, uint64_t tbl) { uint32_t crc; le64enc(&hdr->hdr_lba_self, self); le64enc(&hdr->hdr_lba_alt, alt); le64enc(&hdr->hdr_lba_table, tbl); hdr->hdr_crc_self = 0; crc = crc32(hdr, offsetof(struct gpt_hdr, padding)); le64enc(&hdr->hdr_crc_self, crc); return (image_write(self, hdr, 1)); } static int gpt_write(lba_t imgsz, void *bootcode) { uuid_t uuid; struct gpt_ent *tbl; struct gpt_hdr *hdr; uint32_t crc; u_int tblsz; int error; /* PMBR */ error = gpt_write_pmbr(imgsz, bootcode); if (error) return (error); /* GPT table(s) */ tblsz = gpt_tblsz(); tbl = gpt_mktbl(tblsz); if (tbl == NULL) return (errno); error = image_write(2, tbl, tblsz); if (error) goto out; error = image_write(imgsz - (tblsz + 1), tbl, tblsz); if (error) goto out; /* GPT header(s) */ hdr = malloc(secsz); if (hdr == NULL) { error = errno; goto out; } memset(hdr, 0, secsz); memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)); le32enc(&hdr->hdr_revision, GPT_HDR_REVISION); le32enc(&hdr->hdr_size, offsetof(struct gpt_hdr, padding)); le64enc(&hdr->hdr_lba_start, 2 + tblsz); le64enc(&hdr->hdr_lba_end, imgsz - tblsz - 2); mkimg_uuid(&uuid); gpt_uuid_enc(&hdr->hdr_uuid, &uuid); le32enc(&hdr->hdr_entries, nparts); le32enc(&hdr->hdr_entsz, sizeof(struct gpt_ent)); crc = crc32(tbl, nparts * sizeof(struct gpt_ent)); le32enc(&hdr->hdr_crc_table, crc); error = gpt_write_hdr(hdr, 1, imgsz - 1, 2); if (!error) error = gpt_write_hdr(hdr, imgsz - 1, 1, imgsz - tblsz - 1); free(hdr); out: free(tbl); return (error); } static struct mkimg_scheme gpt_scheme = { .name = "gpt", .description = "GUID Partition Table", .aliases = gpt_aliases, .metadata = gpt_metadata, .write = gpt_write, .nparts = 4096, .labellen = 36, .bootcode = 512, .maxsecsz = 4096 }; SCHEME_DEFINE(gpt_scheme); Index: head/usr.bin/mkimg/mbr.c =================================================================== --- head/usr.bin/mkimg/mbr.c (revision 306329) +++ head/usr.bin/mkimg/mbr.c (revision 306330) @@ -1,132 +1,132 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * 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 __FBSDID("$FreeBSD$"); #include #include -#include #include #include #include #include +#include "endian.h" #include "image.h" #include "mkimg.h" #include "scheme.h" #ifndef DOSPTYP_FAT16B #define DOSPTYP_FAT16B 0x06 #endif #ifndef DOSPTYP_FAT32 #define DOSPTYP_FAT32 0x0b #endif #ifndef DOSPTYP_PPCBOOT #define DOSPTYP_PPCBOOT 0x41 #endif #ifndef DOSPTYP_EFI #define DOSPTYP_EFI 0xef #endif static struct mkimg_alias mbr_aliases[] = { { ALIAS_EBR, ALIAS_INT2TYPE(DOSPTYP_EXT) }, { ALIAS_EFI, ALIAS_INT2TYPE(DOSPTYP_EFI) }, { ALIAS_FAT16B, ALIAS_INT2TYPE(DOSPTYP_FAT16B) }, { ALIAS_FAT32, ALIAS_INT2TYPE(DOSPTYP_FAT32) }, { ALIAS_FREEBSD, ALIAS_INT2TYPE(DOSPTYP_386BSD) }, { ALIAS_NTFS, ALIAS_INT2TYPE(DOSPTYP_NTFS) }, { ALIAS_PPCBOOT, ALIAS_INT2TYPE(DOSPTYP_PPCBOOT) }, { ALIAS_NONE, 0 } /* Keep last! */ }; static lba_t mbr_metadata(u_int where, lba_t blk) { blk += (where == SCHEME_META_IMG_START) ? 1 : 0; return (round_track(blk)); } static void mbr_chs(u_char *cylp, u_char *hdp, u_char *secp, lba_t lba) { u_int cyl, hd, sec; mkimg_chs(lba, 1023, &cyl, &hd, &sec); *cylp = cyl; *hdp = hd; *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0); } static int mbr_write(lba_t imgsz __unused, void *bootcode) { u_char *mbr; struct dos_partition *dpbase, *dp; struct part *part; lba_t size; int error; mbr = malloc(secsz); if (mbr == NULL) return (ENOMEM); if (bootcode != NULL) { memcpy(mbr, bootcode, DOSPARTOFF); memset(mbr + DOSPARTOFF, 0, secsz - DOSPARTOFF); } else memset(mbr, 0, secsz); le16enc(mbr + DOSMAGICOFFSET, DOSMAGIC); dpbase = (void *)(mbr + DOSPARTOFF); STAILQ_FOREACH(part, &partlist, link) { size = round_track(part->size); dp = dpbase + part->index; dp->dp_flag = (part->index == 0 && bootcode != NULL) ? 0x80 : 0; mbr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect, part->block); dp->dp_typ = ALIAS_TYPE2INT(part->type); mbr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect, part->block + size - 1); le32enc(&dp->dp_start, part->block); le32enc(&dp->dp_size, size); } error = image_write(0, mbr, 1); free(mbr); return (error); } static struct mkimg_scheme mbr_scheme = { .name = "mbr", .description = "Master Boot Record", .aliases = mbr_aliases, .metadata = mbr_metadata, .write = mbr_write, .bootcode = 512, .nparts = NDOSPART, .maxsecsz = 4096 }; SCHEME_DEFINE(mbr_scheme); Index: head/usr.bin/mkimg/pc98.c =================================================================== --- head/usr.bin/mkimg/pc98.c (revision 306329) +++ head/usr.bin/mkimg/pc98.c (revision 306330) @@ -1,130 +1,130 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * 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 __FBSDID("$FreeBSD$"); #include #include -#include #include #include #include #include +#include "endian.h" #include "image.h" #include "mkimg.h" #include "scheme.h" #ifndef PC98_MAGIC #define PC98_MAGIC 0xaa55 #endif #ifndef PC98_MAGICOFS #define PC98_MAGICOFS 510 #endif #ifndef PC98_NPARTS #define PC98_NPARTS 16 #endif #ifndef PC98_PTYP_386BSD #define PC98_PTYP_386BSD 0xc494 #endif #define PC98_BOOTCODESZ 8192 static struct mkimg_alias pc98_aliases[] = { { ALIAS_FREEBSD, ALIAS_INT2TYPE(PC98_PTYP_386BSD) }, { ALIAS_NONE, 0 } }; static lba_t pc98_metadata(u_int where, lba_t blk) { if (where == SCHEME_META_IMG_START) blk += PC98_BOOTCODESZ / secsz; return (round_track(blk)); } static void pc98_chs(u_short *cylp, u_char *hdp, u_char *secp, lba_t lba) { u_int cyl, hd, sec; mkimg_chs(lba, 0xffff, &cyl, &hd, &sec); le16enc(cylp, cyl); *hdp = hd; *secp = sec; } static int pc98_write(lba_t imgsz __unused, void *bootcode) { struct part *part; struct pc98_partition *dpbase, *dp; u_char *buf; lba_t size; int error, ptyp; buf = malloc(PC98_BOOTCODESZ); if (buf == NULL) return (ENOMEM); if (bootcode != NULL) { memcpy(buf, bootcode, PC98_BOOTCODESZ); memset(buf + secsz, 0, secsz); } else memset(buf, 0, PC98_BOOTCODESZ); le16enc(buf + PC98_MAGICOFS, PC98_MAGIC); dpbase = (void *)(buf + secsz); STAILQ_FOREACH(part, &partlist, link) { size = round_track(part->size); dp = dpbase + part->index; ptyp = ALIAS_TYPE2INT(part->type); dp->dp_mid = ptyp; dp->dp_sid = ptyp >> 8; pc98_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect, part->block); pc98_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect, part->block + size - 1); if (part->label != NULL) memcpy(dp->dp_name, part->label, strlen(part->label)); } error = image_write(0, buf, PC98_BOOTCODESZ / secsz); free(buf); return (error); } static struct mkimg_scheme pc98_scheme = { .name = "pc98", .description = "PC-9800 disk partitions", .aliases = pc98_aliases, .metadata = pc98_metadata, .write = pc98_write, .bootcode = PC98_BOOTCODESZ, .labellen = 16, .nparts = PC98_NPARTS, .maxsecsz = 512 }; SCHEME_DEFINE(pc98_scheme); Index: head/usr.bin/mkimg/qcow.c =================================================================== --- head/usr.bin/mkimg/qcow.c (revision 306329) +++ head/usr.bin/mkimg/qcow.c (revision 306330) @@ -1,370 +1,370 @@ /*- * Copyright (c) 2014 Marcel Moolenaar * 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 __FBSDID("$FreeBSD$"); #include -#include #include #include #include #include #include #include #include +#include "endian.h" #include "image.h" #include "format.h" #include "mkimg.h" /* Default cluster sizes. */ #define QCOW1_CLSTR_LOG2SZ 12 /* 4KB */ #define QCOW2_CLSTR_LOG2SZ 16 /* 64KB */ /* Flag bits in cluster offsets */ #define QCOW_CLSTR_COMPRESSED (1ULL << 62) #define QCOW_CLSTR_COPIED (1ULL << 63) struct qcow_header { uint32_t magic; #define QCOW_MAGIC 0x514649fb uint32_t version; #define QCOW_VERSION_1 1 #define QCOW_VERSION_2 2 uint64_t path_offset; uint32_t path_length; uint32_t clstr_log2sz; /* v2 only */ uint64_t disk_size; union { struct { uint8_t clstr_log2sz; uint8_t l2_log2sz; uint16_t _pad; uint32_t encryption; uint64_t l1_offset; } v1; struct { uint32_t encryption; uint32_t l1_entries; uint64_t l1_offset; uint64_t refcnt_offset; uint32_t refcnt_clstrs; uint32_t snapshot_count; uint64_t snapshot_offset; } v2; } u; }; static u_int clstr_log2sz; static uint64_t round_clstr(uint64_t ofs) { uint64_t clstrsz; clstrsz = 1UL << clstr_log2sz; return ((ofs + clstrsz - 1) & ~(clstrsz - 1)); } static int qcow_resize(lba_t imgsz, u_int version) { uint64_t imagesz; switch (version) { case QCOW_VERSION_1: clstr_log2sz = QCOW1_CLSTR_LOG2SZ; break; case QCOW_VERSION_2: clstr_log2sz = QCOW2_CLSTR_LOG2SZ; break; default: assert(0); } imagesz = round_clstr(imgsz * secsz); if (verbose) fprintf(stderr, "QCOW: image size = %ju, cluster size = %u\n", (uintmax_t)imagesz, (u_int)(1U << clstr_log2sz)); return (image_set_size(imagesz / secsz)); } static int qcow1_resize(lba_t imgsz) { return (qcow_resize(imgsz, QCOW_VERSION_1)); } static int qcow2_resize(lba_t imgsz) { return (qcow_resize(imgsz, QCOW_VERSION_2)); } static int qcow_write(int fd, u_int version) { struct qcow_header *hdr; uint64_t *l1tbl, *l2tbl, *rctbl; uint16_t *rcblk; uint64_t clstr_imgsz, clstr_l2tbls, clstr_l1tblsz; uint64_t clstr_rcblks, clstr_rctblsz; uint64_t n, imagesz, nclstrs, ofs, ofsflags; lba_t blk, blkofs, blk_imgsz; u_int l1clno, l2clno, rcclno; u_int blk_clstrsz, refcnt_clstrs; u_int clstrsz, l1idx, l2idx; int error; assert(clstr_log2sz != 0); clstrsz = 1U << clstr_log2sz; blk_clstrsz = clstrsz / secsz; blk_imgsz = image_get_size(); imagesz = blk_imgsz * secsz; clstr_imgsz = imagesz >> clstr_log2sz; clstr_l2tbls = round_clstr(clstr_imgsz * 8) >> clstr_log2sz; clstr_l1tblsz = round_clstr(clstr_l2tbls * 8) >> clstr_log2sz; nclstrs = clstr_imgsz + clstr_l2tbls + clstr_l1tblsz + 1; clstr_rcblks = clstr_rctblsz = 0; do { n = clstr_rcblks + clstr_rctblsz; clstr_rcblks = round_clstr((nclstrs + n) * 2) >> clstr_log2sz; clstr_rctblsz = round_clstr(clstr_rcblks * 8) >> clstr_log2sz; } while (n < (clstr_rcblks + clstr_rctblsz)); /* * We got all the sizes in clusters. Start the layout. * 0 - header * 1 - L1 table * 2 - RC table (v2 only) * 3 - L2 tables * 4 - RC block (v2 only) * 5 - data */ l1clno = 1; rcclno = 0; rctbl = l2tbl = l1tbl = NULL; rcblk = NULL; hdr = calloc(1, clstrsz); if (hdr == NULL) return (errno); be32enc(&hdr->magic, QCOW_MAGIC); be32enc(&hdr->version, version); be64enc(&hdr->disk_size, imagesz); switch (version) { case QCOW_VERSION_1: ofsflags = 0; l2clno = l1clno + clstr_l1tblsz; hdr->u.v1.clstr_log2sz = clstr_log2sz; hdr->u.v1.l2_log2sz = clstr_log2sz - 3; be64enc(&hdr->u.v1.l1_offset, clstrsz * l1clno); break; case QCOW_VERSION_2: ofsflags = QCOW_CLSTR_COPIED; rcclno = l1clno + clstr_l1tblsz; l2clno = rcclno + clstr_rctblsz; be32enc(&hdr->clstr_log2sz, clstr_log2sz); be32enc(&hdr->u.v2.l1_entries, clstr_l2tbls); be64enc(&hdr->u.v2.l1_offset, clstrsz * l1clno); be64enc(&hdr->u.v2.refcnt_offset, clstrsz * rcclno); refcnt_clstrs = round_clstr(clstr_rcblks * 8) >> clstr_log2sz; be32enc(&hdr->u.v2.refcnt_clstrs, refcnt_clstrs); break; default: assert(0); } if (sparse_write(fd, hdr, clstrsz) < 0) { error = errno; goto out; } free(hdr); hdr = NULL; ofs = clstrsz * l2clno; nclstrs = 1 + clstr_l1tblsz + clstr_rctblsz; l1tbl = calloc(1, clstrsz * clstr_l1tblsz); if (l1tbl == NULL) { error = ENOMEM; goto out; } for (n = 0; n < clstr_imgsz; n++) { blk = n * blk_clstrsz; if (image_data(blk, blk_clstrsz)) { nclstrs++; l1idx = n >> (clstr_log2sz - 3); if (l1tbl[l1idx] == 0) { be64enc(l1tbl + l1idx, ofs + ofsflags); ofs += clstrsz; nclstrs++; } } } if (sparse_write(fd, l1tbl, clstrsz * clstr_l1tblsz) < 0) { error = errno; goto out; } clstr_rcblks = 0; do { n = clstr_rcblks; clstr_rcblks = round_clstr((nclstrs + n) * 2) >> clstr_log2sz; } while (n < clstr_rcblks); if (rcclno > 0) { rctbl = calloc(1, clstrsz * clstr_rctblsz); if (rctbl == NULL) { error = ENOMEM; goto out; } for (n = 0; n < clstr_rcblks; n++) { be64enc(rctbl + n, ofs); ofs += clstrsz; nclstrs++; } if (sparse_write(fd, rctbl, clstrsz * clstr_rctblsz) < 0) { error = errno; goto out; } free(rctbl); rctbl = NULL; } l2tbl = malloc(clstrsz); if (l2tbl == NULL) { error = ENOMEM; goto out; } for (l1idx = 0; l1idx < clstr_l2tbls; l1idx++) { if (l1tbl[l1idx] == 0) continue; memset(l2tbl, 0, clstrsz); blkofs = (lba_t)l1idx * blk_clstrsz * (clstrsz >> 3); for (l2idx = 0; l2idx < (clstrsz >> 3); l2idx++) { blk = blkofs + (lba_t)l2idx * blk_clstrsz; if (blk >= blk_imgsz) break; if (image_data(blk, blk_clstrsz)) { be64enc(l2tbl + l2idx, ofs + ofsflags); ofs += clstrsz; } } if (sparse_write(fd, l2tbl, clstrsz) < 0) { error = errno; goto out; } } free(l2tbl); l2tbl = NULL; free(l1tbl); l1tbl = NULL; if (rcclno > 0) { rcblk = calloc(1, clstrsz * clstr_rcblks); if (rcblk == NULL) { error = ENOMEM; goto out; } for (n = 0; n < nclstrs; n++) be16enc(rcblk + n, 1); if (sparse_write(fd, rcblk, clstrsz * clstr_rcblks) < 0) { error = errno; goto out; } free(rcblk); rcblk = NULL; } error = 0; for (n = 0; n < clstr_imgsz; n++) { blk = n * blk_clstrsz; if (image_data(blk, blk_clstrsz)) { error = image_copyout_region(fd, blk, blk_clstrsz); if (error) break; } } if (!error) error = image_copyout_done(fd); out: if (rcblk != NULL) free(rcblk); if (l2tbl != NULL) free(l2tbl); if (rctbl != NULL) free(rctbl); if (l1tbl != NULL) free(l1tbl); if (hdr != NULL) free(hdr); return (error); } static int qcow1_write(int fd) { return (qcow_write(fd, QCOW_VERSION_1)); } static int qcow2_write(int fd) { return (qcow_write(fd, QCOW_VERSION_2)); } static struct mkimg_format qcow1_format = { .name = "qcow", .description = "QEMU Copy-On-Write, version 1", .resize = qcow1_resize, .write = qcow1_write, }; FORMAT_DEFINE(qcow1_format); static struct mkimg_format qcow2_format = { .name = "qcow2", .description = "QEMU Copy-On-Write, version 2", .resize = qcow2_resize, .write = qcow2_write, }; FORMAT_DEFINE(qcow2_format); Index: head/usr.bin/mkimg/raw.c =================================================================== --- head/usr.bin/mkimg/raw.c (revision 306329) +++ head/usr.bin/mkimg/raw.c (revision 306330) @@ -1,62 +1,61 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * 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 __FBSDID("$FreeBSD$"); #include -#include #include #include #include #include #include "image.h" #include "format.h" #include "mkimg.h" static int raw_resize(lba_t imgsz __unused) { return (0); } static int raw_write(int fd) { return (image_copyout(fd)); } static struct mkimg_format raw_format = { .name = "raw", .description = "Raw Disk", .resize = raw_resize, .write = raw_write, }; FORMAT_DEFINE(raw_format); Index: head/usr.bin/mkimg/vhd.c =================================================================== --- head/usr.bin/mkimg/vhd.c (revision 306329) +++ head/usr.bin/mkimg/vhd.c (revision 306330) @@ -1,432 +1,432 @@ /*- * Copyright (c) 2014, 2015 Marcel Moolenaar * 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 __FBSDID("$FreeBSD$"); #include -#include #include #include #include #include #include #include +#include "endian.h" #include "image.h" #include "format.h" #include "mkimg.h" #ifndef __has_extension #define __has_extension(x) 0 #endif /* * General notes: * o File is in network byte order. * o The timestamp is seconds since 1/1/2000 12:00:00 AM UTC * * This file is divided in 3 parts: * 1. Common definitions * 2. Dynamic VHD support * 3. Fixed VHD support */ /* * PART 1: Common definitions */ #define VHD_SECTOR_SIZE 512 #define VHD_BLOCK_SIZE (4096 * VHD_SECTOR_SIZE) /* 2MB blocks */ struct vhd_geom { uint16_t cylinders; uint8_t heads; uint8_t sectors; }; struct vhd_footer { uint64_t cookie; #define VHD_FOOTER_COOKIE 0x636f6e6563746978ULL 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; #define VHD_CREATOR_VERSION 0x00020000 uint32_t creator_os; #define VHD_CREATOR_OS 0x5769326b /* Wi2k */ uint64_t original_size; uint64_t current_size; struct vhd_geom geometry; 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]; }; #if __has_extension(c_static_assert) _Static_assert(sizeof(struct vhd_footer) == VHD_SECTOR_SIZE, "Wrong size for footer"); #endif 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 vhd_geometry(uint64_t image_size, struct vhd_geom *geom) { lba_t imgsz; long cth; imgsz = image_size / VHD_SECTOR_SIZE; /* Respect command line options if possible. */ if (nheads > 1 && nheads < 256 && nsecs > 1 && nsecs < 256 && ncyls < 65536) { geom->cylinders = (ncyls != 0) ? ncyls : imgsz / (nheads * nsecs); geom->heads = nheads; geom->sectors = nsecs; return; } if (imgsz > 65536 * 16 * 255) imgsz = 65536 * 16 * 255; if (imgsz >= 65535 * 16 * 63) { geom->cylinders = imgsz / (16 * 255); geom->heads = 16; geom->sectors = 255; return; } geom->sectors = 17; cth = imgsz / 17; 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; cth = imgsz / 31; } if (cth >= (geom->heads * 1024)) { geom->heads = 16; geom->sectors = 63; cth = imgsz / 63; } geom->cylinders = cth / geom->heads; } static uint64_t vhd_resize(uint64_t origsz) { struct vhd_geom geom; uint64_t newsz; /* * 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. * The first time, newsz equals origsz and the geometry will * typically yield a new size that's smaller. We keep adding * cylinder's worth of sectors to the new size until its * larger or equal or origsz. But during those iterations, * the geometry can change, so we need to account for that. */ newsz = origsz; while (1) { vhd_geometry(newsz, &geom); newsz = (int64_t)geom.cylinders * geom.heads * geom.sectors * VHD_SECTOR_SIZE; if (newsz >= origsz) break; newsz += geom.heads * geom.sectors * VHD_SECTOR_SIZE; } return (newsz); } 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); vhd_geometry(image_size, &footer->geometry); be16enc(&footer->geometry.cylinders, footer->geometry.cylinders); 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; #define VHD_HEADER_COOKIE 0x6378737061727365ULL 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]; }; #if __has_extension(c_static_assert) _Static_assert(sizeof(struct vhd_dyn_header) == VHD_SECTOR_SIZE * 2, "Wrong size for header"); #endif static int vhd_dyn_resize(lba_t imgsz) { uint64_t imagesz; imagesz = vhd_resize(imgsz * secsz); return (image_set_size(imagesz / secsz)); } static int vhd_dyn_write(int fd) { struct vhd_footer footer; struct vhd_dyn_header header; uint64_t imgsz, rawsz; lba_t blk, blkcnt, nblks; uint32_t *bat; void *bitmap; size_t batsz; uint32_t sector; int bat_entries, error, entry; rawsz = image_get_size() * secsz; imgsz = (rawsz + VHD_BLOCK_SIZE - 1) & ~(VHD_BLOCK_SIZE - 1); vhd_make_footer(&footer, rawsz, VHD_DISK_TYPE_DYNAMIC, sizeof(footer)); if (sparse_write(fd, &footer, sizeof(footer)) < 0) return (errno); bat_entries = imgsz / VHD_BLOCK_SIZE; 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); blkcnt = VHD_BLOCK_SIZE / secsz; sector = (sizeof(footer) + sizeof(header) + batsz) / VHD_SECTOR_SIZE; for (entry = 0; entry < bat_entries; entry++) { blk = entry * blkcnt; if (image_data(blk, blkcnt)) { be32enc(&bat[entry], sector); sector += (VHD_BLOCK_SIZE / VHD_SECTOR_SIZE) + 1; } } 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; blkcnt = VHD_BLOCK_SIZE / secsz; error = 0; nblks = rawsz / secsz; while (blk < nblks) { if (!image_data(blk, blkcnt)) { blk += blkcnt; continue; } if (sparse_write(fd, bitmap, VHD_SECTOR_SIZE) < 0) { error = errno; break; } /* Handle partial last block */ if (blk + blkcnt > nblks) blkcnt = nblks - blk; error = image_copyout_region(fd, blk, blkcnt); if (error) break; blk += blkcnt; } free(bitmap); if (error) return (error); error = image_copyout_zeroes(fd, imgsz - rawsz); if (error) return (error); if (sparse_write(fd, &footer, sizeof(footer)) < 0) return (errno); return (0); } static struct mkimg_format vhd_dyn_format = { .name = "vhd", .description = "Virtual Hard Disk", .resize = vhd_dyn_resize, .write = vhd_dyn_write, }; FORMAT_DEFINE(vhd_dyn_format); /* * PART 3: Fixed VHD */ static int vhd_fix_resize(lba_t imgsz) { uint64_t imagesz; imagesz = vhd_resize(imgsz * secsz); /* * Azure demands that images are a whole number of megabytes. */ imagesz = (imagesz + 0xfffffULL) & ~0xfffffULL; return (image_set_size(imagesz / secsz)); } static int vhd_fix_write(int fd) { struct vhd_footer footer; uint64_t imagesz; int error; error = image_copyout(fd); if (error) return (error); imagesz = image_get_size() * secsz; vhd_make_footer(&footer, imagesz, VHD_DISK_TYPE_FIXED, ~0ULL); error = (sparse_write(fd, &footer, sizeof(footer)) < 0) ? errno : 0; return (error); } static struct mkimg_format vhd_fix_format = { .name = "vhdf", .description = "Fixed Virtual Hard Disk", .resize = vhd_fix_resize, .write = vhd_fix_write, }; FORMAT_DEFINE(vhd_fix_format); Index: head/usr.bin/mkimg/vmdk.c =================================================================== --- head/usr.bin/mkimg/vmdk.c (revision 306329) +++ head/usr.bin/mkimg/vmdk.c (revision 306330) @@ -1,261 +1,261 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * 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 __FBSDID("$FreeBSD$"); #include -#include #include #include #include #include #include #include +#include "endian.h" #include "image.h" #include "format.h" #include "mkimg.h" #define VMDK_IMAGE_ROUND 1048576 #define VMDK_MIN_GRAIN_SIZE 8192 #define VMDK_SECTOR_SIZE 512 struct vmdk_header { uint32_t magic; #define VMDK_MAGIC 0x564d444b uint32_t version; #define VMDK_VERSION 1 uint32_t flags; #define VMDK_FLAGS_NL_TEST (1 << 0) #define VMDK_FLAGS_RGT_USED (1 << 1) #define VMDK_FLAGS_COMPRESSED (1 << 16) #define VMDK_FLAGS_MARKERS (1 << 17) uint64_t capacity; uint64_t grain_size; uint64_t desc_offset; uint64_t desc_size; uint32_t ngtes; #define VMDK_NGTES 512 uint64_t rgd_offset; uint64_t gd_offset; uint64_t overhead; uint8_t unclean; uint32_t nl_test; #define VMDK_NL_TEST 0x0a200d0a uint16_t compress; #define VMDK_COMPRESS_NONE 0 #define VMDK_COMPRESS_DEFLATE 1 char padding[433]; } __attribute__((__packed__)); static const char desc_fmt[] = "# Disk DescriptorFile\n" "version=%d\n" "CID=%08x\n" "parentCID=ffffffff\n" "createType=\"monolithicSparse\"\n" "# Extent description\n" "RW %ju SPARSE \"%s\"\n" "# The Disk Data Base\n" "#DDB\n" "ddb.adapterType = \"ide\"\n" "ddb.geometry.cylinders = \"%u\"\n" "ddb.geometry.heads = \"%u\"\n" "ddb.geometry.sectors = \"%u\"\n"; static uint64_t grainsz; static int vmdk_resize(lba_t imgsz) { uint64_t imagesz; imagesz = imgsz * secsz; imagesz = (imagesz + VMDK_IMAGE_ROUND - 1) & ~(VMDK_IMAGE_ROUND - 1); grainsz = (blksz < VMDK_MIN_GRAIN_SIZE) ? VMDK_MIN_GRAIN_SIZE : blksz; if (verbose) fprintf(stderr, "VMDK: image size = %ju, grain size = %ju\n", (uintmax_t)imagesz, (uintmax_t)grainsz); grainsz /= VMDK_SECTOR_SIZE; return (image_set_size(imagesz / secsz)); } static int vmdk_write(int fd) { struct vmdk_header hdr; uint32_t *gt, *gd, *rgd; char *buf, *desc; off_t cur, lim; uint64_t imagesz; lba_t blkofs, blkcnt; size_t gdsz, gtsz; uint32_t sec, cursec; int error, desc_len, n, ngrains, ngts; imagesz = (image_get_size() * secsz) / VMDK_SECTOR_SIZE; memset(&hdr, 0, sizeof(hdr)); le32enc(&hdr.magic, VMDK_MAGIC); le32enc(&hdr.version, VMDK_VERSION); le32enc(&hdr.flags, VMDK_FLAGS_NL_TEST | VMDK_FLAGS_RGT_USED); le64enc(&hdr.capacity, imagesz); le64enc(&hdr.grain_size, grainsz); n = asprintf(&desc, desc_fmt, 1 /*version*/, 0 /*CID*/, (uintmax_t)imagesz /*size*/, "" /*name*/, ncyls /*cylinders*/, nheads /*heads*/, nsecs /*sectors*/); if (n == -1) return (ENOMEM); desc_len = (n + VMDK_SECTOR_SIZE - 1) & ~(VMDK_SECTOR_SIZE - 1); desc = realloc(desc, desc_len); memset(desc + n, 0, desc_len - n); le64enc(&hdr.desc_offset, 1); le64enc(&hdr.desc_size, desc_len / VMDK_SECTOR_SIZE); le32enc(&hdr.ngtes, VMDK_NGTES); sec = desc_len / VMDK_SECTOR_SIZE + 1; ngrains = imagesz / grainsz; ngts = (ngrains + VMDK_NGTES - 1) / VMDK_NGTES; gdsz = (ngts * sizeof(uint32_t) + VMDK_SECTOR_SIZE - 1) & ~(VMDK_SECTOR_SIZE - 1); gd = calloc(1, gdsz); if (gd == NULL) { free(desc); return (ENOMEM); } le64enc(&hdr.gd_offset, sec); sec += gdsz / VMDK_SECTOR_SIZE; for (n = 0; n < ngts; n++) { le32enc(gd + n, sec); sec += VMDK_NGTES * sizeof(uint32_t) / VMDK_SECTOR_SIZE; } rgd = calloc(1, gdsz); if (rgd == NULL) { free(gd); free(desc); return (ENOMEM); } le64enc(&hdr.rgd_offset, sec); sec += gdsz / VMDK_SECTOR_SIZE; for (n = 0; n < ngts; n++) { le32enc(rgd + n, sec); sec += VMDK_NGTES * sizeof(uint32_t) / VMDK_SECTOR_SIZE; } sec = (sec + grainsz - 1) & ~(grainsz - 1); if (verbose) fprintf(stderr, "VMDK: overhead = %ju\n", (uintmax_t)(sec * VMDK_SECTOR_SIZE)); le64enc(&hdr.overhead, sec); be32enc(&hdr.nl_test, VMDK_NL_TEST); gt = calloc(ngts, VMDK_NGTES * sizeof(uint32_t)); if (gt == NULL) { free(rgd); free(gd); free(desc); return (ENOMEM); } gtsz = ngts * VMDK_NGTES * sizeof(uint32_t); cursec = sec; blkcnt = (grainsz * VMDK_SECTOR_SIZE) / secsz; for (n = 0; n < ngrains; n++) { blkofs = n * blkcnt; if (image_data(blkofs, blkcnt)) { le32enc(gt + n, cursec); cursec += grainsz; } } error = 0; if (!error && sparse_write(fd, &hdr, VMDK_SECTOR_SIZE) < 0) error = errno; if (!error && sparse_write(fd, desc, desc_len) < 0) error = errno; if (!error && sparse_write(fd, gd, gdsz) < 0) error = errno; if (!error && sparse_write(fd, gt, gtsz) < 0) error = errno; if (!error && sparse_write(fd, rgd, gdsz) < 0) error = errno; if (!error && sparse_write(fd, gt, gtsz) < 0) error = errno; free(gt); free(rgd); free(gd); free(desc); if (error) return (error); cur = VMDK_SECTOR_SIZE + desc_len + (gdsz + gtsz) * 2; lim = sec * VMDK_SECTOR_SIZE; if (cur < lim) { buf = calloc(1, VMDK_SECTOR_SIZE); if (buf == NULL) error = ENOMEM; while (!error && cur < lim) { if (sparse_write(fd, buf, VMDK_SECTOR_SIZE) < 0) error = errno; cur += VMDK_SECTOR_SIZE; } if (buf != NULL) free(buf); } if (error) return (error); blkcnt = (grainsz * VMDK_SECTOR_SIZE) / secsz; for (n = 0; n < ngrains; n++) { blkofs = n * blkcnt; if (image_data(blkofs, blkcnt)) { error = image_copyout_region(fd, blkofs, blkcnt); if (error) return (error); } } return (image_copyout_done(fd)); } static struct mkimg_format vmdk_format = { .name = "vmdk", .description = "Virtual Machine Disk", .resize = vmdk_resize, .write = vmdk_write, }; FORMAT_DEFINE(vmdk_format); Index: head/usr.bin/mkimg/vtoc8.c =================================================================== --- head/usr.bin/mkimg/vtoc8.c (revision 306329) +++ head/usr.bin/mkimg/vtoc8.c (revision 306330) @@ -1,118 +1,118 @@ /*- * Copyright (c) 2014 Juniper Networks, Inc. * 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 __FBSDID("$FreeBSD$"); #include -#include #include #include #include #include #include #include +#include "endian.h" #include "image.h" #include "mkimg.h" #include "scheme.h" #ifndef VTOC_TAG_FREEBSD_NANDFS #define VTOC_TAG_FREEBSD_NANDFS 0x0905 #endif static struct mkimg_alias vtoc8_aliases[] = { { ALIAS_FREEBSD_NANDFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_NANDFS) }, { ALIAS_FREEBSD_SWAP, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_SWAP) }, { ALIAS_FREEBSD_UFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_UFS) }, { ALIAS_FREEBSD_VINUM, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_VINUM) }, { ALIAS_FREEBSD_ZFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_NANDFS) }, { ALIAS_NONE, 0 } }; static lba_t vtoc8_metadata(u_int where, lba_t blk) { blk += (where == SCHEME_META_IMG_START) ? 1 : 0; return (round_cylinder(blk)); } static int vtoc8_write(lba_t imgsz, void *bootcode __unused) { struct vtoc8 vtoc8; struct part *part; u_char *p; int error, n; uint16_t ofs, sum; imgsz = (lba_t)ncyls * nheads * nsecs; memset(&vtoc8, 0, sizeof(vtoc8)); sprintf(vtoc8.ascii, "FreeBSD%lldM", (long long)(imgsz * secsz / 1048576)); be32enc(&vtoc8.version, VTOC_VERSION); be16enc(&vtoc8.nparts, VTOC8_NPARTS); be32enc(&vtoc8.sanity, VTOC_SANITY); be16enc(&vtoc8.rpm, 3600); be16enc(&vtoc8.physcyls, ncyls); be16enc(&vtoc8.ncyls, ncyls); be16enc(&vtoc8.altcyls, 0); be16enc(&vtoc8.nheads, nheads); be16enc(&vtoc8.nsecs, nsecs); be16enc(&vtoc8.magic, VTOC_MAGIC); be32enc(&vtoc8.map[VTOC_RAW_PART].nblks, imgsz); STAILQ_FOREACH(part, &partlist, link) { n = part->index + ((part->index >= VTOC_RAW_PART) ? 1 : 0); be16enc(&vtoc8.part[n].tag, ALIAS_TYPE2INT(part->type)); be32enc(&vtoc8.map[n].cyl, part->block / (nsecs * nheads)); be32enc(&vtoc8.map[n].nblks, part->size); } /* Calculate checksum. */ sum = 0; p = (void *)&vtoc8; for (ofs = 0; ofs < sizeof(vtoc8) - 2; ofs += 2) sum ^= be16dec(p + ofs); be16enc(&vtoc8.cksum, sum); error = image_write(0, &vtoc8, 1); return (error); } static struct mkimg_scheme vtoc8_scheme = { .name = "vtoc8", .description = "SMI VTOC8 disk labels", .aliases = vtoc8_aliases, .metadata = vtoc8_metadata, .write = vtoc8_write, .nparts = VTOC8_NPARTS - 1, .maxsecsz = 512 }; SCHEME_DEFINE(vtoc8_scheme);