IPC (SDK)

From WiiBrew
Jump to navigation Jump to search

IPC is the SDK library that provides functions for communication with IOS. It seems to also mirror some IOS syscalls, suggesting that IOS was originally planned to run on the Broadway.

Public functions

Signature Description
int IPCCltInit(void) Initializes the library. Returns an error code.
int IOS_OpenAsync(char *path, int mode, void (*callback)(int, void *), void *userData) Calls IOS_Open, calling callback when a response is received. May return an error code if something fails before sending the request. Unlike the IOS syscall, this does not block, although because the IPC thread in IOS blocks, no other requests will go through until the device is opened.
int IOS_Open(char *path, int mode) Calls IOS_Open and blocks until a response is received. Returns an error code, or an FD for the opened file.
int IOS_CloseAsync(int fd, void (*callback)(int, void *), void *userData) Calls IOS_Close, calling callback when a response is received. May return an error code if something fails before sending the request.
int IOS_Close(int fd) Calls IOS_Close and blocks until a response is received. Returns an error code.
int IOS_ReadAsync(int fd, char *data, int len, void (*callback)(int, void *), void *userData) Calls IOS_Read, calling callback when a response is received. May return an error code if something fails before sending the request.
int IOS_Read(int fd, char *data, int len) Calls IOS_Read and blocks until a response is received. Returns an error code.
int IOS_WriteAsync(int fd, char *data, int len, void (*callback)(int, void *), void *userData) Calls IOS_Write, calling callback when a response is received. May return an error code if something fails before sending the request.
int IOS_Write(int fd, char *data, int len) Calls IOS_Write and blocks until a response is received. Returns an error code.
int IOS_SeekAsync(int fd, int offset, SeekMode mode, void (*callback)(int, void *), void *userData) Calls IOS_Seek, calling callback when a response is received. May return an error code if something fails before sending the request.
int IOS_IoctlAsync(int fd, int request, char *in, int inSize, char *out, int outSize, void (*callback)(int, void *), void *userData) Calls IOS_Ioctl, calling callback when a response is received. May return an error code if something fails before sending the request.
int IOS_Ioctl(int fd, int request, char *in, int inSize, char *out, int outSize) Calls IOS_Ioctl and blocks until a response is received. Returns an error code.
int IOS_IoctlvAsync(int fd, int request, int inCount, int ioCount, struct IOVector *vectors, void (*callback)(int, void *), void *userData) Calls IOS_Ioctlv, calling callback when a response is received. May return an error code if something fails before sending the request.
int IOS_Ioctlv(int fd, int request, int inCount, int ioCount, struct IOVector *vectors) Calls IOS_Ioctlv and blocks until a response is received. Returns an error code.
int IOS_IoctlvReboot(int fd, int request, int inCount, int ioCount, struct IOVector *vectors) Calls IOS_Ioctlv, but expects 2 acknowledgements instead of 1. Exclusively used by ES_LaunchTitle, where an acknowledgement is received from both the old IOS and the new IOS.
int iosCreateHeap(void *ptr, int size) Creates a heap and returns the heapId.
void *iosAllocAligned(int heapId, int size, int align) Allocates size bytes from heap heapId, plus however many are needed for the base pointer to be aligned to align bytes. align must be a power of 2. Returns the base pointer.
int iosFree(int heapId, void *ptr) Frees ptr back into heap heapId. Returns an error code.

Error codes

In addition to these error codes, any error returned by IOS can be returned from a blocking function.

Number Description
0 No error. This is returned from async functions if the request was successfully sent.
-4 General memory error. Returned when there is not enough stack space, or when improperly freeing memory.
-22 Not enough memory in the heap

All of the IPC functions include code to return error -4 if the heap does not have enough memory, but the error -22 code takes priority over it; this probably means error -22 was added later.

Message wrapper

IPC wraps IOS messages around a wrapper to store user data.

struct IOSRequestWrapper {
	struct IOSRequest request;
	void (*callback)(int, void *); // NULL for synchronous requests
	void *userData; // NULL for synchronous requests
	u32 relaunch; // always 0 or 1
	struct OSThreadQueue waitingQueue;
	u32 padding[3]; // pad to 0x40 so this struct can be 32-aligned
}

Memory allocation

Memory used by IPC and NAND is mainly allocated with iosAllocAligned, which allocates from one of 8 heaps, each with the following structure:

struct HeapBlockHeader {
	u16 magic;                    // always 0xbabe
	u16 status;                   // 0 means free, 1 means it is allocated, 2 means it is an aligned alias for another block stored in prev.
	u32 size;
	struct HeapBlockHeader *prev; // NULL for the beginning of the heap or for status 1, points to the main block for status 2.
	struct HeapBlockHeader *next; // NULL for the end of the heap or if this heap block is allocated
}

struct Heap {
	void *base;
	u32 owner; // See note below
	u32 size;
	struct HeapBlockHeader *freeList;
}

processId is probably only present to match the struct used by IOS; it is never set or checked by IPC, but it may be used by IOS's memory management.