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

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

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

~ [ 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