diff --git a/nx/include/switch/services/pl.h b/nx/include/switch/services/pl.h index 62547245..fa016dba 100644 --- a/nx/include/switch/services/pl.h +++ b/nx/include/switch/services/pl.h @@ -29,6 +29,9 @@ Result plInitialize(void); void plExit(void); void* plGetSharedmemAddr(void); +///< Gets a specific shared-font via SharedFontType, see \ref PlSharedFontType. +Result plGetSharedFontByType(PlFontData* font, u32 SharedFontType); + ///< Gets shared font(s). Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, size_t* total_fonts); diff --git a/nx/source/services/pl.c b/nx/source/services/pl.c index ca83d03e..6592af95 100644 --- a/nx/source/services/pl.c +++ b/nx/source/services/pl.c @@ -57,6 +57,147 @@ void* plGetSharedmemAddr(void) { return shmemGetAddr(&g_plSharedmem); } +static Result _plRequestLoad(u32 SharedFontType) { + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + u32 SharedFontType; + } *raw; + + raw = ipcPrepareHeader(&c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 0; + raw->SharedFontType = SharedFontType; + + Result rc = serviceIpcDispatch(&g_plSrv); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + ipcParse(&r); + + struct { + u64 magic; + u64 result; + } *resp = r.Raw; + + rc = resp->result; + } + + return rc; +} + +static Result _plGetLoadState(u32 SharedFontType, u32* LoadState) { + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + u32 SharedFontType; + } *raw; + + raw = ipcPrepareHeader(&c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 1; + raw->SharedFontType = SharedFontType; + + Result rc = serviceIpcDispatch(&g_plSrv); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + ipcParse(&r); + + struct { + u64 magic; + u64 result; + u32 LoadState; + } *resp = r.Raw; + + rc = resp->result; + + if (R_SUCCEEDED(rc) && LoadState) *LoadState = resp->LoadState; + } + + return rc; +} + +static Result _plGetSize(u32 SharedFontType, u32* size) { + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + u32 SharedFontType; + } *raw; + + raw = ipcPrepareHeader(&c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 2; + raw->SharedFontType = SharedFontType; + + Result rc = serviceIpcDispatch(&g_plSrv); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + ipcParse(&r); + + struct { + u64 magic; + u64 result; + u32 size; + } *resp = r.Raw; + + rc = resp->result; + + if (R_SUCCEEDED(rc) && size) *size = resp->size; + } + + return rc; +} + +static Result _plGetSharedMemoryAddressOffset(u32 SharedFontType, u32* offset) { + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + u32 SharedFontType; + } *raw; + + raw = ipcPrepareHeader(&c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 3; + raw->SharedFontType = SharedFontType; + + Result rc = serviceIpcDispatch(&g_plSrv); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + ipcParse(&r); + + struct { + u64 magic; + u64 result; + u32 offset; + } *resp = r.Raw; + + rc = resp->result; + + if (R_SUCCEEDED(rc) && offset) *offset = resp->offset; + } + + return rc; +} + static Result _plGetSharedMemoryNativeHandle(Handle* handle_out) { IpcCommand c; ipcInitialize(&c); @@ -92,7 +233,60 @@ static Result _plGetSharedMemoryNativeHandle(Handle* handle_out) { return rc; } +static Result _plVerifyFontRange(u32 offset, u32 size) { + if (offset >= SHAREDMEMFONT_SIZE || size > SHAREDMEMFONT_SIZE || ((u64)offset + (u64)size) > SHAREDMEMFONT_SIZE) + return MAKERESULT(Module_Libnx, LibnxError_BadInput); + + return 0; +} + +static Result _plRequestLoadWait(u32 SharedFontType) { + Result rc=0; + u32 LoadState=0; + bool load_started = 0; + + while(1) {//Wait/request for the font to be loaded if it's not already loaded. + if (load_started) svcSleepThread(1000000000ULL / 60); + + rc = _plGetLoadState(SharedFontType, &LoadState); + if (R_FAILED(rc) || LoadState==0x1) break; + + if (load_started) continue; + load_started = 1; + rc = _plRequestLoad(SharedFontType); + if (R_FAILED(rc)) break; + } + + return rc; +} + +Result plGetSharedFontByType(PlFontData* font, u32 SharedFontType) { + Result rc=0; + u8* sharedmem_addr = (u8*)plGetSharedmemAddr(); + + memset(font, 0, sizeof(PlFontData)); + + font->type = SharedFontType; + + rc = _plRequestLoadWait(SharedFontType); + if (R_FAILED(rc)) return rc; + + rc = _plGetSize(SharedFontType, &font->size); + if (R_FAILED(rc)) return rc; + + rc = _plGetSharedMemoryAddressOffset(SharedFontType, &font->offset); + if (R_FAILED(rc)) return rc; + + rc = _plVerifyFontRange(font->offset, font->size); + if (R_FAILED(rc)) return rc; + + font->address = &sharedmem_addr[font->offset]; + + return rc; +} + Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, size_t* total_fonts) { + Result rc=0; u32 types[PlSharedFontType_Total]; u32 offsets[PlSharedFontType_Total]; u32 sizes[PlSharedFontType_Total]; @@ -100,12 +294,6 @@ Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, si u32 font_count=0, i; u8* sharedmem_addr = (u8*)plGetSharedmemAddr(); - IpcCommand c; - ipcInitialize(&c); - ipcAddRecvBuffer(&c, types, size, 0); - ipcAddRecvBuffer(&c, offsets, size, 0); - ipcAddRecvBuffer(&c, sizes, size, 0); - memset(types, 0, sizeof(types)); memset(offsets, 0, sizeof(offsets)); memset(sizes, 0, sizeof(sizes)); @@ -113,6 +301,17 @@ Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, si if (total_fonts) *total_fonts = 0; + for (i=0; icmd_id = 5; raw->LanguageCode = LanguageCode; - Result rc = serviceIpcDispatch(&g_plSrv); + rc = serviceIpcDispatch(&g_plSrv); if (R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -150,8 +349,8 @@ Result plGetSharedFont(u64 LanguageCode, PlFontData* fonts, size_t max_fonts, si if (font_count==0) return rc; for (i=0; i= SHAREDMEMFONT_SIZE || sizes[i] > SHAREDMEMFONT_SIZE || ((u64)offsets[i] + (u64)sizes[i]) > SHAREDMEMFONT_SIZE) - return MAKERESULT(Module_Libnx, LibnxError_BadInput); + rc = _plVerifyFontRange(offsets[i], sizes[i]); + if (R_FAILED(rc)) return rc; fonts[i].type = types[i]; fonts[i].offset = offsets[i];