Linux Audio

Check our new training course

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