Skip to content

Instantly share code, notes, and snippets.

@handsomematt
Last active August 22, 2017 11:50
Show Gist options
  • Save handsomematt/4f556abbe07dc16c0190 to your computer and use it in GitHub Desktop.
Save handsomematt/4f556abbe07dc16c0190 to your computer and use it in GitHub Desktop.
not that great, mainly for usage on games and not firmware
"""
//
// 3ds_svc.py
//
// This will go through each svc call and leave a comment based on it's
// service call
//
// also maps some service calls to procedures
//
"""
from idaapi import *
from idautils import *
import re
svcCalls = {
0x01: "Result ControlMemory(u32* outaddr, u32 addr0, u32 addr1, u32 size, u32 operation, u32 permissions) Outaddr is usually the same as the input addr0.",
0x02: "Result QueryMemory(MemoryInfo* info, PageInfo* out, u32 Addr)",
0x03: "void ExitProcess(void)",
0x04: "Result GetProcessAffinityMask(u8* affinitymask, Handle process, s32 processorcount)",
0x05: "Result SetProcessAffinityMask(Handle process, u8* affinitymask, s32 processorcount)",
0x06: "Result GetProcessIdealProcessor(s32 *idealprocessor, Handle process)",
0x07: "Result SetProcessIdealProcessor(Handle process, s32 idealprocessor)",
0x08: "Result CreateThread(Handle* thread, func entrypoint, u32 arg, u32 stacktop, s32 threadpriority, s32 processorid)",
0x09: "void ExitThread(void)",
0x0A: "void SleepThread(s64 nanoseconds)",
0x0B: "Result GetThreadPriority(s32* priority, Handle thread)",
0x0C: "Result SetThreadPriority(Handle thread, s32 priority)",
0x0D: "Result GetThreadAffinityMask(u8* affinitymask, Handle thread, s32 processorcount)",
0x0E: "Result SetThreadAffinityMask(Handle thread, u8* affinitymask, s32 processorcount)",
0x0F: "Result GetThreadIdealProcessor(s32* processorid, Handle thread)",
0x10: "Result SetThreadIdealProcessor(Handle thread, s32 processorid)",
0x11: "s32 GetCurrentProcessorNumber(void)",
0x12: "Result Run(Handle process, StartupInfo* info)",
0x13: "Result CreateMutex(Handle* mutex, bool initialLocked)",
0x14: "Result ReleaseMutex(Handle mutex)",
0x15: "Result CreateSemaphore(Handle* semaphore, s32 initialCount, s32 maxCount)",
0x16: "Result ReleaseSemaphore(s32* count, Handle semaphore, s32 releaseCount)",
0x17: "Result CreateEvent(Handle* event, ResetType resettype)",
0x18: "Result SignalEvent(Handle event)",
0x19: "Result ClearEvent(Handle event)",
0x1A: "Result CreateTimer(Handle* timer, ResetType resettype)",
0x1B: "Result SetTimer(Handle timer, s64 initial, s64 interval)",
0x1C: "Result CancelTimer(Handle timer)",
0x1D: "Result ClearTimer(Handle timer)",
0x1E: "Result CreateMemoryBlock(Handle* memblock, u32 addr, u32 size, u32 mypermission, u32 otherpermission)",
0x1F: "Result MapMemoryBlock(Handle memblock, u32 addr, u32 mypermissions, u32 otherpermission)",
0x20: "Result UnmapMemoryBlock(Handle memblock, u32 addr)",
0x21: "Result CreateAddressArbiter(Handle* arbiter)",
0x22: "Result ArbitrateAddress(Handle arbiter, u32 addr, ArbitrationType type, s32 value, s64 nanoseconds)",
0x23: "Result CloseHandle(Handle handle)",
0x24: "Result WaitSynchronization1(Handle handle, s64 nanoseconds)",
0x25: "Result WaitSynchronizationN(s32* out, Handle* handles, s32 handlecount, bool waitAll, s64 nanoseconds)",
0x26: "Result SignalAndWait(s32* out, Handle signal, Handle* handles, s32 handleCount, bool waitAll, s64 nanoseconds)",
0x27: "Result DuplicateHandle(Handle* out, Handle original)",
0x28: "s64 GetSystemTick(void) (This returns the total CPU ticks elapsed since the CPU was powered-on)",
0x29: "Result GetHandleInfo(s64* out, Handle handle, HandleInfoType type)",
0x2A: "Result GetSystemInfo(s64* out, SystemInfoType type, s32 param)",
0x2B: "Result GetProcessInfo(s64* out, Handle process, ProcessInfoType type)",
0x2C: "Result GetThreadInfo(s64* out, Handle thread, ThreadInfoType type)",
0x2D: "Result ConnectToPort(Handle* out, const char* portName)",
0x2E: "Result SendSyncRequest1(Handle session)",
0x2F: "Result SendSyncRequest2(Handle session)",
0x30: "Result SendSyncRequest3(Handle session)",
0x31: "Result SendSyncRequest4(Handle session)",
0x32: "Result SendSyncRequest(Handle session)",
0x33: "Result OpenProcess(Handle* process, u32 processId)",
0x34: "Result OpenThread(Handle* thread, Handle process, u32 threadId)",
0x35: "Result GetProcessId(u32* processId, Handle process)",
0x36: "Result GetProcessIdOfThread(u32* processId, Handle thread)",
0x37: "Result GetThreadId(u32* threadId, Handle thread)",
0x38: "Result GetResourceLimit(Handle* resourceLimit, Handle process)",
0x39: "Result GetResourceLimitLimitValues(s64* values, Handle resourceLimit, LimitableResource* names, s32 nameCount)",
0x3A: "Result GetResourceLimitCurrentValues(s64* values, Handle resourceLimit, LimitableResource* names, s32 nameCount)",
0x3B: "Result GetThreadContext(ThreadContext* context, Handle thread)",
0x3C: "Break(BreakReason)",
0x3D: "OutputDebugString(void const, int) Does nothing on non-debug units.",
0x3E: "ControlPerformanceCounter(unsigned long long, int, unsigned int, unsigned long long)",
0x47: "Result CreatePort(Handle* portServer, Handle* portClient, const char* name, s32 maxSessions)",
0x48: "Result CreateSessionToPort(Handle* session, Handle port)",
0x49: "Result CreateSession(Handle* sessionServer, Handle* sessionClient)",
0x4A: "Result AcceptSession(Handle* session, Handle port)",
0x4B: "Result ReplyAndReceive1(s32* index, Handle* handles, s32 handleCount, Handle replyTarget)",
0x4C: "Result ReplyAndReceive2(s32* index, Handle* handles, s32 handleCount, Handle replyTarget)",
0x4D: "Result ReplyAndReceive3(s32* index, Handle* handles, s32 handleCount, Handle replyTarget)",
0x4E: "Result ReplyAndReceive4(s32* index, Handle* handles, s32 handleCount, Handle replyTarget)",
0x4F: "Result ReplyAndReceive(s32* index, Handle* handles, s32 handleCount, Handle replyTarget)",
0x50: "Result BindInterrupt(Interrupt name, Handle syncObject, s32 priority, bool isManualClear)",
0x51: "Result UnbindInterrupt(Interrupt name, Handle syncObject)",
0x52: "Result InvalidateProcessDataCache(Handle process, void* addr, u32 size)",
0x53: "Result StoreProcessDataCache(Handle process, void const* addr, u32 size)",
0x54: "Result FlushProcessDataCache(Handle process, void const* addr, u32 size)",
0x55: "Result StartInterProcessDma(Handle* dma, Handle dstProcess, void* dst, Handle srcProcess, const void* src, u32 size, const DmaConfig& config)",
0x56: "Result StopDma(Handle dma)",
0x57: "Result GetDmaState(DmaState* state, Handle dma)",
0x58: "RestartDma(nn::Handle, void *, void const*, unsigned int, signed char)",
0x60: "Result DebugActiveProcess(Handle* debug, u32 processID)",
0x61: "Result BreakDebugProcess(Handle debug)",
0x62: "Result TerminateDebugProcess(Handle debug)",
0x63: "Result GetProcessDebugEvent(DebugEventInfo* info, Handle debug)",
0x64: "Result ContinueDebugEvent(Handle debug, u32 flags)",
0x65: "Result GetProcessList(s32* processCount, u32* processIds, s32 processIdMaxCount)",
0x66: "Result GetThreadList(s32* threadCount, u32* threadIds, s32 threadIdMaxCount, Handle domain)",
0x67: "Result GetDebugThreadContext(ThreadContext* context, Handle debug, u32 threadId, u32 controlFlags)",
0x68: "Result SetDebugThreadContext(Handle debug, u32 threadId, ThreadContext* context, u32 controlFlags)",
0x69: "Result QueryDebugProcessMemory(MemoryInfo* blockInfo, PageInfo* pageInfo, Handle process, u32 addr)",
0x6A: "Result ReadProcessMemory(void* buffer, Handle debug, u32 addr, u32 size)",
0x6B: "Result WriteProcessMemory(Handle debug, void const* buffer, u32 addr, u32 size)",
0x6C: "Result SetHardwareBreakPoint(s32 registerId, u32 control, u32 value)",
0x6D: "GetDebugThreadParam(long long *, int *, nn::Handle, unsigned int, nn::dmnt::DebugThreadParam)",
0x70: "Result ControlProcessMemory(Handle KProcess, unsigned int Addr0, unsigned int Addr1, unsigned int Size, unsigned int Type, unsigned int Permissions)",
0x71: "Result MapProcessMemory(Handle KProcess, unsigned int StartAddr, unsigned int EndAddr)",
0x72: "Result UnmapProcessMemory(Handle KProcess, unsigned int StartAddr, unsigned int EndAddr)",
0x73: "Result CreateCodeSet(Handle* handle_out, struct CodeSetInfo, u32 code_ptr, u32 ro_ptr, u32 data_ptr)",
0x74: "Result RandomStub()",
0x75: "Result CreateProcess(Handle* handle_out, Handle codeset_handle, u32 arm11kernelcaps_ptr, u32 arm11kernelcaps_num)",
0x76: "TerminateProcess(Handle)",
0x77: "Result SetProcessResourceLimits(Handle KProcess, Handle KResourceLimit)",
0x78: "Result CreateResourceLimit(Handle *KResourceLimit)",
0x79: "Result SetResourceLimitValues(Handle res_limit, LimitableResource* resource_type_list, s64* resource_list, u32 count)",
0x7A: "AddCodeSegment (unsigned int Addr, unsigned int Size)",
0x7B: "Backdoor(unsigned int CodeAddress)",
0x7C: "KernelSetState(unsigned int Type, unsigned int Param0, unsigned int Param1, unsigned int Param2)",
0x7D: "Result QueryProcessMemory(MemInfo *Info, unsigned int *Out, Handle KProcess, unsigned int Addr)",
0xFF: "???"
}
ipccalls = {
0x000100C0: "NSS:LaunchFIRM",
0x000200C0: "NSS:LaunchTitle",
0x000500C0: "NSS:LaunchApplicationFIRM",
0x00060042: "NSS:SetFIRMParams4A0",
0x00070042: "NSS:CardUpdateInitialize",
0x00080000: "NSS:ShutDownGamecardSystemUpdateInterface",
0x000D0140: "NSS:SetFIRMParams4B0",
0x000E0000: "NSS:ShutdownAsync",
0x000F0000: "NSS:APT:AppletUtilityWrapper",
0x00100180: "NSS:RebootSystem",
0x00150140: "NSS:LaunchApplication",
0x00160000: "NSS:HWReboot",
0x00010040: "APT:GetLockHandle",
0x00020080: "APT:Initialize",
0x00030040: "APT:Enable",
0x00040040: "APT:Finalize",
0x00050040: "APT:GetAppletManInfo",
0x00060040: "APT:GetAppletInfo",
0x00070000: "APT:GetLastSignaledAppletId",
0x00080000: "APT:CountRegisteredApplet",
0x00090040: "APT:IsRegistered",
0x000A0040: "APT:GetAttribute",
0x000B0040: "APT:InquireNotification",
0x000C0104: "APT:SendParameter",
0x000D0080: "APT:ReceiveParameter",
0x000E0080: "APT:GlanceParameter",
0x000F0100: "APT:CancelParameter",
0x001000C2: "APT:DebugFunc",
0x001100C0: "APT:MapProgramIdForDebug",
0x00120040: "APT:SetHomeMenuAppletIdForDebug",
0x00130000: "APT:GetPreparationState",
0x00140040: "APT:SetPreparationState",
0x00150140: "APT:PrepareToStartApplication",
0x00160040: "APT:PreloadLibraryApplet",
0x00170040: "APT:FinishPreloadingLibraryApplet",
0x00180040: "APT:PrepareToStartLibraryApplet",
0x00190040: "APT:PrepareToStartSystemApplet",
0x001A0000: "APT:PrepareToStartNewestHomeMenu",
0x001B00C4: "APT:StartApplication",
0x001C0000: "APT:WakeupApplication",
0x001D0000: "APT:CancelApplication",
0x001E0084: "APT:StartLibraryApplet",
0x001F0084: "APT:StartSystemApplet",
0x00200044: "APT:StartNewestHomeMenu",
0x00210000: "APT:OrderToCloseApplication",
0x00220040: "APT:PrepareToCloseApplication",
0x00230040: "APT:PrepareToJumpToApplication",
0x00240044: "APT:JumpToApplication",
0x002500C0: "APT:PrepareToCloseLibraryApplet",
0x00260000: "APT:PrepareToCloseSystemApplet",
0x00270044: "APT:CloseApplication",
0x00280044: "APT:CloseLibraryApplet",
0x00290044: "APT:CloseSystemApplet",
0x002A0000: "APT:OrderToCloseSystemApplet",
0x002B0000: "APT:PrepareToJumpToHomeMenu",
0x002C0044: "APT:JumpToHomeMenu",
0x002D0000: "APT:PrepareToLeaveHomeMenu",
0x002E0044: "APT:LeaveHomeMenu",
0x002F0040: "APT:PrepareToLeaveResidentApplet",
0x00300044: "APT:LeaveResidentApplet",
0x00310100: "APT:PrepareToDoApplicationJump",
0x00320084: "APT:DoApplicationJump",
0x00330000: "APT:GetProgramIdOnApplicationJump",
0x00340084: "APT:SendDeliverArg",
0x00350080: "APT:ReceiveDeliverArg",
0x00360040: "APT:LoadSysMenuArg",
0x00370042: "APT:StoreSysMenuArg",
0x00380040: "APT:PreloadResidentApplet",
0x00390040: "APT:PrepareToStartResidentApplet",
0x003A0044: "APT:StartResidentApplet",
0x003B0040: "APT:CancelLibraryApplet",
0x003C0042: "APT:SendDspSleep",
0x003D0042: "APT:SendDspWakeUp",
0x003E0080: "APT:ReplySleepQuery",
0x003F0040: "APT:ReplySleepNotificationComplete",
0x00400042: "APT:SendCaptureBufferInfo",
0x00410040: "APT:ReceiveCaptureBufferInfo",
0x00420080: "APT:SleepSystem",
0x00430040: "APT:NotifyToWait",
0x00440000: "APT:GetSharedFont",
0x00450040: "APT:GetWirelessRebootInfo",
0x00460104: "APT:Wrap",
0x00470104: "APT:Unwrap",
0x00480100: "APT:GetProgramInfo",
0x00490180: "APT:Reboot",
0x004A0040: "APT:GetCaptureInfo",
0x004B00C2: "APT:AppletUtility",
0x004C0000: "APT:SetFatalErrDispMode",
0x004D0080: "APT:GetAppletProgramInfo",
0x004E0000: "APT:HardwareResetAsync",
0x004F0080: "APT:SetApplicationCpuTimeLimit",
0x00500040: "APT:GetApplicationCpuTimeLimit",
0x00510080: "APT:GetStartupArgument",
0x00520104: "APT:Wrap1",
0x00530104: "APT:Unwrap1",
0x00550040: "APT:SetNSState",
0x00560000: "APT:GetNSState",
0x00570044: "APT:WakeupApplication2",
0x00580002: "APT:GetProgramID",
0x01010000: "APT:CheckNew3DSApp",
0x01020000: "APT:CheckNew3DS",
0x01040000: "APT:IsStandardMemoryLayout",
0x01050100: "APT:IsTitleAllowed",
0x000100C6: "fs:USER:Dummy1",
0x040100C4: "fs:USER:Control",
0x08010002: "fs:USER:Initialize",
0x080201C2: "fs:USER:OpenFile",
0x08030204: "fs:USER:OpenFileDirectly",
0x08040142: "fs:USER:DeleteFile",
0x08050244: "fs:USER:RenameFile",
0x08060142: "fs:USER:DeleteDirectory",
0x08070142: "fs:USER:DeleteDirectoryRecursively",
0x08080202: "fs:USER:CreateFile",
0x08090182: "fs:USER:CreateDirectory",
0x080A0244: "fs:USER:RenameDirectory",
0x080B0102: "fs:USER:OpenDirectory",
0x080C00C2: "fs:USER:OpenArchive",
0x080D0144: "fs:USER:ControlArchive",
0x080E0080: "fs:USER:CloseArchive",
0x080F0180: "fs:USER:Obsoleted_2_0_FormatThisUserSaveData",
0x08100200: "fs:USER:Obsoleted_3_0_CreateSystemSaveData",
0x08110040: "fs:USER:Obsoleted_3_0_DeleteSystemSaveData",
0x08120080: "fs:USER:GetFreeBytes",
0x08130000: "fs:USER:GetCardType",
0x08140000: "fs:USER:GetSdmcArchiveResource",
0x08150000: "fs:USER:GetNandArchiveResource",
0x08160000: "fs:USER:GetSdmcFatfsError",
0x08170000: "fs:USER:IsSdmcDetected",
0x08180000: "fs:USER:IsSdmcWritable",
0x08190042: "fs:USER:GetSdmcCid",
0x081A0042: "fs:USER:GetNandCid",
0x081B0000: "fs:USER:GetSdmcSpeedInfo",
0x081C0000: "fs:USER:GetNandSpeedInfo",
0x081D0042: "fs:USER:GetSdmcLog",
0x081E0042: "fs:USER:GetNandLog",
0x081F0000: "fs:USER:ClearSdmcLog",
0x08200000: "fs:USER:ClearNandLog",
0x08210000: "fs:USER:CardSlotIsInserted",
0x08220000: "fs:USER:CardSlotPowerOn",
0x08230000: "fs:USER:CardSlotPowerOff",
0x08240000: "fs:USER:CardSlotGetCardIFPowerStatus",
0x08250040: "fs:USER:CardNorDirectCommand",
0x08260080: "fs:USER:CardNorDirectCommandWithAddress",
0x08270082: "fs:USER:CardNorDirectRead",
0x082800C2: "fs:USER:CardNorDirectReadWithAddress",
0x08290082: "fs:USER:CardNorDirectWrite",
0x082A00C2: "fs:USER:CardNorDirectWriteWithAddress",
0x082B00C2: "fs:USER:CardNorDirectRead_4xIO",
0x082C0082: "fs:USER:CardNorDirectCpuWriteWithoutVerify",
0x082D0040: "fs:USER:CardNorDirectSectorEraseWithoutVerify",
0x082E0040: "fs:USER:GetProductInfo",
0x082F0040: "fs:USER:GetProgramLaunchInfo",
0x08300182: "fs:USER:Obsoleted_3_0_CreateExtSaveData",
0x08310180: "fs:USER:Obsoleted_3_0_CreateSharedExtSaveData",
0x08320102: "fs:USER:Obsoleted_3_0_ReadExtSaveDataIcon",
0x08330082: "fs:USER:Obsoleted_3_0_EnumerateExtSaveData",
0x08340082: "fs:USER:Obsoleted_3_0_EnumerateSharedExtSaveData",
0x08350080: "fs:USER:Obsoleted_3_0_DeleteExtSaveData",
0x08360080: "fs:USER:Obsoleted_3_0_DeleteSharedExtSaveData",
0x08370040: "fs:USER:SetCardSpiBaudRate",
0x08380040: "fs:USER:SetCardSpiBusMode",
0x08390000: "fs:USER:SendInitializeInfoTo9",
0x083A0100: "fs:USER:GetSpecialContentIndex",
0x083B00C2: "fs:USER:GetLegacyRomHeader",
0x083C00C2: "fs:USER:GetLegacyBannerData",
0x083D0100: "fs:USER:CheckAuthorityToAccessExtSaveData",
0x083E00C2: "fs:USER:QueryTotalQuotaSize",
0x083F00C0: "fs:USER:Obsoleted_3_0_GetExtDataBlockSize",
0x08400040: "fs:USER:AbnegateAccessRight",
0x08410000: "fs:USER:DeleteSdmcRoot",
0x08420040: "fs:USER:DeleteAllExtSaveDataOnNand",
0x08430000: "fs:USER:InitializeCtrFileSystem",
0x08440000: "fs:USER:CreateSeed",
0x084500C2: "fs:USER:GetFormatInfo",
0x08460102: "fs:USER:GetLegacyRomHeader2",
0x08470180: "fs:USER:Obsoleted_2_0_FormatCtrCardUserSaveData",
0x08480042: "fs:USER:GetSdmcCtrRootPath",
0x08490040: "fs:USER:GetArchiveResource",
0x084A0002: "fs:USER:ExportIntegrityVerificationSeed",
0x084B0002: "fs:USER:ImportIntegrityVerificationSeed",
0x084C0242: "fs:USER:FormatSaveData",
0x084D0102: "fs:USER:GetLegacySubBannerData",
0x084E0342: "fs:USER:UpdateSha256Context",
0x084F0102: "fs:USER:ReadSpecialFile",
0x08500040: "fs:USER:GetSpecialFileSize",
0x00010244: "PS:SignRsaSha256",
0x00020244: "PS:VerifyRsaSha256",
0x00040204: "PS:EncryptDecryptAes",
0x00050284: "PS:EncryptSignDecryptVerifyAesCcm",
0x00060040: "PS:GetRomId",
0x00070040: "PS:GetRomId2",
0x00080040: "PS:GetRomMakerCode",
0x00090000: "PS:GetCTRCardAutoStartupBit",
0x000A0000: "PS:GetLocalFriendCodeSeed",
0x000B0000: "PS:GetDeviceId",
0x000C0000: "PS:GatherEntropy",
0x000D0042: "PS:GenerateRandomBytes",
# you can implement the rest (y)
}
def sanitizeString(s):
ret = s
ret = re.sub( r'%[\+ -#0]*[\d\.]*[lhLzjt]{0,2}[diufFeEgGxXoscpaAn]', '_', ret )
ret = re.sub( r'[^a-zA-Z0-9_]+', '_', ret )
ret = re.sub( r'_+', '_', ret )
return ret.strip('_')
def main():
ea = SegByName(".text")
for funcea in Functions(ea, SegEnd(ea)):
E = list(FuncItems(funcea))
for e in E:
if (GetMnem(e) == "SVC"):
opval = idc.GetOperandValue(e, 0)
if opval in svcCalls:
print "%X"%e, "SVC", opval, "found in", GetFunctionName(funcea), svcCalls[opval]
MakeComm(e, svcCalls[opval])
else:
print "%X"%e, "UNKNOWN SVC", opval, "found in", GetFunctionName(funcea)
MakeComm(e, "Unknown Supervisor Call")
if (GetMnem(e) == "MRC" and idc.GetOpnd(e, 2) == "3" and GetMnem(e+4) == "LDR"):
# Thread Local Storage
MakeComm(e, "ThreadLocalStorage");
opval = idc.Dword(idc.GetOperandValue(e+4, 1));
if opval in ipccalls:
print "%X"%e, "IPC", ipccalls[opval], "found in", GetFunctionName(funcea);
newName = 'z_%s' % sanitizeString(ipccalls[opval])
MakeNameEx( funcea , newName, SN_NOWARN)
else:
print "%X"%e, "UNKNOWN IPC", "%X"%opval, "found in", GetFunctionName(funcea);
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment