Commit | Line | Data |
---|---|---|
291f3632 MF |
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 | #include "eboot.h" | |
16 | ||
17 | static efi_system_table_t *sys_table; | |
18 | ||
9fa7deda MF |
19 | static 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 | ||
291f3632 MF |
39 | static 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; | |
48 | again: | |
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 | ||
69 | fail: | |
70 | *map = m; | |
71 | return status; | |
72 | } | |
73 | ||
74 | /* | |
75 | * Allocate at the highest possible address that is not above 'max'. | |
76 | */ | |
77 | static 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; | |
92 | again: | |
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 | ||
145 | free_pool: | |
146 | efi_call_phys1(sys_table->boottime->free_pool, map); | |
147 | ||
148 | fail: | |
149 | return status; | |
150 | } | |
151 | ||
152 | /* | |
153 | * Allocate at the lowest possible address. | |
154 | */ | |
155 | static 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 | ||
209 | free_pool: | |
210 | efi_call_phys1(sys_table->boottime->free_pool, map); | |
211 | fail: | |
212 | return status; | |
213 | } | |
214 | ||
215 | static 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 | ||
223 | static 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 | */ | |
249 | static 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; | |
38cb5ef4 MG |
279 | efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID; |
280 | bool conout_found = false; | |
281 | void *dummy; | |
291f3632 MF |
282 | 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 | ||
38cb5ef4 MG |
289 | status = efi_call_phys3(sys_table->boottime->handle_protocol, |
290 | h, &conout_proto, &dummy); | |
291 | ||
292 | if (status == EFI_SUCCESS) | |
293 | conout_found = true; | |
291f3632 MF |
294 | |
295 | status = efi_call_phys4(gop->query_mode, gop, | |
296 | gop->mode->mode, &size, &info); | |
38cb5ef4 | 297 | if (status == EFI_SUCCESS && (!first_gop || conout_found)) { |
291f3632 | 298 | /* |
38cb5ef4 MG |
299 | * 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. | |
291f3632 MF |
304 | */ |
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 | /* | |
38cb5ef4 | 314 | * Once we've found a GOP supporting ConOut, |
291f3632 MF |
315 | * don't bother looking any further. |
316 | */ | |
38cb5ef4 | 317 | if (conout_found) |
291f3632 MF |
318 | 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; | |
291f3632 MF |
334 | si->pages = 1; |
335 | ||
336 | if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) { | |
337 | si->lfb_depth = 32; | |
338 | si->lfb_linelength = pixels_per_scan_line * 4; | |
339 | si->red_size = 8; | |
340 | si->red_pos = 0; | |
341 | si->green_size = 8; | |
342 | si->green_pos = 8; | |
343 | si->blue_size = 8; | |
344 | si->blue_pos = 16; | |
345 | si->rsvd_size = 8; | |
346 | si->rsvd_pos = 24; | |
347 | } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) { | |
348 | si->lfb_depth = 32; | |
349 | si->lfb_linelength = pixels_per_scan_line * 4; | |
350 | si->red_size = 8; | |
351 | si->red_pos = 16; | |
352 | si->green_size = 8; | |
353 | si->green_pos = 8; | |
354 | si->blue_size = 8; | |
355 | si->blue_pos = 0; | |
356 | si->rsvd_size = 8; | |
357 | si->rsvd_pos = 24; | |
358 | } else if (pixel_format == PIXEL_BIT_MASK) { | |
359 | find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size); | |
360 | find_bits(pixel_info.green_mask, &si->green_pos, | |
361 | &si->green_size); | |
362 | find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size); | |
363 | find_bits(pixel_info.reserved_mask, &si->rsvd_pos, | |
364 | &si->rsvd_size); | |
365 | si->lfb_depth = si->red_size + si->green_size + | |
366 | si->blue_size + si->rsvd_size; | |
367 | si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8; | |
368 | } else { | |
369 | si->lfb_depth = 4; | |
370 | si->lfb_linelength = si->lfb_width / 2; | |
371 | si->red_size = 0; | |
372 | si->red_pos = 0; | |
373 | si->green_size = 0; | |
374 | si->green_pos = 0; | |
375 | si->blue_size = 0; | |
376 | si->blue_pos = 0; | |
377 | si->rsvd_size = 0; | |
378 | si->rsvd_pos = 0; | |
379 | } | |
380 | ||
e9b10953 MG |
381 | si->lfb_size = si->lfb_linelength * si->lfb_height; |
382 | ||
f462ed93 MG |
383 | si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS; |
384 | ||
291f3632 MF |
385 | free_handle: |
386 | efi_call_phys1(sys_table->boottime->free_pool, gop_handle); | |
387 | return status; | |
388 | } | |
389 | ||
390 | /* | |
391 | * See if we have Universal Graphics Adapter (UGA) protocol | |
392 | */ | |
393 | static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto, | |
394 | unsigned long size) | |
395 | { | |
396 | struct efi_uga_draw_protocol *uga, *first_uga; | |
397 | unsigned long nr_ugas; | |
398 | efi_status_t status; | |
399 | u32 width, height; | |
400 | void **uga_handle = NULL; | |
401 | int i; | |
402 | ||
403 | status = efi_call_phys3(sys_table->boottime->allocate_pool, | |
404 | EFI_LOADER_DATA, size, &uga_handle); | |
405 | if (status != EFI_SUCCESS) | |
406 | return status; | |
407 | ||
408 | status = efi_call_phys5(sys_table->boottime->locate_handle, | |
409 | EFI_LOCATE_BY_PROTOCOL, uga_proto, | |
410 | NULL, &size, uga_handle); | |
411 | if (status != EFI_SUCCESS) | |
412 | goto free_handle; | |
413 | ||
414 | first_uga = NULL; | |
415 | ||
416 | nr_ugas = size / sizeof(void *); | |
417 | for (i = 0; i < nr_ugas; i++) { | |
418 | efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; | |
419 | void *handle = uga_handle[i]; | |
420 | u32 w, h, depth, refresh; | |
421 | void *pciio; | |
422 | ||
423 | status = efi_call_phys3(sys_table->boottime->handle_protocol, | |
424 | handle, uga_proto, &uga); | |
425 | if (status != EFI_SUCCESS) | |
426 | continue; | |
427 | ||
428 | efi_call_phys3(sys_table->boottime->handle_protocol, | |
429 | handle, &pciio_proto, &pciio); | |
430 | ||
431 | status = efi_call_phys5(uga->get_mode, uga, &w, &h, | |
432 | &depth, &refresh); | |
433 | if (status == EFI_SUCCESS && (!first_uga || pciio)) { | |
434 | width = w; | |
435 | height = h; | |
436 | ||
437 | /* | |
438 | * Once we've found a UGA supporting PCIIO, | |
439 | * don't bother looking any further. | |
440 | */ | |
441 | if (pciio) | |
442 | break; | |
443 | ||
444 | first_uga = uga; | |
445 | } | |
446 | } | |
447 | ||
448 | if (!first_uga) | |
449 | goto free_handle; | |
450 | ||
451 | /* EFI framebuffer */ | |
452 | si->orig_video_isVGA = VIDEO_TYPE_EFI; | |
453 | ||
454 | si->lfb_depth = 32; | |
455 | si->lfb_width = width; | |
456 | si->lfb_height = height; | |
457 | ||
458 | si->red_size = 8; | |
459 | si->red_pos = 16; | |
460 | si->green_size = 8; | |
461 | si->green_pos = 8; | |
462 | si->blue_size = 8; | |
463 | si->blue_pos = 0; | |
464 | si->rsvd_size = 8; | |
465 | si->rsvd_pos = 24; | |
466 | ||
467 | ||
468 | free_handle: | |
469 | efi_call_phys1(sys_table->boottime->free_pool, uga_handle); | |
470 | return status; | |
471 | } | |
472 | ||
473 | void setup_graphics(struct boot_params *boot_params) | |
474 | { | |
475 | efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; | |
476 | struct screen_info *si; | |
477 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; | |
478 | efi_status_t status; | |
479 | unsigned long size; | |
480 | void **gop_handle = NULL; | |
481 | void **uga_handle = NULL; | |
482 | ||
483 | si = &boot_params->screen_info; | |
484 | memset(si, 0, sizeof(*si)); | |
485 | ||
486 | size = 0; | |
487 | status = efi_call_phys5(sys_table->boottime->locate_handle, | |
488 | EFI_LOCATE_BY_PROTOCOL, &graphics_proto, | |
489 | NULL, &size, gop_handle); | |
490 | if (status == EFI_BUFFER_TOO_SMALL) | |
491 | status = setup_gop(si, &graphics_proto, size); | |
492 | ||
493 | if (status != EFI_SUCCESS) { | |
494 | size = 0; | |
495 | status = efi_call_phys5(sys_table->boottime->locate_handle, | |
496 | EFI_LOCATE_BY_PROTOCOL, &uga_proto, | |
497 | NULL, &size, uga_handle); | |
498 | if (status == EFI_BUFFER_TOO_SMALL) | |
499 | setup_uga(si, &uga_proto, size); | |
500 | } | |
501 | } | |
502 | ||
503 | struct initrd { | |
504 | efi_file_handle_t *handle; | |
505 | u64 size; | |
506 | }; | |
507 | ||
508 | /* | |
509 | * Check the cmdline for a LILO-style initrd= arguments. | |
510 | * | |
511 | * We only support loading an initrd from the same filesystem as the | |
512 | * kernel image. | |
513 | */ | |
514 | static efi_status_t handle_ramdisks(efi_loaded_image_t *image, | |
515 | struct setup_header *hdr) | |
516 | { | |
517 | struct initrd *initrds; | |
518 | unsigned long initrd_addr; | |
519 | efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; | |
520 | u64 initrd_total; | |
521 | efi_file_io_interface_t *io; | |
522 | efi_file_handle_t *fh; | |
523 | efi_status_t status; | |
524 | int nr_initrds; | |
525 | char *str; | |
526 | int i, j, k; | |
527 | ||
528 | initrd_addr = 0; | |
529 | initrd_total = 0; | |
530 | ||
531 | str = (char *)(unsigned long)hdr->cmd_line_ptr; | |
532 | ||
533 | j = 0; /* See close_handles */ | |
534 | ||
535 | if (!str || !*str) | |
536 | return EFI_SUCCESS; | |
537 | ||
538 | for (nr_initrds = 0; *str; nr_initrds++) { | |
539 | str = strstr(str, "initrd="); | |
540 | if (!str) | |
541 | break; | |
542 | ||
543 | str += 7; | |
544 | ||
545 | /* Skip any leading slashes */ | |
546 | while (*str == '/' || *str == '\\') | |
547 | str++; | |
548 | ||
549 | while (*str && *str != ' ' && *str != '\n') | |
550 | str++; | |
551 | } | |
552 | ||
553 | if (!nr_initrds) | |
554 | return EFI_SUCCESS; | |
555 | ||
556 | status = efi_call_phys3(sys_table->boottime->allocate_pool, | |
557 | EFI_LOADER_DATA, | |
558 | nr_initrds * sizeof(*initrds), | |
559 | &initrds); | |
9fa7deda MF |
560 | if (status != EFI_SUCCESS) { |
561 | efi_printk("Failed to alloc mem for initrds\n"); | |
291f3632 | 562 | goto fail; |
9fa7deda | 563 | } |
291f3632 MF |
564 | |
565 | str = (char *)(unsigned long)hdr->cmd_line_ptr; | |
566 | for (i = 0; i < nr_initrds; i++) { | |
567 | struct initrd *initrd; | |
568 | efi_file_handle_t *h; | |
569 | efi_file_info_t *info; | |
c7b73835 | 570 | efi_char16_t filename_16[256]; |
291f3632 MF |
571 | unsigned long info_sz; |
572 | efi_guid_t info_guid = EFI_FILE_INFO_ID; | |
573 | efi_char16_t *p; | |
574 | u64 file_sz; | |
575 | ||
576 | str = strstr(str, "initrd="); | |
577 | if (!str) | |
578 | break; | |
579 | ||
580 | str += 7; | |
581 | ||
582 | initrd = &initrds[i]; | |
c7b73835 | 583 | p = filename_16; |
291f3632 MF |
584 | |
585 | /* Skip any leading slashes */ | |
586 | while (*str == '/' || *str == '\\') | |
587 | str++; | |
588 | ||
589 | while (*str && *str != ' ' && *str != '\n') { | |
c7b73835 | 590 | if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16)) |
291f3632 MF |
591 | break; |
592 | ||
593 | *p++ = *str++; | |
594 | } | |
595 | ||
596 | *p = '\0'; | |
597 | ||
598 | /* Only open the volume once. */ | |
599 | if (!i) { | |
600 | efi_boot_services_t *boottime; | |
601 | ||
602 | boottime = sys_table->boottime; | |
603 | ||
604 | status = efi_call_phys3(boottime->handle_protocol, | |
605 | image->device_handle, &fs_proto, &io); | |
9fa7deda MF |
606 | if (status != EFI_SUCCESS) { |
607 | efi_printk("Failed to handle fs_proto\n"); | |
291f3632 | 608 | goto free_initrds; |
9fa7deda | 609 | } |
291f3632 MF |
610 | |
611 | status = efi_call_phys2(io->open_volume, io, &fh); | |
9fa7deda MF |
612 | if (status != EFI_SUCCESS) { |
613 | efi_printk("Failed to open volume\n"); | |
291f3632 | 614 | goto free_initrds; |
9fa7deda | 615 | } |
291f3632 MF |
616 | } |
617 | ||
c7b73835 | 618 | status = efi_call_phys5(fh->open, fh, &h, filename_16, |
291f3632 | 619 | EFI_FILE_MODE_READ, (u64)0); |
9fa7deda MF |
620 | if (status != EFI_SUCCESS) { |
621 | efi_printk("Failed to open initrd file\n"); | |
291f3632 | 622 | goto close_handles; |
9fa7deda | 623 | } |
291f3632 MF |
624 | |
625 | initrd->handle = h; | |
626 | ||
627 | info_sz = 0; | |
628 | status = efi_call_phys4(h->get_info, h, &info_guid, | |
629 | &info_sz, NULL); | |
9fa7deda MF |
630 | if (status != EFI_BUFFER_TOO_SMALL) { |
631 | efi_printk("Failed to get initrd info size\n"); | |
291f3632 | 632 | goto close_handles; |
9fa7deda | 633 | } |
291f3632 MF |
634 | |
635 | grow: | |
636 | status = efi_call_phys3(sys_table->boottime->allocate_pool, | |
637 | EFI_LOADER_DATA, info_sz, &info); | |
9fa7deda MF |
638 | if (status != EFI_SUCCESS) { |
639 | efi_printk("Failed to alloc mem for initrd info\n"); | |
291f3632 | 640 | goto close_handles; |
9fa7deda | 641 | } |
291f3632 MF |
642 | |
643 | status = efi_call_phys4(h->get_info, h, &info_guid, | |
644 | &info_sz, info); | |
645 | if (status == EFI_BUFFER_TOO_SMALL) { | |
646 | efi_call_phys1(sys_table->boottime->free_pool, info); | |
647 | goto grow; | |
648 | } | |
649 | ||
650 | file_sz = info->file_size; | |
651 | efi_call_phys1(sys_table->boottime->free_pool, info); | |
652 | ||
9fa7deda MF |
653 | if (status != EFI_SUCCESS) { |
654 | efi_printk("Failed to get initrd info\n"); | |
291f3632 | 655 | goto close_handles; |
9fa7deda | 656 | } |
291f3632 MF |
657 | |
658 | initrd->size = file_sz; | |
659 | initrd_total += file_sz; | |
660 | } | |
661 | ||
662 | if (initrd_total) { | |
663 | unsigned long addr; | |
664 | ||
665 | /* | |
666 | * Multiple initrd's need to be at consecutive | |
667 | * addresses in memory, so allocate enough memory for | |
668 | * all the initrd's. | |
669 | */ | |
670 | status = high_alloc(initrd_total, 0x1000, | |
671 | &initrd_addr, hdr->initrd_addr_max); | |
9fa7deda MF |
672 | if (status != EFI_SUCCESS) { |
673 | efi_printk("Failed to alloc highmem for initrds\n"); | |
291f3632 | 674 | goto close_handles; |
9fa7deda | 675 | } |
291f3632 MF |
676 | |
677 | /* We've run out of free low memory. */ | |
678 | if (initrd_addr > hdr->initrd_addr_max) { | |
9fa7deda | 679 | efi_printk("We've run out of free low memory\n"); |
291f3632 MF |
680 | status = EFI_INVALID_PARAMETER; |
681 | goto free_initrd_total; | |
682 | } | |
683 | ||
684 | addr = initrd_addr; | |
685 | for (j = 0; j < nr_initrds; j++) { | |
686 | u64 size; | |
687 | ||
688 | size = initrds[j].size; | |
2d2da60f ML |
689 | while (size) { |
690 | u64 chunksize; | |
691 | if (size > EFI_READ_CHUNK_SIZE) | |
692 | chunksize = EFI_READ_CHUNK_SIZE; | |
693 | else | |
694 | chunksize = size; | |
695 | status = efi_call_phys3(fh->read, | |
696 | initrds[j].handle, | |
697 | &chunksize, addr); | |
9fa7deda MF |
698 | if (status != EFI_SUCCESS) { |
699 | efi_printk("Failed to read initrd\n"); | |
2d2da60f | 700 | goto free_initrd_total; |
9fa7deda | 701 | } |
2d2da60f ML |
702 | addr += chunksize; |
703 | size -= chunksize; | |
704 | } | |
291f3632 MF |
705 | |
706 | efi_call_phys1(fh->close, initrds[j].handle); | |
291f3632 MF |
707 | } |
708 | ||
709 | } | |
710 | ||
711 | efi_call_phys1(sys_table->boottime->free_pool, initrds); | |
712 | ||
713 | hdr->ramdisk_image = initrd_addr; | |
714 | hdr->ramdisk_size = initrd_total; | |
715 | ||
716 | return status; | |
717 | ||
718 | free_initrd_total: | |
719 | low_free(initrd_total, initrd_addr); | |
720 | ||
721 | close_handles: | |
30dc0d0f | 722 | for (k = j; k < i; k++) |
291f3632 MF |
723 | efi_call_phys1(fh->close, initrds[k].handle); |
724 | free_initrds: | |
725 | efi_call_phys1(sys_table->boottime->free_pool, initrds); | |
726 | fail: | |
727 | hdr->ramdisk_image = 0; | |
728 | hdr->ramdisk_size = 0; | |
729 | ||
730 | return status; | |
731 | } | |
732 | ||
733 | /* | |
734 | * Because the x86 boot code expects to be passed a boot_params we | |
735 | * need to create one ourselves (usually the bootloader would create | |
736 | * one for us). | |
737 | */ | |
9ca8f72a | 738 | struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table) |
291f3632 | 739 | { |
9ca8f72a MF |
740 | struct boot_params *boot_params; |
741 | struct sys_desc_table *sdt; | |
742 | struct apm_bios_info *bi; | |
743 | struct setup_header *hdr; | |
744 | struct efi_info *efi; | |
745 | efi_loaded_image_t *image; | |
746 | void *options; | |
747 | u32 load_options_size; | |
748 | efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; | |
291f3632 MF |
749 | int options_size = 0; |
750 | efi_status_t status; | |
291f3632 | 751 | unsigned long cmdline; |
291f3632 MF |
752 | u16 *s2; |
753 | u8 *s1; | |
754 | int i; | |
755 | ||
9ca8f72a MF |
756 | sys_table = _table; |
757 | ||
758 | /* Check if we were booted by the EFI firmware */ | |
759 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) | |
760 | return NULL; | |
761 | ||
762 | status = efi_call_phys3(sys_table->boottime->handle_protocol, | |
763 | handle, &proto, (void *)&image); | |
764 | if (status != EFI_SUCCESS) { | |
765 | efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n"); | |
766 | return NULL; | |
767 | } | |
768 | ||
769 | status = low_alloc(0x4000, 1, (unsigned long *)&boot_params); | |
770 | if (status != EFI_SUCCESS) { | |
771 | efi_printk("Failed to alloc lowmem for boot params\n"); | |
772 | return NULL; | |
773 | } | |
774 | ||
775 | memset(boot_params, 0x0, 0x4000); | |
776 | ||
777 | hdr = &boot_params->hdr; | |
778 | efi = &boot_params->efi_info; | |
779 | bi = &boot_params->apm_bios_info; | |
780 | sdt = &boot_params->sys_desc_table; | |
781 | ||
782 | /* Copy the second sector to boot_params */ | |
783 | memcpy(&hdr->jump, image->image_base + 512, 512); | |
784 | ||
785 | /* | |
786 | * Fill out some of the header fields ourselves because the | |
787 | * EFI firmware loader doesn't load the first sector. | |
788 | */ | |
789 | hdr->root_flags = 1; | |
790 | hdr->vid_mode = 0xffff; | |
791 | hdr->boot_flag = 0xAA55; | |
792 | ||
793 | hdr->code32_start = (__u64)(unsigned long)image->image_base; | |
794 | ||
291f3632 MF |
795 | hdr->type_of_loader = 0x21; |
796 | ||
797 | /* Convert unicode cmdline to ascii */ | |
9ca8f72a MF |
798 | options = image->load_options; |
799 | load_options_size = image->load_options_size / 2; /* ASCII */ | |
291f3632 MF |
800 | cmdline = 0; |
801 | s2 = (u16 *)options; | |
802 | ||
803 | if (s2) { | |
804 | while (*s2 && *s2 != '\n' && options_size < load_options_size) { | |
805 | s2++; | |
806 | options_size++; | |
807 | } | |
808 | ||
809 | if (options_size) { | |
810 | if (options_size > hdr->cmdline_size) | |
811 | options_size = hdr->cmdline_size; | |
812 | ||
813 | options_size++; /* NUL termination */ | |
814 | ||
815 | status = low_alloc(options_size, 1, &cmdline); | |
9fa7deda MF |
816 | if (status != EFI_SUCCESS) { |
817 | efi_printk("Failed to alloc mem for cmdline\n"); | |
291f3632 | 818 | goto fail; |
9fa7deda | 819 | } |
291f3632 MF |
820 | |
821 | s1 = (u8 *)(unsigned long)cmdline; | |
822 | s2 = (u16 *)options; | |
823 | ||
824 | for (i = 0; i < options_size - 1; i++) | |
825 | *s1++ = *s2++; | |
826 | ||
827 | *s1 = '\0'; | |
828 | } | |
829 | } | |
830 | ||
831 | hdr->cmd_line_ptr = cmdline; | |
832 | ||
833 | hdr->ramdisk_image = 0; | |
834 | hdr->ramdisk_size = 0; | |
835 | ||
291f3632 MF |
836 | /* Clear APM BIOS info */ |
837 | memset(bi, 0, sizeof(*bi)); | |
838 | ||
839 | memset(sdt, 0, sizeof(*sdt)); | |
840 | ||
9ca8f72a MF |
841 | status = handle_ramdisks(image, hdr); |
842 | if (status != EFI_SUCCESS) | |
843 | goto fail2; | |
844 | ||
845 | return boot_params; | |
846 | fail2: | |
847 | if (options_size) | |
848 | low_free(options_size, hdr->cmd_line_ptr); | |
849 | fail: | |
850 | low_free(0x4000, (unsigned long)boot_params); | |
851 | return NULL; | |
852 | } | |
853 | ||
854 | static efi_status_t exit_boot(struct boot_params *boot_params, | |
855 | void *handle) | |
856 | { | |
857 | struct efi_info *efi = &boot_params->efi_info; | |
858 | struct e820entry *e820_map = &boot_params->e820_map[0]; | |
859 | struct e820entry *prev = NULL; | |
860 | unsigned long size, key, desc_size, _size; | |
861 | efi_memory_desc_t *mem_map; | |
862 | efi_status_t status; | |
863 | __u32 desc_version; | |
864 | u8 nr_entries; | |
865 | int i; | |
291f3632 MF |
866 | |
867 | size = sizeof(*mem_map) * 32; | |
868 | ||
869 | again: | |
870 | size += sizeof(*mem_map); | |
871 | _size = size; | |
872 | status = low_alloc(size, 1, (unsigned long *)&mem_map); | |
873 | if (status != EFI_SUCCESS) | |
9ca8f72a | 874 | return status; |
291f3632 MF |
875 | |
876 | status = efi_call_phys5(sys_table->boottime->get_memory_map, &size, | |
877 | mem_map, &key, &desc_size, &desc_version); | |
878 | if (status == EFI_BUFFER_TOO_SMALL) { | |
879 | low_free(_size, (unsigned long)mem_map); | |
880 | goto again; | |
881 | } | |
882 | ||
883 | if (status != EFI_SUCCESS) | |
884 | goto free_mem_map; | |
885 | ||
9ca8f72a | 886 | memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32)); |
291f3632 MF |
887 | efi->efi_systab = (unsigned long)sys_table; |
888 | efi->efi_memdesc_size = desc_size; | |
889 | efi->efi_memdesc_version = desc_version; | |
890 | efi->efi_memmap = (unsigned long)mem_map; | |
891 | efi->efi_memmap_size = size; | |
892 | ||
893 | #ifdef CONFIG_X86_64 | |
894 | efi->efi_systab_hi = (unsigned long)sys_table >> 32; | |
895 | efi->efi_memmap_hi = (unsigned long)mem_map >> 32; | |
896 | #endif | |
897 | ||
898 | /* Might as well exit boot services now */ | |
899 | status = efi_call_phys2(sys_table->boottime->exit_boot_services, | |
900 | handle, key); | |
901 | if (status != EFI_SUCCESS) | |
902 | goto free_mem_map; | |
903 | ||
904 | /* Historic? */ | |
905 | boot_params->alt_mem_k = 32 * 1024; | |
906 | ||
907 | /* | |
908 | * Convert the EFI memory map to E820. | |
909 | */ | |
910 | nr_entries = 0; | |
911 | for (i = 0; i < size / desc_size; i++) { | |
912 | efi_memory_desc_t *d; | |
913 | unsigned int e820_type = 0; | |
914 | unsigned long m = (unsigned long)mem_map; | |
915 | ||
916 | d = (efi_memory_desc_t *)(m + (i * desc_size)); | |
917 | switch (d->type) { | |
918 | case EFI_RESERVED_TYPE: | |
919 | case EFI_RUNTIME_SERVICES_CODE: | |
920 | case EFI_RUNTIME_SERVICES_DATA: | |
921 | case EFI_MEMORY_MAPPED_IO: | |
922 | case EFI_MEMORY_MAPPED_IO_PORT_SPACE: | |
923 | case EFI_PAL_CODE: | |
924 | e820_type = E820_RESERVED; | |
925 | break; | |
926 | ||
927 | case EFI_UNUSABLE_MEMORY: | |
928 | e820_type = E820_UNUSABLE; | |
929 | break; | |
930 | ||
931 | case EFI_ACPI_RECLAIM_MEMORY: | |
932 | e820_type = E820_ACPI; | |
933 | break; | |
934 | ||
935 | case EFI_LOADER_CODE: | |
936 | case EFI_LOADER_DATA: | |
937 | case EFI_BOOT_SERVICES_CODE: | |
938 | case EFI_BOOT_SERVICES_DATA: | |
939 | case EFI_CONVENTIONAL_MEMORY: | |
940 | e820_type = E820_RAM; | |
941 | break; | |
942 | ||
943 | case EFI_ACPI_MEMORY_NVS: | |
944 | e820_type = E820_NVS; | |
945 | break; | |
946 | ||
947 | default: | |
948 | continue; | |
949 | } | |
950 | ||
951 | /* Merge adjacent mappings */ | |
952 | if (prev && prev->type == e820_type && | |
953 | (prev->addr + prev->size) == d->phys_addr) | |
954 | prev->size += d->num_pages << 12; | |
955 | else { | |
956 | e820_map->addr = d->phys_addr; | |
957 | e820_map->size = d->num_pages << 12; | |
958 | e820_map->type = e820_type; | |
959 | prev = e820_map++; | |
960 | nr_entries++; | |
961 | } | |
962 | } | |
963 | ||
964 | boot_params->e820_entries = nr_entries; | |
965 | ||
966 | return EFI_SUCCESS; | |
967 | ||
968 | free_mem_map: | |
969 | low_free(_size, (unsigned long)mem_map); | |
291f3632 MF |
970 | return status; |
971 | } | |
972 | ||
9ca8f72a | 973 | static efi_status_t relocate_kernel(struct setup_header *hdr) |
291f3632 | 974 | { |
291f3632 | 975 | unsigned long start, nr_pages; |
291f3632 | 976 | efi_status_t status; |
e31be363 | 977 | |
291f3632 MF |
978 | /* |
979 | * The EFI firmware loader could have placed the kernel image | |
980 | * anywhere in memory, but the kernel has various restrictions | |
981 | * on the max physical address it can run at. Attempt to move | |
982 | * the kernel to boot_params.pref_address, or as low as | |
983 | * possible. | |
984 | */ | |
985 | start = hdr->pref_address; | |
986 | nr_pages = round_up(hdr->init_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; | |
987 | ||
988 | status = efi_call_phys4(sys_table->boottime->allocate_pages, | |
989 | EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, | |
990 | nr_pages, &start); | |
991 | if (status != EFI_SUCCESS) { | |
992 | status = low_alloc(hdr->init_size, hdr->kernel_alignment, | |
993 | &start); | |
9ca8f72a | 994 | if (status != EFI_SUCCESS) |
9fa7deda | 995 | efi_printk("Failed to alloc mem for kernel\n"); |
291f3632 MF |
996 | } |
997 | ||
9ca8f72a MF |
998 | if (status == EFI_SUCCESS) |
999 | memcpy((void *)start, (void *)(unsigned long)hdr->code32_start, | |
1000 | hdr->init_size); | |
1001 | ||
1002 | hdr->pref_address = hdr->code32_start; | |
291f3632 | 1003 | hdr->code32_start = (__u32)start; |
291f3632 | 1004 | |
9ca8f72a MF |
1005 | return status; |
1006 | } | |
1007 | ||
1008 | /* | |
1009 | * On success we return a pointer to a boot_params structure, and NULL | |
1010 | * on failure. | |
1011 | */ | |
1012 | struct boot_params *efi_main(void *handle, efi_system_table_t *_table, | |
1013 | struct boot_params *boot_params) | |
1014 | { | |
1015 | struct desc_ptr *gdt, *idt; | |
1016 | efi_loaded_image_t *image; | |
1017 | struct setup_header *hdr = &boot_params->hdr; | |
1018 | efi_status_t status; | |
1019 | struct desc_struct *desc; | |
1020 | ||
1021 | sys_table = _table; | |
1022 | ||
1023 | /* Check if we were booted by the EFI firmware */ | |
1024 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) | |
1025 | goto fail; | |
1026 | ||
1027 | setup_graphics(boot_params); | |
291f3632 MF |
1028 | |
1029 | status = efi_call_phys3(sys_table->boottime->allocate_pool, | |
1030 | EFI_LOADER_DATA, sizeof(*gdt), | |
1031 | (void **)&gdt); | |
9fa7deda MF |
1032 | if (status != EFI_SUCCESS) { |
1033 | efi_printk("Failed to alloc mem for gdt structure\n"); | |
291f3632 | 1034 | goto fail; |
9fa7deda | 1035 | } |
291f3632 MF |
1036 | |
1037 | gdt->size = 0x800; | |
1038 | status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address); | |
9fa7deda MF |
1039 | if (status != EFI_SUCCESS) { |
1040 | efi_printk("Failed to alloc mem for gdt\n"); | |
291f3632 | 1041 | goto fail; |
9fa7deda | 1042 | } |
291f3632 MF |
1043 | |
1044 | status = efi_call_phys3(sys_table->boottime->allocate_pool, | |
1045 | EFI_LOADER_DATA, sizeof(*idt), | |
1046 | (void **)&idt); | |
9fa7deda MF |
1047 | if (status != EFI_SUCCESS) { |
1048 | efi_printk("Failed to alloc mem for idt structure\n"); | |
291f3632 | 1049 | goto fail; |
9fa7deda | 1050 | } |
291f3632 MF |
1051 | |
1052 | idt->size = 0; | |
1053 | idt->address = 0; | |
1054 | ||
9ca8f72a MF |
1055 | /* |
1056 | * If the kernel isn't already loaded at the preferred load | |
1057 | * address, relocate it. | |
1058 | */ | |
1059 | if (hdr->pref_address != hdr->code32_start) { | |
1060 | status = relocate_kernel(hdr); | |
1061 | ||
1062 | if (status != EFI_SUCCESS) | |
1063 | goto fail; | |
1064 | } | |
1065 | ||
1066 | status = exit_boot(boot_params, handle); | |
291f3632 MF |
1067 | if (status != EFI_SUCCESS) |
1068 | goto fail; | |
1069 | ||
1070 | memset((char *)gdt->address, 0x0, gdt->size); | |
1071 | desc = (struct desc_struct *)gdt->address; | |
1072 | ||
1073 | /* The first GDT is a dummy and the second is unused. */ | |
1074 | desc += 2; | |
1075 | ||
1076 | desc->limit0 = 0xffff; | |
1077 | desc->base0 = 0x0000; | |
1078 | desc->base1 = 0x0000; | |
1079 | desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ; | |
1080 | desc->s = DESC_TYPE_CODE_DATA; | |
1081 | desc->dpl = 0; | |
1082 | desc->p = 1; | |
1083 | desc->limit = 0xf; | |
1084 | desc->avl = 0; | |
1085 | desc->l = 0; | |
1086 | desc->d = SEG_OP_SIZE_32BIT; | |
1087 | desc->g = SEG_GRANULARITY_4KB; | |
1088 | desc->base2 = 0x00; | |
1089 | ||
1090 | desc++; | |
1091 | desc->limit0 = 0xffff; | |
1092 | desc->base0 = 0x0000; | |
1093 | desc->base1 = 0x0000; | |
1094 | desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE; | |
1095 | desc->s = DESC_TYPE_CODE_DATA; | |
1096 | desc->dpl = 0; | |
1097 | desc->p = 1; | |
1098 | desc->limit = 0xf; | |
1099 | desc->avl = 0; | |
1100 | desc->l = 0; | |
1101 | desc->d = SEG_OP_SIZE_32BIT; | |
1102 | desc->g = SEG_GRANULARITY_4KB; | |
1103 | desc->base2 = 0x00; | |
1104 | ||
1105 | #ifdef CONFIG_X86_64 | |
1106 | /* Task segment value */ | |
1107 | desc++; | |
1108 | desc->limit0 = 0x0000; | |
1109 | desc->base0 = 0x0000; | |
1110 | desc->base1 = 0x0000; | |
1111 | desc->type = SEG_TYPE_TSS; | |
1112 | desc->s = 0; | |
1113 | desc->dpl = 0; | |
1114 | desc->p = 1; | |
1115 | desc->limit = 0x0; | |
1116 | desc->avl = 0; | |
1117 | desc->l = 0; | |
1118 | desc->d = 0; | |
1119 | desc->g = SEG_GRANULARITY_4KB; | |
1120 | desc->base2 = 0x00; | |
1121 | #endif /* CONFIG_X86_64 */ | |
1122 | ||
1123 | asm volatile ("lidt %0" : : "m" (*idt)); | |
1124 | asm volatile ("lgdt %0" : : "m" (*gdt)); | |
1125 | ||
1126 | asm volatile("cli"); | |
1127 | ||
1128 | return boot_params; | |
1129 | fail: | |
1130 | return NULL; | |
1131 | } |