blob: 7d7a2749b6410650362b6b0fa9c1b618872b893f [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>
Jason Hobbs6e39cce2011-08-23 11:06:56 +00007#include <linux/ctype.h>
Przemyslaw Marczakfa907342014-04-02 10:20:02 +02008#include <errno.h>
9#include <common.h>
Przemyslaw Marczak0c813362014-04-02 10:20:03 +020010#include <asm/io.h>
11#include <part_efi.h>
12#include <malloc.h>
Jason Hobbs6e39cce2011-08-23 11:06:56 +000013
14/*
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020015 * UUID - Universally Unique IDentifier - 128 bits unique number.
16 * There are 5 versions and one variant of UUID defined by RFC4122
Przemyslaw Marczakb4285142014-04-02 10:20:04 +020017 * specification. A UUID contains a set of fields. The set varies
18 * depending on the version of the UUID, as shown below:
19 * - time, MAC address(v1),
20 * - user ID(v2),
21 * - MD5 of name or URL(v3),
22 * - random data(v4),
23 * - SHA-1 of name or URL(v5),
24 *
25 * Layout of UUID:
26 * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
27 * version - 4 bit (bit 4 through 7 of the time_hi_and_version)
28 * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
29 * variant: - bit 6 and 7 of clock_seq_hi_and_reserved
30 * node - 48 bit
31 *
32 * source: https://www.ietf.org/rfc/rfc4122.txt
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020033 *
34 * UUID binary format (16 bytes):
35 *
36 * 4B-2B-2B-2B-6B (big endian - network byte order)
37 *
38 * UUID string is 36 length of characters (36 bytes):
39 *
40 * 0 9 14 19 24
41 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
42 * be be be be be
43 *
44 * where x is a hexadecimal character. Fields are separated by '-'s.
45 * When converting to a binary UUID, le means the field should be converted
46 * to little endian and be means it should be converted to big endian.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000047 *
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020048 * UUID is also used as GUID (Globally Unique Identifier) with the same binary
49 * format but it differs in string format like below.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000050 *
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020051 * GUID:
Jason Hobbs6e39cce2011-08-23 11:06:56 +000052 * 0 9 14 19 24
53 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
54 * le le le be be
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020055 *
56 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000057 */
Jason Hobbs6e39cce2011-08-23 11:06:56 +000058int uuid_str_valid(const char *uuid)
59{
60 int i, valid;
61
62 if (uuid == NULL)
63 return 0;
64
65 for (i = 0, valid = 1; uuid[i] && valid; i++) {
66 switch (i) {
67 case 8: case 13: case 18: case 23:
68 valid = (uuid[i] == '-');
69 break;
70 default:
71 valid = isxdigit(uuid[i]);
72 break;
73 }
74 }
75
Przemyslaw Marczak0c813362014-04-02 10:20:03 +020076 if (i != UUID_STR_LEN || !valid)
Jason Hobbs6e39cce2011-08-23 11:06:56 +000077 return 0;
78
79 return 1;
80}
81
Patrick Delaunay65f94ed2015-10-27 11:00:28 +010082#ifdef CONFIG_PARTITION_TYPE_GUID
83static const struct {
84 const char *string;
85 efi_guid_t guid;
86} list_guid[] = {
87 {"system", PARTITION_SYSTEM_GUID},
88 {"mbr", LEGACY_MBR_PARTITION_GUID},
89 {"msft", PARTITION_MSFT_RESERVED_GUID},
90 {"data", PARTITION_BASIC_DATA_GUID},
91 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
92 {"raid", PARTITION_LINUX_RAID_GUID},
93 {"swap", PARTITION_LINUX_SWAP_GUID},
94 {"lvm", PARTITION_LINUX_LVM_GUID}
95};
96
97/*
98 * uuid_guid_get_bin() - this function get GUID bin for string
99 *
100 * @param guid_str - pointer to partition type string
101 * @param guid_bin - pointer to allocated array for big endian output [16B]
102 */
103int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
104{
105 int i;
106
107 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
108 if (!strcmp(list_guid[i].string, guid_str)) {
109 memcpy(guid_bin, &list_guid[i].guid, 16);
110 return 0;
111 }
112 }
113 return -ENODEV;
114}
115
116/*
117 * uuid_guid_get_str() - this function get string for GUID.
118 *
119 * @param guid_bin - pointer to string with partition type guid [16B]
120 * @param guid_str - pointer to allocated partition type string [7B]
121 */
122int uuid_guid_get_str(unsigned char *guid_bin, char *guid_str)
123{
124 int i;
125
126 *guid_str = 0;
127 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
128 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
129 strcpy(guid_str, list_guid[i].string);
130 return 0;
131 }
132 }
133 return -ENODEV;
134}
135#endif
136
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200137/*
138 * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
139 *
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100140 * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200141 * @param uuid_bin - pointer to allocated array for big endian output [16B]
142 * @str_format - UUID string format: 0 - UUID; 1 - GUID
143 */
144int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format)
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000145{
146 uint16_t tmp16;
147 uint32_t tmp32;
148 uint64_t tmp64;
149
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100150 if (!uuid_str_valid(uuid_str)) {
151#ifdef CONFIG_PARTITION_TYPE_GUID
152 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
153 return 0;
154#endif
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200155 return -EINVAL;
Patrick Delaunay65f94ed2015-10-27 11:00:28 +0100156 }
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200157
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200158 if (str_format == UUID_STR_FORMAT_STD) {
159 tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
160 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000161
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200162 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
163 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000164
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200165 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
166 memcpy(uuid_bin + 6, &tmp16, 2);
167 } else {
168 tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
169 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000170
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200171 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
172 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000173
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200174 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
175 memcpy(uuid_bin + 6, &tmp16, 2);
176 }
177
178 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
179 memcpy(uuid_bin + 8, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000180
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200181 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
182 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200183
184 return 0;
185}
186
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200187/*
188 * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
189 *
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200190 * @param uuid_bin: pointer to binary data of UUID (big endian) [16B]
191 * @param uuid_str: pointer to allocated array for output string [37B]
192 * @str_format: bit 0: 0 - UUID; 1 - GUID
193 * bit 1: 0 - lower case; 2 - upper case
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200194 */
195void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format)
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200196{
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200197 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;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200202 const char *format;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200203 int i;
204
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200205 /*
206 * UUID and GUID bin data - always in big endian:
207 * 4B-2B-2B-2B-6B
208 * be be be be be
209 */
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200210 if (str_format & UUID_STR_FORMAT_GUID)
211 char_order = guid_char_order;
212 else
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200213 char_order = uuid_char_order;
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200214 if (str_format & UUID_STR_UPPER_CASE)
215 format = "%02X";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200216 else
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200217 format = "%02x";
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200218
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200219 for (i = 0; i < 16; i++) {
Heinrich Schuchardt6f035662019-04-29 08:08:43 +0200220 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200221 uuid_str += 2;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200222 switch (i) {
223 case 3:
224 case 5:
225 case 7:
226 case 9:
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200227 *uuid_str++ = '-';
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200228 break;
229 }
230 }
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000231}
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200232
233/*
234 * gen_rand_uuid() - this function generates a random binary UUID version 4.
235 * In this version all fields beside 4 bits of version and
236 * 2 bits of variant are randomly generated.
237 *
238 * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
239*/
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200240#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200241void gen_rand_uuid(unsigned char *uuid_bin)
242{
243 struct uuid uuid;
244 unsigned int *ptr = (unsigned int *)&uuid;
245 int i;
246
Eugeniu Rosca9d74ba62019-05-02 14:27:06 +0200247 srand(get_ticks() + rand());
248
Przemyslaw Marczakb4285142014-04-02 10:20:04 +0200249 /* Set all fields randomly */
250 for (i = 0; i < sizeof(struct uuid) / sizeof(*ptr); i++)
251 *(ptr + i) = cpu_to_be32(rand());
252
253 clrsetbits_be16(&uuid.time_hi_and_version,
254 UUID_VERSION_MASK,
255 UUID_VERSION << UUID_VERSION_SHIFT);
256
257 clrsetbits_8(&uuid.clock_seq_hi_and_reserved,
258 UUID_VARIANT_MASK,
259 UUID_VARIANT << UUID_VARIANT_SHIFT);
260
261 memcpy(uuid_bin, &uuid, sizeof(struct uuid));
262}
263
264/*
265 * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
266 * formats UUID or GUID.
267 *
268 * @param uuid_str - pointer to allocated array [37B].
269 * @param - uuid output type: UUID - 0, GUID - 1
270 */
271void gen_rand_uuid_str(char *uuid_str, int str_format)
272{
273 unsigned char uuid_bin[UUID_BIN_LEN];
274
275 /* Generate UUID (big endian) */
276 gen_rand_uuid(uuid_bin);
277
278 /* Convert UUID bin to UUID or GUID formated STRING */
279 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
280}
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200281
Marek Vasut51d85f62019-01-07 21:23:38 +0100282#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200283int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
284{
285 char uuid[UUID_STR_LEN + 1];
286 int str_format;
287
288 if (!strcmp(argv[0], "uuid"))
289 str_format = UUID_STR_FORMAT_STD;
290 else
291 str_format = UUID_STR_FORMAT_GUID;
292
293 if (argc > 2)
294 return CMD_RET_USAGE;
295
296 gen_rand_uuid_str(uuid, str_format);
297
298 if (argc == 1)
299 printf("%s\n", uuid);
300 else
Simon Glass6a38e412017-08-03 12:22:09 -0600301 env_set(argv[1], uuid);
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +0200302
303 return CMD_RET_SUCCESS;
304}
305
306U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
307 "UUID - generate random Universally Unique Identifier",
308 "[<varname>]\n"
309 "Argument:\n"
310 "varname: for set result in a environment variable\n"
311 "e.g. uuid uuid_env"
312);
313
314U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
315 "GUID - generate Globally Unique Identifier based on random UUID",
316 "[<varname>]\n"
317 "Argument:\n"
318 "varname: for set result in a environment variable\n"
319 "e.g. guid guid_env"
320);
Przemyslaw Marczak5ec2ed22014-04-02 10:20:06 +0200321#endif /* CONFIG_CMD_UUID */
322#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */