/*****************************************************************************
*
* InitInstance
*
* Called once per instance of application.
*
* HANDLE hInst              - Instance handle of this instance
* int nCmdShow              - Code for ShowWindow which tells us what state
*                             to initially show the main application window.
* Returns TRUE on success.
*
* Initialize debug library.
* Save the instance handle.
* Load global resource strings.
* Allocate and initialize the global sequencer structure.
* Create the main window.
* Add time formats to the options menu.
* Show the main window.
*
*****************************************************************************/
BOOL FNLOCAL InitInstance(
    HINSTANCE               hInst,
    int                     nCmdShow)
{
    HWND                    hWnd;
    int                     idx;

    DbgInitialize(TRUE);

    ghInst = hInst;

    LoadString(hInst, IDS_APPTITLEMASK, gszAppTitleMask, sizeof(gszAppTitleMask));
    LoadString(hInst, IDS_APPNAME,      gszAppLongName,  sizeof(gszAppLongName));
    LoadString(hInst, IDS_UNTITLED,     gszUntitled,     sizeof(gszUntitled));

    for (idx = 0; idx < N_TIME_FORMATS; idx++)
    {
        *grgszTimeFormats[idx] = '\0';
        LoadString(hInst,
                   IDS_TF_FIRST+idx,
                   grgszTimeFormats[idx],
                   sizeof(grgszTimeFormats[idx]));
    }

    if ((gpSeq = (PSEQ)LocalAlloc(LPTR, sizeof(SEQ))) == NULL)
        return FALSE;

    gpSeq->cBuffer  = C_MIDI_BUFFERS;
    gpSeq->cbBuffer = CB_MIDI_BUFFERS;

    if (seqAllocBuffers(gpSeq) != MMSYSERR_NOERROR)
        return FALSE;

    hWnd = CreateWindow(
        gszMWndClass,
        NULL,
        WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN,
        CW_USEDEFAULT,CW_USEDEFAULT,
        CW_USEDEFAULT,CW_USEDEFAULT,
        HWND_DESKTOP,
        NULL,
        hInst,
        NULL);

    if (hWnd == (HWND)NULL)
        return FALSE;

    gpSeq->hWnd = hWnd;

    ShowWindow(hWnd, nCmdShow);

    return TRUE;
}
Example #2
0
/**
Initialize the AGC Simulator; this means setting up the debugger, AGC
engine and initializing the simulator time parameters.
*/
int SimInitialize(Options_t* Options)
{
	int result = 0;

	/* Without Options we can't Run */
	if (!Options) return(6);

	/* Set the basic simulator variables */
	Simulator.Options = Options;
	Simulator.DebugRules = DebugRules;
	Simulator.DumpInterval = Options->dump_time * sysconf (_SC_CLK_TCK);

	/* Set legacy Option variables */
	Portnum = Options->port;
	DebugDsky = Options->debug_dsky;
	DebugDeda = Options->debug_deda;
	DedaQuiet = Options->deda_quiet;

	Simulator.DumpInterval = Simulator.DumpInterval;
	SocketInterlaceReload = Options->interlace;

	/* If we are not in quiet mode display the version info */
	if (!Options->quiet) DbgDisplayVersion();

	/* Initialize the AGC Engine */
	result = SimInitializeEngine();

	/* Initialize the Debugger if running with debug mode */
	if(Options->debug) DbgInitialize(Options,&(Simulator.State));

//	if (Options->cdu_log)
//	{
//	  extern FILE *CduLog;
//	  CduLog = fopen (Options->cdu_log, "w");
//	}

	/* Initialize realtime and cycle counters */
	Simulator.RealTimeOffset = times (&Simulator.DummyTime);	// The starting time of the program.
	Simulator.NextCoreDump = Simulator.RealTimeOffset + Simulator.DumpInterval;
	SimSetCycleCount(SIM_CYCLECOUNT_AGC); // Num. of AGC cycles so far.
	Simulator.RealTimeOffset -= (Simulator.CycleCount + AGC_PER_SECOND / 2) / AGC_PER_SECOND;
	Simulator.LastRealTime = ~0UL;

	return (result | Options->version);
}
Example #3
0
NTSTATUS
BootStartUp(
	)

/*++

Routine Description

	This routine is called during Windows boot up from ngboot.sys
	This routine performs initialization like DriverEntry, but 
	 without driver-specific initialization (like modifying DriverObject, etc)
	 becase ngbood.sys simply imports from ngdbg.sys and Windows does not
	 create driver object for driver being imported from another drivers.
	 Also Windows Kernel does not call entry point of driver being imported.
	So ngboot.sys have to manually call BootStartUp and BootStartUp should
	 perform initialization.

Arguments

	None

Return Value

	NTSTATUS of initialization

Environment

	System startup

--*/

{
	NTSTATUS Status;

	//
	// Indicate that debugging is initiated at boot time.
	//

	BootDebuggingInitiated = TRUE;


	//
	// At system startup we cannot use several features:
	//
	// 1) i8042prt routines to hook keyboard (i8042prt.sys possibly is not loaded yet).
	//    So we have to hook keyboard IRQ manually.
	//
	// 2) win32k services to output to screen. Win32 subsystem may be not loaded yet.
	//    So we have to output to screen using bootvid.dll
	//

	//
	// Initialize boot gui
	//

	BootGuiInitialize ();

	//
	// Perform multiprocessor initialization
	//

	DbgHalInitializeMP ();

	//
	// Load symbols (usually Worker() loads symbols, which is not called at boot time)
	//

	if(!FindBaseAndSize (GetKernelAddress(L"DbgPrint"), &pNtBase, NULL))
	{
		KdPrint(("Could not get nt base\n"));
		return STATUS_UNSUCCESSFUL;
	}

	// Initialize symbol engine and load symbols
	SymInitialize ( FALSE );	// don't use EngMapFile

	Status = SymLoadSymbolFile (L"nt", pNtBase);
	KdPrint(("nt symbols loaded with status %X\n", Status));

	Status = SymLoadSymbolFile (L"hal.dll", NULL);
	KdPrint(("hal.dll symbols loaded with status %X\n", Status));


	//
	// Set keyboard hook to 8042.sys instead of i8042prt.sys system driver
	//

	I8042HookKeyboardIsr(&ProcessScanCode);

	//
	// Initialize mouse hook
	//

	MouseInitialize (StateChangeCallbackRoutine);

	//
	// Initialize debug engine
	//

	DbgInitialize ();


	//
	// Write some chars
	//

	for (ULONG i=0; i<90; i++)
	{
		GuiPrintf ("Line %d\n", i);
	}

	return STATUS_SUCCESS;
}
Example #4
0
//
// Driver entry point
//
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING )
{
	DriverObject->DriverUnload = DriverUnload;
	KdPrint(("[~] DriverEntry()\n"));

	PsGetVersion (&MajorVersion, &MinorVersion, 0, 0);

	if (MajorVersion >= 6)
	{
		KdPrint(("Windows Vista and later are not supported yet\n"));
		return STATUS_NOT_SUPPORTED;
	}
	if (MajorVersion < 5 || MinorVersion == 0)
	{
		KdPrint(("Windows NT and 2000 are not supported\n"));
		return STATUS_NOT_SUPPORTED;
	}

	ASSERT (MajorVersion == 5);
	ASSERT (MinorVersion >= 1 && MinorVersion <= 2);

	if (MinorVersion == 1)
	{
		KdPrint(("Running on Windows XP\n"));
	}
	else
	{
		KdPrint(("Running on Windows 2003 Server\n"));
	}

	if (KeNumberProcessors > 1)
	{
		KdPrint(("Loading on multiprocessor system (NumberProcessors %d)\n", KeNumberProcessors));
	}
	else
	{
		KdPrint(("Loading on uniprocessor system\n"));
	}

	KdPrint (("First hello from nt\n"));

	if(!NT_SUCCESS(W32FindAndSwapIAT ()))
	{
		KdPrint(("could not swap import\n"));
		return STATUS_INVALID_FILE_FOR_SECTION;
	}

	// import something from W32k
	EngPrint ("Second hello from win32k\n");

	HANDLE hCsrProcess;
	NTSTATUS Status;

	Status = ObOpenObjectByPointer (
		CsrProcess,
		OBJ_KERNEL_HANDLE,
		NULL, 
		PROCESS_ALL_ACCESS,
		*PsProcessType, 
		KernelMode, 
		&hCsrProcess
		); 

	if (!NT_SUCCESS(Status))
	{
		KdPrint(("ObOpenObjectByPointer failed with status %X\n", Status));
		W32ReleaseCall();
		return Status;
	}

	KdPrint(("csr opened, handle %X\n", hCsrProcess));

	//
	// EngLoadImage uses KeAttachProcess/KeDetachProcess to attach to csrss process
	// KeDetachProcess detaches to thread's original process, but our thread's
	// original process is System! (because we are running in the context of system
	// worker thread that loads a driver). 
	//    (
	//    |  fucken windows programmers could not call KeStackAttachProcess 
	//    |   instead of KeAttachProcess :(
	//    )
	// So we have to run our function in the context of csrss.exe
	//

	HANDLE ThreadHandle;
	CLIENT_ID ClientId;
	OBJECT_ATTRIBUTES Oa;
	InitializeObjectAttributes (&Oa, NULL, OBJ_KERNEL_HANDLE, 0, 0);

	Status = PsCreateSystemThread (
		&ThreadHandle,
		THREAD_ALL_ACCESS,
		&Oa,
		hCsrProcess,
		&ClientId,
		REINITIALIZE_ADAPTER,
		NULL
		);

	if (!NT_SUCCESS(Status))
	{
		KdPrint(("PsCreateSystemThread failed with status %X\n", Status));
		ZwClose (hCsrProcess);
		W32ReleaseCall();
		return Status;
	}

	KdPrint(("thread created, handle %X\n", ThreadHandle));

	PETHREAD Thread;

	Status = ObReferenceObjectByHandle(
		ThreadHandle,
		THREAD_ALL_ACCESS,
		*PsThreadType,
		KernelMode,
		(PVOID*) &Thread,
		NULL
		);

	if (!NT_SUCCESS(Status))
	{
		KdPrint(("ObReferenceObjectByHandle failed with status %X\n", Status));
		// cannot unload because thread is running
		KeBugCheck (0);
	}

	KdPrint(("thread referenced to %X\n", Thread));

	KeWaitForSingleObject (Thread, Executive, KernelMode, FALSE, NULL);

	KdPrint(("Thread terminated\n"));

	ZwClose (hCsrProcess);
	ObDereferenceObject (Thread);
	ZwClose (ThreadHandle);

	KdPrint(("success\n", hCsrProcess));

	if (!pDrvCopyBits)
	{
		KdPrint(("Could not find DrvCopyBits\n"));
		W32ReleaseCall();
		return STATUS_UNSUCCESSFUL;
	}

	//
	// Query keyboard LEDs
	//

	if(!NT_SUCCESS(KbdWinQueryLeds()))
	{
		W32ReleaseCall();
		return STATUS_UNSUCCESSFUL;
	}

	PSHARED_DISP_DATA disp = GetSharedData();
	if (!disp)
	{
		EngPrint ("ngvid: could not get shared data\n");
		W32ReleaseCall();
		return STATUS_UNSUCCESSFUL;
	}
	if (disp->Signature != SHARED_SIGNATURE)
	{
		EngPrint ("ngvid: Damaged shared block %X signature %X should be %X\n",
			disp, disp->Signature, SHARED_SIGNATURE);
		//__asm int 3

		W32ReleaseCall();
		return STATUS_UNSUCCESSFUL;
	}

	KdPrint (("Got shared %X Sign %X Surf %X\n", disp, disp->Signature, disp->pPrimarySurf));

#if 0
	//
	// Temporarily hook DrvCopyBits
	//

	pDrvCopyBits = disp->pDrvCopyBits;

#endif

	if (!disp->pPrimarySurf)
	{
		KdPrint(("DrvCopyBits %X\n", pDrvCopyBits));

		KeInitializeEvent (&SynchEvent, SynchronizationEvent, FALSE);

		if (SpliceFunctionStart (pDrvCopyBits, NewDrvCopyBits, SplicingBuffer, sizeof(SplicingBuffer), BackupBuffer, &BackupWritten, FALSE))
		{
			KdPrint(("SpliceFunctionStart FAILED!!!\n"));
			W32ReleaseCall();
			return STATUS_UNSUCCESSFUL;
		}

		KdPrint(("Now you have to move mouse pointer across the display ...\n"));

		KeWaitForSingleObject (&SynchEvent, Executive, KernelMode, FALSE, NULL);

		UnspliceFunctionStart (pDrvCopyBits, BackupBuffer, FALSE);

		KdPrint(("Wait succeeded, so got primary surf %X\n", pPrimarySurf));
		disp->pPrimarySurf = pPrimarySurf;
	}
	else
	{
		KdPrint(("Already have primary surface\n"));
		pPrimarySurf = disp->pPrimarySurf;
	}

	// Hook kbd & mouse

#if KBD_HOOK_ISR
	OldKbd = GetIOAPICIntVector (1);
	*(PVOID*)&OldISR = IoHookInterrupt ( (UCHAR)OldKbd, InterruptService);
#else
	CreateTrampoline();
	//I8042HookKeyboard  ((PI8042_KEYBOARD_ISR) IsrHookRoutine);
#endif

	MouseInitialize (StateChangeCallbackRoutine);

	KdPrint(("Keyboard & mouse hooked\n"));

	// Initialize reset DPC
	KeInitializeDpc (&HotkeyResetStateDpc, HotkeyResetStateDeferredRoutine, NULL);

	//
	// Perform multiprocessor initialization
	//

	DbgHalInitializeMP ();

	///

	Worker();

	///

	W32ReleaseCall();

	DbgInitialize ();

	KdPrint(("[+] Driver initialization successful\n"));
	return STATUS_SUCCESS;
}