2 OVMF support for QEMU system firmware flash device
4 Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/DebugLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/PcdLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/UefiRuntimeLib.h>
21 #include <Guid/EventGroup.h>
23 #include "QemuFlash.h"
25 #define WRITE_BYTE_CMD 0x10
26 #define BLOCK_ERASE_CMD 0x20
27 #define CLEAR_STATUS_CMD 0x50
28 #define READ_STATUS_CMD 0x70
29 #define READ_DEVID_CMD 0x90
30 #define BLOCK_ERASE_CONFIRM_CMD 0xd0
31 #define READ_ARRAY_CMD 0xff
33 #define CLEARED_ARRAY_STATUS 0x00
36 STATIC UINT8
*mFlashBase
= NULL
;
37 STATIC UINTN mFdBlockSize
= 0;
38 STATIC UINTN mFdBlockCount
= 0;
42 QemuFlashConvertPointers (
46 EfiConvertPointer (0x0, (VOID
**) &mFlashBase
);
57 return mFlashBase
+ ((UINTN
)Lba
* mFdBlockSize
) + Offset
;
62 Determines if the QEMU flash memory device is present.
64 @retval FALSE The QEMU flash device is not present.
65 @retval TRUE The QEMU flash device is present.
74 BOOLEAN FlashDetected
;
81 FlashDetected
= FALSE
;
82 Ptr
= QemuFlashPtr (0, 0);
84 for (Offset
= 0; Offset
< mFdBlockSize
; Offset
++) {
85 Ptr
= QemuFlashPtr (0, Offset
);
87 if (ProbeUint8
!= CLEAR_STATUS_CMD
&&
88 ProbeUint8
!= READ_STATUS_CMD
&&
89 ProbeUint8
!= CLEARED_ARRAY_STATUS
) {
94 if (Offset
>= mFdBlockSize
) {
95 DEBUG ((EFI_D_INFO
, "QEMU Flash: Failed to find probe location\n"));
99 DEBUG ((EFI_D_INFO
, "QEMU Flash: Attempting flash detection at %p\n", Ptr
));
101 OriginalUint8
= *Ptr
;
102 *Ptr
= CLEAR_STATUS_CMD
;
104 if (OriginalUint8
!= CLEAR_STATUS_CMD
&&
105 ProbeUint8
== CLEAR_STATUS_CMD
) {
106 DEBUG ((EFI_D_INFO
, "QemuFlashDetected => FD behaves as RAM\n"));
107 *Ptr
= OriginalUint8
;
109 *Ptr
= READ_STATUS_CMD
;
111 if (ProbeUint8
== OriginalUint8
) {
112 DEBUG ((EFI_D_INFO
, "QemuFlashDetected => FD behaves as ROM\n"));
113 } else if (ProbeUint8
== READ_STATUS_CMD
) {
114 DEBUG ((EFI_D_INFO
, "QemuFlashDetected => FD behaves as RAM\n"));
115 *Ptr
= OriginalUint8
;
116 } else if (ProbeUint8
== CLEARED_ARRAY_STATUS
) {
117 DEBUG ((EFI_D_INFO
, "QemuFlashDetected => FD behaves as FLASH\n"));
118 FlashDetected
= TRUE
;
119 *Ptr
= READ_ARRAY_CMD
;
123 DEBUG ((EFI_D_INFO
, "QemuFlashDetected => %a\n",
124 FlashDetected
? "Yes" : "No"));
125 return FlashDetected
;
132 @param[in] Lba The starting logical block index to read from.
133 @param[in] Offset Offset into the block at which to begin reading.
134 @param[in] NumBytes On input, indicates the requested read size. On
135 output, indicates the actual number of bytes read
136 @param[in] Buffer Pointer to the buffer to read into.
150 // Only write to the first 64k. We don't bother saving the FTW Spare
151 // block into the flash memory.
153 if (Lba
>= mFdBlockCount
) {
154 return EFI_INVALID_PARAMETER
;
160 Ptr
= (UINT8
*) QemuFlashPtr (Lba
, Offset
);
162 CopyMem (Buffer
, Ptr
, *NumBytes
);
171 @param[in] Lba The starting logical block index to write to.
172 @param[in] Offset Offset into the block at which to begin writing.
173 @param[in] NumBytes On input, indicates the requested write size. On
174 output, indicates the actual number of bytes written
175 @param[in] Buffer Pointer to the data to write.
190 // Only write to the first 64k. We don't bother saving the FTW Spare
191 // block into the flash memory.
193 if (Lba
>= mFdBlockCount
) {
194 return EFI_INVALID_PARAMETER
;
200 Ptr
= QemuFlashPtr (Lba
, Offset
);
201 for (Loop
= 0; Loop
< *NumBytes
; Loop
++) {
202 *Ptr
= WRITE_BYTE_CMD
;
208 // Restore flash to read mode
211 *(Ptr
- 1) = READ_ARRAY_CMD
;
219 Erase a QEMU Flash block
221 @param Lba The logical block index to erase.
225 QemuFlashEraseBlock (
231 if (Lba
>= mFdBlockCount
) {
232 return EFI_INVALID_PARAMETER
;
235 Ptr
= QemuFlashPtr (Lba
, 0);
236 *Ptr
= BLOCK_ERASE_CMD
;
237 *Ptr
= BLOCK_ERASE_CONFIRM_CMD
;
243 Initializes QEMU flash memory support
245 @retval EFI_WRITE_PROTECTED The QEMU flash device is not present.
246 @retval EFI_SUCCESS The QEMU flash device is supported.
250 QemuFlashInitialize (
254 mFlashBase
= (UINT8
*)(UINTN
) PcdGet32 (PcdOvmfFdBaseAddress
);
255 mFdBlockSize
= PcdGet32 (PcdOvmfFirmwareBlockSize
);
256 ASSERT(PcdGet32 (PcdOvmfFirmwareFdSize
) % mFdBlockSize
== 0);
257 mFdBlockCount
= PcdGet32 (PcdOvmfFirmwareFdSize
) / mFdBlockSize
;
259 if (!QemuFlashDetected ()) {
260 return EFI_WRITE_PROTECTED
;