xref: /openbmc/u-boot/include/efi_selftest.h (revision 58008cba)
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 
8 #ifndef _EFI_SELFTEST_H
9 #define _EFI_SELFTEST_H
10 
11 #include <common.h>
12 #include <efi.h>
13 #include <efi_api.h>
14 #include <efi_loader.h>
15 #include <linker_lists.h>
16 
17 #define EFI_ST_SUCCESS 0
18 #define EFI_ST_FAILURE 1
19 
20 /*
21  * Prints a message.
22  */
23 #define efi_st_printf(...) \
24 	(efi_st_printc(-1, __VA_ARGS__))
25 
26 /*
27  * 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 /*
36  * Prints a TODO message.
37  *
38  * @...	format string followed by fields to print
39  */
40 #define efi_st_todo(...) \
41 	(efi_st_printc(EFI_YELLOW, "%s(%u):\nTODO: ", __FILE__, __LINE__), \
42 	efi_st_printc(EFI_YELLOW, __VA_ARGS__)) \
43 
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 	EFI_EXECUTE_BEFORE_BOOTTIME_EXIT = 1,
51 	EFI_SETUP_BEFORE_BOOTTIME_EXIT,
52 	EFI_SETUP_AFTER_BOOTTIME_EXIT,
53 };
54 
55 extern struct efi_simple_text_output_protocol *con_out;
56 extern struct efi_simple_input_interface *con_in;
57 
58 /*
59  * Exit the boot services.
60  *
61  * The size of the memory map is determined.
62  * Pool memory is allocated to copy the memory map.
63  * The memory amp is copied and the map key is obtained.
64  * The map key is used to exit the boot services.
65  */
66 void efi_st_exit_boot_services(void);
67 
68 /*
69  * Print a colored message
70  *
71  * @color	color, see constants in efi_api.h, use -1 for no color
72  * @fmt		printf format
73  * @...		arguments to be printed
74  *		on return position of terminating zero word
75  */
76 void efi_st_printc(int color, const char *fmt, ...)
77 		 __attribute__ ((format (__printf__, 2, 3)));
78 
79 /*
80  * Compare memory.
81  * We cannot use lib/string.c due to different CFLAGS values.
82  *
83  * @buf1:	first buffer
84  * @buf2:	second buffer
85  * @length:	number of bytes to compare
86  * @return:	0 if both buffers contain the same bytes
87  */
88 int efi_st_memcmp(const void *buf1, const void *buf2, size_t length);
89 
90 /*
91  * Compare an u16 string to a char string.
92  *
93  * @buf1:	u16 string
94  * @buf2:	char string
95  * @return:	0 if both buffers contain the same bytes
96  */
97 int efi_st_strcmp_16_8(const u16 *buf1, const char *buf2);
98 
99 /*
100  * Reads an Unicode character from the input device.
101  *
102  * @return: Unicode character
103  */
104 u16 efi_st_get_key(void);
105 
106 /**
107  * struct efi_unit_test - EFI unit test
108  *
109  * An efi_unit_test provides a interface to an EFI unit test.
110  *
111  * @name:	name of unit test
112  * @phase:	specifies when setup and execute are executed
113  * @setup:	set up the unit test
114  * @teardown:	tear down the unit test
115  * @execute:	execute the unit test
116  * @setup_ok:	setup was successful (set at runtime)
117  * @on_request:	test is only executed on request
118  */
119 struct efi_unit_test {
120 	const char *name;
121 	const enum efi_test_phase phase;
122 	int (*setup)(const efi_handle_t handle,
123 		     const struct efi_system_table *systable);
124 	int (*execute)(void);
125 	int (*teardown)(void);
126 	int setup_ok;
127 	bool on_request;
128 };
129 
130 /* Declare a new EFI unit test */
131 #define EFI_UNIT_TEST(__name)						\
132 	ll_entry_declare(struct efi_unit_test, __name, efi_unit_test)
133 
134 #endif /* _EFI_SELFTEST_H */
135