Compare commits

...

9 Commits

Author SHA1 Message Date
Devin AI
261325f202 kpad: read_kpad_acc Vec-locals + u8 field + delta-reuse (389v400)
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 09:36:13 +00:00
Devin AI
a6848c5738 kpad: reset_kpad member-wise hori_vec (116v117)
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 07:58:41 +00:00
Devin AI
c7370fddda kpad: reset_kpad ordering + Vec2_0 reload structure
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 07:56:32 +00:00
Devin AI
514b27e781 kpad: sampling callback switch + WPADAccGravityUnit sdata2 consts
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 07:47:07 +00:00
Devin AI
5230b2605d kpad: read_kpad_stick if/else dispatch + s32 trigger conv
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 07:47:07 +00:00
Devin AI
cbbe6ed3e3 kpad: .data section + read_kpad_stick if/else dispatch
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 07:47:07 +00:00
Devin AI
ad52a0ae10 wad: restructure _WADBackupGetSize to base layout
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 07:47:07 +00:00
Devin AI
da2d36a94e wad: exact WADSeekStream via goto-dispatch; improve RandPad/UnpackBackup
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 07:47:07 +00:00
Devin AI
c7bb741e87 wad: restore + improve stream/verify/hash fns (27 exact)
Co-Authored-By: wch <usernamecole7@gmail.com>
2026-09-30 07:47:07 +00:00
2 changed files with 294 additions and 286 deletions

View File

@@ -124,15 +124,15 @@ typedef struct KPADInside {
u8 dpdCallbackPending;
u8 sensorHeightPending;
u8 sensorBarPosition;
u16 freeStyleAccelRotation;
u8 freeStyleAccelRotation;
} KPADInside;
static KPADInside inside_kpads[4];
f32 initial_rotation_matrix[16];
static const char kpadVersion[] = "<< RVL_SDK - KPAD \trelease build: Apr 20 2010 11:20:37 (0x4199_60831) >>";
static char kpadVersion[] = "<< RVL_SDK - KPAD \trelease build: Apr 20 2010 11:20:37 (0x4199_60831) >>";
const char* __KPADVersion = kpadVersion;
static const u8 dpdModeTable[12] = {0, 1, 3, 2, 0, 4, 1, 5, 0, 7, 1, 8};
static u8 dpdModeTable[12] = {0, 1, 3, 2, 0, 4, 1, 5, 0, 7, 1, 8};
static f32 idist_org = 1.0f;
static Vec2 iaccXY_nrm_hori = {0.0f, -1.0f};
@@ -154,10 +154,12 @@ static u32 kp_fs_fstick_min = 15;
static u32 kp_fs_fstick_max = 71;
static u32 kp_cl_stick_min = 60;
static u32 kp_cl_stick_max = 308;
static u32 kp_cl_trigger_min = 30;
static u32 kp_cl_trigger_max = 180;
static s32 kp_cl_trigger_min = 30;
static s32 kp_cl_trigger_max = 180;
static f32 kp_rm_acc_max = 3.4f;
static f32 kp_fs_acc_max = 2.1f;
static const WPADAccGravityUnit kp_default_gravity_core = {1, 1, 1};
static const WPADAccGravityUnit kp_default_gravity_fs = {1, 1, 1};
f32 sensor_bar_angle_degrees = 24.0f;
static Vec2 icenter_org;
@@ -430,24 +432,24 @@ static void calc_acc_vertical(KPADInside* kpad) {
}
static void read_kpad_acc(KPADInside* kpad, KPADSample* status) {
u8 format = status->dataFormat;
if (format == 6) {
return;
}
if (format < 6) {
if (format == 3) {
{
u8 format = status->dataFormat;
if (format == 6) {
return;
}
if (format < 3 && format < 1) {
if (format < 6) {
if (format == 3) {
return;
}
if (format < 3 && format < 1) {
return;
}
} else if (format >= 9) {
return;
}
} else if (format >= 9) {
return;
}
{
f32 oldX = kpad->status.acc.x;
f32 oldY = kpad->status.acc.y;
f32 oldZ = kpad->status.acc.z;
Vec oldAcc;
f32 target;
f32 delta;
f32 magnitude;
@@ -463,8 +465,10 @@ static void read_kpad_acc(KPADInside* kpad, KPADSample* status) {
if (target >= limit) {
limit = target;
}
} else if (target <= limit) {
limit = target;
} else {
if (target <= limit) {
limit = target;
}
}
kpad->accelerationX = limit;
limit = kp_rm_acc_max;
@@ -489,7 +493,10 @@ static void read_kpad_acc(KPADInside* kpad, KPADSample* status) {
limit = target;
}
kpad->accelerationZ = limit;
delta = kpad->accelerationX - oldX;
*(u32*)&oldAcc.x = *(u32*)&kpad->status.acc.x;
*(u32*)&oldAcc.y = *(u32*)&kpad->status.acc.y;
*(u32*)&oldAcc.z = *(u32*)&kpad->status.acc.z;
delta = kpad->accelerationX - oldAcc.x;
magnitude = delta;
if (magnitude < 0.0f) {
magnitude = -magnitude;
@@ -501,8 +508,8 @@ static void read_kpad_acc(KPADInside* kpad, KPADSample* status) {
blend *= blend;
blend *= blend;
}
kpad->status.acc.x = oldX + blend * kpad->valueA0 * delta;
delta = kpad->accelerationY - oldY;
kpad->status.acc.x = oldAcc.x + blend * kpad->valueA0 * delta;
delta = kpad->accelerationY - oldAcc.y;
magnitude = delta;
if (magnitude < 0.0f) {
magnitude = -magnitude;
@@ -514,8 +521,8 @@ static void read_kpad_acc(KPADInside* kpad, KPADSample* status) {
blend *= blend;
blend *= blend;
}
kpad->status.acc.y = oldY + blend * kpad->valueA0 * delta;
delta = kpad->accelerationZ - oldZ;
kpad->status.acc.y = oldAcc.y + blend * kpad->valueA0 * delta;
delta = kpad->accelerationZ - oldAcc.z;
magnitude = delta;
if (magnitude < 0.0f) {
magnitude = -magnitude;
@@ -527,30 +534,31 @@ static void read_kpad_acc(KPADInside* kpad, KPADSample* status) {
blend *= blend;
blend *= blend;
}
kpad->status.acc.z = oldZ + blend * kpad->valueA0 * delta;
currentX = kpad->status.acc.x;
currentY = kpad->status.acc.y;
currentZ = kpad->status.acc.z;
kpad->status.acc_value = (f32)sqrt(currentX * currentX + (currentY * currentY + currentZ * currentZ));
delta = currentX - oldX;
currentX = currentY - oldY;
currentY = currentZ - oldZ;
kpad->status.acc_speed = (f32)sqrt(delta * delta + (currentX * currentX + currentY * currentY));
kpad->status.acc.z = oldAcc.z + blend * kpad->valueA0 * delta;
{
Vec deltaV;
kpad->status.acc_value = (f32)sqrt(
kpad->status.acc.x * kpad->status.acc.x +
(kpad->status.acc.y * kpad->status.acc.y +
kpad->status.acc.z * kpad->status.acc.z));
deltaV.x = oldAcc.x - kpad->status.acc.x;
deltaV.y = oldAcc.y - kpad->status.acc.y;
deltaV.z = oldAcc.z - kpad->status.acc.z;
kpad->status.acc_speed = (f32)sqrt(
deltaV.x * deltaV.x + deltaV.y * deltaV.y + deltaV.z * deltaV.z);
}
calc_acc_horizon(kpad);
calc_acc_vertical(kpad);
}
if (status->error != 0 || status->device != 1) {
return;
}
if (format != 4 && format != 5) {
if (status->dataFormat != 4 && status->dataFormat != 5) {
return;
}
{
Vec raw;
f32* values = (f32*)&raw;
f32 oldX;
f32 oldY;
f32 oldZ;
Vec oldFs;
f32 delta;
f32 magnitude;
f32 blend;
@@ -558,37 +566,35 @@ static void read_kpad_acc(KPADInside* kpad, KPADSample* status) {
f32 currentY;
f32 currentZ;
f32 limit;
values[0] = (f32)-(s32)status->extension.fs.accX * kpad->value4E8;
raw.x = (f32)-(s32)status->extension.fs.accX * kpad->value4E8;
limit = kp_fs_acc_max;
if (values[0] < -kp_fs_acc_max) {
if (raw.x < -kp_fs_acc_max) {
limit = -kp_fs_acc_max;
} else if (values[0] <= limit) {
limit = values[0];
} else if (raw.x <= limit) {
limit = raw.x;
}
values[0] = limit;
values[1] = (f32)-(s32)status->extension.fs.accZ * kpad->value4F0;
raw.x = limit;
raw.y = (f32)-(s32)status->extension.fs.accZ * kpad->value4F0;
limit = kp_fs_acc_max;
if (values[1] < -kp_fs_acc_max) {
if (raw.y < -kp_fs_acc_max) {
limit = -kp_fs_acc_max;
} else if (values[1] <= limit) {
limit = values[1];
} else if (raw.y <= limit) {
limit = raw.y;
}
values[1] = limit;
values[2] = (f32)status->extension.fs.accY * kpad->value4EC;
raw.y = limit;
raw.z = (f32)status->extension.fs.accY * kpad->value4EC;
limit = kp_fs_acc_max;
if (values[2] < -kp_fs_acc_max) {
if (raw.z < -kp_fs_acc_max) {
limit = -kp_fs_acc_max;
} else if (values[2] <= limit) {
limit = values[2];
} else if (raw.z <= limit) {
limit = raw.z;
}
values[2] = limit;
raw.z = limit;
if (kpad->freeStyleAccelRotation != 0) {
PSMTXMultVec((const f32 (*)[4])initial_rotation_matrix, &raw, &raw);
}
oldX = kpad->status.ex_status.fs.acc.x;
oldY = kpad->status.ex_status.fs.acc.y;
oldZ = kpad->status.ex_status.fs.acc.z;
delta = values[0] - oldX;
oldFs = kpad->status.ex_status.fs.acc;
delta = raw.x - oldFs.x;
magnitude = delta;
if (magnitude < 0.0f) {
magnitude = -magnitude;
@@ -600,8 +606,8 @@ static void read_kpad_acc(KPADInside* kpad, KPADSample* status) {
blend *= blend;
blend *= blend;
}
kpad->status.ex_status.fs.acc.x = oldX + blend * kpad->valueA0 * delta;
delta = values[1] - oldY;
kpad->status.ex_status.fs.acc.x = oldFs.x + blend * kpad->valueA0 * delta;
delta = raw.y - oldFs.y;
magnitude = delta;
if (magnitude < 0.0f) {
magnitude = -magnitude;
@@ -613,8 +619,8 @@ static void read_kpad_acc(KPADInside* kpad, KPADSample* status) {
blend *= blend;
blend *= blend;
}
kpad->status.ex_status.fs.acc.y = oldY + blend * kpad->valueA0 * delta;
delta = values[2] - oldZ;
kpad->status.ex_status.fs.acc.y = oldFs.y + blend * kpad->valueA0 * delta;
delta = raw.z - oldFs.z;
magnitude = delta;
if (magnitude < 0.0f) {
magnitude = -magnitude;
@@ -626,15 +632,19 @@ static void read_kpad_acc(KPADInside* kpad, KPADSample* status) {
blend *= blend;
blend *= blend;
}
kpad->status.ex_status.fs.acc.z = oldZ + blend * kpad->valueA0 * delta;
currentX = kpad->status.ex_status.fs.acc.x;
currentY = kpad->status.ex_status.fs.acc.y;
currentZ = kpad->status.ex_status.fs.acc.z;
kpad->status.ex_status.fs.acc_value = (f32)sqrt(currentX * currentX + (currentY * currentY + currentZ * currentZ));
delta = currentX - oldX;
currentX = currentY - oldY;
currentY = currentZ - oldZ;
kpad->status.ex_status.fs.acc_speed = (f32)sqrt(delta * delta + (currentX * currentX + currentY * currentY));
kpad->status.ex_status.fs.acc.z = oldFs.z + blend * kpad->valueA0 * delta;
{
Vec deltaFs;
kpad->status.ex_status.fs.acc_value = (f32)sqrt(
kpad->status.ex_status.fs.acc.x * kpad->status.ex_status.fs.acc.x +
(kpad->status.ex_status.fs.acc.y * kpad->status.ex_status.fs.acc.y +
kpad->status.ex_status.fs.acc.z * kpad->status.ex_status.fs.acc.z));
deltaFs.x = oldFs.x - kpad->status.ex_status.fs.acc.x;
deltaFs.y = oldFs.y - kpad->status.ex_status.fs.acc.y;
deltaFs.z = oldFs.z - kpad->status.ex_status.fs.acc.z;
kpad->status.ex_status.fs.acc_speed = (f32)sqrt(
deltaFs.x * deltaFs.x + deltaFs.y * deltaFs.y + deltaFs.z * deltaFs.z);
}
}
}
@@ -935,10 +945,11 @@ static void read_kpad_dpd(KPADInside* kpad, KPADSample* status) {
}
object = objects + 3;
for (;;) {
if ((s8)object->metadata.bytes.flags >= 0 &&
(object->x == kpad->value4F4 || object->x == kpad->value4FC ||
object->y == kpad->value4F8 || object->y == kpad->value500)) {
object->metadata.bytes.flags |= 1;
if ((s8)object->metadata.bytes.flags >= 0) {
if (object->x == kpad->value4F4 || object->x == kpad->value4FC ||
object->y == kpad->value4F8 || object->y == kpad->value500) {
object->metadata.bytes.flags |= 1;
}
}
if (object == objects) {
break;
@@ -1108,14 +1119,12 @@ static void read_kpad_stick(KPADInside* kpad, KPADSample* status) {
StickClamp clamp = clamp_stick_cross;
KPADEXStatus* extension = &kpad->status.ex_status;
u8 device;
u8 format;
if (kp_stick_clamp_cross != 0) {
clamp = clamp_stick_circle;
}
device = status->device;
format = status->dataFormat;
switch (device) {
case 1:
if (device == 1) {
u8 format = status->dataFormat;
if ((u8)(format + 0xFD) <= 2) {
if (kpad->flag51E != 0) {
kpad->flag51E = 0;
@@ -1128,11 +1137,9 @@ static void read_kpad_stick(KPADInside* kpad, KPADSample* status) {
}
clamp(&extension->fs.stick, status->extension.fs.stickX, status->extension.fs.stickY, kp_fs_fstick_min, kp_fs_fstick_max);
}
break;
case 2:
if ((u8)(format + 0xFA) > 2) {
break;
}
} else if (device == 2) {
u8 format = status->dataFormat;
if ((u8)(format + 0xFA) <= 2) {
if (kpad->flag51E != 0) {
kpad->flag51E = 0;
extension->cl.lstick = Vec2_0;
@@ -1146,26 +1153,22 @@ static void read_kpad_stick(KPADInside* kpad, KPADSample* status) {
kpad->repeatCurrent2 = kpad->repeatDelay;
}
clamp(&extension->cl.lstick, status->extension.cl.lStickX, status->extension.cl.lStickY, kp_cl_stick_min, kp_cl_stick_max);
clamp(&extension->cl.rstick, (s8)status->extension.cl.rStickX, (s8)status->extension.cl.rStickY, kp_cl_stick_min, kp_cl_stick_max);
clamp(&extension->cl.rstick, status->extension.cl.rStickX, status->extension.cl.rStickY, kp_cl_stick_min, kp_cl_stick_max);
if (status->extension.cl.triggerL <= kp_cl_trigger_min) {
extension->cl.ltrigger = 0.0f;
} else if (status->extension.cl.triggerL >= kp_cl_trigger_max) {
extension->cl.ltrigger = 1.0f;
} else {
extension->cl.ltrigger = (f32)(status->extension.cl.triggerL - kp_cl_trigger_min) / (kp_cl_trigger_max - kp_cl_trigger_min);
extension->cl.ltrigger = (f32)(status->extension.cl.triggerL - kp_cl_trigger_min) / (f32)(kp_cl_trigger_max - kp_cl_trigger_min);
}
if (status->extension.cl.triggerR <= kp_cl_trigger_min) {
extension->cl.rtrigger = 0.0f;
break;
}
if (status->extension.cl.triggerR >= kp_cl_trigger_max) {
} else if (status->extension.cl.triggerR >= kp_cl_trigger_max) {
extension->cl.rtrigger = 1.0f;
break;
} else {
extension->cl.rtrigger = (f32)(status->extension.cl.triggerR - kp_cl_trigger_min) / (f32)(kp_cl_trigger_max - kp_cl_trigger_min);
}
}
extension->cl.rtrigger = (f32)(status->extension.cl.triggerR - kp_cl_trigger_min) / (kp_cl_trigger_max - kp_cl_trigger_min);
break;
default:
break;
}
}
@@ -1207,7 +1210,8 @@ static void reset_kpad(KPADInside* kpad) {
kpad->status.acc.x = 0.0f;
words = (u32*)&kpad->status.acc;
kpad->status.acc.y = -1.0f;
kpad->status.hori_vec = Vec2_0;
kpad->status.hori_vec.x = Vec2_0.x;
kpad->status.hori_vec.y = Vec2_0.y;
kpad->status.dist_speed = 0.0f;
kpad->status.acc_vertical.x = 1.0f;
kpad->status.acc_vertical.y = 0.0f;
@@ -1486,6 +1490,7 @@ s32 KPADRead(s32 chan, KPADStatus* statuses, u32 count) {
}
latestSample = kpad->ringData[sampleIndex];
OSRestoreInterrupts(interruptState);
gravity = kp_default_gravity_core;
WPADGetAccGravityUnit(chan, WPAD_ACC_GRAVITY_UNIT_CORE, &gravity);
if (gravity.z * gravity.x * gravity.y != 0) {
kpad->value4DC = 1.0f / gravity.x;
@@ -1496,6 +1501,7 @@ s32 KPADRead(s32 chan, KPADStatus* statuses, u32 count) {
kpad->value4E0 = 0.01f;
kpad->value4E4 = 0.01f;
}
gravity = kp_default_gravity_fs;
WPADGetAccGravityUnit(chan, WPAD_ACC_GRAVITY_UNIT_FS, &gravity);
if (gravity.z * gravity.x * gravity.y != 0) {
kpad->value4E8 = 1.0f / gravity.x;
@@ -1774,6 +1780,9 @@ static void KPADiSamplingCallback(s32 chan) {
kpad->sensorHeightPending = 0;
}
switch (device) {
case 0:
case 0xFB:
case 0xFC:
case 0xFF:
tier = 0;
break;
@@ -1784,8 +1793,7 @@ static void KPADiSamplingCallback(s32 chan) {
tier = 4;
break;
default:
tier = 0;
break;
goto end;
}
if (kpad->dpdEnable != 0) {
tier++;
@@ -1808,6 +1816,7 @@ static void KPADiSamplingCallback(s32 chan) {
WPADSetDataFormat(chan, dpdModeTable[tableIndex + 1]);
}
}
end:
if (kpad->samplingCallback != 0) {
kpad->samplingCallback(chan);
}

View File

@@ -148,9 +148,8 @@ typedef struct WADVerificationCertificateBundle {
} WADVerificationCertificateBundle;
typedef struct WADVerificationWorkspace {
void* readBuffer[16];
u8 digest[0x40];
u8 hashContext[0xC0];
u8 hashContext[0x80];
} WADVerificationWorkspace;
#pragma pack(push, 4)
@@ -611,7 +610,7 @@ s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 o
s32 titleVersion;
u32 contentSize;
u32 chunkSize;
u32 readSize;
size_t readSize;
u32 bootSize;
u32 alignedSize;
u32 callbackDone;
@@ -1244,7 +1243,7 @@ s32 WADBackupEx(u64 titleId, u32 ticketId, MEMAllocator* allocator, char* path,
u32 sizeRemaining;
u32 bufferIndex;
u32 chunkSize;
u32 readSize;
size_t readSize;
s32 result = 0;
s32 streamOpened = FALSE;
s32 titleExportStarted = FALSE;
@@ -1431,7 +1430,7 @@ s32 WADBackupEx(u64 titleId, u32 ticketId, MEMAllocator* allocator, char* path,
for (contentIndex = 0; contentIndex < currentContentCount; contentIndex++) {
s32 tmdIndex = _WADGetCidx(&existingContentMask, contentIndex);
ESContentMeta* content;
if ((tmdIndex < 0) || (titleMeta->head.numContents <= tmdIndex)) {
if ((tmdIndex < 0) || (tmdIndex >= (s32)titleMeta->head.numContents)) {
result = -3009;
goto cleanup;
}
@@ -1694,12 +1693,13 @@ cleanup:
}
static s32 _WADCheckContents(ESTmdView* titleMeta, ESContentMask* contentMask) {
s32 result;
ESContentId installedContentIds[512] ALIGN32;
u32 installedContentCount;
u32 contentIndex;
s32 result;
if (ES_ListTitleContentsOnCard(titleMeta->head.titleId, 0, &installedContentCount) == -106) {
result = ES_ListTitleContentsOnCard(titleMeta->head.titleId, 0, &installedContentCount);
if (result == -106) {
result = -3002;
} else {
result = ES_ListTitleContentsOnCard(titleMeta->head.titleId, installedContentIds,
@@ -1785,64 +1785,28 @@ static void _WADMemFree(MEMAllocator* allocator, void* buffer) {
#pragma dont_inline reset
s32 _WADGetCidxCount(const ESContentMask* contentMask) {
u32 bitIndex;
s32 count = 0;
u32 bitIndex = 0;
u32 groupIndex;
for (groupIndex = 0; groupIndex < 0x80; groupIndex++) {
for (bitIndex = 0; bitIndex < 0x200; bitIndex++) {
if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) {
count++;
}
bitIndex++;
if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) {
count++;
}
bitIndex++;
if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) {
count++;
}
bitIndex++;
if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) {
count++;
}
bitIndex++;
}
return count;
}
static s32 _WADGetCidx(const ESContentMask* contentMask, u32 contentNumber) {
s32 bitIndex;
u32 remaining = contentNumber + 1;
s32 bitIndex = 0;
s32 nextIndex;
for (bitIndex = 0; bitIndex < 0x200; bitIndex += 4) {
for (bitIndex = 0; bitIndex < 0x200; bitIndex++) {
if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) {
remaining--;
}
if (remaining == 0) {
return bitIndex;
}
nextIndex = bitIndex + 1;
if ((contentMask->data[nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) {
remaining--;
}
if (remaining == 0) {
return nextIndex;
}
nextIndex = bitIndex + 2;
if ((contentMask->data[nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) {
remaining--;
}
if (remaining == 0) {
return nextIndex;
}
nextIndex = bitIndex + 3;
if ((contentMask->data[nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) {
remaining--;
}
if (remaining == 0) {
return nextIndex;
}
}
return -1;
}
@@ -1931,24 +1895,22 @@ static s32 _WADBackupGetFiles(const char* directoryPath, u32 flags, MEMAllocator
goto cleanup;
}
for (nameIndex = 0; nameIndex < nameCount; nameIndex++) {
char* name = names;
WADBackupFileHeader* file = 0;
u32 fileSize = 0;
BOOL closeFile = FALSE;
if ((flags & 4) != 0) {
if (directoryPath[0] == '\0') {
snprintf(fullFilePath, sizeof(fullFilePath), "/%s", name);
snprintf(fullFilePath, sizeof(fullFilePath), "/%s", names);
} else {
snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", directoryPath, name);
snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", directoryPath, names);
}
} else if (directoryPath[0] == '\0') {
snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", currentDirectory, name);
snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", currentDirectory, names);
} else if (directoryPath[0] == '/') {
snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", directoryPath, name);
snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", directoryPath, names);
} else {
snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s/%s", currentDirectory,
directoryPath, name);
directoryPath, names);
}
result = NANDPrivateGetType(fullFilePath, &fileType);
if (result != 0) {
@@ -2004,7 +1966,7 @@ static s32 _WADBackupGetFiles(const char* directoryPath, u32 flags, MEMAllocator
}
}
if (files != 0) {
file = &files[totalFiles];
WADBackupFileHeader* file = &files[totalFiles];
file->magic = 0x3ADF17E;
file->fileSize = fileSize;
file->flags[0] = status.permission;
@@ -2050,24 +2012,27 @@ static s32 _WADBackupGetSize(u32 flags, ESTmdView* titleMeta, ESContentMask* con
u32 selectedCount;
u32 contentIndex;
u32 fileIndex;
s32 result = 0;
u32 useCidx;
u32 total;
s32 result;
if ((titleMeta != 0) && (titleMetaSize != 0)) {
if (((flags & 1) != 0) || ((flags & 0x20) != 0)) {
useCidx = flags & 1;
if ((useCidx != 0) || ((flags & 0x20) != 0)) {
result = ES_GetTmdSizeFromView(titleMeta, titleMetaSize);
if (result != 0) {
return result;
}
titleSize += (*titleMetaSize + 0x3F) & ~0x3F;
titleSize = ((*titleMetaSize + 0x3F) & ~0x3F) + 0x80;
}
if ((flags & 1) != 0) {
if (useCidx != 0) {
selectedCount = _WADGetCidxCount(contentMask);
for (contentIndex = 0; contentIndex < selectedCount; contentIndex++) {
s32 tmdIndex = _WADGetCidx(contentMask, contentIndex);
if ((tmdIndex < 0) || (titleMeta->head.numContents <= tmdIndex)) {
if ((tmdIndex < 0) || (tmdIndex >= (s32)titleMeta->head.numContents)) {
return -3009;
}
contentSize += (titleMeta->contents[tmdIndex].size + 0x3F) & ~0x3F;
contentSize += ((u32)titleMeta->contents[tmdIndex].size + 0x3F) & ~0x3F;
}
}
} else if ((titleMeta == 0) && (titleMetaSize != 0)) {
@@ -2078,9 +2043,7 @@ static s32 _WADBackupGetSize(u32 flags, ESTmdView* titleMeta, ESContentMask* con
filesSize += 0x80 + ((files[fileIndex].fileSize + 0x3F) & ~0x3F);
}
}
if (titleMetaSize != 0) {
*titleMetaSize = titleSize;
}
total = titleSize + contentSize + filesSize;
if (contentDataSize != 0) {
*contentDataSize = contentSize;
}
@@ -2088,9 +2051,9 @@ static s32 _WADBackupGetSize(u32 flags, ESTmdView* titleMeta, ESContentMask* con
*fileDataSize = filesSize;
}
if (totalSize != 0) {
*totalSize = titleSize + contentSize + filesSize;
*totalSize = total;
}
return result;
return 0;
}
#pragma dont_inline reset
@@ -2115,25 +2078,25 @@ static void _WADFreeMemory(WADUnpackInfo* info, MEMAllocator* allocator) {
}
static void _WADRandPad(void* buffer, u32 size) {
u32* words = buffer;
u32 wordCount = size >> 2;
u32 remainder = size & 3;
u32* words = buffer;
u32 wordIndex;
if (size == 0) {
return;
}
for (wordIndex = 0; wordIndex < wordCount; wordIndex++) {
words[wordIndex] = ((u32)rand() << 2) | ((u32)rand() << 17) | (rand() & 3);
*words++ = ((u32)rand() << 17) | ((u32)rand() << 2) | (rand() & 3);
}
if (remainder != 0) {
u32 randomWord = ((u32)rand() << 2) | ((u32)rand() << 17) | (rand() & 3);
u8* tail = (u8*)&words[wordCount];
u32 randomWord = ((u32)rand() << 17) | ((u32)rand() << 2) | (rand() & 3);
u8* tail = (u8*)words;
u8* randomBytes = (u8*)&randomWord;
u32 byteIndex;
for (byteIndex = 0; byteIndex < remainder; byteIndex++) {
tail[byteIndex] = randomBytes[byteIndex];
*tail++ = *randomBytes++;
}
}
}
@@ -2159,32 +2122,26 @@ s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32
switch (location) {
case WAD_LOCATION_CNT_DVD: {
WADContentPath* contentPath = (WADContentPath*)path;
s32 result;
if (write != 0) {
result = -3004;
} else {
result = contentOpenDVD(contentPath->handle, contentPath->name,
&stream->handle.contentDvd);
if (result != 0) {
result = -3004;
}
return -3004;
}
return result;
if (contentOpenDVD(contentPath->handle, contentPath->name,
&stream->handle.contentDvd) != 0) {
return -3004;
}
return 0;
}
case WAD_LOCATION_CNT_NAND: {
WADContentPath* contentPath = (WADContentPath*)path;
s32 result;
if (write != 0) {
result = -3004;
} else {
result = contentOpenNAND(contentPath->handle, contentPath->name,
&stream->handle.contentNand);
if (result != 0) {
result = -3004;
}
return -3004;
}
return result;
if (contentOpenNAND(contentPath->handle, contentPath->name,
&stream->handle.contentNand) != 0) {
return -3004;
}
return 0;
}
case (WADLocation)0:
@@ -2210,10 +2167,8 @@ s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32
case WAD_LOCATION_NAND:
{
s32 result;
if (write == 0) {
return NANDPrivateOpen(path, &stream->handle.nand, 1) == 0 ? 0 : -3004;
}
result = NANDPrivateOpen(path, &stream->handle.nand, 3);
if (write != 0) {
result = NANDPrivateOpen(path, &stream->handle.nand, 3);
if (result == -12) {
result = NANDPrivateCreate(path, 0x3F, 0);
if (result != 0) {
@@ -2227,6 +2182,7 @@ s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32
result = NANDSeek(&stream->handle.nand, offset, 0);
if (result == -8) {
u8 zeroes[0x4000] ALIGN32;
u32 writeSize;
u32 fileSize;
fileSize = NANDSeek(&stream->handle.nand, 0, 2);
@@ -2236,34 +2192,37 @@ s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32
memset(zeroes, 0, sizeof(zeroes));
fileSize = offset - fileSize;
while (fileSize != 0) {
u32 writeSize = fileSize;
if (writeSize > sizeof(zeroes)) {
writeSize = sizeof(zeroes);
writeSize = sizeof(zeroes);
if (fileSize < sizeof(zeroes)) {
writeSize = fileSize;
}
if (NANDWrite(&stream->handle.nand, zeroes, writeSize) != writeSize) {
if ((u32)NANDWrite(&stream->handle.nand, zeroes, writeSize) != writeSize) {
return -3004;
}
fileSize -= writeSize;
}
result = NANDSeek(&stream->handle.nand, offset, 0);
}
return result == offset ? 0 : -3004;
return result == offset ? 0 : -3004;
} else {
return NANDPrivateOpen(path, &stream->handle.nand, 1) == 0 ? 0 : -3004;
}
}
case WAD_LOCATION_SD_CARD: {
if (write == 0) {
stream->handle.fa = FAFopen(path, "r");
} else {
stream->handle.fa = FAFopen(path, "r+");
if (write != 0) {
stream->handle.fa = FAFopen(path, "w");
if (stream->handle.fa == 0) {
stream->handle.fa = FACreate(path, 0);
}
if (stream->handle.fa == 0) {
return -3004;
}
if ((offset != 0) && (WADSeekStream(stream, offset, 0) != offset)) {
if ((offset != 0) && ((u32)WADSeekStream(stream, offset, 0) != offset)) {
return -3004;
}
} else {
stream->handle.fa = FAFopen(path, "r");
}
return stream->handle.fa == 0 ? -3004 : 0;
}
@@ -2356,51 +2315,72 @@ s32 WADWriteStream(WADStream* stream, void* buffer, u32 size) {
if ((stream == 0) || (buffer == 0)) {
result = -3000;
} else if (stream->location != WAD_LOCATION_NAND) {
if (stream->location == (WADLocation)0) {
} else {
switch (stream->location) {
case (WADLocation)0:
memcpy(stream->handle.memoryBase, buffer, size);
} else if ((stream->location > WAD_LOCATION_NAND) &&
(stream->location < WAD_LOCATION_CNT_DVD)) {
break;
case WAD_LOCATION_NAND:
result = NANDWrite(&stream->handle.nand, buffer, size);
break;
case WAD_LOCATION_SD_CARD:
if (stream->handle.fa == 0) {
result = -3000;
} else {
result = FAFwrite(buffer, 1, size, stream->handle.fa);
}
} else {
break;
case WAD_LOCATION_CNT_DVD:
case WAD_LOCATION_CNT_NAND:
result = -3000;
break;
}
} else {
result = NANDWrite(&stream->handle.nand, buffer, size);
}
return result;
}
s32 WADSeekStream(WADStream* stream, u32 offset, s32 origin) {
WADFileInfoView info;
s32 result;
if ((stream == 0) || (origin < 0) || (origin > 2)) {
return -3000;
}
if (stream->location == WAD_LOCATION_SD_CARD) {
WADFileInfoView info;
if (stream->handle.fa == 0) {
return -3000;
}
result = FAFseek(stream->handle.fa, offset, origin);
if ((result == 0) && (FAFinfo(stream->handle.fa, &info.info) == 0)) {
goto sd;
}
if (stream->location >= WAD_LOCATION_SD_CARD) {
goto cnt;
}
if (stream->location >= WAD_LOCATION_NAND) {
goto nand;
}
goto err;
cnt:
if (stream->location == WAD_LOCATION_CNT_NAND) {
goto cntnand;
}
if (stream->location >= WAD_LOCATION_CNT_NAND) {
goto err;
}
return contentSeekDVD(&stream->handle.contentDvd, offset, origin);
cntnand:
return contentSeekNAND(&stream->handle.contentNand, offset, origin);
nand:
return NANDSeek(&stream->handle.nand, offset, origin);
sd:
if (stream->handle.fa == 0) {
return -3000;
}
result = FAFseek(stream->handle.fa, offset, origin);
if (result == 0) {
result = FAFinfo(stream->handle.fa, &info.info);
if (result == 0) {
result = info.fields.fileSize;
}
return result;
}
if (stream->location < WAD_LOCATION_SD_CARD) {
if (stream->location > WAD_LOCATION_DVD) {
return NANDSeek(&stream->handle.nand, offset, origin);
}
} else if (stream->location == WAD_LOCATION_CNT_NAND) {
return contentSeekNAND(&stream->handle.contentNand, offset, origin);
} else if (stream->location < WAD_LOCATION_CNT_NAND) {
return contentSeekDVD(&stream->handle.contentDvd, offset, origin);
}
return result;
err:
return -3000;
}
@@ -2731,6 +2711,8 @@ static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo*
s32 result;
u32 sectionOffset;
u32 alignedSize;
u16 wadVersion;
u32 tmdSize;
if (backupHeader->wadVersion != 1) {
return -3001;
@@ -2740,7 +2722,10 @@ static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo*
}
if (backupHeader->contentSize != 0) {
result = ES_GetDeviceId(&currentDeviceId);
if (result != 0) {
switch (result) {
case 0:
break;
default:
return result;
}
if (currentDeviceId != backupHeader->deviceId) {
@@ -2748,19 +2733,21 @@ static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo*
}
}
info->cidxMode = backupHeader->wadVersion;
if (backupHeader->wadVersion >= 1) {
wadVersion = backupHeader->wadVersion;
info->cidxMode = wadVersion;
if (wadVersion >= 1) {
info->contentIndex = &backupHeader->cidx;
}
info->fileNames = backupHeader->deviceMac;
tmdSize = backupHeader->tmdSize;
sectionOffset = (backupHeader->hdrSize + WAD_STREAM_ALIGNMENT - 1) &
~(WAD_STREAM_ALIGNMENT - 1);
if (backupHeader->tmdSize != 0) {
if (tmdSize != 0) {
if ((flags & 4) == 0) {
info->sectionSize = backupHeader->tmdSize;
info->sectionSize = tmdSize;
info->sectionOffset = sectionOffset;
alignedSize = WAD_ALIGN32(backupHeader->tmdSize);
alignedSize = WAD_ALIGN32(tmdSize);
info->titleMeta = _WADMemAlloc(allocator, alignedSize);
if (info->titleMeta == 0) {
return -3003;
@@ -3237,7 +3224,7 @@ s32 WADImportDVDExForBS(const char* path, void* buffer, u32 bufferSize) {
remainingContentSize = ((u32)contentMeta->size + 0xF) & ~0xF;
while (remainingContentSize != 0) {
u32 chunkSize = remainingContentSize;
u32 readSize;
size_t readSize;
if (chunkSize > remainingBufferSize) {
chunkSize = remainingBufferSize;
@@ -3321,35 +3308,43 @@ static s32 WAD_815C43E0(WADHashThreadArgs* args) {
static s32 _WADHash(WADStream* stream, u32 offset, u32 size, void* context, void* buffer,
u32 chunkSize, void* secondBuffer, void* threadStack, u32 threadStackSize) {
s32 result = 0;
s32 result;
void* readBuffer;
u32 completed = 0;
if ((stream == 0) || (context == 0) || (buffer == 0)) {
return -3000;
result = -3000;
goto done;
}
if (size != WAD_ALIGN32(size)) {
return -3000;
result = -3000;
goto done;
}
if (((u32)buffer & 0x1F) != 0) {
return -3007;
result = -3007;
goto done;
}
if (chunkSize != WAD_ALIGN32(chunkSize)) {
return -3000;
result = -3000;
goto done;
}
result = 0;
if ((secondBuffer == 0) || (threadStack == 0)) {
u32 completed = 0;
size_t readSize;
while (size != 0) {
void* readBuffer = buffer;
u32 readSize = size;
if (chunkSize < size) {
readSize = size;
if (size > chunkSize) {
readSize = chunkSize;
}
result = WADReadStream(stream, &readBuffer, readSize, offset + completed);
if ((u32)result != readSize) {
readBuffer = buffer;
if ((u32)WADReadStream(stream, &readBuffer, readSize,
offset + completed) != readSize) {
result = -3005;
break;
}
result = 0;
result = SHA1Input(context, readBuffer, readSize);
if (result != 0) {
break;
@@ -3361,27 +3356,25 @@ static s32 _WADHash(WADStream* stream, u32 offset, u32 size, void* context, void
WADImportTransfer transfer;
WADHashThreadArgs args;
OSThread thread;
s32 threadResult = 0;
u32 completed = 0;
u32 bufferIndex = 0;
BOOL readFailed = FALSE;
OSMutex* mutex;
size_t readSize;
WAD_815C4A2C(&transfer, buffer, secondBuffer, chunkSize);
args.context = context;
args.size = size;
args.transfer = &transfer;
if (!OSCreateThread(&thread, (void* (*)(void*))WAD_815C43E0, &args,
(u8*)threadStack + threadStackSize, threadStackSize,
OSGetThreadPriority(OSGetCurrentThread()), 0)) {
return -3009;
(u8*)threadStack + threadStackSize, threadStackSize,
OSGetThreadPriority(OSGetCurrentThread()), 0)) {
result = -3009;
goto done;
}
OSResumeThread(&thread);
while ((size != 0) && (transfer.error == 0)) {
void* readBuffer;
u32 readSize = size;
OSMutex* mutex = &transfer.mutex[bufferIndex];
if (chunkSize < size) {
readSize = size;
mutex = &transfer.mutex[bufferIndex];
if (size > chunkSize) {
readSize = chunkSize;
}
OSLockMutex(mutex);
@@ -3393,18 +3386,17 @@ static s32 _WADHash(WADStream* stream, u32 offset, u32 size, void* context, void
break;
}
readBuffer = transfer.buffers[bufferIndex];
result = WADReadStream(stream, &readBuffer, readSize, offset + completed);
result = WADReadStream(stream, &readBuffer, readSize,
offset + completed);
if ((u32)result != readSize) {
OSMutex* otherMutex = &transfer.mutex[bufferIndex ^ 1];
OSLockMutex(otherMutex);
OSCancelThread(&thread);
OSJoinThread(&thread, 0);
OSUnlockMutex(otherMutex);
OSUnlockMutex(mutex);
result = -3005;
readFailed = TRUE;
break;
goto done;
}
transfer.ready[bufferIndex] = readSize;
OSUnlockMutex(mutex);
@@ -3413,75 +3405,80 @@ static s32 _WADHash(WADStream* stream, u32 offset, u32 size, void* context, void
size -= readSize;
bufferIndex ^= 1;
}
if (!readFailed) {
if (!OSJoinThread(&thread, &threadResult)) {
result = -3009;
} else {
result = threadResult;
}
if (OSJoinThread(&thread, &result) == 0) {
return -3009;
}
}
done:
return result;
}
s32 WADVerify(WADStream* stream, MEMAllocator* allocator, u32 offset, u32 size) {
WADVerificationWorkspace workspace ALIGN64;
void* verificationBuffer = 0;
WADBackupSignature* backupSignature;
WADVerificationCertificateBundle* certificateBundle;
s32 result;
void* readBuffer = 0;
void* firstBuffer = 0;
void* buffer2 = 0;
void* buffer3 = 0;
u32 signatureOffset;
void* verificationBuffer = 0;
WADVerificationCertificateBundle* certificateBundle;
workspace.readBuffer[0] = 0;
if (size != WAD_ALIGN32(size)) {
result = -3000;
} else if (size <= 0x340) {
result = -3000;
} else {
workspace.readBuffer[0] = _WADMemAlloc(allocator, 0x8000);
if (workspace.readBuffer[0] == 0) {
firstBuffer = _WADMemAlloc(allocator, 0x8000);
if (firstBuffer == 0) {
result = -3003;
} else if (((u32)workspace.readBuffer[0] & 0x3F) != 0) {
} else if (((u32)firstBuffer & 0x3F) != 0) {
result = -3007;
} else {
u32 hashSize = size - 0x340;
size -= 0x340;
signatureOffset = offset + size;
SHA1Reset(workspace.hashContext);
result = _WADHash(stream, offset, hashSize, workspace.hashContext,
workspace.readBuffer[0], 0x8000, 0, 0, 0x1000);
result = _WADHash(stream, offset, size, workspace.hashContext,
firstBuffer, 0x8000, 0, 0, 0x1000);
if (result == 0) {
result = SHA1Result(workspace.hashContext, workspace.digest);
if (result == 0) {
result = WADReadStream(stream, workspace.readBuffer, 0x340,
offset + hashSize);
if (result == 0x340) {
readBuffer = firstBuffer;
result = WADReadStream(stream, &readBuffer, 0x340,
signatureOffset);
if (result != 0x340) {
result = -3005;
} else {
verificationBuffer = _WADMemAlloc(allocator, 0xF80);
if (verificationBuffer == 0) {
result = -3003;
} else {
backupSignature = workspace.readBuffer[0];
certificateBundle = verificationBuffer;
memcpy(certificateBundle->caProduction, ca_ppki, 0x400);
memcpy(certificateBundle->msProduction, ms_ppki, 0x240);
memcpy(certificateBundle->caDevelopment, ca_dpki, 0x400);
memcpy(certificateBundle->msDevelopment, ms_dpki, 0x240);
memcpy(certificateBundle->firstCertificate,
backupSignature->firstCertificate, 0x180);
((WADBackupSignature*)readBuffer)->firstCertificate, 0x180);
memcpy(certificateBundle->secondCertificate,
backupSignature->secondCertificate, 0x180);
result = ES_VerifySign(workspace.digest, 0x14,
workspace.readBuffer[0], verificationBuffer,
0xF80);
((WADBackupSignature*)readBuffer)->secondCertificate, 0x180);
result = ES_VerifySign(workspace.digest, 0x14, readBuffer,
verificationBuffer, 0xF80);
}
} else {
result = -3005;
}
}
}
}
}
if (workspace.readBuffer[0] != 0) {
_WADMemFree(allocator, workspace.readBuffer[0]);
if (firstBuffer != 0) {
_WADMemFree(allocator, firstBuffer);
}
if (buffer2 != 0) {
_WADMemFree(allocator, buffer2);
}
if (buffer3 != 0) {
_WADMemFree(allocator, buffer3);
}
if (verificationBuffer != 0) {
_WADMemFree(allocator, verificationBuffer);
@@ -3489,6 +3486,7 @@ s32 WADVerify(WADStream* stream, MEMAllocator* allocator, u32 offset, u32 size)
return result;
}
#pragma dont_inline on
void WAD_815C4A2C(WADImportTransfer* transfer, void* firstBuffer, void* secondBuffer,
u32 chunkSize) {
transfer->chunkSize = chunkSize;
@@ -3504,6 +3502,7 @@ void WAD_815C4A2C(WADImportTransfer* transfer, void* firstBuffer, void* secondBu
OSInitCond(&transfer->signalCond[0]);
OSInitCond(&transfer->signalCond[1]);
}
#pragma dont_inline reset
#pragma dont_inline on
static u32 _WADIsTerminated(const char* text, u32 maxLength) {