/* * Free memory used by protected modules and free the memory containing the * information about the modules to boot on a kernel (re-)start. */ static s32 CleanupPhase1(SceLoadCoreBootInfo *bootInfo) { SceUID partId; SceUID blkId; s32 status; if ((u32)g_init->discModAddr > 0) return SCE_ERROR_OK; g_init->discModAddr += 1; s32 i; for (i = 0; i < bootInfo->numProtects; i++) { SceLoadCoreProtectInfo *prot = &bootInfo->protects[i]; if (GET_PROTECT_INFO_TYPE(prot->attr) == 0x200 && (GET_PROTECT_INFO_STATE(prot->attr) & SCE_PROTECT_INFO_STATE_IS_ALLOCATED)) { sceKernelFreePartitionMemory(prot->partId); prot->attr = REMOVE_PROTECT_INFO_STATE(SCE_PROTECT_INFO_STATE_IS_ALLOCATED, prot->attr); } } if (bootInfo->modules == NULL) return SCE_ERROR_OK; status = sceKernelQueryMemoryInfo((u32)bootInfo->modules, &partId, &blkId); if (status < SCE_ERROR_OK) return status; sceKernelFreePartitionMemory(blkId); return SCE_ERROR_OK; }
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); }
int main(int argc, char *argv[]) { checkpointNext("Establishing base values:"); SceSize baseMaxFree = sceKernelMaxFreeMemSize(); if (baseMaxFree < 0) { checkpoint("sceKernelMaxFreeMemSize: %08x", baseMaxFree); } else { checkpoint("sceKernelMaxFreeMemSize: OK"); } SceSize baseTotal = sceKernelTotalFreeMemSize(); if (baseTotal < 0) { checkpoint("sceKernelTotalFreeMemSize: %08x", baseTotal); } else { checkpoint("sceKernelTotalFreeMemSize: OK"); } checkpointNext("After allocating:"); SceUID blocks[8]; int i; for (i = 0; i < 8; ++i) { blocks[i] = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "test", PSP_SMEM_Low, 0x8000, NULL); } checkpoint("sceKernelMaxFreeMemSize: at base - %d", baseMaxFree - sceKernelMaxFreeMemSize()); checkpoint("sceKernelTotalFreeMemSize: at base - %d", baseTotal - sceKernelTotalFreeMemSize()); checkpointNext("After fragmenting:"); sceKernelFreePartitionMemory(blocks[5]); blocks[5] = -1; checkpoint("sceKernelMaxFreeMemSize: at base - %d", baseMaxFree - sceKernelMaxFreeMemSize()); checkpoint("sceKernelTotalFreeMemSize: at base - %d", baseTotal - sceKernelTotalFreeMemSize()); checkpointNext("After free again:"); for (i = 0; i < 8; ++i) { if (blocks[i] >= 0) { sceKernelFreePartitionMemory(blocks[i]); } } checkpoint("sceKernelMaxFreeMemSize: at base - %d", baseMaxFree - sceKernelMaxFreeMemSize()); checkpoint("sceKernelTotalFreeMemSize: at base - %d", baseTotal - sceKernelTotalFreeMemSize()); checkpointNext("Allocate near limits:"); tryAllocate("Allocate sceKernelMaxFreeMemSize", sceKernelMaxFreeMemSize()); tryAllocate("Allocate sceKernelMaxFreeMemSize + 0x100", sceKernelMaxFreeMemSize() + 0x100); tryAllocate("Allocate sceKernelTotalFreeMemSize", sceKernelTotalFreeMemSize()); return 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; }
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; }
//OK void zeroCtrlFreeUserBuffer(SceUID uid) { if (uid >= 0) { k1 = pspSdkSetK1(0); sceKernelFreePartitionMemory(uid); pspSdkSetK1(k1); } }
void tryAllocate(const char *title, SceSize size) { SceUID alloc = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER, "test", PSP_SMEM_Low, size, NULL); if (alloc >= 0) { checkpoint("%s: OK", title); sceKernelFreePartitionMemory(alloc); } else { checkpoint("%s: %08x", title, alloc); } }
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; }
int mhp3_close(SceUID fd) { if (fd == datafd) { datafd = -1; if (memid >= 0) { sceKernelFreePartitionMemory(memid); } } else { unregister_install(fd); } int res = sceIoClose(fd); return res; }
void geFree(void* data){ gePrintDebug(0x100, "geFree(0x%08lX)\n", (t_ptr)data); if(data != NULL && data != (void*)0xDEADBEEF && data != (void*)0xBAADF00D){ t_ptr* var = (t_ptr*)data; u32 id = var[-2]; // Get the ID t_ptr size = var[-1]; // Get the size sceKernelFreePartitionMemory(id); libge_context->mem -= size; libge_context->frees++; } }
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; } }
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); }
/* * Free memory used by protected modules, free the memory containing protection * information and free the memory holding the kernel boot information. */ static s32 CleanupPhase2(SceLoadCoreBootInfo *bootInfo) { SceUID partId; SceUID blkId; s32 status; s32 i; for (i = 0; i < bootInfo->numProtects; i++) { SceLoadCoreProtectInfo *prot = &bootInfo->protects[i]; switch (GET_PROTECT_INFO_TYPE(prot->attr)) { case SCE_PROTECT_INFO_TYPE_USER_PARAM: case 0x200: if (GET_PROTECT_INFO_STATE(prot->attr) & SCE_PROTECT_INFO_STATE_IS_ALLOCATED) { sceKernelFreePartitionMemory(prot->partId); prot->attr = REMOVE_PROTECT_INFO_STATE(SCE_PROTECT_INFO_STATE_IS_ALLOCATED, prot->attr); } break; case SCE_PROTECT_INFO_TYPE_FILE_NAME: if (GET_PROTECT_INFO_STATE(prot->attr) & SCE_PROTECT_INFO_STATE_IS_ALLOCATED) { sceKernelFreePartitionMemory(prot->partId); prot->attr = REMOVE_PROTECT_INFO_STATE(SCE_PROTECT_INFO_STATE_IS_ALLOCATED, prot->attr); g_init->fileModAddr = NULL; } break; } } if (bootInfo->protects != NULL) { status = sceKernelQueryMemoryInfo((u32)bootInfo->protects, &partId, &blkId); if (status < SCE_ERROR_OK) return status; sceKernelFreePartitionMemory(partId); } sceKernelQueryMemoryInfo((u32)bootInfo, &partId, &blkId); sceKernelFreePartitionMemory(partId); return SCE_ERROR_OK; }
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; } }
void* geRealloc(void* last, int size){ gePrintDebug(0x100, "geRealloc(0x%p, %d)\n", (t_ptr)last, size); if(size <= 0){ geFree(last); return NULL; } if(last == NULL || last == (void*)0xDEADBEEF || last == (void*)0xBAADF00D){ return geMalloc(size); } t_ptr last_size = ((t_ptr*)last)[-1]; int block_sz = sizeof(t_ptr)*2; t_ptr addr = 0; u32 id = AllocMemBlock(size+16+block_sz, &addr); t_ptr* var = (t_ptr*)(ALIGN(addr+block_sz, 16) - block_sz); var[0] = id; //Store the ID in the first int var[1] = size; //Store the size on the 2nd int void* new_ptr = (void*)&var[2]; memset(new_ptr, 0x0, size); int sz_copy = last_size < size ? last_size : size; memcpy(new_ptr, last, sz_copy); //Copy the old data sceKernelFreePartitionMemory((u32)((t_ptr*)last)[-2]); libge_context->mem -= last_size; libge_context->mem += size; return new_ptr; //Then return the new pointer /* gePrintDebug(0x100, "geRealloc(0x%08X, %d)\n", (u32)last, size); u32 old_size = ((u32*)last)[-1]; //The size is stored in the 2nd byte u32* new_ptr = (u32*)geMalloc(size); //Do a malloc with new size memcpy(new_ptr, last, old_size); //Copy the old data geFree(last); //free the last block gePrintDebug(0x100, "FreeMem After Realloc: %d KB\n\n", sceKernelTotalFreeMemSize() / 1024); libge_context->mem -= old_size; libge_context->mem += size; return new_ptr; //Then return the new pointer */ }
void sfree(void *blockaddr) { if(blockaddr==NULL) return; SceUID blockid = *(((SceUID*)blockaddr) - 16); sceKernelFreePartitionMemory(blockid); }
int main_thread(SceSize args, void *argp) { #ifdef DEBUG pspDebugScreenInit(); printf("Free Memory: %u KB\n",sceKernelTotalFreeMemSize()/1024); #else initGraphics(); loadTheme(); background = loadImageFromMemory(images[BACKGROUND].data, images[BACKGROUND].hdr.size); sceKernelFreePartitionMemory(images[BACKGROUND].blockid); images[BACKGROUND].blockid = -1; mask = loadImageFromMemory(images[MASK].data, images[MASK].hdr.size); sceKernelFreePartitionMemory(images[MASK].blockid); images[MASK].blockid = -1; #endif int firstUse = 0; int recovery = 0; int bytesRead = 0; Config cfg; char input[11]; if ((args == 9) && (strcmp(argp, "recovery") == 0)) { recovery = 1; } SceUID fp; fp = sceIoOpen("flash0:/buttons.ini", PSP_O_RDONLY, 0777); if (fp < 0) { #ifdef DEBUG printf("\nflash0:/buttons.ini could not be opned. Assuming first usage."); #endif firstUse = 1; } else { bytesRead = sceIoRead(fp, &cfg, sizeof(cfg)); sceIoClose(fp); if(bytesRead != sizeof(cfg)) { firstUse = 1; } } if (firstUse) { setPassword(); } else if (((args == 0) || (recovery != 0)) || !(cfg.onlyBoot)) { int len; main_password: #ifndef DEBUG footer_pressselect = loadImageFromMemory(images[FOOTER_PRESSSELECT].data, images[FOOTER_PRESSSELECT].hdr.size); title_password = loadImageFromMemory(images[TITLE_PASSWORD].data, images[TITLE_PASSWORD].hdr.size); #endif while(1) { selectEnabled = 1; #ifdef DEBUG printf("\nPress START to accept.\nPress SELECT to change password.\nEnter password: "******"\nPassword OK."); #else int temp; blitImageToScreen(0, 0, images[BACKGROUND].hdr.w, images[BACKGROUND].hdr.h, background, images[BACKGROUND].hdr.x, images[BACKGROUND].hdr.y); blitAlphaImageToScreen(0, 0, images[FOOTER_PRESSSELECT].hdr.w, images[FOOTER_PRESSSELECT].hdr.h, footer_pressselect, images[FOOTER_PRESSSELECT].hdr.x, images[FOOTER_PRESSSELECT].hdr.y); blitAlphaImageToScreen(0, 0, images[TITLE_PASSWORD].hdr.w, images[TITLE_PASSWORD].hdr.h, title_password, images[TITLE_PASSWORD].hdr.x, images[TITLE_PASSWORD].hdr.y); msg_passwordok = loadImageFromMemory(images[MSG_PASSWORDOK].data, images[MSG_PASSWORDOK].hdr.size); blitAlphaImageToScreen(0, 0, images[MSG_PASSWORDOK].hdr.w, images[MSG_PASSWORDOK].hdr.h, msg_passwordok, images[MSG_PASSWORDOK].hdr.x, images[MSG_PASSWORDOK].hdr.y); freeImage(msg_passwordok); for(temp = 0; temp < len; temp++) { blitAlphaImageToScreen(0, 0, images[MASK].hdr.w, images[MASK].hdr.h, mask, images[MASK].hdr.x+(temp*images[MASK].hdr.w), images[MASK].hdr.y); } sceDisplayWaitVblankStart(); flipScreen(); #endif sceKernelDelayThread(1000*1000); break; } else { #ifdef DEBUG printf("\nIncorrect password."); #else int temp; blitImageToScreen(0, 0, images[BACKGROUND].hdr.w, images[BACKGROUND].hdr.h, background, images[BACKGROUND].hdr.x, images[BACKGROUND].hdr.y); blitAlphaImageToScreen(0, 0, images[FOOTER_PRESSSELECT].hdr.w, images[FOOTER_PRESSSELECT].hdr.h, footer_pressselect, images[FOOTER_PRESSSELECT].hdr.x, images[FOOTER_PRESSSELECT].hdr.y); blitAlphaImageToScreen(0, 0, images[TITLE_PASSWORD].hdr.w, images[TITLE_PASSWORD].hdr.h, title_password, images[TITLE_PASSWORD].hdr.x, images[TITLE_PASSWORD].hdr.y); msg_passwordincorrect = loadImageFromMemory(images[MSG_PASSWORDINCORRECT].data, images[MSG_PASSWORDINCORRECT].hdr.size); blitAlphaImageToScreen(0, 0, images[MSG_PASSWORDINCORRECT].hdr.w, images[MSG_PASSWORDINCORRECT].hdr.h, msg_passwordincorrect, images[MSG_PASSWORDINCORRECT].hdr.x, images[MSG_PASSWORDINCORRECT].hdr.y); freeImage(msg_passwordincorrect); for(temp = 0; temp < len; temp++) { blitAlphaImageToScreen(0, 0, images[MASK].hdr.w, images[MASK].hdr.h, mask, images[MASK].hdr.x+(temp*images[MASK].hdr.w), images[MASK].hdr.y); } sceDisplayWaitVblankStart(); flipScreen(); #endif sceKernelDelayThread(3000*1000); } } #ifndef DEBUG freeImage(footer_pressselect); freeImage(title_password); #endif if(len == -1) { #ifndef DEBUG footer_changemode = loadImageFromMemory(images[FOOTER_CHANGEMODE].data, images[FOOTER_CHANGEMODE].hdr.size); title_oldpassword = loadImageFromMemory(images[TITLE_OLDPASSWORD].data, images[TITLE_OLDPASSWORD].hdr.size); #endif while(1) { #ifdef DEBUG printf("\nChange password mode.\nEnter old password: "******"\nPassword OK."); #else int temp; blitImageToScreen(0, 0, images[BACKGROUND].hdr.w, images[BACKGROUND].hdr.h, background, images[BACKGROUND].hdr.x, images[BACKGROUND].hdr.y); blitAlphaImageToScreen(0, 0, images[FOOTER_CHANGEMODE].hdr.w, images[FOOTER_CHANGEMODE].hdr.h, footer_changemode, images[FOOTER_CHANGEMODE].hdr.x, images[FOOTER_CHANGEMODE].hdr.y); blitAlphaImageToScreen(0, 0, images[TITLE_OLDPASSWORD].hdr.w, images[TITLE_OLDPASSWORD].hdr.h, title_oldpassword, images[TITLE_OLDPASSWORD].hdr.x, images[TITLE_OLDPASSWORD].hdr.y); msg_passwordok = loadImageFromMemory(images[MSG_PASSWORDOK].data, images[MSG_PASSWORDOK].hdr.size); blitAlphaImageToScreen(0, 0, images[MSG_PASSWORDOK].hdr.w, images[MSG_PASSWORDOK].hdr.h, msg_passwordok, images[MSG_PASSWORDOK].hdr.x, images[MSG_PASSWORDOK].hdr.y); freeImage(msg_passwordok); for(temp = 0; temp < len; temp++) { blitAlphaImageToScreen(0, 0, images[MASK].hdr.w, images[MASK].hdr.h, mask, images[MASK].hdr.x+(temp*images[MASK].hdr.w), images[MASK].hdr.y); } sceDisplayWaitVblankStart(); flipScreen(); #endif sceKernelDelayThread(3000*1000); break; } else { #ifdef DEBUG printf("\nIncorrect password."); #else int temp; blitImageToScreen(0, 0, images[BACKGROUND].hdr.w, images[BACKGROUND].hdr.h, background, images[BACKGROUND].hdr.x, images[BACKGROUND].hdr.y); blitAlphaImageToScreen(0, 0, images[FOOTER_CHANGEMODE].hdr.w, images[FOOTER_CHANGEMODE].hdr.h, footer_changemode, images[FOOTER_CHANGEMODE].hdr.x, images[FOOTER_CHANGEMODE].hdr.y); blitAlphaImageToScreen(0, 0, images[TITLE_OLDPASSWORD].hdr.w, images[TITLE_OLDPASSWORD].hdr.h, title_oldpassword, images[TITLE_OLDPASSWORD].hdr.x, images[TITLE_OLDPASSWORD].hdr.y); msg_passwordincorrect = loadImageFromMemory(images[MSG_PASSWORDINCORRECT].data, images[MSG_PASSWORDINCORRECT].hdr.size); blitAlphaImageToScreen(0, 0, images[MSG_PASSWORDINCORRECT].hdr.w, images[MSG_PASSWORDINCORRECT].hdr.h, msg_passwordincorrect, images[MSG_PASSWORDINCORRECT].hdr.x, images[MSG_PASSWORDINCORRECT].hdr.y); freeImage(msg_passwordincorrect); for(temp = 0; temp < len; temp++) { blitAlphaImageToScreen(0, 0, images[MASK].hdr.w, images[MASK].hdr.h, mask, images[MASK].hdr.x+(temp*images[MASK].hdr.w), images[MASK].hdr.y); } sceDisplayWaitVblankStart(); flipScreen(); #endif sceKernelDelayThread(3000*1000); } } #ifndef DEBUG freeImage(footer_changemode); freeImage(title_oldpassword); #endif if(len == -1) { goto main_password; } selectEnabled = 0; setPassword(); } } #ifndef DEBUG freeImage(background); freeImage(mask); sceGuTerm(); int i; for (i = 0; i < NUMFILES; i++) { if (images[i].blockid != -1) { sceKernelFreePartitionMemory(images[i].blockid); } } #endif __psp_free_heap(); #ifdef DEBUG printf("\n__psp_free_heap(): %u KB\n",sceKernelTotalFreeMemSize()/1024); sceKernelDelayThread(3000*1000); #endif #ifdef DEBUG printf("\nLoading loader.prx"); #endif SceUID mod = sceKernelLoadModule("flash0:/loader.prx", 0, NULL); if (mod & 0x80000000) { #ifdef DEBUG printf("\nLoadModule failed 0x%x", mod); #endif } else { SceUID startmod; startmod = sceKernelStartModule(mod, args, argp, NULL, NULL); if (mod != startmod) { #ifdef DEBUG printf("\nStartModule failed 0x%x", startmod); #endif } } return sceKernelExitDeleteThread(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; }
void unload_mod_index() { if(index_table) { sceKernelFreePartitionMemory(index_id); } }
void free_libName_buffer(LibStubSegment *libSegPtr) { if (libSegPtr->libName != NULL) { sceKernelFreePartitionMemory(libSegPtr->blockId); } }
// 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; }