blob: c1cb9df6aac60f064428dc2216c537017fa73034 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Hobbs6e39cce2011-08-23 11:06:56 +00002/*
3 * Copyright 2011 Calxeda, Inc.
Jason Hobbs6e39cce2011-08-23 11:06:56 +00004 */
5
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +02006#include <common.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06007#include <env.h>
Simon Glass495a5dc2019-11-14 12:57:30 -07008#include <time.h>
Jason Hobbs6e39cce2011-08-23 11:06:56 +00009#include <linux/ctype.h>
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020010#include <errno.h>
11#include <common.h>
Przemyslaw Marczak0c813362014-04-02 10:20:03 +020012#include <asm/io.h>
13#include <part_efi.h>
14#include <malloc.h>
Jason Hobbs6e39cce2011-08-23 11:06:56 +000015
16/*
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020017 * UUID - Universally Unique IDentifier - 128 bits unique number.
18 * There are 5 versions and one variant of UUID defined by RFC4122
Przemyslaw Marczakb4285142014-04-02 10:20:04 +020019 * specification. A UUID contains a set of fields. The set varies
20 * depending on the version of the UUID, as shown below:
21 * - time, MAC address(v1),
22 * - user ID(v2),
23 * - MD5 of name or URL(v3),
24 * - random data(v4),
25 * - SHA-1 of name or URL(v5),
26 *
27 * Layout of UUID:
28 * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
29 * version - 4 bit (bit 4 through 7 of the time_hi_and_version)
30 * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
31 * variant: - bit 6 and 7 of clock_seq_hi_and_reserved
32 * node - 48 bit
33 *
34 * source: https://www.ietf.org/rfc/rfc4122.txt
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020035 *
36 * UUID binary format (16 bytes):
37 *
38 * 4B-2B-2B-2B-6B (big endian - network byte order)
39 *
40 * UUID string is 36 length of characters (36 bytes):
41 *
42 * 0 9 14 19 24
43 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
44 * be be be be be
45 *
46 * where x is a hexadecimal character. Fields are separated by '-'s.
47 * When converting to a binary UUID, le means the field should be converted
48 * to little endian and be means it should be converted to big endian.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000049 *
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020050 * UUID is also used as GUID (Globally Unique Identifier) with the same binary
51 * format but it differs in string format like below.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000052 *
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020053 * GUID:
Jason Hobbs6e39cce2011-08-23 11:06:56 +000054 * 0 9 14 19 24
55 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
56 * le le le be be
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020057 *
58 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000059 */
Jason Hobbs6e39cce2011-08-23 11:06:56 +000060int uuid_str_valid(const char *uuid)
61{
62 int i, valid;
63
64 if (uuid == NULL)
65 return 0;
66
67 for (i = 0, valid = 1; uuid[i] && valid; i++) {
68 switch (i) {
69 case 8: case 13: case 18: case 23:
70 valid = (uuid[i] == '-');
71 break;
72 default:
73 valid = isxdigit(uuid[i]);
74 break;
75 }
76 }
77
Przemyslaw Marczak0c813362014-04-02 10:20:03 +020078 if (i != UUID_STR_LEN || !valid)
Jason Hobbs6e39cce2011-08-23 11:06:56 +000079 return 0;
80
81 return 1;
82}
83
Patrick Delaunay65f94ed2015-10-27 11:00:28 +010084#ifdef CONFIG_PARTITION_TYPE_GUID
85static const struct {
86 const char *string;
87 efi_guid_t guid;
88} list_guid[] = {
89 {"system", PARTITION_SYSTEM_GUID},
90 {"mbr", LEGACY_MBR_PARTITION_GUID},
91 {"msft", PARTITION_MSFT_RESERVED_GUID},
92 {"data", PARTITION_BASIC_DATA_GUID},
93 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
94 {"raid", PARTITION_LINUX_RAID_GUID},
95 {"swap", PARTITION_LINUX_SWAP_GUID},
96 {"lvm", PARTITION_LINUX_LVM_GUID}
97};
98
99/*
100 * uuid_guid_get_bin() - this function get GUID bin for string
101 *
102 * @param guid_str - pointer to partition type string
103 * @param guid_bin - pointer to allocated array for big endian output [16B]
104 */
105int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
106{
107 int i;
108
109 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
110 if (!strcmp(list_guid[i].string, guid_str)) {
111 memcpy(guid_bin, &list_guid[i].guid, 16);
112 return 0;
113 }
114 }
115 return -ENODEV;
116}
117
118/*
119 * uuid_guid_get_str() - this function get string for GUID.
120 *
121 * @param guid_bin - pointer to string with partition type guid [16B]
122 * @param guid_str - pointer to allocated partition type string [7B]
123 */
Simon Glass4acd4f02020-04-08 08:32:58 -0600124int uuid_guid_get_str(const unsigned char *guid_bin, char *guid_str)
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100125{
126 int i;
127
128 *guid_str = 0;
129 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
130 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
131 strcpy(guid_str, list_guid[i].string);
132 return 0;
133 }
134 }
135 return -ENODEV;
136}
137#endif
138
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200139/*
140 * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
141 *
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100142 * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200143 * @param uuid_bin - pointer to allocated array for big endian output [16B]
144 * @str_format - UUID string format: 0 - UUID; 1 - GUID
145 */
Simon Glass4acd4f02020-04-08 08:32:58 -0600146int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
147 int str_format)
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000148{
149 uint16_t tmp16;
150 uint32_t tmp32;
151 uint64_t tmp64;
152
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100153 if (!uuid_str_valid(uuid_str)) {
154#ifdef CONFIG_PARTITION_TYPE_GUID
155 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
156 return 0;
157#endif
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200158 return -EINVAL;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100159 }
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200160
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200161 if (str_format == UUID_STR_FORMAT_STD) {
162 tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
163 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000164
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200165 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
166 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000167
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200168 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
169 memcpy(uuid_bin + 6, &tmp16, 2);
170 } else {
171 tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
172 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000173
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200174 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
175 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000176
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200177 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
178 memcpy(uuid_bin + 6, &tmp16, 2);
179 }
180
181 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
182 memcpy(uuid_bin + 8, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000183
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200184 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
185 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200186
187 return 0;
188}
189
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200190/*
191 * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
192 *
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200193 * @param uuid_bin: pointer to binary data of UUID (big endian) [16B]
194 * @param uuid_str: pointer to allocated array for output string [37B]
195 * @str_format: bit 0: 0 - UUID; 1 - GUID
196 * bit 1: 0 - lower case; 2 - upper case
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200197 */
Simon Glass4acd4f02020-04-08 08:32:58 -0600198void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
199 int str_format)
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200200{
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200201 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
202 9, 10, 11, 12, 13, 14, 15};
203 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
204 9, 10, 11, 12, 13, 14, 15};
205 const u8 *char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200206 const char *format;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200207 int i;
208
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200209 /*
210 * UUID and GUID bin data - always in big endian:
211 * 4B-2B-2B-2B-6B
212 * be be be be be
213 */
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200214 if (str_format & UUID_STR_FORMAT_GUID)
215 char_order = guid_char_order;
216 else
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200217 char_order = uuid_char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200218 if (str_format & UUID_STR_UPPER_CASE)
219 format = "%02X";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200220 else
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200221 format = "%02x";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200222
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200223 for (i = 0; i < 16; i++) {
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200224 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200225 uuid_str += 2;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200226 switch (i) {
227 case 3:
228 case 5:
229 case 7:
230 case 9:
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200231 *uuid_str++ = '-';
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200232 break;
233 }
234 }
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000235}
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200236
237/*
238 * gen_rand_uuid() - this function generates a random binary UUID version 4.
239 * In this version all fields beside 4 bits of version and
240 * 2 bits of variant are randomly generated.
241 *
242 * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
243*/
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200244#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200245void gen_rand_uuid(unsigned char *uuid_bin)
246{
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200247 u32 ptr[4];
248 struct uuid *uuid = (struct uuid *)ptr;
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200249 int i;
250
Eugeniu Rosca9d74ba62019-05-02 14:27:06 +0200251 srand(get_ticks() + rand());
252
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200253 /* Set all fields randomly */
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200254 for (i = 0; i < 4; i++)
255 ptr[i] = rand();
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200256
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200257 clrsetbits_be16(&uuid->time_hi_and_version,
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200258 UUID_VERSION_MASK,
259 UUID_VERSION << UUID_VERSION_SHIFT);
260
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200261 clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200262 UUID_VARIANT_MASK,
263 UUID_VARIANT << UUID_VARIANT_SHIFT);
264
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200265 memcpy(uuid_bin, uuid, 16);
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200266}
267
268/*
269 * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
270 * formats UUID or GUID.
271 *
272 * @param uuid_str - pointer to allocated array [37B].
273 * @param - uuid output type: UUID - 0, GUID - 1
274 */
275void gen_rand_uuid_str(char *uuid_str, int str_format)
276{
277 unsigned char uuid_bin[UUID_BIN_LEN];
278
279 /* Generate UUID (big endian) */
280 gen_rand_uuid(uuid_bin);
281
282 /* Convert UUID bin to UUID or GUID formated STRING */
283 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
284}
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200285
Marek Vasut51d85f62019-01-07 21:23:38 +0100286#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200287int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
288{
289 char uuid[UUID_STR_LEN + 1];
290 int str_format;
291
292 if (!strcmp(argv[0], "uuid"))
293 str_format = UUID_STR_FORMAT_STD;
294 else
295 str_format = UUID_STR_FORMAT_GUID;
296
297 if (argc > 2)
298 return CMD_RET_USAGE;
299
300 gen_rand_uuid_str(uuid, str_format);
301
302 if (argc == 1)
303 printf("%s\n", uuid);
304 else
Simon Glass6a38e412017-08-03 12:22:09 -0600305 env_set(argv[1], uuid);
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200306
307 return CMD_RET_SUCCESS;
308}
309
310U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
311 "UUID - generate random Universally Unique Identifier",
312 "[<varname>]\n"
313 "Argument:\n"
314 "varname: for set result in a environment variable\n"
315 "e.g. uuid uuid_env"
316);
317
318U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
319 "GUID - generate Globally Unique Identifier based on random UUID",
320 "[<varname>]\n"
321 "Argument:\n"
322 "varname: for set result in a environment variable\n"
323 "e.g. guid guid_env"
324);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +0200325#endif /* CONFIG_CMD_UUID */
326#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */