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> | |
dd5fc854 | 11 | #include <linux/pci.h> |
291f3632 MF |
12 | #include <asm/efi.h> |
13 | #include <asm/setup.h> | |
14 | #include <asm/desc.h> | |
15 | ||
0f905a43 MF |
16 | #undef memcpy /* Use memcpy from misc.c */ |
17 | ||
291f3632 MF |
18 | #include "eboot.h" |
19 | ||
20 | static efi_system_table_t *sys_table; | |
21 | ||
84be8805 MF |
22 | static struct efi_config *efi_early; |
23 | ||
24 | #define efi_call_early(f, ...) \ | |
25 | efi_early->call(efi_early->f, __VA_ARGS__); | |
204b0a1a | 26 | |
54b52d87 MF |
27 | #define BOOT_SERVICES(bits) \ |
28 | static void setup_boot_services##bits(struct efi_config *c) \ | |
29 | { \ | |
30 | efi_system_table_##bits##_t *table; \ | |
31 | efi_boot_services_##bits##_t *bt; \ | |
32 | \ | |
33 | table = (typeof(table))sys_table; \ | |
34 | \ | |
35 | c->text_output = table->con_out; \ | |
36 | \ | |
37 | bt = (typeof(bt))(unsigned long)(table->boottime); \ | |
38 | \ | |
39 | c->allocate_pool = bt->allocate_pool; \ | |
40 | c->allocate_pages = bt->allocate_pages; \ | |
41 | c->get_memory_map = bt->get_memory_map; \ | |
42 | c->free_pool = bt->free_pool; \ | |
43 | c->free_pages = bt->free_pages; \ | |
44 | c->locate_handle = bt->locate_handle; \ | |
45 | c->handle_protocol = bt->handle_protocol; \ | |
46 | c->exit_boot_services = bt->exit_boot_services; \ | |
47 | } | |
48 | BOOT_SERVICES(32); | |
49 | BOOT_SERVICES(64); | |
291f3632 | 50 | |
bd669475 | 51 | void efi_char16_printk(efi_system_table_t *, efi_char16_t *); |
54b52d87 MF |
52 | |
53 | static efi_status_t | |
c116e8d6 MF |
54 | __file_size32(void *__fh, efi_char16_t *filename_16, |
55 | void **handle, u64 *file_sz) | |
56 | { | |
57 | efi_file_handle_32_t *h, *fh = __fh; | |
58 | efi_file_info_t *info; | |
59 | efi_status_t status; | |
60 | efi_guid_t info_guid = EFI_FILE_INFO_ID; | |
61 | u32 info_sz; | |
62 | ||
63 | status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16, | |
64 | EFI_FILE_MODE_READ, (u64)0); | |
65 | if (status != EFI_SUCCESS) { | |
66 | efi_printk(sys_table, "Failed to open file: "); | |
67 | efi_char16_printk(sys_table, filename_16); | |
68 | efi_printk(sys_table, "\n"); | |
69 | return status; | |
70 | } | |
71 | ||
72 | *handle = h; | |
73 | ||
74 | info_sz = 0; | |
75 | status = efi_early->call((unsigned long)h->get_info, h, &info_guid, | |
76 | &info_sz, NULL); | |
77 | if (status != EFI_BUFFER_TOO_SMALL) { | |
78 | efi_printk(sys_table, "Failed to get file info size\n"); | |
79 | return status; | |
80 | } | |
81 | ||
82 | grow: | |
204b0a1a MF |
83 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
84 | info_sz, (void **)&info); | |
c116e8d6 MF |
85 | if (status != EFI_SUCCESS) { |
86 | efi_printk(sys_table, "Failed to alloc mem for file info\n"); | |
87 | return status; | |
88 | } | |
89 | ||
90 | status = efi_early->call((unsigned long)h->get_info, h, &info_guid, | |
91 | &info_sz, info); | |
92 | if (status == EFI_BUFFER_TOO_SMALL) { | |
204b0a1a | 93 | efi_call_early(free_pool, info); |
c116e8d6 MF |
94 | goto grow; |
95 | } | |
96 | ||
97 | *file_sz = info->file_size; | |
204b0a1a | 98 | efi_call_early(free_pool, info); |
c116e8d6 MF |
99 | |
100 | if (status != EFI_SUCCESS) | |
101 | efi_printk(sys_table, "Failed to get initrd info\n"); | |
102 | ||
103 | return status; | |
104 | } | |
105 | ||
106 | static efi_status_t | |
107 | __file_size64(void *__fh, efi_char16_t *filename_16, | |
108 | void **handle, u64 *file_sz) | |
54b52d87 | 109 | { |
c116e8d6 | 110 | efi_file_handle_64_t *h, *fh = __fh; |
54b52d87 MF |
111 | efi_file_info_t *info; |
112 | efi_status_t status; | |
113 | efi_guid_t info_guid = EFI_FILE_INFO_ID; | |
396f1a08 | 114 | u64 info_sz; |
54b52d87 MF |
115 | |
116 | status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16, | |
117 | EFI_FILE_MODE_READ, (u64)0); | |
118 | if (status != EFI_SUCCESS) { | |
119 | efi_printk(sys_table, "Failed to open file: "); | |
120 | efi_char16_printk(sys_table, filename_16); | |
121 | efi_printk(sys_table, "\n"); | |
122 | return status; | |
123 | } | |
124 | ||
125 | *handle = h; | |
126 | ||
127 | info_sz = 0; | |
128 | status = efi_early->call((unsigned long)h->get_info, h, &info_guid, | |
129 | &info_sz, NULL); | |
130 | if (status != EFI_BUFFER_TOO_SMALL) { | |
131 | efi_printk(sys_table, "Failed to get file info size\n"); | |
132 | return status; | |
133 | } | |
134 | ||
135 | grow: | |
204b0a1a MF |
136 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
137 | info_sz, (void **)&info); | |
54b52d87 MF |
138 | if (status != EFI_SUCCESS) { |
139 | efi_printk(sys_table, "Failed to alloc mem for file info\n"); | |
140 | return status; | |
141 | } | |
142 | ||
143 | status = efi_early->call((unsigned long)h->get_info, h, &info_guid, | |
144 | &info_sz, info); | |
145 | if (status == EFI_BUFFER_TOO_SMALL) { | |
204b0a1a | 146 | efi_call_early(free_pool, info); |
54b52d87 MF |
147 | goto grow; |
148 | } | |
149 | ||
150 | *file_sz = info->file_size; | |
204b0a1a | 151 | efi_call_early(free_pool, info); |
54b52d87 MF |
152 | |
153 | if (status != EFI_SUCCESS) | |
154 | efi_printk(sys_table, "Failed to get initrd info\n"); | |
155 | ||
156 | return status; | |
157 | } | |
bd669475 | 158 | efi_status_t |
c116e8d6 MF |
159 | efi_file_size(efi_system_table_t *sys_table, void *__fh, |
160 | efi_char16_t *filename_16, void **handle, u64 *file_sz) | |
161 | { | |
162 | if (efi_early->is64) | |
163 | return __file_size64(__fh, filename_16, handle, file_sz); | |
164 | ||
165 | return __file_size32(__fh, filename_16, handle, file_sz); | |
166 | } | |
54b52d87 | 167 | |
bd669475 | 168 | efi_status_t |
47514c99 | 169 | efi_file_read(void *handle, unsigned long *size, void *addr) |
54b52d87 | 170 | { |
c116e8d6 MF |
171 | unsigned long func; |
172 | ||
173 | if (efi_early->is64) { | |
47514c99 | 174 | efi_file_handle_64_t *fh = handle; |
c116e8d6 MF |
175 | |
176 | func = (unsigned long)fh->read; | |
177 | return efi_early->call(func, handle, size, addr); | |
178 | } else { | |
47514c99 | 179 | efi_file_handle_32_t *fh = handle; |
c116e8d6 MF |
180 | |
181 | func = (unsigned long)fh->read; | |
182 | return efi_early->call(func, handle, size, addr); | |
183 | } | |
54b52d87 MF |
184 | } |
185 | ||
bd669475 | 186 | efi_status_t efi_file_close(void *handle) |
54b52d87 | 187 | { |
c116e8d6 | 188 | if (efi_early->is64) { |
47514c99 | 189 | efi_file_handle_64_t *fh = handle; |
c116e8d6 MF |
190 | |
191 | return efi_early->call((unsigned long)fh->close, handle); | |
192 | } else { | |
47514c99 | 193 | efi_file_handle_32_t *fh = handle; |
c116e8d6 MF |
194 | |
195 | return efi_early->call((unsigned long)fh->close, handle); | |
196 | } | |
197 | } | |
198 | ||
199 | static inline efi_status_t __open_volume32(void *__image, void **__fh) | |
200 | { | |
201 | efi_file_io_interface_t *io; | |
202 | efi_loaded_image_32_t *image = __image; | |
203 | efi_file_handle_32_t *fh; | |
204 | efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; | |
205 | efi_status_t status; | |
206 | void *handle = (void *)(unsigned long)image->device_handle; | |
207 | unsigned long func; | |
208 | ||
204b0a1a MF |
209 | status = efi_call_early(handle_protocol, handle, |
210 | &fs_proto, (void **)&io); | |
c116e8d6 MF |
211 | if (status != EFI_SUCCESS) { |
212 | efi_printk(sys_table, "Failed to handle fs_proto\n"); | |
213 | return status; | |
214 | } | |
215 | ||
216 | func = (unsigned long)io->open_volume; | |
217 | status = efi_early->call(func, io, &fh); | |
218 | if (status != EFI_SUCCESS) | |
219 | efi_printk(sys_table, "Failed to open volume\n"); | |
291f3632 | 220 | |
c116e8d6 MF |
221 | *__fh = fh; |
222 | return status; | |
54b52d87 MF |
223 | } |
224 | ||
c116e8d6 | 225 | static inline efi_status_t __open_volume64(void *__image, void **__fh) |
54b52d87 MF |
226 | { |
227 | efi_file_io_interface_t *io; | |
c116e8d6 MF |
228 | efi_loaded_image_64_t *image = __image; |
229 | efi_file_handle_64_t *fh; | |
54b52d87 MF |
230 | efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID; |
231 | efi_status_t status; | |
232 | void *handle = (void *)(unsigned long)image->device_handle; | |
c116e8d6 | 233 | unsigned long func; |
54b52d87 | 234 | |
204b0a1a MF |
235 | status = efi_call_early(handle_protocol, handle, |
236 | &fs_proto, (void **)&io); | |
54b52d87 MF |
237 | if (status != EFI_SUCCESS) { |
238 | efi_printk(sys_table, "Failed to handle fs_proto\n"); | |
239 | return status; | |
240 | } | |
291f3632 | 241 | |
54b52d87 MF |
242 | func = (unsigned long)io->open_volume; |
243 | status = efi_early->call(func, io, &fh); | |
244 | if (status != EFI_SUCCESS) | |
245 | efi_printk(sys_table, "Failed to open volume\n"); | |
246 | ||
247 | *__fh = fh; | |
248 | return status; | |
249 | } | |
250 | ||
bd669475 | 251 | efi_status_t |
c116e8d6 MF |
252 | efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh) |
253 | { | |
254 | if (efi_early->is64) | |
255 | return __open_volume64(__image, __fh); | |
256 | ||
257 | return __open_volume32(__image, __fh); | |
258 | } | |
259 | ||
bd669475 | 260 | void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str) |
54b52d87 | 261 | { |
54b52d87 MF |
262 | unsigned long output_string; |
263 | size_t offset; | |
54b52d87 | 264 | |
c116e8d6 MF |
265 | if (efi_early->is64) { |
266 | struct efi_simple_text_output_protocol_64 *out; | |
267 | u64 *func; | |
54b52d87 | 268 | |
c116e8d6 MF |
269 | offset = offsetof(typeof(*out), output_string); |
270 | output_string = efi_early->text_output + offset; | |
271 | func = (u64 *)output_string; | |
272 | ||
273 | efi_early->call(*func, efi_early->text_output, str); | |
274 | } else { | |
275 | struct efi_simple_text_output_protocol_32 *out; | |
276 | u32 *func; | |
277 | ||
278 | offset = offsetof(typeof(*out), output_string); | |
279 | output_string = efi_early->text_output + offset; | |
280 | func = (u32 *)output_string; | |
281 | ||
282 | efi_early->call(*func, efi_early->text_output, str); | |
283 | } | |
54b52d87 MF |
284 | } |
285 | ||
84be8805 MF |
286 | #include "../../../../drivers/firmware/efi/libstub/efi-stub-helper.c" |
287 | ||
291f3632 MF |
288 | static void find_bits(unsigned long mask, u8 *pos, u8 *size) |
289 | { | |
290 | u8 first, len; | |
291 | ||
292 | first = 0; | |
293 | len = 0; | |
294 | ||
295 | if (mask) { | |
296 | while (!(mask & 0x1)) { | |
297 | mask = mask >> 1; | |
298 | first++; | |
299 | } | |
300 | ||
301 | while (mask & 0x1) { | |
302 | mask = mask >> 1; | |
303 | len++; | |
304 | } | |
305 | } | |
306 | ||
307 | *pos = first; | |
308 | *size = len; | |
309 | } | |
310 | ||
c116e8d6 MF |
311 | static efi_status_t |
312 | __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom) | |
dd5fc854 | 313 | { |
c116e8d6 | 314 | struct pci_setup_rom *rom = NULL; |
dd5fc854 | 315 | efi_status_t status; |
c116e8d6 MF |
316 | unsigned long size; |
317 | uint64_t attributes; | |
dd5fc854 | 318 | |
c116e8d6 MF |
319 | status = efi_early->call(pci->attributes, pci, |
320 | EfiPciIoAttributeOperationGet, 0, 0, | |
321 | &attributes); | |
322 | if (status != EFI_SUCCESS) | |
323 | return status; | |
dd5fc854 | 324 | |
c116e8d6 MF |
325 | if (!pci->romimage || !pci->romsize) |
326 | return EFI_INVALID_PARAMETER; | |
dd5fc854 | 327 | |
c116e8d6 | 328 | size = pci->romsize + sizeof(*rom); |
dd5fc854 | 329 | |
204b0a1a | 330 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom); |
c116e8d6 MF |
331 | if (status != EFI_SUCCESS) |
332 | return status; | |
dd5fc854 | 333 | |
c116e8d6 MF |
334 | memset(rom, 0, sizeof(*rom)); |
335 | ||
336 | rom->data.type = SETUP_PCI; | |
337 | rom->data.len = size - sizeof(struct setup_data); | |
338 | rom->data.next = 0; | |
339 | rom->pcilen = pci->romsize; | |
340 | *__rom = rom; | |
341 | ||
342 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, | |
343 | PCI_VENDOR_ID, 1, &(rom->vendor)); | |
dd5fc854 MG |
344 | |
345 | if (status != EFI_SUCCESS) | |
c116e8d6 MF |
346 | goto free_struct; |
347 | ||
348 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, | |
349 | PCI_DEVICE_ID, 1, &(rom->devid)); | |
350 | ||
351 | if (status != EFI_SUCCESS) | |
352 | goto free_struct; | |
353 | ||
354 | status = efi_early->call(pci->get_location, pci, &(rom->segment), | |
355 | &(rom->bus), &(rom->device), &(rom->function)); | |
356 | ||
357 | if (status != EFI_SUCCESS) | |
358 | goto free_struct; | |
359 | ||
360 | memcpy(rom->romdata, pci->romimage, pci->romsize); | |
361 | return status; | |
362 | ||
363 | free_struct: | |
204b0a1a | 364 | efi_call_early(free_pool, rom); |
c116e8d6 MF |
365 | return status; |
366 | } | |
367 | ||
56394ab8 | 368 | static void |
c116e8d6 MF |
369 | setup_efi_pci32(struct boot_params *params, void **pci_handle, |
370 | unsigned long size) | |
371 | { | |
372 | efi_pci_io_protocol_32 *pci = NULL; | |
373 | efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; | |
374 | u32 *handles = (u32 *)(unsigned long)pci_handle; | |
375 | efi_status_t status; | |
376 | unsigned long nr_pci; | |
377 | struct setup_data *data; | |
378 | int i; | |
379 | ||
380 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; | |
381 | ||
382 | while (data && data->next) | |
383 | data = (struct setup_data *)(unsigned long)data->next; | |
dd5fc854 | 384 | |
c116e8d6 | 385 | nr_pci = size / sizeof(u32); |
dd5fc854 | 386 | for (i = 0; i < nr_pci; i++) { |
c116e8d6 MF |
387 | struct pci_setup_rom *rom = NULL; |
388 | u32 h = handles[i]; | |
dd5fc854 | 389 | |
204b0a1a MF |
390 | status = efi_call_early(handle_protocol, h, |
391 | &pci_proto, (void **)&pci); | |
dd5fc854 MG |
392 | |
393 | if (status != EFI_SUCCESS) | |
394 | continue; | |
395 | ||
396 | if (!pci) | |
397 | continue; | |
398 | ||
c116e8d6 | 399 | status = __setup_efi_pci32(pci, &rom); |
dd5fc854 MG |
400 | if (status != EFI_SUCCESS) |
401 | continue; | |
402 | ||
c116e8d6 MF |
403 | if (data) |
404 | data->next = (unsigned long)rom; | |
405 | else | |
406 | params->hdr.setup_data = (unsigned long)rom; | |
407 | ||
408 | data = (struct setup_data *)rom; | |
dd5fc854 | 409 | |
c116e8d6 | 410 | } |
c116e8d6 | 411 | } |
dd5fc854 | 412 | |
c116e8d6 MF |
413 | static efi_status_t |
414 | __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom) | |
415 | { | |
416 | struct pci_setup_rom *rom; | |
417 | efi_status_t status; | |
418 | unsigned long size; | |
419 | uint64_t attributes; | |
dd5fc854 | 420 | |
c116e8d6 MF |
421 | status = efi_early->call(pci->attributes, pci, |
422 | EfiPciIoAttributeOperationGet, 0, | |
423 | &attributes); | |
424 | if (status != EFI_SUCCESS) | |
425 | return status; | |
dd5fc854 | 426 | |
c116e8d6 MF |
427 | if (!pci->romimage || !pci->romsize) |
428 | return EFI_INVALID_PARAMETER; | |
dd5fc854 | 429 | |
c116e8d6 | 430 | size = pci->romsize + sizeof(*rom); |
dd5fc854 | 431 | |
204b0a1a | 432 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom); |
c116e8d6 MF |
433 | if (status != EFI_SUCCESS) |
434 | return status; | |
435 | ||
436 | rom->data.type = SETUP_PCI; | |
437 | rom->data.len = size - sizeof(struct setup_data); | |
438 | rom->data.next = 0; | |
439 | rom->pcilen = pci->romsize; | |
440 | *__rom = rom; | |
441 | ||
442 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, | |
443 | PCI_VENDOR_ID, 1, &(rom->vendor)); | |
444 | ||
445 | if (status != EFI_SUCCESS) | |
446 | goto free_struct; | |
447 | ||
448 | status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16, | |
449 | PCI_DEVICE_ID, 1, &(rom->devid)); | |
450 | ||
451 | if (status != EFI_SUCCESS) | |
452 | goto free_struct; | |
453 | ||
454 | status = efi_early->call(pci->get_location, pci, &(rom->segment), | |
455 | &(rom->bus), &(rom->device), &(rom->function)); | |
456 | ||
457 | if (status != EFI_SUCCESS) | |
458 | goto free_struct; | |
459 | ||
460 | memcpy(rom->romdata, pci->romimage, pci->romsize); | |
461 | return status; | |
462 | ||
463 | free_struct: | |
204b0a1a | 464 | efi_call_early(free_pool, rom); |
c116e8d6 MF |
465 | return status; |
466 | ||
467 | } | |
468 | ||
56394ab8 | 469 | static void |
c116e8d6 MF |
470 | setup_efi_pci64(struct boot_params *params, void **pci_handle, |
471 | unsigned long size) | |
472 | { | |
473 | efi_pci_io_protocol_64 *pci = NULL; | |
474 | efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; | |
475 | u64 *handles = (u64 *)(unsigned long)pci_handle; | |
476 | efi_status_t status; | |
477 | unsigned long nr_pci; | |
478 | struct setup_data *data; | |
479 | int i; | |
dd5fc854 | 480 | |
c116e8d6 MF |
481 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
482 | ||
483 | while (data && data->next) | |
484 | data = (struct setup_data *)(unsigned long)data->next; | |
485 | ||
486 | nr_pci = size / sizeof(u64); | |
487 | for (i = 0; i < nr_pci; i++) { | |
488 | struct pci_setup_rom *rom = NULL; | |
489 | u64 h = handles[i]; | |
490 | ||
204b0a1a MF |
491 | status = efi_call_early(handle_protocol, h, |
492 | &pci_proto, (void **)&pci); | |
dd5fc854 MG |
493 | |
494 | if (status != EFI_SUCCESS) | |
c116e8d6 MF |
495 | continue; |
496 | ||
497 | if (!pci) | |
498 | continue; | |
dd5fc854 | 499 | |
c116e8d6 MF |
500 | status = __setup_efi_pci64(pci, &rom); |
501 | if (status != EFI_SUCCESS) | |
502 | continue; | |
dd5fc854 MG |
503 | |
504 | if (data) | |
bc754790 | 505 | data->next = (unsigned long)rom; |
dd5fc854 | 506 | else |
bc754790 | 507 | params->hdr.setup_data = (unsigned long)rom; |
dd5fc854 MG |
508 | |
509 | data = (struct setup_data *)rom; | |
510 | ||
dd5fc854 | 511 | } |
dd5fc854 MG |
512 | } |
513 | ||
56394ab8 MF |
514 | /* |
515 | * There's no way to return an informative status from this function, | |
516 | * because any analysis (and printing of error messages) needs to be | |
517 | * done directly at the EFI function call-site. | |
518 | * | |
519 | * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we | |
520 | * just didn't find any PCI devices, but there's no way to tell outside | |
521 | * the context of the call. | |
522 | */ | |
523 | static void setup_efi_pci(struct boot_params *params) | |
291f3632 | 524 | { |
291f3632 | 525 | efi_status_t status; |
c116e8d6 MF |
526 | void **pci_handle = NULL; |
527 | efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; | |
528 | unsigned long size = 0; | |
291f3632 | 529 | |
204b0a1a MF |
530 | status = efi_call_early(locate_handle, |
531 | EFI_LOCATE_BY_PROTOCOL, | |
532 | &pci_proto, NULL, &size, pci_handle); | |
291f3632 | 533 | |
c116e8d6 | 534 | if (status == EFI_BUFFER_TOO_SMALL) { |
204b0a1a MF |
535 | status = efi_call_early(allocate_pool, |
536 | EFI_LOADER_DATA, | |
537 | size, (void **)&pci_handle); | |
291f3632 | 538 | |
291f3632 | 539 | if (status != EFI_SUCCESS) |
56394ab8 | 540 | return; |
291f3632 | 541 | |
204b0a1a MF |
542 | status = efi_call_early(locate_handle, |
543 | EFI_LOCATE_BY_PROTOCOL, &pci_proto, | |
544 | NULL, &size, pci_handle); | |
291f3632 MF |
545 | } |
546 | ||
c116e8d6 | 547 | if (status != EFI_SUCCESS) |
291f3632 MF |
548 | goto free_handle; |
549 | ||
c116e8d6 | 550 | if (efi_early->is64) |
56394ab8 | 551 | setup_efi_pci64(params, pci_handle, size); |
c116e8d6 | 552 | else |
56394ab8 | 553 | setup_efi_pci32(params, pci_handle, size); |
291f3632 | 554 | |
c116e8d6 | 555 | free_handle: |
204b0a1a | 556 | efi_call_early(free_pool, pci_handle); |
c116e8d6 | 557 | } |
291f3632 | 558 | |
c116e8d6 MF |
559 | static void |
560 | setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line, | |
561 | struct efi_pixel_bitmask pixel_info, int pixel_format) | |
562 | { | |
291f3632 MF |
563 | if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) { |
564 | si->lfb_depth = 32; | |
565 | si->lfb_linelength = pixels_per_scan_line * 4; | |
566 | si->red_size = 8; | |
567 | si->red_pos = 0; | |
568 | si->green_size = 8; | |
569 | si->green_pos = 8; | |
570 | si->blue_size = 8; | |
571 | si->blue_pos = 16; | |
572 | si->rsvd_size = 8; | |
573 | si->rsvd_pos = 24; | |
574 | } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) { | |
575 | si->lfb_depth = 32; | |
576 | si->lfb_linelength = pixels_per_scan_line * 4; | |
577 | si->red_size = 8; | |
578 | si->red_pos = 16; | |
579 | si->green_size = 8; | |
580 | si->green_pos = 8; | |
581 | si->blue_size = 8; | |
582 | si->blue_pos = 0; | |
583 | si->rsvd_size = 8; | |
584 | si->rsvd_pos = 24; | |
585 | } else if (pixel_format == PIXEL_BIT_MASK) { | |
586 | find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size); | |
587 | find_bits(pixel_info.green_mask, &si->green_pos, | |
588 | &si->green_size); | |
589 | find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size); | |
590 | find_bits(pixel_info.reserved_mask, &si->rsvd_pos, | |
591 | &si->rsvd_size); | |
592 | si->lfb_depth = si->red_size + si->green_size + | |
593 | si->blue_size + si->rsvd_size; | |
594 | si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8; | |
595 | } else { | |
596 | si->lfb_depth = 4; | |
597 | si->lfb_linelength = si->lfb_width / 2; | |
598 | si->red_size = 0; | |
599 | si->red_pos = 0; | |
600 | si->green_size = 0; | |
601 | si->green_pos = 0; | |
602 | si->blue_size = 0; | |
603 | si->blue_pos = 0; | |
604 | si->rsvd_size = 0; | |
605 | si->rsvd_pos = 0; | |
606 | } | |
c116e8d6 MF |
607 | } |
608 | ||
609 | static efi_status_t | |
610 | __gop_query32(struct efi_graphics_output_protocol_32 *gop32, | |
611 | struct efi_graphics_output_mode_info **info, | |
612 | unsigned long *size, u32 *fb_base) | |
613 | { | |
614 | struct efi_graphics_output_protocol_mode_32 *mode; | |
615 | efi_status_t status; | |
616 | unsigned long m; | |
617 | ||
618 | m = gop32->mode; | |
619 | mode = (struct efi_graphics_output_protocol_mode_32 *)m; | |
620 | ||
621 | status = efi_early->call(gop32->query_mode, gop32, | |
622 | mode->mode, size, info); | |
623 | if (status != EFI_SUCCESS) | |
624 | return status; | |
625 | ||
626 | *fb_base = mode->frame_buffer_base; | |
627 | return status; | |
628 | } | |
629 | ||
630 | static efi_status_t | |
631 | setup_gop32(struct screen_info *si, efi_guid_t *proto, | |
632 | unsigned long size, void **gop_handle) | |
633 | { | |
634 | struct efi_graphics_output_protocol_32 *gop32, *first_gop; | |
635 | unsigned long nr_gops; | |
636 | u16 width, height; | |
637 | u32 pixels_per_scan_line; | |
638 | u32 fb_base; | |
639 | struct efi_pixel_bitmask pixel_info; | |
640 | int pixel_format; | |
641 | efi_status_t status; | |
642 | u32 *handles = (u32 *)(unsigned long)gop_handle; | |
643 | int i; | |
644 | ||
645 | first_gop = NULL; | |
646 | gop32 = NULL; | |
647 | ||
648 | nr_gops = size / sizeof(u32); | |
649 | for (i = 0; i < nr_gops; i++) { | |
650 | struct efi_graphics_output_mode_info *info = NULL; | |
651 | efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID; | |
652 | bool conout_found = false; | |
653 | void *dummy = NULL; | |
654 | u32 h = handles[i]; | |
655 | ||
204b0a1a MF |
656 | status = efi_call_early(handle_protocol, h, |
657 | proto, (void **)&gop32); | |
c116e8d6 MF |
658 | if (status != EFI_SUCCESS) |
659 | continue; | |
660 | ||
204b0a1a MF |
661 | status = efi_call_early(handle_protocol, h, |
662 | &conout_proto, &dummy); | |
c116e8d6 MF |
663 | if (status == EFI_SUCCESS) |
664 | conout_found = true; | |
665 | ||
666 | status = __gop_query32(gop32, &info, &size, &fb_base); | |
667 | if (status == EFI_SUCCESS && (!first_gop || conout_found)) { | |
668 | /* | |
669 | * Systems that use the UEFI Console Splitter may | |
670 | * provide multiple GOP devices, not all of which are | |
671 | * backed by real hardware. The workaround is to search | |
672 | * for a GOP implementing the ConOut protocol, and if | |
673 | * one isn't found, to just fall back to the first GOP. | |
674 | */ | |
675 | width = info->horizontal_resolution; | |
676 | height = info->vertical_resolution; | |
677 | pixel_format = info->pixel_format; | |
678 | pixel_info = info->pixel_information; | |
679 | pixels_per_scan_line = info->pixels_per_scan_line; | |
680 | ||
681 | /* | |
682 | * Once we've found a GOP supporting ConOut, | |
683 | * don't bother looking any further. | |
684 | */ | |
685 | first_gop = gop32; | |
686 | if (conout_found) | |
687 | break; | |
688 | } | |
689 | } | |
690 | ||
691 | /* Did we find any GOPs? */ | |
692 | if (!first_gop) | |
693 | goto out; | |
694 | ||
695 | /* EFI framebuffer */ | |
696 | si->orig_video_isVGA = VIDEO_TYPE_EFI; | |
697 | ||
698 | si->lfb_width = width; | |
699 | si->lfb_height = height; | |
700 | si->lfb_base = fb_base; | |
701 | si->pages = 1; | |
702 | ||
703 | setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format); | |
291f3632 | 704 | |
e9b10953 MG |
705 | si->lfb_size = si->lfb_linelength * si->lfb_height; |
706 | ||
f462ed93 | 707 | si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS; |
c116e8d6 MF |
708 | out: |
709 | return status; | |
710 | } | |
f462ed93 | 711 | |
c116e8d6 MF |
712 | static efi_status_t |
713 | __gop_query64(struct efi_graphics_output_protocol_64 *gop64, | |
714 | struct efi_graphics_output_mode_info **info, | |
715 | unsigned long *size, u32 *fb_base) | |
716 | { | |
717 | struct efi_graphics_output_protocol_mode_64 *mode; | |
718 | efi_status_t status; | |
719 | unsigned long m; | |
720 | ||
721 | m = gop64->mode; | |
722 | mode = (struct efi_graphics_output_protocol_mode_64 *)m; | |
723 | ||
724 | status = efi_early->call(gop64->query_mode, gop64, | |
725 | mode->mode, size, info); | |
726 | if (status != EFI_SUCCESS) | |
727 | return status; | |
728 | ||
729 | *fb_base = mode->frame_buffer_base; | |
730 | return status; | |
731 | } | |
732 | ||
733 | static efi_status_t | |
734 | setup_gop64(struct screen_info *si, efi_guid_t *proto, | |
735 | unsigned long size, void **gop_handle) | |
736 | { | |
737 | struct efi_graphics_output_protocol_64 *gop64, *first_gop; | |
738 | unsigned long nr_gops; | |
739 | u16 width, height; | |
740 | u32 pixels_per_scan_line; | |
741 | u32 fb_base; | |
742 | struct efi_pixel_bitmask pixel_info; | |
743 | int pixel_format; | |
744 | efi_status_t status; | |
745 | u64 *handles = (u64 *)(unsigned long)gop_handle; | |
746 | int i; | |
747 | ||
748 | first_gop = NULL; | |
749 | gop64 = NULL; | |
750 | ||
751 | nr_gops = size / sizeof(u64); | |
752 | for (i = 0; i < nr_gops; i++) { | |
753 | struct efi_graphics_output_mode_info *info = NULL; | |
754 | efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID; | |
755 | bool conout_found = false; | |
756 | void *dummy = NULL; | |
757 | u64 h = handles[i]; | |
758 | ||
204b0a1a MF |
759 | status = efi_call_early(handle_protocol, h, |
760 | proto, (void **)&gop64); | |
c116e8d6 MF |
761 | if (status != EFI_SUCCESS) |
762 | continue; | |
763 | ||
204b0a1a MF |
764 | status = efi_call_early(handle_protocol, h, |
765 | &conout_proto, &dummy); | |
c116e8d6 MF |
766 | if (status == EFI_SUCCESS) |
767 | conout_found = true; | |
768 | ||
769 | status = __gop_query64(gop64, &info, &size, &fb_base); | |
770 | if (status == EFI_SUCCESS && (!first_gop || conout_found)) { | |
771 | /* | |
772 | * Systems that use the UEFI Console Splitter may | |
773 | * provide multiple GOP devices, not all of which are | |
774 | * backed by real hardware. The workaround is to search | |
775 | * for a GOP implementing the ConOut protocol, and if | |
776 | * one isn't found, to just fall back to the first GOP. | |
777 | */ | |
778 | width = info->horizontal_resolution; | |
779 | height = info->vertical_resolution; | |
780 | pixel_format = info->pixel_format; | |
781 | pixel_info = info->pixel_information; | |
782 | pixels_per_scan_line = info->pixels_per_scan_line; | |
783 | ||
784 | /* | |
785 | * Once we've found a GOP supporting ConOut, | |
786 | * don't bother looking any further. | |
787 | */ | |
788 | first_gop = gop64; | |
789 | if (conout_found) | |
790 | break; | |
791 | } | |
792 | } | |
793 | ||
794 | /* Did we find any GOPs? */ | |
795 | if (!first_gop) | |
796 | goto out; | |
797 | ||
798 | /* EFI framebuffer */ | |
799 | si->orig_video_isVGA = VIDEO_TYPE_EFI; | |
800 | ||
801 | si->lfb_width = width; | |
802 | si->lfb_height = height; | |
803 | si->lfb_base = fb_base; | |
804 | si->pages = 1; | |
805 | ||
806 | setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format); | |
807 | ||
808 | si->lfb_size = si->lfb_linelength * si->lfb_height; | |
809 | ||
810 | si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS; | |
811 | out: | |
291f3632 MF |
812 | return status; |
813 | } | |
814 | ||
815 | /* | |
c116e8d6 | 816 | * See if we have Graphics Output Protocol |
291f3632 | 817 | */ |
c116e8d6 | 818 | static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto, |
291f3632 MF |
819 | unsigned long size) |
820 | { | |
291f3632 | 821 | efi_status_t status; |
c116e8d6 | 822 | void **gop_handle = NULL; |
291f3632 | 823 | |
204b0a1a MF |
824 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
825 | size, (void **)&gop_handle); | |
291f3632 MF |
826 | if (status != EFI_SUCCESS) |
827 | return status; | |
828 | ||
204b0a1a MF |
829 | status = efi_call_early(locate_handle, |
830 | EFI_LOCATE_BY_PROTOCOL, | |
831 | proto, NULL, &size, gop_handle); | |
291f3632 MF |
832 | if (status != EFI_SUCCESS) |
833 | goto free_handle; | |
834 | ||
c116e8d6 MF |
835 | if (efi_early->is64) |
836 | status = setup_gop64(si, proto, size, gop_handle); | |
837 | else | |
838 | status = setup_gop32(si, proto, size, gop_handle); | |
839 | ||
840 | free_handle: | |
204b0a1a | 841 | efi_call_early(free_pool, gop_handle); |
c116e8d6 MF |
842 | return status; |
843 | } | |
844 | ||
845 | static efi_status_t | |
846 | setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height) | |
847 | { | |
848 | struct efi_uga_draw_protocol *uga = NULL, *first_uga; | |
849 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; | |
850 | unsigned long nr_ugas; | |
851 | u32 *handles = (u32 *)uga_handle;; | |
852 | efi_status_t status; | |
853 | int i; | |
854 | ||
291f3632 | 855 | first_uga = NULL; |
c116e8d6 MF |
856 | nr_ugas = size / sizeof(u32); |
857 | for (i = 0; i < nr_ugas; i++) { | |
858 | efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; | |
859 | u32 w, h, depth, refresh; | |
860 | void *pciio; | |
861 | u32 handle = handles[i]; | |
862 | ||
204b0a1a MF |
863 | status = efi_call_early(handle_protocol, handle, |
864 | &uga_proto, (void **)&uga); | |
c116e8d6 MF |
865 | if (status != EFI_SUCCESS) |
866 | continue; | |
867 | ||
204b0a1a | 868 | efi_call_early(handle_protocol, handle, &pciio_proto, &pciio); |
c116e8d6 MF |
869 | |
870 | status = efi_early->call((unsigned long)uga->get_mode, uga, | |
871 | &w, &h, &depth, &refresh); | |
872 | if (status == EFI_SUCCESS && (!first_uga || pciio)) { | |
873 | *width = w; | |
874 | *height = h; | |
875 | ||
876 | /* | |
877 | * Once we've found a UGA supporting PCIIO, | |
878 | * don't bother looking any further. | |
879 | */ | |
880 | if (pciio) | |
881 | break; | |
882 | ||
883 | first_uga = uga; | |
884 | } | |
885 | } | |
886 | ||
887 | return status; | |
888 | } | |
291f3632 | 889 | |
c116e8d6 MF |
890 | static efi_status_t |
891 | setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height) | |
892 | { | |
893 | struct efi_uga_draw_protocol *uga = NULL, *first_uga; | |
894 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; | |
895 | unsigned long nr_ugas; | |
896 | u64 *handles = (u64 *)uga_handle;; | |
897 | efi_status_t status; | |
898 | int i; | |
899 | ||
900 | first_uga = NULL; | |
901 | nr_ugas = size / sizeof(u64); | |
291f3632 MF |
902 | for (i = 0; i < nr_ugas; i++) { |
903 | efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; | |
291f3632 MF |
904 | u32 w, h, depth, refresh; |
905 | void *pciio; | |
c116e8d6 | 906 | u64 handle = handles[i]; |
291f3632 | 907 | |
204b0a1a MF |
908 | status = efi_call_early(handle_protocol, handle, |
909 | &uga_proto, (void **)&uga); | |
291f3632 MF |
910 | if (status != EFI_SUCCESS) |
911 | continue; | |
912 | ||
204b0a1a | 913 | efi_call_early(handle_protocol, handle, &pciio_proto, &pciio); |
291f3632 | 914 | |
54b52d87 MF |
915 | status = efi_early->call((unsigned long)uga->get_mode, uga, |
916 | &w, &h, &depth, &refresh); | |
291f3632 | 917 | if (status == EFI_SUCCESS && (!first_uga || pciio)) { |
c116e8d6 MF |
918 | *width = w; |
919 | *height = h; | |
291f3632 MF |
920 | |
921 | /* | |
922 | * Once we've found a UGA supporting PCIIO, | |
923 | * don't bother looking any further. | |
924 | */ | |
925 | if (pciio) | |
926 | break; | |
927 | ||
928 | first_uga = uga; | |
929 | } | |
930 | } | |
931 | ||
c116e8d6 MF |
932 | return status; |
933 | } | |
934 | ||
935 | /* | |
936 | * See if we have Universal Graphics Adapter (UGA) protocol | |
937 | */ | |
938 | static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto, | |
939 | unsigned long size) | |
940 | { | |
941 | efi_status_t status; | |
942 | u32 width, height; | |
943 | void **uga_handle = NULL; | |
944 | ||
204b0a1a MF |
945 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
946 | size, (void **)&uga_handle); | |
c116e8d6 MF |
947 | if (status != EFI_SUCCESS) |
948 | return status; | |
949 | ||
204b0a1a MF |
950 | status = efi_call_early(locate_handle, |
951 | EFI_LOCATE_BY_PROTOCOL, | |
952 | uga_proto, NULL, &size, uga_handle); | |
c116e8d6 MF |
953 | if (status != EFI_SUCCESS) |
954 | goto free_handle; | |
955 | ||
956 | height = 0; | |
957 | width = 0; | |
958 | ||
959 | if (efi_early->is64) | |
960 | status = setup_uga64(uga_handle, size, &width, &height); | |
961 | else | |
962 | status = setup_uga32(uga_handle, size, &width, &height); | |
963 | ||
964 | if (!width && !height) | |
291f3632 MF |
965 | goto free_handle; |
966 | ||
967 | /* EFI framebuffer */ | |
968 | si->orig_video_isVGA = VIDEO_TYPE_EFI; | |
969 | ||
970 | si->lfb_depth = 32; | |
971 | si->lfb_width = width; | |
972 | si->lfb_height = height; | |
973 | ||
974 | si->red_size = 8; | |
975 | si->red_pos = 16; | |
976 | si->green_size = 8; | |
977 | si->green_pos = 8; | |
978 | si->blue_size = 8; | |
979 | si->blue_pos = 0; | |
980 | si->rsvd_size = 8; | |
981 | si->rsvd_pos = 24; | |
982 | ||
291f3632 | 983 | free_handle: |
204b0a1a | 984 | efi_call_early(free_pool, uga_handle); |
291f3632 MF |
985 | return status; |
986 | } | |
987 | ||
988 | void setup_graphics(struct boot_params *boot_params) | |
989 | { | |
990 | efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; | |
991 | struct screen_info *si; | |
992 | efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; | |
993 | efi_status_t status; | |
994 | unsigned long size; | |
995 | void **gop_handle = NULL; | |
996 | void **uga_handle = NULL; | |
997 | ||
998 | si = &boot_params->screen_info; | |
999 | memset(si, 0, sizeof(*si)); | |
1000 | ||
1001 | size = 0; | |
204b0a1a MF |
1002 | status = efi_call_early(locate_handle, |
1003 | EFI_LOCATE_BY_PROTOCOL, | |
1004 | &graphics_proto, NULL, &size, gop_handle); | |
291f3632 MF |
1005 | if (status == EFI_BUFFER_TOO_SMALL) |
1006 | status = setup_gop(si, &graphics_proto, size); | |
1007 | ||
1008 | if (status != EFI_SUCCESS) { | |
1009 | size = 0; | |
204b0a1a MF |
1010 | status = efi_call_early(locate_handle, |
1011 | EFI_LOCATE_BY_PROTOCOL, | |
1012 | &uga_proto, NULL, &size, uga_handle); | |
291f3632 MF |
1013 | if (status == EFI_BUFFER_TOO_SMALL) |
1014 | setup_uga(si, &uga_proto, size); | |
1015 | } | |
1016 | } | |
1017 | ||
291f3632 MF |
1018 | /* |
1019 | * Because the x86 boot code expects to be passed a boot_params we | |
1020 | * need to create one ourselves (usually the bootloader would create | |
1021 | * one for us). | |
7e8213c1 MF |
1022 | * |
1023 | * The caller is responsible for filling out ->code32_start in the | |
1024 | * returned boot_params. | |
291f3632 | 1025 | */ |
54b52d87 | 1026 | struct boot_params *make_boot_params(struct efi_config *c) |
291f3632 | 1027 | { |
9ca8f72a MF |
1028 | struct boot_params *boot_params; |
1029 | struct sys_desc_table *sdt; | |
1030 | struct apm_bios_info *bi; | |
1031 | struct setup_header *hdr; | |
1032 | struct efi_info *efi; | |
1033 | efi_loaded_image_t *image; | |
54b52d87 | 1034 | void *options, *handle; |
9ca8f72a | 1035 | efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; |
291f3632 MF |
1036 | int options_size = 0; |
1037 | efi_status_t status; | |
5fef3870 | 1038 | char *cmdline_ptr; |
291f3632 MF |
1039 | u16 *s2; |
1040 | u8 *s1; | |
1041 | int i; | |
46f4582e RF |
1042 | unsigned long ramdisk_addr; |
1043 | unsigned long ramdisk_size; | |
291f3632 | 1044 | |
54b52d87 MF |
1045 | efi_early = c; |
1046 | sys_table = (efi_system_table_t *)(unsigned long)efi_early->table; | |
1047 | handle = (void *)(unsigned long)efi_early->image_handle; | |
9ca8f72a MF |
1048 | |
1049 | /* Check if we were booted by the EFI firmware */ | |
1050 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) | |
1051 | return NULL; | |
1052 | ||
54b52d87 MF |
1053 | if (efi_early->is64) |
1054 | setup_boot_services64(efi_early); | |
1055 | else | |
1056 | setup_boot_services32(efi_early); | |
1057 | ||
204b0a1a MF |
1058 | status = efi_call_early(handle_protocol, handle, |
1059 | &proto, (void *)&image); | |
9ca8f72a | 1060 | if (status != EFI_SUCCESS) { |
876dc36a | 1061 | efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n"); |
9ca8f72a MF |
1062 | return NULL; |
1063 | } | |
1064 | ||
40e4530a RF |
1065 | status = efi_low_alloc(sys_table, 0x4000, 1, |
1066 | (unsigned long *)&boot_params); | |
9ca8f72a | 1067 | if (status != EFI_SUCCESS) { |
876dc36a | 1068 | efi_printk(sys_table, "Failed to alloc lowmem for boot params\n"); |
9ca8f72a MF |
1069 | return NULL; |
1070 | } | |
1071 | ||
1072 | memset(boot_params, 0x0, 0x4000); | |
1073 | ||
1074 | hdr = &boot_params->hdr; | |
1075 | efi = &boot_params->efi_info; | |
1076 | bi = &boot_params->apm_bios_info; | |
1077 | sdt = &boot_params->sys_desc_table; | |
1078 | ||
1079 | /* Copy the second sector to boot_params */ | |
1080 | memcpy(&hdr->jump, image->image_base + 512, 512); | |
1081 | ||
1082 | /* | |
1083 | * Fill out some of the header fields ourselves because the | |
1084 | * EFI firmware loader doesn't load the first sector. | |
1085 | */ | |
1086 | hdr->root_flags = 1; | |
1087 | hdr->vid_mode = 0xffff; | |
1088 | hdr->boot_flag = 0xAA55; | |
1089 | ||
291f3632 MF |
1090 | hdr->type_of_loader = 0x21; |
1091 | ||
1092 | /* Convert unicode cmdline to ascii */ | |
c625d1c2 | 1093 | cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size); |
5fef3870 RF |
1094 | if (!cmdline_ptr) |
1095 | goto fail; | |
1096 | hdr->cmd_line_ptr = (unsigned long)cmdline_ptr; | |
291f3632 MF |
1097 | |
1098 | hdr->ramdisk_image = 0; | |
1099 | hdr->ramdisk_size = 0; | |
1100 | ||
291f3632 MF |
1101 | /* Clear APM BIOS info */ |
1102 | memset(bi, 0, sizeof(*bi)); | |
1103 | ||
1104 | memset(sdt, 0, sizeof(*sdt)); | |
1105 | ||
46f4582e RF |
1106 | status = handle_cmdline_files(sys_table, image, |
1107 | (char *)(unsigned long)hdr->cmd_line_ptr, | |
47226ad4 | 1108 | "initrd=", hdr->initrd_addr_max, |
46f4582e | 1109 | &ramdisk_addr, &ramdisk_size); |
47226ad4 YL |
1110 | |
1111 | if (status != EFI_SUCCESS && | |
1112 | hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) { | |
1113 | efi_printk(sys_table, "Trying to load files to higher address\n"); | |
1114 | status = handle_cmdline_files(sys_table, image, | |
1115 | (char *)(unsigned long)hdr->cmd_line_ptr, | |
1116 | "initrd=", -1UL, | |
1117 | &ramdisk_addr, &ramdisk_size); | |
1118 | } | |
1119 | ||
9ca8f72a MF |
1120 | if (status != EFI_SUCCESS) |
1121 | goto fail2; | |
4bf7111f YL |
1122 | hdr->ramdisk_image = ramdisk_addr & 0xffffffff; |
1123 | hdr->ramdisk_size = ramdisk_size & 0xffffffff; | |
1124 | boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32; | |
1125 | boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32; | |
9ca8f72a MF |
1126 | |
1127 | return boot_params; | |
1128 | fail2: | |
0e1cadb0 | 1129 | efi_free(sys_table, options_size, hdr->cmd_line_ptr); |
9ca8f72a | 1130 | fail: |
40e4530a | 1131 | efi_free(sys_table, 0x4000, (unsigned long)boot_params); |
9ca8f72a MF |
1132 | return NULL; |
1133 | } | |
1134 | ||
d2078d5a LC |
1135 | static void add_e820ext(struct boot_params *params, |
1136 | struct setup_data *e820ext, u32 nr_entries) | |
9ca8f72a | 1137 | { |
d2078d5a | 1138 | struct setup_data *data; |
9ca8f72a | 1139 | efi_status_t status; |
d2078d5a | 1140 | unsigned long size; |
291f3632 | 1141 | |
d2078d5a LC |
1142 | e820ext->type = SETUP_E820_EXT; |
1143 | e820ext->len = nr_entries * sizeof(struct e820entry); | |
1144 | e820ext->next = 0; | |
291f3632 | 1145 | |
d2078d5a | 1146 | data = (struct setup_data *)(unsigned long)params->hdr.setup_data; |
291f3632 | 1147 | |
d2078d5a LC |
1148 | while (data && data->next) |
1149 | data = (struct setup_data *)(unsigned long)data->next; | |
d3768d88 | 1150 | |
d2078d5a LC |
1151 | if (data) |
1152 | data->next = (unsigned long)e820ext; | |
1153 | else | |
1154 | params->hdr.setup_data = (unsigned long)e820ext; | |
1155 | } | |
291f3632 | 1156 | |
d2078d5a LC |
1157 | static efi_status_t setup_e820(struct boot_params *params, |
1158 | struct setup_data *e820ext, u32 e820ext_size) | |
1159 | { | |
1160 | struct e820entry *e820_map = ¶ms->e820_map[0]; | |
1161 | struct efi_info *efi = ¶ms->efi_info; | |
1162 | struct e820entry *prev = NULL; | |
1163 | u32 nr_entries; | |
1164 | u32 nr_desc; | |
1165 | int i; | |
291f3632 | 1166 | |
291f3632 | 1167 | nr_entries = 0; |
d2078d5a LC |
1168 | nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size; |
1169 | ||
1170 | for (i = 0; i < nr_desc; i++) { | |
291f3632 MF |
1171 | efi_memory_desc_t *d; |
1172 | unsigned int e820_type = 0; | |
d2078d5a | 1173 | unsigned long m = efi->efi_memmap; |
291f3632 | 1174 | |
d2078d5a | 1175 | d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size)); |
291f3632 MF |
1176 | switch (d->type) { |
1177 | case EFI_RESERVED_TYPE: | |
1178 | case EFI_RUNTIME_SERVICES_CODE: | |
1179 | case EFI_RUNTIME_SERVICES_DATA: | |
1180 | case EFI_MEMORY_MAPPED_IO: | |
1181 | case EFI_MEMORY_MAPPED_IO_PORT_SPACE: | |
1182 | case EFI_PAL_CODE: | |
1183 | e820_type = E820_RESERVED; | |
1184 | break; | |
1185 | ||
1186 | case EFI_UNUSABLE_MEMORY: | |
1187 | e820_type = E820_UNUSABLE; | |
1188 | break; | |
1189 | ||
1190 | case EFI_ACPI_RECLAIM_MEMORY: | |
1191 | e820_type = E820_ACPI; | |
1192 | break; | |
1193 | ||
1194 | case EFI_LOADER_CODE: | |
1195 | case EFI_LOADER_DATA: | |
1196 | case EFI_BOOT_SERVICES_CODE: | |
1197 | case EFI_BOOT_SERVICES_DATA: | |
1198 | case EFI_CONVENTIONAL_MEMORY: | |
1199 | e820_type = E820_RAM; | |
1200 | break; | |
1201 | ||
1202 | case EFI_ACPI_MEMORY_NVS: | |
1203 | e820_type = E820_NVS; | |
1204 | break; | |
1205 | ||
1206 | default: | |
1207 | continue; | |
1208 | } | |
1209 | ||
1210 | /* Merge adjacent mappings */ | |
1211 | if (prev && prev->type == e820_type && | |
d2078d5a | 1212 | (prev->addr + prev->size) == d->phys_addr) { |
291f3632 | 1213 | prev->size += d->num_pages << 12; |
d2078d5a | 1214 | continue; |
291f3632 | 1215 | } |
d2078d5a LC |
1216 | |
1217 | if (nr_entries == ARRAY_SIZE(params->e820_map)) { | |
1218 | u32 need = (nr_desc - i) * sizeof(struct e820entry) + | |
1219 | sizeof(struct setup_data); | |
1220 | ||
1221 | if (!e820ext || e820ext_size < need) | |
1222 | return EFI_BUFFER_TOO_SMALL; | |
1223 | ||
1224 | /* boot_params map full, switch to e820 extended */ | |
1225 | e820_map = (struct e820entry *)e820ext->data; | |
1226 | } | |
1227 | ||
1228 | e820_map->addr = d->phys_addr; | |
1229 | e820_map->size = d->num_pages << PAGE_SHIFT; | |
1230 | e820_map->type = e820_type; | |
1231 | prev = e820_map++; | |
1232 | nr_entries++; | |
291f3632 MF |
1233 | } |
1234 | ||
d2078d5a LC |
1235 | if (nr_entries > ARRAY_SIZE(params->e820_map)) { |
1236 | u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map); | |
1237 | ||
1238 | add_e820ext(params, e820ext, nr_e820ext); | |
1239 | nr_entries -= nr_e820ext; | |
1240 | } | |
1241 | ||
1242 | params->e820_entries = (u8)nr_entries; | |
1243 | ||
1244 | return EFI_SUCCESS; | |
1245 | } | |
1246 | ||
1247 | static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, | |
1248 | u32 *e820ext_size) | |
1249 | { | |
1250 | efi_status_t status; | |
1251 | unsigned long size; | |
1252 | ||
1253 | size = sizeof(struct setup_data) + | |
1254 | sizeof(struct e820entry) * nr_desc; | |
1255 | ||
1256 | if (*e820ext) { | |
204b0a1a | 1257 | efi_call_early(free_pool, *e820ext); |
d2078d5a LC |
1258 | *e820ext = NULL; |
1259 | *e820ext_size = 0; | |
1260 | } | |
1261 | ||
204b0a1a MF |
1262 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
1263 | size, (void **)e820ext); | |
d2078d5a LC |
1264 | if (status == EFI_SUCCESS) |
1265 | *e820ext_size = size; | |
1266 | ||
1267 | return status; | |
1268 | } | |
1269 | ||
1270 | static efi_status_t exit_boot(struct boot_params *boot_params, | |
b8ff87a6 | 1271 | void *handle, bool is64) |
d2078d5a LC |
1272 | { |
1273 | struct efi_info *efi = &boot_params->efi_info; | |
1274 | unsigned long map_sz, key, desc_size; | |
1275 | efi_memory_desc_t *mem_map; | |
1276 | struct setup_data *e820ext; | |
b8ff87a6 | 1277 | const char *signature; |
d2078d5a LC |
1278 | __u32 e820ext_size; |
1279 | __u32 nr_desc, prev_nr_desc; | |
1280 | efi_status_t status; | |
1281 | __u32 desc_version; | |
1282 | bool called_exit = false; | |
1283 | u8 nr_entries; | |
1284 | int i; | |
1285 | ||
1286 | nr_desc = 0; | |
1287 | e820ext = NULL; | |
1288 | e820ext_size = 0; | |
1289 | ||
1290 | get_map: | |
1291 | status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size, | |
1292 | &desc_version, &key); | |
1293 | ||
1294 | if (status != EFI_SUCCESS) | |
1295 | return status; | |
1296 | ||
1297 | prev_nr_desc = nr_desc; | |
1298 | nr_desc = map_sz / desc_size; | |
1299 | if (nr_desc > prev_nr_desc && | |
1300 | nr_desc > ARRAY_SIZE(boot_params->e820_map)) { | |
1301 | u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map); | |
1302 | ||
1303 | status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size); | |
1304 | if (status != EFI_SUCCESS) | |
1305 | goto free_mem_map; | |
1306 | ||
204b0a1a | 1307 | efi_call_early(free_pool, mem_map); |
d2078d5a LC |
1308 | goto get_map; /* Allocated memory, get map again */ |
1309 | } | |
1310 | ||
b8ff87a6 MF |
1311 | signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE; |
1312 | memcpy(&efi->efi_loader_signature, signature, sizeof(__u32)); | |
1313 | ||
d2078d5a LC |
1314 | efi->efi_systab = (unsigned long)sys_table; |
1315 | efi->efi_memdesc_size = desc_size; | |
1316 | efi->efi_memdesc_version = desc_version; | |
1317 | efi->efi_memmap = (unsigned long)mem_map; | |
1318 | efi->efi_memmap_size = map_sz; | |
1319 | ||
1320 | #ifdef CONFIG_X86_64 | |
1321 | efi->efi_systab_hi = (unsigned long)sys_table >> 32; | |
1322 | efi->efi_memmap_hi = (unsigned long)mem_map >> 32; | |
1323 | #endif | |
1324 | ||
1325 | /* Might as well exit boot services now */ | |
204b0a1a | 1326 | status = efi_call_early(exit_boot_services, handle, key); |
d2078d5a LC |
1327 | if (status != EFI_SUCCESS) { |
1328 | /* | |
1329 | * ExitBootServices() will fail if any of the event | |
1330 | * handlers change the memory map. In which case, we | |
1331 | * must be prepared to retry, but only once so that | |
1332 | * we're guaranteed to exit on repeated failures instead | |
1333 | * of spinning forever. | |
1334 | */ | |
1335 | if (called_exit) | |
1336 | goto free_mem_map; | |
1337 | ||
1338 | called_exit = true; | |
204b0a1a | 1339 | efi_call_early(free_pool, mem_map); |
d2078d5a LC |
1340 | goto get_map; |
1341 | } | |
1342 | ||
1343 | /* Historic? */ | |
1344 | boot_params->alt_mem_k = 32 * 1024; | |
1345 | ||
1346 | status = setup_e820(boot_params, e820ext, e820ext_size); | |
1347 | if (status != EFI_SUCCESS) | |
1348 | return status; | |
291f3632 MF |
1349 | |
1350 | return EFI_SUCCESS; | |
1351 | ||
1352 | free_mem_map: | |
204b0a1a | 1353 | efi_call_early(free_pool, mem_map); |
291f3632 MF |
1354 | return status; |
1355 | } | |
1356 | ||
9ca8f72a MF |
1357 | /* |
1358 | * On success we return a pointer to a boot_params structure, and NULL | |
1359 | * on failure. | |
1360 | */ | |
54b52d87 | 1361 | struct boot_params *efi_main(struct efi_config *c, |
9ca8f72a MF |
1362 | struct boot_params *boot_params) |
1363 | { | |
54b52d87 | 1364 | struct desc_ptr *gdt = NULL; |
9ca8f72a MF |
1365 | efi_loaded_image_t *image; |
1366 | struct setup_header *hdr = &boot_params->hdr; | |
1367 | efi_status_t status; | |
1368 | struct desc_struct *desc; | |
54b52d87 MF |
1369 | void *handle; |
1370 | efi_system_table_t *_table; | |
1371 | bool is64; | |
1372 | ||
1373 | efi_early = c; | |
1374 | ||
1375 | _table = (efi_system_table_t *)(unsigned long)efi_early->table; | |
1376 | handle = (void *)(unsigned long)efi_early->image_handle; | |
1377 | is64 = efi_early->is64; | |
9ca8f72a MF |
1378 | |
1379 | sys_table = _table; | |
1380 | ||
1381 | /* Check if we were booted by the EFI firmware */ | |
1382 | if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) | |
1383 | goto fail; | |
1384 | ||
54b52d87 MF |
1385 | if (is64) |
1386 | setup_boot_services64(efi_early); | |
1387 | else | |
1388 | setup_boot_services32(efi_early); | |
1389 | ||
9ca8f72a | 1390 | setup_graphics(boot_params); |
291f3632 | 1391 | |
56394ab8 | 1392 | setup_efi_pci(boot_params); |
dd5fc854 | 1393 | |
204b0a1a MF |
1394 | status = efi_call_early(allocate_pool, EFI_LOADER_DATA, |
1395 | sizeof(*gdt), (void **)&gdt); | |
9fa7deda | 1396 | if (status != EFI_SUCCESS) { |
876dc36a | 1397 | efi_printk(sys_table, "Failed to alloc mem for gdt structure\n"); |
291f3632 | 1398 | goto fail; |
9fa7deda | 1399 | } |
291f3632 MF |
1400 | |
1401 | gdt->size = 0x800; | |
40e4530a | 1402 | status = efi_low_alloc(sys_table, gdt->size, 8, |
876dc36a | 1403 | (unsigned long *)&gdt->address); |
9fa7deda | 1404 | if (status != EFI_SUCCESS) { |
876dc36a | 1405 | efi_printk(sys_table, "Failed to alloc mem for gdt\n"); |
291f3632 | 1406 | goto fail; |
9fa7deda | 1407 | } |
291f3632 | 1408 | |
9ca8f72a MF |
1409 | /* |
1410 | * If the kernel isn't already loaded at the preferred load | |
1411 | * address, relocate it. | |
1412 | */ | |
1413 | if (hdr->pref_address != hdr->code32_start) { | |
4a9f3a7c RF |
1414 | unsigned long bzimage_addr = hdr->code32_start; |
1415 | status = efi_relocate_kernel(sys_table, &bzimage_addr, | |
1416 | hdr->init_size, hdr->init_size, | |
1417 | hdr->pref_address, | |
1418 | hdr->kernel_alignment); | |
fb86b244 UW |
1419 | if (status != EFI_SUCCESS) { |
1420 | efi_printk(sys_table, "efi_relocate_kernel() failed!\n"); | |
9ca8f72a | 1421 | goto fail; |
fb86b244 | 1422 | } |
4a9f3a7c RF |
1423 | |
1424 | hdr->pref_address = hdr->code32_start; | |
1425 | hdr->code32_start = bzimage_addr; | |
9ca8f72a MF |
1426 | } |
1427 | ||
b8ff87a6 | 1428 | status = exit_boot(boot_params, handle, is64); |
fb86b244 UW |
1429 | if (status != EFI_SUCCESS) { |
1430 | efi_printk(sys_table, "exit_boot() failed!\n"); | |
291f3632 | 1431 | goto fail; |
fb86b244 | 1432 | } |
291f3632 MF |
1433 | |
1434 | memset((char *)gdt->address, 0x0, gdt->size); | |
1435 | desc = (struct desc_struct *)gdt->address; | |
1436 | ||
1437 | /* The first GDT is a dummy and the second is unused. */ | |
1438 | desc += 2; | |
1439 | ||
1440 | desc->limit0 = 0xffff; | |
1441 | desc->base0 = 0x0000; | |
1442 | desc->base1 = 0x0000; | |
1443 | desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ; | |
1444 | desc->s = DESC_TYPE_CODE_DATA; | |
1445 | desc->dpl = 0; | |
1446 | desc->p = 1; | |
1447 | desc->limit = 0xf; | |
1448 | desc->avl = 0; | |
1449 | desc->l = 0; | |
1450 | desc->d = SEG_OP_SIZE_32BIT; | |
1451 | desc->g = SEG_GRANULARITY_4KB; | |
1452 | desc->base2 = 0x00; | |
1453 | ||
1454 | desc++; | |
1455 | desc->limit0 = 0xffff; | |
1456 | desc->base0 = 0x0000; | |
1457 | desc->base1 = 0x0000; | |
1458 | desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE; | |
1459 | desc->s = DESC_TYPE_CODE_DATA; | |
1460 | desc->dpl = 0; | |
1461 | desc->p = 1; | |
1462 | desc->limit = 0xf; | |
1463 | desc->avl = 0; | |
1464 | desc->l = 0; | |
1465 | desc->d = SEG_OP_SIZE_32BIT; | |
1466 | desc->g = SEG_GRANULARITY_4KB; | |
1467 | desc->base2 = 0x00; | |
1468 | ||
1469 | #ifdef CONFIG_X86_64 | |
1470 | /* Task segment value */ | |
1471 | desc++; | |
1472 | desc->limit0 = 0x0000; | |
1473 | desc->base0 = 0x0000; | |
1474 | desc->base1 = 0x0000; | |
1475 | desc->type = SEG_TYPE_TSS; | |
1476 | desc->s = 0; | |
1477 | desc->dpl = 0; | |
1478 | desc->p = 1; | |
1479 | desc->limit = 0x0; | |
1480 | desc->avl = 0; | |
1481 | desc->l = 0; | |
1482 | desc->d = 0; | |
1483 | desc->g = SEG_GRANULARITY_4KB; | |
1484 | desc->base2 = 0x00; | |
1485 | #endif /* CONFIG_X86_64 */ | |
1486 | ||
291f3632 | 1487 | asm volatile("cli"); |
0ce6cda2 | 1488 | asm volatile ("lgdt %0" : : "m" (*gdt)); |
291f3632 MF |
1489 | |
1490 | return boot_params; | |
1491 | fail: | |
fb86b244 | 1492 | efi_printk(sys_table, "efi_main() failed!\n"); |
291f3632 MF |
1493 | return NULL; |
1494 | } |