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