uuid.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright 2011 Calxeda, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <linux/ctype.h>
  8. #include <errno.h>
  9. #include <common.h>
  10. #include <asm/io.h>
  11. #include <part_efi.h>
  12. #include <malloc.h>
  13. /*
  14. * UUID - Universally Unique IDentifier - 128 bits unique number.
  15. * There are 5 versions and one variant of UUID defined by RFC4122
  16. * specification. A UUID contains a set of fields. The set varies
  17. * depending on the version of the UUID, as shown below:
  18. * - time, MAC address(v1),
  19. * - user ID(v2),
  20. * - MD5 of name or URL(v3),
  21. * - random data(v4),
  22. * - SHA-1 of name or URL(v5),
  23. *
  24. * Layout of UUID:
  25. * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
  26. * version - 4 bit (bit 4 through 7 of the time_hi_and_version)
  27. * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
  28. * variant: - bit 6 and 7 of clock_seq_hi_and_reserved
  29. * node - 48 bit
  30. *
  31. * source: https://www.ietf.org/rfc/rfc4122.txt
  32. *
  33. * UUID binary format (16 bytes):
  34. *
  35. * 4B-2B-2B-2B-6B (big endian - network byte order)
  36. *
  37. * UUID string is 36 length of characters (36 bytes):
  38. *
  39. * 0 9 14 19 24
  40. * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  41. * be be be be be
  42. *
  43. * where x is a hexadecimal character. Fields are separated by '-'s.
  44. * When converting to a binary UUID, le means the field should be converted
  45. * to little endian and be means it should be converted to big endian.
  46. *
  47. * UUID is also used as GUID (Globally Unique Identifier) with the same binary
  48. * format but it differs in string format like below.
  49. *
  50. * GUID:
  51. * 0 9 14 19 24
  52. * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  53. * le le le be be
  54. *
  55. * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
  56. */
  57. int uuid_str_valid(const char *uuid)
  58. {
  59. int i, valid;
  60. if (uuid == NULL)
  61. return 0;
  62. for (i = 0, valid = 1; uuid[i] && valid; i++) {
  63. switch (i) {
  64. case 8: case 13: case 18: case 23:
  65. valid = (uuid[i] == '-');
  66. break;
  67. default:
  68. valid = isxdigit(uuid[i]);
  69. break;
  70. }
  71. }
  72. if (i != UUID_STR_LEN || !valid)
  73. return 0;
  74. return 1;
  75. }
  76. /*
  77. * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
  78. *
  79. * @param uuid_str - pointer to UUID or GUID string [37B]
  80. * @param uuid_bin - pointer to allocated array for big endian output [16B]
  81. * @str_format - UUID string format: 0 - UUID; 1 - GUID
  82. */
  83. int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format)
  84. {
  85. uint16_t tmp16;
  86. uint32_t tmp32;
  87. uint64_t tmp64;
  88. if (!uuid_str_valid(uuid_str))
  89. return -EINVAL;
  90. if (str_format == UUID_STR_FORMAT_STD) {
  91. tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
  92. memcpy(uuid_bin, &tmp32, 4);
  93. tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
  94. memcpy(uuid_bin + 4, &tmp16, 2);
  95. tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
  96. memcpy(uuid_bin + 6, &tmp16, 2);
  97. } else {
  98. tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
  99. memcpy(uuid_bin, &tmp32, 4);
  100. tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
  101. memcpy(uuid_bin + 4, &tmp16, 2);
  102. tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
  103. memcpy(uuid_bin + 6, &tmp16, 2);
  104. }
  105. tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
  106. memcpy(uuid_bin + 8, &tmp16, 2);
  107. tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
  108. memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
  109. return 0;
  110. }
  111. /*
  112. * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
  113. *
  114. * @param uuid_bin - pointer to binary data of UUID (big endian) [16B]
  115. * @param uuid_str - pointer to allocated array for output string [37B]
  116. * @str_format - UUID string format: 0 - UUID; 1 - GUID
  117. */
  118. void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format)
  119. {
  120. const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
  121. 9, 10, 11, 12, 13, 14, 15};
  122. const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
  123. 9, 10, 11, 12, 13, 14, 15};
  124. const u8 *char_order;
  125. int i;
  126. /*
  127. * UUID and GUID bin data - always in big endian:
  128. * 4B-2B-2B-2B-6B
  129. * be be be be be
  130. */
  131. if (str_format == UUID_STR_FORMAT_STD)
  132. char_order = uuid_char_order;
  133. else
  134. char_order = guid_char_order;
  135. for (i = 0; i < 16; i++) {
  136. sprintf(uuid_str, "%02x", uuid_bin[char_order[i]]);
  137. uuid_str += 2;
  138. switch (i) {
  139. case 3:
  140. case 5:
  141. case 7:
  142. case 9:
  143. *uuid_str++ = '-';
  144. break;
  145. }
  146. }
  147. }
  148. /*
  149. * gen_rand_uuid() - this function generates a random binary UUID version 4.
  150. * In this version all fields beside 4 bits of version and
  151. * 2 bits of variant are randomly generated.
  152. *
  153. * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
  154. */
  155. #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
  156. void gen_rand_uuid(unsigned char *uuid_bin)
  157. {
  158. struct uuid uuid;
  159. unsigned int *ptr = (unsigned int *)&uuid;
  160. int i;
  161. /* Set all fields randomly */
  162. for (i = 0; i < sizeof(struct uuid) / sizeof(*ptr); i++)
  163. *(ptr + i) = cpu_to_be32(rand());
  164. clrsetbits_be16(&uuid.time_hi_and_version,
  165. UUID_VERSION_MASK,
  166. UUID_VERSION << UUID_VERSION_SHIFT);
  167. clrsetbits_8(&uuid.clock_seq_hi_and_reserved,
  168. UUID_VARIANT_MASK,
  169. UUID_VARIANT << UUID_VARIANT_SHIFT);
  170. memcpy(uuid_bin, &uuid, sizeof(struct uuid));
  171. }
  172. /*
  173. * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
  174. * formats UUID or GUID.
  175. *
  176. * @param uuid_str - pointer to allocated array [37B].
  177. * @param - uuid output type: UUID - 0, GUID - 1
  178. */
  179. void gen_rand_uuid_str(char *uuid_str, int str_format)
  180. {
  181. unsigned char uuid_bin[UUID_BIN_LEN];
  182. /* Generate UUID (big endian) */
  183. gen_rand_uuid(uuid_bin);
  184. /* Convert UUID bin to UUID or GUID formated STRING */
  185. uuid_bin_to_str(uuid_bin, uuid_str, str_format);
  186. }
  187. #ifdef CONFIG_CMD_UUID
  188. int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  189. {
  190. char uuid[UUID_STR_LEN + 1];
  191. int str_format;
  192. if (!strcmp(argv[0], "uuid"))
  193. str_format = UUID_STR_FORMAT_STD;
  194. else
  195. str_format = UUID_STR_FORMAT_GUID;
  196. if (argc > 2)
  197. return CMD_RET_USAGE;
  198. gen_rand_uuid_str(uuid, str_format);
  199. if (argc == 1)
  200. printf("%s\n", uuid);
  201. else
  202. setenv(argv[1], uuid);
  203. return CMD_RET_SUCCESS;
  204. }
  205. U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
  206. "UUID - generate random Universally Unique Identifier",
  207. "[<varname>]\n"
  208. "Argument:\n"
  209. "varname: for set result in a environment variable\n"
  210. "e.g. uuid uuid_env"
  211. );
  212. U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
  213. "GUID - generate Globally Unique Identifier based on random UUID",
  214. "[<varname>]\n"
  215. "Argument:\n"
  216. "varname: for set result in a environment variable\n"
  217. "e.g. guid guid_env"
  218. );
  219. #endif /* CONFIG_CMD_UUID */
  220. #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */