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