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