nguid.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * QEMU NVMe NGUID functions
  3. *
  4. * Copyright 2024 Google LLC
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. */
  16. #include "qemu/osdep.h"
  17. #include "qapi/visitor.h"
  18. #include "qemu/ctype.h"
  19. #include "nvme.h"
  20. #define NGUID_SEPARATOR '-'
  21. #define NGUID_VALUE_AUTO "auto"
  22. #define NGUID_FMT \
  23. "%02hhx%02hhx%02hhx%02hhx" \
  24. "%02hhx%02hhx%02hhx%02hhx" \
  25. "%02hhx%02hhx%02hhx%02hhx" \
  26. "%02hhx%02hhx%02hhx%02hhx"
  27. #define NGUID_STR_LEN (2 * NGUID_LEN + 1)
  28. bool nvme_nguid_is_null(const NvmeNGUID *nguid)
  29. {
  30. static NvmeNGUID null_nguid;
  31. return memcmp(nguid, &null_nguid, sizeof(NvmeNGUID)) == 0;
  32. }
  33. static void nvme_nguid_generate(NvmeNGUID *out)
  34. {
  35. int i;
  36. uint32_t x;
  37. QEMU_BUILD_BUG_ON((NGUID_LEN % sizeof(x)) != 0);
  38. for (i = 0; i < NGUID_LEN; i += sizeof(x)) {
  39. x = g_random_int();
  40. memcpy(&out->data[i], &x, sizeof(x));
  41. }
  42. }
  43. /*
  44. * The Linux Kernel typically prints the NGUID of an NVMe namespace using the
  45. * same format as the UUID. For instance:
  46. *
  47. * $ cat /sys/class/block/nvme0n1/nguid
  48. * e9accd3b-8390-4e13-167c-f0593437f57d
  49. *
  50. * When there is no UUID but there is NGUID the Kernel will print the NGUID as
  51. * wwid and it won't use the UUID format:
  52. *
  53. * $ cat /sys/class/block/nvme0n1/wwid
  54. * eui.e9accd3b83904e13167cf0593437f57d
  55. *
  56. * The NGUID has different fields compared to the UUID, so the grouping used in
  57. * the UUID format has no relation with the 3 fields of the NGUID.
  58. *
  59. * This implementation won't expect a strict format as the UUID one and instead
  60. * it will admit any string of hexadecimal digits. Byte groups could be created
  61. * using the '-' separator. The number of bytes needs to be exactly 16 and the
  62. * separator '-' has to be exactly in a byte boundary. The following are
  63. * examples of accepted formats for the NGUID string:
  64. *
  65. * nguid="e9accd3b-8390-4e13-167c-f0593437f57d"
  66. * nguid="e9accd3b83904e13167cf0593437f57d"
  67. * nguid="FEDCBA9876543210-ABCDEF-0123456789"
  68. */
  69. static bool nvme_nguid_is_valid(const char *str)
  70. {
  71. int i;
  72. int digit_count = 0;
  73. for (i = 0; i < strlen(str); i++) {
  74. const char c = str[i];
  75. if (qemu_isxdigit(c)) {
  76. digit_count++;
  77. continue;
  78. }
  79. if (c == NGUID_SEPARATOR) {
  80. /*
  81. * We need to make sure the separator is in a byte boundary, the
  82. * string does not start with the separator and they are not back to
  83. * back "--".
  84. */
  85. if ((i > 0) && (str[i - 1] != NGUID_SEPARATOR) &&
  86. (digit_count % 2) == 0) {
  87. continue;
  88. }
  89. }
  90. return false;
  91. }
  92. /*
  93. * The string should have the correct byte length and not finish with the
  94. * separator
  95. */
  96. return (digit_count == (2 * NGUID_LEN)) && (str[i - 1] != NGUID_SEPARATOR);
  97. }
  98. static int nvme_nguid_parse(const char *str, NvmeNGUID *nguid)
  99. {
  100. uint8_t *id = &nguid->data[0];
  101. int ret = 0;
  102. int i;
  103. const char *ptr = str;
  104. if (!nvme_nguid_is_valid(str)) {
  105. return -1;
  106. }
  107. for (i = 0; i < NGUID_LEN; i++) {
  108. ret = sscanf(ptr, "%02hhx", &id[i]);
  109. if (ret != 1) {
  110. return -1;
  111. }
  112. ptr += 2;
  113. if (*ptr == NGUID_SEPARATOR) {
  114. ptr++;
  115. }
  116. }
  117. return 0;
  118. }
  119. /*
  120. * When converted back to string this implementation will use a raw hex number
  121. * with no separators, for instance:
  122. *
  123. * "e9accd3b83904e13167cf0593437f57d"
  124. */
  125. static void nvme_nguid_stringify(const NvmeNGUID *nguid, char *out)
  126. {
  127. const uint8_t *id = &nguid->data[0];
  128. snprintf(out, NGUID_STR_LEN, NGUID_FMT,
  129. id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7],
  130. id[8], id[9], id[10], id[11], id[12], id[13], id[14], id[15]);
  131. }
  132. static void get_nguid(Object *obj, Visitor *v, const char *name, void *opaque,
  133. Error **errp)
  134. {
  135. const Property *prop = opaque;
  136. NvmeNGUID *nguid = object_field_prop_ptr(obj, prop);
  137. char buffer[NGUID_STR_LEN];
  138. char *p = buffer;
  139. nvme_nguid_stringify(nguid, buffer);
  140. visit_type_str(v, name, &p, errp);
  141. }
  142. static void set_nguid(Object *obj, Visitor *v, const char *name, void *opaque,
  143. Error **errp)
  144. {
  145. const Property *prop = opaque;
  146. NvmeNGUID *nguid = object_field_prop_ptr(obj, prop);
  147. char *str;
  148. if (!visit_type_str(v, name, &str, errp)) {
  149. return;
  150. }
  151. if (!strcmp(str, NGUID_VALUE_AUTO)) {
  152. nvme_nguid_generate(nguid);
  153. } else if (nvme_nguid_parse(str, nguid) < 0) {
  154. error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
  155. }
  156. g_free(str);
  157. }
  158. const PropertyInfo qdev_prop_nguid = {
  159. .type = "str",
  160. .description =
  161. "NGUID or \"" NGUID_VALUE_AUTO "\" for random value",
  162. .get = get_nguid,
  163. .set = set_nguid,
  164. };