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