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 
17 	if (IS_ENABLED(CONFIG_X86) ||
18 	    IS_ENABLED(CONFIG_EFI_EARLYCON) ||
19 	    IS_ENABLED(CONFIG_SYSFB))
20 		return (void *)&screen_info + screen_info_offset;
21 
22 	return NULL;
23 }
24 
25 /*
26  * EFI entry point for the generic EFI stub used by ARM, arm64, RISC-V and
27  * LoongArch. This is the entrypoint that is described in the PE/COFF header
28  * of the core kernel.
29  */
30 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
31 				   efi_system_table_t *systab)
32 {
33 	efi_loaded_image_t *image;
34 	efi_status_t status;
35 	unsigned long image_addr;
36 	unsigned long image_size = 0;
37 	/* addr/point and size pairs for memory management*/
38 	char *cmdline_ptr = NULL;
39 	efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
40 	unsigned long reserve_addr = 0;
41 	unsigned long reserve_size = 0;
42 
43 	WRITE_ONCE(efi_system_table, systab);
44 
45 	/* Check if we were booted by the EFI firmware */
46 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
47 		return EFI_INVALID_PARAMETER;
48 
49 	/*
50 	 * Get a handle to the loaded image protocol.  This is used to get
51 	 * information about the running image, such as size and the command
52 	 * line.
53 	 */
54 	status = efi_bs_call(handle_protocol, handle, &loaded_image_proto,
55 			     (void *)&image);
56 	if (status != EFI_SUCCESS) {
57 		efi_err("Failed to get loaded image protocol\n");
58 		return status;
59 	}
60 
61 	status = efi_handle_cmdline(image, &cmdline_ptr);
62 	if (status != EFI_SUCCESS)
63 		return status;
64 
65 	efi_info("Booting Linux Kernel...\n");
66 
67 	status = handle_kernel_image(&image_addr, &image_size,
68 				     &reserve_addr,
69 				     &reserve_size,
70 				     image, handle);
71 	if (status != EFI_SUCCESS) {
72 		efi_err("Failed to relocate kernel\n");
73 		return status;
74 	}
75 
76 	screen_info_offset = image_addr - (unsigned long)image->image_base;
77 
78 	status = efi_stub_common(handle, image, image_addr, cmdline_ptr);
79 
80 	efi_free(image_size, image_addr);
81 	efi_free(reserve_size, reserve_addr);
82 
83 	return status;
84 }
85