1/*	$NetBSD: gpt_uuid.c,v 1.21 2023/09/27 09:44:10 riastradh 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.21 2023/09/27 09:44:10 riastradh Exp $");
36#endif
37
38#include <err.h>
39#include <fcntl.h>
40#include <stdio.h>
41#include <unistd.h>
42
43#include "map.h"
44#include "gpt.h"
45#include "gpt_private.h"
46
47#if defined(HAVE_SYS_ENDIAN_H) || ! defined(HAVE_NBTOOL_CONFIG_H)
48#include <sys/endian.h>
49#endif
50
51
52const gpt_uuid_t gpt_uuid_nil;
53
54struct dce_uuid {
55	uint32_t	time_low;
56	uint16_t	time_mid;
57	uint16_t	time_hi_and_version;
58	uint8_t		clock_seq_hi_and_reserved;
59	uint8_t		clock_seq_low;
60	uint8_t		node[6];
61};
62
63static const struct {
64	struct dce_uuid u;
65	const char *n;
66	const char *d;
67} gpt_nv[] = {
68	/* Must match the gpt_type_t enum in gpt_uuid.h */
69	{ GPT_ENT_TYPE_APPLE_HFS, "apple", "Apple HFS" },
70	{ GPT_ENT_TYPE_APPLE_UFS, "apple-ufs", "Apple UFS" },
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, "zfs", "ZFS" },
78	{ GPT_ENT_TYPE_LINUX_DATA, "linux-data", "Linux data" },
79	{ GPT_ENT_TYPE_LINUX_RAID, "linux-raid", "Linux RAID" },
80	{ GPT_ENT_TYPE_LINUX_SWAP, "linux-swap", "Linux swap" },
81	{ GPT_ENT_TYPE_LINUX_LVM, "linux-lvm", "Linux LVM" },
82	{ GPT_ENT_TYPE_MS_BASIC_DATA, "windows", "Windows basic data" },
83	{ GPT_ENT_TYPE_MS_RESERVED, "windows-reserved", "Windows reserved" },
84	{ GPT_ENT_TYPE_MS_RECOVERY, "windows-recovery", "Windows recovery" },
85	{ GPT_ENT_TYPE_NETBSD_CCD, "ccd", "NetBSD ccd component" },
86	{ GPT_ENT_TYPE_NETBSD_CGD, "cgd", "NetBSD Cryptographic Disk" },
87	{ GPT_ENT_TYPE_NETBSD_FFS, "ffs", "NetBSD FFSv1/FFSv2" },
88	{ GPT_ENT_TYPE_NETBSD_LFS, "lfs", "NetBSD LFS" },
89	{ GPT_ENT_TYPE_NETBSD_RAIDFRAME, "raid",
90	    "NetBSD RAIDFrame component" },
91	{ GPT_ENT_TYPE_NETBSD_SWAP, "swap", "NetBSD swap" },
92	{ GPT_ENT_TYPE_VMWARE_VMKCORE, "vmcore", "VMware VMkernel core dump" },
93	{ GPT_ENT_TYPE_VMWARE_VMFS, "vmfs", "VMware VMFS" },
94	{ GPT_ENT_TYPE_VMWARE_RESERVED, "vmresered", "VMware reserved" },
95};
96
97static void
98gpt_uuid_to_dce(const gpt_uuid_t buf, struct dce_uuid *uuid)
99{
100	const uint8_t *p = buf;
101	size_t i;
102
103	uuid->time_low = le32dec(p);
104	uuid->time_mid = le16dec(p + 4);
105	uuid->time_hi_and_version = le16dec(p + 6);
106	uuid->clock_seq_hi_and_reserved = p[8];
107	uuid->clock_seq_low = p[9];
108	for (i = 0; i < sizeof(uuid->node); i++)
109		uuid->node[i] = p[10 + i];
110}
111
112static void
113gpt_dce_to_uuid(const struct dce_uuid *uuid, uint8_t *buf)
114{
115	uint8_t *p = buf;
116	size_t i;
117
118	le32enc(p, uuid->time_low);
119	le16enc(p + 4, uuid->time_mid);
120	le16enc(p + 6, uuid->time_hi_and_version);
121	p[8] = uuid->clock_seq_hi_and_reserved;
122	p[9] = uuid->clock_seq_low;
123	for (i = 0; i < sizeof(uuid->node); i++)
124		p[10 + i] = uuid->node[i];
125}
126
127static int
128gpt_uuid_numeric(char *buf, size_t bufsiz, const struct dce_uuid *u)
129{
130	return snprintf(buf, bufsiz,
131	    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
132	    u->time_low, u->time_mid, u->time_hi_and_version,
133	    u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0],
134	    u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
135}
136
137
138static int
139gpt_uuid_symbolic(char *buf, size_t bufsiz, const struct dce_uuid *u)
140{
141	size_t i;
142
143	for (i = 0; i < __arraycount(gpt_nv); i++)
144		if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0)
145			return (int)strlcpy(buf, gpt_nv[i].n, bufsiz);
146	return -1;
147}
148
149static int
150gpt_uuid_descriptive(char *buf, size_t bufsiz, const struct dce_uuid *u)
151{
152	size_t i;
153
154	for (i = 0; i < __arraycount(gpt_nv); i++)
155		if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0)
156			return (int)strlcpy(buf, gpt_nv[i].d, bufsiz);
157	return -1;
158}
159
160int
161gpt_uuid_snprintf(char *buf, size_t bufsiz, const char *fmt,
162    const gpt_uuid_t uu)
163{
164	struct dce_uuid u;
165	gpt_uuid_to_dce(uu, &u);
166
167	if (fmt[1] == 's') {
168		int r;
169		if ((r = gpt_uuid_symbolic(buf, bufsiz, &u)) != -1)
170			return r;
171	}
172	if (fmt[1] == 'l') {
173		int r;
174		if ((r = gpt_uuid_descriptive(buf, bufsiz, &u)) != -1)
175			return r;
176	}
177	return gpt_uuid_numeric(buf, bufsiz, &u);
178}
179
180static int
181gpt_uuid_parse_numeric(const char *s, struct dce_uuid *u)
182{
183	int n;
184
185	if (s == NULL || *s == '\0') {
186		memset(u, 0, sizeof(*u));
187		return 0;
188	}
189
190	n = sscanf(s,
191	    "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
192	    &u->time_low, &u->time_mid, &u->time_hi_and_version,
193	    &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
194	    &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
195
196	/* Make sure we have all conversions. */
197	if (n != 11)
198		return -1;
199
200	/* We have a successful scan. Check semantics... */
201	n = u->clock_seq_hi_and_reserved;
202	if ((n & 0x80) != 0x00 &&			/* variant 0? */
203	    (n & 0xc0) != 0x80 &&			/* variant 1? */
204	    (n & 0xe0) != 0xc0) 			/* variant 2? */
205		return -1;
206	return 0;
207}
208
209static int
210gpt_uuid_parse_symbolic(const char *s, struct dce_uuid *u)
211{
212	size_t i;
213
214	for (i = 0; i < __arraycount(gpt_nv); i++)
215		if (strcmp(gpt_nv[i].n, s) == 0) {
216			*u = gpt_nv[i].u;
217			return 0;
218		}
219	return -1;
220}
221
222int
223gpt_uuid_parse(const char *s, gpt_uuid_t uuid)
224{
225	struct dce_uuid u;
226
227	if (gpt_uuid_parse_numeric(s, &u) != -1) {
228		gpt_dce_to_uuid(&u, uuid);
229		return 0;
230	}
231
232	if (gpt_uuid_parse_symbolic(s, &u) == -1)
233		return -1;
234
235	gpt_dce_to_uuid(&u, uuid);
236	return 0;
237}
238
239size_t
240gpt_uuid_query(
241    void (*func)(const char *uuid, const char *short_name, const char *desc))
242{
243	size_t i;
244	char buf[64];
245
246	if (func != NULL) {
247		for (i = 0; i < __arraycount(gpt_nv); i++) {
248			gpt_uuid_numeric(buf, sizeof(buf), &gpt_nv[i].u);
249			(*func)(buf, gpt_nv[i].n, gpt_nv[i].d);
250		}
251	}
252	return __arraycount(gpt_nv);
253}
254
255#ifndef GPT_UUID_QUERY_ONLY
256void
257gpt_uuid_help(const char *prefix)
258{
259	size_t i;
260
261	for (i = 0; i < __arraycount(gpt_nv); i++)
262		printf("%s%18.18s\t%s\n", prefix, gpt_nv[i].n, gpt_nv[i].d);
263}
264
265void
266gpt_uuid_create(gpt_type_t t, gpt_uuid_t u, uint16_t *b, size_t s)
267{
268	gpt_dce_to_uuid(&gpt_nv[t].u, u);
269	if (b)
270		utf8_to_utf16((const uint8_t *)gpt_nv[t].d, b, s / sizeof(*b));
271}
272
273static int
274gpt_uuid_random(gpt_t gpt, void *v, size_t n)
275{
276	int fd;
277	uint8_t *p;
278	ssize_t nread;
279
280	/* Randomly generate the content.  */
281	fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
282	if (fd == -1) {
283		gpt_warn(gpt, "Can't open `/dev/urandom'");
284		return -1;
285	}
286	for (p = v;  n > 0; p += nread, n -= (size_t)nread) {
287		nread = read(fd, p, n);
288		if (nread < 0) {
289			gpt_warn(gpt, "Can't read `/dev/urandom'");
290			goto out;
291		}
292		if (nread == 0) {
293			gpt_warn(gpt, "EOF from /dev/urandom");
294			goto out;
295		}
296		if ((size_t)nread > n) {
297			gpt_warnx(gpt, "read too much: %zd > %zu", nread, n);
298			goto out;
299		}
300	}
301	(void)close(fd);
302	return 0;
303out:
304	(void)close(fd);
305	return -1;
306}
307
308static int
309gpt_uuid_tstamp(gpt_t gpt, void *v, size_t l)
310{
311	uint8_t *p;
312	// Perhaps use SHA?
313	uint32_t x = (uint32_t)gpt->timestamp;
314
315	for (p = v; l > 0; p += sizeof(x), l -= sizeof(x))
316		memcpy(p, &x, sizeof(x));
317
318	return 0;
319}
320
321int
322gpt_uuid_generate(gpt_t gpt, gpt_uuid_t t)
323{
324	int rv;
325	struct dce_uuid u;
326	if (gpt && (gpt->flags & GPT_TIMESTAMP))
327		rv = gpt_uuid_tstamp(gpt, &u, sizeof(u));
328	else
329		rv = gpt_uuid_random(gpt, &u, sizeof(u));
330
331	if (rv == -1)
332		return -1;
333
334	/* Set the version number to 4.  */
335	u.time_hi_and_version &= (uint16_t)~0xf000;
336	u.time_hi_and_version |= 0x4000;
337
338	/* Fix the reserved bits.  */
339	u.clock_seq_hi_and_reserved &= (uint8_t)~0x40;
340	u.clock_seq_hi_and_reserved |= 0x80;
341
342	gpt_dce_to_uuid(&u, t);
343	return 0;
344}
345#endif
346