blob: bbd83b9cb4dad29e36632dd7395f4992be4dea74 [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
Matthew Garrettf462ed92012-07-27 12:58:53 -0400382 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
383
Matt Fleming291f3632011-12-12 21:27:52 +0000384free_handle:
385 efi_call_phys1(sys_table->boottime->free_pool, gop_handle);
386 return status;
387}
388
389/*
390 * See if we have Universal Graphics Adapter (UGA) protocol
391 */
392static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
393 unsigned long size)
394{
395 struct efi_uga_draw_protocol *uga, *first_uga;
396 unsigned long nr_ugas;
397 efi_status_t status;
398 u32 width, height;
399 void **uga_handle = NULL;
400 int i;
401
402 status = efi_call_phys3(sys_table->boottime->allocate_pool,
403 EFI_LOADER_DATA, size, &uga_handle);
404 if (status != EFI_SUCCESS)
405 return status;
406
407 status = efi_call_phys5(sys_table->boottime->locate_handle,
408 EFI_LOCATE_BY_PROTOCOL, uga_proto,
409 NULL, &size, uga_handle);
410 if (status != EFI_SUCCESS)
411 goto free_handle;
412
413 first_uga = NULL;
414
415 nr_ugas = size / sizeof(void *);
416 for (i = 0; i < nr_ugas; i++) {
417 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
418 void *handle = uga_handle[i];
419 u32 w, h, depth, refresh;
420 void *pciio;
421
422 status = efi_call_phys3(sys_table->boottime->handle_protocol,
423 handle, uga_proto, &uga);
424 if (status != EFI_SUCCESS)
425 continue;
426
427 efi_call_phys3(sys_table->boottime->handle_protocol,
428 handle, &pciio_proto, &pciio);
429
430 status = efi_call_phys5(uga->get_mode, uga, &w, &h,
431 &depth, &refresh);
432 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
433 width = w;
434 height = h;
435
436 /*
437 * Once we've found a UGA supporting PCIIO,
438 * don't bother looking any further.
439 */
440 if (pciio)
441 break;
442
443 first_uga = uga;
444 }
445 }
446
447 if (!first_uga)
448 goto free_handle;
449
450 /* EFI framebuffer */
451 si->orig_video_isVGA = VIDEO_TYPE_EFI;
452
453 si->lfb_depth = 32;
454 si->lfb_width = width;
455 si->lfb_height = height;
456
457 si->red_size = 8;
458 si->red_pos = 16;
459 si->green_size = 8;
460 si->green_pos = 8;
461 si->blue_size = 8;
462 si->blue_pos = 0;
463 si->rsvd_size = 8;
464 si->rsvd_pos = 24;
465
466
467free_handle:
468 efi_call_phys1(sys_table->boottime->free_pool, uga_handle);
469 return status;
470}
471
472void setup_graphics(struct boot_params *boot_params)
473{
474 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
475 struct screen_info *si;
476 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
477 efi_status_t status;
478 unsigned long size;
479 void **gop_handle = NULL;
480 void **uga_handle = NULL;
481
482 si = &boot_params->screen_info;
483 memset(si, 0, sizeof(*si));
484
485 size = 0;
486 status = efi_call_phys5(sys_table->boottime->locate_handle,
487 EFI_LOCATE_BY_PROTOCOL, &graphics_proto,
488 NULL, &size, gop_handle);
489 if (status == EFI_BUFFER_TOO_SMALL)
490 status = setup_gop(si, &graphics_proto, size);
491
492 if (status != EFI_SUCCESS) {
493 size = 0;
494 status = efi_call_phys5(sys_table->boottime->locate_handle,
495 EFI_LOCATE_BY_PROTOCOL, &uga_proto,
496 NULL, &size, uga_handle);
497 if (status == EFI_BUFFER_TOO_SMALL)
498 setup_uga(si, &uga_proto, size);
499 }
500}
501
502struct initrd {
503 efi_file_handle_t *handle;
504 u64 size;
505};
506
507/*
508 * Check the cmdline for a LILO-style initrd= arguments.
509 *
510 * We only support loading an initrd from the same filesystem as the
511 * kernel image.
512 */
513static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
514 struct setup_header *hdr)
515{
516 struct initrd *initrds;
517 unsigned long initrd_addr;
518 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
519 u64 initrd_total;
520 efi_file_io_interface_t *io;
521 efi_file_handle_t *fh;
522 efi_status_t status;
523 int nr_initrds;
524 char *str;
525 int i, j, k;
526
527 initrd_addr = 0;
528 initrd_total = 0;
529
530 str = (char *)(unsigned long)hdr->cmd_line_ptr;
531
532 j = 0; /* See close_handles */
533
534 if (!str || !*str)
535 return EFI_SUCCESS;
536
537 for (nr_initrds = 0; *str; nr_initrds++) {
538 str = strstr(str, "initrd=");
539 if (!str)
540 break;
541
542 str += 7;
543
544 /* Skip any leading slashes */
545 while (*str == '/' || *str == '\\')
546 str++;
547
548 while (*str && *str != ' ' && *str != '\n')
549 str++;
550 }
551
552 if (!nr_initrds)
553 return EFI_SUCCESS;
554
555 status = efi_call_phys3(sys_table->boottime->allocate_pool,
556 EFI_LOADER_DATA,
557 nr_initrds * sizeof(*initrds),
558 &initrds);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000559 if (status != EFI_SUCCESS) {
560 efi_printk("Failed to alloc mem for initrds\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000561 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000562 }
Matt Fleming291f3632011-12-12 21:27:52 +0000563
564 str = (char *)(unsigned long)hdr->cmd_line_ptr;
565 for (i = 0; i < nr_initrds; i++) {
566 struct initrd *initrd;
567 efi_file_handle_t *h;
568 efi_file_info_t *info;
Dan Carpenterc7b73832012-03-05 21:06:14 +0300569 efi_char16_t filename_16[256];
Matt Fleming291f3632011-12-12 21:27:52 +0000570 unsigned long info_sz;
571 efi_guid_t info_guid = EFI_FILE_INFO_ID;
572 efi_char16_t *p;
573 u64 file_sz;
574
575 str = strstr(str, "initrd=");
576 if (!str)
577 break;
578
579 str += 7;
580
581 initrd = &initrds[i];
Dan Carpenterc7b73832012-03-05 21:06:14 +0300582 p = filename_16;
Matt Fleming291f3632011-12-12 21:27:52 +0000583
584 /* Skip any leading slashes */
585 while (*str == '/' || *str == '\\')
586 str++;
587
588 while (*str && *str != ' ' && *str != '\n') {
Dan Carpenterc7b73832012-03-05 21:06:14 +0300589 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
Matt Fleming291f3632011-12-12 21:27:52 +0000590 break;
591
592 *p++ = *str++;
593 }
594
595 *p = '\0';
596
597 /* Only open the volume once. */
598 if (!i) {
599 efi_boot_services_t *boottime;
600
601 boottime = sys_table->boottime;
602
603 status = efi_call_phys3(boottime->handle_protocol,
604 image->device_handle, &fs_proto, &io);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000605 if (status != EFI_SUCCESS) {
606 efi_printk("Failed to handle fs_proto\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000607 goto free_initrds;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000608 }
Matt Fleming291f3632011-12-12 21:27:52 +0000609
610 status = efi_call_phys2(io->open_volume, io, &fh);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000611 if (status != EFI_SUCCESS) {
612 efi_printk("Failed to open volume\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000613 goto free_initrds;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000614 }
Matt Fleming291f3632011-12-12 21:27:52 +0000615 }
616
Dan Carpenterc7b73832012-03-05 21:06:14 +0300617 status = efi_call_phys5(fh->open, fh, &h, filename_16,
Matt Fleming291f3632011-12-12 21:27:52 +0000618 EFI_FILE_MODE_READ, (u64)0);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000619 if (status != EFI_SUCCESS) {
620 efi_printk("Failed to open initrd file\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000621 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000622 }
Matt Fleming291f3632011-12-12 21:27:52 +0000623
624 initrd->handle = h;
625
626 info_sz = 0;
627 status = efi_call_phys4(h->get_info, h, &info_guid,
628 &info_sz, NULL);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000629 if (status != EFI_BUFFER_TOO_SMALL) {
630 efi_printk("Failed to get initrd info size\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000631 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000632 }
Matt Fleming291f3632011-12-12 21:27:52 +0000633
634grow:
635 status = efi_call_phys3(sys_table->boottime->allocate_pool,
636 EFI_LOADER_DATA, info_sz, &info);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000637 if (status != EFI_SUCCESS) {
638 efi_printk("Failed to alloc mem for initrd info\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000639 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000640 }
Matt Fleming291f3632011-12-12 21:27:52 +0000641
642 status = efi_call_phys4(h->get_info, h, &info_guid,
643 &info_sz, info);
644 if (status == EFI_BUFFER_TOO_SMALL) {
645 efi_call_phys1(sys_table->boottime->free_pool, info);
646 goto grow;
647 }
648
649 file_sz = info->file_size;
650 efi_call_phys1(sys_table->boottime->free_pool, info);
651
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000652 if (status != EFI_SUCCESS) {
653 efi_printk("Failed to get initrd info\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000654 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000655 }
Matt Fleming291f3632011-12-12 21:27:52 +0000656
657 initrd->size = file_sz;
658 initrd_total += file_sz;
659 }
660
661 if (initrd_total) {
662 unsigned long addr;
663
664 /*
665 * Multiple initrd's need to be at consecutive
666 * addresses in memory, so allocate enough memory for
667 * all the initrd's.
668 */
669 status = high_alloc(initrd_total, 0x1000,
670 &initrd_addr, hdr->initrd_addr_max);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000671 if (status != EFI_SUCCESS) {
672 efi_printk("Failed to alloc highmem for initrds\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000673 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000674 }
Matt Fleming291f3632011-12-12 21:27:52 +0000675
676 /* We've run out of free low memory. */
677 if (initrd_addr > hdr->initrd_addr_max) {
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000678 efi_printk("We've run out of free low memory\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000679 status = EFI_INVALID_PARAMETER;
680 goto free_initrd_total;
681 }
682
683 addr = initrd_addr;
684 for (j = 0; j < nr_initrds; j++) {
685 u64 size;
686
687 size = initrds[j].size;
Maarten Lankhorst2d2da60f2011-12-16 13:30:58 +0100688 while (size) {
689 u64 chunksize;
690 if (size > EFI_READ_CHUNK_SIZE)
691 chunksize = EFI_READ_CHUNK_SIZE;
692 else
693 chunksize = size;
694 status = efi_call_phys3(fh->read,
695 initrds[j].handle,
696 &chunksize, addr);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000697 if (status != EFI_SUCCESS) {
698 efi_printk("Failed to read initrd\n");
Maarten Lankhorst2d2da60f2011-12-16 13:30:58 +0100699 goto free_initrd_total;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000700 }
Maarten Lankhorst2d2da60f2011-12-16 13:30:58 +0100701 addr += chunksize;
702 size -= chunksize;
703 }
Matt Fleming291f3632011-12-12 21:27:52 +0000704
705 efi_call_phys1(fh->close, initrds[j].handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000706 }
707
708 }
709
710 efi_call_phys1(sys_table->boottime->free_pool, initrds);
711
712 hdr->ramdisk_image = initrd_addr;
713 hdr->ramdisk_size = initrd_total;
714
715 return status;
716
717free_initrd_total:
718 low_free(initrd_total, initrd_addr);
719
720close_handles:
Matt Fleming30dc0d02012-03-15 19:13:25 +0000721 for (k = j; k < i; k++)
Matt Fleming291f3632011-12-12 21:27:52 +0000722 efi_call_phys1(fh->close, initrds[k].handle);
723free_initrds:
724 efi_call_phys1(sys_table->boottime->free_pool, initrds);
725fail:
726 hdr->ramdisk_image = 0;
727 hdr->ramdisk_size = 0;
728
729 return status;
730}
731
732/*
733 * Because the x86 boot code expects to be passed a boot_params we
734 * need to create one ourselves (usually the bootloader would create
735 * one for us).
736 */
Matt Fleming9ca8f722012-07-19 10:23:48 +0100737struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
Matt Fleming291f3632011-12-12 21:27:52 +0000738{
Matt Fleming9ca8f722012-07-19 10:23:48 +0100739 struct boot_params *boot_params;
740 struct sys_desc_table *sdt;
741 struct apm_bios_info *bi;
742 struct setup_header *hdr;
743 struct efi_info *efi;
744 efi_loaded_image_t *image;
745 void *options;
746 u32 load_options_size;
747 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000748 int options_size = 0;
749 efi_status_t status;
Matt Fleming291f3632011-12-12 21:27:52 +0000750 unsigned long cmdline;
Matt Fleming291f3632011-12-12 21:27:52 +0000751 u16 *s2;
752 u8 *s1;
753 int i;
754
Matt Fleming9ca8f722012-07-19 10:23:48 +0100755 sys_table = _table;
756
757 /* Check if we were booted by the EFI firmware */
758 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
759 return NULL;
760
761 status = efi_call_phys3(sys_table->boottime->handle_protocol,
762 handle, &proto, (void *)&image);
763 if (status != EFI_SUCCESS) {
764 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
765 return NULL;
766 }
767
768 status = low_alloc(0x4000, 1, (unsigned long *)&boot_params);
769 if (status != EFI_SUCCESS) {
770 efi_printk("Failed to alloc lowmem for boot params\n");
771 return NULL;
772 }
773
774 memset(boot_params, 0x0, 0x4000);
775
776 hdr = &boot_params->hdr;
777 efi = &boot_params->efi_info;
778 bi = &boot_params->apm_bios_info;
779 sdt = &boot_params->sys_desc_table;
780
781 /* Copy the second sector to boot_params */
782 memcpy(&hdr->jump, image->image_base + 512, 512);
783
784 /*
785 * Fill out some of the header fields ourselves because the
786 * EFI firmware loader doesn't load the first sector.
787 */
788 hdr->root_flags = 1;
789 hdr->vid_mode = 0xffff;
790 hdr->boot_flag = 0xAA55;
791
792 hdr->code32_start = (__u64)(unsigned long)image->image_base;
793
Matt Fleming291f3632011-12-12 21:27:52 +0000794 hdr->type_of_loader = 0x21;
795
796 /* Convert unicode cmdline to ascii */
Matt Fleming9ca8f722012-07-19 10:23:48 +0100797 options = image->load_options;
798 load_options_size = image->load_options_size / 2; /* ASCII */
Matt Fleming291f3632011-12-12 21:27:52 +0000799 cmdline = 0;
800 s2 = (u16 *)options;
801
802 if (s2) {
803 while (*s2 && *s2 != '\n' && options_size < load_options_size) {
804 s2++;
805 options_size++;
806 }
807
808 if (options_size) {
809 if (options_size > hdr->cmdline_size)
810 options_size = hdr->cmdline_size;
811
812 options_size++; /* NUL termination */
813
814 status = low_alloc(options_size, 1, &cmdline);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000815 if (status != EFI_SUCCESS) {
816 efi_printk("Failed to alloc mem for cmdline\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000817 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000818 }
Matt Fleming291f3632011-12-12 21:27:52 +0000819
820 s1 = (u8 *)(unsigned long)cmdline;
821 s2 = (u16 *)options;
822
823 for (i = 0; i < options_size - 1; i++)
824 *s1++ = *s2++;
825
826 *s1 = '\0';
827 }
828 }
829
830 hdr->cmd_line_ptr = cmdline;
831
832 hdr->ramdisk_image = 0;
833 hdr->ramdisk_size = 0;
834
Matt Fleming291f3632011-12-12 21:27:52 +0000835 /* Clear APM BIOS info */
836 memset(bi, 0, sizeof(*bi));
837
838 memset(sdt, 0, sizeof(*sdt));
839
Matt Fleming9ca8f722012-07-19 10:23:48 +0100840 status = handle_ramdisks(image, hdr);
841 if (status != EFI_SUCCESS)
842 goto fail2;
843
844 return boot_params;
845fail2:
846 if (options_size)
847 low_free(options_size, hdr->cmd_line_ptr);
848fail:
849 low_free(0x4000, (unsigned long)boot_params);
850 return NULL;
851}
852
853static efi_status_t exit_boot(struct boot_params *boot_params,
854 void *handle)
855{
856 struct efi_info *efi = &boot_params->efi_info;
857 struct e820entry *e820_map = &boot_params->e820_map[0];
858 struct e820entry *prev = NULL;
859 unsigned long size, key, desc_size, _size;
860 efi_memory_desc_t *mem_map;
861 efi_status_t status;
862 __u32 desc_version;
863 u8 nr_entries;
864 int i;
Matt Fleming291f3632011-12-12 21:27:52 +0000865
866 size = sizeof(*mem_map) * 32;
867
868again:
869 size += sizeof(*mem_map);
870 _size = size;
871 status = low_alloc(size, 1, (unsigned long *)&mem_map);
872 if (status != EFI_SUCCESS)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100873 return status;
Matt Fleming291f3632011-12-12 21:27:52 +0000874
875 status = efi_call_phys5(sys_table->boottime->get_memory_map, &size,
876 mem_map, &key, &desc_size, &desc_version);
877 if (status == EFI_BUFFER_TOO_SMALL) {
878 low_free(_size, (unsigned long)mem_map);
879 goto again;
880 }
881
882 if (status != EFI_SUCCESS)
883 goto free_mem_map;
884
Matt Fleming9ca8f722012-07-19 10:23:48 +0100885 memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32));
Matt Fleming291f3632011-12-12 21:27:52 +0000886 efi->efi_systab = (unsigned long)sys_table;
887 efi->efi_memdesc_size = desc_size;
888 efi->efi_memdesc_version = desc_version;
889 efi->efi_memmap = (unsigned long)mem_map;
890 efi->efi_memmap_size = size;
891
892#ifdef CONFIG_X86_64
893 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
894 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
895#endif
896
897 /* Might as well exit boot services now */
898 status = efi_call_phys2(sys_table->boottime->exit_boot_services,
899 handle, key);
900 if (status != EFI_SUCCESS)
901 goto free_mem_map;
902
903 /* Historic? */
904 boot_params->alt_mem_k = 32 * 1024;
905
906 /*
907 * Convert the EFI memory map to E820.
908 */
909 nr_entries = 0;
910 for (i = 0; i < size / desc_size; i++) {
911 efi_memory_desc_t *d;
912 unsigned int e820_type = 0;
913 unsigned long m = (unsigned long)mem_map;
914
915 d = (efi_memory_desc_t *)(m + (i * desc_size));
916 switch (d->type) {
917 case EFI_RESERVED_TYPE:
918 case EFI_RUNTIME_SERVICES_CODE:
919 case EFI_RUNTIME_SERVICES_DATA:
920 case EFI_MEMORY_MAPPED_IO:
921 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
922 case EFI_PAL_CODE:
923 e820_type = E820_RESERVED;
924 break;
925
926 case EFI_UNUSABLE_MEMORY:
927 e820_type = E820_UNUSABLE;
928 break;
929
930 case EFI_ACPI_RECLAIM_MEMORY:
931 e820_type = E820_ACPI;
932 break;
933
934 case EFI_LOADER_CODE:
935 case EFI_LOADER_DATA:
936 case EFI_BOOT_SERVICES_CODE:
937 case EFI_BOOT_SERVICES_DATA:
938 case EFI_CONVENTIONAL_MEMORY:
939 e820_type = E820_RAM;
940 break;
941
942 case EFI_ACPI_MEMORY_NVS:
943 e820_type = E820_NVS;
944 break;
945
946 default:
947 continue;
948 }
949
950 /* Merge adjacent mappings */
951 if (prev && prev->type == e820_type &&
952 (prev->addr + prev->size) == d->phys_addr)
953 prev->size += d->num_pages << 12;
954 else {
955 e820_map->addr = d->phys_addr;
956 e820_map->size = d->num_pages << 12;
957 e820_map->type = e820_type;
958 prev = e820_map++;
959 nr_entries++;
960 }
961 }
962
963 boot_params->e820_entries = nr_entries;
964
965 return EFI_SUCCESS;
966
967free_mem_map:
968 low_free(_size, (unsigned long)mem_map);
Matt Fleming291f3632011-12-12 21:27:52 +0000969 return status;
970}
971
Matt Fleming9ca8f722012-07-19 10:23:48 +0100972static efi_status_t relocate_kernel(struct setup_header *hdr)
Matt Fleming291f3632011-12-12 21:27:52 +0000973{
Matt Fleming291f3632011-12-12 21:27:52 +0000974 unsigned long start, nr_pages;
Matt Fleming291f3632011-12-12 21:27:52 +0000975 efi_status_t status;
Matt Fleminge31be362012-03-23 09:35:05 -0700976
Matt Fleming291f3632011-12-12 21:27:52 +0000977 /*
978 * The EFI firmware loader could have placed the kernel image
979 * anywhere in memory, but the kernel has various restrictions
980 * on the max physical address it can run at. Attempt to move
981 * the kernel to boot_params.pref_address, or as low as
982 * possible.
983 */
984 start = hdr->pref_address;
985 nr_pages = round_up(hdr->init_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
986
987 status = efi_call_phys4(sys_table->boottime->allocate_pages,
988 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
989 nr_pages, &start);
990 if (status != EFI_SUCCESS) {
991 status = low_alloc(hdr->init_size, hdr->kernel_alignment,
992 &start);
Matt Fleming9ca8f722012-07-19 10:23:48 +0100993 if (status != EFI_SUCCESS)
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000994 efi_printk("Failed to alloc mem for kernel\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000995 }
996
Matt Fleming9ca8f722012-07-19 10:23:48 +0100997 if (status == EFI_SUCCESS)
998 memcpy((void *)start, (void *)(unsigned long)hdr->code32_start,
999 hdr->init_size);
Matt Fleming291f3632011-12-12 21:27:52 +00001000
Matt Fleming9ca8f722012-07-19 10:23:48 +01001001 hdr->pref_address = hdr->code32_start;
1002 hdr->code32_start = (__u32)start;
1003
1004 return status;
1005}
1006
1007/*
1008 * On success we return a pointer to a boot_params structure, and NULL
1009 * on failure.
1010 */
1011struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
1012 struct boot_params *boot_params)
1013{
1014 struct desc_ptr *gdt, *idt;
1015 efi_loaded_image_t *image;
1016 struct setup_header *hdr = &boot_params->hdr;
1017 efi_status_t status;
1018 struct desc_struct *desc;
1019
1020 sys_table = _table;
1021
1022 /* Check if we were booted by the EFI firmware */
1023 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1024 goto fail;
1025
1026 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +00001027
1028 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1029 EFI_LOADER_DATA, sizeof(*gdt),
1030 (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001031 if (status != EFI_SUCCESS) {
1032 efi_printk("Failed to alloc mem for gdt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001033 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001034 }
Matt Fleming291f3632011-12-12 21:27:52 +00001035
1036 gdt->size = 0x800;
1037 status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001038 if (status != EFI_SUCCESS) {
1039 efi_printk("Failed to alloc mem for gdt\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001040 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001041 }
Matt Fleming291f3632011-12-12 21:27:52 +00001042
1043 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1044 EFI_LOADER_DATA, sizeof(*idt),
1045 (void **)&idt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001046 if (status != EFI_SUCCESS) {
1047 efi_printk("Failed to alloc mem for idt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001048 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001049 }
Matt Fleming291f3632011-12-12 21:27:52 +00001050
1051 idt->size = 0;
1052 idt->address = 0;
1053
Matt Fleming9ca8f722012-07-19 10:23:48 +01001054 /*
1055 * If the kernel isn't already loaded at the preferred load
1056 * address, relocate it.
1057 */
1058 if (hdr->pref_address != hdr->code32_start) {
1059 status = relocate_kernel(hdr);
1060
1061 if (status != EFI_SUCCESS)
1062 goto fail;
1063 }
1064
1065 status = exit_boot(boot_params, handle);
Matt Fleming291f3632011-12-12 21:27:52 +00001066 if (status != EFI_SUCCESS)
1067 goto fail;
1068
1069 memset((char *)gdt->address, 0x0, gdt->size);
1070 desc = (struct desc_struct *)gdt->address;
1071
1072 /* The first GDT is a dummy and the second is unused. */
1073 desc += 2;
1074
1075 desc->limit0 = 0xffff;
1076 desc->base0 = 0x0000;
1077 desc->base1 = 0x0000;
1078 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1079 desc->s = DESC_TYPE_CODE_DATA;
1080 desc->dpl = 0;
1081 desc->p = 1;
1082 desc->limit = 0xf;
1083 desc->avl = 0;
1084 desc->l = 0;
1085 desc->d = SEG_OP_SIZE_32BIT;
1086 desc->g = SEG_GRANULARITY_4KB;
1087 desc->base2 = 0x00;
1088
1089 desc++;
1090 desc->limit0 = 0xffff;
1091 desc->base0 = 0x0000;
1092 desc->base1 = 0x0000;
1093 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1094 desc->s = DESC_TYPE_CODE_DATA;
1095 desc->dpl = 0;
1096 desc->p = 1;
1097 desc->limit = 0xf;
1098 desc->avl = 0;
1099 desc->l = 0;
1100 desc->d = SEG_OP_SIZE_32BIT;
1101 desc->g = SEG_GRANULARITY_4KB;
1102 desc->base2 = 0x00;
1103
1104#ifdef CONFIG_X86_64
1105 /* Task segment value */
1106 desc++;
1107 desc->limit0 = 0x0000;
1108 desc->base0 = 0x0000;
1109 desc->base1 = 0x0000;
1110 desc->type = SEG_TYPE_TSS;
1111 desc->s = 0;
1112 desc->dpl = 0;
1113 desc->p = 1;
1114 desc->limit = 0x0;
1115 desc->avl = 0;
1116 desc->l = 0;
1117 desc->d = 0;
1118 desc->g = SEG_GRANULARITY_4KB;
1119 desc->base2 = 0x00;
1120#endif /* CONFIG_X86_64 */
1121
1122 asm volatile ("lidt %0" : : "m" (*idt));
1123 asm volatile ("lgdt %0" : : "m" (*gdt));
1124
1125 asm volatile("cli");
1126
1127 return boot_params;
1128fail:
1129 return NULL;
1130}