Skip to content

Commit b18f04d

Browse files
committed
apt: add deliver arg support to chainloader + minor fixes
1 parent 98324d4 commit b18f04d

File tree

2 files changed

+45
-6
lines changed
  • libctru

2 files changed

+45
-6
lines changed

libctru/include/3ds/services/apt.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,22 @@ void aptClearChainloader(void);
237237
*/
238238
void aptSetChainloader(u64 programID, u8 mediatype);
239239

240+
/// Configures the chainloader to launch the previous application.
241+
void aptSetChainloaderToCaller(void);
242+
240243
/// Configures the chainloader to relaunch the current application (i.e. soft-reset)
241244
void aptSetChainloaderToSelf(void);
242245

246+
/**
247+
* @brief Sets the "deliver arg" and HMAC for the chainloader, which will
248+
* be passed to the target 3DS/DS(i) application. The meaning of each
249+
* parameter varies on a per-application basis.
250+
* @param deliverArg Deliver arg to pass to the target application.
251+
* @param deliverArgSize Size of the deliver arg, maximum 0x300 bytes.
252+
* @param hmac HMAC buffer, 32 bytes. Use NULL to pass an all-zero dummy HMAC.
253+
*/
254+
void aptSetChainloaderArgs(const void *deliverArg, size_t deliverArgSize, const void *hmac);
255+
243256
/**
244257
* @brief Gets an APT lock handle.
245258
* @param flags Flags to use.
@@ -565,4 +578,4 @@ Result APT_GetSharedFont(Handle* fontHandle, u32* mapAddr);
565578
* @param sender Pointer to output the sender's AppID to.
566579
* @param received Pointer to output whether an argument was received to.
567580
*/
568-
Result APT_ReceiveDeliverArg(const void* param, size_t paramSize, const void* hmac, u64* sender, bool* received);
581+
Result APT_ReceiveDeliverArg(void* param, size_t paramSize, void* hmac, u64* sender, bool* received);

libctru/source/services/apt.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ enum
6363
static u8 aptHomeButtonState;
6464
static u32 aptFlags;
6565
static u32 aptParameters[0x1000/4];
66-
static u8 aptChainloadFlags;
6766
static u64 aptChainloadTid;
67+
static u8 aptChainloadDeliverArg[0x300];
68+
static u32 aptChainloadDeliverArgSize = sizeof(aptChainloadDeliverArg);
69+
static u8 aptChainloadHmac[0x20];
6870
static u8 aptChainloadMediatype;
71+
static u8 aptChainloadFlags;
6972

7073
typedef enum
7174
{
@@ -315,6 +318,9 @@ static void aptClearJumpToHome(void)
315318
void aptClearChainloader(void)
316319
{
317320
aptFlags &= ~FLAG_CHAINLOAD;
321+
aptChainloadDeliverArgSize = sizeof(aptChainloadDeliverArg);
322+
memset(aptChainloadDeliverArg, 0, sizeof(aptChainloadDeliverArg));
323+
memset(aptChainloadHmac, 0, sizeof(aptChainloadHmac));
318324
}
319325

320326
void aptSetChainloader(u64 programID, u8 mediatype)
@@ -325,6 +331,14 @@ void aptSetChainloader(u64 programID, u8 mediatype)
325331
aptChainloadMediatype = mediatype;
326332
}
327333

334+
void aptSetChainloaderToCaller(void)
335+
{
336+
aptFlags |= FLAG_CHAINLOAD;
337+
aptChainloadFlags = 1;
338+
aptChainloadTid = 0;
339+
aptChainloadMediatype = 0;
340+
}
341+
328342
void aptSetChainloaderToSelf(void)
329343
{
330344
aptFlags |= FLAG_CHAINLOAD;
@@ -333,6 +347,20 @@ void aptSetChainloaderToSelf(void)
333347
aptChainloadMediatype = 0;
334348
}
335349

350+
void aptSetChainloaderArgs(const void *deliverArg, size_t deliverArgSize, const void *hmac)
351+
{
352+
if (deliverArgSize >= sizeof(aptChainloadDeliverArg))
353+
deliverArgSize = sizeof(aptChainloadDeliverArg);
354+
355+
aptChainloadDeliverArgSize = deliverArgSize;
356+
memcpy(aptChainloadDeliverArg, deliverArg, deliverArgSize);
357+
358+
if (hmac != NULL)
359+
memcpy(aptChainloadHmac, hmac, sizeof(aptChainloadHmac));
360+
else
361+
memset(aptChainloadHmac, 0, sizeof(aptChainloadHmac));
362+
}
363+
336364
extern void (*__system_retAddr)(void);
337365

338366
static void aptExitProcess(void)
@@ -370,10 +398,8 @@ void aptExit(void)
370398
if (R_SUCCEEDED(APT_IsRegistered(aptGetMenuAppID(), &hmRegistered)) && hmRegistered)
371399
{
372400
// Normal, sane chainload
373-
u8 param[0x300] = {0};
374-
u8 hmac[0x20] = {0};
375401
APT_PrepareToDoApplicationJump(aptChainloadFlags, aptChainloadTid, aptChainloadMediatype);
376-
APT_DoApplicationJump(param, sizeof(param), hmac);
402+
APT_DoApplicationJump(aptChainloadDeliverArg, aptChainloadDeliverArgSize, aptChainloadHmac);
377403
}
378404
else
379405
{
@@ -1430,7 +1456,7 @@ Result APT_GetSharedFont(Handle* fontHandle, u32* mapAddr)
14301456
return ret;
14311457
}
14321458

1433-
Result APT_ReceiveDeliverArg(const void* param, size_t paramSize, const void* hmac, u64* sender, bool* received)
1459+
Result APT_ReceiveDeliverArg(void* param, size_t paramSize, void* hmac, u64* sender, bool* received)
14341460
{
14351461
u32 cmdbuf[16];
14361462
cmdbuf[0]=IPC_MakeHeader(0x35,2,0); // 0x350080

0 commit comments

Comments
 (0)