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
39 return mFlashBase + ((
UINTN)Lba * mFdBlockSize) + Offset;
55 BOOLEAN FlashDetected;
62 FlashDetected =
FALSE;
63 Ptr = QemuFlashPtr (0, 0);
65 for (Offset = 0; Offset < mFdBlockSize; Offset++) {
66 Ptr = QemuFlashPtr (0, Offset);
68 if ((ProbeUint8 != CLEAR_STATUS_CMD) &&
69 (ProbeUint8 != READ_STATUS_CMD) &&
70 (ProbeUint8 != CLEARED_ARRAY_STATUS))
76 if (Offset >= mFdBlockSize) {
77 DEBUG ((DEBUG_INFO,
"QEMU Flash: Failed to find probe location\n"));
81 DEBUG ((DEBUG_INFO,
"QEMU Flash: Attempting flash detection at %p\n", Ptr));
95 "QEMU Flash: SEV-ES enabled, assuming FD behaves as FLASH\n"
100 OriginalUint8 = *Ptr;
101 *Ptr = CLEAR_STATUS_CMD;
103 if ((OriginalUint8 != CLEAR_STATUS_CMD) &&
104 (ProbeUint8 == CLEAR_STATUS_CMD))
106 DEBUG ((DEBUG_INFO,
"QemuFlashDetected => FD behaves as RAM\n"));
107 *Ptr = OriginalUint8;
109 *Ptr = READ_STATUS_CMD;
111 if (ProbeUint8 == OriginalUint8) {
112 DEBUG ((DEBUG_INFO,
"QemuFlashDetected => FD behaves as ROM\n"));
113 }
else if (ProbeUint8 == READ_STATUS_CMD) {
114 DEBUG ((DEBUG_INFO,
"QemuFlashDetected => FD behaves as RAM\n"));
115 *Ptr = OriginalUint8;
116 }
else if (ProbeUint8 == CLEARED_ARRAY_STATUS) {
117 *Ptr = WRITE_BYTE_CMD;
118 *Ptr = OriginalUint8;
119 *Ptr = READ_STATUS_CMD;
121 *Ptr = READ_ARRAY_CMD;
122 if (ProbeUint8 & 0x10 ) {
123 DEBUG ((DEBUG_INFO,
"QemuFlashDetected => FD behaves as FLASH, write-protected\n"));
125 DEBUG ((DEBUG_INFO,
"QemuFlashDetected => FD behaves as FLASH, writable\n"));
126 FlashDetected =
TRUE;
133 "QemuFlashDetected => %a\n",
134 FlashDetected ?
"Yes" :
"No"
136 return FlashDetected;
163 if (Lba >= mFdBlockCount) {
164 return EFI_INVALID_PARAMETER;
170 Ptr = (UINT8 *)QemuFlashPtr (Lba, Offset);
172 CopyMem (Buffer, Ptr, *NumBytes);
202 if (Lba >= mFdBlockCount) {
203 return EFI_INVALID_PARAMETER;
209 Ptr = QemuFlashPtr (Lba, Offset);
210 for (Loop = 0; Loop < *NumBytes; Loop++) {
240 if (Lba >= mFdBlockCount) {
241 return EFI_INVALID_PARAMETER;
244 Ptr = QemuFlashPtr (Lba, 0);
262 mFlashBase = (UINT8 *)(
UINTN)
PcdGet32 (PcdOvmfFdBaseAddress);
263 mFdBlockSize =
PcdGet32 (PcdOvmfFirmwareBlockSize);
264 ASSERT (
PcdGet32 (PcdOvmfFirmwareFdSize) % mFdBlockSize == 0);
265 mFdBlockCount =
PcdGet32 (PcdOvmfFirmwareFdSize) / mFdBlockSize;
270 QemuFlashBeforeProbe (
278 return EFI_WRITE_PROTECTED;
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define DEBUG(Expression)
BOOLEAN EFIAPI MemEncryptSevEsIsEnabled(VOID)
#define PcdGet32(TokenName)
#define FeaturePcdGet(TokenName)
STATIC BOOLEAN QemuFlashDetected(VOID)
EFI_STATUS QemuFlashWrite(IN EFI_LBA Lba, IN UINTN Offset, IN UINTN *NumBytes, IN UINT8 *Buffer)
EFI_STATUS QemuFlashEraseBlock(IN EFI_LBA Lba)
EFI_STATUS QemuFlashRead(IN EFI_LBA Lba, IN UINTN Offset, IN UINTN *NumBytes, IN UINT8 *Buffer)
EFI_STATUS QemuFlashInitialize(VOID)
VOID QemuFlashPtrWrite(IN volatile UINT8 *Ptr, IN UINT8 Value)
UINT64 EFI_PHYSICAL_ADDRESS