Linux Audio

Check our new training course

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