Skip to content

Commit

Permalink
Added ability to inject NAND partitions
Browse files Browse the repository at this point in the history
  • Loading branch information
d0k3 committed Jul 8, 2015
1 parent 376f5b6 commit 2d0103b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
60 changes: 58 additions & 2 deletions source/decryptor/decryptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,47 @@ u32 DecryptNandToFile(char* filename, u32 offset, u32 size, PartitionInfo* parti
return 0;
}

u32 EncryptMemToNand(u8* buffer, u32 offset, u32 size, PartitionInfo* partition)
{
DecryptBufferInfo info = {.keyslot = partition->keyslot, .setKeyY = 0, .size = size, .buffer = buffer, .mode = partition->mode};
if(GetNandCtr(info.CTR, offset) != 0)
return 1;

u32 n_sectors = size / NAND_SECTOR_SIZE;
u32 start_sector = offset / NAND_SECTOR_SIZE;
DecryptBuffer(&info);
sdmmc_nand_writesectors(start_sector, n_sectors, buffer);

return 0;
}

u32 EncryptFileToNand(char* filename, u32 offset, u32 size, PartitionInfo* partition)
{
u8* buffer = BUFFER_ADDRESS;

if (!DebugFileOpen(filename))
return 1;

if (FileGetSize() != size) {
Debug("%s has wrong size", filename);
FileClose();
return 1;
}

for (u32 i = 0; i < size; i += NAND_SECTOR_SIZE * SECTORS_PER_READ) {
u32 read_bytes = min(NAND_SECTOR_SIZE * SECTORS_PER_READ, (size - i));
ShowProgress(i, size);
if(!DebugFileRead(buffer, read_bytes, i))
return 1;
EncryptMemToNand(buffer, offset + i, read_bytes, partition);
}

ShowProgress(0, 0);
FileClose();

return 0;
}

u32 NandPadgen()
{
u32 keyslot;
Expand Down Expand Up @@ -529,7 +570,7 @@ u32 DecryptNandPartitions() {
if ( !(o3ds && (p == 6)) && !(!o3ds && (p == 5)) ) { // skip unavailable partitions (O3DS CTRNAND / N3DS CTRNAND)
Debug("Dumping & Decrypting %s, size (MB): %u", partitions[p].name, partitions[p].size / (1024 * 1024));
snprintf(filename, 256, "/%s.bin", partitions[p].name);
result += DecryptNandToFile(filename, partitions[p].offset, partitions[p].size, &partitions[p]);
result |= DecryptNandToFile(filename, partitions[p].offset, partitions[p].size, &partitions[p]);
}
}

Expand All @@ -544,7 +585,6 @@ u32 DecryptNandSystemTitles() {
char filename[256];
u32 nTitles = 0;


Debug("Seeking for 'NCCH'...");
for (u32 i = 0; i < ctrnand_size; i += NAND_SECTOR_SIZE) {
ShowProgress(i, ctrnand_size);
Expand Down Expand Up @@ -602,3 +642,19 @@ u32 RestoreNand()
return 0;
}

u32 EncryptNandPartitions() {
u32 result = 1;
char filename[256];
bool o3ds = (GetUnitPlatform() == PLATFORM_3DS);

for (u32 p = 0; p < 7; p++) {
if ( !(o3ds && (p == 6)) && !(!o3ds && (p == 5)) ) { // skip unavailable partitions (O3DS CTRNAND / N3DS CTRNAND)
Debug("Encrypting & injecting %s, size (MB): %u", partitions[p].name, partitions[p].size / (1024 * 1024));
snprintf(filename, 256, "/%s.bin", partitions[p].name);
result &= EncryptFileToNand(filename, partitions[p].offset, partitions[p].size, &partitions[p]);
}
}

return result;
}

3 changes: 3 additions & 0 deletions source/decryptor/decryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,6 @@ u32 CreatePad(PadInfo *info);
u32 GetNandCtr(u8* ctr, u32 offset);
u32 DecryptNandToMem(u8* buffer, u32 offset, u32 size, PartitionInfo* partition);
u32 DecryptNandToFile(char* filename, u32 offset, u32 size, PartitionInfo* partition);

u32 EncryptMemToNand(u8* buffer, u32 offset, u32 size, PartitionInfo* partition);
u32 EncryptFileToNand(char* filename, u32 offset, u32 size, PartitionInfo* partition);
1 change: 1 addition & 0 deletions source/decryptor/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ u32 DecryptNandPartitions(void);
u32 DecryptNandSystemTitles(void);

u32 RestoreNand(void);
u32 EncryptNandPartitions(void);
2 changes: 1 addition & 1 deletion source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ MenuInfo menu[] =
"!DANGER_ZONE!",
{
{ "NAND Restore", &RestoreNand },
{ NULL, NULL },
{ "NAND Partition Inject", &EncryptNandPartitions},
{ NULL, NULL },
{ NULL, NULL }
}
Expand Down

0 comments on commit 2d0103b

Please sign in to comment.