blob: bdb137884c013a6ddf700940b21b53a9b9836c5e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Hobbse11938e2011-08-23 11:06:56 +00002/*
3 * Copyright 2011 Calxeda, Inc.
Jason Hobbse11938e2011-08-23 11:06:56 +00004 */
5
Przemyslaw Marczak89c82302014-04-02 10:20:05 +02006#include <common.h>
Simon Glass9fb625c2019-08-01 09:46:51 -06007#include <env.h>
Simon Glass10453152019-11-14 12:57:30 -07008#include <time.h>
Simon Glassba06b3c2020-05-10 11:39:52 -06009#include <uuid.h>
Jason Hobbse11938e2011-08-23 11:06:56 +000010#include <linux/ctype.h>
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020011#include <errno.h>
12#include <common.h>
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020013#include <asm/io.h>
14#include <part_efi.h>
15#include <malloc.h>
Jason Hobbse11938e2011-08-23 11:06:56 +000016
17/*
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020018 * UUID - Universally Unique IDentifier - 128 bits unique number.
19 * There are 5 versions and one variant of UUID defined by RFC4122
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +020020 * specification. A UUID contains a set of fields. The set varies
21 * depending on the version of the UUID, as shown below:
22 * - time, MAC address(v1),
23 * - user ID(v2),
24 * - MD5 of name or URL(v3),
25 * - random data(v4),
26 * - SHA-1 of name or URL(v5),
27 *
28 * Layout of UUID:
29 * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
30 * version - 4 bit (bit 4 through 7 of the time_hi_and_version)
31 * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
32 * variant: - bit 6 and 7 of clock_seq_hi_and_reserved
33 * node - 48 bit
34 *
35 * source: https://www.ietf.org/rfc/rfc4122.txt
Jason Hobbse11938e2011-08-23 11:06:56 +000036 *
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020037 * UUID binary format (16 bytes):
38 *
39 * 4B-2B-2B-2B-6B (big endian - network byte order)
40 *
41 * UUID string is 36 length of characters (36 bytes):
Jason Hobbse11938e2011-08-23 11:06:56 +000042 *
43 * 0 9 14 19 24
44 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020045 * be be be be be
46 *
47 * where x is a hexadecimal character. Fields are separated by '-'s.
48 * When converting to a binary UUID, le means the field should be converted
49 * to little endian and be means it should be converted to big endian.
50 *
51 * UUID is also used as GUID (Globally Unique Identifier) with the same binary
52 * format but it differs in string format like below.
53 *
54 * GUID:
55 * 0 9 14 19 24
56 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Jason Hobbse11938e2011-08-23 11:06:56 +000057 * le le le be be
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020058 *
59 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
Jason Hobbse11938e2011-08-23 11:06:56 +000060 */
Jason Hobbse11938e2011-08-23 11:06:56 +000061int uuid_str_valid(const char *uuid)
62{
63 int i, valid;
64
65 if (uuid == NULL)
66 return 0;
67
68 for (i = 0, valid = 1; uuid[i] && valid; i++) {
69 switch (i) {
70 case 8: case 13: case 18: case 23:
71 valid = (uuid[i] == '-');
72 break;
73 default:
74 valid = isxdigit(uuid[i]);
75 break;
76 }
77 }
78
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020079 if (i != UUID_STR_LEN || !valid)
Jason Hobbse11938e2011-08-23 11:06:56 +000080 return 0;
81
82 return 1;
83}
84
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +010085#ifdef CONFIG_PARTITION_TYPE_GUID
86static const struct {
87 const char *string;
88 efi_guid_t guid;
89} list_guid[] = {
90 {"system", PARTITION_SYSTEM_GUID},
91 {"mbr", LEGACY_MBR_PARTITION_GUID},
92 {"msft", PARTITION_MSFT_RESERVED_GUID},
93 {"data", PARTITION_BASIC_DATA_GUID},
94 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
95 {"raid", PARTITION_LINUX_RAID_GUID},
96 {"swap", PARTITION_LINUX_SWAP_GUID},
97 {"lvm", PARTITION_LINUX_LVM_GUID}
98};
99
100/*
101 * uuid_guid_get_bin() - this function get GUID bin for string
102 *
103 * @param guid_str - pointer to partition type string
104 * @param guid_bin - pointer to allocated array for big endian output [16B]
105 */
106int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
107{
108 int i;
109
110 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
111 if (!strcmp(list_guid[i].string, guid_str)) {
112 memcpy(guid_bin, &list_guid[i].guid, 16);
113 return 0;
114 }
115 }
116 return -ENODEV;
117}
118
119/*
120 * uuid_guid_get_str() - this function get string for GUID.
121 *
122 * @param guid_bin - pointer to string with partition type guid [16B]
123 * @param guid_str - pointer to allocated partition type string [7B]
124 */
Simon Glass2c2ca202020-04-08 08:32:58 -0600125int uuid_guid_get_str(const unsigned char *guid_bin, char *guid_str)
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100126{
127 int i;
128
129 *guid_str = 0;
130 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
131 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
132 strcpy(guid_str, list_guid[i].string);
133 return 0;
134 }
135 }
136 return -ENODEV;
137}
138#endif
139
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200140/*
141 * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
142 *
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100143 * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200144 * @param uuid_bin - pointer to allocated array for big endian output [16B]
145 * @str_format - UUID string format: 0 - UUID; 1 - GUID
146 */
Simon Glass2c2ca202020-04-08 08:32:58 -0600147int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
148 int str_format)
Jason Hobbse11938e2011-08-23 11:06:56 +0000149{
150 uint16_t tmp16;
151 uint32_t tmp32;
152 uint64_t tmp64;
153
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100154 if (!uuid_str_valid(uuid_str)) {
155#ifdef CONFIG_PARTITION_TYPE_GUID
156 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
157 return 0;
158#endif
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200159 return -EINVAL;
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100160 }
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200161
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200162 if (str_format == UUID_STR_FORMAT_STD) {
163 tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
164 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbse11938e2011-08-23 11:06:56 +0000165
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200166 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
167 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbse11938e2011-08-23 11:06:56 +0000168
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200169 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
170 memcpy(uuid_bin + 6, &tmp16, 2);
171 } else {
172 tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
173 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbse11938e2011-08-23 11:06:56 +0000174
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200175 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
176 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbse11938e2011-08-23 11:06:56 +0000177
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200178 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
179 memcpy(uuid_bin + 6, &tmp16, 2);
180 }
Jason Hobbse11938e2011-08-23 11:06:56 +0000181
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200182 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
183 memcpy(uuid_bin + 8, &tmp16, 2);
184
185 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
186 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200187
188 return 0;
189}
190
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200191/*
192 * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
193 *
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +0200194 * @param uuid_bin: pointer to binary data of UUID (big endian) [16B]
195 * @param uuid_str: pointer to allocated array for output string [37B]
196 * @str_format: bit 0: 0 - UUID; 1 - GUID
197 * bit 1: 0 - lower case; 2 - upper case
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200198 */
Simon Glass2c2ca202020-04-08 08:32:58 -0600199void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
200 int str_format)
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200201{
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200202 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
203 9, 10, 11, 12, 13, 14, 15};
204 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
205 9, 10, 11, 12, 13, 14, 15};
206 const u8 *char_order;
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +0200207 const char *format;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200208 int i;
209
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200210 /*
211 * UUID and GUID bin data - always in big endian:
212 * 4B-2B-2B-2B-6B
213 * be be be be be
214 */
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +0200215 if (str_format & UUID_STR_FORMAT_GUID)
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200216 char_order = guid_char_order;
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +0200217 else
218 char_order = uuid_char_order;
219 if (str_format & UUID_STR_UPPER_CASE)
220 format = "%02X";
221 else
222 format = "%02x";
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200223
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200224 for (i = 0; i < 16; i++) {
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +0200225 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200226 uuid_str += 2;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200227 switch (i) {
228 case 3:
229 case 5:
230 case 7:
231 case 9:
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200232 *uuid_str++ = '-';
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200233 break;
234 }
235 }
Jason Hobbse11938e2011-08-23 11:06:56 +0000236}
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200237
238/*
239 * gen_rand_uuid() - this function generates a random binary UUID version 4.
240 * In this version all fields beside 4 bits of version and
241 * 2 bits of variant are randomly generated.
242 *
243 * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
244*/
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200245#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200246void gen_rand_uuid(unsigned char *uuid_bin)
247{
Heinrich Schuchardta1b633d2019-07-14 23:31:50 +0200248 u32 ptr[4];
249 struct uuid *uuid = (struct uuid *)ptr;
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200250 int i;
251
Eugeniu Rosca4ccf6782019-05-02 14:27:06 +0200252 srand(get_ticks() + rand());
253
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200254 /* Set all fields randomly */
Heinrich Schuchardta1b633d2019-07-14 23:31:50 +0200255 for (i = 0; i < 4; i++)
256 ptr[i] = rand();
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200257
Heinrich Schuchardta1b633d2019-07-14 23:31:50 +0200258 clrsetbits_be16(&uuid->time_hi_and_version,
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200259 UUID_VERSION_MASK,
260 UUID_VERSION << UUID_VERSION_SHIFT);
261
Heinrich Schuchardta1b633d2019-07-14 23:31:50 +0200262 clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200263 UUID_VARIANT_MASK,
264 UUID_VARIANT << UUID_VARIANT_SHIFT);
265
Heinrich Schuchardta1b633d2019-07-14 23:31:50 +0200266 memcpy(uuid_bin, uuid, 16);
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200267}
268
269/*
270 * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
271 * formats UUID or GUID.
272 *
273 * @param uuid_str - pointer to allocated array [37B].
274 * @param - uuid output type: UUID - 0, GUID - 1
275 */
276void gen_rand_uuid_str(char *uuid_str, int str_format)
277{
278 unsigned char uuid_bin[UUID_BIN_LEN];
279
280 /* Generate UUID (big endian) */
281 gen_rand_uuid(uuid_bin);
282
283 /* Convert UUID bin to UUID or GUID formated STRING */
284 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
285}
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200286
Marek Vasut05f6da32019-01-07 21:23:38 +0100287#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200288int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
289{
290 char uuid[UUID_STR_LEN + 1];
291 int str_format;
292
293 if (!strcmp(argv[0], "uuid"))
294 str_format = UUID_STR_FORMAT_STD;
295 else
296 str_format = UUID_STR_FORMAT_GUID;
297
298 if (argc > 2)
299 return CMD_RET_USAGE;
300
301 gen_rand_uuid_str(uuid, str_format);
302
303 if (argc == 1)
304 printf("%s\n", uuid);
305 else
Simon Glass382bee52017-08-03 12:22:09 -0600306 env_set(argv[1], uuid);
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200307
308 return CMD_RET_SUCCESS;
309}
310
311U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
312 "UUID - generate random Universally Unique Identifier",
313 "[<varname>]\n"
314 "Argument:\n"
315 "varname: for set result in a environment variable\n"
316 "e.g. uuid uuid_env"
317);
318
319U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
320 "GUID - generate Globally Unique Identifier based on random UUID",
321 "[<varname>]\n"
322 "Argument:\n"
323 "varname: for set result in a environment variable\n"
324 "e.g. guid guid_env"
325);
Przemyslaw Marczak39206382014-04-02 10:20:06 +0200326#endif /* CONFIG_CMD_UUID */
327#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */