Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- |
| 2 | * |
| 3 | * Copyright 2011 Intel Corporation; author Matt Fleming |
| 4 | * |
| 5 | * This file is part of the Linux kernel, and is made available under |
| 6 | * the terms of the GNU General Public License version 2. |
| 7 | * |
| 8 | * ----------------------------------------------------------------------- */ |
| 9 | |
| 10 | #include <linux/efi.h> |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 11 | #include <linux/pci.h> |
Ingo Molnar | 5520b7e | 2017-01-27 11:59:46 +0100 | [diff] [blame] | 12 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 13 | #include <asm/efi.h> |
Ingo Molnar | 5520b7e | 2017-01-27 11:59:46 +0100 | [diff] [blame] | 14 | #include <asm/e820/types.h> |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 15 | #include <asm/setup.h> |
| 16 | #include <asm/desc.h> |
| 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 | \ |
David Howells | a2cd2f3 | 2017-02-06 11:22:40 +0000 | [diff] [blame] | 37 | c->runtime_services = table->runtime; \ |
Lukas Wunner | 0a637ee | 2016-08-22 12:01:21 +0200 | [diff] [blame] | 38 | c->boot_services = table->boottime; \ |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 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 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 44 | static inline efi_status_t __open_volume32(void *__image, void **__fh) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 45 | { |
| 46 | efi_file_io_interface_t *io; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 47 | efi_loaded_image_32_t *image = __image; |
| 48 | efi_file_handle_32_t *fh; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 49 | efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; |
| 50 | efi_status_t status; |
| 51 | void *handle = (void *)(unsigned long)image->device_handle; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 52 | unsigned long func; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 53 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 54 | status = efi_call_early(handle_protocol, handle, |
| 55 | &fs_proto, (void **)&io); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 56 | if (status != EFI_SUCCESS) { |
| 57 | efi_printk(sys_table, "Failed to handle fs_proto\n"); |
| 58 | return status; |
| 59 | } |
| 60 | |
| 61 | func = (unsigned long)io->open_volume; |
| 62 | status = efi_early->call(func, io, &fh); |
| 63 | if (status != EFI_SUCCESS) |
| 64 | efi_printk(sys_table, "Failed to open volume\n"); |
| 65 | |
| 66 | *__fh = fh; |
| 67 | return status; |
| 68 | } |
| 69 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 70 | static inline efi_status_t __open_volume64(void *__image, void **__fh) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 71 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 72 | efi_file_io_interface_t *io; |
| 73 | efi_loaded_image_64_t *image = __image; |
| 74 | efi_file_handle_64_t *fh; |
| 75 | efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; |
| 76 | efi_status_t status; |
| 77 | void *handle = (void *)(unsigned long)image->device_handle; |
| 78 | unsigned long func; |
| 79 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 80 | status = efi_call_early(handle_protocol, handle, |
| 81 | &fs_proto, (void **)&io); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 82 | if (status != EFI_SUCCESS) { |
| 83 | efi_printk(sys_table, "Failed to handle fs_proto\n"); |
| 84 | return status; |
| 85 | } |
| 86 | |
| 87 | func = (unsigned long)io->open_volume; |
| 88 | status = efi_early->call(func, io, &fh); |
| 89 | if (status != EFI_SUCCESS) |
| 90 | efi_printk(sys_table, "Failed to open volume\n"); |
| 91 | |
| 92 | *__fh = fh; |
| 93 | return status; |
| 94 | } |
| 95 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 96 | efi_status_t |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 97 | efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh) |
| 98 | { |
| 99 | if (efi_early->is64) |
| 100 | return __open_volume64(__image, __fh); |
| 101 | |
| 102 | return __open_volume32(__image, __fh); |
| 103 | } |
| 104 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 105 | 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] | 106 | { |
Lukas Wunner | db4545d | 2017-01-31 13:21:34 +0000 | [diff] [blame] | 107 | efi_call_proto(efi_simple_text_output_protocol, output_string, |
| 108 | efi_early->text_output, str); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 109 | } |
Lee, Chun-Yi | deb9410 | 2012-12-20 19:33:22 +0800 | [diff] [blame] | 110 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 111 | static efi_status_t |
| 112 | __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom) |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 113 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 114 | struct pci_setup_rom *rom = NULL; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 115 | efi_status_t status; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 116 | unsigned long size; |
| 117 | uint64_t attributes; |
| 118 | |
| 119 | status = efi_early->call(pci->attributes, pci, |
| 120 | EfiPciIoAttributeOperationGet, 0, 0, |
| 121 | &attributes); |
| 122 | if (status != EFI_SUCCESS) |
| 123 | return status; |
| 124 | |
| 125 | if (!pci->romimage || !pci->romsize) |
| 126 | return EFI_INVALID_PARAMETER; |
| 127 | |
| 128 | size = pci->romsize + sizeof(*rom); |
| 129 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 130 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom); |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 131 | if (status != EFI_SUCCESS) { |
| 132 | efi_printk(sys_table, "Failed to alloc mem for rom\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 133 | return status; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 134 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 135 | |
| 136 | memset(rom, 0, sizeof(*rom)); |
| 137 | |
| 138 | rom->data.type = SETUP_PCI; |
| 139 | rom->data.len = size - sizeof(struct setup_data); |
| 140 | rom->data.next = 0; |
| 141 | rom->pcilen = pci->romsize; |
| 142 | *__rom = rom; |
| 143 | |
| 144 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, |
| 145 | PCI_VENDOR_ID, 1, &(rom->vendor)); |
| 146 | |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 147 | if (status != EFI_SUCCESS) { |
| 148 | efi_printk(sys_table, "Failed to read rom->vendor\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 149 | goto free_struct; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 150 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 151 | |
| 152 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, |
| 153 | PCI_DEVICE_ID, 1, &(rom->devid)); |
| 154 | |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 155 | if (status != EFI_SUCCESS) { |
| 156 | efi_printk(sys_table, "Failed to read rom->devid\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 157 | goto free_struct; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 158 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 159 | |
| 160 | status = efi_early->call(pci->get_location, pci, &(rom->segment), |
| 161 | &(rom->bus), &(rom->device), &(rom->function)); |
| 162 | |
| 163 | if (status != EFI_SUCCESS) |
| 164 | goto free_struct; |
| 165 | |
| 166 | memcpy(rom->romdata, pci->romimage, pci->romsize); |
| 167 | return status; |
| 168 | |
| 169 | free_struct: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 170 | efi_call_early(free_pool, rom); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 171 | return status; |
| 172 | } |
| 173 | |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 174 | static void |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 175 | setup_efi_pci32(struct boot_params *params, void **pci_handle, |
| 176 | unsigned long size) |
| 177 | { |
| 178 | efi_pci_io_protocol_32 *pci = NULL; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 179 | efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 180 | u32 *handles = (u32 *)(unsigned long)pci_handle; |
| 181 | efi_status_t status; |
| 182 | unsigned long nr_pci; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 183 | struct setup_data *data; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 184 | int i; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 185 | |
Jan Beulich | bc75479 | 2013-01-18 12:35:14 +0000 | [diff] [blame] | 186 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 187 | |
| 188 | while (data && data->next) |
Jan Beulich | bc75479 | 2013-01-18 12:35:14 +0000 | [diff] [blame] | 189 | data = (struct setup_data *)(unsigned long)data->next; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 190 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 191 | nr_pci = size / sizeof(u32); |
| 192 | for (i = 0; i < nr_pci; i++) { |
| 193 | struct pci_setup_rom *rom = NULL; |
| 194 | u32 h = handles[i]; |
| 195 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 196 | status = efi_call_early(handle_protocol, h, |
| 197 | &pci_proto, (void **)&pci); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 198 | |
| 199 | if (status != EFI_SUCCESS) |
| 200 | continue; |
| 201 | |
| 202 | if (!pci) |
| 203 | continue; |
| 204 | |
| 205 | status = __setup_efi_pci32(pci, &rom); |
| 206 | if (status != EFI_SUCCESS) |
| 207 | continue; |
| 208 | |
| 209 | if (data) |
| 210 | data->next = (unsigned long)rom; |
| 211 | else |
| 212 | params->hdr.setup_data = (unsigned long)rom; |
| 213 | |
| 214 | data = (struct setup_data *)rom; |
| 215 | |
| 216 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static efi_status_t |
| 220 | __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom) |
| 221 | { |
| 222 | struct pci_setup_rom *rom; |
| 223 | efi_status_t status; |
| 224 | unsigned long size; |
| 225 | uint64_t attributes; |
| 226 | |
| 227 | status = efi_early->call(pci->attributes, pci, |
| 228 | EfiPciIoAttributeOperationGet, 0, |
| 229 | &attributes); |
| 230 | if (status != EFI_SUCCESS) |
| 231 | return status; |
| 232 | |
| 233 | if (!pci->romimage || !pci->romsize) |
| 234 | return EFI_INVALID_PARAMETER; |
| 235 | |
| 236 | size = pci->romsize + sizeof(*rom); |
| 237 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 238 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom); |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 239 | if (status != EFI_SUCCESS) { |
| 240 | efi_printk(sys_table, "Failed to alloc mem for rom\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 241 | return status; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 242 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 243 | |
| 244 | rom->data.type = SETUP_PCI; |
| 245 | rom->data.len = size - sizeof(struct setup_data); |
| 246 | rom->data.next = 0; |
| 247 | rom->pcilen = pci->romsize; |
| 248 | *__rom = rom; |
| 249 | |
| 250 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, |
| 251 | PCI_VENDOR_ID, 1, &(rom->vendor)); |
| 252 | |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 253 | if (status != EFI_SUCCESS) { |
| 254 | efi_printk(sys_table, "Failed to read rom->vendor\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 255 | goto free_struct; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 256 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 257 | |
| 258 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, |
| 259 | PCI_DEVICE_ID, 1, &(rom->devid)); |
| 260 | |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 261 | if (status != EFI_SUCCESS) { |
| 262 | efi_printk(sys_table, "Failed to read rom->devid\n"); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 263 | goto free_struct; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 264 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 265 | |
| 266 | status = efi_early->call(pci->get_location, pci, &(rom->segment), |
| 267 | &(rom->bus), &(rom->device), &(rom->function)); |
| 268 | |
| 269 | if (status != EFI_SUCCESS) |
| 270 | goto free_struct; |
| 271 | |
| 272 | memcpy(rom->romdata, pci->romimage, pci->romsize); |
| 273 | return status; |
| 274 | |
| 275 | free_struct: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 276 | efi_call_early(free_pool, rom); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 277 | return status; |
| 278 | |
| 279 | } |
| 280 | |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 281 | static void |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 282 | setup_efi_pci64(struct boot_params *params, void **pci_handle, |
| 283 | unsigned long size) |
| 284 | { |
| 285 | efi_pci_io_protocol_64 *pci = NULL; |
| 286 | efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; |
| 287 | u64 *handles = (u64 *)(unsigned long)pci_handle; |
| 288 | efi_status_t status; |
| 289 | unsigned long nr_pci; |
| 290 | struct setup_data *data; |
| 291 | int i; |
| 292 | |
| 293 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
| 294 | |
| 295 | while (data && data->next) |
| 296 | data = (struct setup_data *)(unsigned long)data->next; |
| 297 | |
| 298 | nr_pci = size / sizeof(u64); |
| 299 | for (i = 0; i < nr_pci; i++) { |
| 300 | struct pci_setup_rom *rom = NULL; |
| 301 | u64 h = handles[i]; |
| 302 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 303 | status = efi_call_early(handle_protocol, h, |
| 304 | &pci_proto, (void **)&pci); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 305 | |
| 306 | if (status != EFI_SUCCESS) |
| 307 | continue; |
| 308 | |
| 309 | if (!pci) |
| 310 | continue; |
| 311 | |
| 312 | status = __setup_efi_pci64(pci, &rom); |
| 313 | if (status != EFI_SUCCESS) |
| 314 | continue; |
| 315 | |
| 316 | if (data) |
| 317 | data->next = (unsigned long)rom; |
| 318 | else |
| 319 | params->hdr.setup_data = (unsigned long)rom; |
| 320 | |
| 321 | data = (struct setup_data *)rom; |
| 322 | |
| 323 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 326 | /* |
| 327 | * There's no way to return an informative status from this function, |
| 328 | * because any analysis (and printing of error messages) needs to be |
| 329 | * done directly at the EFI function call-site. |
| 330 | * |
| 331 | * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we |
| 332 | * just didn't find any PCI devices, but there's no way to tell outside |
| 333 | * the context of the call. |
| 334 | */ |
| 335 | static void setup_efi_pci(struct boot_params *params) |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 336 | { |
| 337 | efi_status_t status; |
| 338 | void **pci_handle = NULL; |
| 339 | efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; |
| 340 | unsigned long size = 0; |
| 341 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 342 | status = efi_call_early(locate_handle, |
| 343 | EFI_LOCATE_BY_PROTOCOL, |
| 344 | &pci_proto, NULL, &size, pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 345 | |
| 346 | if (status == EFI_BUFFER_TOO_SMALL) { |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 347 | status = efi_call_early(allocate_pool, |
| 348 | EFI_LOADER_DATA, |
| 349 | size, (void **)&pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 350 | |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 351 | if (status != EFI_SUCCESS) { |
| 352 | efi_printk(sys_table, "Failed to alloc mem for pci_handle\n"); |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 353 | return; |
Andre Müller | 77e21e8 | 2014-09-10 01:00:22 +0200 | [diff] [blame] | 354 | } |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 355 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 356 | status = efi_call_early(locate_handle, |
| 357 | EFI_LOCATE_BY_PROTOCOL, &pci_proto, |
| 358 | NULL, &size, pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | if (status != EFI_SUCCESS) |
| 362 | goto free_handle; |
| 363 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 364 | if (efi_early->is64) |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 365 | setup_efi_pci64(params, pci_handle, size); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 366 | else |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 367 | setup_efi_pci32(params, pci_handle, size); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 368 | |
| 369 | free_handle: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 370 | efi_call_early(free_pool, pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 373 | static void retrieve_apple_device_properties(struct boot_params *boot_params) |
| 374 | { |
| 375 | efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID; |
| 376 | struct setup_data *data, *new; |
| 377 | efi_status_t status; |
| 378 | u32 size = 0; |
| 379 | void *p; |
| 380 | |
| 381 | status = efi_call_early(locate_protocol, &guid, NULL, &p); |
| 382 | if (status != EFI_SUCCESS) |
| 383 | return; |
| 384 | |
| 385 | if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) { |
| 386 | efi_printk(sys_table, "Unsupported properties proto version\n"); |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size); |
| 391 | if (!size) |
| 392 | return; |
| 393 | |
| 394 | do { |
| 395 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 396 | size + sizeof(struct setup_data), &new); |
| 397 | if (status != EFI_SUCCESS) { |
| 398 | efi_printk(sys_table, |
| 399 | "Failed to alloc mem for properties\n"); |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | status = efi_call_proto(apple_properties_protocol, get_all, p, |
| 404 | new->data, &size); |
| 405 | |
| 406 | if (status == EFI_BUFFER_TOO_SMALL) |
| 407 | efi_call_early(free_pool, new); |
| 408 | } while (status == EFI_BUFFER_TOO_SMALL); |
| 409 | |
| 410 | new->type = SETUP_APPLE_PROPERTIES; |
| 411 | new->len = size; |
| 412 | new->next = 0; |
| 413 | |
| 414 | data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data; |
| 415 | if (!data) |
| 416 | boot_params->hdr.setup_data = (unsigned long)new; |
| 417 | else { |
| 418 | while (data->next) |
| 419 | data = (struct setup_data *)(unsigned long)data->next; |
| 420 | data->next = (unsigned long)new; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | static void setup_quirks(struct boot_params *boot_params) |
| 425 | { |
| 426 | efi_char16_t const apple[] = { 'A', 'p', 'p', 'l', 'e', 0 }; |
| 427 | efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long) |
| 428 | efi_table_attr(efi_system_table, fw_vendor, sys_table); |
| 429 | |
| 430 | if (!memcmp(fw_vendor, apple, sizeof(apple))) { |
| 431 | if (IS_ENABLED(CONFIG_APPLE_PROPERTIES)) |
| 432 | retrieve_apple_device_properties(boot_params); |
| 433 | } |
| 434 | } |
| 435 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 436 | static efi_status_t |
| 437 | setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 438 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 439 | struct efi_uga_draw_protocol *uga = NULL, *first_uga; |
| 440 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 441 | unsigned long nr_ugas; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 442 | u32 *handles = (u32 *)uga_handle;; |
Colin Ian King | ac0e94b | 2016-07-20 11:11:06 +0100 | [diff] [blame] | 443 | efi_status_t status = EFI_INVALID_PARAMETER; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 444 | int i; |
| 445 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 446 | first_uga = NULL; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 447 | nr_ugas = size / sizeof(u32); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 448 | for (i = 0; i < nr_ugas; i++) { |
| 449 | efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 450 | u32 w, h, depth, refresh; |
| 451 | void *pciio; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 452 | u32 handle = handles[i]; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 453 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 454 | status = efi_call_early(handle_protocol, handle, |
| 455 | &uga_proto, (void **)&uga); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 456 | if (status != EFI_SUCCESS) |
| 457 | continue; |
| 458 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 459 | efi_call_early(handle_protocol, handle, &pciio_proto, &pciio); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 460 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 461 | status = efi_early->call((unsigned long)uga->get_mode, uga, |
| 462 | &w, &h, &depth, &refresh); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 463 | if (status == EFI_SUCCESS && (!first_uga || pciio)) { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 464 | *width = w; |
| 465 | *height = h; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 466 | |
| 467 | /* |
| 468 | * Once we've found a UGA supporting PCIIO, |
| 469 | * don't bother looking any further. |
| 470 | */ |
| 471 | if (pciio) |
| 472 | break; |
| 473 | |
| 474 | first_uga = uga; |
| 475 | } |
| 476 | } |
| 477 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 478 | return status; |
| 479 | } |
| 480 | |
| 481 | static efi_status_t |
| 482 | setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height) |
| 483 | { |
| 484 | struct efi_uga_draw_protocol *uga = NULL, *first_uga; |
| 485 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; |
| 486 | unsigned long nr_ugas; |
| 487 | u64 *handles = (u64 *)uga_handle;; |
Colin Ian King | ac0e94b | 2016-07-20 11:11:06 +0100 | [diff] [blame] | 488 | efi_status_t status = EFI_INVALID_PARAMETER; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 489 | int i; |
| 490 | |
| 491 | first_uga = NULL; |
| 492 | nr_ugas = size / sizeof(u64); |
| 493 | for (i = 0; i < nr_ugas; i++) { |
| 494 | efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; |
| 495 | u32 w, h, depth, refresh; |
| 496 | void *pciio; |
| 497 | u64 handle = handles[i]; |
| 498 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 499 | status = efi_call_early(handle_protocol, handle, |
| 500 | &uga_proto, (void **)&uga); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 501 | if (status != EFI_SUCCESS) |
| 502 | continue; |
| 503 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 504 | efi_call_early(handle_protocol, handle, &pciio_proto, &pciio); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 505 | |
| 506 | status = efi_early->call((unsigned long)uga->get_mode, uga, |
| 507 | &w, &h, &depth, &refresh); |
| 508 | if (status == EFI_SUCCESS && (!first_uga || pciio)) { |
| 509 | *width = w; |
| 510 | *height = h; |
| 511 | |
| 512 | /* |
| 513 | * Once we've found a UGA supporting PCIIO, |
| 514 | * don't bother looking any further. |
| 515 | */ |
| 516 | if (pciio) |
| 517 | break; |
| 518 | |
| 519 | first_uga = uga; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | return status; |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * See if we have Universal Graphics Adapter (UGA) protocol |
| 528 | */ |
| 529 | static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto, |
| 530 | unsigned long size) |
| 531 | { |
| 532 | efi_status_t status; |
| 533 | u32 width, height; |
| 534 | void **uga_handle = NULL; |
| 535 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 536 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 537 | size, (void **)&uga_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 538 | if (status != EFI_SUCCESS) |
| 539 | return status; |
| 540 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 541 | status = efi_call_early(locate_handle, |
| 542 | EFI_LOCATE_BY_PROTOCOL, |
| 543 | uga_proto, NULL, &size, uga_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 544 | if (status != EFI_SUCCESS) |
| 545 | goto free_handle; |
| 546 | |
| 547 | height = 0; |
| 548 | width = 0; |
| 549 | |
| 550 | if (efi_early->is64) |
| 551 | status = setup_uga64(uga_handle, size, &width, &height); |
| 552 | else |
| 553 | status = setup_uga32(uga_handle, size, &width, &height); |
| 554 | |
| 555 | if (!width && !height) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 556 | goto free_handle; |
| 557 | |
| 558 | /* EFI framebuffer */ |
| 559 | si->orig_video_isVGA = VIDEO_TYPE_EFI; |
| 560 | |
| 561 | si->lfb_depth = 32; |
| 562 | si->lfb_width = width; |
| 563 | si->lfb_height = height; |
| 564 | |
| 565 | si->red_size = 8; |
| 566 | si->red_pos = 16; |
| 567 | si->green_size = 8; |
| 568 | si->green_pos = 8; |
| 569 | si->blue_size = 8; |
| 570 | si->blue_pos = 0; |
| 571 | si->rsvd_size = 8; |
| 572 | si->rsvd_pos = 24; |
| 573 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 574 | free_handle: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 575 | efi_call_early(free_pool, uga_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 576 | return status; |
| 577 | } |
| 578 | |
| 579 | void setup_graphics(struct boot_params *boot_params) |
| 580 | { |
| 581 | efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; |
| 582 | struct screen_info *si; |
| 583 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; |
| 584 | efi_status_t status; |
| 585 | unsigned long size; |
| 586 | void **gop_handle = NULL; |
| 587 | void **uga_handle = NULL; |
| 588 | |
| 589 | si = &boot_params->screen_info; |
| 590 | memset(si, 0, sizeof(*si)); |
| 591 | |
| 592 | size = 0; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 593 | status = efi_call_early(locate_handle, |
| 594 | EFI_LOCATE_BY_PROTOCOL, |
| 595 | &graphics_proto, NULL, &size, gop_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 596 | if (status == EFI_BUFFER_TOO_SMALL) |
Ard Biesheuvel | 2c23b73 | 2016-04-25 21:06:48 +0100 | [diff] [blame] | 597 | status = efi_setup_gop(NULL, si, &graphics_proto, size); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 598 | |
| 599 | if (status != EFI_SUCCESS) { |
| 600 | size = 0; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 601 | status = efi_call_early(locate_handle, |
| 602 | EFI_LOCATE_BY_PROTOCOL, |
| 603 | &uga_proto, NULL, &size, uga_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 604 | if (status == EFI_BUFFER_TOO_SMALL) |
| 605 | setup_uga(si, &uga_proto, size); |
| 606 | } |
| 607 | } |
| 608 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 609 | /* |
| 610 | * Because the x86 boot code expects to be passed a boot_params we |
| 611 | * need to create one ourselves (usually the bootloader would create |
| 612 | * one for us). |
Matt Fleming | 7e8213c | 2014-04-08 13:14:00 +0100 | [diff] [blame] | 613 | * |
| 614 | * The caller is responsible for filling out ->code32_start in the |
| 615 | * returned boot_params. |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 616 | */ |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 617 | struct boot_params *make_boot_params(struct efi_config *c) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 618 | { |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 619 | struct boot_params *boot_params; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 620 | struct apm_bios_info *bi; |
| 621 | struct setup_header *hdr; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 622 | efi_loaded_image_t *image; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 623 | void *options, *handle; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 624 | efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 625 | int options_size = 0; |
| 626 | efi_status_t status; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 627 | char *cmdline_ptr; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 628 | u16 *s2; |
| 629 | u8 *s1; |
| 630 | int i; |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 631 | unsigned long ramdisk_addr; |
| 632 | unsigned long ramdisk_size; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 633 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 634 | efi_early = c; |
| 635 | sys_table = (efi_system_table_t *)(unsigned long)efi_early->table; |
| 636 | handle = (void *)(unsigned long)efi_early->image_handle; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 637 | |
| 638 | /* Check if we were booted by the EFI firmware */ |
| 639 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 640 | return NULL; |
| 641 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 642 | if (efi_early->is64) |
| 643 | setup_boot_services64(efi_early); |
| 644 | else |
| 645 | setup_boot_services32(efi_early); |
| 646 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 647 | status = efi_call_early(handle_protocol, handle, |
| 648 | &proto, (void *)&image); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 649 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 650 | 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] | 651 | return NULL; |
| 652 | } |
| 653 | |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 654 | status = efi_low_alloc(sys_table, 0x4000, 1, |
| 655 | (unsigned long *)&boot_params); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 656 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 657 | efi_printk(sys_table, "Failed to alloc lowmem for boot params\n"); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 658 | return NULL; |
| 659 | } |
| 660 | |
| 661 | memset(boot_params, 0x0, 0x4000); |
| 662 | |
| 663 | hdr = &boot_params->hdr; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 664 | bi = &boot_params->apm_bios_info; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 665 | |
| 666 | /* Copy the second sector to boot_params */ |
| 667 | memcpy(&hdr->jump, image->image_base + 512, 512); |
| 668 | |
| 669 | /* |
| 670 | * Fill out some of the header fields ourselves because the |
| 671 | * EFI firmware loader doesn't load the first sector. |
| 672 | */ |
| 673 | hdr->root_flags = 1; |
| 674 | hdr->vid_mode = 0xffff; |
| 675 | hdr->boot_flag = 0xAA55; |
| 676 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 677 | hdr->type_of_loader = 0x21; |
| 678 | |
| 679 | /* Convert unicode cmdline to ascii */ |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 680 | cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size); |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 681 | if (!cmdline_ptr) |
| 682 | goto fail; |
| 683 | hdr->cmd_line_ptr = (unsigned long)cmdline_ptr; |
Roy Franz | 98b228f | 2015-04-15 16:32:24 -0700 | [diff] [blame] | 684 | /* Fill in upper bits of command line address, NOP on 32 bit */ |
| 685 | boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 686 | |
| 687 | hdr->ramdisk_image = 0; |
| 688 | hdr->ramdisk_size = 0; |
| 689 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 690 | /* Clear APM BIOS info */ |
| 691 | memset(bi, 0, sizeof(*bi)); |
| 692 | |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame] | 693 | status = efi_parse_options(cmdline_ptr); |
| 694 | if (status != EFI_SUCCESS) |
| 695 | goto fail2; |
| 696 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 697 | status = handle_cmdline_files(sys_table, image, |
| 698 | (char *)(unsigned long)hdr->cmd_line_ptr, |
Yinghai Lu | 47226ad | 2014-09-03 21:50:07 -0700 | [diff] [blame] | 699 | "initrd=", hdr->initrd_addr_max, |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 700 | &ramdisk_addr, &ramdisk_size); |
Yinghai Lu | 47226ad | 2014-09-03 21:50:07 -0700 | [diff] [blame] | 701 | |
| 702 | if (status != EFI_SUCCESS && |
| 703 | hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) { |
| 704 | efi_printk(sys_table, "Trying to load files to higher address\n"); |
| 705 | status = handle_cmdline_files(sys_table, image, |
| 706 | (char *)(unsigned long)hdr->cmd_line_ptr, |
| 707 | "initrd=", -1UL, |
| 708 | &ramdisk_addr, &ramdisk_size); |
| 709 | } |
| 710 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 711 | if (status != EFI_SUCCESS) |
| 712 | goto fail2; |
Yinghai Lu | 4bf7111 | 2014-06-14 12:23:41 -0700 | [diff] [blame] | 713 | hdr->ramdisk_image = ramdisk_addr & 0xffffffff; |
| 714 | hdr->ramdisk_size = ramdisk_size & 0xffffffff; |
| 715 | boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32; |
| 716 | boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 717 | |
| 718 | return boot_params; |
| 719 | fail2: |
Roy Franz | 0e1cadb | 2013-09-22 15:45:38 -0700 | [diff] [blame] | 720 | efi_free(sys_table, options_size, hdr->cmd_line_ptr); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 721 | fail: |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 722 | efi_free(sys_table, 0x4000, (unsigned long)boot_params); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 723 | return NULL; |
| 724 | } |
| 725 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 726 | static void add_e820ext(struct boot_params *params, |
| 727 | struct setup_data *e820ext, u32 nr_entries) |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 728 | { |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 729 | struct setup_data *data; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 730 | efi_status_t status; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 731 | unsigned long size; |
| 732 | |
| 733 | e820ext->type = SETUP_E820_EXT; |
Ingo Molnar | 7410aa1 | 2017-01-29 12:56:13 +0100 | [diff] [blame] | 734 | e820ext->len = nr_entries * sizeof(struct boot_e820_entry); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 735 | e820ext->next = 0; |
| 736 | |
| 737 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
| 738 | |
| 739 | while (data && data->next) |
| 740 | data = (struct setup_data *)(unsigned long)data->next; |
| 741 | |
| 742 | if (data) |
| 743 | data->next = (unsigned long)e820ext; |
| 744 | else |
| 745 | params->hdr.setup_data = (unsigned long)e820ext; |
| 746 | } |
| 747 | |
| 748 | static efi_status_t setup_e820(struct boot_params *params, |
| 749 | struct setup_data *e820ext, u32 e820ext_size) |
| 750 | { |
Ingo Molnar | 7410aa1 | 2017-01-29 12:56:13 +0100 | [diff] [blame] | 751 | struct boot_e820_entry *entry = params->e820_table; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 752 | struct efi_info *efi = ¶ms->efi_info; |
Ingo Molnar | 7410aa1 | 2017-01-29 12:56:13 +0100 | [diff] [blame] | 753 | struct boot_e820_entry *prev = NULL; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 754 | u32 nr_entries; |
| 755 | u32 nr_desc; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 756 | int i; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 757 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 758 | nr_entries = 0; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 759 | nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size; |
| 760 | |
| 761 | for (i = 0; i < nr_desc; i++) { |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 762 | efi_memory_desc_t *d; |
| 763 | unsigned int e820_type = 0; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 764 | unsigned long m = efi->efi_memmap; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 765 | |
Dmitry Skorodumov | 7cc03e4 | 2015-07-28 18:38:32 +0400 | [diff] [blame] | 766 | #ifdef CONFIG_X86_64 |
| 767 | m |= (u64)efi->efi_memmap_hi << 32; |
| 768 | #endif |
| 769 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 770 | d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size)); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 771 | switch (d->type) { |
| 772 | case EFI_RESERVED_TYPE: |
| 773 | case EFI_RUNTIME_SERVICES_CODE: |
| 774 | case EFI_RUNTIME_SERVICES_DATA: |
| 775 | case EFI_MEMORY_MAPPED_IO: |
| 776 | case EFI_MEMORY_MAPPED_IO_PORT_SPACE: |
| 777 | case EFI_PAL_CODE: |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 778 | e820_type = E820_TYPE_RESERVED; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 779 | break; |
| 780 | |
| 781 | case EFI_UNUSABLE_MEMORY: |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 782 | e820_type = E820_TYPE_UNUSABLE; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 783 | break; |
| 784 | |
| 785 | case EFI_ACPI_RECLAIM_MEMORY: |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 786 | e820_type = E820_TYPE_ACPI; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 787 | break; |
| 788 | |
| 789 | case EFI_LOADER_CODE: |
| 790 | case EFI_LOADER_DATA: |
| 791 | case EFI_BOOT_SERVICES_CODE: |
| 792 | case EFI_BOOT_SERVICES_DATA: |
| 793 | case EFI_CONVENTIONAL_MEMORY: |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 794 | e820_type = E820_TYPE_RAM; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 795 | break; |
| 796 | |
| 797 | case EFI_ACPI_MEMORY_NVS: |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 798 | e820_type = E820_TYPE_NVS; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 799 | break; |
| 800 | |
Dan Williams | ad5fb87 | 2015-04-03 12:05:28 -0400 | [diff] [blame] | 801 | case EFI_PERSISTENT_MEMORY: |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 802 | e820_type = E820_TYPE_PMEM; |
Dan Williams | ad5fb87 | 2015-04-03 12:05:28 -0400 | [diff] [blame] | 803 | break; |
| 804 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 805 | default: |
| 806 | continue; |
| 807 | } |
| 808 | |
| 809 | /* Merge adjacent mappings */ |
| 810 | if (prev && prev->type == e820_type && |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 811 | (prev->addr + prev->size) == d->phys_addr) { |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 812 | prev->size += d->num_pages << 12; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 813 | continue; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 814 | } |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 815 | |
Ingo Molnar | 61a5010 | 2017-01-27 13:54:38 +0100 | [diff] [blame] | 816 | if (nr_entries == ARRAY_SIZE(params->e820_table)) { |
Ingo Molnar | 8ec67d9 | 2017-01-27 12:54:38 +0100 | [diff] [blame] | 817 | u32 need = (nr_desc - i) * sizeof(struct e820_entry) + |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 818 | sizeof(struct setup_data); |
| 819 | |
| 820 | if (!e820ext || e820ext_size < need) |
| 821 | return EFI_BUFFER_TOO_SMALL; |
| 822 | |
| 823 | /* boot_params map full, switch to e820 extended */ |
Ingo Molnar | 7410aa1 | 2017-01-29 12:56:13 +0100 | [diff] [blame] | 824 | entry = (struct boot_e820_entry *)e820ext->data; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 825 | } |
| 826 | |
Ingo Molnar | 7410aa1 | 2017-01-29 12:56:13 +0100 | [diff] [blame] | 827 | entry->addr = d->phys_addr; |
| 828 | entry->size = d->num_pages << PAGE_SHIFT; |
| 829 | entry->type = e820_type; |
| 830 | prev = entry++; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 831 | nr_entries++; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Ingo Molnar | 61a5010 | 2017-01-27 13:54:38 +0100 | [diff] [blame] | 834 | if (nr_entries > ARRAY_SIZE(params->e820_table)) { |
| 835 | u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 836 | |
| 837 | add_e820ext(params, e820ext, nr_e820ext); |
| 838 | nr_entries -= nr_e820ext; |
| 839 | } |
| 840 | |
| 841 | params->e820_entries = (u8)nr_entries; |
| 842 | |
| 843 | return EFI_SUCCESS; |
| 844 | } |
| 845 | |
| 846 | static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, |
| 847 | u32 *e820ext_size) |
| 848 | { |
| 849 | efi_status_t status; |
| 850 | unsigned long size; |
| 851 | |
| 852 | size = sizeof(struct setup_data) + |
Ingo Molnar | 8ec67d9 | 2017-01-27 12:54:38 +0100 | [diff] [blame] | 853 | sizeof(struct e820_entry) * nr_desc; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 854 | |
| 855 | if (*e820ext) { |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 856 | efi_call_early(free_pool, *e820ext); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 857 | *e820ext = NULL; |
| 858 | *e820ext_size = 0; |
| 859 | } |
| 860 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 861 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 862 | size, (void **)e820ext); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 863 | if (status == EFI_SUCCESS) |
| 864 | *e820ext_size = size; |
| 865 | |
| 866 | return status; |
| 867 | } |
| 868 | |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 869 | struct exit_boot_struct { |
| 870 | struct boot_params *boot_params; |
| 871 | struct efi_info *efi; |
| 872 | struct setup_data *e820ext; |
| 873 | __u32 e820ext_size; |
| 874 | bool is64; |
| 875 | }; |
| 876 | |
| 877 | static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg, |
| 878 | struct efi_boot_memmap *map, |
| 879 | void *priv) |
| 880 | { |
| 881 | static bool first = true; |
| 882 | const char *signature; |
| 883 | __u32 nr_desc; |
| 884 | efi_status_t status; |
| 885 | struct exit_boot_struct *p = priv; |
| 886 | |
| 887 | if (first) { |
| 888 | nr_desc = *map->buff_size / *map->desc_size; |
Ingo Molnar | 61a5010 | 2017-01-27 13:54:38 +0100 | [diff] [blame] | 889 | if (nr_desc > ARRAY_SIZE(p->boot_params->e820_table)) { |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 890 | u32 nr_e820ext = nr_desc - |
Ingo Molnar | 61a5010 | 2017-01-27 13:54:38 +0100 | [diff] [blame] | 891 | ARRAY_SIZE(p->boot_params->e820_table); |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 892 | |
| 893 | status = alloc_e820ext(nr_e820ext, &p->e820ext, |
| 894 | &p->e820ext_size); |
| 895 | if (status != EFI_SUCCESS) |
| 896 | return status; |
| 897 | } |
| 898 | first = false; |
| 899 | } |
| 900 | |
| 901 | signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE; |
| 902 | memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32)); |
| 903 | |
| 904 | p->efi->efi_systab = (unsigned long)sys_table_arg; |
| 905 | p->efi->efi_memdesc_size = *map->desc_size; |
| 906 | p->efi->efi_memdesc_version = *map->desc_ver; |
| 907 | p->efi->efi_memmap = (unsigned long)*map->map; |
| 908 | p->efi->efi_memmap_size = *map->map_size; |
| 909 | |
| 910 | #ifdef CONFIG_X86_64 |
| 911 | p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32; |
| 912 | p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32; |
| 913 | #endif |
| 914 | |
| 915 | return EFI_SUCCESS; |
| 916 | } |
| 917 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 918 | static efi_status_t exit_boot(struct boot_params *boot_params, |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 919 | void *handle, bool is64) |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 920 | { |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 921 | unsigned long map_sz, key, desc_size, buff_size; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 922 | efi_memory_desc_t *mem_map; |
| 923 | struct setup_data *e820ext; |
| 924 | __u32 e820ext_size; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 925 | efi_status_t status; |
| 926 | __u32 desc_version; |
Jeffrey Hugo | dadb57a | 2016-08-29 14:38:51 -0600 | [diff] [blame] | 927 | struct efi_boot_memmap map; |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 928 | struct exit_boot_struct priv; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 929 | |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 930 | map.map = &mem_map; |
| 931 | map.map_size = &map_sz; |
| 932 | map.desc_size = &desc_size; |
| 933 | map.desc_ver = &desc_version; |
| 934 | map.key_ptr = &key; |
| 935 | map.buff_size = &buff_size; |
| 936 | priv.boot_params = boot_params; |
| 937 | priv.efi = &boot_params->efi_info; |
| 938 | priv.e820ext = NULL; |
| 939 | priv.e820ext_size = 0; |
| 940 | priv.is64 = is64; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 941 | |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 942 | /* Might as well exit boot services now */ |
| 943 | status = efi_exit_boot_services(sys_table, handle, &map, &priv, |
| 944 | exit_boot_func); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 945 | if (status != EFI_SUCCESS) |
| 946 | return status; |
| 947 | |
Jeffrey Hugo | d649340 | 2016-08-29 14:38:54 -0600 | [diff] [blame] | 948 | e820ext = priv.e820ext; |
| 949 | e820ext_size = priv.e820ext_size; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 950 | /* Historic? */ |
| 951 | boot_params->alt_mem_k = 32 * 1024; |
| 952 | |
| 953 | status = setup_e820(boot_params, e820ext, e820ext_size); |
| 954 | if (status != EFI_SUCCESS) |
| 955 | return status; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 956 | |
| 957 | return EFI_SUCCESS; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 960 | /* |
| 961 | * On success we return a pointer to a boot_params structure, and NULL |
| 962 | * on failure. |
| 963 | */ |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 964 | struct boot_params *efi_main(struct efi_config *c, |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 965 | struct boot_params *boot_params) |
| 966 | { |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 967 | struct desc_ptr *gdt = NULL; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 968 | efi_loaded_image_t *image; |
| 969 | struct setup_header *hdr = &boot_params->hdr; |
| 970 | efi_status_t status; |
| 971 | struct desc_struct *desc; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 972 | void *handle; |
| 973 | efi_system_table_t *_table; |
| 974 | bool is64; |
| 975 | |
| 976 | efi_early = c; |
| 977 | |
| 978 | _table = (efi_system_table_t *)(unsigned long)efi_early->table; |
| 979 | handle = (void *)(unsigned long)efi_early->image_handle; |
| 980 | is64 = efi_early->is64; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 981 | |
| 982 | sys_table = _table; |
| 983 | |
| 984 | /* Check if we were booted by the EFI firmware */ |
| 985 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 986 | goto fail; |
| 987 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 988 | if (is64) |
| 989 | setup_boot_services64(efi_early); |
| 990 | else |
| 991 | setup_boot_services32(efi_early); |
| 992 | |
David Howells | de8cb45 | 2017-02-06 11:22:43 +0000 | [diff] [blame] | 993 | /* |
| 994 | * If the boot loader gave us a value for secure_boot then we use that, |
| 995 | * otherwise we ask the BIOS. |
| 996 | */ |
| 997 | if (boot_params->secure_boot == efi_secureboot_mode_unset) |
| 998 | boot_params->secure_boot = efi_get_secureboot(sys_table); |
| 999 | |
Matthew Garrett | ccc829b | 2017-08-25 16:50:15 +0100 | [diff] [blame^] | 1000 | /* Ask the firmware to clear memory on unclean shutdown */ |
| 1001 | efi_enable_reset_attack_mitigation(sys_table); |
| 1002 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1003 | setup_graphics(boot_params); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1004 | |
Matt Fleming | 56394ab | 2014-09-11 09:04:25 +0100 | [diff] [blame] | 1005 | setup_efi_pci(boot_params); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 1006 | |
Lukas Wunner | 58c5475 | 2016-11-12 21:32:36 +0000 | [diff] [blame] | 1007 | setup_quirks(boot_params); |
| 1008 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1009 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 1010 | sizeof(*gdt), (void **)&gdt); |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1011 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1012 | efi_printk(sys_table, "Failed to alloc mem for gdt structure\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1013 | goto fail; |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1014 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1015 | |
| 1016 | gdt->size = 0x800; |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 1017 | status = efi_low_alloc(sys_table, gdt->size, 8, |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1018 | (unsigned long *)&gdt->address); |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1019 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1020 | efi_printk(sys_table, "Failed to alloc mem for gdt\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1021 | goto fail; |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1022 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1023 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1024 | /* |
| 1025 | * If the kernel isn't already loaded at the preferred load |
| 1026 | * address, relocate it. |
| 1027 | */ |
| 1028 | if (hdr->pref_address != hdr->code32_start) { |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 1029 | unsigned long bzimage_addr = hdr->code32_start; |
| 1030 | status = efi_relocate_kernel(sys_table, &bzimage_addr, |
| 1031 | hdr->init_size, hdr->init_size, |
| 1032 | hdr->pref_address, |
| 1033 | hdr->kernel_alignment); |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1034 | if (status != EFI_SUCCESS) { |
| 1035 | efi_printk(sys_table, "efi_relocate_kernel() failed!\n"); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1036 | goto fail; |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1037 | } |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 1038 | |
| 1039 | hdr->pref_address = hdr->code32_start; |
| 1040 | hdr->code32_start = bzimage_addr; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 1043 | status = exit_boot(boot_params, handle, is64); |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1044 | if (status != EFI_SUCCESS) { |
| 1045 | efi_printk(sys_table, "exit_boot() failed!\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1046 | goto fail; |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1047 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1048 | |
| 1049 | memset((char *)gdt->address, 0x0, gdt->size); |
| 1050 | desc = (struct desc_struct *)gdt->address; |
| 1051 | |
Kirill A. Shutemov | 919a02d | 2017-06-06 14:31:24 +0300 | [diff] [blame] | 1052 | /* The first GDT is a dummy. */ |
| 1053 | desc++; |
| 1054 | |
| 1055 | if (IS_ENABLED(CONFIG_X86_64)) { |
| 1056 | /* __KERNEL32_CS */ |
| 1057 | desc->limit0 = 0xffff; |
| 1058 | desc->base0 = 0x0000; |
| 1059 | desc->base1 = 0x0000; |
| 1060 | desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ; |
| 1061 | desc->s = DESC_TYPE_CODE_DATA; |
| 1062 | desc->dpl = 0; |
| 1063 | desc->p = 1; |
| 1064 | desc->limit = 0xf; |
| 1065 | desc->avl = 0; |
| 1066 | desc->l = 0; |
| 1067 | desc->d = SEG_OP_SIZE_32BIT; |
| 1068 | desc->g = SEG_GRANULARITY_4KB; |
| 1069 | desc->base2 = 0x00; |
| 1070 | desc++; |
| 1071 | } else { |
| 1072 | /* Second entry is unused on 32-bit */ |
| 1073 | desc++; |
| 1074 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1075 | |
Kirill A. Shutemov | f8fceac | 2017-06-06 14:31:22 +0300 | [diff] [blame] | 1076 | /* __KERNEL_CS */ |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1077 | desc->limit0 = 0xffff; |
| 1078 | desc->base0 = 0x0000; |
| 1079 | desc->base1 = 0x0000; |
| 1080 | desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ; |
| 1081 | desc->s = DESC_TYPE_CODE_DATA; |
| 1082 | desc->dpl = 0; |
| 1083 | desc->p = 1; |
| 1084 | desc->limit = 0xf; |
| 1085 | desc->avl = 0; |
Kirill A. Shutemov | 4c94117 | 2017-06-06 14:31:23 +0300 | [diff] [blame] | 1086 | if (IS_ENABLED(CONFIG_X86_64)) { |
| 1087 | desc->l = 1; |
| 1088 | desc->d = 0; |
| 1089 | } else { |
| 1090 | desc->l = 0; |
| 1091 | desc->d = SEG_OP_SIZE_32BIT; |
| 1092 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1093 | desc->g = SEG_GRANULARITY_4KB; |
| 1094 | desc->base2 = 0x00; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1095 | desc++; |
Kirill A. Shutemov | f8fceac | 2017-06-06 14:31:22 +0300 | [diff] [blame] | 1096 | |
| 1097 | /* __KERNEL_DS */ |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1098 | desc->limit0 = 0xffff; |
| 1099 | desc->base0 = 0x0000; |
| 1100 | desc->base1 = 0x0000; |
| 1101 | desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE; |
| 1102 | desc->s = DESC_TYPE_CODE_DATA; |
| 1103 | desc->dpl = 0; |
| 1104 | desc->p = 1; |
| 1105 | desc->limit = 0xf; |
| 1106 | desc->avl = 0; |
| 1107 | desc->l = 0; |
| 1108 | desc->d = SEG_OP_SIZE_32BIT; |
| 1109 | desc->g = SEG_GRANULARITY_4KB; |
| 1110 | desc->base2 = 0x00; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1111 | desc++; |
Kirill A. Shutemov | f8fceac | 2017-06-06 14:31:22 +0300 | [diff] [blame] | 1112 | |
| 1113 | if (IS_ENABLED(CONFIG_X86_64)) { |
| 1114 | /* Task segment value */ |
| 1115 | desc->limit0 = 0x0000; |
| 1116 | desc->base0 = 0x0000; |
| 1117 | desc->base1 = 0x0000; |
| 1118 | desc->type = SEG_TYPE_TSS; |
| 1119 | desc->s = 0; |
| 1120 | desc->dpl = 0; |
| 1121 | desc->p = 1; |
| 1122 | desc->limit = 0x0; |
| 1123 | desc->avl = 0; |
| 1124 | desc->l = 0; |
| 1125 | desc->d = 0; |
| 1126 | desc->g = SEG_GRANULARITY_4KB; |
| 1127 | desc->base2 = 0x00; |
| 1128 | desc++; |
| 1129 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1130 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1131 | asm volatile("cli"); |
Bart Kuivenhoven | 0ce6cda | 2013-09-23 11:45:28 +0200 | [diff] [blame] | 1132 | asm volatile ("lgdt %0" : : "m" (*gdt)); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1133 | |
| 1134 | return boot_params; |
| 1135 | fail: |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1136 | efi_printk(sys_table, "efi_main() failed!\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1137 | return NULL; |
| 1138 | } |