1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/efi.h>
4 #include <asm/efi.h>
5 
6 #include "efistub.h"
7 
8 /*
9  * EFI entry point for the generic EFI stub used by ARM, arm64, RISC-V and
10  * LoongArch. This is the entrypoint that is described in the PE/COFF header
11  * of the core kernel.
12  */
13 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
14 				   efi_system_table_t *systab)
15 {
16 	efi_loaded_image_t *image;
17 	efi_status_t status;
18 	unsigned long image_addr;
19 	unsigned long image_size = 0;
20 	/* addr/point and size pairs for memory management*/
21 	char *cmdline_ptr = NULL;
22 	efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
23 	unsigned long reserve_addr = 0;
24 	unsigned long reserve_size = 0;
25 
26 	WRITE_ONCE(efi_system_table, systab);
27 
28 	/* Check if we were booted by the EFI firmware */
29 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
30 		return EFI_INVALID_PARAMETER;
31 
32 	/*
33 	 * Get a handle to the loaded image protocol.  This is used to get
34 	 * information about the running image, such as size and the command
35 	 * line.
36 	 */
37 	status = efi_bs_call(handle_protocol, handle, &loaded_image_proto,
38 			     (void *)&image);
39 	if (status != EFI_SUCCESS) {
40 		efi_err("Failed to get loaded image protocol\n");
41 		return status;
42 	}
43 
44 	status = efi_handle_cmdline(image, &cmdline_ptr);
45 	if (status != EFI_SUCCESS)
46 		return status;
47 
48 	efi_info("Booting Linux Kernel...\n");
49 
50 	status = handle_kernel_image(&image_addr, &image_size,
51 				     &reserve_addr,
52 				     &reserve_size,
53 				     image, handle);
54 	if (status != EFI_SUCCESS) {
55 		efi_err("Failed to relocate kernel\n");
56 		return status;
57 	}
58 
59 	status = efi_stub_common(handle, image, image_addr, cmdline_ptr);
60 
61 	efi_free(image_size, image_addr);
62 	efi_free(reserve_size, reserve_addr);
63 
64 	return status;
65 }
66