efi_selftest_console.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * EFI efi_selftest
  3. *
  4. * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <efi_selftest.h>
  9. #include <vsprintf.h>
  10. struct efi_simple_text_output_protocol *con_out;
  11. struct efi_simple_input_interface *con_in;
  12. /*
  13. * Print a pointer to an u16 string
  14. *
  15. * @pointer: pointer
  16. * @buf: pointer to buffer address
  17. * on return position of terminating zero word
  18. */
  19. static void pointer(void *pointer, u16 **buf)
  20. {
  21. int i;
  22. u16 c;
  23. uintptr_t p = (uintptr_t)pointer;
  24. u16 *pos = *buf;
  25. for (i = 8 * sizeof(p) - 4; i >= 0; i -= 4) {
  26. c = (p >> i) & 0x0f;
  27. c += '0';
  28. if (c > '9')
  29. c += 'a' - '9' - 1;
  30. *pos++ = c;
  31. }
  32. *pos = 0;
  33. *buf = pos;
  34. }
  35. /*
  36. * Print an unsigned 32bit value as decimal number to an u16 string
  37. *
  38. * @value: value to be printed
  39. * @buf: pointer to buffer address
  40. * on return position of terminating zero word
  41. */
  42. static void uint2dec(u32 value, u16 **buf)
  43. {
  44. u16 *pos = *buf;
  45. int i;
  46. u16 c;
  47. u64 f;
  48. /*
  49. * Increment by .5 and multiply with
  50. * (2 << 60) / 1,000,000,000 = 0x44B82FA0.9B5A52CC
  51. * to move the first digit to bit 60-63.
  52. */
  53. f = 0x225C17D0;
  54. f += (0x9B5A52DULL * value) >> 28;
  55. f += 0x44B82FA0ULL * value;
  56. for (i = 0; i < 10; ++i) {
  57. /* Write current digit */
  58. c = f >> 60;
  59. if (c || pos != *buf)
  60. *pos++ = c + '0';
  61. /* Eliminate current digit */
  62. f &= 0xfffffffffffffff;
  63. /* Get next digit */
  64. f *= 0xaULL;
  65. }
  66. if (pos == *buf)
  67. *pos++ = '0';
  68. *pos = 0;
  69. *buf = pos;
  70. }
  71. /*
  72. * Print a signed 32bit value as decimal number to an u16 string
  73. *
  74. * @value: value to be printed
  75. * @buf: pointer to buffer address
  76. * on return position of terminating zero word
  77. */
  78. static void int2dec(s32 value, u16 **buf)
  79. {
  80. u32 u;
  81. u16 *pos = *buf;
  82. if (value < 0) {
  83. *pos++ = '-';
  84. u = -value;
  85. } else {
  86. u = value;
  87. }
  88. uint2dec(u, &pos);
  89. *buf = pos;
  90. }
  91. /*
  92. * Print a formatted string to the EFI console
  93. *
  94. * @fmt: format string
  95. * @...: optional arguments
  96. */
  97. void efi_st_printf(const char *fmt, ...)
  98. {
  99. va_list args;
  100. u16 buf[160];
  101. const char *c;
  102. u16 *pos = buf;
  103. const char *s;
  104. va_start(args, fmt);
  105. c = fmt;
  106. for (; *c; ++c) {
  107. switch (*c) {
  108. case '\\':
  109. ++c;
  110. switch (*c) {
  111. case '\0':
  112. --c;
  113. break;
  114. case 'n':
  115. *pos++ = '\n';
  116. break;
  117. case 'r':
  118. *pos++ = '\r';
  119. break;
  120. case 't':
  121. *pos++ = '\t';
  122. break;
  123. default:
  124. *pos++ = *c;
  125. }
  126. break;
  127. case '%':
  128. ++c;
  129. switch (*c) {
  130. case '\0':
  131. --c;
  132. break;
  133. case 'd':
  134. int2dec(va_arg(args, s32), &pos);
  135. break;
  136. case 'p':
  137. pointer(va_arg(args, void*), &pos);
  138. break;
  139. case 's':
  140. s = va_arg(args, const char *);
  141. for (; *s; ++s)
  142. *pos++ = *s;
  143. break;
  144. case 'u':
  145. uint2dec(va_arg(args, u32), &pos);
  146. break;
  147. default:
  148. break;
  149. }
  150. break;
  151. default:
  152. *pos++ = *c;
  153. }
  154. }
  155. va_end(args);
  156. *pos = 0;
  157. con_out->output_string(con_out, buf);
  158. }
  159. /*
  160. * Reads an Unicode character from the input device.
  161. *
  162. * @return: Unicode character
  163. */
  164. u16 efi_st_get_key(void)
  165. {
  166. struct efi_input_key input_key;
  167. efi_status_t ret;
  168. /* Wait for next key */
  169. do {
  170. ret = con_in->read_key_stroke(con_in, &input_key);
  171. } while (ret == EFI_NOT_READY);
  172. return input_key.unicode_char;
  173. }