efi_selftest_console.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI efi_selftest
  4. *
  5. * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. */
  7. #include <efi_selftest.h>
  8. #include <vsprintf.h>
  9. struct efi_simple_text_output_protocol *con_out;
  10. struct efi_simple_text_input_protocol *con_in;
  11. /*
  12. * Print a MAC address to an u16 string
  13. *
  14. * @pointer: mac address
  15. * @buf: pointer to buffer address
  16. * on return position of terminating zero word
  17. */
  18. static void mac(void *pointer, u16 **buf)
  19. {
  20. int i, j;
  21. u16 c;
  22. u8 *p = (u8 *)pointer;
  23. u8 byte;
  24. u16 *pos = *buf;
  25. for (i = 0; i < ARP_HLEN; ++i) {
  26. if (i)
  27. *pos++ = ':';
  28. byte = p[i];
  29. for (j = 4; j >= 0; j -= 4) {
  30. c = (byte >> j) & 0x0f;
  31. c += '0';
  32. if (c > '9')
  33. c += 'a' - '9' - 1;
  34. *pos++ = c;
  35. }
  36. }
  37. *pos = 0;
  38. *buf = pos;
  39. }
  40. /*
  41. * Print a pointer to an u16 string
  42. *
  43. * @pointer: pointer
  44. * @buf: pointer to buffer address
  45. * on return position of terminating zero word
  46. */
  47. static void pointer(void *pointer, u16 **buf)
  48. {
  49. int i;
  50. u16 c;
  51. uintptr_t p = (uintptr_t)pointer;
  52. u16 *pos = *buf;
  53. for (i = 8 * sizeof(p) - 4; i >= 0; i -= 4) {
  54. c = (p >> i) & 0x0f;
  55. c += '0';
  56. if (c > '9')
  57. c += 'a' - '9' - 1;
  58. *pos++ = c;
  59. }
  60. *pos = 0;
  61. *buf = pos;
  62. }
  63. /*
  64. * Print an unsigned 32bit value as decimal number to an u16 string
  65. *
  66. * @value: value to be printed
  67. * @prec: minimum number of digits to display
  68. * @buf: pointer to buffer address
  69. * on return position of terminating zero word
  70. */
  71. static void uint2dec(u32 value, int prec, u16 **buf)
  72. {
  73. u16 *pos = *buf;
  74. int i;
  75. u16 c;
  76. u64 f;
  77. /*
  78. * Increment by .5 and multiply with
  79. * (2 << 60) / 1,000,000,000 = 0x44B82FA0.9B5A52CC
  80. * to move the first digit to bit 60-63.
  81. */
  82. f = 0x225C17D0;
  83. f += (0x9B5A52DULL * value) >> 28;
  84. f += 0x44B82FA0ULL * value;
  85. for (i = 0; i < 10; ++i) {
  86. /* Write current digit */
  87. c = f >> 60;
  88. if (c || pos != *buf || 10 - i <= prec)
  89. *pos++ = c + '0';
  90. /* Eliminate current digit */
  91. f &= 0xfffffffffffffff;
  92. /* Get next digit */
  93. f *= 0xaULL;
  94. }
  95. if (pos == *buf)
  96. *pos++ = '0';
  97. *pos = 0;
  98. *buf = pos;
  99. }
  100. /*
  101. * Print a signed 32bit value as decimal number to an u16 string
  102. *
  103. * @value: value to be printed
  104. * @prec: minimum number of digits to display
  105. * @buf: pointer to buffer address
  106. * on return position of terminating zero word
  107. */
  108. static void int2dec(s32 value, int prec, u16 **buf)
  109. {
  110. u32 u;
  111. u16 *pos = *buf;
  112. if (value < 0) {
  113. *pos++ = '-';
  114. u = -value;
  115. } else {
  116. u = value;
  117. }
  118. uint2dec(u, prec, &pos);
  119. *buf = pos;
  120. }
  121. /*
  122. * Print a colored formatted string to the EFI console
  123. *
  124. * @color color, see constants in efi_api.h, use -1 for no color
  125. * @fmt format string
  126. * @... optional arguments
  127. */
  128. void efi_st_printc(int color, const char *fmt, ...)
  129. {
  130. va_list args;
  131. u16 buf[160];
  132. const char *c;
  133. u16 *pos = buf;
  134. const char *s;
  135. u16 *u;
  136. int prec;
  137. va_start(args, fmt);
  138. if (color >= 0)
  139. con_out->set_attribute(con_out, (unsigned long)color);
  140. c = fmt;
  141. for (; *c; ++c) {
  142. switch (*c) {
  143. case '\\':
  144. ++c;
  145. switch (*c) {
  146. case '\0':
  147. --c;
  148. break;
  149. case 'n':
  150. *pos++ = '\n';
  151. break;
  152. case 'r':
  153. *pos++ = '\r';
  154. break;
  155. case 't':
  156. *pos++ = '\t';
  157. break;
  158. default:
  159. *pos++ = *c;
  160. }
  161. break;
  162. case '%':
  163. ++c;
  164. /* Parse precision */
  165. if (*c == '.') {
  166. ++c;
  167. prec = *c - '0';
  168. ++c;
  169. } else {
  170. prec = 0;
  171. }
  172. switch (*c) {
  173. case '\0':
  174. --c;
  175. break;
  176. case 'd':
  177. int2dec(va_arg(args, s32), prec, &pos);
  178. break;
  179. case 'p':
  180. ++c;
  181. switch (*c) {
  182. /* MAC address */
  183. case 'm':
  184. mac(va_arg(args, void*), &pos);
  185. break;
  186. /* u16 string */
  187. case 's':
  188. u = va_arg(args, u16*);
  189. if (pos > buf) {
  190. *pos = 0;
  191. con_out->output_string(con_out,
  192. buf);
  193. }
  194. con_out->output_string(con_out, u);
  195. pos = buf;
  196. break;
  197. default:
  198. --c;
  199. pointer(va_arg(args, void*), &pos);
  200. }
  201. break;
  202. case 's':
  203. s = va_arg(args, const char *);
  204. for (; *s; ++s)
  205. *pos++ = *s;
  206. break;
  207. case 'u':
  208. uint2dec(va_arg(args, u32), prec, &pos);
  209. break;
  210. default:
  211. break;
  212. }
  213. break;
  214. default:
  215. *pos++ = *c;
  216. }
  217. }
  218. va_end(args);
  219. *pos = 0;
  220. con_out->output_string(con_out, buf);
  221. if (color >= 0)
  222. con_out->set_attribute(con_out, EFI_LIGHTGRAY);
  223. }
  224. /*
  225. * Reads an Unicode character from the input device.
  226. *
  227. * @return: Unicode character
  228. */
  229. u16 efi_st_get_key(void)
  230. {
  231. struct efi_input_key input_key;
  232. efi_status_t ret;
  233. /* Wait for next key */
  234. do {
  235. ret = con_in->read_key_stroke(con_in, &input_key);
  236. } while (ret == EFI_NOT_READY);
  237. return input_key.unicode_char;
  238. }