2 OVMF support for QEMU system firmware flash device
4 Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/BaseMemoryLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/MemEncryptSevLib.h>
13 #include <Library/PcdLib.h>
15 #include "QemuFlash.h"
17 #define WRITE_BYTE_CMD 0x10
18 #define BLOCK_ERASE_CMD 0x20
19 #define CLEAR_STATUS_CMD 0x50
20 #define READ_STATUS_CMD 0x70
21 #define READ_DEVID_CMD 0x90
22 #define BLOCK_ERASE_CONFIRM_CMD 0xd0
23 #define READ_ARRAY_CMD 0xff
25 #define CLEARED_ARRAY_STATUS 0x00
30 STATIC UINTN mFdBlockSize
= 0;
31 STATIC UINTN mFdBlockCount
= 0;
40 return mFlashBase
+ ((UINTN
)Lba
* mFdBlockSize
) + Offset
;
45 Determines if the QEMU flash memory device is present.
47 @retval FALSE The QEMU flash device is not present.
48 @retval TRUE The QEMU flash device is present.
57 BOOLEAN FlashDetected
;
64 FlashDetected
= FALSE
;
65 Ptr
= QemuFlashPtr (0, 0);
67 for (Offset
= 0; Offset
< mFdBlockSize
; Offset
++) {
68 Ptr
= QemuFlashPtr (0, Offset
);
70 if (ProbeUint8
!= CLEAR_STATUS_CMD
&&
71 ProbeUint8
!= READ_STATUS_CMD
&&
72 ProbeUint8
!= CLEARED_ARRAY_STATUS
) {
77 if (Offset
>= mFdBlockSize
) {
78 DEBUG ((DEBUG_INFO
, "QEMU Flash: Failed to find probe location\n"));
82 DEBUG ((DEBUG_INFO
, "QEMU Flash: Attempting flash detection at %p\n", Ptr
));
84 if (MemEncryptSevEsIsEnabled ()) {
86 // When SEV-ES is enabled, the check below can result in an infinite
87 // loop with respect to a nested page fault. When the memslot is mapped
88 // read-only, the nested page table entry is read-only. The check below
89 // will cause a nested page fault that cannot be emulated, causing
90 // the instruction to retried over and over. For SEV-ES, acknowledge that
91 // the FD appears as ROM and not as FLASH, but report FLASH anyway because
92 // FLASH behavior can be simulated using VMGEXIT.
95 "QEMU Flash: SEV-ES enabled, assuming FD behaves as FLASH\n"));
100 *Ptr
= CLEAR_STATUS_CMD
;
102 if (OriginalUint8
!= CLEAR_STATUS_CMD
&&
103 ProbeUint8
== CLEAR_STATUS_CMD
) {
104 DEBUG ((DEBUG_INFO
, "QemuFlashDetected => FD behaves as RAM\n"));
105 *Ptr
= OriginalUint8
;
107 *Ptr
= READ_STATUS_CMD
;
109 if (ProbeUint8
== OriginalUint8
) {
110 DEBUG ((DEBUG_INFO
, "QemuFlashDetected => FD behaves as ROM\n"));
111 } else if (ProbeUint8
== READ_STATUS_CMD
) {
112 DEBUG ((DEBUG_INFO
, "QemuFlashDetected => FD behaves as RAM\n"));
113 *Ptr
= OriginalUint8
;
114 } else if (ProbeUint8
== CLEARED_ARRAY_STATUS
) {
115 DEBUG ((DEBUG_INFO
, "QemuFlashDetected => FD behaves as FLASH\n"));
116 FlashDetected
= TRUE
;
117 *Ptr
= READ_ARRAY_CMD
;
121 DEBUG ((DEBUG_INFO
, "QemuFlashDetected => %a\n",
122 FlashDetected
? "Yes" : "No"));
123 return FlashDetected
;
130 @param[in] Lba The starting logical block index to read from.
131 @param[in] Offset Offset into the block at which to begin reading.
132 @param[in] NumBytes On input, indicates the requested read size. On
133 output, indicates the actual number of bytes read
134 @param[in] Buffer Pointer to the buffer to read into.
148 // Only write to the first 64k. We don't bother saving the FTW Spare
149 // block into the flash memory.
151 if (Lba
>= mFdBlockCount
) {
152 return EFI_INVALID_PARAMETER
;
158 Ptr
= (UINT8
*) QemuFlashPtr (Lba
, Offset
);
160 CopyMem (Buffer
, Ptr
, *NumBytes
);
169 @param[in] Lba The starting logical block index to write to.
170 @param[in] Offset Offset into the block at which to begin writing.
171 @param[in] NumBytes On input, indicates the requested write size. On
172 output, indicates the actual number of bytes written
173 @param[in] Buffer Pointer to the data to write.
188 // Only write to the first 64k. We don't bother saving the FTW Spare
189 // block into the flash memory.
191 if (Lba
>= mFdBlockCount
) {
192 return EFI_INVALID_PARAMETER
;
198 Ptr
= QemuFlashPtr (Lba
, Offset
);
199 for (Loop
= 0; Loop
< *NumBytes
; Loop
++) {
200 QemuFlashPtrWrite (Ptr
, WRITE_BYTE_CMD
);
201 QemuFlashPtrWrite (Ptr
, Buffer
[Loop
]);
207 // Restore flash to read mode
210 QemuFlashPtrWrite (Ptr
- 1, READ_ARRAY_CMD
);
218 Erase a QEMU Flash block
220 @param Lba The logical block index to erase.
224 QemuFlashEraseBlock (
230 if (Lba
>= mFdBlockCount
) {
231 return EFI_INVALID_PARAMETER
;
234 Ptr
= QemuFlashPtr (Lba
, 0);
235 QemuFlashPtrWrite (Ptr
, BLOCK_ERASE_CMD
);
236 QemuFlashPtrWrite (Ptr
, BLOCK_ERASE_CONFIRM_CMD
);
242 Initializes QEMU flash memory support
244 @retval EFI_WRITE_PROTECTED The QEMU flash device is not present.
245 @retval EFI_SUCCESS The QEMU flash device is supported.
249 QemuFlashInitialize (
253 mFlashBase
= (UINT8
*)(UINTN
) PcdGet32 (PcdOvmfFdBaseAddress
);
254 mFdBlockSize
= PcdGet32 (PcdOvmfFirmwareBlockSize
);
255 ASSERT(PcdGet32 (PcdOvmfFirmwareFdSize
) % mFdBlockSize
== 0);
256 mFdBlockCount
= PcdGet32 (PcdOvmfFirmwareFdSize
) / mFdBlockSize
;
259 // execute module specific hooks before probing the flash
261 QemuFlashBeforeProbe (
262 (EFI_PHYSICAL_ADDRESS
)(UINTN
) mFlashBase
,
267 if (!QemuFlashDetected ()) {
268 ASSERT (!FeaturePcdGet (PcdSmmSmramRequire
));
269 return EFI_WRITE_PROTECTED
;