gpt_uuid.c revision 1.2
1/* $NetBSD: gpt_uuid.c,v 1.2 2014/09/30 22:56:36 jnemeth 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.2 2014/09/30 22:56:36 jnemeth Exp $"); 36#endif 37 38#include <stdio.h> 39 40#include "map.h" 41#include "gpt.h" 42 43#ifndef HAVE_NBTOOLS_CONFIG_H 44#include <sys/endian.h> 45#endif 46 47const gpt_uuid_t gpt_uuid_nil; 48 49struct dce_uuid { 50 uint32_t time_low; 51 uint16_t time_mid; 52 uint16_t time_hi_and_version; 53 uint8_t clock_seq_hi_and_reserved; 54 uint8_t clock_seq_low; 55 uint8_t node[6]; 56}; 57 58static const struct { 59 struct dce_uuid u; 60 const char *n; 61 const char *d; 62} gpt_nv[] = { 63 { GPT_ENT_TYPE_APPLE_HFS, "apple", "Apple HFS" }, 64 { GPT_ENT_TYPE_BIOS, "bios", "BIOS Boot" }, 65 { GPT_ENT_TYPE_EFI, "efi", "EFI System" }, 66 { GPT_ENT_TYPE_FREEBSD, "fbsd-legacy", "FreeBSD legacy" }, 67 { GPT_ENT_TYPE_FREEBSD_SWAP, "fbsd-swap", "FreeBSD swap" }, 68 { GPT_ENT_TYPE_FREEBSD_UFS, "fbsd-ufs", "FreeBSD UFS/UFS2" }, 69 { GPT_ENT_TYPE_FREEBSD_VINUM, "fbsd-vinum", "FreeBSD vinum" }, 70 { GPT_ENT_TYPE_FREEBSD_ZFS, "fbsd-zfs", "FreeBSD ZFS" }, 71 { GPT_ENT_TYPE_LINUX_DATA, "linux-data", "Linux data" }, 72 { GPT_ENT_TYPE_LINUX_SWAP, "linux-swap", "Linux swap" }, 73 { GPT_ENT_TYPE_MS_BASIC_DATA, "windows", "Windows basic data" }, 74 { GPT_ENT_TYPE_MS_RESERVED, "windows-reserved", "Windows reserved" }, 75 { GPT_ENT_TYPE_NETBSD_CCD, "ccd", "NetBSD ccd component" }, 76 { GPT_ENT_TYPE_NETBSD_CGD, "cgd", "NetBSD Cryptographic Disk" }, 77 { GPT_ENT_TYPE_NETBSD_FFS, "ffs", "NetBSD FFSv1/FFSv2" }, 78 { GPT_ENT_TYPE_NETBSD_LFS, "lfs", "NetBSD LFS" }, 79 { GPT_ENT_TYPE_NETBSD_RAIDFRAME, "raid", 80 "NetBSD RAIDFrame component" }, 81 { GPT_ENT_TYPE_NETBSD_SWAP, "swap", "NetBSD swap" }, 82}; 83 84static void 85gpt_uuid_to_dce(const gpt_uuid_t buf, struct dce_uuid *uuid) 86{ 87 const uint8_t *p = buf; 88 size_t i; 89 90 uuid->time_low = le32dec(p); 91 uuid->time_mid = le16dec(p + 4); 92 uuid->time_hi_and_version = le16dec(p + 6); 93 uuid->clock_seq_hi_and_reserved = p[8]; 94 uuid->clock_seq_low = p[9]; 95 for (i = 0; i < sizeof(uuid->node); i++) 96 uuid->node[i] = p[10 + i]; 97} 98 99static void 100gpt_dce_to_uuid(const struct dce_uuid *uuid, uint8_t *buf) 101{ 102 uint8_t *p = buf; 103 size_t i; 104 105 le32enc(p, uuid->time_low); 106 le16enc(p + 4, uuid->time_mid); 107 le16enc(p + 6, uuid->time_hi_and_version); 108 p[8] = uuid->clock_seq_hi_and_reserved; 109 p[9] = uuid->clock_seq_low; 110 for (i = 0; i < sizeof(uuid->node); i++) 111 p[10 + i] = uuid->node[i]; 112} 113 114static int 115gpt_uuid_numeric(char *buf, size_t bufsiz, const struct dce_uuid *u) 116{ 117 return snprintf(buf, bufsiz, 118 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", 119 u->time_low, u->time_mid, u->time_hi_and_version, 120 u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0], 121 u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]); 122} 123 124 125static int 126gpt_uuid_symbolic(char *buf, size_t bufsiz, const struct dce_uuid *u) 127{ 128 size_t i; 129 130 for (i = 0; i < __arraycount(gpt_nv); i++) 131 if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0) 132 return strlcpy(buf, gpt_nv[i].n, bufsiz); 133 return -1; 134} 135 136static int 137gpt_uuid_descriptive(char *buf, size_t bufsiz, const struct dce_uuid *u) 138{ 139 size_t i; 140 141 for (i = 0; i < __arraycount(gpt_nv); i++) 142 if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0) 143 return strlcpy(buf, gpt_nv[i].d, bufsiz); 144 return -1; 145} 146 147int 148gpt_uuid_snprintf(char *buf, size_t bufsiz, const char *fmt, 149 const gpt_uuid_t uu) 150{ 151 struct dce_uuid u; 152 gpt_uuid_to_dce(uu, &u); 153 154 if (fmt[1] == 's') { 155 int r; 156 if ((r = gpt_uuid_symbolic(buf, bufsiz, &u)) != -1) 157 return r; 158 } 159 if (fmt[1] == 'l') { 160 int r; 161 if ((r = gpt_uuid_descriptive(buf, bufsiz, &u)) != -1) 162 return r; 163 } 164 return gpt_uuid_numeric(buf, bufsiz, &u); 165} 166 167static int 168gpt_uuid_parse_numeric(const char *s, struct dce_uuid *u) 169{ 170 int n; 171 172 if (s == NULL || *s == '\0') { 173 memset(u, 0, sizeof(*u)); 174 return 0; 175 } 176 177 n = sscanf(s, 178 "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", 179 &u->time_low, &u->time_mid, &u->time_hi_and_version, 180 &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0], 181 &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]); 182 183 /* Make sure we have all conversions. */ 184 if (n != 11) 185 return -1; 186 187 /* We have a successful scan. Check semantics... */ 188 n = u->clock_seq_hi_and_reserved; 189 if ((n & 0x80) != 0x00 && /* variant 0? */ 190 (n & 0xc0) != 0x80 && /* variant 1? */ 191 (n & 0xe0) != 0xc0) /* variant 2? */ 192 return -1; 193 return 0; 194} 195 196static int 197gpt_uuid_parse_symbolic(const char *s, struct dce_uuid *u) 198{ 199 size_t i; 200 201 for (i = 0; i < __arraycount(gpt_nv); i++) 202 if (strcmp(gpt_nv[i].n, s) == 0) { 203 *u = gpt_nv[i].u; 204 return 0; 205 } 206 return -1; 207} 208 209int 210gpt_uuid_parse(const char *s, gpt_uuid_t uuid) 211{ 212 struct dce_uuid u; 213 214 if (gpt_uuid_parse_numeric(s, &u) != -1) 215 return 0; 216 217 if (gpt_uuid_parse_symbolic(s, &u) == -1) 218 return -1; 219 220 gpt_dce_to_uuid(&u, uuid); 221 return 0; 222} 223 224void 225gpt_uuid_create(gpt_type_t t, gpt_uuid_t u, uint16_t *b, size_t s) 226{ 227 gpt_dce_to_uuid(&gpt_nv[t].u, u); 228 if (b) 229 utf8_to_utf16((const uint8_t *)gpt_nv[t].d, b, s / sizeof(*b)); 230} 231