prepare.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/types.h>
  23. #include <grub/misc.h>
  24. #include <grub/efiemu/efiemu.h>
  25. #include <grub/crypto.h>
  26. grub_err_t
  27. SUFFIX (grub_efiemu_prepare) (struct grub_efiemu_prepare_hook *prepare_hooks,
  28. struct grub_efiemu_configuration_table
  29. *config_tables)
  30. {
  31. grub_err_t err;
  32. int conftable_handle;
  33. struct grub_efiemu_configuration_table *cur;
  34. struct grub_efiemu_prepare_hook *curhook;
  35. int cntconftables = 0;
  36. struct SUFFIX (grub_efiemu_configuration_table) *conftables = 0;
  37. int i;
  38. int handle;
  39. grub_off_t off;
  40. grub_dprintf ("efiemu", "Preparing EfiEmu\n");
  41. /* Request space for the list of configuration tables */
  42. for (cur = config_tables; cur; cur = cur->next)
  43. cntconftables++;
  44. conftable_handle
  45. = grub_efiemu_request_memalign (GRUB_EFIEMU_PAGESIZE,
  46. cntconftables * sizeof (*conftables),
  47. GRUB_EFI_RUNTIME_SERVICES_DATA);
  48. /* Switch from phase 1 (counting) to phase 2 (real job) */
  49. grub_efiemu_alloc_syms ();
  50. grub_efiemu_mm_do_alloc ();
  51. grub_efiemu_write_sym_markers ();
  52. grub_efiemu_system_table32 = 0;
  53. grub_efiemu_system_table64 = 0;
  54. /* Execute hooks */
  55. for (curhook = prepare_hooks; curhook; curhook = curhook->next)
  56. curhook->hook (curhook->data);
  57. /* Move runtime to its due place */
  58. err = grub_efiemu_loadcore_load ();
  59. if (err)
  60. {
  61. grub_efiemu_unload ();
  62. return err;
  63. }
  64. err = grub_efiemu_resolve_symbol ("efiemu_system_table", &handle, &off);
  65. if (err)
  66. {
  67. grub_efiemu_unload ();
  68. return err;
  69. }
  70. SUFFIX (grub_efiemu_system_table)
  71. = (struct SUFFIX (grub_efi_system_table) *)
  72. ((grub_uint8_t *) grub_efiemu_mm_obtain_request (handle) + off);
  73. /* Put pointer to the list of configuration tables in system table */
  74. err = grub_efiemu_write_value
  75. (&(SUFFIX (grub_efiemu_system_table)->configuration_table), 0,
  76. conftable_handle, 0, 1,
  77. sizeof (SUFFIX (grub_efiemu_system_table)->configuration_table));
  78. if (err)
  79. {
  80. grub_efiemu_unload ();
  81. return err;
  82. }
  83. SUFFIX(grub_efiemu_system_table)->num_table_entries = cntconftables;
  84. /* Fill the list of configuration tables */
  85. conftables = (struct SUFFIX (grub_efiemu_configuration_table) *)
  86. grub_efiemu_mm_obtain_request (conftable_handle);
  87. i = 0;
  88. for (cur = config_tables; cur; cur = cur->next, i++)
  89. {
  90. grub_memcpy (&(conftables[i].vendor_guid), &(cur->guid),
  91. sizeof (cur->guid));
  92. if (cur->get_table)
  93. conftables[i].vendor_table = (grub_addr_t) cur->get_table (cur->data);
  94. else
  95. conftables[i].vendor_table = (grub_addr_t) cur->data;
  96. }
  97. err = SUFFIX (grub_efiemu_crc) ();
  98. if (err)
  99. {
  100. grub_efiemu_unload ();
  101. return err;
  102. }
  103. grub_dprintf ("efiemu","system_table = %p, conftables = %p (%d entries)\n",
  104. SUFFIX (grub_efiemu_system_table), conftables, cntconftables);
  105. return GRUB_ERR_NONE;
  106. }
  107. grub_err_t
  108. SUFFIX (grub_efiemu_crc) (void)
  109. {
  110. grub_err_t err;
  111. int handle;
  112. grub_off_t off;
  113. struct SUFFIX (grub_efiemu_runtime_services) *runtime_services;
  114. grub_uint32_t crc32_val;
  115. if (GRUB_MD_CRC32->mdlen != 4)
  116. return grub_error (GRUB_ERR_BUG, "incorrect mdlen");
  117. /* compute CRC32 of runtime_services */
  118. err = grub_efiemu_resolve_symbol ("efiemu_runtime_services",
  119. &handle, &off);
  120. if (err)
  121. return err;
  122. runtime_services = (struct SUFFIX (grub_efiemu_runtime_services) *)
  123. ((grub_uint8_t *) grub_efiemu_mm_obtain_request (handle) + off);
  124. runtime_services->hdr.crc32 = 0;
  125. grub_crypto_hash (GRUB_MD_CRC32, &crc32_val,
  126. runtime_services, runtime_services->hdr.header_size);
  127. runtime_services->hdr.crc32 =
  128. grub_be_to_cpu32(crc32_val);
  129. err = grub_efiemu_resolve_symbol ("efiemu_system_table", &handle, &off);
  130. if (err)
  131. return err;
  132. /* compute CRC32 of system table */
  133. SUFFIX (grub_efiemu_system_table)->hdr.crc32 = 0;
  134. grub_crypto_hash (GRUB_MD_CRC32, &crc32_val,
  135. SUFFIX (grub_efiemu_system_table),
  136. SUFFIX (grub_efiemu_system_table)->hdr.header_size);
  137. SUFFIX (grub_efiemu_system_table)->hdr.crc32 =
  138. grub_be_to_cpu32(crc32_val);
  139. grub_dprintf ("efiemu","system_table = %p, runtime_services = %p\n",
  140. SUFFIX (grub_efiemu_system_table), runtime_services);
  141. return GRUB_ERR_NONE;
  142. }