blob: a48e19c06eafab6c39249f7a005b97298768d6e0 [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>
Jason Hobbs6e39cce2011-08-23 11:06:56 +00008#include <linux/ctype.h>
Przemyslaw Marczakfa907342014-04-02 10:20:02 +02009#include <errno.h>
10#include <common.h>
Przemyslaw Marczak0c813362014-04-02 10:20:03 +020011#include <asm/io.h>
12#include <part_efi.h>
13#include <malloc.h>
Jason Hobbs6e39cce2011-08-23 11:06:56 +000014
15/*
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020016 * UUID - Universally Unique IDentifier - 128 bits unique number.
17 * There are 5 versions and one variant of UUID defined by RFC4122
Przemyslaw Marczakb4285142014-04-02 10:20:04 +020018 * 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),
21 * - user ID(v2),
22 * - MD5 of name or URL(v3),
23 * - random data(v4),
24 * - SHA-1 of name or URL(v5),
25 *
26 * Layout of UUID:
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
31 * node - 48 bit
32 *
33 * source: https://www.ietf.org/rfc/rfc4122.txt
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020034 *
35 * UUID binary format (16 bytes):
36 *
37 * 4B-2B-2B-2B-6B (big endian - network byte order)
38 *
39 * UUID string is 36 length of characters (36 bytes):
40 *
41 * 0 9 14 19 24
42 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
43 * be be be be be
44 *
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.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000048 *
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020049 * UUID is also used as GUID (Globally Unique Identifier) with the same binary
50 * format but it differs in string format like below.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000051 *
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020052 * GUID:
Jason Hobbs6e39cce2011-08-23 11:06:56 +000053 * 0 9 14 19 24
54 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
55 * le le le be be
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020056 *
57 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000058 */
Jason Hobbs6e39cce2011-08-23 11:06:56 +000059int uuid_str_valid(const char *uuid)
60{
61 int i, valid;
62
63 if (uuid == NULL)
64 return 0;
65
66 for (i = 0, valid = 1; uuid[i] && valid; i++) {
67 switch (i) {
68 case 8: case 13: case 18: case 23:
69 valid = (uuid[i] == '-');
70 break;
71 default:
72 valid = isxdigit(uuid[i]);
73 break;
74 }
75 }
76
Przemyslaw Marczak0c813362014-04-02 10:20:03 +020077 if (i != UUID_STR_LEN || !valid)
Jason Hobbs6e39cce2011-08-23 11:06:56 +000078 return 0;
79
80 return 1;
81}
82
Patrick Delaunay65f94ed2015-10-27 11:00:28 +010083#ifdef CONFIG_PARTITION_TYPE_GUID
84static const struct {
85 const char *string;
86 efi_guid_t guid;
87} list_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}
96};
97
98/*
99 * uuid_guid_get_bin() - this function get GUID bin for string
100 *
101 * @param guid_str - pointer to partition type string
102 * @param guid_bin - pointer to allocated array for big endian output [16B]
103 */
104int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
105{
106 int i;
107
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);
111 return 0;
112 }
113 }
114 return -ENODEV;
115}
116
117/*
118 * uuid_guid_get_str() - this function get string for GUID.
119 *
120 * @param guid_bin - pointer to string with partition type guid [16B]
121 * @param guid_str - pointer to allocated partition type string [7B]
122 */
123int uuid_guid_get_str(unsigned char *guid_bin, char *guid_str)
124{
125 int i;
126
127 *guid_str = 0;
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);
131 return 0;
132 }
133 }
134 return -ENODEV;
135}
136#endif
137
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200138/*
139 * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
140 *
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100141 * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200142 * @param uuid_bin - pointer to allocated array for big endian output [16B]
143 * @str_format - UUID string format: 0 - UUID; 1 - GUID
144 */
145int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format)
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000146{
147 uint16_t tmp16;
148 uint32_t tmp32;
149 uint64_t tmp64;
150
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100151 if (!uuid_str_valid(uuid_str)) {
152#ifdef CONFIG_PARTITION_TYPE_GUID
153 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
154 return 0;
155#endif
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200156 return -EINVAL;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100157 }
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200158
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200159 if (str_format == UUID_STR_FORMAT_STD) {
160 tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
161 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000162
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200163 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
164 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000165
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200166 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
167 memcpy(uuid_bin + 6, &tmp16, 2);
168 } else {
169 tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
170 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000171
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200172 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
173 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000174
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200175 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
176 memcpy(uuid_bin + 6, &tmp16, 2);
177 }
178
179 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
180 memcpy(uuid_bin + 8, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000181
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200182 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
183 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200184
185 return 0;
186}
187
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200188/*
189 * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
190 *
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200191 * @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: bit 0: 0 - UUID; 1 - GUID
194 * bit 1: 0 - lower case; 2 - upper case
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200195 */
196void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format)
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200197{
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200198 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
199 9, 10, 11, 12, 13, 14, 15};
200 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
201 9, 10, 11, 12, 13, 14, 15};
202 const u8 *char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200203 const char *format;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200204 int i;
205
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200206 /*
207 * UUID and GUID bin data - always in big endian:
208 * 4B-2B-2B-2B-6B
209 * be be be be be
210 */
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200211 if (str_format & UUID_STR_FORMAT_GUID)
212 char_order = guid_char_order;
213 else
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200214 char_order = uuid_char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200215 if (str_format & UUID_STR_UPPER_CASE)
216 format = "%02X";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200217 else
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200218 format = "%02x";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200219
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200220 for (i = 0; i < 16; i++) {
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200221 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200222 uuid_str += 2;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200223 switch (i) {
224 case 3:
225 case 5:
226 case 7:
227 case 9:
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200228 *uuid_str++ = '-';
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200229 break;
230 }
231 }
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000232}
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200233
234/*
235 * gen_rand_uuid() - this function generates a random binary UUID version 4.
236 * In this version all fields beside 4 bits of version and
237 * 2 bits of variant are randomly generated.
238 *
239 * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
240*/
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200241#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200242void gen_rand_uuid(unsigned char *uuid_bin)
243{
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200244 u32 ptr[4];
245 struct uuid *uuid = (struct uuid *)ptr;
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200246 int i;
247
Eugeniu Rosca9d74ba62019-05-02 14:27:06 +0200248 srand(get_ticks() + rand());
249
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200250 /* Set all fields randomly */
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200251 for (i = 0; i < 4; i++)
252 ptr[i] = rand();
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200253
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200254 clrsetbits_be16(&uuid->time_hi_and_version,
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200255 UUID_VERSION_MASK,
256 UUID_VERSION << UUID_VERSION_SHIFT);
257
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200258 clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200259 UUID_VARIANT_MASK,
260 UUID_VARIANT << UUID_VARIANT_SHIFT);
261
Heinrich Schuchardt1c126e22019-07-14 23:31:50 +0200262 memcpy(uuid_bin, uuid, 16);
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200263}
264
265/*
266 * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
267 * formats UUID or GUID.
268 *
269 * @param uuid_str - pointer to allocated array [37B].
270 * @param - uuid output type: UUID - 0, GUID - 1
271 */
272void gen_rand_uuid_str(char *uuid_str, int str_format)
273{
274 unsigned char uuid_bin[UUID_BIN_LEN];
275
276 /* Generate UUID (big endian) */
277 gen_rand_uuid(uuid_bin);
278
279 /* Convert UUID bin to UUID or GUID formated STRING */
280 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
281}
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200282
Marek Vasut51d85f62019-01-07 21:23:38 +0100283#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200284int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
285{
286 char uuid[UUID_STR_LEN + 1];
287 int str_format;
288
289 if (!strcmp(argv[0], "uuid"))
290 str_format = UUID_STR_FORMAT_STD;
291 else
292 str_format = UUID_STR_FORMAT_GUID;
293
294 if (argc > 2)
295 return CMD_RET_USAGE;
296
297 gen_rand_uuid_str(uuid, str_format);
298
299 if (argc == 1)
300 printf("%s\n", uuid);
301 else
Simon Glass6a38e412017-08-03 12:22:09 -0600302 env_set(argv[1], uuid);
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200303
304 return CMD_RET_SUCCESS;
305}
306
307U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
308 "UUID - generate random Universally Unique Identifier",
309 "[<varname>]\n"
310 "Argument:\n"
311 "varname: for set result in a environment variable\n"
312 "e.g. uuid uuid_env"
313);
314
315U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
316 "GUID - generate Globally Unique Identifier based on random UUID",
317 "[<varname>]\n"
318 "Argument:\n"
319 "varname: for set result in a environment variable\n"
320 "e.g. guid guid_env"
321);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +0200322#endif /* CONFIG_CMD_UUID */
323#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */