pc_sysfw.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * QEMU PC System Firmware
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. * Copyright (c) 2011-2012 Intel Corporation
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qapi/error.h"
  27. #include "sysemu/block-backend.h"
  28. #include "qemu/error-report.h"
  29. #include "qemu/option.h"
  30. #include "qemu/units.h"
  31. #include "hw/sysbus.h"
  32. #include "hw/i386/x86.h"
  33. #include "hw/i386/pc.h"
  34. #include "hw/loader.h"
  35. #include "hw/qdev-properties.h"
  36. #include "hw/block/flash.h"
  37. #include "sysemu/kvm.h"
  38. #include "target/i386/sev.h"
  39. #define FLASH_SECTOR_SIZE 4096
  40. static void pc_isa_bios_init(PCMachineState *pcms, MemoryRegion *isa_bios,
  41. MemoryRegion *rom_memory, MemoryRegion *flash_mem)
  42. {
  43. int isa_bios_size;
  44. uint64_t flash_size;
  45. void *flash_ptr, *isa_bios_ptr;
  46. flash_size = memory_region_size(flash_mem);
  47. /* map the last 128KB of the BIOS in ISA space */
  48. isa_bios_size = MIN(flash_size, 128 * KiB);
  49. if (machine_require_guest_memfd(MACHINE(pcms))) {
  50. memory_region_init_ram_guest_memfd(isa_bios, NULL, "isa-bios",
  51. isa_bios_size, &error_fatal);
  52. } else {
  53. memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size,
  54. &error_fatal);
  55. }
  56. memory_region_add_subregion_overlap(rom_memory,
  57. 0x100000 - isa_bios_size,
  58. isa_bios,
  59. 1);
  60. /* copy ISA rom image from top of flash memory */
  61. flash_ptr = memory_region_get_ram_ptr(flash_mem);
  62. isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
  63. memcpy(isa_bios_ptr,
  64. ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
  65. isa_bios_size);
  66. if (!machine_require_guest_memfd(current_machine)) {
  67. memory_region_set_readonly(isa_bios, true);
  68. }
  69. }
  70. static PFlashCFI01 *pc_pflash_create(PCMachineState *pcms,
  71. const char *name,
  72. const char *alias_prop_name)
  73. {
  74. DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
  75. qdev_prop_set_uint64(dev, "sector-length", FLASH_SECTOR_SIZE);
  76. qdev_prop_set_uint8(dev, "width", 1);
  77. qdev_prop_set_string(dev, "name", name);
  78. object_property_add_child(OBJECT(pcms), name, OBJECT(dev));
  79. object_property_add_alias(OBJECT(pcms), alias_prop_name,
  80. OBJECT(dev), "drive");
  81. /*
  82. * The returned reference is tied to the child property and
  83. * will be removed with object_unparent.
  84. */
  85. object_unref(OBJECT(dev));
  86. return PFLASH_CFI01(dev);
  87. }
  88. void pc_system_flash_create(PCMachineState *pcms)
  89. {
  90. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  91. if (pcmc->pci_enabled) {
  92. pcms->flash[0] = pc_pflash_create(pcms, "system.flash0",
  93. "pflash0");
  94. pcms->flash[1] = pc_pflash_create(pcms, "system.flash1",
  95. "pflash1");
  96. }
  97. }
  98. void pc_system_flash_cleanup_unused(PCMachineState *pcms)
  99. {
  100. char *prop_name;
  101. int i;
  102. assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
  103. for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
  104. if (!qdev_is_realized(DEVICE(pcms->flash[i]))) {
  105. prop_name = g_strdup_printf("pflash%d", i);
  106. object_property_del(OBJECT(pcms), prop_name);
  107. g_free(prop_name);
  108. object_unparent(OBJECT(pcms->flash[i]));
  109. pcms->flash[i] = NULL;
  110. }
  111. }
  112. }
  113. /*
  114. * Map the pcms->flash[] from 4GiB downward, and realize.
  115. * Map them in descending order, i.e. pcms->flash[0] at the top,
  116. * without gaps.
  117. * Stop at the first pcms->flash[0] lacking a block backend.
  118. * Set each flash's size from its block backend. Fatal error if the
  119. * size isn't a non-zero multiple of 4KiB, or the total size exceeds
  120. * pcms->max_fw_size.
  121. *
  122. * If pcms->flash[0] has a block backend, its memory is passed to
  123. * pc_isa_bios_init(). Merging several flash devices for isa-bios is
  124. * not supported.
  125. */
  126. static void pc_system_flash_map(PCMachineState *pcms,
  127. MemoryRegion *rom_memory)
  128. {
  129. X86MachineState *x86ms = X86_MACHINE(pcms);
  130. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  131. hwaddr total_size = 0;
  132. int i;
  133. BlockBackend *blk;
  134. int64_t size;
  135. PFlashCFI01 *system_flash;
  136. MemoryRegion *flash_mem;
  137. void *flash_ptr;
  138. int flash_size;
  139. assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
  140. for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
  141. hwaddr gpa;
  142. system_flash = pcms->flash[i];
  143. blk = pflash_cfi01_get_blk(system_flash);
  144. if (!blk) {
  145. break;
  146. }
  147. size = blk_getlength(blk);
  148. if (size < 0) {
  149. error_report("can't get size of block device %s: %s",
  150. blk_name(blk), strerror(-size));
  151. exit(1);
  152. }
  153. if (size == 0 || !QEMU_IS_ALIGNED(size, FLASH_SECTOR_SIZE)) {
  154. error_report("system firmware block device %s has invalid size "
  155. "%" PRId64,
  156. blk_name(blk), size);
  157. info_report("its size must be a non-zero multiple of 0x%x",
  158. FLASH_SECTOR_SIZE);
  159. exit(1);
  160. }
  161. if ((hwaddr)size != size
  162. || total_size > HWADDR_MAX - size
  163. || total_size + size > pcms->max_fw_size) {
  164. error_report("combined size of system firmware exceeds "
  165. "%" PRIu64 " bytes",
  166. pcms->max_fw_size);
  167. exit(1);
  168. }
  169. total_size += size;
  170. gpa = 0x100000000ULL - total_size; /* where the flash is mapped */
  171. qdev_prop_set_uint32(DEVICE(system_flash), "num-blocks",
  172. size / FLASH_SECTOR_SIZE);
  173. sysbus_realize_and_unref(SYS_BUS_DEVICE(system_flash), &error_fatal);
  174. sysbus_mmio_map(SYS_BUS_DEVICE(system_flash), 0, gpa);
  175. if (i == 0) {
  176. flash_mem = pflash_cfi01_get_memory(system_flash);
  177. if (pcmc->isa_bios_alias) {
  178. x86_isa_bios_init(&x86ms->isa_bios, rom_memory, flash_mem,
  179. true);
  180. } else {
  181. pc_isa_bios_init(pcms, &x86ms->isa_bios, rom_memory, flash_mem);
  182. }
  183. /* Encrypt the pflash boot ROM */
  184. if (sev_enabled()) {
  185. flash_ptr = memory_region_get_ram_ptr(flash_mem);
  186. flash_size = memory_region_size(flash_mem);
  187. x86_firmware_configure(gpa, flash_ptr, flash_size);
  188. }
  189. }
  190. }
  191. }
  192. void pc_system_firmware_init(PCMachineState *pcms,
  193. MemoryRegion *rom_memory)
  194. {
  195. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  196. int i;
  197. BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)];
  198. if (!pcmc->pci_enabled) {
  199. x86_bios_rom_init(X86_MACHINE(pcms), "bios.bin", rom_memory, true);
  200. return;
  201. }
  202. /* Map legacy -drive if=pflash to machine properties */
  203. for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
  204. pflash_cfi01_legacy_drive(pcms->flash[i],
  205. drive_get(IF_PFLASH, 0, i));
  206. pflash_blk[i] = pflash_cfi01_get_blk(pcms->flash[i]);
  207. }
  208. /* Reject gaps */
  209. for (i = 1; i < ARRAY_SIZE(pcms->flash); i++) {
  210. if (pflash_blk[i] && !pflash_blk[i - 1]) {
  211. error_report("pflash%d requires pflash%d", i, i - 1);
  212. exit(1);
  213. }
  214. }
  215. if (!pflash_blk[0]) {
  216. /* Machine property pflash0 not set, use ROM mode */
  217. x86_bios_rom_init(X86_MACHINE(pcms), "bios.bin", rom_memory, false);
  218. } else {
  219. if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
  220. /*
  221. * Older KVM cannot execute from device memory. So, flash
  222. * memory cannot be used unless the readonly memory kvm
  223. * capability is present.
  224. */
  225. error_report("pflash with kvm requires KVM readonly memory support");
  226. exit(1);
  227. }
  228. pc_system_flash_map(pcms, rom_memory);
  229. }
  230. pc_system_flash_cleanup_unused(pcms);
  231. }
  232. void x86_firmware_configure(hwaddr gpa, void *ptr, int size)
  233. {
  234. int ret;
  235. /*
  236. * OVMF places a GUIDed structures in the flash, so
  237. * search for them
  238. */
  239. pc_system_parse_ovmf_flash(ptr, size);
  240. if (sev_enabled()) {
  241. /* Copy the SEV metadata table (if it exists) */
  242. pc_system_parse_sev_metadata(ptr, size);
  243. ret = sev_es_save_reset_vector(ptr, size);
  244. if (ret) {
  245. error_report("failed to locate and/or save reset vector");
  246. exit(1);
  247. }
  248. sev_encrypt_flash(gpa, ptr, size, &error_fatal);
  249. }
  250. }