Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Calxeda, Inc. |
| 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Przemyslaw Marczak | 89c8230 | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 7 | #include <common.h> |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 8 | #include <linux/ctype.h> |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <common.h> |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <part_efi.h> |
| 13 | #include <malloc.h> |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 14 | |
| 15 | /* |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 16 | * UUID - Universally Unique IDentifier - 128 bits unique number. |
| 17 | * There are 5 versions and one variant of UUID defined by RFC4122 |
Przemyslaw Marczak | 4e4815f | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 18 | * 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 |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 34 | * |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 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): |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 40 | * |
| 41 | * 0 9 14 19 24 |
| 42 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 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. |
| 48 | * |
| 49 | * UUID is also used as GUID (Globally Unique Identifier) with the same binary |
| 50 | * format but it differs in string format like below. |
| 51 | * |
| 52 | * GUID: |
| 53 | * 0 9 14 19 24 |
| 54 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 55 | * le le le be be |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 56 | * |
| 57 | * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id. |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 58 | */ |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 59 | int 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 Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 77 | if (i != UUID_STR_LEN || !valid) |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 78 | return 0; |
| 79 | |
| 80 | return 1; |
| 81 | } |
| 82 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 83 | /* |
| 84 | * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data. |
| 85 | * |
| 86 | * @param uuid_str - pointer to UUID or GUID string [37B] |
| 87 | * @param uuid_bin - pointer to allocated array for big endian output [16B] |
| 88 | * @str_format - UUID string format: 0 - UUID; 1 - GUID |
| 89 | */ |
| 90 | int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format) |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 91 | { |
| 92 | uint16_t tmp16; |
| 93 | uint32_t tmp32; |
| 94 | uint64_t tmp64; |
| 95 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 96 | if (!uuid_str_valid(uuid_str)) |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 97 | return -EINVAL; |
| 98 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 99 | if (str_format == UUID_STR_FORMAT_STD) { |
| 100 | tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16)); |
| 101 | memcpy(uuid_bin, &tmp32, 4); |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 102 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 103 | tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16)); |
| 104 | memcpy(uuid_bin + 4, &tmp16, 2); |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 105 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 106 | tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16)); |
| 107 | memcpy(uuid_bin + 6, &tmp16, 2); |
| 108 | } else { |
| 109 | tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16)); |
| 110 | memcpy(uuid_bin, &tmp32, 4); |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 111 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 112 | tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16)); |
| 113 | memcpy(uuid_bin + 4, &tmp16, 2); |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 114 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 115 | tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16)); |
| 116 | memcpy(uuid_bin + 6, &tmp16, 2); |
| 117 | } |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 118 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 119 | tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16)); |
| 120 | memcpy(uuid_bin + 8, &tmp16, 2); |
| 121 | |
| 122 | tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16)); |
| 123 | memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6); |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 128 | /* |
| 129 | * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID. |
| 130 | * |
| 131 | * @param uuid_bin - pointer to binary data of UUID (big endian) [16B] |
| 132 | * @param uuid_str - pointer to allocated array for output string [37B] |
| 133 | * @str_format - UUID string format: 0 - UUID; 1 - GUID |
| 134 | */ |
| 135 | void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format) |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 136 | { |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 137 | const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8, |
| 138 | 9, 10, 11, 12, 13, 14, 15}; |
| 139 | const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8, |
| 140 | 9, 10, 11, 12, 13, 14, 15}; |
| 141 | const u8 *char_order; |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 142 | int i; |
| 143 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 144 | /* |
| 145 | * UUID and GUID bin data - always in big endian: |
| 146 | * 4B-2B-2B-2B-6B |
| 147 | * be be be be be |
| 148 | */ |
| 149 | if (str_format == UUID_STR_FORMAT_STD) |
| 150 | char_order = uuid_char_order; |
| 151 | else |
| 152 | char_order = guid_char_order; |
| 153 | |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 154 | for (i = 0; i < 16; i++) { |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 155 | sprintf(uuid_str, "%02x", uuid_bin[char_order[i]]); |
| 156 | uuid_str += 2; |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 157 | switch (i) { |
| 158 | case 3: |
| 159 | case 5: |
| 160 | case 7: |
| 161 | case 9: |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 162 | *uuid_str++ = '-'; |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 163 | break; |
| 164 | } |
| 165 | } |
Jason Hobbs | e11938e | 2011-08-23 11:06:56 +0000 | [diff] [blame] | 166 | } |
Przemyslaw Marczak | 4e4815f | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * gen_rand_uuid() - this function generates a random binary UUID version 4. |
| 170 | * In this version all fields beside 4 bits of version and |
| 171 | * 2 bits of variant are randomly generated. |
| 172 | * |
| 173 | * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian. |
| 174 | */ |
Przemyslaw Marczak | 89c8230 | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 175 | #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID) |
Przemyslaw Marczak | 4e4815f | 2014-04-02 10:20:04 +0200 | [diff] [blame] | 176 | void gen_rand_uuid(unsigned char *uuid_bin) |
| 177 | { |
| 178 | struct uuid uuid; |
| 179 | unsigned int *ptr = (unsigned int *)&uuid; |
| 180 | int i; |
| 181 | |
| 182 | /* Set all fields randomly */ |
| 183 | for (i = 0; i < sizeof(struct uuid) / sizeof(*ptr); i++) |
| 184 | *(ptr + i) = cpu_to_be32(rand()); |
| 185 | |
| 186 | clrsetbits_be16(&uuid.time_hi_and_version, |
| 187 | UUID_VERSION_MASK, |
| 188 | UUID_VERSION << UUID_VERSION_SHIFT); |
| 189 | |
| 190 | clrsetbits_8(&uuid.clock_seq_hi_and_reserved, |
| 191 | UUID_VARIANT_MASK, |
| 192 | UUID_VARIANT << UUID_VARIANT_SHIFT); |
| 193 | |
| 194 | memcpy(uuid_bin, &uuid, sizeof(struct uuid)); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string |
| 199 | * formats UUID or GUID. |
| 200 | * |
| 201 | * @param uuid_str - pointer to allocated array [37B]. |
| 202 | * @param - uuid output type: UUID - 0, GUID - 1 |
| 203 | */ |
| 204 | void gen_rand_uuid_str(char *uuid_str, int str_format) |
| 205 | { |
| 206 | unsigned char uuid_bin[UUID_BIN_LEN]; |
| 207 | |
| 208 | /* Generate UUID (big endian) */ |
| 209 | gen_rand_uuid(uuid_bin); |
| 210 | |
| 211 | /* Convert UUID bin to UUID or GUID formated STRING */ |
| 212 | uuid_bin_to_str(uuid_bin, uuid_str, str_format); |
| 213 | } |
Przemyslaw Marczak | 89c8230 | 2014-04-02 10:20:05 +0200 | [diff] [blame] | 214 | |
| 215 | #ifdef CONFIG_CMD_UUID |
| 216 | int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 217 | { |
| 218 | char uuid[UUID_STR_LEN + 1]; |
| 219 | int str_format; |
| 220 | |
| 221 | if (!strcmp(argv[0], "uuid")) |
| 222 | str_format = UUID_STR_FORMAT_STD; |
| 223 | else |
| 224 | str_format = UUID_STR_FORMAT_GUID; |
| 225 | |
| 226 | if (argc > 2) |
| 227 | return CMD_RET_USAGE; |
| 228 | |
| 229 | gen_rand_uuid_str(uuid, str_format); |
| 230 | |
| 231 | if (argc == 1) |
| 232 | printf("%s\n", uuid); |
| 233 | else |
| 234 | setenv(argv[1], uuid); |
| 235 | |
| 236 | return CMD_RET_SUCCESS; |
| 237 | } |
| 238 | |
| 239 | U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid, |
| 240 | "UUID - generate random Universally Unique Identifier", |
| 241 | "[<varname>]\n" |
| 242 | "Argument:\n" |
| 243 | "varname: for set result in a environment variable\n" |
| 244 | "e.g. uuid uuid_env" |
| 245 | ); |
| 246 | |
| 247 | U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid, |
| 248 | "GUID - generate Globally Unique Identifier based on random UUID", |
| 249 | "[<varname>]\n" |
| 250 | "Argument:\n" |
| 251 | "varname: for set result in a environment variable\n" |
| 252 | "e.g. guid guid_env" |
| 253 | ); |
Przemyslaw Marczak | 3920638 | 2014-04-02 10:20:06 +0200 | [diff] [blame] | 254 | #endif /* CONFIG_CMD_UUID */ |
| 255 | #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */ |