efi_selftest.h 5.1 KB

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