uuid.c 5.7 KB

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