Blame src/process/ntapi_tt_get_runtime_data.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
64e606
/*  Copyright (C) 2013--2021  SysDeer Technologies, LLC */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_memory.h>
dd89bb
#include <ntapi/nt_process.h>
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
7f8d50
#if (__SIZEOF_POINTER__ == 4)
dd89bb
static wchar16_t runtime_arg[12] = {
dd89bb
	' ','-','r',' ',
dd89bb
	'i','n','t','e','g','r','a','l'};
7f8d50
#elif (__SIZEOF_POINTER__ == 8)
dd89bb
static wchar16_t runtime_arg[20] = {
dd89bb
	' ','-','r',' ',
dd89bb
	'i','n','t','e','g','r','a','l',
dd89bb
	'-','r','u','n','t','i','m','e'};
dd89bb
#endif
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_get_runtime_data(
dd89bb
	__out		nt_runtime_data **	rtdata,
f7b999
	__in		wchar16_t **		wargv)
dd89bb
{
dd89bb
	int32_t				status;
dd89bb
	nt_process_parameters *		process_params;
f7b999
	wchar16_t *			addrarg;
365987
	uintptr_t			address;
365987
	uintptr_t			buffer;
dd89bb
	nt_runtime_data *		prtdata;
dd89bb
	ntapi_internals *		__internals;
dd89bb
dd89bb
	/* init */
dd89bb
	__internals = __ntapi_internals();
dd89bb
dd89bb
	/* once? */
dd89bb
	if (__internals->rtdata) {
dd89bb
		*rtdata = __internals->rtdata;
dd89bb
		return NT_STATUS_SUCCESS;
dd89bb
	}
dd89bb
f7b999
	if (!(wargv = wargv ? wargv : __internals->ntapi_img_sec_bss->argv_envp_array))
365987
		return NT_STATUS_INVALID_PARAMETER;
dd89bb
f7b999
	if (!wargv[1] || !wargv[2])
f7b999
		return NT_STATUS_MORE_PROCESSING_REQUIRED;
f7b999
dd89bb
	/* integral process? */
365987
	addrarg = ((wargv[1][0] == '-') && (wargv[1][1] == 'r') && (wargv[1][2] == 0))
365987
		? wargv[2] : 0;
dd89bb
365987
	/* top-level framework process? */
f7b999
	if (!addrarg || wargv[3])
365987
		return NT_STATUS_MORE_PROCESSING_REQUIRED;
dd89bb
f7b999
	/* obtain pointer to data block */
dd89bb
	if ((status = __ntapi->tt_hex_utf16_to_uintptr(
365987
			addrarg,&address)))
dd89bb
		return status;
dd89bb
365987
	/* invalid pointer? */
365987
	if (address & 0xFFF)
365987
		return NT_STATUS_MORE_PROCESSING_REQUIRED;
365987
365987
	/* address is aligned at page boundary */
dd89bb
	if ((status = __ntapi->zw_read_virtual_memory(
dd89bb
			NT_CURRENT_PROCESS_HANDLE,
365987
			(void *)address,
dd89bb
			(char *)&buffer,
365987
			sizeof(buffer),
365987
			0)))
dd89bb
		return status;
dd89bb
804edf
	/* abi */
365987
	prtdata = (nt_runtime_data *)address;
804edf
804edf
	if (__ntapi->tt_guid_compare(&prtdata->abi,&(nt_guid)NT_PROCESS_GUID_RTDATA))
804edf
		return NT_STATUS_MORE_PROCESSING_REQUIRED;
804edf
804edf
	/* update state */
7ddcea
	prtdata->flags |= NT_RUNTIME_DATA_INTEGRAL_PROCESS;
7ddcea
dd89bb
	/* avoid confusion :-) */
dd89bb
	process_params = ((nt_peb *)pe_get_peb_address())->process_params;
dd89bb
dd89bb
	__ntapi->tt_memcpy_utf16(
dd89bb
		(wchar16_t *)pe_va_from_rva(
dd89bb
			process_params->command_line.buffer,
dd89bb
			process_params->command_line.strlen - sizeof(runtime_arg)),
dd89bb
		runtime_arg,
dd89bb
		sizeof(runtime_arg));
dd89bb
dd89bb
	*rtdata = prtdata;
dd89bb
dd89bb
	return NT_STATUS_SUCCESS;
dd89bb
}