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