1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * efi_selftest_gop
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 *
7 * Test the graphical output protocol.
8 */
9
10#include <efi_selftest.h>
11
12static struct efi_boot_services *boottime;
13static efi_guid_t efi_gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
14static struct efi_gop *gop;
15
16/*
17 * Setup unit test.
18 *
19 * @handle:	handle of the loaded image
20 * @systable:	system table
21 * Return:	EFI_ST_SUCCESS for success
22 */
23static int setup(const efi_handle_t handle,
24		 const struct efi_system_table *systable)
25{
26	efi_status_t ret;
27
28	boottime = systable->boottime;
29
30	ret = boottime->locate_protocol(&efi_gop_guid, NULL, (void **)&gop);
31	if (ret != EFI_SUCCESS) {
32		gop = NULL;
33		efi_st_printf("Graphical output protocol is not available.\n");
34	}
35
36	return EFI_ST_SUCCESS;
37}
38
39/*
40 * Tear down unit test.
41 *
42 * Return:	EFI_ST_SUCCESS for success
43 */
44static int teardown(void)
45{
46	return EFI_ST_SUCCESS;
47}
48
49/*
50 * Execute unit test.
51 *
52 * Return:	EFI_ST_SUCCESS for success
53 */
54static int execute(void)
55{
56	efi_status_t ret;
57	u32 i, max_mode;
58	efi_uintn_t size;
59	struct efi_gop_mode_info *info;
60
61	if (!gop)
62		return EFI_ST_SUCCESS;
63
64	if (!gop->mode) {
65		efi_st_error("EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE missing\n");
66		return EFI_ST_FAILURE;
67	}
68	max_mode = gop->mode->max_mode;
69	if (!max_mode) {
70		efi_st_error("No graphical mode available\n");
71		return EFI_ST_FAILURE;
72	}
73	efi_st_printf("Number of available modes: %u\n", max_mode);
74
75	for (i = 0; i < max_mode; ++i) {
76		ret = gop->query_mode(gop, i, &size, &info);
77		if (ret != EFI_SUCCESS) {
78			efi_st_printf("Could not query mode %u\n", i);
79			return EFI_ST_FAILURE;
80		}
81		efi_st_printf("Mode %u: %u x %u\n",
82			      i, info->width, info->height);
83		ret = boottime->free_pool(info);
84		if (ret != EFI_SUCCESS) {
85			efi_st_printf("FreePool failed");
86			return EFI_ST_FAILURE;
87		}
88	}
89
90	return EFI_ST_SUCCESS;
91}
92
93EFI_UNIT_TEST(gop) = {
94	.name = "graphical output",
95	.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
96	.setup = setup,
97	.execute = execute,
98	.teardown = teardown,
99};
100