prepare.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Prepare efiemu. E.g. allocate memory, load the runtime
  2. to appropriate place, etc */
  3. /*
  4. * GRUB -- GRand Unified Bootloader
  5. * Copyright (C) 2009 Free Software Foundation, Inc.
  6. *
  7. * GRUB is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * GRUB is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <grub/err.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/efiemu/efiemu.h>
  24. #include <grub/lib.h>
  25. grub_err_t
  26. SUFFIX (grub_efiemu_prepare) (struct grub_efiemu_prepare_hook *prepare_hooks,
  27. struct grub_efiemu_configuration_table
  28. *config_tables)
  29. {
  30. grub_err_t err;
  31. int conftable_handle;
  32. struct grub_efiemu_configuration_table *cur;
  33. struct grub_efiemu_prepare_hook *curhook;
  34. int cntconftables = 0;
  35. struct SUFFIX (grub_efiemu_configuration_table) *conftables = 0;
  36. int i;
  37. int handle;
  38. grub_off_t off;
  39. grub_dprintf ("efiemu", "Preparing EfiEmu\n");
  40. /* Request space for the list of configuration tables */
  41. for (cur = config_tables; cur; cur = cur->next)
  42. cntconftables++;
  43. conftable_handle
  44. = grub_efiemu_request_memalign (GRUB_EFIEMU_PAGESIZE,
  45. cntconftables * sizeof (*conftables),
  46. GRUB_EFI_RUNTIME_SERVICES_DATA);
  47. /* Switch from phase 1 (counting) to phase 2 (real job) */
  48. grub_efiemu_alloc_syms ();
  49. grub_efiemu_mm_do_alloc ();
  50. grub_efiemu_write_sym_markers ();
  51. grub_efiemu_system_table32 = 0;
  52. grub_efiemu_system_table64 = 0;
  53. /* Execute hooks */
  54. for (curhook = prepare_hooks; curhook; curhook = curhook->next)
  55. curhook->hook (curhook->data);
  56. /* Move runtime to its due place */
  57. err = grub_efiemu_loadcore_load ();
  58. if (err)
  59. {
  60. grub_efiemu_unload ();
  61. return err;
  62. }
  63. err = grub_efiemu_resolve_symbol ("efiemu_system_table", &handle, &off);
  64. if (err)
  65. {
  66. grub_efiemu_unload ();
  67. return err;
  68. }
  69. SUFFIX (grub_efiemu_system_table)
  70. = (struct SUFFIX (grub_efi_system_table) *)
  71. ((grub_uint8_t *) grub_efiemu_mm_obtain_request (handle) + off);
  72. /* Put pointer to the list of configuration tables in system table */
  73. grub_efiemu_write_value
  74. (&(SUFFIX (grub_efiemu_system_table)->configuration_table), 0,
  75. conftable_handle, 0, 1,
  76. sizeof (SUFFIX (grub_efiemu_system_table)->configuration_table));
  77. SUFFIX(grub_efiemu_system_table)->num_table_entries = cntconftables;
  78. /* Fill the list of configuration tables */
  79. conftables = (struct SUFFIX (grub_efiemu_configuration_table) *)
  80. grub_efiemu_mm_obtain_request (conftable_handle);
  81. i = 0;
  82. for (cur = config_tables; cur; cur = cur->next, i++)
  83. {
  84. grub_memcpy (&(conftables[i].vendor_guid), &(cur->guid),
  85. sizeof (cur->guid));
  86. if (cur->get_table)
  87. conftables[i].vendor_table
  88. = PTR_TO_UINT64 (cur->get_table (cur->data));
  89. else
  90. conftables[i].vendor_table = PTR_TO_UINT64 (cur->data);
  91. }
  92. err = SUFFIX (grub_efiemu_crc) ();
  93. if (err)
  94. {
  95. grub_efiemu_unload ();
  96. return err;
  97. }
  98. grub_dprintf ("efiemu","system_table = %p, conftables = %p (%d entries)\n",
  99. SUFFIX (grub_efiemu_system_table), conftables, cntconftables);
  100. return GRUB_ERR_NONE;
  101. }
  102. grub_err_t
  103. SUFFIX (grub_efiemu_crc) (void)
  104. {
  105. grub_err_t err;
  106. int handle;
  107. grub_off_t off;
  108. struct SUFFIX (grub_efiemu_runtime_services) *runtime_services;
  109. /* compute CRC32 of runtime_services */
  110. err = grub_efiemu_resolve_symbol ("efiemu_runtime_services",
  111. &handle, &off);
  112. if (err)
  113. return err;
  114. runtime_services = (struct SUFFIX (grub_efiemu_runtime_services) *)
  115. ((grub_uint8_t *) grub_efiemu_mm_obtain_request (handle) + off);
  116. runtime_services->hdr.crc32 = 0;
  117. runtime_services->hdr.crc32 = grub_getcrc32
  118. (0, runtime_services, runtime_services->hdr.header_size);
  119. err = grub_efiemu_resolve_symbol ("efiemu_system_table", &handle, &off);
  120. if (err)
  121. return err;
  122. /* compute CRC32 of system table */
  123. SUFFIX (grub_efiemu_system_table)->hdr.crc32 = 0;
  124. SUFFIX (grub_efiemu_system_table)->hdr.crc32
  125. = grub_getcrc32 (0, SUFFIX (grub_efiemu_system_table),
  126. SUFFIX (grub_efiemu_system_table)->hdr.header_size);
  127. grub_dprintf ("efiemu","system_table = %p, runtime_services = %p\n",
  128. SUFFIX (grub_efiemu_system_table), runtime_services);
  129. return GRUB_ERR_NONE;
  130. }