uuid.c 8.3 KB

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