uuid.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. Depends on version uuid number base on a time,
  16. * host name, MAC address or random data.
  17. *
  18. * UUID binary format (16 bytes):
  19. *
  20. * 4B-2B-2B-2B-6B (big endian - network byte order)
  21. *
  22. * UUID string is 36 length of characters (36 bytes):
  23. *
  24. * 0 9 14 19 24
  25. * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  26. * be be be be be
  27. *
  28. * where x is a hexadecimal character. Fields are separated by '-'s.
  29. * When converting to a binary UUID, le means the field should be converted
  30. * to little endian and be means it should be converted to big endian.
  31. *
  32. * UUID is also used as GUID (Globally Unique Identifier) with the same binary
  33. * format but it differs in string format like below.
  34. *
  35. * GUID:
  36. * 0 9 14 19 24
  37. * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  38. * le le le be be
  39. *
  40. * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
  41. */
  42. int uuid_str_valid(const char *uuid)
  43. {
  44. int i, valid;
  45. if (uuid == NULL)
  46. return 0;
  47. for (i = 0, valid = 1; uuid[i] && valid; i++) {
  48. switch (i) {
  49. case 8: case 13: case 18: case 23:
  50. valid = (uuid[i] == '-');
  51. break;
  52. default:
  53. valid = isxdigit(uuid[i]);
  54. break;
  55. }
  56. }
  57. if (i != UUID_STR_LEN || !valid)
  58. return 0;
  59. return 1;
  60. }
  61. /*
  62. * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
  63. *
  64. * @param uuid_str - pointer to UUID or GUID string [37B]
  65. * @param uuid_bin - pointer to allocated array for big endian output [16B]
  66. * @str_format - UUID string format: 0 - UUID; 1 - GUID
  67. */
  68. int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format)
  69. {
  70. uint16_t tmp16;
  71. uint32_t tmp32;
  72. uint64_t tmp64;
  73. if (!uuid_str_valid(uuid_str))
  74. return -EINVAL;
  75. if (str_format == UUID_STR_FORMAT_STD) {
  76. tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
  77. memcpy(uuid_bin, &tmp32, 4);
  78. tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
  79. memcpy(uuid_bin + 4, &tmp16, 2);
  80. tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
  81. memcpy(uuid_bin + 6, &tmp16, 2);
  82. } else {
  83. tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
  84. memcpy(uuid_bin, &tmp32, 4);
  85. tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
  86. memcpy(uuid_bin + 4, &tmp16, 2);
  87. tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
  88. memcpy(uuid_bin + 6, &tmp16, 2);
  89. }
  90. tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
  91. memcpy(uuid_bin + 8, &tmp16, 2);
  92. tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
  93. memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
  94. return 0;
  95. }
  96. /*
  97. * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
  98. *
  99. * @param uuid_bin - pointer to binary data of UUID (big endian) [16B]
  100. * @param uuid_str - pointer to allocated array for output string [37B]
  101. * @str_format - UUID string format: 0 - UUID; 1 - GUID
  102. */
  103. void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format)
  104. {
  105. const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
  106. 9, 10, 11, 12, 13, 14, 15};
  107. const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
  108. 9, 10, 11, 12, 13, 14, 15};
  109. const u8 *char_order;
  110. int i;
  111. /*
  112. * UUID and GUID bin data - always in big endian:
  113. * 4B-2B-2B-2B-6B
  114. * be be be be be
  115. */
  116. if (str_format == UUID_STR_FORMAT_STD)
  117. char_order = uuid_char_order;
  118. else
  119. char_order = guid_char_order;
  120. for (i = 0; i < 16; i++) {
  121. sprintf(uuid_str, "%02x", uuid_bin[char_order[i]]);
  122. uuid_str += 2;
  123. switch (i) {
  124. case 3:
  125. case 5:
  126. case 7:
  127. case 9:
  128. *uuid_str++ = '-';
  129. break;
  130. }
  131. }
  132. }