summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Olbrich <m.olbrich@pengutronix.de>2019-02-15 10:34:15 +0000
committerGitHub <noreply@github.com>2019-02-15 10:34:15 +0000
commitd5211c023cf6f2b2dc508cdfbf5f66f44d2e8d01 (patch)
tree0188947737f9a61389351e643f912d15910b4906
parent4186db82b6a5ae2a3ae6ee2520c37bf9ccf8240c (diff)
parent7412992cd800719ea3620ae9fe7f9c07d8394914 (diff)
downloadgenimage-d5211c023cf6f2b2dc508cdfbf5f66f44d2e8d01.tar.gz
genimage-d5211c023cf6f2b2dc508cdfbf5f66f44d2e8d01.tar.xz
Merge pull request #51 from ssorensen/gpt
hdimage: Add GPT support
-rw-r--r--.travis.yml2
-rw-r--r--Makefile.am3
-rw-r--r--README.rst18
-rw-r--r--crc32.c60
-rw-r--r--genimage.c10
-rw-r--r--genimage.h11
-rw-r--r--image-hd.c299
-rwxr-xr-xtest/basic-images.test14
-rw-r--r--test/hdimage4.config40
-rw-r--r--test/hdimage4.fdisk7
-rw-r--r--util.c68
11 files changed, 494 insertions, 38 deletions
diff --git a/.travis.yml b/.travis.yml
index 2d2251d..395937e 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,6 @@
language: c
compiler: gcc
-dist: trusty
+dist: xenial
addons:
apt:
packages:
diff --git a/Makefile.am b/Makefile.am
index 8455bfc..3c96229 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -16,6 +16,7 @@ genimage_SOURCES = \
genimage.c \
config.c \
util.c \
+ crc32.c \
image-cpio.c \
image-cramfs.c \
image-ext2.c \
@@ -63,6 +64,8 @@ EXTRA_DIST += \
test/hdimage2.config \
test/hdimage.fdisk \
test/hdimage.fdisk-2 \
+ test/hdimage4.config \
+ test/hdimage4.fdisk \
test/iso.config \
test/jffs2.config \
test/jffs2.md5 \
diff --git a/README.rst b/README.rst
index 17a0314..67a648a 100644
--- a/README.rst
+++ b/README.rst
@@ -98,6 +98,18 @@ Partition options:
:bootable: Boolean specifying whether to set the bootable flag.
:in-partition-table: Boolean specifying whether to include this partition in
the partition table.
+:partition-uuid: UUID string used by GPT partition tables to specify the partition
+ id. Defaults to a random value.
+:partition-type-uuid: String used by GPT partition tables to specify the partition type.
+ Either a UUID or a shortcut:
+ * L: Linux filesystem (0fc63daf-8483-4772-8e79-3d69d8477de4)
+ * S: Swap (0657fd6d-a4ab-43c4-84e5-0933c84b4f4f)
+ * H: Home (933ac7e1-2eb4-4f13-b844-0e14e2aef915)
+ * U: EFI System (c12a7328-f81f-11d2-ba4b-00a0c93ec93b)
+ * R: Linux RAID (a19d880f-05fc-4d3b-a006-743f0f84911e)
+ * V: Linux LVM (e6d6d379-f507-44c2-a23c-238f2a3df928)
+ * F: FAT32 / Basic Data Partition (ebd0a0a2-b9e5-4433-87c0-68b6b72699c7)
+ Defaults to L.
The image configuration options
-------------------------------
@@ -164,12 +176,16 @@ Generates DOS partition images.
Options:
:align: Partition alignment. Defaults to 512 bytes
-:partition-table: Boolean. If true, writes a DOS partition table. If false, no
+:partition-table: Boolean. If true, writes a partition table. If false, no
partition table is generated. Defaults to true.
:extended-partition: Number of the extended partition. Contains the number of the
extended partition between 1 and 4 or 0 for automatic. Defaults
to 0.
:disk-signature: 32 bit integer used as disk signature (offset 440 in the MBR)
+:gpt: Boolean. If true, a GPT type partion table is written. If false
+ a DOS type partition table is written. Defaults to false.
+:disk-uuid: UUID string used as disk id in GPT partitioning. Defaults to a
+ random value.
iso
***
diff --git a/crc32.c b/crc32.c
new file mode 100644
index 0000000..7fe30f6
--- /dev/null
+++ b/crc32.c
@@ -0,0 +1,60 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include "genimage.h"
+
+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
+};
+
+uint32_t crc32(const void *data, size_t len)
+{
+ uint32_t crc = ~0;
+ const char *p = data;
+
+ while(len--)
+ crc = crc32_tab[(crc ^ *p++) & 0xff] ^ (crc >> 8);
+
+ return ~crc;
+}
diff --git a/genimage.c b/genimage.c
index d7014cf..0be3f5c 100644
--- a/genimage.c
+++ b/genimage.c
@@ -20,6 +20,7 @@
#include <string.h>
#include <errno.h>
#include <libgen.h>
+#include <sys/time.h>
#include <sys/types.h>
#include <dirent.h>
@@ -94,6 +95,8 @@ static cfg_opt_t partition_opts[] = {
CFG_STR("image", NULL, CFGF_NONE),
CFG_BOOL("autoresize", 0, CFGF_NONE),
CFG_BOOL("in-partition-table", cfg_true, CFGF_NONE),
+ CFG_STR("partition-uuid", NULL, CFGF_NONE),
+ CFG_STR("partition-type-uuid", "L", CFGF_NONE),
CFG_END()
};
@@ -302,6 +305,8 @@ static int parse_partitions(struct image *image, cfg_t *imagesec)
part->image = cfg_getstr(partsec, "image");
part->autoresize = cfg_getbool(partsec, "autoresize");
part->in_partition_table = cfg_getbool(partsec, "in-partition-table");
+ part->partition_type_uuid = cfg_getstr(partsec, "partition-type-uuid");
+ part->partition_uuid = cfg_getstr(partsec, "partition-uuid");
}
return 0;
@@ -589,6 +594,11 @@ int main(int argc, char *argv[])
cfg_opt_t image_end[] = {
CFG_END()
};
+ struct timeval tv;
+
+ /* Seed the rng */
+ gettimeofday(&tv, NULL);
+ srandom(tv.tv_usec);
memcpy(imageopts, image_common_opts, sizeof(image_common_opts));
diff --git a/genimage.h b/genimage.h
index d243f3c..60874e9 100644
--- a/genimage.h
+++ b/genimage.h
@@ -1,6 +1,8 @@
#ifndef __PTX_IMAGE_H
#define __PTX_IMAGE_H
+#include <stdint.h>
+#include <confuse.h>
#include "list.h"
struct image_handler;
@@ -41,6 +43,8 @@ struct partition {
int autoresize;
int in_partition_table;
const char *name;
+ const char *partition_type_uuid;
+ const char *partition_uuid;
};
struct image {
@@ -142,4 +146,11 @@ static inline const char *imageoutfile(const struct image *image)
{
return image->outfile;
}
+
+int uuid_validate(const char *str);
+void uuid_parse(const char *str, unsigned char *uuid);
+char *uuid_random(void);
+
+uint32_t crc32(const void *data, size_t len);
+
#endif /* __PTX_IMAGE_H */
diff --git a/image-hd.c b/image-hd.c
index 9555148..25fced3 100644
--- a/image-hd.c
+++ b/image-hd.c
@@ -21,6 +21,7 @@
#include <stdlib.h>
#include <errno.h>
#include <inttypes.h>
+#include <endian.h>
#include "genimage.h"
@@ -30,9 +31,11 @@ struct hdimage {
unsigned long long align;
unsigned long long extended_lba;
uint32_t disksig;
+ const char *disk_uuid;
+ cfg_bool_t gpt;
};
-struct partition_entry {
+struct mbr_partition_entry {
unsigned char boot;
unsigned char first_chs[3];
@@ -45,6 +48,36 @@ struct partition_entry {
uint32_t total_sectors;
} __attribute__((packed));
+struct gpt_header {
+ unsigned char signature[8];
+ uint32_t revision;
+ uint32_t header_size;
+ uint32_t header_crc;
+ uint32_t reserved;
+ uint64_t current_lba;
+ uint64_t backup_lba;
+ uint64_t first_usable_lba;
+ uint64_t last_usable_lba;
+ unsigned char disk_uuid[16];
+ uint64_t starting_lba;
+ uint32_t number_entries;
+ uint32_t entry_size;
+ uint32_t table_crc;
+} __attribute__((packed));
+
+struct gpt_partition_entry {
+ unsigned char type_uuid[16];
+ unsigned char uuid[16];
+ uint64_t first_lba;
+ uint64_t last_lba;
+ uint64_t flags;
+ uint16_t name[36];
+} __attribute__((packed));
+
+#define GPT_ENTRIES 128
+#define GPT_SECTORS (1 + GPT_ENTRIES * sizeof(struct gpt_partition_entry) / 512)
+#define GPT_REVISION_1_0 0x00010000
+
static void hdimage_setup_chs(unsigned int lba, unsigned char *chs)
{
const unsigned int hpc = 255;
@@ -58,25 +91,27 @@ static void hdimage_setup_chs(unsigned int lba, unsigned char *chs)
chs[2] = (c & 0xff);
}
-static int hdimage_setup_mbr(struct image *image, char *part_table)
+static int hdimage_insert_mbr(struct image *image, struct list_head *partitions)
{
struct hdimage *hd = image->handler_priv;
+ char mbr[6+4*sizeof(struct mbr_partition_entry)+2], *part_table;
struct partition *part;
- int i = 0;
+ int ret, i = 0;
image_info(image, "writing MBR\n");
- *((int*)part_table) = hd->disksig;
- part_table += 6;
+ memset(mbr, 0, sizeof(mbr));
+ memcpy(mbr, &hd->disksig, sizeof(hd->disksig));
+ part_table = mbr + 6;
- list_for_each_entry(part, &image->partitions, list) {
- struct partition_entry *entry;
+ list_for_each_entry(part, partitions, list) {
+ struct mbr_partition_entry *entry;
if (!part->in_partition_table)
continue;
- entry = (struct partition_entry *)(part_table + i *
- sizeof(struct partition_entry));
+ entry = (struct mbr_partition_entry *)(part_table + i *
+ sizeof(struct mbr_partition_entry));
entry->boot = part->bootable ? 0x80 : 0x00;
if (!part->extended) {
@@ -87,7 +122,7 @@ static int hdimage_setup_mbr(struct image *image, char *part_table)
else {
unsigned long long size = 0;
struct partition *p = part;
- list_for_each_entry_from(p, &image->partitions, list) {
+ list_for_each_entry_from(p, partitions, list) {
if (!p->extended)
break;
size += hd->align + p->size;
@@ -104,20 +139,31 @@ static int hdimage_setup_mbr(struct image *image, char *part_table)
break;
i++;
}
- part_table += 4 * sizeof(struct partition_entry);
+ part_table += 4 * sizeof(struct mbr_partition_entry);
part_table[0] = 0x55;
part_table[1] = 0xaa;
+
+ ret = insert_data(image, mbr, imageoutfile(image), sizeof(mbr), 440);
+ if (ret) {
+ image_error(image, "failed to write MBR\n");
+ return ret;
+ }
+
return 0;
}
-static int hdimage_setup_ebr(struct image *image, struct partition *part, char *ebr)
+static int hdimage_insert_ebr(struct image *image, struct partition *part)
{
struct hdimage *hd = image->handler_priv;
- struct partition_entry *entry;
+ struct mbr_partition_entry *entry;
+ char ebr[4*sizeof(struct mbr_partition_entry)+2], *part_table;
+ int ret;
image_info(image, "writing EBR\n");
- entry = (struct partition_entry *)ebr;
+ memset(ebr, 0, sizeof(ebr));
+ part_table = ebr;
+ entry = (struct mbr_partition_entry *)part_table;
entry->boot = 0x00;
entry->partition_type = part->partition_type;
@@ -139,12 +185,147 @@ static int hdimage_setup_ebr(struct image *image, struct partition *part, char *
break;
}
- ebr += 4 * sizeof(struct partition_entry);
- ebr[0] = 0x55;
- ebr[1] = 0xaa;
+ part_table += 4 * sizeof(struct mbr_partition_entry);
+ part_table[0] = 0x55;
+ part_table[1] = 0xaa;
+
+ ret = insert_data(image, ebr, imageoutfile(image), sizeof(ebr),
+ part->offset - hd->align + 446);
+ if (ret) {
+ image_error(image, "failed to write EBR\n");
+ return ret;
+ }
+
+ return 0;
+}
+
+static const char *
+gpt_partition_type_lookup(char shortcut)
+{
+ switch(shortcut) {
+ case 'L': return "0fc63daf-8483-4772-8e79-3d69d8477de4";
+ case 'S': return "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f";
+ case 'H': return "933ac7e1-2eb4-4f13-b844-0e14e2aef915";
+ case 'U': return "c12a7328-f81f-11d2-ba4b-00a0c93ec93b";
+ case 'R': return "a19d880f-05fc-4d3b-a006-743f0f84911e";
+ case 'V': return "e6d6d379-f507-44c2-a23c-238f2a3df928";
+ case 'F': return "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7";
+ }
+ return NULL;
+}
+
+
+static int hdimage_insert_protective_mbr(struct image *image)
+{
+ struct partition mbr;
+ struct list_head mbr_list = LIST_HEAD_INIT(mbr_list);
+ int ret = 0;
+
+ image_info(image, "writing protective MBR\n");
+
+ memset(&mbr, 0, sizeof(struct partition));
+ mbr.offset = 512;
+ mbr.size = image->size - 512;
+ mbr.in_partition_table = 1;
+ mbr.partition_type = 0xee;
+ list_add_tail(&mbr.list, &mbr_list);
+ ret = hdimage_insert_mbr(image, &mbr_list);
+ if (ret) {
+ image_error(image,"failed to write protective MBR\n");
+ return ret;
+ }
+
+ return 0;
+}
+
+static int hdimage_insert_gpt(struct image *image, struct list_head *partitions)
+{
+ struct hdimage *hd = image->handler_priv;
+ const char *outfile = imageoutfile(image);
+ struct gpt_header header;
+ struct gpt_partition_entry table[GPT_ENTRIES];
+ struct partition *part;
+ unsigned i, j;
+ int ret;
+
+ image_info(image, "writing GPT\n");
+
+ memset(&header, 0, sizeof(struct gpt_header));
+ memcpy(header.signature, "EFI PART", 8);
+ header.revision = htole32(GPT_REVISION_1_0);
+ header.header_size = htole32(sizeof(struct gpt_header));
+ header.current_lba = htole64(1);
+ header.backup_lba = htole64(image->size/512 - 1);
+ header.last_usable_lba = htole64(image->size/512 - 1 - GPT_SECTORS);
+ uuid_parse(hd->disk_uuid, header.disk_uuid);
+ header.starting_lba = htole64(2);
+ header.number_entries = htole32(GPT_ENTRIES);
+ header.entry_size = htole32(sizeof(struct gpt_partition_entry));
+
+ i = 0;
+ memset(&table, 0, sizeof(table));
+ list_for_each_entry(part, partitions, list) {
+ if (header.first_usable_lba == 0)
+ header.first_usable_lba = htole64(part->offset / 512);
+
+ if (!part->in_partition_table)
+ continue;
+
+ uuid_parse(part->partition_type_uuid, table[i].type_uuid);
+ uuid_parse(part->partition_uuid, table[i].uuid);
+ table[i].first_lba = htole64(part->offset/512);
+ table[i].last_lba = htole64((part->offset + part->size)/512 - 1);
+ for (j = 0; j < strlen(part->name) && j < 36; j++)
+ table[i].name[j] = htole16(part->name[j]);
+ i++;
+ }
+ header.table_crc = htole32(crc32(table, sizeof(table)));
+
+ header.header_crc = htole32(crc32(&header, sizeof(header)));
+ ret = insert_data(image, (char *)&header, outfile, sizeof(header), 512);
+ if (ret) {
+ image_error(image, "failed to write GPT\n");
+ return ret;
+ }
+ ret = insert_data(image, (char *)&table, outfile, sizeof(table), 2*512);
+ if (ret) {
+ image_error(image, "failed to write GPT table\n");
+ return ret;
+ }
+
+ ret = pad_file(image, NULL, outfile, image->size, 0x0, MODE_APPEND);
+ if (ret) {
+ image_error(image, "failed to pad image to size %lld\n",
+ part->offset);
+ return ret;
+ }
+
+ header.header_crc = 0;
+ header.current_lba = htole64(image->size/512 - 1);
+ header.backup_lba = htole64(1);
+ header.header_crc = htole32(crc32(&header, sizeof(header)));
+ ret = insert_data(image, (char *)&table, outfile, sizeof(table),
+ image->size - GPT_SECTORS*512);
+ if (ret) {
+ image_error(image, "failed to write backup GPT table\n");
+ return ret;
+ }
+ ret = insert_data(image, (char *)&header, outfile, sizeof(header),
+ image->size - 512);
+ if (ret) {
+ image_error(image, "failed to write backup GPT\n");
+ return ret;
+ }
+
+ ret = hdimage_insert_protective_mbr(image);
+ if (ret) {
+ return ret;
+ }
+
return 0;
}
+
static int hdimage_generate(struct image *image)
{
struct partition *part;
@@ -174,11 +355,7 @@ static int hdimage_generate(struct image *image)
}
if (part->extended) {
- char ebr[4*sizeof(struct partition_entry)+2];
- memset(ebr, 0, sizeof(ebr));
- ret = hdimage_setup_ebr(image, part, ebr);
- ret = insert_data(image, ebr, outfile, sizeof(ebr),
- part->offset - hd->align + 446);
+ ret = hdimage_insert_ebr(image, part);
if (ret) {
image_error(image, "failed to write EBR\n");
return ret;
@@ -201,19 +378,16 @@ static int hdimage_generate(struct image *image)
}
if (hd->partition_table) {
- char part_table[6+4*sizeof(struct partition_entry)+2];
-
- memset(part_table, 0, sizeof(part_table));
- ret = hdimage_setup_mbr(image, part_table);
- if (ret)
- return ret;
-
- ret = insert_data(image, part_table, outfile, sizeof(part_table), 440);
- if (ret) {
- image_error(image, "failed to write MBR\n");
- return ret;
+ if (hd->gpt) {
+ ret = hdimage_insert_gpt(image, &image->partitions);
+ if (ret)
+ return ret;
+ }
+ else {
+ ret = hdimage_insert_mbr(image, &image->partitions);
+ if (ret)
+ return ret;
}
- mode = MODE_APPEND;
}
return 0;
@@ -236,6 +410,8 @@ static int hdimage_setup(struct image *image, cfg_t *cfg)
hd->partition_table = cfg_getbool(cfg, "partition-table");
hd->extended_partition = cfg_getint(cfg, "extended-partition");
hd->disksig = strtoul(cfg_getstr(cfg, "disk-signature"), NULL, 0);
+ hd->gpt = cfg_getbool(cfg, "gpt");
+ hd->disk_uuid = cfg_getstr(cfg, "disk-uuid");
if (hd->extended_partition > 4) {
image_error(image, "invalid extended partition index (%i). must be "
@@ -253,10 +429,20 @@ static int hdimage_setup(struct image *image, cfg_t *cfg)
if (part->in_partition_table)
++partition_table_entries;
}
- if (!hd->extended_partition && partition_table_entries > 4)
+ if (!hd->gpt && !hd->extended_partition && partition_table_entries > 4)
hd->extended_partition = 4;
has_extended = hd->extended_partition > 0;
+ if (hd->disk_uuid ) {
+ if (uuid_validate(hd->disk_uuid) == -1) {
+ image_error(image, "invalid disk UUID: %s\n", hd->disk_uuid);
+ return -EINVAL;
+ }
+ }
+ else {
+ hd->disk_uuid = uuid_random();
+ }
+
partition_table_entries = 0;
list_for_each_entry(part, &image->partitions, list) {
if (part->image) {
@@ -288,6 +474,36 @@ static int hdimage_setup(struct image *image, cfg_t *cfg)
part->name, part->size);
return -EINVAL;
}
+ if (hd->gpt) {
+ if (strlen(part->partition_type_uuid) == 1) {
+ const char *uuid;
+ uuid = gpt_partition_type_lookup(part->partition_type_uuid[0]);
+ if (!uuid) {
+ image_error(image,
+ "part %s has invalid type shortcut: %c\n",
+ part->name, part->partition_type_uuid[0]);
+ return -EINVAL;
+ }
+ part->partition_type_uuid = uuid;
+ }
+ if (uuid_validate(part->partition_type_uuid) == -1) {
+ image_error(image,
+ "part %s has invalid partition type UUID: %s\n",
+ part->name, part->partition_type_uuid);
+ return -EINVAL;
+ }
+ if (part->partition_uuid) {
+ if (uuid_validate(part->partition_uuid) == -1) {
+ image_error(image,
+ "part %s has invalid partition UUID: %s\n",
+ part->name, part->partition_uuid);
+ return -EINVAL;
+ }
+ }
+ else {
+ part->partition_uuid = uuid_random();
+ }
+ }
/* reserve space for extended boot record if necessary */
if (part->in_partition_table)
++partition_table_entries;
@@ -312,18 +528,27 @@ static int hdimage_setup(struct image *image, cfg_t *cfg)
return -EINVAL;
}
} else if (!part->offset && part->in_partition_table) {
- if (!now && hd->partition_table)
+ if (!now && hd->partition_table) {
now = 512;
+ if (hd->gpt)
+ now += GPT_SECTORS * 512;
+ }
part->offset = roundup(now, hd->align);
}
now = part->offset + part->size;
}
+ if (hd->gpt)
+ now += GPT_SECTORS * 512;
+
if (image->size > 0 && now > image->size) {
image_error(image, "partitions exceed device size\n");
return -EINVAL;
}
+ if (image->size == 0)
+ image->size = now;
+
image->handler_priv = hd;
return 0;
@@ -332,8 +557,10 @@ static int hdimage_setup(struct image *image, cfg_t *cfg)
cfg_opt_t hdimage_opts[] = {
CFG_STR("align", "512", CFGF_NONE),
CFG_STR("disk-signature", "", CFGF_NONE),
+ CFG_STR("disk-uuid", NULL, CFGF_NONE),
CFG_BOOL("partition-table", cfg_true, CFGF_NONE),
CFG_INT("extended-partition", 0, CFGF_NONE),
+ CFG_BOOL("gpt", cfg_false, CFGF_NONE),
CFG_END()
};
diff --git a/test/basic-images.test b/test/basic-images.test
index 4439fc2..9b53b45 100755
--- a/test/basic-images.test
+++ b/test/basic-images.test
@@ -214,6 +214,20 @@ test_expect_success "hdimage2" "
test_must_fail run_genimage hdimage2.config test.hdimage
"
+sfdisk -h | grep -q gpt && test_set_prereq sfdisk-gpt
+fdisk -h | grep -q gpt && test_set_prereq fdisk-gpt
+test_expect_success fdisk-gpt,sfdisk-gpt "hdimage4" "
+ setup_test_images &&
+ run_genimage hdimage4.config test.hdimage &&
+ check_size images/test.hdimage 7356928 &&
+ # check the this identifier
+ fdisk -l images/test.hdimage | grep identifier: > hdimage4.fdisk &&
+ # check partitions; filter output to handle different sfdisk versions
+ sfdisk -d images/test.hdimage 2>/dev/null | grep '^images/' | \
+ sed -e 's/ *//g' -e 's;Id=;type=;' >> hdimage4.fdisk &&
+ test_cmp '${testdir}/hdimage4.fdisk' hdimage4.fdisk
+"
+
exec_test_set_prereq genisoimage
test_expect_success genisoimage "iso" "
run_genimage iso.config test.iso &&
diff --git a/test/hdimage4.config b/test/hdimage4.config
new file mode 100644
index 0000000..7bd977f
--- /dev/null
+++ b/test/hdimage4.config
@@ -0,0 +1,40 @@
+image test.hdimage {
+ hdimage {
+ align = 1M
+ gpt = "true"
+ disk-uuid = "afcfea87-e41a-40e0-85ae-295c60773c7a"
+ }
+ partition part1 {
+ image = "part1.img"
+ size = 1M
+ partition-uuid = "92762261-e854-45c1-b4c9-fc5e752034ab"
+ }
+ partition part2 {
+ image = "part2.img"
+ size = 1M
+ partition-type-uuid = "L"
+ partition-uuid = "41061242-1d5a-4657-892d-fcc1fdb11a6c"
+ }
+ partition part3 {
+ image = "part1.img"
+ size = 1M
+ partition-type-uuid = "S"
+ partition-uuid = "954532ea-bd86-4992-a1ed-2cdb2c18581a"
+ }
+ partition part4 {
+ image = "part2.img"
+ size = 1M
+ partition-type-uuid = "F"
+ partition-uuid = "6d04bf47-3ddf-4a75-919b-c7bf46f2ef92"
+ }
+ partition part5 {
+ image = "part1.img"
+ size = 1M
+ partition-uuid = "92762261-e854-45c1-b4c9-fc5e752034ab"
+ }
+ partition part6 {
+ image = "part2.img"
+ size = 1M
+ partition-uuid = "c9460c06-fbc0-48ae-b4f3-3e897d3ebe71"
+ }
+}
diff --git a/test/hdimage4.fdisk b/test/hdimage4.fdisk
new file mode 100644
index 0000000..187fd1a
--- /dev/null
+++ b/test/hdimage4.fdisk
@@ -0,0 +1,7 @@
+Disk identifier: AFCFEA87-E41A-40E0-85AE-295C60773C7A
+images/test.hdimage1:start=2048,size=2048,type=0FC63DAF-8483-4772-8E79-3D69D8477DE4,uuid=92762261-E854-45C1-B4C9-FC5E752034AB,name="part1"
+images/test.hdimage2:start=4096,size=2048,type=0FC63DAF-8483-4772-8E79-3D69D8477DE4,uuid=41061242-1D5A-4657-892D-FCC1FDB11A6C,name="part2"
+images/test.hdimage3:start=6144,size=2048,type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F,uuid=954532EA-BD86-4992-A1ED-2CDB2C18581A,name="part3"
+images/test.hdimage4:start=8192,size=2048,type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7,uuid=6D04BF47-3DDF-4A75-919B-C7BF46F2EF92,name="part4"
+images/test.hdimage5:start=10240,size=2048,type=0FC63DAF-8483-4772-8E79-3D69D8477DE4,uuid=92762261-E854-45C1-B4C9-FC5E752034AB,name="part5"
+images/test.hdimage6:start=12288,size=2048,type=0FC63DAF-8483-4772-8E79-3D69D8477DE4,uuid=C9460C06-FBC0-48AE-B4F3-3E897D3EBE71,name="part6"
diff --git a/util.c b/util.c
index 018acb1..5b8fe95 100644
--- a/util.c
+++ b/util.c
@@ -25,6 +25,7 @@
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
+#include <ctype.h>
#include "genimage.h"
@@ -410,3 +411,70 @@ err_out:
return ret;
}
+
+int uuid_validate(const char *str)
+{
+ int i;
+
+ if (strlen(str) != 36)
+ return -1;
+ for (i = 0; i < 36; i++) {
+ if (i == 8 || i == 13 || i == 18 || i == 23) {
+ if (str[i] != '-')
+ return -1;
+ continue;
+ }
+ if (!isxdigit(str[i]))
+ return -1;
+ }
+
+ return 0;
+}
+
+static unsigned char uuid_byte(const char *hex)
+{
+ char buf[3];
+
+ buf[0] = hex[0];
+ buf[1] = hex[1];
+ buf[2] = 0;
+ return strtoul(buf, NULL, 16);
+}
+
+void uuid_parse(const char *str, unsigned char *uuid)
+{
+ uuid[0] = uuid_byte(str + 6);
+ uuid[1] = uuid_byte(str + 4);
+ uuid[2] = uuid_byte(str + 2);
+ uuid[3] = uuid_byte(str);
+
+ uuid[4] = uuid_byte(str + 11);
+ uuid[5] = uuid_byte(str + 9);
+
+ uuid[6] = uuid_byte(str + 16);
+ uuid[7] = uuid_byte(str + 14);
+
+ uuid[8] = uuid_byte(str + 19);
+ uuid[9] = uuid_byte(str + 21);
+
+ uuid[10] = uuid_byte(str + 24);
+ uuid[11] = uuid_byte(str + 26);
+ uuid[12] = uuid_byte(str + 28);
+ uuid[13] = uuid_byte(str + 30);
+ uuid[14] = uuid_byte(str + 32);
+ uuid[15] = uuid_byte(str + 34);
+}
+
+char *uuid_random(void)
+{
+ char *uuid;
+
+ asprintf(&uuid, "%04lx%04lx-%04lx-%04lx-%04lx-%04lx%04lx%04lx",
+ random() & 0xffff, random() & 0xffff,
+ random() & 0xffff,
+ (random() & 0x0fff) | 0x4000,
+ (random() & 0x3fff) | 0x8000,
+ random() & 0xffff, random() & 0xffff, random() & 0xffff);
+
+ return uuid;
+}