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