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