uuid.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2011 Calxeda, Inc.
  4. */
  5. #include <common.h>
  6. #include <env.h>
  7. #include <time.h>
  8. #include <linux/ctype.h>
  9. #include <errno.h>
  10. #include <common.h>
  11. #include <asm/io.h>
  12. #include <part_efi.h>
  13. #include <malloc.h>
  14. /*
  15. * UUID - Universally Unique IDentifier - 128 bits unique number.
  16. * There are 5 versions and one variant of UUID defined by RFC4122
  17. * 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
  33. *
  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.
  47. *
  48. * UUID is also used as GUID (Globally Unique Identifier) with the same binary
  49. * format but it differs in string format like below.
  50. *
  51. * GUID:
  52. * 0 9 14 19 24
  53. * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  54. * le le le be be
  55. *
  56. * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
  57. */
  58. int uuid_str_valid(const char *uuid)
  59. {
  60. int i, valid;
  61. if (uuid == NULL)
  62. return 0;
  63. for (i = 0, valid = 1; uuid[i] && valid; i++) {
  64. switch (i) {
  65. case 8: case 13: case 18: case 23:
  66. valid = (uuid[i] == '-');
  67. break;
  68. default:
  69. valid = isxdigit(uuid[i]);
  70. break;
  71. }
  72. }
  73. if (i != UUID_STR_LEN || !valid)
  74. return 0;
  75. return 1;
  76. }
  77. #ifdef CONFIG_PARTITION_TYPE_GUID
  78. static const struct {
  79. const char *string;
  80. efi_guid_t guid;
  81. } list_guid[] = {
  82. {"system", PARTITION_SYSTEM_GUID},
  83. {"mbr", LEGACY_MBR_PARTITION_GUID},
  84. {"msft", PARTITION_MSFT_RESERVED_GUID},
  85. {"data", PARTITION_BASIC_DATA_GUID},
  86. {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
  87. {"raid", PARTITION_LINUX_RAID_GUID},
  88. {"swap", PARTITION_LINUX_SWAP_GUID},
  89. {"lvm", PARTITION_LINUX_LVM_GUID}
  90. };
  91. /*
  92. * uuid_guid_get_bin() - this function get GUID bin for string
  93. *
  94. * @param guid_str - pointer to partition type string
  95. * @param guid_bin - pointer to allocated array for big endian output [16B]
  96. */
  97. int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
  98. {
  99. int i;
  100. for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
  101. if (!strcmp(list_guid[i].string, guid_str)) {
  102. memcpy(guid_bin, &list_guid[i].guid, 16);
  103. return 0;
  104. }
  105. }
  106. return -ENODEV;
  107. }
  108. /*
  109. * uuid_guid_get_str() - this function get string for GUID.
  110. *
  111. * @param guid_bin - pointer to string with partition type guid [16B]
  112. * @param guid_str - pointer to allocated partition type string [7B]
  113. */
  114. int uuid_guid_get_str(unsigned char *guid_bin, char *guid_str)
  115. {
  116. int i;
  117. *guid_str = 0;
  118. for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
  119. if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
  120. strcpy(guid_str, list_guid[i].string);
  121. return 0;
  122. }
  123. }
  124. return -ENODEV;
  125. }
  126. #endif
  127. /*
  128. * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
  129. *
  130. * @param uuid_str - pointer to UUID or GUID string [37B] or GUID shorcut
  131. * @param uuid_bin - pointer to allocated array for big endian output [16B]
  132. * @str_format - UUID string format: 0 - UUID; 1 - GUID
  133. */
  134. int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format)
  135. {
  136. uint16_t tmp16;
  137. uint32_t tmp32;
  138. uint64_t tmp64;
  139. if (!uuid_str_valid(uuid_str)) {
  140. #ifdef CONFIG_PARTITION_TYPE_GUID
  141. if (!uuid_guid_get_bin(uuid_str, uuid_bin))
  142. return 0;
  143. #endif
  144. return -EINVAL;
  145. }
  146. if (str_format == UUID_STR_FORMAT_STD) {
  147. tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
  148. memcpy(uuid_bin, &tmp32, 4);
  149. tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
  150. memcpy(uuid_bin + 4, &tmp16, 2);
  151. tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
  152. memcpy(uuid_bin + 6, &tmp16, 2);
  153. } else {
  154. tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
  155. memcpy(uuid_bin, &tmp32, 4);
  156. tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
  157. memcpy(uuid_bin + 4, &tmp16, 2);
  158. tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
  159. memcpy(uuid_bin + 6, &tmp16, 2);
  160. }
  161. tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
  162. memcpy(uuid_bin + 8, &tmp16, 2);
  163. tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
  164. memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
  165. return 0;
  166. }
  167. /*
  168. * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
  169. *
  170. * @param uuid_bin: pointer to binary data of UUID (big endian) [16B]
  171. * @param uuid_str: pointer to allocated array for output string [37B]
  172. * @str_format: bit 0: 0 - UUID; 1 - GUID
  173. * bit 1: 0 - lower case; 2 - upper case
  174. */
  175. void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format)
  176. {
  177. const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
  178. 9, 10, 11, 12, 13, 14, 15};
  179. const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
  180. 9, 10, 11, 12, 13, 14, 15};
  181. const u8 *char_order;
  182. const char *format;
  183. int i;
  184. /*
  185. * UUID and GUID bin data - always in big endian:
  186. * 4B-2B-2B-2B-6B
  187. * be be be be be
  188. */
  189. if (str_format & UUID_STR_FORMAT_GUID)
  190. char_order = guid_char_order;
  191. else
  192. char_order = uuid_char_order;
  193. if (str_format & UUID_STR_UPPER_CASE)
  194. format = "%02X";
  195. else
  196. format = "%02x";
  197. for (i = 0; i < 16; i++) {
  198. sprintf(uuid_str, format, uuid_bin[char_order[i]]);
  199. uuid_str += 2;
  200. switch (i) {
  201. case 3:
  202. case 5:
  203. case 7:
  204. case 9:
  205. *uuid_str++ = '-';
  206. break;
  207. }
  208. }
  209. }
  210. /*
  211. * gen_rand_uuid() - this function generates a random binary UUID version 4.
  212. * In this version all fields beside 4 bits of version and
  213. * 2 bits of variant are randomly generated.
  214. *
  215. * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
  216. */
  217. #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
  218. void gen_rand_uuid(unsigned char *uuid_bin)
  219. {
  220. u32 ptr[4];
  221. struct uuid *uuid = (struct uuid *)ptr;
  222. int i;
  223. srand(get_ticks() + rand());
  224. /* Set all fields randomly */
  225. for (i = 0; i < 4; i++)
  226. ptr[i] = rand();
  227. clrsetbits_be16(&uuid->time_hi_and_version,
  228. UUID_VERSION_MASK,
  229. UUID_VERSION << UUID_VERSION_SHIFT);
  230. clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
  231. UUID_VARIANT_MASK,
  232. UUID_VARIANT << UUID_VARIANT_SHIFT);
  233. memcpy(uuid_bin, uuid, 16);
  234. }
  235. /*
  236. * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
  237. * formats UUID or GUID.
  238. *
  239. * @param uuid_str - pointer to allocated array [37B].
  240. * @param - uuid output type: UUID - 0, GUID - 1
  241. */
  242. void gen_rand_uuid_str(char *uuid_str, int str_format)
  243. {
  244. unsigned char uuid_bin[UUID_BIN_LEN];
  245. /* Generate UUID (big endian) */
  246. gen_rand_uuid(uuid_bin);
  247. /* Convert UUID bin to UUID or GUID formated STRING */
  248. uuid_bin_to_str(uuid_bin, uuid_str, str_format);
  249. }
  250. #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
  251. int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  252. {
  253. char uuid[UUID_STR_LEN + 1];
  254. int str_format;
  255. if (!strcmp(argv[0], "uuid"))
  256. str_format = UUID_STR_FORMAT_STD;
  257. else
  258. str_format = UUID_STR_FORMAT_GUID;
  259. if (argc > 2)
  260. return CMD_RET_USAGE;
  261. gen_rand_uuid_str(uuid, str_format);
  262. if (argc == 1)
  263. printf("%s\n", uuid);
  264. else
  265. env_set(argv[1], uuid);
  266. return CMD_RET_SUCCESS;
  267. }
  268. U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
  269. "UUID - generate random Universally Unique Identifier",
  270. "[<varname>]\n"
  271. "Argument:\n"
  272. "varname: for set result in a environment variable\n"
  273. "e.g. uuid uuid_env"
  274. );
  275. U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
  276. "GUID - generate Globally Unique Identifier based on random UUID",
  277. "[<varname>]\n"
  278. "Argument:\n"
  279. "varname: for set result in a environment variable\n"
  280. "e.g. guid guid_env"
  281. );
  282. #endif /* CONFIG_CMD_UUID */
  283. #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */