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