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