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