Exemplo n.º 1
0
void setup() {
	SceUID lowID = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "low", PSP_SMEM_Low, 0x10, NULL);
	SceUID highID = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "high", PSP_SMEM_High, 0x10, NULL);
	if (lowID < 0 || highID < 0) {
		printf("Test failure: can't allocate two 0x10 chunks\n");
	}

	low = (char *)sceKernelGetBlockHeadAddr(lowID);
	high = (char *)sceKernelGetBlockHeadAddr(highID) + 0x10;

	sceKernelFreePartitionMemory(lowID);
	sceKernelFreePartitionMemory(highID);
}
Exemplo n.º 2
0
static void *modmgrMalloc(const char *name, SceSize size, void *p)
{
	SceUID blockid;

	dbg_printf("%s: size: %d, p: 0x%08X\n", __func__, size, (int)p);

	if (name == NULL)
		return NULL;

	if (p == NULL) {
		blockid = _hook_sceKernelAllocPartitionMemory(2, name,
			PSP_SMEM_Low, size + (1 << 16), NULL);
		if (blockid < 0) {
			dbg_printf("FAILED: 0x%08X\n", blockid);
			return NULL;
		}

		p = sceKernelGetBlockHeadAddr(blockid);
		if ((int)p & ((1 << 16) - 1))
			p = (void *)(((int)p & ~((1 << 16) - 1)) + (1 << 16));
	} else {
		blockid = _hook_sceKernelAllocPartitionMemory(2, name,
			PSP_SMEM_Addr, size, p);
		if (blockid < 0) {
			dbg_printf("FAILED: 0x%08X\n", blockid);
			return NULL;
		}
	}

	return p;
}
Exemplo n.º 3
0
int nkThreadSuspend(SceUID thId){

	int i, j;
	SceUID myThread, *Thread_Now;

	if(bufNow.pThread != 0) return 1;

	bufNow.pThread = sceKernelAllocPartitionMemory(1, "th", 0, MAX_THREAD*sizeof(SceUID), NULL);
	if(bufNow.pThread < 0){
		bufNow.count = 0;
		bufNow.pThread = 0;
		return 1;
	}
	Thread_Now = (SceUID*)sceKernelGetBlockHeadAddr(bufNow.pThread);

	sceKernelGetThreadmanIdList(SCE_KERNEL_TMID_Thread, Thread_Now, MAX_THREAD, &(bufNow.count));
	myThread = sceKernelGetThreadId();

	for(i = 0; i < bufNow.count; i++){
		unsigned char match = 0;
		SceUID tmp_thid = Thread_Now[i];
		
		for(j = 0; j < count_Start; j++){
			if((tmp_thid == Thread_Start[j]) || (tmp_thid == thId) || (tmp_thid == myThread)){
				match = 1;
				j = count_Start;
			}
		}
		if(match == 0){
			sceKernelSuspendThread(tmp_thid);
		}
	}

	return 0;
}
Exemplo n.º 4
0
int fill_tables(SceUID fd) {
    if (fd < 0)
        return -1;
    sceIoLseek(fd, 0, PSP_SEEK_SET);
    sceIoRead(fd, &patch_count, 4);
    // max permitted: 6KiB
    if (patch_count > 6144) {
        patch_count = 6144;
    }
    kprintf("Allocating %i bytes\n", patch_count * 4 * 3);
    memid = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_KERNEL, "mhp3tbl", PSP_SMEM_High, patch_count * 4 * 3, NULL);
    if (memid < 0) {
        kprintf("Mamory alloc failed\n");
        return -1;
    }
    patch_offset = sceKernelGetBlockHeadAddr(memid);
    kprintf("patch_offset addr: %08X\n", (u32)patch_offset);
    patch_size = &patch_offset[patch_count];
    kprintf("patch_size addr: %08X\n", (u32)patch_size);
    for (u32 i = 0; i < patch_count; i++) {
        sceIoRead(fd, &patch_offset[i], 4);
        sceIoRead(fd, &patch_size[i], 4);
    }
    data_start = ((patch_count + 1) * 8);
    if (data_start % 16 > 0) {
        data_start += 16 - (data_start % 16);
    }
    return 0;
}
Exemplo n.º 5
0
int stackCheckFunc(SceSize argc, void *argv) {
	if ((int)argv & 0xf) {
		schedf("    %s: ERROR: arg pointer not aligned.\n", stackCheckName);
	}

	u32 *stack = (u32 *) stackCheckInfo.stack;
	u32 *stackEnd = stack + stackCheckInfo.stackSize / 4;

	if (stack[0] != sceKernelGetThreadId()) {
		schedf("    %s: ERROR: stack should start with thread ID.\n", stackCheckName);
	}
	if (stackEnd[-1] != 0xFFFFFFFF || stackEnd[-2] != 0xFFFFFFFF) {
		schedf("    %s: WARNING: k0 laid out differently?\n", stackCheckName);
	}
	if (stackEnd[-14] != (u32)stack) {
		schedf("    %s: WARNING: stack pointer not correct in k0.\n", stackCheckName);
	}
	if (stackEnd[-16] != sceKernelGetThreadId()) {
		schedf("    %s: WARNING: thread id not correct in k0.\n", stackCheckName);
	}

	SceUID uid = sceKernelAllocPartitionMemory(2, "TEST", PSP_SMEM_Low, 0x100, NULL);
	if (stack < (u32 *)sceKernelGetBlockHeadAddr(uid)) {
		schedf("    %s: WARNING: stack allocated low.\n", stackCheckName);
	}
	sceKernelFreePartitionMemory(uid);

	if (stack[1] != 0xFFFFFFFF) {
		schedf("    %s: WARNING: stack not set to FF, instead: %08x.\n", stackCheckName, stack[1]);
	}

	return 0;
}
Exemplo n.º 6
0
int load_quest_index() {
    mib_table = NULL;
    mib_elems = 0;
    k1 = pspSdkSetK1(0);
    model_go = sceKernelGetModel() == 4 ? 1 : 0;
    strcpy(filename, "xxx:/mhp3rd/quest/mib_id.dat");
    SET_DEVICENAME(filename, model_go);
    kprintf("trying to open %s\n", filename);
    SceUID fd = sceIoOpen(filename, PSP_O_RDONLY, 0777);
    if(fd < 0) {
        kprintf("Cannot find mib_id.dat\n");
        pspSdkSetK1(k1);
        return fd;
    }
    SceSize size = (SceSize)sceIoLseek(fd, 0, PSP_SEEK_END);
    sceIoLseek(fd, 0, PSP_SEEK_SET);
    index_id = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_KERNEL, "mhp3mib", PSP_SMEM_High, size, NULL);
    if(index_id >= 0) {
        mib_table = sceKernelGetBlockHeadAddr(index_id);
        sceIoRead(fd, mib_table, size);
        mib_elems = size / (sizeof(u32) * 2);
        quest_number = mib_table + mib_elems;
        kprintf("index size: %i bytes, entries: %i\n", size, index_elems);
    } else {
        kprintf("failed to allocate memory for table\n");
    }
    sceIoClose(fd);
    pspSdkSetK1(k1);
    return 0;
}
Exemplo n.º 7
0
int nkThreadResume(SceUID thId){

	int i, j;
	SceUID myThread, *Thread_Now;

	if(bufNow.pThread == 0) return 1;

	Thread_Now = (SceUID*)sceKernelGetBlockHeadAddr(bufNow.pThread);
	myThread = sceKernelGetThreadId();
	

	for(i = 0; i < bufNow.count; i++){
		unsigned char match = 0;
		SceUID tmp_thid = Thread_Now[i];
		
		for(j = 0; j < count_Start; j++){
			if((tmp_thid == Thread_Start[j]) || (tmp_thid == thId) || (tmp_thid == myThread)){
				match = 1;
				j = count_Start;
			}
		}

		if(match == 0){
			sceKernelResumeThread(tmp_thid);
		}
	}
	
	sceKernelFreePartitionMemory(bufNow.pThread);
	bufNow.count = 0;
	bufNow.pThread = 0;

	return 0;
}
Exemplo n.º 8
0
//Subroutine LoadCoreForKernel_3FE631F0 - Address 0x00007178
SceUID sceKernelGetModuleListWithAlloc(u32 *modCount)
{    
    u32 i;
    u32 intrState;
    SceUID blockId;
    SceUID *modIdList;
    SceModule *curMod;
    
    if (modCount == NULL) 
        return SCE_ERROR_KERNEL_ILLEGAL_ADDR; //0x00007194
    
    intrState = loadCoreCpuSuspendIntr(); //0x0000719C
    
    *modCount = g_loadCore.regModCount;
    /*
     * Allocate an array able to hold g_loadCore.regModCount elements
     * (the number of currently loaded modules) and fill that
     * array with the UIDs of the loaded modules.
     */
    blockId = sceKernelAllocPartitionMemory(SCE_KERNEL_PRIMARY_KERNEL_PARTITION, MODULE_LIST_MEM_BLOCK_NAME, 
                                            SCE_KERNEL_SMEM_High, g_loadCore.regModCount << 2, 0); //0x000071CC
    if (blockId > 0) { //0x000071CC
        modIdList = sceKernelGetBlockHeadAddr(blockId);
        for (i = 0, curMod = g_loadCore.registeredMods; curMod; curMod = curMod->next, i++)
             modIdList[i] = curMod->modId; //0x000071F8
    }
    loadCoreCpuResumeIntr(intrState);//0x00007204
    return blockId;
}
Exemplo n.º 9
0
void *allocate_memory_buffer(int *blockId, SceUID partitionId, const char *name, int type, SceSize size, void *addr) {
    
    *blockId = sceKernelAllocPartitionMemory(partitionId, name, type, size, addr);
    if (*blockId >= 0) {
        return sceKernelGetBlockHeadAddr(*blockId);
    }
    return NULL;
}
Exemplo n.º 10
0
extern "C" int main(int argc, char *argv[]) {
	checkpointNext("Names:");
	testAlloc("  NULL", NULL, 1, 0x100, NULL);
	testAlloc("  Blank", "", 1, 0x100, NULL);
	testAlloc("  Long", "123456789012345678901234567890123456789012345678901234567890", 1, 0x100, NULL);

	checkpointNext("Types:");
	static const int types[] = {-1, 0, 1, 2, 3, 4, 5, 0x11, 0x101, 0x1001, 0x10001, 0x100001, 0x1000001, 0x10000001};
	for (size_t i = 0; i < ARRAY_SIZE(types); ++i) {
		char temp[32];
		sprintf(temp, "  Type %x", types[i]);
		testAlloc(temp, "test", types[i], 0x100, NULL);
	}

	checkpointNext("Sizes:");
	static const int sizes[] = {-1, 0, 1, 2, 3, 4, 5, 0x11, 0x101, 0x1001, 0x10001};
	for (size_t i = 0; i < ARRAY_SIZE(sizes); ++i) {
		SceUID uid1 = sceKernelAllocMemoryBlock("test", 1, 0x100, NULL);
		SceUID uid2 = sceKernelAllocMemoryBlock("test", 1, sizes[i], NULL);
		if (uid2 >= 0) {
			u8 *ptr1;
			u8 *ptr2;
			sceKernelGetMemoryBlockPtr(uid1, (void **) &ptr1);
			sceKernelGetMemoryBlockPtr(uid2, (void **) &ptr2);

			checkpoint("  Size %x: OK, delta=%x", sizes[i], ptr1 - ptr2);
		} else {
			checkpoint("  Size %x: Failed (%08x)", sizes[i], uid2);
		}
	}

	checkpointNext("Param sizes:");
	u32 params[2];
	static const int paramSizes[] = {-1, 0, 1, 2, 3, 4, 5, 0x11, 0x101, 0x1001, 0x10001, 0x100001, 0x1000001, 0x10000001};
	for (size_t i = 0; i < ARRAY_SIZE(paramSizes); ++i) {
		char temp[32];
		sprintf(temp, "  Type %x", paramSizes[i]);
		params[0] = paramSizes[i];
		params[1] = 0x100;
		testAlloc(temp, "test", 1, 0x100, params);
	}

	checkpointNext("Partition usage:");
	void *ptr;
	SceUID uid = sceKernelAllocMemoryBlock("test", 1, 0x100, NULL);
	checkpoint("  sceKernelGetBlockHeadAddr: %08x", sceKernelGetBlockHeadAddr(uid) == NULL ? NULL : 0x1337);
	checkpoint("  sceKernelFreePartitionMemory: %08x", sceKernelFreePartitionMemory(uid));
	uid = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "test", PSP_SMEM_High, 0x100, NULL);
	checkpoint("  sceKernelGetMemoryBlockPtr: %08x", sceKernelGetMemoryBlockPtr(uid, &ptr));
	checkpoint("  sceKernelFreeMemoryBlock: %08x", sceKernelFreeMemoryBlock(uid));

	checkpointNext("Invalid uids:");
	checkpoint("  Free: %08x", sceKernelFreeMemoryBlock(uid));
	checkpoint("  BlockPtr: %08x", sceKernelGetMemoryBlockPtr(uid, &ptr));

	return 0;
}
Exemplo n.º 11
0
//OK
void *zeroCtrlAllocUserBuffer(SceUID uid, int size) {
    void *addr;
    k1 = pspSdkSetK1(0);
    uid = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "pathBuf",
            PSP_SMEM_High, size, NULL);
    addr = (uid >= 0) ? sceKernelGetBlockHeadAddr(uid) : NULL;
    pspSdkSetK1(k1);
    return addr;
}
Exemplo n.º 12
0
static u32 AllocMemBlock(int size, u32* addr){
	u32 id = sceKernelAllocPartitionMemory(2, "alloc", PSP_SMEM_Low, size, NULL);
	if((int)id < 0){
		id = sceKernelAllocPartitionMemory(2, "alloc", PSP_SMEM_High, size, NULL);
	}
	if((int)id < 0){
		gePrintDebug(0x102, "Memory allocation failed ! (sz=%d)\n", size);
	}
	*addr = (u32)sceKernelGetBlockHeadAddr(id);
	return id;
}
Exemplo n.º 13
0
void loadTheme()
{
	int i, bytesRead;

	SceUID fp;
	fp = sceIoOpen("ms0:/lockdown.thm", PSP_O_RDONLY, 0777);

	if (fp < 0)
	{
		fp = sceIoOpen("flash0:/lockdown.thm", PSP_O_RDONLY, 0777);

		if (fp < 0)
		{
			printTextScreen(0, 0, "Error loading flash0:/lockdown.thm.", RGB(255, 0, 0));
			printTextScreen(0, 8, "Place theme at ms0:/lockdown.thm to load from memory stick.", RGB(255, 0, 0));
			sceDisplayWaitVblankStart();
			flipScreen();
			sceKernelSleepThread();
		}
	}

	for (i = 0; i < NUMFILES; i++)
	{
		bytesRead = sceIoRead(fp, &images[i].hdr, sizeof(imagehdr));
		if (bytesRead != sizeof(imagehdr))
		{
			printTextScreen(0, 0, "Unexpected end of header.", RGB(255, 0, 0));
			sceDisplayWaitVblankStart();
			flipScreen();
			sceKernelSleepThread();
		}
		images[i].blockid = sceKernelAllocPartitionMemory(2, "block", 0, (sizeof(unsigned char) * images[i].hdr.size), NULL);
		if (images[i].blockid < 0)
		{
			printTextScreen(0, 0, "Memory allocation error.", RGB(255, 0, 0));
			sceDisplayWaitVblankStart();
			flipScreen();
			sceKernelSleepThread();
		}
		images[i].data = (unsigned char*) sceKernelGetBlockHeadAddr(images[i].blockid);
		bytesRead = sceIoRead(fp, images[i].data, images[i].hdr.size);
		if (bytesRead != images[i].hdr.size)
		{
			printTextScreen(0, 0, "Unexpected end of data.", RGB(255, 0, 0));
			sceDisplayWaitVblankStart();
			flipScreen();
			sceKernelSleepThread();
		}
	}

	sceIoClose(fp);
}
Exemplo n.º 14
0
char *testAlloc(const char *title, int partition, const char *name, int type, SceSize size, char *pos) {
	int result = sceKernelAllocPartitionMemory(partition, name, type, size, pos);
	if (result >= 0) {
		char *addr = (char *)sceKernelGetBlockHeadAddr(result);
		sceKernelFreePartitionMemory(result);

		printf("%s: OK (%s)\n", title, allocatedPos(addr, size));
		return addr;
	} else {
		printf("%s: Failed (%08X)\n", title, result);
		return NULL;
	}
}
Exemplo n.º 15
0
Arquivo: init.c Projeto: Kaegell/uofw
/* Exit Init and restart the kernel. */
static s32 ExitInit(s32 error)
{
    void *blk;
    SceUID blkId;
    
    g_init->vshParam.size = sizeof(SceKernelLoadExecVSHParam);
    blkId = sceKernelGetChunk(0);
    if (blkId <= 0) {
        blk = &g_init->unk60;
        g_init->unk60 = 32;
        g_init->unk64 = 32; 
        g_init->unk68 = 0;
        g_init->unk76 = 0;
    } else
        blk = sceKernelGetBlockHeadAddr(blkId);

    *(s32 *)(blk + 24) = error;
    g_init->vshParam.args = *(s32 *)blk;
    g_init->vshParam.configFile = PSP_BOOT_CONFIG_FILE_PATH;
    g_init->vshParam.vshmainArgp = blk;
    g_init->vshParam.string = NULL;
    g_init->vshParam.argp = blk;
    g_init->vshParam.vshmainArgs = *(s32 *)blk;
    g_init->vshParam.key = NULL;
    
    blkId = sceKernelGetChunk(4);
    if (blkId <= 0) {
        g_init->vshParam.extArgp = NULL;
        g_init->vshParam.extArgs = 0;
    } else {
        g_init->vshParam.extArgs = SysMemForKernel_CC31DEAD(blkId);
        g_init->vshParam.extArgp = sceKernelGetBlockHeadAddr(blkId);
    }
    /* Reboot the kernel. */
    sceKernelExitVSHKernel(&g_init->vshParam);
    return SCE_ERROR_OK;
}
Exemplo n.º 16
0
void _main(SceSize args, void *argp)
{
	static SceUID modid;
	static int retVal, status;
	static SceKernelLMOption option;
	extern u8 *payload;
	extern u32 size_payload;
	extern u32 size_payload_uncomp;
	u8 *buffer;
	SceUID bufferBlock;
	
	pspDebugScreenInit();
    	pspDebugScreenClear();
	pspDebugInstallKprintfHandler(NULL);
	//pspDebugInstallErrorHandler(NULL);
	pspSdkInstallNoPlainModuleCheckPatch();

	memset(&option, 0, sizeof(option));
	option.size = sizeof(option);
	option.mpidtext = 1;
	option.mpiddata = 1;
	option.position = 0;
	option.access = 1;
	bufferBlock = sceKernelAllocPartitionMemory(2, "PSPPackerBuffer", PSP_SMEM_High, size_payload_uncomp, NULL);
	if (bufferBlock < 0) {
		printf("allocpartitionmemory failed with 0x%08X\n", bufferBlock);
		goto exit;
	}
	buffer = sceKernelGetBlockHeadAddr(bufferBlock);
	retVal = sceKernelGzipDecompress(buffer, size_payload_uncomp, (u8*)&payload, 0);
	if (retVal < 0) {
		printf("sceKernelGzipDecompress failed with 0x%08X\n", retVal);
		goto exit;
	}
	
	modid = sceKernelLoadModuleBufferUsbWlan(size_payload, buffer, 0, &option);
	retVal = sceKernelFreePartitionMemory(bufferBlock);
	if (retVal < 0)
		printf("Error freeing decompress buffer (0x%08X)\n", retVal);
		
	if(modid > 0)
		retVal = sceKernelStartModule(modid, 0, NULL, &status, NULL);

exit:
	/* Let's bug out */
	//sceKernelSelfStopUnloadModule(0, 0, NULL);
	//return 0;
	sceKernelExitDeleteThread(0);
}
Exemplo n.º 17
0
Arquivo: init.c Projeto: Kaegell/uofw
/* Check if the kernel has to be re-started. */
static u32 ExitCheck(void)
{
    SceUID blkId;
    void *blk;
    
    blkId = sceKernelGetChunk(0);
    if (blkId <= 0)
        return SCE_ERROR_OK;
    
    blk = sceKernelGetBlockHeadAddr(blkId);
    s32 arg = *(s32 *)(blk + 20);
    if (arg != 0)
        ExitInit(arg);
        
    return SCE_ERROR_OK;
}
Exemplo n.º 18
0
int main_thread(SceSize args, void *argp)
{
	SceUID block_id;
	void *vram;

	block_id = sceKernelAllocPartitionMemory(4, "debugmenu", PSP_SMEM_Low, 512*272*2, NULL);
	if(block_id < 0)
	{
		Kprintf("Error could not allocate memory buffer 0x%08X\n", block_id);
		goto error;
	}
	 
	vram = (void*) (0xA0000000 | (unsigned int) sceKernelGetBlockHeadAddr(block_id));
	g_eventflag = sceKernelCreateEventFlag("DebugMenuEvent", 0, 0, NULL);
	if(g_eventflag < 0)
	{
		Kprintf("Could not create event flag %08X\n", g_eventflag);
		goto error;
	}

	//sceCtrlRegisterButtonCallback(0, PSP_CTRL_HOME, button_callback, NULL);
	sceCtrlRegisterButtonCallback(3, TRIGGER, button_callback, NULL);
	while(1)
	{
		unsigned int bits;
		if(sceKernelWaitEventFlag(g_eventflag, START_MENU,
					PSP_EVENT_WAITOR | PSP_EVENT_WAITCLEAR, &bits, NULL) < 0)
		{
			break;
		}
		sceCtrlSetButtonMasks(0xFFFF, 1);  // Mask lower 16bits
		sceCtrlSetButtonMasks(0x10000, 2); // Always return HOME key
		sceDisplaySetFrameBufferInternal(0, vram, 512, 0, 1);
		pspDebugScreenInitEx(vram, 0, 0);
		do_menu();
		sceDisplaySetFrameBufferInternal(0, 0, 512, 0, 1);
		sceCtrlSetButtonMasks(0x10000, 0); // Unset HOME key
		sceCtrlSetButtonMasks(0xFFFF, 0);  // Unset mask
		sceKernelClearEventFlag(g_eventflag, ~START_MENU);
	}

error:
	sceKernelExitDeleteThread(0);

	return 0;
}
Exemplo n.º 19
0
char *testAllocDiff(const char *title, int partition, const char *name, int type, SceSize size, char *pos, int diffHigh, char *diff) {
	int result = sceKernelAllocPartitionMemory(partition, name, type, size, pos);
	if (result >= 0) {
		char *addr = (char *)sceKernelGetBlockHeadAddr(result);
		sceKernelFreePartitionMemory(result);

		if ((diffHigh ? diff - addr : addr - diff) == 0x1800) {
			printf("That's strange: addr: %08x, diff: %08x\n", addr, diff);
		}

		printf("%s: OK (%s, difference %x)\n", title, allocatedPos(addr, size), diffHigh ? diff - addr : addr - diff);
		return addr;
	} else {
		printf("%s: Failed (%08X)\n", title, result);
		return NULL;
	}
}
Exemplo n.º 20
0
//id最高4位表示自动选择分区时是否释放半透明背景,次高4位表示分配空间类型,最低4位表示分区
void *smalloc(int size, int id)
{
	size = size + 64;
	int pid = id&0x000F;
	if(pid==0)
		pid = (id&0xF000) ? _choose_alloc(size) : choose_alloc(size);		
	if(pid==0)
		return NULL;
		
	int type;
	if(id&0x0F00)
		type = PSP_SMEM_High;
	else
		type = PSP_SMEM_Low;
		
	SceUID memID = sceKernelAllocPartitionMemory(pid, "", type, size, NULL);
	if (memID < 0)
		return NULL;
	unsigned int * lptr = sceKernelGetBlockHeadAddr(memID);
	*lptr = memID;
	return (lptr+16);
}
Exemplo n.º 21
0
/**
 * load an image index table from the storage media
 * @param file_index - the filename array index to be used
 */
int load_image_index(int file_index) {
    u32 index_size;
    void *block_addr;

    SceUID fd = open_file(image_files[file_index]);
    if(fd < 0) {
        return 0;
    }

    SceSize size = (SceSize)sceIoLseek(fd, 0, PSP_SEEK_END);
    sceIoLseek(fd, 0, PSP_SEEK_SET);

    kprintf("image index size: %i bytes\n", size);
    block_id = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_KERNEL, "diva_img", PSP_SMEM_High, size, NULL);
    if(block_id < 0) {
        kprintf("cannot allocate memory block\n");
        return 0;
    }
    block_addr = sceKernelGetBlockHeadAddr(block_id);
    kprintf("allocated memory block: %08X\n", (u32)block_addr);

    sceIoRead(fd, block_addr, size);
    sceIoClose(fd);

    cpk_count = *(u32 *)block_addr;
    kprintf("image entries: %i\n", cpk_count);
    cpk_table = (cpknode *)((u32)block_addr + 4);
    kprintf("cpk table addr: %08X\n", (u32)cpk_table);

    index_size = (cpk_count * 8) + 4;
    index_size += 16 - (index_size % 16);
    image_filenames = (char *)block_addr + index_size;
    kprintf("image filenames start: %08X\n", (u32)image_filenames);

    // save the correct cpk container to use
    cpk_file = cpk_pack[file_index];
    kprintf("Using %s as cpk file\n", cpk_file);
    return 1;
}
Exemplo n.º 22
0
void *alloc(int size) {
	void *allocAddr;

	size = ALIGN_UP(size, 4);

	if (commonInfo != NULL && commonInfo->freeAddr != NULL) {
		freeAddr = commonInfo->freeAddr;
		freeSize = commonInfo->freeSize;
	}

	if (freeSize >= size) {
		freeSize -= size;
		allocAddr = freeAddr + freeSize;
	} else {
		int allocSize = ALIGN_UP(size, 256);
		int result = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "JpcspTrace", PSP_SMEM_High, allocSize, 0);
		if (result >= 0) {
			void *newFreeAddr = sceKernelGetBlockHeadAddr(result);
			if (newFreeAddr + allocSize != freeAddr) {
				// Can't merge new allocated memory to previously allocated memory
				freeSize = 0;
			}
			freeAddr = newFreeAddr;
			freeSize += allocSize - size;
			allocAddr = freeAddr + freeSize;
		} else {
			allocAddr = NULL;
		}
	}

	if (commonInfo != NULL) {
		commonInfo->freeAddr = freeAddr;
		commonInfo->freeSize = freeSize;
	}

	return allocAddr;
}
Exemplo n.º 23
0
Arquivo: init.c Projeto: Kaegell/uofw
/* Allocate memory for protected modules. */
static void ProtectHandling(SceLoadCoreBootInfo *bootInfo)
{
    SceUID partId;
    SceSysmemMemoryBlockInfo blkInfo;
    
    s32 i;
    for (i = 0; i < bootInfo->numProtects; i++) {
        SceLoadCoreProtectInfo *protectInfo = &bootInfo->protects[i];
        switch (GET_PROTECT_INFO_TYPE(protectInfo->attr)) { 
        case SCE_PROTECT_INFO_TYPE_FILE_NAME:
            if (protectInfo->size != 0) { //0x000009AC
                partId = sceKernelAllocPartitionMemory(SCE_KERNEL_PRIMARY_KERNEL_PARTITION, "SceInitFileName", 
                        SCE_KERNEL_SMEM_High, protectInfo->size, 0);
                if (partId > 0) {
                    void *addr = sceKernelGetBlockHeadAddr(partId);
                    memmove(addr, (void *)protectInfo->addr, protectInfo->size);
                    
                    protectInfo->addr = (u32)addr;
                    protectInfo->partId = partId;
                    protectInfo->attr = SET_PROTECT_INFO_STATE(SCE_PROTECT_INFO_STATE_IS_ALLOCATED, protectInfo->attr);
                }
            }
            if (i == bootInfo->modProtId)
                g_init->fileModAddr = (void *)protectInfo->addr;
            break;
        case SCE_PROTECT_INFO_TYPE_VSH_PARAM: 
            if (protectInfo->size != 0) {
                partId = sceKernelAllocPartitionMemory(SCE_KERNEL_PRIMARY_KERNEL_PARTITION, "SceInitVSHParam", 
                        SCE_KERNEL_SMEM_High, protectInfo->size, 0);
                if (partId > 0) {
                    blkInfo.size = sizeof blkInfo;
                    sceKernelQueryMemoryBlockInfo(partId, &blkInfo);
                    void *addr = (void *)blkInfo.addr;
                    memmove(addr, (void *)protectInfo->addr, protectInfo->size);
                    
                    protectInfo->addr = (u32)addr;
                    protectInfo->attr = SET_PROTECT_INFO_STATE(SCE_PROTECT_INFO_STATE_IS_ALLOCATED, protectInfo->attr);
                    protectInfo->partId = partId;
                    
                    *(u32 *)(addr + 4) = 32;
                    *(u32 *)(addr + 0) = blkInfo.memSize;
                    *(s32 *)(addr + 24) = 0;
                    *(s32 *)(addr + 20) = 0;
                    sceKernelRegisterChunk(0, partId);
                }
            }
            break;
        case SCE_PROTECT_INFO_TYPE_DISC_IMAGE:
            if (protectInfo->size != 0) {
                partId = sceKernelAllocPartitionMemory(SCE_KERNEL_PRIMARY_KERNEL_PARTITION, "SceInitDiscImage", 
                        SCE_KERNEL_SMEM_High, protectInfo->size, 0);
                if (partId > 0) {
                    void *addr = sceKernelGetBlockHeadAddr(partId);
                    memmove(addr, (void *)protectInfo->addr, protectInfo->size);
                    
                    protectInfo->addr = (u32)addr;
                    protectInfo->attr = SET_PROTECT_INFO_STATE(SCE_PROTECT_INFO_STATE_IS_ALLOCATED, protectInfo->attr);
                    protectInfo->partId = partId;
                    
                    sceKernelRegisterChunk(3, partId);
                    g_init->discModAddr = addr;
                }
            }
            break;
        case SCE_PROTECT_INFO_TYPE_NPDRM_DATA: 
            if ((protectInfo->size) != 0 && (*(u32 *)(protectInfo->addr) == sizeof(SceNpDrm)))
                memcpy(&g_npDrmData, (void *)protectInfo->addr, sizeof(SceNpDrm));
            break;
        case SCE_PROTECT_INFO_TYPE_USER_PARAM:
            if (protectInfo->size != 0) {
                partId = sceKernelAllocPartitionMemory(SCE_KERNEL_PRIMARY_KERNEL_PARTITION, "SceInitUserParam", 
                        SCE_KERNEL_SMEM_High, protectInfo->size, 0);
                if (partId > 0) {
                    void *addr = sceKernelGetBlockHeadAddr(partId);
                    memmove(addr, (void *)protectInfo->addr, protectInfo->size);
                    
                    protectInfo->addr = (u32)addr;
                    protectInfo->attr = SET_PROTECT_INFO_STATE(SCE_PROTECT_INFO_STATE_IS_ALLOCATED, protectInfo->attr);
                    protectInfo->partId = partId;
                }
            }
            break;
        case SCE_PROTECT_INFO_TYPE_PARAM_SFO:
            if (protectInfo->size != 0) { //0x00000B24
                partId = sceKernelAllocPartitionMemory(SCE_KERNEL_PRIMARY_KERNEL_PARTITION, "SceInitParamsfo", 
                        SCE_KERNEL_SMEM_High, protectInfo->size, 0);
                if (partId > 0) { //0x00000B40
                    void *addr = sceKernelGetBlockHeadAddr(partId);
                    memmove(addr, (void *)protectInfo->addr, protectInfo->size);
                    
                    protectInfo->addr = (u32)addr;
                    protectInfo->attr = SET_PROTECT_INFO_STATE(SCE_PROTECT_INFO_STATE_IS_ALLOCATED, protectInfo->attr);
                    protectInfo->partId = partId;
                    
                    sceKernelRegisterChunk(4, partId);
                    g_init->paramSfoBase = addr;
                    g_init->paramSfoSize = protectInfo->size;
                }
            }
            break;
        }
    }
    if (sceKernelGetChunk(0) < SCE_ERROR_OK) {
        partId = sceKernelAllocPartitionMemory(SCE_KERNEL_PRIMARY_KERNEL_PARTITION, "SceInitVSHParam", 
                SCE_KERNEL_SMEM_High, 32, 0);
        if (partId > 0) {
            blkInfo.size = sizeof blkInfo;
            sceKernelQueryMemoryBlockInfo(partId, &blkInfo);
            
            void *addr = (void *)blkInfo.addr;
            *(s32 *)(addr + 4) = 32;
            *(s32 *)(addr + 0) = blkInfo.memSize;
            *(s32 *)(addr + 24) = 0;
            *(s32 *)(addr + 8) = 0;
            *(s32 *)(addr + 12) = 0;
            *(s32 *)(addr + 16) = 0;
            *(s32 *)(addr + 20) = 0;
            sceKernelRegisterChunk(0, partId);
        }
    }
}
Exemplo n.º 24
0
int main(int argc, char *argv[]) {
	int i;
	char temp[128];

	setup();

	testAlloc("Normal", PSP_MEMORY_PARTITION_USER, "test", 0, 0x10, NULL);

	printf("\nNames:\n");
	testAlloc("  NULL name", PSP_MEMORY_PARTITION_USER, NULL, 0, 0x1000, NULL);
	testAlloc("  Blank name", PSP_MEMORY_PARTITION_USER, "", 0, 0x1000, NULL);
	testAlloc("  Long name", PSP_MEMORY_PARTITION_USER, "1234567890123456789012345678901234567890123456789012345678901234", 0, 0x1000, NULL);

	printf("\nPartitions:\n");
	int parts[] = {-5, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	for (i = 0; i < sizeof(parts) / sizeof(parts[0]); ++i) {
		sprintf(temp, "  Partition %d", parts[i]);
		testAlloc(temp, parts[i], "part", 0, 0x1000, NULL);
	}

	printf("\nTypes:\n");
	unsigned int types[] = {-5, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	for (i = 0; i < sizeof(types) / sizeof(types[0]); ++i) {
		sprintf(temp, "  Type %d", types[i]);
		testAlloc(temp, PSP_MEMORY_PARTITION_USER, "type", types[i], 0x1000, NULL);
	}

	printf("\nSizes:\n");
	unsigned int sizes[] = {
		-1, 0, 1, 0x10, 0x20, 0x2F, 0x30, 0x31, 0x32, 0x36, 0x38, 0x39, 0x3A,
		0x131, 0x136, 0x139, 0x1000, 0x10000, 0x100000, 0x1000000, 0x10000000,
		0x1800000, 0x2000000,
	};
	for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); ++i) {
		sprintf(temp, "  Size 0x%08X", sizes[i]);
		testAlloc(temp, PSP_MEMORY_PARTITION_USER, "size", 0, sizes[i], NULL);
	}
	
	printf("\nPositions:\n");
	testAlloc("  Wrong type", PSP_MEMORY_PARTITION_USER, "pos", 0, 0x1000, high - 0x1000);
	testAlloc("  Below low", PSP_MEMORY_PARTITION_USER, "pos", 2, 0x1000, low - 0x1000);
	testAlloc("  At low", PSP_MEMORY_PARTITION_USER, "pos", 2, 0x1000, low);
	testAlloc("  Above low", PSP_MEMORY_PARTITION_USER, "pos", 2, 0x1000, low + 0x1000);
	testAlloc("  Near high", PSP_MEMORY_PARTITION_USER, "pos", 2, 0x1000, high - 0x1000);
	testAlloc("  At high", PSP_MEMORY_PARTITION_USER, "pos", 2, 0x1000, high);
	testAlloc("  Above high", PSP_MEMORY_PARTITION_USER, "pos", 2, 0x1000, high + 0x1000);

	SceUID posPart1 = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "part1", 2, 0x1000, low);
	char *pos1 = (char *)sceKernelGetBlockHeadAddr(posPart1);
	testAlloc("  Second at low", PSP_MEMORY_PARTITION_USER, "part2", 2, 0x1000, low);
	char *pos2 = testAlloc("  Second type low", PSP_MEMORY_PARTITION_USER, "part2", 0, 0x1000, low);
	printf("    Difference: %X\n", pos2 - pos1);
	sceKernelFreePartitionMemory(posPart1);

	printf("\nAlignment:\n");
	SceUID alignbase[0x1000];
	int alignbaseCount;
	for (alignbaseCount = 0; alignbaseCount < 0x1000; ++alignbaseCount) {
		alignbase[alignbaseCount] = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "alignbase", 0, 0x1, NULL);
		char *base = (char *)sceKernelGetBlockHeadAddr(alignbase[alignbaseCount]);
		if (((u32)base & 0xFFF) == 0xF00)
			break;
	}
	SceUID alignLowID = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "part1", 3, 0x1000, (void *)0x1000);
	SceUID alignHighID = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "part2", 4, 0x1000, (void *)0x1000);
	char *alignLow = (char *)sceKernelGetBlockHeadAddr(alignLowID);
	char *alignHigh = (char *)sceKernelGetBlockHeadAddr(alignHighID);

	unsigned int aligns[] = {
		-5, -1, 0, 1, 2, 3, 4, 7, 8, 0x10, 0x11, 0x20, 0x2F, 0x40, 0x60, 0x80, 0x100,
		0x1000, 0x2000, 0x1000000, 0x4000000, 0x40000000, 0x80000000,
	};
	for (i = 0; i < sizeof(aligns) / sizeof(aligns[0]); ++i) {
		sprintf(temp, "  Align 0x%08X low", aligns[i]);
		testAllocDiff(temp, PSP_MEMORY_PARTITION_USER, "part2", 3, 0x1000, (char *)aligns[i], 0, alignLow);

		sprintf(temp, "  Align 0x%08X high", aligns[i]);
		testAllocDiff(temp, PSP_MEMORY_PARTITION_USER, "part2", 4, 0x1000, (char *)aligns[i], 1, alignHigh);
	}
	sceKernelFreePartitionMemory(alignLowID);
	while (alignbaseCount >= 0) {
		sceKernelFreePartitionMemory(alignbase[alignbaseCount--]);
	}
	sceKernelFreePartitionMemory(alignHighID);

	printf("\n");
	SceUID part1 = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "part1", 0, 0x1, NULL);
	SceUID part2 = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "part2", 0, 0x1, NULL);
	if (part1 > 0 && part2 > 0) {
		printf("Two with same name: OK\n");
	} else {
		printf("Two with same name: Failed (%08X, %08X)\n", part1, part2);
	}
	char *part1Pos = (char *)sceKernelGetBlockHeadAddr(part1);
	char *part2Pos = (char *)sceKernelGetBlockHeadAddr(part2);
	printf("Minimum difference: %x\n", part2Pos - part1Pos);
	sceKernelFreePartitionMemory(part1);
	sceKernelFreePartitionMemory(part2);

	part1 = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "part1", 3, 0x101, (void *)1);
	part2 = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "part2", 3, 0x101, (void *)1);
	part1Pos = (char *)sceKernelGetBlockHeadAddr(part1);
	part2Pos = (char *)sceKernelGetBlockHeadAddr(part2);
	printf("Offset difference: %x\n", part2Pos - part1Pos);
	sceKernelFreePartitionMemory(part1);
	sceKernelFreePartitionMemory(part2);

	SceUID reschedThread = sceKernelCreateThread("resched", &reschedFunc, sceKernelGetThreadCurrentPriority(), 0x1000, 0, NULL);
	sceKernelStartThread(reschedThread, 0, NULL);
	SceUID reschedPart = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "part", 0, 0x1000, NULL);
	sceKernelGetBlockHeadAddr(reschedPart);
	sceKernelFreePartitionMemory(reschedPart);
	sceKernelTerminateDeleteThread(reschedThread);
	printf("Reschedule: %s\n", didResched ? "yes" : "no");

	SceUID allocs[1024];
	int result = 0;
	for (i = 0; i < 1024; i++)
	{
		allocs[i] = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "create", 0, 0x100, NULL);
		if (allocs[i] < 0)
		{
			result = allocs[i];
			break;
		}
	}

	if (result != 0)
		printf("Create 1024: Failed at %d (%08X)\n", i, result);
	else
		printf("Create 1024: OK\n");

	while (--i >= 0)
		sceKernelFreePartitionMemory(allocs[i]);

	printf("Get deleted: %08X\n", (unsigned int)sceKernelGetBlockHeadAddr(reschedPart));
	printf("Get NULL: %08X\n", (unsigned int)sceKernelGetBlockHeadAddr(0));
	printf("Get invalid: %08X\n", (unsigned int)sceKernelGetBlockHeadAddr(0xDEADBEEF));
	printf("Free deleted: %08X\n", sceKernelFreePartitionMemory(reschedPart));
	printf("Free NULL: %08X\n", sceKernelFreePartitionMemory(0));
	printf("Free invalid: %08X\n", sceKernelFreePartitionMemory(0xDEADBEEF));

	return 0;
}
Exemplo n.º 25
0
// Loads a module to memory
SceUID load_module(SceUID fd, const char *path, void *addr, SceOff off)
{
	_sceModuleInfo modinfo;
	Elf32_Ehdr ehdr;
	Elf32_Phdr *phdrs;
	tStubEntry *stubs;
	SceUID phdrs_block;
	SceUID modid = mod_loaded_num;
	size_t phdrs_size, mod_size, stubs_size;
	int i, ret;

	dbg_printf("\n\n->Entering load_module...\n");

	for (i = 0; path[i]; i++) {
		if (i >= sizeof(mod_table[modid].path) - 1)
			return SCE_KERNEL_ERROR_ILLEGAL_ARGUMENT;

		mod_table[modid].path[i] = path[i];
	}
	mod_table[modid].path[i] = '\0';

	//dbg_printf("mod_table address: 0x%08X\n", mod_table);

	// Read ELF header
	sceIoLseek(fd, off, PSP_SEEK_SET);
	sceIoRead(fd, &ehdr, sizeof(ehdr));

	// Check for module encryption
	if (!strncmp(ehdr.e_ident, "~PSP", 4))
		return SCE_KERNEL_ERROR_UNSUPPORTED_PRX_TYPE;

	dbg_printf("\n->ELF header:\n"
		"Type: 0x%08X\n"
		"Code entry: 0x%08X\n"
		"Program header table offset: 0x%08X\n"
		"Program header size: 0x%08X\n"
		"Number of program headers: 0x%08X\n"
		"Section header table offset: 0x%08X\n"
		"Section header size: 0x%08X\n"
		"Number of section headers: 0x%08X\n",
		ehdr.e_type,
		(int)ehdr.e_entry,
		ehdr.e_phoff,
		ehdr.e_phentsize,
		ehdr.e_phnum,
		ehdr.e_shoff,
		ehdr.e_shentsize,
		ehdr.e_shnum);

	phdrs_size = ehdr.e_phentsize * ehdr.e_phnum;

	ret = sceKernelAllocPartitionMemory(
		2, "HBL Module Program Headers", PSP_SMEM_High, phdrs_size, NULL);
	if (ret < 0)
		goto fail;
	else
		phdrs_block = ret;

	phdrs = sceKernelGetBlockHeadAddr(phdrs_block);
	if (phdrs == NULL) {
		ret = SCE_KERNEL_ERROR_ERROR;
		goto fail;
	}

	ret = sceIoLseek(fd, off + ehdr.e_phoff, PSP_SEEK_SET);
	if (ret < 0)
		goto fail;

	ret = sceIoRead(fd, phdrs, phdrs_size);
	if (ret < 0)
		goto fail;

	switch (ehdr.e_type) {
		case ELF_STATIC:
			if (modid > 0) {
				ret = SCE_KERNEL_ERROR_EXCLUSIVE_LOAD;
				goto fail;
			}

			// Load ELF program section into memory
			ret = elf_load(fd, off, phdrs, ehdr.e_phnum, modmgrMalloc);
			if (ret < 0)
				goto fail;
			else
				mod_size = ret;

			// Locate ELF's .lib.stubs section
			stubs = elf_find_imports(fd, off, &ehdr, &stubs_size);
			if (stubs == NULL) {
				ret = SCE_KERNEL_ERROR_ERROR;
				goto fail;
			}

			mod_table[modid].text_entry = (u32 *)ehdr.e_entry;
			ret = elf_get_gp(fd, off, &ehdr, &mod_table[modid].gp);
			if (ret < 0)
				goto fail;

			break;

		case ELF_RELOC:
			if (modid >= MAX_MODULES) {
				ret = SCE_KERNEL_ERROR_EXCLUSIVE_LOAD;
				goto fail;
			}

			dbg_printf("load_module -> Offset: 0x%08X\n", off);

			// Load PRX program section
			ret = prx_load(fd, off, &ehdr, phdrs,
				&modinfo, &addr, modmgrMalloc);
			if (ret < 0)
				goto fail;
			else
				mod_size = ret;

			stubs = (void *)((int)modinfo.stub_top + (int)addr);
			stubs_size = (int)modinfo.stub_end - (int)modinfo.stub_top;

			// Relocate ELF entry point and GP register
			mod_table[modid].text_entry = (u32 *)((u32)ehdr.e_entry + (int)addr);
			mod_table[modid].gp = (void *)((int)modinfo.gp_value + (int)addr);

			break;

		default:
			dbg_printf("Uknown ELF type: 0x%08X\n", ehdr.e_type);
			ret = SCE_KERNEL_ERROR_UNSUPPORTED_PRX_TYPE;
			goto fail;
	}

	dbg_printf("resolve stubs\n");
	// Resolve ELF's stubs with game's stubs and syscall estimation
	ret = resolve_imports(stubs, stubs_size);
	if (ret)
		dbg_printf("failed to resolve imports: 0x%08X\n", ret);

	mod_table[modid].state = LOADED;

	mod_loaded_num++;
	//dbg_printf("Module table updated\n");

	dbg_printf("\n->Actual number of loaded modules: %d\n", mod_loaded_num);
	dbg_printf("Last loaded module [%d]:\n", modid);
#ifdef DEBUG
	log_mod_entry(mod_table[modid]);
#endif

	synci(addr, addr + mod_size);

	ret = modid;
fail:
	sceKernelFreePartitionMemory(phdrs_block);
	return ret;
}