xref: /OK3568_Linux_fs/u-boot/lib/uuid.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2011 Calxeda, Inc.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <common.h>
8*4882a593Smuzhiyun #include <linux/ctype.h>
9*4882a593Smuzhiyun #include <errno.h>
10*4882a593Smuzhiyun #include <common.h>
11*4882a593Smuzhiyun #include <asm/io.h>
12*4882a593Smuzhiyun #include <part_efi.h>
13*4882a593Smuzhiyun #include <malloc.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun  * UUID - Universally Unique IDentifier - 128 bits unique number.
17*4882a593Smuzhiyun  *        There are 5 versions and one variant of UUID defined by RFC4122
18*4882a593Smuzhiyun  *        specification. A UUID contains a set of fields. The set varies
19*4882a593Smuzhiyun  *        depending on the version of the UUID, as shown below:
20*4882a593Smuzhiyun  *        - time, MAC address(v1),
21*4882a593Smuzhiyun  *        - user ID(v2),
22*4882a593Smuzhiyun  *        - MD5 of name or URL(v3),
23*4882a593Smuzhiyun  *        - random data(v4),
24*4882a593Smuzhiyun  *        - SHA-1 of name or URL(v5),
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * Layout of UUID:
27*4882a593Smuzhiyun  * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
28*4882a593Smuzhiyun  * version   - 4 bit (bit 4 through 7 of the time_hi_and_version)
29*4882a593Smuzhiyun  * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
30*4882a593Smuzhiyun  * variant:  - bit 6 and 7 of clock_seq_hi_and_reserved
31*4882a593Smuzhiyun  * node      - 48 bit
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * source: https://www.ietf.org/rfc/rfc4122.txt
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * UUID binary format (16 bytes):
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * 4B-2B-2B-2B-6B (big endian - network byte order)
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  * UUID string is 36 length of characters (36 bytes):
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * 0        9    14   19   24
42*4882a593Smuzhiyun  * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
43*4882a593Smuzhiyun  *    be     be   be   be       be
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * where x is a hexadecimal character. Fields are separated by '-'s.
46*4882a593Smuzhiyun  * When converting to a binary UUID, le means the field should be converted
47*4882a593Smuzhiyun  * to little endian and be means it should be converted to big endian.
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  * UUID is also used as GUID (Globally Unique Identifier) with the same binary
50*4882a593Smuzhiyun  * format but it differs in string format like below.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * GUID:
53*4882a593Smuzhiyun  * 0        9    14   19   24
54*4882a593Smuzhiyun  * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
55*4882a593Smuzhiyun  *    le     le   le   be       be
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
58*4882a593Smuzhiyun  */
uuid_str_valid(const char * uuid)59*4882a593Smuzhiyun int uuid_str_valid(const char *uuid)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun 	int i, valid;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	if (uuid == NULL)
64*4882a593Smuzhiyun 		return 0;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	for (i = 0, valid = 1; uuid[i] && valid; i++) {
67*4882a593Smuzhiyun 		switch (i) {
68*4882a593Smuzhiyun 		case 8: case 13: case 18: case 23:
69*4882a593Smuzhiyun 			valid = (uuid[i] == '-');
70*4882a593Smuzhiyun 			break;
71*4882a593Smuzhiyun 		default:
72*4882a593Smuzhiyun 			valid = isxdigit(uuid[i]);
73*4882a593Smuzhiyun 			break;
74*4882a593Smuzhiyun 		}
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	if (i != UUID_STR_LEN || !valid)
78*4882a593Smuzhiyun 		return 0;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	return 1;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun #ifdef CONFIG_PARTITION_TYPE_GUID
84*4882a593Smuzhiyun static const struct {
85*4882a593Smuzhiyun 	const char *string;
86*4882a593Smuzhiyun 	efi_guid_t guid;
87*4882a593Smuzhiyun } list_guid[] = {
88*4882a593Smuzhiyun 	{"system",	PARTITION_SYSTEM_GUID},
89*4882a593Smuzhiyun 	{"mbr",		LEGACY_MBR_PARTITION_GUID},
90*4882a593Smuzhiyun 	{"msft",	PARTITION_MSFT_RESERVED_GUID},
91*4882a593Smuzhiyun 	{"data",	PARTITION_BASIC_DATA_GUID},
92*4882a593Smuzhiyun 	{"linux",	PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
93*4882a593Smuzhiyun 	{"raid",	PARTITION_LINUX_RAID_GUID},
94*4882a593Smuzhiyun 	{"swap",	PARTITION_LINUX_SWAP_GUID},
95*4882a593Smuzhiyun 	{"lvm",		PARTITION_LINUX_LVM_GUID}
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun /*
99*4882a593Smuzhiyun  * uuid_guid_get_bin() - this function get GUID bin for string
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  * @param guid_str - pointer to partition type string
102*4882a593Smuzhiyun  * @param guid_bin - pointer to allocated array for big endian output [16B]
103*4882a593Smuzhiyun  */
uuid_guid_get_bin(const char * guid_str,unsigned char * guid_bin)104*4882a593Smuzhiyun int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	int i;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
109*4882a593Smuzhiyun 		if (!strcmp(list_guid[i].string, guid_str)) {
110*4882a593Smuzhiyun 			memcpy(guid_bin, &list_guid[i].guid, 16);
111*4882a593Smuzhiyun 			return 0;
112*4882a593Smuzhiyun 		}
113*4882a593Smuzhiyun 	}
114*4882a593Smuzhiyun 	return -ENODEV;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /*
118*4882a593Smuzhiyun  * uuid_guid_get_str() - this function get string for GUID.
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  * @param guid_bin - pointer to string with partition type guid [16B]
121*4882a593Smuzhiyun  * @param guid_str - pointer to allocated partition type string [7B]
122*4882a593Smuzhiyun  */
uuid_guid_get_str(unsigned char * guid_bin,char * guid_str)123*4882a593Smuzhiyun int uuid_guid_get_str(unsigned char *guid_bin, char *guid_str)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	int i;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	*guid_str = 0;
128*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
129*4882a593Smuzhiyun 		if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
130*4882a593Smuzhiyun 			strcpy(guid_str, list_guid[i].string);
131*4882a593Smuzhiyun 			return 0;
132*4882a593Smuzhiyun 		}
133*4882a593Smuzhiyun 	}
134*4882a593Smuzhiyun 	return -ENODEV;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun #endif
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun  * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
140*4882a593Smuzhiyun  *
141*4882a593Smuzhiyun  * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
142*4882a593Smuzhiyun  * @param uuid_bin - pointer to allocated array for big endian output [16B]
143*4882a593Smuzhiyun  * @str_format     - UUID string format: 0 - UUID; 1 - GUID
144*4882a593Smuzhiyun  */
uuid_str_to_bin(char * uuid_str,unsigned char * uuid_bin,int str_format)145*4882a593Smuzhiyun int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	uint16_t tmp16;
148*4882a593Smuzhiyun 	uint32_t tmp32;
149*4882a593Smuzhiyun 	uint64_t tmp64;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	if (!uuid_str_valid(uuid_str)) {
152*4882a593Smuzhiyun #ifdef CONFIG_PARTITION_TYPE_GUID
153*4882a593Smuzhiyun 		if (!uuid_guid_get_bin(uuid_str, uuid_bin))
154*4882a593Smuzhiyun 			return 0;
155*4882a593Smuzhiyun #endif
156*4882a593Smuzhiyun 		return -EINVAL;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	if (str_format == UUID_STR_FORMAT_STD) {
160*4882a593Smuzhiyun 		tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
161*4882a593Smuzhiyun 		memcpy(uuid_bin, &tmp32, 4);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 		tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
164*4882a593Smuzhiyun 		memcpy(uuid_bin + 4, &tmp16, 2);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 		tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
167*4882a593Smuzhiyun 		memcpy(uuid_bin + 6, &tmp16, 2);
168*4882a593Smuzhiyun 	} else {
169*4882a593Smuzhiyun 		tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
170*4882a593Smuzhiyun 		memcpy(uuid_bin, &tmp32, 4);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 		tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
173*4882a593Smuzhiyun 		memcpy(uuid_bin + 4, &tmp16, 2);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 		tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
176*4882a593Smuzhiyun 		memcpy(uuid_bin + 6, &tmp16, 2);
177*4882a593Smuzhiyun 	}
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
180*4882a593Smuzhiyun 	memcpy(uuid_bin + 8, &tmp16, 2);
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
183*4882a593Smuzhiyun 	memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	return 0;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun /*
189*4882a593Smuzhiyun  * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  * @param uuid_bin - pointer to binary data of UUID (big endian) [16B]
192*4882a593Smuzhiyun  * @param uuid_str - pointer to allocated array for output string [37B]
193*4882a593Smuzhiyun  * @str_format     - UUID string format: 0 - UUID; 1 - GUID
194*4882a593Smuzhiyun  */
uuid_bin_to_str(unsigned char * uuid_bin,char * uuid_str,int str_format)195*4882a593Smuzhiyun void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
198*4882a593Smuzhiyun 						  9, 10, 11, 12, 13, 14, 15};
199*4882a593Smuzhiyun 	const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
200*4882a593Smuzhiyun 						  9, 10, 11, 12, 13, 14, 15};
201*4882a593Smuzhiyun 	const u8 *char_order;
202*4882a593Smuzhiyun 	int i;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	/*
205*4882a593Smuzhiyun 	 * UUID and GUID bin data - always in big endian:
206*4882a593Smuzhiyun 	 * 4B-2B-2B-2B-6B
207*4882a593Smuzhiyun 	 * be be be be be
208*4882a593Smuzhiyun 	 */
209*4882a593Smuzhiyun 	if (str_format == UUID_STR_FORMAT_STD)
210*4882a593Smuzhiyun 		char_order = uuid_char_order;
211*4882a593Smuzhiyun 	else
212*4882a593Smuzhiyun 		char_order = guid_char_order;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	for (i = 0; i < 16; i++) {
215*4882a593Smuzhiyun 		sprintf(uuid_str, "%02x", uuid_bin[char_order[i]]);
216*4882a593Smuzhiyun 		uuid_str += 2;
217*4882a593Smuzhiyun 		switch (i) {
218*4882a593Smuzhiyun 		case 3:
219*4882a593Smuzhiyun 		case 5:
220*4882a593Smuzhiyun 		case 7:
221*4882a593Smuzhiyun 		case 9:
222*4882a593Smuzhiyun 			*uuid_str++ = '-';
223*4882a593Smuzhiyun 			break;
224*4882a593Smuzhiyun 		}
225*4882a593Smuzhiyun 	}
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun /*
229*4882a593Smuzhiyun  * gen_rand_uuid() - this function generates a random binary UUID version 4.
230*4882a593Smuzhiyun  *                   In this version all fields beside 4 bits of version and
231*4882a593Smuzhiyun  *                   2 bits of variant are randomly generated.
232*4882a593Smuzhiyun  *
233*4882a593Smuzhiyun  * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
234*4882a593Smuzhiyun */
235*4882a593Smuzhiyun #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
gen_rand_uuid(unsigned char * uuid_bin)236*4882a593Smuzhiyun void gen_rand_uuid(unsigned char *uuid_bin)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun 	u32 ptr[4];
239*4882a593Smuzhiyun 	struct uuid *uuid = (struct uuid *)ptr;
240*4882a593Smuzhiyun 	int i;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	/* Set all fields randomly */
243*4882a593Smuzhiyun 	for (i = 0; i < 4; i++)
244*4882a593Smuzhiyun 		ptr[i] = rand();
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	clrsetbits_be16(&uuid->time_hi_and_version,
247*4882a593Smuzhiyun 			UUID_VERSION_MASK,
248*4882a593Smuzhiyun 			UUID_VERSION << UUID_VERSION_SHIFT);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
251*4882a593Smuzhiyun 		     UUID_VARIANT_MASK,
252*4882a593Smuzhiyun 		     UUID_VARIANT << UUID_VARIANT_SHIFT);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	memcpy(uuid_bin, uuid, 16);
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun /*
258*4882a593Smuzhiyun  * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
259*4882a593Smuzhiyun  *                       formats UUID or GUID.
260*4882a593Smuzhiyun  *
261*4882a593Smuzhiyun  * @param uuid_str - pointer to allocated array [37B].
262*4882a593Smuzhiyun  * @param          - uuid output type: UUID - 0, GUID - 1
263*4882a593Smuzhiyun  */
gen_rand_uuid_str(char * uuid_str,int str_format)264*4882a593Smuzhiyun void gen_rand_uuid_str(char *uuid_str, int str_format)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun 	unsigned char uuid_bin[UUID_BIN_LEN];
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	/* Generate UUID (big endian) */
269*4882a593Smuzhiyun 	gen_rand_uuid(uuid_bin);
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	/* Convert UUID bin to UUID or GUID formated STRING  */
272*4882a593Smuzhiyun 	uuid_bin_to_str(uuid_bin, uuid_str, str_format);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun #ifdef CONFIG_CMD_UUID
do_uuid(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])276*4882a593Smuzhiyun int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	char uuid[UUID_STR_LEN + 1];
279*4882a593Smuzhiyun 	int str_format;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	if (!strcmp(argv[0], "uuid"))
282*4882a593Smuzhiyun 		str_format = UUID_STR_FORMAT_STD;
283*4882a593Smuzhiyun 	else
284*4882a593Smuzhiyun 		str_format = UUID_STR_FORMAT_GUID;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	if (argc > 2)
287*4882a593Smuzhiyun 		return CMD_RET_USAGE;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	gen_rand_uuid_str(uuid, str_format);
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	if (argc == 1)
292*4882a593Smuzhiyun 		printf("%s\n", uuid);
293*4882a593Smuzhiyun 	else
294*4882a593Smuzhiyun 		env_set(argv[1], uuid);
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	return CMD_RET_SUCCESS;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
300*4882a593Smuzhiyun 	   "UUID - generate random Universally Unique Identifier",
301*4882a593Smuzhiyun 	   "[<varname>]\n"
302*4882a593Smuzhiyun 	   "Argument:\n"
303*4882a593Smuzhiyun 	   "varname: for set result in a environment variable\n"
304*4882a593Smuzhiyun 	   "e.g. uuid uuid_env"
305*4882a593Smuzhiyun );
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
308*4882a593Smuzhiyun 	   "GUID - generate Globally Unique Identifier based on random UUID",
309*4882a593Smuzhiyun 	   "[<varname>]\n"
310*4882a593Smuzhiyun 	   "Argument:\n"
311*4882a593Smuzhiyun 	   "varname: for set result in a environment variable\n"
312*4882a593Smuzhiyun 	   "e.g. guid guid_env"
313*4882a593Smuzhiyun );
314*4882a593Smuzhiyun #endif /* CONFIG_CMD_UUID */
315*4882a593Smuzhiyun #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */
316