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