uuid.c 8.7 KB

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