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 | |
Matt Fleming | 0f905a4 | 2012-11-20 13:07:46 +0000 | [diff] [blame] | 16 | #undef memcpy /* Use memcpy from misc.c */ |
| 17 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 18 | #include "eboot.h" |
| 19 | |
| 20 | static efi_system_table_t *sys_table; |
| 21 | |
Ard Biesheuvel | f23cf8b | 2014-07-02 14:54:40 +0200 | [diff] [blame] | 22 | struct efi_config *efi_early; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 23 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 24 | #define BOOT_SERVICES(bits) \ |
| 25 | static void setup_boot_services##bits(struct efi_config *c) \ |
| 26 | { \ |
| 27 | efi_system_table_##bits##_t *table; \ |
| 28 | efi_boot_services_##bits##_t *bt; \ |
| 29 | \ |
| 30 | table = (typeof(table))sys_table; \ |
| 31 | \ |
| 32 | c->text_output = table->con_out; \ |
| 33 | \ |
| 34 | bt = (typeof(bt))(unsigned long)(table->boottime); \ |
| 35 | \ |
| 36 | c->allocate_pool = bt->allocate_pool; \ |
| 37 | c->allocate_pages = bt->allocate_pages; \ |
| 38 | c->get_memory_map = bt->get_memory_map; \ |
| 39 | c->free_pool = bt->free_pool; \ |
| 40 | c->free_pages = bt->free_pages; \ |
| 41 | c->locate_handle = bt->locate_handle; \ |
| 42 | c->handle_protocol = bt->handle_protocol; \ |
| 43 | c->exit_boot_services = bt->exit_boot_services; \ |
| 44 | } |
| 45 | BOOT_SERVICES(32); |
| 46 | BOOT_SERVICES(64); |
| 47 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 48 | void efi_char16_printk(efi_system_table_t *, efi_char16_t *); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 49 | |
| 50 | static efi_status_t |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 51 | __file_size32(void *__fh, efi_char16_t *filename_16, |
| 52 | void **handle, u64 *file_sz) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 53 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 54 | efi_file_handle_32_t *h, *fh = __fh; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 55 | efi_file_info_t *info; |
| 56 | efi_status_t status; |
| 57 | efi_guid_t info_guid = EFI_FILE_INFO_ID; |
| 58 | u32 info_sz; |
| 59 | |
| 60 | status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16, |
| 61 | EFI_FILE_MODE_READ, (u64)0); |
| 62 | if (status != EFI_SUCCESS) { |
| 63 | efi_printk(sys_table, "Failed to open file: "); |
| 64 | efi_char16_printk(sys_table, filename_16); |
| 65 | efi_printk(sys_table, "\n"); |
| 66 | return status; |
| 67 | } |
| 68 | |
| 69 | *handle = h; |
| 70 | |
| 71 | info_sz = 0; |
| 72 | status = efi_early->call((unsigned long)h->get_info, h, &info_guid, |
| 73 | &info_sz, NULL); |
| 74 | if (status != EFI_BUFFER_TOO_SMALL) { |
| 75 | efi_printk(sys_table, "Failed to get file info size\n"); |
| 76 | return status; |
| 77 | } |
| 78 | |
| 79 | grow: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 80 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 81 | info_sz, (void **)&info); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 82 | if (status != EFI_SUCCESS) { |
| 83 | efi_printk(sys_table, "Failed to alloc mem for file info\n"); |
| 84 | return status; |
| 85 | } |
| 86 | |
| 87 | status = efi_early->call((unsigned long)h->get_info, h, &info_guid, |
| 88 | &info_sz, info); |
| 89 | if (status == EFI_BUFFER_TOO_SMALL) { |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 90 | efi_call_early(free_pool, info); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 91 | goto grow; |
| 92 | } |
| 93 | |
| 94 | *file_sz = info->file_size; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 95 | efi_call_early(free_pool, info); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 96 | |
| 97 | if (status != EFI_SUCCESS) |
| 98 | efi_printk(sys_table, "Failed to get initrd info\n"); |
| 99 | |
| 100 | return status; |
| 101 | } |
| 102 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 103 | static efi_status_t |
| 104 | __file_size64(void *__fh, efi_char16_t *filename_16, |
| 105 | void **handle, u64 *file_sz) |
| 106 | { |
| 107 | efi_file_handle_64_t *h, *fh = __fh; |
| 108 | efi_file_info_t *info; |
| 109 | efi_status_t status; |
| 110 | efi_guid_t info_guid = EFI_FILE_INFO_ID; |
Matt Fleming | 396f1a0 | 2014-04-10 13:30:13 +0100 | [diff] [blame] | 111 | u64 info_sz; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 112 | |
| 113 | status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16, |
| 114 | EFI_FILE_MODE_READ, (u64)0); |
| 115 | if (status != EFI_SUCCESS) { |
| 116 | efi_printk(sys_table, "Failed to open file: "); |
| 117 | efi_char16_printk(sys_table, filename_16); |
| 118 | efi_printk(sys_table, "\n"); |
| 119 | return status; |
| 120 | } |
| 121 | |
| 122 | *handle = h; |
| 123 | |
| 124 | info_sz = 0; |
| 125 | status = efi_early->call((unsigned long)h->get_info, h, &info_guid, |
| 126 | &info_sz, NULL); |
| 127 | if (status != EFI_BUFFER_TOO_SMALL) { |
| 128 | efi_printk(sys_table, "Failed to get file info size\n"); |
| 129 | return status; |
| 130 | } |
| 131 | |
| 132 | grow: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 133 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 134 | info_sz, (void **)&info); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 135 | if (status != EFI_SUCCESS) { |
| 136 | efi_printk(sys_table, "Failed to alloc mem for file info\n"); |
| 137 | return status; |
| 138 | } |
| 139 | |
| 140 | status = efi_early->call((unsigned long)h->get_info, h, &info_guid, |
| 141 | &info_sz, info); |
| 142 | if (status == EFI_BUFFER_TOO_SMALL) { |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 143 | efi_call_early(free_pool, info); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 144 | goto grow; |
| 145 | } |
| 146 | |
| 147 | *file_sz = info->file_size; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 148 | efi_call_early(free_pool, info); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 149 | |
| 150 | if (status != EFI_SUCCESS) |
| 151 | efi_printk(sys_table, "Failed to get initrd info\n"); |
| 152 | |
| 153 | return status; |
| 154 | } |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 155 | efi_status_t |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 156 | efi_file_size(efi_system_table_t *sys_table, void *__fh, |
| 157 | efi_char16_t *filename_16, void **handle, u64 *file_sz) |
| 158 | { |
| 159 | if (efi_early->is64) |
| 160 | return __file_size64(__fh, filename_16, handle, file_sz); |
| 161 | |
| 162 | return __file_size32(__fh, filename_16, handle, file_sz); |
| 163 | } |
| 164 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 165 | efi_status_t |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 166 | efi_file_read(void *handle, unsigned long *size, void *addr) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 167 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 168 | unsigned long func; |
| 169 | |
| 170 | if (efi_early->is64) { |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 171 | efi_file_handle_64_t *fh = handle; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 172 | |
| 173 | func = (unsigned long)fh->read; |
| 174 | return efi_early->call(func, handle, size, addr); |
| 175 | } else { |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 176 | efi_file_handle_32_t *fh = handle; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 177 | |
| 178 | func = (unsigned long)fh->read; |
| 179 | return efi_early->call(func, handle, size, addr); |
| 180 | } |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 183 | efi_status_t efi_file_close(void *handle) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 184 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 185 | if (efi_early->is64) { |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 186 | efi_file_handle_64_t *fh = handle; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 187 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 188 | return efi_early->call((unsigned long)fh->close, handle); |
| 189 | } else { |
Matt Fleming | 47514c9 | 2014-04-10 14:11:45 +0100 | [diff] [blame] | 190 | efi_file_handle_32_t *fh = handle; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 191 | |
| 192 | return efi_early->call((unsigned long)fh->close, handle); |
| 193 | } |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 196 | static inline efi_status_t __open_volume32(void *__image, void **__fh) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 197 | { |
| 198 | efi_file_io_interface_t *io; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 199 | efi_loaded_image_32_t *image = __image; |
| 200 | efi_file_handle_32_t *fh; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 201 | efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; |
| 202 | efi_status_t status; |
| 203 | void *handle = (void *)(unsigned long)image->device_handle; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 204 | unsigned long func; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 205 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 206 | status = efi_call_early(handle_protocol, handle, |
| 207 | &fs_proto, (void **)&io); |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 208 | if (status != EFI_SUCCESS) { |
| 209 | efi_printk(sys_table, "Failed to handle fs_proto\n"); |
| 210 | return status; |
| 211 | } |
| 212 | |
| 213 | func = (unsigned long)io->open_volume; |
| 214 | status = efi_early->call(func, io, &fh); |
| 215 | if (status != EFI_SUCCESS) |
| 216 | efi_printk(sys_table, "Failed to open volume\n"); |
| 217 | |
| 218 | *__fh = fh; |
| 219 | return status; |
| 220 | } |
| 221 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 222 | static inline efi_status_t __open_volume64(void *__image, void **__fh) |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 223 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 224 | efi_file_io_interface_t *io; |
| 225 | efi_loaded_image_64_t *image = __image; |
| 226 | efi_file_handle_64_t *fh; |
| 227 | efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; |
| 228 | efi_status_t status; |
| 229 | void *handle = (void *)(unsigned long)image->device_handle; |
| 230 | unsigned long func; |
| 231 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 232 | status = efi_call_early(handle_protocol, handle, |
| 233 | &fs_proto, (void **)&io); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 234 | if (status != EFI_SUCCESS) { |
| 235 | efi_printk(sys_table, "Failed to handle fs_proto\n"); |
| 236 | return status; |
| 237 | } |
| 238 | |
| 239 | func = (unsigned long)io->open_volume; |
| 240 | status = efi_early->call(func, io, &fh); |
| 241 | if (status != EFI_SUCCESS) |
| 242 | efi_printk(sys_table, "Failed to open volume\n"); |
| 243 | |
| 244 | *__fh = fh; |
| 245 | return status; |
| 246 | } |
| 247 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 248 | efi_status_t |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 249 | efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh) |
| 250 | { |
| 251 | if (efi_early->is64) |
| 252 | return __open_volume64(__image, __fh); |
| 253 | |
| 254 | return __open_volume32(__image, __fh); |
| 255 | } |
| 256 | |
Ard Biesheuvel | bd66947 | 2014-07-02 14:54:42 +0200 | [diff] [blame] | 257 | 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] | 258 | { |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 259 | unsigned long output_string; |
| 260 | size_t offset; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 261 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 262 | if (efi_early->is64) { |
| 263 | struct efi_simple_text_output_protocol_64 *out; |
| 264 | u64 *func; |
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 | offset = offsetof(typeof(*out), output_string); |
| 267 | output_string = efi_early->text_output + offset; |
| 268 | func = (u64 *)output_string; |
| 269 | |
| 270 | efi_early->call(*func, efi_early->text_output, str); |
| 271 | } else { |
| 272 | struct efi_simple_text_output_protocol_32 *out; |
| 273 | u32 *func; |
| 274 | |
| 275 | offset = offsetof(typeof(*out), output_string); |
| 276 | output_string = efi_early->text_output + offset; |
| 277 | func = (u32 *)output_string; |
| 278 | |
| 279 | efi_early->call(*func, efi_early->text_output, str); |
| 280 | } |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 281 | } |
Lee, Chun-Yi | deb9410 | 2012-12-20 19:33:22 +0800 | [diff] [blame] | 282 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 283 | static void find_bits(unsigned long mask, u8 *pos, u8 *size) |
| 284 | { |
| 285 | u8 first, len; |
| 286 | |
| 287 | first = 0; |
| 288 | len = 0; |
| 289 | |
| 290 | if (mask) { |
| 291 | while (!(mask & 0x1)) { |
| 292 | mask = mask >> 1; |
| 293 | first++; |
| 294 | } |
| 295 | |
| 296 | while (mask & 0x1) { |
| 297 | mask = mask >> 1; |
| 298 | len++; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | *pos = first; |
| 303 | *size = len; |
| 304 | } |
| 305 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 306 | static efi_status_t |
| 307 | __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] | 308 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 309 | struct pci_setup_rom *rom = NULL; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 310 | efi_status_t status; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 311 | unsigned long size; |
| 312 | uint64_t attributes; |
| 313 | |
| 314 | status = efi_early->call(pci->attributes, pci, |
| 315 | EfiPciIoAttributeOperationGet, 0, 0, |
| 316 | &attributes); |
| 317 | if (status != EFI_SUCCESS) |
| 318 | return status; |
| 319 | |
| 320 | if (!pci->romimage || !pci->romsize) |
| 321 | return EFI_INVALID_PARAMETER; |
| 322 | |
| 323 | size = pci->romsize + sizeof(*rom); |
| 324 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 325 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 326 | if (status != EFI_SUCCESS) |
| 327 | return status; |
| 328 | |
| 329 | memset(rom, 0, sizeof(*rom)); |
| 330 | |
| 331 | rom->data.type = SETUP_PCI; |
| 332 | rom->data.len = size - sizeof(struct setup_data); |
| 333 | rom->data.next = 0; |
| 334 | rom->pcilen = pci->romsize; |
| 335 | *__rom = rom; |
| 336 | |
| 337 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, |
| 338 | PCI_VENDOR_ID, 1, &(rom->vendor)); |
| 339 | |
| 340 | if (status != EFI_SUCCESS) |
| 341 | goto free_struct; |
| 342 | |
| 343 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, |
| 344 | PCI_DEVICE_ID, 1, &(rom->devid)); |
| 345 | |
| 346 | if (status != EFI_SUCCESS) |
| 347 | goto free_struct; |
| 348 | |
| 349 | status = efi_early->call(pci->get_location, pci, &(rom->segment), |
| 350 | &(rom->bus), &(rom->device), &(rom->function)); |
| 351 | |
| 352 | if (status != EFI_SUCCESS) |
| 353 | goto free_struct; |
| 354 | |
| 355 | memcpy(rom->romdata, pci->romimage, pci->romsize); |
| 356 | return status; |
| 357 | |
| 358 | free_struct: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 359 | efi_call_early(free_pool, rom); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 360 | return status; |
| 361 | } |
| 362 | |
| 363 | static efi_status_t |
| 364 | setup_efi_pci32(struct boot_params *params, void **pci_handle, |
| 365 | unsigned long size) |
| 366 | { |
| 367 | efi_pci_io_protocol_32 *pci = NULL; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 368 | efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 369 | u32 *handles = (u32 *)(unsigned long)pci_handle; |
| 370 | efi_status_t status; |
| 371 | unsigned long nr_pci; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 372 | struct setup_data *data; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 373 | int i; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 374 | |
Jan Beulich | bc75479 | 2013-01-18 12:35:14 +0000 | [diff] [blame] | 375 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 376 | |
| 377 | while (data && data->next) |
Jan Beulich | bc75479 | 2013-01-18 12:35:14 +0000 | [diff] [blame] | 378 | data = (struct setup_data *)(unsigned long)data->next; |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 379 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 380 | nr_pci = size / sizeof(u32); |
| 381 | for (i = 0; i < nr_pci; i++) { |
| 382 | struct pci_setup_rom *rom = NULL; |
| 383 | u32 h = handles[i]; |
| 384 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 385 | status = efi_call_early(handle_protocol, h, |
| 386 | &pci_proto, (void **)&pci); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 387 | |
| 388 | if (status != EFI_SUCCESS) |
| 389 | continue; |
| 390 | |
| 391 | if (!pci) |
| 392 | continue; |
| 393 | |
| 394 | status = __setup_efi_pci32(pci, &rom); |
| 395 | if (status != EFI_SUCCESS) |
| 396 | continue; |
| 397 | |
| 398 | if (data) |
| 399 | data->next = (unsigned long)rom; |
| 400 | else |
| 401 | params->hdr.setup_data = (unsigned long)rom; |
| 402 | |
| 403 | data = (struct setup_data *)rom; |
| 404 | |
| 405 | } |
| 406 | |
| 407 | return status; |
| 408 | } |
| 409 | |
| 410 | static efi_status_t |
| 411 | __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom) |
| 412 | { |
| 413 | struct pci_setup_rom *rom; |
| 414 | efi_status_t status; |
| 415 | unsigned long size; |
| 416 | uint64_t attributes; |
| 417 | |
| 418 | status = efi_early->call(pci->attributes, pci, |
| 419 | EfiPciIoAttributeOperationGet, 0, |
| 420 | &attributes); |
| 421 | if (status != EFI_SUCCESS) |
| 422 | return status; |
| 423 | |
| 424 | if (!pci->romimage || !pci->romsize) |
| 425 | return EFI_INVALID_PARAMETER; |
| 426 | |
| 427 | size = pci->romsize + sizeof(*rom); |
| 428 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 429 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 430 | if (status != EFI_SUCCESS) |
| 431 | return status; |
| 432 | |
| 433 | rom->data.type = SETUP_PCI; |
| 434 | rom->data.len = size - sizeof(struct setup_data); |
| 435 | rom->data.next = 0; |
| 436 | rom->pcilen = pci->romsize; |
| 437 | *__rom = rom; |
| 438 | |
| 439 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, |
| 440 | PCI_VENDOR_ID, 1, &(rom->vendor)); |
| 441 | |
| 442 | if (status != EFI_SUCCESS) |
| 443 | goto free_struct; |
| 444 | |
| 445 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, |
| 446 | PCI_DEVICE_ID, 1, &(rom->devid)); |
| 447 | |
| 448 | if (status != EFI_SUCCESS) |
| 449 | goto free_struct; |
| 450 | |
| 451 | status = efi_early->call(pci->get_location, pci, &(rom->segment), |
| 452 | &(rom->bus), &(rom->device), &(rom->function)); |
| 453 | |
| 454 | if (status != EFI_SUCCESS) |
| 455 | goto free_struct; |
| 456 | |
| 457 | memcpy(rom->romdata, pci->romimage, pci->romsize); |
| 458 | return status; |
| 459 | |
| 460 | free_struct: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 461 | efi_call_early(free_pool, rom); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 462 | return status; |
| 463 | |
| 464 | } |
| 465 | |
| 466 | static efi_status_t |
| 467 | setup_efi_pci64(struct boot_params *params, void **pci_handle, |
| 468 | unsigned long size) |
| 469 | { |
| 470 | efi_pci_io_protocol_64 *pci = NULL; |
| 471 | efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; |
| 472 | u64 *handles = (u64 *)(unsigned long)pci_handle; |
| 473 | efi_status_t status; |
| 474 | unsigned long nr_pci; |
| 475 | struct setup_data *data; |
| 476 | int i; |
| 477 | |
| 478 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
| 479 | |
| 480 | while (data && data->next) |
| 481 | data = (struct setup_data *)(unsigned long)data->next; |
| 482 | |
| 483 | nr_pci = size / sizeof(u64); |
| 484 | for (i = 0; i < nr_pci; i++) { |
| 485 | struct pci_setup_rom *rom = NULL; |
| 486 | u64 h = handles[i]; |
| 487 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 488 | status = efi_call_early(handle_protocol, h, |
| 489 | &pci_proto, (void **)&pci); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 490 | |
| 491 | if (status != EFI_SUCCESS) |
| 492 | continue; |
| 493 | |
| 494 | if (!pci) |
| 495 | continue; |
| 496 | |
| 497 | status = __setup_efi_pci64(pci, &rom); |
| 498 | if (status != EFI_SUCCESS) |
| 499 | continue; |
| 500 | |
| 501 | if (data) |
| 502 | data->next = (unsigned long)rom; |
| 503 | else |
| 504 | params->hdr.setup_data = (unsigned long)rom; |
| 505 | |
| 506 | data = (struct setup_data *)rom; |
| 507 | |
| 508 | } |
| 509 | |
| 510 | return status; |
| 511 | } |
| 512 | |
| 513 | static efi_status_t setup_efi_pci(struct boot_params *params) |
| 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 | |
| 529 | if (status != EFI_SUCCESS) |
| 530 | return status; |
| 531 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 532 | status = efi_call_early(locate_handle, |
| 533 | EFI_LOCATE_BY_PROTOCOL, &pci_proto, |
| 534 | NULL, &size, pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | if (status != EFI_SUCCESS) |
| 538 | goto free_handle; |
| 539 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 540 | if (efi_early->is64) |
| 541 | status = setup_efi_pci64(params, pci_handle, size); |
| 542 | else |
| 543 | status = setup_efi_pci32(params, pci_handle, size); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 544 | |
| 545 | free_handle: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 546 | efi_call_early(free_pool, pci_handle); |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 547 | return status; |
| 548 | } |
| 549 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 550 | static void |
| 551 | setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line, |
| 552 | struct efi_pixel_bitmask pixel_info, int pixel_format) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 553 | { |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 554 | if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) { |
| 555 | si->lfb_depth = 32; |
| 556 | si->lfb_linelength = pixels_per_scan_line * 4; |
| 557 | si->red_size = 8; |
| 558 | si->red_pos = 0; |
| 559 | si->green_size = 8; |
| 560 | si->green_pos = 8; |
| 561 | si->blue_size = 8; |
| 562 | si->blue_pos = 16; |
| 563 | si->rsvd_size = 8; |
| 564 | si->rsvd_pos = 24; |
| 565 | } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) { |
| 566 | si->lfb_depth = 32; |
| 567 | si->lfb_linelength = pixels_per_scan_line * 4; |
| 568 | si->red_size = 8; |
| 569 | si->red_pos = 16; |
| 570 | si->green_size = 8; |
| 571 | si->green_pos = 8; |
| 572 | si->blue_size = 8; |
| 573 | si->blue_pos = 0; |
| 574 | si->rsvd_size = 8; |
| 575 | si->rsvd_pos = 24; |
| 576 | } else if (pixel_format == PIXEL_BIT_MASK) { |
| 577 | find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size); |
| 578 | find_bits(pixel_info.green_mask, &si->green_pos, |
| 579 | &si->green_size); |
| 580 | find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size); |
| 581 | find_bits(pixel_info.reserved_mask, &si->rsvd_pos, |
| 582 | &si->rsvd_size); |
| 583 | si->lfb_depth = si->red_size + si->green_size + |
| 584 | si->blue_size + si->rsvd_size; |
| 585 | si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8; |
| 586 | } else { |
| 587 | si->lfb_depth = 4; |
| 588 | si->lfb_linelength = si->lfb_width / 2; |
| 589 | si->red_size = 0; |
| 590 | si->red_pos = 0; |
| 591 | si->green_size = 0; |
| 592 | si->green_pos = 0; |
| 593 | si->blue_size = 0; |
| 594 | si->blue_pos = 0; |
| 595 | si->rsvd_size = 0; |
| 596 | si->rsvd_pos = 0; |
| 597 | } |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static efi_status_t |
| 601 | __gop_query32(struct efi_graphics_output_protocol_32 *gop32, |
| 602 | struct efi_graphics_output_mode_info **info, |
| 603 | unsigned long *size, u32 *fb_base) |
| 604 | { |
| 605 | struct efi_graphics_output_protocol_mode_32 *mode; |
| 606 | efi_status_t status; |
| 607 | unsigned long m; |
| 608 | |
| 609 | m = gop32->mode; |
| 610 | mode = (struct efi_graphics_output_protocol_mode_32 *)m; |
| 611 | |
| 612 | status = efi_early->call(gop32->query_mode, gop32, |
| 613 | mode->mode, size, info); |
| 614 | if (status != EFI_SUCCESS) |
| 615 | return status; |
| 616 | |
| 617 | *fb_base = mode->frame_buffer_base; |
| 618 | return status; |
| 619 | } |
| 620 | |
| 621 | static efi_status_t |
| 622 | setup_gop32(struct screen_info *si, efi_guid_t *proto, |
| 623 | unsigned long size, void **gop_handle) |
| 624 | { |
| 625 | struct efi_graphics_output_protocol_32 *gop32, *first_gop; |
| 626 | unsigned long nr_gops; |
| 627 | u16 width, height; |
| 628 | u32 pixels_per_scan_line; |
| 629 | u32 fb_base; |
| 630 | struct efi_pixel_bitmask pixel_info; |
| 631 | int pixel_format; |
| 632 | efi_status_t status; |
| 633 | u32 *handles = (u32 *)(unsigned long)gop_handle; |
| 634 | int i; |
| 635 | |
| 636 | first_gop = NULL; |
| 637 | gop32 = NULL; |
| 638 | |
| 639 | nr_gops = size / sizeof(u32); |
| 640 | for (i = 0; i < nr_gops; i++) { |
| 641 | struct efi_graphics_output_mode_info *info = NULL; |
| 642 | efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID; |
| 643 | bool conout_found = false; |
| 644 | void *dummy = NULL; |
| 645 | u32 h = handles[i]; |
| 646 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 647 | status = efi_call_early(handle_protocol, h, |
| 648 | proto, (void **)&gop32); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 649 | if (status != EFI_SUCCESS) |
| 650 | continue; |
| 651 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 652 | status = efi_call_early(handle_protocol, h, |
| 653 | &conout_proto, &dummy); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 654 | if (status == EFI_SUCCESS) |
| 655 | conout_found = true; |
| 656 | |
| 657 | status = __gop_query32(gop32, &info, &size, &fb_base); |
| 658 | if (status == EFI_SUCCESS && (!first_gop || conout_found)) { |
| 659 | /* |
| 660 | * Systems that use the UEFI Console Splitter may |
| 661 | * provide multiple GOP devices, not all of which are |
| 662 | * backed by real hardware. The workaround is to search |
| 663 | * for a GOP implementing the ConOut protocol, and if |
| 664 | * one isn't found, to just fall back to the first GOP. |
| 665 | */ |
| 666 | width = info->horizontal_resolution; |
| 667 | height = info->vertical_resolution; |
| 668 | pixel_format = info->pixel_format; |
| 669 | pixel_info = info->pixel_information; |
| 670 | pixels_per_scan_line = info->pixels_per_scan_line; |
| 671 | |
| 672 | /* |
| 673 | * Once we've found a GOP supporting ConOut, |
| 674 | * don't bother looking any further. |
| 675 | */ |
| 676 | first_gop = gop32; |
| 677 | if (conout_found) |
| 678 | break; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | /* Did we find any GOPs? */ |
| 683 | if (!first_gop) |
| 684 | goto out; |
| 685 | |
| 686 | /* EFI framebuffer */ |
| 687 | si->orig_video_isVGA = VIDEO_TYPE_EFI; |
| 688 | |
| 689 | si->lfb_width = width; |
| 690 | si->lfb_height = height; |
| 691 | si->lfb_base = fb_base; |
| 692 | si->pages = 1; |
| 693 | |
| 694 | setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 695 | |
Matthew Garrett | e9b1095 | 2012-07-27 17:20:49 -0400 | [diff] [blame] | 696 | si->lfb_size = si->lfb_linelength * si->lfb_height; |
| 697 | |
Matthew Garrett | f462ed9 | 2012-07-27 12:58:53 -0400 | [diff] [blame] | 698 | si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 699 | out: |
| 700 | return status; |
| 701 | } |
| 702 | |
| 703 | static efi_status_t |
| 704 | __gop_query64(struct efi_graphics_output_protocol_64 *gop64, |
| 705 | struct efi_graphics_output_mode_info **info, |
| 706 | unsigned long *size, u32 *fb_base) |
| 707 | { |
| 708 | struct efi_graphics_output_protocol_mode_64 *mode; |
| 709 | efi_status_t status; |
| 710 | unsigned long m; |
| 711 | |
| 712 | m = gop64->mode; |
| 713 | mode = (struct efi_graphics_output_protocol_mode_64 *)m; |
| 714 | |
| 715 | status = efi_early->call(gop64->query_mode, gop64, |
| 716 | mode->mode, size, info); |
| 717 | if (status != EFI_SUCCESS) |
| 718 | return status; |
| 719 | |
| 720 | *fb_base = mode->frame_buffer_base; |
| 721 | return status; |
| 722 | } |
| 723 | |
| 724 | static efi_status_t |
| 725 | setup_gop64(struct screen_info *si, efi_guid_t *proto, |
| 726 | unsigned long size, void **gop_handle) |
| 727 | { |
| 728 | struct efi_graphics_output_protocol_64 *gop64, *first_gop; |
| 729 | unsigned long nr_gops; |
| 730 | u16 width, height; |
| 731 | u32 pixels_per_scan_line; |
| 732 | u32 fb_base; |
| 733 | struct efi_pixel_bitmask pixel_info; |
| 734 | int pixel_format; |
| 735 | efi_status_t status; |
| 736 | u64 *handles = (u64 *)(unsigned long)gop_handle; |
| 737 | int i; |
| 738 | |
| 739 | first_gop = NULL; |
| 740 | gop64 = NULL; |
| 741 | |
| 742 | nr_gops = size / sizeof(u64); |
| 743 | for (i = 0; i < nr_gops; i++) { |
| 744 | struct efi_graphics_output_mode_info *info = NULL; |
| 745 | efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID; |
| 746 | bool conout_found = false; |
| 747 | void *dummy = NULL; |
| 748 | u64 h = handles[i]; |
| 749 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 750 | status = efi_call_early(handle_protocol, h, |
| 751 | proto, (void **)&gop64); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 752 | if (status != EFI_SUCCESS) |
| 753 | continue; |
| 754 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 755 | status = efi_call_early(handle_protocol, h, |
| 756 | &conout_proto, &dummy); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 757 | if (status == EFI_SUCCESS) |
| 758 | conout_found = true; |
| 759 | |
| 760 | status = __gop_query64(gop64, &info, &size, &fb_base); |
| 761 | if (status == EFI_SUCCESS && (!first_gop || conout_found)) { |
| 762 | /* |
| 763 | * Systems that use the UEFI Console Splitter may |
| 764 | * provide multiple GOP devices, not all of which are |
| 765 | * backed by real hardware. The workaround is to search |
| 766 | * for a GOP implementing the ConOut protocol, and if |
| 767 | * one isn't found, to just fall back to the first GOP. |
| 768 | */ |
| 769 | width = info->horizontal_resolution; |
| 770 | height = info->vertical_resolution; |
| 771 | pixel_format = info->pixel_format; |
| 772 | pixel_info = info->pixel_information; |
| 773 | pixels_per_scan_line = info->pixels_per_scan_line; |
| 774 | |
| 775 | /* |
| 776 | * Once we've found a GOP supporting ConOut, |
| 777 | * don't bother looking any further. |
| 778 | */ |
| 779 | first_gop = gop64; |
| 780 | if (conout_found) |
| 781 | break; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | /* Did we find any GOPs? */ |
| 786 | if (!first_gop) |
| 787 | goto out; |
| 788 | |
| 789 | /* EFI framebuffer */ |
| 790 | si->orig_video_isVGA = VIDEO_TYPE_EFI; |
| 791 | |
| 792 | si->lfb_width = width; |
| 793 | si->lfb_height = height; |
| 794 | si->lfb_base = fb_base; |
| 795 | si->pages = 1; |
| 796 | |
| 797 | setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format); |
| 798 | |
| 799 | si->lfb_size = si->lfb_linelength * si->lfb_height; |
| 800 | |
| 801 | si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS; |
| 802 | out: |
| 803 | return status; |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * See if we have Graphics Output Protocol |
| 808 | */ |
| 809 | static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto, |
| 810 | unsigned long size) |
| 811 | { |
| 812 | efi_status_t status; |
| 813 | void **gop_handle = NULL; |
| 814 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 815 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 816 | size, (void **)&gop_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 817 | if (status != EFI_SUCCESS) |
| 818 | return status; |
| 819 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 820 | status = efi_call_early(locate_handle, |
| 821 | EFI_LOCATE_BY_PROTOCOL, |
| 822 | proto, NULL, &size, gop_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 823 | if (status != EFI_SUCCESS) |
| 824 | goto free_handle; |
| 825 | |
| 826 | if (efi_early->is64) |
| 827 | status = setup_gop64(si, proto, size, gop_handle); |
| 828 | else |
| 829 | status = setup_gop32(si, proto, size, gop_handle); |
Matthew Garrett | f462ed9 | 2012-07-27 12:58:53 -0400 | [diff] [blame] | 830 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 831 | free_handle: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 832 | efi_call_early(free_pool, gop_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 833 | return status; |
| 834 | } |
| 835 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 836 | static efi_status_t |
| 837 | setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 838 | { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 839 | struct efi_uga_draw_protocol *uga = NULL, *first_uga; |
| 840 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 841 | unsigned long nr_ugas; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 842 | u32 *handles = (u32 *)uga_handle;; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 843 | efi_status_t status; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 844 | int i; |
| 845 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 846 | first_uga = NULL; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 847 | nr_ugas = size / sizeof(u32); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 848 | for (i = 0; i < nr_ugas; i++) { |
| 849 | efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 850 | u32 w, h, depth, refresh; |
| 851 | void *pciio; |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 852 | u32 handle = handles[i]; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 853 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 854 | status = efi_call_early(handle_protocol, handle, |
| 855 | &uga_proto, (void **)&uga); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 856 | if (status != EFI_SUCCESS) |
| 857 | continue; |
| 858 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 859 | efi_call_early(handle_protocol, handle, &pciio_proto, &pciio); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 860 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 861 | status = efi_early->call((unsigned long)uga->get_mode, uga, |
| 862 | &w, &h, &depth, &refresh); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 863 | if (status == EFI_SUCCESS && (!first_uga || pciio)) { |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 864 | *width = w; |
| 865 | *height = h; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 866 | |
| 867 | /* |
| 868 | * Once we've found a UGA supporting PCIIO, |
| 869 | * don't bother looking any further. |
| 870 | */ |
| 871 | if (pciio) |
| 872 | break; |
| 873 | |
| 874 | first_uga = uga; |
| 875 | } |
| 876 | } |
| 877 | |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 878 | return status; |
| 879 | } |
| 880 | |
| 881 | static efi_status_t |
| 882 | setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height) |
| 883 | { |
| 884 | struct efi_uga_draw_protocol *uga = NULL, *first_uga; |
| 885 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; |
| 886 | unsigned long nr_ugas; |
| 887 | u64 *handles = (u64 *)uga_handle;; |
| 888 | efi_status_t status; |
| 889 | int i; |
| 890 | |
| 891 | first_uga = NULL; |
| 892 | nr_ugas = size / sizeof(u64); |
| 893 | for (i = 0; i < nr_ugas; i++) { |
| 894 | efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; |
| 895 | u32 w, h, depth, refresh; |
| 896 | void *pciio; |
| 897 | u64 handle = handles[i]; |
| 898 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 899 | status = efi_call_early(handle_protocol, handle, |
| 900 | &uga_proto, (void **)&uga); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 901 | if (status != EFI_SUCCESS) |
| 902 | continue; |
| 903 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 904 | efi_call_early(handle_protocol, handle, &pciio_proto, &pciio); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 905 | |
| 906 | status = efi_early->call((unsigned long)uga->get_mode, uga, |
| 907 | &w, &h, &depth, &refresh); |
| 908 | if (status == EFI_SUCCESS && (!first_uga || pciio)) { |
| 909 | *width = w; |
| 910 | *height = h; |
| 911 | |
| 912 | /* |
| 913 | * Once we've found a UGA supporting PCIIO, |
| 914 | * don't bother looking any further. |
| 915 | */ |
| 916 | if (pciio) |
| 917 | break; |
| 918 | |
| 919 | first_uga = uga; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | return status; |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * See if we have Universal Graphics Adapter (UGA) protocol |
| 928 | */ |
| 929 | static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto, |
| 930 | unsigned long size) |
| 931 | { |
| 932 | efi_status_t status; |
| 933 | u32 width, height; |
| 934 | void **uga_handle = NULL; |
| 935 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 936 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 937 | size, (void **)&uga_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 938 | if (status != EFI_SUCCESS) |
| 939 | return status; |
| 940 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 941 | status = efi_call_early(locate_handle, |
| 942 | EFI_LOCATE_BY_PROTOCOL, |
| 943 | uga_proto, NULL, &size, uga_handle); |
Matt Fleming | c116e8d | 2014-01-16 11:35:43 +0000 | [diff] [blame] | 944 | if (status != EFI_SUCCESS) |
| 945 | goto free_handle; |
| 946 | |
| 947 | height = 0; |
| 948 | width = 0; |
| 949 | |
| 950 | if (efi_early->is64) |
| 951 | status = setup_uga64(uga_handle, size, &width, &height); |
| 952 | else |
| 953 | status = setup_uga32(uga_handle, size, &width, &height); |
| 954 | |
| 955 | if (!width && !height) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 956 | goto free_handle; |
| 957 | |
| 958 | /* EFI framebuffer */ |
| 959 | si->orig_video_isVGA = VIDEO_TYPE_EFI; |
| 960 | |
| 961 | si->lfb_depth = 32; |
| 962 | si->lfb_width = width; |
| 963 | si->lfb_height = height; |
| 964 | |
| 965 | si->red_size = 8; |
| 966 | si->red_pos = 16; |
| 967 | si->green_size = 8; |
| 968 | si->green_pos = 8; |
| 969 | si->blue_size = 8; |
| 970 | si->blue_pos = 0; |
| 971 | si->rsvd_size = 8; |
| 972 | si->rsvd_pos = 24; |
| 973 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 974 | free_handle: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 975 | efi_call_early(free_pool, uga_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 976 | return status; |
| 977 | } |
| 978 | |
| 979 | void setup_graphics(struct boot_params *boot_params) |
| 980 | { |
| 981 | efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; |
| 982 | struct screen_info *si; |
| 983 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; |
| 984 | efi_status_t status; |
| 985 | unsigned long size; |
| 986 | void **gop_handle = NULL; |
| 987 | void **uga_handle = NULL; |
| 988 | |
| 989 | si = &boot_params->screen_info; |
| 990 | memset(si, 0, sizeof(*si)); |
| 991 | |
| 992 | size = 0; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 993 | status = efi_call_early(locate_handle, |
| 994 | EFI_LOCATE_BY_PROTOCOL, |
| 995 | &graphics_proto, NULL, &size, gop_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 996 | if (status == EFI_BUFFER_TOO_SMALL) |
| 997 | status = setup_gop(si, &graphics_proto, size); |
| 998 | |
| 999 | if (status != EFI_SUCCESS) { |
| 1000 | size = 0; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1001 | status = efi_call_early(locate_handle, |
| 1002 | EFI_LOCATE_BY_PROTOCOL, |
| 1003 | &uga_proto, NULL, &size, uga_handle); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1004 | if (status == EFI_BUFFER_TOO_SMALL) |
| 1005 | setup_uga(si, &uga_proto, size); |
| 1006 | } |
| 1007 | } |
| 1008 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1009 | /* |
| 1010 | * Because the x86 boot code expects to be passed a boot_params we |
| 1011 | * need to create one ourselves (usually the bootloader would create |
| 1012 | * one for us). |
Matt Fleming | 7e8213c | 2014-04-08 13:14:00 +0100 | [diff] [blame] | 1013 | * |
| 1014 | * The caller is responsible for filling out ->code32_start in the |
| 1015 | * returned boot_params. |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1016 | */ |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1017 | struct boot_params *make_boot_params(struct efi_config *c) |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1018 | { |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1019 | struct boot_params *boot_params; |
| 1020 | struct sys_desc_table *sdt; |
| 1021 | struct apm_bios_info *bi; |
| 1022 | struct setup_header *hdr; |
| 1023 | struct efi_info *efi; |
| 1024 | efi_loaded_image_t *image; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1025 | void *options, *handle; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1026 | efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1027 | int options_size = 0; |
| 1028 | efi_status_t status; |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 1029 | char *cmdline_ptr; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1030 | u16 *s2; |
| 1031 | u8 *s1; |
| 1032 | int i; |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 1033 | unsigned long ramdisk_addr; |
| 1034 | unsigned long ramdisk_size; |
Yinghai Lu | 4bf7111 | 2014-06-14 12:23:41 -0700 | [diff] [blame] | 1035 | unsigned long initrd_addr_max; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1036 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1037 | efi_early = c; |
| 1038 | sys_table = (efi_system_table_t *)(unsigned long)efi_early->table; |
| 1039 | handle = (void *)(unsigned long)efi_early->image_handle; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1040 | |
| 1041 | /* Check if we were booted by the EFI firmware */ |
| 1042 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 1043 | return NULL; |
| 1044 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1045 | if (efi_early->is64) |
| 1046 | setup_boot_services64(efi_early); |
| 1047 | else |
| 1048 | setup_boot_services32(efi_early); |
| 1049 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1050 | status = efi_call_early(handle_protocol, handle, |
| 1051 | &proto, (void *)&image); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1052 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1053 | 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] | 1054 | return NULL; |
| 1055 | } |
| 1056 | |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 1057 | status = efi_low_alloc(sys_table, 0x4000, 1, |
| 1058 | (unsigned long *)&boot_params); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1059 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1060 | efi_printk(sys_table, "Failed to alloc lowmem for boot params\n"); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1061 | return NULL; |
| 1062 | } |
| 1063 | |
| 1064 | memset(boot_params, 0x0, 0x4000); |
| 1065 | |
| 1066 | hdr = &boot_params->hdr; |
| 1067 | efi = &boot_params->efi_info; |
| 1068 | bi = &boot_params->apm_bios_info; |
| 1069 | sdt = &boot_params->sys_desc_table; |
| 1070 | |
| 1071 | /* Copy the second sector to boot_params */ |
| 1072 | memcpy(&hdr->jump, image->image_base + 512, 512); |
| 1073 | |
| 1074 | /* |
| 1075 | * Fill out some of the header fields ourselves because the |
| 1076 | * EFI firmware loader doesn't load the first sector. |
| 1077 | */ |
| 1078 | hdr->root_flags = 1; |
| 1079 | hdr->vid_mode = 0xffff; |
| 1080 | hdr->boot_flag = 0xAA55; |
| 1081 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1082 | hdr->type_of_loader = 0x21; |
| 1083 | |
| 1084 | /* Convert unicode cmdline to ascii */ |
H. Peter Anvin | c625d1c | 2013-09-20 09:55:39 -0500 | [diff] [blame] | 1085 | cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size); |
Roy Franz | 5fef387 | 2013-09-22 15:45:33 -0700 | [diff] [blame] | 1086 | if (!cmdline_ptr) |
| 1087 | goto fail; |
| 1088 | hdr->cmd_line_ptr = (unsigned long)cmdline_ptr; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1089 | |
| 1090 | hdr->ramdisk_image = 0; |
| 1091 | hdr->ramdisk_size = 0; |
| 1092 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1093 | /* Clear APM BIOS info */ |
| 1094 | memset(bi, 0, sizeof(*bi)); |
| 1095 | |
| 1096 | memset(sdt, 0, sizeof(*sdt)); |
| 1097 | |
Yinghai Lu | 4bf7111 | 2014-06-14 12:23:41 -0700 | [diff] [blame] | 1098 | if (hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) |
| 1099 | initrd_addr_max = -1UL; |
| 1100 | else |
| 1101 | initrd_addr_max = hdr->initrd_addr_max; |
| 1102 | |
Matt Fleming | 5a17dae | 2014-08-05 11:52:11 +0100 | [diff] [blame^] | 1103 | status = efi_parse_options(cmdline_ptr); |
| 1104 | if (status != EFI_SUCCESS) |
| 1105 | goto fail2; |
| 1106 | |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 1107 | status = handle_cmdline_files(sys_table, image, |
| 1108 | (char *)(unsigned long)hdr->cmd_line_ptr, |
Yinghai Lu | 4bf7111 | 2014-06-14 12:23:41 -0700 | [diff] [blame] | 1109 | "initrd=", initrd_addr_max, |
Roy Franz | 46f4582 | 2013-09-22 15:45:39 -0700 | [diff] [blame] | 1110 | &ramdisk_addr, &ramdisk_size); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1111 | if (status != EFI_SUCCESS) |
| 1112 | goto fail2; |
Yinghai Lu | 4bf7111 | 2014-06-14 12:23:41 -0700 | [diff] [blame] | 1113 | hdr->ramdisk_image = ramdisk_addr & 0xffffffff; |
| 1114 | hdr->ramdisk_size = ramdisk_size & 0xffffffff; |
| 1115 | boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32; |
| 1116 | boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1117 | |
| 1118 | return boot_params; |
| 1119 | fail2: |
Roy Franz | 0e1cadb | 2013-09-22 15:45:38 -0700 | [diff] [blame] | 1120 | efi_free(sys_table, options_size, hdr->cmd_line_ptr); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1121 | fail: |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 1122 | efi_free(sys_table, 0x4000, (unsigned long)boot_params); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1123 | return NULL; |
| 1124 | } |
| 1125 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1126 | static void add_e820ext(struct boot_params *params, |
| 1127 | struct setup_data *e820ext, u32 nr_entries) |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1128 | { |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1129 | struct setup_data *data; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1130 | efi_status_t status; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1131 | unsigned long size; |
| 1132 | |
| 1133 | e820ext->type = SETUP_E820_EXT; |
| 1134 | e820ext->len = nr_entries * sizeof(struct e820entry); |
| 1135 | e820ext->next = 0; |
| 1136 | |
| 1137 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
| 1138 | |
| 1139 | while (data && data->next) |
| 1140 | data = (struct setup_data *)(unsigned long)data->next; |
| 1141 | |
| 1142 | if (data) |
| 1143 | data->next = (unsigned long)e820ext; |
| 1144 | else |
| 1145 | params->hdr.setup_data = (unsigned long)e820ext; |
| 1146 | } |
| 1147 | |
| 1148 | static efi_status_t setup_e820(struct boot_params *params, |
| 1149 | struct setup_data *e820ext, u32 e820ext_size) |
| 1150 | { |
| 1151 | struct e820entry *e820_map = ¶ms->e820_map[0]; |
| 1152 | struct efi_info *efi = ¶ms->efi_info; |
| 1153 | struct e820entry *prev = NULL; |
| 1154 | u32 nr_entries; |
| 1155 | u32 nr_desc; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1156 | int i; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1157 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1158 | nr_entries = 0; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1159 | nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size; |
| 1160 | |
| 1161 | for (i = 0; i < nr_desc; i++) { |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1162 | efi_memory_desc_t *d; |
| 1163 | unsigned int e820_type = 0; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1164 | unsigned long m = efi->efi_memmap; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1165 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1166 | d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size)); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1167 | switch (d->type) { |
| 1168 | case EFI_RESERVED_TYPE: |
| 1169 | case EFI_RUNTIME_SERVICES_CODE: |
| 1170 | case EFI_RUNTIME_SERVICES_DATA: |
| 1171 | case EFI_MEMORY_MAPPED_IO: |
| 1172 | case EFI_MEMORY_MAPPED_IO_PORT_SPACE: |
| 1173 | case EFI_PAL_CODE: |
| 1174 | e820_type = E820_RESERVED; |
| 1175 | break; |
| 1176 | |
| 1177 | case EFI_UNUSABLE_MEMORY: |
| 1178 | e820_type = E820_UNUSABLE; |
| 1179 | break; |
| 1180 | |
| 1181 | case EFI_ACPI_RECLAIM_MEMORY: |
| 1182 | e820_type = E820_ACPI; |
| 1183 | break; |
| 1184 | |
| 1185 | case EFI_LOADER_CODE: |
| 1186 | case EFI_LOADER_DATA: |
| 1187 | case EFI_BOOT_SERVICES_CODE: |
| 1188 | case EFI_BOOT_SERVICES_DATA: |
| 1189 | case EFI_CONVENTIONAL_MEMORY: |
| 1190 | e820_type = E820_RAM; |
| 1191 | break; |
| 1192 | |
| 1193 | case EFI_ACPI_MEMORY_NVS: |
| 1194 | e820_type = E820_NVS; |
| 1195 | break; |
| 1196 | |
| 1197 | default: |
| 1198 | continue; |
| 1199 | } |
| 1200 | |
| 1201 | /* Merge adjacent mappings */ |
| 1202 | if (prev && prev->type == e820_type && |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1203 | (prev->addr + prev->size) == d->phys_addr) { |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1204 | prev->size += d->num_pages << 12; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1205 | continue; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1206 | } |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1207 | |
| 1208 | if (nr_entries == ARRAY_SIZE(params->e820_map)) { |
| 1209 | u32 need = (nr_desc - i) * sizeof(struct e820entry) + |
| 1210 | sizeof(struct setup_data); |
| 1211 | |
| 1212 | if (!e820ext || e820ext_size < need) |
| 1213 | return EFI_BUFFER_TOO_SMALL; |
| 1214 | |
| 1215 | /* boot_params map full, switch to e820 extended */ |
| 1216 | e820_map = (struct e820entry *)e820ext->data; |
| 1217 | } |
| 1218 | |
| 1219 | e820_map->addr = d->phys_addr; |
| 1220 | e820_map->size = d->num_pages << PAGE_SHIFT; |
| 1221 | e820_map->type = e820_type; |
| 1222 | prev = e820_map++; |
| 1223 | nr_entries++; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1226 | if (nr_entries > ARRAY_SIZE(params->e820_map)) { |
| 1227 | u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map); |
| 1228 | |
| 1229 | add_e820ext(params, e820ext, nr_e820ext); |
| 1230 | nr_entries -= nr_e820ext; |
| 1231 | } |
| 1232 | |
| 1233 | params->e820_entries = (u8)nr_entries; |
| 1234 | |
| 1235 | return EFI_SUCCESS; |
| 1236 | } |
| 1237 | |
| 1238 | static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, |
| 1239 | u32 *e820ext_size) |
| 1240 | { |
| 1241 | efi_status_t status; |
| 1242 | unsigned long size; |
| 1243 | |
| 1244 | size = sizeof(struct setup_data) + |
| 1245 | sizeof(struct e820entry) * nr_desc; |
| 1246 | |
| 1247 | if (*e820ext) { |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1248 | efi_call_early(free_pool, *e820ext); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1249 | *e820ext = NULL; |
| 1250 | *e820ext_size = 0; |
| 1251 | } |
| 1252 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1253 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 1254 | size, (void **)e820ext); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1255 | if (status == EFI_SUCCESS) |
| 1256 | *e820ext_size = size; |
| 1257 | |
| 1258 | return status; |
| 1259 | } |
| 1260 | |
| 1261 | static efi_status_t exit_boot(struct boot_params *boot_params, |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 1262 | void *handle, bool is64) |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1263 | { |
| 1264 | struct efi_info *efi = &boot_params->efi_info; |
| 1265 | unsigned long map_sz, key, desc_size; |
| 1266 | efi_memory_desc_t *mem_map; |
| 1267 | struct setup_data *e820ext; |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 1268 | const char *signature; |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1269 | __u32 e820ext_size; |
| 1270 | __u32 nr_desc, prev_nr_desc; |
| 1271 | efi_status_t status; |
| 1272 | __u32 desc_version; |
| 1273 | bool called_exit = false; |
| 1274 | u8 nr_entries; |
| 1275 | int i; |
| 1276 | |
| 1277 | nr_desc = 0; |
| 1278 | e820ext = NULL; |
| 1279 | e820ext_size = 0; |
| 1280 | |
| 1281 | get_map: |
| 1282 | status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size, |
| 1283 | &desc_version, &key); |
| 1284 | |
| 1285 | if (status != EFI_SUCCESS) |
| 1286 | return status; |
| 1287 | |
| 1288 | prev_nr_desc = nr_desc; |
| 1289 | nr_desc = map_sz / desc_size; |
| 1290 | if (nr_desc > prev_nr_desc && |
| 1291 | nr_desc > ARRAY_SIZE(boot_params->e820_map)) { |
| 1292 | u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map); |
| 1293 | |
| 1294 | status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size); |
| 1295 | if (status != EFI_SUCCESS) |
| 1296 | goto free_mem_map; |
| 1297 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1298 | efi_call_early(free_pool, mem_map); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1299 | goto get_map; /* Allocated memory, get map again */ |
| 1300 | } |
| 1301 | |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 1302 | signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE; |
| 1303 | memcpy(&efi->efi_loader_signature, signature, sizeof(__u32)); |
| 1304 | |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1305 | efi->efi_systab = (unsigned long)sys_table; |
| 1306 | efi->efi_memdesc_size = desc_size; |
| 1307 | efi->efi_memdesc_version = desc_version; |
| 1308 | efi->efi_memmap = (unsigned long)mem_map; |
| 1309 | efi->efi_memmap_size = map_sz; |
| 1310 | |
| 1311 | #ifdef CONFIG_X86_64 |
| 1312 | efi->efi_systab_hi = (unsigned long)sys_table >> 32; |
| 1313 | efi->efi_memmap_hi = (unsigned long)mem_map >> 32; |
| 1314 | #endif |
| 1315 | |
| 1316 | /* Might as well exit boot services now */ |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1317 | status = efi_call_early(exit_boot_services, handle, key); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1318 | if (status != EFI_SUCCESS) { |
| 1319 | /* |
| 1320 | * ExitBootServices() will fail if any of the event |
| 1321 | * handlers change the memory map. In which case, we |
| 1322 | * must be prepared to retry, but only once so that |
| 1323 | * we're guaranteed to exit on repeated failures instead |
| 1324 | * of spinning forever. |
| 1325 | */ |
| 1326 | if (called_exit) |
| 1327 | goto free_mem_map; |
| 1328 | |
| 1329 | called_exit = true; |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1330 | efi_call_early(free_pool, mem_map); |
Linn Crosetto | d2078d5 | 2013-09-22 19:59:08 -0600 | [diff] [blame] | 1331 | goto get_map; |
| 1332 | } |
| 1333 | |
| 1334 | /* Historic? */ |
| 1335 | boot_params->alt_mem_k = 32 * 1024; |
| 1336 | |
| 1337 | status = setup_e820(boot_params, e820ext, e820ext_size); |
| 1338 | if (status != EFI_SUCCESS) |
| 1339 | return status; |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1340 | |
| 1341 | return EFI_SUCCESS; |
| 1342 | |
| 1343 | free_mem_map: |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1344 | efi_call_early(free_pool, mem_map); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1345 | return status; |
| 1346 | } |
| 1347 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1348 | /* |
| 1349 | * On success we return a pointer to a boot_params structure, and NULL |
| 1350 | * on failure. |
| 1351 | */ |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1352 | struct boot_params *efi_main(struct efi_config *c, |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1353 | struct boot_params *boot_params) |
| 1354 | { |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1355 | struct desc_ptr *gdt = NULL; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1356 | efi_loaded_image_t *image; |
| 1357 | struct setup_header *hdr = &boot_params->hdr; |
| 1358 | efi_status_t status; |
| 1359 | struct desc_struct *desc; |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1360 | void *handle; |
| 1361 | efi_system_table_t *_table; |
| 1362 | bool is64; |
| 1363 | |
| 1364 | efi_early = c; |
| 1365 | |
| 1366 | _table = (efi_system_table_t *)(unsigned long)efi_early->table; |
| 1367 | handle = (void *)(unsigned long)efi_early->image_handle; |
| 1368 | is64 = efi_early->is64; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1369 | |
| 1370 | sys_table = _table; |
| 1371 | |
| 1372 | /* Check if we were booted by the EFI firmware */ |
| 1373 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) |
| 1374 | goto fail; |
| 1375 | |
Matt Fleming | 54b52d8 | 2014-01-10 15:27:14 +0000 | [diff] [blame] | 1376 | if (is64) |
| 1377 | setup_boot_services64(efi_early); |
| 1378 | else |
| 1379 | setup_boot_services32(efi_early); |
| 1380 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1381 | setup_graphics(boot_params); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1382 | |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1383 | status = setup_efi_pci(boot_params); |
| 1384 | if (status != EFI_SUCCESS) { |
| 1385 | efi_printk(sys_table, "setup_efi_pci() failed!\n"); |
| 1386 | } |
Matthew Garrett | dd5fc85 | 2012-12-05 14:33:26 -0700 | [diff] [blame] | 1387 | |
Matt Fleming | 204b0a1 | 2014-03-22 10:09:01 +0000 | [diff] [blame] | 1388 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
| 1389 | sizeof(*gdt), (void **)&gdt); |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1390 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1391 | efi_printk(sys_table, "Failed to alloc mem for gdt structure\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1392 | goto fail; |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1393 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1394 | |
| 1395 | gdt->size = 0x800; |
Roy Franz | 40e4530 | 2013-09-22 15:45:29 -0700 | [diff] [blame] | 1396 | status = efi_low_alloc(sys_table, gdt->size, 8, |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1397 | (unsigned long *)&gdt->address); |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1398 | if (status != EFI_SUCCESS) { |
Roy Franz | 876dc36 | 2013-09-22 15:45:28 -0700 | [diff] [blame] | 1399 | efi_printk(sys_table, "Failed to alloc mem for gdt\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1400 | goto fail; |
Matt Fleming | 9fa7ded | 2012-02-20 13:20:59 +0000 | [diff] [blame] | 1401 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1402 | |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1403 | /* |
| 1404 | * If the kernel isn't already loaded at the preferred load |
| 1405 | * address, relocate it. |
| 1406 | */ |
| 1407 | if (hdr->pref_address != hdr->code32_start) { |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 1408 | unsigned long bzimage_addr = hdr->code32_start; |
| 1409 | status = efi_relocate_kernel(sys_table, &bzimage_addr, |
| 1410 | hdr->init_size, hdr->init_size, |
| 1411 | hdr->pref_address, |
| 1412 | hdr->kernel_alignment); |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1413 | if (status != EFI_SUCCESS) { |
| 1414 | efi_printk(sys_table, "efi_relocate_kernel() failed!\n"); |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1415 | goto fail; |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1416 | } |
Roy Franz | 4a9f3a7 | 2013-09-22 15:45:32 -0700 | [diff] [blame] | 1417 | |
| 1418 | hdr->pref_address = hdr->code32_start; |
| 1419 | hdr->code32_start = bzimage_addr; |
Matt Fleming | 9ca8f72 | 2012-07-19 10:23:48 +0100 | [diff] [blame] | 1420 | } |
| 1421 | |
Matt Fleming | b8ff87a | 2014-01-10 15:54:31 +0000 | [diff] [blame] | 1422 | status = exit_boot(boot_params, handle, is64); |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1423 | if (status != EFI_SUCCESS) { |
| 1424 | efi_printk(sys_table, "exit_boot() failed!\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1425 | goto fail; |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1426 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1427 | |
| 1428 | memset((char *)gdt->address, 0x0, gdt->size); |
| 1429 | desc = (struct desc_struct *)gdt->address; |
| 1430 | |
| 1431 | /* The first GDT is a dummy and the second is unused. */ |
| 1432 | desc += 2; |
| 1433 | |
| 1434 | desc->limit0 = 0xffff; |
| 1435 | desc->base0 = 0x0000; |
| 1436 | desc->base1 = 0x0000; |
| 1437 | desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ; |
| 1438 | desc->s = DESC_TYPE_CODE_DATA; |
| 1439 | desc->dpl = 0; |
| 1440 | desc->p = 1; |
| 1441 | desc->limit = 0xf; |
| 1442 | desc->avl = 0; |
| 1443 | desc->l = 0; |
| 1444 | desc->d = SEG_OP_SIZE_32BIT; |
| 1445 | desc->g = SEG_GRANULARITY_4KB; |
| 1446 | desc->base2 = 0x00; |
| 1447 | |
| 1448 | desc++; |
| 1449 | desc->limit0 = 0xffff; |
| 1450 | desc->base0 = 0x0000; |
| 1451 | desc->base1 = 0x0000; |
| 1452 | desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE; |
| 1453 | desc->s = DESC_TYPE_CODE_DATA; |
| 1454 | desc->dpl = 0; |
| 1455 | desc->p = 1; |
| 1456 | desc->limit = 0xf; |
| 1457 | desc->avl = 0; |
| 1458 | desc->l = 0; |
| 1459 | desc->d = SEG_OP_SIZE_32BIT; |
| 1460 | desc->g = SEG_GRANULARITY_4KB; |
| 1461 | desc->base2 = 0x00; |
| 1462 | |
| 1463 | #ifdef CONFIG_X86_64 |
| 1464 | /* Task segment value */ |
| 1465 | desc++; |
| 1466 | desc->limit0 = 0x0000; |
| 1467 | desc->base0 = 0x0000; |
| 1468 | desc->base1 = 0x0000; |
| 1469 | desc->type = SEG_TYPE_TSS; |
| 1470 | desc->s = 0; |
| 1471 | desc->dpl = 0; |
| 1472 | desc->p = 1; |
| 1473 | desc->limit = 0x0; |
| 1474 | desc->avl = 0; |
| 1475 | desc->l = 0; |
| 1476 | desc->d = 0; |
| 1477 | desc->g = SEG_GRANULARITY_4KB; |
| 1478 | desc->base2 = 0x00; |
| 1479 | #endif /* CONFIG_X86_64 */ |
| 1480 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1481 | asm volatile("cli"); |
Bart Kuivenhoven | 0ce6cda | 2013-09-23 11:45:28 +0200 | [diff] [blame] | 1482 | asm volatile ("lgdt %0" : : "m" (*gdt)); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1483 | |
| 1484 | return boot_params; |
| 1485 | fail: |
Ulf Winkelvos | fb86b24 | 2014-07-10 02:12:41 +0200 | [diff] [blame] | 1486 | efi_printk(sys_table, "efi_main() failed!\n"); |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 1487 | return NULL; |
| 1488 | } |