1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * efi_selftest_miniapp_exit
4 *
5 * Copyright (c) 2018 Heinrich Schuchardt
6 *
7 * This EFI application is run by the StartImage selftest.
8 * It uses the Exit boot service to return.
9 */
10
11#include <efi_selftest.h>
12
13static efi_guid_t loaded_image_protocol_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
14
15/**
16 * check_loaded_image_protocol() - check image_base/image_size
17 *
18 * Try to open the loaded image protocol. Check that this function is located
19 * between image_base and image_base + image_size.
20 *
21 * @image_handle:	handle of the loaded image
22 * @systable:		system table
23 * Return:		status code
24 */
25static efi_status_t EFIAPI check_loaded_image_protocol
26		(efi_handle_t image_handle, struct efi_system_table *systable)
27{
28	struct efi_simple_text_output_protocol *cout = systable->con_out;
29	struct efi_boot_services *boottime = systable->boottime;
30	struct efi_loaded_image *loaded_image_protocol;
31	efi_status_t ret;
32
33	/*
34	 * Open the loaded image protocol.
35	 */
36	ret = boottime->open_protocol
37				(image_handle, &loaded_image_protocol_guid,
38				 (void **)&loaded_image_protocol, NULL,
39				  NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
40	if (ret != EFI_SUCCESS) {
41		cout->output_string(cout,
42				    u"Could not open loaded image protocol\n");
43		return ret;
44	}
45	if ((void *)check_loaded_image_protocol <
46	    loaded_image_protocol->image_base ||
47	    (void *)check_loaded_image_protocol >=
48	    loaded_image_protocol->image_base +
49	    loaded_image_protocol->image_size) {
50		cout->output_string(cout,
51				    u"Incorrect image_base or image_size\n");
52		return EFI_NOT_FOUND;
53	}
54	return EFI_SUCCESS;
55}
56
57/**
58 * Entry point of the EFI application.
59 *
60 * @handle:	handle of the loaded image
61 * @systable:	system table
62 * Return:	status code
63 */
64efi_status_t EFIAPI efi_main(efi_handle_t handle,
65			     struct efi_system_table *systable)
66{
67	struct efi_simple_text_output_protocol *con_out = systable->con_out;
68	efi_status_t ret;
69	u16 text[] = EFI_ST_SUCCESS_STR;
70
71	con_out->output_string(con_out, u"EFI application calling Exit\n");
72
73	if (check_loaded_image_protocol(handle, systable) != EFI_SUCCESS) {
74		con_out->output_string(con_out,
75				       u"Loaded image protocol missing\n");
76		ret = EFI_NOT_FOUND;
77		goto out;
78	}
79
80	/* This return value is expected by the calling test */
81	ret = EFI_UNSUPPORTED;
82out:
83	systable->boottime->exit(handle, ret, sizeof(text), text);
84
85	/*
86	 * This statement should not be reached.
87	 * To enable testing use a different return value.
88	 */
89	return EFI_SUCCESS;
90}
91