[BOOT:BOOTLIB] Parse BCD GUID to application entry

This commit is contained in:
Quinn Stephens 2024-08-09 09:11:09 -04:00
parent 76e007584e
commit 2680e9b4c0
3 changed files with 52 additions and 95 deletions

View File

@ -71,10 +71,14 @@ typedef struct {
ULONG PlatformDataOffset;
} BOOT_INPUT_PARAMETERS, *PBOOT_INPUT_PARAMETERS;
#define BOOT_APPLICATION_ENTRY_SIGNATURE 0x544e4550415442
#define BOOT_APPLICATION_ENTRY_SIGNATURE 0x544e4550415442 /* "BTAPENT" */
#define BOOT_APPLICATION_ENTRY_BCD_IDENTIFIER_NOT_SET 0x01
typedef struct {
ULONGLONG Signature;
ULONG Attributes;
GUID BcdIdentifier;
} BOOT_APPLICATION_ENTRY, *PBOOT_APPLICATION_ENTRY;
#define BOOT_MEMORY_INFO_VERSION 1
@ -87,7 +91,8 @@ typedef struct {
ULONG BasePageOffset;
} BOOT_MEMORY_INFO, *PBOOT_MEMORY_INFO;
#define MEMORY_FLAG_CACHE_WB 0x08
#define MEMORY_ATTRIBUTE_CACHE_WB 0x08
#define MEMORY_TYPE_BOOT_APPLICATION 0xd0000002
typedef struct {
@ -96,7 +101,7 @@ typedef struct {
ULONGLONG BasePage;
ULONG Pages;
ULONG Flags;
ULONG Attributes;
ULONG Type;
} BOOT_MEMORY_DESCRIPTOR, *PBOOT_MEMORY_DESCRIPTOR;

View File

@ -13,6 +13,9 @@ Abstract:
--*/
#include <ntrtl.h>
#include <string.h>
#include <wchar.h>
#include "bootmgr.h"
#include "efi.h"
@ -30,7 +33,7 @@ EfiInitpCreateApplicationEntry (
IN PWCHAR LoadOptions,
IN ULONG LoadOptionsSize,
OUT PULONG BufferUsed,
OUT PBOOT_DEVICE *Device
OUT PBOOT_DEVICE *BootDevice
)
/*++
@ -57,7 +60,7 @@ Arguments:
BufferUsed - Returns the amount of buffer space used by the routine.
Device - Returns a pointer to the device the application was loaded from.
BootDevice - Returns a pointer to the device the application was loaded from.
Return Value:
@ -66,19 +69,49 @@ Return Value:
--*/
{
PWCHAR BcdOptionString;
BOOLEAN BcdIdentifierSet;
UNICODE_STRING UnicodeString;
*BufferUsed = 0;
*BootDevice = NULL;
BcdIdentifierSet = FALSE;
//
// Require enough space for the application entry.
//
if (BufferSize < sizeof(BOOT_APPLICATION_ENTRY)) {
*BufferUsed = 0;
return;
}
//
// Terminate load options string.
//
LoadOptionsSize /= sizeof(WCHAR);
if (LoadOptionsSize != 0 && wcsnlen(LoadOptions, LoadOptionsSize) == LoadOptionsSize) {
LoadOptions[LoadOptionsSize - 1] = L'\0';
}
//
// Set up application entry structure.
//
RtlZeroMemory(Entry, sizeof(BOOT_APPLICATION_ENTRY));
Entry->Signature = BOOT_APPLICATION_ENTRY_SIGNATURE;
*BufferUsed = sizeof(BOOT_APPLICATION_ENTRY);
//
// Parse BCD GUID if present.
//
if (LoadOptions != NULL && (BcdOptionString = wcsstr(LoadOptions, L"BCDOBJECT=")) != NULL) {
RtlInitUnicodeString(&UnicodeString, (PWCHAR)((PUCHAR)BcdOptionString + sizeof(L"BCDOBJECT=") - sizeof(UNICODE_NULL)));
if (NT_SUCCESS(RtlGUIDFromString(&UnicodeString, &Entry->BcdIdentifier))) {
BcdIdentifierSet = TRUE;
}
}
if (!BcdIdentifierSet) {
Entry->Attributes |= BOOT_APPLICATION_ENTRY_BCD_IDENTIFIER_NOT_SET;
}
//
// TODO: This routine is not fully implemented.
@ -86,10 +119,6 @@ Return Value:
(VOID)SystemTable;
(VOID)DevicePath;
(VOID)FilePath;
(VOID)LoadOptions;
(VOID)LoadOptionsSize;
(VOID)Device;
*BufferUsed = sizeof(BOOT_APPLICATION_ENTRY);
}
PBOOT_INPUT_PARAMETERS
@ -192,7 +221,7 @@ Return Value:
ScratchUsed += sizeof(BOOT_MEMORY_DESCRIPTOR);
MemoryDescriptor->BasePage = (UINTN)InputParameters->ImageBase >> PAGE_SHIFT;
MemoryDescriptor->Pages = ALIGN_UP(InputParameters->ImageSize, PAGE_SIZE) >> PAGE_SHIFT;
MemoryDescriptor->Flags = MEMORY_FLAG_CACHE_WB;
MemoryDescriptor->Attributes = MEMORY_ATTRIBUTE_CACHE_WB;
MemoryDescriptor->Type = MEMORY_TYPE_BOOT_APPLICATION;
//
@ -243,6 +272,13 @@ Return Value:
ScratchUsed += sizeof(BOOT_RETURN_DATA);
ReturnData->Version = BOOT_RETURN_DATA_VERSION;
//
// Set and validate total size.
//
InputParameters->Size = ScratchUsed;
if (InputParameters->Size > sizeof(EfiInitScratch)) {
return NULL;
}
return InputParameters;
}

View File

@ -1,84 +0,0 @@
/*++
Copyright (c) 2024, Quinn Stephens.
Provided under the BSD 3-Clause license.
Module Name:
rtl.c
Abstract:
Runtime library routines.
--*/
#include <nt.h>
PVOID
RtlCopyMemory (
PVOID Destination,
CONST PVOID Source,
ULONG Length
)
/*++
Routine Description:
Copies a block of memory from Source to Destination.
Arguments:
Destination - the address to copy to.
Source - the address to copy from.
Length - the number of bytes to copy.
Return Value:
Returns Destination.
--*/
{
for (ULONG Index = 0; Index < Length; Index++) {
((PCHAR)Destination)[Index] = ((PCHAR)Source)[Index];
}
return Destination;
}
PVOID
RtlZeroMemory (
PVOID Destination,
ULONG Length
)
/*++
Routine Description:
Sets a block of memory to zero.
Arguments:
Destination - the address to zero at.
Length - the number of bytes to set.
Return Value:
Returns Destination.
--*/
{
for (ULONG Index = 0; Index < Length; Index++) {
((PCHAR)Destination)[Index] = 0;
}
return Destination;
}