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