Home | History | Annotate | Line # | Download | only in gpt
gpt_uuid.c revision 1.7
      1  1.7  christos /*	$NetBSD: gpt_uuid.c,v 1.7 2014/10/03 20:30:06 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  *
     16  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  christos  */
     28  1.1  christos 
     29  1.1  christos #if HAVE_NBTOOL_CONFIG_H
     30  1.1  christos #include "nbtool_config.h"
     31  1.1  christos #endif
     32  1.1  christos 
     33  1.1  christos #include <sys/cdefs.h>
     34  1.1  christos #ifdef __RCSID
     35  1.7  christos __RCSID("$NetBSD: gpt_uuid.c,v 1.7 2014/10/03 20:30:06 christos Exp $");
     36  1.1  christos #endif
     37  1.1  christos 
     38  1.1  christos #include <stdio.h>
     39  1.1  christos 
     40  1.1  christos #include "map.h"
     41  1.1  christos #include "gpt.h"
     42  1.1  christos 
     43  1.5       apb #if defined(HAVE_SYS_ENDIAN_H) || ! defined(HAVE_NBTOOL_CONFIG_H)
     44  1.1  christos #include <sys/endian.h>
     45  1.1  christos #endif
     46  1.1  christos 
     47  1.6   jnemeth 
     48  1.1  christos const gpt_uuid_t gpt_uuid_nil;
     49  1.1  christos 
     50  1.1  christos struct dce_uuid {
     51  1.1  christos 	uint32_t	time_low;
     52  1.1  christos 	uint16_t	time_mid;
     53  1.1  christos 	uint16_t	time_hi_and_version;
     54  1.1  christos 	uint8_t		clock_seq_hi_and_reserved;
     55  1.1  christos 	uint8_t		clock_seq_low;
     56  1.1  christos 	uint8_t		node[6];
     57  1.1  christos };
     58  1.1  christos 
     59  1.7  christos struct kern_uuid {
     60  1.7  christos 	uint64_t	ll;
     61  1.7  christos 	uint16_t	seq;
     62  1.7  christos 	uint8_t		node[6];
     63  1.7  christos };
     64  1.7  christos 
     65  1.1  christos static const struct {
     66  1.1  christos 	struct dce_uuid u;
     67  1.1  christos 	const char *n;
     68  1.1  christos 	const char *d;
     69  1.1  christos } gpt_nv[] = {
     70  1.1  christos 	{ GPT_ENT_TYPE_APPLE_HFS, "apple", "Apple HFS" },
     71  1.1  christos 	{ GPT_ENT_TYPE_BIOS, "bios", "BIOS Boot" },
     72  1.1  christos 	{ GPT_ENT_TYPE_EFI, "efi", "EFI System" },
     73  1.1  christos 	{ GPT_ENT_TYPE_FREEBSD, "fbsd-legacy", "FreeBSD legacy" },
     74  1.1  christos 	{ GPT_ENT_TYPE_FREEBSD_SWAP, "fbsd-swap", "FreeBSD swap" },
     75  1.1  christos 	{ GPT_ENT_TYPE_FREEBSD_UFS, "fbsd-ufs", "FreeBSD UFS/UFS2" },
     76  1.1  christos 	{ GPT_ENT_TYPE_FREEBSD_VINUM, "fbsd-vinum", "FreeBSD vinum" },
     77  1.1  christos 	{ GPT_ENT_TYPE_FREEBSD_ZFS, "fbsd-zfs", "FreeBSD ZFS" },
     78  1.1  christos 	{ GPT_ENT_TYPE_LINUX_DATA, "linux-data", "Linux data" },
     79  1.1  christos 	{ GPT_ENT_TYPE_LINUX_SWAP, "linux-swap", "Linux swap" },
     80  1.1  christos 	{ GPT_ENT_TYPE_MS_BASIC_DATA, "windows", "Windows basic data" },
     81  1.1  christos 	{ GPT_ENT_TYPE_MS_RESERVED, "windows-reserved", "Windows reserved" },
     82  1.1  christos 	{ GPT_ENT_TYPE_NETBSD_CCD, "ccd", "NetBSD ccd component" },
     83  1.1  christos 	{ GPT_ENT_TYPE_NETBSD_CGD, "cgd", "NetBSD Cryptographic Disk" },
     84  1.1  christos 	{ GPT_ENT_TYPE_NETBSD_FFS, "ffs", "NetBSD FFSv1/FFSv2" },
     85  1.1  christos 	{ GPT_ENT_TYPE_NETBSD_LFS, "lfs", "NetBSD LFS" },
     86  1.1  christos 	{ GPT_ENT_TYPE_NETBSD_RAIDFRAME, "raid",
     87  1.1  christos 	    "NetBSD RAIDFrame component" },
     88  1.1  christos 	{ GPT_ENT_TYPE_NETBSD_SWAP, "swap", "NetBSD swap" },
     89  1.1  christos };
     90  1.1  christos 
     91  1.1  christos static void
     92  1.1  christos gpt_uuid_to_dce(const gpt_uuid_t buf, struct dce_uuid *uuid)
     93  1.1  christos {
     94  1.1  christos 	const uint8_t *p = buf;
     95  1.1  christos 	size_t i;
     96  1.1  christos 
     97  1.1  christos 	uuid->time_low = le32dec(p);
     98  1.1  christos 	uuid->time_mid = le16dec(p + 4);
     99  1.1  christos 	uuid->time_hi_and_version = le16dec(p + 6);
    100  1.1  christos 	uuid->clock_seq_hi_and_reserved = p[8];
    101  1.1  christos 	uuid->clock_seq_low = p[9];
    102  1.1  christos 	for (i = 0; i < sizeof(uuid->node); i++)
    103  1.1  christos 		uuid->node[i] = p[10 + i];
    104  1.1  christos }
    105  1.1  christos 
    106  1.1  christos static void
    107  1.1  christos gpt_dce_to_uuid(const struct dce_uuid *uuid, uint8_t *buf)
    108  1.1  christos {
    109  1.1  christos 	uint8_t *p = buf;
    110  1.1  christos 	size_t i;
    111  1.1  christos 
    112  1.1  christos 	le32enc(p, uuid->time_low);
    113  1.1  christos 	le16enc(p + 4, uuid->time_mid);
    114  1.1  christos 	le16enc(p + 6, uuid->time_hi_and_version);
    115  1.1  christos 	p[8] = uuid->clock_seq_hi_and_reserved;
    116  1.1  christos 	p[9] = uuid->clock_seq_low;
    117  1.1  christos 	for (i = 0; i < sizeof(uuid->node); i++)
    118  1.1  christos 		p[10 + i] = uuid->node[i];
    119  1.1  christos }
    120  1.1  christos 
    121  1.1  christos static int
    122  1.1  christos gpt_uuid_numeric(char *buf, size_t bufsiz, const struct dce_uuid *u)
    123  1.1  christos {
    124  1.1  christos 	return snprintf(buf, bufsiz,
    125  1.1  christos 	    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
    126  1.1  christos 	    u->time_low, u->time_mid, u->time_hi_and_version,
    127  1.1  christos 	    u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0],
    128  1.1  christos 	    u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
    129  1.1  christos }
    130  1.1  christos 
    131  1.1  christos 
    132  1.1  christos static int
    133  1.1  christos gpt_uuid_symbolic(char *buf, size_t bufsiz, const struct dce_uuid *u)
    134  1.1  christos {
    135  1.1  christos 	size_t i;
    136  1.1  christos 
    137  1.1  christos 	for (i = 0; i < __arraycount(gpt_nv); i++)
    138  1.1  christos 		if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0)
    139  1.1  christos 			return strlcpy(buf, gpt_nv[i].n, bufsiz);
    140  1.1  christos 	return -1;
    141  1.1  christos }
    142  1.1  christos 
    143  1.2   jnemeth static int
    144  1.2   jnemeth gpt_uuid_descriptive(char *buf, size_t bufsiz, const struct dce_uuid *u)
    145  1.2   jnemeth {
    146  1.2   jnemeth 	size_t i;
    147  1.2   jnemeth 
    148  1.2   jnemeth 	for (i = 0; i < __arraycount(gpt_nv); i++)
    149  1.2   jnemeth 		if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0)
    150  1.2   jnemeth 			return strlcpy(buf, gpt_nv[i].d, bufsiz);
    151  1.2   jnemeth 	return -1;
    152  1.2   jnemeth }
    153  1.2   jnemeth 
    154  1.1  christos int
    155  1.1  christos gpt_uuid_snprintf(char *buf, size_t bufsiz, const char *fmt,
    156  1.1  christos     const gpt_uuid_t uu)
    157  1.1  christos {
    158  1.1  christos 	struct dce_uuid u;
    159  1.1  christos 	gpt_uuid_to_dce(uu, &u);
    160  1.1  christos 
    161  1.1  christos 	if (fmt[1] == 's') {
    162  1.1  christos 		int r;
    163  1.1  christos 		if ((r = gpt_uuid_symbolic(buf, bufsiz, &u)) != -1)
    164  1.1  christos 			return r;
    165  1.1  christos 	}
    166  1.2   jnemeth 	if (fmt[1] == 'l') {
    167  1.2   jnemeth 		int r;
    168  1.2   jnemeth 		if ((r = gpt_uuid_descriptive(buf, bufsiz, &u)) != -1)
    169  1.2   jnemeth 			return r;
    170  1.2   jnemeth 	}
    171  1.1  christos 	return gpt_uuid_numeric(buf, bufsiz, &u);
    172  1.1  christos }
    173  1.1  christos 
    174  1.1  christos static int
    175  1.1  christos gpt_uuid_parse_numeric(const char *s, struct dce_uuid *u)
    176  1.1  christos {
    177  1.1  christos 	int n;
    178  1.1  christos 
    179  1.1  christos 	if (s == NULL || *s == '\0') {
    180  1.1  christos 		memset(u, 0, sizeof(*u));
    181  1.1  christos 		return 0;
    182  1.1  christos 	}
    183  1.1  christos 
    184  1.1  christos 	n = sscanf(s,
    185  1.1  christos 	    "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
    186  1.1  christos 	    &u->time_low, &u->time_mid, &u->time_hi_and_version,
    187  1.1  christos 	    &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
    188  1.1  christos 	    &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
    189  1.1  christos 
    190  1.1  christos 	/* Make sure we have all conversions. */
    191  1.1  christos 	if (n != 11)
    192  1.1  christos 		return -1;
    193  1.1  christos 
    194  1.1  christos 	/* We have a successful scan. Check semantics... */
    195  1.1  christos 	n = u->clock_seq_hi_and_reserved;
    196  1.1  christos 	if ((n & 0x80) != 0x00 &&			/* variant 0? */
    197  1.1  christos 	    (n & 0xc0) != 0x80 &&			/* variant 1? */
    198  1.1  christos 	    (n & 0xe0) != 0xc0) 			/* variant 2? */
    199  1.1  christos 		return -1;
    200  1.1  christos 	return 0;
    201  1.1  christos }
    202  1.1  christos 
    203  1.1  christos static int
    204  1.1  christos gpt_uuid_parse_symbolic(const char *s, struct dce_uuid *u)
    205  1.1  christos {
    206  1.1  christos 	size_t i;
    207  1.1  christos 
    208  1.1  christos 	for (i = 0; i < __arraycount(gpt_nv); i++)
    209  1.1  christos 		if (strcmp(gpt_nv[i].n, s) == 0) {
    210  1.1  christos 			*u = gpt_nv[i].u;
    211  1.1  christos 			return 0;
    212  1.1  christos 		}
    213  1.1  christos 	return -1;
    214  1.1  christos }
    215  1.1  christos 
    216  1.1  christos int
    217  1.1  christos gpt_uuid_parse(const char *s, gpt_uuid_t uuid)
    218  1.1  christos {
    219  1.1  christos 	struct dce_uuid u;
    220  1.1  christos 
    221  1.3   jnemeth 	if (gpt_uuid_parse_numeric(s, &u) != -1) {
    222  1.3   jnemeth 		gpt_dce_to_uuid(&u, uuid);
    223  1.1  christos 		return 0;
    224  1.3   jnemeth 	}
    225  1.1  christos 
    226  1.1  christos 	if (gpt_uuid_parse_symbolic(s, &u) == -1)
    227  1.1  christos 		return -1;
    228  1.1  christos 
    229  1.1  christos 	gpt_dce_to_uuid(&u, uuid);
    230  1.1  christos 	return 0;
    231  1.1  christos }
    232  1.1  christos 
    233  1.1  christos void
    234  1.1  christos gpt_uuid_create(gpt_type_t t, gpt_uuid_t u, uint16_t *b, size_t s)
    235  1.1  christos {
    236  1.1  christos 	gpt_dce_to_uuid(&gpt_nv[t].u, u);
    237  1.1  christos 	if (b)
    238  1.1  christos 		utf8_to_utf16((const uint8_t *)gpt_nv[t].d, b, s / sizeof(*b));
    239  1.1  christos }
    240  1.6   jnemeth 
    241  1.6   jnemeth #if !defined(HAVE_NBTOOL_CONFIG_H)
    242  1.7  christos #include <sys/types.h>
    243  1.7  christos #include <sys/uuid.h>
    244  1.7  christos #else
    245  1.7  christos #include <time.h>
    246  1.7  christos /*
    247  1.7  christos  * Get the current time as a 5x bit count of 100000-microsecond intervals
    248  1.7  christos  * since 00:00:00.00, October 15,1582. We apply a magic offset to convert
    249  1.7  christos  * the Unix time since 00:00:00.00, January 1, 1970 to the date of the
    250  1.7  christos  * Gregorian reform to the Christian calendar.
    251  1.7  christos  */
    252  1.7  christos static uint64_t
    253  1.7  christos uuid_time(void)
    254  1.7  christos {
    255  1.7  christos 	struct timeval tv;
    256  1.7  christos 	uint64_t xtime = 0x01B21DD213814000LL;
    257  1.7  christos 
    258  1.7  christos 	(void)gettimeofday(&tv, NULL);
    259  1.7  christos 	xtime += (uint64_t)tv.tv_sec * 10000000LL;
    260  1.7  christos 	xtime += (uint64_t)(tv.tv_usec / 100000);
    261  1.7  christos 	return (xtime & ((1LL << 60) - 1LL));
    262  1.7  christos }
    263  1.7  christos 
    264  1.7  christos /*
    265  1.7  christos  * No portable way to get ethernet, use hostid instead
    266  1.7  christos  */
    267  1.7  christos static void
    268  1.7  christos uuid_node(uint8_t node[6])
    269  1.7  christos {
    270  1.7  christos 	long hid = gethostid();
    271  1.7  christos 	node[0] = 'N';
    272  1.7  christos 	node[1] = 'B';
    273  1.7  christos 	node[2] = (hid >> 24) & 0xff;
    274  1.7  christos 	node[3] = (hid >> 16) & 0xff;
    275  1.7  christos 	node[4] = (hid >>  8) & 0xff;
    276  1.7  christos 	node[5] = (hid >>  0) & 0xff;
    277  1.7  christos }
    278  1.7  christos 
    279  1.7  christos static void
    280  1.7  christos uuid_generate(void *u, uint64_t *timep, int count)
    281  1.7  christos {
    282  1.7  christos 	static struct kern_uuid uuid_last;
    283  1.7  christos 	uint64_t xtime, ltime;
    284  1.7  christos 	uint16_t lseq;
    285  1.7  christos 	struct kern_uuid *uuid = u;
    286  1.7  christos 
    287  1.7  christos 	uuid_node(uuid->node);
    288  1.7  christos 	xtime = uuid_time();
    289  1.7  christos 	*timep = xtime;
    290  1.7  christos 
    291  1.7  christos 	if (uuid_last.ll == 0LL || uuid_last.node[0] != uuid->node[0] ||
    292  1.7  christos 	    uuid_last.node[1] != uuid->node[1] ||
    293  1.7  christos 	    uuid_last.node[2] != uuid->node[2]) {
    294  1.7  christos 		srandom((unsigned int) xtime);
    295  1.7  christos 		uuid->seq = (uint16_t)random() & 0x3fff;
    296  1.7  christos 	} else if (uuid_last.ll >= xtime)
    297  1.7  christos 		uuid->seq = (uuid_last.seq + 1) & 0x3fff;
    298  1.7  christos 
    299  1.7  christos 	uuid_last = *uuid;
    300  1.7  christos 	uuid_last.ll = (xtime + count - 1) & ((1LL << 60) - 1LL);
    301  1.7  christos }
    302  1.7  christos 
    303  1.7  christos static void
    304  1.7  christos uuidgen(struct dce_uuid *store, int count)
    305  1.7  christos {
    306  1.7  christos 	uint64_t xtime;
    307  1.7  christos 	struct kern_uuid uuid;
    308  1.7  christos 	int i;
    309  1.7  christos 
    310  1.7  christos 	/* Generate the base UUID. */
    311  1.7  christos 	uuid_generate(&uuid, &xtime, count);
    312  1.7  christos 
    313  1.7  christos 	for (i = 0; i < count; xtime++, i++) {
    314  1.7  christos 		/* Set time and version (=1) and deal with byte order. */
    315  1.7  christos 		store[i].time_low = (uint32_t)xtime;
    316  1.7  christos 		store[i].time_mid = (uint16_t)(xtime >> 32);
    317  1.7  christos 		store[i].time_hi_and_version =
    318  1.7  christos 		    ((uint16_t)(xtime >> 48) & 0xfff) | (1 << 12);
    319  1.7  christos 		store[i].clock_seq_hi_and_reserved = (uuid.seq >> 16) | 0x80;
    320  1.7  christos 		store[i].clock_seq_low = uuid.seq & 0xff;
    321  1.7  christos 		memcpy(store[i].node, uuid.node, sizeof(uuid.node));
    322  1.7  christos 	}
    323  1.7  christos }
    324  1.7  christos #endif
    325  1.7  christos 
    326  1.6   jnemeth void
    327  1.7  christos gpt_uuid_generate(gpt_uuid_t t)
    328  1.6   jnemeth {
    329  1.7  christos 	struct dce_uuid u;
    330  1.6   jnemeth 
    331  1.7  christos 	uuidgen((void *)&u, 1);
    332  1.7  christos 	gpt_dce_to_uuid(&u, t);
    333  1.6   jnemeth }
    334