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