Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) /* -----------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *   Copyright 2011 Intel Corporation; author Matt Fleming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * ----------------------------------------------------------------------- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <asm/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <asm/e820/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <asm/setup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <asm/desc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <asm/boot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "efistub.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /* Maximum physical address for 64-bit kernel with 4-level paging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define MAXMEM_X86_64_4LEVEL (1ull << 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) const efi_system_table_t *efi_system_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) extern u32 image_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static efi_loaded_image_t *image = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static efi_status_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct pci_setup_rom *rom = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	uint64_t romsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	void *romimage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	 * Some firmware images contain EFI function pointers at the place where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	 * the romimage and romsize fields are supposed to be. Typically the EFI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	 * code is mapped at high addresses, translating to an unrealistically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	 * large romsize. The UEFI spec limits the size of option ROMs to 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	 * MiB so we reject any ROMs over 16 MiB in size to catch this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	romimage = efi_table_attr(pci, romimage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	romsize = efi_table_attr(pci, romsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (!romimage || !romsize || romsize > SZ_16M)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		return EFI_INVALID_PARAMETER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	size = romsize + sizeof(*rom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			     (void **)&rom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		efi_err("Failed to allocate memory for 'rom'\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	memset(rom, 0, sizeof(*rom));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	rom->data.type	= SETUP_PCI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	rom->data.len	= size - sizeof(struct setup_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	rom->data.next	= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	rom->pcilen	= pci->romsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	*__rom = rom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 				PCI_VENDOR_ID, 1, &rom->vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		efi_err("Failed to read rom->vendor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		goto free_struct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 				PCI_DEVICE_ID, 1, &rom->devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		efi_err("Failed to read rom->devid\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		goto free_struct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 				&rom->device, &rom->function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		goto free_struct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	memcpy(rom->romdata, romimage, romsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) free_struct:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	efi_bs_call(free_pool, rom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * There's no way to return an informative status from this function,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * because any analysis (and printing of error messages) needs to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * done directly at the EFI function call-site.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  * just didn't find any PCI devices, but there's no way to tell outside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * the context of the call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static void setup_efi_pci(struct boot_params *params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	void **pci_handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	unsigned long size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct setup_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	efi_handle_t h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			     &pci_proto, NULL, &size, pci_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (status == EFI_BUFFER_TOO_SMALL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				     (void **)&pci_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			efi_err("Failed to allocate memory for 'pci_handle'\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				     &pci_proto, NULL, &size, pci_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		goto free_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	while (data && data->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		data = (struct setup_data *)(unsigned long)data->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	for_each_efi_handle(h, pci_handle, size, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		efi_pci_io_protocol_t *pci = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		struct pci_setup_rom *rom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		status = efi_bs_call(handle_protocol, h, &pci_proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 				     (void **)&pci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		if (status != EFI_SUCCESS || !pci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		status = preserve_pci_rom_image(pci, &rom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		if (data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			data->next = (unsigned long)rom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			params->hdr.setup_data = (unsigned long)rom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		data = (struct setup_data *)rom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) free_handle:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	efi_bs_call(free_pool, pci_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static void retrieve_apple_device_properties(struct boot_params *boot_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	struct setup_data *data, *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	u32 size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	apple_properties_protocol_t *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (efi_table_attr(p, version) != 0x10000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		efi_err("Unsupported properties proto version\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	efi_call_proto(p, get_all, NULL, &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 				     size + sizeof(struct setup_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				     (void **)&new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			efi_err("Failed to allocate memory for 'properties'\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		status = efi_call_proto(p, get_all, new->data, &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		if (status == EFI_BUFFER_TOO_SMALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			efi_bs_call(free_pool, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	} while (status == EFI_BUFFER_TOO_SMALL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	new->type = SETUP_APPLE_PROPERTIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	new->len  = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	new->next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		boot_params->hdr.setup_data = (unsigned long)new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		while (data->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			data = (struct setup_data *)(unsigned long)data->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		data->next = (unsigned long)new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static const efi_char16_t apple[] = L"Apple";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static void setup_quirks(struct boot_params *boot_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		efi_table_attr(efi_system_table, fw_vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (!memcmp(fw_vendor, apple, sizeof(apple))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			retrieve_apple_device_properties(boot_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  * See if we have Universal Graphics Adapter (UGA) protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static efi_status_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	u32 width, height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	void **uga_handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	efi_uga_draw_protocol_t *uga = NULL, *first_uga;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	efi_handle_t handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			     (void **)&uga_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			     uga_proto, NULL, &size, uga_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		goto free_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	height = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	width = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	first_uga = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	for_each_efi_handle(handle, uga_handle, size, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		u32 w, h, depth, refresh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		void *pciio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		status = efi_bs_call(handle_protocol, handle, uga_proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 				     (void **)&uga);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		pciio = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		if (status == EFI_SUCCESS && (!first_uga || pciio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			width = w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			height = h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			 * Once we've found a UGA supporting PCIIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			 * don't bother looking any further.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			if (pciio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			first_uga = uga;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (!width && !height)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		goto free_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	/* EFI framebuffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	si->orig_video_isVGA	= VIDEO_TYPE_EFI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	si->lfb_depth		= 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	si->lfb_width		= width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	si->lfb_height		= height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	si->red_size		= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	si->red_pos		= 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	si->green_size		= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	si->green_pos		= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	si->blue_size		= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	si->blue_pos		= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	si->rsvd_size		= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	si->rsvd_pos		= 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) free_handle:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	efi_bs_call(free_pool, uga_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static void setup_graphics(struct boot_params *boot_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	struct screen_info *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	void **gop_handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	void **uga_handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	si = &boot_params->screen_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	memset(si, 0, sizeof(*si));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			     &graphics_proto, NULL, &size, gop_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (status == EFI_BUFFER_TOO_SMALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		status = efi_setup_gop(si, &graphics_proto, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 				     &uga_proto, NULL, &size, uga_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		if (status == EFI_BUFFER_TOO_SMALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			setup_uga(si, &uga_proto, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	efi_bs_call(exit, handle, status, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	for(;;)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		asm("hlt");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) void startup_32(struct boot_params *boot_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) void __noreturn efi_stub_entry(efi_handle_t handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			       efi_system_table_t *sys_table_arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			       struct boot_params *boot_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)  * Because the x86 boot code expects to be passed a boot_params we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  * need to create one ourselves (usually the bootloader would create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)  * one for us).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 				   efi_system_table_t *sys_table_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	struct boot_params *boot_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	struct setup_header *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	void *image_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	int options_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	char *cmdline_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	efi_system_table = sys_table_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	/* Check if we were booted by the EFI firmware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		efi_exit(handle, EFI_INVALID_PARAMETER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		efi_exit(handle, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	image_base = efi_table_attr(image, image_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	image_offset = (void *)startup_32 - image_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	status = efi_allocate_pages(sizeof(struct boot_params),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 				    (unsigned long *)&boot_params, ULONG_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		efi_err("Failed to allocate lowmem for boot params\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		efi_exit(handle, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	memset(boot_params, 0x0, sizeof(struct boot_params));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	hdr = &boot_params->hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	/* Copy the setup header from the second sector to boot_params */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	memcpy(&hdr->jump, image_base + 512,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	       sizeof(struct setup_header) - offsetof(struct setup_header, jump));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	 * Fill out some of the header fields ourselves because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	 * EFI firmware loader doesn't load the first sector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	hdr->root_flags	= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	hdr->vid_mode	= 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	hdr->boot_flag	= 0xAA55;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	hdr->type_of_loader = 0x21;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	/* Convert unicode cmdline to ascii */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	cmdline_ptr = efi_convert_cmdline(image, &options_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (!cmdline_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	efi_set_u64_split((unsigned long)cmdline_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 			  &hdr->cmd_line_ptr, &boot_params->ext_cmd_line_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	hdr->ramdisk_image = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	hdr->ramdisk_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	efi_stub_entry(handle, sys_table_arg, boot_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	/* not reached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	efi_free(sizeof(struct boot_params), (unsigned long)boot_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	efi_exit(handle, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static void add_e820ext(struct boot_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			struct setup_data *e820ext, u32 nr_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	struct setup_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	e820ext->type = SETUP_E820_EXT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	e820ext->len  = nr_entries * sizeof(struct boot_e820_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	e820ext->next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	while (data && data->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		data = (struct setup_data *)(unsigned long)data->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	if (data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		data->next = (unsigned long)e820ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		params->hdr.setup_data = (unsigned long)e820ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static efi_status_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	struct boot_e820_entry *entry = params->e820_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	struct efi_info *efi = &params->efi_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	struct boot_e820_entry *prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	u32 nr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	u32 nr_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	nr_entries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	for (i = 0; i < nr_desc; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		efi_memory_desc_t *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		unsigned int e820_type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		unsigned long m = efi->efi_memmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) #ifdef CONFIG_X86_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		m |= (u64)efi->efi_memmap_hi << 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		switch (d->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		case EFI_RESERVED_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		case EFI_RUNTIME_SERVICES_CODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		case EFI_RUNTIME_SERVICES_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		case EFI_MEMORY_MAPPED_IO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		case EFI_PAL_CODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			e820_type = E820_TYPE_RESERVED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		case EFI_UNUSABLE_MEMORY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 			e820_type = E820_TYPE_UNUSABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		case EFI_ACPI_RECLAIM_MEMORY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			e820_type = E820_TYPE_ACPI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		case EFI_LOADER_CODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		case EFI_LOADER_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		case EFI_BOOT_SERVICES_CODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		case EFI_BOOT_SERVICES_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		case EFI_CONVENTIONAL_MEMORY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			if (efi_soft_reserve_enabled() &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 			    (d->attribute & EFI_MEMORY_SP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 				e820_type = E820_TYPE_SOFT_RESERVED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 				e820_type = E820_TYPE_RAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		case EFI_ACPI_MEMORY_NVS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			e820_type = E820_TYPE_NVS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		case EFI_PERSISTENT_MEMORY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 			e820_type = E820_TYPE_PMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		/* Merge adjacent mappings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		if (prev && prev->type == e820_type &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		    (prev->addr + prev->size) == d->phys_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 			prev->size += d->num_pages << 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		if (nr_entries == ARRAY_SIZE(params->e820_table)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 				   sizeof(struct setup_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 			if (!e820ext || e820ext_size < need)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 				return EFI_BUFFER_TOO_SMALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 			/* boot_params map full, switch to e820 extended */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			entry = (struct boot_e820_entry *)e820ext->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		entry->addr = d->phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		entry->size = d->num_pages << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		entry->type = e820_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		prev = entry++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		nr_entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	if (nr_entries > ARRAY_SIZE(params->e820_table)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		add_e820ext(params, e820ext, nr_e820ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		nr_entries -= nr_e820ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	params->e820_entries = (u8)nr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	return EFI_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 				  u32 *e820ext_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	size = sizeof(struct setup_data) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		sizeof(struct e820_entry) * nr_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	if (*e820ext) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		efi_bs_call(free_pool, *e820ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		*e820ext = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		*e820ext_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			     (void **)e820ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	if (status == EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		*e820ext_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) static efi_status_t allocate_e820(struct boot_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 				  struct setup_data **e820ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 				  u32 *e820ext_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	unsigned long map_size, desc_size, map_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	__u32 nr_desc, desc_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	/* Only need the size of the mem map and size of each mem descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	map_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	status = efi_bs_call(get_memory_map, &map_size, NULL, &map_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 			     &desc_size, &desc_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	if (status != EFI_BUFFER_TOO_SMALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		return (status != EFI_SUCCESS) ? status : EFI_UNSUPPORTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	nr_desc = map_size / desc_size + EFI_MMAP_NR_SLACK_SLOTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	if (nr_desc > ARRAY_SIZE(params->e820_table)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 			return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	return EFI_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) struct exit_boot_struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	struct boot_params	*boot_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	struct efi_info		*efi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 				   void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	const char *signature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	struct exit_boot_struct *p = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 				   : EFI32_LOADER_SIGNATURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	efi_set_u64_split((unsigned long)efi_system_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 			  &p->efi->efi_systab, &p->efi->efi_systab_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	p->efi->efi_memdesc_size	= *map->desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	p->efi->efi_memdesc_version	= *map->desc_ver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	efi_set_u64_split((unsigned long)*map->map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 			  &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	p->efi->efi_memmap_size		= *map->map_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	return EFI_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	unsigned long map_sz, key, desc_size, buff_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	efi_memory_desc_t *mem_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	struct setup_data *e820ext = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	__u32 e820ext_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	__u32 desc_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	struct efi_boot_memmap map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	struct exit_boot_struct priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	map.map			= &mem_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	map.map_size		= &map_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	map.desc_size		= &desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	map.desc_ver		= &desc_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	map.key_ptr		= &key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	map.buff_size		= &buff_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	priv.boot_params	= boot_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	priv.efi		= &boot_params->efi_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	status = allocate_e820(boot_params, &e820ext, &e820ext_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	/* Might as well exit boot services now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	/* Historic? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	boot_params->alt_mem_k	= 32 * 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	status = setup_e820(boot_params, e820ext, e820ext_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	return EFI_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)  * On success, we return the address of startup_32, which has potentially been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)  * relocated by efi_relocate_kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)  * On failure, we exit to the firmware via efi_exit instead of returning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) unsigned long efi_main(efi_handle_t handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 			     efi_system_table_t *sys_table_arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 			     struct boot_params *boot_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	unsigned long bzimage_addr = (unsigned long)startup_32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	unsigned long buffer_start, buffer_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	struct setup_header *hdr = &boot_params->hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	efi_system_table = sys_table_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	/* Check if we were booted by the EFI firmware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 		efi_exit(handle, EFI_INVALID_PARAMETER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	 * If the kernel isn't already loaded at a suitable address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	 * relocate it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	 * It must be loaded above LOAD_PHYSICAL_ADDR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	 * The maximum address for 64-bit is 1 << 46 for 4-level paging. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	 * is defined as the macro MAXMEM, but unfortunately that is not a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	 * compile-time constant if 5-level paging is configured, so we instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	 * define our own macro for use here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	 * For 32-bit, the maximum address is complicated to figure out, for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	 * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	 * KASLR uses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	 * Also relocate it if image_offset is zero, i.e. the kernel wasn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	 * loaded by LoadImage, but rather by a bootloader that called the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	 * handover entry. The reason we must always relocate in this case is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	 * to handle the case of systemd-boot booting a unified kernel image,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	 * which is a PE executable that contains the bzImage and an initrd as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	 * COFF sections. The initrd section is placed after the bzImage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	 * without ensuring that there are at least init_size bytes available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	 * for the bzImage, and thus the compressed kernel's startup code may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	 * overwrite the initrd unless it is moved out of the way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	buffer_start = ALIGN(bzimage_addr - image_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 			     hdr->kernel_alignment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	buffer_end = buffer_start + hdr->init_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	if ((buffer_start < LOAD_PHYSICAL_ADDR)				     ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	    (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE)    ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	    (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	    (image_offset == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 		status = efi_relocate_kernel(&bzimage_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 					     hdr->init_size, hdr->init_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 					     hdr->pref_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 					     hdr->kernel_alignment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 					     LOAD_PHYSICAL_ADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 		if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 			efi_err("efi_relocate_kernel() failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 		 * Now that we've copied the kernel elsewhere, we no longer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		 * have a set up block before startup_32(), so reset image_offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		 * to zero in case it was set earlier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 		image_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) #ifdef CONFIG_CMDLINE_BOOL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	status = efi_parse_options(CONFIG_CMDLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 		efi_err("Failed to parse options\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 		unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 					       ((u64)boot_params->ext_cmd_line_ptr << 32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 		status = efi_parse_options((char *)cmdline_paddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 		if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 			efi_err("Failed to parse options\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	 * At this point, an initrd may already have been loaded by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	 * bootloader and passed via bootparams. We permit an initrd loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	 * If the device path is not present, any command-line initrd=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	 * arguments will be processed only if image is not NULL, which will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	 * the case only if we were loaded via the PE entry point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	if (!efi_noinitrd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 		unsigned long addr, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 		status = efi_load_initrd(image, &addr, &size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 					 hdr->initrd_addr_max, ULONG_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 		if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 			efi_err("Failed to load initrd!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 		if (size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 			efi_set_u64_split(addr, &hdr->ramdisk_image,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 					  &boot_params->ext_ramdisk_image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 			efi_set_u64_split(size, &hdr->ramdisk_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 					  &boot_params->ext_ramdisk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	 * If the boot loader gave us a value for secure_boot then we use that,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	 * otherwise we ask the BIOS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	if (boot_params->secure_boot == efi_secureboot_mode_unset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 		boot_params->secure_boot = efi_get_secureboot();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	/* Ask the firmware to clear memory on unclean shutdown */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	efi_enable_reset_attack_mitigation();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	efi_random_get_seed();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	efi_retrieve_tpm2_eventlog();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	setup_graphics(boot_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	setup_efi_pci(boot_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	setup_quirks(boot_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	status = exit_boot(boot_params, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 		efi_err("exit_boot() failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	return bzimage_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	efi_err("efi_main() failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	efi_exit(handle, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) }