Home | History | Annotate | Line # | Download | only in gpt
      1 /*	$NetBSD: gpt_uuid.c,v 1.26 2025/10/11 18:55:31 thorpej 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.26 2025/10/11 18:55:31 thorpej 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 
     52 const gpt_uuid_t gpt_uuid_nil;
     53 
     54 struct 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 
     63 static 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_LINUX_XBOOTLDR, "linux-xbootldr", "Linux XBOOTLDR" },
     83 	{ GPT_ENT_TYPE_MS_BASIC_DATA, "windows", "Windows basic data" },
     84 	{ GPT_ENT_TYPE_MS_RESERVED, "windows-reserved", "Windows reserved" },
     85 	{ GPT_ENT_TYPE_MS_RECOVERY, "windows-recovery", "Windows recovery" },
     86 	{ GPT_ENT_TYPE_NETBSD_CCD, "ccd", "NetBSD ccd component" },
     87 	{ GPT_ENT_TYPE_NETBSD_CGD, "cgd", "NetBSD Cryptographic Disk" },
     88 	{ GPT_ENT_TYPE_NETBSD_FFS, "ffs", "NetBSD FFSv1/FFSv2" },
     89 	{ GPT_ENT_TYPE_NETBSD_LFS, "lfs", "NetBSD LFS" },
     90 	{ GPT_ENT_TYPE_NETBSD_RAIDFRAME, "raid",
     91 	    "NetBSD RAIDFrame component" },
     92 	{ GPT_ENT_TYPE_NETBSD_SWAP, "swap", "NetBSD swap" },
     93 	{ GPT_ENT_TYPE_OPENBSD_DATA, "obsd", "OpenBSD data" },
     94 	{ GPT_ENT_TYPE_VMWARE_VMKCORE, "vmcore", "VMware VMkernel core dump" },
     95 	{ GPT_ENT_TYPE_VMWARE_VMFS, "vmfs", "VMware VMFS" },
     96 	{ GPT_ENT_TYPE_VMWARE_RESERVED, "vmresered", "VMware reserved" },
     97 	{ GPT_ENT_TYPE_SIFIVE_BBL, "sifive-bbl", "SiFive BBL" },
     98 };
     99 
    100 static void
    101 gpt_uuid_to_dce(const gpt_uuid_t buf, struct dce_uuid *uuid)
    102 {
    103 	const uint8_t *p = buf;
    104 	size_t i;
    105 
    106 	uuid->time_low = le32dec(p);
    107 	uuid->time_mid = le16dec(p + 4);
    108 	uuid->time_hi_and_version = le16dec(p + 6);
    109 	uuid->clock_seq_hi_and_reserved = p[8];
    110 	uuid->clock_seq_low = p[9];
    111 	for (i = 0; i < sizeof(uuid->node); i++)
    112 		uuid->node[i] = p[10 + i];
    113 }
    114 
    115 static void
    116 gpt_dce_to_uuid(const struct dce_uuid *uuid, uint8_t *buf)
    117 {
    118 	uint8_t *p = buf;
    119 	size_t i;
    120 
    121 	le32enc(p, uuid->time_low);
    122 	le16enc(p + 4, uuid->time_mid);
    123 	le16enc(p + 6, uuid->time_hi_and_version);
    124 	p[8] = uuid->clock_seq_hi_and_reserved;
    125 	p[9] = uuid->clock_seq_low;
    126 	for (i = 0; i < sizeof(uuid->node); i++)
    127 		p[10 + i] = uuid->node[i];
    128 }
    129 
    130 static int
    131 gpt_uuid_numeric(char *buf, size_t bufsiz, const struct dce_uuid *u)
    132 {
    133 	return snprintf(buf, bufsiz,
    134 	    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
    135 	    u->time_low, u->time_mid, u->time_hi_and_version,
    136 	    u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0],
    137 	    u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
    138 }
    139 
    140 
    141 static int
    142 gpt_uuid_symbolic(char *buf, size_t bufsiz, const struct dce_uuid *u)
    143 {
    144 	size_t i;
    145 
    146 	for (i = 0; i < __arraycount(gpt_nv); i++)
    147 		if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0)
    148 			return (int)strlcpy(buf, gpt_nv[i].n, bufsiz);
    149 	return -1;
    150 }
    151 
    152 static int
    153 gpt_uuid_descriptive(char *buf, size_t bufsiz, const struct dce_uuid *u)
    154 {
    155 	size_t i;
    156 
    157 	for (i = 0; i < __arraycount(gpt_nv); i++)
    158 		if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0)
    159 			return (int)strlcpy(buf, gpt_nv[i].d, bufsiz);
    160 	return -1;
    161 }
    162 
    163 int
    164 gpt_uuid_snprintf(char *buf, size_t bufsiz, const char *fmt,
    165     const gpt_uuid_t uu)
    166 {
    167 	struct dce_uuid u;
    168 	gpt_uuid_to_dce(uu, &u);
    169 
    170 	if (fmt[1] == 's') {
    171 		int r;
    172 		if ((r = gpt_uuid_symbolic(buf, bufsiz, &u)) != -1)
    173 			return r;
    174 	}
    175 	if (fmt[1] == 'l') {
    176 		int r;
    177 		if ((r = gpt_uuid_descriptive(buf, bufsiz, &u)) != -1)
    178 			return r;
    179 	}
    180 	return gpt_uuid_numeric(buf, bufsiz, &u);
    181 }
    182 
    183 static int
    184 gpt_uuid_parse_numeric(const char *s, struct dce_uuid *u)
    185 {
    186 	int n;
    187 
    188 	if (s == NULL || *s == '\0') {
    189 		memset(u, 0, sizeof(*u));
    190 		return 0;
    191 	}
    192 
    193 	n = sscanf(s,
    194 	    "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
    195 	    &u->time_low, &u->time_mid, &u->time_hi_and_version,
    196 	    &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
    197 	    &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
    198 
    199 	/* Make sure we have all conversions. */
    200 	if (n != 11)
    201 		return -1;
    202 
    203 	/* We have a successful scan. Check semantics... */
    204 	n = u->clock_seq_hi_and_reserved;
    205 	if ((n & 0x80) != 0x00 &&			/* variant 0? */
    206 	    (n & 0xc0) != 0x80 &&			/* variant 1? */
    207 	    (n & 0xe0) != 0xc0) 			/* variant 2? */
    208 		return -1;
    209 	return 0;
    210 }
    211 
    212 static int
    213 gpt_uuid_parse_symbolic(const char *s, struct dce_uuid *u)
    214 {
    215 	size_t i;
    216 
    217 	for (i = 0; i < __arraycount(gpt_nv); i++)
    218 		if (strcmp(gpt_nv[i].n, s) == 0) {
    219 			*u = gpt_nv[i].u;
    220 			return 0;
    221 		}
    222 	return -1;
    223 }
    224 
    225 int
    226 gpt_uuid_parse(const char *s, gpt_uuid_t uuid)
    227 {
    228 	struct dce_uuid u;
    229 
    230 	if (gpt_uuid_parse_numeric(s, &u) != -1) {
    231 		gpt_dce_to_uuid(&u, uuid);
    232 		return 0;
    233 	}
    234 
    235 	if (gpt_uuid_parse_symbolic(s, &u) == -1)
    236 		return -1;
    237 
    238 	gpt_dce_to_uuid(&u, uuid);
    239 	return 0;
    240 }
    241 
    242 size_t
    243 gpt_uuid_query(
    244     void (*func)(const char *uuid, const char *short_name, const char *desc))
    245 {
    246 	size_t i;
    247 	char buf[64];
    248 
    249 	if (func != NULL) {
    250 		for (i = 0; i < __arraycount(gpt_nv); i++) {
    251 			gpt_uuid_numeric(buf, sizeof(buf), &gpt_nv[i].u);
    252 			(*func)(buf, gpt_nv[i].n, gpt_nv[i].d);
    253 		}
    254 	}
    255 	return __arraycount(gpt_nv);
    256 }
    257 
    258 #ifndef GPT_UUID_QUERY_ONLY
    259 void
    260 gpt_uuid_help(const char *prefix)
    261 {
    262 	size_t i;
    263 
    264 	for (i = 0; i < __arraycount(gpt_nv); i++)
    265 		printf("%s%18.18s\t%s\n", prefix, gpt_nv[i].n, gpt_nv[i].d);
    266 }
    267 
    268 void
    269 gpt_uuid_create(gpt_type_t t, gpt_uuid_t u, uint16_t *b, size_t s)
    270 {
    271 	gpt_dce_to_uuid(&gpt_nv[t].u, u);
    272 	if (b)
    273 		utf8_to_utf16((const uint8_t *)gpt_nv[t].d, b, s / sizeof(*b));
    274 }
    275 
    276 static int
    277 gpt_uuid_random(gpt_t gpt, struct dce_uuid *u, size_t n)
    278 {
    279 	int fd;
    280 	uint8_t *p;
    281 	ssize_t nread;
    282 
    283 	/* Randomly generate the content.  */
    284 	fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
    285 	if (fd == -1) {
    286 		gpt_warn(gpt, "Can't open `/dev/urandom'");
    287 		return -1;
    288 	}
    289 	for (p = (uint8_t *)u;  n > 0; p += nread, n -= (size_t)nread) {
    290 		nread = read(fd, p, n);
    291 		if (nread < 0) {
    292 			gpt_warn(gpt, "Can't read `/dev/urandom'");
    293 			goto out;
    294 		}
    295 		if (nread == 0) {
    296 			gpt_warn(gpt, "EOF from /dev/urandom");
    297 			goto out;
    298 		}
    299 		if ((size_t)nread > n) {
    300 			gpt_warnx(gpt, "read too much: %zd > %zu", nread, n);
    301 			goto out;
    302 		}
    303 	}
    304 	(void)close(fd);
    305 
    306 	/* Set the version number to 4.  */
    307 	u->time_hi_and_version &= (uint16_t)~0xf000;
    308 	u->time_hi_and_version |= 0x4000;
    309 
    310 	return 0;
    311 out:
    312 	(void)close(fd);
    313 	return -1;
    314 }
    315 
    316 /*
    317  * For reproducible builds, we can base UUIDs on one external timestamp.
    318  *
    319  * Bump timestamp by one 100ns unit to make them unique within a GPT.
    320  * Use zero clock sequence and node id, ideally these should also be
    321  * passed as input.
    322  */
    323 static int
    324 gpt_uuid_tstamp(gpt_t gpt, struct dce_uuid *u, size_t l __unused)
    325 {
    326 	uint64_t x;
    327 
    328 	/* check for underflow/overflow of 60bit UUID time */
    329 	if (gpt->timestamp < -12219292800 ||
    330 	    gpt->timestamp > 103072857660)
    331 		return -1;
    332 
    333 	/*
    334 	 * Convert to UUID epoch (Gregorian)
    335 	 * and 100ns units
    336 	 */
    337 	x = (uint64_t)(gpt->timestamp + 12219292800) * 10000000;
    338 
    339 	/* Make UUID unique */
    340 	x += gpt->uuidgen++;
    341 
    342 	/* Set UUID fields for version 1 */
    343 	u->time_low = x & UINT64_C(0xffffffff);
    344 	u->time_mid = (x >> 32) & 0xffff;
    345 	u->time_hi_and_version = 0x1000 | ((x >> 48) & 0xfff);
    346 
    347 	/*
    348 	 * The clock sequence should make UUIDs unique in case
    349 	 * the clock went backwards.
    350 	 */
    351 	u->clock_seq_hi_and_reserved = 0;
    352 	u->clock_seq_low = 0;
    353 
    354 	/*
    355 	 * A unique system identifier (usually MAC address)
    356 	 */
    357 	memset(u->node, 0, sizeof(u->node));
    358 
    359 	return 0;
    360 }
    361 
    362 int
    363 gpt_uuid_generate(gpt_t gpt, gpt_uuid_t t)
    364 {
    365 	int rv;
    366 	struct dce_uuid u;
    367 
    368 	if (gpt && (gpt->flags & GPT_TIMESTAMP))
    369 		rv = gpt_uuid_tstamp(gpt, &u, sizeof(u));
    370 	else
    371 		rv = gpt_uuid_random(gpt, &u, sizeof(u));
    372 
    373 	if (rv == -1)
    374 		return -1;
    375 
    376 	/* Fix the reserved bits.  */
    377 	u.clock_seq_hi_and_reserved &= (uint8_t)~0x40U;
    378 	u.clock_seq_hi_and_reserved |= 0x80;
    379 
    380 	gpt_dce_to_uuid(&u, t);
    381 	return 0;
    382 }
    383 #endif
    384