2008-10-30 20:23:42 +01:00
|
|
|
|
|
|
|
#include <types.h>
|
|
|
|
#include <core.h>
|
|
|
|
#include <spinlock.h>
|
|
|
|
#include <link.h>
|
|
|
|
#include <mm.h>
|
|
|
|
#include <slab.h>
|
2008-11-01 02:25:51 +01:00
|
|
|
#include <pe.h>
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
int __stdcall strncmp(const char *s1, const char *s2, size_t n);
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
static dll_t core_dll;
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
static char* strupr(char *str )
|
2008-10-30 20:23:42 +01:00
|
|
|
{
|
2008-11-01 02:25:51 +01:00
|
|
|
char *p;
|
|
|
|
unsigned char c;
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
p = str;
|
|
|
|
while( (c = *p) )
|
2008-10-30 20:23:42 +01:00
|
|
|
{
|
2008-11-01 02:25:51 +01:00
|
|
|
if( c >= 'a' && c <= 'z' )
|
|
|
|
*p = c - 'a' + 'A';
|
|
|
|
++p;
|
|
|
|
}
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
return( str );
|
2008-10-30 20:23:42 +01:00
|
|
|
}
|
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
void init_core_dll()
|
2008-10-30 20:23:42 +01:00
|
|
|
{
|
2008-11-01 02:25:51 +01:00
|
|
|
PIMAGE_DOS_HEADER dos;
|
|
|
|
PIMAGE_NT_HEADERS32 nt;
|
|
|
|
PIMAGE_EXPORT_DIRECTORY exp;
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
dos = (PIMAGE_DOS_HEADER)LOAD_BASE;
|
|
|
|
nt = MakePtr( PIMAGE_NT_HEADERS32, dos, dos->e_lfanew);
|
|
|
|
exp = MakePtr(PIMAGE_EXPORT_DIRECTORY,LOAD_BASE,
|
|
|
|
nt->OptionalHeader.DataDirectory[0].VirtualAddress);
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
list_initialize(&core_dll.link);
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
core_dll.img_base = LOAD_BASE;
|
|
|
|
core_dll.img_size = nt->OptionalHeader.SizeOfImage;
|
|
|
|
core_dll.img_md = NULL;
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
core_dll.img_hdr = nt;
|
|
|
|
core_dll.img_sec = MakePtr(PIMAGE_SECTION_HEADER,nt, sizeof(IMAGE_NT_HEADERS32));
|
|
|
|
core_dll.img_exp = MakePtr(PIMAGE_EXPORT_DIRECTORY,LOAD_BASE,
|
|
|
|
nt->OptionalHeader.DataDirectory[0].VirtualAddress);
|
|
|
|
core_dll.img_name = strupr(MakePtr(char*, LOAD_BASE, exp->Name));
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
DBG("%s base %x size %x sections %d exports %x\n",
|
|
|
|
core_dll.img_name, core_dll.img_base,
|
|
|
|
core_dll.img_size, nt->FileHeader.NumberOfSections,
|
|
|
|
core_dll.img_exp );
|
|
|
|
};
|
2008-10-30 20:23:42 +01:00
|
|
|
|
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
dll_t * find_dll(const char *name)
|
2008-10-30 20:23:42 +01:00
|
|
|
{
|
2008-11-01 02:25:51 +01:00
|
|
|
dll_t* dll = &core_dll;
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if( !strncmp(name,dll->img_name,16))
|
|
|
|
return dll;
|
|
|
|
|
|
|
|
dll = (dll_t*)dll->link.next;
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
}while(&dll->link != &core_dll.link);
|
2008-10-30 20:23:42 +01:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
};
|
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
|
2008-10-30 20:23:42 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char srv_name[16]; // ASCIIZ string
|
|
|
|
u32_t magic; // +0x10 'SRV '
|
|
|
|
size_t size; // +0x14 size of structure SRV
|
|
|
|
void *fd; // +0x18 next SRV descriptor
|
|
|
|
void *bk; // +0x1C prev SRV descriptor
|
|
|
|
addr_t base; // +0x20 service base address
|
|
|
|
addr_t entry; // +0x24 service START function
|
|
|
|
void *srv_proc; // +0x28 main service handler
|
|
|
|
}srv_t;
|
|
|
|
|
|
|
|
typedef srv_t* __stdcall drv_entry_t(int);
|
|
|
|
|
|
|
|
srv_t* __fastcall load_pe_driver(const char *path)
|
|
|
|
{
|
|
|
|
PIMAGE_DOS_HEADER dos;
|
|
|
|
PIMAGE_NT_HEADERS32 nt;
|
|
|
|
|
|
|
|
drv_entry_t *drv_entry;
|
|
|
|
md_t *md;
|
|
|
|
srv_t *srv;
|
|
|
|
|
|
|
|
md = load_image(path);
|
|
|
|
|
|
|
|
if( ! md )
|
|
|
|
return 0;
|
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
if( link_image( md->base ) )
|
2008-10-30 20:23:42 +01:00
|
|
|
{
|
2008-11-01 02:25:51 +01:00
|
|
|
dos = (PIMAGE_DOS_HEADER)md->base;
|
|
|
|
nt = MakePtr( PIMAGE_NT_HEADERS32, dos, dos->e_lfanew);
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
drv_entry = MakePtr(drv_entry_t*, md->base,
|
|
|
|
nt->OptionalHeader.AddressOfEntryPoint);
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
srv = drv_entry(1);
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
if(srv != NULL)
|
|
|
|
srv->entry = nt->OptionalHeader.AddressOfEntryPoint + md->base;
|
2008-10-30 20:23:42 +01:00
|
|
|
|
2008-11-01 02:25:51 +01:00
|
|
|
return srv;
|
2008-10-30 20:23:42 +01:00
|
|
|
}
|
2008-11-01 02:25:51 +01:00
|
|
|
else
|
2008-10-30 20:23:42 +01:00
|
|
|
{
|
2008-11-01 02:25:51 +01:00
|
|
|
md_free( md );
|
2008-10-30 20:23:42 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-11-01 02:25:51 +01:00
|
|
|
}
|
2008-10-30 20:23:42 +01:00
|
|
|
|