blob: 7c69c3f3761080a585dfa7be4011b530cf149601 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +02002/*
3 * EFI application loader
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +02006 */
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 Schuchardtd78e40d2017-10-18 18:13:13 +020014#include <efi_loader.h>
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020015#include <linker_lists.h>
16
Heinrich Schuchardte67e7242017-10-04 15:31:26 +020017#define EFI_ST_SUCCESS 0
18#define EFI_ST_FAILURE 1
Simon Glass156ccbc2022-01-23 12:55:12 -070019#define EFI_ST_SUCCESS_STR u"SUCCESS"
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020020
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +020021extern const struct efi_system_table *st_systable;
22extern const struct efi_boot_services *st_boottime;
23
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020024/**
25 * efi_st_printf() - print a message
26 *
27 * @...: format string followed by fields to print
Heinrich Schuchardt853540c2018-01-11 08:15:54 +010028 */
29#define efi_st_printf(...) \
30 (efi_st_printc(-1, __VA_ARGS__))
31
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020032/**
33 * efi_st_error() - prints an error message
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020034 *
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020035 * @...: format string followed by fields to print
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020036 */
37#define efi_st_error(...) \
Heinrich Schuchardt853540c2018-01-11 08:15:54 +010038 (efi_st_printc(EFI_LIGHTRED, "%s(%u):\nERROR: ", __FILE__, __LINE__), \
39 efi_st_printc(EFI_LIGHTRED, __VA_ARGS__))
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020040
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020041/**
42 * efi_st_todo() - prints a TODO message
Heinrich Schuchardt927ca892017-11-06 21:17:43 +010043 *
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020044 * @...: format string followed by fields to print
Heinrich Schuchardt927ca892017-11-06 21:17:43 +010045 */
46#define efi_st_todo(...) \
Heinrich Schuchardt853540c2018-01-11 08:15:54 +010047 (efi_st_printc(EFI_YELLOW, "%s(%u):\nTODO: ", __FILE__, __LINE__), \
48 efi_st_printc(EFI_YELLOW, __VA_ARGS__)) \
Heinrich Schuchardt927ca892017-11-06 21:17:43 +010049
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020050/**
51 * enum efi_test_phase - phase when test will be executed
52 *
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020053 * A test may be setup and executed at boottime,
54 * it may be setup at boottime and executed at runtime,
55 * or it may be setup and executed at runtime.
56 */
57enum efi_test_phase {
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020058 /**
Heinrich Schuchardtb411b622021-03-28 10:41:00 +020059 * @EFI_EXECUTE_BEFORE_BOOTTIME_EXIT:
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020060 *
61 * Setup, execute, and teardown are executed before ExitBootServices().
62 */
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020063 EFI_EXECUTE_BEFORE_BOOTTIME_EXIT = 1,
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020064 /**
Heinrich Schuchardtb411b622021-03-28 10:41:00 +020065 * @EFI_SETUP_BEFORE_BOOTTIME_EXIT:
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020066 *
67 * Setup is executed before ExitBootServices() while execute, and
68 * teardown are executed after ExitBootServices().
69 */
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020070 EFI_SETUP_BEFORE_BOOTTIME_EXIT,
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020071 /**
Heinrich Schuchardtb411b622021-03-28 10:41:00 +020072 * @EFI_SETTING_VIRTUAL_ADDRESS_MAP:
73 *
74 * Execute calls SetVirtualAddressMap(). Setup is executed before
75 * ExitBootServices() while execute is executed after
76 * ExitBootServices(), and after the execute of tests marked as
77 * @EFI_SETUP_BEFORE_BOOTTIME_EXIT. Teardown is executed thereafter.
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020078 */
Heinrich Schuchardt5e219582021-03-24 17:48:01 +010079 EFI_SETTING_VIRTUAL_ADDRESS_MAP,
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020080};
81
82extern struct efi_simple_text_output_protocol *con_out;
Heinrich Schuchardt3e603ec2018-09-08 10:20:10 +020083extern struct efi_simple_text_input_protocol *con_in;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020084
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020085/**
86 * efi_st_exit_boot_services() - exit the boot services
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020087 *
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020088 * * The size of the memory map is determined.
89 * * Pool memory is allocated to copy the memory map.
90 * * The memory map is copied and the map key is obtained.
91 * * The map key is used to exit the boot services.
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020092 */
93void efi_st_exit_boot_services(void);
94
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020095/**
96 * efi_st_printc() - print a colored message
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +020097 *
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +020098 * @color: color, see constants in efi_api.h, use -1 for no color
99 * @fmt: printf style format string
100 * @...: arguments to be printed
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200101 */
Heinrich Schuchardt853540c2018-01-11 08:15:54 +0100102void efi_st_printc(int color, const char *fmt, ...)
103 __attribute__ ((format (__printf__, 2, 3)));
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200104
Heinrich Schuchardt262ff412018-09-11 22:38:04 +0200105/**
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +0200106 * efi_st_translate_char() - translate a Unicode character to a string
Heinrich Schuchardt262ff412018-09-11 22:38:04 +0200107 *
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +0200108 * @code: Unicode character
Heinrich Schuchardt262ff412018-09-11 22:38:04 +0200109 * Return: string
110 */
111u16 *efi_st_translate_char(u16 code);
112
113/**
114 * efi_st_translate_code() - translate a scan code to a human readable string
115 *
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +0200116 * This function translates the scan code returned by the simple text input
Simon Glass156ccbc2022-01-23 12:55:12 -0700117 * protocol to a human readable string, e.g. 0x04 is translated to u"Left".
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +0200118 *
119 * @code: scan code
120 * Return: Unicode string
Heinrich Schuchardt262ff412018-09-11 22:38:04 +0200121 */
122u16 *efi_st_translate_code(u16 code);
123
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +0200124/**
125 * efi_st_strcmp_16_8() - compare an u16 string to a char string
126 *
127 * This function compares each u16 value to the char value at the same
128 * position. This function is only useful for ANSI strings.
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200129 *
130 * @buf1: u16 string
131 * @buf2: char string
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +0200132 * Return: 0 if both buffers contain equivalent strings
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200133 */
Heinrich Schuchardtcaf29d12022-11-22 11:32:36 +0100134int efi_st_strcmp_16_8(const u16 *buf1, const unsigned char *buf2);
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200135
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +0200136/**
Heinrich Schuchardtb33f2462022-09-03 15:56:51 +0200137 * efi_st_get_config_table() - get configuration table
138 *
139 * @guid: GUID of the configuration table
140 * Return: pointer to configuration table or NULL
141 */
142void *efi_st_get_config_table(const efi_guid_t *guid);
143
144/**
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +0200145 * efi_st_get_key() - reads an Unicode character from the input device
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200146 *
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +0200147 * Return: Unicode character
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200148 */
149u16 efi_st_get_key(void);
150
151/**
152 * struct efi_unit_test - EFI unit test
153 *
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +0200154 * The &struct efi_unit_test structure provides a interface to an EFI unit test.
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200155 *
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +0200156 * @name: name of the unit test used in the user interface
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200157 * @phase: specifies when setup and execute are executed
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +0200158 * @setup: set up function of the unit test
159 * @execute: execute function of the unit test
160 * @teardown: tear down function of the unit test
161 * @on_request: flag indicating that the test shall only be executed on request
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200162 */
163struct efi_unit_test {
164 const char *name;
165 const enum efi_test_phase phase;
166 int (*setup)(const efi_handle_t handle,
167 const struct efi_system_table *systable);
168 int (*execute)(void);
169 int (*teardown)(void);
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200170 bool on_request;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200171};
172
Heinrich Schuchardt7fec2492020-05-09 07:33:43 +0200173/**
174 * EFI_UNIT_TEST() - macro to declare a new EFI unit test
175 *
176 * The macro EFI_UNIT_TEST() declares an EFI unit test using the &struct
177 * efi_unit_test structure. The test is added to a linker generated list which
178 * is evaluated by the 'bootefi selftest' command.
179 *
180 * @__name: string identifying the unit test in the linker generated list
181 */
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200182#define EFI_UNIT_TEST(__name) \
183 ll_entry_declare(struct efi_unit_test, __name, efi_unit_test)
184
185#endif /* _EFI_SELFTEST_H */