Skip to content

Commit 347513a

Browse files
dump/restore files decrypted
Most notable is that you can now restore atmosphere prodinfo backups
1 parent d8f0c71 commit 347513a

1 file changed

Lines changed: 43 additions & 7 deletions

File tree

source/storage/emmcfile.c

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
#include <storage/nx_sd.h>
1313
#include <string.h>
1414
#include "../fs/fsutils.h"
15+
#include "nx_emmc_bis.h"
1516

1617
// Uses default storage in nx_emmc.c
1718
// Expects the correct mmc & partition to be set
18-
ErrCode_t EmmcDumpToFile(const char *path, u32 lba_start, u32 lba_end, u8 force){
19+
ErrCode_t EmmcDumpToFile(const char *path, u32 lba_start, u32 lba_end, u8 force, u8 crypt){
1920
FIL fp;
2021
u32 curLba = lba_start;
2122
u32 totalSectors = lba_end - lba_start + 1;
@@ -41,7 +42,14 @@ ErrCode_t EmmcDumpToFile(const char *path, u32 lba_start, u32 lba_end, u8 force)
4142

4243
while (totalSectors > 0){
4344
u32 num = MIN(totalSectors, TConf.FSBuffSize / NX_EMMC_BLOCKSIZE);
44-
if (!emummc_storage_read(&emmc_storage, curLba, num, buff)){
45+
46+
int readRes = 0;
47+
if (crypt)
48+
readRes = !nx_emmc_bis_read(curLba, num, buff);
49+
else
50+
readRes = emummc_storage_read(&emmc_storage, curLba, num, buff);
51+
52+
if (!readRes){
4553
err = newErrCode(TE_ERR_EMMC_READ_FAIL);
4654
break;
4755
}
@@ -64,7 +72,7 @@ ErrCode_t EmmcDumpToFile(const char *path, u32 lba_start, u32 lba_end, u8 force)
6472
return err;
6573
}
6674

67-
ErrCode_t EmmcRestoreFromFile(const char *path, u32 lba_start, u32 lba_end, u8 force){
75+
ErrCode_t EmmcRestoreFromFile(const char *path, u32 lba_start, u32 lba_end, u8 force, u8 crypt){
6876
FIL fp;
6977
u32 curLba = lba_start;
7078
u32 totalSectorsDest = lba_end - lba_start + 1;
@@ -99,7 +107,13 @@ ErrCode_t EmmcRestoreFromFile(const char *path, u32 lba_start, u32 lba_end, u8 f
99107
break;
100108
}
101109

102-
if (!emummc_storage_write(&emmc_storage, curLba, num, buff)){
110+
int writeRes = 0;
111+
if (crypt)
112+
writeRes = !nx_emmc_bis_write(curLba, num, buff);
113+
else
114+
writeRes = emummc_storage_write(&emmc_storage, curLba, num, buff);
115+
116+
if (!writeRes){
103117
err = newErrCode(TE_ERR_EMMC_WRITE_FAIL);
104118
break;
105119
}
@@ -117,10 +131,24 @@ ErrCode_t EmmcRestoreFromFile(const char *path, u32 lba_start, u32 lba_end, u8 f
117131
return err;
118132
}
119133

134+
int isSystemPartCrypt(emmc_part_t *part){
135+
switch (part->index){
136+
case 0:
137+
case 1:
138+
case 8:
139+
case 9:
140+
case 10:
141+
return 1;
142+
default:
143+
return 0;
144+
}
145+
}
146+
120147
ErrCode_t DumpOrWriteEmmcPart(const char *path, const char *part, u8 write, u8 force){
121148
const u32 BOOT_PART_SIZE = emmc_storage.ext_csd.boot_mult << 17;
122149
u32 lba_start = 0;
123150
u32 lba_end = 0;
151+
u8 crypt = false;
124152

125153
if (!sd_mount())
126154
return newErrCode(TE_ERR_NO_SD);
@@ -143,9 +171,17 @@ ErrCode_t DumpOrWriteEmmcPart(const char *path, const char *part, u8 write, u8 f
143171
if (!system_part)
144172
return newErrCode(TE_ERR_PARTITION_NOT_FOUND);
145173

146-
lba_start = system_part->lba_start;
147-
lba_end = system_part->lba_end;
174+
if (isSystemPartCrypt(system_part)){
175+
nx_emmc_bis_init(system_part);
176+
crypt = true;
177+
lba_start = 0;
178+
lba_end = system_part->lba_end - system_part->lba_start;
179+
}
180+
else {
181+
lba_start = system_part->lba_start;
182+
lba_end = system_part->lba_end;
183+
}
148184
}
149185

150-
return ((write) ? EmmcRestoreFromFile(path, lba_start, lba_end, force) : EmmcDumpToFile(path, lba_start, lba_end, force));
186+
return ((write) ? EmmcRestoreFromFile(path, lba_start, lba_end, force, crypt) : EmmcDumpToFile(path, lba_start, lba_end, force, crypt));
151187
}

0 commit comments

Comments
 (0)