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