blob: d5e4044505d3ea69f307ce1a11c6f9a02f0dda93 [file] [log] [blame]
Matt Fleming291f3632011-12-12 21:27:52 +00001/* -----------------------------------------------------------------------
2 *
3 * Copyright 2011 Intel Corporation; author Matt Fleming
4 *
5 * This file is part of the Linux kernel, and is made available under
6 * the terms of the GNU General Public License version 2.
7 *
8 * ----------------------------------------------------------------------- */
9
10#include <linux/efi.h>
11#include <asm/efi.h>
12#include <asm/setup.h>
13#include <asm/desc.h>
14
15#include "eboot.h"
16
17static efi_system_table_t *sys_table;
18
Matt Fleming9fa7ded2012-02-20 13:20:59 +000019static void efi_printk(char *str)
20{
21 char *s8;
22
23 for (s8 = str; *s8; s8++) {
24 struct efi_simple_text_output_protocol *out;
25 efi_char16_t ch[2] = { 0 };
26
27 ch[0] = *s8;
28 out = (struct efi_simple_text_output_protocol *)sys_table->con_out;
29
30 if (*s8 == '\n') {
31 efi_char16_t nl[2] = { '\r', 0 };
32 efi_call_phys2(out->output_string, out, nl);
33 }
34
35 efi_call_phys2(out->output_string, out, ch);
36 }
37}
38
Matt Fleming291f3632011-12-12 21:27:52 +000039static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size,
40 unsigned long *desc_size)
41{
42 efi_memory_desc_t *m = NULL;
43 efi_status_t status;
44 unsigned long key;
45 u32 desc_version;
46
47 *map_size = sizeof(*m) * 32;
48again:
49 /*
50 * Add an additional efi_memory_desc_t because we're doing an
51 * allocation which may be in a new descriptor region.
52 */
53 *map_size += sizeof(*m);
54 status = efi_call_phys3(sys_table->boottime->allocate_pool,
55 EFI_LOADER_DATA, *map_size, (void **)&m);
56 if (status != EFI_SUCCESS)
57 goto fail;
58
59 status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size,
60 m, &key, desc_size, &desc_version);
61 if (status == EFI_BUFFER_TOO_SMALL) {
62 efi_call_phys1(sys_table->boottime->free_pool, m);
63 goto again;
64 }
65
66 if (status != EFI_SUCCESS)
67 efi_call_phys1(sys_table->boottime->free_pool, m);
68
69fail:
70 *map = m;
71 return status;
72}
73
74/*
75 * Allocate at the highest possible address that is not above 'max'.
76 */
77static efi_status_t high_alloc(unsigned long size, unsigned long align,
78 unsigned long *addr, unsigned long max)
79{
80 unsigned long map_size, desc_size;
81 efi_memory_desc_t *map;
82 efi_status_t status;
83 unsigned long nr_pages;
84 u64 max_addr = 0;
85 int i;
86
87 status = __get_map(&map, &map_size, &desc_size);
88 if (status != EFI_SUCCESS)
89 goto fail;
90
91 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
92again:
93 for (i = 0; i < map_size / desc_size; i++) {
94 efi_memory_desc_t *desc;
95 unsigned long m = (unsigned long)map;
96 u64 start, end;
97
98 desc = (efi_memory_desc_t *)(m + (i * desc_size));
99 if (desc->type != EFI_CONVENTIONAL_MEMORY)
100 continue;
101
102 if (desc->num_pages < nr_pages)
103 continue;
104
105 start = desc->phys_addr;
106 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
107
108 if ((start + size) > end || (start + size) > max)
109 continue;
110
111 if (end - size > max)
112 end = max;
113
114 if (round_down(end - size, align) < start)
115 continue;
116
117 start = round_down(end - size, align);
118
119 /*
120 * Don't allocate at 0x0. It will confuse code that
121 * checks pointers against NULL.
122 */
123 if (start == 0x0)
124 continue;
125
126 if (start > max_addr)
127 max_addr = start;
128 }
129
130 if (!max_addr)
131 status = EFI_NOT_FOUND;
132 else {
133 status = efi_call_phys4(sys_table->boottime->allocate_pages,
134 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
135 nr_pages, &max_addr);
136 if (status != EFI_SUCCESS) {
137 max = max_addr;
138 max_addr = 0;
139 goto again;
140 }
141
142 *addr = max_addr;
143 }
144
145free_pool:
146 efi_call_phys1(sys_table->boottime->free_pool, map);
147
148fail:
149 return status;
150}
151
152/*
153 * Allocate at the lowest possible address.
154 */
155static efi_status_t low_alloc(unsigned long size, unsigned long align,
156 unsigned long *addr)
157{
158 unsigned long map_size, desc_size;
159 efi_memory_desc_t *map;
160 efi_status_t status;
161 unsigned long nr_pages;
162 int i;
163
164 status = __get_map(&map, &map_size, &desc_size);
165 if (status != EFI_SUCCESS)
166 goto fail;
167
168 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
169 for (i = 0; i < map_size / desc_size; i++) {
170 efi_memory_desc_t *desc;
171 unsigned long m = (unsigned long)map;
172 u64 start, end;
173
174 desc = (efi_memory_desc_t *)(m + (i * desc_size));
175
176 if (desc->type != EFI_CONVENTIONAL_MEMORY)
177 continue;
178
179 if (desc->num_pages < nr_pages)
180 continue;
181
182 start = desc->phys_addr;
183 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
184
185 /*
186 * Don't allocate at 0x0. It will confuse code that
187 * checks pointers against NULL. Skip the first 8
188 * bytes so we start at a nice even number.
189 */
190 if (start == 0x0)
191 start += 8;
192
193 start = round_up(start, align);
194 if ((start + size) > end)
195 continue;
196
197 status = efi_call_phys4(sys_table->boottime->allocate_pages,
198 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
199 nr_pages, &start);
200 if (status == EFI_SUCCESS) {
201 *addr = start;
202 break;
203 }
204 }
205
206 if (i == map_size / desc_size)
207 status = EFI_NOT_FOUND;
208
209free_pool:
210 efi_call_phys1(sys_table->boottime->free_pool, map);
211fail:
212 return status;
213}
214
215static void low_free(unsigned long size, unsigned long addr)
216{
217 unsigned long nr_pages;
218
219 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
220 efi_call_phys2(sys_table->boottime->free_pages, addr, size);
221}
222
223static void find_bits(unsigned long mask, u8 *pos, u8 *size)
224{
225 u8 first, len;
226
227 first = 0;
228 len = 0;
229
230 if (mask) {
231 while (!(mask & 0x1)) {
232 mask = mask >> 1;
233 first++;
234 }
235
236 while (mask & 0x1) {
237 mask = mask >> 1;
238 len++;
239 }
240 }
241
242 *pos = first;
243 *size = len;
244}
245
246/*
247 * See if we have Graphics Output Protocol
248 */
249static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
250 unsigned long size)
251{
252 struct efi_graphics_output_protocol *gop, *first_gop;
253 struct efi_pixel_bitmask pixel_info;
254 unsigned long nr_gops;
255 efi_status_t status;
256 void **gop_handle;
257 u16 width, height;
258 u32 fb_base, fb_size;
259 u32 pixels_per_scan_line;
260 int pixel_format;
261 int i;
262
263 status = efi_call_phys3(sys_table->boottime->allocate_pool,
264 EFI_LOADER_DATA, size, &gop_handle);
265 if (status != EFI_SUCCESS)
266 return status;
267
268 status = efi_call_phys5(sys_table->boottime->locate_handle,
269 EFI_LOCATE_BY_PROTOCOL, proto,
270 NULL, &size, gop_handle);
271 if (status != EFI_SUCCESS)
272 goto free_handle;
273
274 first_gop = NULL;
275
276 nr_gops = size / sizeof(void *);
277 for (i = 0; i < nr_gops; i++) {
278 struct efi_graphics_output_mode_info *info;
Matthew Garrett38cb5ef2012-07-26 18:00:27 -0400279 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
280 bool conout_found = false;
281 void *dummy;
Matt Fleming291f3632011-12-12 21:27:52 +0000282 void *h = gop_handle[i];
283
284 status = efi_call_phys3(sys_table->boottime->handle_protocol,
285 h, proto, &gop);
286 if (status != EFI_SUCCESS)
287 continue;
288
Matthew Garrett38cb5ef2012-07-26 18:00:27 -0400289 status = efi_call_phys3(sys_table->boottime->handle_protocol,
290 h, &conout_proto, &dummy);
291
292 if (status == EFI_SUCCESS)
293 conout_found = true;
Matt Fleming291f3632011-12-12 21:27:52 +0000294
295 status = efi_call_phys4(gop->query_mode, gop,
296 gop->mode->mode, &size, &info);
Matthew Garrett38cb5ef2012-07-26 18:00:27 -0400297 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
Matt Fleming291f3632011-12-12 21:27:52 +0000298 /*
Matthew Garrett38cb5ef2012-07-26 18:00:27 -0400299 * Systems that use the UEFI Console Splitter may
300 * provide multiple GOP devices, not all of which are
301 * backed by real hardware. The workaround is to search
302 * for a GOP implementing the ConOut protocol, and if
303 * one isn't found, to just fall back to the first GOP.
Matt Fleming291f3632011-12-12 21:27:52 +0000304 */
305 width = info->horizontal_resolution;
306 height = info->vertical_resolution;
307 fb_base = gop->mode->frame_buffer_base;
308 fb_size = gop->mode->frame_buffer_size;
309 pixel_format = info->pixel_format;
310 pixel_info = info->pixel_information;
311 pixels_per_scan_line = info->pixels_per_scan_line;
312
313 /*
Matthew Garrett38cb5ef2012-07-26 18:00:27 -0400314 * Once we've found a GOP supporting ConOut,
Matt Fleming291f3632011-12-12 21:27:52 +0000315 * don't bother looking any further.
316 */
Matthew Garrett38cb5ef2012-07-26 18:00:27 -0400317 if (conout_found)
Matt Fleming291f3632011-12-12 21:27:52 +0000318 break;
319
320 first_gop = gop;
321 }
322 }
323
324 /* Did we find any GOPs? */
325 if (!first_gop)
326 goto free_handle;
327
328 /* EFI framebuffer */
329 si->orig_video_isVGA = VIDEO_TYPE_EFI;
330
331 si->lfb_width = width;
332 si->lfb_height = height;
333 si->lfb_base = fb_base;
334 si->lfb_size = fb_size;
335 si->pages = 1;
336
337 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
338 si->lfb_depth = 32;
339 si->lfb_linelength = pixels_per_scan_line * 4;
340 si->red_size = 8;
341 si->red_pos = 0;
342 si->green_size = 8;
343 si->green_pos = 8;
344 si->blue_size = 8;
345 si->blue_pos = 16;
346 si->rsvd_size = 8;
347 si->rsvd_pos = 24;
348 } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
349 si->lfb_depth = 32;
350 si->lfb_linelength = pixels_per_scan_line * 4;
351 si->red_size = 8;
352 si->red_pos = 16;
353 si->green_size = 8;
354 si->green_pos = 8;
355 si->blue_size = 8;
356 si->blue_pos = 0;
357 si->rsvd_size = 8;
358 si->rsvd_pos = 24;
359 } else if (pixel_format == PIXEL_BIT_MASK) {
360 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
361 find_bits(pixel_info.green_mask, &si->green_pos,
362 &si->green_size);
363 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
364 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
365 &si->rsvd_size);
366 si->lfb_depth = si->red_size + si->green_size +
367 si->blue_size + si->rsvd_size;
368 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
369 } else {
370 si->lfb_depth = 4;
371 si->lfb_linelength = si->lfb_width / 2;
372 si->red_size = 0;
373 si->red_pos = 0;
374 si->green_size = 0;
375 si->green_pos = 0;
376 si->blue_size = 0;
377 si->blue_pos = 0;
378 si->rsvd_size = 0;
379 si->rsvd_pos = 0;
380 }
381
382free_handle:
383 efi_call_phys1(sys_table->boottime->free_pool, gop_handle);
384 return status;
385}
386
387/*
388 * See if we have Universal Graphics Adapter (UGA) protocol
389 */
390static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
391 unsigned long size)
392{
393 struct efi_uga_draw_protocol *uga, *first_uga;
394 unsigned long nr_ugas;
395 efi_status_t status;
396 u32 width, height;
397 void **uga_handle = NULL;
398 int i;
399
400 status = efi_call_phys3(sys_table->boottime->allocate_pool,
401 EFI_LOADER_DATA, size, &uga_handle);
402 if (status != EFI_SUCCESS)
403 return status;
404
405 status = efi_call_phys5(sys_table->boottime->locate_handle,
406 EFI_LOCATE_BY_PROTOCOL, uga_proto,
407 NULL, &size, uga_handle);
408 if (status != EFI_SUCCESS)
409 goto free_handle;
410
411 first_uga = NULL;
412
413 nr_ugas = size / sizeof(void *);
414 for (i = 0; i < nr_ugas; i++) {
415 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
416 void *handle = uga_handle[i];
417 u32 w, h, depth, refresh;
418 void *pciio;
419
420 status = efi_call_phys3(sys_table->boottime->handle_protocol,
421 handle, uga_proto, &uga);
422 if (status != EFI_SUCCESS)
423 continue;
424
425 efi_call_phys3(sys_table->boottime->handle_protocol,
426 handle, &pciio_proto, &pciio);
427
428 status = efi_call_phys5(uga->get_mode, uga, &w, &h,
429 &depth, &refresh);
430 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
431 width = w;
432 height = h;
433
434 /*
435 * Once we've found a UGA supporting PCIIO,
436 * don't bother looking any further.
437 */
438 if (pciio)
439 break;
440
441 first_uga = uga;
442 }
443 }
444
445 if (!first_uga)
446 goto free_handle;
447
448 /* EFI framebuffer */
449 si->orig_video_isVGA = VIDEO_TYPE_EFI;
450
451 si->lfb_depth = 32;
452 si->lfb_width = width;
453 si->lfb_height = height;
454
455 si->red_size = 8;
456 si->red_pos = 16;
457 si->green_size = 8;
458 si->green_pos = 8;
459 si->blue_size = 8;
460 si->blue_pos = 0;
461 si->rsvd_size = 8;
462 si->rsvd_pos = 24;
463
464
465free_handle:
466 efi_call_phys1(sys_table->boottime->free_pool, uga_handle);
467 return status;
468}
469
470void setup_graphics(struct boot_params *boot_params)
471{
472 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
473 struct screen_info *si;
474 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
475 efi_status_t status;
476 unsigned long size;
477 void **gop_handle = NULL;
478 void **uga_handle = NULL;
479
480 si = &boot_params->screen_info;
481 memset(si, 0, sizeof(*si));
482
483 size = 0;
484 status = efi_call_phys5(sys_table->boottime->locate_handle,
485 EFI_LOCATE_BY_PROTOCOL, &graphics_proto,
486 NULL, &size, gop_handle);
487 if (status == EFI_BUFFER_TOO_SMALL)
488 status = setup_gop(si, &graphics_proto, size);
489
490 if (status != EFI_SUCCESS) {
491 size = 0;
492 status = efi_call_phys5(sys_table->boottime->locate_handle,
493 EFI_LOCATE_BY_PROTOCOL, &uga_proto,
494 NULL, &size, uga_handle);
495 if (status == EFI_BUFFER_TOO_SMALL)
496 setup_uga(si, &uga_proto, size);
497 }
498}
499
500struct initrd {
501 efi_file_handle_t *handle;
502 u64 size;
503};
504
505/*
506 * Check the cmdline for a LILO-style initrd= arguments.
507 *
508 * We only support loading an initrd from the same filesystem as the
509 * kernel image.
510 */
511static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
512 struct setup_header *hdr)
513{
514 struct initrd *initrds;
515 unsigned long initrd_addr;
516 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
517 u64 initrd_total;
518 efi_file_io_interface_t *io;
519 efi_file_handle_t *fh;
520 efi_status_t status;
521 int nr_initrds;
522 char *str;
523 int i, j, k;
524
525 initrd_addr = 0;
526 initrd_total = 0;
527
528 str = (char *)(unsigned long)hdr->cmd_line_ptr;
529
530 j = 0; /* See close_handles */
531
532 if (!str || !*str)
533 return EFI_SUCCESS;
534
535 for (nr_initrds = 0; *str; nr_initrds++) {
536 str = strstr(str, "initrd=");
537 if (!str)
538 break;
539
540 str += 7;
541
542 /* Skip any leading slashes */
543 while (*str == '/' || *str == '\\')
544 str++;
545
546 while (*str && *str != ' ' && *str != '\n')
547 str++;
548 }
549
550 if (!nr_initrds)
551 return EFI_SUCCESS;
552
553 status = efi_call_phys3(sys_table->boottime->allocate_pool,
554 EFI_LOADER_DATA,
555 nr_initrds * sizeof(*initrds),
556 &initrds);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000557 if (status != EFI_SUCCESS) {
558 efi_printk("Failed to alloc mem for initrds\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000559 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000560 }
Matt Fleming291f3632011-12-12 21:27:52 +0000561
562 str = (char *)(unsigned long)hdr->cmd_line_ptr;
563 for (i = 0; i < nr_initrds; i++) {
564 struct initrd *initrd;
565 efi_file_handle_t *h;
566 efi_file_info_t *info;
Dan Carpenterc7b73832012-03-05 21:06:14 +0300567 efi_char16_t filename_16[256];
Matt Fleming291f3632011-12-12 21:27:52 +0000568 unsigned long info_sz;
569 efi_guid_t info_guid = EFI_FILE_INFO_ID;
570 efi_char16_t *p;
571 u64 file_sz;
572
573 str = strstr(str, "initrd=");
574 if (!str)
575 break;
576
577 str += 7;
578
579 initrd = &initrds[i];
Dan Carpenterc7b73832012-03-05 21:06:14 +0300580 p = filename_16;
Matt Fleming291f3632011-12-12 21:27:52 +0000581
582 /* Skip any leading slashes */
583 while (*str == '/' || *str == '\\')
584 str++;
585
586 while (*str && *str != ' ' && *str != '\n') {
Dan Carpenterc7b73832012-03-05 21:06:14 +0300587 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
Matt Fleming291f3632011-12-12 21:27:52 +0000588 break;
589
590 *p++ = *str++;
591 }
592
593 *p = '\0';
594
595 /* Only open the volume once. */
596 if (!i) {
597 efi_boot_services_t *boottime;
598
599 boottime = sys_table->boottime;
600
601 status = efi_call_phys3(boottime->handle_protocol,
602 image->device_handle, &fs_proto, &io);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000603 if (status != EFI_SUCCESS) {
604 efi_printk("Failed to handle fs_proto\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000605 goto free_initrds;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000606 }
Matt Fleming291f3632011-12-12 21:27:52 +0000607
608 status = efi_call_phys2(io->open_volume, io, &fh);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000609 if (status != EFI_SUCCESS) {
610 efi_printk("Failed to open volume\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000611 goto free_initrds;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000612 }
Matt Fleming291f3632011-12-12 21:27:52 +0000613 }
614
Dan Carpenterc7b73832012-03-05 21:06:14 +0300615 status = efi_call_phys5(fh->open, fh, &h, filename_16,
Matt Fleming291f3632011-12-12 21:27:52 +0000616 EFI_FILE_MODE_READ, (u64)0);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000617 if (status != EFI_SUCCESS) {
618 efi_printk("Failed to open initrd file\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000619 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000620 }
Matt Fleming291f3632011-12-12 21:27:52 +0000621
622 initrd->handle = h;
623
624 info_sz = 0;
625 status = efi_call_phys4(h->get_info, h, &info_guid,
626 &info_sz, NULL);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000627 if (status != EFI_BUFFER_TOO_SMALL) {
628 efi_printk("Failed to get initrd info size\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000629 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000630 }
Matt Fleming291f3632011-12-12 21:27:52 +0000631
632grow:
633 status = efi_call_phys3(sys_table->boottime->allocate_pool,
634 EFI_LOADER_DATA, info_sz, &info);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000635 if (status != EFI_SUCCESS) {
636 efi_printk("Failed to alloc mem for initrd info\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000637 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000638 }
Matt Fleming291f3632011-12-12 21:27:52 +0000639
640 status = efi_call_phys4(h->get_info, h, &info_guid,
641 &info_sz, info);
642 if (status == EFI_BUFFER_TOO_SMALL) {
643 efi_call_phys1(sys_table->boottime->free_pool, info);
644 goto grow;
645 }
646
647 file_sz = info->file_size;
648 efi_call_phys1(sys_table->boottime->free_pool, info);
649
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000650 if (status != EFI_SUCCESS) {
651 efi_printk("Failed to get initrd info\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000652 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000653 }
Matt Fleming291f3632011-12-12 21:27:52 +0000654
655 initrd->size = file_sz;
656 initrd_total += file_sz;
657 }
658
659 if (initrd_total) {
660 unsigned long addr;
661
662 /*
663 * Multiple initrd's need to be at consecutive
664 * addresses in memory, so allocate enough memory for
665 * all the initrd's.
666 */
667 status = high_alloc(initrd_total, 0x1000,
668 &initrd_addr, hdr->initrd_addr_max);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000669 if (status != EFI_SUCCESS) {
670 efi_printk("Failed to alloc highmem for initrds\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000671 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000672 }
Matt Fleming291f3632011-12-12 21:27:52 +0000673
674 /* We've run out of free low memory. */
675 if (initrd_addr > hdr->initrd_addr_max) {
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000676 efi_printk("We've run out of free low memory\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000677 status = EFI_INVALID_PARAMETER;
678 goto free_initrd_total;
679 }
680
681 addr = initrd_addr;
682 for (j = 0; j < nr_initrds; j++) {
683 u64 size;
684
685 size = initrds[j].size;
Maarten Lankhorst2d2da60f2011-12-16 13:30:58 +0100686 while (size) {
687 u64 chunksize;
688 if (size > EFI_READ_CHUNK_SIZE)
689 chunksize = EFI_READ_CHUNK_SIZE;
690 else
691 chunksize = size;
692 status = efi_call_phys3(fh->read,
693 initrds[j].handle,
694 &chunksize, addr);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000695 if (status != EFI_SUCCESS) {
696 efi_printk("Failed to read initrd\n");
Maarten Lankhorst2d2da60f2011-12-16 13:30:58 +0100697 goto free_initrd_total;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000698 }
Maarten Lankhorst2d2da60f2011-12-16 13:30:58 +0100699 addr += chunksize;
700 size -= chunksize;
701 }
Matt Fleming291f3632011-12-12 21:27:52 +0000702
703 efi_call_phys1(fh->close, initrds[j].handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000704 }
705
706 }
707
708 efi_call_phys1(sys_table->boottime->free_pool, initrds);
709
710 hdr->ramdisk_image = initrd_addr;
711 hdr->ramdisk_size = initrd_total;
712
713 return status;
714
715free_initrd_total:
716 low_free(initrd_total, initrd_addr);
717
718close_handles:
Matt Fleming30dc0d02012-03-15 19:13:25 +0000719 for (k = j; k < i; k++)
Matt Fleming291f3632011-12-12 21:27:52 +0000720 efi_call_phys1(fh->close, initrds[k].handle);
721free_initrds:
722 efi_call_phys1(sys_table->boottime->free_pool, initrds);
723fail:
724 hdr->ramdisk_image = 0;
725 hdr->ramdisk_size = 0;
726
727 return status;
728}
729
730/*
731 * Because the x86 boot code expects to be passed a boot_params we
732 * need to create one ourselves (usually the bootloader would create
733 * one for us).
734 */
Matt Fleming9ca8f722012-07-19 10:23:48 +0100735struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
Matt Fleming291f3632011-12-12 21:27:52 +0000736{
Matt Fleming9ca8f722012-07-19 10:23:48 +0100737 struct boot_params *boot_params;
738 struct sys_desc_table *sdt;
739 struct apm_bios_info *bi;
740 struct setup_header *hdr;
741 struct efi_info *efi;
742 efi_loaded_image_t *image;
743 void *options;
744 u32 load_options_size;
745 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000746 int options_size = 0;
747 efi_status_t status;
Matt Fleming291f3632011-12-12 21:27:52 +0000748 unsigned long cmdline;
Matt Fleming291f3632011-12-12 21:27:52 +0000749 u16 *s2;
750 u8 *s1;
751 int i;
752
Matt Fleming9ca8f722012-07-19 10:23:48 +0100753 sys_table = _table;
754
755 /* Check if we were booted by the EFI firmware */
756 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
757 return NULL;
758
759 status = efi_call_phys3(sys_table->boottime->handle_protocol,
760 handle, &proto, (void *)&image);
761 if (status != EFI_SUCCESS) {
762 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
763 return NULL;
764 }
765
766 status = low_alloc(0x4000, 1, (unsigned long *)&boot_params);
767 if (status != EFI_SUCCESS) {
768 efi_printk("Failed to alloc lowmem for boot params\n");
769 return NULL;
770 }
771
772 memset(boot_params, 0x0, 0x4000);
773
774 hdr = &boot_params->hdr;
775 efi = &boot_params->efi_info;
776 bi = &boot_params->apm_bios_info;
777 sdt = &boot_params->sys_desc_table;
778
779 /* Copy the second sector to boot_params */
780 memcpy(&hdr->jump, image->image_base + 512, 512);
781
782 /*
783 * Fill out some of the header fields ourselves because the
784 * EFI firmware loader doesn't load the first sector.
785 */
786 hdr->root_flags = 1;
787 hdr->vid_mode = 0xffff;
788 hdr->boot_flag = 0xAA55;
789
790 hdr->code32_start = (__u64)(unsigned long)image->image_base;
791
Matt Fleming291f3632011-12-12 21:27:52 +0000792 hdr->type_of_loader = 0x21;
793
794 /* Convert unicode cmdline to ascii */
Matt Fleming9ca8f722012-07-19 10:23:48 +0100795 options = image->load_options;
796 load_options_size = image->load_options_size / 2; /* ASCII */
Matt Fleming291f3632011-12-12 21:27:52 +0000797 cmdline = 0;
798 s2 = (u16 *)options;
799
800 if (s2) {
801 while (*s2 && *s2 != '\n' && options_size < load_options_size) {
802 s2++;
803 options_size++;
804 }
805
806 if (options_size) {
807 if (options_size > hdr->cmdline_size)
808 options_size = hdr->cmdline_size;
809
810 options_size++; /* NUL termination */
811
812 status = low_alloc(options_size, 1, &cmdline);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000813 if (status != EFI_SUCCESS) {
814 efi_printk("Failed to alloc mem for cmdline\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000815 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000816 }
Matt Fleming291f3632011-12-12 21:27:52 +0000817
818 s1 = (u8 *)(unsigned long)cmdline;
819 s2 = (u16 *)options;
820
821 for (i = 0; i < options_size - 1; i++)
822 *s1++ = *s2++;
823
824 *s1 = '\0';
825 }
826 }
827
828 hdr->cmd_line_ptr = cmdline;
829
830 hdr->ramdisk_image = 0;
831 hdr->ramdisk_size = 0;
832
Matt Fleming291f3632011-12-12 21:27:52 +0000833 /* Clear APM BIOS info */
834 memset(bi, 0, sizeof(*bi));
835
836 memset(sdt, 0, sizeof(*sdt));
837
Matt Fleming9ca8f722012-07-19 10:23:48 +0100838 status = handle_ramdisks(image, hdr);
839 if (status != EFI_SUCCESS)
840 goto fail2;
841
842 return boot_params;
843fail2:
844 if (options_size)
845 low_free(options_size, hdr->cmd_line_ptr);
846fail:
847 low_free(0x4000, (unsigned long)boot_params);
848 return NULL;
849}
850
851static efi_status_t exit_boot(struct boot_params *boot_params,
852 void *handle)
853{
854 struct efi_info *efi = &boot_params->efi_info;
855 struct e820entry *e820_map = &boot_params->e820_map[0];
856 struct e820entry *prev = NULL;
857 unsigned long size, key, desc_size, _size;
858 efi_memory_desc_t *mem_map;
859 efi_status_t status;
860 __u32 desc_version;
861 u8 nr_entries;
862 int i;
Matt Fleming291f3632011-12-12 21:27:52 +0000863
864 size = sizeof(*mem_map) * 32;
865
866again:
867 size += sizeof(*mem_map);
868 _size = size;
869 status = low_alloc(size, 1, (unsigned long *)&mem_map);
870 if (status != EFI_SUCCESS)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100871 return status;
Matt Fleming291f3632011-12-12 21:27:52 +0000872
873 status = efi_call_phys5(sys_table->boottime->get_memory_map, &size,
874 mem_map, &key, &desc_size, &desc_version);
875 if (status == EFI_BUFFER_TOO_SMALL) {
876 low_free(_size, (unsigned long)mem_map);
877 goto again;
878 }
879
880 if (status != EFI_SUCCESS)
881 goto free_mem_map;
882
Matt Fleming9ca8f722012-07-19 10:23:48 +0100883 memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32));
Matt Fleming291f3632011-12-12 21:27:52 +0000884 efi->efi_systab = (unsigned long)sys_table;
885 efi->efi_memdesc_size = desc_size;
886 efi->efi_memdesc_version = desc_version;
887 efi->efi_memmap = (unsigned long)mem_map;
888 efi->efi_memmap_size = size;
889
890#ifdef CONFIG_X86_64
891 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
892 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
893#endif
894
895 /* Might as well exit boot services now */
896 status = efi_call_phys2(sys_table->boottime->exit_boot_services,
897 handle, key);
898 if (status != EFI_SUCCESS)
899 goto free_mem_map;
900
901 /* Historic? */
902 boot_params->alt_mem_k = 32 * 1024;
903
904 /*
905 * Convert the EFI memory map to E820.
906 */
907 nr_entries = 0;
908 for (i = 0; i < size / desc_size; i++) {
909 efi_memory_desc_t *d;
910 unsigned int e820_type = 0;
911 unsigned long m = (unsigned long)mem_map;
912
913 d = (efi_memory_desc_t *)(m + (i * desc_size));
914 switch (d->type) {
915 case EFI_RESERVED_TYPE:
916 case EFI_RUNTIME_SERVICES_CODE:
917 case EFI_RUNTIME_SERVICES_DATA:
918 case EFI_MEMORY_MAPPED_IO:
919 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
920 case EFI_PAL_CODE:
921 e820_type = E820_RESERVED;
922 break;
923
924 case EFI_UNUSABLE_MEMORY:
925 e820_type = E820_UNUSABLE;
926 break;
927
928 case EFI_ACPI_RECLAIM_MEMORY:
929 e820_type = E820_ACPI;
930 break;
931
932 case EFI_LOADER_CODE:
933 case EFI_LOADER_DATA:
934 case EFI_BOOT_SERVICES_CODE:
935 case EFI_BOOT_SERVICES_DATA:
936 case EFI_CONVENTIONAL_MEMORY:
937 e820_type = E820_RAM;
938 break;
939
940 case EFI_ACPI_MEMORY_NVS:
941 e820_type = E820_NVS;
942 break;
943
944 default:
945 continue;
946 }
947
948 /* Merge adjacent mappings */
949 if (prev && prev->type == e820_type &&
950 (prev->addr + prev->size) == d->phys_addr)
951 prev->size += d->num_pages << 12;
952 else {
953 e820_map->addr = d->phys_addr;
954 e820_map->size = d->num_pages << 12;
955 e820_map->type = e820_type;
956 prev = e820_map++;
957 nr_entries++;
958 }
959 }
960
961 boot_params->e820_entries = nr_entries;
962
963 return EFI_SUCCESS;
964
965free_mem_map:
966 low_free(_size, (unsigned long)mem_map);
Matt Fleming291f3632011-12-12 21:27:52 +0000967 return status;
968}
969
Matt Fleming9ca8f722012-07-19 10:23:48 +0100970static efi_status_t relocate_kernel(struct setup_header *hdr)
Matt Fleming291f3632011-12-12 21:27:52 +0000971{
Matt Fleming291f3632011-12-12 21:27:52 +0000972 unsigned long start, nr_pages;
Matt Fleming291f3632011-12-12 21:27:52 +0000973 efi_status_t status;
Matt Fleminge31be362012-03-23 09:35:05 -0700974
Matt Fleming291f3632011-12-12 21:27:52 +0000975 /*
976 * The EFI firmware loader could have placed the kernel image
977 * anywhere in memory, but the kernel has various restrictions
978 * on the max physical address it can run at. Attempt to move
979 * the kernel to boot_params.pref_address, or as low as
980 * possible.
981 */
982 start = hdr->pref_address;
983 nr_pages = round_up(hdr->init_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
984
985 status = efi_call_phys4(sys_table->boottime->allocate_pages,
986 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
987 nr_pages, &start);
988 if (status != EFI_SUCCESS) {
989 status = low_alloc(hdr->init_size, hdr->kernel_alignment,
990 &start);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100991 if (status != EFI_SUCCESS)
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000992 efi_printk("Failed to alloc mem for kernel\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000993 }
994
Matt Fleming9ca8f722012-07-19 10:23:48 +0100995 if (status == EFI_SUCCESS)
996 memcpy((void *)start, (void *)(unsigned long)hdr->code32_start,
997 hdr->init_size);
Matt Fleming291f3632011-12-12 21:27:52 +0000998
Matt Fleming9ca8f722012-07-19 10:23:48 +0100999 hdr->pref_address = hdr->code32_start;
1000 hdr->code32_start = (__u32)start;
1001
1002 return status;
1003}
1004
1005/*
1006 * On success we return a pointer to a boot_params structure, and NULL
1007 * on failure.
1008 */
1009struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
1010 struct boot_params *boot_params)
1011{
1012 struct desc_ptr *gdt, *idt;
1013 efi_loaded_image_t *image;
1014 struct setup_header *hdr = &boot_params->hdr;
1015 efi_status_t status;
1016 struct desc_struct *desc;
1017
1018 sys_table = _table;
1019
1020 /* Check if we were booted by the EFI firmware */
1021 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1022 goto fail;
1023
1024 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +00001025
1026 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1027 EFI_LOADER_DATA, sizeof(*gdt),
1028 (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001029 if (status != EFI_SUCCESS) {
1030 efi_printk("Failed to alloc mem for gdt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001031 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001032 }
Matt Fleming291f3632011-12-12 21:27:52 +00001033
1034 gdt->size = 0x800;
1035 status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001036 if (status != EFI_SUCCESS) {
1037 efi_printk("Failed to alloc mem for gdt\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001038 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001039 }
Matt Fleming291f3632011-12-12 21:27:52 +00001040
1041 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1042 EFI_LOADER_DATA, sizeof(*idt),
1043 (void **)&idt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001044 if (status != EFI_SUCCESS) {
1045 efi_printk("Failed to alloc mem for idt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001046 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001047 }
Matt Fleming291f3632011-12-12 21:27:52 +00001048
1049 idt->size = 0;
1050 idt->address = 0;
1051
Matt Fleming9ca8f722012-07-19 10:23:48 +01001052 /*
1053 * If the kernel isn't already loaded at the preferred load
1054 * address, relocate it.
1055 */
1056 if (hdr->pref_address != hdr->code32_start) {
1057 status = relocate_kernel(hdr);
1058
1059 if (status != EFI_SUCCESS)
1060 goto fail;
1061 }
1062
1063 status = exit_boot(boot_params, handle);
Matt Fleming291f3632011-12-12 21:27:52 +00001064 if (status != EFI_SUCCESS)
1065 goto fail;
1066
1067 memset((char *)gdt->address, 0x0, gdt->size);
1068 desc = (struct desc_struct *)gdt->address;
1069
1070 /* The first GDT is a dummy and the second is unused. */
1071 desc += 2;
1072
1073 desc->limit0 = 0xffff;
1074 desc->base0 = 0x0000;
1075 desc->base1 = 0x0000;
1076 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1077 desc->s = DESC_TYPE_CODE_DATA;
1078 desc->dpl = 0;
1079 desc->p = 1;
1080 desc->limit = 0xf;
1081 desc->avl = 0;
1082 desc->l = 0;
1083 desc->d = SEG_OP_SIZE_32BIT;
1084 desc->g = SEG_GRANULARITY_4KB;
1085 desc->base2 = 0x00;
1086
1087 desc++;
1088 desc->limit0 = 0xffff;
1089 desc->base0 = 0x0000;
1090 desc->base1 = 0x0000;
1091 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1092 desc->s = DESC_TYPE_CODE_DATA;
1093 desc->dpl = 0;
1094 desc->p = 1;
1095 desc->limit = 0xf;
1096 desc->avl = 0;
1097 desc->l = 0;
1098 desc->d = SEG_OP_SIZE_32BIT;
1099 desc->g = SEG_GRANULARITY_4KB;
1100 desc->base2 = 0x00;
1101
1102#ifdef CONFIG_X86_64
1103 /* Task segment value */
1104 desc++;
1105 desc->limit0 = 0x0000;
1106 desc->base0 = 0x0000;
1107 desc->base1 = 0x0000;
1108 desc->type = SEG_TYPE_TSS;
1109 desc->s = 0;
1110 desc->dpl = 0;
1111 desc->p = 1;
1112 desc->limit = 0x0;
1113 desc->avl = 0;
1114 desc->l = 0;
1115 desc->d = 0;
1116 desc->g = SEG_GRANULARITY_4KB;
1117 desc->base2 = 0x00;
1118#endif /* CONFIG_X86_64 */
1119
1120 asm volatile ("lidt %0" : : "m" (*idt));
1121 asm volatile ("lgdt %0" : : "m" (*gdt));
1122
1123 asm volatile("cli");
1124
1125 return boot_params;
1126fail:
1127 return NULL;
1128}