var-service-utils.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * SPDX-License-Identifier: GPL-2.0-or-later
  3. *
  4. * uefi vars device - helper functions for ucs2 strings and tracing
  5. */
  6. #include "qemu/osdep.h"
  7. #include "system/dma.h"
  8. #include "hw/uefi/var-service.h"
  9. #include "trace/trace-hw_uefi.h"
  10. /* ------------------------------------------------------------------ */
  11. /*
  12. * string helper functions.
  13. *
  14. * Most of the time uefi ucs2 strings are NULL-terminated, except
  15. * sometimes when they are not (for example in variable policies).
  16. */
  17. gboolean uefi_str_is_valid(const uint16_t *str, size_t len,
  18. gboolean must_be_null_terminated)
  19. {
  20. size_t pos = 0;
  21. for (;;) {
  22. if (pos == len) {
  23. if (must_be_null_terminated) {
  24. return false;
  25. } else {
  26. return true;
  27. }
  28. }
  29. switch (str[pos]) {
  30. case 0:
  31. /* end of string */
  32. return true;
  33. case 0xd800 ... 0xdfff:
  34. /* reject surrogates */
  35. return false;
  36. default:
  37. /* char is good, check next */
  38. break;
  39. }
  40. pos++;
  41. }
  42. }
  43. size_t uefi_strlen(const uint16_t *str, size_t len)
  44. {
  45. size_t pos = 0;
  46. for (;;) {
  47. if (pos == len) {
  48. return pos;
  49. }
  50. if (str[pos] == 0) {
  51. return pos;
  52. }
  53. pos++;
  54. }
  55. }
  56. gboolean uefi_str_equal_ex(const uint16_t *a, size_t alen,
  57. const uint16_t *b, size_t blen,
  58. gboolean wildcards_in_a)
  59. {
  60. size_t pos = 0;
  61. alen = alen / 2;
  62. blen = blen / 2;
  63. for (;;) {
  64. if (pos == alen && pos == blen) {
  65. return true;
  66. }
  67. if (pos == alen && b[pos] == 0) {
  68. return true;
  69. }
  70. if (pos == blen && a[pos] == 0) {
  71. return true;
  72. }
  73. if (pos == alen || pos == blen) {
  74. return false;
  75. }
  76. if (a[pos] == 0 && b[pos] == 0) {
  77. return true;
  78. }
  79. if (wildcards_in_a && a[pos] == '#') {
  80. if (!isxdigit(b[pos])) {
  81. return false;
  82. }
  83. } else {
  84. if (a[pos] != b[pos]) {
  85. return false;
  86. }
  87. }
  88. pos++;
  89. }
  90. }
  91. gboolean uefi_str_equal(const uint16_t *a, size_t alen,
  92. const uint16_t *b, size_t blen)
  93. {
  94. return uefi_str_equal_ex(a, alen, b, blen, false);
  95. }
  96. char *uefi_ucs2_to_ascii(const uint16_t *ucs2, uint64_t ucs2_size)
  97. {
  98. char *str = g_malloc0(ucs2_size / 2 + 1);
  99. int i;
  100. for (i = 0; i * 2 < ucs2_size; i++) {
  101. if (ucs2[i] == 0) {
  102. break;
  103. }
  104. if (ucs2[i] < 128) {
  105. str[i] = ucs2[i];
  106. } else {
  107. str[i] = '?';
  108. }
  109. }
  110. str[i] = 0;
  111. return str;
  112. }
  113. /* ------------------------------------------------------------------ */
  114. /* time helper functions */
  115. int uefi_time_compare(efi_time *a, efi_time *b)
  116. {
  117. if (a->year < b->year) {
  118. return -1;
  119. }
  120. if (a->year > b->year) {
  121. return 1;
  122. }
  123. if (a->month < b->month) {
  124. return -1;
  125. }
  126. if (a->month > b->month) {
  127. return 1;
  128. }
  129. if (a->day < b->day) {
  130. return -1;
  131. }
  132. if (a->day > b->day) {
  133. return 1;
  134. }
  135. if (a->hour < b->hour) {
  136. return -1;
  137. }
  138. if (a->hour > b->hour) {
  139. return 1;
  140. }
  141. if (a->minute < b->minute) {
  142. return -1;
  143. }
  144. if (a->minute > b->minute) {
  145. return 1;
  146. }
  147. if (a->second < b->second) {
  148. return -1;
  149. }
  150. if (a->second > b->second) {
  151. return 1;
  152. }
  153. if (a->nanosecond < b->nanosecond) {
  154. return -1;
  155. }
  156. if (a->nanosecond > b->nanosecond) {
  157. return 1;
  158. }
  159. return 0;
  160. }
  161. /* ------------------------------------------------------------------ */
  162. /* tracing helper functions */
  163. void uefi_trace_variable(const char *action, QemuUUID guid,
  164. const uint16_t *name, uint64_t name_size)
  165. {
  166. QemuUUID be = qemu_uuid_bswap(guid);
  167. char *str_uuid = qemu_uuid_unparse_strdup(&be);
  168. char *str_name = uefi_ucs2_to_ascii(name, name_size);
  169. trace_uefi_variable(action, str_name, name_size, str_uuid);
  170. g_free(str_name);
  171. g_free(str_uuid);
  172. }
  173. void uefi_trace_status(const char *action, efi_status status)
  174. {
  175. switch (status) {
  176. case EFI_SUCCESS:
  177. trace_uefi_status(action, "success");
  178. break;
  179. case EFI_INVALID_PARAMETER:
  180. trace_uefi_status(action, "invalid parameter");
  181. break;
  182. case EFI_UNSUPPORTED:
  183. trace_uefi_status(action, "unsupported");
  184. break;
  185. case EFI_BAD_BUFFER_SIZE:
  186. trace_uefi_status(action, "bad buffer size");
  187. break;
  188. case EFI_BUFFER_TOO_SMALL:
  189. trace_uefi_status(action, "buffer too small");
  190. break;
  191. case EFI_WRITE_PROTECTED:
  192. trace_uefi_status(action, "write protected");
  193. break;
  194. case EFI_OUT_OF_RESOURCES:
  195. trace_uefi_status(action, "out of resources");
  196. break;
  197. case EFI_NOT_FOUND:
  198. trace_uefi_status(action, "not found");
  199. break;
  200. case EFI_ACCESS_DENIED:
  201. trace_uefi_status(action, "access denied");
  202. break;
  203. case EFI_ALREADY_STARTED:
  204. trace_uefi_status(action, "already started");
  205. break;
  206. case EFI_SECURITY_VIOLATION:
  207. trace_uefi_status(action, "security violation");
  208. break;
  209. default:
  210. trace_uefi_status(action, "unknown error");
  211. break;
  212. }
  213. }