efi_selftest.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * EFI application loader
  4. *
  5. * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. */
  7. #ifndef _EFI_SELFTEST_H
  8. #define _EFI_SELFTEST_H
  9. #include <common.h>
  10. #include <efi.h>
  11. #include <efi_api.h>
  12. #include <efi_loader.h>
  13. #include <linker_lists.h>
  14. #define EFI_ST_SUCCESS 0
  15. #define EFI_ST_FAILURE 1
  16. #define EFI_ST_SUCCESS_STR L"SUCCESS"
  17. /**
  18. * efi_st_printf() - print a message
  19. *
  20. * @...: format string followed by fields to print
  21. */
  22. #define efi_st_printf(...) \
  23. (efi_st_printc(-1, __VA_ARGS__))
  24. /**
  25. * efi_st_error() - prints an error message
  26. *
  27. * @...: format string followed by fields to print
  28. */
  29. #define efi_st_error(...) \
  30. (efi_st_printc(EFI_LIGHTRED, "%s(%u):\nERROR: ", __FILE__, __LINE__), \
  31. efi_st_printc(EFI_LIGHTRED, __VA_ARGS__))
  32. /**
  33. * efi_st_todo() - prints a TODO message
  34. *
  35. * @...: format string followed by fields to print
  36. */
  37. #define efi_st_todo(...) \
  38. (efi_st_printc(EFI_YELLOW, "%s(%u):\nTODO: ", __FILE__, __LINE__), \
  39. efi_st_printc(EFI_YELLOW, __VA_ARGS__)) \
  40. /**
  41. * enum efi_test_phase - phase when test will be executed
  42. *
  43. * A test may be setup and executed at boottime,
  44. * it may be setup at boottime and executed at runtime,
  45. * or it may be setup and executed at runtime.
  46. */
  47. enum efi_test_phase {
  48. /**
  49. * @EFI_EXECUTE_BEFORE_BOOTTIME_EXIT: - execute before ExitBootServices
  50. *
  51. * Setup, execute, and teardown are executed before ExitBootServices().
  52. */
  53. EFI_EXECUTE_BEFORE_BOOTTIME_EXIT = 1,
  54. /**
  55. * @EFI_SETUP_BEFORE_BOOTTIME_EXIT: - setup before ExitBootServices
  56. *
  57. * Setup is executed before ExitBootServices() while execute, and
  58. * teardown are executed after ExitBootServices().
  59. */
  60. EFI_SETUP_BEFORE_BOOTTIME_EXIT,
  61. /**
  62. * @EFI_SETUP_AFTER_BOOTTIME_EXIT: - setup after ExitBootServices
  63. *
  64. * Setup, execute, and teardown are executed after ExitBootServices().
  65. */
  66. EFI_SETUP_AFTER_BOOTTIME_EXIT,
  67. };
  68. extern struct efi_simple_text_output_protocol *con_out;
  69. extern struct efi_simple_text_input_protocol *con_in;
  70. /**
  71. * efi_st_exit_boot_services() - exit the boot services
  72. *
  73. * * The size of the memory map is determined.
  74. * * Pool memory is allocated to copy the memory map.
  75. * * The memory map is copied and the map key is obtained.
  76. * * The map key is used to exit the boot services.
  77. */
  78. void efi_st_exit_boot_services(void);
  79. /**
  80. * efi_st_printc() - print a colored message
  81. *
  82. * @color: color, see constants in efi_api.h, use -1 for no color
  83. * @fmt: printf style format string
  84. * @...: arguments to be printed
  85. */
  86. void efi_st_printc(int color, const char *fmt, ...)
  87. __attribute__ ((format (__printf__, 2, 3)));
  88. /**
  89. * efi_st_translate_char() - translate a Unicode character to a string
  90. *
  91. * @code: Unicode character
  92. * Return: string
  93. */
  94. u16 *efi_st_translate_char(u16 code);
  95. /**
  96. * efi_st_translate_code() - translate a scan code to a human readable string
  97. *
  98. * This function translates the scan code returned by the simple text input
  99. * protocol to a human readable string, e.g. 0x04 is translated to L"Left".
  100. *
  101. * @code: scan code
  102. * Return: Unicode string
  103. */
  104. u16 *efi_st_translate_code(u16 code);
  105. /**
  106. * efi_st_strcmp_16_8() - compare an u16 string to a char string
  107. *
  108. * This function compares each u16 value to the char value at the same
  109. * position. This function is only useful for ANSI strings.
  110. *
  111. * @buf1: u16 string
  112. * @buf2: char string
  113. * Return: 0 if both buffers contain equivalent strings
  114. */
  115. int efi_st_strcmp_16_8(const u16 *buf1, const char *buf2);
  116. /**
  117. * efi_st_get_key() - reads an Unicode character from the input device
  118. *
  119. * Return: Unicode character
  120. */
  121. u16 efi_st_get_key(void);
  122. /**
  123. * struct efi_unit_test - EFI unit test
  124. *
  125. * The &struct efi_unit_test structure provides a interface to an EFI unit test.
  126. *
  127. * @name: name of the unit test used in the user interface
  128. * @phase: specifies when setup and execute are executed
  129. * @setup: set up function of the unit test
  130. * @execute: execute function of the unit test
  131. * @teardown: tear down function of the unit test
  132. * @on_request: flag indicating that the test shall only be executed on request
  133. */
  134. struct efi_unit_test {
  135. const char *name;
  136. const enum efi_test_phase phase;
  137. int (*setup)(const efi_handle_t handle,
  138. const struct efi_system_table *systable);
  139. int (*execute)(void);
  140. int (*teardown)(void);
  141. bool on_request;
  142. };
  143. /**
  144. * EFI_UNIT_TEST() - macro to declare a new EFI unit test
  145. *
  146. * The macro EFI_UNIT_TEST() declares an EFI unit test using the &struct
  147. * efi_unit_test structure. The test is added to a linker generated list which
  148. * is evaluated by the 'bootefi selftest' command.
  149. *
  150. * @__name: string identifying the unit test in the linker generated list
  151. */
  152. #define EFI_UNIT_TEST(__name) \
  153. ll_entry_declare(struct efi_unit_test, __name, efi_unit_test)
  154. #endif /* _EFI_SELFTEST_H */