Skip to content

Commit

Permalink
Put log entries into HOBs in PEI during log buffer initialization (#466)
Browse files Browse the repository at this point in the history
# Preface

Please ensure you have read the [contribution
docs](https://github.com/microsoft/mu/blob/master/CONTRIBUTING.md) prior
to submitting the pull request. In particular,
[pull request
guidelines](https://github.com/microsoft/mu/blob/master/CONTRIBUTING.md#pull-request-best-practices).

## Description

The current implementation will directly allocation pages through PEI
services when Advanced logger is not initialized. This cause an issue of
permanent loop if/when there are prints from the allocation routine.

This change will create a hob during this interim state and log messages
to hobs temporarily and coalesce the hobs after the allocation is
finalized. This will reduce the print restriction down to hob creation
only.

For each item, place an "x" in between `[` and `]` if true. Example:
`[x]`.
_(you can also check items in the GitHub UI)_

- [x] Impacts functionality?
- **Functionality** - Does the change ultimately impact how firmware
functions?
- Examples: Add a new library, publish a new PPI, update an algorithm,
...
- [ ] Impacts security?
- **Security** - Does the change have a direct security impact on an
application,
    flow, or firmware?
  - Examples: Crypto algorithm change, buffer overflow fix, parameter
    validation improvement, ...
- [ ] Breaking change?
- **Breaking change** - Will anyone consuming this change experience a
break
    in build or boot behavior?
- Examples: Add a new library class, move a module to a different repo,
call
    a function in a new library class in a pre-existing module, ...
- [ ] Includes tests?
  - **Tests** - Does the change include any explicit test code?
  - Examples: Unit tests, integration tests, robot tests, ...
- [ ] Includes documentation?
- **Documentation** - Does the change contain explicit documentation
additions
    outside direct code modifications (and comments)?
- Examples: Update readme file, add feature readme file, link to
documentation
    on an a separate Web page, ...

## How This Was Tested

This is tested on QEMU SBSA as well as Q35. Both booted to UEFI shell.

## Integration Instructions

N/A
  • Loading branch information
kuqin12 authored Apr 17, 2024
1 parent 3a9579a commit b748acb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
10 changes: 10 additions & 0 deletions AdvLoggerPkg/AdvLoggerPkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@
#
gAdvancedLoggerPreDxeLogsGuid = { 0x751fc006, 0x5804, 0x440d, { 0x8b, 0x15, 0x61, 0x8c, 0xf6, 0x56, 0xae, 0x76 } }

## GUID for specifying Advanced logger interim hob, which is used to indicate whether the logger in the
# interim phase of logger buffer being initialized.
#
gAdvancedLoggerInterimHobGuid = { 0x36587034, 0xfcde, 0x45b4, { 0x9a, 0x2b, 0xd5, 0xc4, 0xc1, 0x39, 0x96, 0x5a } }

## GUID for specifying Advanced logger interim buffer hob, which is used to carry interim log entries during the
# interim phase of being initialized.
#
gAdvancedLoggerInterimBufHobGuid = { 0x4c4f8c0b, 0xbe54, 0x49b3, { 0xba, 0x54, 0x33, 0xbf, 0x14, 0x47, 0x75, 0x24 } }

[Ppis]
## Advanced Logger Ppi - Communication from PEIM to PEI_CORE library implementation
#
Expand Down
80 changes: 69 additions & 11 deletions AdvLoggerPkg/Library/AdvancedLoggerLib/PeiCore/AdvancedLoggerLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
#include <Base.h>
#include <Uefi.h>

#include <Protocol/AdvancedLogger.h>
#include <AdvancedLoggerInternal.h>
#include <AdvancedLoggerInternalProtocol.h>
#include <Library/AdvancedLoggerAccessLib.h>

/**
Including the PeiMain.h from PeiCore in order to access the Platform Blob data member.
Expand Down Expand Up @@ -440,16 +443,20 @@ AdvancedLoggerGetLoggerInfo (
VOID
)
{
UINTN BufferSize;
EFI_HOB_GUID_TYPE *GuidHob;
PEI_CORE_INSTANCE *PeiCoreInstance;
ADVANCED_LOGGER_INFO *LoggerInfo;
ADVANCED_LOGGER_INFO *LoggerInfoSec;
ADVANCED_LOGGER_PTR *LogPtr;
EFI_PHYSICAL_ADDRESS NewLoggerInfo;
UINTN Pages;
CONST EFI_PEI_SERVICES **PeiServices;
EFI_STATUS Status;
UINTN BufferSize;
EFI_HOB_GUID_TYPE *GuidHob;
EFI_HOB_GUID_TYPE *GuidHobInterim;
EFI_HOB_GUID_TYPE *GuidHobInterimBuf;
PEI_CORE_INSTANCE *PeiCoreInstance;
ADVANCED_LOGGER_INFO *LoggerInfo;
ADVANCED_LOGGER_INFO *LoggerInfoSec;
ADVANCED_LOGGER_PTR *LogPtr;
EFI_PHYSICAL_ADDRESS NewLoggerInfo;
UINTN Pages;
CONST EFI_PEI_SERVICES **PeiServices;
EFI_STATUS Status;
EFI_MEMORY_TYPE Type;
ADVANCED_LOGGER_MESSAGE_ENTRY_V2 *LogEntry;

// Try to do the minimum work at the start of this function as this
// is called quite often.
Expand Down Expand Up @@ -492,6 +499,40 @@ AdvancedLoggerGetLoggerInfo (
}
}

GuidHobInterim = GetFirstGuidHob (&gAdvancedLoggerInterimHobGuid);
if (GuidHobInterim != NULL) {
// In the middle of initialization, save the log to the interim hobs
Status = PeiServicesCreateHob (
EFI_HOB_TYPE_GUID_EXTENSION,
(UINT16)(sizeof (EFI_HOB_GUID_TYPE) + sizeof (ADVANCED_LOGGER_INFO) + ADVANCED_LOGGER_MAX_MESSAGE_SIZE),
(VOID **)&GuidHobInterimBuf
);
if (EFI_ERROR (Status)) {
return NULL;
}

LoggerInfo = (ADVANCED_LOGGER_INFO *)GET_GUID_HOB_DATA (GuidHobInterimBuf);
BufferSize = sizeof (ADVANCED_LOGGER_INFO) + ADVANCED_LOGGER_MAX_MESSAGE_SIZE;
ZeroMem ((VOID *)LoggerInfo, BufferSize);
LoggerInfo->Signature = ADVANCED_LOGGER_SIGNATURE;
LoggerInfo->Version = ADVANCED_LOGGER_VERSION;
LoggerInfo->LogBuffer = PA_FROM_PTR (LoggerInfo + 1);
LoggerInfo->LogBufferSize = (UINT32)(BufferSize - sizeof (ADVANCED_LOGGER_INFO));
LoggerInfo->LogCurrent = LoggerInfo->LogBuffer;
LoggerInfo->HwPrintLevel = FixedPcdGet32 (PcdAdvancedLoggerHdwPortDebugPrintErrorLevel);
AdvancedLoggerHdwPortInitialize ();
CopyGuid (&GuidHobInterimBuf->Name, &gAdvancedLoggerInterimBufHobGuid);
LoggerInfo->HdwPortInitialized = TRUE;
return LoggerInfo;
} else {
Status = PeiServicesCreateHob (
EFI_HOB_TYPE_GUID_EXTENSION,
(UINT16)(sizeof (EFI_HOB_GUID_TYPE)),
(VOID **)&GuidHobInterim
);
CopyGuid (&GuidHobInterim->Name, &gAdvancedLoggerInterimHobGuid);
}

//
// No Logger Info - this must be the time to allocate a new LoggerInfo and save
// the pointer in the PeiCoreInstance.
Expand All @@ -516,14 +557,17 @@ AdvancedLoggerGetLoggerInfo (

if (FeaturePcdGet (PcdAdvancedLoggerPeiInRAM)) {
Pages = FixedPcdGet32 (PcdAdvancedLoggerPages);
Type = EfiReservedMemoryType;
} else {
Pages = FixedPcdGet32 (PcdAdvancedLoggerPreMemPages);
// This is to avoid the interim buffer being allocated to consume 64KB on ARM64 platforms.
Type = EfiBootServicesData;
}

BufferSize = EFI_PAGES_TO_SIZE (Pages);

Status = PeiServicesAllocatePages (
EfiReservedMemoryType,
Type,
Pages,
&NewLoggerInfo
);
Expand Down Expand Up @@ -561,6 +605,20 @@ AdvancedLoggerGetLoggerInfo (
UpdateSecLoggerInfo (LoggerInfo);
}

//
// Check to see if we have anything in the interim buffer
//
GuidHobInterimBuf = GetFirstGuidHob (&gAdvancedLoggerInterimBufHobGuid);
while (GuidHobInterimBuf != NULL) {
//
// If we have an interim buffer, copy it to the new buffer
//
LoggerInfo = (ADVANCED_LOGGER_INFO *)GET_GUID_HOB_DATA (GuidHobInterimBuf);
LogEntry = (ADVANCED_LOGGER_MESSAGE_ENTRY_V2 *)(UINTN)LoggerInfo->LogBuffer;
AdvancedLoggerMemoryLoggerWrite (LogEntry->DebugLevel, LogEntry->MessageText, LogEntry->MessageLen);
GuidHobInterimBuf = GetNextGuidHob (&gAdvancedLoggerInterimHobGuid, GuidHobInterimBuf);
}

//
// Publish the Advanced Logger Ppi
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
[Guids]
gAdvancedLoggerHobGuid
gEfiFirmwareFileSystem2Guid
gAdvancedLoggerInterimHobGuid
gAdvancedLoggerInterimBufHobGuid

[Ppis]
gAdvancedLoggerPpiGuid ## PRODUCES
Expand Down

0 comments on commit b748acb

Please sign in to comment.