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)
83 #ifdef CONFIG_PARTITION_TYPE_GUID
88 {"system", PARTITION_SYSTEM_GUID},
89 {"mbr", LEGACY_MBR_PARTITION_GUID},
90 {"msft", PARTITION_MSFT_RESERVED_GUID},
91 {"data", PARTITION_BASIC_DATA_GUID},
92 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
93 {"raid", PARTITION_LINUX_RAID_GUID},
94 {"swap", PARTITION_LINUX_SWAP_GUID},
95 {"lvm", PARTITION_LINUX_LVM_GUID}
99 * uuid_guid_get_bin() - this function get GUID bin for string
101 * @param guid_str - pointer to partition type string
102 * @param guid_bin - pointer to allocated array for big endian output [16B]
104 int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
108 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
109 if (!strcmp(list_guid[i].string, guid_str)) {
110 memcpy(guid_bin, &list_guid[i].guid, 16);
118 * uuid_guid_get_str() - this function get string for GUID.
120 * @param guid_bin - pointer to string with partition type guid [16B]
121 * @param guid_str - pointer to allocated partition type string [7B]
123 int uuid_guid_get_str(unsigned char *guid_bin, char *guid_str)
128 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
129 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
130 strcpy(guid_str, list_guid[i].string);
139 * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
141 * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
142 * @param uuid_bin - pointer to allocated array for big endian output [16B]
143 * @str_format - UUID string format: 0 - UUID; 1 - GUID
145 int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format)
151 if (!uuid_str_valid(uuid_str)) {
152 #ifdef CONFIG_PARTITION_TYPE_GUID
153 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
159 if (str_format == UUID_STR_FORMAT_STD) {
160 tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
161 memcpy(uuid_bin, &tmp32, 4);
163 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
164 memcpy(uuid_bin + 4, &tmp16, 2);
166 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
167 memcpy(uuid_bin + 6, &tmp16, 2);
169 tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
170 memcpy(uuid_bin, &tmp32, 4);
172 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
173 memcpy(uuid_bin + 4, &tmp16, 2);
175 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
176 memcpy(uuid_bin + 6, &tmp16, 2);
179 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
180 memcpy(uuid_bin + 8, &tmp16, 2);
182 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
183 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
189 * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
191 * @param uuid_bin - pointer to binary data of UUID (big endian) [16B]
192 * @param uuid_str - pointer to allocated array for output string [37B]
193 * @str_format - UUID string format: 0 - UUID; 1 - GUID
195 void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format)
197 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
198 9, 10, 11, 12, 13, 14, 15};
199 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
200 9, 10, 11, 12, 13, 14, 15};
201 const u8 *char_order;
205 * UUID and GUID bin data - always in big endian:
209 if (str_format == UUID_STR_FORMAT_STD)
210 char_order = uuid_char_order;
212 char_order = guid_char_order;
214 for (i = 0; i < 16; i++) {
215 sprintf(uuid_str, "%02x", uuid_bin[char_order[i]]);
229 * gen_rand_uuid() - this function generates a random binary UUID version 4.
230 * In this version all fields beside 4 bits of version and
231 * 2 bits of variant are randomly generated.
233 * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
235 #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
236 void gen_rand_uuid(unsigned char *uuid_bin)
239 unsigned int *ptr = (unsigned int *)&uuid;
242 /* Set all fields randomly */
243 for (i = 0; i < sizeof(struct uuid) / sizeof(*ptr); i++)
244 *(ptr + i) = cpu_to_be32(rand());
246 clrsetbits_be16(&uuid.time_hi_and_version,
248 UUID_VERSION << UUID_VERSION_SHIFT);
250 clrsetbits_8(&uuid.clock_seq_hi_and_reserved,
252 UUID_VARIANT << UUID_VARIANT_SHIFT);
254 memcpy(uuid_bin, &uuid, sizeof(struct uuid));
258 * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
259 * formats UUID or GUID.
261 * @param uuid_str - pointer to allocated array [37B].
262 * @param - uuid output type: UUID - 0, GUID - 1
264 void gen_rand_uuid_str(char *uuid_str, int str_format)
266 unsigned char uuid_bin[UUID_BIN_LEN];
268 /* Generate UUID (big endian) */
269 gen_rand_uuid(uuid_bin);
271 /* Convert UUID bin to UUID or GUID formated STRING */
272 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
275 #ifdef CONFIG_CMD_UUID
276 int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
278 char uuid[UUID_STR_LEN + 1];
281 if (!strcmp(argv[0], "uuid"))
282 str_format = UUID_STR_FORMAT_STD;
284 str_format = UUID_STR_FORMAT_GUID;
287 return CMD_RET_USAGE;
289 gen_rand_uuid_str(uuid, str_format);
292 printf("%s\n", uuid);
294 setenv(argv[1], uuid);
296 return CMD_RET_SUCCESS;
299 U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
300 "UUID - generate random Universally Unique Identifier",
303 "varname: for set result in a environment variable\n"
307 U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
308 "GUID - generate Globally Unique Identifier based on random UUID",
311 "varname: for set result in a environment variable\n"
314 #endif /* CONFIG_CMD_UUID */
315 #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */