Compare commits
30 Commits
LazyWriter
...
ef1ac515dd
Author | SHA1 | Date | |
---|---|---|---|
ef1ac515dd | |||
bbd8f475bb | |||
caec9bc42e | |||
1581638c26 | |||
664bf6d713 | |||
76b01cfb00 | |||
4dbb5235e3
|
|||
dd7ae11851 | |||
7125a17aca | |||
de9501aee9 | |||
e61d0f5155 | |||
b8afb1aad4 | |||
fb0e36f5cf | |||
c091f7ef59 | |||
3759f7dbfe | |||
b96b3d769c | |||
7aac701cc1 | |||
77d2f84e97 | |||
b870d5a015 | |||
5235ebdfa4 | |||
874d95ae4c | |||
8f56881d02 | |||
0743fa0106 | |||
fd670ace0d | |||
9b66f166d6 | |||
a6aa028e5c | |||
ccb62c05f5 | |||
1c2c797365 | |||
08d017171d
|
|||
d0d6097d57
|
5
.gitignore
vendored
5
.gitignore
vendored
@@ -9,6 +9,7 @@
|
||||
Makefile
|
||||
/BUILD/
|
||||
/UNUSED/
|
||||
/DISK/
|
||||
|
||||
# Prerequisites
|
||||
*.d
|
||||
@@ -43,3 +44,7 @@ Makefile
|
||||
*.out
|
||||
*.app
|
||||
|
||||
# Disk image files
|
||||
*.img
|
||||
*.iso
|
||||
*.fd
|
||||
|
56
BOOT/ENVIRON/APP/BOOTMGR/bcd.c
Normal file
56
BOOT/ENVIRON/APP/BOOTMGR/bcd.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/*++
|
||||
|
||||
Copyright (c) 2024, Quinn Stephens.
|
||||
Provided under the BSD 3-Clause license.
|
||||
|
||||
Module Name:
|
||||
|
||||
bcd.c
|
||||
|
||||
Abstract:
|
||||
|
||||
BCD (Boot Configuration Data, aka Boot Data Store) routines.
|
||||
|
||||
--*/
|
||||
|
||||
#include "bootlib.h"
|
||||
|
||||
NTSTATUS
|
||||
BmOpenDataStore (
|
||||
IN OUT PHANDLE DataStore
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Opens the boot configuration data store.
|
||||
|
||||
Arguments:
|
||||
|
||||
DataStore - pointer to memory to put the data store handle in.
|
||||
|
||||
Return Value:
|
||||
|
||||
STATUS_SUCCESS if successful,
|
||||
Other NTSTATUS value on failure.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
*DataStore = INVALID_HANDLE_VALUE;
|
||||
|
||||
/*
|
||||
NTSTATUS Status;
|
||||
PBOOT_DEVICE Device;
|
||||
PWSTR FilePath;
|
||||
BOOLEAN FilePathSet;
|
||||
|
||||
Device = NULL;
|
||||
FilePath = NULL;
|
||||
FilePathSet = FALSE;
|
||||
|
||||
return BmGetDataStorePath(&Device, &FilePath, &FilePathSet);
|
||||
*/
|
||||
return STATUS_SUCCESS;
|
||||
}
|
@@ -41,17 +41,27 @@ Return Value:
|
||||
{
|
||||
NTSTATUS Status;
|
||||
BOOT_LIBRARY_PARAMETERS LibraryParameters;
|
||||
HANDLE DataStore;
|
||||
|
||||
LibraryParameters.Flags = 0;
|
||||
LibraryParameters.MinimumPageAllocation = 16;
|
||||
|
||||
//
|
||||
// Initialize the boot library.
|
||||
//
|
||||
Status = BlInitializeLibrary(InputParameters, &LibraryParameters);
|
||||
if (!NT_SUCCESS(Status)) {
|
||||
ConsolePrintf(L"BlInitializeLibrary() failed: 0x%x\r\n", Status);
|
||||
goto Exit;
|
||||
}
|
||||
|
||||
//
|
||||
// Open the boot data store.
|
||||
//
|
||||
(VOID)BmOpenDataStore(&DataStore);
|
||||
|
||||
while (TRUE);
|
||||
|
||||
Exit:
|
||||
BlDestroyLibrary();
|
||||
return Status;
|
||||
|
@@ -21,8 +21,55 @@ Abstract:
|
||||
|
||||
typedef struct {
|
||||
ULONG Flags;
|
||||
ULONG TranslationType;
|
||||
ULONG MinimumPageAllocation;
|
||||
} BOOT_LIBRARY_PARAMETERS, *PBOOT_LIBRARY_PARAMETERS;
|
||||
|
||||
VOID
|
||||
ConsolePrint (
|
||||
IN PWSTR String
|
||||
);
|
||||
|
||||
VOID
|
||||
ConsolePrintf (
|
||||
IN PWSTR Format,
|
||||
...
|
||||
);
|
||||
|
||||
//
|
||||
// Enable/disable debug printing.
|
||||
//
|
||||
#ifdef _DEBUG
|
||||
#define DebugPrint(String) ConsolePrint(String)
|
||||
#define DebugPrintf(Format, ...) ConsolePrintf(Format, __VA_ARGS__)
|
||||
#else
|
||||
#define DebugPrint(String)
|
||||
#define DebugPrintf(Format, ...)
|
||||
#endif
|
||||
|
||||
ULONG
|
||||
BlGetBootOptionSize (
|
||||
IN PBOOT_APPLICATION_OPTION Option
|
||||
);
|
||||
|
||||
ULONG
|
||||
BlGetBootOptionListSize (
|
||||
IN PBOOT_APPLICATION_OPTION Options
|
||||
);
|
||||
|
||||
NTSTATUS
|
||||
BlpFwInitialize (
|
||||
IN ULONG Stage,
|
||||
IN PBOOT_FIRMWARE_DATA FirmwareData
|
||||
);
|
||||
|
||||
NTSTATUS
|
||||
BlpMmInitialize (
|
||||
IN PBOOT_MEMORY_INFO MemoryInfo,
|
||||
IN ULONG TranslationType,
|
||||
IN PBOOT_LIBRARY_PARAMETERS LibraryParameters
|
||||
);
|
||||
|
||||
NTSTATUS
|
||||
BlInitializeLibrary (
|
||||
IN PBOOT_INPUT_PARAMETERS InputParameters,
|
||||
|
@@ -28,17 +28,18 @@ Abstract:
|
||||
#define BOOT_MACHINE_TYPE IMAGE_FILE_MACHINE_I386
|
||||
#endif
|
||||
|
||||
//
|
||||
// No address translation.
|
||||
//
|
||||
#define BOOT_TRANSLATION_TYPE 0
|
||||
|
||||
//
|
||||
// Use EFI page size.
|
||||
//
|
||||
#define PAGE_SIZE EFI_PAGE_SIZE
|
||||
#define PAGE_SHIFT EFI_PAGE_SHIFT
|
||||
|
||||
//
|
||||
// Address translation types.
|
||||
//
|
||||
#define BOOT_TRANSLATION_TYPE_NONE 0
|
||||
#define BOOT_TRANSLATION_TYPE_MAX 1
|
||||
|
||||
#define BOOT_INPUT_PARAMETERS_SIGNATURE 0x50504120544f4f42 /* "BOOT APP" */
|
||||
#define BOOT_INPUT_PARAMETERS_VERSION 2
|
||||
|
||||
@@ -67,18 +68,35 @@ typedef struct {
|
||||
ULONG BootDeviceOffset;
|
||||
ULONG FirmwareDataOffset;
|
||||
ULONG ReturnDataOffset;
|
||||
|
||||
ULONG PlatformDataOffset;
|
||||
} BOOT_INPUT_PARAMETERS, *PBOOT_INPUT_PARAMETERS;
|
||||
|
||||
#define BOOT_APPLICATION_ENTRY_SIGNATURE 0x544e4550415442 /* "BTAPENT" */
|
||||
typedef struct {
|
||||
ULONG Type;
|
||||
ULONG DataOffset;
|
||||
ULONG DataSize;
|
||||
ULONG OtherOptionsOffset;
|
||||
ULONG NextOptionOffset;
|
||||
BOOLEAN IsInvalid;
|
||||
UCHAR Unknown[3];
|
||||
} BOOT_APPLICATION_OPTION, *PBOOT_APPLICATION_OPTION;
|
||||
|
||||
#define BOOT_APPLICATION_ENTRY_BCD_IDENTIFIER_NOT_SET 0x01
|
||||
#define BOOT_INPUT_APPLICATION_ENTRY_SIGNATURE 0x544e4550415442 /* "BTAPENT" */
|
||||
|
||||
#define BOOT_INPUT_APPLICATION_ENTRY_NO_BCD_IDENTIFIER 0x01
|
||||
|
||||
typedef struct {
|
||||
ULONGLONG Signature;
|
||||
ULONG Attributes;
|
||||
GUID BcdIdentifier;
|
||||
UCHAR Unknown[16];
|
||||
BOOT_APPLICATION_OPTION Options;
|
||||
} BOOT_INPUT_APPLICATION_ENTRY, *PBOOT_INPUT_APPLICATION_ENTRY;
|
||||
|
||||
typedef struct {
|
||||
ULONG Attributes;
|
||||
GUID BcdIdentifier;
|
||||
PBOOT_APPLICATION_OPTION Options;
|
||||
} BOOT_APPLICATION_ENTRY, *PBOOT_APPLICATION_ENTRY;
|
||||
|
||||
#define BOOT_MEMORY_INFO_VERSION 1
|
||||
@@ -103,7 +121,20 @@ typedef struct {
|
||||
|
||||
ULONG Attributes;
|
||||
ULONG Type;
|
||||
} BOOT_MEMORY_DESCRIPTOR, *PBOOT_MEMORY_DESCRIPTOR;
|
||||
} MEMORY_DESCRIPTOR, *PMEMORY_DESCRIPTOR;
|
||||
|
||||
typedef enum {
|
||||
MDL_TYPE_PHYSICAL,
|
||||
MDL_TYPE_VIRTUAL
|
||||
} MEMORY_DESCRIPTOR_LIST_TYPE;
|
||||
|
||||
typedef struct {
|
||||
LIST_ENTRY ListEntry;
|
||||
|
||||
PLIST_ENTRY Head;
|
||||
PLIST_ENTRY Current;
|
||||
MEMORY_DESCRIPTOR_LIST_TYPE Type;
|
||||
} MEMORY_DESCRIPTOR_LIST, *PMEMORY_DESCRIPTOR_LIST;
|
||||
|
||||
#define BOOT_FIRMWARE_DATA_VERSION 2
|
||||
|
||||
@@ -123,9 +154,125 @@ typedef struct {
|
||||
} BOOT_RETURN_DATA, *PBOOT_RETURN_DATA;
|
||||
|
||||
typedef struct {
|
||||
LARGE_INTEGER ImageBase;
|
||||
ULONG ImageSize;
|
||||
ULONG ImageOffset;
|
||||
} BOOT_RAMDISK_IDENTIFIER, *PBOOT_RAMDISK_IDENTIFIER;
|
||||
|
||||
#define BOOT_HARDDRIVE_PARTITION_TYPE_GPT 0x00
|
||||
#define BOOT_HARDDRIVE_PARTITION_TYPE_MBR 0x01
|
||||
#define BOOT_HARDDRIVE_PARTITION_TYPE_RAW 0x02
|
||||
|
||||
typedef struct {
|
||||
ULONG PartitionType;
|
||||
|
||||
union {
|
||||
struct {
|
||||
ULONG Signature;
|
||||
} Mbr;
|
||||
|
||||
struct {
|
||||
GUID Signature;
|
||||
} Gpt;
|
||||
|
||||
struct {
|
||||
ULONG DriveNumber;
|
||||
} Raw;
|
||||
};
|
||||
} BOOT_HARDDRIVE_IDENTIFIER, *PBOOT_HARDDRIVE_IDENTIFIER;
|
||||
|
||||
typedef struct {
|
||||
ULONG DriveNumber;
|
||||
} BOOT_CDROM_IDENTIFIER, *PBOOT_CDROM_IDENTIFIER;
|
||||
|
||||
#define BOOT_BLOCK_DEVICE_TYPE_HARDDRIVE 0x00
|
||||
#define BOOT_BLOCK_DEVICE_TYPE_CDROM 0x02
|
||||
#define BOOT_BLOCK_DEVICE_TYPE_RAMDISK 0x03
|
||||
|
||||
typedef struct {
|
||||
ULONG Type;
|
||||
|
||||
union {
|
||||
BOOT_RAMDISK_IDENTIFIER Ramdisk;
|
||||
BOOT_HARDDRIVE_IDENTIFIER Harddrive;
|
||||
BOOT_CDROM_IDENTIFIER Cdrom;
|
||||
};
|
||||
} BOOT_BLOCK_IDENTIFIER, *PBOOT_BLOCK_IDENTIFIER;
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
struct {
|
||||
ULONG PartitionNumber;
|
||||
} Mbr;
|
||||
|
||||
struct {
|
||||
GUID PartitionIdentifier;
|
||||
} Gpt;
|
||||
|
||||
struct {
|
||||
ULONG BootEntry;
|
||||
} ElTorito;
|
||||
};
|
||||
|
||||
BOOT_BLOCK_IDENTIFIER Parent;
|
||||
} BOOT_PARTITION_IDENTIFIER, *PBOOT_PARTITION_IDENTIFIER;
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
struct {
|
||||
PVOID PartitionOffset;
|
||||
} Mbr;
|
||||
|
||||
struct {
|
||||
GUID PartitionIdentifier;
|
||||
} Gpt;
|
||||
|
||||
struct {
|
||||
ULONG BootEntry;
|
||||
} ElTorito;
|
||||
};
|
||||
|
||||
BOOT_BLOCK_IDENTIFIER Parent;
|
||||
} BOOT_PARTITION_IDENTIFIER_EX, *PBOOT_PARTITION_IDENTIFIER_EX;
|
||||
|
||||
#define BOOT_DEVICE_TYPE_BLOCK 0x00
|
||||
#define BOOT_DEVICE_TYPE_PARTITION 0x02
|
||||
#define BOOT_DEVICE_TYPE_PARTITION_EX 0x06
|
||||
|
||||
#define BOOT_DEVICE_ATTRIBUTE_NO_PARENT_SIGNATURE 0x04
|
||||
|
||||
typedef struct {
|
||||
ULONG Type;
|
||||
ULONG Attributes;
|
||||
ULONG Size;
|
||||
ULONG Pad;
|
||||
|
||||
union {
|
||||
BOOT_BLOCK_IDENTIFIER Block;
|
||||
BOOT_PARTITION_IDENTIFIER Partition;
|
||||
BOOT_PARTITION_IDENTIFIER_EX PartitionEx;
|
||||
};
|
||||
} BOOT_DEVICE, *PBOOT_DEVICE;
|
||||
|
||||
#define BCDE_DATA_FORMAT_MASK 0x0F000000
|
||||
|
||||
#define BCDE_DATA_FORMAT_DEVICE 1
|
||||
|
||||
typedef enum {
|
||||
BCDE_DATA_TYPE_APPLICATION_DEVICE = 0x11000001,
|
||||
BCDE_DATA_TYPE_APPLICATION_PATH = 0x22000002
|
||||
} BCDE_DATA_TYPE;
|
||||
|
||||
typedef struct {
|
||||
GUID OtherOptions;
|
||||
BOOT_DEVICE Device;
|
||||
} BCDE_DEVICE, *PBCDE_DEVICE;
|
||||
|
||||
NTSTATUS
|
||||
BmOpenDataStore (
|
||||
IN OUT PHANDLE DataStore
|
||||
);
|
||||
|
||||
NTSTATUS
|
||||
BmMain (
|
||||
IN PBOOT_INPUT_PARAMETERS InputParameters
|
||||
|
@@ -24,8 +24,40 @@ typedef struct _EFI_DEVICE_PATH_PROTOCOL {
|
||||
typedef struct _EFI_DEVICE_PATH_PROTOCOL _EFI_DEVICE_PATH;
|
||||
typedef EFI_DEVICE_PATH_PROTOCOL EFI_DEVICE_PATH;
|
||||
|
||||
#define END_DEVICE_PATH_TYPE 0x7f
|
||||
|
||||
#define HARDWARE_DEVICE_PATH 0x01
|
||||
|
||||
#define HW_MEMMAP_DP 0x03
|
||||
|
||||
typedef struct {
|
||||
EFI_DEVICE_PATH Header;
|
||||
UINT32 MemoryType;
|
||||
EFI_PHYSICAL_ADDRESS StartingAddress;
|
||||
EFI_PHYSICAL_ADDRESS EndingAddress;
|
||||
} MEMMAP_DEVICE_PATH;
|
||||
|
||||
#define MEDIA_DEVICE_PATH 0x04
|
||||
|
||||
#define MEDIA_HARDDRIVE_DP 0x01
|
||||
|
||||
typedef struct {
|
||||
EFI_DEVICE_PATH Header;
|
||||
UINT32 PartitionNumber;
|
||||
UINT64 PartitionStart;
|
||||
UINT64 PartitionSize;
|
||||
UINT8 Signature[16];
|
||||
UINT8 MBRType;
|
||||
UINT8 SignatureType;
|
||||
} HARDDRIVE_DEVICE_PATH;
|
||||
|
||||
#define MBR_TYPE_PCAT 0x01
|
||||
#define MBR_TYPE_EFI_PARTITION_TABLE_HEADER 0x02
|
||||
|
||||
#define NO_DISK_SIGNATURE 0x00
|
||||
#define SIGNATURE_TYPE_MBR 0x01
|
||||
#define SIGNATURE_TYPE_GUID 0x02
|
||||
|
||||
#define MEDIA_CDROM_DP 0x02
|
||||
|
||||
typedef struct {
|
||||
@@ -35,6 +67,13 @@ typedef struct {
|
||||
UINT64 PartitionSize;
|
||||
} CDROM_DEVICE_PATH;
|
||||
|
||||
#define MEDIA_FILEPATH_DP 0x04
|
||||
|
||||
typedef struct {
|
||||
EFI_DEVICE_PATH Header;
|
||||
CHAR16 PathName[1];
|
||||
} FILEPATH_DEVICE_PATH;
|
||||
|
||||
FORCEINLINE
|
||||
UINT8
|
||||
DevicePathType (
|
||||
|
@@ -16,7 +16,6 @@ Abstract:
|
||||
#ifndef _EFILIB_H
|
||||
#define _EFILIB_H
|
||||
|
||||
#include <nt.h>
|
||||
#include "bootmgr.h"
|
||||
#include "efi.h"
|
||||
|
||||
|
55
BOOT/ENVIRON/INC/mm.h
Normal file
55
BOOT/ENVIRON/INC/mm.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/*++
|
||||
|
||||
Copyright (c) 2024, Quinn Stephens.
|
||||
Provided under the BSD 3-Clause license.
|
||||
|
||||
Module Name:
|
||||
|
||||
mm.h
|
||||
|
||||
Abstract:
|
||||
|
||||
Boot memory manager definitions.
|
||||
|
||||
--*/
|
||||
|
||||
#ifndef _MM_H
|
||||
#define _MM_H
|
||||
|
||||
#include "bootlib.h"
|
||||
|
||||
NTSTATUS
|
||||
MmFwGetMemoryMap (
|
||||
IN OUT PMEMORY_DESCRIPTOR_LIST Mdl,
|
||||
IN ULONG Flags
|
||||
);
|
||||
|
||||
VOID
|
||||
MmMdRemoveDescriptorFromList (
|
||||
IN PMEMORY_DESCRIPTOR_LIST Mdl,
|
||||
IN PMEMORY_DESCRIPTOR Descriptor
|
||||
);
|
||||
|
||||
NTSTATUS
|
||||
MmMdFreeDescriptor (
|
||||
IN PMEMORY_DESCRIPTOR Descriptor
|
||||
);
|
||||
|
||||
VOID
|
||||
MmMdFreeList (
|
||||
IN PMEMORY_DESCRIPTOR_LIST Mdl
|
||||
);
|
||||
|
||||
VOID
|
||||
MmMdInitialize (
|
||||
IN ULONG Unused,
|
||||
IN PBOOT_LIBRARY_PARAMETERS LibraryParameters
|
||||
);
|
||||
|
||||
NTSTATUS
|
||||
MmPaInitialize (
|
||||
IN PBOOT_MEMORY_INFO MemoryInfo,
|
||||
IN ULONG MinimumAllocation
|
||||
);
|
||||
|
||||
#endif
|
82
BOOT/ENVIRON/LIB/EFI/eficon.c
Normal file
82
BOOT/ENVIRON/LIB/EFI/eficon.c
Normal file
@@ -0,0 +1,82 @@
|
||||
/*++
|
||||
|
||||
Copyright (c) 2024, Quinn Stephens.
|
||||
Provided under the BSD 3-Clause license.
|
||||
|
||||
Module Name:
|
||||
|
||||
eficon.c
|
||||
|
||||
Abstract:
|
||||
|
||||
Provides EFI console utilities.
|
||||
|
||||
--*/
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <wchar.h>
|
||||
#include "bootmgr.h"
|
||||
|
||||
extern SIMPLE_TEXT_OUTPUT_INTERFACE *EfiConOut;
|
||||
|
||||
VOID
|
||||
ConsolePrint (
|
||||
IN PWSTR String
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Prints a string to the console.
|
||||
|
||||
Arguments:
|
||||
|
||||
String - string to print.
|
||||
|
||||
Return Value:
|
||||
|
||||
None.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
EfiConOut->OutputString(EfiConOut, String);
|
||||
}
|
||||
|
||||
VOID
|
||||
ConsolePrintf (
|
||||
IN PWSTR Format,
|
||||
...
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Prints a formatted string to the console.
|
||||
|
||||
Arguments:
|
||||
|
||||
Format - format string handled by vswprintf().
|
||||
|
||||
... - arguments.
|
||||
|
||||
Return Value:
|
||||
|
||||
None.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
int Status;
|
||||
va_list Args;
|
||||
WCHAR Buffer[256];
|
||||
|
||||
va_start(Args, Format);
|
||||
Status = vswprintf(Buffer, sizeof(Buffer) / sizeof(WCHAR) - 1, Format, Args);
|
||||
va_end(Args);
|
||||
if (Status > 0) {
|
||||
ConsolePrint(Buffer);
|
||||
}
|
||||
}
|
64
BOOT/ENVIRON/LIB/EFI/efifw.c
Normal file
64
BOOT/ENVIRON/LIB/EFI/efifw.c
Normal file
@@ -0,0 +1,64 @@
|
||||
/*++
|
||||
|
||||
Copyright (c) 2024, Quinn Stephens.
|
||||
Provided under the BSD 3-Clause license.
|
||||
|
||||
Module Name:
|
||||
|
||||
efifw.c
|
||||
|
||||
Abstract:
|
||||
|
||||
Provides EFI firmware utilities.
|
||||
|
||||
--*/
|
||||
|
||||
#include "bootlib.h"
|
||||
#include "bootmgr.h"
|
||||
|
||||
EFI_SYSTEM_TABLE *EfiST;
|
||||
EFI_BOOT_SERVICES *EfiBS;
|
||||
EFI_RUNTIME_SERVICES *EfiRT;
|
||||
SIMPLE_TEXT_OUTPUT_INTERFACE *EfiConOut;
|
||||
SIMPLE_INPUT_INTERFACE *EfiConIn;
|
||||
|
||||
NTSTATUS
|
||||
BlpFwInitialize (
|
||||
IN ULONG Stage,
|
||||
IN PBOOT_FIRMWARE_DATA FirmwareData
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Internal routine to initialize the boot library.
|
||||
|
||||
Arguments:
|
||||
|
||||
Stage - 0 or 1.
|
||||
|
||||
FirmwareData - firmware data structure to use for initialization.
|
||||
|
||||
Return Value:
|
||||
|
||||
STATUS_SUCCESS if successful,
|
||||
STATUS_INVALID_PARAMETER if FirmwareData is invalid.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
if (FirmwareData == NULL || FirmwareData->Version == 0) {
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
if (Stage == 0) {
|
||||
EfiST = FirmwareData->SystemTable;
|
||||
EfiBS = EfiST->BootServices;
|
||||
EfiRT = EfiST->RuntimeServices;
|
||||
EfiConOut = EfiST->ConOut;
|
||||
EfiConIn = EfiST->ConIn;
|
||||
}
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
@@ -16,6 +16,7 @@ Abstract:
|
||||
#include <ntrtl.h>
|
||||
#include <string.h>
|
||||
#include <wchar.h>
|
||||
#include "bootlib.h"
|
||||
#include "bootmgr.h"
|
||||
#include "efi.h"
|
||||
|
||||
@@ -23,15 +24,447 @@ UCHAR EfiInitScratch[2048];
|
||||
const EFI_GUID EfiLoadedImageProtocol = LOADED_IMAGE_PROTOCOL;
|
||||
const EFI_GUID EfiDevicePathProtocol = DEVICE_PATH_PROTOCOL;
|
||||
|
||||
NTSTATUS
|
||||
EfiInitpAppendPathString (
|
||||
IN PWCHAR Destination,
|
||||
IN ULONG BufferSize,
|
||||
IN PWCHAR Source,
|
||||
IN ULONG SourceSize,
|
||||
IN OUT PULONG BufferUsed
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Appends a soure path to a destination path.
|
||||
|
||||
Arguments:
|
||||
|
||||
Destination - the path to append to.
|
||||
|
||||
BufferSize - the maximum number of bytes to append.
|
||||
|
||||
Source - the source path to append to Destination.
|
||||
|
||||
SourceSize - the size of Source, in bytes.
|
||||
|
||||
BufferUsed - pointer to a ULONG to store the number of bytes appended in.
|
||||
|
||||
Return Value:
|
||||
|
||||
STATUS_SUCCESS if successful,
|
||||
STATUS_INVALID_PARAMETER if Destination is not valid,
|
||||
STATUS_BUFFER_TOO_SMALL if BufferSize is too small.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
ULONG Position;
|
||||
|
||||
//
|
||||
// Verify that Source uses wide characters.
|
||||
//
|
||||
if (SourceSize % sizeof(WCHAR) != 0) {
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
//
|
||||
// Remove NULL terminator.
|
||||
//
|
||||
if (SourceSize >= sizeof(WCHAR)) {
|
||||
Position = (SourceSize / sizeof(WCHAR)) - 1;
|
||||
if (Source[Position] == UNICODE_NULL) {
|
||||
SourceSize -= sizeof(UNICODE_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Remove leading separator.
|
||||
//
|
||||
if (SourceSize >= sizeof(WCHAR)) {
|
||||
if (Source[0] == L'\\') {
|
||||
Source++;
|
||||
SourceSize -= sizeof(WCHAR);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Remove trailing separator.
|
||||
//
|
||||
if (SourceSize >= sizeof(WCHAR)) {
|
||||
Position = (SourceSize / sizeof(WCHAR)) - 1;
|
||||
if (Source[Position] == L'\\') {
|
||||
SourceSize -= sizeof(WCHAR);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Check if Source is empty.
|
||||
//
|
||||
if (SourceSize == 0) {
|
||||
*BufferUsed = 0;
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
//
|
||||
// Make sure the buffer is large enough.
|
||||
//
|
||||
if (BufferSize < SourceSize + sizeof(WCHAR)) {
|
||||
return STATUS_BUFFER_TOO_SMALL;
|
||||
}
|
||||
|
||||
//
|
||||
// Append separator and Source to Destination.
|
||||
//
|
||||
Destination[0] = L'\\';
|
||||
RtlCopyMemory(Destination + 1, Source, SourceSize);
|
||||
|
||||
*BufferUsed = SourceSize + sizeof(WCHAR);
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
EFI_DEVICE_PATH *
|
||||
EfiInitpGetDeviceNode (
|
||||
IN EFI_DEVICE_PATH *DevicePath
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Searches an EFI device path for the last device path node
|
||||
before a file path node.
|
||||
|
||||
Arguments:
|
||||
|
||||
DevicePath - EFI device path to search.
|
||||
|
||||
Return Value:
|
||||
|
||||
Pointer to the last device path node.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
EFI_DEVICE_PATH *Node;
|
||||
|
||||
//
|
||||
// Check if the current node is the end of the path.
|
||||
//
|
||||
if (IsDevicePathEndType(DevicePath)) {
|
||||
return DevicePath;
|
||||
}
|
||||
|
||||
//
|
||||
// Find the last non-filepath node.
|
||||
//
|
||||
Node = NextDevicePathNode(DevicePath);
|
||||
while (!IsDevicePathEndType(Node)) {
|
||||
if (DevicePathType(Node) == MEDIA_DEVICE_PATH && DevicePathSubType(Node) == MEDIA_FILEPATH_DP) {
|
||||
break;
|
||||
}
|
||||
|
||||
DevicePath = Node;
|
||||
Node = NextDevicePathNode(Node);
|
||||
}
|
||||
|
||||
return DevicePath;
|
||||
}
|
||||
|
||||
NTSTATUS
|
||||
EfiInitTranslateDevicePath (
|
||||
IN EFI_DEVICE_PATH *EfiDevicePath,
|
||||
IN OUT PBOOT_DEVICE BootDevice,
|
||||
IN ULONG BufferSize
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Translates an EFI device path into boot device format.
|
||||
|
||||
Arguments:
|
||||
|
||||
EfiDevicePath - The EFI device path to be translated.
|
||||
|
||||
BootDevice - Pointer to the destination device structure.
|
||||
|
||||
BufferSize - The amount of available space in the buffer.
|
||||
|
||||
Return Value:
|
||||
|
||||
STATUS_SUCCESS if successful,
|
||||
STATUS_INVALID_PARAMETER if the buffer is too small.
|
||||
STATUS_UNSUCCESSFUL if the path could not be translated.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
EFI_DEVICE_PATH *DeviceNode;
|
||||
MEMMAP_DEVICE_PATH *MemmapNode;
|
||||
HARDDRIVE_DEVICE_PATH *HarddriveNode;
|
||||
PBOOT_BLOCK_IDENTIFIER BlockDevice;
|
||||
|
||||
//
|
||||
// Check for available buffer space.
|
||||
//
|
||||
if (BufferSize < sizeof(BOOT_DEVICE)) {
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
BootDevice->Size = sizeof(BOOT_DEVICE);
|
||||
|
||||
//
|
||||
// Memory mapped device paths are treated as ramdisks.
|
||||
//
|
||||
if (DevicePathType(EfiDevicePath) == HARDWARE_DEVICE_PATH && DevicePathSubType(EfiDevicePath) == HW_MEMMAP_DP) {
|
||||
MemmapNode = (MEMMAP_DEVICE_PATH *)EfiDevicePath;
|
||||
BlockDevice = &BootDevice->Block;
|
||||
BootDevice->Type = BOOT_DEVICE_TYPE_BLOCK;
|
||||
BlockDevice->Type = BOOT_BLOCK_DEVICE_TYPE_RAMDISK;
|
||||
BlockDevice->Ramdisk.ImageBase.QuadPart = MemmapNode->StartingAddress;
|
||||
BlockDevice->Ramdisk.ImageSize = MemmapNode->EndingAddress - MemmapNode->StartingAddress;
|
||||
BlockDevice->Ramdisk.ImageOffset = 0;
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
//
|
||||
// Get the device node, the device the application was loaded from.
|
||||
// TODO: Only media devices and ramdisks are currently supported.
|
||||
//
|
||||
DeviceNode = EfiInitpGetDeviceNode(EfiDevicePath);
|
||||
if (DevicePathType(DeviceNode) != MEDIA_DEVICE_PATH) {
|
||||
return STATUS_UNSUCCESSFUL;
|
||||
}
|
||||
|
||||
//
|
||||
// Check device node subtype.
|
||||
//
|
||||
switch (DevicePathSubType(DeviceNode)) {
|
||||
case MEDIA_HARDDRIVE_DP:
|
||||
HarddriveNode = (HARDDRIVE_DEVICE_PATH *)DeviceNode;
|
||||
|
||||
//
|
||||
// Use correct block device and partition format.
|
||||
//
|
||||
if (HarddriveNode->SignatureType != SIGNATURE_TYPE_MBR) {
|
||||
BlockDevice = &BootDevice->PartitionEx.Parent;
|
||||
BootDevice->Type = BOOT_DEVICE_TYPE_PARTITION_EX;
|
||||
} else {
|
||||
BlockDevice = &BootDevice->Partition.Parent;
|
||||
BootDevice->Type = BOOT_DEVICE_TYPE_PARTITION;
|
||||
}
|
||||
BlockDevice->Type = BOOT_BLOCK_DEVICE_TYPE_HARDDRIVE;
|
||||
|
||||
//
|
||||
// Initialize partition based on the drive's partitioning system.
|
||||
//
|
||||
switch (HarddriveNode->SignatureType) {
|
||||
case SIGNATURE_TYPE_MBR:
|
||||
BlockDevice->Harddrive.PartitionType = BOOT_HARDDRIVE_PARTITION_TYPE_MBR;
|
||||
BlockDevice->Harddrive.Mbr.Signature = *((ULONG *)HarddriveNode->Signature);
|
||||
BootDevice->Partition.Mbr.PartitionNumber = HarddriveNode->PartitionNumber;
|
||||
break;
|
||||
case SIGNATURE_TYPE_GUID:
|
||||
BootDevice->Attributes |= BOOT_DEVICE_ATTRIBUTE_NO_PARENT_SIGNATURE;
|
||||
BlockDevice->Harddrive.PartitionType = BOOT_HARDDRIVE_PARTITION_TYPE_GPT;
|
||||
RtlCopyMemory(&BootDevice->PartitionEx.Gpt.PartitionIdentifier, &HarddriveNode->Signature, sizeof(HarddriveNode->Signature));
|
||||
break;
|
||||
default:
|
||||
BlockDevice->Harddrive.PartitionType = BOOT_HARDDRIVE_PARTITION_TYPE_RAW;
|
||||
BlockDevice->Harddrive.Raw.DriveNumber = 0;
|
||||
}
|
||||
|
||||
break;
|
||||
case MEDIA_CDROM_DP:
|
||||
BootDevice->Type = BOOT_DEVICE_TYPE_BLOCK;
|
||||
BootDevice->Block.Type = BOOT_BLOCK_DEVICE_TYPE_CDROM;
|
||||
BootDevice->Block.Cdrom.DriveNumber = 0;
|
||||
break;
|
||||
default:
|
||||
return STATUS_UNSUCCESSFUL;
|
||||
}
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
NTSTATUS
|
||||
EfiInitpConvertEfiDevicePath (
|
||||
IN EFI_DEVICE_PATH *EfiDevicePath,
|
||||
IN BCDE_DATA_TYPE OptionType,
|
||||
IN OUT PBOOT_APPLICATION_OPTION Option,
|
||||
IN ULONG BufferSize
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Converts an EFI device path into BCD format.
|
||||
|
||||
Arguments:
|
||||
|
||||
EfiDevicePath - The EFI device path to be converted.
|
||||
|
||||
OptionType - The data type to be assigned to option.
|
||||
|
||||
Option - Pointer to the destination option structure.
|
||||
|
||||
BufferSize - The amount of available space in the buffer.
|
||||
|
||||
Return Value:
|
||||
|
||||
STATUS_SUCCESS if successful.
|
||||
other NTSTATUS value if failure occurs.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
NTSTATUS Status;
|
||||
PBCDE_DEVICE DeviceElement;
|
||||
|
||||
//
|
||||
// Check for available buffer space.
|
||||
//
|
||||
if (BufferSize < sizeof(BOOT_APPLICATION_OPTION) + FIELD_OFFSET(BCDE_DEVICE, Device)) {
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
//
|
||||
// Translate device path.
|
||||
//
|
||||
RtlZeroMemory(Option, sizeof(BOOT_APPLICATION_OPTION));
|
||||
DeviceElement = (PBCDE_DEVICE)((PUCHAR)Option + sizeof(BOOT_APPLICATION_OPTION));
|
||||
Status = EfiInitTranslateDevicePath(
|
||||
EfiDevicePath,
|
||||
&DeviceElement->Device,
|
||||
BufferSize - (sizeof(BOOT_APPLICATION_OPTION) + FIELD_OFFSET(BCDE_DEVICE, Device))
|
||||
);
|
||||
if (!NT_SUCCESS(Status)) {
|
||||
return Status;
|
||||
}
|
||||
|
||||
//
|
||||
// Set up option structure.
|
||||
//
|
||||
Option->Type = OptionType;
|
||||
Option->DataOffset = sizeof(BOOT_APPLICATION_OPTION);
|
||||
Option->DataSize = FIELD_OFFSET(BCDE_DEVICE, Device) + DeviceElement->Device.Size;
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
NTSTATUS
|
||||
EfiInitpConvertEfiFilePath (
|
||||
IN EFI_DEVICE_PATH *EfiFilePath,
|
||||
IN BCDE_DATA_TYPE OptionType,
|
||||
IN OUT PBOOT_APPLICATION_OPTION Option,
|
||||
IN ULONG BufferSize
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Converts an EFI file path into BCD format.
|
||||
|
||||
Arguments:
|
||||
|
||||
EfiFilePath - The EFI file path to be converted.
|
||||
|
||||
OptionType - The data type to be assigned to option.
|
||||
|
||||
Option - Pointer to the destination option structure.
|
||||
|
||||
BufferSize - The amount of available space in the buffer.
|
||||
|
||||
Return Value:
|
||||
|
||||
STATUS_SUCCESS if successful.
|
||||
other NTSTATUS value if failure occurs.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
NTSTATUS Status;
|
||||
EFI_DEVICE_PATH *Node;
|
||||
PWCHAR PathStart, Position;
|
||||
ULONG BufferRemaining, Length, Appended;
|
||||
|
||||
//
|
||||
// Check for available buffer space.
|
||||
//
|
||||
if (BufferSize < sizeof(BOOT_APPLICATION_OPTION)) {
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
//
|
||||
// Set up option structure.
|
||||
//
|
||||
RtlZeroMemory(Option, sizeof(BOOT_APPLICATION_OPTION));
|
||||
Option->Type = OptionType;
|
||||
Option->DataOffset = sizeof(BOOT_APPLICATION_OPTION);
|
||||
|
||||
//
|
||||
// Add to the path one file path node at a time.
|
||||
//
|
||||
Option->DataSize = 0;
|
||||
BufferRemaining = BufferSize - sizeof(BOOT_APPLICATION_OPTION);
|
||||
Node = EfiFilePath;
|
||||
PathStart = (PWCHAR)((PUCHAR)Option + Option->DataOffset);
|
||||
Position = PathStart;
|
||||
while (!IsDevicePathEndType(Node)) {
|
||||
if (DevicePathType(Node) != MEDIA_DEVICE_PATH || DevicePathSubType(Node) != MEDIA_FILEPATH_DP) {
|
||||
Node = NextDevicePathNode(Node);
|
||||
continue;
|
||||
}
|
||||
|
||||
Status = RtlULongSub(DevicePathNodeLength(Node), FIELD_OFFSET(FILEPATH_DEVICE_PATH, PathName), &Length);
|
||||
if (!NT_SUCCESS(Status)) {
|
||||
return Status;
|
||||
}
|
||||
|
||||
Status = EfiInitpAppendPathString(Position, BufferRemaining, &((FILEPATH_DEVICE_PATH *)Node)->PathName[0], Length, &Appended);
|
||||
if (!NT_SUCCESS(Status)) {
|
||||
return Status;
|
||||
}
|
||||
|
||||
Option->DataSize += Appended;
|
||||
BufferRemaining -= Appended;
|
||||
Position = (PWCHAR)((PUCHAR)Position + Appended);
|
||||
Node = NextDevicePathNode(Node);
|
||||
}
|
||||
|
||||
//
|
||||
// Terminate path string.
|
||||
//
|
||||
if (BufferRemaining < sizeof(UNICODE_NULL)) {
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
*Position = L'\0';
|
||||
Option->DataSize += sizeof(UNICODE_NULL);
|
||||
|
||||
//
|
||||
// The path option is invalid if the path is NULL.
|
||||
//
|
||||
if (Position == PathStart) {
|
||||
Option->IsInvalid = TRUE;
|
||||
}
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
VOID
|
||||
EfiInitpCreateApplicationEntry (
|
||||
IN EFI_SYSTEM_TABLE *SystemTable,
|
||||
IN OUT PBOOT_APPLICATION_ENTRY Entry,
|
||||
IN OUT PBOOT_INPUT_APPLICATION_ENTRY Entry,
|
||||
IN ULONG BufferSize,
|
||||
IN EFI_DEVICE_PATH *DevicePath,
|
||||
IN EFI_DEVICE_PATH *FilePath,
|
||||
IN EFI_DEVICE_PATH *EfiDevicePath,
|
||||
IN EFI_DEVICE_PATH *EfiFilePath,
|
||||
IN PWCHAR LoadOptions,
|
||||
IN ULONG LoadOptionsSize,
|
||||
IN ULONG Flags,
|
||||
OUT PULONG BufferUsed,
|
||||
OUT PBOOT_DEVICE *BootDevice
|
||||
)
|
||||
@@ -50,14 +483,16 @@ Arguments:
|
||||
|
||||
BufferSize - The amount of available space in the buffer.
|
||||
|
||||
DevicePath - The device path for the application.
|
||||
EfiDevicePath - The device path for the application.
|
||||
|
||||
FilePath - The file path for the application.
|
||||
EfiFilePath - The file path for the application.
|
||||
|
||||
LoadOptions - Firmware load options string.
|
||||
|
||||
LoadOptionsSize - Length of the string pointed to by LoadOptions.
|
||||
|
||||
Flags - Unused.
|
||||
|
||||
BufferUsed - Returns the amount of buffer space used by the routine.
|
||||
|
||||
BootDevice - Returns a pointer to the device the application was loaded from.
|
||||
@@ -69,21 +504,39 @@ Return Value:
|
||||
--*/
|
||||
|
||||
{
|
||||
NTSTATUS Status;
|
||||
ULONG BufferRemaining, OptionsSize, Size;
|
||||
PWCHAR BcdOptionString;
|
||||
BOOLEAN BcdIdentifierSet;
|
||||
UNICODE_STRING UnicodeString;
|
||||
PBOOT_APPLICATION_OPTION Option, PrevOption;
|
||||
PBCDE_DEVICE BootDeviceElement;
|
||||
|
||||
(VOID)SystemTable;
|
||||
(VOID)EfiDevicePath;
|
||||
(VOID)EfiFilePath;
|
||||
(VOID)Flags;
|
||||
|
||||
*BufferUsed = 0;
|
||||
*BootDevice = NULL;
|
||||
OptionsSize = 0;
|
||||
BcdIdentifierSet = FALSE;
|
||||
|
||||
//
|
||||
// Require enough space for the application entry.
|
||||
//
|
||||
if (BufferSize < sizeof(BOOT_APPLICATION_ENTRY)) {
|
||||
BufferRemaining = BufferSize;
|
||||
if (BufferRemaining < sizeof(BOOT_INPUT_APPLICATION_ENTRY)) {
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Set up application entry structure.
|
||||
//
|
||||
RtlZeroMemory(Entry, sizeof(BOOT_INPUT_APPLICATION_ENTRY));
|
||||
Entry->Signature = BOOT_INPUT_APPLICATION_ENTRY_SIGNATURE;
|
||||
BufferRemaining -= FIELD_OFFSET(BOOT_INPUT_APPLICATION_ENTRY, Options);
|
||||
|
||||
//
|
||||
// Terminate load options string.
|
||||
//
|
||||
@@ -92,13 +545,6 @@ Return Value:
|
||||
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.
|
||||
//
|
||||
@@ -110,15 +556,51 @@ Return Value:
|
||||
}
|
||||
|
||||
if (!BcdIdentifierSet) {
|
||||
Entry->Attributes |= BOOT_APPLICATION_ENTRY_BCD_IDENTIFIER_NOT_SET;
|
||||
Entry->Attributes |= BOOT_INPUT_APPLICATION_ENTRY_NO_BCD_IDENTIFIER;
|
||||
}
|
||||
|
||||
//
|
||||
// TODO: This routine is not fully implemented.
|
||||
// Convert the EFI device path into a boot device option.
|
||||
//
|
||||
(VOID)SystemTable;
|
||||
(VOID)DevicePath;
|
||||
(VOID)FilePath;
|
||||
Option = &Entry->Options;
|
||||
Status = EfiInitpConvertEfiDevicePath(EfiDevicePath, BCDE_DATA_TYPE_APPLICATION_DEVICE, Option, BufferRemaining);
|
||||
if (!NT_SUCCESS(Status)) {
|
||||
Option->IsInvalid = TRUE;
|
||||
goto exit;
|
||||
}
|
||||
BootDeviceElement = (PBCDE_DEVICE)((PUCHAR)Option + Option->DataOffset);
|
||||
*BootDevice = &BootDeviceElement->Device;
|
||||
Size = BlGetBootOptionSize(Option);
|
||||
OptionsSize += Size;
|
||||
BufferRemaining -= Size;
|
||||
|
||||
//
|
||||
// Convert the EFI file path into a boot file path option.
|
||||
// TODO: UDP/PXE boot is not supported.
|
||||
//
|
||||
PrevOption = Option;
|
||||
Option = (PBOOT_APPLICATION_OPTION)((PUCHAR)&Entry->Options + OptionsSize);
|
||||
Status = EfiInitpConvertEfiFilePath(EfiFilePath, BCDE_DATA_TYPE_APPLICATION_PATH, Option, BufferRemaining);
|
||||
if (!NT_SUCCESS(Status)) {
|
||||
goto exit;
|
||||
}
|
||||
PrevOption->NextOptionOffset = (PUCHAR)Option - (PUCHAR)&Entry->Options;
|
||||
Size = BlGetBootOptionSize(Option);
|
||||
OptionsSize += Size;
|
||||
BufferRemaining -= Size;
|
||||
|
||||
//
|
||||
// TODO: This section is incomplete.
|
||||
//
|
||||
PrevOption = Option;
|
||||
Option = (PBOOT_APPLICATION_OPTION)((PUCHAR)&Entry->Options + OptionsSize);
|
||||
// Status = Unknown(LoadOptions, &Entry->Options, RemainingSize, &OptionsSize, &PrevOption, &Size);
|
||||
if (!NT_SUCCESS(Status)) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
exit:
|
||||
*BufferUsed = BufferSize - BufferRemaining;
|
||||
}
|
||||
|
||||
PBOOT_INPUT_PARAMETERS
|
||||
@@ -154,7 +636,7 @@ Return Value:
|
||||
EFI_DEVICE_PATH *DevicePath;
|
||||
PBOOT_INPUT_PARAMETERS InputParameters;
|
||||
PBOOT_MEMORY_INFO MemoryInfo;
|
||||
PBOOT_MEMORY_DESCRIPTOR MemoryDescriptor;
|
||||
PMEMORY_DESCRIPTOR MemoryDescriptor;
|
||||
PBOOT_DEVICE BootDevice;
|
||||
PBOOT_FIRMWARE_DATA FirmwareData;
|
||||
PBOOT_RETURN_DATA ReturnData;
|
||||
@@ -198,7 +680,7 @@ Return Value:
|
||||
InputParameters->Signature = BOOT_INPUT_PARAMETERS_SIGNATURE;
|
||||
InputParameters->Version = BOOT_INPUT_PARAMETERS_VERSION;
|
||||
InputParameters->MachineType = BOOT_MACHINE_TYPE;
|
||||
InputParameters->TranslationType = BOOT_TRANSLATION_TYPE;
|
||||
InputParameters->TranslationType = BOOT_TRANSLATION_TYPE_NONE;
|
||||
InputParameters->ImageBase = LoadedImage->ImageBase;
|
||||
InputParameters->ImageSize = LoadedImage->ImageSize;
|
||||
|
||||
@@ -211,14 +693,14 @@ Return Value:
|
||||
MemoryInfo->Version = BOOT_MEMORY_INFO_VERSION;
|
||||
MemoryInfo->MdlOffset = sizeof(BOOT_MEMORY_INFO);
|
||||
MemoryInfo->DescriptorCount = 1;
|
||||
MemoryInfo->DescriptorSize = sizeof(BOOT_MEMORY_DESCRIPTOR);
|
||||
MemoryInfo->BasePageOffset = FIELD_OFFSET(BOOT_MEMORY_DESCRIPTOR, BasePage);
|
||||
MemoryInfo->DescriptorSize = sizeof(MEMORY_DESCRIPTOR);
|
||||
MemoryInfo->BasePageOffset = FIELD_OFFSET(MEMORY_DESCRIPTOR, BasePage);
|
||||
|
||||
//
|
||||
// Create a memory descriptor for the boot manager image.
|
||||
//
|
||||
MemoryDescriptor = (PBOOT_MEMORY_DESCRIPTOR)(&EfiInitScratch[ScratchUsed]);
|
||||
ScratchUsed += sizeof(BOOT_MEMORY_DESCRIPTOR);
|
||||
MemoryDescriptor = (PMEMORY_DESCRIPTOR)(&EfiInitScratch[ScratchUsed]);
|
||||
ScratchUsed += sizeof(MEMORY_DESCRIPTOR);
|
||||
MemoryDescriptor->BasePage = (UINTN)InputParameters->ImageBase >> PAGE_SHIFT;
|
||||
MemoryDescriptor->Pages = ALIGN_UP(InputParameters->ImageSize, PAGE_SIZE) >> PAGE_SHIFT;
|
||||
MemoryDescriptor->Attributes = MEMORY_ATTRIBUTE_CACHE_WB;
|
||||
@@ -230,12 +712,13 @@ Return Value:
|
||||
InputParameters->ApplicationEntryOffset = ScratchUsed;
|
||||
EfiInitpCreateApplicationEntry(
|
||||
SystemTable,
|
||||
(PBOOT_APPLICATION_ENTRY)(&EfiInitScratch[ScratchUsed]),
|
||||
(PBOOT_INPUT_APPLICATION_ENTRY)(&EfiInitScratch[ScratchUsed]),
|
||||
sizeof(EfiInitScratch) - ScratchUsed,
|
||||
DevicePath,
|
||||
LoadedImage->FilePath,
|
||||
LoadedImage->LoadOptions,
|
||||
LoadedImage->LoadOptionsSize,
|
||||
0,
|
||||
&ApplicationEntrySize,
|
||||
&BootDevice
|
||||
);
|
||||
|
56
BOOT/ENVIRON/LIB/EFI/efimm.c
Normal file
56
BOOT/ENVIRON/LIB/EFI/efimm.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/*++
|
||||
|
||||
Copyright (c) 2024, Quinn Stephens.
|
||||
Provided under the BSD 3-Clause license.
|
||||
|
||||
Module Name:
|
||||
|
||||
efimm.c
|
||||
|
||||
Abstract:
|
||||
|
||||
Provides EFI memory manager routines.
|
||||
|
||||
--*/
|
||||
|
||||
#include "bootmgr.h"
|
||||
#include "efi.h"
|
||||
#include "mm.h"
|
||||
|
||||
NTSTATUS
|
||||
MmFwGetMemoryMap (
|
||||
IN OUT PMEMORY_DESCRIPTOR_LIST Mdl,
|
||||
IN ULONG Flags
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Converts an NT status code into an EFI status code.
|
||||
|
||||
Arguments:
|
||||
|
||||
Status - The NT status code to be converted.
|
||||
|
||||
Return Value:
|
||||
|
||||
STATUS_SUCCESS if successful,
|
||||
STATUS_INVALID_PARAMETER if Mdl is invalid.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
(VOID)Flags;
|
||||
|
||||
//
|
||||
// Make sure Mdl is valid.
|
||||
//
|
||||
if (Mdl == NULL) {
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
MmMdFreeList(Mdl);
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
79
BOOT/ENVIRON/LIB/MM/mm.c
Normal file
79
BOOT/ENVIRON/LIB/MM/mm.c
Normal file
@@ -0,0 +1,79 @@
|
||||
/*++
|
||||
|
||||
Copyright (c) 2024, Quinn Stephens.
|
||||
Provided under the BSD 3-Clause license.
|
||||
|
||||
Module Name:
|
||||
|
||||
mm.c
|
||||
|
||||
Abstract:
|
||||
|
||||
Provides boot library memory manager routines.
|
||||
|
||||
--*/
|
||||
|
||||
#include <ntrtl.h>
|
||||
#include "bootlib.h"
|
||||
#include "mm.h"
|
||||
|
||||
NTSTATUS
|
||||
BlpMmInitialize (
|
||||
IN PBOOT_MEMORY_INFO MemoryInfo,
|
||||
IN ULONG TranslationType,
|
||||
IN PBOOT_LIBRARY_PARAMETERS LibraryParameters
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Initializes the boot memory manager.
|
||||
|
||||
Arguments:
|
||||
|
||||
MemoryInfo - pointer to the memory info structure.
|
||||
|
||||
TranslationType - the current translation type being used.
|
||||
|
||||
LibraryParameters - pointer to the library parameters structure.
|
||||
|
||||
Return Value:
|
||||
|
||||
STATUS_SUCCESS if successful,
|
||||
STATUS_INVALID_PARAMETER if TranslationType is invalid,
|
||||
Other NTSTATUS value if an error occurs.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
NTSTATUS Status;
|
||||
|
||||
DebugPrint(L"Initializing memory manager...\r\n");
|
||||
|
||||
//
|
||||
// Check TranslationType.
|
||||
//
|
||||
if (
|
||||
TranslationType > BOOT_TRANSLATION_TYPE_MAX ||
|
||||
LibraryParameters->TranslationType > BOOT_TRANSLATION_TYPE_MAX
|
||||
) {
|
||||
DebugPrint(L"BlpMmInitialize(): TranslationType is invalid\r\n");
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
//
|
||||
// Initialize memory descriptor manager.
|
||||
//
|
||||
MmMdInitialize(0, LibraryParameters);
|
||||
|
||||
//
|
||||
// Initialize page allocator.
|
||||
//
|
||||
Status = MmPaInitialize(MemoryInfo, LibraryParameters->MinimumPageAllocation);
|
||||
if (!NT_SUCCESS(Status)) {
|
||||
return Status;
|
||||
}
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
206
BOOT/ENVIRON/LIB/MM/mmmd.c
Normal file
206
BOOT/ENVIRON/LIB/MM/mmmd.c
Normal file
@@ -0,0 +1,206 @@
|
||||
/*++
|
||||
|
||||
Copyright (c) 2024, Quinn Stephens.
|
||||
Provided under the BSD 3-Clause license.
|
||||
|
||||
Module Name:
|
||||
|
||||
mmmd.c
|
||||
|
||||
Abstract:
|
||||
|
||||
Provides memory descriptor routines.
|
||||
|
||||
--*/
|
||||
|
||||
#include <ntrtl.h>
|
||||
#include "bootmgr.h"
|
||||
#include "mm.h"
|
||||
|
||||
#define MAX_STATIC_DESCRIPTOR_COUNT 1024
|
||||
|
||||
MEMORY_DESCRIPTOR MmStaticMemoryDescriptors[MAX_STATIC_DESCRIPTOR_COUNT];
|
||||
|
||||
PMEMORY_DESCRIPTOR MmGlobalMemoryDescriptors;
|
||||
ULONG MmGlobalMemoryDescriptorCount;
|
||||
|
||||
PMEMORY_DESCRIPTOR MmDynamicMemoryDescriptors;
|
||||
ULONG MmDynamicMemoryDescriptorCount;
|
||||
|
||||
VOID
|
||||
MmMdRemoveDescriptorFromList (
|
||||
IN PMEMORY_DESCRIPTOR_LIST Mdl,
|
||||
IN PMEMORY_DESCRIPTOR Descriptor
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Removes a descriptor from a MDL.
|
||||
|
||||
Arguments:
|
||||
|
||||
Mdl - the MDL to remove Descriptor from.
|
||||
|
||||
Descriptor - the descriptor to remove from Mdl.
|
||||
|
||||
Return Value:
|
||||
|
||||
None.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
PLIST_ENTRY Blink;
|
||||
|
||||
Blink = Descriptor->ListEntry.Blink;
|
||||
|
||||
//
|
||||
// Remove the descriptor from the MDL.
|
||||
//
|
||||
RemoveEntryList(&Descriptor->ListEntry);
|
||||
|
||||
//
|
||||
// Check if the removed descriptor was cached.
|
||||
//
|
||||
if (Mdl->Current != &Descriptor->ListEntry) {
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Cache the previous descriptor if possible.
|
||||
//
|
||||
if (
|
||||
(
|
||||
(ULONG_PTR)Blink < (ULONG_PTR)MmGlobalMemoryDescriptors
|
||||
|| (ULONG_PTR)Blink >= (ULONG_PTR)&MmGlobalMemoryDescriptors[MmGlobalMemoryDescriptorCount]
|
||||
)
|
||||
&& Blink != Mdl->Head
|
||||
) {
|
||||
Mdl->Current = Blink;
|
||||
} else {
|
||||
Mdl->Current = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
NTSTATUS
|
||||
MmMdFreeDescriptor (
|
||||
IN PMEMORY_DESCRIPTOR Descriptor
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Frees a memory descriptor.
|
||||
|
||||
Arguments:
|
||||
|
||||
Descriptor - the descriptor to free.
|
||||
|
||||
Return Value:
|
||||
|
||||
STATUS_SUCCESS if successful,
|
||||
other NTSTATUS value if an error occurs.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
if (
|
||||
(
|
||||
MmDynamicMemoryDescriptors != NULL
|
||||
&& (ULONG_PTR)Descriptor >= (ULONG_PTR)MmDynamicMemoryDescriptors
|
||||
&& (ULONG_PTR)Descriptor <= (ULONG_PTR)&MmDynamicMemoryDescriptors[MmDynamicMemoryDescriptorCount]
|
||||
)
|
||||
|| (
|
||||
(ULONG_PTR)Descriptor >= (ULONG_PTR)MmStaticMemoryDescriptors
|
||||
&& (ULONG_PTR)Descriptor <= (ULONG_PTR)&MmStaticMemoryDescriptors[MAX_STATIC_DESCRIPTOR_COUNT]
|
||||
)
|
||||
) {
|
||||
//
|
||||
// Clear the descriptor from static/dynamic MDL.
|
||||
//
|
||||
RtlZeroMemory(Descriptor, sizeof(MEMORY_DESCRIPTOR));
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
//
|
||||
// Free the descriptor from the heap.
|
||||
// TODO: Use BlMmFreeHeap()
|
||||
//
|
||||
ConsolePrint(L"Cannot free memory descriptor: BlMmFreeHeap() is not implemented\r\n");
|
||||
return STATUS_NOT_IMPLEMENTED;
|
||||
// return BlMmFreeHeap(Descriptor);
|
||||
}
|
||||
|
||||
VOID
|
||||
MmMdFreeList (
|
||||
IN PMEMORY_DESCRIPTOR_LIST Mdl
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Frees a memory descriptor list (MDL).
|
||||
|
||||
Arguments:
|
||||
|
||||
Mdl - the MDL to free.
|
||||
|
||||
Return Value:
|
||||
|
||||
None.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
PLIST_ENTRY Entry;
|
||||
|
||||
Entry = Mdl->Head->Flink;
|
||||
while (Entry != Mdl->Head) {
|
||||
MmMdRemoveDescriptorFromList(Mdl, (PMEMORY_DESCRIPTOR)Entry);
|
||||
MmMdFreeDescriptor((PMEMORY_DESCRIPTOR)Entry);
|
||||
Entry = Entry->Flink;
|
||||
}
|
||||
}
|
||||
|
||||
VOID
|
||||
MmMdInitialize (
|
||||
IN ULONG Unused,
|
||||
IN PBOOT_LIBRARY_PARAMETERS LibraryParameters
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Initializes the memory descriptor manager.
|
||||
|
||||
Arguments:
|
||||
|
||||
Unused - Ignored.
|
||||
|
||||
LibraryParameters - pointer to the library parameters structure.
|
||||
|
||||
Return Value:
|
||||
|
||||
None.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
(VOID)Unused;
|
||||
(VOID)LibraryParameters;
|
||||
|
||||
DebugPrint(L"Initializing memory descriptor manager...\r\n");
|
||||
|
||||
//
|
||||
// Initialize global memory descriptor list.
|
||||
//
|
||||
MmGlobalMemoryDescriptors = &MmStaticMemoryDescriptors[0];
|
||||
MmGlobalMemoryDescriptorCount = MAX_STATIC_DESCRIPTOR_COUNT;
|
||||
RtlZeroMemory(MmGlobalMemoryDescriptors, MAX_STATIC_DESCRIPTOR_COUNT * sizeof(MEMORY_DESCRIPTOR));
|
||||
DebugPrintf(L"Global memory descriptor count: %x\r\n", MmGlobalMemoryDescriptorCount);
|
||||
}
|
122
BOOT/ENVIRON/LIB/MM/mmpa.c
Normal file
122
BOOT/ENVIRON/LIB/MM/mmpa.c
Normal file
@@ -0,0 +1,122 @@
|
||||
/*++
|
||||
|
||||
Copyright (c) 2024, Quinn Stephens.
|
||||
Provided under the BSD 3-Clause license.
|
||||
|
||||
Module Name:
|
||||
|
||||
mmpa.c
|
||||
|
||||
Abstract:
|
||||
|
||||
Provides memory manager page routines.
|
||||
|
||||
--*/
|
||||
|
||||
#include "bootlib.h"
|
||||
#include "mm.h"
|
||||
|
||||
ULONG PapMinimumAllocationCount;
|
||||
ULONGLONG PapMinimumPhysicalPage;
|
||||
ULONGLONG PapMaximumPhysicalPage;
|
||||
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlFwAllocationTracker;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlBadMemory;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlTruncatedMemory;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlPersistentMemory;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlReservedAllocated;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlMappedAllocated;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlMappedUnallocated;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlUnmappedAllocated;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlUnmappedUnallocated;
|
||||
|
||||
FORCEINLINE
|
||||
VOID
|
||||
InitializeList (
|
||||
IN PMEMORY_DESCRIPTOR_LIST List
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Initializes a MDL.
|
||||
|
||||
Arguments:
|
||||
|
||||
List - the MDL to initialize.
|
||||
|
||||
Return Value:
|
||||
|
||||
None.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
List->Head = NULL;
|
||||
List->Current = NULL;
|
||||
List->Type = MDL_TYPE_PHYSICAL;
|
||||
}
|
||||
|
||||
NTSTATUS
|
||||
MmPaInitialize (
|
||||
IN PBOOT_MEMORY_INFO MemoryInfo,
|
||||
IN ULONG MinimumAllocation
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Initializes the page allocator.
|
||||
|
||||
Arguments:
|
||||
|
||||
MemoryInfo - pointer to the memory info structure.
|
||||
|
||||
MinimumAllocation - minimum amount of pages to grow the pool by at a time.
|
||||
|
||||
Return Value:
|
||||
|
||||
STATUS_SUCCESS if successful,
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
NTSTATUS Status;
|
||||
|
||||
(VOID)MemoryInfo;
|
||||
|
||||
DebugPrint(L"Initializing page allocator...\r\n");
|
||||
|
||||
//
|
||||
// Initialize page allocator settings.
|
||||
//
|
||||
PapMinimumAllocationCount = MinimumAllocation;
|
||||
PapMinimumPhysicalPage = 1;
|
||||
PapMaximumPhysicalPage = MAXULONGLONG >> PAGE_SHIFT;
|
||||
DebugPrintf(L"Maximum physical page: %x %x\r\n", (ULONG)(PapMaximumPhysicalPage >> 32), (ULONG)(PapMaximumPhysicalPage));
|
||||
|
||||
//
|
||||
// Initialize MDLs.
|
||||
//
|
||||
InitializeList(&MmMdlFwAllocationTracker);
|
||||
InitializeList(&MmMdlBadMemory);
|
||||
InitializeList(&MmMdlTruncatedMemory);
|
||||
InitializeList(&MmMdlPersistentMemory);
|
||||
InitializeList(&MmMdlReservedAllocated);;
|
||||
InitializeList(&MmMdlMappedAllocated);
|
||||
InitializeList(&MmMdlMappedUnallocated);
|
||||
InitializeList(&MmMdlUnmappedAllocated);
|
||||
InitializeList(&MmMdlUnmappedUnallocated);
|
||||
|
||||
//
|
||||
// Get the firmware memory map.
|
||||
//
|
||||
Status = MmFwGetMemoryMap(&MmMdlUnmappedUnallocated, 0x03);
|
||||
if (!NT_SUCCESS(Status)) {
|
||||
return Status;
|
||||
}
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
@@ -13,6 +13,7 @@ Abstract:
|
||||
|
||||
--*/
|
||||
|
||||
#include <ntrtl.h>
|
||||
#include "bootlib.h"
|
||||
|
||||
//
|
||||
@@ -25,6 +26,10 @@ Abstract:
|
||||
sizeof(BOOT_RETURN_DATA) \
|
||||
)
|
||||
|
||||
PBOOT_INPUT_PARAMETERS BlpApplicationParameters;
|
||||
BOOT_APPLICATION_ENTRY BlpApplicationEntry;
|
||||
PBOOT_DEVICE BlpBootDevice;
|
||||
|
||||
NTSTATUS
|
||||
InitializeLibrary (
|
||||
IN PBOOT_INPUT_PARAMETERS InputParameters,
|
||||
@@ -50,6 +55,13 @@ Return Value:
|
||||
--*/
|
||||
|
||||
{
|
||||
NTSTATUS Status;
|
||||
PBOOT_MEMORY_INFO MemoryInfo;
|
||||
PBOOT_INPUT_APPLICATION_ENTRY ApplicationEntry;
|
||||
PBOOT_FIRMWARE_DATA FirmwareData;
|
||||
PBOOT_BLOCK_IDENTIFIER BlockDevice;
|
||||
PBOOT_APPLICATION_OPTION Option;
|
||||
|
||||
(VOID)LibraryParameters;
|
||||
|
||||
//
|
||||
@@ -61,6 +73,111 @@ Return Value:
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate structure addresses from offsets.
|
||||
//
|
||||
MemoryInfo = (PBOOT_MEMORY_INFO)((PUCHAR)InputParameters + InputParameters->MemoryInfoOffset);
|
||||
ApplicationEntry = (PBOOT_INPUT_APPLICATION_ENTRY)((PUCHAR)InputParameters + InputParameters->ApplicationEntryOffset);
|
||||
BlpBootDevice = (PBOOT_DEVICE)((PUCHAR)InputParameters + InputParameters->BootDeviceOffset);
|
||||
FirmwareData = (PBOOT_FIRMWARE_DATA)((PUCHAR)InputParameters + InputParameters->FirmwareDataOffset);
|
||||
|
||||
//
|
||||
// Initialize firmware library.
|
||||
// It is important to do this early so that
|
||||
// ConsolePrint() and DebugPrint() can be used.
|
||||
//
|
||||
Status = BlpFwInitialize(0, FirmwareData);
|
||||
if (!NT_SUCCESS(Status)) {
|
||||
return Status;
|
||||
}
|
||||
|
||||
//
|
||||
// Print image information.
|
||||
//
|
||||
ConsolePrintf(L"Image base: %x %x\r\n", (ULONG)((ULONG_PTR)InputParameters->ImageBase >> 32), (ULONG)((ULONG_PTR)InputParameters->ImageBase));
|
||||
ConsolePrintf(L"Image size: %x\r\n", InputParameters->ImageSize);
|
||||
|
||||
//
|
||||
// Check application entry signature.
|
||||
//
|
||||
if (ApplicationEntry->Signature != BOOT_INPUT_APPLICATION_ENTRY_SIGNATURE) {
|
||||
DebugPrint(L"InitializeLibrary(): ApplicationEntry Signature is invalid\r\n");
|
||||
return STATUS_INVALID_PARAMETER_9;
|
||||
}
|
||||
|
||||
//
|
||||
// Save input parameters and application entry data.
|
||||
//
|
||||
BlpApplicationParameters = InputParameters;
|
||||
BlpApplicationEntry.Attributes = ApplicationEntry->Attributes;
|
||||
RtlCopyMemory(&BlpApplicationEntry.BcdIdentifier, &ApplicationEntry->BcdIdentifier, sizeof(GUID));
|
||||
BlpApplicationEntry.Options = &ApplicationEntry->Options;
|
||||
|
||||
//
|
||||
// Initialize memory manager.
|
||||
//
|
||||
Status = BlpMmInitialize(MemoryInfo, InputParameters->TranslationType, LibraryParameters);
|
||||
if (!NT_SUCCESS(Status)) {
|
||||
return Status;
|
||||
}
|
||||
|
||||
//
|
||||
// Print debug information.
|
||||
// TODO: Remove this once the project is more stable?
|
||||
//
|
||||
#ifdef _DEBUG
|
||||
DebugPrint(L"Boot device type: ");
|
||||
switch (BlpBootDevice->Type) {
|
||||
case BOOT_DEVICE_TYPE_PARTITION:
|
||||
DebugPrint(L"partition\r\n");
|
||||
BlockDevice = &BlpBootDevice->Partition.Parent;
|
||||
break;
|
||||
case BOOT_DEVICE_TYPE_PARTITION_EX:
|
||||
DebugPrint(L"partition\r\n");
|
||||
BlockDevice = &BlpBootDevice->PartitionEx.Parent;
|
||||
break;
|
||||
default:
|
||||
DebugPrint(L"generic block device\r\n");
|
||||
BlockDevice = &BlpBootDevice->Block;
|
||||
break;
|
||||
}
|
||||
|
||||
DebugPrint(L"Boot device parent type: ");
|
||||
switch (BlockDevice->Type) {
|
||||
case BOOT_BLOCK_DEVICE_TYPE_HARDDRIVE:
|
||||
DebugPrint(L"hard drive\r\n");
|
||||
break;
|
||||
case BOOT_BLOCK_DEVICE_TYPE_CDROM:
|
||||
DebugPrint(L"CD-ROM\r\n");
|
||||
break;
|
||||
case BOOT_BLOCK_DEVICE_TYPE_RAMDISK:
|
||||
DebugPrint(L"RAM disk\r\n");
|
||||
break;
|
||||
default:
|
||||
DebugPrint(L"generic block device\r\n");
|
||||
break;
|
||||
}
|
||||
|
||||
Option = &ApplicationEntry->Options;
|
||||
for (ULONG Index = 0; !Option->IsInvalid; Index++) {
|
||||
DebugPrintf(L"Boot entry option %x: ", Index);
|
||||
|
||||
if (Option->Type == BCDE_DATA_TYPE_APPLICATION_PATH) {
|
||||
DebugPrint(L"application path \"");
|
||||
DebugPrint((PWSTR)((PUCHAR)Option + Option->DataOffset));
|
||||
DebugPrint(L"\"\r\n");
|
||||
} else {
|
||||
DebugPrintf(L"type %x, data size %x\r\n", Option->Type, Option->DataSize);
|
||||
}
|
||||
|
||||
if (Option->NextOptionOffset == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
Option = (PBOOT_APPLICATION_OPTION)((PUCHAR)Option + Option->NextOptionOffset);
|
||||
}
|
||||
#endif
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
89
BOOT/ENVIRON/LIB/bootopt.c
Normal file
89
BOOT/ENVIRON/LIB/bootopt.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/*++
|
||||
|
||||
Copyright (c) 2024, Quinn Stephens.
|
||||
Provided under the BSD 3-Clause license.
|
||||
|
||||
Module Name:
|
||||
|
||||
bootopt.c
|
||||
|
||||
Abstract:
|
||||
|
||||
Provides boot option utilities.
|
||||
|
||||
--*/
|
||||
|
||||
#include "bootlib.h"
|
||||
|
||||
ULONG
|
||||
BlGetBootOptionSize (
|
||||
IN PBOOT_APPLICATION_OPTION Option
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Gets the size of a boot option.
|
||||
|
||||
Arguments:
|
||||
|
||||
Option - the boot option to get the size of.
|
||||
|
||||
Return Value:
|
||||
|
||||
The size of the option.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
ULONG TotalSize;
|
||||
|
||||
if (Option->DataOffset != 0) {
|
||||
TotalSize = Option->DataOffset + Option->DataSize;
|
||||
} else {
|
||||
TotalSize = sizeof(BOOT_APPLICATION_OPTION);
|
||||
}
|
||||
|
||||
if (Option->OtherOptionsOffset != 0) {
|
||||
TotalSize += BlGetBootOptionListSize((PBOOT_APPLICATION_OPTION)((PUCHAR)Option + Option->OtherOptionsOffset));
|
||||
}
|
||||
|
||||
return TotalSize;
|
||||
}
|
||||
|
||||
ULONG
|
||||
BlGetBootOptionListSize (
|
||||
IN PBOOT_APPLICATION_OPTION Options
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Gets the total size of a list boot options.
|
||||
|
||||
Arguments:
|
||||
|
||||
Options - the boot option list to get the size of.
|
||||
|
||||
Return Value:
|
||||
|
||||
The size of the options.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
ULONG TotalSize, NextOffset;
|
||||
PBOOT_APPLICATION_OPTION Option;
|
||||
|
||||
TotalSize = 0;
|
||||
NextOffset = 0;
|
||||
do {
|
||||
Option = (PBOOT_APPLICATION_OPTION)((PUCHAR)Options + NextOffset);
|
||||
NextOffset = Option->NextOptionOffset;
|
||||
TotalSize += BlGetBootOptionSize(Option);
|
||||
} while (NextOffset != 0);
|
||||
|
||||
return TotalSize;
|
||||
}
|
18
BOOT/ENVIRON/README.md
Normal file
18
BOOT/ENVIRON/README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# Boot Environment Source Files
|
||||
|
||||
> XX = anything
|
||||
|
||||
| File | Public Routines |
|
||||
|-|-|
|
||||
| APP/BOOTMGR/EFI/efientry.c | EfiMain() |
|
||||
| APP/BOOTMGR/bootmgr.c | BmMain() |
|
||||
| APP/BOOTMGR/bcd.c | BmXXDataStore() |
|
||||
| LIB/EFI/efifw.c | BlpFwXX() |
|
||||
| LIB/EFI/efimm.c | MmFwXX() |
|
||||
| LIB/EFI/efiinit.c | EfiInitXX() |
|
||||
| LIB/EFI/eficon.c | ConsoleXX() |
|
||||
| LIB/EFI/efilib.c | EfiGetEfiStatusCode() |
|
||||
| LIB/MM/mm.c | BlpMmXX() |
|
||||
| LIB/MM/mmpa.c | MmPaXX() |
|
||||
| LIB/bootlib.c | BlXXLibrary() |
|
||||
| LIB/bootopt.c | BlXXBootOptionXX() |
|
210
NTOSKRNL/CC/ccexternalcache.cpp
Normal file
210
NTOSKRNL/CC/ccexternalcache.cpp
Normal file
@@ -0,0 +1,210 @@
|
||||
/*
|
||||
* PROJECT: Alcyone System Kernel
|
||||
* LICENSE: BSD Clause 3
|
||||
* PURPOSE: Cache Controller:: External Cache Compatibility Layer
|
||||
* NT KERNEL: 5.11.9360
|
||||
* COPYRIGHT: 2023-2029 Dibymartanda Samanta <>
|
||||
*/
|
||||
|
||||
NOTE::Alcyone will not notify,in case of Empty External Cache at the moment, feature is planned for threadpool revamp
|
||||
|
||||
/* Move Typedef later cctypes.hpp */
|
||||
typedef struct alignas(24) _DIRTY_PAGE_STATISTICS {
|
||||
ULONGLONG DirtyPages; // 0x0
|
||||
ULONGLONG DirtyPagesLastScan; // 0x8
|
||||
ULONG DirtyPagesScheduledLastScan; // 0x10
|
||||
} DIRTY_PAGE_STATISTICS, *PDIRTY_PAGE_STATISTICS; // 0x18 bytes (sizeof)
|
||||
|
||||
typedef struct alignas(30) _CC_EXTERNAL_CACHE_INFO {
|
||||
VOID (*Callback)(VOID* arg1, ULONGLONG arg2, ULONG arg3); // 0x0
|
||||
struct _DIRTY_PAGE_STATISTICS DirtyPageStatistics; // 0x8
|
||||
struct _LIST_ENTRY Links; // 0x20
|
||||
} CC_EXTERNAL_CACHE_INFO, *PCC_EXTERNAL_CACHE_INFO; // 0x30 bytes (sizeof)
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
CcDeductDirtyPagesFromExternalCache(
|
||||
IN PCC_EXTERNAL_CACHE_INFO ExternalCacheContext,
|
||||
IN ULONG Pages
|
||||
)
|
||||
{
|
||||
KIRQL OldIrql ={0};
|
||||
ULONG PagesToDeduct = {0};
|
||||
|
||||
|
||||
if (Pages > 0)
|
||||
{
|
||||
/* Acquire the master lock */
|
||||
KeAcquireQueuedSpinLock(LockQueueMasterLock, &OldIrql);
|
||||
|
||||
/* Calculate the number of pages to deduct */
|
||||
PagesToDeduct = min(Pages, ExternalCacheContext->DirtyPageStatistics.DirtyPages);
|
||||
|
||||
/* Deduct the pages from the external cache context */
|
||||
ExternalCacheContext->DirtyPageStatistics.DirtyPages -= PagesToDeduct;
|
||||
|
||||
/* Deduct the pages from the global statistics */
|
||||
CcTotalDirtyPages -= PagesToDeduct;
|
||||
|
||||
/* Release the master lock */
|
||||
KeReleaseQueuedSpinLock(LockQueueMasterLock, OldIrql);
|
||||
}
|
||||
|
||||
/* Check if there are any deferred writes and post them if necessary */
|
||||
if (!IsListEmpty(&CcDeferredWrites))
|
||||
{
|
||||
CcPostDeferredWrites();
|
||||
}
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
CcAddExternalCache(
|
||||
IN PCC_EXTERNAL_CACHE_INFO CacheInfo
|
||||
)
|
||||
{
|
||||
|
||||
|
||||
KLOCK_QUEUE_HANDLE LockHandle ={0};
|
||||
|
||||
/* Acquire the external cache list lock */
|
||||
KeAcquireInStackQueuedSpinLock(&CcExternalCacheListLock,&LockHandle);
|
||||
|
||||
/* Insert the new cache info at the end of the list */
|
||||
InsertTailList(&CcExternalCacheList, &CacheInfo->Links);
|
||||
|
||||
/* Increment the number of external caches */
|
||||
if (_InterlockedIncrement(&CcNumberOfExternalCaches) <= 0)
|
||||
{
|
||||
/* Overflow occurred, bug check */
|
||||
KeBugCheckEx(CACHE_MANAGER,0,STATUS_INTEGER_OVERFLOW,0,0);
|
||||
}
|
||||
|
||||
/* Release the external cache list lock */
|
||||
KeReleaseInStackQueuedSpinLock(&LockHandle);
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
CcRemoveExternalCache(
|
||||
IN PCC_EXTERNAL_CACHE_INFO CacheInfo
|
||||
)
|
||||
{
|
||||
|
||||
|
||||
|
||||
KLOCK_QUEUE_HANDLE LockHandle ={0};
|
||||
|
||||
/* Acquire the external cache list lock */
|
||||
KeAcquireInStackQueuedSpinLock(&CcExternalCacheListLock,&LockHandle);
|
||||
|
||||
/* Remove the entry from the list */
|
||||
RemoveTailList(&CacheInfo->Links.Blink);
|
||||
|
||||
/* Decrement the number of external caches */
|
||||
if (_InterlockedDecrement(&CcNumberOfExternalCaches) < 0)
|
||||
{
|
||||
/* Underflow occurred, bug check */
|
||||
KeBugCheckEx(CACHE_MANAGER,0,STATUS_INTEGER_OVERFLOW,0,0);
|
||||
}
|
||||
|
||||
/* Release the external cache list lock */
|
||||
KeReleaseInStackQueuedSpinLock(&LockHandle);
|
||||
}
|
||||
|
||||
|
||||
/*Exported */
|
||||
VOID
|
||||
NTAPI
|
||||
CcAddDirtyPagesToExternalCache(
|
||||
IN PCC_EXTERNAL_CACHE_INFO ExternalCacheContext,
|
||||
IN ULONG Pages
|
||||
)
|
||||
{
|
||||
KIRQL OldIrql ={0};
|
||||
|
||||
|
||||
/* Only proceed if there are pages to add */
|
||||
if (Pages == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* Acquire the master lock */
|
||||
KeAcquireQueuedSpinLock(LockQueueMasterLock, &OldIrql);
|
||||
|
||||
/* If this is the first dirty page, schedule a lazy write scan */
|
||||
if (ExternalCacheContext->DirtyPageStatistics.DirtyPages == 0)
|
||||
{
|
||||
CcScheduleLazyWriteScan(FALSE, NULL);
|
||||
}
|
||||
|
||||
/* Increment the dirty page count */
|
||||
ExternalCacheContext->DirtyPageStatistics.DirtyPages += Pages;
|
||||
|
||||
/* Charge the dirty pages */
|
||||
CcChargeDirtyPages(NULL, NULL, NULL, Pages);
|
||||
|
||||
/* Release the master lock */
|
||||
KeReleaseQueuedSpinLock(LockQueueMasterLock, OldIrql);
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
CcUnregisterExternalCache(
|
||||
IN PCC_EXTERNAL_CACHE_INFO ExternalCacheContext
|
||||
)
|
||||
{
|
||||
|
||||
|
||||
/* Remove the external cache from the list */
|
||||
CcRemoveExternalCache(ExternalCacheContext);
|
||||
|
||||
/* Deduct any remaining dirty pages */
|
||||
if (ExternalCacheContext->DirtyPageStatistics.DirtyPages > 0)
|
||||
{
|
||||
CcDeductDirtyPagesFromExternalCache(ExternalCacheContext,ExternalCacheContext->DirtyPageStatistics.DirtyPages);
|
||||
}
|
||||
|
||||
/* Free the external cache context */
|
||||
ExFreePoolWithTag(ExternalCacheContext, CC_EXTERNAL_CACHE_INFO_TAG);
|
||||
}
|
||||
|
||||
NTSTATUS
|
||||
NTAPI
|
||||
CcRegisterExternalCache(
|
||||
IN PCC_EXTERNAL_CACHE_CALLBACK Callback,
|
||||
OUT PVOID *ExternalCacheContext
|
||||
)
|
||||
{
|
||||
NTSTATUS Status = STATUS_SUCCESS;
|
||||
PCC_EXTERNAL_CACHE_INFO CacheInfo;
|
||||
|
||||
|
||||
/* Ensure the Cache Manager is initialized */
|
||||
if (!CcInitializationComplete)
|
||||
{
|
||||
KeBugCheckEx(CACHE_MANAGER,0,STATUS_UNSUCCESSFUL,0,0);
|
||||
}
|
||||
|
||||
/* Allocate memory for the external cache info structure */
|
||||
CacheInfo = ExAllocatePoolWithTag(NonPagedPool,
|
||||
sizeof(CC_EXTERNAL_CACHE_INFO),
|
||||
CC_EXTERNAL_CACHE_INFO_TAG);
|
||||
if (CacheInfo == NULL)
|
||||
{
|
||||
return STATUS_INSUFFICIENT_RESOURCES;
|
||||
}
|
||||
|
||||
/* Initialize the cache info structure */
|
||||
RtlZeroMemory(CacheInfo, sizeof(CC_EXTERNAL_CACHE_INFO));
|
||||
CacheInfo->Callback = Callback;
|
||||
|
||||
/* Add the external cache to the list */
|
||||
CcAddExternalCache(CacheInfo);
|
||||
|
||||
/* Return the cache info as the context */
|
||||
*ExternalCacheContext = CacheInfo;
|
||||
|
||||
return Status;
|
||||
}
|
@@ -13,341 +13,7 @@
|
||||
|
||||
extern "C"
|
||||
|
||||
/* Move Typedef Later to cctypes.hpp */
|
||||
typedef struct _WRITE_BEHIND_THROUGHPUT
|
||||
{
|
||||
ULONG PagesYetToWrite;
|
||||
ULONG Throughput;
|
||||
} WRITE_BEHIND_THROUGHPUT, *PWRITE_BEHIND_THROUGHPUT;
|
||||
|
||||
BOOLEAN
|
||||
CcOkToAddWriteBehindThread(VOID)
|
||||
{
|
||||
ULONG ActiveExtraWriteBehindThreads = {0};
|
||||
PWRITE_BEHIND_THROUGHPUT ThroughputStats = nullptr;
|
||||
ULONG PagesYetToWrite = {0};
|
||||
ULONG Throughput = {0};
|
||||
ULONG PreviousThroughput = {0};
|
||||
LONG ThroughputTrend = {0};
|
||||
BOOLEAN Result = false;
|
||||
|
||||
ActiveExtraWriteBehindThreads = CcActiveExtraWriteBehindThreads;
|
||||
ThroughputStats = CcThroughputStats;
|
||||
PagesYetToWrite = CcPagesYetToWrite;
|
||||
|
||||
Throughput = ThroughputStats[ActiveExtraWriteBehindThreads].PagesYetToWrite;
|
||||
PreviousThroughput = 0;
|
||||
|
||||
if (Throughput >= PagesYetToWrite)
|
||||
{
|
||||
Throughput -= PagesYetToWrite;
|
||||
}
|
||||
else
|
||||
{
|
||||
Throughput = 0;
|
||||
}
|
||||
|
||||
ThroughputStats[ActiveExtraWriteBehindThreads].PagesYetToWrite = PagesYetToWrite;
|
||||
Result = true;
|
||||
|
||||
if (ActiveExtraWriteBehindThreads > 0)
|
||||
{
|
||||
PreviousThroughput = ThroughputStats[ActiveExtraWriteBehindThreads - 1].Throughput;
|
||||
}
|
||||
|
||||
ThroughputStats[ActiveExtraWriteBehindThreads].Throughput = Throughput;
|
||||
|
||||
if (Throughput > 0)
|
||||
{
|
||||
ThroughputTrend = CcThroughputTrend;
|
||||
|
||||
if (Throughput < PreviousThroughput)
|
||||
{
|
||||
if (ThroughputTrend > 0)
|
||||
ThroughputTrend = 0;
|
||||
ThroughputTrend--;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ThroughputTrend < 0)
|
||||
ThroughputTrend = 0;
|
||||
ThroughputTrend++;
|
||||
}
|
||||
|
||||
CcThroughputTrend = ThroughputTrend;
|
||||
|
||||
if (ThroughputTrend == 3)
|
||||
{
|
||||
CcThroughputTrend = 0;
|
||||
Result = true;
|
||||
|
||||
if (ActiveExtraWriteBehindThreads < CcMaxExtraWriteBehindThreads)
|
||||
{
|
||||
ThroughputStats[ActiveExtraWriteBehindThreads + 1].Throughput = 0;
|
||||
ThroughputStats[ActiveExtraWriteBehindThreads + 1].PagesYetToWrite = PagesYetToWrite;
|
||||
}
|
||||
}
|
||||
else if (ThroughputTrend == -3)
|
||||
{
|
||||
CcThroughputTrend = 0;
|
||||
Result = true;
|
||||
|
||||
if (ActiveExtraWriteBehindThreads > 0)
|
||||
{
|
||||
ThroughputStats[ActiveExtraWriteBehindThreads - 1].Throughput = 0;
|
||||
ThroughputStats[ActiveExtraWriteBehindThreads - 1].PagesYetToWrite = PagesYetToWrite;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Result;
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
CcSetLazyWriteScanQueued(
|
||||
IN ULONG FlushReason,
|
||||
IN BOOLEAN QueuedState)
|
||||
{
|
||||
switch (FlushReason)
|
||||
{
|
||||
case 1:
|
||||
LazyWriter.PendingLowMemoryScan = QueuedState;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
LazyWriter.PendingPowerScan = QueuedState;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
LazyWriter.PendingPeriodicScan = QueuedState;
|
||||
break;
|
||||
|
||||
case 8:
|
||||
LazyWriter.PendingTeardownScan = QueuedState;
|
||||
break;
|
||||
|
||||
case 16:
|
||||
LazyWriter.PendingCoalescingFlushScan = QueuedState;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
BOOLEAN
|
||||
VECTORCALL
|
||||
CcIsLazyWriteScanQueued(
|
||||
_In_ ULONG ReasonForFlush
|
||||
)
|
||||
{
|
||||
switch (ReasonForFlush)
|
||||
{
|
||||
case 0:
|
||||
return false;
|
||||
|
||||
case 1:
|
||||
case 2:
|
||||
case 16:
|
||||
if (LazyWriter.PendingLowMemoryScan ||
|
||||
LazyWriter.PendingPowerScan ||
|
||||
LazyWriter.PendingCoalescingFlushScan)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
case 4:
|
||||
if (LazyWriter.PendingPeriodicScan ||
|
||||
LazyWriter.PendingTeardownScan)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
case 8:
|
||||
return (BOOLEAN)LazyWriter.PendingTeardownScan;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
CcQueueLazyWriteScanThread(
|
||||
_In_ PVOID NULL_PARAM
|
||||
)
|
||||
{
|
||||
UNREFERENCED_PARAMETER(NULL_PARAM);
|
||||
|
||||
ULONG Reason = 0;
|
||||
BOOLEAN NeedAdjustment;
|
||||
NTSTATUS Status;
|
||||
KIRQL OldIrql;
|
||||
PWORK_QUEUE_ENTRY WorkQueueEntry;
|
||||
PVOID WaitObjects[5];
|
||||
|
||||
WaitObjects[0] = &CcLowMemoryEvent;
|
||||
WaitObjects[1] = &CcPowerEvent;
|
||||
WaitObjects[2] = &CcPeriodicEvent;
|
||||
WaitObjects[3] = &CcWaitingForTeardownEvent;
|
||||
WaitObjects[4] = &CcCoalescingFlushEvent;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
NeedAdjustment = false;
|
||||
|
||||
Status = KeWaitForMultipleObjects(
|
||||
5,
|
||||
WaitObjects,
|
||||
WaitAny,
|
||||
WrFreePage,
|
||||
KernelMode,
|
||||
false,
|
||||
NULL,
|
||||
WaitBlockArray);
|
||||
|
||||
switch (Status)
|
||||
{
|
||||
case STATUS_WAIT_0:
|
||||
Reason = 1;
|
||||
NeedAdjustment = true;
|
||||
break;
|
||||
|
||||
case STATUS_WAIT_1:
|
||||
Reason = 2;
|
||||
break;
|
||||
|
||||
case STATUS_WAIT_2:
|
||||
Reason = 4;
|
||||
break;
|
||||
|
||||
case STATUS_WAIT_3:
|
||||
Reason = 8;
|
||||
break;
|
||||
|
||||
case STATUS_WAIT_4:
|
||||
Reason = 16;
|
||||
break;
|
||||
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
|
||||
if (CcNumberOfExternalCaches && !IsListEmpty(&CcExternalCacheList))
|
||||
{
|
||||
CcNotifyExternalCaches(Reason);
|
||||
}
|
||||
|
||||
CcAdjustWriteBehindThreadPoolIfNeeded(NeedAdjustment);
|
||||
|
||||
OldIrql = KeAcquireQueuedSpinLock(LockQueueMasterLock);
|
||||
|
||||
if (CcIsLazyWriteScanQueued(Reason))
|
||||
{
|
||||
KeReleaseQueuedSpinLock(LockQueueMasterLock, OldIrql);
|
||||
continue;
|
||||
}
|
||||
|
||||
CcSetLazyWriteScanQueued(Reason, true);
|
||||
KeReleaseQueuedSpinLock(LockQueueMasterLock, OldIrql);
|
||||
|
||||
if (!NT_SUCCESS(CcAllocateWorkQueueEntry(&WorkQueueEntry)))
|
||||
{
|
||||
KSPIN_LOCK_QUEUE_NUMBER queueNumber = LockQueueMasterLock;
|
||||
SpinLockGuard guard(queueNumber);
|
||||
LazyWriter.ScanActive = false;
|
||||
CcSetLazyWriteScanQueued(Reason, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
WorkQueueEntry->Function = 3;
|
||||
WorkQueueEntry->Parameters.Notification.Reason = Reason;
|
||||
|
||||
CcPostWorkQueue(
|
||||
WorkQueueEntry,
|
||||
(Reason != 8) ? &CcRegularWorkQueue : &CcFastTeardownWorkQueue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
CcRescheduleLazyWriteScan( IN PLARGE_INTEGER NextScanDelay)
|
||||
{
|
||||
UNREFERENCED_PARAMETER(NextScanDelay);
|
||||
LARGE_INTEGER Delay = {0};
|
||||
|
||||
if (LazyWriter.ScanActive)
|
||||
{
|
||||
|
||||
if (NextScanDelay && NextScanDelay->QuadPart != 0x7FFFFFFFFFFFFFFF && NextScanDelay->QuadPart != 0)
|
||||
{
|
||||
Delay.QuadPart = NextScanDelay->QuadPart * KeMaximumIncrement;
|
||||
if (Delay.QuadPart > 160000000)
|
||||
Delay.QuadPart = 160000000;
|
||||
if(Delay.QuadPart < 10000000 )
|
||||
Delay = CcIdleDelay;
|
||||
}
|
||||
KeSetCoalescableTimer(&LazyWriter.ScanTimer,CcIdleDelay,0,1000,&LazyWriter.ScanDpc);
|
||||
}
|
||||
else
|
||||
{
|
||||
CcScheduleLazyWriteScan(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
CcComputeNextScanTime(PLARGE_INTEGER OldestTICKTIMEForMetadata, PLARGE_INTEGER NextScanDelay)
|
||||
{
|
||||
NextScanDelay- = 0;
|
||||
LARGE_INTEGER CurrentTickCount = {0};
|
||||
LARGE_INTEGER TICKTIME = {0};
|
||||
LARGE_INTEGER WRITE_DELAY = {0};
|
||||
LARGE_INTEGER TICK_ELAPSED = {0};
|
||||
|
||||
if (CcMaxWriteBehindThreads < CcNumberofWorkerThreads)
|
||||
{
|
||||
|
||||
|
||||
KeQueryTickCount(&CurrentTickCount);
|
||||
|
||||
// Calculate Tick Time based on the current tick count and the oldest scan time
|
||||
TICKTIME.QuadPart = 160000000 / KeMaximumIncrement;
|
||||
WRITE_DELAY.QuadPart = (OldestTICKTIMEForMetadata->QuadPart - CurrentTickCount.QuadPart) / KeMaximumIncrement;
|
||||
|
||||
// Increment the consecutive workless lazy scan count
|
||||
++CcConsecutiveWorklessLazyScanCount;
|
||||
|
||||
// Check if the oldest scan time is not the maximum and the calculated delay is greater than the current tick
|
||||
// count
|
||||
if (OldestTICKTIMEForMetadata->QuadPart != -1 && OldestTICKTIMEForMetadata->QuadPart != 0x7FFFFFFFFFFFFFFF &&
|
||||
(TICKTIME.QuadPart + OldestTICKTIMEForMetadata->QuadPart) > CurrentTickCount.QuadPart)
|
||||
{
|
||||
|
||||
TICK_ELAPSED.QuadPart = OldestTICKTIMEForMetadata->QuadPart - CurrentTickCount.QuadPart;
|
||||
|
||||
// Calculate the next scan delay
|
||||
NextScanDelay->QuadPart = TICKTIME.QuadPart + TICK_ELAPSED.QuadPart;
|
||||
|
||||
// Reset the consecutive workless lazy scan count
|
||||
CcConsecutiveWorklessLazyScanCount = 0;
|
||||
}
|
||||
|
||||
// Check if the number of consecutive workless lazy scans has reached the maximum
|
||||
if (CcConsecutiveWorklessLazyScanCount >= CcMaxWorklessLazywriteScans)
|
||||
{
|
||||
// Disable the scan by setting the next scan delay to the maximum values
|
||||
NextScanDelay->QuadPart = -1;
|
||||
CcConsecutiveWorklessLazyScanCount = 0;
|
||||
NextScanDelay->HighPart = 0x7FFFFFFF;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*Internal Function*/
|
||||
|
||||
VOID
|
||||
VECTORCALL
|
||||
@@ -1234,10 +900,10 @@ NTAPI CcLazyWriteScan()
|
||||
|
||||
CcPostWorkQueue(workItem, &CcRegularWorkQueue);
|
||||
}
|
||||
CcComputeNextScanTime(&OldestLWSTimeStamp, &NextScanDelay);
|
||||
// CcComputeNextScanTime(&OldestLWSTimeStamp, &NextScanDelay); Enable When Threadpool is finished
|
||||
|
||||
if (!IsListEmpty(&PostWorkList) || !IsListEmpty(&CcDeferredWrites) || MmRegistryStatus.ProductStatus ||
|
||||
NextScanDelay.QuadPart != 0x7FFFFFFFFFFFFFFF))
|
||||
// if (!IsListEmpty(&PostWorkList) || !IsListEmpty(&CcDeferredWrites) || MmRegistryStatus.ProductStatus ||NextScanDelay.QuadPart != 0x7FFFFFFFFFFFFFFF))
|
||||
if (!IsListEmpty(&PostWorkList) || !IsListEmpty(&CcDeferredWrites) || MmRegistryStatus.ProductStatus))
|
||||
{
|
||||
/* Schedule a lazy write scan */
|
||||
CcRescheduleLazyWriteScan(&NextScanDelay);
|
||||
@@ -1264,7 +930,44 @@ NTAPI CcLazyWriteScan()
|
||||
CcScheduleLazyWriteScan(FALSE);
|
||||
}
|
||||
}
|
||||
NTSTATUS CcWaitForCurrentLazyWriterActivity()
|
||||
{
|
||||
NTSTATUS result;
|
||||
PWORK_QUEUE_ENTRY WorkQueueEntry = nullptr;
|
||||
KEVENT Event = {0};
|
||||
KIRQL irql = {0};
|
||||
|
||||
result = CcAllocateWorkQueueEntry(&WorkQueueEntry);
|
||||
if (NT_SUCCESS(result))
|
||||
{
|
||||
WorkQueueEntry->Function = SetDone;
|
||||
KeInitializeEvent(&Event, NotificationEvent, FALSE);
|
||||
WorkQueueEntry->Parameters.Notification.Reason = (ULONG_PTR)&Event;
|
||||
|
||||
if ((PerfGlobalGroupMask.Masks[4] & 0x20000) != 0)
|
||||
CcPerfLogWorkItemEnqueue(&CcPostTickWorkQueue, WorkQueueEntry, 0, 0);
|
||||
|
||||
irql = KeAcquireQueuedSpinLock(LockQueueMasterLock);
|
||||
|
||||
WorkQueueEntry->WorkQueueLinks.Flink = &CcPostTickWorkQueue;
|
||||
WorkQueueEntry->WorkQueueLinks.Blink = CcPostTickWorkQueue.Blink;
|
||||
CcPostTickWorkQueue.Blink->Flink = &WorkQueueEntry->WorkQueueLinks;
|
||||
CcPostTickWorkQueue.Blink = &WorkQueueEntry->WorkQueueLinks;
|
||||
|
||||
LazyWriter.OtherWork = 1;
|
||||
_InterlockedIncrement(&CcPostTickWorkItemCount);
|
||||
|
||||
CcScheduleLazyWriteScan(1, 1);
|
||||
|
||||
KeReleaseQueuedSpinLock(LockQueueMasterLock, irql);
|
||||
|
||||
result = KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
|
||||
|
||||
_InterlockedDecrement(&CcPostTickWorkItemCount);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
VOID VECTORCALL CcReEngageWorkerThreads(
|
||||
ULONG NormalThreadsToActivate,
|
||||
ULONG ExtraWriteBehindThreadsToActivate
|
||||
@@ -1333,62 +1036,6 @@ VOID VECTORCALL CcReEngageWorkerThreads(
|
||||
ExQueueWorkItem(currentExtraThreadEntry, CriticalWorkQueue);
|
||||
}
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
CcAdjustWriteBehindThreadPool(
|
||||
IN BOOLEAN IsThreadPriorityLow)
|
||||
{
|
||||
if (IsThreadPriorityLow)
|
||||
{
|
||||
CcMaxNumberOfWriteBehindThreads = 1;
|
||||
if (CcAddExtraWriteBehindThreads)
|
||||
{
|
||||
CcAddExtraWriteBehindThreads = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CcMaxNumberOfWriteBehindThreads = (ULONG)-1;
|
||||
if (!IsListEmpty(&CcRegularWorkQueue) && !CcQueueThrottle)
|
||||
{
|
||||
CcReEngageWorkerThreads(CcNumberofWorkerThreads, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
VOID
|
||||
NTAPI
|
||||
CcAdjustWriteBehindThreadPoolIfNeeded(
|
||||
IN BOOLEAN NeedAdjustment)
|
||||
{
|
||||
BOOLEAN NeedBoost = false;
|
||||
KSPIN_LOCK_QUEUE_NUMBER queueNumber = LockQueueMasterLock;
|
||||
SpinLockGuard guard(queueNumber);
|
||||
|
||||
if (CcPostTickWorkItemCount != 0)
|
||||
{
|
||||
if (CcIsWriteBehindThreadpoolAtLowPriority())
|
||||
{
|
||||
CcAdjustWriteBehindThreadPool(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{ auto DirtyPages = (CcTotalDirtyPages + CcPagesWrittenLastTime);
|
||||
if (DirtyPages > 0x2000 || NeedAdjustment)
|
||||
{
|
||||
if (CcIsWriteBehindThreadpoolAtLowPriority())
|
||||
{
|
||||
CcAdjustWriteBehindThreadPool(false);
|
||||
|
||||
}
|
||||
}
|
||||
else if (!CcExecutingWriteBehindWorkItems && IsListEmpty(&CcRegularWorkQueue))
|
||||
{
|
||||
CcAdjustWriteBehindThreadPool(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
VOID
|
||||
NTAPI
|
||||
CcWorkerThread(PVOID Parameter)
|
||||
@@ -1419,7 +1066,7 @@ CcWorkerThread(PVOID Parameter)
|
||||
{
|
||||
CcQueueThrottle = FALSE;
|
||||
DropThrottle = FALSE;
|
||||
CcReEngageWorkerThreads(CcThreadsActiveBeforeThrottle, CcExtraThreadsActiveBeforeThrottle);
|
||||
// CcReEngageWorkerThreads(CcThreadsActiveBeforeThrottle, CcExtraThreadsActiveBeforeThrottle); Enable When Threadpool is ready
|
||||
}
|
||||
|
||||
if (IoStatus.Information == 0x8A5E)
|
||||
|
@@ -20,6 +20,12 @@ constexpr ULONG MUTEX_READY_TO_BE_AQUIRED = 0;
|
||||
|
||||
/*Internal Function*/
|
||||
|
||||
/* Fast Mutex definitions */
|
||||
#define FM_LOCK_BIT 0x1
|
||||
#define FM_LOCK_BIT_V 0x0
|
||||
#define FM_LOCK_WAITER_WOKEN 0x2
|
||||
#define FM_LOCK_WAITER_INC 0x4
|
||||
|
||||
typedef struct _FAST_MUTEX
|
||||
{
|
||||
LONG Count; //0x0
|
||||
@@ -29,6 +35,8 @@ typedef struct _FAST_MUTEX
|
||||
ULONG OldIrql; //0x1c
|
||||
} FAST_MUTEX, *PFAST_MUTEX; //0x20 bytes (sizeof)
|
||||
|
||||
typedef PFAST_MUTEX PKGUARDED_MUTEX;
|
||||
|
||||
/*Internal Functio*/
|
||||
VOID
|
||||
FASTCALL
|
||||
@@ -36,9 +44,9 @@ KiAcquireFastMutex(
|
||||
_Inout_ PFAST_MUTEX Mutex
|
||||
)
|
||||
{
|
||||
LONG AcquireMarker;
|
||||
LONG AcquireBit;
|
||||
LONG OldCount;
|
||||
LONG AcquireMarker = {0};
|
||||
LONG AcquireBit = {0};
|
||||
LONG OldCount = {0};
|
||||
|
||||
PAGED_CODE();
|
||||
|
||||
@@ -49,7 +57,6 @@ KiAcquireFastMutex(
|
||||
AcquireMarker = 4;
|
||||
AcquireBit = 1;
|
||||
|
||||
AcquireLoop:
|
||||
while(true)
|
||||
{
|
||||
/* Read current count */
|
||||
@@ -66,7 +73,7 @@ AcquireLoop:
|
||||
|
||||
AcquireMarker = 2;
|
||||
AcquireBit = 3;
|
||||
goto AcquireLoop;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -81,6 +88,43 @@ AcquireLoop:
|
||||
}
|
||||
}
|
||||
|
||||
FASTCALL
|
||||
KeReleaseFastMutexContended(
|
||||
IN PFAST_MUTEX FastMutex,
|
||||
IN LONG OldValue)
|
||||
{
|
||||
BOOLEAN WakeWaiter = false;
|
||||
LONG NewValue = {0};
|
||||
PKTHREAD WokenThread = nullptr;
|
||||
KPRIORITY HandoffPriority = {0};
|
||||
|
||||
/* Loop until we successfully update the mutex state */
|
||||
for (;;)
|
||||
{
|
||||
WakeWaiter = false;
|
||||
NewValue = OldValue + FM_LOCK_BIT;
|
||||
|
||||
if (!(OldValue & FM_LOCK_WAITER_WOKEN))
|
||||
{
|
||||
NewValue = OldValue - FM_LOCK_BIT;
|
||||
WakeWaiter = true;
|
||||
}
|
||||
|
||||
LONG PreviousValue = InterlockedCompareExchange(&FastMutex->Lock, NewValue, OldValue);
|
||||
if (PreviousValue == OldValue)
|
||||
break;
|
||||
|
||||
OldValue = PreviousValue;
|
||||
}
|
||||
|
||||
if (WakeWaiter)
|
||||
{
|
||||
/* Wake up a waiter */
|
||||
KeSetEventBoostPriority(&FastMutex->Event);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Exported Function */
|
||||
|
||||
VOID
|
||||
@@ -89,7 +133,6 @@ KeInitializeFastMutex(
|
||||
_Out_ PFAST_MUTEX Mutex
|
||||
)
|
||||
{
|
||||
PAGED_CODE();
|
||||
|
||||
/* Initialize the mutex structure */
|
||||
RtlZeroMemory(Mutex, sizeof(FAST_MUTEX));
|
||||
@@ -127,7 +170,240 @@ KeTryToAcquireFastMutex(
|
||||
return Result;
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
KeEnterCriticalRegionAndAcquireFastMutexUnsafe(
|
||||
_In_ PFAST_MUTEX FastMutex)
|
||||
{
|
||||
PKTHREAD OwnerThread = nullptr;
|
||||
KeEnterCriticalRegion();
|
||||
|
||||
/* Get the current thread again (following the pseudocode) */
|
||||
OwnerThread = KeGetCurrentThread();
|
||||
|
||||
/* Try to acquire the FastMutex */
|
||||
if (_InterlockedBitTestAndReset(&FastMutex->Lock, 0))
|
||||
{
|
||||
/* FastMutex was free, we acquired it */
|
||||
FastMutex->Owner = OwnerThread;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* FastMutex was locked, we need to wait */
|
||||
KiAcquireFastMutex(FastMutex);
|
||||
FastMutex->Owner = OwnerThread;
|
||||
}
|
||||
}
|
||||
|
||||
VOID
|
||||
FASTCALL
|
||||
KeReleaseFastMutexUnsafeAndLeaveCriticalRegion(
|
||||
_In_ PFAST_MUTEX FastMutex)
|
||||
{
|
||||
LONG OldValue = {0};
|
||||
PKTHREAD CurrentThread = nullptr ;
|
||||
SHORT NewValue ={0};
|
||||
|
||||
/* Clear the owner */
|
||||
FastMutex->Owner = nullptr;
|
||||
|
||||
/* Try to release the FastMutex */
|
||||
OldValue = InterlockedCompareExchange(&FastMutex->Lock, 1, 0);
|
||||
if (OldValue != 0)
|
||||
{
|
||||
/* Contended case, call the contended release function */
|
||||
KeReleaseFastMutexContended(FastMutex, OldValue);
|
||||
}
|
||||
|
||||
/* leave critical region*/
|
||||
KeLeaveCriticalRegion();
|
||||
}
|
||||
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
KeAcquireFastMutex(
|
||||
_In_ PFAST_MUTEX FastMutex)
|
||||
{
|
||||
KIRQL OldIrql = {0};
|
||||
|
||||
/* Raise IRQL to APC_LEVEL */
|
||||
OldIrql = KeRaiseIrqlToSynchLevel();
|
||||
|
||||
/* Try to acquire the FastMutex */
|
||||
if (InterlockedBitTestAndReset(&FastMutex->Lock, 0) == 0)
|
||||
{
|
||||
/* We didn't acquire it, we'll have to wait */
|
||||
KiAcquireFastMutex(FastMutex);
|
||||
}
|
||||
|
||||
/* Set the owner thread and save the original IRQL */
|
||||
FastMutex->Owner = KeGetCurrentThread();
|
||||
FastMutex->OldIrql = OldIrql;
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
KeAcquireFastMutexUnsafe(
|
||||
_In_ PFAST_MUTEX FastMutex)
|
||||
{
|
||||
PKTHREAD CurrentThread = nullptr;
|
||||
|
||||
/* Get the current thread */
|
||||
CurrentThread = KeGetCurrentThread();
|
||||
|
||||
/* Try to acquire the FastMutex */
|
||||
if (!InterlockedBitTestAndReset(&FastMutex->Lock, 0))
|
||||
{
|
||||
/* FastMutex was locked, we need to wait */
|
||||
KiAcquireFastMutex(FastMutex);
|
||||
}
|
||||
|
||||
/* Set the owner */
|
||||
FastMutex->Owner = CurrentThread;
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
KeReleaseFastMutex(
|
||||
_Inout_ PFAST_MUTEX FastMutex
|
||||
)
|
||||
{
|
||||
KIRQL OldIrql ={0};
|
||||
LONG OldCount ={0};
|
||||
|
||||
FastMutex->Owner = nullptr;
|
||||
OldIrql = FastMutex->OldIrql;
|
||||
|
||||
OldCount = InterlockedExchangeAdd(&FastMutex->Count, 1);
|
||||
|
||||
if (OldCount != 0 &&
|
||||
(OldCount & 2) == 0 &&
|
||||
InterlockedCompareExchange(&FastMutex->Count, OldCount - 1, OldCount + 1) == OldCount + 1)
|
||||
{
|
||||
KeSetEvent(&FastMutex->Event, IO_NO_INCREMENT, FALSE);
|
||||
}
|
||||
|
||||
KeLowerIrql(OldIrql);
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
KeReleaseFastMutexUnsafe(
|
||||
_In_ PFAST_MUTEX FastMutex)
|
||||
{
|
||||
LONG OldValue = {0};
|
||||
|
||||
/* Clear the owner */
|
||||
FastMutex->Owner = nullptr;
|
||||
|
||||
/* Release the lock and get the old value */
|
||||
OldValue = InterlockedExchangeAdd(&FastMutex->Lock, 1);
|
||||
|
||||
/* Check if there were waiters */
|
||||
if (OldValue != 0)
|
||||
{
|
||||
/* Check if no waiter has been woken up yet */
|
||||
if ((OldValue & FM_LOCK_WAITER_WOKEN) == 0)
|
||||
{
|
||||
/* Try to wake up a waiter */
|
||||
if (OldValue + 1 == InterlockedCompareExchange(&FastMutex->Lock,
|
||||
OldValue - 1,
|
||||
OldValue + 1))
|
||||
{
|
||||
/* Wake up one waiter */
|
||||
KeSetEvent(&FastMutex->Event, IO_NO_INCREMENT, FALSE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*Guarded Mutexes in Modern NT behave just like Fast Mutexes with bit of protection */
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
KeInitializeGuardedMutex(_Out_ PKGUARDED_MUTEX GuardedMutex)
|
||||
{
|
||||
/* Initialize the GuardedMutex*/
|
||||
GuardedMutex->Count = 1;
|
||||
GuardedMutex->Owner = nullptr;
|
||||
GuardedMutex->Contention = 0;
|
||||
/* Initialize the Mutex Gate */
|
||||
KeInitializeEvent(&Mutex->Event, SynchronizationEvent, FALSE);
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
KeAcquireGuardedMutex(_Inout_ PKGUARDED_MUTEX Mutex)
|
||||
{
|
||||
PKTHREAD OwnerThread = KeGetCurrentThread();
|
||||
KeEnterGuardedRegion();
|
||||
if (!_Interlockedbittestandreset(&Mutex->Count, 0) )
|
||||
KiAcquireFastMutex(Mutex);
|
||||
Mutex->Owner = OwnerThread;
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
KeAcquireGuardedMutexUnsafe(
|
||||
_Inout_ PKGUARDED_MUTEX FastMutex
|
||||
)
|
||||
{
|
||||
PKTHREAD CurrentThread = nullptr;
|
||||
KeEnterGuardedRegion();
|
||||
CurrentThread = KeGetCurrentThread();
|
||||
|
||||
if (!_InterlockedBitTestAndReset(&FastMutex->Count, 0))
|
||||
{
|
||||
KiAcquireFastMutex(FastMutex);
|
||||
}
|
||||
|
||||
FastMutex->Owner = CurrentThread;
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
KeReleaseGuardedMutexUnsafe(
|
||||
_Inout_ PKGUARDED_MUTEX FastMutex
|
||||
)
|
||||
{
|
||||
LONG OldCount ={0};
|
||||
|
||||
FastMutex->Owner = nullptr;
|
||||
|
||||
OldCount = _InterlockedExchangeAdd(&FastMutex->Count, 1);
|
||||
|
||||
if (OldCount != 0 &&
|
||||
(OldCount & FM_LOCK_WAITER_WOKEN) == 0 &&
|
||||
OldCount + 1 == InterlockedCompareExchange(&FastMutex->Count, OldCount - 1, OldCount + 1))
|
||||
{
|
||||
KeSetEvent(&FastMutex->Event, IO_NO_INCREMENT, FALSE);
|
||||
}
|
||||
KeLeaveGuardedRegion();
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
KeReleaseGuardedMutex(
|
||||
_In_ PKGUARDED_MUTEX FastMutex)
|
||||
{
|
||||
KIRQL OldIrql ={0};
|
||||
LONG OldValue ={0};
|
||||
|
||||
/* Save the old IRQL and clear the owner */
|
||||
OldIrql = FastMutex->OldIrql;
|
||||
FastMutex->Owner = nullptr;
|
||||
|
||||
/* Try to release the FastMutex */
|
||||
OldValue = _InterlockedExchangeAdd(&Mutex->Count, 1);
|
||||
if (OldCount != 0 &&
|
||||
(OldCount & FM_LOCK_WAITER_WOKEN) == 0 &&
|
||||
OldCount + 1 == InterlockedCompareExchange(&FastMutex->Count, OldCount - 1, OldCount + 1))
|
||||
{
|
||||
KeSetEvent(&FastMutex->Event, IO_NO_INCREMENT, FALSE);
|
||||
}
|
||||
|
||||
/* Lower IRQL */
|
||||
KeLowerIrql(OldIrql);
|
||||
KeLeaveGuardedRegion();
|
||||
}
|
||||
|
@@ -34,7 +34,14 @@ Alcyone is written in a subset of C++11 with selected features from modern stand
|
||||
- XDDM: Components related to Display Driver Model
|
||||
|
||||
### SDK Source Directory Structure:
|
||||
- CRT: C RunTime
|
||||
- INC: Global header files
|
||||
- CRT: C RunTime source
|
||||
- RTL: Kernel Run-Time Library source
|
||||
|
||||
### Boot Source Directory Structure:
|
||||
- INC: Boot-specific header files
|
||||
- APP/BOOTMGR: Boot Manager source
|
||||
- LIB: Boot library routines and firmware abstractions
|
||||
|
||||
### Kernel Source Directory Structure:
|
||||
- ALPC: Asynchronous Local Procedure Call
|
||||
|
92
SDK/CRT/STDIO/wprintf.c
Normal file
92
SDK/CRT/STDIO/wprintf.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/*++
|
||||
|
||||
Copyright (c) 2024, Quinn Stephens.
|
||||
Provided under the BSD 3-Clause license.
|
||||
|
||||
Module Name:
|
||||
|
||||
printf.c
|
||||
|
||||
Abstract:
|
||||
|
||||
Provides wide formatted string printing routines.
|
||||
|
||||
--*/
|
||||
|
||||
#include <wchar.h>
|
||||
|
||||
static
|
||||
size_t
|
||||
print_hex (
|
||||
wchar_t *wcs,
|
||||
size_t maxlen,
|
||||
unsigned int num
|
||||
)
|
||||
|
||||
{
|
||||
wchar_t *dest;
|
||||
size_t n;
|
||||
int shift;
|
||||
unsigned int x;
|
||||
|
||||
dest = wcs;
|
||||
n = 0;
|
||||
shift = 28;
|
||||
while (n < maxlen && shift >= 0) {
|
||||
x = (num >> shift) & 0xf;
|
||||
if (x >= 0xa) {
|
||||
*dest = 'a' + (x - 0xa);
|
||||
} else {
|
||||
*dest = '0' + x;
|
||||
}
|
||||
|
||||
dest++;
|
||||
n++;
|
||||
shift -= 4;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
int
|
||||
vswprintf (
|
||||
wchar_t *wcs,
|
||||
size_t maxlen,
|
||||
const wchar_t *format,
|
||||
va_list args
|
||||
)
|
||||
|
||||
{
|
||||
wchar_t *dest;
|
||||
size_t n, size;
|
||||
|
||||
dest = wcs;
|
||||
n = 0;
|
||||
while (n < maxlen && *format != '\0') {
|
||||
if (*format != '%') {
|
||||
*dest++ = *format++;
|
||||
n++;
|
||||
continue;
|
||||
}
|
||||
|
||||
format++;
|
||||
switch (*format) {
|
||||
case 'x':
|
||||
size = print_hex(dest, maxlen - n, va_arg(args, unsigned int));
|
||||
n += size;
|
||||
dest += size;
|
||||
format++;
|
||||
break;
|
||||
case '\0':
|
||||
break;
|
||||
case '%':
|
||||
default:
|
||||
*dest++ = *format++;
|
||||
n++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
wcs[n] = '\0';
|
||||
return (int)n;
|
||||
}
|
@@ -22,6 +22,7 @@ Abstract:
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
size_t wcslen(const wchar_t *str);
|
||||
@@ -35,6 +36,8 @@ wchar_t *wmemset(wchar_t *dest, wchar_t c, size_t count);
|
||||
wchar_t *wmemcpy(wchar_t *dest, const wchar_t *src, size_t count);
|
||||
wchar_t *wmemmove(wchar_t *dest, const wchar_t *src, size_t count);
|
||||
|
||||
int vswprintf(wchar_t *wcs, size_t maxlen, const wchar_t *format, va_list args);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@@ -97,29 +97,6 @@ typedef unsigned long ULONG;
|
||||
#define MAXUSHORT 0xffff
|
||||
#define MAXULONG 0xffffffff
|
||||
|
||||
//
|
||||
// Basic pointer types.
|
||||
//
|
||||
typedef VOID *PVOID;
|
||||
typedef CHAR *PCHAR;
|
||||
typedef SHORT *PSHORT;
|
||||
typedef UCHAR *PUCHAR;
|
||||
typedef USHORT *PUSHORT;
|
||||
typedef ULONG *PULONG;
|
||||
|
||||
//
|
||||
// String types.
|
||||
//
|
||||
typedef CHAR *PSTR, *LPSTR;
|
||||
typedef CONST CHAR *PCSTR, *LPCSTR;
|
||||
|
||||
//
|
||||
// Wide character/string types.
|
||||
//
|
||||
typedef USHORT WCHAR;
|
||||
typedef WCHAR *PWCHAR, *PWSTR, *LPWSTR;
|
||||
typedef CONST WCHAR *PCWSTR, *LPCWSTR;
|
||||
|
||||
//
|
||||
// Long long types.
|
||||
//
|
||||
@@ -137,19 +114,64 @@ typedef unsigned long long ULONGLONG;
|
||||
typedef LONGLONG *PLONGLONG;
|
||||
typedef ULONGLONG *PULONGLONG;
|
||||
|
||||
#define MAXLONGLONG (0x7fffffffffffffff)
|
||||
#define MAXLONGLONG 0x7fffffffffffffff
|
||||
#define MAXULONGLONG 0xffffffffffffffff
|
||||
|
||||
//
|
||||
// Logical/boolean value types.
|
||||
//
|
||||
typedef ULONG LOGICAL;
|
||||
typedef ULONG *PLOGICAL;
|
||||
typedef int BOOL;
|
||||
typedef BOOL *PBOOL;
|
||||
typedef UCHAR BOOLEAN;
|
||||
typedef BOOLEAN *PBOOLEAN;
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
//
|
||||
// Basic pointer types.
|
||||
//
|
||||
typedef VOID *PVOID;
|
||||
typedef CHAR *PCHAR;
|
||||
typedef SHORT *PSHORT;
|
||||
typedef UCHAR *PUCHAR;
|
||||
typedef USHORT *PUSHORT;
|
||||
typedef ULONG *PULONG;
|
||||
|
||||
//
|
||||
// Numeric pointer types.
|
||||
//
|
||||
#ifdef _WIN64
|
||||
typedef LONGLONG LONG_PTR;
|
||||
typedef ULONGLONG ULONG_PTR;
|
||||
#else
|
||||
typedef LONG LONG_PTR;
|
||||
typedef ULONG ULONG_PTR;
|
||||
#endif
|
||||
|
||||
//
|
||||
// String types.
|
||||
//
|
||||
typedef CHAR *PSTR, *LPSTR;
|
||||
typedef CONST CHAR *PCSTR, *LPCSTR;
|
||||
|
||||
//
|
||||
// Wide character/string types.
|
||||
//
|
||||
typedef USHORT WCHAR;
|
||||
typedef WCHAR *PWCHAR, *PWSTR, *LPWSTR;
|
||||
typedef CONST WCHAR *PCWSTR, *LPCWSTR;
|
||||
|
||||
//
|
||||
// Handle types.
|
||||
//
|
||||
typedef PVOID HANDLE;
|
||||
typedef HANDLE *PHANDLE;
|
||||
|
||||
#define INVALID_HANDLE_VALUE ((HANDLE)(LONG_PTR)-1)
|
||||
|
||||
//
|
||||
// Status code types.
|
||||
//
|
||||
@@ -207,10 +229,44 @@ typedef union ULARGE_INTEGER {
|
||||
// Doubly-linked list entry.
|
||||
//
|
||||
typedef struct _LIST_ENTRY {
|
||||
struct _LIST_ENTRY *ForwardLink;
|
||||
struct _LIST_ENTRY *BackLink;
|
||||
struct _LIST_ENTRY *Flink;
|
||||
struct _LIST_ENTRY *Blink;
|
||||
} LIST_ENTRY, *PLIST_ENTRY;
|
||||
|
||||
FORCEINLINE
|
||||
BOOLEAN
|
||||
RemoveEntryList (
|
||||
IN PLIST_ENTRY Entry
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Removes a list entry from a list.
|
||||
|
||||
Arguments:
|
||||
|
||||
Entry - the entry to remove.
|
||||
|
||||
Return Value:
|
||||
|
||||
True if the list is now empty,
|
||||
False if the list still has at least one entry.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
PLIST_ENTRY Blink, Flink;
|
||||
|
||||
Blink = Entry->Blink;
|
||||
Flink = Entry->Flink;
|
||||
Blink->Flink = Flink;
|
||||
Flink->Blink = Blink;
|
||||
|
||||
return (BOOLEAN)(Flink == Blink);
|
||||
}
|
||||
|
||||
//
|
||||
// Unicode string.
|
||||
//
|
||||
|
@@ -18,6 +18,7 @@ Abstract:
|
||||
|
||||
#include <string.h>
|
||||
#include <ntdef.h>
|
||||
#include <ntstatus.h>
|
||||
|
||||
//
|
||||
// Memory operations.
|
||||
@@ -27,6 +28,47 @@ Abstract:
|
||||
#define RtlFillMemory(Destination, Length, Fill) memset((Destination), (Fill), (Length))
|
||||
#define RtlZeroMemory(Destination, Length) memset((Destination), 0, (Length))
|
||||
|
||||
#define ULONG_ERROR 0xFFFFFFFFUL
|
||||
|
||||
FORCEINLINE
|
||||
NTSTATUS
|
||||
RtlULongSub (
|
||||
IN ULONG ulMinuend,
|
||||
IN ULONG ulSubtrahend,
|
||||
IN OUT PULONG pulResult
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Calculates the difference of two ULONG values.
|
||||
|
||||
Arguments:
|
||||
|
||||
ulMinuend - The value to subtract ulSubtrahend from.
|
||||
|
||||
ulSubtrahend - The value to subtract from ulMinuend.
|
||||
|
||||
pulResult - Pointer to a ULONG to store the difference in.
|
||||
|
||||
Return Value:
|
||||
|
||||
STATUS_SUCCESS if successful.
|
||||
STATUS_INTEGER_OVERFLOW if unsuccessful.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
if (ulMinuend >= ulSubtrahend) {
|
||||
*pulResult = ulMinuend - ulSubtrahend;
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
*pulResult = ULONG_ERROR;
|
||||
return STATUS_INTEGER_OVERFLOW;
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
RtlInitUnicodeString (
|
||||
|
@@ -22,16 +22,31 @@ Abstract:
|
||||
// TODO: There are an insane amount of status values.
|
||||
//
|
||||
#define STATUS_MEDIA_CHANGED ((NTSTATUS) 0x8000001CL)
|
||||
#define STATUS_UNSUCCESSFUL ((NTSTATUS) 0xC0000001L)
|
||||
#define STATUS_NOT_IMPLEMENTED ((NTSTATUS) 0xC0000002L)
|
||||
#define STATUS_INVALID_PARAMETER ((NTSTATUS) 0xC000000DL)
|
||||
#define STATUS_ACCESS_DENIED ((NTSTATUS) 0xC0000022L)
|
||||
#define STATUS_BUFFER_TOO_SMALL ((NTSTATUS) 0xC0000023L)
|
||||
#define STATUS_DISK_CORRUPT_ERROR ((NTSTATUS) 0xC0000032L)
|
||||
#define STATUS_DEVICE_ALREADY_ATTACHED ((NTSTATUS) 0xC0000038L)
|
||||
#define STATUS_DISK_FULL ((NTSTATUS) 0xC000007FL)
|
||||
#define STATUS_INTEGER_OVERFLOW ((NTSTATUS) 0xC0000095L)
|
||||
#define STATUS_INSUFFICIENT_RESOURCES ((NTSTATUS) 0xC000009AL)
|
||||
#define STATUS_MEDIA_WRITE_PROTECTED ((NTSTATUS) 0xC00000A2L)
|
||||
#define STATUS_DEVICE_NOT_READY ((NTSTATUS) 0xC00000A3L)
|
||||
#define STATUS_NOT_SUPPORTED ((NTSTATUS) 0xC00000BBL)
|
||||
#define STATUS_INVALID_PARAMETER_1 ((NTSTATUS) 0xC00000EFL)
|
||||
#define STATUS_INVALID_PARAMETER_2 ((NTSTATUS) 0xC00000F0L)
|
||||
#define STATUS_INVALID_PARAMETER_3 ((NTSTATUS) 0xC00000F1L)
|
||||
#define STATUS_INVALID_PARAMETER_4 ((NTSTATUS) 0xC00000F2L)
|
||||
#define STATUS_INVALID_PARAMETER_5 ((NTSTATUS) 0xC00000F3L)
|
||||
#define STATUS_INVALID_PARAMETER_6 ((NTSTATUS) 0xC00000F4L)
|
||||
#define STATUS_INVALID_PARAMETER_7 ((NTSTATUS) 0xC00000F5L)
|
||||
#define STATUS_INVALID_PARAMETER_8 ((NTSTATUS) 0xC00000F6L)
|
||||
#define STATUS_INVALID_PARAMETER_9 ((NTSTATUS) 0xC00000F7L)
|
||||
#define STATUS_INVALID_PARAMETER_10 ((NTSTATUS) 0xC00000F8L)
|
||||
#define STATUS_INVALID_PARAMETER_11 ((NTSTATUS) 0xC00000F9L)
|
||||
#define STATUS_INVALID_PARAMETER_12 ((NTSTATUS) 0xC00000FAL)
|
||||
#define STATUS_TIMEOUT ((NTSTATUS) 0x00000102L)
|
||||
#define STATUS_NO_MEDIA ((NTSTATUS) 0xC0000178L)
|
||||
#define STATUS_IO_DEVICE_ERROR ((NTSTATUS) 0xC0000185L)
|
||||
|
@@ -49,7 +49,7 @@ project("BOOTMGR")
|
||||
libdirs({ "BUILD/SDK" })
|
||||
objdir("BUILD/BOOT")
|
||||
targetdir("BUILD/BOOT")
|
||||
files({ "BOOT/ENVIRON/INC/**.h", "BOOT/ENVIRON/**.c" })
|
||||
files({ "BOOT/ENVIRON/INC/**.h", "BOOT/ENVIRON/LIB/**.c", "BOOT/ENVIRON/APP/BOOTMGR/**.c" })
|
||||
|
||||
filter("toolset:clang")
|
||||
buildoptions({ "-fshort-wchar", "-fno-strict-aliasing", "-fno-stack-protector", "-fno-stack-check", "-mno-red-zone" })
|
||||
|
Reference in New Issue
Block a user