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