Skip to content

Commit

Permalink
Apply clang-format to fwcfg64 folder
Browse files Browse the repository at this point in the history
Signed-off-by: Vitalii Chulak <[email protected]>
  • Loading branch information
Jedoku committed Dec 4, 2024
1 parent 4d712c8 commit d546614
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 147 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
matrix:
path:
- check: '.'
exclude: './fwcfg64|./ivshmem|./NetKVM|./pciserial|./pvpanic|./viocrypt|./viofs|./viogpu|./vioinput|./viomem|./viorng|./vioscsi|./vioserial|./viosock|./viostor|./VirtIO'
exclude: './ivshmem|./NetKVM|./pciserial|./pvpanic|./viocrypt|./viofs|./viogpu|./vioinput|./viomem|./viorng|./vioscsi|./vioserial|./viosock|./viostor|./VirtIO'
- check: 'VirtIO'
exclude: ''

Expand Down
51 changes: 23 additions & 28 deletions fwcfg64/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ NTSTATUS VMCoreInfoFill(PDEVICE_CONTEXT ctx)
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_ALL, "Obtaining header");

hdr_buf = (PUCHAR)ctx->vmci_data.pNote + FIELD_OFFSET(VMCI_ELF64_NOTE, n_desc);
status = KeInitializeCrashDumpHeader(DUMP_TYPE_FULL, 0, hdr_buf,
DUMP_HDR_SIZE, &bufSizeNeeded);
status = KeInitializeCrashDumpHeader(DUMP_TYPE_FULL, 0, hdr_buf, DUMP_HDR_SIZE, &bufSizeNeeded);

if (!NT_SUCCESS(status))
{
Expand Down Expand Up @@ -53,8 +52,12 @@ NTSTATUS VMCoreInfoSend(PDEVICE_CONTEXT ctx)

TraceEvents(TRACE_LEVEL_VERBOSE, DBG_ALL, "Sending header");

status = FWCfgDmaSend(ctx->ioBase, ctx->vmci_data.vmci_pa, ctx->index,
sizeof(VMCOREINFO), ctx->dma_access, ctx->dma_access_pa);
status = FWCfgDmaSend(ctx->ioBase,
ctx->vmci_data.vmci_pa,
ctx->index,
sizeof(VMCOREINFO),
ctx->dma_access,
ctx->dma_access_pa);

return status;
}
Expand All @@ -73,7 +76,7 @@ NTSTATUS GetKdbg(PDEVICE_CONTEXT ctx)
PUCHAR minidump;
ULONG32 kdbg_offset;
ULONG32 kdbg_size;
CONTEXT context = { 0 };
CONTEXT context = {0};
NTSTATUS status = STATUS_SUCCESS;

minidump = ExAllocatePoolUninitialized(NonPagedPoolNx, MINIDUMP_BUFFER_SIZE, 'pmdm');
Expand All @@ -88,9 +91,11 @@ NTSTATUS GetKdbg(PDEVICE_CONTEXT ctx)
kdbg_offset = *(PULONG32)(minidump + MINIDUMP_OFFSET_KDBG_OFFSET);
kdbg_size = *(PULONG32)(minidump + MINIDUMP_OFFSET_KDBG_SIZE);

TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT,
"KdDebuggerDataBlock size = %lx, offset = 0x%lx",
kdbg_size, kdbg_offset);
TraceEvents(TRACE_LEVEL_VERBOSE,
DBG_INIT,
"KdDebuggerDataBlock size = %lx, offset = 0x%lx",
kdbg_size,
kdbg_offset);

/*
* KeCapturePersistentThreadState is supposed to save Small Memory Dump to the buffer.
Expand Down Expand Up @@ -164,35 +169,28 @@ NTSTATUS FwCfgEvtDeviceAdd(IN WDFDRIVER Driver, IN PWDFDEVICE_INIT DeviceInit)

if (!NT_SUCCESS(status))
{
TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT,
"WdfDeviceCreate failed: %!STATUS!", status);
TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT, "WdfDeviceCreate failed: %!STATUS!", status);
return status;
}

ctx = GetDeviceContext(device);
memset(ctx, 0, sizeof(*ctx));

WDF_DMA_ENABLER_CONFIG_INIT(&dmaEnablerConfig, WdfDmaProfilePacket64,
sizeof(CBUF_DATA));
status = WdfDmaEnablerCreate(device, &dmaEnablerConfig,
WDF_NO_OBJECT_ATTRIBUTES, &ctx->dmaEnabler);
WDF_DMA_ENABLER_CONFIG_INIT(&dmaEnablerConfig, WdfDmaProfilePacket64, sizeof(CBUF_DATA));
status = WdfDmaEnablerCreate(device, &dmaEnablerConfig, WDF_NO_OBJECT_ATTRIBUTES, &ctx->dmaEnabler);

if (!NT_SUCCESS(status))
{
TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT,
"Failed to create DMA enabler");
TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT, "Failed to create DMA enabler");
return status;
}

TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT,
"DMA enabler created");
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT, "DMA enabler created");

status = WdfCommonBufferCreate(ctx->dmaEnabler, sizeof(CBUF_DATA),
WDF_NO_OBJECT_ATTRIBUTES, &ctx->cbuf);
status = WdfCommonBufferCreate(ctx->dmaEnabler, sizeof(CBUF_DATA), WDF_NO_OBJECT_ATTRIBUTES, &ctx->cbuf);
if (!NT_SUCCESS(status))
{
TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT,
"Failed to create common buffer");
TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT, "Failed to create common buffer");
return status;
}

Expand All @@ -205,8 +203,7 @@ NTSTATUS FwCfgEvtDeviceAdd(IN WDFDRIVER Driver, IN PWDFDEVICE_INIT DeviceInit)
return STATUS_SUCCESS;
}

NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath)
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
{
NTSTATUS status;
WDF_DRIVER_CONFIG config;
Expand All @@ -219,12 +216,10 @@ NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,

WDF_DRIVER_CONFIG_INIT(&config, FwCfgEvtDeviceAdd);

status = WdfDriverCreate(DriverObject, RegistryPath, &attributes,
&config, WDF_NO_HANDLE);
status = WdfDriverCreate(DriverObject, RegistryPath, &attributes, &config, WDF_NO_HANDLE);
if (!NT_SUCCESS(status))
{
TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT,
"WdfDriverCreate failed: %!STATUS!", status);
TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT, "WdfDriverCreate failed: %!STATUS!", status);
WPP_CLEANUP(DriverObject);
}

Expand Down
77 changes: 41 additions & 36 deletions fwcfg64/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,29 @@
#include <wdf.h>
#include "fwcfg.h"

#define ENTRY_NAME "etc/vmcoreinfo"
#define VMCI_ELF_NOTE_NAME "VMCOREINFO"
#define DUMP_TYPE_FULL 1
#define VMCOREINFO_FORMAT_ELF 0x1
#define ENTRY_NAME "etc/vmcoreinfo"
#define VMCI_ELF_NOTE_NAME "VMCOREINFO"
#define DUMP_TYPE_FULL 1
#define VMCOREINFO_FORMAT_ELF 0x1
#ifdef _AMD64_
#define DUMP_HDR_SIZE (PAGE_SIZE * 2)
#define MINIDUMP_OFFSET_KDBG_OFFSET (DUMP_HDR_SIZE + 0x70)
#define MINIDUMP_OFFSET_KDBG_SIZE (DUMP_HDR_SIZE + 0x74)
#define DUMP_HDR_OFFSET_BUGCHECK_PARAM1 0x40
#define DUMP_HDR_SIZE (PAGE_SIZE * 2)
#define MINIDUMP_OFFSET_KDBG_OFFSET (DUMP_HDR_SIZE + 0x70)
#define MINIDUMP_OFFSET_KDBG_SIZE (DUMP_HDR_SIZE + 0x74)
#define DUMP_HDR_OFFSET_BUGCHECK_PARAM1 0x40
#else
#define DUMP_HDR_SIZE (PAGE_SIZE)
#define MINIDUMP_OFFSET_KDBG_OFFSET (DUMP_HDR_SIZE + 0x58)
#define MINIDUMP_OFFSET_KDBG_SIZE (DUMP_HDR_SIZE + 0x5c)
#define DUMP_HDR_OFFSET_BUGCHECK_PARAM1 0x2c
#define DUMP_HDR_SIZE (PAGE_SIZE)
#define MINIDUMP_OFFSET_KDBG_OFFSET (DUMP_HDR_SIZE + 0x58)
#define MINIDUMP_OFFSET_KDBG_SIZE (DUMP_HDR_SIZE + 0x5c)
#define DUMP_HDR_OFFSET_BUGCHECK_PARAM1 0x2c
#endif

#define MINIDUMP_BUFFER_SIZE 0x40000

#define ROUND_UP(x, n) (((x) + (n) - 1) & (-(n)))
#define ROUND_UP(x, n) (((x) + (n) - 1) & (-(n)))

#pragma pack(push, 1)
typedef struct VMCOREINFO {
typedef struct VMCOREINFO
{
UINT16 host_fmt;
UINT16 guest_fmt;
UINT32 size;
Expand All @@ -38,38 +39,42 @@ typedef struct VMCOREINFO {
#pragma pack(pop)

#pragma pack(push, 1)
typedef struct VMCI_ELF64_NOTE {
UINT32 n_namesz;
UINT32 n_descsz;
UINT32 n_type;
UCHAR n_name[ROUND_UP(sizeof(VMCI_ELF_NOTE_NAME), 4)];
UCHAR n_desc[ROUND_UP(DUMP_HDR_SIZE, 4)];
typedef struct VMCI_ELF64_NOTE
{
UINT32 n_namesz;
UINT32 n_descsz;
UINT32 n_type;
UCHAR n_name[ROUND_UP(sizeof(VMCI_ELF_NOTE_NAME), 4)];
UCHAR n_desc[ROUND_UP(DUMP_HDR_SIZE, 4)];
} VMCI_ELF64_NOTE, *PVMCI_ELF64_NOTE;
#pragma pack(pop)

typedef struct CBUF_DATA {
typedef struct CBUF_DATA
{
VMCI_ELF64_NOTE note;
VMCOREINFO vmci;
FWCfgDmaAccess fwcfg_da;
} CBUF_DATA, *PCBUF_DATA;

typedef struct VMCI_DATA {
PVMCOREINFO pVmci;
PVMCI_ELF64_NOTE pNote;
LONGLONG vmci_pa;
LONGLONG note_pa;
typedef struct VMCI_DATA
{
PVMCOREINFO pVmci;
PVMCI_ELF64_NOTE pNote;
LONGLONG vmci_pa;
LONGLONG note_pa;
} VMCI_DATA, *PVMCI_DATA;

typedef struct DEVICE_CONTEXT {
PVOID ioBase;
ULONG ioSize;
UINT16 index;
WDFCOMMONBUFFER cbuf;
WDFDMAENABLER dmaEnabler;
VMCI_DATA vmci_data;
FWCfgDmaAccess *dma_access;
LONGLONG dma_access_pa;
PUCHAR kdbg;
typedef struct DEVICE_CONTEXT
{
PVOID ioBase;
ULONG ioSize;
UINT16 index;
WDFCOMMONBUFFER cbuf;
WDFDMAENABLER dmaEnabler;
VMCI_DATA vmci_data;
FWCfgDmaAccess *dma_access;
LONGLONG dma_access_pa;
PUCHAR kdbg;
} DEVICE_CONTEXT, *PDEVICE_CONTEXT;

ULONG NTAPI KeCapturePersistentThreadState(PCONTEXT Context,
Expand Down
27 changes: 17 additions & 10 deletions fwcfg64/fwcfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ NTSTATUS FWCfgCheckSig(PVOID ioBase)
UCHAR signature[FW_CFG_SIG_SIZE];

FWCfgReadBlob(ioBase, FW_CFG_SIGNATURE, signature, FW_CFG_SIG_SIZE);
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_ALL,
"Signature is [%.4s]", (PCHAR)signature);
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_ALL, "Signature is [%.4s]", (PCHAR)signature);
if (memcmp(signature, FW_CFG_QEMU, FW_CFG_SIG_SIZE))
{
TraceEvents(TRACE_LEVEL_ERROR, DBG_ALL, "Signature check failed, "
"["FW_CFG_QEMU"] expected");
TraceEvents(TRACE_LEVEL_ERROR,
DBG_ALL,
"Signature check failed, "
"[" FW_CFG_QEMU "] expected");
return STATUS_BAD_DATA;
}

Expand All @@ -39,8 +40,11 @@ NTSTATUS FWCfgCheckFeatures(PVOID ioBase, UINT32 features)
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_ALL, "Features are 0x%lx", f_bitmap);
if ((f_bitmap & features) != features)
{
TraceEvents(TRACE_LEVEL_ERROR, DBG_ALL, "Features check failed, "
" 0x%lx expected", features);
TraceEvents(TRACE_LEVEL_ERROR,
DBG_ALL,
"Features check failed, "
" 0x%lx expected",
features);
return STATUS_BAD_DATA;
}

Expand Down Expand Up @@ -89,8 +93,7 @@ UINT32 FWCfgGetEntriesNum(PVOID ioBase)
return num;
}

NTSTATUS FWCfgFindEntry(PVOID ioBase, const char *name,
PUSHORT index, ULONG size)
NTSTATUS FWCfgFindEntry(PVOID ioBase, const char *name, PUSHORT index, ULONG size)
{
UINT32 i;
UINT32 total = FWCfgGetEntriesNum(ioBase);
Expand Down Expand Up @@ -119,8 +122,12 @@ NTSTATUS FWCfgFindEntry(PVOID ioBase, const char *name,
return STATUS_BAD_DATA;
}

NTSTATUS FWCfgDmaSend(PVOID ioBase, LONGLONG data_pa, USHORT index,
UINT32 size, FWCfgDmaAccess *pDmaAccess, LONGLONG dmaAccess_pa)
NTSTATUS FWCfgDmaSend(PVOID ioBase,
LONGLONG data_pa,
USHORT index,
UINT32 size,
FWCfgDmaAccess *pDmaAccess,
LONGLONG dmaAccess_pa)
{
UINT16 ctrl = FW_CFG_DMA_CTL_SELECT | FW_CFG_DMA_CTL_WRITE;
NTSTATUS status;
Expand Down
60 changes: 30 additions & 30 deletions fwcfg64/fwcfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,44 @@
#pragma once
#include <ntddk.h>

#define FW_CFG_CTL_OFFSET 0x00
#define FW_CFG_DAT_OFFSET 0x01
#define FW_CFG_DMA_OFFSET 0x04
#define FW_CFG_CTL_OFFSET 0x00
#define FW_CFG_DAT_OFFSET 0x01
#define FW_CFG_DMA_OFFSET 0x04

#define FW_CFG_CTL(p) ((PUCHAR)(p) + FW_CFG_CTL_OFFSET)
#define FW_CFG_DAT(p) ((PUCHAR)(p) + FW_CFG_DAT_OFFSET)
#define FW_CFG_DMA(p) ((PUCHAR)(p) + FW_CFG_DMA_OFFSET)
#define FW_CFG_CTL(p) ((PUCHAR)(p) + FW_CFG_CTL_OFFSET)
#define FW_CFG_DAT(p) ((PUCHAR)(p) + FW_CFG_DAT_OFFSET)
#define FW_CFG_DMA(p) ((PUCHAR)(p) + FW_CFG_DMA_OFFSET)

#define FW_CFG_SIG_SIZE 4
#define FW_CFG_MAX_FILE_PATH 56
#define FW_CFG_SIG_SIZE 4
#define FW_CFG_MAX_FILE_PATH 56

#define FW_CFG_SIGNATURE 0x00
#define FW_CFG_ID 0x01
#define FW_CFG_FILE_DIR 0x19
#define FW_CFG_SIGNATURE 0x00
#define FW_CFG_ID 0x01
#define FW_CFG_FILE_DIR 0x19

#define FW_CFG_VERSION 0x01
#define FW_CFG_VERSION_DMA 0x02
#define FW_CFG_VERSION 0x01
#define FW_CFG_VERSION_DMA 0x02

#define FW_CFG_DMA_CTL_ERROR 0x01
#define FW_CFG_DMA_CTL_READ 0x02
#define FW_CFG_DMA_CTL_SKIP 0x04
#define FW_CFG_DMA_CTL_SELECT 0x08
#define FW_CFG_DMA_CTL_WRITE 0x10
#define FW_CFG_DMA_CTL_ERROR 0x01
#define FW_CFG_DMA_CTL_READ 0x02
#define FW_CFG_DMA_CTL_SKIP 0x04
#define FW_CFG_DMA_CTL_SELECT 0x08
#define FW_CFG_DMA_CTL_WRITE 0x10

#define FW_CFG_QEMU "QEMU"
#define FW_CFG_QEMU_DMA 0x51454d5520434647ULL
#define FW_CFG_QEMU "QEMU"
#define FW_CFG_QEMU_DMA 0x51454d5520434647ULL

#pragma pack(push, 1)
typedef struct FWCfgFile {
UINT32 size;
UINT16 select;
UINT16 reserved;
char name[FW_CFG_MAX_FILE_PATH];
typedef struct FWCfgFile
{
UINT32 size;
UINT16 select;
UINT16 reserved;
char name[FW_CFG_MAX_FILE_PATH];
} FWCfgFile;

typedef struct FWCfgDmaAccess {
typedef struct FWCfgDmaAccess
{
UINT32 control;
UINT32 length;
UINT64 address;
Expand All @@ -56,7 +58,5 @@ UINT64 FWCfgReadDmaReg(PVOID ioBase);
VOID FWCfgWriteDmaReg(PVOID ioBase, UINT64 val);
NTSTATUS FWCfgCheckDma(PVOID ioBase);
UINT32 FWCfgGetEntriesNum(PVOID ioBase);
NTSTATUS FWCfgFindEntry(PVOID ioBase, const char *name,
PUSHORT index, ULONG size);
NTSTATUS FWCfgDmaSend(PVOID ioBase, LONGLONG data_pa, USHORT index,
UINT32 size, FWCfgDmaAccess *da, LONGLONG da_pa);
NTSTATUS FWCfgFindEntry(PVOID ioBase, const char *name, PUSHORT index, ULONG size);
NTSTATUS FWCfgDmaSend(PVOID ioBase, LONGLONG data_pa, USHORT index, UINT32 size, FWCfgDmaAccess *da, LONGLONG da_pa);
Loading

0 comments on commit d546614

Please sign in to comment.