summaryrefslogtreecommitdiff
path: root/include/efi_selftest.h
blob: 7c69c3f3761080a585dfa7be4011b530cf149601 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* SPDX-License-Identifier: GPL-2.0+ */
/*
 *  EFI application loader
 *
 *  Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
 */

#ifndef _EFI_SELFTEST_H
#define _EFI_SELFTEST_H

#include <common.h>
#include <efi.h>
#include <efi_api.h>
#include <efi_loader.h>
#include <linker_lists.h>

#define EFI_ST_SUCCESS 0
#define EFI_ST_FAILURE 1
#define EFI_ST_SUCCESS_STR u"SUCCESS"

extern const struct efi_system_table *st_systable;
extern const struct efi_boot_services *st_boottime;

/**
 * efi_st_printf() - print a message
 *
 * @...:	format string followed by fields to print
 */
#define efi_st_printf(...) \
	(efi_st_printc(-1, __VA_ARGS__))

/**
 * efi_st_error() - prints an error message
 *
 * @...:	format string followed by fields to print
 */
#define efi_st_error(...) \
	(efi_st_printc(EFI_LIGHTRED, "%s(%u):\nERROR: ", __FILE__, __LINE__), \
	efi_st_printc(EFI_LIGHTRED, __VA_ARGS__))

/**
 * efi_st_todo() - prints a TODO message
 *
 * @...:	format string followed by fields to print
 */
#define efi_st_todo(...) \
	(efi_st_printc(EFI_YELLOW, "%s(%u):\nTODO: ", __FILE__, __LINE__), \
	efi_st_printc(EFI_YELLOW, __VA_ARGS__)) \

/**
 * enum efi_test_phase - phase when test will be executed
 *
 * A test may be setup and executed at boottime,
 * it may be setup at boottime and executed at runtime,
 * or it may be setup and executed at runtime.
 */
enum efi_test_phase {
	/**
	 * @EFI_EXECUTE_BEFORE_BOOTTIME_EXIT:
	 *
	 * Setup, execute, and teardown are executed before ExitBootServices().
	 */
	EFI_EXECUTE_BEFORE_BOOTTIME_EXIT = 1,
	/**
	 * @EFI_SETUP_BEFORE_BOOTTIME_EXIT:
	 *
	 * Setup is executed before ExitBootServices() while execute, and
	 * teardown are executed after ExitBootServices().
	 */
	EFI_SETUP_BEFORE_BOOTTIME_EXIT,
	/**
	 * @EFI_SETTING_VIRTUAL_ADDRESS_MAP:
	 *
	 * Execute calls SetVirtualAddressMap(). Setup is executed before
	 * ExitBootServices() while execute is executed after
	 * ExitBootServices(), and after the execute of tests marked as
	 * @EFI_SETUP_BEFORE_BOOTTIME_EXIT. Teardown is executed thereafter.
	 */
	EFI_SETTING_VIRTUAL_ADDRESS_MAP,
};

extern struct efi_simple_text_output_protocol *con_out;
extern struct efi_simple_text_input_protocol *con_in;

/**
 * efi_st_exit_boot_services() - exit the boot services
 *
 * * The size of the memory map is determined.
 * * Pool memory is allocated to copy the memory map.
 * * The memory map is copied and the map key is obtained.
 * * The map key is used to exit the boot services.
 */
void efi_st_exit_boot_services(void);

/**
 * efi_st_printc() - print a colored message
 *
 * @color:	color, see constants in efi_api.h, use -1 for no color
 * @fmt:	printf style format string
 * @...:	arguments to be printed
 */
void efi_st_printc(int color, const char *fmt, ...)
		 __attribute__ ((format (__printf__, 2, 3)));

/**
 * efi_st_translate_char() - translate a Unicode character to a string
 *
 * @code:	Unicode character
 * Return:	string
 */
u16 *efi_st_translate_char(u16 code);

/**
 * efi_st_translate_code() - translate a scan code to a human readable string
 *
 * This function translates the scan code returned by the simple text input
 * protocol to a human readable string, e.g. 0x04 is translated to u"Left".
 *
 * @code:	scan code
 * Return:	Unicode string
 */
u16 *efi_st_translate_code(u16 code);

/**
 * efi_st_strcmp_16_8() - compare an u16 string to a char string
 *
 * This function compares each u16 value to the char value at the same
 * position. This function is only useful for ANSI strings.
 *
 * @buf1:	u16 string
 * @buf2:	char string
 * Return:	0 if both buffers contain equivalent strings
 */
int efi_st_strcmp_16_8(const u16 *buf1, const unsigned char *buf2);

/**
 * efi_st_get_config_table() - get configuration table
 *
 * @guid:	GUID of the configuration table
 * Return:	pointer to configuration table or NULL
 */
void *efi_st_get_config_table(const efi_guid_t *guid);

/**
 * efi_st_get_key() - reads an Unicode character from the input device
 *
 * Return:	Unicode character
 */
u16 efi_st_get_key(void);

/**
 * struct efi_unit_test - EFI unit test
 *
 * The &struct efi_unit_test structure provides a interface to an EFI unit test.
 *
 * @name:	name of the unit test used in the user interface
 * @phase:	specifies when setup and execute are executed
 * @setup:	set up function of the unit test
 * @execute:	execute function of the unit test
 * @teardown:	tear down function of the unit test
 * @on_request:	flag indicating that the test shall only be executed on request
 */
struct efi_unit_test {
	const char *name;
	const enum efi_test_phase phase;
	int (*setup)(const efi_handle_t handle,
		     const struct efi_system_table *systable);
	int (*execute)(void);
	int (*teardown)(void);
	bool on_request;
};

/**
 * EFI_UNIT_TEST() - macro to declare a new EFI unit test
 *
 * The macro EFI_UNIT_TEST() declares an EFI unit test using the &struct
 * efi_unit_test structure. The test is added to a linker generated list which
 * is evaluated by the 'bootefi selftest' command.
 *
 * @__name:	string identifying the unit test in the linker generated list
 */
#define EFI_UNIT_TEST(__name)						\
	ll_entry_declare(struct efi_unit_test, __name, efi_unit_test)

#endif /* _EFI_SELFTEST_H */