loadbios.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* loadbios.c - command to load a bios dump */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/misc.h>
  21. #include <grub/file.h>
  22. #include <grub/efi/efi.h>
  23. #include <grub/pci.h>
  24. #include <grub/command.h>
  25. #include <grub/i18n.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. static grub_efi_guid_t acpi_guid = GRUB_EFI_ACPI_TABLE_GUID;
  28. static grub_efi_guid_t acpi2_guid = GRUB_EFI_ACPI_20_TABLE_GUID;
  29. static grub_efi_guid_t smbios_guid = GRUB_EFI_SMBIOS_TABLE_GUID;
  30. #define EBDA_SEG_ADDR 0x40e
  31. #define LOW_MEM_ADDR 0x413
  32. #define FAKE_EBDA_SEG 0x9fc0
  33. #define BLANK_MEM 0xffffffff
  34. #define VBIOS_ADDR 0xc0000
  35. #define SBIOS_ADDR 0xf0000
  36. static int
  37. enable_rom_area (void)
  38. {
  39. grub_pci_address_t addr;
  40. grub_uint32_t *rom_ptr;
  41. grub_pci_device_t dev = { .bus = 0, .device = 0, .function = 0};
  42. rom_ptr = (grub_uint32_t *) VBIOS_ADDR;
  43. if (*rom_ptr != BLANK_MEM)
  44. {
  45. grub_puts_ (N_("ROM image is present."));
  46. return 0;
  47. }
  48. /* FIXME: should be macroified. */
  49. addr = grub_pci_make_address (dev, 144);
  50. grub_pci_write_byte (addr++, 0x30);
  51. grub_pci_write_byte (addr++, 0x33);
  52. grub_pci_write_byte (addr++, 0x33);
  53. grub_pci_write_byte (addr++, 0x33);
  54. grub_pci_write_byte (addr++, 0x33);
  55. grub_pci_write_byte (addr++, 0x33);
  56. grub_pci_write_byte (addr++, 0x33);
  57. grub_pci_write_byte (addr, 0);
  58. *rom_ptr = 0;
  59. if (*rom_ptr != 0)
  60. {
  61. grub_puts_ (N_("Can\'t enable ROM area."));
  62. return 0;
  63. }
  64. return 1;
  65. }
  66. static void
  67. lock_rom_area (void)
  68. {
  69. grub_pci_address_t addr;
  70. grub_pci_device_t dev = { .bus = 0, .device = 0, .function = 0};
  71. /* FIXME: should be macroified. */
  72. addr = grub_pci_make_address (dev, 144);
  73. grub_pci_write_byte (addr++, 0x10);
  74. grub_pci_write_byte (addr++, 0x11);
  75. grub_pci_write_byte (addr++, 0x11);
  76. grub_pci_write_byte (addr++, 0x11);
  77. grub_pci_write_byte (addr, 0x11);
  78. }
  79. static void
  80. fake_bios_data (int use_rom)
  81. {
  82. unsigned i;
  83. void *acpi, *smbios;
  84. grub_uint16_t *ebda_seg_ptr, *low_mem_ptr;
  85. ebda_seg_ptr = (grub_uint16_t *) EBDA_SEG_ADDR;
  86. low_mem_ptr = (grub_uint16_t *) LOW_MEM_ADDR;
  87. if ((*ebda_seg_ptr) || (*low_mem_ptr))
  88. return;
  89. acpi = 0;
  90. smbios = 0;
  91. for (i = 0; i < grub_efi_system_table->num_table_entries; i++)
  92. {
  93. grub_efi_packed_guid_t *guid =
  94. &grub_efi_system_table->configuration_table[i].vendor_guid;
  95. if (! grub_memcmp (guid, &acpi2_guid, sizeof (grub_efi_guid_t)))
  96. {
  97. acpi = grub_efi_system_table->configuration_table[i].vendor_table;
  98. grub_dprintf ("efi", "ACPI2: %p\n", acpi);
  99. }
  100. else if (! grub_memcmp (guid, &acpi_guid, sizeof (grub_efi_guid_t)))
  101. {
  102. void *t;
  103. t = grub_efi_system_table->configuration_table[i].vendor_table;
  104. if (! acpi)
  105. acpi = t;
  106. grub_dprintf ("efi", "ACPI: %p\n", t);
  107. }
  108. else if (! grub_memcmp (guid, &smbios_guid, sizeof (grub_efi_guid_t)))
  109. {
  110. smbios = grub_efi_system_table->configuration_table[i].vendor_table;
  111. grub_dprintf ("efi", "SMBIOS: %p\n", smbios);
  112. }
  113. }
  114. *ebda_seg_ptr = FAKE_EBDA_SEG;
  115. *low_mem_ptr = (FAKE_EBDA_SEG >> 6);
  116. *((grub_uint16_t *) (FAKE_EBDA_SEG << 4)) = 640 - *low_mem_ptr;
  117. if (acpi)
  118. grub_memcpy ((char *) ((FAKE_EBDA_SEG << 4) + 16), acpi, 1024 - 16);
  119. if ((use_rom) && (smbios))
  120. grub_memcpy ((char *) SBIOS_ADDR, (char *) smbios + 16, 16);
  121. }
  122. static grub_err_t
  123. grub_cmd_fakebios (struct grub_command *cmd __attribute__ ((unused)),
  124. int argc __attribute__ ((unused)),
  125. char *argv[] __attribute__ ((unused)))
  126. {
  127. if (enable_rom_area ())
  128. {
  129. fake_bios_data (1);
  130. lock_rom_area ();
  131. }
  132. else
  133. fake_bios_data (0);
  134. return 0;
  135. }
  136. static grub_err_t
  137. grub_cmd_loadbios (grub_command_t cmd __attribute__ ((unused)),
  138. int argc, char *argv[])
  139. {
  140. grub_file_t file;
  141. int size;
  142. if (argc == 0)
  143. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  144. if (argc > 1)
  145. {
  146. file = grub_file_open (argv[1]);
  147. if (! file)
  148. return grub_errno;
  149. if (file->size != 4)
  150. grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid int10 dump size");
  151. else
  152. grub_file_read (file, (void *) 0x40, 4);
  153. grub_file_close (file);
  154. if (grub_errno)
  155. return grub_errno;
  156. }
  157. file = grub_file_open (argv[0]);
  158. if (! file)
  159. return grub_errno;
  160. size = file->size;
  161. if ((size < 0x10000) || (size > 0x40000))
  162. grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid bios dump size");
  163. else if (enable_rom_area ())
  164. {
  165. grub_file_read (file, (void *) VBIOS_ADDR, size);
  166. fake_bios_data (size <= 0x40000);
  167. lock_rom_area ();
  168. }
  169. grub_file_close (file);
  170. return grub_errno;
  171. }
  172. static grub_command_t cmd_fakebios, cmd_loadbios;
  173. GRUB_MOD_INIT(loadbios)
  174. {
  175. cmd_fakebios = grub_register_command ("fakebios", grub_cmd_fakebios,
  176. 0, N_("Create BIOS-like structures for"
  177. " backward compatibility with"
  178. " existing OS."));
  179. cmd_loadbios = grub_register_command ("loadbios", grub_cmd_loadbios,
  180. N_("BIOS_DUMP [INT10_DUMP]"),
  181. N_("Load BIOS dump."));
  182. }
  183. GRUB_MOD_FINI(loadbios)
  184. {
  185. grub_unregister_command (cmd_fakebios);
  186. grub_unregister_command (cmd_loadbios);
  187. }