Thomas Gleixner | 97873a3 | 2019-06-04 10:11:30 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Eric Snowberg | b84a64f | 2018-11-29 18:12:20 +0100 | [diff] [blame] | 2 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 3 | /* ----------------------------------------------------------------------- |
| 4 | * |
| 5 | * Copyright 2011 Intel Corporation; author Matt Fleming |
| 6 | * |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 7 | * ----------------------------------------------------------------------- */ |
| 8 | |
| 9 | #include <linux/efi.h> |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 10 | #include <linux/pci.h> |
Ingo Molnar | 5520b7e | 2017-01-27 11:59:46 +0100 | [diff] [blame] | 11 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 12 | #include <asm/efi.h> |
Ingo Molnar | 5520b7e | 2017-01-27 11:59:46 +0100 | [diff] [blame] | 13 | #include <asm/e820/types.h> |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 14 | #include <asm/setup.h> |
| 15 | #include <asm/desc.h> |
Kairui Song | 220dd76 | 2019-10-29 18:37:54 +0100 | [diff] [blame] | 16 | #include <asm/boot.h> |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 17 | |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 18 | #include "../string.h" |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 19 | #include "eboot.h" |
| 20 | |
| 21 | static efi_system_table_t *sys_table; |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 22 | static bool efi_is64 = IS_ENABLED(CONFIG_X86_64); |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 23 | |
Ard Biesheuvel | 2fcdad2 | 2019-12-24 16:10:15 +0100 | [diff] [blame] | 24 | __pure efi_system_table_t *efi_system_table(void) |
| 25 | { |
| 26 | return sys_table; |
| 27 | } |
| 28 | |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 29 | __pure bool efi_is_64bit(void) |
| 30 | { |
| 31 | return efi_is64; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 32 | } |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 33 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 34 | static efi_status_t |
Ard Biesheuvel | 75c5a71 | 2018-07-20 10:47:19 +0900 | [diff] [blame] | 35 | preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom) |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 36 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 37 | struct pci_setup_rom *rom = NULL; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 38 | efi_status_t status; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 39 | unsigned long size; |
Ard Biesheuvel | e296701 | 2018-07-11 11:02:35 +0200 | [diff] [blame] | 40 | uint64_t romsize; |
Ard Biesheuvel | 2c3625c | 2018-05-04 08:00:00 +0200 | [diff] [blame] | 41 | void *romimage; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 42 | |
Hans de Goede | 1de3a1b | 2018-05-04 08:00:01 +0200 | [diff] [blame] | 43 | /* |
Ard Biesheuvel | e296701 | 2018-07-11 11:02:35 +0200 | [diff] [blame] | 44 | * Some firmware images contain EFI function pointers at the place where |
| 45 | * the romimage and romsize fields are supposed to be. Typically the EFI |
Hans de Goede | 1de3a1b | 2018-05-04 08:00:01 +0200 | [diff] [blame] | 46 | * code is mapped at high addresses, translating to an unrealistically |
| 47 | * large romsize. The UEFI spec limits the size of option ROMs to 16 |
| 48 | * MiB so we reject any ROMs over 16 MiB in size to catch this. |
| 49 | */ |
Ard Biesheuvel | 99ea8b1 | 2019-12-24 16:10:22 +0100 | [diff] [blame] | 50 | romimage = efi_table_attr(pci, romimage); |
| 51 | romsize = efi_table_attr(pci, romsize); |
Hans de Goede | 1de3a1b | 2018-05-04 08:00:01 +0200 | [diff] [blame] | 52 | if (!romimage || !romsize || romsize > SZ_16M) |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 53 | return EFI_INVALID_PARAMETER; |
| 54 | |
Ard Biesheuvel | 2c3625c | 2018-05-04 08:00:00 +0200 | [diff] [blame] | 55 | size = romsize + sizeof(*rom); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 56 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 57 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, |
| 58 | (void **)&rom); |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 59 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 60 | efi_printk("Failed to allocate memory for 'rom'\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 61 | return status; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 62 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 63 | |
| 64 | memset(rom, 0, sizeof(*rom)); |
| 65 | |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 66 | rom->data.type = SETUP_PCI; |
| 67 | rom->data.len = size - sizeof(struct setup_data); |
| 68 | rom->data.next = 0; |
| 69 | rom->pcilen = pci->romsize; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 70 | *__rom = rom; |
| 71 | |
Ard Biesheuvel | 47c0fd3 | 2019-12-24 16:10:21 +0100 | [diff] [blame] | 72 | status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16, |
| 73 | PCI_VENDOR_ID, 1, &rom->vendor); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 74 | |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 75 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 76 | efi_printk("Failed to read rom->vendor\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 77 | goto free_struct; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 78 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 79 | |
Ard Biesheuvel | 47c0fd3 | 2019-12-24 16:10:21 +0100 | [diff] [blame] | 80 | status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16, |
| 81 | PCI_DEVICE_ID, 1, &rom->devid); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 82 | |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 83 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 84 | efi_printk("Failed to read rom->devid\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 85 | goto free_struct; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 86 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 87 | |
Ard Biesheuvel | 47c0fd3 | 2019-12-24 16:10:21 +0100 | [diff] [blame] | 88 | status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus, |
| 89 | &rom->device, &rom->function); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 90 | |
| 91 | if (status != EFI_SUCCESS) |
| 92 | goto free_struct; |
| 93 | |
Ard Biesheuvel | 2c3625c | 2018-05-04 08:00:00 +0200 | [diff] [blame] | 94 | memcpy(rom->romdata, romimage, romsize); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 95 | return status; |
| 96 | |
| 97 | free_struct: |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 98 | efi_bs_call(free_pool, rom); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 99 | return status; |
| 100 | } |
| 101 | |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 102 | /* |
| 103 | * There's no way to return an informative status from this function, |
| 104 | * because any analysis (and printing of error messages) needs to be |
| 105 | * done directly at the EFI function call-site. |
| 106 | * |
| 107 | * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we |
| 108 | * just didn't find any PCI devices, but there's no way to tell outside |
| 109 | * the context of the call. |
| 110 | */ |
| 111 | static void setup_efi_pci(struct boot_params *params) |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 112 | { |
| 113 | efi_status_t status; |
| 114 | void **pci_handle = NULL; |
| 115 | efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; |
| 116 | unsigned long size = 0; |
Ard Biesheuvel | 75c5a71 | 2018-07-20 10:47:19 +0900 | [diff] [blame] | 117 | unsigned long nr_pci; |
| 118 | struct setup_data *data; |
Ard Biesheuvel | 2732ea0 | 2019-12-24 16:10:07 +0100 | [diff] [blame] | 119 | efi_handle_t h; |
Ard Biesheuvel | 75c5a71 | 2018-07-20 10:47:19 +0900 | [diff] [blame] | 120 | int i; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 121 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 122 | status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, |
| 123 | &pci_proto, NULL, &size, pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 124 | |
| 125 | if (status == EFI_BUFFER_TOO_SMALL) { |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 126 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, |
| 127 | (void **)&pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 128 | |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 129 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 130 | efi_printk("Failed to allocate memory for 'pci_handle'\n"); |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 131 | return; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 132 | } |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 133 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 134 | status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, |
| 135 | &pci_proto, NULL, &size, pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | if (status != EFI_SUCCESS) |
| 139 | goto free_handle; |
| 140 | |
Ard Biesheuvel | 75c5a71 | 2018-07-20 10:47:19 +0900 | [diff] [blame] | 141 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
| 142 | |
| 143 | while (data && data->next) |
| 144 | data = (struct setup_data *)(unsigned long)data->next; |
| 145 | |
Ard Biesheuvel | 2732ea0 | 2019-12-24 16:10:07 +0100 | [diff] [blame] | 146 | for_each_efi_handle(h, pci_handle, size, i) { |
Ard Biesheuvel | 75c5a71 | 2018-07-20 10:47:19 +0900 | [diff] [blame] | 147 | efi_pci_io_protocol_t *pci = NULL; |
| 148 | struct pci_setup_rom *rom; |
| 149 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 150 | status = efi_bs_call(handle_protocol, h, &pci_proto, |
| 151 | (void **)&pci); |
Ard Biesheuvel | 75c5a71 | 2018-07-20 10:47:19 +0900 | [diff] [blame] | 152 | if (status != EFI_SUCCESS || !pci) |
| 153 | continue; |
| 154 | |
| 155 | status = preserve_pci_rom_image(pci, &rom); |
| 156 | if (status != EFI_SUCCESS) |
| 157 | continue; |
| 158 | |
| 159 | if (data) |
| 160 | data->next = (unsigned long)rom; |
| 161 | else |
| 162 | params->hdr.setup_data = (unsigned long)rom; |
| 163 | |
| 164 | data = (struct setup_data *)rom; |
| 165 | } |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 166 | |
| 167 | free_handle: |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 168 | efi_bs_call(free_pool, pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 171 | static void retrieve_apple_device_properties(struct boot_params *boot_params) |
| 172 | { |
| 173 | efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID; |
| 174 | struct setup_data *data, *new; |
| 175 | efi_status_t status; |
| 176 | u32 size = 0; |
Ard Biesheuvel | 960a8d0 | 2019-12-24 16:10:11 +0100 | [diff] [blame] | 177 | apple_properties_protocol_t *p; |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 178 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 179 | status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p); |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 180 | if (status != EFI_SUCCESS) |
| 181 | return; |
| 182 | |
Ard Biesheuvel | 99ea8b1 | 2019-12-24 16:10:22 +0100 | [diff] [blame] | 183 | if (efi_table_attr(p, version) != 0x10000) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 184 | efi_printk("Unsupported properties proto version\n"); |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
Ard Biesheuvel | 47c0fd3 | 2019-12-24 16:10:21 +0100 | [diff] [blame] | 188 | efi_call_proto(p, get_all, NULL, &size); |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 189 | if (!size) |
| 190 | return; |
| 191 | |
| 192 | do { |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 193 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, |
| 194 | size + sizeof(struct setup_data), |
| 195 | (void **)&new); |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 196 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 197 | efi_printk("Failed to allocate memory for 'properties'\n"); |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | |
Ard Biesheuvel | 47c0fd3 | 2019-12-24 16:10:21 +0100 | [diff] [blame] | 201 | status = efi_call_proto(p, get_all, new->data, &size); |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 202 | |
| 203 | if (status == EFI_BUFFER_TOO_SMALL) |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 204 | efi_bs_call(free_pool, new); |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 205 | } while (status == EFI_BUFFER_TOO_SMALL); |
| 206 | |
| 207 | new->type = SETUP_APPLE_PROPERTIES; |
| 208 | new->len = size; |
| 209 | new->next = 0; |
| 210 | |
| 211 | data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data; |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 212 | if (!data) { |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 213 | boot_params->hdr.setup_data = (unsigned long)new; |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 214 | } else { |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 215 | while (data->next) |
| 216 | data = (struct setup_data *)(unsigned long)data->next; |
| 217 | data->next = (unsigned long)new; |
| 218 | } |
| 219 | } |
| 220 | |
Ard Biesheuvel | 36b6497 | 2018-03-12 08:45:00 +0000 | [diff] [blame] | 221 | static const efi_char16_t apple[] = L"Apple"; |
| 222 | |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 223 | static void setup_quirks(struct boot_params *boot_params) |
| 224 | { |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 225 | efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long) |
Ard Biesheuvel | 99ea8b1 | 2019-12-24 16:10:22 +0100 | [diff] [blame] | 226 | efi_table_attr(efi_system_table(), fw_vendor); |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 227 | |
| 228 | if (!memcmp(fw_vendor, apple, sizeof(apple))) { |
| 229 | if (IS_ENABLED(CONFIG_APPLE_PROPERTIES)) |
| 230 | retrieve_apple_device_properties(boot_params); |
| 231 | } |
| 232 | } |
| 233 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 234 | /* |
| 235 | * See if we have Universal Graphics Adapter (UGA) protocol |
| 236 | */ |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 237 | static efi_status_t |
| 238 | setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size) |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 239 | { |
| 240 | efi_status_t status; |
| 241 | u32 width, height; |
| 242 | void **uga_handle = NULL; |
Ard Biesheuvel | 290084c | 2018-07-20 10:47:21 +0900 | [diff] [blame] | 243 | efi_uga_draw_protocol_t *uga = NULL, *first_uga; |
| 244 | unsigned long nr_ugas; |
Ard Biesheuvel | 2732ea0 | 2019-12-24 16:10:07 +0100 | [diff] [blame] | 245 | efi_handle_t handle; |
Ard Biesheuvel | 290084c | 2018-07-20 10:47:21 +0900 | [diff] [blame] | 246 | int i; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 247 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 248 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, |
| 249 | (void **)&uga_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 250 | if (status != EFI_SUCCESS) |
| 251 | return status; |
| 252 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 253 | status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, |
| 254 | uga_proto, NULL, &size, uga_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 255 | if (status != EFI_SUCCESS) |
| 256 | goto free_handle; |
| 257 | |
| 258 | height = 0; |
| 259 | width = 0; |
| 260 | |
Ard Biesheuvel | 290084c | 2018-07-20 10:47:21 +0900 | [diff] [blame] | 261 | first_uga = NULL; |
Ard Biesheuvel | 2732ea0 | 2019-12-24 16:10:07 +0100 | [diff] [blame] | 262 | for_each_efi_handle(handle, uga_handle, size, i) { |
Ard Biesheuvel | 290084c | 2018-07-20 10:47:21 +0900 | [diff] [blame] | 263 | efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; |
| 264 | u32 w, h, depth, refresh; |
| 265 | void *pciio; |
Ard Biesheuvel | 290084c | 2018-07-20 10:47:21 +0900 | [diff] [blame] | 266 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 267 | status = efi_bs_call(handle_protocol, handle, uga_proto, |
| 268 | (void **)&uga); |
Ard Biesheuvel | 290084c | 2018-07-20 10:47:21 +0900 | [diff] [blame] | 269 | if (status != EFI_SUCCESS) |
| 270 | continue; |
| 271 | |
Ard Biesheuvel | 093174f | 2018-07-20 10:47:22 +0900 | [diff] [blame] | 272 | pciio = NULL; |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 273 | efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio); |
Ard Biesheuvel | 290084c | 2018-07-20 10:47:21 +0900 | [diff] [blame] | 274 | |
Ard Biesheuvel | 47c0fd3 | 2019-12-24 16:10:21 +0100 | [diff] [blame] | 275 | status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh); |
Ard Biesheuvel | 290084c | 2018-07-20 10:47:21 +0900 | [diff] [blame] | 276 | if (status == EFI_SUCCESS && (!first_uga || pciio)) { |
| 277 | width = w; |
| 278 | height = h; |
| 279 | |
| 280 | /* |
| 281 | * Once we've found a UGA supporting PCIIO, |
| 282 | * don't bother looking any further. |
| 283 | */ |
| 284 | if (pciio) |
| 285 | break; |
| 286 | |
| 287 | first_uga = uga; |
| 288 | } |
| 289 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 290 | |
| 291 | if (!width && !height) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 292 | goto free_handle; |
| 293 | |
| 294 | /* EFI framebuffer */ |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 295 | si->orig_video_isVGA = VIDEO_TYPE_EFI; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 296 | |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 297 | si->lfb_depth = 32; |
| 298 | si->lfb_width = width; |
| 299 | si->lfb_height = height; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 300 | |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 301 | si->red_size = 8; |
| 302 | si->red_pos = 16; |
| 303 | si->green_size = 8; |
| 304 | si->green_pos = 8; |
| 305 | si->blue_size = 8; |
| 306 | si->blue_pos = 0; |
| 307 | si->rsvd_size = 8; |
| 308 | si->rsvd_pos = 24; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 309 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 310 | free_handle: |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 311 | efi_bs_call(free_pool, uga_handle); |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 312 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 313 | return status; |
| 314 | } |
| 315 | |
| 316 | void setup_graphics(struct boot_params *boot_params) |
| 317 | { |
| 318 | efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; |
| 319 | struct screen_info *si; |
| 320 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; |
| 321 | efi_status_t status; |
| 322 | unsigned long size; |
| 323 | void **gop_handle = NULL; |
| 324 | void **uga_handle = NULL; |
| 325 | |
| 326 | si = &boot_params->screen_info; |
| 327 | memset(si, 0, sizeof(*si)); |
| 328 | |
| 329 | size = 0; |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 330 | status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, |
| 331 | &graphics_proto, NULL, &size, gop_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 332 | if (status == EFI_BUFFER_TOO_SMALL) |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 333 | status = efi_setup_gop(si, &graphics_proto, size); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 334 | |
| 335 | if (status != EFI_SUCCESS) { |
| 336 | size = 0; |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 337 | status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, |
| 338 | &uga_proto, NULL, &size, uga_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 339 | if (status == EFI_BUFFER_TOO_SMALL) |
| 340 | setup_uga(si, &uga_proto, size); |
| 341 | } |
| 342 | } |
| 343 | |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 344 | void startup_32(struct boot_params *boot_params); |
| 345 | |
| 346 | void __noreturn efi_stub_entry(efi_handle_t handle, |
| 347 | efi_system_table_t *sys_table_arg, |
| 348 | struct boot_params *boot_params); |
| 349 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 350 | /* |
| 351 | * Because the x86 boot code expects to be passed a boot_params we |
| 352 | * need to create one ourselves (usually the bootloader would create |
| 353 | * one for us). |
| 354 | */ |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 355 | efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, |
| 356 | efi_system_table_t *sys_table_arg) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 357 | { |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 358 | struct boot_params *boot_params; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 359 | struct apm_bios_info *bi; |
| 360 | struct setup_header *hdr; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 361 | efi_loaded_image_t *image; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 362 | efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 363 | int options_size = 0; |
| 364 | efi_status_t status; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 365 | char *cmdline_ptr; |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 366 | unsigned long ramdisk_addr; |
| 367 | unsigned long ramdisk_size; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 368 | |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 369 | sys_table = sys_table_arg; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 370 | |
| 371 | /* Check if we were booted by the EFI firmware */ |
| 372 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 373 | return EFI_INVALID_PARAMETER; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 374 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 375 | status = efi_bs_call(handle_protocol, handle, &proto, (void *)&image); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 376 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 377 | efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n"); |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 378 | return status; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 379 | } |
| 380 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 381 | status = efi_low_alloc(0x4000, 1, (unsigned long *)&boot_params); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 382 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 383 | efi_printk("Failed to allocate lowmem for boot params\n"); |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 384 | return status; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | memset(boot_params, 0x0, 0x4000); |
| 388 | |
| 389 | hdr = &boot_params->hdr; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 390 | bi = &boot_params->apm_bios_info; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 391 | |
| 392 | /* Copy the second sector to boot_params */ |
| 393 | memcpy(&hdr->jump, image->image_base + 512, 512); |
| 394 | |
| 395 | /* |
| 396 | * Fill out some of the header fields ourselves because the |
| 397 | * EFI firmware loader doesn't load the first sector. |
| 398 | */ |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 399 | hdr->root_flags = 1; |
| 400 | hdr->vid_mode = 0xffff; |
| 401 | hdr->boot_flag = 0xAA55; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 402 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 403 | hdr->type_of_loader = 0x21; |
| 404 | |
| 405 | /* Convert unicode cmdline to ascii */ |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 406 | cmdline_ptr = efi_convert_cmdline(image, &options_size); |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 407 | if (!cmdline_ptr) |
| 408 | goto fail; |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 409 | |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 410 | hdr->cmd_line_ptr = (unsigned long)cmdline_ptr; |
Roy Franz | 98b228f | 2015-04-15 16:32:24 -0700 | [diff] [blame] | 411 | /* Fill in upper bits of command line address, NOP on 32 bit */ |
| 412 | boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 413 | |
| 414 | hdr->ramdisk_image = 0; |
| 415 | hdr->ramdisk_size = 0; |
| 416 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 417 | /* Clear APM BIOS info */ |
| 418 | memset(bi, 0, sizeof(*bi)); |
| 419 | |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 420 | status = efi_parse_options(cmdline_ptr); |
| 421 | if (status != EFI_SUCCESS) |
| 422 | goto fail2; |
| 423 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 424 | status = handle_cmdline_files(image, |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 425 | (char *)(unsigned long)hdr->cmd_line_ptr, |
Yinghai Lu | 47226ad | 2014-09-03 21:50:07 -0700 | [diff] [blame] | 426 | "initrd=", hdr->initrd_addr_max, |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 427 | &ramdisk_addr, &ramdisk_size); |
Yinghai Lu | 47226ad | 2014-09-03 21:50:07 -0700 | [diff] [blame] | 428 | |
| 429 | if (status != EFI_SUCCESS && |
| 430 | hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 431 | efi_printk("Trying to load files to higher address\n"); |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 432 | status = handle_cmdline_files(image, |
Yinghai Lu | 47226ad | 2014-09-03 21:50:07 -0700 | [diff] [blame] | 433 | (char *)(unsigned long)hdr->cmd_line_ptr, |
| 434 | "initrd=", -1UL, |
| 435 | &ramdisk_addr, &ramdisk_size); |
| 436 | } |
| 437 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 438 | if (status != EFI_SUCCESS) |
| 439 | goto fail2; |
Yinghai Lu | 4bf7111 | 2014-06-14 12:23:41 -0700 | [diff] [blame] | 440 | hdr->ramdisk_image = ramdisk_addr & 0xffffffff; |
| 441 | hdr->ramdisk_size = ramdisk_size & 0xffffffff; |
| 442 | boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32; |
| 443 | boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 444 | |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 445 | hdr->code32_start = (u32)(unsigned long)startup_32; |
| 446 | |
| 447 | efi_stub_entry(handle, sys_table, boot_params); |
| 448 | /* not reached */ |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 449 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 450 | fail2: |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 451 | efi_free(options_size, hdr->cmd_line_ptr); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 452 | fail: |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 453 | efi_free(0x4000, (unsigned long)boot_params); |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 454 | |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 455 | return status; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 458 | static void add_e820ext(struct boot_params *params, |
| 459 | struct setup_data *e820ext, u32 nr_entries) |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 460 | { |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 461 | struct setup_data *data; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 462 | |
| 463 | e820ext->type = SETUP_E820_EXT; |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 464 | e820ext->len = nr_entries * sizeof(struct boot_e820_entry); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 465 | e820ext->next = 0; |
| 466 | |
| 467 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
| 468 | |
| 469 | while (data && data->next) |
| 470 | data = (struct setup_data *)(unsigned long)data->next; |
| 471 | |
| 472 | if (data) |
| 473 | data->next = (unsigned long)e820ext; |
| 474 | else |
| 475 | params->hdr.setup_data = (unsigned long)e820ext; |
| 476 | } |
| 477 | |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 478 | static efi_status_t |
| 479 | setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size) |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 480 | { |
Ingo Molnar | 7410aa1 | 2017-01-29 12:56:13 +0100 | [diff] [blame] | 481 | struct boot_e820_entry *entry = params->e820_table; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 482 | struct efi_info *efi = ¶ms->efi_info; |
Ingo Molnar | 7410aa1 | 2017-01-29 12:56:13 +0100 | [diff] [blame] | 483 | struct boot_e820_entry *prev = NULL; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 484 | u32 nr_entries; |
| 485 | u32 nr_desc; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 486 | int i; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 487 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 488 | nr_entries = 0; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 489 | nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size; |
| 490 | |
| 491 | for (i = 0; i < nr_desc; i++) { |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 492 | efi_memory_desc_t *d; |
| 493 | unsigned int e820_type = 0; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 494 | unsigned long m = efi->efi_memmap; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 495 | |
Dmitry Skorodumov | 7cc03e4 | 2015-07-28 18:38:32 +0400 | [diff] [blame] | 496 | #ifdef CONFIG_X86_64 |
| 497 | m |= (u64)efi->efi_memmap_hi << 32; |
| 498 | #endif |
| 499 | |
Baoquan He | 02e43c2 | 2017-08-16 21:46:51 +0800 | [diff] [blame] | 500 | d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 501 | switch (d->type) { |
| 502 | case EFI_RESERVED_TYPE: |
| 503 | case EFI_RUNTIME_SERVICES_CODE: |
| 504 | case EFI_RUNTIME_SERVICES_DATA: |
| 505 | case EFI_MEMORY_MAPPED_IO: |
| 506 | case EFI_MEMORY_MAPPED_IO_PORT_SPACE: |
| 507 | case EFI_PAL_CODE: |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 508 | e820_type = E820_TYPE_RESERVED; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 509 | break; |
| 510 | |
| 511 | case EFI_UNUSABLE_MEMORY: |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 512 | e820_type = E820_TYPE_UNUSABLE; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 513 | break; |
| 514 | |
| 515 | case EFI_ACPI_RECLAIM_MEMORY: |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 516 | e820_type = E820_TYPE_ACPI; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 517 | break; |
| 518 | |
| 519 | case EFI_LOADER_CODE: |
| 520 | case EFI_LOADER_DATA: |
| 521 | case EFI_BOOT_SERVICES_CODE: |
| 522 | case EFI_BOOT_SERVICES_DATA: |
| 523 | case EFI_CONVENTIONAL_MEMORY: |
Dan Williams | 262b45a | 2019-11-06 17:43:16 -0800 | [diff] [blame] | 524 | if (efi_soft_reserve_enabled() && |
| 525 | (d->attribute & EFI_MEMORY_SP)) |
| 526 | e820_type = E820_TYPE_SOFT_RESERVED; |
| 527 | else |
| 528 | e820_type = E820_TYPE_RAM; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 529 | break; |
| 530 | |
| 531 | case EFI_ACPI_MEMORY_NVS: |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 532 | e820_type = E820_TYPE_NVS; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 533 | break; |
| 534 | |
Dan Williams | ad5fb87 | 2015-04-03 12:05:28 -0400 | [diff] [blame] | 535 | case EFI_PERSISTENT_MEMORY: |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 536 | e820_type = E820_TYPE_PMEM; |
Dan Williams | ad5fb87 | 2015-04-03 12:05:28 -0400 | [diff] [blame] | 537 | break; |
| 538 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 539 | default: |
| 540 | continue; |
| 541 | } |
| 542 | |
| 543 | /* Merge adjacent mappings */ |
| 544 | if (prev && prev->type == e820_type && |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 545 | (prev->addr + prev->size) == d->phys_addr) { |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 546 | prev->size += d->num_pages << 12; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 547 | continue; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 548 | } |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 549 | |
Ingo Molnar | 61a5010 | 2017-01-27 13:54:38 +0100 | [diff] [blame] | 550 | if (nr_entries == ARRAY_SIZE(params->e820_table)) { |
Ingo Molnar | 8ec67d9 | 2017-01-27 12:54:38 +0100 | [diff] [blame] | 551 | u32 need = (nr_desc - i) * sizeof(struct e820_entry) + |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 552 | sizeof(struct setup_data); |
| 553 | |
| 554 | if (!e820ext || e820ext_size < need) |
| 555 | return EFI_BUFFER_TOO_SMALL; |
| 556 | |
| 557 | /* boot_params map full, switch to e820 extended */ |
Ingo Molnar | 7410aa1 | 2017-01-29 12:56:13 +0100 | [diff] [blame] | 558 | entry = (struct boot_e820_entry *)e820ext->data; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 559 | } |
| 560 | |
Ingo Molnar | 7410aa1 | 2017-01-29 12:56:13 +0100 | [diff] [blame] | 561 | entry->addr = d->phys_addr; |
| 562 | entry->size = d->num_pages << PAGE_SHIFT; |
| 563 | entry->type = e820_type; |
| 564 | prev = entry++; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 565 | nr_entries++; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Ingo Molnar | 61a5010 | 2017-01-27 13:54:38 +0100 | [diff] [blame] | 568 | if (nr_entries > ARRAY_SIZE(params->e820_table)) { |
| 569 | u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 570 | |
| 571 | add_e820ext(params, e820ext, nr_e820ext); |
| 572 | nr_entries -= nr_e820ext; |
| 573 | } |
| 574 | |
| 575 | params->e820_entries = (u8)nr_entries; |
| 576 | |
| 577 | return EFI_SUCCESS; |
| 578 | } |
| 579 | |
| 580 | static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, |
| 581 | u32 *e820ext_size) |
| 582 | { |
| 583 | efi_status_t status; |
| 584 | unsigned long size; |
| 585 | |
| 586 | size = sizeof(struct setup_data) + |
Ingo Molnar | 8ec67d9 | 2017-01-27 12:54:38 +0100 | [diff] [blame] | 587 | sizeof(struct e820_entry) * nr_desc; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 588 | |
| 589 | if (*e820ext) { |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 590 | efi_bs_call(free_pool, *e820ext); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 591 | *e820ext = NULL; |
| 592 | *e820ext_size = 0; |
| 593 | } |
| 594 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 595 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, |
| 596 | (void **)e820ext); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 597 | if (status == EFI_SUCCESS) |
| 598 | *e820ext_size = size; |
| 599 | |
| 600 | return status; |
| 601 | } |
| 602 | |
Eric Snowberg | b84a64f | 2018-11-29 18:12:20 +0100 | [diff] [blame] | 603 | static efi_status_t allocate_e820(struct boot_params *params, |
| 604 | struct setup_data **e820ext, |
| 605 | u32 *e820ext_size) |
| 606 | { |
| 607 | unsigned long map_size, desc_size, buff_size; |
| 608 | struct efi_boot_memmap boot_map; |
| 609 | efi_memory_desc_t *map; |
| 610 | efi_status_t status; |
| 611 | __u32 nr_desc; |
| 612 | |
| 613 | boot_map.map = ↦ |
| 614 | boot_map.map_size = &map_size; |
| 615 | boot_map.desc_size = &desc_size; |
| 616 | boot_map.desc_ver = NULL; |
| 617 | boot_map.key_ptr = NULL; |
| 618 | boot_map.buff_size = &buff_size; |
| 619 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 620 | status = efi_get_memory_map(&boot_map); |
Eric Snowberg | b84a64f | 2018-11-29 18:12:20 +0100 | [diff] [blame] | 621 | if (status != EFI_SUCCESS) |
| 622 | return status; |
| 623 | |
| 624 | nr_desc = buff_size / desc_size; |
| 625 | |
| 626 | if (nr_desc > ARRAY_SIZE(params->e820_table)) { |
| 627 | u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table); |
| 628 | |
| 629 | status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size); |
| 630 | if (status != EFI_SUCCESS) |
| 631 | return status; |
| 632 | } |
| 633 | |
| 634 | return EFI_SUCCESS; |
| 635 | } |
| 636 | |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 637 | struct exit_boot_struct { |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 638 | struct boot_params *boot_params; |
| 639 | struct efi_info *efi; |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 640 | }; |
| 641 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 642 | static efi_status_t exit_boot_func(struct efi_boot_memmap *map, |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 643 | void *priv) |
| 644 | { |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 645 | const char *signature; |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 646 | struct exit_boot_struct *p = priv; |
| 647 | |
Ard Biesheuvel | aab9593 | 2018-07-20 10:47:24 +0900 | [diff] [blame] | 648 | signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE |
| 649 | : EFI32_LOADER_SIGNATURE; |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 650 | memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32)); |
| 651 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 652 | p->efi->efi_systab = (unsigned long)efi_system_table(); |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 653 | p->efi->efi_memdesc_size = *map->desc_size; |
| 654 | p->efi->efi_memdesc_version = *map->desc_ver; |
| 655 | p->efi->efi_memmap = (unsigned long)*map->map; |
| 656 | p->efi->efi_memmap_size = *map->map_size; |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 657 | |
| 658 | #ifdef CONFIG_X86_64 |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 659 | p->efi->efi_systab_hi = (unsigned long)efi_system_table() >> 32; |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 660 | p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32; |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 661 | #endif |
| 662 | |
| 663 | return EFI_SUCCESS; |
| 664 | } |
| 665 | |
Ard Biesheuvel | aab9593 | 2018-07-20 10:47:24 +0900 | [diff] [blame] | 666 | static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 667 | { |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 668 | unsigned long map_sz, key, desc_size, buff_size; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 669 | efi_memory_desc_t *mem_map; |
Eric Snowberg | b84a64f | 2018-11-29 18:12:20 +0100 | [diff] [blame] | 670 | struct setup_data *e820ext = NULL; |
| 671 | __u32 e820ext_size = 0; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 672 | efi_status_t status; |
| 673 | __u32 desc_version; |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 674 | struct efi_boot_memmap map; |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 675 | struct exit_boot_struct priv; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 676 | |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 677 | map.map = &mem_map; |
| 678 | map.map_size = &map_sz; |
| 679 | map.desc_size = &desc_size; |
| 680 | map.desc_ver = &desc_version; |
| 681 | map.key_ptr = &key; |
| 682 | map.buff_size = &buff_size; |
| 683 | priv.boot_params = boot_params; |
| 684 | priv.efi = &boot_params->efi_info; |
Eric Snowberg | b84a64f | 2018-11-29 18:12:20 +0100 | [diff] [blame] | 685 | |
| 686 | status = allocate_e820(boot_params, &e820ext, &e820ext_size); |
| 687 | if (status != EFI_SUCCESS) |
| 688 | return status; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 689 | |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 690 | /* Might as well exit boot services now */ |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 691 | status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 692 | if (status != EFI_SUCCESS) |
| 693 | return status; |
| 694 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 695 | /* Historic? */ |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 696 | boot_params->alt_mem_k = 32 * 1024; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 697 | |
| 698 | status = setup_e820(boot_params, e820ext, e820ext_size); |
| 699 | if (status != EFI_SUCCESS) |
| 700 | return status; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 701 | |
| 702 | return EFI_SUCCESS; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 705 | /* |
| 706 | * On success we return a pointer to a boot_params structure, and NULL |
| 707 | * on failure. |
| 708 | */ |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 709 | struct boot_params *efi_main(efi_handle_t handle, |
| 710 | efi_system_table_t *sys_table_arg, |
| 711 | struct boot_params *boot_params, |
| 712 | bool is64) |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 713 | { |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 714 | struct desc_ptr *gdt = NULL; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 715 | struct setup_header *hdr = &boot_params->hdr; |
| 716 | efi_status_t status; |
| 717 | struct desc_struct *desc; |
Hans de Goede | c33ce98 | 2018-09-12 20:32:05 +0200 | [diff] [blame] | 718 | unsigned long cmdline_paddr; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 719 | |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 720 | sys_table = sys_table_arg; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 721 | |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 722 | if (IS_ENABLED(CONFIG_EFI_MIXED)) |
| 723 | efi_is64 = is64; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 724 | |
| 725 | /* Check if we were booted by the EFI firmware */ |
| 726 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 727 | goto fail; |
| 728 | |
David Howells | de8cb45 | 2017-02-06 11:22:43 +0000 | [diff] [blame] | 729 | /* |
Hans de Goede | c33ce98 | 2018-09-12 20:32:05 +0200 | [diff] [blame] | 730 | * make_boot_params() may have been called before efi_main(), in which |
| 731 | * case this is the second time we parse the cmdline. This is ok, |
| 732 | * parsing the cmdline multiple times does not have side-effects. |
| 733 | */ |
| 734 | cmdline_paddr = ((u64)hdr->cmd_line_ptr | |
| 735 | ((u64)boot_params->ext_cmd_line_ptr << 32)); |
| 736 | efi_parse_options((char *)cmdline_paddr); |
| 737 | |
| 738 | /* |
David Howells | de8cb45 | 2017-02-06 11:22:43 +0000 | [diff] [blame] | 739 | * If the boot loader gave us a value for secure_boot then we use that, |
| 740 | * otherwise we ask the BIOS. |
| 741 | */ |
| 742 | if (boot_params->secure_boot == efi_secureboot_mode_unset) |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 743 | boot_params->secure_boot = efi_get_secureboot(); |
David Howells | de8cb45 | 2017-02-06 11:22:43 +0000 | [diff] [blame] | 744 | |
Matthew Garrett | ccc829b | 2017-08-25 16:50:15 +0100 | [diff] [blame] | 745 | /* Ask the firmware to clear memory on unclean shutdown */ |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 746 | efi_enable_reset_attack_mitigation(); |
Dominik Brodowski | 0d95981 | 2019-11-06 08:06:13 +0100 | [diff] [blame] | 747 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 748 | efi_random_get_seed(); |
Dominik Brodowski | 0d95981 | 2019-11-06 08:06:13 +0100 | [diff] [blame] | 749 | |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 750 | efi_retrieve_tpm2_eventlog(); |
Matthew Garrett | ccc829b | 2017-08-25 16:50:15 +0100 | [diff] [blame] | 751 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 752 | setup_graphics(boot_params); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 753 | |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 754 | setup_efi_pci(boot_params); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 755 | |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 756 | setup_quirks(boot_params); |
| 757 | |
Ard Biesheuvel | 966291f | 2019-12-24 16:10:23 +0100 | [diff] [blame^] | 758 | status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*gdt), |
| 759 | (void **)&gdt); |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 760 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 761 | efi_printk("Failed to allocate memory for 'gdt' structure\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 762 | goto fail; |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 763 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 764 | |
| 765 | gdt->size = 0x800; |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 766 | status = efi_low_alloc(gdt->size, 8, (unsigned long *)&gdt->address); |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 767 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 768 | efi_printk("Failed to allocate memory for 'gdt'\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 769 | goto fail; |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 770 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 771 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 772 | /* |
| 773 | * If the kernel isn't already loaded at the preferred load |
| 774 | * address, relocate it. |
| 775 | */ |
| 776 | if (hdr->pref_address != hdr->code32_start) { |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 777 | unsigned long bzimage_addr = hdr->code32_start; |
Ard Biesheuvel | cd33a5c | 2019-12-24 16:10:19 +0100 | [diff] [blame] | 778 | status = efi_relocate_kernel(&bzimage_addr, |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 779 | hdr->init_size, hdr->init_size, |
| 780 | hdr->pref_address, |
Kairui Song | 220dd76 | 2019-10-29 18:37:54 +0100 | [diff] [blame] | 781 | hdr->kernel_alignment, |
| 782 | LOAD_PHYSICAL_ADDR); |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 783 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 784 | efi_printk("efi_relocate_kernel() failed!\n"); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 785 | goto fail; |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 786 | } |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 787 | |
| 788 | hdr->pref_address = hdr->code32_start; |
| 789 | hdr->code32_start = bzimage_addr; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 790 | } |
| 791 | |
Ard Biesheuvel | aab9593 | 2018-07-20 10:47:24 +0900 | [diff] [blame] | 792 | status = exit_boot(boot_params, handle); |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 793 | if (status != EFI_SUCCESS) { |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 794 | efi_printk("exit_boot() failed!\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 795 | goto fail; |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 796 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 797 | |
| 798 | memset((char *)gdt->address, 0x0, gdt->size); |
| 799 | desc = (struct desc_struct *)gdt->address; |
| 800 | |
Kirill A. Shutemov | 919a02d | 2017-06-06 14:31:24 +0300 | [diff] [blame] | 801 | /* The first GDT is a dummy. */ |
| 802 | desc++; |
| 803 | |
| 804 | if (IS_ENABLED(CONFIG_X86_64)) { |
| 805 | /* __KERNEL32_CS */ |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 806 | desc->limit0 = 0xffff; |
| 807 | desc->base0 = 0x0000; |
| 808 | desc->base1 = 0x0000; |
| 809 | desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ; |
| 810 | desc->s = DESC_TYPE_CODE_DATA; |
| 811 | desc->dpl = 0; |
| 812 | desc->p = 1; |
| 813 | desc->limit1 = 0xf; |
| 814 | desc->avl = 0; |
| 815 | desc->l = 0; |
| 816 | desc->d = SEG_OP_SIZE_32BIT; |
| 817 | desc->g = SEG_GRANULARITY_4KB; |
| 818 | desc->base2 = 0x00; |
| 819 | |
Kirill A. Shutemov | 919a02d | 2017-06-06 14:31:24 +0300 | [diff] [blame] | 820 | desc++; |
| 821 | } else { |
| 822 | /* Second entry is unused on 32-bit */ |
| 823 | desc++; |
| 824 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 825 | |
Kirill A. Shutemov | f8fceac | 2017-06-06 14:31:22 +0300 | [diff] [blame] | 826 | /* __KERNEL_CS */ |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 827 | desc->limit0 = 0xffff; |
| 828 | desc->base0 = 0x0000; |
| 829 | desc->base1 = 0x0000; |
| 830 | desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ; |
| 831 | desc->s = DESC_TYPE_CODE_DATA; |
| 832 | desc->dpl = 0; |
| 833 | desc->p = 1; |
| 834 | desc->limit1 = 0xf; |
| 835 | desc->avl = 0; |
| 836 | |
Kirill A. Shutemov | 4c94117 | 2017-06-06 14:31:23 +0300 | [diff] [blame] | 837 | if (IS_ENABLED(CONFIG_X86_64)) { |
| 838 | desc->l = 1; |
| 839 | desc->d = 0; |
| 840 | } else { |
| 841 | desc->l = 0; |
| 842 | desc->d = SEG_OP_SIZE_32BIT; |
| 843 | } |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 844 | desc->g = SEG_GRANULARITY_4KB; |
| 845 | desc->base2 = 0x00; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 846 | desc++; |
Kirill A. Shutemov | f8fceac | 2017-06-06 14:31:22 +0300 | [diff] [blame] | 847 | |
| 848 | /* __KERNEL_DS */ |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 849 | desc->limit0 = 0xffff; |
| 850 | desc->base0 = 0x0000; |
| 851 | desc->base1 = 0x0000; |
| 852 | desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE; |
| 853 | desc->s = DESC_TYPE_CODE_DATA; |
| 854 | desc->dpl = 0; |
| 855 | desc->p = 1; |
| 856 | desc->limit1 = 0xf; |
| 857 | desc->avl = 0; |
| 858 | desc->l = 0; |
| 859 | desc->d = SEG_OP_SIZE_32BIT; |
| 860 | desc->g = SEG_GRANULARITY_4KB; |
| 861 | desc->base2 = 0x00; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 862 | desc++; |
Kirill A. Shutemov | f8fceac | 2017-06-06 14:31:22 +0300 | [diff] [blame] | 863 | |
| 864 | if (IS_ENABLED(CONFIG_X86_64)) { |
| 865 | /* Task segment value */ |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 866 | desc->limit0 = 0x0000; |
| 867 | desc->base0 = 0x0000; |
| 868 | desc->base1 = 0x0000; |
| 869 | desc->type = SEG_TYPE_TSS; |
| 870 | desc->s = 0; |
| 871 | desc->dpl = 0; |
| 872 | desc->p = 1; |
| 873 | desc->limit1 = 0x0; |
| 874 | desc->avl = 0; |
| 875 | desc->l = 0; |
| 876 | desc->d = 0; |
| 877 | desc->g = SEG_GRANULARITY_4KB; |
| 878 | desc->base2 = 0x00; |
Kirill A. Shutemov | f8fceac | 2017-06-06 14:31:22 +0300 | [diff] [blame] | 879 | desc++; |
| 880 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 881 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 882 | asm volatile("cli"); |
Bart Kuivenhoven | 0ce6cda | 2013-09-23 11:45:28 +0200 | [diff] [blame] | 883 | asm volatile ("lgdt %0" : : "m" (*gdt)); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 884 | |
| 885 | return boot_params; |
| 886 | fail: |
Ard Biesheuvel | 8173ec7 | 2019-12-24 16:10:18 +0100 | [diff] [blame] | 887 | efi_printk("efi_main() failed!\n"); |
Ingo Molnar | 90a2186 | 2018-07-11 11:40:33 +0200 | [diff] [blame] | 888 | |
Ard Biesheuvel | c3710de | 2019-12-24 16:10:17 +0100 | [diff] [blame] | 889 | for (;;) |
| 890 | asm("hlt"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 891 | } |
Ard Biesheuvel | 23e6039 | 2019-12-24 16:10:20 +0100 | [diff] [blame] | 892 | |
| 893 | #ifdef CONFIG_EFI_MIXED |
| 894 | void efi_free_native(unsigned long size, unsigned long addr); |
| 895 | |
| 896 | void efi_free(unsigned long size, unsigned long addr) |
| 897 | { |
| 898 | if (!size) |
| 899 | return; |
| 900 | |
| 901 | if (efi_is_native()) |
| 902 | efi_free_native(size, addr); |
| 903 | else |
| 904 | efi64_thunk(efi_system_table()->boottime->mixed_mode.free_pages, |
| 905 | addr, 0, DIV_ROUND_UP(size, EFI_PAGE_SIZE)); |
| 906 | } |
| 907 | #endif |