efi_selftest_console.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_input_interface *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. * @buf: pointer to buffer address
  68. * on return position of terminating zero word
  69. */
  70. static void uint2dec(u32 value, u16 **buf)
  71. {
  72. u16 *pos = *buf;
  73. int i;
  74. u16 c;
  75. u64 f;
  76. /*
  77. * Increment by .5 and multiply with
  78. * (2 << 60) / 1,000,000,000 = 0x44B82FA0.9B5A52CC
  79. * to move the first digit to bit 60-63.
  80. */
  81. f = 0x225C17D0;
  82. f += (0x9B5A52DULL * value) >> 28;
  83. f += 0x44B82FA0ULL * value;
  84. for (i = 0; i < 10; ++i) {
  85. /* Write current digit */
  86. c = f >> 60;
  87. if (c || pos != *buf)
  88. *pos++ = c + '0';
  89. /* Eliminate current digit */
  90. f &= 0xfffffffffffffff;
  91. /* Get next digit */
  92. f *= 0xaULL;
  93. }
  94. if (pos == *buf)
  95. *pos++ = '0';
  96. *pos = 0;
  97. *buf = pos;
  98. }
  99. /*
  100. * Print a signed 32bit value as decimal number to an u16 string
  101. *
  102. * @value: value to be printed
  103. * @buf: pointer to buffer address
  104. * on return position of terminating zero word
  105. */
  106. static void int2dec(s32 value, u16 **buf)
  107. {
  108. u32 u;
  109. u16 *pos = *buf;
  110. if (value < 0) {
  111. *pos++ = '-';
  112. u = -value;
  113. } else {
  114. u = value;
  115. }
  116. uint2dec(u, &pos);
  117. *buf = pos;
  118. }
  119. /*
  120. * Print a colored formatted string to the EFI console
  121. *
  122. * @color color, see constants in efi_api.h, use -1 for no color
  123. * @fmt format string
  124. * @... optional arguments
  125. */
  126. void efi_st_printc(int color, const char *fmt, ...)
  127. {
  128. va_list args;
  129. u16 buf[160];
  130. const char *c;
  131. u16 *pos = buf;
  132. const char *s;
  133. u16 *u;
  134. va_start(args, fmt);
  135. if (color >= 0)
  136. con_out->set_attribute(con_out, (unsigned long)color);
  137. c = fmt;
  138. for (; *c; ++c) {
  139. switch (*c) {
  140. case '\\':
  141. ++c;
  142. switch (*c) {
  143. case '\0':
  144. --c;
  145. break;
  146. case 'n':
  147. *pos++ = '\n';
  148. break;
  149. case 'r':
  150. *pos++ = '\r';
  151. break;
  152. case 't':
  153. *pos++ = '\t';
  154. break;
  155. default:
  156. *pos++ = *c;
  157. }
  158. break;
  159. case '%':
  160. ++c;
  161. switch (*c) {
  162. case '\0':
  163. --c;
  164. break;
  165. case 'd':
  166. int2dec(va_arg(args, s32), &pos);
  167. break;
  168. case 'p':
  169. ++c;
  170. switch (*c) {
  171. /* MAC address */
  172. case 'm':
  173. mac(va_arg(args, void*), &pos);
  174. break;
  175. /* u16 string */
  176. case 's':
  177. u = va_arg(args, u16*);
  178. if (pos > buf) {
  179. *pos = 0;
  180. con_out->output_string(con_out,
  181. buf);
  182. }
  183. con_out->output_string(con_out, u);
  184. pos = buf;
  185. break;
  186. default:
  187. --c;
  188. pointer(va_arg(args, void*), &pos);
  189. }
  190. break;
  191. case 's':
  192. s = va_arg(args, const char *);
  193. for (; *s; ++s)
  194. *pos++ = *s;
  195. break;
  196. case 'u':
  197. uint2dec(va_arg(args, u32), &pos);
  198. break;
  199. default:
  200. break;
  201. }
  202. break;
  203. default:
  204. *pos++ = *c;
  205. }
  206. }
  207. va_end(args);
  208. *pos = 0;
  209. con_out->output_string(con_out, buf);
  210. if (color >= 0)
  211. con_out->set_attribute(con_out, EFI_LIGHTGRAY);
  212. }
  213. /*
  214. * Reads an Unicode character from the input device.
  215. *
  216. * @return: Unicode character
  217. */
  218. u16 efi_st_get_key(void)
  219. {
  220. struct efi_input_key input_key;
  221. efi_status_t ret;
  222. /* Wait for next key */
  223. do {
  224. ret = con_in->read_key_stroke(con_in, &input_key);
  225. } while (ret == EFI_NOT_READY);
  226. return input_key.unicode_char;
  227. }