~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/x86/boot/compressed/eboot.c

Version: ~ [ linux-4.14 ] ~ [ linux-4.13.12 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.61 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.97 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.46 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.80 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.50 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.95 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.27.62 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* -----------------------------------------------------------------------
  2  *
  3  *   Copyright 2011 Intel Corporation; author Matt Fleming
  4  *
  5  *   This file is part of the Linux kernel, and is made available under
  6  *   the terms of the GNU General Public License version 2.
  7  *
  8  * ----------------------------------------------------------------------- */
  9 
 10 #include <linux/efi.h>
 11 #include <linux/pci.h>
 12 
 13 #include <asm/efi.h>
 14 #include <asm/e820/types.h>
 15 #include <asm/setup.h>
 16 #include <asm/desc.h>
 17 
 18 #include "../string.h"
 19 #include "eboot.h"
 20 
 21 static efi_system_table_t *sys_table;
 22 
 23 static struct efi_config *efi_early;
 24 
 25 __pure const struct efi_config *__efi_early(void)
 26 {
 27         return efi_early;
 28 }
 29 
 30 #define BOOT_SERVICES(bits)                                             \
 31 static void setup_boot_services##bits(struct efi_config *c)             \
 32 {                                                                       \
 33         efi_system_table_##bits##_t *table;                             \
 34                                                                         \
 35         table = (typeof(table))sys_table;                               \
 36                                                                         \
 37         c->runtime_services = table->runtime;                           \
 38         c->boot_services = table->boottime;                             \
 39         c->text_output = table->con_out;                                \
 40 }
 41 BOOT_SERVICES(32);
 42 BOOT_SERVICES(64);
 43 
 44 static inline efi_status_t __open_volume32(void *__image, void **__fh)
 45 {
 46         efi_file_io_interface_t *io;
 47         efi_loaded_image_32_t *image = __image;
 48         efi_file_handle_32_t *fh;
 49         efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
 50         efi_status_t status;
 51         void *handle = (void *)(unsigned long)image->device_handle;
 52         unsigned long func;
 53 
 54         status = efi_call_early(handle_protocol, handle,
 55                                 &fs_proto, (void **)&io);
 56         if (status != EFI_SUCCESS) {
 57                 efi_printk(sys_table, "Failed to handle fs_proto\n");
 58                 return status;
 59         }
 60 
 61         func = (unsigned long)io->open_volume;
 62         status = efi_early->call(func, io, &fh);
 63         if (status != EFI_SUCCESS)
 64                 efi_printk(sys_table, "Failed to open volume\n");
 65 
 66         *__fh = fh;
 67         return status;
 68 }
 69 
 70 static inline efi_status_t __open_volume64(void *__image, void **__fh)
 71 {
 72         efi_file_io_interface_t *io;
 73         efi_loaded_image_64_t *image = __image;
 74         efi_file_handle_64_t *fh;
 75         efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
 76         efi_status_t status;
 77         void *handle = (void *)(unsigned long)image->device_handle;
 78         unsigned long func;
 79 
 80         status = efi_call_early(handle_protocol, handle,
 81                                 &fs_proto, (void **)&io);
 82         if (status != EFI_SUCCESS) {
 83                 efi_printk(sys_table, "Failed to handle fs_proto\n");
 84                 return status;
 85         }
 86 
 87         func = (unsigned long)io->open_volume;
 88         status = efi_early->call(func, io, &fh);
 89         if (status != EFI_SUCCESS)
 90                 efi_printk(sys_table, "Failed to open volume\n");
 91 
 92         *__fh = fh;
 93         return status;
 94 }
 95 
 96 efi_status_t
 97 efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
 98 {
 99         if (efi_early->is64)
100                 return __open_volume64(__image, __fh);
101 
102         return __open_volume32(__image, __fh);
103 }
104 
105 void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
106 {
107         efi_call_proto(efi_simple_text_output_protocol, output_string,
108                        efi_early->text_output, str);
109 }
110 
111 static efi_status_t
112 __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
113 {
114         struct pci_setup_rom *rom = NULL;
115         efi_status_t status;
116         unsigned long size;
117         uint64_t attributes;
118 
119         status = efi_early->call(pci->attributes, pci,
120                                  EfiPciIoAttributeOperationGet, 0, 0,
121                                  &attributes);
122         if (status != EFI_SUCCESS)
123                 return status;
124 
125         if (!pci->romimage || !pci->romsize)
126                 return EFI_INVALID_PARAMETER;
127 
128         size = pci->romsize + sizeof(*rom);
129 
130         status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
131         if (status != EFI_SUCCESS) {
132                 efi_printk(sys_table, "Failed to alloc mem for rom\n");
133                 return status;
134         }
135 
136         memset(rom, 0, sizeof(*rom));
137 
138         rom->data.type = SETUP_PCI;
139         rom->data.len = size - sizeof(struct setup_data);
140         rom->data.next = 0;
141         rom->pcilen = pci->romsize;
142         *__rom = rom;
143 
144         status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
145                                  PCI_VENDOR_ID, 1, &(rom->vendor));
146 
147         if (status != EFI_SUCCESS) {
148                 efi_printk(sys_table, "Failed to read rom->vendor\n");
149                 goto free_struct;
150         }
151 
152         status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
153                                  PCI_DEVICE_ID, 1, &(rom->devid));
154 
155         if (status != EFI_SUCCESS) {
156                 efi_printk(sys_table, "Failed to read rom->devid\n");
157                 goto free_struct;
158         }
159 
160         status = efi_early->call(pci->get_location, pci, &(rom->segment),
161                                  &(rom->bus), &(rom->device), &(rom->function));
162 
163         if (status != EFI_SUCCESS)
164                 goto free_struct;
165 
166         memcpy(rom->romdata, pci->romimage, pci->romsize);
167         return status;
168 
169 free_struct:
170         efi_call_early(free_pool, rom);
171         return status;
172 }
173 
174 static void
175 setup_efi_pci32(struct boot_params *params, void **pci_handle,
176                 unsigned long size)
177 {
178         efi_pci_io_protocol_32 *pci = NULL;
179         efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
180         u32 *handles = (u32 *)(unsigned long)pci_handle;
181         efi_status_t status;
182         unsigned long nr_pci;
183         struct setup_data *data;
184         int i;
185 
186         data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
187 
188         while (data && data->next)
189                 data = (struct setup_data *)(unsigned long)data->next;
190 
191         nr_pci = size / sizeof(u32);
192         for (i = 0; i < nr_pci; i++) {
193                 struct pci_setup_rom *rom = NULL;
194                 u32 h = handles[i];
195 
196                 status = efi_call_early(handle_protocol, h,
197                                         &pci_proto, (void **)&pci);
198 
199                 if (status != EFI_SUCCESS)
200                         continue;
201 
202                 if (!pci)
203                         continue;
204 
205                 status = __setup_efi_pci32(pci, &rom);
206                 if (status != EFI_SUCCESS)
207                         continue;
208 
209                 if (data)
210                         data->next = (unsigned long)rom;
211                 else
212                         params->hdr.setup_data = (unsigned long)rom;
213 
214                 data = (struct setup_data *)rom;
215 
216         }
217 }
218 
219 static efi_status_t
220 __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
221 {
222         struct pci_setup_rom *rom;
223         efi_status_t status;
224         unsigned long size;
225         uint64_t attributes;
226 
227         status = efi_early->call(pci->attributes, pci,
228                                  EfiPciIoAttributeOperationGet, 0,
229                                  &attributes);
230         if (status != EFI_SUCCESS)
231                 return status;
232 
233         if (!pci->romimage || !pci->romsize)
234                 return EFI_INVALID_PARAMETER;
235 
236         size = pci->romsize + sizeof(*rom);
237 
238         status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
239         if (status != EFI_SUCCESS) {
240                 efi_printk(sys_table, "Failed to alloc mem for rom\n");
241                 return status;
242         }
243 
244         rom->data.type = SETUP_PCI;
245         rom->data.len = size - sizeof(struct setup_data);
246         rom->data.next = 0;
247         rom->pcilen = pci->romsize;
248         *__rom = rom;
249 
250         status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
251                                  PCI_VENDOR_ID, 1, &(rom->vendor));
252 
253         if (status != EFI_SUCCESS) {
254                 efi_printk(sys_table, "Failed to read rom->vendor\n");
255                 goto free_struct;
256         }
257 
258         status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
259                                  PCI_DEVICE_ID, 1, &(rom->devid));
260 
261         if (status != EFI_SUCCESS) {
262                 efi_printk(sys_table, "Failed to read rom->devid\n");
263                 goto free_struct;
264         }
265 
266         status = efi_early->call(pci->get_location, pci, &(rom->segment),
267                                  &(rom->bus), &(rom->device), &(rom->function));
268 
269         if (status != EFI_SUCCESS)
270                 goto free_struct;
271 
272         memcpy(rom->romdata, pci->romimage, pci->romsize);
273         return status;
274 
275 free_struct:
276         efi_call_early(free_pool, rom);
277         return status;
278 
279 }
280 
281 static void
282 setup_efi_pci64(struct boot_params *params, void **pci_handle,
283                 unsigned long size)
284 {
285         efi_pci_io_protocol_64 *pci = NULL;
286         efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
287         u64 *handles = (u64 *)(unsigned long)pci_handle;
288         efi_status_t status;
289         unsigned long nr_pci;
290         struct setup_data *data;
291         int i;
292 
293         data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
294 
295         while (data && data->next)
296                 data = (struct setup_data *)(unsigned long)data->next;
297 
298         nr_pci = size / sizeof(u64);
299         for (i = 0; i < nr_pci; i++) {
300                 struct pci_setup_rom *rom = NULL;
301                 u64 h = handles[i];
302 
303                 status = efi_call_early(handle_protocol, h,
304                                         &pci_proto, (void **)&pci);
305 
306                 if (status != EFI_SUCCESS)
307                         continue;
308 
309                 if (!pci)
310                         continue;
311 
312                 status = __setup_efi_pci64(pci, &rom);
313                 if (status != EFI_SUCCESS)
314                         continue;
315 
316                 if (data)
317                         data->next = (unsigned long)rom;
318                 else
319                         params->hdr.setup_data = (unsigned long)rom;
320 
321                 data = (struct setup_data *)rom;
322 
323         }
324 }
325 
326 /*
327  * There's no way to return an informative status from this function,
328  * because any analysis (and printing of error messages) needs to be
329  * done directly at the EFI function call-site.
330  *
331  * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
332  * just didn't find any PCI devices, but there's no way to tell outside
333  * the context of the call.
334  */
335 static void setup_efi_pci(struct boot_params *params)
336 {
337         efi_status_t status;
338         void **pci_handle = NULL;
339         efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
340         unsigned long size = 0;
341 
342         status = efi_call_early(locate_handle,
343                                 EFI_LOCATE_BY_PROTOCOL,
344                                 &pci_proto, NULL, &size, pci_handle);
345 
346         if (status == EFI_BUFFER_TOO_SMALL) {
347                 status = efi_call_early(allocate_pool,
348                                         EFI_LOADER_DATA,
349                                         size, (void **)&pci_handle);
350 
351                 if (status != EFI_SUCCESS) {
352                         efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
353                         return;
354                 }
355 
356                 status = efi_call_early(locate_handle,
357                                         EFI_LOCATE_BY_PROTOCOL, &pci_proto,
358                                         NULL, &size, pci_handle);
359         }
360 
361         if (status != EFI_SUCCESS)
362                 goto free_handle;
363 
364         if (efi_early->is64)
365                 setup_efi_pci64(params, pci_handle, size);
366         else
367                 setup_efi_pci32(params, pci_handle, size);
368 
369 free_handle:
370         efi_call_early(free_pool, pci_handle);
371 }
372 
373 static void retrieve_apple_device_properties(struct boot_params *boot_params)
374 {
375         efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
376         struct setup_data *data, *new;
377         efi_status_t status;
378         u32 size = 0;
379         void *p;
380 
381         status = efi_call_early(locate_protocol, &guid, NULL, &p);
382         if (status != EFI_SUCCESS)
383                 return;
384 
385         if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
386                 efi_printk(sys_table, "Unsupported properties proto version\n");
387                 return;
388         }
389 
390         efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
391         if (!size)
392                 return;
393 
394         do {
395                 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
396                                         size + sizeof(struct setup_data), &new);
397                 if (status != EFI_SUCCESS) {
398                         efi_printk(sys_table,
399                                         "Failed to alloc mem for properties\n");
400                         return;
401                 }
402 
403                 status = efi_call_proto(apple_properties_protocol, get_all, p,
404                                         new->data, &size);
405 
406                 if (status == EFI_BUFFER_TOO_SMALL)
407                         efi_call_early(free_pool, new);
408         } while (status == EFI_BUFFER_TOO_SMALL);
409 
410         new->type = SETUP_APPLE_PROPERTIES;
411         new->len  = size;
412         new->next = 0;
413 
414         data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
415         if (!data)
416                 boot_params->hdr.setup_data = (unsigned long)new;
417         else {
418                 while (data->next)
419                         data = (struct setup_data *)(unsigned long)data->next;
420                 data->next = (unsigned long)new;
421         }
422 }
423 
424 static void setup_quirks(struct boot_params *boot_params)
425 {
426         efi_char16_t const apple[] = { 'A', 'p', 'p', 'l', 'e', 0 };
427         efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
428                 efi_table_attr(efi_system_table, fw_vendor, sys_table);
429 
430         if (!memcmp(fw_vendor, apple, sizeof(apple))) {
431                 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
432                         retrieve_apple_device_properties(boot_params);
433         }
434 }
435 
436 static efi_status_t
437 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
438 {
439         struct efi_uga_draw_protocol *uga = NULL, *first_uga;
440         efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
441         unsigned long nr_ugas;
442         u32 *handles = (u32 *)uga_handle;;
443         efi_status_t status = EFI_INVALID_PARAMETER;
444         int i;
445 
446         first_uga = NULL;
447         nr_ugas = size / sizeof(u32);
448         for (i = 0; i < nr_ugas; i++) {
449                 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
450                 u32 w, h, depth, refresh;
451                 void *pciio;
452                 u32 handle = handles[i];
453 
454                 status = efi_call_early(handle_protocol, handle,
455                                         &uga_proto, (void **)&uga);
456                 if (status != EFI_SUCCESS)
457                         continue;
458 
459                 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
460 
461                 status = efi_early->call((unsigned long)uga->get_mode, uga,
462                                          &w, &h, &depth, &refresh);
463                 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
464                         *width = w;
465                         *height = h;
466 
467                         /*
468                          * Once we've found a UGA supporting PCIIO,
469                          * don't bother looking any further.
470                          */
471                         if (pciio)
472                                 break;
473 
474                         first_uga = uga;
475                 }
476         }
477 
478         return status;
479 }
480 
481 static efi_status_t
482 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
483 {
484         struct efi_uga_draw_protocol *uga = NULL, *first_uga;
485         efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
486         unsigned long nr_ugas;
487         u64 *handles = (u64 *)uga_handle;;
488         efi_status_t status = EFI_INVALID_PARAMETER;
489         int i;
490 
491         first_uga = NULL;
492         nr_ugas = size / sizeof(u64);
493         for (i = 0; i < nr_ugas; i++) {
494                 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
495                 u32 w, h, depth, refresh;
496                 void *pciio;
497                 u64 handle = handles[i];
498 
499                 status = efi_call_early(handle_protocol, handle,
500                                         &uga_proto, (void **)&uga);
501                 if (status != EFI_SUCCESS)
502                         continue;
503 
504                 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
505 
506                 status = efi_early->call((unsigned long)uga->get_mode, uga,
507                                          &w, &h, &depth, &refresh);
508                 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
509                         *width = w;
510                         *height = h;
511 
512                         /*
513                          * Once we've found a UGA supporting PCIIO,
514                          * don't bother looking any further.
515                          */
516                         if (pciio)
517                                 break;
518 
519                         first_uga = uga;
520                 }
521         }
522 
523         return status;
524 }
525 
526 /*
527  * See if we have Universal Graphics Adapter (UGA) protocol
528  */
529 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
530                               unsigned long size)
531 {
532         efi_status_t status;
533         u32 width, height;
534         void **uga_handle = NULL;
535 
536         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
537                                 size, (void **)&uga_handle);
538         if (status != EFI_SUCCESS)
539                 return status;
540 
541         status = efi_call_early(locate_handle,
542                                 EFI_LOCATE_BY_PROTOCOL,
543                                 uga_proto, NULL, &size, uga_handle);
544         if (status != EFI_SUCCESS)
545                 goto free_handle;
546 
547         height = 0;
548         width = 0;
549 
550         if (efi_early->is64)
551                 status = setup_uga64(uga_handle, size, &width, &height);
552         else
553                 status = setup_uga32(uga_handle, size, &width, &height);
554 
555         if (!width && !height)
556                 goto free_handle;
557 
558         /* EFI framebuffer */
559         si->orig_video_isVGA = VIDEO_TYPE_EFI;
560 
561         si->lfb_depth = 32;
562         si->lfb_width = width;
563         si->lfb_height = height;
564 
565         si->red_size = 8;
566         si->red_pos = 16;
567         si->green_size = 8;
568         si->green_pos = 8;
569         si->blue_size = 8;
570         si->blue_pos = 0;
571         si->rsvd_size = 8;
572         si->rsvd_pos = 24;
573 
574 free_handle:
575         efi_call_early(free_pool, uga_handle);
576         return status;
577 }
578 
579 void setup_graphics(struct boot_params *boot_params)
580 {
581         efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
582         struct screen_info *si;
583         efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
584         efi_status_t status;
585         unsigned long size;
586         void **gop_handle = NULL;
587         void **uga_handle = NULL;
588 
589         si = &boot_params->screen_info;
590         memset(si, 0, sizeof(*si));
591 
592         size = 0;
593         status = efi_call_early(locate_handle,
594                                 EFI_LOCATE_BY_PROTOCOL,
595                                 &graphics_proto, NULL, &size, gop_handle);
596         if (status == EFI_BUFFER_TOO_SMALL)
597                 status = efi_setup_gop(NULL, si, &graphics_proto, size);
598 
599         if (status != EFI_SUCCESS) {
600                 size = 0;
601                 status = efi_call_early(locate_handle,
602                                         EFI_LOCATE_BY_PROTOCOL,
603                                         &uga_proto, NULL, &size, uga_handle);
604                 if (status == EFI_BUFFER_TOO_SMALL)
605                         setup_uga(si, &uga_proto, size);
606         }
607 }
608 
609 /*
610  * Because the x86 boot code expects to be passed a boot_params we
611  * need to create one ourselves (usually the bootloader would create
612  * one for us).
613  *
614  * The caller is responsible for filling out ->code32_start in the
615  * returned boot_params.
616  */
617 struct boot_params *make_boot_params(struct efi_config *c)
618 {
619         struct boot_params *boot_params;
620         struct apm_bios_info *bi;
621         struct setup_header *hdr;
622         efi_loaded_image_t *image;
623         void *options, *handle;
624         efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
625         int options_size = 0;
626         efi_status_t status;
627         char *cmdline_ptr;
628         u16 *s2;
629         u8 *s1;
630         int i;
631         unsigned long ramdisk_addr;
632         unsigned long ramdisk_size;
633 
634         efi_early = c;
635         sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
636         handle = (void *)(unsigned long)efi_early->image_handle;
637 
638         /* Check if we were booted by the EFI firmware */
639         if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
640                 return NULL;
641 
642         if (efi_early->is64)
643                 setup_boot_services64(efi_early);
644         else
645                 setup_boot_services32(efi_early);
646 
647         status = efi_call_early(handle_protocol, handle,
648                                 &proto, (void *)&image);
649         if (status != EFI_SUCCESS) {
650                 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
651                 return NULL;
652         }
653 
654         status = efi_low_alloc(sys_table, 0x4000, 1,
655                                (unsigned long *)&boot_params);
656         if (status != EFI_SUCCESS) {
657                 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
658                 return NULL;
659         }
660 
661         memset(boot_params, 0x0, 0x4000);
662 
663         hdr = &boot_params->hdr;
664         bi = &boot_params->apm_bios_info;
665 
666         /* Copy the second sector to boot_params */
667         memcpy(&hdr->jump, image->image_base + 512, 512);
668 
669         /*
670          * Fill out some of the header fields ourselves because the
671          * EFI firmware loader doesn't load the first sector.
672          */
673         hdr->root_flags = 1;
674         hdr->vid_mode = 0xffff;
675         hdr->boot_flag = 0xAA55;
676 
677         hdr->type_of_loader = 0x21;
678 
679         /* Convert unicode cmdline to ascii */
680         cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
681         if (!cmdline_ptr)
682                 goto fail;
683         hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
684         /* Fill in upper bits of command line address, NOP on 32 bit  */
685         boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
686 
687         hdr->ramdisk_image = 0;
688         hdr->ramdisk_size = 0;
689 
690         /* Clear APM BIOS info */
691         memset(bi, 0, sizeof(*bi));
692 
693         status = efi_parse_options(cmdline_ptr);
694         if (status != EFI_SUCCESS)
695                 goto fail2;
696 
697         status = handle_cmdline_files(sys_table, image,
698                                       (char *)(unsigned long)hdr->cmd_line_ptr,
699                                       "initrd=", hdr->initrd_addr_max,
700                                       &ramdisk_addr, &ramdisk_size);
701 
702         if (status != EFI_SUCCESS &&
703             hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
704                 efi_printk(sys_table, "Trying to load files to higher address\n");
705                 status = handle_cmdline_files(sys_table, image,
706                                       (char *)(unsigned long)hdr->cmd_line_ptr,
707                                       "initrd=", -1UL,
708                                       &ramdisk_addr, &ramdisk_size);
709         }
710 
711         if (status != EFI_SUCCESS)
712                 goto fail2;
713         hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
714         hdr->ramdisk_size  = ramdisk_size & 0xffffffff;
715         boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
716         boot_params->ext_ramdisk_size  = (u64)ramdisk_size >> 32;
717 
718         return boot_params;
719 fail2:
720         efi_free(sys_table, options_size, hdr->cmd_line_ptr);
721 fail:
722         efi_free(sys_table, 0x4000, (unsigned long)boot_params);
723         return NULL;
724 }
725 
726 static void add_e820ext(struct boot_params *params,
727                         struct setup_data *e820ext, u32 nr_entries)
728 {
729         struct setup_data *data;
730         efi_status_t status;
731         unsigned long size;
732 
733         e820ext->type = SETUP_E820_EXT;
734         e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
735         e820ext->next = 0;
736 
737         data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
738 
739         while (data && data->next)
740                 data = (struct setup_data *)(unsigned long)data->next;
741 
742         if (data)
743                 data->next = (unsigned long)e820ext;
744         else
745                 params->hdr.setup_data = (unsigned long)e820ext;
746 }
747 
748 static efi_status_t setup_e820(struct boot_params *params,
749                                struct setup_data *e820ext, u32 e820ext_size)
750 {
751         struct boot_e820_entry *entry = params->e820_table;
752         struct efi_info *efi = &params->efi_info;
753         struct boot_e820_entry *prev = NULL;
754         u32 nr_entries;
755         u32 nr_desc;
756         int i;
757 
758         nr_entries = 0;
759         nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
760 
761         for (i = 0; i < nr_desc; i++) {
762                 efi_memory_desc_t *d;
763                 unsigned int e820_type = 0;
764                 unsigned long m = efi->efi_memmap;
765 
766 #ifdef CONFIG_X86_64
767                 m |= (u64)efi->efi_memmap_hi << 32;
768 #endif
769 
770                 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
771                 switch (d->type) {
772                 case EFI_RESERVED_TYPE:
773                 case EFI_RUNTIME_SERVICES_CODE:
774                 case EFI_RUNTIME_SERVICES_DATA:
775                 case EFI_MEMORY_MAPPED_IO:
776                 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
777                 case EFI_PAL_CODE:
778                         e820_type = E820_TYPE_RESERVED;
779                         break;
780 
781                 case EFI_UNUSABLE_MEMORY:
782                         e820_type = E820_TYPE_UNUSABLE;
783                         break;
784 
785                 case EFI_ACPI_RECLAIM_MEMORY:
786                         e820_type = E820_TYPE_ACPI;
787                         break;
788 
789                 case EFI_LOADER_CODE:
790                 case EFI_LOADER_DATA:
791                 case EFI_BOOT_SERVICES_CODE:
792                 case EFI_BOOT_SERVICES_DATA:
793                 case EFI_CONVENTIONAL_MEMORY:
794                         e820_type = E820_TYPE_RAM;
795                         break;
796 
797                 case EFI_ACPI_MEMORY_NVS:
798                         e820_type = E820_TYPE_NVS;
799                         break;
800 
801                 case EFI_PERSISTENT_MEMORY:
802                         e820_type = E820_TYPE_PMEM;
803                         break;
804 
805                 default:
806                         continue;
807                 }
808 
809                 /* Merge adjacent mappings */
810                 if (prev && prev->type == e820_type &&
811                     (prev->addr + prev->size) == d->phys_addr) {
812                         prev->size += d->num_pages << 12;
813                         continue;
814                 }
815 
816                 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
817                         u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
818                                    sizeof(struct setup_data);
819 
820                         if (!e820ext || e820ext_size < need)
821                                 return EFI_BUFFER_TOO_SMALL;
822 
823                         /* boot_params map full, switch to e820 extended */
824                         entry = (struct boot_e820_entry *)e820ext->data;
825                 }
826 
827                 entry->addr = d->phys_addr;
828                 entry->size = d->num_pages << PAGE_SHIFT;
829                 entry->type = e820_type;
830                 prev = entry++;
831                 nr_entries++;
832         }
833 
834         if (nr_entries > ARRAY_SIZE(params->e820_table)) {
835                 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
836 
837                 add_e820ext(params, e820ext, nr_e820ext);
838                 nr_entries -= nr_e820ext;
839         }
840 
841         params->e820_entries = (u8)nr_entries;
842 
843         return EFI_SUCCESS;
844 }
845 
846 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
847                                   u32 *e820ext_size)
848 {
849         efi_status_t status;
850         unsigned long size;
851 
852         size = sizeof(struct setup_data) +
853                 sizeof(struct e820_entry) * nr_desc;
854 
855         if (*e820ext) {
856                 efi_call_early(free_pool, *e820ext);
857                 *e820ext = NULL;
858                 *e820ext_size = 0;
859         }
860 
861         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
862                                 size, (void **)e820ext);
863         if (status == EFI_SUCCESS)
864                 *e820ext_size = size;
865 
866         return status;
867 }
868 
869 struct exit_boot_struct {
870         struct boot_params *boot_params;
871         struct efi_info *efi;
872         struct setup_data *e820ext;
873         __u32 e820ext_size;
874         bool is64;
875 };
876 
877 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
878                                    struct efi_boot_memmap *map,
879                                    void *priv)
880 {
881         static bool first = true;
882         const char *signature;
883         __u32 nr_desc;
884         efi_status_t status;
885         struct exit_boot_struct *p = priv;
886 
887         if (first) {
888                 nr_desc = *map->buff_size / *map->desc_size;
889                 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_table)) {
890                         u32 nr_e820ext = nr_desc -
891                                         ARRAY_SIZE(p->boot_params->e820_table);
892 
893                         status = alloc_e820ext(nr_e820ext, &p->e820ext,
894                                                &p->e820ext_size);
895                         if (status != EFI_SUCCESS)
896                                 return status;
897                 }
898                 first = false;
899         }
900 
901         signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
902         memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
903 
904         p->efi->efi_systab = (unsigned long)sys_table_arg;
905         p->efi->efi_memdesc_size = *map->desc_size;
906         p->efi->efi_memdesc_version = *map->desc_ver;
907         p->efi->efi_memmap = (unsigned long)*map->map;
908         p->efi->efi_memmap_size = *map->map_size;
909 
910 #ifdef CONFIG_X86_64
911         p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
912         p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
913 #endif
914 
915         return EFI_SUCCESS;
916 }
917 
918 static efi_status_t exit_boot(struct boot_params *boot_params,
919                               void *handle, bool is64)
920 {
921         unsigned long map_sz, key, desc_size, buff_size;
922         efi_memory_desc_t *mem_map;
923         struct setup_data *e820ext;
924         __u32 e820ext_size;
925         efi_status_t status;
926         __u32 desc_version;
927         struct efi_boot_memmap map;
928         struct exit_boot_struct priv;
929 
930         map.map =               &mem_map;
931         map.map_size =          &map_sz;
932         map.desc_size =         &desc_size;
933         map.desc_ver =          &desc_version;
934         map.key_ptr =           &key;
935         map.buff_size =         &buff_size;
936         priv.boot_params =      boot_params;
937         priv.efi =              &boot_params->efi_info;
938         priv.e820ext =          NULL;
939         priv.e820ext_size =     0;
940         priv.is64 =             is64;
941 
942         /* Might as well exit boot services now */
943         status = efi_exit_boot_services(sys_table, handle, &map, &priv,
944                                         exit_boot_func);
945         if (status != EFI_SUCCESS)
946                 return status;
947 
948         e820ext = priv.e820ext;
949         e820ext_size = priv.e820ext_size;
950         /* Historic? */
951         boot_params->alt_mem_k = 32 * 1024;
952 
953         status = setup_e820(boot_params, e820ext, e820ext_size);
954         if (status != EFI_SUCCESS)
955                 return status;
956 
957         return EFI_SUCCESS;
958 }
959 
960 /*
961  * On success we return a pointer to a boot_params structure, and NULL
962  * on failure.
963  */
964 struct boot_params *efi_main(struct efi_config *c,
965                              struct boot_params *boot_params)
966 {
967         struct desc_ptr *gdt = NULL;
968         efi_loaded_image_t *image;
969         struct setup_header *hdr = &boot_params->hdr;
970         efi_status_t status;
971         struct desc_struct *desc;
972         void *handle;
973         efi_system_table_t *_table;
974         bool is64;
975 
976         efi_early = c;
977 
978         _table = (efi_system_table_t *)(unsigned long)efi_early->table;
979         handle = (void *)(unsigned long)efi_early->image_handle;
980         is64 = efi_early->is64;
981 
982         sys_table = _table;
983 
984         /* Check if we were booted by the EFI firmware */
985         if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
986                 goto fail;
987 
988         if (is64)
989                 setup_boot_services64(efi_early);
990         else
991                 setup_boot_services32(efi_early);
992 
993         /*
994          * If the boot loader gave us a value for secure_boot then we use that,
995          * otherwise we ask the BIOS.
996          */
997         if (boot_params->secure_boot == efi_secureboot_mode_unset)
998                 boot_params->secure_boot = efi_get_secureboot(sys_table);
999 
1000         /* Ask the firmware to clear memory on unclean shutdown */
1001         efi_enable_reset_attack_mitigation(sys_table);
1002 
1003         setup_graphics(boot_params);
1004 
1005         setup_efi_pci(boot_params);
1006 
1007         setup_quirks(boot_params);
1008 
1009         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1010                                 sizeof(*gdt), (void **)&gdt);
1011         if (status != EFI_SUCCESS) {
1012                 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
1013                 goto fail;
1014         }
1015 
1016         gdt->size = 0x800;
1017         status = efi_low_alloc(sys_table, gdt->size, 8,
1018                            (unsigned long *)&gdt->address);
1019         if (status != EFI_SUCCESS) {
1020                 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
1021                 goto fail;
1022         }
1023 
1024         /*
1025          * If the kernel isn't already loaded at the preferred load
1026          * address, relocate it.
1027          */
1028         if (hdr->pref_address != hdr->code32_start) {
1029                 unsigned long bzimage_addr = hdr->code32_start;
1030                 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1031                                              hdr->init_size, hdr->init_size,
1032                                              hdr->pref_address,
1033                                              hdr->kernel_alignment);
1034                 if (status != EFI_SUCCESS) {
1035                         efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
1036                         goto fail;
1037                 }
1038 
1039                 hdr->pref_address = hdr->code32_start;
1040                 hdr->code32_start = bzimage_addr;
1041         }
1042 
1043         status = exit_boot(boot_params, handle, is64);
1044         if (status != EFI_SUCCESS) {
1045                 efi_printk(sys_table, "exit_boot() failed!\n");
1046                 goto fail;
1047         }
1048 
1049         memset((char *)gdt->address, 0x0, gdt->size);
1050         desc = (struct desc_struct *)gdt->address;
1051 
1052         /* The first GDT is a dummy. */
1053         desc++;
1054 
1055         if (IS_ENABLED(CONFIG_X86_64)) {
1056                 /* __KERNEL32_CS */
1057                 desc->limit0 = 0xffff;
1058                 desc->base0 = 0x0000;
1059                 desc->base1 = 0x0000;
1060                 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1061                 desc->s = DESC_TYPE_CODE_DATA;
1062                 desc->dpl = 0;
1063                 desc->p = 1;
1064                 desc->limit1 = 0xf;
1065                 desc->avl = 0;
1066                 desc->l = 0;
1067                 desc->d = SEG_OP_SIZE_32BIT;
1068                 desc->g = SEG_GRANULARITY_4KB;
1069                 desc->base2 = 0x00;
1070                 desc++;
1071         } else {
1072                 /* Second entry is unused on 32-bit */
1073                 desc++;
1074         }
1075 
1076         /* __KERNEL_CS */
1077         desc->limit0 = 0xffff;
1078         desc->base0 = 0x0000;
1079         desc->base1 = 0x0000;
1080         desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1081         desc->s = DESC_TYPE_CODE_DATA;
1082         desc->dpl = 0;
1083         desc->p = 1;
1084         desc->limit1 = 0xf;
1085         desc->avl = 0;
1086         if (IS_ENABLED(CONFIG_X86_64)) {
1087                 desc->l = 1;
1088                 desc->d = 0;
1089         } else {
1090                 desc->l = 0;
1091                 desc->d = SEG_OP_SIZE_32BIT;
1092         }
1093         desc->g = SEG_GRANULARITY_4KB;
1094         desc->base2 = 0x00;
1095         desc++;
1096 
1097         /* __KERNEL_DS */
1098         desc->limit0 = 0xffff;
1099         desc->base0 = 0x0000;
1100         desc->base1 = 0x0000;
1101         desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1102         desc->s = DESC_TYPE_CODE_DATA;
1103         desc->dpl = 0;
1104         desc->p = 1;
1105         desc->limit1 = 0xf;
1106         desc->avl = 0;
1107         desc->l = 0;
1108         desc->d = SEG_OP_SIZE_32BIT;
1109         desc->g = SEG_GRANULARITY_4KB;
1110         desc->base2 = 0x00;
1111         desc++;
1112 
1113         if (IS_ENABLED(CONFIG_X86_64)) {
1114                 /* Task segment value */
1115                 desc->limit0 = 0x0000;
1116                 desc->base0 = 0x0000;
1117                 desc->base1 = 0x0000;
1118                 desc->type = SEG_TYPE_TSS;
1119                 desc->s = 0;
1120                 desc->dpl = 0;
1121                 desc->p = 1;
1122                 desc->limit1 = 0x0;
1123                 desc->avl = 0;
1124                 desc->l = 0;
1125                 desc->d = 0;
1126                 desc->g = SEG_GRANULARITY_4KB;
1127                 desc->base2 = 0x00;
1128                 desc++;
1129         }
1130 
1131         asm volatile("cli");
1132         asm volatile ("lgdt %0" : : "m" (*gdt));
1133 
1134         return boot_params;
1135 fail:
1136         efi_printk(sys_table, "efi_main() failed!\n");
1137         return NULL;
1138 }
1139 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp