Commit graph

6787 commits

Author SHA1 Message Date
Aaron Pop
816b35fe52 MdeModulePkg/UefiHiiLib: Fix regression from 12828
12828 introduced an ASSERT in HiiGetBrowserData() that fires when
InternalHiiBrowserCallback() returns NULL. This is a valid return
value indicating the browser has no data for the requested variable,
and callers already handle this by checking the FALSE return value.

The ASSERT is incorrect because it triggers on a non-error path,
causing a crash when the browser callback legitimately returns no data.
Remove the unnecessary ASSERT while keeping the existing FALSE return
so callers continue to handle this case gracefully.

Cc: Qihang Gao <gaoqihang@loongson.cn>
Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-11 01:22:08 +00:00
Aaron Pop
d1c5d47014 MdeModulePkg: Fix unchecked return status
https://github.com/github/codeql/blob/codeql-cli-2.7.3/csharp/ql/src/API%20Abuse/UncheckedReturnValue.qhelp

When a function has a return status, it should
be checked to verify the function completed successfully.

Failing to check the return status can result in null pointer
dereferences or use of uninitialized variables.

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 11:34:09 +00:00
Aaron Pop
1cc0af9d6d MdeModulePkg: Fix missing NULL tests
https://github.com/github/codeql/blob/codeql-cli-2.7.3/cpp/ql/src/Critical/MissingNullTest.qhelp

For items which allocate memory, or get a pointer from another
structure, it is important to validate that the pointers
are not null before they are dereferenced.

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 11:34:09 +00:00
Aaron Pop
af24f366a4 MdeModulePkg: Fix comparison with wider widths
https://codeql.github.com/codeql-query-help/cpp/cpp-comparison-with-wider-type

If the narrow type (smaller range) is compared against a wide type
(larger range), the narrow value may overflow before reaching the wide
value. This can cause unexpected behavior, such as:

Infinite loops (loop condition never becomes false).
Incorrect logic (comparison results are misleading).

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 11:34:09 +00:00
Aaron Pop
72d0846c4c MdeModulePkg: Fix conditionally uninitialized variables
https://github.com/github/codeql/blob/codeql-cli-2.7.3/cpp/ql/src/Security/CWE/CWE-457/ConditionallyUninitializedVariable.qhelp

Some local variables, when going through a code path, can
end up uninitialized (using the value they had at the start
of the function). This is generally due to an error path
that can occur based on the library instances, or the
unchecked error (i.e. a allocation failing).

These variables should be initialized with a known value
that will result in the function being able to exit
gracefully.

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 08:59:15 +00:00
Aaron Pop
77cf8c8c10 MdeModulePkg: Fix missing NULL tests
https://github.com/github/codeql/blob/codeql-cli-2.7.3/cpp/ql/src/Critical/MissingNullTest.qhelp

For items which allocate memory, or get a pointer from another
structure, it is important to validate that the pointers
are not null before they are dereferenced.

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 08:59:15 +00:00
Aaron Pop
476b78bbad MdeModulePkg: Fix Comparison overflow
https://github.com/github/codeql/blob/codeql-cli-2.7.3/cpp/ql/src/Security/CWE/CWE-190/ComparisonWithWiderType.qhelp

Switch to using SafeUint16Add for calculating offsets into
block data. The data being used in the calculation comes from
config block strings, and there is no validation of the values
before the calculation occurs.

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 06:27:20 +00:00
Aaron Pop
1d63461c91 MdeModulePkg: Fix missing NULL tests
https://github.com/github/codeql/blob/codeql-cli-2.7.3/cpp/ql/src/Critical/MissingNullTest.qhelp

For items which allocate memory, or get a pointer from another
structure, it is important to validate that the pointers
are not null before they are dereferenced.

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 06:27:20 +00:00
Aaron Pop
8c66d98963 MdeModulePkg: Fix comparison with wider widths
https://codeql.github.com/codeql-query-help/cpp/cpp-comparison-with-wider-type

If the narrow type (smaller range) is compared against a wide type
(larger range), the narrow value may overflow before reaching the wide
value. This can cause unexpected behavior, such as:

Infinite loops (loop condition never becomes false).
Incorrect logic (comparison results are misleading).

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 06:27:20 +00:00
Aaron Pop
9b3ceeb254 MdeModulePkg: Fix missing NULL tests
https://github.com/github/codeql/blob/codeql-cli-2.7.3/cpp/ql/src/Critical/MissingNullTest.qhelp

For items which allocate memory, or get a pointer from another
structure, it is important to validate that the pointers
are not null before they are dereferenced.

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 05:09:41 +00:00
Aaron Pop
dfd0edeb4e MdeModulePkg: Fix missing NULL tests
https://github.com/github/codeql/blob/codeql-cli-2.7.3/cpp/ql/src/Critical/MissingNullTest.qhelp

For items which allocate memory, or get a pointer from another
structure, it is important to validate that the pointers
are not null before they are dereferenced.

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 03:07:16 +00:00
Aaron Pop
699382e342 MdeModulePkg: Fix unchecked return status
https://github.com/github/codeql/blob/codeql-cli-2.7.3/csharp/ql/src/API%20Abuse/UncheckedReturnValue.qhelp

When a function has a return status, it should
be checked to verify the function completed successfully.

Failing to check the return status can result in null pointer
dereferences or use of uninitialized variables.

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 03:07:16 +00:00
Aaron Pop
0f0515f71b MdeModulePkg: Fix unchecked return status
https://github.com/github/codeql/blob/codeql-cli-2.7.3/csharp/ql/src/API%20Abuse/UncheckedReturnValue.qhelp

When a function has a return status, it should
be checked to verify the function completed successfully.

Failing to check the return status can result in null pointer
dereferences or use of uninitialized variables.

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 02:13:24 +00:00
Aaron Pop
0bc1db4adf MdeModulePkg: Fix missing NULL tests
https://github.com/github/codeql/blob/codeql-cli-2.7.3/cpp/ql/src/Critical/MissingNullTest.qhelp

For items which allocate memory, or get a pointer from another
structure, it is important to validate that the pointers
are not null before they are dereferenced.

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 02:13:24 +00:00
Aaron Pop
bf0dc7d787 MdeModulePkg: Fix comparison with wider widths
https://codeql.github.com/codeql-query-help/cpp/cpp-comparison-with-wider-type

If the narrow type (smaller range) is compared against a wide type
(larger range), the narrow value may overflow before reaching the wide
value. This can cause unexpected behavior, such as:

Infinite loops (loop condition never becomes false).
Incorrect logic (comparison results are misleading).

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-08-05 02:13:24 +00:00
Gerd Hoffmann
3f97b8bc7f MdeModulePkg/SmbiosDxe: fix table length check
In case EntryPointStructure does not exist yet use a length of zero instead of
skipping the check altogether.  Fixes a heap overflow in the following code
flow in case the first smbios table installed is larger than
SMBIOS_TABLE_MAX_LENGTH.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2026-08-01 04:35:21 +00:00
Theo
bc71cc972d MdeModulePkg/NvmExpressHci.c: Save time when BIOS reset with NVMes
Dispatch Shutdown Notification to every NVMe first, then polling
every NVMe t omaake sure all NVMe's shutdown processing is
completed.This will help to save a lot time when BIOS trigger
reset for Servers whose have many NVMes. Tested on a platform
with AMD EPYC cpu with 26 NVMes, this method reduce reset time
from 3 minutes to 10 seconds.

Signed-off-by: Theo <theo.tao@foxmail.com>
2026-08-01 03:58:29 +00:00
Jeff Brasen
ca8de19382 MdeModulePkg: MemoryBins: make GoogleTest page-granularity aware
MemoryBinGoogleTest.PopulatesFromValidHob asserted fixed page counts for
each memory type.  PopulateMemoryTypeInformation, however, rounds the
runtime memory types (EfiReservedMemoryType, EfiACPIMemoryNVS,
EfiRuntimeServicesCode, EfiRuntimeServicesData) up to
RUNTIME_PAGE_ALLOCATION_GRANULARITY.  That granularity equals EFI_PAGE_SIZE
on IA32/X64, so the hard-coded values happened to match, but it is 64 KiB
on AArch64, where the same inputs round up to different page counts and the
test failed.

Compute the expected page counts with the same granularity rounding the
production code uses, so the test passes on all host architectures instead
of only x86.

Signed-off-by: Jeff Brasen <jbrasen@nvidia.com>
2026-07-31 15:16:47 +00:00
Kun Qin
47c48984c5 MdeModulePkg: Host Test: Adding AArch64 target
This change adds the AArch64 target for host based unit tests.

Signed-off-by: Kun Qin <kun.qin@microsoft.com>
2026-07-31 15:16:47 +00:00
Jared Pan
7c2ec06a93 MdeModulePkg/UsbBusDxe: Fix UsbPortReset might run into recursive loop
UsbSelectConfig will introduce the UsbConnectDriver call.
If this UsbPortReset is happened in the Usb device driver Start() routine and the device FW can not be recovered by PortReset, the UsbSelectConfig will introduce the recursive loop.

[Suggested solution]
Since UsbPortReset should not change the Bus Topology, the Reset flow should only SetAddress and reconfigure the device.

Signed-off-by: Marlboro Chuang <marlboro.chuang@dell.com>
Signed-off-by: Jared Pan <jared.pan@dell.com>
2026-07-31 10:54:54 +00:00
Michael Kubacki
99afb1ffb0 MdeModulePkg/HiiDatabaseDxe: Fix potential intrinsic error
In some VS22 versions, these code patterns (assiging the scalar in
a loop) have been found to be converted into calls to the `memcpy`
intrinsic. This change updates them to use CopyMem to avoid the
potential error.

Previous:
- MSVC version: 14.31.31103

New:
- MSVC version: 14.32.31326

Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
2026-07-31 01:16:29 +00:00
Aaron Pop
a1aab535ce MdeModulePkg: Fix missing NULL tests
https://github.com/github/codeql/blob/codeql-cli-2.7.3/cpp/ql/src/Critical/MissingNullTest.qhelp

For items which allocate memory, or get a pointer from another
structure, it is important to validate that the pointers
are not null before they are dereferenced.

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-07-30 19:10:07 +00:00
Aaron Pop
8f38acae6b MdeModulePkg: Fix comparison with wider widths
https://codeql.github.com/codeql-query-help/cpp/cpp-comparison-with-wider-type

If the narrow type (smaller range) is compared against a wide type
(larger range), the narrow value may overflow before reaching the wide
value. This can cause unexpected behavior, such as:

Infinite loops (loop condition never becomes false).
Incorrect logic (comparison results are misleading).

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
2026-07-30 19:10:07 +00:00
Michael Kubacki
df9111c2d3 Global: Set MarkdownLintCheck plugin to AuditOnly
In preparation for the MarkdownLintCheck plugin being added to the
repo, this change defaults the plugin to `AuditOnly` mode in each
package. This allows package maintainers to enable the plugin as they
see fit.

Continuous-integration-options: PatchCheck.ignore-multi-package
Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
2026-07-29 12:32:09 +00:00
Jared Pan
4d5f8e68a1 MdeModulePkg/UsbBusDxe: BOS Descriptor Check for SS Devices
Some SuperSpeed-capable devices may fall back to High-Speed
mode and cause subsequent commands to fail.

[Suggested Solution]
Check the BOS descriptor to verify SuperSpeed support and
trigger a port reset if needed to re-enumerate the device
properly.

Signed-off-by: Marlboro Chuang <marlboro.chuang@dell.com>
Signed-off-by: Jared Pan <jared.pan@dell.com>
2026-07-22 07:25:57 +00:00
DC-Damien
6a9c048ba5 MdeModulePkg/UsbBusPei: Use dynamic buffer for USB configuration data
USB devices whose configuration descriptor TotalLength exceeds 1024
bytes (e.g. IR cameras with large descriptor tables) previously hit
an EFI_DEVICE_ERROR hard-limit and failed to enumerate in PEI.

Replace the fixed array with a UINT8 * pointer and dynamically
allocate the exact amount of memory required via
PeiServicesAllocatePool() after the TotalLength is learned from the
initial 4-byte descriptor probe.

Signed-off-by: Damien Chen <damien.chen@dell.com>
2026-07-21 03:50:20 +00:00
Rebecca Cran
97665a4ef0 MdeModulePkg: Bump the default SMBIOS version to 3.8
Commit 7f505d377b in 2024 added the
Type 4 field SocketType. Bump the default SMBIOS version to 3.8
in order for the larger table size to be valid.

Signed-off-by: Rebecca Cran <rebecca@bsdio.com>
2026-07-21 00:02:39 +00:00
Richard Lyu
b1029265b1 MdeModulePkg/GptLib: Add host-based unit tests for malformed GPT input
Extend the GptLib host-based tests with negative cases that guard the
security hardening in PartitionValidGptTable(), PartitionCheckGptEntry()
and PartitionRestoreGptTable() against future regressions. These tests
exercise the shared parser, not the specific fix itself.

The new cases drive the parser with malformed GPT structures that an
attacker may present: bad signature/revision, header-size boundaries,
CRC corruption, MyLBA replay, zero/non-power-of-two entry sizes, LBA
multiplication overflow, out-of-range and overlapping entries, and
restore failure on write-protected media. The INF file header is
updated to note the added malformed coverage.

Signed-off-by: Richard Lyu <richard.lyu@suse.com>
2026-07-20 21:50:57 +00:00
Richard Lyu
77585e5004 MdeModulePkg/GptLib: Add host-based unit tests for valid GPT behavior
Add the positive-path host-based tests for the shared GptLib parser
(extracted as part of the parser security hardening), ensuring the
tightened checks in PartitionValidGptTable(), PartitionCheckGptEntry()
and PartitionRestoreGptTable() do not falsely reject well-formed GPTs.

The tests run against an in-memory mock disk and cover accepted
primary/backup headers, boundary but legal header/entry sizes, correct
entry-status flagging on valid entries, and primary/backup restore
round-trips.

Signed-off-by: Richard Lyu <richard.lyu@suse.com>
2026-07-20 21:50:57 +00:00
Richard Lyu
070c9026f7 MdeModulePkg/PartitionDxe: Abort on primary GPT recovery failure
When the primary GPT is invalid, PartitionInstallGptChildHandles()
restores it from the backup and re-validates it. Both the restore write
and the re-validation can fail (e.g. write-protected media, or a backup
AlternateLBA pointing beyond the device), yet the existing code only logs
the failure and parses partitions from a known-invalid PrimaryHeader.

Abort GPT processing when either the restore or the validation fails, so
partitions are only ever parsed from a validated primary GPT. The backup
recovery branch is left unchanged, as the primary is already validated.

A device with an unrecoverable primary GPT now installs no child handles
instead of using an invalid header. This keeps the table PartitionDxe uses
in sync with the one DxeTpm2MeasureBootLib measures into PCR[5].

Ref: https://seclists.org/oss-sec/2026/q2/727
Signed-off-by: Richard Lyu <richard.lyu@suse.com>
2026-07-20 21:50:57 +00:00
Richard Lyu
45732edfff MdeModulePkg/GptLib: Validate GPT header fields before use
PartitionValidGptTable() checked the signature, header CRC32, MyLBA, the
entry-array CRC32 and the entry-array size overflow, but not several other
UEFI-mandated GPT header constraints. DxeTpm2MeasureBootLib used to enforce
these via Tpm2SanitizeEfiPartitionTableHeader(); once it switched to this
shared parser, the checks were lost on the path.

Also reject a header unless Header.Revision is GPT_HEADER_REVISION_V1,
HeaderSize is at least the 92-byte minimum, NumberOfPartitionEntries is
non-zero, SizeOfPartitionEntry is 128 * 2^n, and PartitionEntryLBA *
BlockSize cannot overflow. The "entries lie before FirstUsableLBA" rule is
intentionally omitted, as this routine also validates the backup header
whose entry array follows the usable region.

This restores the validation the measurement path lost and, because GptLib
is shared, tightens PartitionDxe the same way: malformed headers are now
rejected and the parse and measure paths stay identical.

Ref: https://seclists.org/oss-sec/2026/q2/727
Signed-off-by: Richard Lyu <richard.lyu@suse.com>
2026-07-20 21:50:57 +00:00
Richard Lyu
00a865d595 MdeModulePkg/GptLib: Extract shareable GPT parser into a library
As reported in CVE-2024-13745 via oss-sec, DxeTpm2MeasureBootLib can
measure a partition table that differs from the one parsed by the
PartitionDxe driver.

To address this, the more complete GPT parsing logic from PartitionDxe
is extracted into a standalone GptLib library so it can be
shared between PartitionDxe and DxeTpm2MeasureBootLib. This ensures
that the exact same partition table measured into PCR[5] is the one
parsed and used by the system. PartitionDxe behavior is unchanged.

Ref: https://seclists.org/oss-sec/2026/q2/727
Signed-off-by: Richard Lyu <richard.lyu@suse.com>
2026-07-20 21:50:57 +00:00
Kun Qin
89c6073683 MdePkg,MdeModulePkg: ArmFfaLib: Expand to include first 4 registers
The direct message arguments stripped off the header, making the
underlying FF-A function interface to lose information when it comes to
certain return code, i.e. FFA_YIELD and FFA_INTERRUPT.

This change adds back the header field for this purpose so that the
callers can decide how to act on the corresponding return codes.

It then populates the header field for this purpose in FFA direct
message functions so that the callers can decide how to act on the
corresponding return codes.

Continuous-integration-options: PatchCheck.ignore-multi-package

Signed-off-by: Kun Qin <kun.qin@microsoft.com>
2026-07-20 19:13:15 +00:00
VarshitPandya
74b8bfd953 MdeModulePkg: Add definition for 64 bit ACPI GAS
Add a macro definition for defining ACPI Generic Address Space for QWORD
memory.

Signed-off-by: Varshit Pandya <varshit.pandya@arm.com>
2026-07-17 18:31:43 +00:00
Phil Noh
c70637de12 MdeModulePkg/PciBusDxe: Honor SpecificFlag for PMem64 in UpdatePciInfo
When UpdatePciInfo() downgrades a PciBarTypePMem64 BAR to a 32-bit type
via EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL, it unconditionally
assigns PciBarTypePMem32 regardless of SpecificFlag, placing the BAR in
the prefetchable bridge window even when the platform intended the
non-prefetchable window.

The ACPI resource descriptor's SpecificFlag field encodes the intended
prefetchability of the constrained resource, using the bit:
EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE.

Fix this by checking the bit in SpecificFlag to select PciBarTypePMem32 or
PciBarTypeMem32, consistent with DumpPpbPaddingResource() in
PciEnumeratorSupport.c that uses the bit as the sole discriminator
between the two 32-bit BAR types.

Signed-off-by: Phil Noh <Phil.Noh@amd.com>
2026-07-17 09:03:00 +00:00
Mingjie Shen
049dc848c4 MdeModulePkg: Fix incorrect EfiPciWidth* enum literals
In arguments of EFI_PCI_IO_PROTOCOL member functions, replace the
EfiPciWidth* enum literals from
EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH with the matching
EfiPciIoWidth* values from EFI_PCI_IO_PROTOCOL_WIDTH.

This keeps the call sites aligned with the protocol they actually use.
The old values were copied from EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL code,
so they obscured the intent of the calls and relied on an explicit
cast.

This mimics commit 8ba64a9a94 ("UefiPayloadPkg: Fix build failure with
CLANGPDB").

Generated by coccinelle script.

``` smpl
@initialize:python@
@@

def to_pci_io_width(name):
    return name.replace("EfiPciWidth", "EfiPciIoWidth", 1)

@normalize@
typedef EFI_PCI_IO_PROTOCOL;
typedef EDKII_PCI_DEVICE_PPI;
type T =~ "^EFI_PCI_IO_PROTOCOL_WIDTH$";
EFI_PCI_IO_PROTOCOL *x;
EDKII_PCI_DEVICE_PPI *y;
identifier bad =~ "EfiPciWidth(Uint|FifoUint|FillUint)(8|16|32|64)";
identifier top_op =~ "^(PollMem|PollIo|CopyMem)$";
identifier space =~ "^(Mem|Io|Pci)$";
identifier rw =~ "^(Read|Write)$";
fresh identifier good = script:python(bad) { to_pci_io_width(bad) };
expression first;
expression list rest;
@@
(
  x->top_op
|
  y->PciIo.top_op
|
  x->space.rw
|
  y->PciIo.space.rw
) (
  first,
- (T)bad
+ good
  ,
  rest
  )
```

Verified with:
- `build -p MdeModulePkg/MdeModulePkg.dsc -m MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf -a IA32 -b DEBUG -t GCC`
- `build -p MdeModulePkg/MdeModulePkg.dsc -m MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf -a X64 -b DEBUG -t GCC`

Signed-off-by: Mingjie Shen <shen497@purdue.edu>
2026-07-14 00:06:22 +00:00
Oliver Smith-Denny
c38340a0c1 MdeModulePkg: Dxe Core: Correct gMemoryTypeInformation Definition
Commit 43e306806e added support
to DXE Core for EfiUnacceptedMemoryType. However, it incorrectly
added EFI_GCD_MEMORY_TYPE_UNACCEPTED to gMemoryTypeInformation,
which is the GCD memory type that is associated with
EfiUnacceptedMemoryType. All other changes from that PR
appear correct.

This is corrected to the EFI memory type.

The Memory Bin Google Test copied this incorrect definition,
so it is updated as well.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
2026-07-09 12:46:51 +00:00
Mingjie Shen
b38d9eb7c6 MdeModulePkg/ArmFfaLib: Use EFI_PAGES_TO_SIZE
Replace manual page-size multiplication with EFI_PAGES_TO_SIZE
in the ArmFfaLib sources.

This commit mimics 3457388 and is generated by the following coccinelle
scipt:

```smpl
@pages_to_size@
expression PAGE_COUNT;
@@
- PcdGet64 (PAGE_COUNT) * EFI_PAGE_SIZE
+ EFI_PAGES_TO_SIZE (PcdGet64 (PAGE_COUNT))
```

Tested:
- stuart_ci_build -c .pytool/CISettings.py -p MdeModulePkg -a AARCH64 -t DEBUG TOOL_CHAIN_TAG=GCC
- stuart_ci_build -c .pytool/CISettings.py -p MdeModulePkg -a AARCH64 -t RELEASE,NO-TARGET TOOL_CHAIN_TAG=GCC

Signed-off-by: Mingjie Shen <shen497@purdue.edu>
2026-07-08 05:02:11 +00:00
Jared Pan
8c0dea946f MdeModulePkg/UsbBusDxe: Manufacturer String Descriptor Caching
Certain devices require immediate follow-up commands
after reading the LANGID string to fetch Manufacturer,
Product, or SerialNumber strings.

[Suggested Solution]
These strings are now cached after initial retrieval
to allow UsbIoGetStringDescriptor() to return them
directly, improving efficiency and stability.

Signed-off-by: Marlboro Chuang <marlboro.chuang@dell.com>
Signed-off-by: Jared Pan <jared.pan@dell.com>
2026-06-30 11:47:34 +00:00
Jared Pan
23ebccb46c MdeModulePkg/UsbBusDxe: Improve USB enumerating process
The patch enhances the USB enumeration process in EDK2 to improve compatibility with non-standards-compliant devices that may fail during standard enumeration sequences.
The suggested solution is based on USB specifications and references implementations from both Linux and Windows environments.

[Suggested solution]
- Integrated a retry mechanism to sequentially execute enumeration scripts, inspired by the enumeration flows of Windows, Linux, and EDK2. This improves robustness when handling corner-case devices.
- Do sanity check while the device report the device descriptor.
- AMD XHCI might need to wait for more time while sending the CLEAR_FEATURE reuqest.

Signed-off-by: Marlboro Chuang <marlboro.chuang@dell.com>
Signed-off-by: Jared Pan <jared.pan@dell.com>
2026-06-30 10:35:12 +00:00
Jiaqing Zhao
e3e93cf092 MdeModulePkg/PciBusDxe: Fix Mem64 BAR handling in IsPciDeviceRejected()
IsPciDeviceRejected() masks BAR value with 0xFFFFFFF0 before testing
the type bits (2:1) that mark a 64-bit memory BAR, essentially clears
them, making the 64-bit BAR code path unreachable and treated as if
it were 32-bit.

The function rejects a device when BAR looks unprogrammed by comparing
if its size mask equals its value. When a 64-bit BAR is mistaken for a
32-bit one, only its lower part is compared, possibly leading a valid
BAR being falsely rejected. For example, a 2G BAR with size mask
0x80000000 at 0x180000000 matches and the device is dropped.

This code runs during light enumeration (PciEnumeratorLight), used when
PCI resources are already assigned by the platform (e.g. Xen HVM, where
hvmloader programs the BARs). The rejected device never receives a
PciIo handle, so no driver can bind to it. For example, a virtio-vga
with a 64-bit BAR vanishes under OVMF on Xen, leaving the guest with
no graphics output.

Fix by testing the type bits on the raw BAR value before masking.

Signed-off-by: Jiaqing Zhao <Zhao.Jiaqing@amd.com>
2026-06-29 02:25:40 +00:00
Michael Kubacki
16b41f7d02 MdeModulePkg: Follow pragma once coding convention
Update recent changes in MdeModulePkg to follow the latest EDK II
C Coding Standards Specification (5.3) to use '#pragma once' instead
of traditional macro-based include guards in header files.

https://tianocore-docs.github.io/edk2-CCodingStandardsSpecification/draft/5_source_files/53_include_files.html#53-include-files

Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
2026-06-25 19:40:31 +00:00
Oliver Smith-Denny
a5fcc0a6ac MdeModulePkg: CxlDxe: Fix IA32 Build Break
CxlDxe is currently compiled as part of the MdeModulePkg
IA32 CI. When running CI with VS2022 version 14.44.35228.0,
the CI build fails with:

CxlDxe.lib(CxlDxe.obj) : unresolved external symbol __allmul
CxlDxe(CxlDxe.obj) : unresolved external symbol __allshl

CxlDxe is not intended to run on IA32 DXE systems, as such
systems are legacy, but until edk2 drops build support for
IA32 DXE (or at least CI for it), the build needs to work.

This fixes the 64 bit multiplication/shifting that occurs
in CxlDxe to use the BaseLib functions that avoid the
compiler intrinsics.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
2026-06-25 15:15:38 +00:00
Dongyan Qian
5ac1b95b3e MdeModulePkg/PciBusDxe: Scan funcs when func 0 is absent
The PCI specification normally requires function 0 to be present
before functions 1 through 7 are used. PciBusDxe therefore stops
scanning a slot when probing function 0 fails.

Some virtualized PCI topologies may expose selected non-zero functions
to a guest while function 0 is hidden. Add an opt-in Feature PCD so a
platform can continue collecting device information for functions 1
through 7 when function 0 is absent.

The default remains FALSE, so existing platform behavior is unchanged
unless the platform explicitly enables the PCD.

Signed-off-by: Dongyan Qian <qiandongyan@loongson.cn>
2026-06-25 13:09:52 +00:00
Oliver Smith-Denny
1b39cb1d81 MdeModulePkg: MemoryBins: Add GoogleTest and README
This commit adds unit tests and documentation for the Memory Bin
feature.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
2026-06-24 15:53:58 +00:00
Oliver Smith-Denny
cf1a2a541e MdeModulePkg: DxeCore: Update Memory Statistics from PEI
If memory bins are enabled for PEI, PEI will produce
Memory Allocation HOBs marked with gEfiMemoryTypeInformationGuid.
If these exist, DXE core will now process the stats from them
to have accurate numbers.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
2026-06-24 15:53:58 +00:00
Oliver Smith-Denny
2e7aa4810b MdeModulePkg: PeiCore: Add Memory Bin Support to Post-Mem PEI
This commit adds opt-in support for post-mem PEI memory bins.
See the README for full details.

MemoryBin.c is duplicated to PeiCore per request.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
2026-06-24 15:53:58 +00:00
Oliver Smith-Denny
840d07abb1 MdeModulePkg: Add PCD to Enable Memory Bin Support in PEI
This adds a PCD, FALSE by default, that enables the memory
bin feature in PEI.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
2026-06-24 15:53:58 +00:00
Oliver Smith-Denny
eab3300622 MdeModulePkg: Dxe Core: Split Memory Bin Logic Into Separate File
This commits splits out logic currently contained in
Gcd.c and Page.c to a new file called
MemoryBin.c. This is set up in preparation to
add support to PEI for memory bins (an S4 resume stability
feature).

MemoryBin.c takes all global state in as parameters so that
DXE core can use globals and PEI core can use HOBs.

There is no logic change here, just consolidating the
functionality to share with PEI.

This was requested not to be a library.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
2026-06-24 15:53:58 +00:00
Oliver Smith-Denny
a196ef6db5 MdeModulePkg: Dxe Core: Add Resource Desc Hob Generation
This commit adds a parameter to the memory bin allocation function
to tell it whether it should create the Resource Descriptor HOB
owned by gMemoryTypeInformationGuid. This will be used by PEI to
tell DXE where the memory bins are.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
2026-06-24 15:53:58 +00:00