loadbios.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_guid_t acpi_guid = GRUB_EFI_ACPI_TABLE_GUID;
  28. static grub_guid_t acpi2_guid = GRUB_EFI_ACPI_20_TABLE_GUID;
  29. static grub_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_absolute_pointer (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. void *acpi, *smbios;
  83. grub_uint16_t *ebda_seg_ptr, *low_mem_ptr;
  84. ebda_seg_ptr = grub_absolute_pointer (EBDA_SEG_ADDR);
  85. low_mem_ptr = grub_absolute_pointer (LOW_MEM_ADDR);
  86. if ((*ebda_seg_ptr) || (*low_mem_ptr))
  87. return;
  88. acpi = grub_efi_find_configuration_table (&acpi2_guid);
  89. grub_dprintf ("efi", "ACPI2: %p\n", acpi);
  90. if (!acpi) {
  91. acpi = grub_efi_find_configuration_table (&acpi_guid);
  92. grub_dprintf ("efi", "ACPI: %p\n", acpi);
  93. }
  94. smbios = grub_efi_find_configuration_table (&smbios_guid);
  95. grub_dprintf ("efi", "SMBIOS: %p\n", smbios);
  96. *ebda_seg_ptr = FAKE_EBDA_SEG;
  97. *low_mem_ptr = (FAKE_EBDA_SEG >> 6);
  98. /* *((grub_uint16_t *) (FAKE_EBDA_SEG << 4)) = 640 - *low_mem_ptr; */
  99. *((grub_uint16_t *) (grub_absolute_pointer (FAKE_EBDA_SEG << 4))) = 640 - *low_mem_ptr;
  100. if (acpi)
  101. grub_memcpy ((char *) ((FAKE_EBDA_SEG << 4) + 16), acpi, 1024 - 16);
  102. if ((use_rom) && (smbios))
  103. grub_memcpy ((char *) SBIOS_ADDR, (char *) smbios + 16, 16);
  104. }
  105. static grub_err_t
  106. grub_cmd_fakebios (struct grub_command *cmd __attribute__ ((unused)),
  107. int argc __attribute__ ((unused)),
  108. char *argv[] __attribute__ ((unused)))
  109. {
  110. if (enable_rom_area ())
  111. {
  112. fake_bios_data (1);
  113. lock_rom_area ();
  114. }
  115. else
  116. fake_bios_data (0);
  117. return 0;
  118. }
  119. static grub_err_t
  120. grub_cmd_loadbios (grub_command_t cmd __attribute__ ((unused)),
  121. int argc, char *argv[])
  122. {
  123. grub_file_t file;
  124. int size;
  125. if (argc == 0)
  126. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  127. if (argc > 1)
  128. {
  129. file = grub_file_open (argv[1], GRUB_FILE_TYPE_VBE_DUMP);
  130. if (! file)
  131. return grub_errno;
  132. if (file->size != 4)
  133. grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid int10 dump size");
  134. else
  135. grub_file_read (file, (void *) 0x40, 4);
  136. grub_file_close (file);
  137. if (grub_errno)
  138. return grub_errno;
  139. }
  140. file = grub_file_open (argv[0], GRUB_FILE_TYPE_VBE_DUMP);
  141. if (! file)
  142. return grub_errno;
  143. size = file->size;
  144. if ((size < 0x10000) || (size > 0x40000))
  145. grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid bios dump size");
  146. else if (enable_rom_area ())
  147. {
  148. grub_file_read (file, (void *) VBIOS_ADDR, size);
  149. fake_bios_data (size <= 0x40000);
  150. lock_rom_area ();
  151. }
  152. grub_file_close (file);
  153. return grub_errno;
  154. }
  155. static grub_command_t cmd_fakebios, cmd_loadbios;
  156. GRUB_MOD_INIT(loadbios)
  157. {
  158. cmd_fakebios = grub_register_command_lockdown ("fakebios", grub_cmd_fakebios,
  159. 0, N_("Create BIOS-like structures for"
  160. " backward compatibility with"
  161. " existing OS."));
  162. cmd_loadbios = grub_register_command_lockdown ("loadbios", grub_cmd_loadbios,
  163. N_("BIOS_DUMP [INT10_DUMP]"),
  164. N_("Load BIOS dump."));
  165. }
  166. GRUB_MOD_FINI(loadbios)
  167. {
  168. grub_unregister_command (cmd_fakebios);
  169. grub_unregister_command (cmd_loadbios);
  170. }