Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2011 Calxeda, Inc. |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Przemyslaw Marczak | 2eb40ee | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 6 | #include <common.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 7 | #include <command.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 8 | #include <env.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 9 | #include <rand.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 10 | #include <time.h> |
Simon Glass | 6f04498 | 2020-05-10 11:39:52 -0600 | [diff] [blame] | 11 | #include <uuid.h> |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 12 | #include <linux/ctype.h> |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 13 | #include <errno.h> |
| 14 | #include <common.h> |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 15 | #include <asm/io.h> |
| 16 | #include <part_efi.h> |
| 17 | #include <malloc.h> |
Matthias Brugger | d03560b | 2020-12-18 10:28:03 +0100 | [diff] [blame] | 18 | #include <dm/uclass.h> |
| 19 | #include <rng.h> |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 20 | |
| 21 | /* |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 22 | * UUID - Universally Unique IDentifier - 128 bits unique number. |
| 23 | * There are 5 versions and one variant of UUID defined by RFC4122 |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 24 | * specification. A UUID contains a set of fields. The set varies |
| 25 | * depending on the version of the UUID, as shown below: |
| 26 | * - time, MAC address(v1), |
| 27 | * - user ID(v2), |
| 28 | * - MD5 of name or URL(v3), |
| 29 | * - random data(v4), |
| 30 | * - SHA-1 of name or URL(v5), |
| 31 | * |
| 32 | * Layout of UUID: |
| 33 | * timestamp - 60-bit: time_low, time_mid, time_hi_and_version |
| 34 | * version - 4 bit (bit 4 through 7 of the time_hi_and_version) |
| 35 | * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low |
| 36 | * variant: - bit 6 and 7 of clock_seq_hi_and_reserved |
| 37 | * node - 48 bit |
| 38 | * |
| 39 | * source: https://www.ietf.org/rfc/rfc4122.txt |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 40 | * |
| 41 | * UUID binary format (16 bytes): |
| 42 | * |
| 43 | * 4B-2B-2B-2B-6B (big endian - network byte order) |
| 44 | * |
| 45 | * UUID string is 36 length of characters (36 bytes): |
| 46 | * |
| 47 | * 0 9 14 19 24 |
| 48 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 49 | * be be be be be |
| 50 | * |
| 51 | * where x is a hexadecimal character. Fields are separated by '-'s. |
| 52 | * When converting to a binary UUID, le means the field should be converted |
| 53 | * to little endian and be means it should be converted to big endian. |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 54 | * |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 55 | * UUID is also used as GUID (Globally Unique Identifier) with the same binary |
| 56 | * format but it differs in string format like below. |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 57 | * |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 58 | * GUID: |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 59 | * 0 9 14 19 24 |
| 60 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 61 | * le le le be be |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 62 | * |
| 63 | * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id. |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 64 | */ |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 65 | int uuid_str_valid(const char *uuid) |
| 66 | { |
| 67 | int i, valid; |
| 68 | |
| 69 | if (uuid == NULL) |
| 70 | return 0; |
| 71 | |
| 72 | for (i = 0, valid = 1; uuid[i] && valid; i++) { |
| 73 | switch (i) { |
| 74 | case 8: case 13: case 18: case 23: |
| 75 | valid = (uuid[i] == '-'); |
| 76 | break; |
| 77 | default: |
| 78 | valid = isxdigit(uuid[i]); |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 83 | if (i != UUID_STR_LEN || !valid) |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 84 | return 0; |
| 85 | |
| 86 | return 1; |
| 87 | } |
| 88 | |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 89 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 90 | static const struct { |
| 91 | const char *string; |
| 92 | efi_guid_t guid; |
| 93 | } list_guid[] = { |
| 94 | {"system", PARTITION_SYSTEM_GUID}, |
| 95 | {"mbr", LEGACY_MBR_PARTITION_GUID}, |
| 96 | {"msft", PARTITION_MSFT_RESERVED_GUID}, |
| 97 | {"data", PARTITION_BASIC_DATA_GUID}, |
| 98 | {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID}, |
| 99 | {"raid", PARTITION_LINUX_RAID_GUID}, |
| 100 | {"swap", PARTITION_LINUX_SWAP_GUID}, |
Rasmus Villemoes | dad0b63 | 2020-11-20 11:45:36 +0100 | [diff] [blame] | 101 | {"lvm", PARTITION_LINUX_LVM_GUID}, |
| 102 | {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT}, |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /* |
| 106 | * uuid_guid_get_bin() - this function get GUID bin for string |
| 107 | * |
| 108 | * @param guid_str - pointer to partition type string |
| 109 | * @param guid_bin - pointer to allocated array for big endian output [16B] |
| 110 | */ |
| 111 | int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin) |
| 112 | { |
| 113 | int i; |
| 114 | |
| 115 | for (i = 0; i < ARRAY_SIZE(list_guid); i++) { |
| 116 | if (!strcmp(list_guid[i].string, guid_str)) { |
| 117 | memcpy(guid_bin, &list_guid[i].guid, 16); |
| 118 | return 0; |
| 119 | } |
| 120 | } |
| 121 | return -ENODEV; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * uuid_guid_get_str() - this function get string for GUID. |
| 126 | * |
| 127 | * @param guid_bin - pointer to string with partition type guid [16B] |
Rasmus Villemoes | 7b70bdc | 2020-11-20 11:45:35 +0100 | [diff] [blame] | 128 | * |
| 129 | * Returns NULL if the type GUID is not known. |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 130 | */ |
Rasmus Villemoes | 7b70bdc | 2020-11-20 11:45:35 +0100 | [diff] [blame] | 131 | const char *uuid_guid_get_str(const unsigned char *guid_bin) |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 132 | { |
| 133 | int i; |
| 134 | |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 135 | for (i = 0; i < ARRAY_SIZE(list_guid); i++) { |
| 136 | if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) { |
Rasmus Villemoes | 7b70bdc | 2020-11-20 11:45:35 +0100 | [diff] [blame] | 137 | return list_guid[i].string; |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 138 | } |
| 139 | } |
Rasmus Villemoes | 7b70bdc | 2020-11-20 11:45:35 +0100 | [diff] [blame] | 140 | return NULL; |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 141 | } |
| 142 | #endif |
| 143 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 144 | /* |
| 145 | * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data. |
| 146 | * |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 147 | * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 148 | * @param uuid_bin - pointer to allocated array for big endian output [16B] |
| 149 | * @str_format - UUID string format: 0 - UUID; 1 - GUID |
| 150 | */ |
Simon Glass | 4acd4f0 | 2020-04-08 08:32:58 -0600 | [diff] [blame] | 151 | int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin, |
| 152 | int str_format) |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 153 | { |
| 154 | uint16_t tmp16; |
| 155 | uint32_t tmp32; |
| 156 | uint64_t tmp64; |
| 157 | |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 158 | if (!uuid_str_valid(uuid_str)) { |
| 159 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 160 | if (!uuid_guid_get_bin(uuid_str, uuid_bin)) |
| 161 | return 0; |
| 162 | #endif |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 163 | return -EINVAL; |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 164 | } |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 165 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 166 | if (str_format == UUID_STR_FORMAT_STD) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 167 | tmp32 = cpu_to_be32(hextoul(uuid_str, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 168 | memcpy(uuid_bin, &tmp32, 4); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 169 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 170 | tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 171 | memcpy(uuid_bin + 4, &tmp16, 2); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 172 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 173 | tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 174 | memcpy(uuid_bin + 6, &tmp16, 2); |
| 175 | } else { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 176 | tmp32 = cpu_to_le32(hextoul(uuid_str, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 177 | memcpy(uuid_bin, &tmp32, 4); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 178 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 179 | tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 180 | memcpy(uuid_bin + 4, &tmp16, 2); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 181 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 182 | tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 183 | memcpy(uuid_bin + 6, &tmp16, 2); |
| 184 | } |
| 185 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 186 | tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL)); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 187 | memcpy(uuid_bin + 8, &tmp16, 2); |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 188 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 189 | tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16)); |
| 190 | memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6); |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 195 | /* |
| 196 | * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID. |
| 197 | * |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 198 | * @param uuid_bin: pointer to binary data of UUID (big endian) [16B] |
| 199 | * @param uuid_str: pointer to allocated array for output string [37B] |
| 200 | * @str_format: bit 0: 0 - UUID; 1 - GUID |
| 201 | * bit 1: 0 - lower case; 2 - upper case |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 202 | */ |
Simon Glass | 4acd4f0 | 2020-04-08 08:32:58 -0600 | [diff] [blame] | 203 | void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str, |
| 204 | int str_format) |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 205 | { |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 206 | const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8, |
| 207 | 9, 10, 11, 12, 13, 14, 15}; |
| 208 | const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8, |
| 209 | 9, 10, 11, 12, 13, 14, 15}; |
| 210 | const u8 *char_order; |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 211 | const char *format; |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 212 | int i; |
| 213 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 214 | /* |
| 215 | * UUID and GUID bin data - always in big endian: |
| 216 | * 4B-2B-2B-2B-6B |
| 217 | * be be be be be |
| 218 | */ |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 219 | if (str_format & UUID_STR_FORMAT_GUID) |
| 220 | char_order = guid_char_order; |
| 221 | else |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 222 | char_order = uuid_char_order; |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 223 | if (str_format & UUID_STR_UPPER_CASE) |
| 224 | format = "%02X"; |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 225 | else |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 226 | format = "%02x"; |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 227 | |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 228 | for (i = 0; i < 16; i++) { |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 229 | sprintf(uuid_str, format, uuid_bin[char_order[i]]); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 230 | uuid_str += 2; |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 231 | switch (i) { |
| 232 | case 3: |
| 233 | case 5: |
| 234 | case 7: |
| 235 | case 9: |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 236 | *uuid_str++ = '-'; |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 237 | break; |
| 238 | } |
| 239 | } |
Jason Hobbs | 6e39cce | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 240 | } |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | * gen_rand_uuid() - this function generates a random binary UUID version 4. |
| 244 | * In this version all fields beside 4 bits of version and |
| 245 | * 2 bits of variant are randomly generated. |
| 246 | * |
| 247 | * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian. |
| 248 | */ |
Przemyslaw Marczak | 2eb40ee | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 249 | #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID) |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 250 | void gen_rand_uuid(unsigned char *uuid_bin) |
| 251 | { |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 252 | u32 ptr[4]; |
| 253 | struct uuid *uuid = (struct uuid *)ptr; |
Matthias Brugger | d03560b | 2020-12-18 10:28:03 +0100 | [diff] [blame] | 254 | int i, ret; |
| 255 | struct udevice *devp; |
| 256 | u32 randv = 0; |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 257 | |
Matthias Brugger | d03560b | 2020-12-18 10:28:03 +0100 | [diff] [blame] | 258 | if (IS_ENABLED(CONFIG_DM_RNG)) { |
| 259 | ret = uclass_get_device(UCLASS_RNG, 0, &devp); |
| 260 | if (ret) { |
| 261 | ret = dm_rng_read(devp, &randv, sizeof(randv)); |
| 262 | if (ret < 0) |
| 263 | randv = 0; |
| 264 | } |
| 265 | } |
| 266 | if (randv) |
| 267 | srand(randv); |
| 268 | else |
| 269 | srand(get_ticks() + rand()); |
Eugeniu Rosca | 9d74ba6 | 2019-05-02 14:27:06 +0200 | [diff] [blame] | 270 | |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 271 | /* Set all fields randomly */ |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 272 | for (i = 0; i < 4; i++) |
| 273 | ptr[i] = rand(); |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 274 | |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 275 | clrsetbits_be16(&uuid->time_hi_and_version, |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 276 | UUID_VERSION_MASK, |
| 277 | UUID_VERSION << UUID_VERSION_SHIFT); |
| 278 | |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 279 | clrsetbits_8(&uuid->clock_seq_hi_and_reserved, |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 280 | UUID_VARIANT_MASK, |
| 281 | UUID_VARIANT << UUID_VARIANT_SHIFT); |
| 282 | |
Heinrich Schuchardt | 1c126e2 | 2019-07-14 23:31:50 +0200 | [diff] [blame] | 283 | memcpy(uuid_bin, uuid, 16); |
Przemyslaw Marczak | b428514 | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* |
| 287 | * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string |
| 288 | * formats UUID or GUID. |
| 289 | * |
| 290 | * @param uuid_str - pointer to allocated array [37B]. |
| 291 | * @param - uuid output type: UUID - 0, GUID - 1 |
| 292 | */ |
| 293 | void gen_rand_uuid_str(char *uuid_str, int str_format) |
| 294 | { |
| 295 | unsigned char uuid_bin[UUID_BIN_LEN]; |
| 296 | |
| 297 | /* Generate UUID (big endian) */ |
| 298 | gen_rand_uuid(uuid_bin); |
| 299 | |
| 300 | /* Convert UUID bin to UUID or GUID formated STRING */ |
| 301 | uuid_bin_to_str(uuid_bin, uuid_str, str_format); |
| 302 | } |
Przemyslaw Marczak | 2eb40ee | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 303 | |
Marek Vasut | 51d85f6 | 2019-01-07 21:23:38 +0100 | [diff] [blame] | 304 | #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID) |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 305 | int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Przemyslaw Marczak | 2eb40ee | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 306 | { |
| 307 | char uuid[UUID_STR_LEN + 1]; |
| 308 | int str_format; |
| 309 | |
| 310 | if (!strcmp(argv[0], "uuid")) |
| 311 | str_format = UUID_STR_FORMAT_STD; |
| 312 | else |
| 313 | str_format = UUID_STR_FORMAT_GUID; |
| 314 | |
| 315 | if (argc > 2) |
| 316 | return CMD_RET_USAGE; |
| 317 | |
| 318 | gen_rand_uuid_str(uuid, str_format); |
| 319 | |
| 320 | if (argc == 1) |
| 321 | printf("%s\n", uuid); |
| 322 | else |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 323 | env_set(argv[1], uuid); |
Przemyslaw Marczak | 2eb40ee | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 324 | |
| 325 | return CMD_RET_SUCCESS; |
| 326 | } |
| 327 | |
| 328 | U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid, |
| 329 | "UUID - generate random Universally Unique Identifier", |
| 330 | "[<varname>]\n" |
| 331 | "Argument:\n" |
| 332 | "varname: for set result in a environment variable\n" |
| 333 | "e.g. uuid uuid_env" |
| 334 | ); |
| 335 | |
| 336 | U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid, |
| 337 | "GUID - generate Globally Unique Identifier based on random UUID", |
| 338 | "[<varname>]\n" |
| 339 | "Argument:\n" |
| 340 | "varname: for set result in a environment variable\n" |
| 341 | "e.g. guid guid_env" |
| 342 | ); |
Przemyslaw Marczak | 5ec2ed2 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 343 | #endif /* CONFIG_CMD_UUID */ |
| 344 | #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */ |