2 * Copyright 2011 Calxeda, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
8 #include <linux/ctype.h>
16 * UUID - Universally Unique IDentifier - 128 bits unique number.
17 * There are 5 versions and one variant of UUID defined by RFC4122
18 * specification. A UUID contains a set of fields. The set varies
19 * depending on the version of the UUID, as shown below:
20 * - time, MAC address(v1),
22 * - MD5 of name or URL(v3),
24 * - SHA-1 of name or URL(v5),
27 * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
28 * version - 4 bit (bit 4 through 7 of the time_hi_and_version)
29 * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
30 * variant: - bit 6 and 7 of clock_seq_hi_and_reserved
33 * source: https://www.ietf.org/rfc/rfc4122.txt
35 * UUID binary format (16 bytes):
37 * 4B-2B-2B-2B-6B (big endian - network byte order)
39 * UUID string is 36 length of characters (36 bytes):
42 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
45 * where x is a hexadecimal character. Fields are separated by '-'s.
46 * When converting to a binary UUID, le means the field should be converted
47 * to little endian and be means it should be converted to big endian.
49 * UUID is also used as GUID (Globally Unique Identifier) with the same binary
50 * format but it differs in string format like below.
54 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
57 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
59 int uuid_str_valid(const char *uuid)
66 for (i = 0, valid = 1; uuid[i] && valid; i++) {
68 case 8: case 13: case 18: case 23:
69 valid = (uuid[i] == '-');
72 valid = isxdigit(uuid[i]);
77 if (i != UUID_STR_LEN || !valid)
84 * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
86 * @param uuid_str - pointer to UUID or GUID string [37B]
87 * @param uuid_bin - pointer to allocated array for big endian output [16B]
88 * @str_format - UUID string format: 0 - UUID; 1 - GUID
90 int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format)
96 if (!uuid_str_valid(uuid_str))
99 if (str_format == UUID_STR_FORMAT_STD) {
100 tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
101 memcpy(uuid_bin, &tmp32, 4);
103 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
104 memcpy(uuid_bin + 4, &tmp16, 2);
106 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
107 memcpy(uuid_bin + 6, &tmp16, 2);
109 tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
110 memcpy(uuid_bin, &tmp32, 4);
112 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
113 memcpy(uuid_bin + 4, &tmp16, 2);
115 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
116 memcpy(uuid_bin + 6, &tmp16, 2);
119 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
120 memcpy(uuid_bin + 8, &tmp16, 2);
122 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
123 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
129 * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
131 * @param uuid_bin - pointer to binary data of UUID (big endian) [16B]
132 * @param uuid_str - pointer to allocated array for output string [37B]
133 * @str_format - UUID string format: 0 - UUID; 1 - GUID
135 void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format)
137 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
138 9, 10, 11, 12, 13, 14, 15};
139 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
140 9, 10, 11, 12, 13, 14, 15};
141 const u8 *char_order;
145 * UUID and GUID bin data - always in big endian:
149 if (str_format == UUID_STR_FORMAT_STD)
150 char_order = uuid_char_order;
152 char_order = guid_char_order;
154 for (i = 0; i < 16; i++) {
155 sprintf(uuid_str, "%02x", uuid_bin[char_order[i]]);
169 * gen_rand_uuid() - this function generates a random binary UUID version 4.
170 * In this version all fields beside 4 bits of version and
171 * 2 bits of variant are randomly generated.
173 * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
175 #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
176 void gen_rand_uuid(unsigned char *uuid_bin)
179 unsigned int *ptr = (unsigned int *)&uuid;
182 /* Set all fields randomly */
183 for (i = 0; i < sizeof(struct uuid) / sizeof(*ptr); i++)
184 *(ptr + i) = cpu_to_be32(rand());
186 clrsetbits_be16(&uuid.time_hi_and_version,
188 UUID_VERSION << UUID_VERSION_SHIFT);
190 clrsetbits_8(&uuid.clock_seq_hi_and_reserved,
192 UUID_VARIANT << UUID_VARIANT_SHIFT);
194 memcpy(uuid_bin, &uuid, sizeof(struct uuid));
198 * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
199 * formats UUID or GUID.
201 * @param uuid_str - pointer to allocated array [37B].
202 * @param - uuid output type: UUID - 0, GUID - 1
204 void gen_rand_uuid_str(char *uuid_str, int str_format)
206 unsigned char uuid_bin[UUID_BIN_LEN];
208 /* Generate UUID (big endian) */
209 gen_rand_uuid(uuid_bin);
211 /* Convert UUID bin to UUID or GUID formated STRING */
212 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
215 #ifdef CONFIG_CMD_UUID
216 int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
218 char uuid[UUID_STR_LEN + 1];
221 if (!strcmp(argv[0], "uuid"))
222 str_format = UUID_STR_FORMAT_STD;
224 str_format = UUID_STR_FORMAT_GUID;
227 return CMD_RET_USAGE;
229 gen_rand_uuid_str(uuid, str_format);
232 printf("%s\n", uuid);
234 setenv(argv[1], uuid);
236 return CMD_RET_SUCCESS;
239 U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
240 "UUID - generate random Universally Unique Identifier",
243 "varname: for set result in a environment variable\n"
247 U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
248 "GUID - generate Globally Unique Identifier based on random UUID",
251 "varname: for set result in a environment variable\n"
254 #endif /* CONFIG_CMD_UUID */
255 #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */