Skip to content

Commit 0637504

Browse files
committed
Fix GetCapabilities to return the right size
1 parent 01d39c7 commit 0637504

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

qiling/os/uefi/protocols/EfiSmmAccess2Protocol.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from ..ProcessorBind import *
1111
from ..UefiBaseType import *
1212
from ..PiMultiPhase import *
13-
from ..utils import write_int64, read_int64
13+
from .. import utils
1414

1515
# @see: MdePkg\Include\Pi\PiMultiPhase.h
1616
class EFI_MMRAM_DESCRIPTOR(STRUCT):
@@ -108,8 +108,23 @@ def hook_GetCapabilities(ql: Qiling, address: int, params):
108108
size = len(chunks) * EFI_SMRAM_DESCRIPTOR.sizeof()
109109
MmramMapSize = params["MmramMapSize"]
110110

111-
if read_int64(ql, MmramMapSize) < size:
112-
write_int64(ql, MmramMapSize, size)
111+
if utils.read_int64(ql, MmramMapSize) < size:
112+
# since the caller cannot predict how much memory would be required for storing
113+
# the memory map, this method is normally called twice. the first one passes a
114+
# zero size only to determine the expected size, then the caller allocates the
115+
# required amount of memory and call it again.
116+
#
117+
# our memory map is managed differently from the real one, and memory allocations
118+
# are likely to generate an additional "map block" (or two, if allocated somewhere
119+
# in the last free heap chunk). because the caller allocates a new memory chunk
120+
# between the two calls, that would cause the second call to always complain the
121+
# buffer is too small.
122+
#
123+
# to work around that, we have the first call return a larger number than it should
124+
# have, to compensate on the coming allocation.
125+
extra = 2 * EFI_SMRAM_DESCRIPTOR.sizeof()
126+
127+
utils.write_int64(ql, MmramMapSize, size + extra)
113128
return EFI_BUFFER_TOO_SMALL
114129

115130
MmramMap = params["MmramMap"]

0 commit comments

Comments
 (0)